1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
27 #define ENABLE_CURLX_PRINTF
28 /* use our own printf() functions */
31 #include "tool_cfgable.h"
32 #include "tool_convert.h"
33 #include "tool_msgs.h"
34 #include "tool_binmode.h"
35 #include "tool_getparam.h"
36 #include "tool_paramhlp.h"
37 #include "tool_formparse.h"
39 #include "memdebug.h" /* keep this as LAST include */
41 /* Stdin parameters. */
43 char *data; /* Memory data. */
44 curl_off_t origin; /* File read origin offset. */
45 curl_off_t size; /* Data size. */
46 curl_off_t curpos; /* Current read position. */
51 * helper function to get a word from form param
52 * after call get_parm_word, str either point to string end
53 * or point to any of end chars.
55 static char *get_param_word(char **str, char **end_pos)
58 char *word_begin = NULL;
61 const char *end_chars = ";,";
63 /* the first non-space char is here */
69 if(ptr[1] == '\\' || ptr[1] == '"') {
70 /* remember the first escape position */
73 /* skip escape of back-slash or double-quote */
81 /* has escape, we restore the unescaped string here */
84 if(*ptr == '\\' && (ptr[1] == '\\' || ptr[1] == '"'))
88 while(ptr < *end_pos);
91 while(*ptr && NULL == strchr(end_chars, *ptr))
94 return word_begin + 1;
98 /* end quote is missing, treat it as non-quoted. */
102 while(*ptr && NULL == strchr(end_chars, *ptr))
104 *str = *end_pos = ptr;
108 /* Append slist item and return -1 if failed. */
109 static int slist_append(struct curl_slist **plist, const char *data)
111 struct curl_slist *s = curl_slist_append(*plist, data);
120 /* Read headers from a file and append to list. */
121 static int read_field_headers(struct OperationConfig *config,
122 const char *filename, FILE *fp,
123 struct curl_slist **pheaders)
128 bool incomment = FALSE;
130 char hdrbuf[999]; /* Max. header length + 1. */
134 if(c == EOF || (!pos && !ISSPACE(c))) {
135 /* Strip and flush the current header. */
136 while(hdrlen && ISSPACE(hdrbuf[hdrlen - 1]))
139 hdrbuf[hdrlen] = '\0';
140 if(slist_append(pheaders, hdrbuf)) {
141 fprintf(config->global->errors,
142 "Out of memory for field headers!\n");
152 fprintf(config->global->errors,
153 "Header file %s read error: %s\n", filename, strerror(errno));
156 return 0; /* Done. */
158 continue; /* Ignore. */
172 if(hdrlen == sizeof hdrbuf - 1) {
173 warnf(config->global, "File %s line %d: header too long (truncated)\n",
177 if(hdrlen <= sizeof hdrbuf - 1)
178 hdrbuf[hdrlen++] = (char) c;
184 static int get_param_part(struct OperationConfig *config, char **str,
185 char **pdata, char **ptype, char **pfilename,
186 char **pencoder, struct curl_slist **pheaders)
190 char *filename = NULL;
191 char *encoder = NULL;
195 char type_major[128] = "";
196 char type_minor[128] = "";
198 struct curl_slist *headers = NULL;
211 *pdata = get_param_word(&p, &endpos);
212 /* If not quoted, strip trailing spaces. */
214 while(endpos > *pdata && ISSPACE(endpos[-1]))
222 if(!endct && checkprefix("type=", p)) {
223 for(p += 5; ISSPACE(*p); p++)
225 /* set type pointer */
228 /* verify that this is a fine type specifier */
229 if(2 != sscanf(type, "%127[^/ ]/%127[^;, \n]", type_major, type_minor)) {
230 warnf(config->global, "Illegally formatted content-type field!\n");
231 curl_slist_free_all(headers);
232 return -1; /* illegal content-type syntax! */
235 /* now point beyond the content-type specifier */
236 endpos = type + strlen(type_major) + strlen(type_minor) + 1;
237 for(p = endpos; ISSPACE(*p); p++)
239 while(*p && *p != ';' && *p != ',')
244 else if(checkprefix("filename=", p)) {
249 for(p += 9; ISSPACE(*p); p++)
252 filename = get_param_word(&p, &endpos);
253 /* If not quoted, strip trailing spaces. */
255 while(endpos > filename && ISSPACE(endpos[-1]))
260 else if(checkprefix("headers=", p)) {
266 if(*p == '@' || *p == '<') {
269 /* Read headers from a file. */
273 } while(ISSPACE(*p));
275 hdrfile = get_param_word(&p, &endpos);
276 /* If not quoted, strip trailing spaces. */
278 while(endpos > hdrfile && ISSPACE(endpos[-1]))
282 /* TODO: maybe special fopen for VMS? */
283 fp = fopen(hdrfile, FOPEN_READTEXT);
285 warnf(config->global, "Cannot read from %s: %s\n", hdrfile,
288 int i = read_field_headers(config, hdrfile, fp, &headers);
292 curl_slist_free_all(headers);
303 hdr = get_param_word(&p, &endpos);
304 /* If not quoted, strip trailing spaces. */
306 while(endpos > hdr && ISSPACE(endpos[-1]))
310 if(slist_append(&headers, hdr)) {
311 fprintf(config->global->errors, "Out of memory for field header!\n");
312 curl_slist_free_all(headers);
317 else if(checkprefix("encoder=", p)) {
322 for(p += 8; ISSPACE(*p); p++)
325 encoder = get_param_word(&p, &endpos);
326 /* If not quoted, strip trailing spaces. */
328 while(endpos > encoder && ISSPACE(endpos[-1]))
334 /* unknown prefix, skip to next block */
335 char *unknown = get_param_word(&p, &endpos);
343 warnf(config->global, "skip unknown form field: %s\n", unknown);
348 /* Terminate and strip content type. */
351 endct = type + strlen(type);
352 while(endct > type && ISSPACE(endct[-1]))
360 warnf(config->global, "Field content type not allowed here: %s\n", type);
363 *pfilename = filename;
365 warnf(config->global,
366 "Field file name not allowed here: %s\n", filename);
371 warnf(config->global,
372 "Field encoder not allowed here: %s\n", encoder);
377 warnf(config->global,
378 "Field headers not allowed here: %s\n", headers->data);
379 curl_slist_free_all(headers);
387 /* Mime part callbacks for stdin. */
388 static size_t stdin_read(char *buffer, size_t size, size_t nitems, void *arg)
390 standard_input *sip = (standard_input *) arg;
391 curl_off_t bytesleft;
392 (void) size; /* Always 1: ignored. */
394 if(sip->curpos >= sip->size)
395 return 0; /* At eof. */
396 bytesleft = sip->size - sip->curpos;
397 if((curl_off_t) nitems > bytesleft)
398 nitems = (size_t) bytesleft;
400 /* Return data from memory. */
401 memcpy(buffer, sip->data + (size_t) sip->curpos, nitems);
404 /* Read from stdin. */
405 nitems = fread(buffer, 1, nitems, stdin);
407 sip->curpos += nitems;
411 static int stdin_seek(void *instream, curl_off_t offset, int whence)
413 standard_input *sip = (standard_input *) instream;
417 offset += sip->curpos;
424 return CURL_SEEKFUNC_CANTSEEK;
426 if(fseek(stdin, (long) (offset + sip->origin), SEEK_SET))
427 return CURL_SEEKFUNC_CANTSEEK;
429 sip->curpos = offset;
430 return CURL_SEEKFUNC_OK;
433 static void stdin_free(void *ptr)
435 standard_input *sip = (standard_input *) ptr;
437 Curl_safefree(sip->data);
441 /* Set a part's data from a file, taking care about the pseudo filename "-" as
442 * a shortcut to read stdin: if so, use a callback to read OUR stdin (to
443 * workaround Windows DLL file handle caveat).
444 * If stdin is a regular file opened in binary mode, save current offset as
445 * origin for rewind and do not buffer data. Else read to EOF and keep in
446 * memory. In all cases, compute the stdin data size.
448 static CURLcode file_or_stdin(curl_mimepart *part, const char *file)
450 standard_input *sip = NULL;
452 CURLcode result = CURLE_OK;
455 if(strcmp(file, "-"))
456 return curl_mime_filedata(part, file);
458 sip = (standard_input *) malloc(sizeof *sip);
460 return CURLE_OUT_OF_MEMORY;
462 memset((char *) sip, 0, sizeof *sip);
465 /* If stdin is a regular file, do not buffer data but read it when needed. */
467 sip->origin = ftell(stdin);
468 if(fd >= 0 && sip->origin >= 0 && !fstat(fd, &sbuf) &&
470 sbuf.st_fab_rfm != FAB$C_VAR && sbuf.st_fab_rfm != FAB$C_VFC &&
472 S_ISREG(sbuf.st_mode)) {
473 sip->size = sbuf.st_size - sip->origin;
477 else { /* Not suitable for direct use, buffer stdin data. */
478 size_t stdinsize = 0;
481 if(file2memory(&sip->data, &stdinsize, stdin) != PARAM_OK)
482 result = CURLE_OUT_OF_MEMORY;
485 sip->data = NULL; /* Has been freed if no data. */
486 sip->size = stdinsize;
488 result = CURLE_READ_ERROR;
492 /* Set remote file name. */
494 result = curl_mime_filename(part, file);
496 /* Set part's data from callback. */
498 result = curl_mime_data_cb(part, sip->size,
499 stdin_read, stdin_seek, stdin_free, sip);
506 /***************************************************************************
510 * Reads a 'name=value' parameter and builds the appropriate linked list.
512 * Specify files to upload with 'name=@filename', or 'name=@"filename"'
513 * in case the filename contain ',' or ';'. Supports specified
514 * given Content-Type of the files. Such as ';type=<content-type>'.
516 * If literal_value is set, any initial '@' or '<' in the value string
517 * loses its special meaning, as does any embedded ';type='.
519 * You may specify more than one file for a single name (field). Specify
520 * multiple files by writing it like:
522 * 'name=@filename,filename2,filename3'
524 * or use double-quotes quote the filename:
526 * 'name=@"filename","filename2","filename3"'
528 * If you want content-types specified for each too, write them like:
530 * 'name=@filename;type=image/gif,filename2,filename3'
532 * If you want custom headers added for a single part, write them in a separate
533 * file and do like this:
535 * 'name=foo;headers=@headerfile' or why not
536 * 'name=@filemame;headers=@headerfile'
538 * To upload a file, but to fake the file name that will be included in the
539 * formpost, do like this:
541 * 'name=@filename;filename=/dev/null' or quote the faked filename like:
542 * 'name=@filename;filename="play, play, and play.txt"'
544 * If filename/path contains ',' or ';', it must be quoted by double-quotes,
545 * else curl will fail to figure out the correct filename. if the filename
546 * tobe quoted contains '"' or '\', '"' and '\' must be escaped by backslash.
548 * This function uses curl_formadd to fulfill it's job. Is heavily based on
549 * the old curl_formparse code.
551 ***************************************************************************/
553 int formparse(struct OperationConfig *config,
555 curl_mime **mimepost,
556 curl_mime **mimecurrent,
559 /* input MUST be a string in the format 'name=contents' and we'll
560 build a linked list with the info */
562 char *contents = NULL;
566 char *filename = NULL;
567 char *encoder = NULL;
568 struct curl_slist *headers = NULL;
569 curl_mimepart *part = NULL;
573 /* Allocate the main mime structure if needed. */
575 *mimepost = curl_mime_init(config->easy);
577 warnf(config->global, "curl_mime_init failed!\n");
580 *mimecurrent = *mimepost;
583 /* Make a copy we can overwrite. */
584 contents = strdup(input);
586 fprintf(config->global->errors, "out of memory\n");
590 /* Scan for the end of the name. */
591 contp = strchr(contents, '=');
597 if(*contp == '(' && !literal_value) {
600 /* Starting a multipart. */
601 sep = get_param_part(config, &contp, &data, &type, NULL, NULL, &headers);
603 Curl_safefree(contents);
606 subparts = curl_mime_init(config->easy);
608 warnf(config->global, "curl_mime_init failed!\n");
609 curl_slist_free_all(headers);
610 Curl_safefree(contents);
613 part = curl_mime_addpart(*mimecurrent);
615 warnf(config->global, "curl_mime_addpart failed!\n");
616 curl_mime_free(subparts);
617 curl_slist_free_all(headers);
618 Curl_safefree(contents);
621 if(curl_mime_subparts(part, subparts)) {
622 warnf(config->global, "curl_mime_subparts failed!\n");
623 curl_mime_free(subparts);
624 curl_slist_free_all(headers);
625 Curl_safefree(contents);
628 *mimecurrent = subparts;
629 if(curl_mime_headers(part, headers, 1)) {
630 warnf(config->global, "curl_mime_headers failed!\n");
631 curl_slist_free_all(headers);
632 Curl_safefree(contents);
635 if(curl_mime_type(part, type)) {
636 warnf(config->global, "curl_mime_type failed!\n");
637 Curl_safefree(contents);
641 else if(!name && !strcmp(contp, ")") && !literal_value) {
642 /* Ending a mutipart. */
643 if(*mimecurrent == *mimepost) {
644 warnf(config->global, "no multipart to terminate!\n");
645 Curl_safefree(contents);
648 *mimecurrent = (*mimecurrent)->parent->parent;
650 else if('@' == contp[0] && !literal_value) {
652 /* we use the @-letter to indicate file name(s) */
654 curl_mime *subparts = NULL;
657 /* since this was a file, it may have a content-type specifier
658 at the end too, or a filename. Or both. */
660 sep = get_param_part(config, &contp,
661 &data, &type, &filename, &encoder, &headers);
663 if(subparts != *mimecurrent)
664 curl_mime_free(subparts);
665 Curl_safefree(contents);
669 /* now contp point to comma or string end.
670 If more files to come, make sure we have multiparts. */
672 if(sep != ',') /* If there is a single file. */
673 subparts = *mimecurrent;
675 subparts = curl_mime_init(config->easy);
677 warnf(config->global, "curl_mime_init failed!\n");
678 curl_slist_free_all(headers);
679 Curl_safefree(contents);
685 /* Allocate a part for that file. */
686 part = curl_mime_addpart(subparts);
688 warnf(config->global, "curl_mime_addpart failed!\n");
689 if(subparts != *mimecurrent)
690 curl_mime_free(subparts);
691 curl_slist_free_all(headers);
692 Curl_safefree(contents);
696 /* Set part headers. */
697 if(curl_mime_headers(part, headers, 1)) {
698 warnf(config->global, "curl_mime_headers failed!\n");
699 if(subparts != *mimecurrent)
700 curl_mime_free(subparts);
701 curl_slist_free_all(headers);
702 Curl_safefree(contents);
706 /* Setup file in part. */
707 res = file_or_stdin(part, data);
709 warnf(config->global, "setting file %s failed!\n", data);
710 if(res != CURLE_READ_ERROR) {
711 if(subparts != *mimecurrent)
712 curl_mime_free(subparts);
713 Curl_safefree(contents);
717 if(filename && curl_mime_filename(part, filename)) {
718 warnf(config->global, "curl_mime_filename failed!\n");
719 if(subparts != *mimecurrent)
720 curl_mime_free(subparts);
721 Curl_safefree(contents);
724 if(curl_mime_type(part, type)) {
725 warnf(config->global, "curl_mime_type failed!\n");
726 if(subparts != *mimecurrent)
727 curl_mime_free(subparts);
728 Curl_safefree(contents);
731 if(curl_mime_encoder(part, encoder)) {
732 warnf(config->global, "curl_mime_encoder failed!\n");
733 if(subparts != *mimecurrent)
734 curl_mime_free(subparts);
735 Curl_safefree(contents);
739 /* *contp could be '\0', so we just check with the delimiter */
740 } while(sep); /* loop if there's another file name */
742 /* now we add the multiple files section */
743 if(subparts != *mimecurrent) {
744 part = curl_mime_addpart(*mimecurrent);
746 warnf(config->global, "curl_mime_addpart failed!\n");
747 curl_mime_free(subparts);
748 Curl_safefree(contents);
751 if(curl_mime_subparts(part, subparts)) {
752 warnf(config->global, "curl_mime_subparts failed!\n");
753 curl_mime_free(subparts);
754 Curl_safefree(contents);
760 /* Allocate a mime part. */
761 part = curl_mime_addpart(*mimecurrent);
763 warnf(config->global, "curl_mime_addpart failed!\n");
764 Curl_safefree(contents);
768 if(*contp == '<' && !literal_value) {
770 sep = get_param_part(config, &contp,
771 &data, &type, NULL, &encoder, &headers);
773 Curl_safefree(contents);
777 /* Set part headers. */
778 if(curl_mime_headers(part, headers, 1)) {
779 warnf(config->global, "curl_mime_headers failed!\n");
780 curl_slist_free_all(headers);
781 Curl_safefree(contents);
785 /* Setup file in part. */
786 res = file_or_stdin(part, data);
788 warnf(config->global, "setting file %s failed!\n", data);
789 if(res != CURLE_READ_ERROR) {
790 Curl_safefree(contents);
799 sep = get_param_part(config, &contp,
800 &data, &type, &filename, &encoder, &headers);
802 Curl_safefree(contents);
807 /* Set part headers. */
808 if(curl_mime_headers(part, headers, 1)) {
809 warnf(config->global, "curl_mime_headers failed!\n");
810 curl_slist_free_all(headers);
811 Curl_safefree(contents);
815 #ifdef CURL_DOES_CONVERSIONS
816 if(convert_to_network(data, strlen(data))) {
817 warnf(config->global, "curl_formadd failed!\n");
818 Curl_safefree(contents);
823 if(curl_mime_data(part, data, CURL_ZERO_TERMINATED)) {
824 warnf(config->global, "curl_mime_data failed!\n");
825 Curl_safefree(contents);
830 if(curl_mime_filename(part, filename)) {
831 warnf(config->global, "curl_mime_filename failed!\n");
832 Curl_safefree(contents);
835 if(curl_mime_type(part, type)) {
836 warnf(config->global, "curl_mime_type failed!\n");
837 Curl_safefree(contents);
840 if(curl_mime_encoder(part, encoder)) {
841 warnf(config->global, "curl_mime_encoder failed!\n");
842 Curl_safefree(contents);
848 warnf(config->global,
849 "garbage at end of field specification: %s\n", contp);
854 if(name && curl_mime_name(part, name)) {
855 warnf(config->global, "curl_mime_name failed!\n");
856 Curl_safefree(contents);
861 warnf(config->global, "Illegally formatted input field!\n");
862 Curl_safefree(contents);
865 Curl_safefree(contents);