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/* <DESC>
25 * Save failed transfer verbose log to disk
26 * </DESC>
27 */
28/*
29 *
30 * This example demonstrates per-transfer verbose logging to memory.
31 * The transfer's log is written to disk only if the transfer fails.
32 *
33 */
34#ifdef _MSC_VER
35#ifndef _CRT_SECURE_NO_WARNINGS
36#define _CRT_SECURE_NO_WARNINGS /* for fopen(), strerror(), vsnprintf() */
37#endif
38#endif
40#include <errno.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <limits.h>
44#include <string.h>
46#include <curl/curl.h>
48#ifdef _WIN32
49#include <windows.h>
50#define unlink _unlink
51#else
52#include <strings.h>
53#include <unistd.h>
54#endif
56struct mem {
57 /* 'buf' points to memory contents that is always null-terminated so that it
58 can be treated like a string if appropriate. 'recent' points to the most
59 recent data written to 'buf'. */
60 char *buf, *recent;
61 /* 'len' and 'allocsize' are the length and allocated size of 'buf' */
62 size_t len, allocsize;
63};
65struct transfer {
66 const char *url, *bodyfile, *logfile;
67 struct mem log;
68 FILE *bodyfp;
69 CURL *curl;
70};
72static void mem_reset(struct mem *mem)
73{
74 free(mem->buf);
75 mem->buf = NULL;
76 mem->recent = NULL;
77 mem->len = 0;
78 mem->allocsize = 0;
79}
81/* expand free buffer space to needed size. return -1 or 'needed'. */
82static int mem_need(struct mem *mem, size_t needed)
83{
84 char *newbuf;
85 size_t newsize;
87 if(needed > (unsigned)INT_MAX)
88 return -1;
90 if(needed <= (mem->allocsize - mem->len))
91 return (int)needed;
93 /* min 4k makes reallocations much less frequent when lengths are small */
94 newsize = needed < 4096 ? 4096 : needed;
96 newsize += mem->len;
98 if(newsize < mem->len || newsize > (unsigned)INT_MAX)
99 return -1;
101 newbuf = realloc(mem->buf, newsize);
103 if(!newbuf)
104 return -1;
106 if(mem->recent && mem->buf != newbuf)
107 mem->recent = newbuf + (mem->recent - mem->buf);
109 mem->buf = newbuf;
110 mem->allocsize = newsize;
112 return (int)needed;
113}
115static int mem_addn(struct mem *mem, const char *buf, size_t len)
116{
117 if(len + 1 < len || mem_need(mem, len + 1) < 0)
118 return -1;
119 mem->recent = mem->buf + mem->len;
120 memcpy(mem->recent, buf, len);
121 mem->len += len;
122 mem->buf[mem->len] = '\0';
123 return (int)len;
124}
126static int mem_add(struct mem *mem, const char *str)
127{
128 return mem_addn(mem, str, strlen(str));
129}
131#if defined(__GNUC__) || defined(__clang__)
132__attribute__((format(printf, 2, 3)))
133#endif
134static int mem_addf(struct mem *mem, const char *format, ...)
135{
136 int i, x;
137 va_list va;
139 /* we need about 100 chars or less to write 95% of lines */
140 x = 128;
142 /* first try: there is probably enough memory to write everything.
143 second try: there is definitely enough memory to write everything. */
144 for(i = 0; i < 2; ++i) {
145 if(x < 0 || mem_need(mem, (size_t)x + 1) < 0)
146 break;
148 va_start(va, format);
149 x = vsnprintf(mem->buf + mem->len, mem->allocsize - mem->len, format, va);
150 va_end(va);
152 if(x >= 0 && (size_t)x < (mem->allocsize - mem->len)) {
153 mem->recent = mem->buf + mem->len;
154 mem->len += (size_t)x;
155 return x;
156 }
158#ifdef _WIN32
159 /* Not all versions of Windows CRT vsnprintf are compliant with C99. Some
160 return -1 if buffer too small. Try _vscprintf to get the needed size. */
161 if(!i && x < 0) {
162 va_start(va, format);
163 x = _vscprintf(format, va);
164 va_end(va);
165 }
166#endif
167 }
169 if(mem->buf)
170 mem->buf[mem->len] = '\0';
171 return -1;
172}
174static int mydebug(CURL *curl, curl_infotype type,
175 char *data, size_t size, void *userdata)
176{
177 struct transfer *t = (struct transfer *)userdata;
178 static const char s_infotype[CURLINFO_END][3] = {
179 "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
181 (void)curl;
183 switch(type) {
184 case CURLINFO_TEXT:
185 case CURLINFO_HEADER_OUT:
186 case CURLINFO_HEADER_IN:
187 /* mem_addn is faster than passing large data as %s to mem_addf */
188 mem_addn(&t->log, s_infotype[type], 2);
189 mem_addn(&t->log, data, size);
190 if(!size || data[size - 1] != '\n')
191 mem_add(&t->log, "\n");
192 break;
193 default:
194 break;
195 }
197 return 0;
198}
200static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
201{
202 struct transfer *t = (struct transfer *)userdata;
204 return fwrite(ptr, size, nmemb, t->bodyfp);
205}
207int main(void)
208{
209 CURLcode result;
210 unsigned i;
211 int total_failed = 0;
212 char errbuf[CURL_ERROR_SIZE] = { 0, };
213 struct transfer transfer[2];
215 memset(transfer, 0, sizeof(transfer));
217 transfer[0].url = "https://httpbin.org/get";
218 transfer[0].bodyfile = "200.txt";
219 transfer[0].logfile = "200_transfer_log.txt";
221 transfer[1].url = "https://httpbin.org/status/400";
222 transfer[1].bodyfile = "400.txt";
223 transfer[1].logfile = "400_transfer_log.txt";
225 result = curl_global_init(CURL_GLOBAL_ALL);
226 if(result != CURLE_OK) {
227 fprintf(stderr, "curl_global_init failed\n");
228 return (int)result;
229 }
231 /* You could enable global tracing for extra verbosity when verbosity is
232 enabled for a transfer. */
233#if 0
234 curl_global_trace("all");
235#endif
237 for(i = 0; i < sizeof(transfer) / sizeof(transfer[0]); ++i) {
238 int failed = 0;
239 struct transfer *t = &transfer[i];
241 t->curl = curl_easy_init();
243 if(!t->curl) {
244 fprintf(stderr, "curl_easy_init failed\n");
245 curl_global_cleanup();
246 return 1;
247 }
249 curl_easy_setopt(t->curl, CURLOPT_URL, t->url);
251 /* Enable following redirects */
252 curl_easy_setopt(t->curl, CURLOPT_FOLLOWLOCATION, 1L);
254 /* Enable verbose logging to memory */
255 curl_easy_setopt(t->curl, CURLOPT_VERBOSE, 1L);
256 curl_easy_setopt(t->curl, CURLOPT_DEBUGFUNCTION, mydebug);
257 curl_easy_setopt(t->curl, CURLOPT_DEBUGDATA, t);
259 /* Enable writing the body to a file */
260 curl_easy_setopt(t->curl, CURLOPT_WRITEFUNCTION, write_cb);
261 curl_easy_setopt(t->curl, CURLOPT_WRITEDATA, t);
263 /* Enable immediate error on HTTP status codes >= 400 in most cases,
264 instead of downloading the body to a file */
265 curl_easy_setopt(t->curl, CURLOPT_FAILONERROR, 1L);
267 /* Enable detailed error messages */
268 curl_easy_setopt(t->curl, CURLOPT_ERRORBUFFER, errbuf);
270 mem_addf(&t->log, "Downloading %s to file %s\n", t->url, t->bodyfile);
271 printf("%s", t->log.recent);
273 /* Create the body file */
274 t->bodyfp = fopen(t->bodyfile, "wb");
276 if(t->bodyfp) {
277 /* Perform the transfer */
278 result = curl_easy_perform(t->curl);
280 /* Save the body file */
281 fclose(t->bodyfp);
282 t->bodyfp = NULL;
284 if(result == CURLE_OK) {
285 /* You could retrieve more information about the transfer here via
286 curl_easy_getinfo and mark the transfer as failed if needed. */
287 mem_addf(&t->log, "Transfer successful.\n");
288 fprintf(stderr, "%s", t->log.recent);
289 failed = 0;
290 }
291 else {
292 mem_addf(&t->log, "Transfer failed: (%d) %s\n", (int)result,
293 (errbuf[0] ? errbuf : curl_easy_strerror(result)));
294 fprintf(stderr, "%s", t->log.recent);
295 failed = 1;
296 }
297 }
298 else {
299 mem_addf(&t->log, "Failed to create body output file %s: %s\n",
300 t->bodyfile, strerror(errno));
301 fprintf(stderr, "%s", t->log.recent);
302 failed = 1;
303 }
305 if(failed) {
306 FILE *fp = fopen(t->logfile, "wb");
308 if(fp && fwrite(t->log.buf, 1, t->log.len, fp) == t->log.len)
309 fprintf(stderr, "Transfer log written to %s\n", t->logfile);
310 else {
311 fprintf(stderr, "Failed to write transfer log to %s: %s\n",
312 t->logfile, strerror(errno));
313 }
315 if(fp)
316 fclose(fp);
318 /* Depending on how the transfer failed a body file may or may not have
319 been written, and you may or may not want it. */
320 unlink(t->bodyfile);
322 ++total_failed;
323 }
325 mem_reset(&t->log);
327 curl_easy_cleanup(t->curl);
329 t->curl = NULL;
331 printf("\n");
332 }
334 curl_global_cleanup();
336 return total_failed ? 1 : 0;
337}
Diagnostics4 git calls · 35 ms
CallStatusSizeTime035 ms
discover200152 B2 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 · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 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=30 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
fetch2003.4 KiB · hit30 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3446 git.response.cache=HIT git.response.duration_ms=30 git.fetch.wants=19e2a232e84ad53ee6280a96bb93f6fd0279b3cd 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=3446 git.pkt.payload.max=3425 git.sideband.band1.bytes=3424 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
- a5e2d6c409b1689d
- Request
- 890e62b2…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format