5
#include "hex-ll.h"
6
#include "strbuf.h"
7
#include "urlmatch.h"
8
+#include "url.h"
9
10
#define URL_ALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
11
#define URL_DIGIT "0123456789"
441
return url_normalize_1(url, out_info, 0);
442
}
443
444
+char *url_parse(const char *url_orig, struct url_info *out_info)
445
+{
446
+ struct strbuf url;
447
+ char *host, *separator;
448
+ char *detached, *normalized;
449
+ char *url_decoded;
450
+ enum url_scheme scheme = URL_SCHEME_LOCAL;
451
+ struct url_info local_info;
452
+ struct url_info *info = out_info ? out_info : &local_info;
453
+ bool scp_syntax = false;
454
+
455
+ if (is_url(url_orig))
456
+ url_decoded = url_decode(url_orig);
457
+ else
458
+ url_decoded = xstrdup(url_orig);
459
+
460
+ strbuf_init(&url, strlen(url_decoded) + sizeof("ssh://"));
461
+ strbuf_addstr(&url, url_decoded);
462
+ free(url_decoded);
463
+
464
+ host = strstr(url.buf, "://");
465
+ if (host) {
466
+ /*
467
+ * Temporarily NUL-terminate the scheme name
468
+ * so we can pass it to url_get_scheme(),
469
+ * then restore the ':' so the buffer
470
+ * is intact for url_normalize() below.
471
+ */
472
+ char saved = *host;
473
+ *host = '\0';
474
+ scheme = url_get_scheme(url.buf);
475
+ *host = saved;
476
+ host += 3;
477
+ } else {
478
+ if (!url_is_local_not_ssh(url.buf)) {
479
+ scp_syntax = true;
480
+ scheme = URL_SCHEME_SSH;
481
+ strbuf_insertstr(&url, 0, "ssh://");
482
+ host = url.buf + strlen("ssh://");
483
+ }
484
+ }
485
+
486
+ /*
487
+ * Path starts after ':' in scp style SSH URLs.
488
+ *
489
+ * The host portion can begin with an optional "user@",
490
+ * and the host itself can be wrapped in '[' ']' brackets.
491
+ * The bracket form is git's legacy way of supporting:
492
+ *
493
+ * - IPv6 literals: [::1]:repo
494
+ * - host:port pairs in the short form: [myhost:123]:src
495
+ * - Plain hostnames that happen to need bracketing: [host]:path
496
+ *
497
+ * Treat '[' followed by 0 or 1 inner colons as the host:port
498
+ * or plain hostname form and strip the brackets so url_normalize
499
+ * sees host[:port] natively. Two or more inner colons mark an
500
+ * IPv6 literal: keep the brackets for url_normalize to recognize.
501
+ *
502
+ * The scp path separator is the ':' that follows the host part,
503
+ * and we must skip over user@ and any '[...]' before searching.
504
+ */
505
+ if (scp_syntax) {
506
+ char *user_at;
507
+ char *host_start;
508
+ char *bracket_end;
509
+
510
+ user_at = strchr(host, '@');
511
+ host_start = user_at ? user_at + 1 : host;
512
+
513
+ if (*host_start == '[') {
514
+ char *p;
515
+ int inner_colons;
516
+
517
+ bracket_end = strchr(host_start, ']');
518
+ inner_colons = 0;
519
+ for (p = host_start + 1; bracket_end && p < bracket_end; p++)
520
+ if (*p == ':')
521
+ inner_colons++;
522
+
523
+ if (bracket_end && inner_colons <= 1) {
524
+ size_t close_off = bracket_end - url.buf;
525
+ size_t open_off = host_start - url.buf;
526
+ strbuf_remove(&url, close_off, 1);
527
+ strbuf_remove(&url, open_off, 1);
528
+ separator = url.buf + close_off - 1;
529
+ } else if (bracket_end) {
530
+ separator = strchr(bracket_end + 1, ':');
531
+ } else {
532
+ separator = strchr(host_start, ':');
533
+ }
534
+ } else {
535
+ separator = strchr(host_start, ':');
536
+ }
537
+
538
+ if (separator) {
539
+ if (separator[1] == '/')
540
+ strbuf_remove(&url, separator - url.buf, 1);
541
+ else
542
+ *separator = '/';
543
+ }
544
+ }
545
+
546
+ detached = strbuf_detach(&url, NULL);
547
+ normalized = url_normalize(detached, info);
548
+ free(detached);
549
+
550
+ if (!normalized)
551
+ return NULL;
552
+
553
+ /*
554
+ * Point path to ~ for URLs like this:
555
+ *
556
+ * ssh://host.xz/~user/repo
557
+ * git://host.xz/~user/repo
558
+ * host.xz:~user/repo
559
+ */
560
+ if (scheme == URL_SCHEME_GIT || scheme == URL_SCHEME_SSH) {
561
+ if (normalized[info->path_off + 1] == '~') {
562
+ info->path_off++;
563
+ info->path_len--;
564
+ }
565
+ }
566
+
567
+ return normalized;
568
+}
569
+
570
static size_t url_match_prefix(const char *url,
571
const char *url_prefix,
572
size_t url_prefix_len)