mongoose.h 7.8 KB

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