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.

730 lines
13 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-common.c,v 1.30 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003 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: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_STDIO_H
  28. #include <stdio.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_STDARG_H
  34. #include <stdarg.h>
  35. #endif
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #ifdef HAVE_CTYPE_H
  40. #include <ctype.h>
  41. #endif
  42. #ifdef HAVE_PTHREAD_H
  43. #include <pthread.h>
  44. #endif
  45. #ifdef MEM_DEBUG
  46. #include <utils/alloc.h>
  47. #endif
  48. #include "nanohttp-common.h"
  49. #include "nanohttp-logging.h"
  50. static int
  51. strcmpigcase(const char *s1, const char *s2)
  52. {
  53. int l1, l2, i;
  54. if (s1 == NULL && s2 == NULL)
  55. return 1;
  56. if (s1 == NULL || s2 == NULL)
  57. return 0;
  58. l1 = strlen(s1);
  59. l2 = strlen(s2);
  60. if (l1 != l2)
  61. return 0;
  62. for (i = 0; i < l1; i++)
  63. if (toupper(s1[i]) != toupper(s2[i]))
  64. return 0;
  65. return 1;
  66. }
  67. typedef struct _herror_impl_t
  68. {
  69. int errcode;
  70. char message[250];
  71. char func[100];
  72. } herror_impl_t;
  73. herror_t
  74. herror_new(const char *func, int errcode, const char *format, ...)
  75. {
  76. va_list ap;
  77. herror_impl_t *impl = (herror_impl_t *) malloc(sizeof(herror_impl_t));
  78. impl->errcode = errcode;
  79. strcpy(impl->func, func);
  80. va_start(ap, format);
  81. vsprintf(impl->message, format, ap);
  82. va_end(ap);
  83. return (herror_t) impl;
  84. }
  85. int
  86. herror_code(herror_t err)
  87. {
  88. herror_impl_t *impl = (herror_impl_t *) err;
  89. if (!err)
  90. return H_OK;
  91. return impl->errcode;
  92. }
  93. char *
  94. herror_func(herror_t err)
  95. {
  96. herror_impl_t *impl = (herror_impl_t *) err;
  97. if (!err)
  98. return "";
  99. return impl->func;
  100. }
  101. char *
  102. herror_message(herror_t err)
  103. {
  104. herror_impl_t *impl = (herror_impl_t *) err;
  105. if (!err)
  106. return "";
  107. return impl->message;
  108. }
  109. void
  110. herror_release(herror_t err)
  111. {
  112. herror_impl_t *impl = (herror_impl_t *) err;
  113. if (!err)
  114. return;
  115. free(impl);
  116. }
  117. hpair_t *
  118. hpairnode_new(const char *key, const char *value, hpair_t * next)
  119. {
  120. hpair_t *pair;
  121. log_verbose3("new pair ('%s','%s')", SAVE_STR(key), SAVE_STR(value));
  122. pair = (hpair_t *) malloc(sizeof(hpair_t));
  123. if (key != NULL)
  124. {
  125. pair->key = (char *) malloc(strlen(key) + 1);
  126. strcpy(pair->key, key);
  127. }
  128. else
  129. {
  130. pair->key = NULL;
  131. }
  132. if (value != NULL)
  133. {
  134. pair->value = (char *) malloc(strlen(value) + 1);
  135. strcpy(pair->value, value);
  136. }
  137. else
  138. {
  139. pair->value = NULL;
  140. }
  141. pair->next = next;
  142. return pair;
  143. }
  144. hpair_t *
  145. hpairnode_parse(const char *str, const char *delim, hpair_t * next)
  146. {
  147. hpair_t *pair;
  148. char *key, *value;
  149. int c;
  150. pair = (hpair_t *) malloc(sizeof(hpair_t));
  151. pair->key = "";
  152. pair->value = "";
  153. pair->next = next;
  154. key = strtok_r((char *) str, delim, &value);
  155. if (key != NULL)
  156. {
  157. pair->key = (char *) malloc(strlen(key) + 1);
  158. strcpy(pair->key, key);
  159. }
  160. if (value != NULL)
  161. {
  162. for (c = 0; value[c] == ' '; c++); /* skip white space */
  163. pair->value = (char *) malloc(strlen(&value[c]) + 1);
  164. strcpy(pair->value, &value[c]);
  165. }
  166. return pair;
  167. }
  168. hpair_t *
  169. hpairnode_copy(const hpair_t * src)
  170. {
  171. hpair_t *pair;
  172. if (src == NULL)
  173. return NULL;
  174. pair = hpairnode_new(src->key, src->value, NULL);
  175. return pair;
  176. }
  177. hpair_t *
  178. hpairnode_copy_deep(const hpair_t * src)
  179. {
  180. hpair_t *pair, *result, *next;
  181. if (src == NULL)
  182. return NULL;
  183. result = hpairnode_copy(src);
  184. next = src->next;
  185. pair = result;
  186. while (next != NULL)
  187. {
  188. pair->next = hpairnode_copy(next);
  189. pair = pair->next;
  190. next = next->next;
  191. }
  192. return result;
  193. }
  194. void
  195. hpairnode_dump(hpair_t * pair)
  196. {
  197. if (pair == NULL)
  198. {
  199. log_verbose1("(NULL)[]");
  200. return;
  201. }
  202. log_verbose5("(%p)['%s','%s','%p']", pair,
  203. SAVE_STR(pair->key), SAVE_STR(pair->value), pair->next);
  204. }
  205. void
  206. hpairnode_dump_deep(hpair_t * pair)
  207. {
  208. hpair_t *p;
  209. p = pair;
  210. log_verbose1("-- BEGIN dump hpairnode_t --");
  211. while (p != NULL)
  212. {
  213. hpairnode_dump(p);
  214. p = p->next;
  215. }
  216. log_verbose1("-- END dump hpairnode_t --\n");
  217. }
  218. void
  219. hpairnode_free(hpair_t * pair)
  220. {
  221. if (pair == NULL)
  222. return;
  223. free(pair->key);
  224. free(pair->value);
  225. free(pair);
  226. }
  227. void
  228. hpairnode_free_deep(hpair_t * pair)
  229. {
  230. hpair_t *tmp;
  231. while (pair != NULL)
  232. {
  233. tmp = pair->next;
  234. hpairnode_free(pair);
  235. pair = tmp;
  236. }
  237. }
  238. char *
  239. hpairnode_get_ignore_case(hpair_t * pair, const char *key)
  240. {
  241. if (key == NULL)
  242. {
  243. log_error1("key is NULL");
  244. return NULL;
  245. }
  246. while (pair != NULL)
  247. {
  248. if (pair->key != NULL)
  249. {
  250. if (strcmpigcase(pair->key, key))
  251. {
  252. return pair->value;
  253. }
  254. }
  255. pair = pair->next;
  256. }
  257. return NULL;
  258. }
  259. char *
  260. hpairnode_get(hpair_t * pair, const char *key)
  261. {
  262. if (key == NULL)
  263. {
  264. log_error1("key is NULL");
  265. return NULL;
  266. }
  267. while (pair != NULL)
  268. {
  269. if (pair->key != NULL)
  270. {
  271. if (!strcmp(pair->key, key))
  272. {
  273. return pair->value;
  274. }
  275. }
  276. pair = pair->next;
  277. }
  278. return NULL;
  279. }
  280. static void
  281. hurl_dump(const hurl_t * url)
  282. {
  283. if (url == NULL)
  284. {
  285. log_error1("url is NULL!");
  286. return;
  287. }
  288. log_verbose2("PROTOCOL : %d", url->protocol);
  289. log_verbose2(" HOST : %s", url->host);
  290. log_verbose2(" PORT : %d", url->port);
  291. log_verbose2(" CONTEXT : %s", url->context);
  292. }
  293. herror_t
  294. hurl_parse(hurl_t * url, const char *urlstr)
  295. {
  296. int iprotocol;
  297. int ihost;
  298. int iport;
  299. int len;
  300. int size;
  301. char tmp[8];
  302. char protocol[1024];
  303. iprotocol = 0;
  304. len = strlen(urlstr);
  305. /* find protocol */
  306. while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0')
  307. {
  308. iprotocol++;
  309. }
  310. if (iprotocol == 0)
  311. {
  312. log_error1("no protocol");
  313. return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol");
  314. }
  315. if (iprotocol + 3 >= len)
  316. {
  317. log_error1("no host");
  318. return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host");
  319. }
  320. if (urlstr[iprotocol] != ':'
  321. && urlstr[iprotocol + 1] != '/' && urlstr[iprotocol + 2] != '/')
  322. {
  323. log_error1("no protocol");
  324. return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol");
  325. }
  326. /* find host */
  327. ihost = iprotocol + 3;
  328. while (urlstr[ihost] != ':'
  329. && urlstr[ihost] != '/' && urlstr[ihost] != '\0')
  330. {
  331. ihost++;
  332. }
  333. if (ihost == iprotocol + 1)
  334. {
  335. log_error1("no host");
  336. return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host");
  337. }
  338. /* find port */
  339. iport = ihost;
  340. if (ihost + 1 < len)
  341. {
  342. if (urlstr[ihost] == ':')
  343. {
  344. while (urlstr[iport] != '/' && urlstr[iport] != '\0')
  345. {
  346. iport++;
  347. }
  348. }
  349. }
  350. /* find protocol */
  351. strncpy(protocol, urlstr, iprotocol);
  352. protocol[iprotocol] = '\0';
  353. if (strcmpigcase(protocol, "http"))
  354. url->protocol = PROTOCOL_HTTP;
  355. else if (strcmpigcase(protocol, "https"))
  356. url->protocol = PROTOCOL_HTTPS;
  357. else if (strcmpigcase(protocol, "ftp"))
  358. url->protocol = PROTOCOL_FTP;
  359. else
  360. return herror_new("hurl_parse",
  361. URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol '%s'",
  362. protocol);
  363. /* TODO (#1#): add max of size and URL_MAX_HOST_SIZE */
  364. size = ihost - iprotocol - 3;
  365. strncpy(url->host, &urlstr[iprotocol + 3], size);
  366. url->host[size] = '\0';
  367. if (iport > ihost)
  368. {
  369. size = iport - ihost;
  370. strncpy(tmp, &urlstr[ihost + 1], size);
  371. url->port = atoi(tmp);
  372. }
  373. else
  374. {
  375. switch (url->protocol)
  376. {
  377. case PROTOCOL_HTTP:
  378. url->port = URL_DEFAULT_PORT_HTTP;
  379. break;
  380. case PROTOCOL_HTTPS:
  381. url->port = URL_DEFAULT_PORT_HTTPS;
  382. break;
  383. case PROTOCOL_FTP:
  384. url->port = URL_DEFAULT_PORT_FTP;
  385. break;
  386. }
  387. }
  388. len = strlen(urlstr);
  389. if (len > iport)
  390. {
  391. /* TODO (#1#): find max of size and URL_MAX_CONTEXT_SIZE */
  392. size = len - iport;
  393. strncpy(url->context, &urlstr[iport], size);
  394. url->context[size] = '\0';
  395. }
  396. else
  397. {
  398. url->context[0] = '\0';
  399. }
  400. hurl_dump(url);
  401. return H_OK;
  402. }
  403. /* Content-type stuff */
  404. content_type_t *
  405. content_type_new(const char *content_type_str)
  406. {
  407. hpair_t *pair = NULL, *last = NULL;
  408. content_type_t *ct;
  409. char ch, key[256], value[256];
  410. int inQuote = 0, i = 0, c = 0, begin = 0, len;
  411. int mode = 0;
  412. /* 0: searching ';' 1: process key 2: process value */
  413. /* Create object */
  414. ct = (content_type_t *) malloc(sizeof(content_type_t));
  415. ct->params = NULL;
  416. len = strlen(content_type_str);
  417. while (i <= len)
  418. {
  419. if (i != len)
  420. ch = content_type_str[i++];
  421. else
  422. {
  423. ch = ' ';
  424. i++;
  425. }
  426. switch (mode)
  427. {
  428. case 0:
  429. if (ch == ';')
  430. {
  431. ct->type[c] = '\0';
  432. c = 0;
  433. mode = 1;
  434. }
  435. else if (ch != ' ' && ch != '\t' && ch != '\r')
  436. ct->type[c++] = ch;
  437. break;
  438. case 1:
  439. if (ch == '=')
  440. {
  441. key[c] = '\0';
  442. c = 0;
  443. mode = 2;
  444. }
  445. else if (ch != ' ' && ch != '\t' && ch != '\r')
  446. key[c++] = ch;
  447. break;
  448. case 2:
  449. if (ch != ' ')
  450. begin = 1;
  451. if ((ch == ' ' || ch == ';') && !inQuote && begin)
  452. {
  453. value[c] = '\0';
  454. pair = hpairnode_new(key, value, NULL);
  455. if (ct->params == NULL)
  456. ct->params = pair;
  457. else
  458. last->next = pair;
  459. last = pair;
  460. c = 0;
  461. begin = 0;
  462. mode = 1;
  463. }
  464. else if (ch == '"')
  465. inQuote = !inQuote;
  466. else if (begin && ch != '\r')
  467. value[c++] = ch;
  468. break;
  469. }
  470. }
  471. return ct;
  472. }
  473. void
  474. content_type_free(content_type_t * ct)
  475. {
  476. if (!ct)
  477. return;
  478. hpairnode_free_deep(ct->params);
  479. free(ct);
  480. }
  481. part_t *
  482. part_new(const char *id, const char *filename,
  483. const char *content_type, const char *transfer_encoding,
  484. part_t * next)
  485. {
  486. part_t *part = (part_t *) malloc(sizeof(part_t));
  487. part->header = NULL;
  488. part->next = next;
  489. part->deleteOnExit = 0;
  490. strcpy(part->id, id);
  491. strcpy(part->filename, filename);
  492. if (content_type)
  493. strcpy(part->content_type, content_type);
  494. else
  495. part->content_type[0] = '\0';
  496. part->header = hpairnode_new(HEADER_CONTENT_ID, id, part->header);
  497. /* TODO (#1#): encoding is always binary. implement also others! */
  498. /* part->header = hpairnode_new(HEADER_CONTENT_TRANSFER_ENCODING, "binary", part->header);*/
  499. strcpy(part->transfer_encoding,
  500. transfer_encoding ? transfer_encoding : "binary");
  501. if (content_type)
  502. {
  503. part->header =
  504. hpairnode_new(HEADER_CONTENT_TYPE, content_type, part->header);
  505. }
  506. else
  507. {
  508. /* TODO (#1#): get content-type from mime type list */
  509. }
  510. return part;
  511. }
  512. void
  513. part_free(part_t * part)
  514. {
  515. if (part == NULL)
  516. return;
  517. if (part->deleteOnExit)
  518. {
  519. remove(part->filename);
  520. }
  521. hpairnode_free_deep(part->header);
  522. free(part);
  523. }
  524. attachments_t *
  525. attachments_new() /* should be used internally */
  526. {
  527. attachments_t *attachments =
  528. (attachments_t *) malloc(sizeof(attachments_t));
  529. attachments->parts = NULL;
  530. attachments->last = NULL;
  531. attachments->root_part = NULL;
  532. return attachments;
  533. }
  534. void
  535. attachments_add_part(attachments_t * attachments, part_t * part)
  536. {
  537. /* paranoya check */
  538. if (!attachments)
  539. return;
  540. if (attachments->last)
  541. attachments->last->next = part;
  542. else
  543. attachments->parts = part;
  544. attachments->last = part;
  545. }
  546. /*
  547. Free a mime message
  548. */
  549. void
  550. attachments_free(attachments_t * message)
  551. {
  552. part_t *tmp, *part;
  553. if (message == NULL)
  554. return;
  555. part = message->parts;
  556. while (part)
  557. {
  558. tmp = part->next;
  559. part_free(part);
  560. part = tmp;
  561. }
  562. if (message->root_part)
  563. part_free(message->root_part);
  564. /* TODO (#1#): HERE IS A BUG!!!! */
  565. free(message);
  566. }
  567. #ifdef WIN32
  568. /* strtok_r() */
  569. char *
  570. strtok_r(char *s, const char *delim, char **save_ptr)
  571. {
  572. char *token;
  573. if (s == NULL)
  574. s = *save_ptr;
  575. /* Scan leading delimiters. */
  576. s += strspn(s, delim);
  577. if (*s == '\0')
  578. return NULL;
  579. /* Find the end of the token. */
  580. token = s;
  581. s = strpbrk(token, delim);
  582. if (s == NULL)
  583. /* This token finishes the string. */
  584. *save_ptr = strchr(token, '\0');
  585. else
  586. {
  587. /* Terminate the token and make *SAVE_PTR point past it. */
  588. *s = '\0';
  589. *save_ptr = s + 1;
  590. }
  591. return token;
  592. }
  593. /* localtime_r() */
  594. struct tm *
  595. localtime_r(const time_t * const timep, struct tm *p_tm)
  596. {
  597. static struct tm *tmp;
  598. tmp = localtime(timep);
  599. if (tmp)
  600. {
  601. memcpy(p_tm, tmp, sizeof(struct tm));
  602. tmp = p_tm;
  603. }
  604. return tmp;
  605. }
  606. #endif