You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

565 lines
14 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-common.h,v 1.31 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #ifndef NANO_HTTP_COMMON_H
  25. #define NANO_HTTP_COMMON_H
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #define HEADER_CONTENT_ID "Content-Id"
  29. #define HEADER_CONTENT_TRANSFER_ENCODING "Content-Transfer-Encoding"
  30. #define TRANSFER_ENCODING_CHUNKED "chunked"
  31. /**
  32. *
  33. * General Header Fields
  34. *
  35. * There are a few header fields which have general applicability for both
  36. * request and response messages, but which do not apply to the entity being
  37. * transferred. These header fields apply only to the message being transmitted.
  38. * (see RFC2616)
  39. *
  40. */
  41. #define HEADER_CACHE_CONTROL "Cache-Control"
  42. #define HEADER_CONNECTION "Connection"
  43. #define HEADER_DATE "Date"
  44. #define HEADER_PRAGMA "Pragma"
  45. #define HEADER_TRAILER "Trailer"
  46. #define HEADER_TRANSFER_ENCODING "Transfer-Encoding"
  47. #define HEADER_UPGRADE "Upgrade"
  48. #define HEADER_VIA "Via"
  49. #define HEADER_WARNING "Warning"
  50. /**
  51. *
  52. * Entity Header Fields
  53. *
  54. * Entity-header fields define metainformation about the entity-body or, if no
  55. * body is present, about the resource identified by the request. Some of this
  56. * metainformation is OPTIONAL; some might be REQUIRED by portions of this
  57. * specification. (see RFC2616 7.1)
  58. *
  59. */
  60. #define HEADER_ALLOW "Allow"
  61. #define HEADER_CONTENT_ENCODING "Content-Encoding"
  62. #define HEADER_CONTENT_LANGUAGE "Content-Language"
  63. #define HEADER_CONTENT_LENGTH "Content-Length"
  64. #define HEADER_CONTENT_LOCATION "Content-Location"
  65. #define HEADER_CONTENT_MD5 "Content-MD5"
  66. #define HEADER_CONTENT_RANGE "Content-Range"
  67. #define HEADER_CONTENT_TYPE "Content-Type"
  68. #define HEADER_EXPIRES "Expires"
  69. #define HEADER_LAST_MODIFIED "Last-Modified"
  70. /**
  71. *
  72. * XXX: move to nanohttp-response.h
  73. *
  74. * Response Header Fields
  75. *
  76. * The response-header fields allow the server to pass additional information
  77. * about the response which cannot be placed in the Status-Line. These header
  78. * fields give information about the server and about further access to the
  79. * resource identified by the Request-URI. (see RFC2616)
  80. *
  81. */
  82. #define HEADER_ACCEPT_RANGES "Accept-Ranges"
  83. #define HEADER_AGE "Age"
  84. #define HEADER_EXTENSION_TAG "ETag"
  85. #define HEADER_LOCATION "Location"
  86. #define HEADER_PROXY_AUTHENTICATE "Proxy-Authenticate"
  87. #define HEADER_RETRY_AFTER "Retry-After"
  88. #define HEADER_SERVER "Server"
  89. #define HEADER_VARY "Vary"
  90. #define HEADER_WWW_AUTHENTICATE "WWW-Authenticate"
  91. /**
  92. *
  93. * XXX: move to nanohttp-request.h
  94. *
  95. * Request Header Fields
  96. *
  97. * The request-header fields allow the client to pass additional information
  98. * about the request, and about the client itself, to the server. These fields
  99. * act as request modifiers, with semantics equivalent to the parameters on a
  100. * programming language method invocation (see RFC2616).
  101. *
  102. */
  103. #define HEADER_ACCEPT "Accept"
  104. #define HEADER_CHARSET "Accept-Charset"
  105. #define HEADER_ACCEPT_ENCODING "Accept-Encoding"
  106. #define HEADER_ACCEPT_LANGUAGE "Accept-Language"
  107. #define HEADER_AUTHORIZATION "Authorization"
  108. #define HEADER_EXPECT "Expect"
  109. #define HEADER_FROM "From"
  110. #define HEADER_HOST "Host"
  111. #define HEADER_IF_MATCH "If-Match"
  112. #define HEADER_IF_MODIFIED_SINCE "If-Modified-Since"
  113. #define HEADER_IF_NONE_MATCH "If-None-Match"
  114. #define HEADER_IF_RANGE "If-Range"
  115. #define HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since"
  116. #define HEADER_IF_MAX_FORWARDS "Max-Forwards"
  117. #define HEADER_PROXY_AUTHORIZATION "Proxy-Authorization"
  118. #define HEADER_RANGE "Range"
  119. #define HEADER_REFERER "Referer"
  120. #define HEADER_TRANSFER_EXTENSION "TE"
  121. #define HEADER_USER_AGENT "User-Agent"
  122. /**
  123. *
  124. * nanohttp command line flags
  125. *
  126. */
  127. #define NHTTPD_ARG_PORT "-NHTTPport"
  128. #define NHTTPD_ARG_TERMSIG "-NHTTPtsig"
  129. #define NHTTPD_ARG_MAXCONN "-NHTTPmaxconn"
  130. #define NHTTPD_ARG_TIMEOUT "-NHTTPtimeout"
  131. #define NHTTP_ARG_CERT "-NHTTPcert"
  132. #define NHTTP_ARG_CERTPASS "-NHTTPcertpass"
  133. #define NHTTP_ARG_CA "-NHTTPCA"
  134. #define NHTTP_ARG_HTTPS "-NHTTPS"
  135. #ifndef SAVE_STR
  136. #define SAVE_STR(str) ((str==0)?("(null)"):(str))
  137. #endif
  138. #define BOUNDARY_LENGTH 18
  139. #define MAX_HEADER_SIZE 4256
  140. #define MAX_SOCKET_BUFFER_SIZE 4256
  141. #define MAX_FILE_BUFFER_SIZE 4256
  142. #define URL_MAX_HOST_SIZE 120
  143. #define URL_MAX_CONTEXT_SIZE 1024
  144. #define HSOCKET_MAX_BUFSIZE 1024
  145. #define REQUEST_MAX_PATH_SIZE 1024
  146. #define RESPONSE_MAX_DESC_SIZE 1024
  147. /*
  148. DIME common stuff
  149. */
  150. #define DIME_VERSION_1 0x08
  151. #define DIME_FIRST_PACKAGE 0x04
  152. #define DIME_LAST_PACKAGE 0x02
  153. #define DIME_CHUNKED 0x01
  154. #define DIME_TYPE_URI 0x2
  155. /* TODO (#1#): find proper ports */
  156. #define URL_DEFAULT_PORT_HTTP 80
  157. #define URL_DEFAULT_PORT_HTTPS 81
  158. #define URL_DEFAULT_PORT_FTP 120
  159. /* Success flag */
  160. #define H_OK 0
  161. /* File errors */
  162. #define FILE_ERROR_OPEN 8000
  163. #define FILE_ERROR_READ 8001
  164. /* Socket errors */
  165. #define HSOCKET_ERROR_CREATE 1001
  166. #define HSOCKET_ERROR_GET_HOSTNAME 1002
  167. #define HSOCKET_ERROR_CONNECT 1003
  168. #define HSOCKET_ERROR_SEND 1004
  169. #define HSOCKET_ERROR_RECEIVE 1005
  170. #define HSOCKET_ERROR_BIND 1006
  171. #define HSOCKET_ERROR_LISTEN 1007
  172. #define HSOCKET_ERROR_ACCEPT 1008
  173. #define HSOCKET_ERROR_NOT_INITIALIZED 1009
  174. #define HSOCKET_ERROR_IOCTL 1010
  175. #define HSOCKET_ERROR_SSLCLOSE 1011
  176. #define HSOCKET_ERROR_SSLCTX 1011
  177. /* URL errors */
  178. #define URL_ERROR_UNKNOWN_PROTOCOL 1101
  179. #define URL_ERROR_NO_PROTOCOL 1102
  180. #define URL_ERROR_NO_HOST 1103
  181. /* Stream errors */
  182. #define STREAM_ERROR_INVALID_TYPE 1201
  183. #define STREAM_ERROR_SOCKET_ERROR 1202
  184. #define STREAM_ERROR_NO_CHUNK_SIZE 1203
  185. #define STREAM_ERROR_WRONG_CHUNK_SIZE 1204
  186. /* MIME errors */
  187. #define MIME_ERROR_NO_BOUNDARY_PARAM 1301
  188. #define MIME_ERROR_NO_START_PARAM 1302
  189. #define MIME_ERROR_PARSE_ERROR 1303
  190. #define MIME_ERROR_NO_ROOT_PART 1304
  191. #define MIME_ERROR_NOT_MIME_MESSAGE 1305
  192. /* General errors */
  193. #define GENERAL_INVALID_PARAM 1400
  194. #define GENERAL_HEADER_PARSE_ERROR 1401
  195. /* Thread errors */
  196. #define THREAD_BEGIN_ERROR 1500
  197. /* XML Errors */
  198. #define XML_ERROR_EMPTY_DOCUMENT 1600
  199. #define XML_ERROR_PARSE 1601
  200. /* SSL Errors */
  201. #define HSSL_ERROR_CA_LIST 1710
  202. #define HSSL_ERROR_CONTEXT 1720
  203. #define HSSL_ERROR_CERTIFICATE 1730
  204. #define HSSL_ERROR_PEM 1740
  205. #define HSSL_ERROR_CLIENT 1750
  206. #define HSSL_ERROR_SERVER 1760
  207. #define HSSL_ERROR_CONNECT 1770
  208. /*
  209. Set Sleep function platform depended
  210. */
  211. #ifdef WIN32
  212. #define system_sleep(seconds) Sleep(seconds*1000);
  213. #else
  214. #define system_sleep(seconds) sleep(seconds);
  215. #endif
  216. /**
  217. Indicates the version of the
  218. used HTTP protocol.
  219. */
  220. typedef enum _http_version
  221. {
  222. HTTP_1_0,
  223. HTTP_1_1 /* default */
  224. } http_version_t;
  225. /**
  226. Indicates the used method
  227. */
  228. typedef enum _hreq_method
  229. {
  230. HTTP_REQUEST_POST,
  231. HTTP_REQUEST_GET,
  232. HTTP_REQUEST_OPTIONS,
  233. HTTP_REQUEST_HEAD,
  234. HTTP_REQUEST_PUT,
  235. HTTP_REQUEST_DELETE,
  236. HTTP_REQUEST_TRACE,
  237. HTTP_REQUEST_CONNECT,
  238. HTTP_REQUEST_UNKOWN
  239. } hreq_method_t;
  240. #ifdef __cplusplus
  241. extern "C" {
  242. #endif
  243. #ifdef WIN32
  244. #include <string.h>
  245. char *strtok_r(char *s, const char *delim, char **save_ptr);
  246. struct tm *localtime_r(const time_t * const timep, struct tm *p_tm);
  247. #endif
  248. typedef unsigned char byte_t;
  249. typedef void *herror_t;
  250. herror_t herror_new(const char *func, int errcode, const char *format, ...);
  251. int herror_code(herror_t err);
  252. char *herror_func(herror_t err);
  253. char *herror_message(herror_t err);
  254. void herror_release(herror_t err);
  255. /*
  256. hpairnode_t represents a pair (key, value) pair.
  257. This is also a linked list.
  258. */
  259. typedef struct hpair hpair_t;
  260. struct hpair
  261. {
  262. char *key;
  263. char *value;
  264. hpair_t *next;
  265. };
  266. /**
  267. Creates a new pair with the given parameters. Both strings
  268. key and value will be cloned while creating the pair.
  269. @param key the key of the (key,value) pair
  270. @param value the value of the (key,value) pair
  271. @param next next pair node in the linked list
  272. @returns A newly crated hpair_t object. Use hpair_free()
  273. or hpair_free_deep() to free the pair.
  274. */
  275. hpair_t *hpairnode_new(const char *key, const char *value, hpair_t * next);
  276. /**
  277. Creates a new pair from a given string. This function
  278. will split 'str' with the found first delimiter 'delim'.
  279. The 'value' field of the newly created pair will have
  280. the value "", if no delimiter was found/
  281. Whitespaces (' ') will be removed from the beginnig of
  282. the parsed value.
  283. @param str A string to parse
  284. @param delim a delimiter to use while splitting into key,value
  285. @param next next pair node in the linked list
  286. @returns A newly crated hpair_t object. Use hpair_free()
  287. or hpair_free_deep() to free the pair.
  288. */
  289. hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t * next);
  290. /**
  291. Frees a given pair.
  292. @param pair the pair to free
  293. */
  294. void hpairnode_free(hpair_t * pair);
  295. /**
  296. Makes a deep free operation. All pairnodes,
  297. beginning with the given pari, in the
  298. linked list will be destroyed.
  299. @param pair the pair to start to free the linked list
  300. */
  301. void hpairnode_free_deep(hpair_t * pair);
  302. /**
  303. Returns the (key,value) pair, which key is the
  304. given 'key'.
  305. @param pair the first pair to start to search from.
  306. @param key key to find the in the pair.
  307. @returns if a value will be found, this function will
  308. return the value (do not free this string) or NULL
  309. if no pair was found with the key 'key'.
  310. */
  311. char *hpairnode_get(hpair_t * pair, const char *key);
  312. /**
  313. Returns the (key,value) pair, which key is the
  314. given 'key'. The case will be ignored while
  315. comparing the key strings.
  316. @param pair the first pair to start to search from.
  317. @param key key to find the in the pair.
  318. @returns if a value will be found, this function will
  319. return the value (do not free this string) or NULL
  320. if no pair was found with the key 'key'.
  321. */
  322. char *hpairnode_get_ignore_case(hpair_t * pair, const char *key);
  323. /**
  324. This function will create a new pair and fills the
  325. (key,value) fields of a given pair. Note that the 'next'
  326. field will not be copied.
  327. @param src the source pair object to copy.
  328. @returns a newly created pair with the same (key,value)
  329. pairs as in 'src'. This fields will be cloned. The'next'
  330. field will be set to NULL.
  331. @see hpairnode_copy_deep
  332. */
  333. hpair_t *hpairnode_copy(const hpair_t * src);
  334. /**
  335. Clones the hole linked list.
  336. @param src the source pair object to copy from
  337. @returns the first object in the linked list.
  338. @see hpairnode_copy
  339. */
  340. hpair_t *hpairnode_copy_deep(const hpair_t * src);
  341. /* Debug functions */
  342. void hpairnode_dump_deep(hpair_t * pair);
  343. void hpairnode_dump(hpair_t * pair);
  344. /**
  345. The protocol types in enumeration
  346. format. Used in some other nanohttp objects
  347. like hurl_t.
  348. @see hurl_t
  349. */
  350. typedef enum _hprotocol
  351. {
  352. PROTOCOL_HTTP,
  353. PROTOCOL_HTTPS,
  354. PROTOCOL_FTP
  355. } hprotocol_t;
  356. /**
  357. The URL object. A representation
  358. of an URL like:<P>
  359. [protocol]://[host]:[port]/[context]
  360. */
  361. typedef struct _hurl
  362. {
  363. /**
  364. The transfer protocol.
  365. Note that only PROTOCOL_HTTP is supported by nanohttp.
  366. */
  367. hprotocol_t protocol;
  368. /**
  369. The port number. If no port number was given in the URL,
  370. one of the default port numbers will be selected.
  371. URL_HTTP_DEFAULT_PORT
  372. URL_HTTPS_DEFAULT_PORT
  373. URL_FTP_DEFAULT_PORT
  374. */
  375. int port;
  376. /** The hostname */
  377. char host[URL_MAX_HOST_SIZE];
  378. /** The string after the hostname. */
  379. char context[URL_MAX_CONTEXT_SIZE];
  380. } hurl_t;
  381. /**
  382. Parses the given 'urlstr' and fills the given hurl_t object.
  383. @param obj the destination URL object to fill
  384. @param url the URL in string format
  385. @returns H_OK on success or one of the following otherwise
  386. URL_ERROR_UNKNOWN_PROTOCOL
  387. URL_ERROR_NO_PROTOCOL
  388. URL_ERROR_NO_HOST
  389. */
  390. herror_t hurl_parse(hurl_t * obj, const char *url);
  391. /*
  392. Object representation of the content-type field
  393. in a HTTP header:
  394. <P>
  395. Example:<P>
  396. text/xml; key="value" key2="value2' ...
  397. */
  398. typedef struct _content_type
  399. {
  400. char type[128];
  401. hpair_t *params;
  402. } content_type_t;
  403. /**
  404. Parses the given string and creates a new ccontent_type_t
  405. object.
  406. @param content_type_str the string representation of the
  407. content-type field in a HTTP header.
  408. @returns A newly created content_type_t object. Free this
  409. object with content_type_free();
  410. @see content_type_free
  411. */
  412. content_type_t *content_type_new(const char *content_type_str);
  413. /**
  414. Frees the given content_type_t object
  415. */
  416. void content_type_free(content_type_t * ct);
  417. /*
  418. part. Attachment
  419. */
  420. typedef struct _part
  421. {
  422. char id[250];
  423. char location[250];
  424. hpair_t *header;
  425. char content_type[128];
  426. char transfer_encoding[128];
  427. char filename[250];
  428. struct _part *next;
  429. int deleteOnExit; /* default is 0 */
  430. } part_t;
  431. part_t *part_new(const char *id, const char *filename,
  432. const char *content_type, const char *transfer_encoding,
  433. part_t * next);
  434. void part_free(part_t * part);
  435. /*
  436. Attachments
  437. */
  438. typedef struct _attachments
  439. {
  440. part_t *parts;
  441. part_t *last;
  442. part_t *root_part;
  443. } attachments_t;
  444. attachments_t *attachments_new(); /* should be used internally */
  445. /*
  446. Free a attachment. Create attachments with MIME
  447. and DIME (DIME is not supported yet).
  448. @see mime_get_attachments
  449. */
  450. void attachments_free(attachments_t * message);
  451. void attachments_add_part(attachments_t * attachments, part_t * part);
  452. #ifdef __cplusplus
  453. }
  454. #endif
  455. #endif