diff options
Diffstat (limited to 'src/lib/internal.h')
-rw-r--r-- | src/lib/internal.h | 215 |
1 files changed, 212 insertions, 3 deletions
diff --git a/src/lib/internal.h b/src/lib/internal.h index 5b147df8..17e5c634 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h | |||
@@ -826,6 +826,202 @@ struct MHD_Connection | |||
826 | }; | 826 | }; |
827 | 827 | ||
828 | 828 | ||
829 | #ifdef UPGRADE_SUPPORT | ||
830 | /** | ||
831 | * Buffer we use for upgrade response handling in the unlikely | ||
832 | * case where the memory pool was so small it had no buffer | ||
833 | * capacity left. Note that we don't expect to _ever_ use this | ||
834 | * buffer, so it's mostly wasted memory (except that it allows | ||
835 | * us to handle a tricky error condition nicely). So no need to | ||
836 | * make this one big. Applications that want to perform well | ||
837 | * should just pick an adequate size for the memory pools. | ||
838 | */ | ||
839 | #define RESERVE_EBUF_SIZE 8 | ||
840 | |||
841 | /** | ||
842 | * Context we pass to epoll() for each of the two sockets | ||
843 | * of a `struct MHD_UpgradeResponseHandle`. We need to do | ||
844 | * this so we can distinguish the two sockets when epoll() | ||
845 | * gives us event notifications. | ||
846 | */ | ||
847 | struct UpgradeEpollHandle | ||
848 | { | ||
849 | /** | ||
850 | * Reference to the overall response handle this struct is | ||
851 | * included within. | ||
852 | */ | ||
853 | struct MHD_UpgradeResponseHandle *urh; | ||
854 | |||
855 | /** | ||
856 | * The socket this event is kind-of about. Note that this is NOT | ||
857 | * necessarily the socket we are polling on, as for when we read | ||
858 | * from TLS, we epoll() on the connection's socket | ||
859 | * (`urh->connection->socket_fd`), while this then the application's | ||
860 | * socket (where the application will read from). Nevertheless, for | ||
861 | * the application to read, we need to first read from TLS, hence | ||
862 | * the two are related. | ||
863 | * | ||
864 | * Similarly, for writing to TLS, this epoll() will be on the | ||
865 | * connection's `socket_fd`, and this will merely be the FD which | ||
866 | * the applicatio would write to. Hence this struct must always be | ||
867 | * interpreted based on which field in `struct | ||
868 | * MHD_UpgradeResponseHandle` it is (`app` or `mhd`). | ||
869 | */ | ||
870 | MHD_socket socket; | ||
871 | |||
872 | /** | ||
873 | * IO-state of the @e socket (or the connection's `socket_fd`). | ||
874 | */ | ||
875 | enum MHD_EpollState celi; | ||
876 | |||
877 | }; | ||
878 | |||
879 | |||
880 | /** | ||
881 | * Handle given to the application to manage special | ||
882 | * actions relating to MHD responses that "upgrade" | ||
883 | * the HTTP protocol (i.e. to WebSockets). | ||
884 | */ | ||
885 | struct MHD_UpgradeResponseHandle | ||
886 | { | ||
887 | /** | ||
888 | * The connection for which this is an upgrade handle. Note that | ||
889 | * because a response may be shared over many connections, this may | ||
890 | * not be the only upgrade handle for the response of this connection. | ||
891 | */ | ||
892 | struct MHD_Connection *connection; | ||
893 | |||
894 | #ifdef HTTPS_SUPPORT | ||
895 | /** | ||
896 | * Kept in a DLL per daemon. | ||
897 | */ | ||
898 | struct MHD_UpgradeResponseHandle *next; | ||
899 | |||
900 | /** | ||
901 | * Kept in a DLL per daemon. | ||
902 | */ | ||
903 | struct MHD_UpgradeResponseHandle *prev; | ||
904 | |||
905 | #ifdef EPOLL_SUPPORT | ||
906 | /** | ||
907 | * Next pointer for the EDLL listing urhs that are epoll-ready. | ||
908 | */ | ||
909 | struct MHD_UpgradeResponseHandle *nextE; | ||
910 | |||
911 | /** | ||
912 | * Previous pointer for the EDLL listing urhs that are epoll-ready. | ||
913 | */ | ||
914 | struct MHD_UpgradeResponseHandle *prevE; | ||
915 | |||
916 | /** | ||
917 | * Specifies whether urh already in EDLL list of ready connections. | ||
918 | */ | ||
919 | bool in_eready_list; | ||
920 | #endif | ||
921 | |||
922 | /** | ||
923 | * The buffer for receiving data from TLS to | ||
924 | * be passed to the application. Contains @e in_buffer_size | ||
925 | * bytes (unless @e in_buffer_size is zero). Do not free! | ||
926 | */ | ||
927 | char *in_buffer; | ||
928 | |||
929 | /** | ||
930 | * The buffer for receiving data from the application to | ||
931 | * be passed to TLS. Contains @e out_buffer_size | ||
932 | * bytes (unless @e out_buffer_size is zero). Do not free! | ||
933 | */ | ||
934 | char *out_buffer; | ||
935 | |||
936 | /** | ||
937 | * Size of the @e in_buffer. | ||
938 | * Set to 0 if the TLS connection went down for reading or socketpair | ||
939 | * went down for writing. | ||
940 | */ | ||
941 | size_t in_buffer_size; | ||
942 | |||
943 | /** | ||
944 | * Size of the @e out_buffer. | ||
945 | * Set to 0 if the TLS connection went down for writing or socketpair | ||
946 | * went down for reading. | ||
947 | */ | ||
948 | size_t out_buffer_size; | ||
949 | |||
950 | /** | ||
951 | * Number of bytes actually in use in the @e in_buffer. Can be larger | ||
952 | * than @e in_buffer_size if and only if @a in_buffer_size is zero and | ||
953 | * we still have bytes that can be forwarded. | ||
954 | * Reset to zero if all data was forwarded to socketpair or | ||
955 | * if socketpair went down for writing. | ||
956 | */ | ||
957 | size_t in_buffer_used; | ||
958 | |||
959 | /** | ||
960 | * Number of bytes actually in use in the @e out_buffer. Can be larger | ||
961 | * than @e out_buffer_size if and only if @a out_buffer_size is zero and | ||
962 | * we still have bytes that can be forwarded. | ||
963 | * Reset to zero if all data was forwarded to TLS connection or | ||
964 | * if TLS connection went down for writing. | ||
965 | */ | ||
966 | size_t out_buffer_used; | ||
967 | |||
968 | /** | ||
969 | * The socket we gave to the application (r/w). | ||
970 | */ | ||
971 | struct UpgradeEpollHandle app; | ||
972 | |||
973 | /** | ||
974 | * If @a app_sock was a socketpair, our end of it, otherwise | ||
975 | * #MHD_INVALID_SOCKET; (r/w). | ||
976 | */ | ||
977 | struct UpgradeEpollHandle mhd; | ||
978 | |||
979 | /** | ||
980 | * Emergency IO buffer we use in case the memory pool has literally | ||
981 | * nothing left. | ||
982 | */ | ||
983 | char e_buf[RESERVE_EBUF_SIZE]; | ||
984 | |||
985 | #endif /* HTTPS_SUPPORT */ | ||
986 | |||
987 | /** | ||
988 | * Set to true after the application finished with the socket | ||
989 | * by #MHD_UPGRADE_ACTION_CLOSE. | ||
990 | * | ||
991 | * When BOTH @e was_closed (changed by command from application) | ||
992 | * AND @e clean_ready (changed internally by MHD) are set to | ||
993 | * #MHD_YES, function #MHD_resume_connection() will move this | ||
994 | * connection to cleanup list. | ||
995 | * @remark This flag could be changed from any thread. | ||
996 | */ | ||
997 | volatile bool was_closed; | ||
998 | |||
999 | /** | ||
1000 | * Set to true if connection is ready for cleanup. | ||
1001 | * | ||
1002 | * In TLS mode functions #MHD_connection_finish_forward_() must | ||
1003 | * be called before setting this flag to true. | ||
1004 | * | ||
1005 | * In thread-per-connection mode, true in this flag means | ||
1006 | * that connection's thread exited or about to exit and will | ||
1007 | * not use MHD_Connection::urh data anymore. | ||
1008 | * | ||
1009 | * In any mode true in this flag also means that | ||
1010 | * MHD_Connection::urh data will not be used for socketpair | ||
1011 | * forwarding and forwarding itself is finished. | ||
1012 | * | ||
1013 | * When BOTH @e was_closed (changed by command from application) | ||
1014 | * AND @e clean_ready (changed internally by MHD) are set to | ||
1015 | * true, function #MHD_resume_connection() will move this | ||
1016 | * connection to cleanup list. | ||
1017 | * @remark This flag could be changed from thread that process | ||
1018 | * connection's recv(), send() and response. | ||
1019 | */ | ||
1020 | bool clean_ready; | ||
1021 | }; | ||
1022 | #endif /* UPGRADE_SUPPORT */ | ||
1023 | |||
1024 | |||
829 | /** | 1025 | /** |
830 | * State kept for each MHD daemon. All connections are kept in two | 1026 | * State kept for each MHD daemon. All connections are kept in two |
831 | * doubly-linked lists. The first one reflects the state of the | 1027 | * doubly-linked lists. The first one reflects the state of the |
@@ -910,6 +1106,22 @@ struct MHD_Daemon | |||
910 | 1106 | ||
911 | 1107 | ||
912 | #if HTTPS_SUPPORT | 1108 | #if HTTPS_SUPPORT |
1109 | #ifdef UPGRADE_SUPPORT | ||
1110 | /** | ||
1111 | * Head of DLL of upgrade response handles we are processing. | ||
1112 | * Used for upgraded TLS connections when thread-per-connection | ||
1113 | * is not used. | ||
1114 | */ | ||
1115 | struct MHD_UpgradeResponseHandle *urh_head; | ||
1116 | |||
1117 | /** | ||
1118 | * Tail of DLL of upgrade response handles we are processing. | ||
1119 | * Used for upgraded TLS connections when thread-per-connection | ||
1120 | * is not used. | ||
1121 | */ | ||
1122 | struct MHD_UpgradeResponseHandle *urh_tail; | ||
1123 | #endif /* UPGRADE_SUPPORT */ | ||
1124 | |||
913 | /** | 1125 | /** |
914 | * Which TLS backend should be used. NULL for no TLS. | 1126 | * Which TLS backend should be used. NULL for no TLS. |
915 | * This is merely the handle to the dlsym() object, not | 1127 | * This is merely the handle to the dlsym() object, not |
@@ -1012,13 +1224,10 @@ struct MHD_Daemon | |||
1012 | */ | 1224 | */ |
1013 | struct MHD_Connection *eready_tail; | 1225 | struct MHD_Connection *eready_tail; |
1014 | 1226 | ||
1015 | #ifdef EPOLL_SUPPORT | ||
1016 | /** | 1227 | /** |
1017 | * Pointer to marker used to indicate ITC slot in epoll sets. | 1228 | * Pointer to marker used to indicate ITC slot in epoll sets. |
1018 | */ | 1229 | */ |
1019 | const char *epoll_itc_marker; | 1230 | const char *epoll_itc_marker; |
1020 | #endif | ||
1021 | |||
1022 | #ifdef UPGRADE_SUPPORT | 1231 | #ifdef UPGRADE_SUPPORT |
1023 | /** | 1232 | /** |
1024 | * Head of EDLL of upgraded connections ready for processing (in epoll mode). | 1233 | * Head of EDLL of upgraded connections ready for processing (in epoll mode). |