handle_form.inl 30 KB

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