master
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#include "curl_setup.h"
26struct Curl_easy;
28#include "formdata.h"
30#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_FORM_API)
32#include "urldata.h" /* for struct Curl_easy */
33#include "mime.h"
34#include "curlx/strdup.h"
35#include "bufref.h"
36#include "curlx/win32-fopen.h"
39#define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME
40#define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME
41#define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
42#define HTTPPOST_READFILE CURL_HTTPPOST_READFILE
43#define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER
44#define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK
45#define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER
47/***************************************************************************
48 *
49 * httppost_add()
50 *
51 * Adds an HttpPost structure to the list, if parent_post is given becomes
52 * a subpost of parent_post instead of a direct list element.
53 *
54 * Returns newly allocated HttpPost on success and NULL if malloc failed.
55 *
56 ***************************************************************************/
57static struct curl_httppost *httppost_add(struct FormInfo *src,
58 struct curl_httppost *parent_post,
59 struct curl_httppost **httppost,
60 struct curl_httppost **last_post)
61{
62 struct curl_httppost *post;
63 size_t namelength = src->namelength;
64 if(!namelength && Curl_bufref_ptr(&src->name))
65 namelength = strlen(Curl_bufref_ptr(&src->name));
66 if((src->bufferlength > LONG_MAX) || (namelength > LONG_MAX))
67 /* avoid overflow in typecasts below */
68 return NULL;
69 post = curlx_calloc(1, sizeof(struct curl_httppost));
70 if(post) {
71 post->name = CURL_UNCONST(Curl_bufref_ptr(&src->name));
72 post->namelength = (long)namelength;
73 post->contents = CURL_UNCONST(Curl_bufref_ptr(&src->value));
74 post->contentlen = src->contentslength;
75 post->buffer = src->buffer;
76 post->bufferlength = (long)src->bufferlength;
77 post->contenttype = CURL_UNCONST(Curl_bufref_ptr(&src->contenttype));
78 post->flags = src->flags | CURL_HTTPPOST_LARGE;
79 post->contentheader = src->contentheader;
80 post->showfilename = CURL_UNCONST(Curl_bufref_ptr(&src->showfilename));
81 post->userp = src->userp;
82 }
83 else
84 return NULL;
86 if(parent_post) {
87 /* now, point our 'more' to the original 'more' */
88 post->more = parent_post->more;
90 /* then move the original 'more' to point to ourselves */
91 parent_post->more = post;
92 }
93 else {
94 /* make the previous point to this */
95 if(*last_post)
96 (*last_post)->next = post;
97 else
98 (*httppost) = post;
100 (*last_post) = post;
101 }
102 return post;
103}
105/* Allocate and initialize a new FormInfo structure. */
106static struct FormInfo *forminfo_new(void)
107{
108 struct FormInfo *form_info = curlx_calloc(1, sizeof(struct FormInfo));
110 if(form_info) {
111 Curl_bufref_init(&form_info->name);
112 Curl_bufref_init(&form_info->value);
113 Curl_bufref_init(&form_info->contenttype);
114 Curl_bufref_init(&form_info->showfilename);
115 }
117 return form_info;
118}
120/* Replace the target field data by a dynamic copy of it. */
121static CURLcode forminfo_copyfield(struct bufref *field, size_t len)
122{
123 const char *value = Curl_bufref_ptr(field);
124 CURLcode result = CURLE_OK;
126 if(value) {
127 if(!len)
128 len = strlen(value);
129 result = Curl_bufref_memdup0(field, value, len);
130 }
132 return result;
133}
135/***************************************************************************
136 *
137 * forminfo_add()
138 *
139 * Adds a FormInfo structure to the list presented by parent.
140 *
141 ***************************************************************************/
142static void forminfo_add(struct FormInfo *form_info, struct FormInfo *parent)
143{
144 form_info->flags |= HTTPPOST_FILENAME;
146 if(parent) {
147 /* now, point our 'more' to the original 'more' */
148 form_info->more = parent->more;
150 /* then move the original 'more' to point to ourselves */
151 parent->more = form_info;
152 }
153}
155static void free_formlist(struct FormInfo *ptr)
156{
157 for(; ptr; ptr = ptr->more) {
158 Curl_bufref_free(&ptr->name);
159 Curl_bufref_free(&ptr->value);
160 Curl_bufref_free(&ptr->contenttype);
161 Curl_bufref_free(&ptr->showfilename);
162 }
163}
165/***************************************************************************
166 *
167 * formadd_check()
168 *
169 * Stores a formpost parameter and builds the appropriate linked list.
170 *
171 * Has two principal functionalities: using files and byte arrays as post
172 * parts. Byte arrays are either copied or the pointer is stored (as the user
173 * requests) while for files only the filename and not the content is stored.
174 *
175 * While you may have only one byte array for each name, multiple filenames
176 * are allowed (and because of this feature CURLFORM_END is needed after
177 * using CURLFORM_FILE).
178 *
179 * Examples:
180 *
181 * Simple name/value pair with copied contents:
182 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
183 * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
184 *
185 * name/value pair where only the content pointer is remembered:
186 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
187 * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10L,
188 * CURLFORM_END);
189 * (if CURLFORM_CONTENTSLENGTH is missing strlen() is used)
190 *
191 * storing a filename (CONTENTTYPE is optional!):
192 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
193 * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
194 * CURLFORM_END);
195 *
196 * storing multiple filenames:
197 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
198 * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2",
199 * CURLFORM_END);
200 *
201 * Returns:
202 * CURL_FORMADD_OK on success
203 * CURL_FORMADD_MEMORY if the FormInfo allocation fails
204 * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
205 * CURL_FORMADD_NULL if a null pointer was given for a char
206 * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
207 * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
208 * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
209 * CURL_FORMADD_MEMORY if an HttpPost struct cannot be allocated
210 * CURL_FORMADD_MEMORY if some allocation for string copying failed.
211 * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
212 *
213 ***************************************************************************/
215static CURLFORMcode formadd_check(struct FormInfo *first_form,
216 struct curl_httppost **httppost,
217 struct curl_httppost **last_post)
218{
219 const char *prevtype = NULL;
220 struct FormInfo *form = NULL;
221 struct curl_httppost *post = NULL;
223 /* go through the list, check for completeness and if everything is
224 * alright add the HttpPost item otherwise set retval accordingly */
226 for(form = first_form; form; form = form->more) {
227 const char *name = Curl_bufref_ptr(&form->name);
229 if(((!name || !Curl_bufref_ptr(&form->value)) && !post) ||
230 (form->contentslength &&
231 (form->flags & HTTPPOST_FILENAME)) ||
232 ((form->flags & HTTPPOST_FILENAME) &&
233 (form->flags & HTTPPOST_PTRCONTENTS)) ||
235 (!form->buffer &&
236 (form->flags & HTTPPOST_BUFFER) &&
237 (form->flags & HTTPPOST_PTRBUFFER)) ||
239 ((form->flags & HTTPPOST_READFILE) &&
240 (form->flags & HTTPPOST_PTRCONTENTS))
241 ) {
242 return CURL_FORMADD_INCOMPLETE;
243 }
244 if(((form->flags & HTTPPOST_FILENAME) ||
245 (form->flags & HTTPPOST_BUFFER)) &&
246 !Curl_bufref_ptr(&form->contenttype)) {
247 const char *f = Curl_bufref_ptr((form->flags & HTTPPOST_BUFFER) ?
248 &form->showfilename : &form->value);
249 const char *type = Curl_mime_contenttype(f);
250 if(!type)
251 type = prevtype;
252 if(!type)
253 type = FILE_CONTENTTYPE_DEFAULT;
255 /* our contenttype is missing */
256 if(Curl_bufref_memdup0(&form->contenttype, type, strlen(type)))
257 return CURL_FORMADD_MEMORY;
258 }
259 if(name && form->namelength && memchr(name, 0, form->namelength))
260 return CURL_FORMADD_NULL;
261 if(!(form->flags & HTTPPOST_PTRNAME) &&
262 /* Note that there is small risk that form->name is NULL here if the app
263 passed in a bad combo, so we check for that. */
264 forminfo_copyfield(&form->name, form->namelength))
265 return CURL_FORMADD_MEMORY;
266 if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
267 HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
268 HTTPPOST_CALLBACK)) &&
269 forminfo_copyfield(&form->value, (size_t)form->contentslength))
270 return CURL_FORMADD_MEMORY;
271 post = httppost_add(form, post, httppost, last_post);
273 if(!post)
274 return CURL_FORMADD_MEMORY;
276 if(Curl_bufref_ptr(&form->contenttype))
277 prevtype = Curl_bufref_ptr(&form->contenttype);
278 }
280 return CURL_FORMADD_OK;
281}
283/* Shallow cleanup. Remove the newly created chain, the structs only and not
284 the content they point to */
285static void free_chain(struct curl_httppost *c)
286{
287 while(c) {
288 struct curl_httppost *next = c->next;
289 if(c->more)
290 free_chain(c->more);
291 curlx_free(c);
292 c = next;
293 }
294}
296static CURLFORMcode formadd(struct curl_httppost **httppost,
297 struct curl_httppost **last_post, va_list params)
298{
299 struct FormInfo *first_form, *curr, *form = NULL;
300 CURLFORMcode retval = CURL_FORMADD_OK;
301 CURLformoption option;
302 const struct curl_forms *forms = NULL;
303 char *avalue = NULL;
304 struct curl_httppost *newchain = NULL;
305 struct curl_httppost *lastnode = NULL;
307#define form_ptr_arg(t) (forms ? (t)(void *)avalue : va_arg(params, t))
308#ifdef HAVE_UINTPTR_T
309#define form_int_arg(t) (forms ? (t)(uintptr_t)avalue : va_arg(params, t))
310#else
311#define form_int_arg(t) (forms ? (t)(void *)avalue : va_arg(params, t))
312#endif
314 /*
315 * We need to allocate the first struct to fill in.
316 */
317 first_form = forminfo_new();
318 if(!first_form)
319 return CURL_FORMADD_MEMORY;
321 curr = first_form;
323 /*
324 * Loop through all the options set. Break if we have an error to report.
325 */
326 while(retval == CURL_FORMADD_OK) {
328 /* first see if we have more parts of the array param */
329 if(forms) {
330 /* get the upcoming option from the given array */
331 option = forms->option;
332 avalue = (char *)CURL_UNCONST(forms->value);
334 forms++; /* advance this to next entry */
335 if(CURLFORM_END == option) {
336 /* end of array state */
337 forms = NULL;
338 continue;
339 }
340 }
341 else {
342 /* This is not array-state, get next option. This gets an 'int' with
343 va_arg() because CURLformoption might be a smaller type than int and
344 might cause compiler warnings and wrong behavior. */
345 option = (CURLformoption)va_arg(params, int);
346 if(CURLFORM_END == option)
347 break;
348 }
350 switch(option) {
351 case CURLFORM_ARRAY:
352 if(forms)
353 /* we do not support an array from within an array */
354 retval = CURL_FORMADD_ILLEGAL_ARRAY;
355 else {
356 forms = va_arg(params, struct curl_forms *);
357 if(!forms)
358 retval = CURL_FORMADD_NULL;
359 }
360 break;
362 /*
363 * Set the Name property.
364 */
365 case CURLFORM_PTRNAME:
366 curr->flags |= HTTPPOST_PTRNAME;
367 FALLTHROUGH();
368 case CURLFORM_COPYNAME:
369 if(Curl_bufref_ptr(&curr->name))
370 retval = CURL_FORMADD_OPTION_TWICE;
371 else {
372 avalue = form_ptr_arg(char *);
373 if(avalue)
374 Curl_bufref_set(&curr->name, avalue, 0, NULL); /* No copy yet. */
375 else
376 retval = CURL_FORMADD_NULL;
377 }
378 break;
379 case CURLFORM_NAMELENGTH:
380 if(curr->namelength)
381 retval = CURL_FORMADD_OPTION_TWICE;
382 else
383 curr->namelength = (size_t)form_int_arg(long);
384 break;
386 /*
387 * Set the contents property.
388 */
389 case CURLFORM_PTRCONTENTS:
390 curr->flags |= HTTPPOST_PTRCONTENTS;
391 FALLTHROUGH();
392 case CURLFORM_COPYCONTENTS:
393 if(Curl_bufref_ptr(&curr->value))
394 retval = CURL_FORMADD_OPTION_TWICE;
395 else {
396 avalue = form_ptr_arg(char *);
397 if(avalue)
398 Curl_bufref_set(&curr->value, avalue, 0, NULL); /* No copy yet. */
399 else
400 retval = CURL_FORMADD_NULL;
401 }
402 break;
403 case CURLFORM_CONTENTSLENGTH: {
404 long clen = form_int_arg(long);
405 if(clen < 0)
406 retval = CURL_FORMADD_INCOMPLETE;
407 else
408 curr->contentslength = (curl_off_t)clen;
409 break;
410 }
412 case CURLFORM_CONTENTLEN: {
413 curl_off_t clen = form_int_arg(curl_off_t);
414 if(clen < 0)
415 retval = CURL_FORMADD_INCOMPLETE;
416 else {
417 curr->flags |= CURL_HTTPPOST_LARGE;
418 curr->contentslength = clen;
419 }
420 break;
421 }
423 /* Get contents from a given filename */
424 case CURLFORM_FILECONTENT:
425 if(curr->flags & (HTTPPOST_PTRCONTENTS | HTTPPOST_READFILE))
426 retval = CURL_FORMADD_OPTION_TWICE;
427 else {
428 avalue = form_ptr_arg(char *);
429 if(avalue) {
430 if(Curl_bufref_memdup0(&curr->value, avalue, strlen(avalue)))
431 retval = CURL_FORMADD_MEMORY;
432 else
433 curr->flags |= HTTPPOST_READFILE;
434 }
435 else
436 retval = CURL_FORMADD_NULL;
437 }
438 break;
440 /* We upload a file */
441 case CURLFORM_FILE:
442 avalue = form_ptr_arg(char *);
443 if(Curl_bufref_ptr(&curr->value)) {
444 if(curr->flags & HTTPPOST_FILENAME) {
445 if(avalue) {
446 form = forminfo_new();
447 if(!form ||
448 Curl_bufref_memdup0(&form->value, avalue, strlen(avalue))) {
449 curlx_free(form);
450 retval = CURL_FORMADD_MEMORY;
451 }
452 else {
453 forminfo_add(form, curr);
454 curr = form;
455 form = NULL;
456 }
457 }
458 else
459 retval = CURL_FORMADD_NULL;
460 }
461 else
462 retval = CURL_FORMADD_OPTION_TWICE;
463 }
464 else {
465 if(avalue) {
466 if(Curl_bufref_memdup0(&curr->value, avalue, strlen(avalue)))
467 retval = CURL_FORMADD_MEMORY;
468 else
469 curr->flags |= HTTPPOST_FILENAME;
470 }
471 else
472 retval = CURL_FORMADD_NULL;
473 }
474 break;
476 case CURLFORM_BUFFERPTR:
477 curr->flags |= HTTPPOST_PTRBUFFER | HTTPPOST_BUFFER;
478 if(curr->buffer)
479 retval = CURL_FORMADD_OPTION_TWICE;
480 else {
481 avalue = form_ptr_arg(char *);
482 if(avalue) {
483 curr->buffer = avalue; /* store for the moment */
484 /* Make value non-NULL to be accepted as fine */
485 Curl_bufref_set(&curr->value, avalue, 0, NULL);
486 }
487 else
488 retval = CURL_FORMADD_NULL;
489 }
490 break;
492 case CURLFORM_BUFFERLENGTH:
493 if(curr->bufferlength)
494 retval = CURL_FORMADD_OPTION_TWICE;
495 else
496 curr->bufferlength = (size_t)form_int_arg(long);
497 break;
499 case CURLFORM_STREAM:
500 curr->flags |= HTTPPOST_CALLBACK;
501 if(curr->userp)
502 retval = CURL_FORMADD_OPTION_TWICE;
503 else {
504 avalue = form_ptr_arg(char *);
505 if(avalue) {
506 curr->userp = avalue;
507 /* The following line is not strictly true but we derive a value
508 from this later on and we need this non-NULL to be accepted as
509 a fine form part */
510 Curl_bufref_set(&curr->value, avalue, 0, NULL);
511 }
512 else
513 retval = CURL_FORMADD_NULL;
514 }
515 break;
517 case CURLFORM_CONTENTTYPE:
518 avalue = form_ptr_arg(char *);
519 if(Curl_bufref_ptr(&curr->contenttype)) {
520 if(curr->flags & HTTPPOST_FILENAME) {
521 if(avalue) {
522 form = forminfo_new();
523 if(!form || Curl_bufref_memdup0(&form->contenttype, avalue,
524 strlen(avalue))) {
525 curlx_free(form);
526 retval = CURL_FORMADD_MEMORY;
527 }
528 else {
529 forminfo_add(form, curr);
530 curr = form;
531 form = NULL;
532 }
533 }
534 else
535 retval = CURL_FORMADD_NULL;
536 }
537 else
538 retval = CURL_FORMADD_OPTION_TWICE;
539 }
540 else if(avalue) {
541 if(Curl_bufref_memdup0(&curr->contenttype, avalue, strlen(avalue)))
542 retval = CURL_FORMADD_MEMORY;
543 }
544 else
545 retval = CURL_FORMADD_NULL;
546 break;
548 case CURLFORM_CONTENTHEADER: {
549 /* this "cast increases required alignment of target type" but
550 we consider it OK anyway */
551 struct curl_slist *list = form_ptr_arg(struct curl_slist *);
553 if(curr->contentheader)
554 retval = CURL_FORMADD_OPTION_TWICE;
555 else
556 curr->contentheader = list;
558 break;
559 }
560 case CURLFORM_FILENAME:
561 case CURLFORM_BUFFER:
562 avalue = form_ptr_arg(char *);
563 if(!avalue)
564 retval = CURL_FORMADD_NULL;
565 else if(Curl_bufref_ptr(&curr->showfilename))
566 retval = CURL_FORMADD_OPTION_TWICE;
567 else if(Curl_bufref_memdup0(&curr->showfilename, avalue, strlen(avalue)))
568 retval = CURL_FORMADD_MEMORY;
569 break;
571 default:
572 retval = CURL_FORMADD_UNKNOWN_OPTION;
573 break;
574 }
575 }
577 if(!retval)
578 retval = formadd_check(first_form, &newchain, &lastnode);
580 if(retval)
581 /* On error, free allocated fields for all nodes of the FormInfo linked
582 list without deallocating nodes. List nodes are deallocated later on */
583 free_formlist(first_form);
585 /* Always deallocate FormInfo linked list nodes without touching node
586 fields given that these have either been deallocated or are owned
587 now by the httppost linked list */
588 while(first_form) {
589 struct FormInfo *ptr = first_form->more;
590 curlx_free(first_form);
591 first_form = ptr;
592 }
594 if(!retval) {
595 /* Only if all is fine, link the new chain into the provided list */
596 if(*last_post)
597 (*last_post)->next = newchain;
598 else
599 (*httppost) = newchain;
601 (*last_post) = lastnode;
602 }
603 else
604 free_chain(newchain);
606 return retval;
607#undef form_ptr_arg
608#undef form_int_arg
609}
611/*
612 * curl_formadd() is a public API to add a section to the multipart formpost.
613 *
614 * @unittest: 1308
615 */
617CURLFORMcode curl_formadd(struct curl_httppost **httppost,
618 struct curl_httppost **last_post, ...)
619{
620 va_list arg;
621 CURLFORMcode form;
622 va_start(arg, last_post);
623 form = formadd(httppost, last_post, arg);
624 va_end(arg);
625 return form;
626}
628/*
629 * curl_formget()
630 * Serialize a curl_httppost struct.
631 * Returns 0 on success.
632 *
633 * @unittest: 1308
634 */
635int curl_formget(struct curl_httppost *form, void *arg,
636 curl_formget_callback append)
637{
638 CURLcode result;
639 curl_mimepart toppart;
641 /* Validate callback is provided */
642 if(!append)
643 return (int)CURLE_BAD_FUNCTION_ARGUMENT;
645 Curl_mime_initpart(&toppart); /* default form is empty */
646 result = Curl_getformdata(NULL, &toppart, form, NULL);
647 if(!result)
648 result = Curl_mime_prepare_headers(NULL, &toppart, "multipart/form-data",
649 NULL, MIMESTRATEGY_FORM);
651 while(!result) {
652 char buffer[8192];
653 size_t nread = Curl_mime_read(buffer, 1, sizeof(buffer), &toppart);
655 if(!nread)
656 break;
658 if(nread > sizeof(buffer) || append(arg, buffer, nread) != nread) {
659 result = CURLE_READ_ERROR;
660 if(nread == CURL_READFUNC_ABORT)
661 result = CURLE_ABORTED_BY_CALLBACK;
662 }
663 }
665 Curl_mime_cleanpart(&toppart);
666 return (int)result;
667}
669/*
670 * curl_formfree() is an external function to free up a whole form post
671 * chain
672 */
673void curl_formfree(struct curl_httppost *form)
674{
675 struct curl_httppost *next;
677 if(!form)
678 /* no form to free, get out of this */
679 return;
681 do {
682 next = form->next; /* the following form line */
684 /* recurse to sub-contents */
685 curl_formfree(form->more);
687 if(!(form->flags & HTTPPOST_PTRNAME))
688 curlx_free(form->name); /* free the name */
689 if(!(form->flags &
690 (HTTPPOST_PTRCONTENTS | HTTPPOST_BUFFER | HTTPPOST_CALLBACK)))
691 curlx_free(form->contents); /* free the contents */
692 curlx_free(form->contenttype); /* free the content type */
693 curlx_free(form->showfilename); /* free the faked filename */
694 curlx_free(form); /* free the struct */
695 form = next;
696 } while(form); /* continue */
697}
699/* Set mime part name, taking care of non null-terminated name string. */
700static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
701{
702 char *zname;
703 CURLcode result;
705 if(!name || !len)
706 return curl_mime_name(part, name);
707 zname = curlx_memdup0(name, len);
708 if(!zname)
709 return CURLE_OUT_OF_MEMORY;
710 result = curl_mime_name(part, zname);
711 curlx_free(zname);
712 return result;
713}
715/*
716 * Curl_getformdata() converts a linked list of "meta data" into a mime
717 * structure. The input list is in 'post', while the output is stored in
718 * mime part at '*finalform'.
719 *
720 * This function will not do a failf() for the potential memory failures but
721 * should for all other errors it spots. Note that this function MAY get a
722 * NULL pointer in the 'data' argument.
723 */
725CURLcode Curl_getformdata(CURL *data,
726 curl_mimepart *finalform,
727 struct curl_httppost *post,
728 curl_read_callback fread_func)
729{
730 CURLcode result = CURLE_OK;
731 curl_mime *form = NULL;
732 curl_mimepart *part;
733 struct curl_httppost *file;
735 Curl_mime_cleanpart(finalform); /* default form is empty */
737 if(!post)
738 return result; /* no input => no output! */
740 form = curl_mime_init(data);
741 if(!form)
742 result = CURLE_OUT_OF_MEMORY;
744 if(!result)
745 result = curl_mime_subparts(finalform, form);
747 /* Process each top part. */
748 for(; !result && post; post = post->next) {
749 /* If we have more than a file here, create a mime subpart and fill it. */
750 curl_mime *multipart = form;
751 if(post->more) {
752 part = curl_mime_addpart(form);
753 if(!part)
754 result = CURLE_OUT_OF_MEMORY;
755 if(!result)
756 result = setname(part, post->name, post->namelength);
757 if(!result) {
758 multipart = curl_mime_init(data);
759 if(!multipart)
760 result = CURLE_OUT_OF_MEMORY;
761 }
762 if(!result)
763 result = curl_mime_subparts(part, multipart);
764 }
766 /* Generate all the part contents. */
767 for(file = post; !result && file; file = file->more) {
768 /* Create the part. */
769 part = curl_mime_addpart(multipart);
770 if(!part)
771 result = CURLE_OUT_OF_MEMORY;
773 /* Set the headers. */
774 if(!result)
775 result = curl_mime_headers(part, file->contentheader, 0);
777 /* Set the content type. */
778 if(!result && file->contenttype)
779 result = curl_mime_type(part, file->contenttype);
781 /* Set field name. */
782 if(!result && !post->more)
783 result = setname(part, post->name, post->namelength);
785 /* Process contents. */
786 if(!result) {
787 curl_off_t clen = post->contentslength;
789 if(post->flags & CURL_HTTPPOST_LARGE)
790 clen = post->contentlen;
792 if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
793 if(!strcmp(file->contents, "-")) {
794 /* There are a few cases where the code below will not work; in
795 particular, freopen(stdin) by the caller is not guaranteed
796 to result as expected. This feature has been kept for backward
797 compatibility: use of "-" pseudo filename should be avoided. */
798#if defined(__clang__) && __clang_major__ >= 16
799#pragma clang diagnostic push
800#pragma clang diagnostic ignored "-Wcast-function-type-strict"
801#endif
802 result = curl_mime_data_cb(part, (curl_off_t)-1,
803 (curl_read_callback)fread,
804 curlx_fseek,
805 NULL, (void *)stdin);
806#if defined(__clang__) && __clang_major__ >= 16
807#pragma clang diagnostic pop
808#endif
809 }
810 else
811 result = curl_mime_filedata(part, file->contents);
812 if(!result && (post->flags & HTTPPOST_READFILE))
813 result = curl_mime_filename(part, NULL);
814 }
815 else if(post->flags & HTTPPOST_BUFFER)
816 result = curl_mime_data(part, post->buffer,
817 post->bufferlength ?
818 post->bufferlength : -1);
819 else if(post->flags & HTTPPOST_CALLBACK) {
820 /* the contents should be read with the callback and the size is set
821 with the contentslength */
822 if(!clen)
823 clen = -1;
824 result = curl_mime_data_cb(part, clen,
825 fread_func, NULL, NULL, post->userp);
826 }
827 else {
828 size_t uclen;
829 if(!clen)
830 uclen = CURL_ZERO_TERMINATED;
831 else
832 uclen = (size_t)clen;
833 result = curl_mime_data(part, post->contents, uclen);
834 }
835 }
837 /* Set fake filename. */
838 if(!result && post->showfilename &&
839 (post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
840 HTTPPOST_CALLBACK))))
841 result = curl_mime_filename(part, post->showfilename);
842 }
843 }
845 if(result)
846 Curl_mime_cleanpart(finalform);
848 return result;
849}
851#else /* if disabled */
852CURLFORMcode curl_formadd(struct curl_httppost **httppost,
853 struct curl_httppost **last_post, ...)
854{
855 (void)httppost;
856 (void)last_post;
857 return CURL_FORMADD_DISABLED;
858}
860int curl_formget(struct curl_httppost *form, void *arg,
861 curl_formget_callback append)
862{
863 (void)form;
864 (void)arg;
865 (void)append;
866 return CURL_FORMADD_DISABLED;
867}
869void curl_formfree(struct curl_httppost *form)
870{
871 (void)form;
872 /* Nothing to do. */
873}
875#endif /* if disabled */
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2007.5 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=7684 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=5d9e5def30f191563ed5e158c38baeb745463a7d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=7684 git.pkt.payload.max=7663 git.sideband.band1.bytes=7662 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e21b7a38ceecd4
- Request
- be994220…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format