civetweb.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. URI's are ordered and prefixed URI's are supported. For example,
  206. consider two URIs: /a/b and /a
  207. /a matches /a
  208. /a/b matches /a/b
  209. /a/c matches /a
  210. Parameters:
  211. ctx: server context
  212. uri: the URI to configure
  213. handler: the callback handler to use when the URI is requested.
  214. If NULL, the URI will be removed.
  215. cbdata: the callback data to give to the handler when it s requested. */
  216. CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata);
  217. /* Get the value of particular configuration parameter.
  218. The value returned is read-only. Civetweb does not allow changing
  219. configuration at run time.
  220. If given parameter name is not valid, NULL is returned. For valid
  221. names, return value is guaranteed to be non-NULL. If parameter is not
  222. set, zero-length string is returned. */
  223. CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx, const char *name);
  224. /* Get context from connection. */
  225. CIVETWEB_API struct mg_context *mg_get_context(struct mg_connection *conn);
  226. /* Get user data passed to mg_start from context. */
  227. CIVETWEB_API void *mg_get_user_data(struct mg_context *ctx);
  228. #if defined(MG_LEGACY_INTERFACE)
  229. /* Return array of strings that represent valid configuration options.
  230. For each option, option name and default value is returned, i.e. the
  231. number of entries in the array equals to number_of_options x 2.
  232. Array is NULL terminated. */
  233. /* Deprecated: Use mg_get_valid_options instead. */
  234. CIVETWEB_API const char **mg_get_valid_option_names(void);
  235. #endif
  236. struct mg_option {
  237. const char * name;
  238. int type;
  239. const char * default_value;
  240. };
  241. enum {
  242. CONFIG_TYPE_UNKNOWN = 0x0,
  243. CONFIG_TYPE_NUMBER = 0x1,
  244. CONFIG_TYPE_STRING = 0x2,
  245. CONFIG_TYPE_FILE = 0x3,
  246. CONFIG_TYPE_DIRECTORY = 0x4,
  247. CONFIG_TYPE_BOOLEAN = 0x5,
  248. CONFIG_TYPE_EXT_PATTERN = 0x6
  249. };
  250. /* Return array of struct mg_option, representing all valid configuration
  251. options of civetweb.c.
  252. The array is terminated by a NULL name option. */
  253. CIVETWEB_API const struct mg_option *mg_get_valid_options(void);
  254. /* Get the list of ports that civetweb is listening on.
  255. size is the size of the ports int array and ssl int array to fill.
  256. It is the caller's responsibility to make sure ports and ssl each
  257. contain at least size int elements worth of memory to write into.
  258. Return value is the number of ports and ssl information filled in.
  259. The value returned is read-only. Civetweb does not allow changing
  260. configuration at run time. */
  261. CIVETWEB_API size_t mg_get_ports(const struct mg_context *ctx, size_t size, int* ports, int* ssl);
  262. /* Add, edit or delete the entry in the passwords file.
  263. This function allows an application to manipulate .htpasswd files on the
  264. fly by adding, deleting and changing user records. This is one of the
  265. several ways of implementing authentication on the server side. For another,
  266. cookie-based way please refer to the examples/chat in the source tree.
  267. If password is not NULL, entry is added (or modified if already exists).
  268. If password is NULL, entry is deleted.
  269. Return:
  270. 1 on success, 0 on error. */
  271. CIVETWEB_API int mg_modify_passwords_file(const char *passwords_file_name,
  272. const char *domain,
  273. const char *user,
  274. const char *password);
  275. /* Return information associated with the request. */
  276. CIVETWEB_API struct mg_request_info *mg_get_request_info(struct mg_connection *);
  277. /* Send data to the client.
  278. Return:
  279. 0 when the connection has been closed
  280. -1 on error
  281. >0 number of bytes written on success */
  282. CIVETWEB_API int mg_write(struct mg_connection *, const void *buf, size_t len);
  283. /* Send data to a websocket client wrapped in a websocket frame. Uses mg_lock
  284. to ensure that the transmission is not interrupted, i.e., when the
  285. application is proactively communicating and responding to a request
  286. simultaneously.
  287. Send data to a websocket client wrapped in a websocket frame.
  288. This function is available when civetweb is compiled with -DUSE_WEBSOCKET
  289. Return:
  290. 0 when the connection has been closed
  291. -1 on error
  292. >0 number of bytes written on success */
  293. CIVETWEB_API int mg_websocket_write(struct mg_connection* conn, int opcode,
  294. const char *data, size_t data_len);
  295. /* Blocks until unique access is obtained to this connection. Intended for use
  296. with websockets only.
  297. Invoke this before mg_write or mg_printf when communicating with a
  298. websocket if your code has server-initiated communication as well as
  299. communication in direct response to a message. */
  300. CIVETWEB_API void mg_lock_connection(struct mg_connection* conn);
  301. CIVETWEB_API void mg_unlock_connection(struct mg_connection* conn);
  302. #if defined(MG_LEGACY_INTERFACE)
  303. #define mg_lock mg_lock_connection
  304. #define mg_unlock mg_unlock_connection
  305. #endif
  306. /* Lock server context. This lock may be used to protect ressources
  307. that are shared between different connection/worker threads. */
  308. CIVETWEB_API void mg_lock_context(struct mg_context* ctx);
  309. CIVETWEB_API void mg_unlock_context(struct mg_context* ctx);
  310. /* Opcodes, from http://tools.ietf.org/html/rfc6455 */
  311. enum {
  312. WEBSOCKET_OPCODE_CONTINUATION = 0x0,
  313. WEBSOCKET_OPCODE_TEXT = 0x1,
  314. WEBSOCKET_OPCODE_BINARY = 0x2,
  315. WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
  316. WEBSOCKET_OPCODE_PING = 0x9,
  317. WEBSOCKET_OPCODE_PONG = 0xa
  318. };
  319. /* Macros for enabling compiler-specific checks for printf-like arguments. */
  320. #undef PRINTF_FORMAT_STRING
  321. #if defined(_MSC_VER) && _MSC_VER >= 1400
  322. #include <sal.h>
  323. #if defined(_MSC_VER) && _MSC_VER > 1400
  324. #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
  325. #else
  326. #define PRINTF_FORMAT_STRING(s) __format_string s
  327. #endif
  328. #else
  329. #define PRINTF_FORMAT_STRING(s) s
  330. #endif
  331. #ifdef __GNUC__
  332. #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
  333. #else
  334. #define PRINTF_ARGS(x, y)
  335. #endif
  336. /* Send data to the client using printf() semantics.
  337. Works exactly like mg_write(), but allows to do message formatting. */
  338. CIVETWEB_API int mg_printf(struct mg_connection *,
  339. PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
  340. /* Send contents of the entire file together with HTTP headers. */
  341. CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
  342. /* Read data from the remote end, return number of bytes read.
  343. Return:
  344. 0 connection has been closed by peer. No more data could be read.
  345. < 0 read error. No more data could be read from the connection.
  346. > 0 number of bytes read into the buffer. */
  347. CIVETWEB_API int mg_read(struct mg_connection *, void *buf, size_t len);
  348. /* Get the value of particular HTTP header.
  349. This is a helper function. It traverses request_info->http_headers array,
  350. and if the header is present in the array, returns its value. If it is
  351. not present, NULL is returned. */
  352. CIVETWEB_API const char *mg_get_header(const struct mg_connection *, const char *name);
  353. /* Get a value of particular form variable.
  354. Parameters:
  355. data: pointer to form-uri-encoded buffer. This could be either POST data,
  356. or request_info.query_string.
  357. data_len: length of the encoded data.
  358. var_name: variable name to decode from the buffer
  359. dst: destination buffer for the decoded variable
  360. dst_len: length of the destination buffer
  361. Return:
  362. On success, length of the decoded variable.
  363. On error:
  364. -1 (variable not found).
  365. -2 (destination buffer is NULL, zero length or too small to hold the
  366. decoded variable).
  367. Destination buffer is guaranteed to be '\0' - terminated if it is not
  368. NULL or zero length. */
  369. CIVETWEB_API int mg_get_var(const char *data, size_t data_len,
  370. const char *var_name, char *dst, size_t dst_len);
  371. /* Get a value of particular form variable.
  372. Parameters:
  373. data: pointer to form-uri-encoded buffer. This could be either POST data,
  374. or request_info.query_string.
  375. data_len: length of the encoded data.
  376. var_name: variable name to decode from the buffer
  377. dst: destination buffer for the decoded variable
  378. dst_len: length of the destination buffer
  379. occurrence: which occurrence of the variable, 0 is the first, 1 the
  380. second...
  381. this makes it possible to parse a query like
  382. b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1
  383. Return:
  384. On success, length of the decoded variable.
  385. On error:
  386. -1 (variable not found).
  387. -2 (destination buffer is NULL, zero length or too small to hold the
  388. decoded variable).
  389. Destination buffer is guaranteed to be '\0' - terminated if it is not
  390. NULL or zero length. */
  391. CIVETWEB_API int mg_get_var2(const char *data, size_t data_len,
  392. const char *var_name, char *dst, size_t dst_len, size_t occurrence);
  393. /* Fetch value of certain cookie variable into the destination buffer.
  394. Destination buffer is guaranteed to be '\0' - terminated. In case of
  395. failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  396. parameter. This function returns only first occurrence.
  397. Return:
  398. On success, value length.
  399. On error:
  400. -1 (either "Cookie:" header is not present at all or the requested
  401. parameter is not found).
  402. -2 (destination buffer is NULL, zero length or too small to hold the
  403. value). */
  404. CIVETWEB_API int mg_get_cookie(const char *cookie, const char *var_name,
  405. char *buf, size_t buf_len);
  406. /* Download data from the remote web server.
  407. host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
  408. port: port number, e.g. 80.
  409. use_ssl: wether to use SSL connection.
  410. error_buffer, error_buffer_size: error message placeholder.
  411. request_fmt,...: HTTP request.
  412. Return:
  413. On success, valid pointer to the new connection, suitable for mg_read().
  414. On error, NULL. error_buffer contains error message.
  415. Example:
  416. char ebuf[100];
  417. struct mg_connection *conn;
  418. conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
  419. "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
  420. */
  421. CIVETWEB_API struct mg_connection *mg_download(const char *host, int port, int use_ssl,
  422. char *error_buffer, size_t error_buffer_size,
  423. PRINTF_FORMAT_STRING(const char *request_fmt),
  424. ...) PRINTF_ARGS(6, 7);
  425. /* Close the connection opened by mg_download(). */
  426. CIVETWEB_API void mg_close_connection(struct mg_connection *conn);
  427. /* File upload functionality. Each uploaded file gets saved into a temporary
  428. file and MG_UPLOAD event is sent.
  429. Return number of uploaded files. */
  430. CIVETWEB_API int mg_upload(struct mg_connection *conn, const char *destination_dir);
  431. /* Convenience function -- create detached thread.
  432. Return: 0 on success, non-0 on error. */
  433. typedef void * (*mg_thread_func_t)(void *);
  434. CIVETWEB_API int mg_start_thread(mg_thread_func_t f, void *p);
  435. /* Return builtin mime type for the given file name.
  436. For unrecognized extensions, "text/plain" is returned. */
  437. CIVETWEB_API const char *mg_get_builtin_mime_type(const char *file_name);
  438. /* Return Civetweb version. */
  439. CIVETWEB_API const char *mg_version(void);
  440. /* URL-decode input buffer into destination buffer.
  441. 0-terminate the destination buffer.
  442. form-url-encoded data differs from URI encoding in a way that it
  443. uses '+' as character for space, see RFC 1866 section 8.2.1
  444. http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  445. Return: length of the decoded data, or -1 if dst buffer is too small. */
  446. CIVETWEB_API int mg_url_decode(const char *src, int src_len, char *dst,
  447. int dst_len, int is_form_url_encoded);
  448. /* URL-encode input buffer into destination buffer.
  449. returns the length of the resulting buffer or -1
  450. is the buffer is too small. */
  451. CIVETWEB_API int mg_url_encode(const char *src, char *dst, size_t dst_len);
  452. /* MD5 hash given strings.
  453. Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  454. ASCIIz strings. When function returns, buf will contain human-readable
  455. MD5 hash. Example:
  456. char buf[33];
  457. mg_md5(buf, "aa", "bb", NULL); */
  458. CIVETWEB_API char *mg_md5(char buf[33], ...);
  459. /* Print error message to the opened error log stream.
  460. This utilizes the provided logging configuration.
  461. conn: connection
  462. fmt: format string without the line return
  463. ...: variable argument list
  464. Example:
  465. mg_cry(conn,"i like %s", "logging"); */
  466. CIVETWEB_API void mg_cry(struct mg_connection *conn,
  467. PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
  468. /* utility method to compare two buffers, case incensitive. */
  469. CIVETWEB_API int mg_strncasecmp(const char *s1, const char *s2, size_t len);
  470. /* Connect to a websocket as a client
  471. Parameters:
  472. host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or "localhost"
  473. port: server port
  474. use_ssl: make a secure connection to server
  475. error_buffer, error_buffer_size: buffer for an error message
  476. path: server path you are trying to connect to, i.e. if connection to localhost/app, path should be "/app"
  477. origin: value of the Origin HTTP header
  478. data_func: callback that should be used when data is received from the server
  479. user_data: user supplied argument
  480. Return:
  481. On success, valid mg_connection object.
  482. On error, NULL. Se error_buffer for details.
  483. */
  484. typedef int (*websocket_data_func)(struct mg_connection *, int bits,
  485. char *data, size_t data_len);
  486. typedef void (*websocket_close_func)(struct mg_connection *);
  487. CIVETWEB_API struct mg_connection *mg_connect_websocket_client(const char *host, int port, int use_ssl,
  488. char *error_buffer, size_t error_buffer_size,
  489. const char *path, const char *origin,
  490. websocket_data_func data_func, websocket_close_func close_func,
  491. void * user_data);
  492. /* Connect to a TCP server as a client (can be used to connect to a HTTP server)
  493. Parameters:
  494. host: host to connect to, i.e. "www.wikipedia.org" or "192.168.1.1" or "localhost"
  495. port: server port
  496. use_ssl: make a secure connection to server
  497. error_buffer, error_buffer_size: buffer for an error message
  498. Return:
  499. On success, valid mg_connection object.
  500. On error, NULL. Se error_buffer for details.
  501. */
  502. CIVETWEB_API struct mg_connection *mg_connect_client(const char *host, int port, int use_ssl,
  503. char *error_buffer, size_t error_buffer_size);
  504. enum {
  505. TIMEOUT_INFINITE = -1
  506. };
  507. /* Wait for a response from the server
  508. Parameters:
  509. conn: connection
  510. ebuf, ebuf_len: error message placeholder.
  511. timeout: time to wait for a response in milliseconds (if < 0 then wait forever)
  512. Return:
  513. On success, >= 0
  514. On error/timeout, < 0
  515. */
  516. CIVETWEB_API int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int timeout);
  517. #ifdef __cplusplus
  518. }
  519. #endif /* __cplusplus */
  520. #endif /* CIVETWEB_HEADER_INCLUDED */