handle_form.inl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* Copyright (c) 2016 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. static int
  22. url_encoded_field_found(const struct mg_connection *conn,
  23. const char *key,
  24. size_t key_len,
  25. const char *filename,
  26. size_t filename_len,
  27. char *path,
  28. size_t path_len,
  29. struct mg_form_data_handler *fdh)
  30. {
  31. char key_dec[1024];
  32. char filename_dec[1024];
  33. int key_dec_len;
  34. int filename_dec_len;
  35. int ret;
  36. key_dec_len =
  37. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  38. if (((size_t)key_dec_len >= (size_t)sizeof(key_dec)) || (key_dec_len < 0)) {
  39. return FORM_FIELD_STORAGE_SKIP;
  40. }
  41. if (filename) {
  42. filename_dec_len = mg_url_decode(filename,
  43. (int)filename_len,
  44. filename_dec,
  45. (int)sizeof(filename_dec),
  46. 1);
  47. if (((size_t)filename_dec_len >= (size_t)sizeof(filename_dec))
  48. || (filename_dec_len < 0)) {
  49. /* Log error message and skip this field. */
  50. mg_cry(conn, "%s: Cannot decode filename", __func__);
  51. return FORM_FIELD_STORAGE_SKIP;
  52. }
  53. } else {
  54. filename_dec[0] = 0;
  55. }
  56. ret =
  57. fdh->field_found(key_dec, filename_dec, path, path_len, fdh->user_data);
  58. if ((ret & 0xF) == FORM_FIELD_STORAGE_GET) {
  59. if (fdh->field_get == NULL) {
  60. mg_cry(conn, "%s: Function \"Get\" not available", __func__);
  61. return FORM_FIELD_STORAGE_SKIP;
  62. }
  63. }
  64. if ((ret & 0xF) == FORM_FIELD_STORAGE_STORE) {
  65. if (fdh->field_store == NULL) {
  66. mg_cry(conn, "%s: Function \"Store\" not available", __func__);
  67. return FORM_FIELD_STORAGE_SKIP;
  68. }
  69. }
  70. return ret;
  71. }
  72. static int
  73. url_encoded_field_get(const struct mg_connection *conn,
  74. const char *key,
  75. size_t key_len,
  76. const char *value,
  77. size_t value_len,
  78. struct mg_form_data_handler *fdh)
  79. {
  80. char key_dec[1024];
  81. char *value_dec = mg_malloc(value_len + 1);
  82. int value_dec_len, ret;
  83. if (!value_dec) {
  84. /* Log error message and stop parsing the form data. */
  85. mg_cry(conn,
  86. "%s: Not enough memory (required: %lu)",
  87. __func__,
  88. (unsigned long)(value_len + 1));
  89. return FORM_FIELD_STORAGE_ABORT;
  90. }
  91. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  92. value_dec_len =
  93. mg_url_decode(value, (int)value_len, value_dec, (int)value_len + 1, 1);
  94. ret = fdh->field_get(key_dec,
  95. value_dec,
  96. (size_t)value_dec_len,
  97. fdh->user_data);
  98. mg_free(value_dec);
  99. return ret;
  100. }
  101. static int
  102. unencoded_field_get(const struct mg_connection *conn,
  103. const char *key,
  104. size_t key_len,
  105. const char *value,
  106. size_t value_len,
  107. struct mg_form_data_handler *fdh)
  108. {
  109. char key_dec[1024];
  110. (void)conn;
  111. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  112. return fdh->field_get(key_dec, value, value_len, fdh->user_data);
  113. }
  114. static int
  115. field_stored(const struct mg_connection *conn,
  116. const char *path,
  117. long long file_size,
  118. struct mg_form_data_handler *fdh)
  119. {
  120. /* Equivalent to "upload" callback of "mg_upload". */
  121. (void)conn; /* we do not need mg_cry here, so conn is currently unused */
  122. return fdh->field_store(path, file_size, fdh->user_data);
  123. }
  124. static const char *
  125. search_boundary(const char *buf,
  126. size_t buf_len,
  127. const char *boundary,
  128. size_t boundary_len)
  129. {
  130. /* We must do a binary search here, not a string search, since the buffer
  131. * may contain '\x00' bytes, if binary data is transferred. */
  132. int clen = (int)buf_len - (int)boundary_len - 4;
  133. int i;
  134. for (i = 0; i <= clen; i++) {
  135. if (!memcmp(buf + i, "\r\n--", 4)) {
  136. if (!memcmp(buf + i + 4, boundary, boundary_len)) {
  137. return buf + i;
  138. }
  139. }
  140. }
  141. return NULL;
  142. }
  143. int
  144. mg_handle_form_request(struct mg_connection *conn,
  145. struct mg_form_data_handler *fdh)
  146. {
  147. const char *content_type;
  148. char path[512];
  149. char buf[1024];
  150. int field_storage;
  151. int buf_fill = 0;
  152. int r;
  153. int field_count = 0;
  154. struct mg_file fstore = STRUCT_FILE_INITIALIZER;
  155. int64_t file_size = 0; /* init here, to a avoid a false positive
  156. "uninitialized variable used" warning */
  157. int has_body_data =
  158. (conn->request_info.content_length > 0) || (conn->is_chunked);
  159. /* There are three ways to encode data from a HTML form:
  160. * 1) method: GET (default)
  161. * The form data is in the HTTP query string.
  162. * 2) method: POST, enctype: "application/x-www-form-urlencoded"
  163. * The form data is in the request body.
  164. * The body is url encoded (the default encoding for POST).
  165. * 3) method: POST, enctype: "multipart/form-data".
  166. * The form data is in the request body of a multipart message.
  167. * This is the typical way to handle file upload from a form.
  168. */
  169. if (!has_body_data) {
  170. const char *data;
  171. if (strcmp(conn->request_info.request_method, "GET")) {
  172. /* No body data, but not a GET request.
  173. * This is not a valid form request. */
  174. return -1;
  175. }
  176. /* GET request: form data is in the query string. */
  177. /* The entire data has already been loaded, so there is no nead to
  178. * call mg_read. We just need to split the query string into key-value
  179. * pairs. */
  180. data = conn->request_info.query_string;
  181. if (!data) {
  182. /* No query string. */
  183. return -1;
  184. }
  185. /* Split data in a=1&b=xy&c=3&c=4 ... */
  186. while (*data) {
  187. const char *val = strchr(data, '=');
  188. const char *next;
  189. ptrdiff_t keylen, vallen;
  190. if (!val) {
  191. break;
  192. }
  193. keylen = val - data;
  194. /* In every "field_found" callback we ask what to do with the
  195. * data ("field_storage"). This could be:
  196. * FORM_FIELD_STORAGE_SKIP (0) ... ignore the value of this field
  197. * FORM_FIELD_STORAGE_GET (1) ... read the data and call the get
  198. * callback function
  199. * FORM_FIELD_STORAGE_STORE (2) ... store the data in a file
  200. * FORM_FIELD_STORAGE_READ (3) ... let the user read the data
  201. * (for parsing long data on the fly)
  202. * (currently not implemented)
  203. * FORM_FIELD_STORAGE_ABORT (flag) ... stop parsing
  204. */
  205. memset(path, 0, sizeof(path));
  206. field_count++;
  207. field_storage = url_encoded_field_found(conn,
  208. data,
  209. (size_t)keylen,
  210. NULL,
  211. 0,
  212. path,
  213. sizeof(path) - 1,
  214. fdh);
  215. val++;
  216. next = strchr(val, '&');
  217. if (next) {
  218. vallen = next - val;
  219. next++;
  220. } else {
  221. vallen = (ptrdiff_t)strlen(val);
  222. next = val + vallen;
  223. }
  224. if (field_storage == FORM_FIELD_STORAGE_GET) {
  225. /* Call callback */
  226. url_encoded_field_get(
  227. conn, data, (size_t)keylen, val, (size_t)vallen, fdh);
  228. }
  229. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  230. /* Store the content to a file */
  231. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  232. fstore.access.fp = NULL;
  233. }
  234. file_size = 0;
  235. if (fstore.access.fp != NULL) {
  236. size_t n = (size_t)
  237. fwrite(val, 1, (size_t)vallen, fstore.access.fp);
  238. if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
  239. mg_cry(conn,
  240. "%s: Cannot write file %s",
  241. __func__,
  242. path);
  243. (void)mg_fclose(&fstore.access);
  244. remove_bad_file(conn, path);
  245. }
  246. file_size += (int64_t)n;
  247. if (fstore.access.fp) {
  248. r = mg_fclose(&fstore.access);
  249. if (r == 0) {
  250. /* stored successfully */
  251. field_stored(conn, path, file_size, fdh);
  252. } else {
  253. mg_cry(conn,
  254. "%s: Error saving file %s",
  255. __func__,
  256. path);
  257. remove_bad_file(conn, path);
  258. }
  259. fstore.access.fp = NULL;
  260. }
  261. } else {
  262. mg_cry(conn, "%s: Cannot create file %s", __func__, path);
  263. }
  264. }
  265. /* if (field_storage == FORM_FIELD_STORAGE_READ) { */
  266. /* The idea of "field_storage=read" is to let the API user read
  267. * data chunk by chunk and to some data processing on the fly.
  268. * This should avoid the need to store data in the server:
  269. * It should neither be stored in memory, like
  270. * "field_storage=get" does, nor in a file like
  271. * "field_storage=store".
  272. * However, for a "GET" request this does not make any much
  273. * sense, since the data is already stored in memory, as it is
  274. * part of the query string.
  275. */
  276. /* } */
  277. if ((field_storage & FORM_FIELD_STORAGE_ABORT)
  278. == FORM_FIELD_STORAGE_ABORT) {
  279. /* Stop parsing the request */
  280. break;
  281. }
  282. /* Proceed to next entry */
  283. data = next;
  284. }
  285. return field_count;
  286. }
  287. content_type = mg_get_header(conn, "Content-Type");
  288. if (!content_type
  289. || !mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")
  290. || !mg_strcasecmp(content_type, "APPLICATION/WWW-FORM-URLENCODED")) {
  291. /* The form data is in the request body data, encoded in key/value
  292. * pairs. */
  293. int all_data_read = 0;
  294. /* Read body data and split it in keys and values.
  295. * The encoding is like in the "GET" case above: a=1&b&c=3&c=4.
  296. * Here we use "POST", and read the data from the request body.
  297. * The data read on the fly, so it is not required to buffer the
  298. * entire request in memory before processing it. */
  299. for (;;) {
  300. const char *val;
  301. const char *next;
  302. ptrdiff_t keylen, vallen;
  303. ptrdiff_t used;
  304. int end_of_key_value_pair_found = 0;
  305. int get_block;
  306. if ((size_t)buf_fill < (sizeof(buf) - 1)) {
  307. size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
  308. r = mg_read(conn, buf + (size_t)buf_fill, to_read);
  309. if (r < 0) {
  310. /* read error */
  311. return -1;
  312. }
  313. if (r != (int)to_read) {
  314. /* TODO: Create a function to get "all_data_read" from
  315. * the conn object. All data is read if the Content-Length
  316. * has been reached, or if chunked encoding is used and
  317. * the end marker has been read, or if the connection has
  318. * been closed. */
  319. all_data_read = 1;
  320. }
  321. buf_fill += r;
  322. buf[buf_fill] = 0;
  323. if (buf_fill < 1) {
  324. break;
  325. }
  326. }
  327. val = strchr(buf, '=');
  328. if (!val) {
  329. break;
  330. }
  331. keylen = val - buf;
  332. val++;
  333. /* Call callback */
  334. memset(path, 0, sizeof(path));
  335. field_count++;
  336. field_storage = url_encoded_field_found(conn,
  337. buf,
  338. (size_t)keylen,
  339. NULL,
  340. 0,
  341. path,
  342. sizeof(path) - 1,
  343. fdh);
  344. if ((field_storage & FORM_FIELD_STORAGE_ABORT)
  345. == FORM_FIELD_STORAGE_ABORT) {
  346. /* Stop parsing the request */
  347. break;
  348. }
  349. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  350. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  351. fstore.access.fp = NULL;
  352. }
  353. file_size = 0;
  354. if (!fstore.access.fp) {
  355. mg_cry(conn, "%s: Cannot create file %s", __func__, path);
  356. }
  357. }
  358. get_block = 0;
  359. /* Loop to read values larger than sizeof(buf)-keylen-2 */
  360. do {
  361. next = strchr(val, '&');
  362. if (next) {
  363. vallen = next - val;
  364. next++;
  365. end_of_key_value_pair_found = 1;
  366. } else {
  367. vallen = (ptrdiff_t)strlen(val);
  368. next = val + vallen;
  369. }
  370. if (field_storage == FORM_FIELD_STORAGE_GET) {
  371. #if 0
  372. if (!end_of_key_value_pair_found && !all_data_read) {
  373. /* This callback will deliver partial contents */
  374. }
  375. #else
  376. (void)all_data_read; /* avoid warning */
  377. #endif
  378. /* Call callback */
  379. url_encoded_field_get(conn,
  380. ((get_block > 0) ? NULL : buf),
  381. ((get_block > 0) ? 0
  382. : (size_t)keylen),
  383. val,
  384. (size_t)vallen,
  385. fdh);
  386. get_block++;
  387. }
  388. if (fstore.access.fp) {
  389. size_t n = (size_t)
  390. fwrite(val, 1, (size_t)vallen, fstore.access.fp);
  391. if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
  392. mg_cry(conn,
  393. "%s: Cannot write file %s",
  394. __func__,
  395. path);
  396. mg_fclose(&fstore.access);
  397. remove_bad_file(conn, path);
  398. }
  399. file_size += (int64_t)n;
  400. }
  401. if (!end_of_key_value_pair_found) {
  402. used = next - buf;
  403. memmove(buf,
  404. buf + (size_t)used,
  405. sizeof(buf) - (size_t)used);
  406. buf_fill -= (int)used;
  407. if ((size_t)buf_fill < (sizeof(buf) - 1)) {
  408. size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
  409. r = mg_read(conn, buf + (size_t)buf_fill, to_read);
  410. if (r < 0) {
  411. /* read error */
  412. return -1;
  413. }
  414. if (r != (int)to_read) {
  415. /* TODO: Create a function to get "all_data_read"
  416. * from the conn object. All data is read if the
  417. * Content-Length has been reached, or if chunked
  418. * encoding is used and the end marker has been
  419. * read, or if the connection has been closed. */
  420. all_data_read = 1;
  421. }
  422. buf_fill += r;
  423. buf[buf_fill] = 0;
  424. if (buf_fill < 1) {
  425. break;
  426. }
  427. val = buf;
  428. }
  429. }
  430. } while (!end_of_key_value_pair_found);
  431. if (fstore.access.fp) {
  432. r = mg_fclose(&fstore.access);
  433. if (r == 0) {
  434. /* stored successfully */
  435. field_stored(conn, path, file_size, fdh);
  436. } else {
  437. mg_cry(conn, "%s: Error saving file %s", __func__, path);
  438. remove_bad_file(conn, path);
  439. }
  440. fstore.access.fp = NULL;
  441. }
  442. /* Proceed to next entry */
  443. used = next - buf;
  444. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  445. buf_fill -= (int)used;
  446. }
  447. return field_count;
  448. }
  449. if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
  450. /* The form data is in the request body data, encoded as multipart
  451. * content (see https://www.ietf.org/rfc/rfc1867.txt,
  452. * https://www.ietf.org/rfc/rfc2388.txt). */
  453. const char *boundary;
  454. size_t bl;
  455. ptrdiff_t used;
  456. struct mg_request_info part_header;
  457. char *hbuf, *hend, *fbeg, *fend, *nbeg, *nend;
  458. const char *content_disp;
  459. const char *next;
  460. memset(&part_header, 0, sizeof(part_header));
  461. /* Skip all spaces between MULTIPART/FORM-DATA; and BOUNDARY= */
  462. bl = 20;
  463. while (content_type[bl] == ' ') {
  464. bl++;
  465. }
  466. /* There has to be a BOUNDARY definition in the Content-Type header */
  467. if (mg_strncasecmp(content_type + bl, "BOUNDARY=", 9)) {
  468. /* Malformed request */
  469. return -1;
  470. }
  471. boundary = content_type + bl + 9;
  472. bl = strlen(boundary);
  473. if (bl + 800 > sizeof(buf)) {
  474. /* Sanity check: The algorithm can not work if bl >= sizeof(buf),
  475. * and it will not work effectively, if the buf is only a few byte
  476. * larger than bl, or it buf can not hold the multipart header
  477. * plus the boundary.
  478. * Check some reasonable number here, that should be fulfilled by
  479. * any reasonable request from every browser. If it is not
  480. * fulfilled, it might be a hand-made request, intended to
  481. * interfere with the algorithm. */
  482. return -1;
  483. }
  484. for (;;) {
  485. size_t towrite, n;
  486. int get_block;
  487. r = mg_read(conn,
  488. buf + (size_t)buf_fill,
  489. sizeof(buf) - 1 - (size_t)buf_fill);
  490. if (r < 0) {
  491. /* read error */
  492. return -1;
  493. }
  494. buf_fill += r;
  495. buf[buf_fill] = 0;
  496. if (buf_fill < 1) {
  497. /* No data */
  498. return -1;
  499. }
  500. if (buf[0] != '-' || buf[1] != '-') {
  501. /* Malformed request */
  502. return -1;
  503. }
  504. if (strncmp(buf + 2, boundary, bl)) {
  505. /* Malformed request */
  506. return -1;
  507. }
  508. if (buf[bl + 2] != '\r' || buf[bl + 3] != '\n') {
  509. /* Every part must end with \r\n, if there is another part.
  510. * The end of the request has an extra -- */
  511. if (((size_t)buf_fill != (size_t)(bl + 6))
  512. || (strncmp(buf + bl + 2, "--\r\n", 4))) {
  513. /* Malformed request */
  514. return -1;
  515. }
  516. /* End of the request */
  517. break;
  518. }
  519. /* Next, we need to get the part header: Read until \r\n\r\n */
  520. hbuf = buf + bl + 4;
  521. hend = strstr(hbuf, "\r\n\r\n");
  522. if (!hend) {
  523. /* Malformed request */
  524. return -1;
  525. }
  526. parse_http_headers(&hbuf, &part_header);
  527. if ((hend + 2) != hbuf) {
  528. /* Malformed request */
  529. return -1;
  530. }
  531. /* Skip \r\n\r\n */
  532. hend += 4;
  533. /* According to the RFC, every part has to have a header field like:
  534. * Content-Disposition: form-data; name="..." */
  535. content_disp = get_header(&part_header, "Content-Disposition");
  536. if (!content_disp) {
  537. /* Malformed request */
  538. return -1;
  539. }
  540. /* Get the mandatory name="..." part of the Content-Disposition
  541. * header. */
  542. nbeg = strstr(content_disp, "name=\"");
  543. if (!nbeg) {
  544. /* Malformed request */
  545. return -1;
  546. }
  547. nbeg += 6;
  548. nend = strchr(nbeg, '\"');
  549. if (!nend) {
  550. /* Malformed request */
  551. return -1;
  552. }
  553. /* Get the optional filename="..." part of the Content-Disposition
  554. * header. */
  555. fbeg = strstr(content_disp, "filename=\"");
  556. if (fbeg) {
  557. fbeg += 10;
  558. fend = strchr(fbeg, '\"');
  559. if (!fend) {
  560. /* Malformed request (the filename field is optional, but if
  561. * it exists, it needs to be terminated correctly). */
  562. return -1;
  563. }
  564. /* TODO: check Content-Type */
  565. /* Content-Type: application/octet-stream */
  566. } else {
  567. fend = fbeg;
  568. }
  569. memset(path, 0, sizeof(path));
  570. field_count++;
  571. field_storage = url_encoded_field_found(conn,
  572. nbeg,
  573. (size_t)(nend - nbeg),
  574. fbeg,
  575. (size_t)(fend - fbeg),
  576. path,
  577. sizeof(path) - 1,
  578. fdh);
  579. /* If the boundary is already in the buffer, get the address,
  580. * otherwise next will be NULL. */
  581. next = search_boundary(hbuf,
  582. (size_t)((buf - hbuf) + buf_fill),
  583. boundary,
  584. bl);
  585. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  586. /* Store the content to a file */
  587. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  588. fstore.access.fp = NULL;
  589. }
  590. file_size = 0;
  591. if (!fstore.access.fp) {
  592. mg_cry(conn, "%s: Cannot create file %s", __func__, path);
  593. }
  594. }
  595. get_block = 0;
  596. while (!next) {
  597. /* Set "towrite" to the number of bytes available
  598. * in the buffer */
  599. towrite = (size_t)(buf - hend + buf_fill);
  600. /* Subtract the boundary length, to deal with
  601. * cases the boundary is only partially stored
  602. * in the buffer. */
  603. towrite -= bl + 4;
  604. if (field_storage == FORM_FIELD_STORAGE_GET) {
  605. unencoded_field_get(conn,
  606. ((get_block > 0) ? NULL : nbeg),
  607. ((get_block > 0)
  608. ? 0
  609. : (size_t)(nend - nbeg)),
  610. hend,
  611. towrite,
  612. fdh);
  613. get_block++;
  614. }
  615. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  616. if (fstore.access.fp) {
  617. /* Store the content of the buffer. */
  618. n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
  619. if ((n != towrite) || (ferror(fstore.access.fp))) {
  620. mg_cry(conn,
  621. "%s: Cannot write file %s",
  622. __func__,
  623. path);
  624. mg_fclose(&fstore.access);
  625. remove_bad_file(conn, path);
  626. }
  627. file_size += (int64_t)n;
  628. }
  629. }
  630. memmove(buf, hend + towrite, bl + 4);
  631. buf_fill = (int)(bl + 4);
  632. hend = buf;
  633. /* Read new data */
  634. r = mg_read(conn,
  635. buf + (size_t)buf_fill,
  636. sizeof(buf) - 1 - (size_t)buf_fill);
  637. if (r < 0) {
  638. /* read error */
  639. return -1;
  640. }
  641. buf_fill += r;
  642. buf[buf_fill] = 0;
  643. if (buf_fill < 1) {
  644. /* No data */
  645. return -1;
  646. }
  647. /* Find boundary */
  648. next = search_boundary(buf, (size_t)buf_fill, boundary, bl);
  649. }
  650. towrite = (size_t)(next - hend);
  651. if (field_storage == FORM_FIELD_STORAGE_GET) {
  652. /* Call callback */
  653. unencoded_field_get(conn,
  654. ((get_block > 0) ? NULL : nbeg),
  655. ((get_block > 0) ? 0
  656. : (size_t)(nend - nbeg)),
  657. hend,
  658. towrite,
  659. fdh);
  660. }
  661. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  662. if (fstore.access.fp) {
  663. n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
  664. if ((n != towrite) || (ferror(fstore.access.fp))) {
  665. mg_cry(conn,
  666. "%s: Cannot write file %s",
  667. __func__,
  668. path);
  669. mg_fclose(&fstore.access);
  670. remove_bad_file(conn, path);
  671. }
  672. file_size += (int64_t)n;
  673. }
  674. }
  675. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  676. if (fstore.access.fp) {
  677. r = mg_fclose(&fstore.access);
  678. if (r == 0) {
  679. /* stored successfully */
  680. field_stored(conn, path, file_size, fdh);
  681. } else {
  682. mg_cry(conn,
  683. "%s: Error saving file %s",
  684. __func__,
  685. path);
  686. remove_bad_file(conn, path);
  687. }
  688. fstore.access.fp = NULL;
  689. }
  690. }
  691. if ((field_storage & FORM_FIELD_STORAGE_ABORT)
  692. == FORM_FIELD_STORAGE_ABORT) {
  693. /* Stop parsing the request */
  694. break;
  695. }
  696. /* Remove from the buffer */
  697. used = next - buf + 2;
  698. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  699. buf_fill -= (int)used;
  700. }
  701. /* All parts handled */
  702. return field_count;
  703. }
  704. /* Unknown Content-Type */
  705. return -1;
  706. }