civetweb.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. <<<<<<< HEAD
  2. // Copyright (c) 2004-2012 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. /* Copyright (c) 2004-2013 Sergey Lyubka
  23. *
  24. * Permission is hereby granted, free of charge, to any person obtaining a copy
  25. * of this software and associated documentation files (the "Software"), to deal
  26. * in the Software without restriction, including without limitation the rights
  27. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28. * copies of the Software, and to permit persons to whom the Software is
  29. * furnished to do so, subject to the following conditions:
  30. *
  31. * The above copyright notice and this permission notice shall be included in
  32. * all copies or substantial portions of the Software.
  33. *
  34. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. * THE SOFTWARE.
  41. */
  42. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  43. #ifndef CIVETWEB_HEADER_INCLUDED
  44. #define CIVETWEB_HEADER_INCLUDED
  45. #include <stdio.h>
  46. #include <stddef.h>
  47. #ifdef __cplusplus
  48. extern "C" {
  49. <<<<<<< HEAD
  50. #endif // __cplusplus
  51. struct mg_context; // Handle for the HTTP service itself
  52. struct mg_connection; // Handle for the individual connection
  53. // This structure contains information about the HTTP request.
  54. struct mg_request_info {
  55. const char *request_method; // "GET", "POST", etc
  56. const char *uri; // URL-decoded URI
  57. const char *http_version; // E.g. "1.0", "1.1"
  58. const char *query_string; // URL part after '?', not including '?', or NULL
  59. const char *remote_user; // Authenticated user, or NULL if no auth used
  60. long remote_ip; // Client's IP address
  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 detailed description, see
  73. // https://github.com/sunsetbrew/civetweb/blob/master/docs/UserManual.md
  74. struct mg_callbacks {
  75. // Called when civetweb has received new HTTP request.
  76. // If callback returns non-zero,
  77. // callback must process the request by sending valid HTTP headers and body,
  78. // and civetweb will not do any further processing.
  79. // If callback returns 0, civetweb processes the request itself. In this case,
  80. // callback must not send any data to the client.
  81. int (*begin_request)(struct mg_connection *);
  82. // Called when civetweb has finished processing request.
  83. void (*end_request)(const struct mg_connection *, int reply_status_code);
  84. // Called when civetweb is about to log a message. If callback returns
  85. // non-zero, civetweb does not log anything.
  86. int (*log_message)(const struct mg_connection *, const char *message);
  87. // Called when civetweb initializes SSL library.
  88. int (*init_ssl)(void *ssl_context, void *user_data);
  89. // Called when websocket request is received, before websocket handshake.
  90. // If callback returns 0, civetweb proceeds with handshake, otherwise
  91. // cinnection is closed immediately.
  92. int (*websocket_connect)(const struct mg_connection *);
  93. // Called when websocket handshake is successfully completed, and
  94. // connection is ready for data exchange.
  95. void (*websocket_ready)(struct mg_connection *);
  96. // Called when data frame has been received from the client.
  97. // Parameters:
  98. // bits: first byte of the websocket frame, see websocket RFC at
  99. // http://tools.ietf.org/html/rfc6455, section 5.2
  100. // data, data_len: payload, with mask (if any) already applied.
  101. // Return value:
  102. // non-0: keep this websocket connection opened.
  103. // 0: close this websocket connection.
  104. int (*websocket_data)(struct mg_connection *, int bits,
  105. char *data, size_t data_len);
  106. // Called when civetweb is closing a connection. The per-context mutex is locked when this
  107. // is invoked. This is primarily useful for noting when a websocket is closing and removing it
  108. // from any application-maintained list of clients.
  109. void (*connection_close)(struct mg_connection *);
  110. // Called when civetweb tries to open a file. Used to intercept file open
  111. // calls, and serve file data from memory instead.
  112. // Parameters:
  113. // path: Full path to the file to open.
  114. // data_len: Placeholder for the file size, if file is served from memory.
  115. // Return value:
  116. // NULL: do not serve file from memory, proceed with normal file open.
  117. // non-NULL: pointer to the file contents in memory. data_len must be
  118. // initilized with the size of the memory block.
  119. const char * (*open_file)(const struct mg_connection *,
  120. const char *path, size_t *data_len);
  121. // Called when civetweb is about to serve Lua server page (.lp file), if
  122. // Lua support is enabled.
  123. // Parameters:
  124. // lua_context: "lua_State *" pointer.
  125. void (*init_lua)(struct mg_connection *, void *lua_context);
  126. // Called when civetweb has uploaded a file to a temporary directory as a
  127. // result of mg_upload() call.
  128. // Parameters:
  129. // file_file: full path name to the uploaded file.
  130. void (*upload)(struct mg_connection *, const char *file_name);
  131. // Called when civetweb is about to send HTTP error to the client.
  132. // Implementing this callback allows to create custom error pages.
  133. // Parameters:
  134. // status: HTTP error status code.
  135. int (*http_error)(struct mg_connection *, int status);
  136. };
  137. // Start web server.
  138. //
  139. // Parameters:
  140. // callbacks: mg_callbacks structure with user-defined callbacks.
  141. // options: NULL terminated list of option_name, option_value pairs that
  142. // specify Civetweb configuration parameters.
  143. //
  144. // Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
  145. // processing is required for these, signal handlers must be set up
  146. // after calling mg_start().
  147. //
  148. //
  149. // Example:
  150. // const char *options[] = {
  151. // "document_root", "/var/www",
  152. // "listening_ports", "80,443s",
  153. // NULL
  154. // };
  155. // struct mg_context *ctx = mg_start(&my_func, NULL, options);
  156. //
  157. // Refer to https://github.com/sunsetbrew/civetweb/blob/master/docs/UserManual.md
  158. // for the list of valid option and their possible values.
  159. //
  160. // Return:
  161. // web server context, or NULL on error.
  162. =======
  163. #endif /* __cplusplus */
  164. struct mg_context; /* Handle for the HTTP service itself */
  165. struct mg_connection; /* Handle for the individual connection */
  166. /* This structure contains information about the HTTP request. */
  167. struct mg_request_info {
  168. const char *request_method; /* "GET", "POST", etc */
  169. const char *uri; /* URL-decoded URI */
  170. const char *http_version; /* E.g. "1.0", "1.1" */
  171. const char *query_string; /* URL part after '?', not including '?', or
  172. NULL */
  173. const char *remote_user; /* Authenticated user, or NULL if no auth
  174. used */
  175. long remote_ip; /* Client's IP address */
  176. int remote_port; /* Client's port */
  177. int is_ssl; /* 1 if SSL-ed, 0 if not */
  178. void *user_data; /* User data pointer passed to mg_start() */
  179. void *conn_data; /* Connection-specific user data */
  180. int num_headers; /* Number of HTTP headers */
  181. struct mg_header {
  182. const char *name; /* HTTP header name */
  183. const char *value; /* HTTP header value */
  184. } http_headers[64]; /* Maximum 64 headers */
  185. };
  186. /* This structure needs to be passed to mg_start(), to let civetweb know
  187. which callbacks to invoke. For detailed description, see
  188. https://github.com/sunsetbrew/civetweb/blob/master/docs/UserManual.md */
  189. struct mg_callbacks {
  190. /* Called when civetweb has received new HTTP request.
  191. If callback returns non-zero,
  192. callback must process the request by sending valid HTTP headers and
  193. body, and civetweb will not do any further processing.
  194. If callback returns 0, civetweb processes the request itself. In this
  195. case, callback must not send any data to the client. */
  196. int (*begin_request)(struct mg_connection *);
  197. /* Called when civetweb has finished processing request. */
  198. void (*end_request)(const struct mg_connection *, int reply_status_code);
  199. /* Called when civetweb is about to log a message. If callback returns
  200. non-zero, civetweb does not log anything. */
  201. int (*log_message)(const struct mg_connection *, const char *message);
  202. /* Called when civetweb initializes SSL library. */
  203. int (*init_ssl)(void *ssl_context, void *user_data);
  204. /* Called when websocket request is received, before websocket handshake.
  205. If callback returns 0, civetweb proceeds with handshake, otherwise
  206. cinnection is closed immediately. */
  207. int (*websocket_connect)(const struct mg_connection *);
  208. /* Called when websocket handshake is successfully completed, and
  209. connection is ready for data exchange. */
  210. void (*websocket_ready)(struct mg_connection *);
  211. /* Called when data frame has been received from the client.
  212. Parameters:
  213. bits: first byte of the websocket frame, see websocket RFC at
  214. http://tools.ietf.org/html/rfc6455, section 5.2
  215. data, data_len: payload, with mask (if any) already applied.
  216. Return value:
  217. non-0: keep this websocket connection opened.
  218. 0: close this websocket connection. */
  219. int (*websocket_data)(struct mg_connection *, int bits,
  220. char *data, size_t data_len);
  221. /* Called when civetweb is closing a connection. The per-context mutex is
  222. locked when this is invoked. This is primarily useful for noting when
  223. a websocket is closing and removing it from any application-maintained
  224. list of clients. */
  225. void (*connection_close)(struct mg_connection *);
  226. /* Called when civetweb tries to open a file. Used to intercept file open
  227. calls, and serve file data from memory instead.
  228. Parameters:
  229. path: Full path to the file to open.
  230. data_len: Placeholder for the file size, if file is served from
  231. memory.
  232. Return value:
  233. NULL: do not serve file from memory, proceed with normal file open.
  234. non-NULL: pointer to the file contents in memory. data_len must be
  235. initilized with the size of the memory block. */
  236. const char * (*open_file)(const struct mg_connection *,
  237. const char *path, size_t *data_len);
  238. /* Called when civetweb is about to serve Lua server page (.lp file), if
  239. Lua support is enabled.
  240. Parameters:
  241. lua_context: "lua_State *" pointer. */
  242. void (*init_lua)(struct mg_connection *, void *lua_context);
  243. /* Called when civetweb has uploaded a file to a temporary directory as a
  244. result of mg_upload() call.
  245. Parameters:
  246. file_file: full path name to the uploaded file. */
  247. void (*upload)(struct mg_connection *, const char *file_name);
  248. /* Called when civetweb is about to send HTTP error to the client.
  249. Implementing this callback allows to create custom error pages.
  250. Parameters:
  251. status: HTTP error status code. */
  252. int (*http_error)(struct mg_connection *, int status);
  253. };
  254. /* Start web server.
  255. Parameters:
  256. callbacks: mg_callbacks structure with user-defined callbacks.
  257. options: NULL terminated list of option_name, option_value pairs that
  258. specify Civetweb configuration parameters.
  259. Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
  260. processing is required for these, signal handlers must be set up
  261. after calling mg_start().
  262. Example:
  263. const char *options[] = {
  264. "document_root", "/var/www",
  265. "listening_ports", "80,443s",
  266. NULL
  267. };
  268. struct mg_context *ctx = mg_start(&my_func, NULL, options);
  269. Refer to https://github.com/sunsetbrew/civetweb/blob/master/docs/UserManual.md
  270. for the list of valid option and their possible values.
  271. Return:
  272. web server context, or NULL on error. */
  273. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  274. struct mg_context *mg_start(const struct mg_callbacks *callbacks,
  275. void *user_data,
  276. const char **configuration_options);
  277. <<<<<<< HEAD
  278. // Stop the web server.
  279. //
  280. // Must be called last, when an application wants to stop the web server and
  281. // release all associated resources. This function blocks until all Civetweb
  282. // threads are stopped. Context pointer becomes invalid.
  283. void mg_stop(struct mg_context *);
  284. // mg_request_handler
  285. //
  286. // Called when a new request comes in. This callback is URI based
  287. // and configured with mg_set_request_handler().
  288. //
  289. // Parameters:
  290. // conn: current connection information.
  291. // cbdata: the callback data configured with mg_set_request_handler().
  292. // Returns:
  293. // 0: the handler could not handle the request, so fall through.
  294. // 1: the handler processed the request.
  295. typedef int (* mg_request_handler)(struct mg_connection *conn, void *cbdata);
  296. // mg_set_request_handler
  297. //
  298. // Sets or removes a URI mapping for a request handler.
  299. //
  300. // URI's are ordered and prefixed URI's are supported. For example,
  301. // consider two URIs: /a/b and /a
  302. // /a matches /a
  303. // /a/b matches /a/b
  304. // /a/c matches /a
  305. //
  306. // Parameters:
  307. // ctx: server context
  308. // uri: the URI to configure
  309. // handler: the callback handler to use when the URI is requested.
  310. // If NULL, the URI will be removed.
  311. // cbdata: the callback data to give to the handler when it s requested.
  312. void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata);
  313. // Get the value of particular configuration parameter.
  314. // The value returned is read-only. Civetweb does not allow changing
  315. // configuration at run time.
  316. // If given parameter name is not valid, NULL is returned. For valid
  317. // names, return value is guaranteed to be non-NULL. If parameter is not
  318. // set, zero-length string is returned.
  319. const char *mg_get_option(const struct mg_context *ctx, const char *name);
  320. // Return array of strings that represent valid configuration options.
  321. // For each option, option name and default value is returned, i.e. the
  322. // number of entries in the array equals to number_of_options x 2.
  323. // Array is NULL terminated.
  324. const char **mg_get_valid_option_names(void);
  325. // Add, edit or delete the entry in the passwords file.
  326. //
  327. // This function allows an application to manipulate .htpasswd files on the
  328. // fly by adding, deleting and changing user records. This is one of the
  329. // several ways of implementing authentication on the server side. For another,
  330. // cookie-based way please refer to the examples/chat in the source tree.
  331. //
  332. // If password is not NULL, entry is added (or modified if already exists).
  333. // If password is NULL, entry is deleted.
  334. //
  335. // Return:
  336. // 1 on success, 0 on error.
  337. =======
  338. /* Stop the web server.
  339. Must be called last, when an application wants to stop the web server and
  340. release all associated resources. This function blocks until all Civetweb
  341. threads are stopped. Context pointer becomes invalid. */
  342. void mg_stop(struct mg_context *);
  343. /* mg_request_handler
  344. Called when a new request comes in. This callback is URI based
  345. and configured with mg_set_request_handler().
  346. Parameters:
  347. conn: current connection information.
  348. cbdata: the callback data configured with mg_set_request_handler().
  349. Returns:
  350. 0: the handler could not handle the request, so fall through.
  351. 1: the handler processed the request. */
  352. typedef int (* mg_request_handler)(struct mg_connection *conn, void *cbdata);
  353. /* mg_set_request_handler
  354. Sets or removes a URI mapping for a request handler.
  355. URI's are ordered and prefixed URI's are supported. For example,
  356. consider two URIs: /a/b and /a
  357. /a matches /a
  358. /a/b matches /a/b
  359. /a/c matches /a
  360. Parameters:
  361. ctx: server context
  362. uri: the URI to configure
  363. handler: the callback handler to use when the URI is requested.
  364. If NULL, the URI will be removed.
  365. cbdata: the callback data to give to the handler when it s requested. */
  366. void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata);
  367. /* Get the value of particular configuration parameter.
  368. The value returned is read-only. Civetweb does not allow changing
  369. configuration at run time.
  370. If given parameter name is not valid, NULL is returned. For valid
  371. names, return value is guaranteed to be non-NULL. If parameter is not
  372. set, zero-length string is returned. */
  373. const char *mg_get_option(const struct mg_context *ctx, const char *name);
  374. /* Return array of strings that represent valid configuration options.
  375. For each option, option name and default value is returned, i.e. the
  376. number of entries in the array equals to number_of_options x 2.
  377. Array is NULL terminated. */
  378. const char **mg_get_valid_option_names(void);
  379. /* Add, edit or delete the entry in the passwords file.
  380. This function allows an application to manipulate .htpasswd files on the
  381. fly by adding, deleting and changing user records. This is one of the
  382. several ways of implementing authentication on the server side. For another,
  383. cookie-based way please refer to the examples/chat in the source tree.
  384. If password is not NULL, entry is added (or modified if already exists).
  385. If password is NULL, entry is deleted.
  386. Return:
  387. 1 on success, 0 on error. */
  388. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  389. int mg_modify_passwords_file(const char *passwords_file_name,
  390. const char *domain,
  391. const char *user,
  392. const char *password);
  393. <<<<<<< HEAD
  394. // Return information associated with the request.
  395. struct mg_request_info *mg_get_request_info(struct mg_connection *);
  396. // Send data to the client.
  397. // Return:
  398. // 0 when the connection has been closed
  399. // -1 on error
  400. // >0 number of bytes written on success
  401. int mg_write(struct mg_connection *, const void *buf, size_t len);
  402. // Send data to a websocket client wrapped in a websocket frame. Uses mg_lock to ensure
  403. // that the transmission is not interrupted, i.e., when the application is proactively
  404. // communicating and responding to a request simultaneously.
  405. //
  406. // Send data to a websocket client wrapped in a websocket frame.
  407. // This function is available when civetweb is compiled with -DUSE_WEBSOCKET
  408. //
  409. // Return:
  410. // 0 when the connection has been closed
  411. // -1 on error
  412. // >0 number of bytes written on success
  413. int mg_websocket_write(struct mg_connection* conn, int opcode,
  414. const char *data, size_t data_len);
  415. // Blocks until unique access is obtained to this connection. Intended for use with websockets only.
  416. // Invoke this before mg_write or mg_printf when communicating with a websocket if your code has
  417. // server-initiated communication as well as communication in direct response to a message.
  418. void mg_lock(struct mg_connection* conn);
  419. void mg_unlock(struct mg_connection* conn);
  420. // Opcodes, from http://tools.ietf.org/html/rfc6455
  421. =======
  422. /* Return information associated with the request. */
  423. struct mg_request_info *mg_get_request_info(struct mg_connection *);
  424. /* Send data to the client.
  425. Return:
  426. 0 when the connection has been closed
  427. -1 on error
  428. >0 number of bytes written on success */
  429. int mg_write(struct mg_connection *, const void *buf, size_t len);
  430. /* Send data to a websocket client wrapped in a websocket frame. Uses mg_lock
  431. to ensure that the transmission is not interrupted, i.e., when the
  432. application is proactively communicating and responding to a request
  433. simultaneously.
  434. Send data to a websocket client wrapped in a websocket frame.
  435. This function is available when civetweb is compiled with -DUSE_WEBSOCKET
  436. Return:
  437. 0 when the connection has been closed
  438. -1 on error
  439. >0 number of bytes written on success */
  440. int mg_websocket_write(struct mg_connection* conn, int opcode,
  441. const char *data, size_t data_len);
  442. /* Blocks until unique access is obtained to this connection. Intended for use
  443. with websockets only.
  444. Invoke this before mg_write or mg_printf when communicating with a
  445. websocket if your code has server-initiated communication as well as
  446. communication in direct response to a message. */
  447. void mg_lock(struct mg_connection* conn);
  448. void mg_unlock(struct mg_connection* conn);
  449. /* Opcodes, from http://tools.ietf.org/html/rfc6455 */
  450. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  451. enum {
  452. WEBSOCKET_OPCODE_CONTINUATION = 0x0,
  453. WEBSOCKET_OPCODE_TEXT = 0x1,
  454. WEBSOCKET_OPCODE_BINARY = 0x2,
  455. WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
  456. WEBSOCKET_OPCODE_PING = 0x9,
  457. WEBSOCKET_OPCODE_PONG = 0xa
  458. };
  459. <<<<<<< HEAD
  460. // Macros for enabling compiler-specific checks for printf-like arguments.
  461. =======
  462. /* Macros for enabling compiler-specific checks for printf-like arguments. */
  463. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  464. #undef PRINTF_FORMAT_STRING
  465. #if defined(_MSC_VER) && _MSC_VER >= 1400
  466. #include <sal.h>
  467. #if defined(_MSC_VER) && _MSC_VER > 1400
  468. #define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
  469. #else
  470. #define PRINTF_FORMAT_STRING(s) __format_string s
  471. #endif
  472. #else
  473. #define PRINTF_FORMAT_STRING(s) s
  474. #endif
  475. #ifdef __GNUC__
  476. #define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
  477. #else
  478. #define PRINTF_ARGS(x, y)
  479. #endif
  480. <<<<<<< HEAD
  481. // Send data to the client using printf() semantics.
  482. //
  483. // Works exactly like mg_write(), but allows to do message formatting.
  484. =======
  485. /* Send data to the client using printf() semantics.
  486. Works exactly like mg_write(), but allows to do message formatting. */
  487. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  488. int mg_printf(struct mg_connection *,
  489. PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
  490. <<<<<<< HEAD
  491. // Send contents of the entire file together with HTTP headers.
  492. void mg_send_file(struct mg_connection *conn, const char *path);
  493. // Read data from the remote end, return number of bytes read.
  494. // Return:
  495. // 0 connection has been closed by peer. No more data could be read.
  496. // < 0 read error. No more data could be read from the connection.
  497. // > 0 number of bytes read into the buffer.
  498. int mg_read(struct mg_connection *, void *buf, size_t len);
  499. // Get the value of particular HTTP header.
  500. //
  501. // This is a helper function. It traverses request_info->http_headers array,
  502. // and if the header is present in the array, returns its value. If it is
  503. // not present, NULL is returned.
  504. const char *mg_get_header(const struct mg_connection *, const char *name);
  505. // Get a value of particular form variable.
  506. //
  507. // Parameters:
  508. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  509. // or request_info.query_string.
  510. // data_len: length of the encoded data.
  511. // var_name: variable name to decode from the buffer
  512. // dst: destination buffer for the decoded variable
  513. // dst_len: length of the destination buffer
  514. //
  515. // Return:
  516. // On success, length of the decoded variable.
  517. // On error:
  518. // -1 (variable not found).
  519. // -2 (destination buffer is NULL, zero length or too small to hold the
  520. // decoded variable).
  521. //
  522. // Destination buffer is guaranteed to be '\0' - terminated if it is not
  523. // NULL or zero length.
  524. int mg_get_var(const char *data, size_t data_len,
  525. const char *var_name, char *dst, size_t dst_len);
  526. // Get a value of particular form variable.
  527. //
  528. // Parameters:
  529. // data: pointer to form-uri-encoded buffer. This could be either POST data,
  530. // or request_info.query_string.
  531. // data_len: length of the encoded data.
  532. // var_name: variable name to decode from the buffer
  533. // dst: destination buffer for the decoded variable
  534. // dst_len: length of the destination buffer
  535. // occurrence: which occurrence of the variable, 0 is the first, 1 the second...
  536. // this makes it possible to parse a query like
  537. // b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1
  538. //
  539. // Return:
  540. // On success, length of the decoded variable.
  541. // On error:
  542. // -1 (variable not found).
  543. // -2 (destination buffer is NULL, zero length or too small to hold the
  544. // decoded variable).
  545. //
  546. // Destination buffer is guaranteed to be '\0' - terminated if it is not
  547. // NULL or zero length.
  548. int mg_get_var2(const char *data, size_t data_len,
  549. const char *var_name, char *dst, size_t dst_len, size_t occurrence);
  550. // Fetch value of certain cookie variable into the destination buffer.
  551. //
  552. // Destination buffer is guaranteed to be '\0' - terminated. In case of
  553. // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  554. // parameter. This function returns only first occurrence.
  555. //
  556. // Return:
  557. // On success, value length.
  558. // On error:
  559. // -1 (either "Cookie:" header is not present at all or the requested
  560. // parameter is not found).
  561. // -2 (destination buffer is NULL, zero length or too small to hold the
  562. // value).
  563. =======
  564. /* Send contents of the entire file together with HTTP headers. */
  565. void mg_send_file(struct mg_connection *conn, const char *path);
  566. /* Read data from the remote end, return number of bytes read.
  567. Return:
  568. 0 connection has been closed by peer. No more data could be read.
  569. < 0 read error. No more data could be read from the connection.
  570. > 0 number of bytes read into the buffer. */
  571. int mg_read(struct mg_connection *, void *buf, size_t len);
  572. /* Get the value of particular HTTP header.
  573. This is a helper function. It traverses request_info->http_headers array,
  574. and if the header is present in the array, returns its value. If it is
  575. not present, NULL is returned. */
  576. const char *mg_get_header(const struct mg_connection *, const char *name);
  577. /* Get a value of particular form variable.
  578. Parameters:
  579. data: pointer to form-uri-encoded buffer. This could be either POST data,
  580. or request_info.query_string.
  581. data_len: length of the encoded data.
  582. var_name: variable name to decode from the buffer
  583. dst: destination buffer for the decoded variable
  584. dst_len: length of the destination buffer
  585. Return:
  586. On success, length of the decoded variable.
  587. On error:
  588. -1 (variable not found).
  589. -2 (destination buffer is NULL, zero length or too small to hold the
  590. decoded variable).
  591. Destination buffer is guaranteed to be '\0' - terminated if it is not
  592. NULL or zero length. */
  593. int mg_get_var(const char *data, size_t data_len,
  594. const char *var_name, char *dst, size_t dst_len);
  595. /* Get a value of particular form variable.
  596. Parameters:
  597. data: pointer to form-uri-encoded buffer. This could be either POST data,
  598. or request_info.query_string.
  599. data_len: length of the encoded data.
  600. var_name: variable name to decode from the buffer
  601. dst: destination buffer for the decoded variable
  602. dst_len: length of the destination buffer
  603. occurrence: which occurrence of the variable, 0 is the first, 1 the
  604. second...
  605. this makes it possible to parse a query like
  606. b=x&a=y&a=z which will have occurrence values b:0, a:0 and a:1
  607. Return:
  608. On success, length of the decoded variable.
  609. On error:
  610. -1 (variable not found).
  611. -2 (destination buffer is NULL, zero length or too small to hold the
  612. decoded variable).
  613. Destination buffer is guaranteed to be '\0' - terminated if it is not
  614. NULL or zero length. */
  615. int mg_get_var2(const char *data, size_t data_len,
  616. const char *var_name, char *dst, size_t dst_len, size_t occurrence);
  617. /* Fetch value of certain cookie variable into the destination buffer.
  618. Destination buffer is guaranteed to be '\0' - terminated. In case of
  619. failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
  620. parameter. This function returns only first occurrence.
  621. Return:
  622. On success, value length.
  623. On error:
  624. -1 (either "Cookie:" header is not present at all or the requested
  625. parameter is not found).
  626. -2 (destination buffer is NULL, zero length or too small to hold the
  627. value). */
  628. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  629. int mg_get_cookie(const char *cookie, const char *var_name,
  630. char *buf, size_t buf_len);
  631. <<<<<<< HEAD
  632. // Download data from the remote web server.
  633. // host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
  634. // port: port number, e.g. 80.
  635. // use_ssl: wether to use SSL connection.
  636. // error_buffer, error_buffer_size: error message placeholder.
  637. // request_fmt,...: HTTP request.
  638. // Return:
  639. // On success, valid pointer to the new connection, suitable for mg_read().
  640. // On error, NULL. error_buffer contains error message.
  641. // Example:
  642. // char ebuf[100];
  643. // struct mg_connection *conn;
  644. // conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
  645. // "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
  646. =======
  647. /* Download data from the remote web server.
  648. host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
  649. port: port number, e.g. 80.
  650. use_ssl: wether to use SSL connection.
  651. error_buffer, error_buffer_size: error message placeholder.
  652. request_fmt,...: HTTP request.
  653. Return:
  654. On success, valid pointer to the new connection, suitable for mg_read().
  655. On error, NULL. error_buffer contains error message.
  656. Example:
  657. char ebuf[100];
  658. struct mg_connection *conn;
  659. conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
  660. "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
  661. */
  662. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  663. struct mg_connection *mg_download(const char *host, int port, int use_ssl,
  664. char *error_buffer, size_t error_buffer_size,
  665. PRINTF_FORMAT_STRING(const char *request_fmt),
  666. ...) PRINTF_ARGS(6, 7);
  667. <<<<<<< HEAD
  668. // Close the connection opened by mg_download().
  669. void mg_close_connection(struct mg_connection *conn);
  670. // File upload functionality. Each uploaded file gets saved into a temporary
  671. // file and MG_UPLOAD event is sent.
  672. // Return number of uploaded files.
  673. int mg_upload(struct mg_connection *conn, const char *destination_dir);
  674. // Convenience function -- create detached thread.
  675. // Return: 0 on success, non-0 on error.
  676. =======
  677. /* Close the connection opened by mg_download(). */
  678. void mg_close_connection(struct mg_connection *conn);
  679. /* File upload functionality. Each uploaded file gets saved into a temporary
  680. file and MG_UPLOAD event is sent.
  681. Return number of uploaded files. */
  682. int mg_upload(struct mg_connection *conn, const char *destination_dir);
  683. /* Convenience function -- create detached thread.
  684. Return: 0 on success, non-0 on error. */
  685. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  686. typedef void * (*mg_thread_func_t)(void *);
  687. int mg_start_thread(mg_thread_func_t f, void *p);
  688. <<<<<<< HEAD
  689. // Return builtin mime type for the given file name.
  690. // For unrecognized extensions, "text/plain" is returned.
  691. const char *mg_get_builtin_mime_type(const char *file_name);
  692. // Return Civetweb version.
  693. const char *mg_version(void);
  694. // URL-decode input buffer into destination buffer.
  695. // 0-terminate the destination buffer.
  696. // form-url-encoded data differs from URI encoding in a way that it
  697. // uses '+' as character for space, see RFC 1866 section 8.2.1
  698. // http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  699. // Return: length of the decoded data, or -1 if dst buffer is too small.
  700. int mg_url_decode(const char *src, int src_len, char *dst,
  701. int dst_len, int is_form_url_encoded);
  702. // URL-encode input buffer into destination buffer.
  703. // returns the length of the resulting buffer or -1
  704. // is the buffer is too small.
  705. int mg_url_encode(const char *src, char *dst, size_t dst_len);
  706. // MD5 hash given strings.
  707. // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  708. // ASCIIz strings. When function returns, buf will contain human-readable
  709. // MD5 hash. Example:
  710. // char buf[33];
  711. // mg_md5(buf, "aa", "bb", NULL);
  712. char *mg_md5(char buf[33], ...);
  713. // Print error message to the opened error log stream.
  714. // This utilizes the provided logging configuration.
  715. // conn: connection
  716. // fmt: format string without the line return
  717. // ...: variable argument list
  718. // Example:
  719. // mg_cry(conn,"i like %s", "logging");
  720. void mg_cry(struct mg_connection *conn,
  721. PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
  722. // utility method to compare two buffers, case incensitive.
  723. =======
  724. /* Return builtin mime type for the given file name.
  725. For unrecognized extensions, "text/plain" is returned. */
  726. const char *mg_get_builtin_mime_type(const char *file_name);
  727. /* Return Civetweb version. */
  728. const char *mg_version(void);
  729. /* URL-decode input buffer into destination buffer.
  730. 0-terminate the destination buffer.
  731. form-url-encoded data differs from URI encoding in a way that it
  732. uses '+' as character for space, see RFC 1866 section 8.2.1
  733. http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
  734. Return: length of the decoded data, or -1 if dst buffer is too small. */
  735. int mg_url_decode(const char *src, int src_len, char *dst,
  736. int dst_len, int is_form_url_encoded);
  737. /* URL-encode input buffer into destination buffer.
  738. returns the length of the resulting buffer or -1
  739. is the buffer is too small. */
  740. int mg_url_encode(const char *src, char *dst, size_t dst_len);
  741. /* MD5 hash given strings.
  742. Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
  743. ASCIIz strings. When function returns, buf will contain human-readable
  744. MD5 hash. Example:
  745. char buf[33];
  746. mg_md5(buf, "aa", "bb", NULL); */
  747. char *mg_md5(char buf[33], ...);
  748. /* Print error message to the opened error log stream.
  749. This utilizes the provided logging configuration.
  750. conn: connection
  751. fmt: format string without the line return
  752. ...: variable argument list
  753. Example:
  754. mg_cry(conn,"i like %s", "logging"); */
  755. void mg_cry(struct mg_connection *conn,
  756. PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
  757. /* utility method to compare two buffers, case incensitive. */
  758. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3
  759. int mg_strncasecmp(const char *s1, const char *s2, size_t len);
  760. #ifdef __cplusplus
  761. }
  762. <<<<<<< HEAD
  763. #endif // __cplusplus
  764. #endif // CIVETWEB_HEADER_INCLUDED
  765. =======
  766. #endif /* __cplusplus */
  767. #endif /* CIVETWEB_HEADER_INCLUDED */
  768. >>>>>>> b393b8d2410737a01a0d18987b5dfc185b3c0ef3