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 #if HTTPS_SUPPORT
33 #include <gnutls/gnutls.h>
34 #if GNUTLS_VERSION_MAJOR >= 3
35 #include <gnutls/abstract.h>
36 #endif
37 #endif
38 #include "mhd_options.h"
39 
40 
41 #ifdef MHD_PANIC
42 /* Override any defined MHD_PANIC macro with proper one */
43 #undef MHD_PANIC
44 #endif /* MHD_PANIC */
45 
46 #ifdef HAVE_MESSAGES
47 
52 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); BUILTIN_NOT_REACHED; } while (0)
53 #else
54 
59 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); BUILTIN_NOT_REACHED; } while (0)
60 #endif
61 
62 #include "mhd_threads.h"
63 #include "mhd_locks.h"
64 #include "mhd_sockets.h"
65 #include "mhd_itc_types.h"
66 
67 
72 #define MHD_fd_close_chk_(fd) do { \
73  if (0 == close ((fd)) && (EBADF == errno)) \
74  MHD_PANIC(_("Failed to close FD.\n")); \
75  } while(0)
76 
81 #define EXTRA_CHECKS MHD_NO
82 
83 #define MHD_MAX(a,b) (((a)<(b)) ? (b) : (a))
84 #define MHD_MIN(a,b) (((a)<(b)) ? (a) : (b))
85 
86 
94 #define MHD_BUF_INC_SIZE 1024
95 
96 
101 
105 extern void *mhd_panic_cls;
106 
107 /* If we have Clang or gcc >= 4.5, use __buildin_unreachable() */
108 #if defined(__clang__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
109 #define BUILTIN_NOT_REACHED __builtin_unreachable()
110 #elif defined(_MSC_FULL_VER)
111 #define BUILTIN_NOT_REACHED __assume(0)
112 #else
113 #define BUILTIN_NOT_REACHED
114 #endif
115 
116 
121 {
122 
128 
134 
140 
145 
150 
155 };
156 
157 
162 {
167 
172 
177 
182 };
183 
184 
191 #define MAX_NONCE_LENGTH 129
192 
193 
199 {
200 
205  uint64_t nc;
206 
211  uint64_t nmask;
212 
217 
218 };
219 
220 #ifdef HAVE_MESSAGES
221 
225 void
226 MHD_DLOG (const struct MHD_Daemon *daemon,
227  const char *format,
228  ...);
229 #endif
230 
231 
236 {
241 
245  char *header;
246 
250  char *value;
251 
257 
258 };
259 
260 
265 {
266 
273 
278  char *data;
279 
284  void *crc_cls;
285 
291 
297 
304 
309 
314  MHD_mutex_ mutex;
315 
319  uint64_t total_size;
320 
325  uint64_t data_start;
326 
330  uint64_t fd_off;
331 
336  size_t data_size;
337 
342 
347  unsigned int reference_count;
348 
352  int fd;
353 
357  enum MHD_ResponseFlags flags;
358 
359 };
360 
361 
378 {
384 
389 
394 
399 
404 
409 
414 
419 
425 
431 
437 
442 
447 
453 
458 
463 
468 
473 
478 
483 
488 
489  /*
490  * SSL/TLS connection states
491  */
492 
499 
505 
512 
513 };
514 
518 #define DEBUG_STATES MHD_NO
519 
520 
521 #ifdef HAVE_MESSAGES
522 #if DEBUG_STATES
523 const char *
524 MHD_state_to_string (enum MHD_CONNECTION_STATE state);
525 #endif
526 #endif
527 
536 typedef ssize_t
538  void *write_to,
539  size_t max_bytes);
540 
541 
550 typedef ssize_t
552  const void *write_to,
553  size_t max_bytes);
554 
555 
560 {
561 
562 #ifdef EPOLL_SUPPORT
563 
566  struct MHD_Connection *nextE;
567 
571  struct MHD_Connection *prevE;
572 #endif
573 
578 
583 
592 
597 
602 
607 
612 
617 
626  struct MemoryPool *pool;
627 
635 
644 
648  char *method;
649 
654  const char *url;
655 
660  char *version;
661 
667  char *read_buffer;
668 
674 
681  char *last;
682 
690  char *colon;
691 
696  struct sockaddr *addr;
697 
702  MHD_thread_handle_ pid;
703 
711 
717 
722 
727 
733 
739 
746 
752 
756  socklen_t addr_len;
757 
763 
768  unsigned int connection_timeout;
769 
776 
783 
791 
796 
801  int in_idle;
802 
803 #ifdef EPOLL_SUPPORT
804 
807  enum MHD_EpollState epoll_state;
808 #endif
809 
814 
818  enum MHD_ConnectionEventLoopInfo event_loop_info;
819 
824  unsigned int responseCode;
825 
834 
843 
851 
857 
861  int (*read_handler) (struct MHD_Connection *connection);
862 
866  int (*write_handler) (struct MHD_Connection *connection);
867 
871  int (*idle_handler) (struct MHD_Connection *connection);
872 
877 
882 
891 
892 #if HTTPS_SUPPORT
893 
897  gnutls_session_t tls_session;
898 
902  int protocol;
903 
907  int cipher;
908 
913  int tls_read_ready;
914 #endif
915 
920 
924  int resuming;
925 };
926 
927 
937 #define RESERVE_EBUF_SIZE 8
938 
946 {
952 
969 
973  enum MHD_EpollState celi;
974 
975 };
976 
977 
984 {
991 
992 #if HTTPS_SUPPORT
993 
996  struct MHD_UpgradeResponseHandle *next;
997 
1001  struct MHD_UpgradeResponseHandle *prev;
1002 
1008  char *in_buffer;
1009 
1015  char *out_buffer;
1016 
1020  size_t in_buffer_size;
1021 
1025  size_t out_buffer_size;
1026 
1030  size_t in_buffer_off;
1031 
1035  size_t out_buffer_off;
1036 
1040  struct UpgradeEpollHandle app;
1041 
1046  struct UpgradeEpollHandle mhd;
1047 
1052  char e_buf[RESERVE_EBUF_SIZE];
1053 
1054 #endif /* HTTPS_SUPPORT */
1055 
1061 };
1062 
1063 
1072 typedef void *
1073 (*LogCallback)(void *cls,
1074  const char *uri,
1075  struct MHD_Connection *con);
1076 
1086 typedef size_t
1087 (*UnescapeCallback)(void *cls,
1088  struct MHD_Connection *conn,
1089  char *uri);
1090 
1091 
1100 {
1101 
1106 
1111 
1116 
1121 
1126 
1131 
1136 
1141 
1142 #ifdef EPOLL_SUPPORT
1143 
1146  struct MHD_Connection *eready_head;
1147 
1151  struct MHD_Connection *eready_tail;
1152 #endif
1153 
1169 
1176 
1184 
1191 
1197 
1201  void *apc_cls;
1202 
1208 
1213 
1219 
1224 
1233 
1238 
1243 
1248 
1249 #ifdef HAVE_MESSAGES
1250 
1254  void (*custom_error_log) (void *cls, const char *fmt, va_list va);
1255 
1259  void *custom_error_log_cls;
1260 #endif
1261 
1266 
1271 
1276 
1280  size_t pool_size;
1281 
1286 
1291 
1295  unsigned int worker_pool_size;
1296 
1300  MHD_thread_handle_ pid;
1301 
1306 
1311 
1316 
1325 
1326 #ifdef EPOLL_SUPPORT
1327 
1330  int epoll_fd;
1331 
1336  int listen_socket_in_epoll;
1337 
1338 #if HTTPS_SUPPORT
1339 
1343  int epoll_upgrade_fd;
1344 
1349  int upgrade_fd_in_epoll;
1350 #endif
1351 
1352 #endif
1353 
1357  struct MHD_itc_ itc;
1358 
1363 
1372 
1373  /*
1374  * Do we need to process resuming connections?
1375  */
1377 
1381  unsigned int connections;
1382 
1386  unsigned int connection_limit;
1387 
1392  unsigned int connection_timeout;
1393 
1399 
1403  enum MHD_FLAG options;
1404 
1408  uint16_t port;
1409 
1410 #if HTTPS_SUPPORT
1411 
1414  struct MHD_UpgradeResponseHandle *urh_head;
1415 
1419  struct MHD_UpgradeResponseHandle *urh_tail;
1420 
1424  gnutls_priority_t priority_cache;
1425 
1430  gnutls_credentials_type_t cred_type;
1431 
1435  gnutls_certificate_credentials_t x509_cred;
1436 
1440  gnutls_dh_params_t dh_params;
1441 
1442 #if GNUTLS_VERSION_MAJOR >= 3
1443 
1447  gnutls_certificate_retrieve_function2 *cert_callback;
1448 #endif
1449 
1453  const char *https_mem_key;
1454 
1458  const char *https_mem_cert;
1459 
1463  const char *https_key_password;
1464 
1468  const char *https_mem_trust;
1469 
1473  gnutls_dh_params_t https_mem_dhparams;
1474 
1478  int have_dhparams;
1479 
1486  unsigned int num_tls_read_ready;
1487 
1488 #endif
1489 
1490 #ifdef DAUTH_SUPPORT
1491 
1495  const char *digest_auth_random;
1496 
1500  struct MHD_NonceNc *nnc;
1501 
1505  MHD_mutex_ nnc_lock;
1506 
1510  size_t digest_auth_rand_size;
1511 
1515  unsigned int nonce_nc_size;
1516 
1517 #endif
1518 
1519 #ifdef TCP_FASTOPEN
1520 
1523  unsigned int fastopen_queue_size;
1524 #endif
1525 
1529  unsigned int listen_backlog_size;
1530 };
1531 
1532 
1533 #if EXTRA_CHECKS
1534 #define EXTRA_CHECK(a) do { if (!(a)) abort(); } while (0)
1535 #else
1536 #define EXTRA_CHECK(a)
1537 #endif
1538 
1539 
1548 #define DLL_insert(head,tail,element) do { \
1549  EXTRA_CHECK (NULL == (element)->next); \
1550  EXTRA_CHECK (NULL == (element)->prev); \
1551  (element)->next = (head); \
1552  (element)->prev = NULL; \
1553  if ((tail) == NULL) \
1554  (tail) = element; \
1555  else \
1556  (head)->prev = element; \
1557  (head) = (element); } while (0)
1558 
1559 
1569 #define DLL_remove(head,tail,element) do { \
1570  EXTRA_CHECK ( (NULL != (element)->next) || ((element) == (tail))); \
1571  EXTRA_CHECK ( (NULL != (element)->prev) || ((element) == (head))); \
1572  if ((element)->prev == NULL) \
1573  (head) = (element)->next; \
1574  else \
1575  (element)->prev->next = (element)->next; \
1576  if ((element)->next == NULL) \
1577  (tail) = (element)->prev; \
1578  else \
1579  (element)->next->prev = (element)->prev; \
1580  (element)->next = NULL; \
1581  (element)->prev = NULL; } while (0)
1582 
1583 
1584 
1593 #define XDLL_insert(head,tail,element) do { \
1594  EXTRA_CHECK (NULL == (element)->nextX); \
1595  EXTRA_CHECK (NULL == (element)->prevX); \
1596  (element)->nextX = (head); \
1597  (element)->prevX = NULL; \
1598  if (NULL == (tail)) \
1599  (tail) = element; \
1600  else \
1601  (head)->prevX = element; \
1602  (head) = (element); } while (0)
1603 
1604 
1614 #define XDLL_remove(head,tail,element) do { \
1615  EXTRA_CHECK ( (NULL != (element)->nextX) || ((element) == (tail))); \
1616  EXTRA_CHECK ( (NULL != (element)->prevX) || ((element) == (head))); \
1617  if (NULL == (element)->prevX) \
1618  (head) = (element)->nextX; \
1619  else \
1620  (element)->prevX->nextX = (element)->nextX; \
1621  if (NULL == (element)->nextX) \
1622  (tail) = (element)->prevX; \
1623  else \
1624  (element)->nextX->prevX = (element)->prevX; \
1625  (element)->nextX = NULL; \
1626  (element)->prevX = NULL; } while (0)
1627 
1628 
1637 #define EDLL_insert(head,tail,element) do { \
1638  (element)->nextE = (head); \
1639  (element)->prevE = NULL; \
1640  if ((tail) == NULL) \
1641  (tail) = element; \
1642  else \
1643  (head)->prevE = element; \
1644  (head) = (element); } while (0)
1645 
1646 
1656 #define EDLL_remove(head,tail,element) do { \
1657  if ((element)->prevE == NULL) \
1658  (head) = (element)->nextE; \
1659  else \
1660  (element)->prevE->nextE = (element)->nextE; \
1661  if ((element)->nextE == NULL) \
1662  (tail) = (element)->prevE; \
1663  else \
1664  (element)->nextE->prevE = (element)->prevE; \
1665  (element)->nextE = NULL; \
1666  (element)->prevE = NULL; } while (0)
1667 
1668 
1674 void
1675 MHD_unescape_plus (char *arg);
1676 
1677 
1689 typedef int
1691  const char *key,
1692  const char *value,
1693  enum MHD_ValueKind kind);
1694 
1695 
1710 int
1711 MHD_parse_arguments_ (struct MHD_Connection *connection,
1712  enum MHD_ValueKind kind,
1713  char *args,
1715  unsigned int *num_headers);
1716 
1717 
1725 void
1727 
1728 #endif
#define MAX_NONCE_LENGTH
Definition: internal.h:191
unsigned int per_ip_connection_limit
Definition: internal.h:1398
void * unescape_callback_cls
Definition: internal.h:1247
uint64_t total_size
Definition: internal.h:319
additional automatic macros for MHD_config.h
uint64_t fd_off
Definition: internal.h:330
socklen_t addr_len
Definition: internal.h:756
int thread_joined
Definition: internal.h:795
void * socket_context
Definition: internal.h:643
size_t(* UnescapeCallback)(void *cls, struct MHD_Connection *conn, char *uri)
Definition: internal.h:1087
struct MHD_UpgradeResponseHandle * urh
Definition: internal.h:890
uint64_t response_write_position
Definition: internal.h:745
public interface to libmicrohttpd
ssize_t(* ReceiveCallback)(struct MHD_Connection *conn, void *write_to, size_t max_bytes)
Definition: internal.h:537
size_t current_chunk_size
Definition: internal.h:850
void * mhd_panic_cls
Definition: daemon.c:124
MHD_ContentReaderFreeCallback crfc
Definition: internal.h:296
void(* MHD_ContentReaderFreeCallback)(void *cls)
Definition: microhttpd.h:1549
char * version
Definition: internal.h:660
MHD_socket socket_fd
Definition: internal.h:1315
struct MHD_Response * response
Definition: internal.h:616
char * colon
Definition: internal.h:690
char * write_buffer
Definition: internal.h:673
struct MHD_Connection * normal_timeout_tail
Definition: internal.h:1175
ssize_t(* TransmitCallback)(struct MHD_Connection *conn, const void *write_to, size_t max_bytes)
Definition: internal.h:551
#define RESERVE_EBUF_SIZE
Definition: internal.h:937
size_t data_size
Definition: internal.h:336
int have_chunked_upload
Definition: internal.h:842
enum MHD_ValueKind kind
Definition: internal.h:256
struct MHD_HTTP_Header * first_header
Definition: internal.h:272
MHD_AccessHandlerCallback default_handler
Definition: internal.h:1105
int MHD_socket
Definition: microhttpd.h:181
size_t current_chunk_offset
Definition: internal.h:856
uint64_t remaining_upload_size
Definition: internal.h:738
unsigned int responseCode
Definition: internal.h:824
platform-specific includes for libmicrohttpd
struct MHD_UpgradeResponseHandle * urh
Definition: internal.h:951
MHD_CONNECTION_STATE
Definition: internal.h:377
char * value
Definition: internal.h:250
void * uri_log_callback_cls
Definition: internal.h:1237
void(* MHD_PanicCallback)(void *cls, const char *file, unsigned int line, const char *reason)
Definition: microhttpd.h:1353
struct MHD_Daemon * daemon
Definition: internal.h:601
int(* idle_handler)(struct MHD_Connection *connection)
Definition: internal.h:871
int listening_address_reuse
Definition: internal.h:1324
MHD_mutex_ per_ip_connection_mutex
Definition: internal.h:1305
struct MHD_Connection * manual_timeout_head
Definition: internal.h:1183
MHD_NotifyConnectionCallback notify_connection
Definition: internal.h:1218
struct MHD_Connection * cleanup_head
Definition: internal.h:1135
int client_aware
Definition: internal.h:775
MHD_PanicCallback mhd_panic
Definition: daemon.c:119
struct MHD_Connection * cleanup_tail
Definition: internal.h:1140
struct MHD_Connection * prev
Definition: internal.h:582
uint64_t nmask
Definition: internal.h:211
size_t data_buffer_size
Definition: internal.h:341
size_t write_buffer_send_offset
Definition: internal.h:726
struct MHD_Daemon * worker_pool
Definition: internal.h:1270
size_t read_buffer_size
Definition: internal.h:710
void * client_context
Definition: internal.h:634
struct MHD_Connection * nextX
Definition: internal.h:591
struct MHD_Connection * manual_timeout_tail
Definition: internal.h:1190
const char * url
Definition: internal.h:654
void MHD_cleanup_upgraded_connection_(struct MHD_Connection *connection)
Definition: daemon.c:920
size_t continue_message_write_offset
Definition: internal.h:751
MHD_UpgradeHandler upgrade_handler
Definition: internal.h:303
MHD_socket socket_fd
Definition: internal.h:782
char * method
Definition: internal.h:648
MHD_thread_handle_ pid
Definition: internal.h:702
unsigned int connection_limit
Definition: internal.h:1386
char nonce[MAX_NONCE_LENGTH]
Definition: internal.h:216
unsigned int worker_pool_size
Definition: internal.h:1295
LogCallback uri_log_callback
Definition: internal.h:1232
ssize_t(* MHD_ContentReaderCallback)(void *cls, uint64_t pos, char *buf, size_t max)
Definition: microhttpd.h:1533
unsigned int reference_count
Definition: internal.h:347
int response_unready
Definition: internal.h:833
struct MHD_Connection * connection
Definition: internal.h:990
int shutdown
Definition: internal.h:1362
MHD_EpollState
Definition: internal.h:120
uint16_t port
Definition: internal.h:1408
char * last
Definition: internal.h:681
unsigned int connections
Definition: internal.h:1381
struct MHD_Connection * normal_timeout_head
Definition: internal.h:1168
MHD_ValueKind
Definition: microhttpd.h:1053
char * read_buffer
Definition: internal.h:667
ReceiveCallback recv_cls
Definition: internal.h:876
size_t thread_stack_size
Definition: internal.h:1290
size_t write_buffer_size
Definition: internal.h:721
Header for platform-independent threads abstraction.
uint64_t data_start
Definition: internal.h:325
MHD_socket socket
Definition: internal.h:968
UnescapeCallback unescape_callback
Definition: internal.h:1242
int(* write_handler)(struct MHD_Connection *connection)
Definition: internal.h:866
unsigned int listen_backlog_size
Definition: internal.h:1529
struct MHD_Connection * connections_head
Definition: internal.h:1115
struct MHD_Daemon * master
Definition: internal.h:1265
size_t pool_size
Definition: internal.h:1280
char * header
Definition: internal.h:245
struct MHD_Connection * next
Definition: internal.h:577
MHD_AcceptPolicyCallback apc
Definition: internal.h:1196
time_t last_activity
Definition: internal.h:762
void *(* LogCallback)(void *cls, const char *uri, struct MHD_Connection *con)
Definition: internal.h:1073
unsigned int connection_timeout
Definition: internal.h:768
int(* MHD_ArgumentIterator_)(struct MHD_Connection *connection, const char *key, const char *value, enum MHD_ValueKind kind)
Definition: internal.h:1690
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:1412
int at_limit
Definition: internal.h:1371
struct MemoryPool * pool
Definition: internal.h:626
uint64_t nc
Definition: internal.h:205
void(* MHD_RequestCompletedCallback)(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
Definition: microhttpd.h:1435
size_t write_buffer_append_offset
Definition: internal.h:732
int resuming
Definition: internal.h:1376
struct MHD_HTTP_Header * next
Definition: internal.h:240
MHD_RequestCompletedCallback notify_completed
Definition: internal.h:1207
struct MHD_Connection * prevX
Definition: internal.h:596
void(* MHD_UpgradeHandler)(void *cls, struct MHD_Connection *connection, void *con_cls, const char *extra_in, size_t extra_in_size, MHD_socket sock, struct MHD_UpgradeResponseHandle *urh)
Definition: microhttpd.h:2342
MHD_thread_handle_ pid
Definition: internal.h:1300
void * notify_completed_cls
Definition: internal.h:1212
TransmitCallback send_cls
Definition: internal.h:881
void * notify_connection_cls
Definition: internal.h:1223
void * apc_cls
Definition: internal.h:1201
MHD_ContentReaderCallback crc
Definition: internal.h:290
Types for platform-independent inter-thread communication.
void * crc_cls
Definition: internal.h:284
void MHD_unescape_plus(char *arg)
Definition: internal.c:119
struct sockaddr * addr
Definition: internal.h:696
MHD_mutex_ mutex
Definition: internal.h:314
struct MHD_Connection * suspended_connections_tail
Definition: internal.h:1130
int MHD_parse_arguments_(struct MHD_Connection *connection, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, unsigned int *num_headers)
Definition: internal.c:186
void * per_ip_connection_count
Definition: internal.h:1275
char * data
Definition: internal.h:278
size_t read_buffer_offset
Definition: internal.h:716
void * default_handler_cls
Definition: internal.h:1110
MHD_ConnectionEventLoopInfo
Definition: internal.h:161
unsigned int connection_timeout
Definition: internal.h:1392
MHD_ResponseFlags
Definition: microhttpd.h:2025
MHD_mutex_ cleanup_connection_mutex
Definition: internal.h:1310
Header for platform-independent locks abstraction.
int(* MHD_AcceptPolicyCallback)(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Definition: microhttpd.h:1367
void(* MHD_NotifyConnectionCallback)(void *cls, struct MHD_Connection *connection, void **socket_context, enum MHD_ConnectionNotificationCode toe)
Definition: microhttpd.h:1460
struct MHD_HTTP_Header * headers_received
Definition: internal.h:606
struct MHD_Connection * connections_tail
Definition: internal.h:1120
void * upgrade_handler_cls
Definition: internal.h:308
MHD_FLAG
Flags for the struct MHD_Daemon.
Definition: microhttpd.h:527
struct MHD_HTTP_Header * headers_received_tail
Definition: internal.h:611
size_t pool_increment
Definition: internal.h:1285
struct MHD_Connection * suspended_connections_head
Definition: internal.h:1125