civetweb.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /* Copyright (c) 2013-2015 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.8"
  25. #ifndef CIVETWEB_API
  26. #if defined(_WIN32)
  27. #if defined(CIVETWEB_DLL_EXPORTS)
  28. #define CIVETWEB_API __declspec(dllexport)
  29. #elif defined(CIVETWEB_DLL_IMPORTS)
  30. #define CIVETWEB_API __declspec(dllimport)
  31. #else
  32. #define CIVETWEB_API
  33. #endif
  34. #elif __GNUC__ >= 4
  35. #define CIVETWEB_API __attribute__((visibility("default")))
  36. #else
  37. #define CIVETWEB_API
  38. #endif
  39. #endif
  40. #include <stdio.h>
  41. #include <stddef.h>
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif /* __cplusplus */
  45. struct mg_context; /* Handle for the HTTP service itself */
  46. struct mg_connection; /* Handle for the individual connection */
  47. /* This structure contains information about the HTTP request. */
  48. struct mg_request_info {
  49. const char *request_method; /* "GET", "POST", etc */
  50. const char *uri; /* URL-decoded URI */
  51. const char *http_version; /* E.g. "1.0", "1.1" */
  52. const char *query_string; /* URL part after '?', not including '?', or
  53. NULL */
  54. const char *remote_user; /* Authenticated user, or NULL if no auth
  55. used */
  56. char remote_addr[48]; /* Client's IP address as a string. */
  57. long remote_ip; /* Client's IP address. Deprecated: use remote_addr instead
  58. */
  59. long long content_length; /* Length (in bytes) of the request body,
  60. can be -1 if no length was given. */
  61. int remote_port; /* Client's port */
  62. int is_ssl; /* 1 if SSL-ed, 0 if not */
  63. void *user_data; /* User data pointer passed to mg_start() */
  64. void *conn_data; /* Connection-specific user data */
  65. int num_headers; /* Number of HTTP headers */
  66. struct mg_header {
  67. const char *name; /* HTTP header name */
  68. const char *value; /* HTTP header value */
  69. } http_headers[64]; /* Maximum 64 headers */
  70. };
  71. /* This structure needs to be passed to mg_start(), to let civetweb know
  72. which callbacks to invoke. For a detailed description, see
  73. https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md */
  74. struct mg_callbacks {
  75. /* Called when civetweb has received new HTTP request.
  76. If the callback returns one, it must process the request
  77. by sending valid HTTP headers and a body. Civetweb will not do
  78. any further processing. Otherwise it must return zero.
  79. Note that since V1.7 the "begin_request" function is called
  80. before an authorization check. If an authorization check is
  81. required, use a request_handler instead.
  82. Return value:
  83. 0: civetweb will process the request itself. In this case,
  84. the callback must not send any data to the client.
  85. 1-999: callback already processed the request. Civetweb will
  86. not send any data after the callback returned. The
  87. return code is stored as a HTTP status code for the
  88. access log. */
  89. int (*begin_request)(struct mg_connection *);
  90. /* Called when civetweb has finished processing request. */
  91. void (*end_request)(const struct mg_connection *, int reply_status_code);
  92. /* Called when civetweb is about to log a message. If callback returns
  93. non-zero, civetweb does not log anything. */
  94. int (*log_message)(const struct mg_connection *, const char *message);
  95. /* Called when civetweb is about to log access. If callback returns
  96. non-zero, civetweb does not log anything. */
  97. int (*log_access)(const struct mg_connection *, const char *message);
  98. /* Called when civetweb initializes SSL library.
  99. Parameters:
  100. user_data: parameter user_data passed when starting the server.
  101. Return value:
  102. 0: civetweb will set up the SSL certificate.
  103. 1: civetweb assumes the callback already set up the certificate.
  104. -1: initializing ssl fails. */
  105. int (*init_ssl)(void *ssl_context, void *user_data);
  106. /* Called when websocket request is received, before websocket handshake.
  107. Return value:
  108. 0: civetweb proceeds with websocket handshake.
  109. 1: connection is closed immediately.
  110. This callback is deprecated, use mg_set_websocket_handler instead. */
  111. int (*websocket_connect)(const struct mg_connection *);
  112. /* Called when websocket handshake is successfully completed, and
  113. connection is ready for data exchange.
  114. This callback is deprecated, use mg_set_websocket_handler instead. */
  115. void (*websocket_ready)(struct mg_connection *);
  116. /* Called when data frame has been received from the client.
  117. Parameters:
  118. bits: first byte of the websocket frame, see websocket RFC at
  119. http://tools.ietf.org/html/rfc6455, section 5.2
  120. data, data_len: payload, with mask (if any) already applied.
  121. Return value:
  122. 1: keep this websocket connection open.
  123. 0: close this websocket connection.
  124. This callback is deprecated, use mg_set_websocket_handler instead. */
  125. int (*websocket_data)(struct mg_connection *,
  126. int bits,
  127. char *data,
  128. size_t data_len);
  129. /* Called when civetweb is closing a connection. The per-context mutex is
  130. locked when this is invoked. This is primarily useful for noting when
  131. a websocket is closing and removing it from any application-maintained
  132. list of clients.
  133. Using this callback for websocket connections is deprecated, use
  134. mg_set_websocket_handler instead. */
  135. void (*connection_close)(const struct mg_connection *);
  136. /* Called when civetweb tries to open a file. Used to intercept file open
  137. calls, and serve file data from memory instead.
  138. Parameters:
  139. path: Full path to the file to open.
  140. data_len: Placeholder for the file size, if file is served from
  141. memory.
  142. Return value:
  143. NULL: do not serve file from memory, proceed with normal file open.
  144. non-NULL: pointer to the file contents in memory. data_len must be
  145. initilized with the size of the memory block. */
  146. const char *(*open_file)(const struct mg_connection *,
  147. const char *path,
  148. size_t *data_len);
  149. /* Called when civetweb is about to serve Lua server page, if
  150. Lua support is enabled.
  151. Parameters:
  152. lua_context: "lua_State *" pointer. */
  153. void (*init_lua)(const struct mg_connection *, void *lua_context);
  154. /* Called when civetweb has uploaded a file to a temporary directory as a
  155. result of mg_upload() call.
  156. Parameters:
  157. file_name: full path name to the uploaded file. */
  158. void (*upload)(struct mg_connection *, const char *file_name);
  159. /* Called when civetweb is about to send HTTP error to the client.
  160. Implementing this callback allows to create custom error pages.
  161. Parameters:
  162. status: HTTP error status code.
  163. Return value:
  164. 1: run civetweb error handler.
  165. 0: callback already handled the error. */
  166. int (*http_error)(struct mg_connection *, int status);
  167. /* Called after civetweb context has been created, before requests
  168. are processed.
  169. Parameters:
  170. ctx: context handle */
  171. void (*init_context)(const struct mg_context *ctx);
  172. /* Called when civetweb context is deleted.
  173. Parameters:
  174. ctx: context handle */
  175. void (*exit_context)(const struct mg_context *ctx);
  176. };
  177. /* Start web server.
  178. Parameters:
  179. callbacks: mg_callbacks structure with user-defined callbacks.
  180. options: NULL terminated list of option_name, option_value pairs that
  181. specify Civetweb configuration parameters.
  182. Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
  183. processing is required for these, signal handlers must be set up
  184. after calling mg_start().
  185. Example:
  186. const char *options[] = {
  187. "document_root", "/var/www",
  188. "listening_ports", "80,443s",
  189. NULL
  190. };
  191. struct mg_context *ctx = mg_start(&my_func, NULL, options);
  192. Refer to https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md
  193. for the list of valid option and their possible values.
  194. Return:
  195. web server context, or NULL on error. */
  196. CIVETWEB_API struct mg_context *mg_start(const struct mg_callbacks *callbacks,
  197. void *user_data,
  198. const char **configuration_options);
  199. /* Stop the web server.
  200. Must be called last, when an application wants to stop the web server and
  201. release all associated resources. This function blocks until all Civetweb
  202. threads are stopped. Context pointer becomes invalid. */
  203. CIVETWEB_API void mg_stop(struct mg_context *);
  204. /* mg_request_handler
  205. Called when a new request comes in. This callback is URI based
  206. and configured with mg_set_request_handler().
  207. Parameters:
  208. conn: current connection information.
  209. cbdata: the callback data configured with mg_set_request_handler().
  210. Returns:
  211. 0: the handler could not handle the request, so fall through.
  212. 1 - 999: the handler processed the request. The return code is
  213. stored as a HTTP status code for the access log. */
  214. typedef int (*mg_request_handler)(struct mg_connection *conn, void *cbdata);
  215. /* mg_set_request_handler
  216. Sets or removes a URI mapping for a request handler.
  217. This function uses mg_lock_context internally.
  218. URI's are ordered and prefixed URI's are supported. For example,
  219. consider two URIs: /a/b and /a
  220. /a matches /a
  221. /a/b matches /a/b
  222. /a/c matches /a
  223. Parameters:
  224. ctx: server context
  225. uri: the URI (exact or pattern) for the handler
  226. handler: the callback handler to use when the URI is requested.
  227. If NULL, an already registered handler for this URI will be
  228. removed.
  229. The URI used to remove a handler must match exactly the one used
  230. to
  231. register it (not only a pattern match).
  232. cbdata: the callback data to give to the handler when it is called. */
  233. CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx,
  234. const char *uri,
  235. mg_request_handler handler,
  236. void *cbdata);
  237. /* Callback types for websocket handlers in C/C++.
  238. mg_websocket_connect_handler
  239. Is called when the client intends to establish a websocket connection,
  240. before websocket handshake.
  241. Return value:
  242. 0: civetweb proceeds with websocket handshake.
  243. 1: connection is closed immediately.
  244. mg_websocket_ready_handler
  245. Is called when websocket handshake is successfully completed, and
  246. connection is ready for data exchange.
  247. mg_websocket_data_handler
  248. Is called when a data frame has been received from the client.
  249. Parameters:
  250. bits: first byte of the websocket frame, see websocket RFC at
  251. http://tools.ietf.org/html/rfc6455, section 5.2
  252. data, data_len: payload, with mask (if any) already applied.
  253. Return value:
  254. 1: keep this websocket connection open.
  255. 0: close this websocket connection.
  256. mg_connection_close_handler
  257. Is called, when the connection is closed.*/
  258. typedef int (*mg_websocket_connect_handler)(const struct mg_connection *,
  259. void *);
  260. typedef void (*mg_websocket_ready_handler)(struct mg_connection *, void *);
  261. typedef int (*mg_websocket_data_handler)(
  262. struct mg_connection *, int, char *, size_t, void *);
  263. typedef void (*mg_websocket_close_handler)(const struct mg_connection *,
  264. void *);
  265. /* mg_set_websocket_handler
  266. Set or remove handler functions for websocket connections.
  267. This function works similar to mg_set_request_handler - see there. */
  268. CIVETWEB_API void
  269. mg_set_websocket_handler(struct mg_context *ctx,
  270. const char *uri,
  271. mg_websocket_connect_handler connect_handler,
  272. mg_websocket_ready_handler ready_handler,
  273. mg_websocket_data_handler data_handler,
  274. mg_websocket_close_handler close_handler,
  275. void *cbdata);
  276. /* Get the value of particular configuration parameter.
  277. The value returned is read-only. Civetweb does not allow changing
  278. configuration at run time.
  279. If given parameter name is not valid, NULL is returned. For valid
  280. names, return value is guaranteed to be non-NULL. If parameter is not
  281. set, zero-length string is returned. */
  282. CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx,
  283. const char *name);
  284. /* Get context from connection. */
  285. CIVETWEB_API struct mg_context *
  286. mg_get_context(const struct mg_connection *conn);
  287. /* Get user data passed to mg_start from context. */
  288. CIVETWEB_API void *mg_get_user_data(const struct mg_context *ctx);
  289. /* Set user data for the current connection. */
  290. CIVETWEB_API void mg_set_user_connection_data(const struct mg_connection *conn,
  291. void *data);
  292. /* Get user data set for the current connection. */
  293. CIVETWEB_API void *
  294. mg_get_user_connection_data(const struct mg_connection *conn);
  295. #if defined(MG_LEGACY_INTERFACE)
  296. /* Return array of strings that represent valid configuration options.
  297. For each option, option name and default value is returned, i.e. the
  298. number of entries in the array equals to number_of_options x 2.
  299. Array is NULL terminated. */
  300. /* Deprecated: Use mg_get_valid_options instead. */
  301. CIVETWEB_API const char **mg_get_valid_option_names(void);
  302. #endif
  303. struct mg_option {
  304. const char *name;
  305. int type;
  306. const char *default_value;
  307. };
  308. enum {
  309. CONFIG_TYPE_UNKNOWN = 0x0,
  310. CONFIG_TYPE_NUMBER = 0x1,
  311. CONFIG_TYPE_STRING = 0x2,
  312. CONFIG_TYPE_FILE = 0x3,
  313. CONFIG_TYPE_DIRECTORY = 0x4,
  314. CONFIG_TYPE_BOOLEAN = 0x5,
  315. CONFIG_TYPE_EXT_PATTERN = 0x6
  316. };
  317. /* Return array of struct mg_option, representing all valid configuration
  318. options of civetweb.c.
  319. The array is terminated by a NULL name option. */
  320. CIVETWEB_API const struct mg_option *mg_get_valid_options(void);
  321. /* Get the list of ports that civetweb is listening on.
  322. size is the size of the ports int array and ssl int array to fill.
  323. It is the caller's responsibility to make sure ports and ssl each
  324. contain at least size int elements worth of memory to write into.
  325. Return value is the number of ports and ssl information filled in.
  326. The value returned is read-only. Civetweb does not allow changing
  327. configuration at run time. */
  328. CIVETWEB_API size_t
  329. mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl);
  330. /* Add, edit or delete the entry in the passwords file.
  331. This function allows an application to manipulate .htpasswd files on the
  332. fly by adding, deleting and changing user records. This is one of the
  333. several ways of implementing authentication on the server side. For another,
  334. cookie-based way please refer to the examples/chat in the source tree.
  335. If password is not NULL, entry is added (or modified if already exists).
  336. If password is NULL, entry is deleted.
  337. Return:
  338. 1 on success, 0 on error. */
  339. CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name,
  340. const char *domain,
  341. const char *user,
  342. const char *password);
  343. /* Return information associated with the request. */
  344. CIVETWEB_API const struct mg_request_info *
  345. mg_get_request_info(const struct mg_connection *);
  346. /* Send data to the client.
  347. Return:
  348. 0 when the connection has been closed
  349. -1 on error
  350. >0 number of bytes written on success */
  351. CIVETWEB_API int mg_write(struct mg_connection *, const void *buf, size_t len);
  352. /* Send data to a websocket client wrapped in a websocket frame. Uses
  353. mg_lock_connection to ensure that the transmission is not interrupted,
  354. i.e., when the application is proactively communicating and responding to
  355. a request simultaneously.
  356. Send data to a websocket client wrapped in a websocket frame.
  357. This function is available when civetweb is compiled with -DUSE_WEBSOCKET
  358. Return:
  359. 0 when the connection has been closed
  360. -1 on error
  361. >0 number of bytes written on success */
  362. CIVETWEB_API int mg_websocket_write(struct mg_connection *conn,
  363. int opcode,
  364. const char *data,
  365. size_t data_len);
  366. /* Blocks until unique access is obtained to this connection. Intended for use
  367. with websockets only.
  368. Invoke this before mg_write or mg_printf when communicating with a
  369. websocket if your code has server-initiated communication as well as
  370. communication in direct response to a message. */
  371. CIVETWEB_API void mg_lock_connection(struct mg_connection *conn);
  372. CIVETWEB_API void mg_unlock_connection(struct mg_connection *conn);
  373. #if defined(MG_LEGACY_INTERFACE)
  374. #define mg_lock mg_lock_connection
  375. #define mg_unlock mg_unlock_connection
  376. #endif
  377. /* Lock server context. This lock may be used to protect resources
  378. that are shared between different connection/worker threads. */
  379. CIVETWEB_API void mg_lock_context(struct mg_context *ctx);
  380. CIVETWEB_API void mg_unlock_context(struct mg_context *ctx);
  381. /* Opcodes, from http://tools.ietf.org/html/rfc6455 */
  382. enum {
  383. WEBSOCKET_OPCODE_CONTINUATION = 0x0,
  384. WEBSOCKET_OPCODE_TEXT = 0x1,
  385. WEBSOCKET_OPCODE_BINARY = 0x2,
  386. WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
  387. WEBSOCKET_OPCODE_PING = 0x9,
  388. WEBSOCKET_OPCODE_PONG = 0xa
  389. };
  390. /* Macros for enabling compiler-specific checks for printf-like arguments. */
  391. #undef PRINTF_FORMAT_STRING
  392. #if defined(_MSC_VER) && _MSC_VER >= 1400
  393. #include <sal.h>
  394. #if defined(_MSC_VER) && _MSC_VER > 1400
  395. #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
  396. #else
  397. #define PRINTF_FORMAT_STRING(s) __format_string s
  398. #endif
  399. #else
  400. #define PRINTF_FORMAT_STRING(s) s
  401. #endif
  402. #ifdef __GNUC__
  403. #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
  404. #else
  405. #define PRINTF_ARGS(x, y)
  406. #endif
  407. /* Send data to the client using printf() semantics.
  408. Works exactly like mg_write(), but allows to do message formatting. */
  409. CIVETWEB_API int mg_printf(struct mg_connection *,
  410. PRINTF_FORMAT_STRING(const char *fmt),
  411. ...) PRINTF_ARGS(2, 3);
  412. /* Send contents of the entire file together with HTTP headers. */
  413. CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
  414. /* Read data from the remote end, return number of bytes read.
  415. Return:
  416. 0 connection has been closed by peer. No more data could be read.
  417. < 0 read error. No more data could be read from the connection.
  418. > 0 number of bytes read into the buffer. */
  419. CIVETWEB_API int mg_read(struct mg_connection *, void *buf, size_t len);
  420. /* Get the value of particular HTTP header.
  421. This is a helper function. It traverses request_info->http_headers array,
  422. and if the header is present in the array, returns its value. If it is
  423. not present, NULL is returned. */
  424. CIVETWEB_API const char *mg_get_header(const struct mg_connection *,
  425. const char *name);
  426. /* Get a value of particular form variable.
  427. Parameters:
  428. data: pointer to form-uri-encoded buffer. This could be either POST data,
  429. or request_info.query_string.
  430. data_len: length of the encoded data.
  431. var_name: variable name to decode from the buffer
  432. dst: destination buffer for the decoded variable
  433. dst_len: length of the destination buffer
  434. Return:
  435. On success, length of the decoded variable.
  436. On error:
  437. -1 (variable not found).
  438. -2 (destination buffer is NULL, zero length or too small to hold the
  439. decoded variable).
  440. Destination buffer is guaranteed to be '\0' - terminated if it is not
  441. NULL or zero length. */
  442. CIVETWEB_API int mg_get_var(const char *data,
  443. size_t data_len,
  444. const char *var_name,
  445. char *dst,
  446. size_t dst_len);
  447. /* Get a value of particular form variable.
  448. Parameters:
  449. data: pointer to form-uri-encoded buffer. This could be either POST data,
  450. or request_info.query_string.
  451. data_len: length of the encoded data.
  452. var_name: variable name to decode from the buffer
  453. dst: destination buffer for the decoded variable
  454. dst_len: length of the destination buffer
  455. occurrence: which occurrence of the variable, 0 is the first, 1 the
  456. second...
  457. this makes it possible to parse a query like
  458. b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1
  459. Return:
  460. On success, length of the decoded variable.
  461. On error:
  462. -1 (variable not found).
  463. -2 (destination buffer is NULL, zero length or too small to hold the
  464. decoded variable).
  465. Destination buffer is guaranteed to be '\0' - terminated if it is not
  466. NULL or zero length. */
  467. CIVETWEB_API int mg_get_var2(const char *data,
  468. size_t data_len,
  469. const char *var_name,
  470. char *dst,
  471. size_t dst_len,
  472. size_t occurrence);
  473. /* Fetch value of certain cookie variable into the destination buffer.
  474. Destination buffer is guaranteed to be '\0' - terminated. In case of
  475. failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  476. parameter. This function returns only first occurrence.
  477. Return:
  478. On success, value length.
  479. On error:
  480. -1 (either "Cookie:" header is not present at all or the requested
  481. parameter is not found).
  482. -2 (destination buffer is NULL, zero length or too small to hold the
  483. value). */
  484. CIVETWEB_API int mg_get_cookie(const char *cookie,
  485. const char *var_name,
  486. char *buf,
  487. size_t buf_len);
  488. /* Download data from the remote web server.
  489. host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
  490. port: port number, e.g. 80.
  491. use_ssl: wether to use SSL connection.
  492. error_buffer, error_buffer_size: error message placeholder.
  493. request_fmt,...: HTTP request.
  494. Return:
  495. On success, valid pointer to the new connection, suitable for mg_read().
  496. On error, NULL. error_buffer contains error message.
  497. Example:
  498. char ebuf[100];
  499. struct mg_connection *conn;
  500. conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
  501. "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
  502. */
  503. CIVETWEB_API struct mg_connection *
  504. mg_download(const char *host,
  505. int port,
  506. int use_ssl,
  507. char *error_buffer,
  508. size_t error_buffer_size,
  509. PRINTF_FORMAT_STRING(const char *request_fmt),
  510. ...) PRINTF_ARGS(6, 7);
  511. /* Close the connection opened by mg_download(). */
  512. CIVETWEB_API void mg_close_connection(struct mg_connection *conn);
  513. /* File upload functionality. Each uploaded file gets saved into a temporary
  514. file and MG_UPLOAD event is sent.
  515. Return number of uploaded files. */
  516. CIVETWEB_API int mg_upload(struct mg_connection *conn,
  517. const char *destination_dir);
  518. /* Convenience function -- create detached thread.
  519. Return: 0 on success, non-0 on error. */
  520. typedef void *(*mg_thread_func_t)(void *);
  521. CIVETWEB_API int mg_start_thread(mg_thread_func_t f, void *p);
  522. /* Return builtin mime type for the given file name.
  523. For unrecognized extensions, "text/plain" is returned. */
  524. CIVETWEB_API const char *mg_get_builtin_mime_type(const char *file_name);
  525. /* Return Civetweb version. */
  526. CIVETWEB_API const char *mg_version(void);
  527. /* URL-decode input buffer into destination buffer.
  528. 0-terminate the destination buffer.
  529. form-url-encoded data differs from URI encoding in a way that it
  530. uses '+' as character for space, see RFC 1866 section 8.2.1
  531. http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  532. Return: length of the decoded data, or -1 if dst buffer is too small. */
  533. CIVETWEB_API int mg_url_decode(const char *src,
  534. int src_len,
  535. char *dst,
  536. int dst_len,
  537. int is_form_url_encoded);
  538. /* URL-encode input buffer into destination buffer.
  539. returns the length of the resulting buffer or -1
  540. is the buffer is too small. */
  541. CIVETWEB_API int mg_url_encode(const char *src, char *dst, size_t dst_len);
  542. /* MD5 hash given strings.
  543. Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  544. ASCIIz strings. When function returns, buf will contain human-readable
  545. MD5 hash. Example:
  546. char buf[33];
  547. mg_md5(buf, "aa", "bb", NULL); */
  548. CIVETWEB_API char *mg_md5(char buf[33], ...);
  549. /* Print error message to the opened error log stream.
  550. This utilizes the provided logging configuration.
  551. conn: connection
  552. fmt: format string without the line return
  553. ...: variable argument list
  554. Example:
  555. mg_cry(conn,"i like %s", "logging"); */
  556. CIVETWEB_API void mg_cry(const struct mg_connection *conn,
  557. PRINTF_FORMAT_STRING(const char *fmt),
  558. ...) PRINTF_ARGS(2, 3);
  559. /* utility method to compare two buffers, case incensitive. */
  560. CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
  561. /* Connect to a websocket as a client
  562. Parameters:
  563. host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or
  564. "localhost"
  565. port: server port
  566. use_ssl: make a secure connection to server
  567. error_buffer, error_buffer_size: buffer for an error message
  568. path: server path you are trying to connect to, i.e. if connection to
  569. localhost/app, path should be "/app"
  570. origin: value of the Origin HTTP header
  571. data_func: callback that should be used when data is received from the
  572. server
  573. user_data: user supplied argument
  574. Return:
  575. On success, valid mg_connection object.
  576. On error, NULL. Se error_buffer for details.
  577. */
  578. CIVETWEB_API struct mg_connection *
  579. mg_connect_websocket_client(const char *host,
  580. int port,
  581. int use_ssl,
  582. char *error_buffer,
  583. size_t error_buffer_size,
  584. const char *path,
  585. const char *origin,
  586. mg_websocket_data_handler data_func,
  587. mg_websocket_close_handler close_func,
  588. void *user_data);
  589. /* Connect to a TCP server as a client (can be used to connect to a HTTP server)
  590. Parameters:
  591. host: host to connect to, i.e. "www.wikipedia.org" or "192.168.1.1" or
  592. "localhost"
  593. port: server port
  594. use_ssl: make a secure connection to server
  595. error_buffer, error_buffer_size: buffer for an error message
  596. Return:
  597. On success, valid mg_connection object.
  598. On error, NULL. Se error_buffer for details.
  599. */
  600. CIVETWEB_API struct mg_connection *mg_connect_client(const char *host,
  601. int port,
  602. int use_ssl,
  603. char *error_buffer,
  604. size_t error_buffer_size);
  605. enum { TIMEOUT_INFINITE = -1 };
  606. /* Wait for a response from the server
  607. Parameters:
  608. conn: connection
  609. ebuf, ebuf_len: error message placeholder.
  610. timeout: time to wait for a response in milliseconds (if < 0 then wait
  611. forever)
  612. Return:
  613. On success, >= 0
  614. On error/timeout, < 0
  615. */
  616. CIVETWEB_API int mg_get_response(struct mg_connection *conn,
  617. char *ebuf,
  618. size_t ebuf_len,
  619. int timeout);
  620. /* Check which features where set when civetweb has been compiled.
  621. Parameters:
  622. feature: specifies which feature should be checked
  623. 1 serve files (NO_FILES not set)
  624. 2 support HTTPS (NO_SSL not set)
  625. 4 support CGI (NO_CGI not set)
  626. 8 support IPv6 (USE_IPV6 set)
  627. 16 support WebSocket (USE_WEBSOCKET set)
  628. 32 support Lua scripts and Lua server pages (USE_LUA is set)
  629. Return:
  630. If feature is available > 0
  631. If feature is not available = 0
  632. */
  633. CIVETWEB_API unsigned mg_check_feature(unsigned feature);
  634. #ifdef __cplusplus
  635. }
  636. #endif /* __cplusplus */
  637. #endif /* CIVETWEB_HEADER_INCLUDED */