mongoose.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright (c) 2004-2012 Sergey Lyubka
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #ifndef MONGOOSE_HEADER_INCLUDED
  21. #define MONGOOSE_HEADER_INCLUDED
  22. #include <stdio.h>
  23. #include <stddef.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif // __cplusplus
  27. struct mg_context; // Handle for the HTTP service itself
  28. struct mg_connection; // Handle for the individual connection
  29. // This structure contains information about the HTTP request.
  30. struct mg_request_info {
  31. char *request_method; // "GET", "POST", etc
  32. char *uri; // URL-decoded URI
  33. char *http_version; // E.g. "1.0", "1.1"
  34. char *query_string; // URL part after '?' (not including '?') or NULL
  35. char *remote_user; // Authenticated user, or NULL if no auth used
  36. long remote_ip; // Client's IP address
  37. int remote_port; // Client's port
  38. int is_ssl; // 1 if SSL-ed, 0 if not
  39. int num_headers; // Number of headers
  40. struct mg_header {
  41. char *name; // HTTP header name
  42. char *value; // HTTP header value
  43. } http_headers[64]; // Maximum 64 headers
  44. };
  45. // Various events on which user-defined function is called by Mongoose.
  46. enum mg_event {
  47. MG_NEW_REQUEST, // New HTTP request has arrived from the client
  48. MG_REQUEST_COMPLETE, // Mongoose has finished handling the request
  49. MG_HTTP_ERROR, // HTTP error must be returned to the client
  50. MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
  51. MG_INIT_SSL, // SSL initialization, sent before certificate setup
  52. MG_WEBSOCKET_CONNECT, // Sent on HTTP connect, before websocket handshake.
  53. // If user callback returns NULL, then mongoose proceeds
  54. // with handshake, otherwise it closes the connection.
  55. MG_WEBSOCKET_READY, // Handshake has been successfully completed.
  56. MG_WEBSOCKET_MESSAGE, // Incoming message from the client
  57. MG_WEBSOCKET_CLOSE, // Client has closed the connection
  58. };
  59. // Prototype for the user-defined function. Mongoose calls this function
  60. // on every MG_* event.
  61. //
  62. // Parameters:
  63. // event: which event has been triggered.
  64. // conn: opaque connection handler. Could be used to read, write data to the
  65. // client, etc. See functions below that have "mg_connection *" arg.
  66. //
  67. // Return:
  68. // If handler returns non-NULL, that means that handler has processed the
  69. // request by sending appropriate HTTP reply to the client. Mongoose treats
  70. // the request as served.
  71. // If handler returns NULL, that means that handler has not processed
  72. // the request. Handler must not send any data to the client in this case.
  73. // Mongoose proceeds with request handling as if nothing happened.
  74. typedef void *(*mg_callback_t)(enum mg_event event, struct mg_connection *conn);
  75. // Start web server.
  76. //
  77. // Parameters:
  78. // callback: user defined event handling function or NULL.
  79. // options: NULL terminated list of option_name, option_value pairs that
  80. // specify Mongoose configuration parameters.
  81. //
  82. // Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
  83. // processing is required for these, signal handlers must be set up
  84. // after calling mg_start().
  85. //
  86. //
  87. // Example:
  88. // const char *options[] = {
  89. // "document_root", "/var/www",
  90. // "listening_ports", "80,443s",
  91. // NULL
  92. // };
  93. // struct mg_context *ctx = mg_start(&my_func, NULL, options);
  94. //
  95. // Please refer to http://code.google.com/p/mongoose/wiki/MongooseManual
  96. // for the list of valid option and their possible values.
  97. //
  98. // Return:
  99. // web server context, or NULL on error.
  100. struct mg_context *mg_start(mg_callback_t callback, void *user_data,
  101. const char **options);
  102. // Stop the web server.
  103. //
  104. // Must be called last, when an application wants to stop the web server and
  105. // release all associated resources. This function blocks until all Mongoose
  106. // threads are stopped. Context pointer becomes invalid.
  107. void mg_stop(struct mg_context *);
  108. // Get the value of particular configuration parameter.
  109. // The value returned is read-only. Mongoose does not allow changing
  110. // configuration at run time.
  111. // If given parameter name is not valid, NULL is returned. For valid
  112. // names, return value is guaranteed to be non-NULL. If parameter is not
  113. // set, zero-length string is returned.
  114. const char *mg_get_option(const struct mg_context *ctx, const char *name);
  115. // Return array of strings that represent valid configuration options.
  116. // For each option, a short name, long name, and default value is returned.
  117. // Array is NULL terminated.
  118. const char **mg_get_valid_option_names(void);
  119. // Add, edit or delete the entry in the passwords file.
  120. //
  121. // This function allows an application to manipulate .htpasswd files on the
  122. // fly by adding, deleting and changing user records. This is one of the
  123. // several ways of implementing authentication on the server side. For another,
  124. // cookie-based way please refer to the examples/chat.c in the source tree.
  125. //
  126. // If password is not NULL, entry is added (or modified if already exists).
  127. // If password is NULL, entry is deleted.
  128. //
  129. // Return:
  130. // 1 on success, 0 on error.
  131. int mg_modify_passwords_file(const char *passwords_file_name,
  132. const char *domain,
  133. const char *user,
  134. const char *password);
  135. // Return information associated with the request.
  136. // These functions always succeed.
  137. const struct mg_request_info *mg_get_request_info(const struct mg_connection *);
  138. void *mg_get_user_data(struct mg_connection *);
  139. const char *mg_get_log_message(const struct mg_connection *);
  140. int mg_get_reply_status_code(const struct mg_connection *);
  141. void *mg_get_ssl_context(const struct mg_connection *);
  142. // Send data to the client.
  143. // Return:
  144. // 0 when the connection has been closed
  145. // -1 on error
  146. // number of bytes written on success
  147. int mg_write(struct mg_connection *, const void *buf, size_t len);
  148. // Send data to the browser using printf() semantics.
  149. //
  150. // Works exactly like mg_write(), but allows to do message formatting.
  151. // Below are the macros for enabling compiler-specific checks for
  152. // printf-like arguments.
  153. #undef PRINTF_FORMAT_STRING
  154. #if _MSC_VER >= 1400
  155. #include <sal.h>
  156. #if _MSC_VER > 1400
  157. #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
  158. #else
  159. #define PRINTF_FORMAT_STRING(s) __format_string s
  160. #endif
  161. #else
  162. #define PRINTF_FORMAT_STRING(s) s
  163. #endif
  164. #ifdef __GNUC__
  165. #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
  166. #else
  167. #define PRINTF_ARGS(x, y)
  168. #endif
  169. int mg_printf(struct mg_connection *,
  170. PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
  171. // Send contents of the entire file together with HTTP headers.
  172. void mg_send_file(struct mg_connection *conn, const char *path);
  173. // Read data from the remote end, return number of bytes read.
  174. int mg_read(struct mg_connection *, void *buf, size_t len);
  175. // Get the value of particular HTTP header.
  176. //
  177. // This is a helper function. It traverses request_info->http_headers array,
  178. // and if the header is present in the array, returns its value. If it is
  179. // not present, NULL is returned.
  180. const char *mg_get_header(const struct mg_connection *, const char *name);
  181. // Get a value of particular form variable.
  182. //
  183. // Parameters:
  184. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  185. // or request_info.query_string.
  186. // data_len: length of the encoded data.
  187. // var_name: variable name to decode from the buffer
  188. // buf: destination buffer for the decoded variable
  189. // buf_len: length of the destination buffer
  190. //
  191. // Return:
  192. // On success, length of the decoded variable.
  193. // On error:
  194. // -1 (variable not found, or destination buffer is too small).
  195. // -2 (destination buffer is NULL or zero length).
  196. //
  197. // Destination buffer is guaranteed to be '\0' - terminated if it is not
  198. // NULL or zero length. In case of failure, dst[0] == '\0'.
  199. int mg_get_var(const char *data, size_t data_len,
  200. const char *var_name, char *buf, size_t buf_len);
  201. // Fetch value of certain cookie variable into the destination buffer.
  202. //
  203. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  204. // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  205. // parameter. This function returns only first occurrence.
  206. //
  207. // Return:
  208. // On success, value length.
  209. // On error, -1 (either "Cookie:" header is not present at all, or the
  210. // requested parameter is not found, or destination buffer is too small
  211. // to hold the value).
  212. int mg_get_cookie(const struct mg_connection *,
  213. const char *cookie_name, char *buf, size_t buf_len);
  214. // Connect to the remote web server.
  215. // Return:
  216. // On success, valid pointer to the new connection
  217. // On error, NULL
  218. struct mg_connection *mg_connect(struct mg_context *ctx,
  219. const char *host, int port, int use_ssl);
  220. // Close the connection opened by mg_connect().
  221. void mg_close_connection(struct mg_connection *conn);
  222. // Download given URL to a given file.
  223. // url: URL to download
  224. // path: file name where to save the data
  225. // request_info: pointer to a structure that will hold parsed reply headers
  226. // buf, bul_len: a buffer for the reply headers
  227. // Return:
  228. // On error, NULL
  229. // On success, opened file stream to the downloaded contents. The stream
  230. // is positioned to the end of the file. It is the user's responsibility
  231. // to fclose() the opened file stream.
  232. FILE *mg_fetch(struct mg_context *ctx, const char *url, const char *path,
  233. char *buf, size_t buf_len, struct mg_request_info *request_info);
  234. // Convenience function -- create detached thread.
  235. // Return: 0 on success, non-0 on error.
  236. typedef void * (*mg_thread_func_t)(void *);
  237. int mg_start_thread(mg_thread_func_t f, void *p);
  238. // Return builtin mime type for the given file name.
  239. // For unrecognized extensions, "text/plain" is returned.
  240. const char *mg_get_builtin_mime_type(const char *file_name);
  241. // Return Mongoose version.
  242. const char *mg_version(void);
  243. // MD5 hash given strings.
  244. // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  245. // ASCIIz strings. When function returns, buf will contain human-readable
  246. // MD5 hash. Example:
  247. // char buf[33];
  248. // mg_md5(buf, "aa", "bb", NULL);
  249. void mg_md5(char buf[33], ...);
  250. #ifdef __cplusplus
  251. }
  252. #endif // __cplusplus
  253. #endif // MONGOOSE_HEADER_INCLUDED