mongoose.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 information about the HTTP request.
  28. struct mg_request_info {
  29. void *user_data; // User-defined pointer passed to mg_start()
  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 reply 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. // Various events on which user-defined function is called by Mongoose.
  47. enum mg_event {
  48. MG_NEW_REQUEST, // New HTTP request has arrived from the client
  49. MG_HTTP_ERROR, // HTTP error must be returned to the client
  50. MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
  51. MG_INIT_SSL, // Mongoose initializes SSL. Instead of mg_connection *,
  52. // SSL context is passed to the callback function.
  53. };
  54. // Prototype for the user-defined function. Mongoose calls this function
  55. // on every event mentioned above.
  56. //
  57. // Parameters:
  58. // event: which event has been triggered.
  59. // conn: opaque connection handler. Could be used to read, write data to the
  60. // client, etc. See functions below that accept "mg_connection *".
  61. // request_info: Information about HTTP request.
  62. //
  63. // Return:
  64. // If handler returns non-NULL, that means that handler has processed the
  65. // request by sending appropriate HTTP reply to the client. Mongoose treats
  66. // the request as served.
  67. // If callback returns NULL, that means that callback has not processed
  68. // the request. Handler must not send any data to the client in this case.
  69. // Mongoose proceeds with request handling as if nothing happened.
  70. typedef void * (*mg_callback_t)(enum mg_event event,
  71. struct mg_connection *conn,
  72. const struct mg_request_info *request_info);
  73. // Start web server.
  74. //
  75. // Parameters:
  76. // callback: user defined event handling function or NULL.
  77. // options: NULL terminated list of option_name, option_value pairs that
  78. // specify Mongoose configuration parameters.
  79. //
  80. // Example:
  81. // const char *options[] = {
  82. // "document_root", "/var/www",
  83. // "listening_ports", "80,443s",
  84. // NULL
  85. // };
  86. // struct mg_context *ctx = mg_start(&my_func, options);
  87. //
  88. // Please refer to http://code.google.com/p/mongoose/wiki/MongooseManual
  89. // for the list of valid option and their possible values.
  90. //
  91. // Return:
  92. // web server context, or NULL on error.
  93. struct mg_context *mg_start(mg_callback_t callback, void *user_data,
  94. const char **options);
  95. // Stop the web server.
  96. //
  97. // Must be called last, when an application wants to stop the web server and
  98. // release all associated resources. This function blocks until all Mongoose
  99. // threads are stopped. Context pointer becomes invalid.
  100. void mg_stop(struct mg_context *);
  101. // Get the value of particular configuration parameter.
  102. // The value returned is read-only. Mongoose does not allow changing
  103. // configuration at run time.
  104. // If given parameter name is not valid, NULL is returned. For valid
  105. // names, return value is guaranteed to be non-NULL. If parameter is not
  106. // set, zero-length string is returned.
  107. const char *mg_get_option(const struct mg_context *ctx, const char *name);
  108. // Return array of strings that represent valid configuration options.
  109. // For each option, a short name, long name, and default value is returned.
  110. // Array is NULL terminated.
  111. const char **mg_get_valid_option_names(void);
  112. // Add, edit or delete the entry in the passwords file.
  113. //
  114. // This function allows an application to manipulate .htpasswd files on the
  115. // fly by adding, deleting and changing user records. This is one of the
  116. // several ways of implementing authentication on the server side. For another,
  117. // cookie-based way please refer to the examples/chat.c in the source tree.
  118. //
  119. // If password is not NULL, entry is added (or modified if already exists).
  120. // If password is NULL, entry is deleted.
  121. //
  122. // Return:
  123. // 1 on success, 0 on error.
  124. int mg_modify_passwords_file(struct mg_context *ctx,
  125. const char *passwords_file_name, const char *user, const char *password);
  126. // Send data to the client.
  127. int mg_write(struct mg_connection *, const void *buf, size_t len);
  128. // Send data to the browser using printf() semantics.
  129. //
  130. // Works exactly like mg_write(), but allows to do message formatting.
  131. // Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
  132. // (8 Kb by default) as temporary message storage for formatting. Do not
  133. // print data that is bigger than that, otherwise it will be truncated.
  134. int mg_printf(struct mg_connection *, const char *fmt, ...);
  135. // Read data from the remote end, return number of bytes read.
  136. int mg_read(struct mg_connection *, void *buf, size_t len);
  137. // Get the value of particular HTTP header.
  138. //
  139. // This is a helper function. It traverses request_info->http_headers array,
  140. // and if the header is present in the array, returns its value. If it is
  141. // not present, NULL is returned.
  142. const char *mg_get_header(const struct mg_connection *, const char *name);
  143. // Get a value of particular form variable.
  144. //
  145. // Parameters:
  146. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  147. // or request_info.query_string.
  148. // data_len: length of the encoded data.
  149. // var_name: variable name to decode from the buffer
  150. // buf: destination buffer for the decoded variable
  151. // buf_len: length of the destination buffer
  152. //
  153. // Return:
  154. // On success, length of the decoded variable.
  155. // On error, -1 (variable not found, or destination buffer is too small).
  156. //
  157. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  158. // failure, dst[0] == '\0'.
  159. int mg_get_var(const char *data, size_t data_len,
  160. const char *var_name, char *buf, size_t buf_len);
  161. // Fetch value of certain cookie variable into the destination buffer.
  162. //
  163. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  164. // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  165. // parameter. This function returns only first occurrence.
  166. //
  167. // Return:
  168. // On success, value length.
  169. // On error, -1 (either "Cookie:" header is not present at all, or the
  170. // requested parameter is not found, or destination buffer is too small
  171. // to hold the value).
  172. int mg_get_cookie(const struct mg_connection *,
  173. const char *cookie_name, char *buf, size_t buf_len);
  174. // Return Mongoose version.
  175. const char *mg_version(void);
  176. // MD5 hash given strings.
  177. // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  178. // asciiz strings. When function returns, buf will contain human-readable
  179. // MD5 hash. Example:
  180. // char buf[33];
  181. // mg_md5(buf, "aa", "bb", NULL);
  182. void mg_md5(char *buf, ...);
  183. #ifdef __cplusplus
  184. }
  185. #endif // __cplusplus
  186. #endif // MONGOOSE_HEADER_INCLUDED