34
35
// This structure contains information about the HTTP request.
35
36
struct mg_request_info {
36
void *user_data; // User-defined pointer passed to mg_start()
37
37
char *request_method; // "GET", "POST", etc
38
38
char *uri; // URL-decoded URI
39
39
char *http_version; // E.g. "1.0", "1.1"
40
40
char *query_string; // URL part after '?' (not including '?') or NULL
41
41
char *remote_user; // Authenticated user, or NULL if no auth used
42
char *log_message; // Mongoose error log message, MG_EVENT_LOG only
43
42
long remote_ip; // Client's IP address
44
43
int remote_port; // Client's port
45
int status_code; // HTTP reply status code, e.g. 200
46
44
int is_ssl; // 1 if SSL-ed, 0 if not
47
45
int num_headers; // Number of headers
51
49
} http_headers[64]; // Maximum 64 headers
54
53
// Various events on which user-defined function is called by Mongoose.
56
MG_NEW_REQUEST, // New HTTP request has arrived from the client
57
MG_HTTP_ERROR, // HTTP error must be returned to the client
58
MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
59
MG_INIT_SSL, // Mongoose initializes SSL. Instead of mg_connection *,
60
// SSL context is passed to the callback function.
61
MG_REQUEST_COMPLETE // Mongoose has finished handling the request
55
MG_NEW_REQUEST, // New HTTP request has arrived from the client
56
MG_REQUEST_COMPLETE, // Mongoose has finished handling the request
57
MG_HTTP_ERROR, // HTTP error must be returned to the client
58
MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
59
MG_INIT_SSL, // SSL initialization, sent before certificate setup
60
MG_WEBSOCKET_CONNECT, // Sent on HTTP connect, before websocket handshake.
61
// If user callback returns NULL, then mongoose proceeds
62
// with handshake, otherwise it closes the connection.
63
MG_WEBSOCKET_READY, // Handshake has been successfully completed.
64
MG_WEBSOCKET_MESSAGE, // Incoming message from the client
65
MG_WEBSOCKET_CLOSE, // Client has closed the connection
64
69
// Prototype for the user-defined function. Mongoose calls this function
65
70
// on every MG_* event.
77
81
// If handler returns NULL, that means that handler has not processed
78
82
// the request. Handler must not send any data to the client in this case.
79
83
// Mongoose proceeds with request handling as if nothing happened.
80
typedef void * (*mg_callback_t)(enum mg_event event,
81
struct mg_connection *conn,
82
const struct mg_request_info *request_info);
84
typedef void *(*mg_callback_t)(enum mg_event event, struct mg_connection *conn);
85
87
// Start web server.
151
153
const char *user,
152
154
const char *password);
157
// Return information associated with the request.
158
// These functions always succeed.
159
const struct mg_request_info *mg_get_request_info(const struct mg_connection *);
160
void *mg_get_user_data(struct mg_connection *);
161
const char *mg_get_log_message(const struct mg_connection *);
162
int mg_get_reply_status_code(const struct mg_connection *);
163
void *mg_get_ssl_context(const struct mg_connection *);
154
166
// Send data to the client.
168
// 0 when the connection has been closed
170
// number of bytes written on success
155
171
int mg_write(struct mg_connection *, const void *buf, size_t len);
158
174
// Send data to the browser using printf() semantics.
160
176
// Works exactly like mg_write(), but allows to do message formatting.
161
// Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
162
// (8 Kb by default) as temporary message storage for formatting. Do not
163
// print data that is bigger than that, otherwise it will be truncated.
164
int mg_printf(struct mg_connection *, const char *fmt, ...)
177
// Below are the macros for enabling compiler-specific checks for
178
// printf-like arguments.
180
#undef PRINTF_FORMAT_STRING
184
#define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
186
#define PRINTF_FORMAT_STRING(s) __format_string s
189
#define PRINTF_FORMAT_STRING(s) s
166
__attribute__((format(printf, 2, 3)))
193
#define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
195
#define PRINTF_ARGS(x, y)
198
int mg_printf(struct mg_connection *,
199
PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
171
202
// Send contents of the entire file together with HTTP headers.
198
229
// On success, length of the decoded variable.
199
// On error, -1 (variable not found, or destination buffer is too small).
231
// -1 (variable not found, or destination buffer is too small).
232
// -2 (destination buffer is NULL or zero length).
201
// Destination buffer is guaranteed to be '\0' - terminated. In case of
202
// failure, dst[0] == '\0'.
234
// Destination buffer is guaranteed to be '\0' - terminated if it is not
235
// NULL or zero length. In case of failure, dst[0] == '\0'.
203
236
int mg_get_var(const char *data, size_t data_len,
204
237
const char *var_name, char *buf, size_t buf_len);
213
246
// On success, value length.
214
// On error, 0 (either "Cookie:" header is not present at all, or the
247
// On error, -1 (either "Cookie:" header is not present at all, or the
215
248
// requested parameter is not found, or destination buffer is too small
216
249
// to hold the value).
217
250
int mg_get_cookie(const struct mg_connection *,
218
251
const char *cookie_name, char *buf, size_t buf_len);
254
// Connect to the remote web server.
256
// On success, valid pointer to the new connection
258
struct mg_connection *mg_connect(struct mg_context *ctx,
259
const char *host, int port, int use_ssl);
262
// Close the connection opened by mg_connect().
263
void mg_close_connection(struct mg_connection *conn);
266
// Download given URL to a given file.
267
// url: URL to download
268
// path: file name where to save the data
269
// request_info: pointer to a structure that will hold parsed reply headers
270
// buf, bul_len: a buffer for the reply headers
273
// On success, opened file stream to the downloaded contents. The stream
274
// is positioned to the end of the file. It is the user's responsibility
275
// to fclose() the opened file stream.
276
FILE *mg_fetch(struct mg_context *ctx, const char *url, const char *path,
277
char *buf, size_t buf_len, struct mg_request_info *request_info);
280
// Convenience function -- create detached thread.
281
// Return: 0 on success, non-0 on error.
282
typedef void * (*mg_thread_func_t)(void *);
283
int mg_start_thread(mg_thread_func_t f, void *p);
286
// Return builtin mime type for the given file name.
287
// For unrecognized extensions, "text/plain" is returned.
288
const char *mg_get_builtin_mime_type(const char *file_name);
221
291
// Return Mongoose version.
222
292
const char *mg_version(void);
225
295
// MD5 hash given strings.
226
296
// Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
227
// asciiz strings. When function returns, buf will contain human-readable
297
// ASCIIz strings. When function returns, buf will contain human-readable
228
298
// MD5 hash. Example:
230
300
// mg_md5(buf, "aa", "bb", NULL);
231
void mg_md5(char *buf, ...);
301
void mg_md5(char buf[33], ...);
234
304
#ifdef __cplusplus