2013-08-16 18:18:16 +08:00
|
|
|
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
|
2014-01-17 19:45:57 +08:00
|
|
|
// Copyright (c) 2013-2014 Cesanta Software Limited
|
2013-08-16 18:18:16 +08:00
|
|
|
// All rights reserved
|
2010-07-07 06:15:14 +08:00
|
|
|
//
|
2013-08-16 18:18:16 +08:00
|
|
|
// This library is dual-licensed: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2 as
|
|
|
|
// published by the Free Software Foundation. For the terms of this
|
|
|
|
// license, see <http://www.gnu.org/licenses/>.
|
2010-07-07 06:15:14 +08:00
|
|
|
//
|
2013-08-16 18:18:16 +08:00
|
|
|
// You are free to use this library under the terms of the GNU General
|
|
|
|
// Public License, but WITHOUT ANY WARRANTY; without even the implied
|
|
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU General Public License for more details.
|
2010-07-07 06:15:14 +08:00
|
|
|
//
|
2013-08-16 18:18:16 +08:00
|
|
|
// Alternatively, you can license this library under a commercial
|
2014-01-20 16:55:03 +08:00
|
|
|
// license, as set out in <http://cesanta.com/>.
|
2013-12-09 22:18:49 +08:00
|
|
|
//
|
2014-01-20 16:55:03 +08:00
|
|
|
// NOTE: Detailed API documentation is at http://cesanta.com/#docs
|
2010-05-04 04:46:42 +08:00
|
|
|
|
|
|
|
#ifndef MONGOOSE_HEADER_INCLUDED
|
2010-07-07 06:15:14 +08:00
|
|
|
#define MONGOOSE_HEADER_INCLUDED
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2014-03-11 01:02:00 +08:00
|
|
|
#define MONGOOSE_VERSION "5.4"
|
2013-12-09 22:18:49 +08:00
|
|
|
|
|
|
|
#include <stdio.h> // required for FILE
|
|
|
|
#include <stddef.h> // required for size_t
|
2011-05-02 16:09:58 +08:00
|
|
|
|
2010-05-04 04:46:42 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
2010-07-07 06:15:14 +08:00
|
|
|
#endif // __cplusplus
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2013-12-09 22:18:49 +08:00
|
|
|
// This structure contains information about HTTP request.
|
|
|
|
struct mg_connection {
|
2012-09-28 04:48:05 +08:00
|
|
|
const char *request_method; // "GET", "POST", etc
|
|
|
|
const char *uri; // URL-decoded URI
|
|
|
|
const char *http_version; // E.g. "1.0", "1.1"
|
|
|
|
const char *query_string; // URL part after '?', not including '?', or NULL
|
2013-12-09 22:18:49 +08:00
|
|
|
|
|
|
|
char remote_ip[48]; // Max IPv6 string length is 45 characters
|
2014-03-31 05:50:10 +08:00
|
|
|
char local_ip[48]; // Local IP address
|
2014-01-27 19:08:32 +08:00
|
|
|
unsigned short remote_port; // Client's port
|
|
|
|
unsigned short local_port; // Local port number
|
2013-02-02 00:48:30 +08:00
|
|
|
|
|
|
|
int num_headers; // Number of HTTP headers
|
2010-07-07 06:15:14 +08:00
|
|
|
struct mg_header {
|
2012-09-28 04:48:05 +08:00
|
|
|
const char *name; // HTTP header name
|
|
|
|
const char *value; // HTTP header value
|
2013-12-09 22:18:49 +08:00
|
|
|
} http_headers[30];
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2013-12-09 22:18:49 +08:00
|
|
|
char *content; // POST (or websocket message) data, or NULL
|
2014-03-25 00:01:45 +08:00
|
|
|
size_t content_len; // Data length
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2013-12-09 22:18:49 +08:00
|
|
|
int is_websocket; // Connection is a websocket connection
|
|
|
|
int status_code; // HTTP status code for HTTP error handler
|
2014-01-20 00:32:43 +08:00
|
|
|
int wsbits; // First byte of the websocket frame
|
2013-12-09 22:18:49 +08:00
|
|
|
void *server_param; // Parameter passed to mg_add_uri_handler()
|
|
|
|
void *connection_param; // Placeholder for connection-specific data
|
2014-03-25 00:01:45 +08:00
|
|
|
void *callback_param; // Needed by mg_iterate_over_connections()
|
2013-07-12 19:25:25 +08:00
|
|
|
};
|
|
|
|
|
2013-12-09 22:18:49 +08:00
|
|
|
struct mg_server; // Opaque structure describing server instance
|
2014-03-05 04:22:46 +08:00
|
|
|
enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
|
2014-03-01 09:17:39 +08:00
|
|
|
enum mg_event {
|
2014-03-03 18:26:27 +08:00
|
|
|
MG_POLL = 100, // Callback return value is ignored
|
|
|
|
MG_CONNECT, // If callback returns MG_FALSE, connect fails
|
2014-03-01 09:17:39 +08:00
|
|
|
MG_AUTH, // If callback returns MG_FALSE, authentication fails
|
2014-03-03 18:26:27 +08:00
|
|
|
MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
|
|
|
|
MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
|
2014-03-26 22:13:30 +08:00
|
|
|
MG_CLOSE, // Connection is closed, callback return value is ignored
|
2014-04-09 18:55:36 +08:00
|
|
|
MG_WS_HANDSHAKE, // New websocket connection, handshake request
|
2014-06-09 23:32:56 +08:00
|
|
|
MG_WS_CONNECT, // New websocket connection established
|
2014-03-01 09:17:39 +08:00
|
|
|
MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
|
|
|
|
};
|
|
|
|
typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
|
2013-12-09 22:18:49 +08:00
|
|
|
|
2014-04-09 22:16:03 +08:00
|
|
|
// Websocket opcodes, from http://tools.ietf.org/html/rfc6455
|
|
|
|
enum {
|
|
|
|
WEBSOCKET_OPCODE_CONTINUATION = 0x0,
|
|
|
|
WEBSOCKET_OPCODE_TEXT = 0x1,
|
|
|
|
WEBSOCKET_OPCODE_BINARY = 0x2,
|
|
|
|
WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
|
|
|
|
WEBSOCKET_OPCODE_PING = 0x9,
|
|
|
|
WEBSOCKET_OPCODE_PONG = 0xa
|
|
|
|
};
|
|
|
|
|
2013-12-09 22:18:49 +08:00
|
|
|
// Server management functions
|
2014-03-01 09:17:39 +08:00
|
|
|
struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
|
2013-12-09 22:18:49 +08:00
|
|
|
void mg_destroy_server(struct mg_server **);
|
|
|
|
const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
|
2014-02-20 00:34:51 +08:00
|
|
|
int mg_poll_server(struct mg_server *, int milliseconds);
|
2013-10-31 07:02:15 +08:00
|
|
|
const char **mg_get_valid_option_names(void);
|
2013-12-09 22:18:49 +08:00
|
|
|
const char *mg_get_option(const struct mg_server *server, const char *name);
|
2013-12-22 04:52:54 +08:00
|
|
|
void mg_set_listening_socket(struct mg_server *, int sock);
|
|
|
|
int mg_get_listening_socket(struct mg_server *);
|
2014-03-25 00:01:45 +08:00
|
|
|
void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *);
|
2014-06-10 00:24:51 +08:00
|
|
|
struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
|
2014-03-01 09:47:01 +08:00
|
|
|
void mg_wakeup_server(struct mg_server *);
|
2014-05-13 06:22:57 +08:00
|
|
|
void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
|
2014-03-03 18:56:17 +08:00
|
|
|
struct mg_connection *mg_connect(struct mg_server *, const char *, int, int);
|
2013-12-09 22:18:49 +08:00
|
|
|
|
|
|
|
// Connection management functions
|
2014-01-04 19:31:55 +08:00
|
|
|
void mg_send_status(struct mg_connection *, int status_code);
|
|
|
|
void mg_send_header(struct mg_connection *, const char *name, const char *val);
|
2014-06-04 01:02:55 +08:00
|
|
|
size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
|
|
|
|
size_t mg_printf_data(struct mg_connection *, const char *format, ...);
|
|
|
|
size_t mg_write(struct mg_connection *, const void *buf, int len);
|
|
|
|
size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
|
2014-01-04 19:31:55 +08:00
|
|
|
|
2014-06-04 01:02:55 +08:00
|
|
|
size_t mg_websocket_write(struct mg_connection *, int opcode,
|
|
|
|
const char *data, size_t data_len);
|
|
|
|
size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
|
|
|
|
const char *fmt, ...);
|
2014-06-04 01:45:37 +08:00
|
|
|
|
|
|
|
void mg_send_file(struct mg_connection *, const char *path);
|
2013-07-12 19:25:25 +08:00
|
|
|
|
2010-05-04 04:46:42 +08:00
|
|
|
const char *mg_get_header(const struct mg_connection *, const char *name);
|
2014-01-28 20:42:13 +08:00
|
|
|
const char *mg_get_mime_type(const char *name, const char *default_mime_type);
|
2013-12-09 22:18:49 +08:00
|
|
|
int mg_get_var(const struct mg_connection *conn, const char *var_name,
|
|
|
|
char *buf, size_t buf_len);
|
|
|
|
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
|
2014-01-14 00:29:12 +08:00
|
|
|
int mg_parse_multipart(const char *buf, int buf_len,
|
|
|
|
char *var_name, int var_name_len,
|
|
|
|
char *file_name, int file_name_len,
|
|
|
|
const char **data, int *data_len);
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2013-12-09 22:18:49 +08:00
|
|
|
// Utility functions
|
|
|
|
void *mg_start_thread(void *(*func)(void *), void *param);
|
2013-03-27 16:31:12 +08:00
|
|
|
char *mg_md5(char buf[33], ...);
|
2014-01-12 00:44:37 +08:00
|
|
|
int mg_authorize_digest(struct mg_connection *c, FILE *fp);
|
2014-04-26 01:04:24 +08:00
|
|
|
int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
|
|
|
|
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int);
|
2010-05-05 07:39:36 +08:00
|
|
|
|
2014-04-26 01:04:24 +08:00
|
|
|
// Templates support
|
2014-03-30 19:45:48 +08:00
|
|
|
struct mg_expansion {
|
|
|
|
const char *keyword;
|
|
|
|
void (*handler)(struct mg_connection *);
|
|
|
|
};
|
|
|
|
void mg_template(struct mg_connection *, const char *text,
|
|
|
|
struct mg_expansion *expansions);
|
|
|
|
|
2014-03-10 04:09:29 +08:00
|
|
|
|
2010-05-04 04:46:42 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2010-07-07 06:15:14 +08:00
|
|
|
#endif // __cplusplus
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2010-07-07 06:15:14 +08:00
|
|
|
#endif // MONGOOSE_HEADER_INCLUDED
|