2011-10-03 16:59:38 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-02-27 15:48:38 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-10-03 16:59:38 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-10-03 16:59:38 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "rawstr.h"
|
2011-10-03 16:59:38 -04:00
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_mfiles.h"
|
|
|
|
#include "tool_msgs.h"
|
|
|
|
#include "tool_formparse.h"
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-10-03 16:59:38 -04:00
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* helper function to get a word from form param
|
|
|
|
* after call get_parm_word, str either point to string end
|
|
|
|
* or point to any of end chars.
|
|
|
|
*/
|
|
|
|
static char *get_param_word(char **str, char **end_pos)
|
|
|
|
{
|
|
|
|
char *ptr = *str;
|
|
|
|
char *word_begin = NULL;
|
|
|
|
char *ptr2;
|
|
|
|
char *escape = NULL;
|
|
|
|
const char *end_chars = ";,";
|
|
|
|
|
|
|
|
/* the first non-space char is here */
|
|
|
|
word_begin = ptr;
|
|
|
|
if(*ptr == '"') {
|
|
|
|
++ptr;
|
|
|
|
while(*ptr) {
|
|
|
|
if(*ptr == '\\') {
|
|
|
|
if(ptr[1] == '\\' || ptr[1] == '"') {
|
|
|
|
/* remember the first escape position */
|
|
|
|
if(!escape)
|
|
|
|
escape = ptr;
|
|
|
|
/* skip escape of back-slash or double-quote */
|
|
|
|
ptr += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(*ptr == '"') {
|
|
|
|
*end_pos = ptr;
|
|
|
|
if(escape) {
|
|
|
|
/* has escape, we restore the unescaped string here */
|
|
|
|
ptr = ptr2 = escape;
|
|
|
|
do {
|
|
|
|
if(*ptr == '\\' && (ptr[1] == '\\' || ptr[1] == '"'))
|
|
|
|
++ptr;
|
|
|
|
*ptr2++ = *ptr++;
|
|
|
|
}
|
|
|
|
while(ptr < *end_pos);
|
|
|
|
*end_pos = ptr2;
|
|
|
|
}
|
|
|
|
while(*ptr && NULL==strchr(end_chars, *ptr))
|
|
|
|
++ptr;
|
|
|
|
*str = ptr;
|
|
|
|
return word_begin+1;
|
|
|
|
}
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
/* end quote is missing, treat it as non-quoted. */
|
|
|
|
ptr = word_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*ptr && NULL==strchr(end_chars, *ptr))
|
|
|
|
++ptr;
|
|
|
|
*str = *end_pos = ptr;
|
|
|
|
return word_begin;
|
|
|
|
}
|
|
|
|
|
2011-10-03 16:59:38 -04:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* formparse()
|
|
|
|
*
|
|
|
|
* Reads a 'name=value' parameter and builds the appropriate linked list.
|
|
|
|
*
|
2013-01-21 17:20:09 -05:00
|
|
|
* Specify files to upload with 'name=@filename', or 'name=@"filename"'
|
|
|
|
* in case the filename contain ',' or ';'. Supports specified
|
2011-10-03 16:59:38 -04:00
|
|
|
* given Content-Type of the files. Such as ';type=<content-type>'.
|
|
|
|
*
|
|
|
|
* If literal_value is set, any initial '@' or '<' in the value string
|
|
|
|
* loses its special meaning, as does any embedded ';type='.
|
|
|
|
*
|
|
|
|
* You may specify more than one file for a single name (field). Specify
|
|
|
|
* multiple files by writing it like:
|
|
|
|
*
|
|
|
|
* 'name=@filename,filename2,filename3'
|
|
|
|
*
|
2013-01-21 17:20:09 -05:00
|
|
|
* or use double-quotes quote the filename:
|
|
|
|
*
|
|
|
|
* 'name=@"filename","filename2","filename3"'
|
|
|
|
*
|
2011-10-03 16:59:38 -04:00
|
|
|
* If you want content-types specified for each too, write them like:
|
|
|
|
*
|
|
|
|
* 'name=@filename;type=image/gif,filename2,filename3'
|
|
|
|
*
|
|
|
|
* If you want custom headers added for a single part, write them in a separate
|
|
|
|
* file and do like this:
|
|
|
|
*
|
|
|
|
* 'name=foo;headers=@headerfile' or why not
|
|
|
|
* 'name=@filemame;headers=@headerfile'
|
|
|
|
*
|
|
|
|
* To upload a file, but to fake the file name that will be included in the
|
|
|
|
* formpost, do like this:
|
|
|
|
*
|
2013-01-21 17:20:09 -05:00
|
|
|
* 'name=@filename;filename=/dev/null' or quote the faked filename like:
|
|
|
|
* 'name=@filename;filename="play, play, and play.txt"'
|
|
|
|
*
|
|
|
|
* If filename/path contains ',' or ';', it must be quoted by double-quotes,
|
|
|
|
* else curl will fail to figure out the correct filename. if the filename
|
|
|
|
* tobe quoted contains '"' or '\', '"' and '\' must be escaped by backslash.
|
2011-10-03 16:59:38 -04:00
|
|
|
*
|
|
|
|
* This function uses curl_formadd to fulfill it's job. Is heavily based on
|
|
|
|
* the old curl_formparse code.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2014-02-23 07:59:59 -05:00
|
|
|
int formparse(struct OperationConfig *config,
|
2011-10-03 16:59:38 -04:00
|
|
|
const char *input,
|
|
|
|
struct curl_httppost **httppost,
|
|
|
|
struct curl_httppost **last_post,
|
|
|
|
bool literal_value)
|
|
|
|
{
|
|
|
|
/* nextarg MUST be a string in the format 'name=contents' and we'll
|
|
|
|
build a linked list with the info */
|
|
|
|
char name[256];
|
|
|
|
char *contents = NULL;
|
2014-04-18 18:17:47 -04:00
|
|
|
char type_major[128] = "";
|
|
|
|
char type_minor[128] = "";
|
2011-10-03 16:59:38 -04:00
|
|
|
char *contp;
|
|
|
|
const char *type = NULL;
|
|
|
|
char *sep;
|
|
|
|
|
|
|
|
if((1 == sscanf(input, "%255[^=]=", name)) &&
|
|
|
|
((contp = strchr(input, '=')) != NULL)) {
|
|
|
|
/* the input was using the correct format */
|
|
|
|
|
|
|
|
/* Allocate the contents */
|
|
|
|
contents = strdup(contp+1);
|
|
|
|
if(!contents) {
|
2014-03-01 08:00:31 -05:00
|
|
|
fprintf(config->global->errors, "out of memory\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
contp = contents;
|
|
|
|
|
|
|
|
if('@' == contp[0] && !literal_value) {
|
|
|
|
|
|
|
|
/* we use the @-letter to indicate file name(s) */
|
|
|
|
|
|
|
|
struct multi_files *multi_start = NULL;
|
|
|
|
struct multi_files *multi_current = NULL;
|
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
char *ptr = contp;
|
|
|
|
char *end = ptr + strlen(ptr);
|
2011-10-03 16:59:38 -04:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* since this was a file, it may have a content-type specifier
|
|
|
|
at the end too, or a filename. Or both. */
|
|
|
|
char *filename = NULL;
|
2013-01-21 17:20:09 -05:00
|
|
|
char *word_end;
|
|
|
|
bool semicolon;
|
2011-10-03 16:59:38 -04:00
|
|
|
|
|
|
|
type = NULL;
|
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
++ptr;
|
|
|
|
contp = get_param_word(&ptr, &word_end);
|
|
|
|
semicolon = (';' == *ptr) ? TRUE : FALSE;
|
|
|
|
*word_end = '\0'; /* terminate the contp */
|
|
|
|
|
|
|
|
/* have other content, continue parse */
|
|
|
|
while(semicolon) {
|
|
|
|
/* have type or filename field */
|
|
|
|
++ptr;
|
|
|
|
while(*ptr && (ISSPACE(*ptr)))
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
if(checkprefix("type=", ptr)) {
|
|
|
|
/* set type pointer */
|
|
|
|
type = &ptr[5];
|
|
|
|
|
|
|
|
/* verify that this is a fine type specifier */
|
|
|
|
if(2 != sscanf(type, "%127[^/]/%127[^;,\n]",
|
|
|
|
type_major, type_minor)) {
|
2015-02-28 04:53:33 -05:00
|
|
|
warnf(config->global,
|
|
|
|
"Illegally formatted content-type field!\n");
|
2013-01-21 17:20:09 -05:00
|
|
|
Curl_safefree(contents);
|
|
|
|
FreeMultiInfo(&multi_start, &multi_current);
|
|
|
|
return 2; /* illegal content-type syntax! */
|
2011-10-03 16:59:38 -04:00
|
|
|
}
|
2013-01-21 17:20:09 -05:00
|
|
|
|
|
|
|
/* now point beyond the content-type specifier */
|
|
|
|
sep = (char *)type + strlen(type_major)+strlen(type_minor)+1;
|
|
|
|
|
|
|
|
/* there's a semicolon following - we check if it is a filename
|
|
|
|
specified and if not we simply assume that it is text that
|
|
|
|
the user wants included in the type and include that too up
|
|
|
|
to the next sep. */
|
|
|
|
ptr = sep;
|
|
|
|
if(*sep==';') {
|
|
|
|
if(!checkprefix(";filename=", sep)) {
|
|
|
|
ptr = sep + 1;
|
|
|
|
(void)get_param_word(&ptr, &sep);
|
|
|
|
semicolon = (';' == *ptr) ? TRUE : FALSE;
|
2011-10-03 16:59:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-01-21 17:20:09 -05:00
|
|
|
semicolon = FALSE;
|
2011-12-16 05:43:25 -05:00
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
if(*sep)
|
|
|
|
*sep = '\0'; /* zero terminate type string */
|
|
|
|
}
|
|
|
|
else if(checkprefix("filename=", ptr)) {
|
|
|
|
ptr += 9;
|
|
|
|
filename = get_param_word(&ptr, &word_end);
|
|
|
|
semicolon = (';' == *ptr) ? TRUE : FALSE;
|
|
|
|
*word_end = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* unknown prefix, skip to next block */
|
|
|
|
char *unknown = NULL;
|
|
|
|
unknown = get_param_word(&ptr, &word_end);
|
|
|
|
semicolon = (';' == *ptr) ? TRUE : FALSE;
|
|
|
|
if(*unknown) {
|
|
|
|
*word_end = '\0';
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "skip unknown form field: %s\n", unknown);
|
2013-01-21 17:20:09 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-03 16:59:38 -04:00
|
|
|
}
|
2013-01-21 17:20:09 -05:00
|
|
|
/* now ptr point to comma or string end */
|
|
|
|
|
2011-12-16 05:43:25 -05:00
|
|
|
|
2011-10-03 16:59:38 -04:00
|
|
|
/* if type == NULL curl_formadd takes care of the problem */
|
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
if(*contp && !AddMultiFiles(contp, type, filename, &multi_start,
|
2011-10-03 16:59:38 -04:00
|
|
|
&multi_current)) {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "Error building form post!\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
Curl_safefree(contents);
|
|
|
|
FreeMultiInfo(&multi_start, &multi_current);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
/* *ptr could be '\0', so we just check with the string end */
|
|
|
|
} while(ptr < end); /* loop if there's another file name */
|
2011-10-03 16:59:38 -04:00
|
|
|
|
|
|
|
/* now we add the multiple files section */
|
|
|
|
if(multi_start) {
|
|
|
|
struct curl_forms *forms = NULL;
|
2013-01-21 17:20:09 -05:00
|
|
|
struct multi_files *start = multi_start;
|
2011-10-03 16:59:38 -04:00
|
|
|
unsigned int i, count = 0;
|
2013-01-21 17:20:09 -05:00
|
|
|
while(start) {
|
|
|
|
start = start->next;
|
2011-10-03 16:59:38 -04:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
forms = malloc((count+1)*sizeof(struct curl_forms));
|
|
|
|
if(!forms) {
|
2014-03-01 08:00:31 -05:00
|
|
|
fprintf(config->global->errors, "Error building form post!\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
Curl_safefree(contents);
|
|
|
|
FreeMultiInfo(&multi_start, &multi_current);
|
|
|
|
return 4;
|
|
|
|
}
|
2013-01-21 17:20:09 -05:00
|
|
|
for(i = 0, start = multi_start; i < count; ++i, start = start->next) {
|
|
|
|
forms[i].option = start->form.option;
|
|
|
|
forms[i].value = start->form.value;
|
2011-10-03 16:59:38 -04:00
|
|
|
}
|
|
|
|
forms[count].option = CURLFORM_END;
|
|
|
|
FreeMultiInfo(&multi_start, &multi_current);
|
|
|
|
if(curl_formadd(httppost, last_post,
|
|
|
|
CURLFORM_COPYNAME, name,
|
|
|
|
CURLFORM_ARRAY, forms, CURLFORM_END) != 0) {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "curl_formadd failed!\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
Curl_safefree(forms);
|
|
|
|
Curl_safefree(contents);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
Curl_safefree(forms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct curl_forms info[4];
|
|
|
|
int i = 0;
|
|
|
|
char *ct = literal_value ? NULL : strstr(contp, ";type=");
|
|
|
|
|
|
|
|
info[i].option = CURLFORM_COPYNAME;
|
|
|
|
info[i].value = name;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if(ct) {
|
|
|
|
info[i].option = CURLFORM_CONTENTTYPE;
|
|
|
|
info[i].value = &ct[6];
|
|
|
|
i++;
|
|
|
|
ct[0] = '\0'; /* zero terminate here */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(contp[0]=='<' && !literal_value) {
|
|
|
|
info[i].option = CURLFORM_FILECONTENT;
|
|
|
|
info[i].value = contp+1;
|
|
|
|
i++;
|
|
|
|
info[i].option = CURLFORM_END;
|
|
|
|
|
|
|
|
if(curl_formadd(httppost, last_post,
|
|
|
|
CURLFORM_ARRAY, info, CURLFORM_END ) != 0) {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "curl_formadd failed, possibly the file %s is "
|
|
|
|
"bad!\n", contp + 1);
|
2011-10-03 16:59:38 -04:00
|
|
|
Curl_safefree(contents);
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
|
|
|
if(convert_to_network(contp, strlen(contp))) {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "curl_formadd failed!\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
Curl_safefree(contents);
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
info[i].option = CURLFORM_COPYCONTENTS;
|
|
|
|
info[i].value = contp;
|
|
|
|
i++;
|
|
|
|
info[i].option = CURLFORM_END;
|
|
|
|
if(curl_formadd(httppost, last_post,
|
|
|
|
CURLFORM_ARRAY, info, CURLFORM_END) != 0) {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "curl_formadd failed!\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
Curl_safefree(contents);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(config->global, "Illegally formatted input field!\n");
|
2011-10-03 16:59:38 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Curl_safefree(contents);
|
|
|
|
return 0;
|
|
|
|
}
|