mongoose.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * User-defined handler function. It must return MG_SUCCESS or MG_ERROR.
  56. *
  57. * If handler returns MG_SUCCESS, that means that handler has processed the
  58. * request by sending appropriate HTTP reply to the client. Mongoose treats
  59. * the request as served.
  60. *
  61. * If callback returns MG_ERROR, that means that callback has not processed
  62. * the request. Handler must not send any data to the client in this case.
  63. * Mongoose proceeds with request handling as if nothing happened.
  64. *
  65. * NOTE: ssl_password_handler must have the following prototype:
  66. * int (*)(char *, int, int, void *)
  67. * Refer to OpenSSL documentation for more details.
  68. */
  69. enum mg_error_t {
  70. MG_ERROR,
  71. MG_SUCCESS,
  72. MG_NOT_FOUND,
  73. MG_BUFFER_TOO_SMALL
  74. };
  75. typedef enum mg_error_t (*mg_callback_t)(struct mg_connection *,
  76. const struct mg_request_info *);
  77. /*
  78. * This structure describes Mongoose configuration.
  79. */
  80. struct mg_config {
  81. char *document_root;
  82. char *index_files;
  83. char *ssl_certificate;
  84. char *listening_ports;
  85. char *cgi_extensions;
  86. char *cgi_interpreter;
  87. char *cgi_environment;
  88. char *ssi_extensions;
  89. char *auth_domain;
  90. char *protect;
  91. char *global_passwords_file;
  92. char *put_delete_passwords_file;
  93. char *access_log_file;
  94. char *error_log_file;
  95. char *acl;
  96. char *uid;
  97. char *mime_types;
  98. char *enable_directory_listing;
  99. char *num_threads;
  100. mg_callback_t new_request_handler;
  101. mg_callback_t http_error_handler;
  102. mg_callback_t event_log_handler;
  103. mg_callback_t ssl_password_handler;
  104. };
  105. /*
  106. * Start the web server.
  107. *
  108. * This must be the first function called by the application.
  109. * It creates a serving thread, and returns a context structure that
  110. * can be used to stop the server.
  111. * After calling mg_start(), configuration data must not be changed.
  112. */
  113. struct mg_context *mg_start(struct mg_config *);
  114. /*
  115. * Stop the web server.
  116. *
  117. * Must be called last, when an application wants to stop the web server and
  118. * release all associated resources. This function blocks until all Mongoose
  119. * threads are stopped. Context pointer becomes invalid.
  120. */
  121. void mg_stop(struct mg_context *);
  122. /*
  123. * Add, edit or delete the entry in the passwords file.
  124. *
  125. * This function allows an application to manipulate .htpasswd files on the
  126. * fly by adding, deleting and changing user records. This is one of the
  127. * several ways of implementing authentication on the server side. For another,
  128. * cookie-based way please refer to the examples/chat.c in the source tree.
  129. *
  130. * If password is not NULL, entry is added (or modified if already exists).
  131. * If password is NULL, entry is deleted.
  132. *
  133. * Return:
  134. * MG_ERROR, MG_SUCCESS
  135. */
  136. enum mg_error_t mg_modify_passwords_file(struct mg_context *ctx,
  137. const char *file_name, const char *user, const char *password);
  138. /*
  139. * Send data to the client.
  140. */
  141. int mg_write(struct mg_connection *, const void *buf, size_t len);
  142. /*
  143. * Send data to the browser using printf() semantics.
  144. *
  145. * Works exactly like mg_write(), but allows to do message formatting.
  146. * Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
  147. * (8 Kb by default) as temporary message storage for formatting. Do not
  148. * print data that is bigger than that, otherwise it will be truncated.
  149. */
  150. int mg_printf(struct mg_connection *, const char *fmt, ...);
  151. /*
  152. * Read data from the remote or local end.
  153. */
  154. int mg_read(struct mg_connection *, void *buf, size_t len);
  155. /*
  156. * Get the value of particular HTTP header.
  157. *
  158. * This is a helper function. It traverses request_info->http_headers array,
  159. * and if the header is present in the array, returns its value. If it is
  160. * not present, NULL is returned.
  161. */
  162. const char *mg_get_header(const struct mg_connection *, const char *name);
  163. /*
  164. * Get a value of particular form variable.
  165. *
  166. * Either request_info->query_string or read POST data can be scanned.
  167. * mg_get_qsvar() is convenience method to get variable from the query string.
  168. * Destination buffer is guaranteed to be '\0' - terminated. In case of
  169. * failure, dst[0] == '\0'.
  170. *
  171. * Return:
  172. * MG_SUCCESS Variable value was successfully copied in the buffer.
  173. * MG_NOT_FOUND Requested variable not found.
  174. * MG_BUFFER_TOO_SMALL Destination buffer is too small to hold the value.
  175. */
  176. enum mg_error_t mg_get_var(const char *data, size_t data_len,
  177. const char *var_name, char *buf, size_t buf_len);
  178. enum mg_error_t mg_get_qsvar(const struct mg_request_info *,
  179. const char *var_name, char *buf, size_t buf_len);
  180. /*
  181. * Fetch value of certain cookie variable into the destination buffer.
  182. *
  183. * Destination buffer is guaranteed to be '\0' - terminated. In case of
  184. * failure, dst[0] == '\0'. Note that RFC allows many occurences of the same
  185. * parameter. This function returns only first occurance.
  186. *
  187. * Return:
  188. * MG_SUCCESS Cookie parameter was successfully copied in the buffer.
  189. * MG_NOT_FOUND Either "Cookie:" header is not present at all, or the
  190. * requested parameter is not found.
  191. * MG_BUFFER_TOO_SMALL Destination buffer is too small to hold the value.
  192. */
  193. enum mg_error_t mg_get_cookie(const struct mg_connection *,
  194. const char *cookie_name, char *buf, size_t buf_len);
  195. /*
  196. * Return Mongoose version.
  197. */
  198. const char *mg_version(void);
  199. /*
  200. * MD5 hash given strings.
  201. * Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  202. * asciiz strings. When function returns, buf will contain human-readable
  203. * MD5 hash. Example:
  204. * char buf[33];
  205. * mg_md5(buf, "aa", "bb", NULL);
  206. */
  207. void mg_md5(char *buf, ...);
  208. #ifdef __cplusplus
  209. }
  210. #endif /* __cplusplus */
  211. #endif /* MONGOOSE_HEADER_INCLUDED */