23 #ifndef CIVETWEB_HEADER_INCLUDED
24 #define CIVETWEB_HEADER_INCLUDED
26 #define CIVETWEB_VERSION "1.12"
27 #define CIVETWEB_VERSION_MAJOR (1)
28 #define CIVETWEB_VERSION_MINOR (12)
29 #define CIVETWEB_VERSION_PATCH (0)
33 #if defined(CIVETWEB_DLL_EXPORTS)
34 #define CIVETWEB_API __declspec(dllexport)
35 #elif defined(CIVETWEB_DLL_IMPORTS)
36 #define CIVETWEB_API __declspec(dllimport)
41 #define CIVETWEB_API __attribute__((visibility("default")))
57 MG_FEATURES_DEFAULT = 0x0u,
61 MG_FEATURES_FILES = 0x1u,
66 MG_FEATURES_TLS = 0x2u,
67 MG_FEATURES_SSL = 0x2u,
71 MG_FEATURES_CGI = 0x4u,
75 MG_FEATURES_IPV6 = 0x8u,
79 MG_FEATURES_WEBSOCKET = 0x10u,
83 MG_FEATURES_LUA = 0x20u,
87 MG_FEATURES_SSJS = 0x40u,
91 MG_FEATURES_CACHE = 0x80u,
95 MG_FEATURES_STATS = 0x100u,
99 MG_FEATURES_COMPRESSION = 0x200u,
103 MG_FEATURES_ALL = 0xFFFFu
121 CIVETWEB_API
unsigned mg_init_library(
unsigned features);
128 CIVETWEB_API
unsigned mg_exit_library(
void);
132 struct mg_connection;
136 #define MG_MAX_HEADERS (64)
145 struct mg_request_info {
146 const char *request_method;
147 const char *request_uri;
149 const char *local_uri;
152 #if defined(MG_LEGACY_INTERFACE)
155 const char *http_version;
156 const char *query_string;
158 const char *remote_user;
160 char remote_addr[48];
162 long long content_length;
171 http_headers[MG_MAX_HEADERS];
173 struct mg_client_cert *client_cert;
175 const char *acceptedWebSocketSubprotocol;
182 struct mg_response_info {
184 const char *status_text;
185 const char *http_version;
187 long long content_length;
192 http_headers[MG_MAX_HEADERS];
198 struct mg_client_cert {
206 #if defined(MG_LEGACY_INTERFACE)
220 struct mg_callbacks {
235 int (*begin_request)(
struct mg_connection *);
238 void (*end_request)(
const struct mg_connection *,
int reply_status_code);
242 int (*log_message)(
const struct mg_connection *,
const char *message);
246 int (*log_access)(
const struct mg_connection *,
const char *message);
255 int (*init_ssl)(
void *ssl_context,
void *user_data);
269 int (*external_ssl_ctx)(
void **ssl_ctx,
void *user_data);
271 #if defined(MG_LEGACY_INTERFACE)
277 int (*websocket_connect)(
const struct mg_connection *);
282 void (*websocket_ready)(
struct mg_connection *);
293 int (*websocket_data)(
struct mg_connection *,
314 void (*connection_close)(
const struct mg_connection *);
321 void (*init_lua)(
const struct mg_connection *conn,
void *lua_context);
323 #if defined(MG_LEGACY_INTERFACE)
329 void (*upload)(
struct mg_connection *,
const char *file_name);
341 int (*http_error)(
struct mg_connection *conn,
349 void (*init_context)(
const struct mg_context *ctx);
359 void (*init_thread)(
const struct mg_context *ctx,
int thread_type);
364 void (*exit_context)(
const struct mg_context *ctx);
379 int (*init_connection)(
const struct mg_connection *conn,
void **conn_data);
408 CIVETWEB_API
struct mg_context *mg_start(
const struct mg_callbacks *callbacks,
410 const char **configuration_options);
418 CIVETWEB_API
void mg_stop(
struct mg_context *);
421 #if defined(MG_EXPERIMENTAL_INTERFACES)
439 CIVETWEB_API
int mg_start_domain(
struct mg_context *ctx,
440 const char **configuration_options);
456 typedef int (*mg_request_handler)(
struct mg_connection *conn,
void *cbdata);
479 CIVETWEB_API
void mg_set_request_handler(
struct mg_context *ctx,
481 mg_request_handler handler,
510 typedef int (*mg_websocket_connect_handler)(
const struct mg_connection *,
512 typedef void (*mg_websocket_ready_handler)(
struct mg_connection *,
void *);
513 typedef int (*mg_websocket_data_handler)(
struct mg_connection *,
518 typedef void (*mg_websocket_close_handler)(
const struct mg_connection *,
525 struct mg_websocket_subprotocols {
535 mg_set_websocket_handler(
struct mg_context *ctx,
537 mg_websocket_connect_handler connect_handler,
538 mg_websocket_ready_handler ready_handler,
539 mg_websocket_data_handler data_handler,
540 mg_websocket_close_handler close_handler,
547 CIVETWEB_API
void mg_set_websocket_handler_with_subprotocols(
548 struct mg_context *ctx,
550 struct mg_websocket_subprotocols *subprotocols,
551 mg_websocket_connect_handler connect_handler,
552 mg_websocket_ready_handler ready_handler,
553 mg_websocket_data_handler data_handler,
554 mg_websocket_close_handler close_handler,
569 typedef int (*mg_authorization_handler)(
struct mg_connection *conn,
577 CIVETWEB_API
void mg_set_auth_handler(
struct mg_context *ctx,
579 mg_authorization_handler handler,
589 CIVETWEB_API
const char *mg_get_option(
const struct mg_context *ctx,
594 CIVETWEB_API
struct mg_context *
595 mg_get_context(
const struct mg_connection *conn);
599 CIVETWEB_API
void *mg_get_user_data(
const struct mg_context *ctx);
611 CIVETWEB_API
void mg_set_user_connection_data(
struct mg_connection *conn,
617 mg_get_user_connection_data(
const struct mg_connection *conn);
630 mg_get_request_link(
const struct mg_connection *conn,
char *buf,
size_t buflen);
633 #if defined(MG_LEGACY_INTERFACE)
639 CIVETWEB_API
const char **mg_get_valid_option_names(
void);
646 const char *default_value;
650 #if defined(MG_LEGACY_INTERFACE)
652 CONFIG_TYPE_UNKNOWN = 0x0,
653 CONFIG_TYPE_NUMBER = 0x1,
654 CONFIG_TYPE_STRING = 0x2,
655 CONFIG_TYPE_FILE = 0x3,
656 CONFIG_TYPE_DIRECTORY = 0x4,
657 CONFIG_TYPE_BOOLEAN = 0x5,
658 CONFIG_TYPE_EXT_PATTERN = 0x6,
659 CONFIG_TYPE_STRING_LIST = 0x7,
660 CONFIG_TYPE_STRING_MULTILINE = 0x8
666 MG_CONFIG_TYPE_UNKNOWN = 0x0,
667 MG_CONFIG_TYPE_NUMBER = 0x1,
668 MG_CONFIG_TYPE_STRING = 0x2,
669 MG_CONFIG_TYPE_FILE = 0x3,
670 MG_CONFIG_TYPE_DIRECTORY = 0x4,
671 MG_CONFIG_TYPE_BOOLEAN = 0x5,
672 MG_CONFIG_TYPE_EXT_PATTERN = 0x6,
673 MG_CONFIG_TYPE_STRING_LIST = 0x7,
674 MG_CONFIG_TYPE_STRING_MULTILINE = 0x8,
675 MG_CONFIG_TYPE_YES_NO_OPTIONAL = 0x9
681 CIVETWEB_API
const struct mg_option *mg_get_valid_options(
void);
684 struct mg_server_ports {
701 CIVETWEB_API
int mg_get_server_ports(
const struct mg_context *ctx,
703 struct mg_server_ports *ports);
706 #if defined(MG_LEGACY_INTERFACE)
708 CIVETWEB_API
size_t mg_get_ports(
const struct mg_context *ctx,
733 CIVETWEB_API
int mg_modify_passwords_file(
const char *passwords_file_name,
736 const char *password);
747 CIVETWEB_API
const struct mg_request_info *
748 mg_get_request_info(
const struct mg_connection *);
754 CIVETWEB_API
const struct mg_response_info *
755 mg_get_response_info(
const struct mg_connection *);
763 CIVETWEB_API
int mg_write(
struct mg_connection *,
const void *buf,
size_t len);
778 CIVETWEB_API
int mg_websocket_write(
struct mg_connection *conn,
796 CIVETWEB_API
int mg_websocket_client_write(
struct mg_connection *conn,
807 CIVETWEB_API
void mg_lock_connection(
struct mg_connection *conn);
808 CIVETWEB_API
void mg_unlock_connection(
struct mg_connection *conn);
811 #if defined(MG_LEGACY_INTERFACE)
812 #define mg_lock mg_lock_connection
813 #define mg_unlock mg_unlock_connection
819 CIVETWEB_API
void mg_lock_context(
struct mg_context *ctx);
820 CIVETWEB_API
void mg_unlock_context(
struct mg_context *ctx);
824 #if defined(MG_LEGACY_INTERFACE)
826 WEBSOCKET_OPCODE_CONTINUATION = 0x0,
827 WEBSOCKET_OPCODE_TEXT = 0x1,
828 WEBSOCKET_OPCODE_BINARY = 0x2,
829 WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
830 WEBSOCKET_OPCODE_PING = 0x9,
831 WEBSOCKET_OPCODE_PONG = 0xa
837 MG_WEBSOCKET_OPCODE_CONTINUATION = 0x0,
838 MG_WEBSOCKET_OPCODE_TEXT = 0x1,
839 MG_WEBSOCKET_OPCODE_BINARY = 0x2,
840 MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
841 MG_WEBSOCKET_OPCODE_PING = 0x9,
842 MG_WEBSOCKET_OPCODE_PONG = 0xa
846 #undef PRINTF_FORMAT_STRING
847 #if defined(_MSC_VER) && _MSC_VER >= 1400
849 #if defined(_MSC_VER) && _MSC_VER > 1400
850 #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
852 #define PRINTF_FORMAT_STRING(s) __format_string s
855 #define PRINTF_FORMAT_STRING(s) s
859 #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
861 #define PRINTF_ARGS(x, y)
867 CIVETWEB_API
int mg_printf(
struct mg_connection *,
868 PRINTF_FORMAT_STRING(
const char *fmt),
869 ...) PRINTF_ARGS(2, 3);
875 CIVETWEB_API
int mg_send_chunk(struct mg_connection *conn,
877 unsigned int chunk_len);
886 CIVETWEB_API
void mg_send_file(struct mg_connection *conn, const
char *path);
899 CIVETWEB_API
int mg_send_file_body(struct mg_connection *conn,
904 CIVETWEB_API
int mg_send_http_error(struct mg_connection *conn,
906 PRINTF_FORMAT_STRING(const
char *fmt),
907 ...) PRINTF_ARGS(3, 4);
921 CIVETWEB_API
int mg_send_http_ok(struct mg_connection *conn,
922 const
char *mime_type,
923 long long content_length);
936 CIVETWEB_API
int mg_send_http_redirect(struct mg_connection *conn,
937 const
char *target_url,
952 mg_send_digest_access_authentication_request(struct mg_connection *conn,
975 mg_check_digest_access_authentication(struct mg_connection *conn,
977 const
char *filename);
987 CIVETWEB_API
void mg_send_mime_file(struct mg_connection *conn,
989 const
char *mime_type);
1003 CIVETWEB_API
void mg_send_mime_file2(struct mg_connection *conn,
1005 const
char *mime_type,
1006 const
char *additional_headers);
1010 CIVETWEB_API
long long mg_store_body(struct mg_connection *conn,
1024 CIVETWEB_API
int mg_read(struct mg_connection *,
void *buf,
size_t len);
1032 CIVETWEB_API const
char *mg_get_header(const struct mg_connection *,
1055 CIVETWEB_API
int mg_get_var(const
char *data,
1057 const
char *var_name,
1085 CIVETWEB_API
int mg_get_var2(const
char *data,
1087 const
char *var_name,
1106 CIVETWEB_API
int mg_get_cookie(const
char *cookie,
1107 const
char *var_name,
1127 CIVETWEB_API struct mg_connection *
1128 mg_download(const
char *host,
1132 size_t error_buffer_size,
1133 PRINTF_FORMAT_STRING(const
char *request_fmt),
1134 ...) PRINTF_ARGS(6, 7);
1138 CIVETWEB_API
void mg_close_connection(struct mg_connection *conn);
1141 #if defined(MG_LEGACY_INTERFACE)
1146 CIVETWEB_API
int mg_upload(
struct mg_connection *conn,
1147 const char *destination_dir);
1153 struct mg_form_data_handler {
1173 int (*field_found)(
const char *key,
1174 const char *filename,
1191 int (*field_get)(
const char *key,
1213 int (*field_store)(
const char *path,
long long file_size,
void *user_data);
1222 #if defined(MG_LEGACY_INTERFACE)
1226 FORM_FIELD_STORAGE_SKIP = 0x0,
1228 FORM_FIELD_STORAGE_GET = 0x1,
1230 FORM_FIELD_STORAGE_STORE = 0x2,
1232 FORM_FIELD_STORAGE_ABORT = 0x10
1239 MG_FORM_FIELD_STORAGE_SKIP = 0x0,
1241 MG_FORM_FIELD_STORAGE_GET = 0x1,
1243 MG_FORM_FIELD_STORAGE_STORE = 0x2,
1245 MG_FORM_FIELD_STORAGE_ABORT = 0x10
1252 MG_FORM_FIELD_HANDLE_GET = 0x1,
1254 MG_FORM_FIELD_HANDLE_NEXT = 0x8,
1256 MG_FORM_FIELD_HANDLE_ABORT = 0x10
1267 CIVETWEB_API
int mg_handle_form_request(
struct mg_connection *conn,
1268 struct mg_form_data_handler *fdh);
1273 typedef void *(*mg_thread_func_t)(
void *);
1274 CIVETWEB_API
int mg_start_thread(mg_thread_func_t f,
void *p);
1279 CIVETWEB_API
const char *mg_get_builtin_mime_type(
const char *file_name);
1283 CIVETWEB_API
const char *
1284 mg_get_response_code_text(
const struct mg_connection *conn,
int response_code);
1288 CIVETWEB_API
const char *mg_version(
void);
1297 CIVETWEB_API
int mg_url_decode(
const char *src,
1301 int is_form_url_encoded);
1307 CIVETWEB_API
int mg_url_encode(
const char *src,
char *dst,
size_t dst_len);
1316 CIVETWEB_API
char *mg_md5(
char buf[33], ...);
1326 CIVETWEB_API
void mg_cry(
const struct mg_connection *conn,
1327 PRINTF_FORMAT_STRING(
const char *fmt),
1328 ...) PRINTF_ARGS(2, 3);
1332 CIVETWEB_API
int mg_strcasecmp(const
char *s1, const
char *s2);
1333 CIVETWEB_API
int mg_strncasecmp(const
char *s1, const
char *s2,
size_t len);
1354 CIVETWEB_API struct mg_connection *
1355 mg_connect_websocket_client(const
char *host,
1359 size_t error_buffer_size,
1362 mg_websocket_data_handler data_func,
1363 mg_websocket_close_handler close_func,
1379 CIVETWEB_API struct mg_connection *mg_connect_client(const
char *host,
1383 size_t error_buffer_size);
1386 struct mg_client_options {
1389 const char *client_cert;
1390 const char *server_cert;
1391 const char *host_name;
1396 CIVETWEB_API
struct mg_connection *
1397 mg_connect_client_secure(
const struct mg_client_options *client_options,
1399 size_t error_buffer_size);
1402 enum { TIMEOUT_INFINITE = -1 };
1403 enum { MG_TIMEOUT_INFINITE = -1 };
1416 CIVETWEB_API
int mg_get_response(
struct mg_connection *conn,
1449 CIVETWEB_API
unsigned mg_check_feature(
unsigned feature);
1465 CIVETWEB_API
int mg_get_system_info(
char *buffer,
int buflen);
1484 mg_get_context_info(
const struct mg_context *ctx,
char *buffer,
int buflen);
1487 #ifdef MG_EXPERIMENTAL_INTERFACES
1504 CIVETWEB_API
int mg_get_connection_info(
const struct mg_context *ctx,