mongoose.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (c) 2004-2010 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; // \0 - terminated
  35. char *remote_user; // Authenticated user
  36. char *log_message; // Mongoose error log message
  37. long remote_ip; // Client's IP address
  38. int remote_port; // Client's port
  39. int status_code; // HTTP reply status code
  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. };
  55. // Prototype for the user-defined function. Mongoose calls this function
  56. // on every event mentioned above.
  57. //
  58. // Parameters:
  59. // event: which event has been triggered.
  60. // conn: opaque connection handler. Could be used to read, write data to the
  61. // client, etc. See functions below that accept "mg_connection *".
  62. // request_info: Information about HTTP request.
  63. //
  64. // Return:
  65. // If handler returns non-NULL, that means that handler has processed the
  66. // request by sending appropriate HTTP reply to the client. Mongoose treats
  67. // the request as served.
  68. // If callback returns NULL, that means that callback has not processed
  69. // the request. Handler must not send any data to the client in this case.
  70. // Mongoose proceeds with request handling as if nothing happened.
  71. typedef void * (*mg_callback_t)(enum mg_event event,
  72. struct mg_connection *conn,
  73. const struct mg_request_info *request_info);
  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. // Send data to the client.
  135. int mg_write(struct mg_connection *, const void *buf, size_t len);
  136. // Send data to the browser using printf() semantics.
  137. //
  138. // Works exactly like mg_write(), but allows to do message formatting.
  139. // Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
  140. // (8 Kb by default) as temporary message storage for formatting. Do not
  141. // print data that is bigger than that, otherwise it will be truncated.
  142. int mg_printf(struct mg_connection *, const char *fmt, ...);
  143. // Send contents of the entire file together with HTTP headers.
  144. void mg_send_file(struct mg_connection *conn, const char *path);
  145. // Read data from the remote end, return number of bytes read.
  146. int mg_read(struct mg_connection *, void *buf, size_t len);
  147. // Get the value of particular HTTP header.
  148. //
  149. // This is a helper function. It traverses request_info->http_headers array,
  150. // and if the header is present in the array, returns its value. If it is
  151. // not present, NULL is returned.
  152. const char *mg_get_header(const struct mg_connection *, const char *name);
  153. // Get a value of particular form variable.
  154. //
  155. // Parameters:
  156. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  157. // or request_info.query_string.
  158. // data_len: length of the encoded data.
  159. // var_name: variable name to decode from the buffer
  160. // buf: destination buffer for the decoded variable
  161. // buf_len: length of the destination buffer
  162. //
  163. // Return:
  164. // On success, length of the decoded variable.
  165. // On error, -1 (variable not found, or destination buffer is too small).
  166. //
  167. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  168. // failure, dst[0] == '\0'.
  169. int mg_get_var(const char *data, size_t data_len,
  170. const char *var_name, char *buf, size_t buf_len);
  171. // Fetch value of certain cookie variable into the destination buffer.
  172. //
  173. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  174. // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  175. // parameter. This function returns only first occurrence.
  176. //
  177. // Return:
  178. // On success, value length.
  179. // On error, -1 (either "Cookie:" header is not present at all, or the
  180. // requested parameter is not found, or destination buffer is too small
  181. // to hold the value).
  182. int mg_get_cookie(const struct mg_connection *,
  183. const char *cookie_name, char *buf, size_t buf_len);
  184. // Return Mongoose version.
  185. const char *mg_version(void);
  186. // MD5 hash given strings.
  187. // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  188. // asciiz strings. When function returns, buf will contain human-readable
  189. // MD5 hash. Example:
  190. // char buf[33];
  191. // mg_md5(buf, "aa", "bb", NULL);
  192. void mg_md5(char *buf, ...);
  193. #ifdef __cplusplus
  194. }
  195. #endif // __cplusplus
  196. #endif // MONGOOSE_HEADER_INCLUDED