handle_form.inl 30 KB

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