/src/PROJ/curl/lib/formdata.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
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.se/docs/copyright.html. |
13 | | * |
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. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include <curl/curl.h> |
28 | | |
29 | | struct Curl_easy; |
30 | | |
31 | | #include "formdata.h" |
32 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_FORM_API) |
33 | | |
34 | | #include "urldata.h" /* for struct Curl_easy */ |
35 | | #include "mime.h" |
36 | | #include "vtls/vtls.h" |
37 | | #include "sendf.h" |
38 | | #include "strdup.h" |
39 | | #include "rand.h" |
40 | | #include "curlx/warnless.h" |
41 | | /* The last 3 #include files should be in this order */ |
42 | | #include "curl_printf.h" |
43 | | #include "curl_memory.h" |
44 | | #include "memdebug.h" |
45 | | |
46 | | |
47 | 0 | #define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME |
48 | 0 | #define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME |
49 | 0 | #define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS |
50 | 0 | #define HTTPPOST_READFILE CURL_HTTPPOST_READFILE |
51 | 0 | #define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER |
52 | 0 | #define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK |
53 | 0 | #define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER |
54 | | |
55 | | /*************************************************************************** |
56 | | * |
57 | | * AddHttpPost() |
58 | | * |
59 | | * Adds an HttpPost structure to the list, if parent_post is given becomes |
60 | | * a subpost of parent_post instead of a direct list element. |
61 | | * |
62 | | * Returns newly allocated HttpPost on success and NULL if malloc failed. |
63 | | * |
64 | | ***************************************************************************/ |
65 | | static struct curl_httppost * |
66 | | AddHttpPost(struct FormInfo *src, |
67 | | struct curl_httppost *parent_post, |
68 | | struct curl_httppost **httppost, |
69 | | struct curl_httppost **last_post) |
70 | 0 | { |
71 | 0 | struct curl_httppost *post; |
72 | 0 | size_t namelength = src->namelength; |
73 | 0 | if(!namelength && src->name) |
74 | 0 | namelength = strlen(src->name); |
75 | 0 | if((src->bufferlength > LONG_MAX) || (namelength > LONG_MAX)) |
76 | | /* avoid overflow in typecasts below */ |
77 | 0 | return NULL; |
78 | 0 | post = calloc(1, sizeof(struct curl_httppost)); |
79 | 0 | if(post) { |
80 | 0 | post->name = src->name; |
81 | 0 | post->namelength = (long)namelength; |
82 | 0 | post->contents = src->value; |
83 | 0 | post->contentlen = src->contentslength; |
84 | 0 | post->buffer = src->buffer; |
85 | 0 | post->bufferlength = (long)src->bufferlength; |
86 | 0 | post->contenttype = src->contenttype; |
87 | 0 | post->flags = src->flags | CURL_HTTPPOST_LARGE; |
88 | 0 | post->contentheader = src->contentheader; |
89 | 0 | post->showfilename = src->showfilename; |
90 | 0 | post->userp = src->userp; |
91 | 0 | } |
92 | 0 | else |
93 | 0 | return NULL; |
94 | | |
95 | 0 | if(parent_post) { |
96 | | /* now, point our 'more' to the original 'more' */ |
97 | 0 | post->more = parent_post->more; |
98 | | |
99 | | /* then move the original 'more' to point to ourselves */ |
100 | 0 | parent_post->more = post; |
101 | 0 | } |
102 | 0 | else { |
103 | | /* make the previous point to this */ |
104 | 0 | if(*last_post) |
105 | 0 | (*last_post)->next = post; |
106 | 0 | else |
107 | 0 | (*httppost) = post; |
108 | |
|
109 | 0 | (*last_post) = post; |
110 | 0 | } |
111 | 0 | return post; |
112 | 0 | } |
113 | | |
114 | | /*************************************************************************** |
115 | | * |
116 | | * AddFormInfo() |
117 | | * |
118 | | * Adds a FormInfo structure to the list presented by parent_form_info. |
119 | | * |
120 | | * Returns newly allocated FormInfo on success and NULL if malloc failed/ |
121 | | * parent_form_info is NULL. |
122 | | * |
123 | | ***************************************************************************/ |
124 | | static struct FormInfo *AddFormInfo(char *value, |
125 | | char *contenttype, |
126 | | struct FormInfo *parent_form_info) |
127 | 0 | { |
128 | 0 | struct FormInfo *form_info; |
129 | 0 | form_info = calloc(1, sizeof(struct FormInfo)); |
130 | 0 | if(!form_info) |
131 | 0 | return NULL; |
132 | 0 | if(value) |
133 | 0 | form_info->value = value; |
134 | 0 | if(contenttype) |
135 | 0 | form_info->contenttype = contenttype; |
136 | 0 | form_info->flags = HTTPPOST_FILENAME; |
137 | |
|
138 | 0 | if(parent_form_info) { |
139 | | /* now, point our 'more' to the original 'more' */ |
140 | 0 | form_info->more = parent_form_info->more; |
141 | | |
142 | | /* then move the original 'more' to point to ourselves */ |
143 | 0 | parent_form_info->more = form_info; |
144 | 0 | } |
145 | |
|
146 | 0 | return form_info; |
147 | 0 | } |
148 | | |
149 | | static void free_formlist(struct FormInfo *ptr) |
150 | 0 | { |
151 | 0 | for(; ptr != NULL; ptr = ptr->more) { |
152 | 0 | if(ptr->name_alloc) { |
153 | 0 | Curl_safefree(ptr->name); |
154 | 0 | ptr->name_alloc = FALSE; |
155 | 0 | } |
156 | 0 | if(ptr->value_alloc) { |
157 | 0 | Curl_safefree(ptr->value); |
158 | 0 | ptr->value_alloc = FALSE; |
159 | 0 | } |
160 | 0 | if(ptr->contenttype_alloc) { |
161 | 0 | Curl_safefree(ptr->contenttype); |
162 | 0 | ptr->contenttype_alloc = FALSE; |
163 | 0 | } |
164 | 0 | if(ptr->showfilename_alloc) { |
165 | 0 | Curl_safefree(ptr->showfilename); |
166 | 0 | ptr->showfilename_alloc = FALSE; |
167 | 0 | } |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | | /*************************************************************************** |
172 | | * |
173 | | * FormAdd() |
174 | | * |
175 | | * Stores a formpost parameter and builds the appropriate linked list. |
176 | | * |
177 | | * Has two principal functionalities: using files and byte arrays as |
178 | | * post parts. Byte arrays are either copied or just the pointer is stored |
179 | | * (as the user requests) while for files only the filename and not the |
180 | | * content is stored. |
181 | | * |
182 | | * While you may have only one byte array for each name, multiple filenames |
183 | | * are allowed (and because of this feature CURLFORM_END is needed after |
184 | | * using CURLFORM_FILE). |
185 | | * |
186 | | * Examples: |
187 | | * |
188 | | * Simple name/value pair with copied contents: |
189 | | * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name", |
190 | | * CURLFORM_COPYCONTENTS, "value", CURLFORM_END); |
191 | | * |
192 | | * name/value pair where only the content pointer is remembered: |
193 | | * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name", |
194 | | * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END); |
195 | | * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used) |
196 | | * |
197 | | * storing a filename (CONTENTTYPE is optional!): |
198 | | * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name", |
199 | | * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text", |
200 | | * CURLFORM_END); |
201 | | * |
202 | | * storing multiple filenames: |
203 | | * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name", |
204 | | * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END); |
205 | | * |
206 | | * Returns: |
207 | | * CURL_FORMADD_OK on success |
208 | | * CURL_FORMADD_MEMORY if the FormInfo allocation fails |
209 | | * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form |
210 | | * CURL_FORMADD_NULL if a null pointer was given for a char |
211 | | * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed |
212 | | * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used |
213 | | * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error) |
214 | | * CURL_FORMADD_MEMORY if an HttpPost struct cannot be allocated |
215 | | * CURL_FORMADD_MEMORY if some allocation for string copying failed. |
216 | | * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array |
217 | | * |
218 | | ***************************************************************************/ |
219 | | |
220 | | static CURLFORMcode FormAddCheck(struct FormInfo *first_form, |
221 | | struct curl_httppost **httppost, |
222 | | struct curl_httppost **last_post) |
223 | 0 | { |
224 | 0 | const char *prevtype = NULL; |
225 | 0 | struct FormInfo *form = NULL; |
226 | 0 | struct curl_httppost *post = NULL; |
227 | | |
228 | | /* go through the list, check for completeness and if everything is |
229 | | * alright add the HttpPost item otherwise set retval accordingly */ |
230 | |
|
231 | 0 | for(form = first_form; |
232 | 0 | form != NULL; |
233 | 0 | form = form->more) { |
234 | 0 | if(((!form->name || !form->value) && !post) || |
235 | 0 | ( (form->contentslength) && |
236 | 0 | (form->flags & HTTPPOST_FILENAME) ) || |
237 | 0 | ( (form->flags & HTTPPOST_FILENAME) && |
238 | 0 | (form->flags & HTTPPOST_PTRCONTENTS) ) || |
239 | |
|
240 | 0 | ( (!form->buffer) && |
241 | 0 | (form->flags & HTTPPOST_BUFFER) && |
242 | 0 | (form->flags & HTTPPOST_PTRBUFFER) ) || |
243 | |
|
244 | 0 | ( (form->flags & HTTPPOST_READFILE) && |
245 | 0 | (form->flags & HTTPPOST_PTRCONTENTS) ) |
246 | 0 | ) { |
247 | 0 | return CURL_FORMADD_INCOMPLETE; |
248 | 0 | } |
249 | 0 | if(((form->flags & HTTPPOST_FILENAME) || |
250 | 0 | (form->flags & HTTPPOST_BUFFER)) && |
251 | 0 | !form->contenttype) { |
252 | 0 | char *f = (form->flags & HTTPPOST_BUFFER) ? |
253 | 0 | form->showfilename : form->value; |
254 | 0 | char const *type; |
255 | 0 | type = Curl_mime_contenttype(f); |
256 | 0 | if(!type) |
257 | 0 | type = prevtype; |
258 | 0 | if(!type) |
259 | 0 | type = FILE_CONTENTTYPE_DEFAULT; |
260 | | |
261 | | /* our contenttype is missing */ |
262 | 0 | form->contenttype = strdup(type); |
263 | 0 | if(!form->contenttype) |
264 | 0 | return CURL_FORMADD_MEMORY; |
265 | | |
266 | 0 | form->contenttype_alloc = TRUE; |
267 | 0 | } |
268 | 0 | if(form->name && form->namelength) { |
269 | 0 | if(memchr(form->name, 0, form->namelength)) |
270 | 0 | return CURL_FORMADD_NULL; |
271 | 0 | } |
272 | 0 | if(!(form->flags & HTTPPOST_PTRNAME) && form->name) { |
273 | | /* Note that there is small risk that form->name is NULL here if the app |
274 | | passed in a bad combo, so we check for that. */ |
275 | | |
276 | | /* copy name (without strdup; possibly not null-terminated) */ |
277 | 0 | char *dupname = Curl_memdup0(form->name, form->namelength ? |
278 | 0 | form->namelength : strlen(form->name)); |
279 | 0 | if(!dupname) |
280 | 0 | return CURL_FORMADD_MEMORY; |
281 | | |
282 | 0 | form->name = dupname; |
283 | 0 | form->name_alloc = TRUE; |
284 | 0 | } |
285 | 0 | if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE | |
286 | 0 | HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER | |
287 | 0 | HTTPPOST_CALLBACK)) && form->value) { |
288 | | /* copy value (without strdup; possibly contains null characters) */ |
289 | 0 | size_t clen = (size_t) form->contentslength; |
290 | 0 | if(!clen) |
291 | 0 | clen = strlen(form->value) + 1; |
292 | |
|
293 | 0 | form->value = Curl_memdup(form->value, clen); |
294 | |
|
295 | 0 | if(!form->value) |
296 | 0 | return CURL_FORMADD_MEMORY; |
297 | | |
298 | 0 | form->value_alloc = TRUE; |
299 | 0 | } |
300 | 0 | post = AddHttpPost(form, post, httppost, last_post); |
301 | |
|
302 | 0 | if(!post) |
303 | 0 | return CURL_FORMADD_MEMORY; |
304 | | |
305 | 0 | if(form->contenttype) |
306 | 0 | prevtype = form->contenttype; |
307 | 0 | } |
308 | | |
309 | 0 | return CURL_FORMADD_OK; |
310 | 0 | } |
311 | | |
312 | | /* Shallow cleanup. Remove the newly created chain, the structs only and not |
313 | | the content they point to */ |
314 | | static void free_chain(struct curl_httppost *c) |
315 | 0 | { |
316 | 0 | while(c) { |
317 | 0 | struct curl_httppost *next = c->next; |
318 | 0 | if(c->more) |
319 | 0 | free_chain(c->more); |
320 | 0 | free(c); |
321 | 0 | c = next; |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | | static |
326 | | CURLFORMcode FormAdd(struct curl_httppost **httppost, |
327 | | struct curl_httppost **last_post, |
328 | | va_list params) |
329 | 0 | { |
330 | 0 | struct FormInfo *first_form, *curr, *form = NULL; |
331 | 0 | CURLFORMcode retval = CURL_FORMADD_OK; |
332 | 0 | CURLformoption option; |
333 | 0 | struct curl_forms *forms = NULL; |
334 | 0 | char *avalue = NULL; |
335 | 0 | struct curl_httppost *newchain = NULL; |
336 | 0 | struct curl_httppost *lastnode = NULL; |
337 | | |
338 | | /* This is a state variable, that if TRUE means that we are parsing an |
339 | | array that we got passed to us. If FALSE we are parsing the input |
340 | | va_list arguments. */ |
341 | 0 | bool array_state = FALSE; |
342 | | |
343 | | /* |
344 | | * We need to allocate the first struct to fill in. |
345 | | */ |
346 | 0 | first_form = calloc(1, sizeof(struct FormInfo)); |
347 | 0 | if(!first_form) |
348 | 0 | return CURL_FORMADD_MEMORY; |
349 | | |
350 | 0 | curr = first_form; |
351 | | |
352 | | /* |
353 | | * Loop through all the options set. Break if we have an error to report. |
354 | | */ |
355 | 0 | while(retval == CURL_FORMADD_OK) { |
356 | | |
357 | | /* first see if we have more parts of the array param */ |
358 | 0 | if(array_state && forms) { |
359 | | /* get the upcoming option from the given array */ |
360 | 0 | option = forms->option; |
361 | 0 | avalue = (char *)CURL_UNCONST(forms->value); |
362 | |
|
363 | 0 | forms++; /* advance this to next entry */ |
364 | 0 | if(CURLFORM_END == option) { |
365 | | /* end of array state */ |
366 | 0 | array_state = FALSE; |
367 | 0 | continue; |
368 | 0 | } |
369 | 0 | } |
370 | 0 | else { |
371 | | /* This is not array-state, get next option. This gets an 'int' with |
372 | | va_arg() because CURLformoption might be a smaller type than int and |
373 | | might cause compiler warnings and wrong behavior. */ |
374 | 0 | option = (CURLformoption)va_arg(params, int); |
375 | 0 | if(CURLFORM_END == option) |
376 | 0 | break; |
377 | 0 | } |
378 | | |
379 | 0 | switch(option) { |
380 | 0 | case CURLFORM_ARRAY: |
381 | 0 | if(array_state) |
382 | | /* we do not support an array from within an array */ |
383 | 0 | retval = CURL_FORMADD_ILLEGAL_ARRAY; |
384 | 0 | else { |
385 | 0 | forms = va_arg(params, struct curl_forms *); |
386 | 0 | if(forms) |
387 | 0 | array_state = TRUE; |
388 | 0 | else |
389 | 0 | retval = CURL_FORMADD_NULL; |
390 | 0 | } |
391 | 0 | break; |
392 | | |
393 | | /* |
394 | | * Set the Name property. |
395 | | */ |
396 | 0 | case CURLFORM_PTRNAME: |
397 | 0 | curr->flags |= HTTPPOST_PTRNAME; /* fall through */ |
398 | |
|
399 | 0 | FALLTHROUGH(); |
400 | 0 | case CURLFORM_COPYNAME: |
401 | 0 | if(curr->name) |
402 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
403 | 0 | else { |
404 | 0 | if(!array_state) |
405 | 0 | avalue = va_arg(params, char *); |
406 | 0 | if(avalue) |
407 | 0 | curr->name = avalue; /* store for the moment */ |
408 | 0 | else |
409 | 0 | retval = CURL_FORMADD_NULL; |
410 | 0 | } |
411 | 0 | break; |
412 | 0 | case CURLFORM_NAMELENGTH: |
413 | 0 | if(curr->namelength) |
414 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
415 | 0 | else |
416 | 0 | curr->namelength = |
417 | 0 | array_state ? (size_t)avalue : (size_t)va_arg(params, long); |
418 | 0 | break; |
419 | | |
420 | | /* |
421 | | * Set the contents property. |
422 | | */ |
423 | 0 | case CURLFORM_PTRCONTENTS: |
424 | 0 | curr->flags |= HTTPPOST_PTRCONTENTS; |
425 | 0 | FALLTHROUGH(); |
426 | 0 | case CURLFORM_COPYCONTENTS: |
427 | 0 | if(curr->value) |
428 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
429 | 0 | else { |
430 | 0 | if(!array_state) |
431 | 0 | avalue = va_arg(params, char *); |
432 | 0 | if(avalue) |
433 | 0 | curr->value = avalue; /* store for the moment */ |
434 | 0 | else |
435 | 0 | retval = CURL_FORMADD_NULL; |
436 | 0 | } |
437 | 0 | break; |
438 | 0 | case CURLFORM_CONTENTSLENGTH: |
439 | 0 | curr->contentslength = |
440 | 0 | array_state ? (size_t)avalue : (size_t)va_arg(params, long); |
441 | 0 | break; |
442 | | |
443 | 0 | case CURLFORM_CONTENTLEN: |
444 | 0 | curr->flags |= CURL_HTTPPOST_LARGE; |
445 | 0 | curr->contentslength = |
446 | 0 | array_state ? (curl_off_t)(size_t)avalue : |
447 | 0 | va_arg(params, curl_off_t); |
448 | 0 | break; |
449 | | |
450 | | /* Get contents from a given filename */ |
451 | 0 | case CURLFORM_FILECONTENT: |
452 | 0 | if(curr->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_READFILE)) |
453 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
454 | 0 | else { |
455 | 0 | if(!array_state) |
456 | 0 | avalue = va_arg(params, char *); |
457 | 0 | if(avalue) { |
458 | 0 | curr->value = strdup(avalue); |
459 | 0 | if(!curr->value) |
460 | 0 | retval = CURL_FORMADD_MEMORY; |
461 | 0 | else { |
462 | 0 | curr->flags |= HTTPPOST_READFILE; |
463 | 0 | curr->value_alloc = TRUE; |
464 | 0 | } |
465 | 0 | } |
466 | 0 | else |
467 | 0 | retval = CURL_FORMADD_NULL; |
468 | 0 | } |
469 | 0 | break; |
470 | | |
471 | | /* We upload a file */ |
472 | 0 | case CURLFORM_FILE: |
473 | 0 | if(!array_state) |
474 | 0 | avalue = va_arg(params, char *); |
475 | |
|
476 | 0 | if(curr->value) { |
477 | 0 | if(curr->flags & HTTPPOST_FILENAME) { |
478 | 0 | if(avalue) { |
479 | 0 | char *fname = strdup(avalue); |
480 | 0 | if(!fname) |
481 | 0 | retval = CURL_FORMADD_MEMORY; |
482 | 0 | else { |
483 | 0 | form = AddFormInfo(fname, NULL, curr); |
484 | 0 | if(!form) { |
485 | 0 | free(fname); |
486 | 0 | retval = CURL_FORMADD_MEMORY; |
487 | 0 | } |
488 | 0 | else { |
489 | 0 | form->value_alloc = TRUE; |
490 | 0 | curr = form; |
491 | 0 | form = NULL; |
492 | 0 | } |
493 | 0 | } |
494 | 0 | } |
495 | 0 | else |
496 | 0 | retval = CURL_FORMADD_NULL; |
497 | 0 | } |
498 | 0 | else |
499 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
500 | 0 | } |
501 | 0 | else { |
502 | 0 | if(avalue) { |
503 | 0 | curr->value = strdup(avalue); |
504 | 0 | if(!curr->value) |
505 | 0 | retval = CURL_FORMADD_MEMORY; |
506 | 0 | else { |
507 | 0 | curr->flags |= HTTPPOST_FILENAME; |
508 | 0 | curr->value_alloc = TRUE; |
509 | 0 | } |
510 | 0 | } |
511 | 0 | else |
512 | 0 | retval = CURL_FORMADD_NULL; |
513 | 0 | } |
514 | 0 | break; |
515 | | |
516 | 0 | case CURLFORM_BUFFERPTR: |
517 | 0 | curr->flags |= HTTPPOST_PTRBUFFER|HTTPPOST_BUFFER; |
518 | 0 | if(curr->buffer) |
519 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
520 | 0 | else { |
521 | 0 | if(!array_state) |
522 | 0 | avalue = va_arg(params, char *); |
523 | 0 | if(avalue) { |
524 | 0 | curr->buffer = avalue; /* store for the moment */ |
525 | 0 | curr->value = avalue; /* make it non-NULL to be accepted |
526 | | as fine */ |
527 | 0 | } |
528 | 0 | else |
529 | 0 | retval = CURL_FORMADD_NULL; |
530 | 0 | } |
531 | 0 | break; |
532 | | |
533 | 0 | case CURLFORM_BUFFERLENGTH: |
534 | 0 | if(curr->bufferlength) |
535 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
536 | 0 | else |
537 | 0 | curr->bufferlength = |
538 | 0 | array_state ? (size_t)avalue : (size_t)va_arg(params, long); |
539 | 0 | break; |
540 | | |
541 | 0 | case CURLFORM_STREAM: |
542 | 0 | curr->flags |= HTTPPOST_CALLBACK; |
543 | 0 | if(curr->userp) |
544 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
545 | 0 | else { |
546 | 0 | if(!array_state) |
547 | 0 | avalue = va_arg(params, char *); |
548 | 0 | if(avalue) { |
549 | 0 | curr->userp = avalue; |
550 | 0 | curr->value = avalue; /* this is not strictly true but we derive a |
551 | | value from this later on and we need this |
552 | | non-NULL to be accepted as a fine form |
553 | | part */ |
554 | 0 | } |
555 | 0 | else |
556 | 0 | retval = CURL_FORMADD_NULL; |
557 | 0 | } |
558 | 0 | break; |
559 | | |
560 | 0 | case CURLFORM_CONTENTTYPE: |
561 | 0 | if(!array_state) |
562 | 0 | avalue = va_arg(params, char *); |
563 | 0 | if(curr->contenttype) { |
564 | 0 | if(curr->flags & HTTPPOST_FILENAME) { |
565 | 0 | if(avalue) { |
566 | 0 | char *type = strdup(avalue); |
567 | 0 | if(!type) |
568 | 0 | retval = CURL_FORMADD_MEMORY; |
569 | 0 | else { |
570 | 0 | form = AddFormInfo(NULL, type, curr); |
571 | 0 | if(!form) { |
572 | 0 | free(type); |
573 | 0 | retval = CURL_FORMADD_MEMORY; |
574 | 0 | } |
575 | 0 | else { |
576 | 0 | form->contenttype_alloc = TRUE; |
577 | 0 | curr = form; |
578 | 0 | form = NULL; |
579 | 0 | } |
580 | 0 | } |
581 | 0 | } |
582 | 0 | else |
583 | 0 | retval = CURL_FORMADD_NULL; |
584 | 0 | } |
585 | 0 | else |
586 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
587 | 0 | } |
588 | 0 | else { |
589 | 0 | if(avalue) { |
590 | 0 | curr->contenttype = strdup(avalue); |
591 | 0 | if(!curr->contenttype) |
592 | 0 | retval = CURL_FORMADD_MEMORY; |
593 | 0 | else |
594 | 0 | curr->contenttype_alloc = TRUE; |
595 | 0 | } |
596 | 0 | else |
597 | 0 | retval = CURL_FORMADD_NULL; |
598 | 0 | } |
599 | 0 | break; |
600 | | |
601 | 0 | case CURLFORM_CONTENTHEADER: |
602 | 0 | { |
603 | | /* this "cast increases required alignment of target type" but |
604 | | we consider it OK anyway */ |
605 | 0 | struct curl_slist *list = array_state ? |
606 | 0 | (struct curl_slist *)(void *)avalue : |
607 | 0 | va_arg(params, struct curl_slist *); |
608 | |
|
609 | 0 | if(curr->contentheader) |
610 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
611 | 0 | else |
612 | 0 | curr->contentheader = list; |
613 | |
|
614 | 0 | break; |
615 | 0 | } |
616 | 0 | case CURLFORM_FILENAME: |
617 | 0 | case CURLFORM_BUFFER: |
618 | 0 | if(!array_state) |
619 | 0 | avalue = va_arg(params, char *); |
620 | 0 | if(curr->showfilename) |
621 | 0 | retval = CURL_FORMADD_OPTION_TWICE; |
622 | 0 | else { |
623 | 0 | curr->showfilename = strdup(avalue); |
624 | 0 | if(!curr->showfilename) |
625 | 0 | retval = CURL_FORMADD_MEMORY; |
626 | 0 | else |
627 | 0 | curr->showfilename_alloc = TRUE; |
628 | 0 | } |
629 | 0 | break; |
630 | | |
631 | 0 | default: |
632 | 0 | retval = CURL_FORMADD_UNKNOWN_OPTION; |
633 | 0 | break; |
634 | 0 | } |
635 | 0 | } |
636 | | |
637 | 0 | if(!retval) |
638 | 0 | retval = FormAddCheck(first_form, &newchain, &lastnode); |
639 | |
|
640 | 0 | if(retval) |
641 | | /* On error, free allocated fields for all nodes of the FormInfo linked |
642 | | list without deallocating nodes. List nodes are deallocated later on */ |
643 | 0 | free_formlist(first_form); |
644 | | |
645 | | /* Always deallocate FormInfo linked list nodes without touching node |
646 | | fields given that these have either been deallocated or are owned |
647 | | now by the httppost linked list */ |
648 | 0 | while(first_form) { |
649 | 0 | struct FormInfo *ptr = first_form->more; |
650 | 0 | free(first_form); |
651 | 0 | first_form = ptr; |
652 | 0 | } |
653 | |
|
654 | 0 | if(!retval) { |
655 | | /* Only if all is fine, link the new chain into the provided list */ |
656 | 0 | if(*last_post) |
657 | 0 | (*last_post)->next = newchain; |
658 | 0 | else |
659 | 0 | (*httppost) = newchain; |
660 | |
|
661 | 0 | (*last_post) = lastnode; |
662 | 0 | } |
663 | 0 | else |
664 | 0 | free_chain(newchain); |
665 | |
|
666 | 0 | return retval; |
667 | 0 | } |
668 | | |
669 | | /* |
670 | | * curl_formadd() is a public API to add a section to the multipart formpost. |
671 | | * |
672 | | * @unittest: 1308 |
673 | | */ |
674 | | |
675 | | CURLFORMcode curl_formadd(struct curl_httppost **httppost, |
676 | | struct curl_httppost **last_post, |
677 | | ...) |
678 | 0 | { |
679 | 0 | va_list arg; |
680 | 0 | CURLFORMcode result; |
681 | 0 | va_start(arg, last_post); |
682 | 0 | result = FormAdd(httppost, last_post, arg); |
683 | 0 | va_end(arg); |
684 | 0 | return result; |
685 | 0 | } |
686 | | |
687 | | /* |
688 | | * curl_formget() |
689 | | * Serialize a curl_httppost struct. |
690 | | * Returns 0 on success. |
691 | | * |
692 | | * @unittest: 1308 |
693 | | */ |
694 | | int curl_formget(struct curl_httppost *form, void *arg, |
695 | | curl_formget_callback append) |
696 | 0 | { |
697 | 0 | CURLcode result; |
698 | 0 | curl_mimepart toppart; |
699 | |
|
700 | 0 | Curl_mime_initpart(&toppart); /* default form is empty */ |
701 | 0 | result = Curl_getformdata(NULL, &toppart, form, NULL); |
702 | 0 | if(!result) |
703 | 0 | result = Curl_mime_prepare_headers(NULL, &toppart, "multipart/form-data", |
704 | 0 | NULL, MIMESTRATEGY_FORM); |
705 | |
|
706 | 0 | while(!result) { |
707 | 0 | char buffer[8192]; |
708 | 0 | size_t nread = Curl_mime_read(buffer, 1, sizeof(buffer), &toppart); |
709 | |
|
710 | 0 | if(!nread) |
711 | 0 | break; |
712 | | |
713 | 0 | if(nread > sizeof(buffer) || append(arg, buffer, nread) != nread) { |
714 | 0 | result = CURLE_READ_ERROR; |
715 | 0 | if(nread == CURL_READFUNC_ABORT) |
716 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
717 | 0 | } |
718 | 0 | } |
719 | |
|
720 | 0 | Curl_mime_cleanpart(&toppart); |
721 | 0 | return (int) result; |
722 | 0 | } |
723 | | |
724 | | /* |
725 | | * curl_formfree() is an external function to free up a whole form post |
726 | | * chain |
727 | | */ |
728 | | void curl_formfree(struct curl_httppost *form) |
729 | 0 | { |
730 | 0 | struct curl_httppost *next; |
731 | |
|
732 | 0 | if(!form) |
733 | | /* no form to free, just get out of this */ |
734 | 0 | return; |
735 | | |
736 | 0 | do { |
737 | 0 | next = form->next; /* the following form line */ |
738 | | |
739 | | /* recurse to sub-contents */ |
740 | 0 | curl_formfree(form->more); |
741 | |
|
742 | 0 | if(!(form->flags & HTTPPOST_PTRNAME)) |
743 | 0 | free(form->name); /* free the name */ |
744 | 0 | if(!(form->flags & |
745 | 0 | (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK)) |
746 | 0 | ) |
747 | 0 | free(form->contents); /* free the contents */ |
748 | 0 | free(form->contenttype); /* free the content type */ |
749 | 0 | free(form->showfilename); /* free the faked filename */ |
750 | 0 | free(form); /* free the struct */ |
751 | 0 | form = next; |
752 | 0 | } while(form); /* continue */ |
753 | 0 | } |
754 | | |
755 | | |
756 | | /* Set mime part name, taking care of non null-terminated name string. */ |
757 | | static CURLcode setname(curl_mimepart *part, const char *name, size_t len) |
758 | 0 | { |
759 | 0 | char *zname; |
760 | 0 | CURLcode res; |
761 | |
|
762 | 0 | if(!name || !len) |
763 | 0 | return curl_mime_name(part, name); |
764 | 0 | zname = Curl_memdup0(name, len); |
765 | 0 | if(!zname) |
766 | 0 | return CURLE_OUT_OF_MEMORY; |
767 | 0 | res = curl_mime_name(part, zname); |
768 | 0 | free(zname); |
769 | 0 | return res; |
770 | 0 | } |
771 | | |
772 | | /* wrap call to fseeko so it matches the calling convention of callback */ |
773 | | static int fseeko_wrapper(void *stream, curl_off_t offset, int whence) |
774 | 0 | { |
775 | | #if defined(_WIN32) && defined(USE_WIN32_LARGE_FILES) |
776 | | return _fseeki64(stream, (__int64)offset, whence); |
777 | | #elif defined(HAVE_FSEEKO) && defined(HAVE_DECL_FSEEKO) |
778 | | return fseeko(stream, (off_t)offset, whence); |
779 | | #else |
780 | | if(offset > LONG_MAX) |
781 | | return -1; |
782 | | return fseek(stream, (long)offset, whence); |
783 | | #endif |
784 | 0 | } |
785 | | |
786 | | /* |
787 | | * Curl_getformdata() converts a linked list of "meta data" into a mime |
788 | | * structure. The input list is in 'post', while the output is stored in |
789 | | * mime part at '*finalform'. |
790 | | * |
791 | | * This function will not do a failf() for the potential memory failures but |
792 | | * should for all other errors it spots. Just note that this function MAY get |
793 | | * a NULL pointer in the 'data' argument. |
794 | | */ |
795 | | |
796 | | CURLcode Curl_getformdata(CURL *data, |
797 | | curl_mimepart *finalform, |
798 | | struct curl_httppost *post, |
799 | | curl_read_callback fread_func) |
800 | 0 | { |
801 | 0 | CURLcode result = CURLE_OK; |
802 | 0 | curl_mime *form = NULL; |
803 | 0 | curl_mimepart *part; |
804 | 0 | struct curl_httppost *file; |
805 | |
|
806 | 0 | Curl_mime_cleanpart(finalform); /* default form is empty */ |
807 | |
|
808 | 0 | if(!post) |
809 | 0 | return result; /* no input => no output! */ |
810 | | |
811 | 0 | form = curl_mime_init(data); |
812 | 0 | if(!form) |
813 | 0 | result = CURLE_OUT_OF_MEMORY; |
814 | |
|
815 | 0 | if(!result) |
816 | 0 | result = curl_mime_subparts(finalform, form); |
817 | | |
818 | | /* Process each top part. */ |
819 | 0 | for(; !result && post; post = post->next) { |
820 | | /* If we have more than a file here, create a mime subpart and fill it. */ |
821 | 0 | curl_mime *multipart = form; |
822 | 0 | if(post->more) { |
823 | 0 | part = curl_mime_addpart(form); |
824 | 0 | if(!part) |
825 | 0 | result = CURLE_OUT_OF_MEMORY; |
826 | 0 | if(!result) |
827 | 0 | result = setname(part, post->name, post->namelength); |
828 | 0 | if(!result) { |
829 | 0 | multipart = curl_mime_init(data); |
830 | 0 | if(!multipart) |
831 | 0 | result = CURLE_OUT_OF_MEMORY; |
832 | 0 | } |
833 | 0 | if(!result) |
834 | 0 | result = curl_mime_subparts(part, multipart); |
835 | 0 | } |
836 | | |
837 | | /* Generate all the part contents. */ |
838 | 0 | for(file = post; !result && file; file = file->more) { |
839 | | /* Create the part. */ |
840 | 0 | part = curl_mime_addpart(multipart); |
841 | 0 | if(!part) |
842 | 0 | result = CURLE_OUT_OF_MEMORY; |
843 | | |
844 | | /* Set the headers. */ |
845 | 0 | if(!result) |
846 | 0 | result = curl_mime_headers(part, file->contentheader, 0); |
847 | | |
848 | | /* Set the content type. */ |
849 | 0 | if(!result && file->contenttype) |
850 | 0 | result = curl_mime_type(part, file->contenttype); |
851 | | |
852 | | /* Set field name. */ |
853 | 0 | if(!result && !post->more) |
854 | 0 | result = setname(part, post->name, post->namelength); |
855 | | |
856 | | /* Process contents. */ |
857 | 0 | if(!result) { |
858 | 0 | curl_off_t clen = post->contentslength; |
859 | |
|
860 | 0 | if(post->flags & CURL_HTTPPOST_LARGE) |
861 | 0 | clen = post->contentlen; |
862 | |
|
863 | 0 | if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) { |
864 | 0 | if(!strcmp(file->contents, "-")) { |
865 | | /* There are a few cases where the code below will not work; in |
866 | | particular, freopen(stdin) by the caller is not guaranteed |
867 | | to result as expected. This feature has been kept for backward |
868 | | compatibility: use of "-" pseudo filename should be avoided. */ |
869 | 0 | result = curl_mime_data_cb(part, (curl_off_t) -1, |
870 | 0 | (curl_read_callback) fread, |
871 | 0 | fseeko_wrapper, |
872 | 0 | NULL, (void *) stdin); |
873 | 0 | } |
874 | 0 | else |
875 | 0 | result = curl_mime_filedata(part, file->contents); |
876 | 0 | if(!result && (post->flags & HTTPPOST_READFILE)) |
877 | 0 | result = curl_mime_filename(part, NULL); |
878 | 0 | } |
879 | 0 | else if(post->flags & HTTPPOST_BUFFER) |
880 | 0 | result = curl_mime_data(part, post->buffer, |
881 | 0 | post->bufferlength ? |
882 | 0 | post->bufferlength : -1); |
883 | 0 | else if(post->flags & HTTPPOST_CALLBACK) { |
884 | | /* the contents should be read with the callback and the size is set |
885 | | with the contentslength */ |
886 | 0 | if(!clen) |
887 | 0 | clen = -1; |
888 | 0 | result = curl_mime_data_cb(part, clen, |
889 | 0 | fread_func, NULL, NULL, post->userp); |
890 | 0 | } |
891 | 0 | else { |
892 | 0 | size_t uclen; |
893 | 0 | if(!clen) |
894 | 0 | uclen = CURL_ZERO_TERMINATED; |
895 | 0 | else |
896 | 0 | uclen = (size_t)clen; |
897 | 0 | result = curl_mime_data(part, post->contents, uclen); |
898 | 0 | } |
899 | 0 | } |
900 | | |
901 | | /* Set fake filename. */ |
902 | 0 | if(!result && post->showfilename) |
903 | 0 | if(post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER | |
904 | 0 | HTTPPOST_CALLBACK))) |
905 | 0 | result = curl_mime_filename(part, post->showfilename); |
906 | 0 | } |
907 | 0 | } |
908 | |
|
909 | 0 | if(result) |
910 | 0 | Curl_mime_cleanpart(finalform); |
911 | |
|
912 | 0 | return result; |
913 | 0 | } |
914 | | |
915 | | #else |
916 | | /* if disabled */ |
917 | | CURLFORMcode curl_formadd(struct curl_httppost **httppost, |
918 | | struct curl_httppost **last_post, |
919 | | ...) |
920 | | { |
921 | | (void)httppost; |
922 | | (void)last_post; |
923 | | return CURL_FORMADD_DISABLED; |
924 | | } |
925 | | |
926 | | int curl_formget(struct curl_httppost *form, void *arg, |
927 | | curl_formget_callback append) |
928 | | { |
929 | | (void) form; |
930 | | (void) arg; |
931 | | (void) append; |
932 | | return CURL_FORMADD_DISABLED; |
933 | | } |
934 | | |
935 | | void curl_formfree(struct curl_httppost *form) |
936 | | { |
937 | | (void)form; |
938 | | /* Nothing to do. */ |
939 | | } |
940 | | |
941 | | #endif /* if disabled */ |