Artifactspublic-v0

curl

master
ply4 git calls639 ms295 KiB2 cachedprotocol v2
991 lines26 KiBRaw
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 "tool_setup.h"
26#include "tool_cfgable.h"
27#include "tool_msgs.h"
28#include "tool_paramhlp.h"
29#include "tool_formparse.h"
30#include "tool_parsecfg.h"
32/* tool_mime functions. */
33static struct tool_mime *tool_mime_new(struct tool_mime *parent,
34 toolmimekind kind)
36 struct tool_mime *m = curlx_calloc(1, sizeof(*m));
38 if(m) {
39 m->kind = kind;
40 m->parent = parent;
41 if(parent) {
42 m->prev = parent->subparts;
43 parent->subparts = m;
44 }
45 }
46 return m;
49static struct tool_mime *tool_mime_new_parts(struct tool_mime *parent)
51 return tool_mime_new(parent, TOOLMIME_PARTS);
54static struct tool_mime *tool_mime_new_data(struct tool_mime *parent,
55 char *mime_data)
57 char *mime_data_copy;
58 struct tool_mime *m = NULL;
60 mime_data_copy = curlx_strdup(mime_data);
61 if(mime_data_copy) {
62 m = tool_mime_new(parent, TOOLMIME_DATA);
63 if(!m)
64 curlx_free(mime_data_copy);
65 else
66 m->data = mime_data_copy;
67 }
68 return m;
71/*
72 * unsigned size_t to signed curl_off_t
73 */
74#define CURL_MASK_UCOFFT ((unsigned CURL_TYPEOF_CURL_OFF_T)~0)
75#define CURL_MASK_SCOFFT (CURL_MASK_UCOFFT >> 1)
77static curl_off_t uztoso(size_t uznum)
79#ifdef __INTEL_COMPILER
80# pragma warning(push)
81# pragma warning(disable:810) /* conversion may lose significant bits */
82#elif defined(_MSC_VER)
83# pragma warning(push)
84# pragma warning(disable:4310) /* cast truncates constant value */
85#endif
87 DEBUGASSERT(uznum <= (size_t)CURL_MASK_SCOFFT);
88 return (curl_off_t)(uznum & (size_t)CURL_MASK_SCOFFT);
90#if defined(__INTEL_COMPILER) || defined(_MSC_VER)
91# pragma warning(pop)
92#endif
95static struct tool_mime *tool_mime_new_filedata(struct tool_mime *parent,
96 const char *filename,
97 bool isremotefile,
98 CURLcode *errcode)
100 CURLcode result = CURLE_OK;
101 struct tool_mime *m = NULL;
103 *errcode = CURLE_OUT_OF_MEMORY;
104 if(strcmp(filename, "-")) {
105 /* This is a normal file. */
106 char *filedup = curlx_strdup(filename);
107 if(filedup) {
108 m = tool_mime_new(parent, TOOLMIME_FILE);
109 if(!m)
110 curlx_free(filedup);
111 else {
112 m->data = filedup;
113 if(!isremotefile)
114 m->kind = TOOLMIME_FILEDATA;
115 *errcode = CURLE_OK;
116 }
117 }
118 }
119 else { /* Standard input. */
120 int fd = fileno(stdin);
121 char *data = NULL;
122 curl_off_t size;
123 curl_off_t origin;
124 curlx_struct_stat sbuf;
126 CURL_BINMODE(stdin);
127 origin = ftell(stdin);
128 /* If stdin is a regular file, do not buffer data but read it
129 when needed. */
130 if(fd >= 0 && origin >= 0 && !curlx_fstat(fd, &sbuf) &&
131#ifdef __VMS
132 sbuf.st_fab_rfm != FAB$C_VAR && sbuf.st_fab_rfm != FAB$C_VFC &&
133#endif
134 S_ISREG(sbuf.st_mode)) {
135 size = sbuf.st_size - origin;
136 if(size < 0)
137 size = 0;
138 }
139 else { /* Not suitable for direct use, buffer stdin data. */
140 size_t stdinsize = 0;
142 switch(file2memory(&data, &stdinsize, stdin)) {
143 case PARAM_NO_MEM:
144 return m;
145 case PARAM_READ_ERROR:
146 result = CURLE_READ_ERROR;
147 break;
148 default:
149 if(!stdinsize) {
150 /* Zero-length data has been freed. Re-create it. */
151 data = curlx_strdup("");
152 if(!data)
153 return m;
154 }
155 break;
156 }
157 size = uztoso(stdinsize);
158 origin = 0;
159 }
160 m = tool_mime_new(parent, TOOLMIME_STDIN);
161 if(!m)
162 curlx_safefree(data);
163 else {
164 m->data = data;
165 m->origin = origin;
166 m->size = size;
167 m->curpos = 0;
168 if(!isremotefile)
169 m->kind = TOOLMIME_STDINDATA;
170 *errcode = result;
171 }
172 }
173 return m;
176void tool_mime_free(struct tool_mime *mime)
178 if(mime) {
179 if(mime->subparts)
180 tool_mime_free(mime->subparts);
181 if(mime->prev)
182 tool_mime_free(mime->prev);
183 curlx_safefree(mime->name);
184 curlx_safefree(mime->filename);
185 curlx_safefree(mime->type);
186 curlx_safefree(mime->encoder);
187 curlx_safefree(mime->data);
188 curl_slist_free_all(mime->headers);
189 curlx_free(mime);
190 }
193/* Mime part callbacks for stdin. */
194static size_t tool_mime_stdin_read(char *buffer,
195 size_t size, size_t nitems, void *arg)
197 struct tool_mime *sip = (struct tool_mime *)arg;
198 curl_off_t bytesleft;
199 (void)size; /* Always 1: ignored. */
201 if(sip->size >= 0) {
202 if(sip->curpos >= sip->size)
203 return 0; /* At eof. */
204 bytesleft = sip->size - sip->curpos;
205 if(uztoso(nitems) > bytesleft)
206 nitems = curlx_sotouz(bytesleft);
207 }
208 if(nitems) {
209 if(sip->data) {
210 /* Return data from memory. */
211 memcpy(buffer, sip->data + curlx_sotouz(sip->curpos), nitems);
212 }
213 else {
214 /* Read from stdin. */
215 nitems = fread(buffer, 1, nitems, stdin);
216 if(ferror(stdin)) {
217 char errbuf[STRERROR_LEN];
218 /* Show error only once. */
219 warnf("Failed to read from stdin: %s",
220 curlx_strerror(errno, errbuf, sizeof(errbuf)));
221 return CURL_READFUNC_ABORT;
222 }
223 }
224 sip->curpos += uztoso(nitems);
225 }
226 return nitems;
229static int tool_mime_stdin_seek(void *instream, curl_off_t offset, int whence)
231 struct tool_mime *sip = (struct tool_mime *)instream;
233 switch(whence) {
234 case SEEK_CUR:
235 offset += sip->curpos;
236 break;
237 case SEEK_END:
238 offset += sip->size;
239 break;
240 }
241 if(offset < 0)
242 return CURL_SEEKFUNC_CANTSEEK;
243 if(!sip->data && curlx_fseek(stdin, offset + sip->origin, SEEK_SET))
244 return CURL_SEEKFUNC_CANTSEEK;
245 sip->curpos = offset;
246 return CURL_SEEKFUNC_OK;
249/* Translate an internal mime tree into a libcurl mime tree. */
251#define MAX_FORMPARTS 100000 /* arbitrarily picked */
253static CURLcode tool2curlparts(CURL *curl, struct tool_mime *m,
254 curl_mime *mime)
256 CURLcode result = CURLE_OK;
257 struct tool_mime *curr;
258 struct tool_mime **nodes = NULL;
259 int count;
260 int i;
262 if(!m)
263 return CURLE_OK;
265 for(curr = m, count = 0; curr; curr = curr->prev) {
266 if(count > MAX_FORMPARTS)
267 return CURLE_BAD_FUNCTION_ARGUMENT;
268 count++;
269 }
271 nodes = curlx_malloc(sizeof(struct tool_mime *) * count);
272 if(!nodes)
273 return CURLE_OUT_OF_MEMORY;
275 /* populate array from the end to the beginning */
276 curr = m;
277 for(i = count - 1; i >= 0; i--) {
278 nodes[i] = curr;
279 curr = curr->prev;
280 }
282 for(i = 0; i < count; i++) {
283 struct tool_mime *node = nodes[i];
284 curl_mimepart *part = NULL;
285 curl_mime *submime = NULL;
286 const char *filename = node->filename;
288 part = curl_mime_addpart(mime);
289 if(!part) {
290 result = CURLE_OUT_OF_MEMORY;
291 break;
292 }
294 switch(node->kind) {
295 case TOOLMIME_PARTS:
296 result = tool2curlmime(curl, node, &submime);
297 if(!result) {
298 result = curl_mime_subparts(part, submime);
299 if(result)
300 curl_mime_free(submime);
301 }
302 break;
304 case TOOLMIME_DATA:
305 result = curl_mime_data(part, node->data, CURL_ZERO_TERMINATED);
306 break;
308 case TOOLMIME_FILE:
309 case TOOLMIME_FILEDATA:
310 result = curl_mime_filedata(part, node->data);
311 if(!result && node->kind == TOOLMIME_FILEDATA && !filename)
312 result = curl_mime_filename(part, NULL);
313 break;
315 case TOOLMIME_STDIN:
316 if(!filename)
317 filename = "-";
318 FALLTHROUGH();
319 case TOOLMIME_STDINDATA:
320 result = curl_mime_data_cb(part, node->size,
321 tool_mime_stdin_read,
322 tool_mime_stdin_seek,
323 NULL, node);
324 break;
326 default:
327 /* Other cases not possible in this context. */
328 break;
329 }
331 /* Common part configuration */
332 if(!result && filename)
333 result = curl_mime_filename(part, filename);
334 if(!result)
335 result = curl_mime_type(part, node->type);
336 if(!result)
337 result = curl_mime_headers(part, node->headers, 0);
338 if(!result)
339 result = curl_mime_encoder(part, node->encoder);
340 if(!result)
341 result = curl_mime_name(part, node->name);
343 if(result)
344 break;
345 }
347 curlx_free(nodes);
348 return result;
351CURLcode tool2curlmime(CURL *curl, struct tool_mime *m, curl_mime **mime)
353 CURLcode result = CURLE_OK;
355 *mime = curl_mime_init(curl);
356 if(!*mime)
357 result = CURLE_OUT_OF_MEMORY;
358 else
359 result = tool2curlparts(curl, m->subparts, *mime);
360 if(result) {
361 curl_mime_free(*mime);
362 *mime = NULL;
363 }
364 return result;
367/*
368 * helper function to get a word from form param
369 * after call get_param_word, str either point to string end
370 * or point to any of end chars.
371 */
372static char *get_param_word(char **str, char **end_pos, char endchar)
374 char *ptr = *str;
375 /* the first non-space char is here */
376 char *word_begin = ptr;
377 char *ptr2;
378 char *escape = NULL;
380 if(*ptr == '"') {
381 ++ptr;
382 while(*ptr) {
383 if(*ptr == '\\' && (ptr[1] == '\\' || ptr[1] == '"')) {
384 /* remember the first escape position */
385 if(!escape)
386 escape = ptr;
387 /* skip escape of back-slash or double-quote */
388 ptr += 2;
389 continue;
390 }
391 if(*ptr == '"') {
392 bool trailing_data = FALSE;
393 *end_pos = ptr;
394 if(escape) {
395 /* has escape, we restore the unescaped string here */
396 ptr = ptr2 = escape;
397 do {
398 if(*ptr == '\\' && (ptr[1] == '\\' || ptr[1] == '"'))
399 ++ptr;
400 *ptr2++ = *ptr++;
401 } while(ptr < *end_pos);
402 *end_pos = ptr2;
403 }
404 ++ptr;
405 while(*ptr && *ptr != ';' && *ptr != endchar) {
406 if(!ISSPACE(*ptr))
407 trailing_data = TRUE;
408 ++ptr;
409 }
410 if(trailing_data)
411 warnf("Trailing data after quoted form parameter");
412 *str = ptr;
413 return word_begin + 1;
414 }
415 ++ptr;
416 }
417 /* end quote is missing, treat it as non-quoted. */
418 ptr = word_begin;
419 }
421 while(*ptr && *ptr != ';' && *ptr != endchar)
422 ++ptr;
423 *str = *end_pos = ptr;
424 return word_begin;
427/* Append slist item and return -1 if failed. */
428static int slist_append(struct curl_slist **plist, const char *data)
430 struct curl_slist *s = curl_slist_append(*plist, data);
432 if(!s)
433 return -1;
435 *plist = s;
436 return 0;
439#define HEADER_LINE_BUFFER_SIZE 8192
441/* Read headers from a file and append to list.
442 Return zero on success, non-zero on error. */
443static int read_field_headers(FILE *fp, struct curl_slist **pheaders)
445 struct dynbuf line;
446 bool error = FALSE;
447 int err = 0;
449 curlx_dyn_init(&line, HEADER_LINE_BUFFER_SIZE);
450 while(my_get_line(fp, &line, &error, NULL)) {
451 const char *ptr = curlx_dyn_ptr(&line);
452 size_t len = curlx_dyn_len(&line);
453 bool folded = FALSE;
454 if(ptr[0] == '#') /* comment */
455 continue;
456 else if(ptr[0] == ' ') /* a continuation from the line before */
457 folded = TRUE;
458 /* trim off trailing CRLFs and whitespaces */
459 while(len && (ISNEWLINE(ptr[len - 1]) || ISBLANK(ptr[len - 1])))
460 len--;
462 if(!len)
463 continue;
464 curlx_dyn_setlen(&line, len); /* set the new length */
466 if(folded && *pheaders) {
467 /* append this new line onto the previous line */
468 struct dynbuf amend;
469 struct curl_slist *l = *pheaders;
470 curlx_dyn_init(&amend, HEADER_LINE_BUFFER_SIZE);
471 /* find the last node */
472 while(l && l->next)
473 l = l->next;
474 /* add both parts */
475 if(curlx_dyn_add(&amend, l->data) || curlx_dyn_addn(&amend, ptr, len)) {
476 err = -1;
477 break;
478 }
479 curl_free(l->data);
480 /* we use maprintf here to make it a libcurl alloc, to match the previous
481 curl_slist_append */
482 l->data = curl_maprintf("%s", curlx_dyn_ptr(&amend));
483 curlx_dyn_free(&amend);
484 if(!l->data)
485 err = -1;
486 }
487 else
488 err = slist_append(pheaders, ptr);
490 if(err) {
491 errorf("Out of memory for field headers");
492 err = -1;
493 break;
494 }
495 }
496 if(error && !err) {
497 errorf("Failed to read field headers");
498 err = -1;
499 }
500 curlx_dyn_free(&line);
501 return err;
504static void param_type(char **ptr, char **ptype, char **endct, char *sep)
506 char *p = *ptr;
507 size_t tlen;
508 for(p += CURL_CSTRLEN("type="); ISBLANK(*p); p++)
509 ;
510 /* set type pointer */
511 *ptype = p;
513 /* find end of content-type */
514 tlen = strcspn(p, "()<>@,;:\\\"[]?=\r\n ");
515 p += tlen;
516 *endct = p;
517 *sep = *p;
518 *ptr = p;
521static void param_filename(char **ptr, char **endct, char **pfilename,
522 char endchar, char *sep)
524 char *p = *ptr;
525 char *endpos;
526 char *tp;
528 if(*endct) {
529 **endct = '\0';
530 *endct = NULL;
531 }
532 for(p += CURL_CSTRLEN("filename="); ISBLANK(*p); p++)
533 ;
534 tp = p;
535 *pfilename = get_param_word(&p, &endpos, endchar);
536 /* If not quoted, strip trailing spaces. */
537 if(*pfilename == tp)
538 while(endpos > *pfilename && ISBLANK(endpos[-1]))
539 endpos--;
540 *sep = *p;
541 *endpos = '\0';
542 *ptr = p;
545static int param_headers(char **ptr, char **endct,
546 struct curl_slist **pheaders, char endchar, char *sep)
548 char *p = *ptr;
549 char *endpos;
550 char *tp;
552 if(*endct) {
553 **endct = '\0';
554 *endct = NULL;
555 }
556 p += CURL_CSTRLEN("headers=");
557 if(*p == '@' || *p == '<') {
558 char *hdrfile;
559 FILE *fp;
560 /* Read headers from a file. */
561 do {
562 p++;
563 } while(ISBLANK(*p));
564 tp = p;
565 hdrfile = get_param_word(&p, &endpos, endchar);
566 /* If not quoted, strip trailing spaces. */
567 if(hdrfile == tp)
568 while(endpos > hdrfile && ISBLANK(endpos[-1]))
569 endpos--;
570 *sep = *p;
571 *endpos = '\0';
572 fp = curlx_fopen(hdrfile, FOPEN_READTEXT);
573 if(!fp) {
574 char errbuf[STRERROR_LEN];
575 warnf("Cannot read from %s: %s", hdrfile,
576 curlx_strerror(errno, errbuf, sizeof(errbuf)));
577 }
578 else {
579 int i = read_field_headers(fp, pheaders);
581 curlx_fclose(fp);
582 if(i) {
583 curl_slist_free_all(*pheaders);
584 return -1;
585 }
586 }
587 }
588 else {
589 char *hdr;
591 while(ISBLANK(*p))
592 p++;
593 tp = p;
594 hdr = get_param_word(&p, &endpos, endchar);
595 /* If not quoted, strip trailing spaces. */
596 if(hdr == tp)
597 while(endpos > hdr && ISBLANK(endpos[-1]))
598 endpos--;
599 *sep = *p;
600 *endpos = '\0';
601 if(slist_append(pheaders, hdr)) {
602 errorf("Out of memory for field header");
603 curl_slist_free_all(*pheaders);
604 return -1;
605 }
606 }
607 *ptr = p;
608 return 0;
611static void param_encoder(char **ptr, char **endct, char **pencoder,
612 char endchar, char *sep)
614 char *p = *ptr;
615 char *endpos;
616 char *tp;
618 if(*endct) {
619 **endct = '\0';
620 *endct = NULL;
621 }
622 for(p += CURL_CSTRLEN("encoder="); ISBLANK(*p); p++)
623 ;
624 tp = p;
625 *pencoder = get_param_word(&p, &endpos, endchar);
626 /* If not quoted, strip trailing spaces. */
627 if(*pencoder == tp)
628 while(endpos > *pencoder && ISBLANK(endpos[-1]))
629 endpos--;
630 *sep = *p;
631 *endpos = '\0';
632 *ptr = p;
635/**
636 * Parses a single parameter part and its associated metadata from a string.
637 *
638 * This function extracts a primary data word and scans for optional
639 * semicolon-separated attributes including 'type=', 'filename=', 'headers=',
640 * and 'encoder='.
641 *
642 * Used for parsing command-line form arguments or multipart/form-data
643 * attributes.
644 *
645 * @param endchar The character that signifies the end of the entire
646 * parameter block (e.g., ',' or '\0').
647 * @param str Pointer to the current position in the input string.
648 * Updated to point at the delimiter or terminator that
649 * ended the parsed part.
650 * @param pdata Pointer to a char * that receives the primary data word.
651 * @param ptype [out] Optional. Receives the extracted 'type=' value.
652 * @param pfilename [out] Optional. Receives the extracted 'filename=' value.
653 * @param pencoder [out] Optional. Receives the extracted 'encoder=' value.
654 * @param pheaders [out] Optional. Receives a pointer to a curl_slist
655 * containing extracted 'headers='.
656 *
657 * @return The character that terminated the parsing (casted to int),
658 * or -1 on memory or parsing error.
659 */
661static int get_param_part(char endchar,
662 char **str, char **pdata, char **ptype,
663 char **pfilename, char **pencoder,
664 struct curl_slist **pheaders)
666 char *p = *str;
667 char *type = NULL;
668 char *filename = NULL;
669 char *encoder = NULL;
670 char *endpos;
671 char *tp;
672 char sep;
673 char *endct = NULL;
674 struct curl_slist *headers = NULL;
676 if(ptype)
677 *ptype = NULL;
678 if(pfilename)
679 *pfilename = NULL;
680 if(pheaders)
681 *pheaders = NULL;
682 if(pencoder)
683 *pencoder = NULL;
684 while(ISBLANK(*p))
685 p++;
686 tp = p;
687 *pdata = get_param_word(&p, &endpos, endchar);
688 /* If not quoted, strip trailing spaces. */
689 if(*pdata == tp)
690 while(endpos > *pdata && ISBLANK(endpos[-1]))
691 endpos--;
692 sep = *p;
693 *endpos = '\0';
694 while(sep == ';') {
695 while(p++ && ISBLANK(*p))
696 ;
698 if(!endct && checkprefix("type=", p))
699 param_type(&p, &type, &endct, &sep);
700 else if(checkprefix("filename=", p))
701 param_filename(&p, &endct, &filename, endchar, &sep);
702 else if(checkprefix("headers=", p)) {
703 if(param_headers(&p, &endct, &headers, endchar, &sep))
704 return -1;
705 }
706 else if(checkprefix("encoder=", p))
707 param_encoder(&p, &endct, &encoder, endchar, &sep);
708 else if(endct) {
709 /* This is part of content type. */
710 for(endct = p; *p && *p != ';' && *p != endchar; p++)
711 if(!ISBLANK(*p))
712 endct = p + 1;
713 sep = *p;
714 }
715 else {
716 /* unknown prefix, skip to next block */
717 char *unknown = get_param_word(&p, &endpos, endchar);
719 sep = *p;
720 *endpos = '\0';
721 if(*unknown)
722 warnf("skip unknown form field: %s", unknown);
723 }
724 }
726 /* Terminate content type. */
727 if(endct)
728 *endct = '\0';
730 if(ptype)
731 *ptype = type;
732 else if(type)
733 warnf("Field content type not allowed here: %s", type);
735 if(pfilename)
736 *pfilename = filename;
737 else if(filename)
738 warnf("Field filename not allowed here: %s", filename);
740 if(pencoder)
741 *pencoder = encoder;
742 else if(encoder)
743 warnf("Field encoder not allowed here: %s", encoder);
745 if(pheaders)
746 *pheaders = headers;
747 else if(headers) {
748 warnf("Field headers not allowed here: %s", headers->data);
749 curl_slist_free_all(headers);
750 }
752 *str = p;
753 return sep & 0xFF;
756/***************************************************************************
757 *
758 * formparse()
759 *
760 * Reads a 'name=value' parameter and builds the appropriate linked list.
761 *
762 * If the value is of the form '<filename', field data is read from the
763 * given file.
765 * Specify files to upload with 'name=@filename', or 'name=@"filename"'
766 * in case the filename contain ',' or ';'. Supports specified
767 * given Content-Type of the files. Such as ';type=<content-type>'.
768 *
769 * If literal_value is set, any initial '@' or '<' in the value string
770 * loses its special meaning, as does any embedded ';type='.
771 *
772 * You may specify more than one file for a single name (field). Specify
773 * multiple files by writing it like:
774 *
775 * 'name=@filename,filename2,filename3'
776 *
777 * or use double-quotes quote the filename:
778 *
779 * 'name=@"filename","filename2","filename3"'
780 *
781 * If you want content-types specified for each too, write them like:
782 *
783 * 'name=@filename;type=image/gif,filename2,filename3'
784 *
785 * If you want custom headers added for a single part, write them in a separate
786 * file and do like this:
787 *
788 * 'name=foo;headers=@headerfile' or why not
789 * 'name=@filename;headers=@headerfile'
790 *
791 * To upload a file, but to fake the filename that is included in the
792 * formpost, do like this:
793 *
794 * 'name=@filename;filename=/dev/null' or quote the faked filename like:
795 * 'name=@filename;filename="play, play, and play.txt"'
796 *
797 * If filename/path contains ',' or ';', it must be quoted by double-quotes,
798 * else curl fails to figure out the correct filename. if the filename to be
799 * quoted contains '"' or '\', '"' and '\' must be escaped by backslash.
800 *
801 ***************************************************************************/
803#define SET_TOOL_MIME_PTR(m, field) \
804 do { \
805 if(field) { \
806 (m)->field = curlx_strdup(field); \
807 if(!(m)->field) \
808 goto fail; \
809 } \
810 } while(0)
812int formparse(const char *input,
813 struct tool_mime **mimeroot,
814 struct tool_mime **mimecurrent,
815 bool literal_value)
817 /* input MUST be a string in the format 'name=contents' and we build
818 a linked list with the info */
819 char *name = NULL;
820 char *contents = NULL;
821 char *contp;
822 char *data;
823 char *type = NULL;
824 char *filename = NULL;
825 char *encoder = NULL;
826 struct curl_slist *headers = NULL;
827 struct tool_mime *part = NULL;
828 CURLcode result;
829 int err = 1;
831 /* Allocate the main mime structure if needed. */
832 if(!*mimecurrent) {
833 *mimeroot = tool_mime_new_parts(NULL);
834 if(!*mimeroot)
835 goto fail;
836 *mimecurrent = *mimeroot;
837 }
839 /* Make a copy we can overwrite. */
840 contents = curlx_strdup(input);
841 if(!contents)
842 goto fail;
844 /* Scan for the end of the name. */
845 contp = strchr(contents, '=');
846 if(contp) {
847 int sep = '\0';
848 if(contp > contents)
849 name = contents;
850 *contp++ = '\0';
852 if(*contp == '(' && !literal_value) {
853 /* Starting a multipart. */
854 sep = get_param_part('\0', &contp, &data, &type, NULL, NULL, &headers);
855 if(sep < 0)
856 goto fail;
857 part = tool_mime_new_parts(*mimecurrent);
858 if(!part)
859 goto fail;
860 *mimecurrent = part;
861 part->headers = headers;
862 headers = NULL;
863 SET_TOOL_MIME_PTR(part, type);
864 }
865 else if(!name && !strcmp(contp, ")") && !literal_value) {
866 /* Ending a multipart. */
867 if(*mimecurrent == *mimeroot) {
868 warnf("no multipart to terminate");
869 goto fail;
870 }
871 *mimecurrent = (*mimecurrent)->parent;
872 }
873 else if('@' == contp[0] && !literal_value) {
875 /* we use the @-letter to indicate filename(s) */
877 struct tool_mime *subparts = NULL;
879 do {
880 /* since this was a file, it may have a content-type specifier
881 at the end too, or a filename. Or both. */
882 ++contp;
883 sep = get_param_part(',', &contp,
884 &data, &type, &filename, &encoder, &headers);
885 if(sep < 0) {
886 goto fail;
887 }
889 /* now contp point to comma or string end.
890 If more files to come, make sure we have multiparts. */
891 if(!subparts) {
892 if(sep != ',') /* If there is a single file. */
893 subparts = *mimecurrent;
894 else {
895 subparts = tool_mime_new_parts(*mimecurrent);
896 if(!subparts)
897 goto fail;
898 }
899 }
901 /* Store that file in a part. */
902 part = tool_mime_new_filedata(subparts, data, TRUE, &result);
903 if(!part)
904 goto fail;
905 part->headers = headers;
906 headers = NULL;
907 if(result == CURLE_READ_ERROR) {
908 /* An error occurred while reading stdin: if read has started,
909 issue the error now. Else, delay it until processed by libcurl. */
910 if(part->size > 0) {
911 warnf("error while reading standard input");
912 goto fail;
913 }
914 curlx_safefree(part->data);
915 part->size = -1;
916 result = CURLE_OK;
917 }
918 SET_TOOL_MIME_PTR(part, filename);
919 SET_TOOL_MIME_PTR(part, type);
920 SET_TOOL_MIME_PTR(part, encoder);
922 /* *contp could be '\0', so we check with the delimiter */
923 } while(sep == ','); /* loop if there is another filename */
924 part = (*mimecurrent)->subparts; /* Set name on group. */
925 }
926 else {
927 if(*contp == '<' && !literal_value) {
928 ++contp;
929 sep = get_param_part('\0', &contp,
930 &data, &type, NULL, &encoder, &headers);
931 if(sep < 0)
932 goto fail;
934 part = tool_mime_new_filedata(*mimecurrent, data, FALSE, &result);
935 if(!part)
936 goto fail;
937 part->headers = headers;
938 headers = NULL;
939 if(result == CURLE_READ_ERROR) {
940 /* An error occurred while reading stdin: if read has started,
941 issue the error now. Else, delay it until processed by
942 libcurl. */
943 if(part->size > 0) {
944 warnf("error while reading standard input");
945 goto fail;
946 }
947 curlx_safefree(part->data);
948 part->size = -1;
949 result = CURLE_OK;
950 }
951 }
952 else {
953 if(literal_value)
954 data = contp;
955 else {
956 sep = get_param_part('\0', &contp,
957 &data, &type, &filename, &encoder, &headers);
958 if(sep < 0)
959 goto fail;
960 }
962 part = tool_mime_new_data(*mimecurrent, data);
963 if(!part)
964 goto fail;
965 part->headers = headers;
966 headers = NULL;
967 }
969 SET_TOOL_MIME_PTR(part, filename);
970 SET_TOOL_MIME_PTR(part, type);
971 SET_TOOL_MIME_PTR(part, encoder);
973 if(sep) {
974 *contp = (char)sep;
975 warnf("garbage at end of field specification: %s", contp);
976 }
977 }
979 /* Set part name. */
980 SET_TOOL_MIME_PTR(part, name);
981 }
982 else {
983 warnf("Illegally formatted input field");
984 goto fail;
985 }
986 err = 0;
987fail:
988 curlx_safefree(contents);
989 curl_slist_free_all(headers);
990 return err;
Diagnostics4 git calls · 639 ms
CallStatusSizeTime0639 ms
discover200152 B5 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=5
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 · hit6 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=6
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=6
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=2
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=86
Open pack map
fetch2008.3 KiB630 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=630
git.response.bytes=8484
git.response.cache=MISS
git.response.duration_ms=630
git.fetch.wants=0366080d617ccdf4ad00c864367040396ab54429
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=8484
git.pkt.payload.max=8463
git.sideband.band1.bytes=8462
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
a5e1ee93ba6befe9
Request
58628d9a…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format