civetweb.h 50 KB

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