Artifactspublic-v0

curl

master
ply4 git calls33 ms289 KiB3 cachedprotocol v2
295 lines6.4 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/* <DESC>
25 * Get a webpage, extract the title with libxml.
26 * </DESC>
27 *
28 * Written by Lars Nilsson
29 *
30 * GNU C++ compile command line suggestion (edit paths accordingly):
31 *
32 * g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \
33 * -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2
34 */
35#include <stdio.h>
36#include <string.h>
37#include <stdlib.h>
38#include <string>
40#include <curl/curl.h>
42#include <libxml/HTMLparser.h>
44//
45// Case-insensitive string comparison
46//
48#ifdef _WIN32
49#define COMPARE(a, b) (!_stricmp(a, b))
50#else
51#define COMPARE(a, b) (!strcasecmp(a, b))
52#endif
54//
55// libxml callback context structure
56//
57struct Context {
58 Context() : addTitle(false) {}
60 bool addTitle;
61 std::string title;
62};
64//
65// libcurl variables for error strings and returned data
67static char errorBuffer[CURL_ERROR_SIZE];
68static std::string buffer;
70//
71// libcurl write callback function
72//
73static size_t writer(char *data, size_t size, size_t nmemb,
74 std::string *writerData)
76 if(!writerData)
77 return 0;
79 writerData->append(data, size * nmemb);
81 return size * nmemb;
84//
85// libcurl connection initialization
86//
87static bool init(CURL *&curl, const char *url)
89 CURLcode result;
91 curl = curl_easy_init();
93 if(!curl) {
94 fprintf(stderr, "Failed to create CURL handle\n");
95 return false;
96 }
98 result = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
99 if(result != CURLE_OK) {
100 fprintf(stderr, "Failed to set error buffer [%d]\n", result);
101 return false;
102 }
104 result = curl_easy_setopt(curl, CURLOPT_URL, url);
105 if(result != CURLE_OK) {
106 fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);
107 return false;
108 }
110 result = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
111 if(result != CURLE_OK) {
112 fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);
113 return false;
114 }
116 result = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
117 if(result != CURLE_OK) {
118 fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);
119 return false;
120 }
122 result = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
123 if(result != CURLE_OK) {
124 fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);
125 return false;
126 }
128 return true;
131//
132// libxml start element callback function
133//
134static void StartElement(void *voidContext,
135 const xmlChar *name,
136 const xmlChar **attributes)
138 Context *context = static_cast<Context *>(voidContext);
140 if(COMPARE(reinterpret_cast<const char *>(name), "TITLE")) {
141 context->title = "";
142 context->addTitle = true;
143 }
144 (void)attributes;
147//
148// libxml end element callback function
149//
150static void EndElement(void *voidContext,
151 const xmlChar *name)
153 Context *context = static_cast<Context *>(voidContext);
155 if(COMPARE(reinterpret_cast<const char *>(name), "TITLE"))
156 context->addTitle = false;
159//
160// Text handling helper function
161//
162static void handleCharacters(Context *context,
163 const xmlChar *chars,
164 int length)
166 if(context->addTitle)
167 context->title.append(reinterpret_cast<const char *>(chars),
168 (unsigned long)length);
171//
172// libxml PCDATA callback function
173//
174static void Characters(void *voidContext,
175 const xmlChar *chars,
176 int length)
178 Context *context = static_cast<Context *>(voidContext);
180 handleCharacters(context, chars, length);
183//
184// libxml CDATA callback function
185//
186static void cdata(void *voidContext,
187 const xmlChar *chars,
188 int length)
190 Context *context = static_cast<Context *>(voidContext);
192 handleCharacters(context, chars, length);
195//
196// libxml SAX callback structure
197//
198static htmlSAXHandler saxHandler = {
199 NULL,
200 NULL,
201 NULL,
202 NULL,
203 NULL,
204 NULL,
205 NULL,
206 NULL,
207 NULL,
208 NULL,
209 NULL,
210 NULL,
211 NULL,
212 NULL,
213 StartElement,
214 EndElement,
215 NULL,
216 Characters,
217 NULL,
218 NULL,
219 NULL,
220 NULL,
221 NULL,
222 NULL,
223 NULL,
224 cdata,
225 NULL,
226 0,
227 0,
228 0,
229 0,
230 NULL
231};
233//
234// Parse given (assumed to be) HTML text and return the title
235//
236static void parseHtml(const std::string &html,
237 std::string &title)
239 htmlParserCtxtPtr ctxt;
240 Context context;
242 ctxt = htmlCreatePushParserCtxt(&saxHandler, &context, "", 0, "",
243 XML_CHAR_ENCODING_NONE);
245 htmlParseChunk(ctxt, html.c_str(), (int)html.size(), 0);
246 htmlParseChunk(ctxt, "", 0, 1);
248 htmlFreeParserCtxt(ctxt);
250 title = context.title;
253int main(int argc, const char *argv[])
255 CURL *curl = NULL;
256 CURLcode result;
257 std::string title;
259 // Ensure one argument is given
261 if(argc != 2) {
262 fprintf(stderr, "Usage: %s <url>\n", argv[0]);
263 return EXIT_FAILURE;
264 }
266 result = curl_global_init(CURL_GLOBAL_ALL);
267 if(result != CURLE_OK)
268 return (int)result;
270 // Initialize CURL handle
272 if(!init(curl, argv[1])) {
273 fprintf(stderr, "Handle initialization failed\n");
274 curl_global_cleanup();
275 return EXIT_FAILURE;
276 }
278 // Retrieve content for the URL
280 result = curl_easy_perform(curl);
281 curl_easy_cleanup(curl);
283 if(result != CURLE_OK) {
284 fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer);
285 return EXIT_FAILURE;
286 }
288 // Parse the (assumed) HTML code
289 parseHtml(buffer, title);
291 // Display the extracted title
292 printf("Title: %s\n", title.c_str());
294 return (int)result;
Diagnostics4 git calls · 33 ms
CallStatusSizeTime033 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=6
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
fetch2002.2 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=24
git.response.bytes=2217
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=c1130ce522e61751585fe0537d973448e9ba6139
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=2217
git.pkt.payload.max=2196
git.sideband.band1.bytes=2195
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
a5e16a0eee664b2a
Request
40e5ac76…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format