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.

959 lines
24 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * _ _ _ _ _ __
  3. * | \/ | | | | \/ | | _/
  4. * |_''_| |_| |_''_| |_'/ PARSER
  5. *
  6. * $Id: nanohttp-mime.c,v 1.13 2006/07/09 16:24:19 snowdrop Exp $
  7. *
  8. * CSOAP Project: A http client/server library in C
  9. * Copyright (C) 2003-2004 Ferhat Ayaz
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Library General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Library General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public
  22. * License along with this library; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 02111-1307, USA.
  25. *
  26. * Email: ferhatayaz@yahoo.com
  27. ******************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #ifdef HAVE_STDIO_H
  32. #include <stdio.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. /*----------------------------------------------------------------
  38. Buffered Reader. A helper object to read bytes from a source
  39. ----------------------------------------------------------------*/
  40. #ifdef MEM_DEBUG
  41. #include <utils/alloc.h>
  42. #endif
  43. #include "nanohttp-logging.h"
  44. #include "nanohttp-mime.h"
  45. /* ------------------------------------------------------------------
  46. MIME Parser
  47. ------------------------------------------------------------------*/
  48. typedef void (*MIME_part_begin) (void *);
  49. typedef void (*MIME_part_end) (void *);
  50. typedef void (*MIME_parse_begin) (void *);
  51. typedef void (*MIME_parse_end) (void *);
  52. typedef void (*MIME_ERROR_bytes) (void *, const unsigned char *, int);
  53. typedef enum _MIME_parser_status
  54. {
  55. MIME_PARSER_INCOMPLETE_MESSAGE,
  56. MIME_PARSER_READ_ERROR,
  57. MIME_PARSER_OK
  58. } MIME_parser_status;
  59. typedef enum _MIME_read_status
  60. {
  61. MIME_READ_OK,
  62. MIME_READ_EOF,
  63. MIME_READ_ERROR
  64. } MIME_read_status;
  65. #define MIME_READER_MAX_BUFFER_SIZE 1054
  66. #define MIME_PARSER_BUFFER_SIZE 1054
  67. typedef MIME_read_status(*MIME_read_function) (void *, unsigned char *,
  68. int *);
  69. /**
  70. Reader structure. This will be use
  71. by the parser
  72. */
  73. typedef struct _MIME_reader
  74. {
  75. int size;
  76. int marker;
  77. int current;
  78. MIME_read_function read_function;
  79. char buffer[MIME_READER_MAX_BUFFER_SIZE];
  80. void *userdata;
  81. } MIME_reader;
  82. MIME_read_status MIME_filereader_function(void *userdata,
  83. unsigned char *dest, int *size);
  84. typedef struct _MIME_callbacks
  85. {
  86. MIME_part_begin part_begin_cb;
  87. MIME_part_end part_end_cb;
  88. MIME_parse_begin parse_begin_cb;
  89. MIME_parse_end parse_end_cb;
  90. MIME_ERROR_bytes received_bytes_cb;
  91. } MIME_callbacks;
  92. MIME_parser_status MIME_parse(MIME_read_function reader_function,
  93. void *reader_userdata,
  94. const char *user_boundary,
  95. const MIME_callbacks * callbacks,
  96. void *callbacks_userdata);
  97. /**
  98. Initialize a reader
  99. */
  100. void
  101. MIME_reader_init(MIME_reader * reader,
  102. MIME_read_function reader_function, void *userdata)
  103. {
  104. reader->size = 0;
  105. reader->marker = -1;
  106. reader->current = 0;
  107. reader->userdata = userdata;
  108. reader->read_function = reader_function;
  109. }
  110. /**
  111. Read data from a reader source.
  112. */
  113. MIME_read_status
  114. MIME_reader_read(MIME_reader * reader, unsigned char *buffer, int size)
  115. {
  116. MIME_read_status status;
  117. int readed_size;
  118. unsigned char tempBuffer[MIME_READER_MAX_BUFFER_SIZE];
  119. int rest_size;
  120. /* Check if buffer is full */
  121. if (reader->size == reader->current)
  122. {
  123. /* Yes, so read some data */
  124. /* First handle marker */
  125. if (reader->marker > -1)
  126. {
  127. if (reader->marker != 0)
  128. {
  129. memcpy(tempBuffer, reader->buffer + reader->marker,
  130. reader->size - reader->marker);
  131. memcpy(reader->buffer, tempBuffer, reader->size - reader->marker);
  132. reader->current = reader->size - reader->marker;
  133. }
  134. else if (reader->current == MIME_READER_MAX_BUFFER_SIZE - 1)
  135. {
  136. fprintf(stderr, "Marker error");
  137. return MIME_READ_ERROR;
  138. }
  139. reader->marker = 0;
  140. }
  141. else
  142. reader->current = 0;
  143. readed_size = MIME_READER_MAX_BUFFER_SIZE - reader->current - 1;
  144. status = reader->read_function(reader->userdata,
  145. reader->buffer + reader->current,
  146. &readed_size);
  147. if (status == MIME_READ_OK)
  148. {
  149. reader->size = readed_size + reader->current;
  150. }
  151. else
  152. return status;
  153. }
  154. if (size <= reader->size - reader->current)
  155. {
  156. memcpy(buffer, reader->buffer + reader->current, size);
  157. reader->current += size;
  158. return MIME_READ_OK;
  159. }
  160. else
  161. {
  162. /* Fill rest data */
  163. rest_size = reader->size - reader->current;
  164. memcpy(buffer, reader->buffer + reader->current, rest_size);
  165. reader->current = reader->size;
  166. return MIME_reader_read(reader, buffer + rest_size, size - rest_size);
  167. }
  168. }
  169. void
  170. MIME_reader_set_marker(MIME_reader * reader)
  171. {
  172. reader->marker = reader->current;
  173. }
  174. void
  175. MIME_reader_unset_marker(MIME_reader * reader)
  176. {
  177. reader->marker = -1;
  178. }
  179. void
  180. MIME_reader_jump_marker(MIME_reader * reader)
  181. {
  182. reader->current = reader->marker;
  183. }
  184. typedef struct _MIME_buffer
  185. {
  186. unsigned char data[MIME_PARSER_BUFFER_SIZE];
  187. int size;
  188. } MIME_buffer;
  189. void
  190. MIME_buffer_init(MIME_buffer * buffer)
  191. {
  192. buffer->size = 0;
  193. }
  194. void
  195. MIME_buffer_add(MIME_buffer * buffer, unsigned char ch)
  196. {
  197. buffer->data[buffer->size++] = ch;
  198. }
  199. void
  200. MIME_buffer_add_bytes(MIME_buffer * buffer, unsigned char *bytes, int size)
  201. {
  202. memcpy(buffer->data, bytes, size);
  203. buffer->size += size;
  204. }
  205. int
  206. MIME_buffer_is_full(MIME_buffer * buffer)
  207. {
  208. return buffer->size + 150 >= MIME_PARSER_BUFFER_SIZE;
  209. }
  210. int
  211. MIME_buffer_is_empty(MIME_buffer * buffer)
  212. {
  213. return buffer->size == 0;
  214. }
  215. void
  216. MIME_buffer_clear(MIME_buffer * buffer)
  217. {
  218. buffer->size = 0;
  219. }
  220. MIME_parser_status
  221. MIME_parse(MIME_read_function reader_function,
  222. void *reader_userdata,
  223. const char *user_boundary,
  224. const MIME_callbacks * callbacks, void *callbacks_userdata)
  225. {
  226. char boundary[150];
  227. unsigned char ch[153];
  228. int boundary_length, n, ignore = 0;
  229. MIME_reader reader;
  230. MIME_buffer buffer;
  231. MIME_read_status status;
  232. /* Init reader */
  233. MIME_reader_init(&reader, reader_function, reader_userdata);
  234. /* Init buffer */
  235. MIME_buffer_init(&buffer);
  236. /* Set boundary related stuff */
  237. sprintf(boundary, "\n--%s", user_boundary);
  238. boundary_length = strlen(boundary);
  239. /* Call parse begin callback */
  240. callbacks->parse_begin_cb(callbacks_userdata);
  241. while (1)
  242. {
  243. set_marker:
  244. /* Set marker */
  245. MIME_reader_set_marker(&reader);
  246. read_byte:
  247. /* Read 1 byte */
  248. status = MIME_reader_read(&reader, ch, 1);
  249. if (status == MIME_READ_EOF)
  250. return MIME_PARSER_INCOMPLETE_MESSAGE;
  251. else if (status == MIME_READ_ERROR)
  252. return MIME_PARSER_READ_ERROR;
  253. if (ch[0] == '\r' && !ignore)
  254. {
  255. n = 0;
  256. while (n < boundary_length)
  257. {
  258. /* Read 1 byte */
  259. status = MIME_reader_read(&reader, ch, 1);
  260. if (status == MIME_READ_EOF)
  261. return MIME_PARSER_INCOMPLETE_MESSAGE;
  262. else if (status == MIME_READ_ERROR)
  263. return MIME_PARSER_READ_ERROR;
  264. /* Check if byte is in boundary */
  265. if (ch[0] == boundary[n])
  266. {
  267. n = n + 1;
  268. continue;
  269. }
  270. else
  271. {
  272. MIME_reader_jump_marker(&reader);
  273. ignore = 1;
  274. goto read_byte;
  275. }
  276. } /* while n < boundary_length */
  277. /* Read 1 byte */
  278. status = MIME_reader_read(&reader, ch, 1);
  279. if (status == MIME_READ_EOF)
  280. return MIME_PARSER_INCOMPLETE_MESSAGE;
  281. else if (status == MIME_READ_ERROR)
  282. return MIME_PARSER_READ_ERROR;
  283. /* Show if byte is '\r' */
  284. if (ch[0] == '\r')
  285. {
  286. /* Read 1 byte */
  287. status = MIME_reader_read(&reader, ch, 1);
  288. if (status == MIME_READ_EOF)
  289. return MIME_PARSER_INCOMPLETE_MESSAGE;
  290. else if (status == MIME_READ_ERROR)
  291. return MIME_PARSER_READ_ERROR;
  292. /* Check if byte is '\n' */
  293. if (ch[0] == '\n')
  294. {
  295. if (!MIME_buffer_is_empty(&buffer))
  296. {
  297. /* Invoke callback */
  298. callbacks->received_bytes_cb(callbacks_userdata, buffer.data,
  299. buffer.size);
  300. /* Empty the buffer */
  301. MIME_buffer_clear(&buffer);
  302. /* Invoke End Part */
  303. callbacks->part_end_cb(callbacks_userdata);
  304. }
  305. /* Invoke start new Part */
  306. callbacks->part_begin_cb(callbacks_userdata);
  307. goto set_marker;
  308. } /* if (ch[0] == '\n') */
  309. else
  310. {
  311. /* Jump to marker and read bytes */
  312. MIME_reader_jump_marker(&reader);
  313. MIME_reader_read(&reader, ch, boundary_length + 2);
  314. MIME_buffer_add_bytes(&buffer, ch, boundary_length + 2);
  315. if (MIME_buffer_is_full(&buffer))
  316. {
  317. /* Invoke callback */
  318. callbacks->received_bytes_cb(callbacks_userdata, buffer.data,
  319. buffer.size);
  320. /* Empty the buffer */
  321. MIME_buffer_clear(&buffer);
  322. }
  323. } /* else of if (ch[0] == '\n') */
  324. } /* if (ch[0] == '\r') */
  325. else
  326. {
  327. if (ch[0] == '-')
  328. {
  329. /* Read 1 byte */
  330. status = MIME_reader_read(&reader, ch, 1);
  331. if (status == MIME_READ_EOF)
  332. return MIME_PARSER_INCOMPLETE_MESSAGE;
  333. else if (status == MIME_READ_ERROR)
  334. return MIME_PARSER_READ_ERROR;
  335. if (ch[0] == '-')
  336. {
  337. if (!MIME_buffer_is_empty(&buffer))
  338. {
  339. /* Invoke callback */
  340. callbacks->received_bytes_cb(callbacks_userdata, buffer.data,
  341. buffer.size);
  342. /* Empty the buffer */
  343. MIME_buffer_clear(&buffer);
  344. /* Invoke End Part */
  345. callbacks->part_end_cb(callbacks_userdata);
  346. }
  347. /* Invoke start new Part */
  348. callbacks->parse_end_cb(callbacks_userdata);
  349. /* Finish parsing */
  350. /* TODO (#1#): We assume that after -- comes \r\n This is not
  351. always correct */
  352. return MIME_PARSER_OK;
  353. } /* if (ch[0] == '-') */
  354. else
  355. {
  356. MIME_reader_jump_marker(&reader);
  357. ignore = 1;
  358. goto read_byte;
  359. } /* else of if (ch[0] == '-') */
  360. } /* if (ch[0] == '-') */
  361. else
  362. {
  363. MIME_reader_jump_marker(&reader);
  364. ignore = 1;
  365. goto read_byte;
  366. } /* else of if (ch[0] == '-') */
  367. } /* else of if (ch[0] == '\r') */
  368. } /* if ch[0] == '\r' && !ignore */
  369. else
  370. {
  371. ignore = 0;
  372. MIME_buffer_add(&buffer, ch[0]);
  373. /* Chec if buffer is full */
  374. if (MIME_buffer_is_full(&buffer))
  375. {
  376. /* Invoke callback */
  377. callbacks->received_bytes_cb(callbacks_userdata, buffer.data,
  378. buffer.size);
  379. /* Empty the buffer */
  380. MIME_buffer_clear(&buffer);
  381. }
  382. } /* else of if ch[0] == '\r' && !ignore */
  383. } /* while (1) */
  384. }
  385. MIME_read_status
  386. MIME_filereader_function(void *userdata, unsigned char *dest, int *size)
  387. {
  388. FILE *f = (FILE *) userdata;
  389. if (feof(f))
  390. return MIME_READ_EOF;
  391. *size = fread(dest, 1, *size, f);
  392. return MIME_READ_OK;
  393. }
  394. /* ------------------------------------------------------------------
  395. "multipart/related" MIME Message Builder
  396. ------------------------------------------------------------------*/
  397. /*
  398. Callback data to use internally
  399. */
  400. typedef struct _mime_callback_data
  401. {
  402. int part_id;
  403. attachments_t *message;
  404. part_t *current_part;
  405. int buffer_capacity;
  406. char header[4064];
  407. char root_id[256];
  408. int header_index;
  409. int header_search;
  410. FILE *current_fd;
  411. char root_dir[512];
  412. } mime_callback_data_t;
  413. MIME_read_status
  414. mime_streamreader_function(void *userdata, unsigned char *dest, int *size)
  415. {
  416. int readed = 0;
  417. http_input_stream_t *in = (http_input_stream_t *) userdata;
  418. if (!http_input_stream_is_ready(in))
  419. return MIME_READ_EOF;
  420. readed = http_input_stream_read(in, dest, *size);
  421. /*
  422. log_info1("http_input_stream_read() returned 0"); */
  423. if (readed == -1)
  424. {
  425. log_error4("[%d] %s():%s ", herror_code(in->err), herror_func(in->err),
  426. herror_message(in->err));
  427. }
  428. *size = readed;
  429. if (*size != -1)
  430. {
  431. return MIME_READ_OK;
  432. }
  433. return MIME_READ_ERROR;
  434. }
  435. /*
  436. Start Callback functions
  437. */
  438. static void
  439. _mime_parse_begin(void *data)
  440. {
  441. /* Nothing to do
  442. mime_callback_data_t *cbdata = (mime_callback_data_t)data;
  443. */
  444. log_verbose2("Begin parse (%p)", data);
  445. }
  446. static void
  447. _mime_parse_end(void *data)
  448. {
  449. /* Nothing to do
  450. mime_callback_data_t *cbdata = (mime_callback_data_t)data;
  451. */
  452. log_verbose2("End parse (%p)", data);
  453. }
  454. static void
  455. _mime_part_begin(void *data)
  456. {
  457. char buffer[1054];
  458. mime_callback_data_t *cbdata = (mime_callback_data_t *) data;
  459. part_t *part;
  460. log_verbose2("Begin Part (%p)", data);
  461. part = (part_t *) malloc(sizeof(part_t));
  462. part->next = NULL;
  463. if (cbdata->current_part)
  464. cbdata->current_part->next = part;
  465. cbdata->current_part = part;
  466. if (!cbdata->message->parts)
  467. cbdata->message->parts = part;
  468. cbdata->header[0] = '\0';
  469. cbdata->header_index = 0;
  470. cbdata->header_search = 0;
  471. #ifdef WIN32
  472. sprintf(buffer, "%s\\mime_%p_%d.part", cbdata->root_dir,
  473. cbdata, cbdata->part_id++);
  474. #else
  475. sprintf(buffer, "%s/mime_%p_%d.part", cbdata->root_dir,
  476. cbdata, cbdata->part_id++);
  477. #endif
  478. /* log_info2("Creating FILE ('%s') deleteOnExit=1", buffer);*/
  479. part->deleteOnExit = 1;
  480. cbdata->current_fd = fopen(buffer, "wb");
  481. if (cbdata->current_fd)
  482. strcpy(cbdata->current_part->filename, buffer);
  483. else
  484. log_error2("Can not open file for write '%s'", buffer);
  485. }
  486. static void
  487. _mime_part_end(void *data)
  488. {
  489. mime_callback_data_t *cbdata = (mime_callback_data_t *) data;
  490. log_verbose2("End Part (%p)", data);
  491. if (cbdata->current_fd)
  492. {
  493. fclose(cbdata->current_fd);
  494. cbdata->current_fd = NULL;
  495. }
  496. }
  497. static hpair_t *
  498. _mime_process_header(char *buffer)
  499. {
  500. int i = 0, c = 0, proc_key, begin = 0;
  501. hpair_t *first = NULL, *last = NULL;
  502. char key[1054], value[1054];
  503. key[0] = '\0';
  504. value[0] = '\0';
  505. proc_key = 1;
  506. while (buffer[i] != '\0')
  507. {
  508. if (buffer[i] == '\r' && buffer[i + 1] == '\n')
  509. {
  510. value[c] = '\0';
  511. if (last)
  512. {
  513. last->next = hpairnode_new(key, value, NULL);
  514. last = last->next;
  515. }
  516. else
  517. {
  518. first = last = hpairnode_new(key, value, NULL);
  519. }
  520. proc_key = 1;
  521. c = 0;
  522. i++;
  523. }
  524. else if (buffer[i] == ':')
  525. {
  526. key[c] = '\0';
  527. c = 0;
  528. begin = 0;
  529. proc_key = 0;
  530. }
  531. else
  532. {
  533. if (proc_key)
  534. key[c++] = buffer[i];
  535. else
  536. {
  537. if (buffer[i] != ' ')
  538. begin = 1;
  539. if (begin)
  540. value[c++] = buffer[i];
  541. }
  542. }
  543. i++;
  544. }
  545. return first;
  546. }
  547. static void
  548. _mime_received_bytes(void *data, const unsigned char *bytes, int size)
  549. {
  550. int i = 0;
  551. char *id, *type, *location;
  552. mime_callback_data_t *cbdata = (mime_callback_data_t *) data;
  553. if (!cbdata)
  554. {
  555. log_error1
  556. ("MIME transport error Called <received bytes> without initializing\n");
  557. return;
  558. }
  559. if (!cbdata->current_part)
  560. {
  561. log_error1
  562. ("MIME transport error Called <received bytes> without initializing\n");
  563. return;
  564. }
  565. /* log_verbose4("Received %d bytes (%p), header_search = %d",
  566. size, data, cbdata->header_search);
  567. */
  568. if (cbdata->header_search < 4)
  569. {
  570. /* Find \r\n\r\n in bytes */
  571. for (i = 0; i < size; i++)
  572. {
  573. if (cbdata->header_search == 0)
  574. {
  575. if (bytes[i] == '\r')
  576. cbdata->header_search++;
  577. else
  578. {
  579. cbdata->header[cbdata->header_index++] = bytes[i];
  580. cbdata->header_search = 0;
  581. }
  582. }
  583. else if (cbdata->header_search == 1)
  584. {
  585. if (bytes[i] == '\n')
  586. cbdata->header_search++;
  587. else
  588. {
  589. cbdata->header[cbdata->header_index++] = '\r';
  590. cbdata->header[cbdata->header_index++] = bytes[i];
  591. cbdata->header_search = 0;
  592. }
  593. }
  594. else if (cbdata->header_search == 2)
  595. {
  596. if (bytes[i] == '\r')
  597. cbdata->header_search++;
  598. else
  599. {
  600. cbdata->header[cbdata->header_index++] = '\r';
  601. cbdata->header[cbdata->header_index++] = '\n';
  602. cbdata->header[cbdata->header_index++] = bytes[i];
  603. cbdata->header_search = 0;
  604. }
  605. }
  606. else if (cbdata->header_search == 3)
  607. {
  608. if (bytes[i] == '\n')
  609. {
  610. cbdata->header[cbdata->header_index++] = '\r';
  611. cbdata->header[cbdata->header_index++] = '\n';
  612. cbdata->header[cbdata->header_index++] = '\0';
  613. cbdata->header_search = 4;
  614. cbdata->current_part->header = _mime_process_header(cbdata->header);
  615. hpairnode_dump_deep(cbdata->current_part->header);
  616. /* set id */
  617. id = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_ID);
  618. if (id != NULL)
  619. {
  620. strcpy(cbdata->current_part->id, id);
  621. if (!strcmp(id, cbdata->root_id))
  622. cbdata->message->root_part = cbdata->current_part;
  623. }
  624. location =
  625. hpairnode_get(cbdata->current_part->header,
  626. HEADER_CONTENT_LOCATION);
  627. if (location != NULL)
  628. {
  629. strcpy(cbdata->current_part->location, location);
  630. }
  631. type =
  632. hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_TYPE);
  633. if (type != NULL)
  634. {
  635. strcpy(cbdata->current_part->content_type, type);
  636. }
  637. i++;
  638. break;
  639. }
  640. else
  641. {
  642. cbdata->header[cbdata->header_index++] = '\r';
  643. cbdata->header[cbdata->header_index++] = '\n';
  644. cbdata->header[cbdata->header_index++] = '\r';
  645. cbdata->header[cbdata->header_index++] = bytes[i];
  646. cbdata->header_search = 0;
  647. }
  648. }
  649. /* TODO (#1#): Check for cbdata->header overflow */
  650. } /* for (i=0;i<size;i++) */
  651. } /* if (cbdata->header_search < 4) */
  652. if (i >= size - 1)
  653. return;
  654. /* Write remaining bytes into the file or buffer (if root) (buffer is
  655. disabled in this version) */
  656. if (cbdata->current_fd)
  657. fwrite(&(bytes[i]), 1, size - i, cbdata->current_fd);
  658. }
  659. /*
  660. The mime message parser
  661. */
  662. attachments_t *
  663. mime_message_parse(http_input_stream_t * in, const char *root_id,
  664. const char *boundary, const char *dest_dir)
  665. {
  666. MIME_parser_status status;
  667. MIME_callbacks callbacks;
  668. attachments_t *message;
  669. mime_callback_data_t *cbdata = (mime_callback_data_t *)
  670. malloc(sizeof(mime_callback_data_t));
  671. cbdata->part_id = 100;
  672. cbdata->buffer_capacity = 0;
  673. cbdata->current_fd = NULL;
  674. cbdata->current_part = NULL;
  675. cbdata->header_index = 0;
  676. cbdata->header_search = 0;
  677. strcpy(cbdata->root_id, root_id);
  678. strcpy(cbdata->root_dir, dest_dir);
  679. message = (attachments_t *) malloc(sizeof(attachments_t));
  680. cbdata->message = message;
  681. cbdata->message->parts = NULL;
  682. cbdata->message->root_part = NULL;
  683. callbacks.parse_begin_cb = _mime_parse_begin;
  684. callbacks.parse_end_cb = _mime_parse_end;
  685. callbacks.part_begin_cb = _mime_part_begin;
  686. callbacks.part_end_cb = _mime_part_end;
  687. callbacks.received_bytes_cb = _mime_received_bytes;
  688. status = MIME_parse(mime_streamreader_function,
  689. in, boundary, &callbacks, cbdata);
  690. if (status == MIME_PARSER_OK)
  691. {
  692. free(cbdata);
  693. return message;
  694. }
  695. else
  696. {
  697. log_error2("MIME parser error '%s'!",
  698. status ==
  699. MIME_PARSER_READ_ERROR ? "read error" : "Incomplete message");
  700. return NULL;
  701. }
  702. }
  703. attachments_t *
  704. mime_message_parse_from_file(FILE * in, const char *root_id,
  705. const char *boundary, const char *dest_dir)
  706. {
  707. MIME_parser_status status;
  708. MIME_callbacks callbacks;
  709. attachments_t *message;
  710. mime_callback_data_t *cbdata = (mime_callback_data_t *)
  711. malloc(sizeof(mime_callback_data_t));
  712. cbdata->part_id = 100;
  713. cbdata->buffer_capacity = 0;
  714. cbdata->current_fd = NULL;
  715. cbdata->current_part = NULL;
  716. cbdata->header_index = 0;
  717. cbdata->header_search = 0;
  718. strcpy(cbdata->root_id, root_id);
  719. strcpy(cbdata->root_dir, dest_dir);
  720. message = (attachments_t *) malloc(sizeof(attachments_t));
  721. cbdata->message = message;
  722. cbdata->message->parts = NULL;
  723. cbdata->message->root_part = NULL;
  724. callbacks.parse_begin_cb = _mime_parse_begin;
  725. callbacks.parse_end_cb = _mime_parse_end;
  726. callbacks.part_begin_cb = _mime_part_begin;
  727. callbacks.part_end_cb = _mime_part_end;
  728. callbacks.received_bytes_cb = _mime_received_bytes;
  729. status = MIME_parse(MIME_filereader_function,
  730. in, boundary, &callbacks, cbdata);
  731. if (status == MIME_PARSER_OK)
  732. {
  733. free(cbdata);
  734. return message;
  735. }
  736. else
  737. {
  738. /* TODO (#1#): Free objects */
  739. log_error2("MIME parser error '%s'!",
  740. status ==
  741. MIME_PARSER_READ_ERROR ? "general error" :
  742. "Incomplete message");
  743. return NULL;
  744. }
  745. }
  746. herror_t
  747. mime_get_attachments(content_type_t * ctype, http_input_stream_t * in,
  748. attachments_t ** dest)
  749. {
  750. /* MIME variables */
  751. attachments_t *mimeMessage;
  752. part_t *part, *tmp_part = NULL;
  753. char *boundary, *root_id;
  754. /* Check for MIME message */
  755. if (!(ctype && !strcmp(ctype->type, "multipart/related")))
  756. return herror_new("mime_get_attachments", MIME_ERROR_NOT_MIME_MESSAGE,
  757. "Not a MIME message '%s'", ctype->type);
  758. boundary = hpairnode_get(ctype->params, "boundary");
  759. root_id = hpairnode_get(ctype->params, "start");
  760. if (boundary == NULL)
  761. {
  762. /* TODO (#1#): Handle Error in http form */
  763. log_error1("'boundary' not set for multipart/related");
  764. return herror_new("mime_get_attachments", MIME_ERROR_NO_BOUNDARY_PARAM,
  765. "'boundary' not set for multipart/related");
  766. }
  767. if (root_id == NULL)
  768. {
  769. /* TODO (#1#): Handle Error in http form */
  770. log_error1("'start' not set for multipart/related");
  771. return herror_new("mime_get_attachments", MIME_ERROR_NO_START_PARAM,
  772. "'start' not set for multipart/related");
  773. }
  774. mimeMessage =
  775. mime_message_parse(in, root_id, boundary, ".");
  776. if (mimeMessage == NULL)
  777. {
  778. /* TODO (#1#): Handle Error in http form */
  779. log_error1("MIME Parse Error");
  780. return herror_new("mime_get_attachments", MIME_ERROR_PARSE_ERROR,
  781. "MIME Parse Error");
  782. }
  783. /* Find root */
  784. if (!mimeMessage->root_part)
  785. {
  786. attachments_free(mimeMessage);
  787. return herror_new("mime_get_attachments", MIME_ERROR_NO_ROOT_PART,
  788. "No root part found!");
  789. }
  790. /* delete root_part from list */
  791. part = mimeMessage->parts;
  792. while (part)
  793. {
  794. if (part == mimeMessage->root_part)
  795. {
  796. if (tmp_part)
  797. tmp_part->next = part->next;
  798. else
  799. mimeMessage->parts = part->next;
  800. break;
  801. }
  802. tmp_part = part;
  803. part = part->next;
  804. }
  805. *dest = mimeMessage;
  806. return H_OK;
  807. }