mongoose.h 7.8 KB

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