1 // Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> 2 // Copyright (c) 2013-2014 Cesanta Software Limited 3 // All rights reserved 4 // 5 // This software is dual-licensed: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License version 2 as 7 // published by the Free Software Foundation. For the terms of this 8 // license, see <http://www.gnu.org/licenses/>. 9 // 10 // You are free to use this software under the terms of the GNU General 11 // Public License, but WITHOUT ANY WARRANTY; without even the implied 12 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 // See the GNU General Public License for more details. 14 // 15 // Alternatively, you can license this software under a commercial 16 // license, as set out in <http://cesanta.com/>. 17 18 #ifndef MONGOOSE_HEADER_INCLUDED 19 #define MONGOOSE_HEADER_INCLUDED 20 21 #define MONGOOSE_VERSION "5.6" 22 23 #include <stdio.h> // required for FILE 24 #include <stddef.h> // required for size_t 25 #include <sys/types.h> // required for time_t 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif // __cplusplus 30 31 // This structure contains information about HTTP request. 32 struct mg_connection { 33 const char *request_method; // "GET", "POST", etc 34 const char *uri; // URL-decoded URI 35 const char *http_version; // E.g. "1.0", "1.1" 36 const char *query_string; // URL part after '?', not including '?', or NULL 37 38 char remote_ip[48]; // Max IPv6 string length is 45 characters 39 char local_ip[48]; // Local IP address 40 unsigned short remote_port; // Client's port 41 unsigned short local_port; // Local port number 42 43 int num_headers; // Number of HTTP headers 44 struct mg_header { 45 const char *name; // HTTP header name 46 const char *value; // HTTP header value 47 } http_headers[30]; 48 49 char *content; // POST (or websocket message) data, or NULL 50 size_t content_len; // Data length 51 52 int is_websocket; // Connection is a websocket connection 53 int status_code; // HTTP status code for HTTP error handler 54 int wsbits; // First byte of the websocket frame 55 void *server_param; // Parameter passed to mg_create_server() 56 void *connection_param; // Placeholder for connection-specific data 57 void *callback_param; 58 }; 59 60 struct mg_server; // Opaque structure describing server instance 61 enum mg_result { MG_FALSE, MG_TRUE, MG_MORE }; 62 enum mg_event { 63 MG_POLL = 100, // Callback return value is ignored 64 MG_CONNECT, // If callback returns MG_FALSE, connect fails 65 MG_AUTH, // If callback returns MG_FALSE, authentication fails 66 MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req 67 MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection 68 MG_RECV, // Mongoose has received POST data chunk. 69 // Callback should return a number of bytes to discard from 70 // the receive buffer, or -1 to close the connection. 71 MG_CLOSE, // Connection is closed, callback return value is ignored 72 MG_WS_HANDSHAKE, // New websocket connection, handshake request 73 MG_WS_CONNECT, // New websocket connection established 74 MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err 75 }; 76 typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event); 77 78 // Websocket opcodes, from http://tools.ietf.org/html/rfc6455 79 enum { 80 WEBSOCKET_OPCODE_CONTINUATION = 0x0, 81 WEBSOCKET_OPCODE_TEXT = 0x1, 82 WEBSOCKET_OPCODE_BINARY = 0x2, 83 WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8, 84 WEBSOCKET_OPCODE_PING = 0x9, 85 WEBSOCKET_OPCODE_PONG = 0xa 86 }; 87 88 // Server management functions 89 struct mg_server *mg_create_server(void *server_param, mg_handler_t handler); 90 void mg_destroy_server(struct mg_server **); 91 const char *mg_set_option(struct mg_server *, const char *opt, const char *val); 92 time_t mg_poll_server(struct mg_server *, int milliseconds); 93 const char **mg_get_valid_option_names(void); 94 const char *mg_get_option(const struct mg_server *server, const char *name); 95 void mg_copy_listeners(struct mg_server *from, struct mg_server *to); 96 struct mg_connection *mg_next(struct mg_server *, struct mg_connection *); 97 void mg_wakeup_server(struct mg_server *); 98 void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...); 99 struct mg_connection *mg_connect(struct mg_server *, const char *); 100 101 // Connection management functions 102 void mg_send_status(struct mg_connection *, int status_code); 103 void mg_send_header(struct mg_connection *, const char *name, const char *val); 104 size_t mg_send_data(struct mg_connection *, const void *data, int data_len); 105 size_t mg_printf_data(struct mg_connection *, const char *format, ...); 106 size_t mg_write(struct mg_connection *, const void *buf, size_t len); 107 size_t mg_printf(struct mg_connection *conn, const char *fmt, ...); 108 109 size_t mg_websocket_write(struct mg_connection *, int opcode, 110 const char *data, size_t data_len); 111 size_t mg_websocket_printf(struct mg_connection* conn, int opcode, 112 const char *fmt, ...); 113 114 void mg_send_file(struct mg_connection *, const char *path, const char *); 115 void mg_send_file_data(struct mg_connection *, int fd); 116 117 const char *mg_get_header(const struct mg_connection *, const char *name); 118 const char *mg_get_mime_type(const char *name, const char *default_mime_type); 119 int mg_get_var(const struct mg_connection *conn, const char *var_name, 120 char *buf, size_t buf_len); 121 int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); 122 int mg_parse_multipart(const char *buf, int buf_len, 123 char *var_name, int var_name_len, 124 char *file_name, int file_name_len, 125 const char **data, int *data_len); 126 127 128 // Utility functions 129 void *mg_start_thread(void *(*func)(void *), void *param); 130 char *mg_md5(char buf[33], ...); 131 int mg_authorize_digest(struct mg_connection *c, FILE *fp); 132 size_t mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len); 133 int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len, int); 134 int mg_terminate_ssl(struct mg_connection *c, const char *cert); 135 int mg_forward(struct mg_connection *c, const char *addr); 136 void *mg_mmap(FILE *fp, size_t size); 137 void mg_munmap(void *p, size_t size); 138 139 140 // Templates support 141 struct mg_expansion { 142 const char *keyword; 143 void (*handler)(struct mg_connection *); 144 }; 145 void mg_template(struct mg_connection *, const char *text, 146 struct mg_expansion *expansions); 147 148 #ifdef __cplusplus 149 } 150 #endif // __cplusplus 151 152 #endif // MONGOOSE_HEADER_INCLUDED 153