1# CivetWeb API Reference
2
3CivetWeb is often used as HTTP and HTTPS library inside a larger application.
4A C API is available to integrate the CivetWeb functionality in a larger
5codebase. A C++ wrapper is also available, although it is not guaranteed
6that all functionality available through the C API can also be accessed
7from C++. This document describes the public C API. Basic usage examples of
8the API can be found in [Embedding.md](Embedding.md), as well as in the
9examples directory.
10
11## Macros
12
13| Macro | Description |
14| :--- | :--- |
15| **`CIVETWEB_VERSION`** | The current version of the software as a string with the major and minor version number separated with a dot. For version 1.9, this string will have the value "1.9", for the first patch of this version "1.9.1". |
16| **`CIVETWEB_VERSION_MAJOR`** | The current major version as number, e.g., (1) for version 1.9. |
17| **`CIVETWEB_VERSION_MINOR`** | The current minor version as number, e.g., (9) for version 1.9. |
18| **`CIVETWEB_VERSION_PATCH`** | The current patch version as number, e.g., (0) for version 1.9 or (1) for version 1.9.1. |
19
20## Handles
21
22* `struct mg_context *`
23Handle for one instance of the HTTP(S) server.
24All functions using `const struct mg_context *` as an argument do not modify a running server instance, but just query information. Functions using a non-const `struct mg_context *` as an argument may modify a server instance (e.g., register a new URI, stop the server, ...).
25
26* `struct mg_connection *`
27Handle for one individual client-server connection.
28Functions working with `const struct mg_connection *` operate on data already known to the server without reading data from or sending data to the client. Callbacks using a `const struct mg_connection *` argument are supposed to not call functions from the `mg_read()` and `mg_write()` family. To support a correct application, reading and writing functions require a non-const `struct mg_connection *` connection handle.
29
30The content of both structures is not defined in the interface - they are only used as opaque pointers (handles).
31
32## Structures
33
34* [`struct mg_client_cert;`](api/mg_client_cert.md)
35* [`struct mg_client_options;`](api/mg_client_options.md)
36* [`struct mg_callbacks;`](api/mg_callbacks.md)
37* [`struct mg_form_data_handler;`](api/mg_form_data_handler.md)
38* [`struct mg_header;`](api/mg_header.md)
39* [`struct mg_option;`](api/mg_option.md)
40* [`struct mg_request_info;`](api/mg_request_info.md)
41* [`struct mg_response_info;`](api/mg_response_info.md)
42* [`struct mg_server_port;`](api/mg_server_port.md)
43
44
45## Library API Functions
46
47* [`mg_init_library( feature );`](api/mg_init_library.md)
48* [`mg_exit_library( feature );`](api/mg_exit_library.md)
49
50* [`mg_check_feature( feature );`](api/mg_check_feature.md)
51* [`mg_version();`](api/mg_version.md)
52
53
54## Server API Functions
55
56* [`mg_start( callbacks, user_data, options );`](api/mg_start.md)
57* [`mg_stop( ctx );`](api/mg_stop.md)
58
59* [`mg_get_builtin_mime_type( file_name );`](api/mg_get_builtin_mime_type.md)
60* [`mg_get_option( ctx, name );`](api/mg_get_option.md)
61* [`mg_get_server_ports( ctx, size, ports );`](api/mg_get_server_ports.md)
62* [`mg_get_user_data( ctx );`](api/mg_get_user_data.md)
63* [`mg_set_auth_handler( ctx, uri, handler, cbdata );`](api/mg_set_auth_handler.md)
64* [`mg_set_request_handler( ctx, uri, handler, cbdata );`](api/mg_set_request_handler.md)
65* [`mg_set_websocket_handler( ctx, uri, connect_handler, ready_handler, data_handler, close_handler, cbdata );`](api/mg_set_websocket_handler.md)
66
67* [`mg_lock_context( ctx );`](api/mg_lock_context.md)
68* [`mg_unlock_context( ctx );`](api/mg_unlock_context.md)
69
70* [`mg_get_context( conn );`](api/mg_get_context.md)
71
72* [`mg_send_http_error( conn, status_code, fmt, ... );`](api/mg_send_http_error.md)
73* [`mg_send_http_ok( conn, mime_type, content_length );`](api/mg_send_http_ok.md)
74* [`mg_send_http_redirect( conn, target_url, redirect_code );`](api/mg_send_http_redirect.md)
75
76* [`mg_send_digest_access_authentication_request( conn, realm );`](api/mg_send_digest_access_authentication_request.md)
77* [`mg_check_digest_access_authentication( conn, realm, filename );`](api/mg_check_digest_access_authentication.md)
78* [`mg_modify_passwords_file( passwords_file_name, realm, user, password );`](api/mg_modify_passwords_file.md)
79
80* [`mg_get_request_info( conn );`](api/mg_get_request_info.md)
81* [`mg_get_request_link( conn, buf, buflen );`](api/mg_get_request_link.md)
82* [`mg_handle_form_request( conn, fdh );`](api/mg_handle_form_request.md)
83
84* [`mg_send_file( conn, path );`](api/mg_send_file.md)
85* [`mg_send_mime_file( conn, path, mime_type );`](api/mg_send_mime_file.md)
86* [`mg_send_mime_file2( conn, path, mime_type, additional_headers );`](api/mg_send_mime_file2.md)
87* [`mg_websocket_write( conn, opcode, data, data_len );`](api/mg_websocket_write.md)
88
89## Client API Functions
90
91* [`mg_connect_client( host, port, use_ssl, error_buffer, error_buffer_size );`](api/mg_connect_client.md)
92* [`mg_connect_client_secure( client_options, error_buffer, error_buffer_size );`](api/mg_connect_client_secure.md)
93* [`mg_connect_websocket_client( host, port, use_ssl, error_buffer, error_buffer_size, path, origin, data_func, close_func, user_data);`](api/mg_connect_websocket_client.md)
94* [`mg_websocket_client_write( conn, opcode, data, data_len );`](api/mg_websocket_client_write.md)
95
96* [`mg_download( host, port, use_ssl, error_buffer, error_buffer_size, fmt, ... );`](api/mg_download.md)
97
98* [`mg_get_response( conn, ebuf, ebuf_len, timeout );`](api/mg_get_response.md)
99* [`mg_get_response_info( conn );`](api/mg_get_response_info.md)
100
101## Common API Functions
102
103* [`mg_close_connection( conn );`](api/mg_close_connection.md)
104* [`mg_cry( conn, fmt, ... );`](api/mg_cry.md)
105
106* [`mg_get_cookie( cookie, var_name, buf, buf_len );`](api/mg_get_cookie.md)
107* [`mg_get_header( conn, name );`](api/mg_get_header.md)
108* [`mg_get_response_code_text( conn, response_code );`](api/mg_get_response_code_text.md)
109* [`mg_get_user_connection_data( conn );`](api/mg_get_user_connection_data.md)
110* [`mg_get_valid_options();`](api/mg_get_valid_options.md)
111* [`mg_get_var( data, data_len, var_name, dst, dst_len );`](api/mg_get_var.md)
112* [`mg_get_var2( data, data_len, var_name, dst, dst_len, occurrence );`](api/mg_get_var2.md)
113* [`mg_lock_connection( conn );`](api/mg_lock_connection.md)
114* [`mg_md5( buf, ... );`](api/mg_md5.md)
115* [`mg_printf( conn, fmt, ... );`](api/mg_printf.md)
116* [`mg_read( conn, buf, len );`](api/mg_read.md)
117* [`mg_send_chunk( conn, buf, len );`](api/mg_send_chunk.md)
118* [`mg_send_file( conn, path );`](api/mg_send_file_body.md)
119* [`mg_set_user_connection_data( conn, data );`](api/mg_set_user_connection_data.md)
120* [`mg_start_thread( f, p );`](api/mg_start_thread.md)
121* [`mg_store_body( conn, path );`](api/mg_store_body.md)
122* [`mg_strcasecmp( s1, s2 );`](api/mg_strcasecmp.md)
123* [`mg_strncasecmp( s1, s2, len );`](api/mg_strncasecmp.md)
124* [`mg_unlock_connection( conn );`](api/mg_unlock_connection.md)
125* [`mg_url_decode( src, src_len, dst, dst_len, is_form_url_encoded );`](api/mg_url_decode.md)
126* [`mg_url_encode( src, dst, dst_len );`](api/mg_url_encode.md)
127* [`mg_write( conn, buf, len );`](api/mg_write.md)
128
129
130## Diagnosis Functions
131
132* [`mg_get_system_info( buffer, buf_len );`](api/mg_get_system_info.md)
133* [`mg_get_context_info( ctx, buffer, buf_len );`](api/mg_get_context_info.md)
134* [`mg_get_connection_info( ctx, idx, buffer, buf_len );`](api/mg_get_connection_info.md)
135
136
137## Deprecated:
138
139* [~~`mg_get_valid_option_names();`~~](api/mg_get_valid_option_names.md)
140* [~~`mg_upload( conn, destination_dir );`~~](api/mg_upload.md)
141* [~~`mg_get_ports( ctx, size, ports, ssl);`~~](api/mg_get_ports.md)
142
143
144