mongoose.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 library 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 library 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 library under a commercial
  16. // license, as set out in <http://cesanta.com/>.
  17. //
  18. // NOTE: Detailed API documentation is at http://cesanta.com/#docs
  19. #ifndef MONGOOSE_HEADER_INCLUDED
  20. #define MONGOOSE_HEADER_INCLUDED
  21. #define MONGOOSE_VERSION "5.4"
  22. #include <stdio.h> // required for FILE
  23. #include <stddef.h> // required for size_t
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif // __cplusplus
  27. // This structure contains information about HTTP request.
  28. struct mg_connection {
  29. const char *request_method; // "GET", "POST", etc
  30. const char *uri; // URL-decoded URI
  31. const char *http_version; // E.g. "1.0", "1.1"
  32. const char *query_string; // URL part after '?', not including '?', or NULL
  33. char remote_ip[48]; // Max IPv6 string length is 45 characters
  34. char local_ip[48]; // Local IP address
  35. unsigned short remote_port; // Client's port
  36. unsigned short local_port; // Local port number
  37. int num_headers; // Number of HTTP headers
  38. struct mg_header {
  39. const char *name; // HTTP header name
  40. const char *value; // HTTP header value
  41. } http_headers[30];
  42. char *content; // POST (or websocket message) data, or NULL
  43. size_t content_len; // Data length
  44. int is_websocket; // Connection is a websocket connection
  45. int status_code; // HTTP status code for HTTP error handler
  46. int wsbits; // First byte of the websocket frame
  47. void *server_param; // Parameter passed to mg_add_uri_handler()
  48. void *connection_param; // Placeholder for connection-specific data
  49. void *callback_param; // Needed by mg_iterate_over_connections()
  50. };
  51. struct mg_server; // Opaque structure describing server instance
  52. enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
  53. enum mg_event {
  54. MG_POLL = 100, // Callback return value is ignored
  55. MG_CONNECT, // If callback returns MG_FALSE, connect fails
  56. MG_AUTH, // If callback returns MG_FALSE, authentication fails
  57. MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
  58. MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
  59. MG_CLOSE, // Connection is closed, callback return value is ignored
  60. MG_WS_HANDSHAKE, // New websocket connection, handshake request
  61. MG_WS_CONNECT, // New websocket connection established
  62. MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
  63. };
  64. typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
  65. // Websocket opcodes, from http://tools.ietf.org/html/rfc6455
  66. enum {
  67. WEBSOCKET_OPCODE_CONTINUATION = 0x0,
  68. WEBSOCKET_OPCODE_TEXT = 0x1,
  69. WEBSOCKET_OPCODE_BINARY = 0x2,
  70. WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
  71. WEBSOCKET_OPCODE_PING = 0x9,
  72. WEBSOCKET_OPCODE_PONG = 0xa
  73. };
  74. // Server management functions
  75. struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
  76. void mg_destroy_server(struct mg_server **);
  77. const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
  78. int mg_poll_server(struct mg_server *, int milliseconds);
  79. const char **mg_get_valid_option_names(void);
  80. const char *mg_get_option(const struct mg_server *server, const char *name);
  81. void mg_set_listening_socket(struct mg_server *, int sock);
  82. int mg_get_listening_socket(struct mg_server *);
  83. void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *);
  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 *, int, int);
  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, int 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);
  100. const char *mg_get_header(const struct mg_connection *, const char *name);
  101. const char *mg_get_mime_type(const char *name, const char *default_mime_type);
  102. int mg_get_var(const struct mg_connection *conn, const char *var_name,
  103. char *buf, size_t buf_len);
  104. int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
  105. int mg_parse_multipart(const char *buf, int buf_len,
  106. char *var_name, int var_name_len,
  107. char *file_name, int file_name_len,
  108. const char **data, int *data_len);
  109. // Utility functions
  110. void *mg_start_thread(void *(*func)(void *), void *param);
  111. char *mg_md5(char buf[33], ...);
  112. int mg_authorize_digest(struct mg_connection *c, FILE *fp);
  113. int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
  114. int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int);
  115. int mg_terminate_ssl(struct mg_connection *c, const char *cert);
  116. // Templates support
  117. struct mg_expansion {
  118. const char *keyword;
  119. void (*handler)(struct mg_connection *);
  120. };
  121. void mg_template(struct mg_connection *, const char *text,
  122. struct mg_expansion *expansions);
  123. #ifdef __cplusplus
  124. }
  125. #endif // __cplusplus
  126. #endif // MONGOOSE_HEADER_INCLUDED