]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/include/civetweb.h
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / include / civetweb.h
1 /* Copyright (c) 2013-2017 the Civetweb developers
2 * Copyright (c) 2004-2013 Sergey Lyubka
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #ifndef CIVETWEB_HEADER_INCLUDED
24 #define CIVETWEB_HEADER_INCLUDED
25
26 #define CIVETWEB_VERSION "1.10"
27 #define CIVETWEB_VERSION_MAJOR (1)
28 #define CIVETWEB_VERSION_MINOR (10)
29 #define CIVETWEB_VERSION_PATCH (0)
30 #define CIVETWEB_VERSION_RELEASED
31
32 #ifndef CIVETWEB_API
33 #if defined(_WIN32)
34 #if defined(CIVETWEB_DLL_EXPORTS)
35 #define CIVETWEB_API __declspec(dllexport)
36 #elif defined(CIVETWEB_DLL_IMPORTS)
37 #define CIVETWEB_API __declspec(dllimport)
38 #else
39 #define CIVETWEB_API
40 #endif
41 #elif __GNUC__ >= 4
42 #define CIVETWEB_API __attribute__((visibility("default")))
43 #else
44 #define CIVETWEB_API
45 #endif
46 #endif
47
48 #include <stdio.h>
49 #include <stddef.h>
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54
55
56 /* Initialize this library. This should be called once before any other
57 * function from this library. This function is not guaranteed to be
58 * thread safe.
59 * Parameters:
60 * features: bit mask for features to be initialized.
61 * Return value:
62 * initialized features
63 * 0: error
64 */
65 CIVETWEB_API unsigned mg_init_library(unsigned features);
66
67
68 /* Un-initialize this library.
69 * Return value:
70 * 0: error
71 */
72 CIVETWEB_API unsigned mg_exit_library(void);
73
74
75 struct mg_context; /* Handle for the HTTP service itself */
76 struct mg_connection; /* Handle for the individual connection */
77
78
79 /* Maximum number of headers */
80 #define MG_MAX_HEADERS (64)
81
82 struct mg_header {
83 const char *name; /* HTTP header name */
84 const char *value; /* HTTP header value */
85 };
86
87
88 /* This structure contains information about the HTTP request. */
89 struct mg_request_info {
90 const char *request_method; /* "GET", "POST", etc */
91 const char *request_uri; /* URL-decoded URI (absolute or relative,
92 * as in the request) */
93 const char *local_uri; /* URL-decoded URI (relative). Can be NULL
94 * if the request_uri does not address a
95 * resource at the server host. */
96 #if defined(MG_LEGACY_INTERFACE)
97 const char *uri; /* Deprecated: use local_uri instead */
98 #endif
99 const char *http_version; /* E.g. "1.0", "1.1" */
100 const char *query_string; /* URL part after '?', not including '?', or
101 NULL */
102 const char *remote_user; /* Authenticated user, or NULL if no auth
103 used */
104 char remote_addr[48]; /* Client's IP address as a string. */
105
106 #if defined(MG_LEGACY_INTERFACE)
107 long remote_ip; /* Client's IP address. Deprecated: use remote_addr instead
108 */
109 #endif
110
111 long long content_length; /* Length (in bytes) of the request body,
112 can be -1 if no length was given. */
113 int remote_port; /* Client's port */
114 int is_ssl; /* 1 if SSL-ed, 0 if not */
115 void *user_data; /* User data pointer passed to mg_start() */
116 void *conn_data; /* Connection-specific user data */
117
118 int num_headers; /* Number of HTTP headers */
119 struct mg_header
120 http_headers[MG_MAX_HEADERS]; /* Allocate maximum headers */
121
122 struct mg_client_cert *client_cert; /* Client certificate information */
123
124 const char *acceptedWebSocketSubprotocol; /* websocket subprotocol,
125 * accepted during handshake */
126 };
127
128
129 /* This structure contains information about the HTTP request. */
130 /* This structure may be extended in future versions. */
131 struct mg_response_info {
132 int status_code; /* E.g. 200 */
133 const char *status_text; /* E.g. "OK" */
134 const char *http_version; /* E.g. "1.0", "1.1" */
135
136 long long content_length; /* Length (in bytes) of the request body,
137 can be -1 if no length was given. */
138
139 int num_headers; /* Number of HTTP headers */
140 struct mg_header
141 http_headers[MG_MAX_HEADERS]; /* Allocate maximum headers */
142 };
143
144
145 /* Client certificate information (part of mg_request_info) */
146 /* New nomenclature. */
147 struct mg_client_cert {
148 const char *subject;
149 const char *issuer;
150 const char *serial;
151 const char *finger;
152 };
153
154 /* Old nomenclature. */
155 struct client_cert {
156 const char *subject;
157 const char *issuer;
158 const char *serial;
159 const char *finger;
160 };
161
162
163 /* This structure needs to be passed to mg_start(), to let civetweb know
164 which callbacks to invoke. For a detailed description, see
165 https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md */
166 struct mg_callbacks {
167 /* Called when civetweb has received new HTTP request.
168 If the callback returns one, it must process the request
169 by sending valid HTTP headers and a body. Civetweb will not do
170 any further processing. Otherwise it must return zero.
171 Note that since V1.7 the "begin_request" function is called
172 before an authorization check. If an authorization check is
173 required, use a request_handler instead.
174 Return value:
175 0: civetweb will process the request itself. In this case,
176 the callback must not send any data to the client.
177 1-999: callback already processed the request. Civetweb will
178 not send any data after the callback returned. The
179 return code is stored as a HTTP status code for the
180 access log. */
181 int (*begin_request)(struct mg_connection *);
182
183 /* Called when civetweb has finished processing request. */
184 void (*end_request)(const struct mg_connection *, int reply_status_code);
185
186 /* Called when civetweb is about to log a message. If callback returns
187 non-zero, civetweb does not log anything. */
188 int (*log_message)(const struct mg_connection *, const char *message);
189
190 /* Called when civetweb is about to log access. If callback returns
191 non-zero, civetweb does not log anything. */
192 int (*log_access)(const struct mg_connection *, const char *message);
193
194 /* Called when civetweb initializes SSL library.
195 Parameters:
196 user_data: parameter user_data passed when starting the server.
197 Return value:
198 0: civetweb will set up the SSL certificate.
199 1: civetweb assumes the callback already set up the certificate.
200 -1: initializing ssl fails. */
201 int (*init_ssl)(void *ssl_context, void *user_data);
202
203 #if defined(MG_LEGACY_INTERFACE)
204 /* Called when websocket request is received, before websocket handshake.
205 Return value:
206 0: civetweb proceeds with websocket handshake.
207 1: connection is closed immediately.
208 This callback is deprecated: Use mg_set_websocket_handler instead. */
209 int (*websocket_connect)(const struct mg_connection *);
210
211 /* Called when websocket handshake is successfully completed, and
212 connection is ready for data exchange.
213 This callback is deprecated: Use mg_set_websocket_handler instead. */
214 void (*websocket_ready)(struct mg_connection *);
215
216 /* Called when data frame has been received from the client.
217 Parameters:
218 bits: first byte of the websocket frame, see websocket RFC at
219 http://tools.ietf.org/html/rfc6455, section 5.2
220 data, data_len: payload, with mask (if any) already applied.
221 Return value:
222 1: keep this websocket connection open.
223 0: close this websocket connection.
224 This callback is deprecated: Use mg_set_websocket_handler instead. */
225 int (*websocket_data)(struct mg_connection *,
226 int bits,
227 char *data,
228 size_t data_len);
229 #endif /* MG_LEGACY_INTERFACE */
230
231 /* Called when civetweb is closing a connection. The per-context mutex is
232 locked when this is invoked.
233
234 Websockets:
235 Before mg_set_websocket_handler has been added, it was primarily useful
236 for noting when a websocket is closing, and used to remove it from any
237 application-maintained list of clients.
238 Using this callback for websocket connections is deprecated: Use
239 mg_set_websocket_handler instead.
240
241 Connection specific data:
242 If memory has been allocated for the connection specific user data
243 (mg_request_info->conn_data, mg_get_user_connection_data),
244 this is the last chance to free it.
245 */
246 void (*connection_close)(const struct mg_connection *);
247
248 #if defined(MG_USE_OPEN_FILE)
249 /* Note: The "file in memory" feature is a deletion candidate, since
250 * it complicates the code, and does not add any value compared to
251 * "mg_add_request_handler".
252 * See this discussion thread:
253 * https://groups.google.com/forum/#!topic/civetweb/h9HT4CmeYqI
254 * If you disagree, if there is any situation this is indeed useful
255 * and cannot trivially be replaced by another existing feature,
256 * please contribute to this discussion during the next 3 month
257 * (till end of April 2017), otherwise this feature might be dropped
258 * in future releases. */
259
260 /* Called when civetweb tries to open a file. Used to intercept file open
261 calls, and serve file data from memory instead.
262 Parameters:
263 path: Full path to the file to open.
264 data_len: Placeholder for the file size, if file is served from
265 memory.
266 Return value:
267 NULL: do not serve file from memory, proceed with normal file open.
268 non-NULL: pointer to the file contents in memory. data_len must be
269 initialized with the size of the memory block. */
270 const char *(*open_file)(const struct mg_connection *,
271 const char *path,
272 size_t *data_len);
273 #endif
274
275 /* Called when civetweb is about to serve Lua server page, if
276 Lua support is enabled.
277 Parameters:
278 lua_context: "lua_State *" pointer. */
279 void (*init_lua)(const struct mg_connection *, void *lua_context);
280
281 #if defined(MG_LEGACY_INTERFACE)
282 /* Called when civetweb has uploaded a file to a temporary directory as a
283 result of mg_upload() call.
284 Note that mg_upload is deprecated. Use mg_handle_form_request instead.
285 Parameters:
286 file_name: full path name to the uploaded file. */
287 void (*upload)(struct mg_connection *, const char *file_name);
288 #endif
289
290 /* Called when civetweb is about to send HTTP error to the client.
291 Implementing this callback allows to create custom error pages.
292 Parameters:
293 status: HTTP error status code.
294 Return value:
295 1: run civetweb error handler.
296 0: callback already handled the error. */
297 int (*http_error)(struct mg_connection *, int status);
298
299 /* Called after civetweb context has been created, before requests
300 are processed.
301 Parameters:
302 ctx: context handle */
303 void (*init_context)(const struct mg_context *ctx);
304
305 /* Called when a new worker thread is initialized.
306 Parameters:
307 ctx: context handle
308 thread_type:
309 0 indicates the master thread
310 1 indicates a worker thread handling client connections
311 2 indicates an internal helper thread (timer thread)
312 */
313 void (*init_thread)(const struct mg_context *ctx, int thread_type);
314
315 /* Called when civetweb context is deleted.
316 Parameters:
317 ctx: context handle */
318 void (*exit_context)(const struct mg_context *ctx);
319
320 /* Called when initializing a new connection object.
321 * Can be used to initialize the connection specific user data
322 * (mg_request_info->conn_data, mg_get_user_connection_data).
323 * When the callback is called, it is not yet known if a
324 * valid HTTP(S) request will be made.
325 * Parameters:
326 * conn: not yet fully initialized connection object
327 * conn_data: output parameter, set to initialize the
328 * connection specific user data
329 * Return value:
330 * must be 0
331 * Otherwise, the result is undefined
332 */
333 int (*init_connection)(const struct mg_connection *conn, void **conn_data);
334 };
335
336
337 /* Start web server.
338
339 Parameters:
340 callbacks: mg_callbacks structure with user-defined callbacks.
341 options: NULL terminated list of option_name, option_value pairs that
342 specify Civetweb configuration parameters.
343
344 Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
345 processing is required for these, signal handlers must be set up
346 after calling mg_start().
347
348
349 Example:
350 const char *options[] = {
351 "document_root", "/var/www",
352 "listening_ports", "80,443s",
353 NULL
354 };
355 struct mg_context *ctx = mg_start(&my_func, NULL, options);
356
357 Refer to https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md
358 for the list of valid option and their possible values.
359
360 Return:
361 web server context, or NULL on error. */
362 CIVETWEB_API struct mg_context *mg_start(const struct mg_callbacks *callbacks,
363 void *user_data,
364 const char **configuration_options);
365
366
367 /* Stop the web server.
368
369 Must be called last, when an application wants to stop the web server and
370 release all associated resources. This function blocks until all Civetweb
371 threads are stopped. Context pointer becomes invalid. */
372 CIVETWEB_API void mg_stop(struct mg_context *);
373
374
375 /* mg_request_handler
376
377 Called when a new request comes in. This callback is URI based
378 and configured with mg_set_request_handler().
379
380 Parameters:
381 conn: current connection information.
382 cbdata: the callback data configured with mg_set_request_handler().
383 Returns:
384 0: the handler could not handle the request, so fall through.
385 1 - 999: the handler processed the request. The return code is
386 stored as a HTTP status code for the access log. */
387 typedef int (*mg_request_handler)(struct mg_connection *conn, void *cbdata);
388
389
390 /* mg_set_request_handler
391
392 Sets or removes a URI mapping for a request handler.
393 This function uses mg_lock_context internally.
394
395 URI's are ordered and prefixed URI's are supported. For example,
396 consider two URIs: /a/b and /a
397 /a matches /a
398 /a/b matches /a/b
399 /a/c matches /a
400
401 Parameters:
402 ctx: server context
403 uri: the URI (exact or pattern) for the handler
404 handler: the callback handler to use when the URI is requested.
405 If NULL, an already registered handler for this URI will
406 be removed.
407 The URI used to remove a handler must match exactly the
408 one used to register it (not only a pattern match).
409 cbdata: the callback data to give to the handler when it is called. */
410 CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx,
411 const char *uri,
412 mg_request_handler handler,
413 void *cbdata);
414
415
416 /* Callback types for websocket handlers in C/C++.
417
418 mg_websocket_connect_handler
419 Is called when the client intends to establish a websocket connection,
420 before websocket handshake.
421 Return value:
422 0: civetweb proceeds with websocket handshake.
423 1: connection is closed immediately.
424
425 mg_websocket_ready_handler
426 Is called when websocket handshake is successfully completed, and
427 connection is ready for data exchange.
428
429 mg_websocket_data_handler
430 Is called when a data frame has been received from the client.
431 Parameters:
432 bits: first byte of the websocket frame, see websocket RFC at
433 http://tools.ietf.org/html/rfc6455, section 5.2
434 data, data_len: payload, with mask (if any) already applied.
435 Return value:
436 1: keep this websocket connection open.
437 0: close this websocket connection.
438
439 mg_connection_close_handler
440 Is called, when the connection is closed.*/
441 typedef int (*mg_websocket_connect_handler)(const struct mg_connection *,
442 void *);
443 typedef void (*mg_websocket_ready_handler)(struct mg_connection *, void *);
444 typedef int (*mg_websocket_data_handler)(struct mg_connection *,
445 int,
446 char *,
447 size_t,
448 void *);
449 typedef void (*mg_websocket_close_handler)(const struct mg_connection *,
450 void *);
451
452 /* struct mg_websocket_subprotocols
453 *
454 * List of accepted subprotocols
455 */
456 struct mg_websocket_subprotocols {
457 int nb_subprotocols;
458 char **subprotocols;
459 };
460
461 /* mg_set_websocket_handler
462
463 Set or remove handler functions for websocket connections.
464 This function works similar to mg_set_request_handler - see there. */
465 CIVETWEB_API void
466 mg_set_websocket_handler(struct mg_context *ctx,
467 const char *uri,
468 mg_websocket_connect_handler connect_handler,
469 mg_websocket_ready_handler ready_handler,
470 mg_websocket_data_handler data_handler,
471 mg_websocket_close_handler close_handler,
472 void *cbdata);
473
474 /* mg_set_websocket_handler
475
476 Set or remove handler functions for websocket connections.
477 This function works similar to mg_set_request_handler - see there. */
478 CIVETWEB_API void mg_set_websocket_handler_with_subprotocols(
479 struct mg_context *ctx,
480 const char *uri,
481 struct mg_websocket_subprotocols *subprotocols,
482 mg_websocket_connect_handler connect_handler,
483 mg_websocket_ready_handler ready_handler,
484 mg_websocket_data_handler data_handler,
485 mg_websocket_close_handler close_handler,
486 void *cbdata);
487
488
489 /* mg_authorization_handler
490
491 Callback function definition for mg_set_auth_handler
492
493 Parameters:
494 conn: current connection information.
495 cbdata: the callback data configured with mg_set_request_handler().
496 Returns:
497 0: access denied
498 1: access granted
499 */
500 typedef int (*mg_authorization_handler)(struct mg_connection *conn,
501 void *cbdata);
502
503
504 /* mg_set_auth_handler
505
506 Sets or removes a URI mapping for an authorization handler.
507 This function works similar to mg_set_request_handler - see there. */
508 CIVETWEB_API void mg_set_auth_handler(struct mg_context *ctx,
509 const char *uri,
510 mg_authorization_handler handler,
511 void *cbdata);
512
513
514 /* Get the value of particular configuration parameter.
515 The value returned is read-only. Civetweb does not allow changing
516 configuration at run time.
517 If given parameter name is not valid, NULL is returned. For valid
518 names, return value is guaranteed to be non-NULL. If parameter is not
519 set, zero-length string is returned. */
520 CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx,
521 const char *name);
522
523
524 /* Get context from connection. */
525 CIVETWEB_API struct mg_context *
526 mg_get_context(const struct mg_connection *conn);
527
528
529 /* Get user data passed to mg_start from context. */
530 CIVETWEB_API void *mg_get_user_data(const struct mg_context *ctx);
531
532
533 /* Set user data for the current connection. */
534 CIVETWEB_API void mg_set_user_connection_data(struct mg_connection *conn,
535 void *data);
536
537
538 /* Get user data set for the current connection. */
539 CIVETWEB_API void *
540 mg_get_user_connection_data(const struct mg_connection *conn);
541
542
543 /* Get a formatted link corresponding to the current request
544
545 Parameters:
546 conn: current connection information.
547 buf: string buffer (out)
548 buflen: length of the string buffer
549 Returns:
550 <0: error
551 >=0: ok */
552 CIVETWEB_API int
553 mg_get_request_link(const struct mg_connection *conn, char *buf, size_t buflen);
554
555
556 #if defined(MG_LEGACY_INTERFACE)
557 /* Return array of strings that represent valid configuration options.
558 For each option, option name and default value is returned, i.e. the
559 number of entries in the array equals to number_of_options x 2.
560 Array is NULL terminated. */
561 /* Deprecated: Use mg_get_valid_options instead. */
562 CIVETWEB_API const char **mg_get_valid_option_names(void);
563 #endif
564
565
566 struct mg_option {
567 const char *name;
568 int type;
569 const char *default_value;
570 };
571
572 /* Old nomenclature */
573 enum {
574 CONFIG_TYPE_UNKNOWN = 0x0,
575 CONFIG_TYPE_NUMBER = 0x1,
576 CONFIG_TYPE_STRING = 0x2,
577 CONFIG_TYPE_FILE = 0x3,
578 CONFIG_TYPE_DIRECTORY = 0x4,
579 CONFIG_TYPE_BOOLEAN = 0x5,
580 CONFIG_TYPE_EXT_PATTERN = 0x6,
581 CONFIG_TYPE_STRING_LIST = 0x7,
582 CONFIG_TYPE_STRING_MULTILINE = 0x8
583 };
584
585 /* New nomenclature */
586 enum {
587 MG_CONFIG_TYPE_UNKNOWN = 0x0,
588 MG_CONFIG_TYPE_NUMBER = 0x1,
589 MG_CONFIG_TYPE_STRING = 0x2,
590 MG_CONFIG_TYPE_FILE = 0x3,
591 MG_CONFIG_TYPE_DIRECTORY = 0x4,
592 MG_CONFIG_TYPE_BOOLEAN = 0x5,
593 MG_CONFIG_TYPE_EXT_PATTERN = 0x6,
594 MG_CONFIG_TYPE_STRING_LIST = 0x7,
595 MG_CONFIG_TYPE_STRING_MULTILINE = 0x8
596 };
597
598 /* Return array of struct mg_option, representing all valid configuration
599 options of civetweb.c.
600 The array is terminated by a NULL name option. */
601 CIVETWEB_API const struct mg_option *mg_get_valid_options(void);
602
603
604 struct mg_server_ports {
605 int protocol; /* 1 = IPv4, 2 = IPv6, 3 = both */
606 int port; /* port number */
607 int is_ssl; /* https port: 0 = no, 1 = yes */
608 int is_redirect; /* redirect all requests: 0 = no, 1 = yes */
609 int _reserved1;
610 int _reserved2;
611 int _reserved3;
612 int _reserved4;
613 };
614
615
616 /* Get the list of ports that civetweb is listening on.
617 The parameter size is the size of the ports array in elements.
618 The caller is responsibility to allocate the required memory.
619 This function returns the number of struct mg_server_ports elements
620 filled in, or <0 in case of an error. */
621 CIVETWEB_API int mg_get_server_ports(const struct mg_context *ctx,
622 int size,
623 struct mg_server_ports *ports);
624
625
626 #if defined(MG_LEGACY_INTERFACE)
627 /* Deprecated: Use mg_get_server_ports instead. */
628 CIVETWEB_API size_t
629 mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl);
630 #endif
631
632
633 /* Add, edit or delete the entry in the passwords file.
634 *
635 * This function allows an application to manipulate .htpasswd files on the
636 * fly by adding, deleting and changing user records. This is one of the
637 * several ways of implementing authentication on the server side. For another,
638 * cookie-based way please refer to the examples/chat in the source tree.
639 *
640 * Parameter:
641 * passwords_file_name: Path and name of a file storing multiple passwords
642 * realm: HTTP authentication realm (authentication domain) name
643 * user: User name
644 * password:
645 * If password is not NULL, entry modified or added.
646 * If password is NULL, entry is deleted.
647 *
648 * Return:
649 * 1 on success, 0 on error.
650 */
651 CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name,
652 const char *realm,
653 const char *user,
654 const char *password);
655
656
657 /* Return information associated with the request.
658 * Use this function to implement a server and get data about a request
659 * from a HTTP/HTTPS client.
660 * Note: Before CivetWeb 1.10, this function could be used to read
661 * a response from a server, when implementing a client, although the
662 * values were never returned in appropriate mg_request_info elements.
663 * It is strongly advised to use mg_get_response_info for clients.
664 */
665 CIVETWEB_API const struct mg_request_info *
666 mg_get_request_info(const struct mg_connection *);
667
668 /* Return the local address (server side) of the socket for a connection */
669 CIVETWEB_API struct sockaddr *
670 mg_get_local_addr(struct mg_connection *);
671
672 /* Return information associated with a HTTP/HTTPS response.
673 * Use this function in a client, to check the response from
674 * the server. */
675 CIVETWEB_API const struct mg_response_info *
676 mg_get_response_info(const struct mg_connection *);
677
678
679 /* Send data to the client.
680 Return:
681 0 when the connection has been closed
682 -1 on error
683 >0 number of bytes written on success */
684 CIVETWEB_API int mg_write(struct mg_connection *, const void *buf, size_t len);
685
686
687 /* Send data to a websocket client wrapped in a websocket frame. Uses
688 mg_lock_connection to ensure that the transmission is not interrupted,
689 i.e., when the application is proactively communicating and responding to
690 a request simultaneously.
691
692 Send data to a websocket client wrapped in a websocket frame.
693 This function is available when civetweb is compiled with -DUSE_WEBSOCKET
694
695 Return:
696 0 when the connection has been closed
697 -1 on error
698 >0 number of bytes written on success */
699 CIVETWEB_API int mg_websocket_write(struct mg_connection *conn,
700 int opcode,
701 const char *data,
702 size_t data_len);
703
704
705 /* Send data to a websocket server wrapped in a masked websocket frame. Uses
706 mg_lock_connection to ensure that the transmission is not interrupted,
707 i.e., when the application is proactively communicating and responding to
708 a request simultaneously.
709
710 Send data to a websocket server wrapped in a masked websocket frame.
711 This function is available when civetweb is compiled with -DUSE_WEBSOCKET
712
713 Return:
714 0 when the connection has been closed
715 -1 on error
716 >0 number of bytes written on success */
717 CIVETWEB_API int mg_websocket_client_write(struct mg_connection *conn,
718 int opcode,
719 const char *data,
720 size_t data_len);
721
722
723 /* Blocks until unique access is obtained to this connection. Intended for use
724 with websockets only.
725 Invoke this before mg_write or mg_printf when communicating with a
726 websocket if your code has server-initiated communication as well as
727 communication in direct response to a message. */
728 CIVETWEB_API void mg_lock_connection(struct mg_connection *conn);
729 CIVETWEB_API void mg_unlock_connection(struct mg_connection *conn);
730
731
732 #if defined(MG_LEGACY_INTERFACE)
733 #define mg_lock mg_lock_connection
734 #define mg_unlock mg_unlock_connection
735 #endif
736
737
738 /* Lock server context. This lock may be used to protect resources
739 that are shared between different connection/worker threads. */
740 CIVETWEB_API void mg_lock_context(struct mg_context *ctx);
741 CIVETWEB_API void mg_unlock_context(struct mg_context *ctx);
742
743
744 /* Opcodes, from http://tools.ietf.org/html/rfc6455 */
745 /* Old nomenclature */
746 enum {
747 WEBSOCKET_OPCODE_CONTINUATION = 0x0,
748 WEBSOCKET_OPCODE_TEXT = 0x1,
749 WEBSOCKET_OPCODE_BINARY = 0x2,
750 WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
751 WEBSOCKET_OPCODE_PING = 0x9,
752 WEBSOCKET_OPCODE_PONG = 0xa
753 };
754
755 /* New nomenclature */
756 enum {
757 MG_WEBSOCKET_OPCODE_CONTINUATION = 0x0,
758 MG_WEBSOCKET_OPCODE_TEXT = 0x1,
759 MG_WEBSOCKET_OPCODE_BINARY = 0x2,
760 MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
761 MG_WEBSOCKET_OPCODE_PING = 0x9,
762 MG_WEBSOCKET_OPCODE_PONG = 0xa
763 };
764
765 /* Macros for enabling compiler-specific checks for printf-like arguments. */
766 #undef PRINTF_FORMAT_STRING
767 #if defined(_MSC_VER) && _MSC_VER >= 1400
768 #include <sal.h>
769 #if defined(_MSC_VER) && _MSC_VER > 1400
770 #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
771 #else
772 #define PRINTF_FORMAT_STRING(s) __format_string s
773 #endif
774 #else
775 #define PRINTF_FORMAT_STRING(s) s
776 #endif
777
778 #ifdef __GNUC__
779 #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
780 #else
781 #define PRINTF_ARGS(x, y)
782 #endif
783
784
785 /* Send data to the client using printf() semantics.
786 Works exactly like mg_write(), but allows to do message formatting. */
787 CIVETWEB_API int mg_printf(struct mg_connection *,
788 PRINTF_FORMAT_STRING(const char *fmt),
789 ...) PRINTF_ARGS(2, 3);
790
791
792 /* Send a part of the message body, if chunked transfer encoding is set.
793 * Only use this function after sending a complete HTTP request or response
794 * header with "Transfer-Encoding: chunked" set. */
795 CIVETWEB_API int mg_send_chunk(struct mg_connection *conn,
796 const char *chunk,
797 unsigned int chunk_len);
798
799
800 /* Send contents of the entire file together with HTTP headers. */
801 CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
802
803
804 /* Send HTTP error reply. */
805 CIVETWEB_API void mg_send_http_error(struct mg_connection *conn,
806 int status_code,
807 PRINTF_FORMAT_STRING(const char *fmt),
808 ...) PRINTF_ARGS(3, 4);
809
810
811 /* Send HTTP digest access authentication request.
812 * Browsers will send a user name and password in their next request, showing
813 * an authentication dialog if the password is not stored.
814 * Parameters:
815 * conn: Current connection handle.
816 * realm: Authentication realm. If NULL is supplied, the sever domain
817 * set in the authentication_domain configuration is used.
818 * Return:
819 * < 0 Error
820 */
821 CIVETWEB_API int
822 mg_send_digest_access_authentication_request(struct mg_connection *conn,
823 const char *realm);
824
825
826 /* Check if the current request has a valid authentication token set.
827 * A file is used to provide a list of valid user names, realms and
828 * password hashes. The file can be created and modified using the
829 * mg_modify_passwords_file API function.
830 * Parameters:
831 * conn: Current connection handle.
832 * realm: Authentication realm. If NULL is supplied, the sever domain
833 * set in the authentication_domain configuration is used.
834 * filename: Path and name of a file storing multiple password hashes.
835 * Return:
836 * > 0 Valid authentication
837 * 0 Invalid authentication
838 * < 0 Error (all values < 0 should be considered as invalid
839 * authentication, future error codes will have negative
840 * numbers)
841 * -1 Parameter error
842 * -2 File not found
843 */
844 CIVETWEB_API int
845 mg_check_digest_access_authentication(struct mg_connection *conn,
846 const char *realm,
847 const char *filename);
848
849
850 /* Send contents of the entire file together with HTTP headers.
851 * Parameters:
852 * conn: Current connection handle.
853 * path: Full path to the file to send.
854 * mime_type: Content-Type for file. NULL will cause the type to be
855 * looked up by the file extension.
856 */
857 CIVETWEB_API void mg_send_mime_file(struct mg_connection *conn,
858 const char *path,
859 const char *mime_type);
860
861
862 /* Send contents of the entire file together with HTTP headers.
863 Parameters:
864 conn: Current connection information.
865 path: Full path to the file to send.
866 mime_type: Content-Type for file. NULL will cause the type to be
867 looked up by the file extension.
868 additional_headers: Additional custom header fields appended to the header.
869 Each header should start with an X-, to ensure it is
870 not included twice.
871 NULL does not append anything.
872 */
873 CIVETWEB_API void mg_send_mime_file2(struct mg_connection *conn,
874 const char *path,
875 const char *mime_type,
876 const char *additional_headers);
877
878
879 /* Store body data into a file. */
880 CIVETWEB_API long long mg_store_body(struct mg_connection *conn,
881 const char *path);
882 /* Read entire request body and store it in a file "path".
883 Return:
884 < 0 Error
885 >= 0 Number of bytes stored in file "path".
886 */
887
888
889 /* Read data from the remote end, return number of bytes read.
890 Return:
891 0 connection has been closed by peer. No more data could be read.
892 < 0 read error. No more data could be read from the connection.
893 > 0 number of bytes read into the buffer. */
894 CIVETWEB_API int mg_read(struct mg_connection *, void *buf, size_t len);
895
896
897 /* Get the value of particular HTTP header.
898
899 This is a helper function. It traverses request_info->http_headers array,
900 and if the header is present in the array, returns its value. If it is
901 not present, NULL is returned. */
902 CIVETWEB_API const char *mg_get_header(const struct mg_connection *,
903 const char *name);
904
905
906 /* Get a value of particular form variable.
907
908 Parameters:
909 data: pointer to form-uri-encoded buffer. This could be either POST data,
910 or request_info.query_string.
911 data_len: length of the encoded data.
912 var_name: variable name to decode from the buffer
913 dst: destination buffer for the decoded variable
914 dst_len: length of the destination buffer
915
916 Return:
917 On success, length of the decoded variable.
918 On error:
919 -1 (variable not found).
920 -2 (destination buffer is NULL, zero length or too small to hold the
921 decoded variable).
922
923 Destination buffer is guaranteed to be '\0' - terminated if it is not
924 NULL or zero length. */
925 CIVETWEB_API int mg_get_var(const char *data,
926 size_t data_len,
927 const char *var_name,
928 char *dst,
929 size_t dst_len);
930
931
932 /* Get a value of particular form variable.
933
934 Parameters:
935 data: pointer to form-uri-encoded buffer. This could be either POST data,
936 or request_info.query_string.
937 data_len: length of the encoded data.
938 var_name: variable name to decode from the buffer
939 dst: destination buffer for the decoded variable
940 dst_len: length of the destination buffer
941 occurrence: which occurrence of the variable, 0 is the first, 1 the
942 second...
943 this makes it possible to parse a query like
944 b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1
945
946 Return:
947 On success, length of the decoded variable.
948 On error:
949 -1 (variable not found).
950 -2 (destination buffer is NULL, zero length or too small to hold the
951 decoded variable).
952
953 Destination buffer is guaranteed to be '\0' - terminated if it is not
954 NULL or zero length. */
955 CIVETWEB_API int mg_get_var2(const char *data,
956 size_t data_len,
957 const char *var_name,
958 char *dst,
959 size_t dst_len,
960 size_t occurrence);
961
962
963 /* Fetch value of certain cookie variable into the destination buffer.
964
965 Destination buffer is guaranteed to be '\0' - terminated. In case of
966 failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
967 parameter. This function returns only first occurrence.
968
969 Return:
970 On success, value length.
971 On error:
972 -1 (either "Cookie:" header is not present at all or the requested
973 parameter is not found).
974 -2 (destination buffer is NULL, zero length or too small to hold the
975 value). */
976 CIVETWEB_API int mg_get_cookie(const char *cookie,
977 const char *var_name,
978 char *buf,
979 size_t buf_len);
980
981
982 /* Download data from the remote web server.
983 host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
984 port: port number, e.g. 80.
985 use_ssl: wether to use SSL connection.
986 error_buffer, error_buffer_size: error message placeholder.
987 request_fmt,...: HTTP request.
988 Return:
989 On success, valid pointer to the new connection, suitable for mg_read().
990 On error, NULL. error_buffer contains error message.
991 Example:
992 char ebuf[100];
993 struct mg_connection *conn;
994 conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
995 "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
996 */
997 CIVETWEB_API struct mg_connection *
998 mg_download(const char *host,
999 int port,
1000 int use_ssl,
1001 char *error_buffer,
1002 size_t error_buffer_size,
1003 PRINTF_FORMAT_STRING(const char *request_fmt),
1004 ...) PRINTF_ARGS(6, 7);
1005
1006
1007 /* Close the connection opened by mg_download(). */
1008 CIVETWEB_API void mg_close_connection(struct mg_connection *conn);
1009
1010
1011 #if defined(MG_LEGACY_INTERFACE)
1012 /* File upload functionality. Each uploaded file gets saved into a temporary
1013 file and MG_UPLOAD event is sent.
1014 Return number of uploaded files.
1015 Deprecated: Use mg_handle_form_request instead. */
1016 CIVETWEB_API int mg_upload(struct mg_connection *conn,
1017 const char *destination_dir);
1018 #endif
1019
1020
1021 /* This structure contains callback functions for handling form fields.
1022 It is used as an argument to mg_handle_form_request. */
1023 struct mg_form_data_handler {
1024 /* This callback function is called, if a new field has been found.
1025 * The return value of this callback is used to define how the field
1026 * should be processed.
1027 *
1028 * Parameters:
1029 * key: Name of the field ("name" property of the HTML input field).
1030 * filename: Name of a file to upload, at the client computer.
1031 * Only set for input fields of type "file", otherwise NULL.
1032 * path: Output parameter: File name (incl. path) to store the file
1033 * at the server computer. Only used if FORM_FIELD_STORAGE_STORE
1034 * is returned by this callback. Existing files will be
1035 * overwritten.
1036 * pathlen: Length of the buffer for path.
1037 * user_data: Value of the member user_data of mg_form_data_handler
1038 *
1039 * Return value:
1040 * The callback must return the intended storage for this field
1041 * (See FORM_FIELD_STORAGE_*).
1042 */
1043 int (*field_found)(const char *key,
1044 const char *filename,
1045 char *path,
1046 size_t pathlen,
1047 void *user_data);
1048
1049 /* If the "field_found" callback returned FORM_FIELD_STORAGE_GET,
1050 * this callback will receive the field data.
1051 *
1052 * Parameters:
1053 * key: Name of the field ("name" property of the HTML input field).
1054 * value: Value of the input field.
1055 * user_data: Value of the member user_data of mg_form_data_handler
1056 *
1057 * Return value:
1058 * TODO: Needs to be defined.
1059 */
1060 int (*field_get)(const char *key,
1061 const char *value,
1062 size_t valuelen,
1063 void *user_data);
1064
1065 /* If the "field_found" callback returned FORM_FIELD_STORAGE_STORE,
1066 * the data will be stored into a file. If the file has been written
1067 * successfully, this callback will be called. This callback will
1068 * not be called for only partially uploaded files. The
1069 * mg_handle_form_request function will either store the file completely
1070 * and call this callback, or it will remove any partial content and
1071 * not call this callback function.
1072 *
1073 * Parameters:
1074 * path: Path of the file stored at the server.
1075 * file_size: Size of the stored file in bytes.
1076 * user_data: Value of the member user_data of mg_form_data_handler
1077 *
1078 * Return value:
1079 * TODO: Needs to be defined.
1080 */
1081 int (*field_store)(const char *path, long long file_size, void *user_data);
1082
1083 /* User supplied argument, passed to all callback functions. */
1084 void *user_data;
1085 };
1086
1087
1088 /* Return values definition for the "field_found" callback in
1089 * mg_form_data_handler. */
1090 /* Old nomenclature */
1091 enum {
1092 /* Skip this field (neither get nor store it). Continue with the
1093 * next field. */
1094 FORM_FIELD_STORAGE_SKIP = 0x0,
1095 /* Get the field value. */
1096 FORM_FIELD_STORAGE_GET = 0x1,
1097 /* Store the field value into a file. */
1098 FORM_FIELD_STORAGE_STORE = 0x2,
1099 /* Stop parsing this request. Skip the remaining fields. */
1100 FORM_FIELD_STORAGE_ABORT = 0x10
1101 };
1102
1103 /* New nomenclature */
1104 enum {
1105 /* Skip this field (neither get nor store it). Continue with the
1106 * next field. */
1107 MG_FORM_FIELD_STORAGE_SKIP = 0x0,
1108 /* Get the field value. */
1109 MG_FORM_FIELD_STORAGE_GET = 0x1,
1110 /* Store the field value into a file. */
1111 MG_FORM_FIELD_STORAGE_STORE = 0x2,
1112 /* Stop parsing this request. Skip the remaining fields. */
1113 MG_FORM_FIELD_STORAGE_ABORT = 0x10
1114 };
1115
1116 /* Process form data.
1117 * Returns the number of fields handled, or < 0 in case of an error.
1118 * Note: It is possible that several fields are already handled successfully
1119 * (e.g., stored into files), before the request handling is stopped with an
1120 * error. In this case a number < 0 is returned as well.
1121 * In any case, it is the duty of the caller to remove files once they are
1122 * no longer required. */
1123 CIVETWEB_API int mg_handle_form_request(struct mg_connection *conn,
1124 struct mg_form_data_handler *fdh);
1125
1126
1127 /* Convenience function -- create detached thread.
1128 Return: 0 on success, non-0 on error. */
1129 typedef void *(*mg_thread_func_t)(void *);
1130 CIVETWEB_API int mg_start_thread(mg_thread_func_t f, void *p);
1131
1132
1133 /* Return builtin mime type for the given file name.
1134 For unrecognized extensions, "text/plain" is returned. */
1135 CIVETWEB_API const char *mg_get_builtin_mime_type(const char *file_name);
1136
1137
1138 /* Get text representation of HTTP status code. */
1139 CIVETWEB_API const char *
1140 mg_get_response_code_text(const struct mg_connection *conn, int response_code);
1141
1142
1143 /* Return CivetWeb version. */
1144 CIVETWEB_API const char *mg_version(void);
1145
1146
1147 /* URL-decode input buffer into destination buffer.
1148 0-terminate the destination buffer.
1149 form-url-encoded data differs from URI encoding in a way that it
1150 uses '+' as character for space, see RFC 1866 section 8.2.1
1151 http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
1152 Return: length of the decoded data, or -1 if dst buffer is too small. */
1153 CIVETWEB_API int mg_url_decode(const char *src,
1154 int src_len,
1155 char *dst,
1156 int dst_len,
1157 int is_form_url_encoded);
1158
1159
1160 /* URL-encode input buffer into destination buffer.
1161 returns the length of the resulting buffer or -1
1162 is the buffer is too small. */
1163 CIVETWEB_API int mg_url_encode(const char *src, char *dst, size_t dst_len);
1164
1165
1166 /* MD5 hash given strings.
1167 Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
1168 ASCIIz strings. When function returns, buf will contain human-readable
1169 MD5 hash. Example:
1170 char buf[33];
1171 mg_md5(buf, "aa", "bb", NULL); */
1172 CIVETWEB_API char *mg_md5(char buf[33], ...);
1173
1174
1175 /* Print error message to the opened error log stream.
1176 This utilizes the provided logging configuration.
1177 conn: connection (not used for sending data, but to get perameters)
1178 fmt: format string without the line return
1179 ...: variable argument list
1180 Example:
1181 mg_cry(conn,"i like %s", "logging"); */
1182 CIVETWEB_API void mg_cry(const struct mg_connection *conn,
1183 PRINTF_FORMAT_STRING(const char *fmt),
1184 ...) PRINTF_ARGS(2, 3);
1185
1186
1187 /* utility methods to compare two buffers, case insensitive. */
1188 CIVETWEB_API int mg_strcasecmp(const char *s1, const char *s2);
1189 CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
1190
1191 /* set connection's http status */
1192 CIVETWEB_API void mg_set_http_status(struct mg_connection *conn, int status);
1193
1194 /* Connect to a websocket as a client
1195 Parameters:
1196 host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or
1197 "localhost"
1198 port: server port
1199 use_ssl: make a secure connection to server
1200 error_buffer, error_buffer_size: buffer for an error message
1201 path: server path you are trying to connect to, i.e. if connection to
1202 localhost/app, path should be "/app"
1203 origin: value of the Origin HTTP header
1204 data_func: callback that should be used when data is received from the
1205 server
1206 user_data: user supplied argument
1207
1208 Return:
1209 On success, valid mg_connection object.
1210 On error, NULL. Se error_buffer for details.
1211 */
1212 CIVETWEB_API struct mg_connection *
1213 mg_connect_websocket_client(const char *host,
1214 int port,
1215 int use_ssl,
1216 char *error_buffer,
1217 size_t error_buffer_size,
1218 const char *path,
1219 const char *origin,
1220 mg_websocket_data_handler data_func,
1221 mg_websocket_close_handler close_func,
1222 void *user_data);
1223
1224
1225 /* Connect to a TCP server as a client (can be used to connect to a HTTP server)
1226 Parameters:
1227 host: host to connect to, i.e. "www.wikipedia.org" or "192.168.1.1" or
1228 "localhost"
1229 port: server port
1230 use_ssl: make a secure connection to server
1231 error_buffer, error_buffer_size: buffer for an error message
1232
1233 Return:
1234 On success, valid mg_connection object.
1235 On error, NULL. Se error_buffer for details.
1236 */
1237 CIVETWEB_API struct mg_connection *mg_connect_client(const char *host,
1238 int port,
1239 int use_ssl,
1240 char *error_buffer,
1241 size_t error_buffer_size);
1242
1243
1244 struct mg_client_options {
1245 const char *host;
1246 int port;
1247 const char *client_cert;
1248 const char *server_cert;
1249 /* TODO: add more data */
1250 };
1251
1252
1253 CIVETWEB_API struct mg_connection *
1254 mg_connect_client_secure(const struct mg_client_options *client_options,
1255 char *error_buffer,
1256 size_t error_buffer_size);
1257
1258
1259 enum { TIMEOUT_INFINITE = -1 };
1260 enum { MG_TIMEOUT_INFINITE = -1 };
1261
1262 /* Wait for a response from the server
1263 Parameters:
1264 conn: connection
1265 ebuf, ebuf_len: error message placeholder.
1266 timeout: time to wait for a response in milliseconds (if < 0 then wait
1267 forever)
1268
1269 Return:
1270 On success, >= 0
1271 On error/timeout, < 0
1272 */
1273 CIVETWEB_API int mg_get_response(struct mg_connection *conn,
1274 char *ebuf,
1275 size_t ebuf_len,
1276 int timeout);
1277
1278
1279 /* Check which features where set when the civetweb library has been compiled.
1280 The function explicitly addresses compile time defines used when building
1281 the library - it does not mean, the feature has been initialized using a
1282 mg_init_library call.
1283 mg_check_feature can be called anytime, even before mg_init_library has
1284 been called.
1285
1286 Parameters:
1287 feature: specifies which feature should be checked
1288 The value is a bit mask. The individual bits are defined as:
1289 1 serve files (NO_FILES not set)
1290 2 support HTTPS (NO_SSL not set)
1291 4 support CGI (NO_CGI not set)
1292 8 support IPv6 (USE_IPV6 set)
1293 16 support WebSocket (USE_WEBSOCKET set)
1294 32 support Lua scripts and Lua server pages (USE_LUA is set)
1295 64 support server side JavaScript (USE_DUKTAPE is set)
1296 128 support caching (NO_CACHING not set)
1297 256 support server statistics (USE_SERVER_STATS is set)
1298 The result is undefined, if bits are set that do not represent a
1299 defined feature (currently: feature >= 512).
1300 The result is undefined, if no bit is set (feature == 0).
1301
1302 Return:
1303 If feature is available, the corresponding bit is set
1304 If feature is not available, the bit is 0
1305 */
1306 CIVETWEB_API unsigned mg_check_feature(unsigned feature);
1307
1308
1309 /* Get information on the system. Useful for support requests.
1310 Parameters:
1311 buffer: Store system information as string here.
1312 buflen: Length of buffer (including a byte required for a terminating 0).
1313 Return:
1314 Available size of system information, exluding a terminating 0.
1315 The information is complete, if the return value is smaller than buflen.
1316 The result is a JSON formatted string, the exact content may vary.
1317 Note:
1318 It is possible to determine the required buflen, by first calling this
1319 function with buffer = NULL and buflen = NULL. The required buflen is
1320 one byte more than the returned value.
1321 */
1322 CIVETWEB_API int mg_get_system_info(char *buffer, int buflen);
1323
1324
1325 /* Get context information. Useful for server diagnosis.
1326 Parameters:
1327 ctx: Context handle
1328 buffer: Store context information here.
1329 buflen: Length of buffer (including a byte required for a terminating 0).
1330 Return:
1331 Available size of system information, exluding a terminating 0.
1332 The information is complete, if the return value is smaller than buflen.
1333 The result is a JSON formatted string, the exact content may vary.
1334 Note:
1335 It is possible to determine the required buflen, by first calling this
1336 function with buffer = NULL and buflen = NULL. The required buflen is
1337 one byte more than the returned value. However, since the available
1338 context information changes, you should allocate a few bytes more.
1339 */
1340 CIVETWEB_API int
1341 mg_get_context_info(const struct mg_context *ctx, char *buffer, int buflen);
1342
1343
1344 #ifdef MG_EXPERIMENTAL_INTERFACES
1345 /* Get connection information. Useful for server diagnosis.
1346 Parameters:
1347 ctx: Context handle
1348 idx: Connection index
1349 buffer: Store context information here.
1350 buflen: Length of buffer (including a byte required for a terminating 0).
1351 Return:
1352 Available size of system information, exluding a terminating 0.
1353 The information is complete, if the return value is smaller than buflen.
1354 The result is a JSON formatted string, the exact content may vary.
1355 Note:
1356 It is possible to determine the required buflen, by first calling this
1357 function with buffer = NULL and buflen = NULL. The required buflen is
1358 one byte more than the returned value. However, since the available
1359 context information changes, you should allocate a few bytes more.
1360 */
1361 CIVETWEB_API int mg_get_connection_info(const struct mg_context *ctx,
1362 int idx,
1363 char *buffer,
1364 int buflen);
1365 #endif
1366
1367
1368 #ifdef __cplusplus
1369 }
1370 #endif /* __cplusplus */
1371
1372 #endif /* CIVETWEB_HEADER_INCLUDED */