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