civetweb.h 25 KB

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