GNU libmicrohttpd  0.9.29
internal.h
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007-2015 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
27 #ifndef INTERNAL_H
28 #define INTERNAL_H
29 
30 #include "platform.h"
31 #include "microhttpd.h"
32 #include "platform_interface.h"
33 #if HTTPS_SUPPORT
34 #include <gnutls/gnutls.h>
35 #if GNUTLS_VERSION_MAJOR >= 3
36 #include <gnutls/abstract.h>
37 #endif
38 #endif
39 #if EPOLL_SUPPORT
40 #include <sys/epoll.h>
41 #endif
42 #if HAVE_NETINET_TCP_H
43 /* for TCP_FASTOPEN */
44 #include <netinet/tcp.h>
45 #endif
46 
47 
52 #define EXTRA_CHECKS MHD_NO
53 
54 #define MHD_MAX(a,b) (((a)<(b)) ? (b) : (a))
55 #define MHD_MIN(a,b) (((a)<(b)) ? (a) : (b))
56 
57 
65 #define MHD_BUF_INC_SIZE 1024
66 
67 
72 
76 extern void *mhd_panic_cls;
77 
78 /* If we have Clang or gcc >= 4.5, use __buildin_unreachable() */
79 #if defined(__clang__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
80 #define BUILTIN_NOT_REACHED __builtin_unreachable()
81 #elif defined(_MSC_FULL_VER)
82 #define BUILTIN_NOT_REACHED __assume(0)
83 #else
84 #define BUILTIN_NOT_REACHED
85 #endif
86 
87 
88 #if HAVE_MESSAGES
89 
94 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); BUILTIN_NOT_REACHED; } while (0)
95 #else
96 
101 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); BUILTIN_NOT_REACHED; } while (0)
102 #endif
103 
104 
109  {
110 
116 
122 
128 
133 
138 
143  };
144 
145 
150  {
155 
160 
165 
170  };
171 
172 
179 #define MAX_NONCE_LENGTH 129
180 
181 
187 {
188 
193  unsigned long int nc;
194 
199 
200 };
201 
202 #if HAVE_MESSAGES
203 
207 void
208 MHD_DLOG (const struct MHD_Daemon *daemon,
209  const char *format, ...);
210 #endif
211 
212 
217 {
222 
227  char *header;
228 
232  char *value;
233 
239 
240 };
241 
242 
247 {
248 
255 
260  char *data;
261 
266  void *crc_cls;
267 
273 
279 
284  MHD_mutex_ mutex;
285 
289  uint64_t total_size;
290 
295  uint64_t data_start;
296 
300  uint64_t fd_off;
301 
306  size_t data_size;
307 
312 
317  unsigned int reference_count;
318 
322  int fd;
323 
327  enum MHD_ResponseFlags flags;
328 
329 };
330 
331 
348 {
354 
359 
364 
369 
374 
379 
384 
389 
395 
401 
407 
412 
417 
423 
428 
433 
438 
443 
448 
453 
458 
459  /*
460  * SSL/TLS connection states
461  */
462 
469 
470 };
471 
475 #define DEBUG_STATES MHD_NO
476 
477 
478 #if HAVE_MESSAGES
479 #if DEBUG_STATES
480 const char *
481 MHD_state_to_string (enum MHD_CONNECTION_STATE state);
482 #endif
483 #endif
484 
493 typedef ssize_t
495  void *write_to,
496  size_t max_bytes);
497 
498 
507 typedef ssize_t
509  const void *write_to,
510  size_t max_bytes);
511 
512 
517 {
518 
519 #if EPOLL_SUPPORT
520 
523  struct MHD_Connection *nextE;
524 
528  struct MHD_Connection *prevE;
529 #endif
530 
535 
540 
549 
554 
559 
564 
569 
574 
585  struct MemoryPool *pool;
586 
594 
603 
608  char *method;
609 
614  char *url;
615 
620  char *version;
621 
628  char *read_buffer;
629 
635 
642  char *last;
643 
651  char *colon;
652 
657  struct sockaddr *addr;
658 
663  MHD_thread_handle_ pid;
664 
672 
678 
683 
688 
694 
700 
707 
713 
717  socklen_t addr_len;
718 
724 
729  unsigned int connection_timeout;
730 
737 
744 
752 
757 
762  int in_idle;
763 
764 #if EPOLL_SUPPORT
765 
768  enum MHD_EpollState epoll_state;
769 #endif
770 
775 
779  enum MHD_ConnectionEventLoopInfo event_loop_info;
780 
785  unsigned int responseCode;
786 
795 
804 
812 
818 
822  int (*read_handler) (struct MHD_Connection *connection);
823 
827  int (*write_handler) (struct MHD_Connection *connection);
828 
832  int (*idle_handler) (struct MHD_Connection *connection);
833 
838 
843 
844 #if HTTPS_SUPPORT
845 
848  gnutls_session_t tls_session;
849 
853  int protocol;
854 
858  int cipher;
859 
864  int tls_read_ready;
865 #endif
866 
871 
875  int resuming;
876 };
877 
886 typedef void *
887 (*LogCallback)(void *cls,
888  const char *uri,
889  struct MHD_Connection *con);
890 
900 typedef size_t
901 (*UnescapeCallback)(void *cls,
902  struct MHD_Connection *conn,
903  char *uri);
904 
905 
914 {
915 
920 
925 
930 
935 
940 
945 
950 
955 
956 #if EPOLL_SUPPORT
957 
960  struct MHD_Connection *eready_head;
961 
965  struct MHD_Connection *eready_tail;
966 #endif
967 
981 
987 
994 
1000 
1006 
1010  void *apc_cls;
1011 
1017 
1022 
1028 
1033 
1042 
1047 
1052 
1057 
1058 #if HAVE_MESSAGES
1059 
1063  void (*custom_error_log) (void *cls, const char *fmt, va_list va);
1064 
1068  void *custom_error_log_cls;
1069 #endif
1070 
1075 
1080 
1085 
1089  size_t pool_size;
1090 
1095 
1100 
1104  unsigned int worker_pool_size;
1105 
1109  MHD_thread_handle_ pid;
1110 
1115 
1120 
1125 
1134 
1135 #if EPOLL_SUPPORT
1136 
1139  int epoll_fd;
1140 
1145  int listen_socket_in_epoll;
1146 #endif
1147 
1156 
1161 
1162  /*
1163  * Do we need to process resuming connections?
1164  */
1166 
1170  unsigned int connections;
1171 
1175  unsigned int connection_limit;
1176 
1181  unsigned int connection_timeout;
1182 
1188 
1193 
1197  uint16_t port;
1198 
1199 #if HTTPS_SUPPORT
1200 
1203  gnutls_priority_t priority_cache;
1204 
1209  gnutls_credentials_type_t cred_type;
1210 
1214  gnutls_certificate_credentials_t x509_cred;
1215 
1219  gnutls_dh_params_t dh_params;
1220 
1221 #if GNUTLS_VERSION_MAJOR >= 3
1222 
1226  gnutls_certificate_retrieve_function2 *cert_callback;
1227 #endif
1228 
1232  const char *https_mem_key;
1233 
1237  const char *https_mem_cert;
1238 
1242  const char *https_key_password;
1243 
1247  const char *https_mem_trust;
1248 
1252  gnutls_dh_params_t https_mem_dhparams;
1253 
1257  int have_dhparams;
1258 
1265  unsigned int num_tls_read_ready;
1266 
1267 #endif
1268 
1269 #ifdef DAUTH_SUPPORT
1270 
1274  const char *digest_auth_random;
1275 
1279  struct MHD_NonceNc *nnc;
1280 
1284  MHD_mutex_ nnc_lock;
1285 
1289  size_t digest_auth_rand_size;
1290 
1294  unsigned int nonce_nc_size;
1295 
1296 #endif
1297 
1298 #ifdef TCP_FASTOPEN
1299 
1302  unsigned int fastopen_queue_size;
1303 #endif
1304 };
1305 
1306 
1307 #if EXTRA_CHECKS
1308 #define EXTRA_CHECK(a) do { if (!(a)) abort(); } while (0)
1309 #else
1310 #define EXTRA_CHECK(a)
1311 #endif
1312 
1313 
1322 #define DLL_insert(head,tail,element) do { \
1323  EXTRA_CHECK (NULL == (element)->next); \
1324  EXTRA_CHECK (NULL == (element)->prev); \
1325  (element)->next = (head); \
1326  (element)->prev = NULL; \
1327  if ((tail) == NULL) \
1328  (tail) = element; \
1329  else \
1330  (head)->prev = element; \
1331  (head) = (element); } while (0)
1332 
1333 
1343 #define DLL_remove(head,tail,element) do { \
1344  EXTRA_CHECK ( (NULL != (element)->next) || ((element) == (tail))); \
1345  EXTRA_CHECK ( (NULL != (element)->prev) || ((element) == (head))); \
1346  if ((element)->prev == NULL) \
1347  (head) = (element)->next; \
1348  else \
1349  (element)->prev->next = (element)->next; \
1350  if ((element)->next == NULL) \
1351  (tail) = (element)->prev; \
1352  else \
1353  (element)->next->prev = (element)->prev; \
1354  (element)->next = NULL; \
1355  (element)->prev = NULL; } while (0)
1356 
1357 
1358 
1367 #define XDLL_insert(head,tail,element) do { \
1368  EXTRA_CHECK (NULL == (element)->nextX); \
1369  EXTRA_CHECK (NULL == (element)->prevX); \
1370  (element)->nextX = (head); \
1371  (element)->prevX = NULL; \
1372  if (NULL == (tail)) \
1373  (tail) = element; \
1374  else \
1375  (head)->prevX = element; \
1376  (head) = (element); } while (0)
1377 
1378 
1388 #define XDLL_remove(head,tail,element) do { \
1389  EXTRA_CHECK ( (NULL != (element)->nextX) || ((element) == (tail))); \
1390  EXTRA_CHECK ( (NULL != (element)->prevX) || ((element) == (head))); \
1391  if (NULL == (element)->prevX) \
1392  (head) = (element)->nextX; \
1393  else \
1394  (element)->prevX->nextX = (element)->nextX; \
1395  if (NULL == (element)->nextX) \
1396  (tail) = (element)->prevX; \
1397  else \
1398  (element)->nextX->prevX = (element)->prevX; \
1399  (element)->nextX = NULL; \
1400  (element)->prevX = NULL; } while (0)
1401 
1402 
1411 #define EDLL_insert(head,tail,element) do { \
1412  (element)->nextE = (head); \
1413  (element)->prevE = NULL; \
1414  if ((tail) == NULL) \
1415  (tail) = element; \
1416  else \
1417  (head)->prevE = element; \
1418  (head) = (element); } while (0)
1419 
1420 
1430 #define EDLL_remove(head,tail,element) do { \
1431  if ((element)->prevE == NULL) \
1432  (head) = (element)->nextE; \
1433  else \
1434  (element)->prevE->nextE = (element)->nextE; \
1435  if ((element)->nextE == NULL) \
1436  (tail) = (element)->prevE; \
1437  else \
1438  (element)->nextE->prevE = (element)->prevE; \
1439  (element)->nextE = NULL; \
1440  (element)->prevE = NULL; } while (0)
1441 
1442 
1448 void
1449 MHD_unescape_plus (char *arg);
1450 
1451 
1463 typedef int
1465  const char *key,
1466  const char *value,
1467  enum MHD_ValueKind kind);
1468 
1469 
1484 int
1485 MHD_parse_arguments_ (struct MHD_Connection *connection,
1486  enum MHD_ValueKind kind,
1487  char *args,
1489  unsigned int *num_headers);
1490 
1491 
1492 #endif
unsigned int per_ip_connection_limit
Definition: internal.h:1187
void * unescape_callback_cls
Definition: internal.h:1056
uint64_t total_size
Definition: internal.h:289
uint64_t fd_off
Definition: internal.h:300
socklen_t addr_len
Definition: internal.h:717
ssize_t(* TransmitCallback)(struct MHD_Connection *conn, const void *write_to, size_t max_bytes)
Definition: internal.h:508
int thread_joined
Definition: internal.h:756
void * socket_context
Definition: internal.h:602
internal platform abstraction functions
uint64_t response_write_position
Definition: internal.h:706
public interface to libmicrohttpd
size_t current_chunk_size
Definition: internal.h:811
MHD_PanicCallback mhd_panic
Definition: daemon.c:149
MHD_ContentReaderFreeCallback crfc
Definition: internal.h:278
void(* MHD_ContentReaderFreeCallback)(void *cls)
Definition: microhttpd.h:1482
MHD_CONNECTION_STATE
Definition: internal.h:347
char * version
Definition: internal.h:620
MHD_socket socket_fd
Definition: internal.h:1124
struct MHD_Response * response
Definition: internal.h:573
char * colon
Definition: internal.h:651
char * write_buffer
Definition: internal.h:634
struct MHD_Connection * normal_timeout_tail
Definition: internal.h:986
size_t data_size
Definition: internal.h:306
void *(* LogCallback)(void *cls, const char *uri, struct MHD_Connection *con)
Definition: internal.h:887
unsigned long int nc
Definition: internal.h:193
int have_chunked_upload
Definition: internal.h:803
enum MHD_ValueKind kind
Definition: internal.h:238
struct MHD_HTTP_Header * first_header
Definition: internal.h:254
MHD_AccessHandlerCallback default_handler
Definition: internal.h:919
int MHD_socket
Definition: microhttpd.h:185
size_t current_chunk_offset
Definition: internal.h:817
uint64_t remaining_upload_size
Definition: internal.h:699
ssize_t(* ReceiveCallback)(struct MHD_Connection *conn, void *write_to, size_t max_bytes)
Definition: internal.h:494
unsigned int responseCode
Definition: internal.h:785
platform-specific includes for libmicrohttpd
char * value
Definition: internal.h:232
void * uri_log_callback_cls
Definition: internal.h:1046
void(* MHD_PanicCallback)(void *cls, const char *file, unsigned int line, const char *reason)
Definition: microhttpd.h:1286
struct MHD_Daemon * daemon
Definition: internal.h:558
int MHD_pipe
Definition: platform.h:205
int(* idle_handler)(struct MHD_Connection *connection)
Definition: internal.h:832
int listening_address_reuse
Definition: internal.h:1133
MHD_mutex_ per_ip_connection_mutex
Definition: internal.h:1114
struct MHD_Connection * manual_timeout_head
Definition: internal.h:993
MHD_NotifyConnectionCallback notify_connection
Definition: internal.h:1027
struct MHD_Connection * cleanup_head
Definition: internal.h:949
int(* MHD_ArgumentIterator_)(struct MHD_Connection *connection, const char *key, const char *value, enum MHD_ValueKind kind)
Definition: internal.h:1464
int client_aware
Definition: internal.h:736
struct MHD_Connection * cleanup_tail
Definition: internal.h:954
struct MHD_Connection * prev
Definition: internal.h:539
size_t data_buffer_size
Definition: internal.h:311
size_t write_buffer_send_offset
Definition: internal.h:687
struct MHD_Daemon * worker_pool
Definition: internal.h:1079
size_t read_buffer_size
Definition: internal.h:671
void * client_context
Definition: internal.h:593
struct MHD_Connection * nextX
Definition: internal.h:548
struct MHD_Connection * manual_timeout_tail
Definition: internal.h:999
size_t continue_message_write_offset
Definition: internal.h:712
MHD_socket socket_fd
Definition: internal.h:743
char * method
Definition: internal.h:608
MHD_thread_handle_ pid
Definition: internal.h:663
unsigned int connection_limit
Definition: internal.h:1175
char nonce[MAX_NONCE_LENGTH]
Definition: internal.h:198
unsigned int worker_pool_size
Definition: internal.h:1104
enum MHD_FLAG options
Definition: internal.h:1192
LogCallback uri_log_callback
Definition: internal.h:1041
ssize_t(* MHD_ContentReaderCallback)(void *cls, uint64_t pos, char *buf, size_t max)
Definition: microhttpd.h:1466
unsigned int reference_count
Definition: internal.h:317
int response_unready
Definition: internal.h:794
int shutdown
Definition: internal.h:1160
#define MAX_NONCE_LENGTH
Definition: internal.h:179
int MHD_parse_arguments_(struct MHD_Connection *connection, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, unsigned int *num_headers)
Definition: internal.c:189
uint16_t port
Definition: internal.h:1197
char * last
Definition: internal.h:642
unsigned int connections
Definition: internal.h:1170
struct MHD_Connection * normal_timeout_head
Definition: internal.h:980
MHD_ValueKind
Definition: microhttpd.h:997
char * read_buffer
Definition: internal.h:628
void * mhd_panic_cls
Definition: daemon.c:154
ReceiveCallback recv_cls
Definition: internal.h:837
size_t thread_stack_size
Definition: internal.h:1099
char * url
Definition: internal.h:614
size_t write_buffer_size
Definition: internal.h:682
uint64_t data_start
Definition: internal.h:295
UnescapeCallback unescape_callback
Definition: internal.h:1051
int(* write_handler)(struct MHD_Connection *connection)
Definition: internal.h:827
struct MHD_Connection * connections_head
Definition: internal.h:929
struct MHD_Daemon * master
Definition: internal.h:1074
size_t pool_size
Definition: internal.h:1089
char * header
Definition: internal.h:227
struct MHD_Connection * next
Definition: internal.h:534
MHD_AcceptPolicyCallback apc
Definition: internal.h:1005
time_t last_activity
Definition: internal.h:723
unsigned int connection_timeout
Definition: internal.h:729
int(* MHD_AccessHandlerCallback)(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
Definition: microhttpd.h:1345
struct MemoryPool * pool
Definition: internal.h:585
void(* MHD_RequestCompletedCallback)(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
Definition: microhttpd.h:1368
MHD_pipe wpipe[2]
Definition: internal.h:1155
size_t write_buffer_append_offset
Definition: internal.h:693
int resuming
Definition: internal.h:1165
struct MHD_HTTP_Header * next
Definition: internal.h:221
MHD_RequestCompletedCallback notify_completed
Definition: internal.h:1016
struct MHD_Connection * prevX
Definition: internal.h:553
MHD_thread_handle_ pid
Definition: internal.h:1109
void * notify_completed_cls
Definition: internal.h:1021
TransmitCallback send_cls
Definition: internal.h:842
size_t(* UnescapeCallback)(void *cls, struct MHD_Connection *conn, char *uri)
Definition: internal.h:901
void * notify_connection_cls
Definition: internal.h:1032
void * apc_cls
Definition: internal.h:1010
MHD_ContentReaderCallback crc
Definition: internal.h:272
void * crc_cls
Definition: internal.h:266
struct sockaddr * addr
Definition: internal.h:657
MHD_mutex_ mutex
Definition: internal.h:284
struct MHD_Connection * suspended_connections_tail
Definition: internal.h:944
MHD_ConnectionEventLoopInfo
Definition: internal.h:149
MHD_EpollState
Definition: internal.h:108
void * per_ip_connection_count
Definition: internal.h:1084
char * data
Definition: internal.h:260
size_t read_buffer_offset
Definition: internal.h:677
void * default_handler_cls
Definition: internal.h:924
unsigned int connection_timeout
Definition: internal.h:1181
MHD_ResponseFlags
Definition: microhttpd.h:1952
MHD_mutex_ cleanup_connection_mutex
Definition: internal.h:1119
int(* MHD_AcceptPolicyCallback)(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Definition: microhttpd.h:1300
void(* MHD_NotifyConnectionCallback)(void *cls, struct MHD_Connection *connection, void **socket_context, enum MHD_ConnectionNotificationCode toe)
Definition: microhttpd.h:1393
struct MHD_HTTP_Header * headers_received
Definition: internal.h:563
struct MHD_Connection * connections_tail
Definition: internal.h:934
MHD_FLAG
Flags for the struct MHD_Daemon.
Definition: microhttpd.h:513
struct MHD_HTTP_Header * headers_received_tail
Definition: internal.h:568
size_t pool_increment
Definition: internal.h:1094
void MHD_unescape_plus(char *arg)
Definition: internal.c:113
struct MHD_Connection * suspended_connections_head
Definition: internal.h:939