handle_form.inl 22 KB

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