mongoose.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright (c) 2004-2011 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. void *user_data; // User-defined pointer passed to mg_start()
  32. char *request_method; // "GET", "POST", etc
  33. char *uri; // URL-decoded URI
  34. char *http_version; // E.g. "1.0", "1.1"
  35. char *query_string; // URL part after '?' (not including '?') or NULL
  36. char *remote_user; // Authenticated user, or NULL if no auth used
  37. char *log_message; // Mongoose error log message, MG_EVENT_LOG only
  38. long remote_ip; // Client's IP address
  39. int remote_port; // Client's port
  40. int status_code; // HTTP reply status code, e.g. 200
  41. int is_ssl; // 1 if SSL-ed, 0 if not
  42. int num_headers; // Number of headers
  43. struct mg_header {
  44. char *name; // HTTP header name
  45. char *value; // HTTP header value
  46. } http_headers[64]; // Maximum 64 headers
  47. };
  48. // Various events on which user-defined function is called by Mongoose.
  49. enum mg_event {
  50. MG_NEW_REQUEST, // New HTTP request has arrived from the client
  51. MG_HTTP_ERROR, // HTTP error must be returned to the client
  52. MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
  53. MG_INIT_SSL, // Mongoose initializes SSL. Instead of mg_connection *,
  54. // SSL context is passed to the callback function.
  55. MG_REQUEST_COMPLETE // Mongoose has finished handling the request
  56. };
  57. // Prototype for the user-defined function. Mongoose calls this function
  58. // on every MG_* event.
  59. //
  60. // Parameters:
  61. // event: which event has been triggered.
  62. // conn: opaque connection handler. Could be used to read, write data to the
  63. // client, etc. See functions below that have "mg_connection *" arg.
  64. //
  65. // Return:
  66. // If handler returns non-NULL, that means that handler has processed the
  67. // request by sending appropriate HTTP reply to the client. Mongoose treats
  68. // the request as served.
  69. // If handler returns NULL, that means that handler has not processed
  70. // the request. Handler must not send any data to the client in this case.
  71. // Mongoose proceeds with request handling as if nothing happened.
  72. typedef void * (*mg_callback_t)(enum mg_event event,
  73. struct mg_connection *conn);
  74. // Start web server.
  75. //
  76. // Parameters:
  77. // callback: user defined event handling function or NULL.
  78. // options: NULL terminated list of option_name, option_value pairs that
  79. // specify Mongoose configuration parameters.
  80. //
  81. // Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
  82. // processing is required for these, signal handlers must be set up
  83. // after calling mg_start().
  84. //
  85. //
  86. // Example:
  87. // const char *options[] = {
  88. // "document_root", "/var/www",
  89. // "listening_ports", "80,443s",
  90. // NULL
  91. // };
  92. // struct mg_context *ctx = mg_start(&my_func, NULL, options);
  93. //
  94. // Please refer to http://code.google.com/p/mongoose/wiki/MongooseManual
  95. // for the list of valid option and their possible values.
  96. //
  97. // Return:
  98. // web server context, or NULL on error.
  99. struct mg_context *mg_start(mg_callback_t callback, void *user_data,
  100. const char **options);
  101. // Stop the web server.
  102. //
  103. // Must be called last, when an application wants to stop the web server and
  104. // release all associated resources. This function blocks until all Mongoose
  105. // threads are stopped. Context pointer becomes invalid.
  106. void mg_stop(struct mg_context *);
  107. // Get the value of particular configuration parameter.
  108. // The value returned is read-only. Mongoose does not allow changing
  109. // configuration at run time.
  110. // If given parameter name is not valid, NULL is returned. For valid
  111. // names, return value is guaranteed to be non-NULL. If parameter is not
  112. // set, zero-length string is returned.
  113. const char *mg_get_option(const struct mg_context *ctx, const char *name);
  114. // Return array of strings that represent valid configuration options.
  115. // For each option, a short name, long name, and default value is returned.
  116. // Array is NULL terminated.
  117. const char **mg_get_valid_option_names(void);
  118. // Add, edit or delete the entry in the passwords file.
  119. //
  120. // This function allows an application to manipulate .htpasswd files on the
  121. // fly by adding, deleting and changing user records. This is one of the
  122. // several ways of implementing authentication on the server side. For another,
  123. // cookie-based way please refer to the examples/chat.c in the source tree.
  124. //
  125. // If password is not NULL, entry is added (or modified if already exists).
  126. // If password is NULL, entry is deleted.
  127. //
  128. // Return:
  129. // 1 on success, 0 on error.
  130. int mg_modify_passwords_file(const char *passwords_file_name,
  131. const char *domain,
  132. const char *user,
  133. const char *password);
  134. // Return mg_request_info structure associated with the request.
  135. // Always succeeds.
  136. const struct mg_request_info *mg_get_request_info(struct mg_connection *);
  137. // Send data to the client.
  138. int mg_write(struct mg_connection *, const void *buf, size_t len);
  139. // Send data to the browser using printf() semantics.
  140. //
  141. // Works exactly like mg_write(), but allows to do message formatting.
  142. // Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
  143. // (8 Kb by default) as temporary message storage for formatting. Do not
  144. // print data that is bigger than that, otherwise it will be truncated.
  145. int mg_printf(struct mg_connection *, const char *fmt, ...)
  146. #ifdef __GNUC__
  147. __attribute__((format(printf, 2, 3)))
  148. #endif
  149. ;
  150. // Send contents of the entire file together with HTTP headers.
  151. void mg_send_file(struct mg_connection *conn, const char *path);
  152. // Read data from the remote end, return number of bytes read.
  153. int mg_read(struct mg_connection *, void *buf, size_t len);
  154. // Get the value of particular HTTP header.
  155. //
  156. // This is a helper function. It traverses request_info->http_headers array,
  157. // and if the header is present in the array, returns its value. If it is
  158. // not present, NULL is returned.
  159. const char *mg_get_header(const struct mg_connection *, const char *name);
  160. // Get a value of particular form variable.
  161. //
  162. // Parameters:
  163. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  164. // or request_info.query_string.
  165. // data_len: length of the encoded data.
  166. // var_name: variable name to decode from the buffer
  167. // buf: destination buffer for the decoded variable
  168. // buf_len: length of the destination buffer
  169. //
  170. // Return:
  171. // On success, length of the decoded variable.
  172. // On error, -1 (variable not found, or destination buffer is too small).
  173. //
  174. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  175. // failure, dst[0] == '\0'.
  176. int mg_get_var(const char *data, size_t data_len,
  177. const char *var_name, char *buf, size_t buf_len);
  178. // Fetch value of certain cookie variable into the destination buffer.
  179. //
  180. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  181. // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  182. // parameter. This function returns only first occurrence.
  183. //
  184. // Return:
  185. // On success, value length.
  186. // On error, 0 (either "Cookie:" header is not present at all, or the
  187. // requested parameter is not found, or destination buffer is too small
  188. // to hold the value).
  189. int mg_get_cookie(const struct mg_connection *,
  190. const char *cookie_name, char *buf, size_t buf_len);
  191. // Connect to the remote web server.
  192. // Return:
  193. // On success, valid pointer to the new connection
  194. // On error, NULL
  195. struct mg_connection *mg_connect(struct mg_context *ctx,
  196. const char *host, int port, int use_ssl);
  197. // Close the connection opened by mg_connect().
  198. void mg_close_connection(struct mg_connection *conn);
  199. // Download given URL to a given file.
  200. // url: URL to download
  201. // path: file name where to save the data
  202. // request_info: pointer to a structure that will hold parsed reply headers
  203. // buf, bul_len: a buffer for the reply headers
  204. // Return:
  205. // On error, NULL
  206. // On success, opened file stream to the downloaded contents. The stream
  207. // is positioned to the end of the file. It is a user responsibility
  208. // to fclose() opened file stream.
  209. FILE *mg_fetch(struct mg_context *ctx, const char *url, const char *path,
  210. char *buf, size_t buf_len, struct mg_request_info *request_info);
  211. // Convenience function -- create detached thread.
  212. // Return: 0 on success, non-0 on error.
  213. typedef void * (*mg_thread_func_t)(void *);
  214. int mg_start_thread(mg_thread_func_t f, void *p);
  215. // Return builtin mime type for the given file name.
  216. // For unrecognized extensions, "text/plain" is returned.
  217. const char *mg_get_builtin_mime_type(const char *file_name);
  218. // Return Mongoose version.
  219. const char *mg_version(void);
  220. // MD5 hash given strings.
  221. // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  222. // asciiz strings. When function returns, buf will contain human-readable
  223. // MD5 hash. Example:
  224. // char buf[33];
  225. // mg_md5(buf, "aa", "bb", NULL);
  226. void mg_md5(char *buf, ...);
  227. #ifdef __cplusplus
  228. }
  229. #endif // __cplusplus
  230. #endif // MONGOOSE_HEADER_INCLUDED