handle_form.inl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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;
  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. return fdh->field_get(key_dec,
  95. value_dec,
  96. (size_t)value_dec_len,
  97. fdh->user_data);
  98. }
  99. static int
  100. field_stored(const struct mg_connection *conn,
  101. const char *path,
  102. size_t file_size,
  103. struct mg_form_data_handler *fdh)
  104. {
  105. /* Equivalent to "upload" callback of "mg_upload". */
  106. (void)conn; /* we do not need mg_cry here, so conn is currently unused */
  107. return fdh->field_store(path, file_size, fdh->user_data);
  108. }
  109. static void
  110. remove_bad_file(const struct mg_connection *conn, const char *path)
  111. {
  112. int r = remove(path);
  113. if (r != 0) {
  114. mg_cry(conn, "%s: Cannot remove invalid file %s", __func__, path);
  115. }
  116. }
  117. static const char *
  118. search_boundary(const char *buf,
  119. size_t buf_len,
  120. const char *boundary,
  121. size_t boundary_len)
  122. {
  123. /* We must do a binary search here, not a string search, since the buffer
  124. * may contain '\x00' bytes, if binary data is transfered. */
  125. int clen = (int)buf_len - (int)boundary_len - 4;
  126. int i;
  127. for (i = 0; i <= clen; i++) {
  128. if (!memcmp(buf + i, "\r\n--", 4)) {
  129. if (!memcmp(buf + i + 4, boundary, boundary_len)) {
  130. return buf + i;
  131. }
  132. }
  133. }
  134. return NULL;
  135. }
  136. int
  137. mg_handle_form_request(struct mg_connection *conn,
  138. struct mg_form_data_handler *fdh)
  139. {
  140. const char *content_type;
  141. char path[512];
  142. char buf[1024];
  143. int field_storage;
  144. int buf_fill = 0;
  145. int r;
  146. int field_count = 0;
  147. struct file fstore;
  148. size_t file_size = 0; /* init here, to a avoid a false positive
  149. "uninitialized variable used" warning */
  150. int has_body_data =
  151. (conn->request_info.content_length > 0) || (conn->is_chunked);
  152. /* There are three ways to encode data from a HTML form:
  153. * 1) method: GET (default)
  154. * The form data is in the HTTP query string.
  155. * 2) method: POST, enctype: "application/x-www-form-urlencoded"
  156. * The form data is in the request body.
  157. * The body is url encoded (the default encoding for POST).
  158. * 3) method: POST, enctype: "multipart/form-data".
  159. * The form data is in the request body of a multipart message.
  160. * This is the typical way to handle file upload from a form.
  161. */
  162. if (!has_body_data) {
  163. const char *data;
  164. if (strcmp(conn->request_info.request_method, "GET")) {
  165. /* No body data, but not a GET request.
  166. * This is not a valid form request. */
  167. return -1;
  168. }
  169. /* GET request: form data is in the query string. */
  170. /* The entire data has already been loaded, so there is no nead to
  171. * call mg_read. We just need to split the query string into key-value
  172. * pairs. */
  173. data = conn->request_info.query_string;
  174. if (!data) {
  175. /* No query string. */
  176. return -1;
  177. }
  178. /* Split data in a=1&b=xy&c=3&c=4 ... */
  179. while (*data) {
  180. const char *val = strchr(data, '=');
  181. const char *next;
  182. ptrdiff_t keylen, vallen;
  183. if (!val) {
  184. break;
  185. }
  186. keylen = val - data;
  187. /* In every "field_found" callback we ask what to do with the
  188. * data ("field_storage"). This could be:
  189. * FORM_FIELD_STORAGE_SKIP (0) ... ignore the value of this field
  190. * FORM_FIELD_STORAGE_GET (1) ... read the data and call the get
  191. * callback function
  192. * FORM_FIELD_STORAGE_STORE (2) ... store the data in a file
  193. * FORM_FIELD_STORAGE_READ (3) ... let the user read the data
  194. * (for parsing long data on the fly)
  195. * (currently not implemented)
  196. * FORM_FIELD_STORAGE_ABORT (flag) ... stop parsing
  197. */
  198. memset(path, 0, sizeof(path));
  199. field_count++;
  200. field_storage = url_encoded_field_found(conn,
  201. data,
  202. (size_t)keylen,
  203. NULL,
  204. 0,
  205. path,
  206. sizeof(path) - 1,
  207. fdh);
  208. val++;
  209. next = strchr(val, '&');
  210. if (next) {
  211. vallen = next - val;
  212. next++;
  213. } else {
  214. vallen = (ptrdiff_t)strlen(val);
  215. next = val + vallen;
  216. }
  217. if (field_storage == FORM_FIELD_STORAGE_GET) {
  218. /* Call callback */
  219. url_encoded_field_get(
  220. conn, data, (size_t)keylen, val, (size_t)vallen, fdh);
  221. }
  222. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  223. /* Store the content to a file */
  224. if (mg_fopen(conn, path, "wb", &fstore) == 0) {
  225. fstore.fp = NULL;
  226. }
  227. file_size = 0;
  228. if (fstore.fp != NULL) {
  229. size_t n =
  230. (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
  231. if ((n != (size_t)vallen) || (ferror(fstore.fp))) {
  232. mg_cry(conn,
  233. "%s: Cannot write file %s",
  234. __func__,
  235. path);
  236. fclose(fstore.fp);
  237. fstore.fp = NULL;
  238. remove_bad_file(conn, path);
  239. }
  240. file_size += (size_t)n;
  241. if (fstore.fp) {
  242. r = fclose(fstore.fp);
  243. if (r == 0) {
  244. /* stored successfully */
  245. field_stored(conn, path, file_size, fdh);
  246. } else {
  247. mg_cry(conn,
  248. "%s: Error saving file %s",
  249. __func__,
  250. path);
  251. remove_bad_file(conn, path);
  252. }
  253. fstore.fp = NULL;
  254. }
  255. } else {
  256. mg_cry(conn, "%s: Cannot create file %s", __func__, path);
  257. }
  258. }
  259. /* if (field_storage == FORM_FIELD_STORAGE_READ) { */
  260. /* The idea of "field_storage=read" is to let the API user read
  261. * data chunk by chunk and to some data processing on the fly.
  262. * This should avoid the need to store data in the server:
  263. * It should neither be stored in memory, like
  264. * "field_storage=get" does, nor in a file like
  265. * "field_storage=store".
  266. * However, for a "GET" request this does not make any much
  267. * sense, since the data is already stored in memory, as it is
  268. * part of the query string.
  269. */
  270. /* } */
  271. if ((field_storage & FORM_FIELD_STORAGE_ABORT)
  272. == FORM_FIELD_STORAGE_ABORT) {
  273. /* Stop parsing the request */
  274. break;
  275. }
  276. /* Proceed to next entry */
  277. data = next;
  278. }
  279. return field_count;
  280. }
  281. content_type = mg_get_header(conn, "Content-Type");
  282. if (!content_type
  283. || !mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")) {
  284. /* The form data is in the request body data, encoded in key/value
  285. * pairs. */
  286. int all_data_read = 0;
  287. /* Read body data and split it in a=1&b&c=3&c=4 ... */
  288. /* The encoding is like in the "GET" case above, but here we read data
  289. * on the fly */
  290. for (;;) {
  291. /* TODO(high): Handle (text) fields with data size > sizeof(buf). */
  292. const char *val;
  293. const char *next;
  294. ptrdiff_t keylen, vallen;
  295. ptrdiff_t used;
  296. int end_of_key_value_pair_found = 0;
  297. if ((size_t)buf_fill < (sizeof(buf) - 1)) {
  298. size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
  299. r = mg_read(conn, buf + (size_t)buf_fill, to_read);
  300. if (r < 0) {
  301. /* read error */
  302. return -1;
  303. }
  304. if (r != (int)to_read) {
  305. /* TODO: Create a function to get "all_data_read" from
  306. * the conn object. Add data is read if the Content-Length
  307. * has been reached, or if chunked encoding is used and
  308. * the end marker has been read, or if the connection has
  309. * been closed. */
  310. all_data_read = 1;
  311. }
  312. buf_fill += r;
  313. buf[buf_fill] = 0;
  314. if (buf_fill < 1) {
  315. break;
  316. }
  317. }
  318. val = strchr(buf, '=');
  319. if (!val) {
  320. break;
  321. }
  322. keylen = val - buf;
  323. val++;
  324. /* Call callback */
  325. memset(path, 0, sizeof(path));
  326. field_count++;
  327. field_storage = url_encoded_field_found(conn,
  328. buf,
  329. (size_t)keylen,
  330. NULL,
  331. 0,
  332. path,
  333. sizeof(path) - 1,
  334. fdh);
  335. if ((field_storage & FORM_FIELD_STORAGE_ABORT)
  336. == FORM_FIELD_STORAGE_ABORT) {
  337. /* Stop parsing the request */
  338. break;
  339. }
  340. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  341. if (mg_fopen(conn, path, "wb", &fstore) == 0) {
  342. fstore.fp = NULL;
  343. }
  344. file_size = 0;
  345. if (!fstore.fp) {
  346. mg_cry(conn, "%s: Cannot create file %s", __func__, path);
  347. }
  348. }
  349. /* Loop to read values larger than sizeof(buf)-keylen-2 */
  350. do {
  351. next = strchr(val, '&');
  352. if (next) {
  353. vallen = next - val;
  354. next++;
  355. end_of_key_value_pair_found = 1;
  356. } else {
  357. vallen = (ptrdiff_t)strlen(val);
  358. next = val + vallen;
  359. }
  360. if (fstore.fp) {
  361. size_t n =
  362. (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
  363. if ((n != (size_t)vallen) || (ferror(fstore.fp))) {
  364. mg_cry(conn,
  365. "%s: Cannot write file %s",
  366. __func__,
  367. path);
  368. fclose(fstore.fp);
  369. fstore.fp = NULL;
  370. remove_bad_file(conn, path);
  371. }
  372. file_size += (size_t)n;
  373. }
  374. if (field_storage == FORM_FIELD_STORAGE_GET) {
  375. if (!end_of_key_value_pair_found && !all_data_read) {
  376. /* TODO: check for an easy way to get longer data */
  377. mg_cry(conn,
  378. "%s: Data too long for callback",
  379. __func__);
  380. return -1;
  381. }
  382. /* Call callback */
  383. url_encoded_field_get(
  384. conn, buf, (size_t)keylen, val, (size_t)vallen, fdh);
  385. }
  386. if (!end_of_key_value_pair_found) {
  387. /* TODO: read more data */
  388. break;
  389. }
  390. } while (!end_of_key_value_pair_found);
  391. if (fstore.fp) {
  392. r = fclose(fstore.fp);
  393. if (r == 0) {
  394. /* stored successfully */
  395. field_stored(conn, path, file_size, fdh);
  396. } else {
  397. mg_cry(conn, "%s: Error saving file %s", __func__, path);
  398. remove_bad_file(conn, path);
  399. }
  400. fstore.fp = NULL;
  401. }
  402. /* Proceed to next entry */
  403. used = next - buf;
  404. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  405. buf_fill -= (int)used;
  406. }
  407. return field_count;
  408. }
  409. if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
  410. /* The form data is in the request body data, encoded as multipart
  411. * content (see https://www.ietf.org/rfc/rfc1867.txt,
  412. * https://www.ietf.org/rfc/rfc2388.txt). */
  413. const char *boundary;
  414. size_t bl;
  415. ptrdiff_t used;
  416. struct mg_request_info part_header;
  417. char *hbuf, *hend, *fbeg, *fend, *nbeg, *nend;
  418. const char *content_disp;
  419. const char *next;
  420. memset(&part_header, 0, sizeof(part_header));
  421. /* There has to be a BOUNDARY definition in the Content-Type header */
  422. if (mg_strncasecmp(content_type + 21, "BOUNDARY=", 9)) {
  423. /* Malformed request */
  424. return -1;
  425. }
  426. boundary = content_type + 30;
  427. bl = strlen(boundary);
  428. if (bl + 800 > sizeof(buf)) {
  429. /* Sanity check: The algorithm can not work if bl >= sizeof(buf),
  430. * and it will not work effectively, if the buf is only a few byte
  431. * larger than bl, or it buf can not hold the multipart header
  432. * plus the boundary.
  433. * Check some reasonable number here, that should be fulfilled by
  434. * any reasonable request from every browser. If it is not
  435. * fulfilled, it might be a hand-made request, intended to
  436. * interfere with the algorithm. */
  437. return -1;
  438. }
  439. for (;;) {
  440. r = mg_read(conn,
  441. buf + (size_t)buf_fill,
  442. sizeof(buf) - 1 - (size_t)buf_fill);
  443. if (r < 0) {
  444. /* read error */
  445. return -1;
  446. }
  447. buf_fill += r;
  448. buf[buf_fill] = 0;
  449. if (buf_fill < 1) {
  450. /* No data */
  451. return -1;
  452. }
  453. if (buf[0] != '-' || buf[1] != '-') {
  454. /* Malformed request */
  455. return -1;
  456. }
  457. if (strncmp(buf + 2, boundary, bl)) {
  458. /* Malformed request */
  459. return -1;
  460. }
  461. if (buf[bl + 2] != '\r' || buf[bl + 3] != '\n') {
  462. /* Every part must end with \r\n, if there is another part.
  463. * The end of the request has an extra -- */
  464. if (((size_t)buf_fill != (size_t)(bl + 6))
  465. || (strncmp(buf + bl + 2, "--\r\n", 4))) {
  466. /* Malformed request */
  467. return -1;
  468. }
  469. /* End of the request */
  470. break;
  471. }
  472. /* Next, we need to get the part header: Read until \r\n\r\n */
  473. hbuf = buf + bl + 4;
  474. hend = strstr(hbuf, "\r\n\r\n");
  475. if (!hend) {
  476. /* Malformed request */
  477. return -1;
  478. }
  479. parse_http_headers(&hbuf, &part_header);
  480. if ((hend + 2) != hbuf) {
  481. /* Malformed request */
  482. return -1;
  483. }
  484. /* Skip \r\n\r\n */
  485. hend += 4;
  486. /* According to the RFC, every part has to have a header field like:
  487. * Content-Disposition: form-data; name="..." */
  488. content_disp = get_header(&part_header, "Content-Disposition");
  489. if (!content_disp) {
  490. /* Malformed request */
  491. return -1;
  492. }
  493. /* Get the mandatory name="..." part of the Content-Disposition
  494. * header. */
  495. nbeg = strstr(content_disp, "name=\"");
  496. if (!nbeg) {
  497. /* Malformed request */
  498. return -1;
  499. }
  500. nbeg += 6;
  501. nend = strchr(nbeg, '\"');
  502. if (!nend) {
  503. /* Malformed request */
  504. return -1;
  505. }
  506. /* Get the optional filename="..." part of the Content-Disposition
  507. * header. */
  508. fbeg = strstr(content_disp, "filename=\"");
  509. if (fbeg) {
  510. fbeg += 10;
  511. fend = strchr(fbeg, '\"');
  512. if (!fend) {
  513. /* Malformed request (the filename field is optional, but if
  514. * it exists, it needs to be terminated correctly). */
  515. return -1;
  516. }
  517. /* TODO: check Content-Type */
  518. /* Content-Type: application/octet-stream */
  519. } else {
  520. fend = fbeg;
  521. }
  522. memset(path, 0, sizeof(path));
  523. field_count++;
  524. field_storage = url_encoded_field_found(conn,
  525. nbeg,
  526. (size_t)(nend - nbeg),
  527. fbeg,
  528. (size_t)(fend - fbeg),
  529. path,
  530. sizeof(path) - 1,
  531. fdh);
  532. /* If the boundary is already in the buffer, get the address,
  533. * otherwise next will be NULL. */
  534. next = search_boundary(hbuf,
  535. (size_t)((buf - hbuf) + buf_fill),
  536. boundary,
  537. bl);
  538. if (field_storage == FORM_FIELD_STORAGE_GET) {
  539. if (!next) {
  540. /* TODO: check for an easy way to get longer data */
  541. mg_cry(conn, "%s: Data too long for callback", __func__);
  542. return -1;
  543. }
  544. /* Call callback */
  545. url_encoded_field_get(conn,
  546. nbeg,
  547. (size_t)(nend - nbeg),
  548. hend,
  549. (size_t)(next - hend),
  550. fdh);
  551. }
  552. if (field_storage == FORM_FIELD_STORAGE_STORE) {
  553. /* Store the content to a file */
  554. size_t towrite, n;
  555. if (mg_fopen(conn, path, "wb", &fstore) == 0) {
  556. fstore.fp = NULL;
  557. }
  558. file_size = 0;
  559. if (!fstore.fp) {
  560. mg_cry(conn, "%s: Cannot create file %s", __func__, path);
  561. }
  562. while (!next) {
  563. /* Set "towrite" to the number of bytes available
  564. * in the buffer */
  565. towrite = (size_t)(buf - hend + buf_fill);
  566. /* Subtract the boundary length, to deal with
  567. * cases the boundary is only partially stored
  568. * in the buffer. */
  569. towrite -= bl + 4;
  570. if (fstore.fp) {
  571. /* Store the content of the buffer. */
  572. n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
  573. if ((n != towrite) || (ferror(fstore.fp))) {
  574. mg_cry(conn,
  575. "%s: Cannot write file %s",
  576. __func__,
  577. path);
  578. fclose(fstore.fp);
  579. fstore.fp = NULL;
  580. remove_bad_file(conn, path);
  581. }
  582. file_size += (size_t)n;
  583. }
  584. memmove(buf, hend + towrite, bl + 4);
  585. buf_fill = (int)(bl + 4);
  586. hend = buf;
  587. /* Read new data */
  588. r = mg_read(conn,
  589. buf + (size_t)buf_fill,
  590. sizeof(buf) - 1 - (size_t)buf_fill);
  591. if (r < 0) {
  592. /* read error */
  593. return -1;
  594. }
  595. buf_fill += r;
  596. buf[buf_fill] = 0;
  597. if (buf_fill < 1) {
  598. /* No data */
  599. return -1;
  600. }
  601. /* Find boundary */
  602. next = search_boundary(buf, (size_t)buf_fill, boundary, bl);
  603. }
  604. if (fstore.fp) {
  605. towrite = (size_t)(next - hend);
  606. n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
  607. if ((n != towrite) || (ferror(fstore.fp))) {
  608. mg_cry(conn,
  609. "%s: Cannot write file %s",
  610. __func__,
  611. path);
  612. fclose(fstore.fp);
  613. fstore.fp = NULL;
  614. remove_bad_file(conn, path);
  615. }
  616. file_size += (size_t)n;
  617. }
  618. if (fstore.fp) {
  619. r = fclose(fstore.fp);
  620. if (r == 0) {
  621. /* stored successfully */
  622. field_stored(conn, path, file_size, fdh);
  623. } else {
  624. mg_cry(conn,
  625. "%s: Error saving file %s",
  626. __func__,
  627. path);
  628. remove_bad_file(conn, path);
  629. }
  630. fstore.fp = NULL;
  631. }
  632. }
  633. if ((field_storage & FORM_FIELD_STORAGE_ABORT)
  634. == FORM_FIELD_STORAGE_ABORT) {
  635. /* Stop parsing the request */
  636. return -1;
  637. }
  638. /* Remove from the buffer */
  639. used = next - buf + 2;
  640. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  641. buf_fill -= (int)used;
  642. }
  643. /* All parts handled */
  644. return field_count;
  645. }
  646. /* Unknown Content-Type */
  647. return -1;
  648. }