1 // Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
2 // Copyright (c) 2013-2014 Cesanta Software Limited
5 // This library 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/>.
10 // You are free to use this library 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.
15 // Alternatively, you can license this library under a commercial
16 // license, as set out in <http://cesanta.com/>.
18 // $Date: 2014-09-09 17:07:55 UTC $
20 #ifndef MONGOOSE_HEADER_INCLUDED
21 #define MONGOOSE_HEADER_INCLUDED
23 #define MONGOOSE_VERSION "5.5"
25 #include <stdio.h> // required for FILE
26 #include <stddef.h> // required for size_t
32 // This structure contains information about HTTP request.
33 struct mg_connection {
34 const char *request_method; // "GET", "POST", etc
35 const char *uri; // URL-decoded URI
36 const char *http_version; // E.g. "1.0", "1.1"
37 const char *query_string; // URL part after '?', not including '?', or NULL
39 char remote_ip[48]; // Max IPv6 string length is 45 characters
40 char local_ip[48]; // Local IP address
41 unsigned short remote_port; // Client's port
42 unsigned short local_port; // Local port number
44 int num_headers; // Number of HTTP headers
46 const char *name; // HTTP header name
47 const char *value; // HTTP header value
50 char *content; // POST (or websocket message) data, or NULL
51 size_t content_len; // Data length
53 int is_websocket; // Connection is a websocket connection
54 int status_code; // HTTP status code for HTTP error handler
55 int wsbits; // First byte of the websocket frame
56 void *server_param; // Parameter passed to mg_add_uri_handler()
57 void *connection_param; // Placeholder for connection-specific data
61 struct mg_server; // Opaque structure describing server instance
62 enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
64 MG_POLL = 100, // Callback return value is ignored
65 MG_CONNECT, // If callback returns MG_FALSE, connect fails
66 MG_AUTH, // If callback returns MG_FALSE, authentication fails
67 MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
68 MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
69 MG_RECV, // Mongoose has received POST data chunk.
70 // Callback should return a number of bytes to discard from
71 // the receive buffer, or -1 to close the connection.
72 MG_CLOSE, // Connection is closed, callback return value is ignored
73 MG_WS_HANDSHAKE, // New websocket connection, handshake request
74 MG_WS_CONNECT, // New websocket connection established
75 MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
77 typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
79 // Websocket opcodes, from http://tools.ietf.org/html/rfc6455
81 WEBSOCKET_OPCODE_CONTINUATION = 0x0,
82 WEBSOCKET_OPCODE_TEXT = 0x1,
83 WEBSOCKET_OPCODE_BINARY = 0x2,
84 WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
85 WEBSOCKET_OPCODE_PING = 0x9,
86 WEBSOCKET_OPCODE_PONG = 0xa
89 // Server management functions
90 struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
91 void mg_destroy_server(struct mg_server **);
92 const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
93 int mg_poll_server(struct mg_server *, int milliseconds);
94 const char **mg_get_valid_option_names(void);
95 const char *mg_get_option(const struct mg_server *server, const char *name);
96 void mg_copy_listeners(struct mg_server *from, struct mg_server *to);
97 struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
98 void mg_wakeup_server(struct mg_server *);
99 void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
100 struct mg_connection *mg_connect(struct mg_server *, const char *);
102 // Connection management functions
103 void mg_send_status(struct mg_connection *, int status_code);
104 void mg_send_header(struct mg_connection *, const char *name, const char *val);
105 size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
106 size_t mg_printf_data(struct mg_connection *, const char *format, ...);
107 size_t mg_write(struct mg_connection *, const void *buf, int len);
108 size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
110 size_t mg_websocket_write(struct mg_connection *, int opcode,
111 const char *data, size_t data_len);
112 size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
113 const char *fmt, ...);
115 void mg_send_file(struct mg_connection *, const char *path, const char *);
116 void mg_send_file_data(struct mg_connection *, int fd);
118 const char *mg_get_header(const struct mg_connection *, const char *name);
119 const char *mg_get_mime_type(const char *name, const char *default_mime_type);
120 int mg_get_var(const struct mg_connection *conn, const char *var_name,
121 char *buf, size_t buf_len);
122 int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
123 int mg_parse_multipart(const char *buf, int buf_len,
124 char *var_name, int var_name_len,
125 char *file_name, int file_name_len,
126 const char **data, int *data_len);
130 void *mg_start_thread(void *(*func)(void *), void *param);
131 char *mg_md5(char buf[33], ...);
132 int mg_authorize_digest(struct mg_connection *c, FILE *fp);
133 int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
134 int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int);
135 int mg_terminate_ssl(struct mg_connection *c, const char *cert);
136 int mg_forward(struct mg_connection *c, const char *addr);
137 void *mg_mmap(FILE *fp, size_t size);
138 void mg_munmap(void *p, size_t size);
142 struct mg_expansion {
144 void (*handler)(struct mg_connection *);
146 void mg_template(struct mg_connection *, const char *text,
147 struct mg_expansion *expansions);
151 #endif // __cplusplus
153 #endif // MONGOOSE_HEADER_INCLUDED