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"
25#include "urldata.h"
26#include "gopher.h"
28#ifndef CURL_DISABLE_GOPHER
30#include "transfer.h"
31#include "sendf.h"
32#include "curl_trc.h"
33#include "cfilters.h"
34#include "connect.h"
35#include "select.h"
36#include "url.h"
37#include "escape.h"
39#ifdef USE_SSL
40static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
41{
42 (void)data;
43 (void)done;
44 return CURLE_OK;
45}
47static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
48{
49 struct connectdata *conn = data->conn;
50 CURLcode result;
52 result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
53 if(result)
54 connclose(conn);
55 *done = TRUE;
56 return result;
57}
58#endif
60/* Sends buf to the server and, optionally, writes it to the client too. */
61static CURLcode send_buf(struct Curl_easy *data,
62 const char *buf,
63 size_t buf_len,
64 bool client_write)
65{
66 CURLcode result = CURLE_OK;
67 struct connectdata *conn = data->conn;
68 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
69 size_t nwritten;
70 timediff_t timeout_ms;
71 int what;
73 while(buf_len) {
74 result = Curl_xfer_send(data, buf, buf_len, FALSE, &nwritten);
75 if(!result) { /* Which may not have written it all! */
76 if(client_write) {
77 result = Curl_client_write(data, CLIENTWRITE_HEADER, buf, nwritten);
78 if(result)
79 break;
80 }
82 if(nwritten > buf_len) {
83 DEBUGASSERT(0);
84 break;
85 }
86 buf_len -= nwritten;
87 buf += nwritten;
88 if(!buf_len)
89 break; /* but it did write it all */
90 }
91 else
92 break;
94 timeout_ms = Curl_timeleft_ms(data);
95 if(timeout_ms < 0) {
96 result = CURLE_OPERATION_TIMEDOUT;
97 break;
98 }
99 if(!timeout_ms)
100 timeout_ms = TIMEDIFF_T_MAX;
102 /* Do not busyloop. The entire loop thing is a workaround as it causes a
103 BLOCKING behavior which is a NO-NO. This function should rather be
104 split up in a do and a doing piece where the pieces that are not
105 possible to send now will be sent in the doing function repeatedly
106 until the entire request is sent. */
107 what = SOCKET_WRITABLE(sockfd, timeout_ms);
108 if(what < 0) {
109 result = CURLE_SEND_ERROR;
110 break;
111 }
112 else if(!what) {
113 result = CURLE_OPERATION_TIMEDOUT;
114 break;
115 }
116 }
118 return result;
119}
121static CURLcode gopher_do(struct Curl_easy *data, bool *done)
122{
123 CURLcode result = CURLE_OK;
124 char *gopherpath;
125 const char *path = data->state.up.path;
126 const char *query = data->state.up.query;
127 const char *buf = NULL;
128 char *buf_alloc = NULL;
129 size_t buf_len;
131 *done = TRUE; /* unconditionally */
133 /* path is guaranteed non-NULL */
134 DEBUGASSERT(path);
136 if(query)
137 gopherpath = curl_maprintf("%s?%s", path, query);
138 else
139 gopherpath = curlx_strdup(path);
141 if(!gopherpath)
142 return CURLE_OUT_OF_MEMORY;
144 /* Create selector. Degenerate cases: / and /1 => convert to "" */
145 if(strlen(gopherpath) <= 2) {
146 buf = "";
147 buf_len = 0;
148 curlx_free(gopherpath);
149 }
150 else {
151 const char *newp;
153 /* Otherwise, drop / and the first character (i.e., item type) ... */
154 newp = gopherpath;
155 newp += 2;
157 /* ... and finally unescape */
158 result = Curl_urldecode(newp, 0, &buf_alloc, &buf_len, REJECT_ZERO);
159 curlx_free(gopherpath);
160 if(result)
161 return result;
162 buf = buf_alloc;
164 /* A decoded CR or LF would terminate the single-line gopher request and
165 let a crafted URL smuggle additional bytes onto the wire. REJECT_ZERO
166 only blocks NUL; reject CR and LF here too. A TAB is left alone as it
167 is the legitimate gopher type-7 selector/search separator. */
168 if(memchr(buf, '\r', buf_len) || memchr(buf, '\n', buf_len)) {
169 curlx_free(buf_alloc);
170 failf(data, "Bad gopher selector, CR or LF not allowed");
171 return CURLE_URL_MALFORMAT;
172 }
173 }
175 result = send_buf(data, buf, buf_len, TRUE);
176 curlx_free(buf_alloc);
178 if(!result)
179 /* Send CRLF to the server now, but defer writing it to the client to
180 preserve the historical behavior of this file. */
181 result = send_buf(data, "\r\n", 2, FALSE);
182 if(result) {
183 failf(data, "Failed sending Gopher request");
184 return result;
185 }
186 result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
187 if(result)
188 return result;
190 Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
191 return CURLE_OK;
192}
194/*
195 * Gopher protocol handler.
196 * This is also a nice simple template to build off for simple
197 * connect-command-download protocols.
198 */
200const struct Curl_protocol Curl_protocol_gopher = {
201 ZERO_NULL, /* setup_connection */
202 ZERO_NULL, /* setup_filters */
203 gopher_do, /* do_it */
204 ZERO_NULL, /* done */
205 ZERO_NULL, /* do_more */
206 ZERO_NULL, /* connect_it */
207 ZERO_NULL, /* connecting */
208 ZERO_NULL, /* doing */
209 ZERO_NULL, /* proto_pollset */
210 ZERO_NULL, /* doing_pollset */
211 ZERO_NULL, /* domore_pollset */
212 ZERO_NULL, /* perform_pollset */
213 ZERO_NULL, /* disconnect */
214 ZERO_NULL, /* write_resp */
215 ZERO_NULL, /* write_resp_hd */
216 ZERO_NULL, /* connection_is_dead */
217 ZERO_NULL, /* attach connection */
218 ZERO_NULL, /* follow */
219};
221#ifdef USE_SSL
222const struct Curl_protocol Curl_protocol_gophers = {
223 ZERO_NULL, /* setup_connection */
224 ZERO_NULL, /* setup_filters */
225 gopher_do, /* do_it */
226 ZERO_NULL, /* done */
227 ZERO_NULL, /* do_more */
228 gopher_connect, /* connect_it */
229 gopher_connecting, /* connecting */
230 ZERO_NULL, /* doing */
231 ZERO_NULL, /* proto_pollset */
232 ZERO_NULL, /* doing_pollset */
233 ZERO_NULL, /* domore_pollset */
234 ZERO_NULL, /* perform_pollset */
235 ZERO_NULL, /* disconnect */
236 ZERO_NULL, /* write_resp */
237 ZERO_NULL, /* write_resp_hd */
238 ZERO_NULL, /* connection_is_dead */
239 ZERO_NULL, /* attach connection */
240 ZERO_NULL, /* follow */
241};
242#endif
244#endif /* CURL_DISABLE_GOPHER */
Diagnostics4 git calls · 38 ms
CallStatusSizeTime038 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 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 · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=34 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=1 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
fetch2002.5 KiB · hit34 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2574 git.response.cache=HIT git.response.duration_ms=34 git.fetch.wants=1caaecfcd5da33bb71cfc2eb8c4a213ab6dbf5d9 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=2574 git.pkt.payload.max=2553 git.sideband.band1.bytes=2552 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
- a5e18ff3f87a5b65
- Request
- ca9a3684…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format