mongoose.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <stddef.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif // __cplusplus
  26. struct mg_context; // Handle for the HTTP service itself
  27. struct mg_connection; // Handle for the individual connection
  28. // This structure contains information about the HTTP request.
  29. struct mg_request_info {
  30. void *user_data; // User-defined pointer passed to mg_start()
  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. char *log_message; // Mongoose error log message, MG_EVENT_LOG only
  37. long remote_ip; // Client's IP address
  38. int remote_port; // Client's port
  39. int status_code; // HTTP reply status code, e.g. 200
  40. int is_ssl; // 1 if SSL-ed, 0 if not
  41. int num_headers; // Number of headers
  42. struct mg_header {
  43. char *name; // HTTP header name
  44. char *value; // HTTP header value
  45. } http_headers[64]; // Maximum 64 headers
  46. };
  47. // Various events on which user-defined function is called by Mongoose.
  48. enum mg_event {
  49. MG_NEW_REQUEST, // New HTTP request has arrived from the client
  50. MG_HTTP_ERROR, // HTTP error must be returned to the client
  51. MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
  52. MG_INIT_SSL, // Mongoose initializes SSL. Instead of mg_connection *,
  53. // SSL context is passed to the callback function.
  54. MG_REQUEST_COMPLETE // Mongoose has finished handling the request
  55. };
  56. // Prototype for the user-defined function. Mongoose calls this function
  57. // on every MG_* event.
  58. //
  59. // Parameters:
  60. // event: which event has been triggered.
  61. // conn: opaque connection handler. Could be used to read, write data to the
  62. // client, etc. See functions below that have "mg_connection *" arg.
  63. // request_info: Information about HTTP request.
  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. const struct mg_request_info *request_info);
  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. // Send data to the client.
  136. int mg_write(struct mg_connection *, const void *buf, size_t len);
  137. // Send data to the browser using printf() semantics.
  138. //
  139. // Works exactly like mg_write(), but allows to do message formatting.
  140. // Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
  141. // (8 Kb by default) as temporary message storage for formatting. Do not
  142. // print data that is bigger than that, otherwise it will be truncated.
  143. int mg_printf(struct mg_connection *, const char *fmt, ...)
  144. #ifdef __GNUC__
  145. __attribute__((format(printf, 2, 3)))
  146. #endif
  147. ;
  148. // Send contents of the entire file together with HTTP headers.
  149. void mg_send_file(struct mg_connection *conn, const char *path);
  150. // Read data from the remote end, return number of bytes read.
  151. int mg_read(struct mg_connection *, void *buf, size_t len);
  152. // Get the value of particular HTTP header.
  153. //
  154. // This is a helper function. It traverses request_info->http_headers array,
  155. // and if the header is present in the array, returns its value. If it is
  156. // not present, NULL is returned.
  157. const char *mg_get_header(const struct mg_connection *, const char *name);
  158. // Get a value of particular form variable.
  159. //
  160. // Parameters:
  161. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  162. // or request_info.query_string.
  163. // data_len: length of the encoded data.
  164. // var_name: variable name to decode from the buffer
  165. // buf: destination buffer for the decoded variable
  166. // buf_len: length of the destination buffer
  167. //
  168. // Return:
  169. // On success, length of the decoded variable.
  170. // On error, -1 (variable not found, or destination buffer is too small).
  171. //
  172. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  173. // failure, dst[0] == '\0'.
  174. int mg_get_var(const char *data, size_t data_len,
  175. const char *var_name, char *buf, size_t buf_len);
  176. // Fetch value of certain cookie variable into the destination buffer.
  177. //
  178. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  179. // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  180. // parameter. This function returns only first occurrence.
  181. //
  182. // Return:
  183. // On success, value length.
  184. // On error, 0 (either "Cookie:" header is not present at all, or the
  185. // requested parameter is not found, or destination buffer is too small
  186. // to hold the value).
  187. int mg_get_cookie(const struct mg_connection *,
  188. const char *cookie_name, char *buf, size_t buf_len);
  189. // Connect to the remote web server.
  190. // Return:
  191. // On success, valid pointer to the new connection
  192. // On error, NULL
  193. struct mg_connection *mg_connect(struct mg_context *ctx,
  194. const char *host, int port, int use_ssl);
  195. // Download given URL to a given file.
  196. // url: URL to download
  197. // path: file name where to save the data
  198. // request_info: pointer to a structure that will hold parsed reply headers
  199. // Return:
  200. // On success, opened file stream to the downloaded contents. The stream
  201. // is positioned to the end of the file.
  202. // On error, NULL
  203. FILE *mg_fetch(struct mg_context *ctx, const char *url, const char *path,
  204. struct mg_request_info *request_info);
  205. // Return Mongoose version.
  206. const char *mg_version(void);
  207. // MD5 hash given strings.
  208. // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  209. // asciiz strings. When function returns, buf will contain human-readable
  210. // MD5 hash. Example:
  211. // char buf[33];
  212. // mg_md5(buf, "aa", "bb", NULL);
  213. void mg_md5(char *buf, ...);
  214. #ifdef __cplusplus
  215. }
  216. #endif // __cplusplus
  217. #endif // MONGOOSE_HEADER_INCLUDED