handle_form.inl 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /* Copyright (c) 2016-2021 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 MG_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_internal(conn, "%s: Cannot decode filename", __func__);
  51. return MG_FORM_FIELD_STORAGE_SKIP;
  52. }
  53. remove_dot_segments(filename_dec);
  54. } else {
  55. filename_dec[0] = 0;
  56. }
  57. ret =
  58. fdh->field_found(key_dec, filename_dec, path, path_len, fdh->user_data);
  59. if ((ret & 0xF) == MG_FORM_FIELD_STORAGE_GET) {
  60. if (fdh->field_get == NULL) {
  61. mg_cry_internal(conn,
  62. "%s: Function \"Get\" not available",
  63. __func__);
  64. return MG_FORM_FIELD_STORAGE_SKIP;
  65. }
  66. }
  67. if ((ret & 0xF) == MG_FORM_FIELD_STORAGE_STORE) {
  68. if (fdh->field_store == NULL) {
  69. mg_cry_internal(conn,
  70. "%s: Function \"Store\" not available",
  71. __func__);
  72. return MG_FORM_FIELD_STORAGE_SKIP;
  73. }
  74. }
  75. return ret;
  76. }
  77. static int
  78. url_encoded_field_get(
  79. const struct mg_connection *conn,
  80. const char *key,
  81. size_t key_len,
  82. const char *value,
  83. size_t *value_len, /* IN: number of bytes available in "value", OUT: number
  84. of bytes processed */
  85. struct mg_form_data_handler *fdh)
  86. {
  87. char key_dec[1024];
  88. char *value_dec = (char *)mg_malloc_ctx(*value_len + 1, conn->phys_ctx);
  89. int value_dec_len, ret;
  90. if (!value_dec) {
  91. /* Log error message and stop parsing the form data. */
  92. mg_cry_internal(conn,
  93. "%s: Not enough memory (required: %lu)",
  94. __func__,
  95. (unsigned long)(*value_len + 1));
  96. return MG_FORM_FIELD_STORAGE_ABORT;
  97. }
  98. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  99. if (*value_len >= 2 && value[*value_len - 2] == '%')
  100. *value_len -= 2;
  101. else if (*value_len >= 1 && value[*value_len - 1] == '%')
  102. (*value_len)--;
  103. value_dec_len = mg_url_decode(
  104. value, (int)*value_len, value_dec, ((int)*value_len) + 1, 1);
  105. ret = fdh->field_get(key_dec,
  106. value_dec,
  107. (size_t)value_dec_len,
  108. fdh->user_data);
  109. mg_free(value_dec);
  110. return ret;
  111. }
  112. static int
  113. unencoded_field_get(const struct mg_connection *conn,
  114. const char *key,
  115. size_t key_len,
  116. const char *value,
  117. size_t value_len,
  118. struct mg_form_data_handler *fdh)
  119. {
  120. char key_dec[1024];
  121. (void)conn;
  122. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  123. return fdh->field_get(key_dec, value, value_len, fdh->user_data);
  124. }
  125. static int
  126. field_stored(const struct mg_connection *conn,
  127. const char *path,
  128. long long file_size,
  129. struct mg_form_data_handler *fdh)
  130. {
  131. /* Equivalent to "upload" callback of "mg_upload". */
  132. (void)conn; /* we do not need mg_cry here, so conn is currently unused */
  133. return fdh->field_store(path, file_size, fdh->user_data);
  134. }
  135. static const char *
  136. search_boundary(const char *buf,
  137. size_t buf_len,
  138. const char *boundary,
  139. size_t boundary_len)
  140. {
  141. char *boundary_start = "\r\n--";
  142. size_t boundary_start_len = strlen(boundary_start);
  143. /* We must do a binary search here, not a string search, since the
  144. * buffer may contain '\x00' bytes, if binary data is transferred. */
  145. int clen = (int)buf_len - (int)boundary_len - boundary_start_len;
  146. int i;
  147. for (i = 0; i <= clen; i++) {
  148. if (!memcmp(buf + i, boundary_start, boundary_start_len)) {
  149. if (!memcmp(buf + i + boundary_start_len, boundary, boundary_len)) {
  150. return buf + i;
  151. }
  152. }
  153. }
  154. return NULL;
  155. }
  156. int
  157. mg_handle_form_request(struct mg_connection *conn,
  158. struct mg_form_data_handler *fdh)
  159. {
  160. const char *content_type;
  161. char path[512];
  162. char buf[MG_BUF_LEN]; /* Must not be smaller than ~900 */
  163. int field_storage;
  164. size_t buf_fill = 0;
  165. int r;
  166. int field_count = 0;
  167. struct mg_file fstore = STRUCT_FILE_INITIALIZER;
  168. int64_t file_size = 0; /* init here, to a avoid a false positive
  169. "uninitialized variable used" warning */
  170. int has_body_data =
  171. (conn->request_info.content_length > 0) || (conn->is_chunked);
  172. /* Unused without filesystems */
  173. (void)fstore;
  174. (void)file_size;
  175. /* There are three ways to encode data from a HTML form:
  176. * 1) method: GET (default)
  177. * The form data is in the HTTP query string.
  178. * 2) method: POST, enctype: "application/x-www-form-urlencoded"
  179. * The form data is in the request body.
  180. * The body is url encoded (the default encoding for POST).
  181. * 3) method: POST, enctype: "multipart/form-data".
  182. * The form data is in the request body of a multipart message.
  183. * This is the typical way to handle file upload from a form.
  184. */
  185. if (!has_body_data) {
  186. const char *data;
  187. if (0 != strcmp(conn->request_info.request_method, "GET")) {
  188. /* No body data, but not a GET request.
  189. * This is not a valid form request. */
  190. return -1;
  191. }
  192. /* GET request: form data is in the query string. */
  193. /* The entire data has already been loaded, so there is no need to
  194. * call mg_read. We just need to split the query string into key-value
  195. * pairs. */
  196. data = conn->request_info.query_string;
  197. if (!data) {
  198. /* No query string. */
  199. return -1;
  200. }
  201. /* Split data in a=1&b=xy&c=3&c=4 ... */
  202. while (*data) {
  203. const char *val = strchr(data, '=');
  204. const char *next;
  205. ptrdiff_t keylen, vallen;
  206. if (!val) {
  207. break;
  208. }
  209. keylen = val - data;
  210. /* In every "field_found" callback we ask what to do with the
  211. * data ("field_storage"). This could be:
  212. * MG_FORM_FIELD_STORAGE_SKIP (0):
  213. * ignore the value of this field
  214. * MG_FORM_FIELD_STORAGE_GET (1):
  215. * read the data and call the get callback function
  216. * MG_FORM_FIELD_STORAGE_STORE (2):
  217. * store the data in a file
  218. * MG_FORM_FIELD_STORAGE_READ (3):
  219. * let the user read the data (for parsing long data on the fly)
  220. * MG_FORM_FIELD_STORAGE_ABORT (flag):
  221. * stop parsing
  222. */
  223. memset(path, 0, sizeof(path));
  224. field_count++;
  225. field_storage = url_encoded_field_found(conn,
  226. data,
  227. (size_t)keylen,
  228. NULL,
  229. 0,
  230. path,
  231. sizeof(path) - 1,
  232. fdh);
  233. val++;
  234. next = strchr(val, '&');
  235. if (next) {
  236. vallen = next - val;
  237. } else {
  238. vallen = (ptrdiff_t)strlen(val);
  239. }
  240. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  241. /* Call callback */
  242. r = url_encoded_field_get(
  243. conn, data, (size_t)keylen, val, (size_t *)&vallen, fdh);
  244. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  245. /* Stop request handling */
  246. break;
  247. }
  248. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  249. /* Skip to next field */
  250. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  251. }
  252. }
  253. if (next) {
  254. next++;
  255. } else {
  256. /* vallen may have been modified by url_encoded_field_get */
  257. next = val + vallen;
  258. }
  259. #if !defined(NO_FILESYSTEMS)
  260. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  261. /* Store the content to a file */
  262. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  263. fstore.access.fp = NULL;
  264. }
  265. file_size = 0;
  266. if (fstore.access.fp != NULL) {
  267. size_t n = (size_t)
  268. fwrite(val, 1, (size_t)vallen, fstore.access.fp);
  269. if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
  270. mg_cry_internal(conn,
  271. "%s: Cannot write file %s",
  272. __func__,
  273. path);
  274. (void)mg_fclose(&fstore.access);
  275. remove_bad_file(conn, path);
  276. }
  277. file_size += (int64_t)n;
  278. if (fstore.access.fp) {
  279. r = mg_fclose(&fstore.access);
  280. if (r == 0) {
  281. /* stored successfully */
  282. r = field_stored(conn, path, file_size, fdh);
  283. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  284. /* Stop request handling */
  285. break;
  286. }
  287. } else {
  288. mg_cry_internal(conn,
  289. "%s: Error saving file %s",
  290. __func__,
  291. path);
  292. remove_bad_file(conn, path);
  293. }
  294. fstore.access.fp = NULL;
  295. }
  296. } else {
  297. mg_cry_internal(conn,
  298. "%s: Cannot create file %s",
  299. __func__,
  300. path);
  301. }
  302. }
  303. #endif /* NO_FILESYSTEMS */
  304. /* if (field_storage == MG_FORM_FIELD_STORAGE_READ) { */
  305. /* The idea of "field_storage=read" is to let the API user read
  306. * data chunk by chunk and to some data processing on the fly.
  307. * This should avoid the need to store data in the server:
  308. * It should neither be stored in memory, like
  309. * "field_storage=get" does, nor in a file like
  310. * "field_storage=store".
  311. * However, for a "GET" request this does not make any much
  312. * sense, since the data is already stored in memory, as it is
  313. * part of the query string.
  314. */
  315. /* } */
  316. if ((field_storage & MG_FORM_FIELD_STORAGE_ABORT)
  317. == MG_FORM_FIELD_STORAGE_ABORT) {
  318. /* Stop parsing the request */
  319. break;
  320. }
  321. /* Proceed to next entry */
  322. data = next;
  323. }
  324. return field_count;
  325. }
  326. content_type = mg_get_header(conn, "Content-Type");
  327. if (!content_type
  328. || !mg_strncasecmp(content_type,
  329. "APPLICATION/X-WWW-FORM-URLENCODED",
  330. 33)
  331. || !mg_strncasecmp(content_type,
  332. "APPLICATION/WWW-FORM-URLENCODED",
  333. 31)) {
  334. /* The form data is in the request body data, encoded in key/value
  335. * pairs. */
  336. int all_data_read = 0;
  337. /* Read body data and split it in keys and values.
  338. * The encoding is like in the "GET" case above: a=1&b&c=3&c=4.
  339. * Here we use "POST", and read the data from the request body.
  340. * The data read on the fly, so it is not required to buffer the
  341. * entire request in memory before processing it. */
  342. for (;;) {
  343. const char *val;
  344. const char *next;
  345. ptrdiff_t keylen, vallen;
  346. ptrdiff_t used;
  347. int end_of_key_value_pair_found = 0;
  348. int get_block;
  349. if (buf_fill < (sizeof(buf) - 1)) {
  350. size_t to_read = sizeof(buf) - 1 - buf_fill;
  351. r = mg_read(conn, buf + buf_fill, to_read);
  352. if ((r < 0) || ((r == 0) && all_data_read)) {
  353. /* read error */
  354. return -1;
  355. }
  356. if (r == 0) {
  357. /* TODO: Create a function to get "all_data_read" from
  358. * the conn object. All data is read if the Content-Length
  359. * has been reached, or if chunked encoding is used and
  360. * the end marker has been read, or if the connection has
  361. * been closed. */
  362. all_data_read = (buf_fill == 0);
  363. }
  364. buf_fill += r;
  365. buf[buf_fill] = 0;
  366. if (buf_fill < 1) {
  367. break;
  368. }
  369. }
  370. val = strchr(buf, '=');
  371. if (!val) {
  372. break;
  373. }
  374. keylen = val - buf;
  375. val++;
  376. /* Call callback */
  377. memset(path, 0, sizeof(path));
  378. field_count++;
  379. field_storage = url_encoded_field_found(conn,
  380. buf,
  381. (size_t)keylen,
  382. NULL,
  383. 0,
  384. path,
  385. sizeof(path) - 1,
  386. fdh);
  387. if ((field_storage & MG_FORM_FIELD_STORAGE_ABORT)
  388. == MG_FORM_FIELD_STORAGE_ABORT) {
  389. /* Stop parsing the request */
  390. break;
  391. }
  392. #if !defined(NO_FILESYSTEMS)
  393. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  394. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  395. fstore.access.fp = NULL;
  396. }
  397. file_size = 0;
  398. if (!fstore.access.fp) {
  399. mg_cry_internal(conn,
  400. "%s: Cannot create file %s",
  401. __func__,
  402. path);
  403. }
  404. }
  405. #endif /* NO_FILESYSTEMS */
  406. get_block = 0;
  407. /* Loop to read values larger than sizeof(buf)-keylen-2 */
  408. do {
  409. next = strchr(val, '&');
  410. if (next) {
  411. vallen = next - val;
  412. end_of_key_value_pair_found = 1;
  413. } else {
  414. vallen = (ptrdiff_t)strlen(val);
  415. end_of_key_value_pair_found = all_data_read;
  416. }
  417. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  418. #if 0
  419. if (!end_of_key_value_pair_found && !all_data_read) {
  420. /* This callback will deliver partial contents */
  421. }
  422. #endif
  423. /* Call callback */
  424. r = url_encoded_field_get(conn,
  425. ((get_block > 0) ? NULL : buf),
  426. ((get_block > 0)
  427. ? 0
  428. : (size_t)keylen),
  429. val,
  430. (size_t *)&vallen,
  431. fdh);
  432. get_block++;
  433. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  434. /* Stop request handling */
  435. break;
  436. }
  437. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  438. /* Skip to next field */
  439. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  440. }
  441. }
  442. if (next) {
  443. next++;
  444. } else {
  445. /* vallen may have been modified by url_encoded_field_get */
  446. next = val + vallen;
  447. }
  448. #if !defined(NO_FILESYSTEMS)
  449. if (fstore.access.fp) {
  450. size_t n = (size_t)
  451. fwrite(val, 1, (size_t)vallen, fstore.access.fp);
  452. if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
  453. mg_cry_internal(conn,
  454. "%s: Cannot write file %s",
  455. __func__,
  456. path);
  457. mg_fclose(&fstore.access);
  458. remove_bad_file(conn, path);
  459. }
  460. file_size += (int64_t)n;
  461. }
  462. #endif /* NO_FILESYSTEMS */
  463. if (!end_of_key_value_pair_found) {
  464. used = next - buf;
  465. memmove(buf,
  466. buf + (size_t)used,
  467. sizeof(buf) - (size_t)used);
  468. next = buf;
  469. buf_fill -= used;
  470. if (buf_fill < (sizeof(buf) - 1)) {
  471. size_t to_read = sizeof(buf) - 1 - buf_fill;
  472. r = mg_read(conn, buf + buf_fill, to_read);
  473. if ((r < 0) || ((r == 0) && all_data_read)) {
  474. #if !defined(NO_FILESYSTEMS)
  475. /* read error */
  476. if (fstore.access.fp) {
  477. mg_fclose(&fstore.access);
  478. remove_bad_file(conn, path);
  479. }
  480. return -1;
  481. #endif /* NO_FILESYSTEMS */
  482. }
  483. if (r == 0) {
  484. /* TODO: Create a function to get "all_data_read"
  485. * from the conn object. All data is read if the
  486. * Content-Length has been reached, or if chunked
  487. * encoding is used and the end marker has been
  488. * read, or if the connection has been closed. */
  489. all_data_read = (buf_fill == 0);
  490. }
  491. buf_fill += r;
  492. buf[buf_fill] = 0;
  493. if (buf_fill < 1) {
  494. break;
  495. }
  496. val = buf;
  497. }
  498. }
  499. } while (!end_of_key_value_pair_found);
  500. #if !defined(NO_FILESYSTEMS)
  501. if (fstore.access.fp) {
  502. r = mg_fclose(&fstore.access);
  503. if (r == 0) {
  504. /* stored successfully */
  505. r = field_stored(conn, path, file_size, fdh);
  506. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  507. /* Stop request handling */
  508. break;
  509. }
  510. } else {
  511. mg_cry_internal(conn,
  512. "%s: Error saving file %s",
  513. __func__,
  514. path);
  515. remove_bad_file(conn, path);
  516. }
  517. fstore.access.fp = NULL;
  518. }
  519. #endif /* NO_FILESYSTEMS */
  520. if (all_data_read && (buf_fill == 0)) {
  521. /* nothing more to process */
  522. break;
  523. }
  524. /* Proceed to next entry */
  525. used = next - buf;
  526. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  527. buf_fill -= used;
  528. }
  529. return field_count;
  530. }
  531. if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
  532. /* The form data is in the request body data, encoded as multipart
  533. * content (see https://www.ietf.org/rfc/rfc1867.txt,
  534. * https://www.ietf.org/rfc/rfc2388.txt). */
  535. char *boundary;
  536. size_t bl;
  537. ptrdiff_t used;
  538. struct mg_request_info part_header;
  539. char *hbuf;
  540. const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
  541. const char *next;
  542. unsigned part_no;
  543. int all_data_read = 0;
  544. memset(&part_header, 0, sizeof(part_header));
  545. /* Skip all spaces between MULTIPART/FORM-DATA; and BOUNDARY= */
  546. bl = 20;
  547. while (content_type[bl] == ' ') {
  548. bl++;
  549. }
  550. /* There has to be a BOUNDARY definition in the Content-Type header */
  551. if (mg_strncasecmp(content_type + bl, "BOUNDARY=", 9)) {
  552. /* Malformed request */
  553. return -1;
  554. }
  555. /* Copy boundary string to variable "boundary" */
  556. /* fbeg is pointer to start of value of boundary */
  557. fbeg = content_type + bl + 9;
  558. bl = strlen(fbeg);
  559. boundary = (char *)mg_malloc(bl + 1);
  560. if (!boundary) {
  561. /* Out of memory */
  562. mg_cry_internal(conn,
  563. "%s: Cannot allocate memory for boundary [%lu]",
  564. __func__,
  565. (unsigned long)bl);
  566. return -1;
  567. }
  568. memcpy(boundary, fbeg, bl);
  569. boundary[bl] = 0;
  570. /* RFC 2046 permits the boundary string to be quoted. */
  571. /* If the boundary is quoted, trim the quotes */
  572. if (boundary[0] == '"') {
  573. hbuf = strchr(boundary + 1, '"');
  574. if ((!hbuf) || (*hbuf != '"')) {
  575. /* Malformed request */
  576. mg_free(boundary);
  577. return -1;
  578. }
  579. *hbuf = 0;
  580. memmove(boundary, boundary + 1, bl);
  581. bl = strlen(boundary);
  582. }
  583. /* Do some sanity checks for boundary lengths */
  584. if (bl > 70) {
  585. /* From RFC 2046:
  586. * Boundary delimiters must not appear within the
  587. * encapsulated material, and must be no longer
  588. * than 70 characters, not counting the two
  589. * leading hyphens.
  590. */
  591. /* The algorithm can not work if bl >= sizeof(buf), or if buf
  592. * can not hold the multipart header plus the boundary.
  593. * Requests with long boundaries are not RFC compliant, maybe they
  594. * are intended attacks to interfere with this algorithm. */
  595. mg_free(boundary);
  596. return -1;
  597. }
  598. if (bl < 4) {
  599. /* Sanity check: A boundary string of less than 4 bytes makes
  600. * no sense either. */
  601. mg_free(boundary);
  602. return -1;
  603. }
  604. for (part_no = 0;; part_no++) {
  605. size_t towrite, fnlen, n;
  606. int get_block;
  607. size_t to_read = sizeof(buf) - 1 - buf_fill;
  608. /* Unused without filesystems */
  609. (void)n;
  610. r = mg_read(conn, buf + buf_fill, to_read);
  611. if ((r < 0) || ((r == 0) && all_data_read)) {
  612. /* read error */
  613. mg_free(boundary);
  614. return -1;
  615. }
  616. if (r == 0) {
  617. all_data_read = (buf_fill == 0);
  618. }
  619. buf_fill += r;
  620. buf[buf_fill] = 0;
  621. if (buf_fill < 1) {
  622. /* No data */
  623. mg_free(boundary);
  624. return -1;
  625. }
  626. /* @see https://www.rfc-editor.org/rfc/rfc2046.html#section-5.1.1
  627. *
  628. * multipart-body := [preamble CRLF]
  629. * dash-boundary transport-padding CRLF
  630. * body-part *encapsulation
  631. * close-delimiter transport-padding
  632. * [CRLF epilogue]
  633. */
  634. if (part_no == 0) {
  635. size_t preamble_length = 0;
  636. /* skip over the preamble until we find a complete boundary
  637. * limit the preamble length to prevent abuse */
  638. /* +2 for the -- preceding the boundary */
  639. while (preamble_length < 1024
  640. && (preamble_length < buf_fill - bl)
  641. && strncmp(buf + preamble_length + 2, boundary, bl)) {
  642. preamble_length++;
  643. }
  644. /* reset the start of buf to remove the preamble */
  645. if (0 == strncmp(buf + preamble_length + 2, boundary, bl)) {
  646. memmove(buf,
  647. buf + preamble_length,
  648. (unsigned)buf_fill - (unsigned)preamble_length);
  649. buf_fill -= preamble_length;
  650. buf[buf_fill] = 0;
  651. }
  652. }
  653. /* either it starts with a boundary and it's fine, or it's malformed
  654. * because:
  655. * - the preamble was longer than accepted
  656. * - couldn't find a boundary at all in the body
  657. * - didn't have a terminating boundary */
  658. if (buf_fill < (bl + 2) || strncmp(buf, "--", 2)
  659. || strncmp(buf + 2, boundary, bl)) {
  660. /* Malformed request */
  661. mg_free(boundary);
  662. return -1;
  663. }
  664. /* skip the -- */
  665. char *boundary_start = buf + 2;
  666. size_t transport_padding = 0;
  667. while (boundary_start[bl + transport_padding] == ' '
  668. || boundary_start[bl + transport_padding] == '\t') {
  669. transport_padding++;
  670. }
  671. char *boundary_end = boundary_start + bl + transport_padding;
  672. /* after the transport padding, if the boundary isn't
  673. * immediately followed by a \r\n then it is either... */
  674. if (strncmp(boundary_end, "\r\n", 2)) {
  675. /* ...the final boundary, and it is followed by --, (in which
  676. * case it's the end of the request) or it's a malformed
  677. * request */
  678. if (strncmp(boundary_end, "--", 2)) {
  679. /* Malformed request */
  680. mg_free(boundary);
  681. return -1;
  682. }
  683. /* Ingore any epilogue here */
  684. break;
  685. }
  686. /* skip the \r\n */
  687. hbuf = boundary_end + 2;
  688. /* Next, we need to get the part header: Read until \r\n\r\n */
  689. hend = strstr(hbuf, "\r\n\r\n");
  690. if (!hend) {
  691. /* Malformed request */
  692. mg_free(boundary);
  693. return -1;
  694. }
  695. part_header.num_headers =
  696. parse_http_headers(&hbuf, part_header.http_headers);
  697. if ((hend + 2) != hbuf) {
  698. /* Malformed request */
  699. mg_free(boundary);
  700. return -1;
  701. }
  702. /* Skip \r\n\r\n */
  703. hend += 4;
  704. /* According to the RFC, every part has to have a header field like:
  705. * Content-Disposition: form-data; name="..." */
  706. content_disp = get_header(part_header.http_headers,
  707. part_header.num_headers,
  708. "Content-Disposition");
  709. if (!content_disp) {
  710. /* Malformed request */
  711. mg_free(boundary);
  712. return -1;
  713. }
  714. /* Get the mandatory name="..." part of the Content-Disposition
  715. * header. */
  716. nbeg = strstr(content_disp, "name=\"");
  717. while ((nbeg != NULL) && (strcspn(nbeg - 1, ":,; \t") != 0)) {
  718. /* It could be somethingname= instead of name= */
  719. nbeg = strstr(nbeg + 1, "name=\"");
  720. }
  721. /* This line is not required, but otherwise some compilers
  722. * generate spurious warnings. */
  723. nend = nbeg;
  724. /* And others complain, the result is unused. */
  725. (void)nend;
  726. /* If name=" is found, search for the closing " */
  727. if (nbeg) {
  728. nbeg += 6;
  729. nend = strchr(nbeg, '\"');
  730. if (!nend) {
  731. /* Malformed request */
  732. mg_free(boundary);
  733. return -1;
  734. }
  735. } else {
  736. /* name= without quotes is also allowed */
  737. nbeg = strstr(content_disp, "name=");
  738. while ((nbeg != NULL) && (strcspn(nbeg - 1, ":,; \t") != 0)) {
  739. /* It could be somethingname= instead of name= */
  740. nbeg = strstr(nbeg + 1, "name=");
  741. }
  742. if (!nbeg) {
  743. /* Malformed request */
  744. mg_free(boundary);
  745. return -1;
  746. }
  747. nbeg += 5;
  748. /* RFC 2616 Sec. 2.2 defines a list of allowed
  749. * separators, but many of them make no sense
  750. * here, e.g. various brackets or slashes.
  751. * If they are used, probably someone is
  752. * trying to attack with curious hand made
  753. * requests. Only ; , space and tab seem to be
  754. * reasonable here. Ignore everything else. */
  755. nend = nbeg + strcspn(nbeg, ",; \t");
  756. }
  757. /* Get the optional filename="..." part of the Content-Disposition
  758. * header. */
  759. fbeg = strstr(content_disp, "filename=\"");
  760. while ((fbeg != NULL) && (strcspn(fbeg - 1, ":,; \t") != 0)) {
  761. /* It could be somethingfilename= instead of filename= */
  762. fbeg = strstr(fbeg + 1, "filename=\"");
  763. }
  764. /* This line is not required, but otherwise some compilers
  765. * generate spurious warnings. */
  766. fend = fbeg;
  767. /* If filename=" is found, search for the closing " */
  768. if (fbeg) {
  769. fbeg += 10;
  770. fend = strchr(fbeg, '\"');
  771. if (!fend) {
  772. /* Malformed request (the filename field is optional, but if
  773. * it exists, it needs to be terminated correctly). */
  774. mg_free(boundary);
  775. return -1;
  776. }
  777. /* TODO: check Content-Type */
  778. /* Content-Type: application/octet-stream */
  779. }
  780. if (!fbeg) {
  781. /* Try the same without quotes */
  782. fbeg = strstr(content_disp, "filename=");
  783. while ((fbeg != NULL) && (strcspn(fbeg - 1, ":,; \t") != 0)) {
  784. /* It could be somethingfilename= instead of filename= */
  785. fbeg = strstr(fbeg + 1, "filename=");
  786. }
  787. if (fbeg) {
  788. fbeg += 9;
  789. fend = fbeg + strcspn(fbeg, ",; \t");
  790. }
  791. }
  792. if (!fbeg || !fend) {
  793. fbeg = NULL;
  794. fend = NULL;
  795. fnlen = 0;
  796. } else {
  797. fnlen = (size_t)(fend - fbeg);
  798. }
  799. /* In theory, it could be possible that someone crafts
  800. * a request like name=filename=xyz. Check if name and
  801. * filename do not overlap. */
  802. if (!(((ptrdiff_t)fbeg > (ptrdiff_t)nend)
  803. || ((ptrdiff_t)nbeg > (ptrdiff_t)fend))) {
  804. mg_free(boundary);
  805. return -1;
  806. }
  807. /* Call callback for new field */
  808. memset(path, 0, sizeof(path));
  809. field_count++;
  810. field_storage = url_encoded_field_found(conn,
  811. nbeg,
  812. (size_t)(nend - nbeg),
  813. ((fnlen > 0) ? fbeg : NULL),
  814. fnlen,
  815. path,
  816. sizeof(path) - 1,
  817. fdh);
  818. /* If the boundary is already in the buffer, get the address,
  819. * otherwise next will be NULL. */
  820. next = search_boundary(hbuf,
  821. (size_t)((buf - hbuf) + buf_fill),
  822. boundary,
  823. bl);
  824. #if !defined(NO_FILESYSTEMS)
  825. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  826. /* Store the content to a file */
  827. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  828. fstore.access.fp = NULL;
  829. }
  830. file_size = 0;
  831. if (!fstore.access.fp) {
  832. mg_cry_internal(conn,
  833. "%s: Cannot create file %s",
  834. __func__,
  835. path);
  836. }
  837. }
  838. #endif /* NO_FILESYSTEMS */
  839. get_block = 0;
  840. while (!next) {
  841. /* Set "towrite" to the number of bytes available
  842. * in the buffer */
  843. towrite = (size_t)(buf - hend + buf_fill);
  844. if (towrite < bl + 4) {
  845. /* Not enough data stored. */
  846. /* Incomplete request. */
  847. mg_free(boundary);
  848. return -1;
  849. }
  850. /* Subtract the boundary length, to deal with
  851. * cases the boundary is only partially stored
  852. * in the buffer. */
  853. towrite -= bl + 4;
  854. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  855. r = unencoded_field_get(conn,
  856. ((get_block > 0) ? NULL : nbeg),
  857. ((get_block > 0)
  858. ? 0
  859. : (size_t)(nend - nbeg)),
  860. hend,
  861. towrite,
  862. fdh);
  863. get_block++;
  864. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  865. /* Stop request handling */
  866. break;
  867. }
  868. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  869. /* Skip to next field */
  870. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  871. }
  872. }
  873. #if !defined(NO_FILESYSTEMS)
  874. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  875. if (fstore.access.fp) {
  876. /* Store the content of the buffer. */
  877. n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
  878. if ((n != towrite) || (ferror(fstore.access.fp))) {
  879. mg_cry_internal(conn,
  880. "%s: Cannot write file %s",
  881. __func__,
  882. path);
  883. mg_fclose(&fstore.access);
  884. remove_bad_file(conn, path);
  885. }
  886. file_size += (int64_t)n;
  887. }
  888. }
  889. #endif /* NO_FILESYSTEMS */
  890. memmove(buf, hend + towrite, bl + 4);
  891. buf_fill = bl + 4;
  892. hend = buf;
  893. /* Read new data */
  894. to_read = sizeof(buf) - 1 - buf_fill;
  895. r = mg_read(conn, buf + buf_fill, to_read);
  896. if ((r < 0) || ((r == 0) && all_data_read)) {
  897. #if !defined(NO_FILESYSTEMS)
  898. /* read error */
  899. if (fstore.access.fp) {
  900. mg_fclose(&fstore.access);
  901. remove_bad_file(conn, path);
  902. }
  903. #endif /* NO_FILESYSTEMS */
  904. mg_free(boundary);
  905. return -1;
  906. }
  907. /* r==0 already handled, all_data_read is false here */
  908. buf_fill += r;
  909. buf[buf_fill] = 0;
  910. /* buf_fill is at least 8 here */
  911. /* Find boundary */
  912. next = search_boundary(buf, buf_fill, boundary, bl);
  913. if (!next && (r == 0)) {
  914. /* incomplete request */
  915. all_data_read = 1;
  916. }
  917. }
  918. towrite = (next ? (size_t)(next - hend) : 0);
  919. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  920. /* Call callback */
  921. r = unencoded_field_get(conn,
  922. ((get_block > 0) ? NULL : nbeg),
  923. ((get_block > 0)
  924. ? 0
  925. : (size_t)(nend - nbeg)),
  926. hend,
  927. towrite,
  928. fdh);
  929. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  930. /* Stop request handling */
  931. break;
  932. }
  933. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  934. /* Skip to next field */
  935. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  936. }
  937. }
  938. #if !defined(NO_FILESYSTEMS)
  939. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  940. if (fstore.access.fp) {
  941. n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
  942. if ((n != towrite) || (ferror(fstore.access.fp))) {
  943. mg_cry_internal(conn,
  944. "%s: Cannot write file %s",
  945. __func__,
  946. path);
  947. mg_fclose(&fstore.access);
  948. remove_bad_file(conn, path);
  949. } else {
  950. file_size += (int64_t)n;
  951. r = mg_fclose(&fstore.access);
  952. if (r == 0) {
  953. /* stored successfully */
  954. r = field_stored(conn, path, file_size, fdh);
  955. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  956. /* Stop request handling */
  957. break;
  958. }
  959. } else {
  960. mg_cry_internal(conn,
  961. "%s: Error saving file %s",
  962. __func__,
  963. path);
  964. remove_bad_file(conn, path);
  965. }
  966. }
  967. fstore.access.fp = NULL;
  968. }
  969. }
  970. #endif /* NO_FILESYSTEMS */
  971. if ((field_storage & MG_FORM_FIELD_STORAGE_ABORT)
  972. == MG_FORM_FIELD_STORAGE_ABORT) {
  973. /* Stop parsing the request */
  974. break;
  975. }
  976. /* Remove from the buffer */
  977. if (next) {
  978. used = next - buf + 2;
  979. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  980. buf_fill -= used;
  981. } else {
  982. buf_fill = 0;
  983. }
  984. }
  985. /* All parts handled */
  986. mg_free(boundary);
  987. return field_count;
  988. }
  989. /* Unknown Content-Type */
  990. return -1;
  991. }
  992. /* End of handle_form.inl */