mongoose.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
  2. // Copyright (c) 2013-2014 Cesanta Software Limited
  3. // All rights reserved
  4. //
  5. // This software is dual-licensed: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License version 2 as
  7. // published by the Free Software Foundation. For the terms of this
  8. // license, see <http://www.gnu.org/licenses/>.
  9. //
  10. // You are free to use this software under the terms of the GNU General
  11. // Public License, but WITHOUT ANY WARRANTY; without even the implied
  12. // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU General Public License for more details.
  14. //
  15. // Alternatively, you can license this software under a commercial
  16. // license, as set out in <http://cesanta.com/>.
  17. #ifndef MONGOOSE_HEADER_INCLUDED
  18. #define MONGOOSE_HEADER_INCLUDED
  19. #define MONGOOSE_VERSION "5.6"
  20. #include <stdio.h> // required for FILE
  21. #include <stddef.h> // required for size_t
  22. #include <sys/types.h> // required for time_t
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif // __cplusplus
  26. // This structure contains information about HTTP request.
  27. struct mg_connection {
  28. const char *request_method; // "GET", "POST", etc
  29. const char *uri; // URL-decoded URI
  30. const char *http_version; // E.g. "1.0", "1.1"
  31. const char *query_string; // URL part after '?', not including '?', or NULL
  32. char remote_ip[48]; // Max IPv6 string length is 45 characters
  33. char local_ip[48]; // Local IP address
  34. unsigned short remote_port; // Client's port
  35. unsigned short local_port; // Local port number
  36. int num_headers; // Number of HTTP headers
  37. struct mg_header {
  38. const char *name; // HTTP header name
  39. const char *value; // HTTP header value
  40. } http_headers[30];
  41. char *content; // POST (or websocket message) data, or NULL
  42. size_t content_len; // Data length
  43. int is_websocket; // Connection is a websocket connection
  44. int status_code; // HTTP status code for HTTP error handler
  45. int wsbits; // First byte of the websocket frame
  46. void *server_param; // Parameter passed to mg_create_server()
  47. void *connection_param; // Placeholder for connection-specific data
  48. void *callback_param;
  49. };
  50. struct mg_server; // Opaque structure describing server instance
  51. enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
  52. enum mg_event {
  53. MG_POLL = 100, // Callback return value is ignored
  54. MG_CONNECT, // If callback returns MG_FALSE, connect fails
  55. MG_AUTH, // If callback returns MG_FALSE, authentication fails
  56. MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
  57. MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
  58. MG_RECV, // Mongoose has received POST data chunk.
  59. // Callback should return a number of bytes to discard from
  60. // the receive buffer, or -1 to close the connection.
  61. MG_CLOSE, // Connection is closed, callback return value is ignored
  62. MG_WS_HANDSHAKE, // New websocket connection, handshake request
  63. MG_WS_CONNECT, // New websocket connection established
  64. MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
  65. };
  66. typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
  67. // Websocket opcodes, from http://tools.ietf.org/html/rfc6455
  68. enum {
  69. WEBSOCKET_OPCODE_CONTINUATION = 0x0,
  70. WEBSOCKET_OPCODE_TEXT = 0x1,
  71. WEBSOCKET_OPCODE_BINARY = 0x2,
  72. WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
  73. WEBSOCKET_OPCODE_PING = 0x9,
  74. WEBSOCKET_OPCODE_PONG = 0xa
  75. };
  76. // Server management functions
  77. struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
  78. void mg_destroy_server(struct mg_server **);
  79. const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
  80. time_t mg_poll_server(struct mg_server *, int milliseconds);
  81. const char **mg_get_valid_option_names(void);
  82. const char *mg_get_option(const struct mg_server *server, const char *name);
  83. void mg_copy_listeners(struct mg_server *from, struct mg_server *to);
  84. struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
  85. void mg_wakeup_server(struct mg_server *);
  86. void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
  87. struct mg_connection *mg_connect(struct mg_server *, const char *);
  88. // Connection management functions
  89. void mg_send_status(struct mg_connection *, int status_code);
  90. void mg_send_header(struct mg_connection *, const char *name, const char *val);
  91. size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
  92. size_t mg_printf_data(struct mg_connection *, const char *format, ...);
  93. size_t mg_write(struct mg_connection *, const void *buf, size_t len);
  94. size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
  95. size_t mg_websocket_write(struct mg_connection *, int opcode,
  96. const char *data, size_t data_len);
  97. size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
  98. const char *fmt, ...);
  99. void mg_send_file(struct mg_connection *, const char *path, const char *);
  100. void mg_send_file_data(struct mg_connection *, int fd);
  101. const char *mg_get_header(const struct mg_connection *, const char *name);
  102. const char *mg_get_mime_type(const char *name, const char *default_mime_type);
  103. int mg_get_var(const struct mg_connection *conn, const char *var_name,
  104. char *buf, size_t buf_len);
  105. int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
  106. int mg_parse_multipart(const char *buf, int buf_len,
  107. char *var_name, int var_name_len,
  108. char *file_name, int file_name_len,
  109. const char **data, int *data_len);
  110. // Utility functions
  111. void *mg_start_thread(void *(*func)(void *), void *param);
  112. char *mg_md5(char buf[33], ...);
  113. int mg_authorize_digest(struct mg_connection *c, FILE *fp);
  114. size_t mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
  115. int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len, int);
  116. int mg_terminate_ssl(struct mg_connection *c, const char *cert);
  117. int mg_forward(struct mg_connection *c, const char *addr);
  118. void *mg_mmap(FILE *fp, size_t size);
  119. void mg_munmap(void *p, size_t size);
  120. // Templates support
  121. struct mg_expansion {
  122. const char *keyword;
  123. void (*handler)(struct mg_connection *);
  124. };
  125. void mg_template(struct mg_connection *, const char *text,
  126. struct mg_expansion *expansions);
  127. #ifdef __cplusplus
  128. }
  129. #endif // __cplusplus
  130. #endif // MONGOOSE_HEADER_INCLUDED