url.con commit t3703, t4208: add test cases for magic pathspec (6fd09f5)
   1#include "cache.h"
   2
   3int is_urlschemechar(int first_flag, int ch)
   4{
   5        /*
   6         * The set of valid URL schemes, as per STD66 (RFC3986) is
   7         * '[A-Za-z][A-Za-z0-9+.-]*'. But use sightly looser check
   8         * of '[A-Za-z0-9][A-Za-z0-9+.-]*' because earlier version
   9         * of check used '[A-Za-z0-9]+' so not to break any remote
  10         * helpers.
  11         */
  12        int alphanumeric, special;
  13        alphanumeric = ch > 0 && isalnum(ch);
  14        special = ch == '+' || ch == '-' || ch == '.';
  15        return alphanumeric || (!first_flag && special);
  16}
  17
  18int is_url(const char *url)
  19{
  20        const char *url2, *first_slash;
  21
  22        if (!url)
  23                return 0;
  24        url2 = url;
  25        first_slash = strchr(url, '/');
  26
  27        /* Input with no slash at all or slash first can't be URL. */
  28        if (!first_slash || first_slash == url)
  29                return 0;
  30        /* Character before must be : and next must be /. */
  31        if (first_slash[-1] != ':' || first_slash[1] != '/')
  32                return 0;
  33        /* There must be something before the :// */
  34        if (first_slash == url + 1)
  35                return 0;
  36        /*
  37         * Check all characters up to first slash - 1. Only alphanum
  38         * is allowed.
  39         */
  40        url2 = url;
  41        while (url2 < first_slash - 1) {
  42                if (!is_urlschemechar(url2 == url, (unsigned char)*url2))
  43                        return 0;
  44                url2++;
  45        }
  46
  47        /* Valid enough. */
  48        return 1;
  49}
  50
  51static int url_decode_char(const char *q)
  52{
  53        int i;
  54        unsigned char val = 0;
  55        for (i = 0; i < 2; i++) {
  56                unsigned char c = *q++;
  57                val <<= 4;
  58                if (c >= '0' && c <= '9')
  59                        val += c - '0';
  60                else if (c >= 'a' && c <= 'f')
  61                        val += c - 'a' + 10;
  62                else if (c >= 'A' && c <= 'F')
  63                        val += c - 'A' + 10;
  64                else
  65                        return -1;
  66        }
  67        return val;
  68}
  69
  70static char *url_decode_internal(const char **query, const char *stop_at,
  71                                 struct strbuf *out, int decode_plus)
  72{
  73        const char *q = *query;
  74
  75        do {
  76                unsigned char c = *q;
  77
  78                if (!c)
  79                        break;
  80                if (stop_at && strchr(stop_at, c)) {
  81                        q++;
  82                        break;
  83                }
  84
  85                if (c == '%') {
  86                        int val = url_decode_char(q + 1);
  87                        if (0 <= val) {
  88                                strbuf_addch(out, val);
  89                                q += 3;
  90                                continue;
  91                        }
  92                }
  93
  94                if (decode_plus && c == '+')
  95                        strbuf_addch(out, ' ');
  96                else
  97                        strbuf_addch(out, c);
  98                q++;
  99        } while (1);
 100        *query = q;
 101        return strbuf_detach(out, NULL);
 102}
 103
 104char *url_decode(const char *url)
 105{
 106        struct strbuf out = STRBUF_INIT;
 107        const char *colon = strchr(url, ':');
 108
 109        /* Skip protocol part if present */
 110        if (colon && url < colon) {
 111                strbuf_add(&out, url, colon - url);
 112                url = colon;
 113        }
 114        return url_decode_internal(&url, NULL, &out, 0);
 115}
 116
 117char *url_decode_parameter_name(const char **query)
 118{
 119        struct strbuf out = STRBUF_INIT;
 120        return url_decode_internal(query, "&=", &out, 1);
 121}
 122
 123char *url_decode_parameter_value(const char **query)
 124{
 125        struct strbuf out = STRBUF_INIT;
 126        return url_decode_internal(query, "&", &out, 1);
 127}
 128
 129void end_url_with_slash(struct strbuf *buf, const char *url)
 130{
 131        strbuf_addstr(buf, url);
 132        if (buf->len && buf->buf[buf->len - 1] != '/')
 133                strbuf_addstr(buf, "/");
 134}
 135
 136void str_end_url_with_slash(const char *url, char **dest) {
 137        struct strbuf buf = STRBUF_INIT;
 138        end_url_with_slash(&buf, url);
 139        free(*dest);
 140        *dest = strbuf_detach(&buf, NULL);
 141}