wn_module_upload.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * File : wn_module_upload.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This software is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http://www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this software under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively for commercial application, you can contact us
  17. * by email <business@rt-thread.com> for commercial license.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2012-06-25 Bernard the first version
  22. */
  23. #include <webnet.h>
  24. #include <wn_module.h>
  25. #include <wn_utils.h>
  26. #ifdef RT_USING_DFS
  27. #if RT_VER_NUM >= 0x40100
  28. #include <fcntl.h> /* fix O_WRONLY */
  29. #else
  30. #include <dfs_posix.h>
  31. #endif /*RT_VER_NUM >= 0x40100*/
  32. #endif
  33. #if defined(WEBNET_USING_UPLOAD)
  34. #define MULTIPART_FORM_DATA_STRING "multipart/form-data"
  35. #define BOUNDARY_STRING "boundary="
  36. #define CONTENT_DISPOSITION_STRING "Content-Disposition:"
  37. #define CONTENT_TYPE_STRING "Content-Type:"
  38. #define CONTENT_RANGE_STRING "Content-Range:"
  39. #define FORM_DATA_STRING "form-data"
  40. #define FILENAME_STRING "filename=\""
  41. #define FIELDNAME_STRING "name=\""
  42. #define FIRST_BOUNDARY 2, upload_session->boundary, "\r\n"
  43. #define NORMAL_BOUNDARY 3, "\r\n", upload_session->boundary, "\r\n"
  44. #define COMMON_BOUNDARY 2, "\r\n", upload_session->boundary
  45. #define LAST_BOUNDARY 3, "\r\n", upload_session->boundary, "--\r\n"
  46. #define FIRST_BOUNDARY_SIZE (strlen(upload_session->boundary) + 2)
  47. #define NORMAL_BOUNDARY_SIZE (strlen(upload_session->boundary) + 4)
  48. #define COMMON_BOUNDARY_SIZE (strlen(upload_session->boundary) + 2)
  49. #define LAST_BOUNDARY_SIZE (strlen(upload_session->boundary) + 6)
  50. struct webnet_upload_name_entry
  51. {
  52. char *name;
  53. char *value;
  54. };
  55. static const struct webnet_module_upload_entry **_upload_entries = RT_NULL;
  56. static rt_uint16_t _upload_entries_count = 0;
  57. struct webnet_module_upload_session
  58. {
  59. char* boundary;
  60. char* filename;
  61. char* content_type;
  62. struct webnet_upload_name_entry* name_entries;
  63. rt_uint16_t name_entries_count;
  64. rt_uint16_t file_opened;
  65. /* upload entry */
  66. const struct webnet_module_upload_entry* entry;
  67. /* user data */
  68. rt_uint32_t user_data;
  69. };
  70. static int str_begin_with_strs(const char* str, int num, ...)
  71. {
  72. char *match;
  73. va_list args;
  74. int index, result;
  75. result = 1;
  76. va_start(args,num);
  77. for (index = 0; index < num; index ++)
  78. {
  79. match = va_arg(args, char*);
  80. if (strncasecmp(str, match, strlen(match)) != 0)
  81. {
  82. result = 0;
  83. break;
  84. }
  85. str += strlen(match);
  86. }
  87. va_end(args);
  88. return result;
  89. }
  90. /* Search string on binary data */
  91. static char *memstr(const char *haystack, size_t length, const char *needle)
  92. {
  93. size_t nl=strlen(needle);
  94. size_t hl=length;
  95. size_t i;
  96. if (!nl) goto __found;
  97. if (nl > length) return 0;
  98. for (i=hl-nl+1; i; --i)
  99. {
  100. if (*haystack==*needle && !memcmp(haystack,needle,nl))
  101. __found:
  102. return (char*)haystack;
  103. ++haystack;
  104. }
  105. return 0;
  106. }
  107. /* Search string array on binary data */
  108. static char* memstrs(const char* str, size_t length, int num, ...)
  109. {
  110. char *substr;
  111. char *index_str, *ptr;
  112. va_list args;
  113. int index;
  114. int total_size;
  115. /* get the match total string length */
  116. va_start(args, num);
  117. total_size = 0;
  118. for (index = 0; index < num; index ++)
  119. {
  120. index_str = va_arg(args, char*);
  121. total_size += strlen(index_str);
  122. }
  123. va_end(args);
  124. substr = wn_malloc(total_size + 1);
  125. ptr = substr;
  126. va_start(args, num);
  127. for (index = 0; index < num; index++)
  128. {
  129. index_str = va_arg(args, char*);
  130. rt_memcpy(ptr, index_str, strlen(index_str));
  131. ptr += strlen(index_str);
  132. }
  133. substr[total_size] = '\0';
  134. va_end(args);
  135. /* find in binary data */
  136. ptr = memstr(str, length, substr);
  137. wn_free(substr);
  138. return ptr;
  139. }
  140. #if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/)
  141. static void* memrchr(const void *s, int c, size_t n)
  142. {
  143. register const char* t=s;
  144. register const char* last=0;
  145. int i;
  146. t = t + n;
  147. for (i=n; i; --i)
  148. {
  149. if (*t==c)
  150. {
  151. last=t;
  152. break;
  153. }
  154. t--;
  155. }
  156. return (void*)last; /* man, what an utterly b0rken prototype */
  157. }
  158. #endif
  159. static char* _webnet_module_upload_parse_header(struct webnet_session* session, char* buffer, rt_size_t length)
  160. {
  161. char *ptr, *ch, *end_ptr;
  162. struct webnet_module_upload_session *upload_session;
  163. char *name = RT_NULL, *filename = RT_NULL, *content_type = RT_NULL;
  164. /* get upload session */
  165. upload_session = (struct webnet_module_upload_session *)session->user_data;
  166. ptr = buffer;
  167. end_ptr = buffer + length;
  168. /* begin with last boundary */
  169. if (str_begin_with_strs(ptr, LAST_BOUNDARY))
  170. {
  171. ptr += LAST_BOUNDARY_SIZE;
  172. return ptr;
  173. }
  174. /* begin with normal boundary */
  175. if (str_begin_with_strs(ptr, NORMAL_BOUNDARY))
  176. ptr += NORMAL_BOUNDARY_SIZE;
  177. else if (str_begin_with_strs(ptr, FIRST_BOUNDARY))
  178. ptr += FIRST_BOUNDARY_SIZE;
  179. if (ptr == buffer) return RT_NULL; /* not begin with boundary */
  180. if (upload_session->filename != RT_NULL &&
  181. upload_session->content_type != RT_NULL)
  182. {
  183. /* end of file name section */
  184. wn_free(upload_session->filename);
  185. wn_free(upload_session->content_type);
  186. upload_session->filename = RT_NULL;
  187. upload_session->content_type = RT_NULL;
  188. }
  189. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < length)
  190. {
  191. /* handle Content-Disposition: */
  192. if (str_begin_with(ptr, CONTENT_DISPOSITION_STRING))
  193. {
  194. ptr += sizeof(CONTENT_DISPOSITION_STRING) - 1;
  195. while (*ptr == ' ') ptr ++;
  196. /* form-data */
  197. if (str_begin_with(ptr, FORM_DATA_STRING))
  198. {
  199. ptr += sizeof(FORM_DATA_STRING) - 1;
  200. while (*ptr == ' ' || *ptr == ';') ptr ++;
  201. /* get name="str" */
  202. if (str_begin_with(ptr, FIELDNAME_STRING))
  203. {
  204. ptr += sizeof(FIELDNAME_STRING) - 1;
  205. name = ptr;
  206. ch = memchr(ptr, '"', buffer - ptr);
  207. if (ch != RT_NULL)
  208. {
  209. *ch = '\0';
  210. ch ++;
  211. while (*ch == ' ' || *ch ==';') ch ++;
  212. ptr = ch;
  213. }
  214. }
  215. /* get filename="str" */
  216. if (str_begin_with(ptr, FILENAME_STRING))
  217. {
  218. ptr += sizeof(FILENAME_STRING) - 1;
  219. filename = ptr;
  220. ch = memchr(ptr, '"', buffer - ptr);
  221. if (ch != RT_NULL)
  222. {
  223. *ch = '\0';
  224. ch ++;
  225. ptr = ch;
  226. }
  227. }
  228. }
  229. }
  230. /* handle Content-Type */
  231. else if (str_begin_with(ptr, CONTENT_TYPE_STRING))
  232. {
  233. ptr += sizeof(CONTENT_TYPE_STRING) - 1;
  234. while (*ptr == ' ') ptr ++;
  235. content_type = ptr;
  236. }
  237. /* handle Content-Range: */
  238. else if (str_begin_with(ptr, CONTENT_RANGE_STRING))
  239. {
  240. ptr += sizeof(CONTENT_RANGE_STRING) - 1;
  241. while (*ptr == ' ') ptr ++;
  242. }
  243. /* whether end of header */
  244. else if (str_begin_with(ptr, "\r\n"))
  245. {
  246. ptr += 2;
  247. if (upload_session->filename != RT_NULL)
  248. {
  249. wn_free(upload_session->filename);
  250. upload_session->filename = RT_NULL;
  251. }
  252. if (upload_session->content_type != RT_NULL)
  253. {
  254. wn_free(upload_session->content_type);
  255. upload_session->content_type = RT_NULL;
  256. }
  257. if (filename != RT_NULL)
  258. {
  259. upload_session->filename = wn_strdup(filename);
  260. }
  261. if (content_type != RT_NULL)
  262. {
  263. upload_session->content_type = wn_strdup(content_type);
  264. }
  265. if (name != RT_NULL)
  266. {
  267. /* add a name entry in the upload session */
  268. if (upload_session->name_entries == RT_NULL)
  269. {
  270. upload_session->name_entries_count = 1;
  271. upload_session->name_entries = (struct webnet_upload_name_entry*)
  272. wn_malloc(sizeof(struct webnet_upload_name_entry));
  273. }
  274. else
  275. {
  276. upload_session->name_entries_count += 1;
  277. upload_session->name_entries = (struct webnet_upload_name_entry*)
  278. wn_realloc(upload_session->name_entries,
  279. sizeof(struct webnet_upload_name_entry) * upload_session->name_entries_count);
  280. }
  281. upload_session->name_entries[upload_session->name_entries_count - 1].name = wn_strdup(name);
  282. upload_session->name_entries[upload_session->name_entries_count - 1].value = RT_NULL;
  283. }
  284. return ptr;
  285. }
  286. /* skip this request header line */
  287. ch = memstr(ptr, end_ptr - ptr, "\r\n");
  288. *ch = '\0';
  289. ch ++;
  290. *ch = '\0';
  291. ch ++;
  292. ptr = ch;
  293. }
  294. return ptr;
  295. }
  296. static char* _next_possible_boundary(struct webnet_session* session, char* buffer, rt_size_t length)
  297. {
  298. char *ptr;
  299. struct webnet_module_upload_session *upload_session;
  300. /* get upload session */
  301. upload_session = (struct webnet_module_upload_session *)session->user_data;
  302. /* try the beginning */
  303. if (str_begin_with_strs(buffer, COMMON_BOUNDARY)) return buffer;
  304. /* search in all the string */
  305. ptr = memstrs(buffer, length, COMMON_BOUNDARY);
  306. if (ptr != RT_NULL) return ptr;
  307. /* search from the end of string */
  308. ptr = memrchr(buffer, '\r', length);
  309. if (ptr == RT_NULL ) return RT_NULL;
  310. if (ptr - buffer == length - 1) return ptr; /* only \r */
  311. if (*(ptr + 1) == '\n' && ptr - buffer == length - 2) return RT_NULL; /* only \r\n */
  312. if (memcmp(ptr + 2, upload_session->boundary, length - (ptr - buffer + 2)) == 0) return ptr;
  313. return RT_NULL;
  314. }
  315. static void _handle_section(struct webnet_session* session, char* buffer, rt_size_t length)
  316. {
  317. char *ptr;
  318. struct webnet_module_upload_session *upload_session;
  319. #define name_entry \
  320. (upload_session->name_entries[upload_session->name_entries_count - 1])
  321. /* get upload session */
  322. upload_session = (struct webnet_module_upload_session *)session->user_data;
  323. if (upload_session->filename != RT_NULL &&
  324. upload_session->content_type != RT_NULL&&
  325. length != 0)
  326. {
  327. upload_session->entry->upload_write(session, buffer, length);
  328. }
  329. else
  330. {
  331. /* get name value */
  332. ptr = memstr(buffer, length, "\r\n");
  333. if (ptr != RT_NULL)
  334. {
  335. int value_size = ptr - buffer + 1;
  336. name_entry.value = wn_malloc(value_size);
  337. rt_memcpy(name_entry.value, buffer, value_size - 1);
  338. name_entry.value[value_size - 1] = '\0';
  339. }
  340. }
  341. }
  342. static void _webnet_module_upload_handle(struct webnet_session* session, int event)
  343. {
  344. int length;
  345. char *ptr, *end_ptr;
  346. char *upload_buffer;
  347. rt_uint32_t chunk_size;
  348. struct webnet_module_upload_session *upload_session;
  349. if (event != WEBNET_EVENT_READ) return;
  350. /* get upload session */
  351. upload_session = (struct webnet_module_upload_session *)session->user_data;
  352. upload_buffer = (char*)session->buffer;
  353. /* read stream */
  354. if (memstrs((const char *)session->buffer, session->buffer_offset, LAST_BOUNDARY))
  355. {
  356. length = session->buffer_offset;
  357. }
  358. else
  359. {
  360. if (session->buffer_offset != 0)
  361. {
  362. length = webnet_session_read(session,
  363. (char*)&session->buffer[session->buffer_offset],
  364. sizeof(session->buffer) - session->buffer_offset - 1);
  365. if (length > 0)
  366. {
  367. length += session->buffer_offset;
  368. }
  369. else
  370. {
  371. length = session->buffer_offset;
  372. }
  373. }
  374. else
  375. {
  376. length = webnet_session_read(session, (char *)session->buffer, sizeof(session->buffer) - 1);
  377. }
  378. }
  379. /* connection break out */
  380. if (length <= 0)
  381. {
  382. /* read stream failed (connection break out), close this session */
  383. session->session_phase = WEB_PHASE_CLOSE;
  384. return;
  385. }
  386. end_ptr = (char*)(session->buffer + length);
  387. session->buffer[length] = '\0';
  388. while (upload_buffer < end_ptr)
  389. {
  390. if (str_begin_with_strs(upload_buffer, LAST_BOUNDARY))
  391. {
  392. /* upload done */
  393. upload_session->entry->upload_done(session);
  394. session->session_phase = WEB_PHASE_CLOSE;
  395. return;
  396. }
  397. /* read more data */
  398. if (end_ptr - upload_buffer < sizeof(session->buffer)/3 &&
  399. memstrs(upload_buffer, end_ptr - upload_buffer, LAST_BOUNDARY) == RT_NULL)
  400. {
  401. /* read more data */
  402. rt_memmove(session->buffer, upload_buffer, end_ptr - upload_buffer);
  403. session->buffer_offset = end_ptr - upload_buffer;
  404. return ;
  405. }
  406. ptr = _webnet_module_upload_parse_header(session, upload_buffer,
  407. (rt_uint32_t)end_ptr - (rt_uint32_t)upload_buffer);
  408. if (ptr == RT_NULL)
  409. {
  410. /* not begin with a boundary */
  411. ptr = _next_possible_boundary(session, upload_buffer,
  412. (rt_uint32_t)end_ptr - (rt_uint32_t)upload_buffer);
  413. if (ptr == RT_NULL)
  414. {
  415. /* all are the data section */
  416. _handle_section(session, upload_buffer, (rt_uint32_t)end_ptr - (rt_uint32_t)upload_buffer);
  417. upload_buffer = end_ptr;
  418. }
  419. else
  420. {
  421. chunk_size = (rt_uint32_t)ptr - (rt_uint32_t)upload_buffer;
  422. _handle_section(session, upload_buffer, chunk_size);
  423. upload_buffer += chunk_size;
  424. }
  425. }
  426. else
  427. {
  428. if (upload_session->filename != RT_NULL &&
  429. upload_session->content_type != RT_NULL)
  430. {
  431. if (upload_session->file_opened == 0)
  432. {
  433. /* open file */
  434. upload_session->user_data = upload_session->entry->upload_open(session);
  435. upload_session->file_opened = 1;
  436. }
  437. }
  438. else
  439. {
  440. if (upload_session->file_opened == 1)
  441. {
  442. /* close file */
  443. upload_session->entry->upload_close(session);
  444. upload_session->user_data = 0;
  445. upload_session->file_opened = 0;
  446. }
  447. }
  448. upload_buffer = ptr;
  449. ptr = _next_possible_boundary(session, upload_buffer,
  450. (rt_uint32_t)end_ptr - (rt_uint32_t)upload_buffer);
  451. if (ptr == RT_NULL)
  452. {
  453. /* all are the data section */
  454. _handle_section(session, upload_buffer, (rt_uint32_t)end_ptr - (rt_uint32_t)upload_buffer);
  455. upload_buffer = end_ptr;
  456. }
  457. else
  458. {
  459. chunk_size = (rt_uint32_t)ptr - (rt_uint32_t)upload_buffer;
  460. _handle_section(session, upload_buffer, chunk_size);
  461. upload_buffer += chunk_size;
  462. }
  463. }
  464. }
  465. session->buffer_offset = 0;
  466. }
  467. static void _webnet_module_upload_close(struct webnet_session* session)
  468. {
  469. struct webnet_module_upload_session *upload_session;
  470. /* get upload session */
  471. upload_session = (struct webnet_module_upload_session *)session->user_data;
  472. if (upload_session == RT_NULL) return;
  473. /* close file */
  474. if (upload_session->file_opened == 1)
  475. {
  476. upload_session->entry->upload_close(session);
  477. upload_session->file_opened = 0;
  478. }
  479. /* free session */
  480. if (upload_session->filename != RT_NULL)
  481. wn_free(upload_session->filename);
  482. if (upload_session->content_type != RT_NULL)
  483. wn_free(upload_session->content_type);
  484. if (upload_session->boundary != RT_NULL)
  485. wn_free(upload_session->boundary);
  486. if (upload_session->entry != RT_NULL)
  487. {
  488. rt_uint32_t index;
  489. for (index = 0; index < upload_session->name_entries_count; index ++)
  490. {
  491. if (upload_session->name_entries[index].value != RT_NULL)
  492. wn_free(upload_session->name_entries[index].value);
  493. if (upload_session->name_entries[index].name != RT_NULL)
  494. wn_free(upload_session->name_entries[index].name);
  495. }
  496. wn_free(upload_session->name_entries);
  497. upload_session->name_entries = RT_NULL;
  498. }
  499. wn_free(upload_session);
  500. /* remove private data */
  501. session->user_data = 0;
  502. session->session_ops = RT_NULL;
  503. session->session_phase = WEB_PHASE_CLOSE;
  504. }
  505. static const struct webnet_session_ops _upload_ops =
  506. {
  507. _webnet_module_upload_handle,
  508. _webnet_module_upload_close
  509. };
  510. int webnet_module_upload_open(struct webnet_session* session)
  511. {
  512. char* boundary;
  513. rt_uint32_t index, length;
  514. const struct webnet_module_upload_entry *entry = RT_NULL;
  515. struct webnet_module_upload_session *upload_session;
  516. if (session->request->method != WEBNET_POST)
  517. return WEBNET_MODULE_CONTINUE;
  518. /* get upload entry */
  519. for (index = 0; index < _upload_entries_count; index ++)
  520. {
  521. if (str_begin_with(session->request->path, _upload_entries[index]->url))
  522. {
  523. length = rt_strlen(_upload_entries[index]->url);
  524. if (session->request->path[length] == '\0' ||
  525. session->request->path[length] == '?')
  526. {
  527. /* found entry */
  528. entry = _upload_entries[index];
  529. break;
  530. }
  531. }
  532. }
  533. if (entry == RT_NULL) /* no this entry */
  534. return WEBNET_MODULE_CONTINUE;
  535. /* create a uploading session */
  536. upload_session = (struct webnet_module_upload_session*) wn_malloc (
  537. sizeof (struct webnet_module_upload_session));
  538. if (upload_session == RT_NULL) return 0; /* no memory */
  539. /* get boundary */
  540. boundary = strstr(session->request->content_type, BOUNDARY_STRING);
  541. if (boundary != RT_NULL)
  542. {
  543. int boundary_size;
  544. /* make boundary */
  545. boundary_size = strlen(boundary + sizeof(BOUNDARY_STRING) - 1) + 3;
  546. upload_session->boundary = wn_malloc(boundary_size);
  547. rt_sprintf(upload_session->boundary, "--%s", boundary + sizeof(BOUNDARY_STRING) - 1);
  548. }
  549. upload_session->filename = RT_NULL;
  550. upload_session->content_type = RT_NULL;
  551. upload_session->name_entries = RT_NULL;
  552. upload_session->name_entries_count = 0;
  553. upload_session->file_opened = 0;
  554. upload_session->entry = entry;
  555. upload_session->user_data = 0;
  556. /* add this upload session into webnet session */
  557. session->user_data = (rt_uint32_t) upload_session;
  558. /* set webnet session operations */
  559. session->session_ops = &_upload_ops;
  560. session->session_ops->session_handle(session, WEBNET_EVENT_READ);
  561. return WEBNET_MODULE_FINISHED;
  562. }
  563. int webnet_module_upload(struct webnet_session* session, int event)
  564. {
  565. if (event == WEBNET_EVENT_URI_PHYSICAL)
  566. {
  567. return webnet_module_upload_open(session);
  568. }
  569. return WEBNET_MODULE_CONTINUE;
  570. }
  571. /* webnet upload module APIs */
  572. void webnet_upload_add(const struct webnet_module_upload_entry* entry)
  573. {
  574. if (_upload_entries == RT_NULL)
  575. {
  576. _upload_entries_count = 1;
  577. /* first entries, malloc upload entry */
  578. _upload_entries = (const struct webnet_module_upload_entry**)
  579. wn_malloc (sizeof(struct webnet_module_upload_entry));
  580. }
  581. else
  582. {
  583. _upload_entries_count += 1;
  584. _upload_entries = (const struct webnet_module_upload_entry**) wn_realloc (_upload_entries,
  585. sizeof(void*) * _upload_entries_count);
  586. }
  587. RT_ASSERT(_upload_entries != RT_NULL);
  588. _upload_entries[_upload_entries_count - 1] = entry;
  589. }
  590. RTM_EXPORT(webnet_upload_add);
  591. const char* webnet_upload_get_filename(struct webnet_session* session)
  592. {
  593. struct webnet_module_upload_session *upload_session;
  594. /* get upload session */
  595. upload_session = (struct webnet_module_upload_session *)session->user_data;
  596. if (upload_session == RT_NULL) return RT_NULL;
  597. return upload_session->filename;
  598. }
  599. RTM_EXPORT(webnet_upload_get_filename);
  600. const char* webnet_upload_get_content_type(struct webnet_session* session)
  601. {
  602. struct webnet_module_upload_session *upload_session;
  603. /* get upload session */
  604. upload_session = (struct webnet_module_upload_session *)session->user_data;
  605. if (upload_session == RT_NULL) return RT_NULL;
  606. return upload_session->content_type;
  607. }
  608. RTM_EXPORT(webnet_upload_get_content_type);
  609. const char* webnet_upload_get_nameentry(struct webnet_session* session, const char* name)
  610. {
  611. rt_uint32_t index;
  612. struct webnet_module_upload_session *upload_session;
  613. /* get upload session */
  614. upload_session = (struct webnet_module_upload_session *)session->user_data;
  615. if (upload_session == RT_NULL) return RT_NULL;
  616. for (index = 0; index < upload_session->name_entries_count; index ++)
  617. {
  618. if (strcasecmp(upload_session->name_entries[index].name, name) == 0)
  619. {
  620. return upload_session->name_entries[index].value;
  621. }
  622. }
  623. return RT_NULL;
  624. }
  625. RTM_EXPORT(webnet_upload_get_nameentry);
  626. const void* webnet_upload_get_userdata(struct webnet_session* session)
  627. {
  628. struct webnet_module_upload_session *upload_session;
  629. /* get upload session */
  630. upload_session = (struct webnet_module_upload_session *)session->user_data;
  631. if (upload_session == RT_NULL) return RT_NULL;
  632. return (const void*) upload_session->user_data;
  633. }
  634. RTM_EXPORT(webnet_upload_get_userdata);
  635. /* ---- upload file interface ---- */
  636. int webnet_upload_file_open(struct webnet_session* session)
  637. {
  638. struct webnet_module_upload_session* upload_session;
  639. /* get upload session */
  640. upload_session = (struct webnet_module_upload_session *)session->user_data;
  641. if (upload_session->filename != RT_NULL)
  642. {
  643. /* open file */
  644. upload_session->user_data = open(upload_session->filename, O_WRONLY, 0);
  645. return upload_session->user_data;
  646. }
  647. return -1;
  648. }
  649. int webnet_upload_file_close(struct webnet_session* session)
  650. {
  651. int fd;
  652. struct webnet_module_upload_session* upload_session;
  653. /* get upload session */
  654. upload_session = (struct webnet_module_upload_session *)session->user_data;
  655. fd = upload_session->user_data;
  656. if (fd >= 0)
  657. {
  658. return close(fd);
  659. }
  660. return -1;
  661. }
  662. int webnet_upload_file_write(struct webnet_session* session, const void* data, rt_size_t length)
  663. {
  664. int fd;
  665. struct webnet_module_upload_session* upload_session;
  666. /* get upload session */
  667. upload_session = (struct webnet_module_upload_session *)session->user_data;
  668. fd = upload_session->user_data;
  669. if (fd >= 0)
  670. {
  671. return write(fd, data, length);
  672. }
  673. return 0;
  674. }
  675. #endif /* WEBNET_USING_UPLOAD */