Codebergdnkl
foot
master
1#include "uri.h"
3#include <stdlib.h>
4#include <string.h>
5#include <limits.h>
6#include <unistd.h>
8#define LOG_MODULE "uri"
9#define LOG_ENABLE_DBG 0
10#include "log.h"
11#include "debug.h"
12#include "util.h"
13#include "xmalloc.h"
15bool
16uri_parse(const char *uri, size_t len,
17 char **scheme, char **user, char **password, char **host,
18 uint16_t *port, char **path, char **query, char **fragment)
19{
20 LOG_DBG("parse URI: \"%.*s\"", (int)len, uri);
22 if (scheme != NULL) *scheme = NULL;
23 if (user != NULL) *user = NULL;
24 if (password != NULL) *password = NULL;
25 if (host != NULL) *host = NULL;
26 if (port != NULL) *port = 0;
27 if (path != NULL) *path = NULL;
28 if (query != NULL) *query = NULL;
29 if (fragment != NULL) *fragment = NULL;
31 size_t left = len;
32 const char *start = uri;
33 const char *end = NULL;
35 if ((end = memchr(start, ':', left)) == NULL)
36 goto err;
38 size_t scheme_len = end - start;
39 if (scheme_len == 0)
40 goto err;
42 if (scheme != NULL)
43 *scheme = xstrndup(start, scheme_len);
45 LOG_DBG("scheme: \"%.*s\"", (int)scheme_len, start);
47 start = end + 1;
48 left = len - (start - uri);
50 /* Authinfo */
51 if (left >= 2 && start[0] == '/' && start[1] == '/') {
52 start += 2;
53 left -= 2;
55 /* [user[:password]@]@host[:port] */
57 /* Find beginning of path segment (required component
58 * following the authinfo) */
59 const char *path_segment = memchr(start, '/', left);
60 if (path_segment == NULL)
61 goto err;
63 size_t auth_left = path_segment - start;
65 /* Do we have a user (and optionally a password)? */
66 const char *user_pw_end = memchr(start, '@', auth_left);
67 if (user_pw_end != NULL) {
68 size_t user_pw_len = user_pw_end - start;
70 /* Do we have a password? */
71 const char *user_end = memchr(start, ':', user_pw_end - start);
72 if (user_end != NULL) {
73 size_t user_len = user_end - start;
74 if (user_len == 0)
75 goto err;
77 if (user != NULL)
78 *user = xstrndup(start, user_len);
80 const char *pw = user_end + 1;
81 size_t pw_len = user_pw_end - pw;
82 if (pw_len == 0)
83 goto err;
85 if (password != NULL)
86 *password = xstrndup(pw, pw_len);
88 LOG_DBG("user: \"%.*s\"", (int)user_len, start);
89 LOG_DBG("password: \"%.*s\"", (int)pw_len, pw);
90 } else {
91 size_t user_len = user_pw_end - start;
92 if (user_len == 0)
93 goto err;
95 if (user != NULL)
96 *user = xstrndup(start, user_len);
98 LOG_DBG("user: \"%.*s\"", (int)user_len, start);
99 }
101 start = user_pw_end + 1;
102 left = len - (start - uri);
103 auth_left -= user_pw_len + 1;
104 }
106 const char *host_end = memchr(start, ':', auth_left);
107 if (host_end != NULL) {
108 size_t host_len = host_end - start;
109 if (host != NULL)
110 *host = xstrndup(start, host_len);
112 const char *port_str = host_end + 1;
113 size_t port_len = path_segment - port_str;
114 if (port_len == 0)
115 goto err;
117 uint16_t _port = 0;
118 for (size_t i = 0; i < port_len; i++) {
119 if (!(port_str[i] >= '0' && port_str[i] <= '9'))
120 goto err;
122 _port *= 10;
123 _port += port_str[i] - '0';
124 }
126 if (port != NULL)
127 *port = _port;
129 LOG_DBG("host: \"%.*s\"", (int)host_len, start);
130 LOG_DBG("port: \"%.*s\" (%hu)", (int)port_len, port_str, _port);
131 } else {
132 size_t host_len = path_segment - start;
133 if (host != NULL)
134 *host = xstrndup(start, host_len);
136 LOG_DBG("host: \"%.*s\"", (int)host_len, start);
137 }
139 start = path_segment;
140 left = len - (start - uri);
141 }
143 /* Do we have a query? */
144 const char *query_start = memchr(start, '?', left);
145 const char *fragment_start = memchr(start, '#', left);
147 if (scheme != NULL && streq(*scheme, "file")) {
148 /* Don't try to parse query/fragment in file URIs, just treat
149 the remaining text as path */
150 query_start = NULL;
151 fragment_start = NULL;
152 }
154 else if (query_start != NULL && fragment_start != NULL &&
155 fragment_start < query_start)
156 {
157 /* Invalid URI - for now, ignore, and treat is as part of path */
158 query_start = NULL;
159 fragment_start = NULL;
160 }
162 size_t path_len =
163 query_start != NULL ? query_start - start :
164 fragment_start != NULL ? fragment_start - start :
165 left;
167 if (path_len == 0)
168 goto err;
170 /* Path - decode %xx encoded characters */
171 if (path != NULL) {
172 const char *encoded = start;
173 char *decoded = xmalloc(path_len + 1);
174 char *p = decoded;
176 size_t encoded_len = path_len;
177 size_t UNUSED decoded_len = 0;
179 while (true) {
180 /* Find next '%' */
181 const char *next = memchr(encoded, '%', encoded_len);
183 if (next == NULL) {
184 strncpy(p, encoded, encoded_len);
185 decoded_len += encoded_len;
186 p += encoded_len;
187 break;
188 }
190 /* Copy everything leading up to the '%' */
191 size_t prefix_len = next - encoded;
192 memcpy(p, encoded, prefix_len);
194 p += prefix_len;
195 encoded_len -= prefix_len;
196 decoded_len += prefix_len;
198 if (encoded_len >= 3 &&
199 hex2nibble(next[1]) <= 15 && hex2nibble(next[2]) <= 15)
200 {
201 *p++ = hex2nibble(next[1]) << 4 | hex2nibble(next[2]);
202 decoded_len++;
203 encoded_len -= 3;
204 encoded = next + 3;
205 } else {
206 *p++ = *next;
207 decoded_len++;
208 encoded_len -= 1;
209 encoded = next + 1;
210 }
211 }
213 *p = '\0';
214 *path = decoded;
216 LOG_DBG("path: encoded=\"%.*s\", decoded=\"%s\"", (int)path_len, start, decoded);
217 } else
218 LOG_DBG("path: encoded=\"%.*s\", decoded=<skipped>", (int)path_len, start);
220 start = query_start != NULL ? query_start + 1 : fragment_start != NULL ? fragment_start + 1 : uri + len;
221 left = len - (start - uri);
223 if (query_start != NULL) {
224 size_t query_len = fragment_start != NULL
225 ? fragment_start - start : left;
227 if (query_len == 0)
228 goto err;
230 if (query != NULL)
231 *query = xstrndup(start, query_len);
233 LOG_DBG("query: \"%.*s\"", (int)query_len, start);
235 start = fragment_start != NULL ? fragment_start + 1 : uri + len;
236 left = len - (start - uri);
237 }
239 if (fragment_start != NULL) {
240 if (left == 0)
241 goto err;
243 if (fragment != NULL)
244 *fragment = xstrndup(start, left);
246 LOG_DBG("fragment: \"%.*s\"", (int)left, start);
247 }
249 return true;
251err:
252 if (scheme != NULL) free(*scheme);
253 if (user != NULL) free(*user);
254 if (password != NULL) free(*password);
255 if (host != NULL) free(*host);
256 if (path != NULL) free(*path);
257 if (query != NULL) free(*query);
258 if (fragment != NULL) free(*fragment);
259 return false;
260}
262bool
263hostname_is_localhost(const char *hostname)
264{
265 char this_host[_POSIX_HOST_NAME_MAX];
266 if (gethostname(this_host, sizeof(this_host)) < 0)
267 this_host[0] = '\0';
269 return (hostname != NULL && (
270 streq(hostname, "") ||
271 streq(hostname, "localhost") ||
272 streq(hostname, this_host)));
273}
275UNITTEST
276{
277 /* Valid URI, all components */
278 const char uri[] = "http://john:wick@www.foobar.com:80/this/is/the%20path?query1=abc&query2=def#fragment";
280 char *scheme, *user, *password, *host, *path, *query, *fragment;
281 uint16_t port = 0;
283 uri_parse(
284 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
285 &query, &fragment);
286 xassert(streq(scheme, "http")); free(scheme);
287 xassert(streq(user, "john")); free(user);
288 xassert(streq(password, "wick")); free(password);
289 xassert(streq(host, "www.foobar.com")); free(host);
290 xassert(port == 80);
291 xassert(streq(path, "/this/is/the path")); free(path);
292 xassert(streq(query, "query1=abc&query2=def")); free(query);
293 xassert(streq(fragment, "fragment")); free(fragment);
294}
296UNITTEST
297{
298 /* Valid URI, all components. Using file scheme, so query+fragment is treated as part of the path */
299 const char uri[] = "file://john:wick@www.foobar.com:80/this/is/the%20path?query1=abc&query2=def#fragment";
301 char *scheme, *user, *password, *host, *path, *query, *fragment;
302 uint16_t port = 0;
304 uri_parse(
305 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
306 &query, &fragment);
307 xassert(streq(scheme, "file")); free(scheme);
308 xassert(streq(user, "john")); free(user);
309 xassert(streq(password, "wick")); free(password);
310 xassert(streq(host, "www.foobar.com")); free(host);
311 xassert(port == 80);
312 xassert(streq(path, "/this/is/the path?query1=abc&query2=def#fragment")); free(path);
313 xassert(query == NULL);
314 xassert(fragment == NULL);
315}
317UNITTEST
318{
319 /* Test a valid URI containing all components, except password */
320 const char uri[] = "http://john@www.foobar.com:80/this/is/the%20path?query1=abc&query2=def#fragment";
322 char *scheme, *user, *password, *host, *path, *query, *fragment;
323 uint16_t port = 0;
325 uri_parse(
326 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
327 &query, &fragment);
328 xassert(streq(scheme, "http")); free(scheme);
329 xassert(streq(user, "john")); free(user);
330 xassert(password == NULL);
331 xassert(streq(host, "www.foobar.com")); free(host);
332 xassert(port == 80);
333 xassert(streq(path, "/this/is/the path")); free(path);
334 xassert(streq(query, "query1=abc&query2=def")); free(query);
335 xassert(streq(fragment, "fragment")); free(fragment);
336}
338UNITTEST
339{
340 /* Valid URI, all components, except user+password */
341 const char uri[] = "http://www.foobar.com:80/this/is/the%20path?query1=abc&query2=def#fragment";
343 char *scheme, *user, *password, *host, *path, *query, *fragment;
344 uint16_t port = 0;
346 uri_parse(
347 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
348 &query, &fragment);
349 xassert(streq(scheme, "http")); free(scheme);
350 xassert(user == NULL);
351 xassert(password == NULL);
352 xassert(streq(host, "www.foobar.com")); free(host);
353 xassert(port == 80);
354 xassert(streq(path, "/this/is/the path")); free(path);
355 xassert(streq(query, "query1=abc&query2=def")); free(query);
356 xassert(streq(fragment, "fragment")); free(fragment);
357}
359UNITTEST
360{
361 /* Valid URI, fragment, no query */
362 const char uri[] = "http://www.foobar.com:80/this/is/the%20path#fragment";
364 char *scheme, *user, *password, *host, *path, *query, *fragment;
365 uint16_t port = 0;
367 uri_parse(
368 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
369 &query, &fragment);
370 xassert(streq(scheme, "http")); free(scheme);
371 xassert(user == NULL);
372 xassert(password == NULL);
373 xassert(streq(host, "www.foobar.com")); free(host);
374 xassert(port == 80);
375 xassert(streq(path, "/this/is/the path")); free(path);
376 xassert(query == NULL);
377 xassert(streq(fragment, "fragment")); free(fragment);
378}
380UNITTEST
381{
382 /* Valid URI, query, no fragment */
383 const char uri[] = "http://www.foobar.com:80/this/is/the%20path?query1=abc&query2=def";
385 char *scheme, *user, *password, *host, *path, *query, *fragment;
386 uint16_t port = 0;
388 uri_parse(
389 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
390 &query, &fragment);
391 xassert(streq(scheme, "http")); free(scheme);
392 xassert(user == NULL);
393 xassert(password == NULL);
394 xassert(streq(host, "www.foobar.com")); free(host);
395 xassert(port == 80);
396 xassert(streq(path, "/this/is/the path")); free(path);
397 xassert(streq(query, "query1=abc&query2=def")); free(query);
398 xassert(fragment == NULL);
399}
401UNITTEST
402{
403 /* Malformed URI, fragment before query (treated as part of path instead) */
404 const char uri[] = "http://www.foobar.com:80/this/is/the%20path#fragment?query1=abc&query2=def";
406 char *scheme, *user, *password, *host, *path, *query, *fragment;
407 uint16_t port = 0;
409 uri_parse(
410 uri, sizeof(uri) - 1, &scheme, &user, &password, &host, &port, &path,
411 &query, &fragment);
412 xassert(streq(scheme, "http")); free(scheme);
413 xassert(user == NULL);
414 xassert(password == NULL);
415 xassert(streq(host, "www.foobar.com")); free(host);
416 xassert(port == 80);
417 xassert(streq(path, "/this/is/the path#fragment?query1=abc&query2=def")); free(path);
418 xassert(query == NULL);
419 xassert(fragment == NULL);
420}
422UNITTEST
423{
424 /* Malformed URI, trailing '%' */
425 const char uri[] = "file:///%ABNOT-PART-OF-INPUT";
426 char *path;
427 uri_parse(uri, 9, NULL, NULL, NULL, NULL, NULL, &path, NULL, NULL);
428 xassert(streq(path, "/%")); free(path);
429}
431UNITTEST
432{
433 /* Malformed URI, trailing '%2' */
434 const char uri[] = "file:///%2ANOT-PART-OF-INPUT";
435 char *path;
436 uri_parse(uri, 10, NULL, NULL, NULL, NULL, NULL, &path, NULL, NULL);
437 xassert(streq(path, "/%2")); free(path);
438}
440UNITTEST
441{
442 /* Malformed URI, trailing '%ag' */
443 const char uri[] = "file:///%ag";
444 char *path;
445 uri_parse(uri, sizeof(uri) - 1, NULL, NULL, NULL, NULL, NULL, &path, NULL, NULL);
446 xassert(streq(path, "/%ag")); free(path);
447}
Diagnostics5 git calls · 26 ms
CallStatusSizeTime026 ms
discover200178 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB2 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=10322 git.response.cache=UPDATING git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit2 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=8830 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit2 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=9489 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 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=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetch2002.5 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2610 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=ddbfacdd60a4888bdd8f3da8a0059315a6be24d6 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2610 git.pkt.payload.max=2583 git.sideband.band1.bytes=2583 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://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5ea934a69aaa87d
- Request
- bf366af3…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format