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