![]() |
|
|||
File indexing completed on 2025-05-11 08:24:12
0001 // Copyright (c) 2004-2012 Sergey Lyubka 0002 // 0003 // Permission is hereby granted, free of charge, to any person obtaining a copy 0004 // of this software and associated documentation files (the "Software"), to deal 0005 // in the Software without restriction, including without limitation the rights 0006 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0007 // copies of the Software, and to permit persons to whom the Software is 0008 // furnished to do so, subject to the following conditions: 0009 // 0010 // The above copyright notice and this permission notice shall be included in 0011 // all copies or substantial portions of the Software. 0012 // 0013 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0014 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0015 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0016 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0017 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0018 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0019 // THE SOFTWARE. 0020 0021 #ifndef MONGOOSE_HEADER_INCLUDED 0022 #define MONGOOSE_HEADER_INCLUDED 0023 0024 #include <stdio.h> 0025 #include <stddef.h> 0026 0027 #ifdef __cplusplus 0028 extern "C" { 0029 #endif // __cplusplus 0030 0031 struct mg_context; // Handle for the HTTP service itself 0032 struct mg_connection; // Handle for the individual connection 0033 0034 0035 // This structure contains information about the HTTP request. 0036 struct mg_request_info { 0037 const char *request_method; // "GET", "POST", etc 0038 const char *uri; // URL-decoded URI 0039 const char *http_version; // E.g. "1.0", "1.1" 0040 const char *query_string; // URL part after '?', not including '?', or NULL 0041 const char *remote_user; // Authenticated user, or NULL if no auth used 0042 long remote_ip; // Client's IP address 0043 int remote_port; // Client's port 0044 int is_ssl; // 1 if SSL-ed, 0 if not 0045 void *user_data; // User data pointer passed to mg_start() 0046 void *conn_data; // Connection-specific user data 0047 0048 int num_headers; // Number of HTTP headers 0049 struct mg_header { 0050 const char *name; // HTTP header name 0051 const char *value; // HTTP header value 0052 } http_headers[64]; // Maximum 64 headers 0053 }; 0054 0055 0056 // This structure needs to be passed to mg_start(), to let mongoose know 0057 // which callbacks to invoke. For detailed description, see 0058 // https://github.com/valenok/mongoose/blob/master/UserManual.md 0059 struct mg_callbacks { 0060 // Called when mongoose has received new HTTP request. 0061 // If callback returns non-zero, 0062 // callback must process the request by sending valid HTTP headers and body, 0063 // and mongoose will not do any further processing. 0064 // If callback returns 0, mongoose processes the request itself. In this case, 0065 // callback must not send any data to the client. 0066 int (*begin_request)(struct mg_connection *); 0067 0068 // Called when mongoose has finished processing request. 0069 void (*end_request)(const struct mg_connection *, int reply_status_code); 0070 0071 // Called when mongoose is about to log a message. If callback returns 0072 // non-zero, mongoose does not log anything. 0073 int (*log_message)(const struct mg_connection *, const char *message); 0074 0075 // Called when mongoose initializes SSL library. 0076 int (*init_ssl)(void *ssl_context, void *user_data); 0077 0078 // Called when websocket request is received, before websocket handshake. 0079 // If callback returns 0, mongoose proceeds with handshake, otherwise 0080 // cinnection is closed immediately. 0081 int (*websocket_connect)(const struct mg_connection *); 0082 0083 // Called when websocket handshake is successfully completed, and 0084 // connection is ready for data exchange. 0085 void (*websocket_ready)(struct mg_connection *); 0086 0087 // Called when data frame has been received from the client. 0088 // Parameters: 0089 // bits: first byte of the websocket frame, see websocket RFC at 0090 // http://tools.ietf.org/html/rfc6455, section 5.2 0091 // data, data_len: payload, with mask (if any) already applied. 0092 // Return value: 0093 // non-0: keep this websocket connection opened. 0094 // 0: close this websocket connection. 0095 int (*websocket_data)(struct mg_connection *, int bits, 0096 char *data, size_t data_len); 0097 0098 // Called when mongoose tries to open a file. Used to intercept file open 0099 // calls, and serve file data from memory instead. 0100 // Parameters: 0101 // path: Full path to the file to open. 0102 // data_len: Placeholder for the file size, if file is served from memory. 0103 // Return value: 0104 // NULL: do not serve file from memory, proceed with normal file open. 0105 // non-NULL: pointer to the file contents in memory. data_len must be 0106 // initilized with the size of the memory block. 0107 const char * (*open_file)(const struct mg_connection *, 0108 const char *path, size_t *data_len); 0109 0110 // Called when mongoose is about to serve Lua server page (.lp file), if 0111 // Lua support is enabled. 0112 // Parameters: 0113 // lua_context: "lua_State *" pointer. 0114 void (*init_lua)(struct mg_connection *, void *lua_context); 0115 0116 // Called when mongoose has uploaded a file to a temporary directory as a 0117 // result of mg_upload() call. 0118 // Parameters: 0119 // file_file: full path name to the uploaded file. 0120 void (*upload)(struct mg_connection *, const char *file_name); 0121 0122 // Called when mongoose is about to send HTTP error to the client. 0123 // Implementing this callback allows to create custom error pages. 0124 // Parameters: 0125 // status: HTTP error status code. 0126 int (*http_error)(struct mg_connection *, int status); 0127 0128 // Called when mongoose needs to generate an HTTP etag. 0129 // Implementing this callback allows a custom etag to be generated. If 0130 // not implemented the standard etag generator is used which is the 0131 // modification time as a hex value and the file size. 0132 // Use this callback if the modification time cannot be controlled. 0133 // Parameters: 0134 // path: path to the file being requested 0135 // etag: buffer to write the etag into 0136 // etag_len: the length of the etag buffer 0137 // Return value: 0138 int (*http_etag)(const struct mg_connection *, 0139 const char *path, char *etag, size_t etag_len); 0140 }; 0141 0142 // Start web server. 0143 // 0144 // Parameters: 0145 // callbacks: mg_callbacks structure with user-defined callbacks. 0146 // options: NULL terminated list of option_name, option_value pairs that 0147 // specify Mongoose configuration parameters. 0148 // 0149 // Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom 0150 // processing is required for these, signal handlers must be set up 0151 // after calling mg_start(). 0152 // 0153 // 0154 // Example: 0155 // const char *options[] = { 0156 // "document_root", "/var/www", 0157 // "listening_ports", "80,443s", 0158 // NULL 0159 // }; 0160 // struct mg_context *ctx = mg_start(&my_func, NULL, options); 0161 // 0162 // Refer to https://github.com/valenok/mongoose/blob/master/UserManual.md 0163 // for the list of valid option and their possible values. 0164 // 0165 // Return: 0166 // web server context, or NULL on error. 0167 struct mg_context *mg_start(const struct mg_callbacks *callbacks, 0168 void *user_data, 0169 const char **configuration_options); 0170 0171 0172 // Stop the web server. 0173 // 0174 // Must be called last, when an application wants to stop the web server and 0175 // release all associated resources. This function blocks until all Mongoose 0176 // threads are stopped. Context pointer becomes invalid. 0177 void mg_stop(struct mg_context *); 0178 0179 0180 // Get the value of particular configuration parameter. 0181 // The value returned is read-only. Mongoose does not allow changing 0182 // configuration at run time. 0183 // If given parameter name is not valid, NULL is returned. For valid 0184 // names, return value is guaranteed to be non-NULL. If parameter is not 0185 // set, zero-length string is returned. 0186 const char *mg_get_option(const struct mg_context *ctx, const char *name); 0187 0188 0189 // Return array of strings that represent valid configuration options. 0190 // For each option, option name and default value is returned, i.e. the 0191 // number of entries in the array equals to number_of_options x 2. 0192 // Array is NULL terminated. 0193 const char **mg_get_valid_option_names(void); 0194 0195 0196 // Add, edit or delete the entry in the passwords file. 0197 // 0198 // This function allows an application to manipulate .htpasswd files on the 0199 // fly by adding, deleting and changing user records. This is one of the 0200 // several ways of implementing authentication on the server side. For another, 0201 // cookie-based way please refer to the examples/chat.c in the source tree. 0202 // 0203 // If password is not NULL, entry is added (or modified if already exists). 0204 // If password is NULL, entry is deleted. 0205 // 0206 // Return: 0207 // 1 on success, 0 on error. 0208 int mg_modify_passwords_file(const char *passwords_file_name, 0209 const char *domain, 0210 const char *user, 0211 const char *password); 0212 0213 0214 // Return information associated with the request. 0215 struct mg_request_info *mg_get_request_info(struct mg_connection *); 0216 0217 0218 // Send data to the client. 0219 // Return: 0220 // 0 when the connection has been closed 0221 // -1 on error 0222 // >0 number of bytes written on success 0223 int mg_write(struct mg_connection *, const void *buf, size_t len); 0224 0225 0226 // Send data to a websocket client wrapped in a websocket frame. 0227 // It is unsafe to read/write to this connection from another thread. 0228 // This function is available when mongoose is compiled with -DUSE_WEBSOCKET 0229 // 0230 // Return: 0231 // 0 when the connection has been closed 0232 // -1 on error 0233 // >0 number of bytes written on success 0234 int mg_websocket_write(struct mg_connection* conn, int opcode, 0235 const char *data, size_t data_len); 0236 0237 // Opcodes, from http://tools.ietf.org/html/rfc6455 0238 enum { 0239 WEBSOCKET_OPCODE_CONTINUATION = 0x0, 0240 WEBSOCKET_OPCODE_TEXT = 0x1, 0241 WEBSOCKET_OPCODE_BINARY = 0x2, 0242 WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8, 0243 WEBSOCKET_OPCODE_PING = 0x9, 0244 WEBSOCKET_OPCODE_PONG = 0xa 0245 }; 0246 0247 0248 // Macros for enabling compiler-specific checks for printf-like arguments. 0249 #undef PRINTF_FORMAT_STRING 0250 #if defined(_MSC_VER) && _MSC_VER >= 1400 0251 #include <sal.h> 0252 #if defined(_MSC_VER) && _MSC_VER > 1400 0253 #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s 0254 #else 0255 #define PRINTF_FORMAT_STRING(s) __format_string s 0256 #endif 0257 #else 0258 #define PRINTF_FORMAT_STRING(s) s 0259 #endif 0260 0261 #ifdef __GNUC__ 0262 #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y))) 0263 #else 0264 #define PRINTF_ARGS(x, y) 0265 #endif 0266 0267 // Send data to the client using printf() semantics. 0268 // 0269 // Works exactly like mg_write(), but allows to do message formatting. 0270 int mg_printf(struct mg_connection *, 0271 PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3); 0272 #ifdef __rtems__ 0273 struct rtems_printer; 0274 void rtems_print_printer_mg_printf(struct rtems_printer *, struct mg_connection *); 0275 #endif /* __rtems__ */ 0276 0277 0278 // Send contents of the entire file together with HTTP headers. 0279 void mg_send_file(struct mg_connection *conn, const char *path); 0280 0281 0282 // Read data from the remote end, return number of bytes read. 0283 // Return: 0284 // 0 connection has been closed by peer. No more data could be read. 0285 // < 0 read error. No more data could be read from the connection. 0286 // > 0 number of bytes read into the buffer. 0287 int mg_read(struct mg_connection *, void *buf, size_t len); 0288 0289 0290 // Get the value of particular HTTP header. 0291 // 0292 // This is a helper function. It traverses request_info->http_headers array, 0293 // and if the header is present in the array, returns its value. If it is 0294 // not present, NULL is returned. 0295 const char *mg_get_header(const struct mg_connection *, const char *name); 0296 0297 0298 // Get a value of particular form variable. 0299 // 0300 // Parameters: 0301 // data: pointer to form-uri-encoded buffer. This could be either POST data, 0302 // or request_info.query_string. 0303 // data_len: length of the encoded data. 0304 // var_name: variable name to decode from the buffer 0305 // dst: destination buffer for the decoded variable 0306 // dst_len: length of the destination buffer 0307 // 0308 // Return: 0309 // On success, length of the decoded variable. 0310 // On error: 0311 // -1 (variable not found). 0312 // -2 (destination buffer is NULL, zero length or too small to hold the 0313 // decoded variable). 0314 // 0315 // Destination buffer is guaranteed to be '\0' - terminated if it is not 0316 // NULL or zero length. 0317 int mg_get_var(const char *data, size_t data_len, 0318 const char *var_name, char *dst, size_t dst_len); 0319 0320 // Fetch value of certain cookie variable into the destination buffer. 0321 // 0322 // Destination buffer is guaranteed to be '\0' - terminated. In case of 0323 // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same 0324 // parameter. This function returns only first occurrence. 0325 // 0326 // Return: 0327 // On success, value length. 0328 // On error: 0329 // -1 (either "Cookie:" header is not present at all or the requested 0330 // parameter is not found). 0331 // -2 (destination buffer is NULL, zero length or too small to hold the 0332 // value). 0333 int mg_get_cookie(const char *cookie, const char *var_name, 0334 char *buf, size_t buf_len); 0335 0336 0337 // Download data from the remote web server. 0338 // host: host name to connect to, e.g. "foo.com", or "10.12.40.1". 0339 // port: port number, e.g. 80. 0340 // use_ssl: wether to use SSL connection. 0341 // error_buffer, error_buffer_size: error message placeholder. 0342 // request_fmt,...: HTTP request. 0343 // Return: 0344 // On success, valid pointer to the new connection, suitable for mg_read(). 0345 // On error, NULL. error_buffer contains error message. 0346 // Example: 0347 // char ebuf[100]; 0348 // struct mg_connection *conn; 0349 // conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf), 0350 // "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n"); 0351 struct mg_connection *mg_download(const char *host, int port, int use_ssl, 0352 char *error_buffer, size_t error_buffer_size, 0353 PRINTF_FORMAT_STRING(const char *request_fmt), 0354 ...) PRINTF_ARGS(6, 7); 0355 0356 0357 // Close the connection opened by mg_download(). 0358 void mg_close_connection(struct mg_connection *conn); 0359 0360 0361 // File upload functionality. Each uploaded file gets saved into a temporary 0362 // file and MG_UPLOAD event is sent. 0363 // Return number of uploaded files. 0364 int mg_upload(struct mg_connection *conn, const char *destination_dir); 0365 0366 0367 // Convenience function -- create detached thread. 0368 // Return: 0 on success, non-0 on error. 0369 typedef void * (*mg_thread_func_t)(void *); 0370 int mg_start_thread(mg_thread_func_t f, void *p); 0371 0372 0373 // Return builtin mime type for the given file name. 0374 // For unrecognized extensions, "text/plain" is returned. 0375 const char *mg_get_builtin_mime_type(const char *file_name); 0376 0377 0378 // Return Mongoose version. 0379 const char *mg_version(void); 0380 0381 // URL-decode input buffer into destination buffer. 0382 // 0-terminate the destination buffer. 0383 // form-url-encoded data differs from URI encoding in a way that it 0384 // uses '+' as character for space, see RFC 1866 section 8.2.1 0385 // http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt 0386 // Return: length of the decoded data, or -1 if dst buffer is too small. 0387 int mg_url_decode(const char *src, int src_len, char *dst, 0388 int dst_len, int is_form_url_encoded); 0389 0390 // MD5 hash given strings. 0391 // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of 0392 // ASCIIz strings. When function returns, buf will contain human-readable 0393 // MD5 hash. Example: 0394 // char buf[33]; 0395 // mg_md5(buf, "aa", "bb", NULL); 0396 char *mg_md5(char buf[33], ...); 0397 0398 0399 #ifdef __cplusplus 0400 } 0401 #endif // __cplusplus 0402 0403 #endif // MONGOOSE_HEADER_INCLUDED
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |