mongoose.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2004-2009 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * $Id: mongoose.h 517 2010-05-03 12:54:59Z valenok $
  23. */
  24. #ifndef MONGOOSE_HEADER_INCLUDED
  25. #define MONGOOSE_HEADER_INCLUDED
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif /* __cplusplus */
  29. struct mg_context; /* Handle for the HTTP service itself */
  30. struct mg_connection; /* Handle for the individual connection */
  31. /*
  32. * This structure contains full information about the HTTP request.
  33. * It is passed to the user-specified callback function as a parameter.
  34. */
  35. struct mg_request_info {
  36. char *request_method; /* "GET", "POST", etc */
  37. char *uri; /* Normalized URI */
  38. char *http_version; /* E.g. "1.0", "1.1" */
  39. char *query_string; /* \0 - terminated */
  40. char *post_data; /* POST data buffer */
  41. char *remote_user; /* Authenticated user */
  42. char *log_message; /* Mongoose error log message */
  43. long remote_ip; /* Client's IP address */
  44. int remote_port; /* Client's port */
  45. int post_data_len; /* POST buffer length */
  46. int status_code; /* HTTP status code */
  47. int is_ssl; /* 1 if SSL-ed, 0 if not */
  48. int num_headers; /* Number of headers */
  49. struct mg_header {
  50. char *name; /* HTTP header name */
  51. char *value; /* HTTP header value */
  52. } http_headers[64]; /* Maximum 64 headers */
  53. };
  54. /*
  55. * Error codes for all functions that return 'int'.
  56. */
  57. enum mg_error_t {
  58. MG_ERROR,
  59. MG_SUCCESS,
  60. MG_NOT_FOUND,
  61. MG_BUFFER_TOO_SMALL
  62. };
  63. /*
  64. * Start the web server.
  65. *
  66. * This must be the first function called by the application.
  67. * It creates a serving thread, and returns a context structure that
  68. * can be used to alter the configuration, and stop the server.
  69. */
  70. struct mg_context *mg_start(void);
  71. /*
  72. * Stop the web server.
  73. *
  74. * Must be called last, when an application wants to stop the web server and
  75. * release all associated resources. This function blocks until all Mongoose
  76. * threads are stopped. Context pointer becomes invalid.
  77. */
  78. void mg_stop(struct mg_context *);
  79. /*
  80. * Get the current value of a particular option.
  81. *
  82. * Return:
  83. * MG_SUCCESS, MG_NOT_FOUND, MG_BUFFER_TOO_SMALL
  84. */
  85. enum mg_error_t mg_get_option(struct mg_context *,
  86. const char *option_name, char *buf, size_t buf_len);
  87. /*
  88. * Set a value for a particular option.
  89. *
  90. * Mongoose makes an internal copy of the option value string, which must be
  91. * valid nul-terminated ASCII or UTF-8 string. It is safe to change any option
  92. * at any time. The order of setting various options is also irrelevant with
  93. * one exception: if "ports" option contains SSL listening ports, a "ssl_cert"
  94. * option must be set BEFORE the "ports" option.
  95. *
  96. * Return:
  97. * MG_ERROR, MG_SUCCESS, or MG_NOT_FOUND if option is unknown.
  98. */
  99. enum mg_error_t mg_set_option(struct mg_context *,
  100. const char *name, const char *value);
  101. /*
  102. * Add, edit or delete the entry in the passwords file.
  103. *
  104. * This function allows an application to manipulate .htpasswd files on the
  105. * fly by adding, deleting and changing user records. This is one of the
  106. * several ways of implementing authentication on the server side. For another,
  107. * cookie-based way please refer to the examples/chat.c in the source tree.
  108. *
  109. * If password is not NULL, entry is added (or modified if already exists).
  110. * If password is NULL, entry is deleted.
  111. *
  112. * Return:
  113. * MG_ERROR, MG_SUCCESS
  114. */
  115. enum mg_error_t mg_modify_passwords_file(struct mg_context *ctx,
  116. const char *file_name, const char *user, const char *password);
  117. /*
  118. * Attach a callback function to certain event.
  119. * Callback must return MG_SUCCESS or MG_ERROR.
  120. *
  121. * If callback returns MG_SUCCESS, that means that callback has processed the
  122. * request by sending appropriate HTTP reply to the client. Mongoose treats
  123. * the request as served.
  124. *
  125. * If callback returns MG_ERROR, that means that callback has not processed
  126. * the request. Callback must not send any data to client in this case.
  127. * Mongoose proceeds with request handling.
  128. *
  129. * NOTE: for MG_EVENT_SSL_PASSWORD event the callback must have
  130. * int (*)(char *, int, int, void *) prototype. Refer to OpenSSL documentation
  131. * for more details about the SSL password callback.
  132. */
  133. enum mg_event_t {
  134. MG_EVENT_NEW_REQUEST, /* New HTTP request has arrived */
  135. MG_EVENT_HTTP_ERROR, /* Mongoose is about to send HTTP error */
  136. MG_EVENT_LOG, /* Mongoose is about to log a message */
  137. MG_EVENT_SSL_PASSWORD, /* SSL certificate needs verification */
  138. NUM_EVENTS
  139. };
  140. typedef enum mg_error_t (*mg_callback_t)(struct mg_connection *,
  141. const struct mg_request_info *);
  142. void mg_set_callback(struct mg_context *, enum mg_event_t, mg_callback_t);
  143. /*
  144. * Send data to the client.
  145. */
  146. int mg_write(struct mg_connection *, const void *buf, size_t len);
  147. /*
  148. * Send data to the browser using printf() semantics.
  149. *
  150. * Works exactly like mg_write(), but allows to do message formatting.
  151. * Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
  152. * (8 Kb by default) as temporary message storage for formatting. Do not
  153. * print data that is bigger than that, otherwise it will be truncated.
  154. */
  155. int mg_printf(struct mg_connection *, const char *fmt, ...);
  156. /*
  157. * Read data from the remote or local end.
  158. */
  159. int mg_read(struct mg_connection *, void *buf, size_t len);
  160. /*
  161. * Get the value of particular HTTP header.
  162. *
  163. * This is a helper function. It traverses request_info->http_headers array,
  164. * and if the header is present in the array, returns its value. If it is
  165. * not present, NULL is returned.
  166. */
  167. const char *mg_get_header(const struct mg_connection *, const char *name);
  168. /*
  169. * Get a value of particular form variable.
  170. *
  171. * Either request_info->query_string or read POST data can be scanned.
  172. * mg_get_qsvar() is convenience method to get variable from the query string.
  173. * Destination buffer is guaranteed to be '\0' - terminated. In case of
  174. * failure, dst[0] == '\0'.
  175. *
  176. * Return:
  177. * MG_SUCCESS, MG_NOT_FOUND or MG_BUFFER_TOO_SMALL
  178. */
  179. enum mg_error_t mg_get_var(const char *data, size_t data_len,
  180. const char *var_name, char *buf, size_t buf_len);
  181. enum mg_error_t mg_get_qsvar(const struct mg_request_info *,
  182. const char *var_name, char *buf, size_t buf_len);
  183. /*
  184. * Fetch value of certain cookie variable into the destination buffer.
  185. *
  186. * Destination buffer is guaranteed to be '\0' - terminated. In case of
  187. * failure, dst[0] == '\0'.
  188. *
  189. * Return:
  190. * MG_SUCCESS, MG_NOT_FOUND or MG_BUFFER_TOO_SMALL
  191. */
  192. enum mg_error_t mg_get_cookie(const struct mg_connection *,
  193. const char *cookie_name, char *buf, size_t buf_len);
  194. /*
  195. * Return Mongoose version.
  196. */
  197. const char *mg_version(void);
  198. /*
  199. * Print command line usage string.
  200. */
  201. void mg_show_usage_string(FILE *fp);
  202. #ifdef __cplusplus
  203. }
  204. #endif /* __cplusplus */
  205. #endif /* MONGOOSE_HEADER_INCLUDED */