url.con commit Merge branch 'dt/remote-helper-doc-re-lock-option' (cab037c)
   1#include "cache.h"
   2#include "url.h"
   3
   4int is_urlschemechar(int first_flag, int ch)
   5{
   6        /*
   7         * The set of valid URL schemes, as per STD66 (RFC3986) is
   8         * '[A-Za-z][A-Za-z0-9+.-]*'. But use sightly looser check
   9         * of '[A-Za-z0-9][A-Za-z0-9+.-]*' because earlier version
  10         * of check used '[A-Za-z0-9]+' so not to break any remote
  11         * helpers.
  12         */
  13        int alphanumeric, special;
  14        alphanumeric = ch > 0 && isalnum(ch);
  15        special = ch == '+' || ch == '-' || ch == '.';
  16        return alphanumeric || (!first_flag && special);
  17}
  18
  19int is_url(const char *url)
  20{
  21        /* Is "scheme" part reasonable? */
  22        if (!url || !is_urlschemechar(1, *url++))
  23                return 0;
  24        while (*url && *url != ':') {
  25                if (!is_urlschemechar(0, *url++))
  26                        return 0;
  27        }
  28        /* We've seen "scheme"; we want colon-slash-slash */
  29        return (url[0] == ':' && url[1] == '/' && url[2] == '/');
  30}
  31
  32static char *url_decode_internal(const char **query, int len,
  33                                 const char *stop_at, struct strbuf *out,
  34                                 int decode_plus)
  35{
  36        const char *q = *query;
  37
  38        while (len) {
  39                unsigned char c = *q;
  40
  41                if (!c)
  42                        break;
  43                if (stop_at && strchr(stop_at, c)) {
  44                        q++;
  45                        len--;
  46                        break;
  47                }
  48
  49                if (c == '%' && (len < 0 || len >= 3)) {
  50                        int val = hex2chr(q + 1);
  51                        if (0 < val) {
  52                                strbuf_addch(out, val);
  53                                q += 3;
  54                                len -= 3;
  55                                continue;
  56                        }
  57                }
  58
  59                if (decode_plus && c == '+')
  60                        strbuf_addch(out, ' ');
  61                else
  62                        strbuf_addch(out, c);
  63                q++;
  64                len--;
  65        }
  66        *query = q;
  67        return strbuf_detach(out, NULL);
  68}
  69
  70char *url_decode(const char *url)
  71{
  72        return url_decode_mem(url, strlen(url));
  73}
  74
  75char *url_decode_mem(const char *url, int len)
  76{
  77        struct strbuf out = STRBUF_INIT;
  78        const char *colon = memchr(url, ':', len);
  79
  80        /* Skip protocol part if present */
  81        if (colon && url < colon) {
  82                strbuf_add(&out, url, colon - url);
  83                len -= colon - url;
  84                url = colon;
  85        }
  86        return url_decode_internal(&url, len, NULL, &out, 0);
  87}
  88
  89char *url_percent_decode(const char *encoded)
  90{
  91        struct strbuf out = STRBUF_INIT;
  92        return url_decode_internal(&encoded, strlen(encoded), NULL, &out, 0);
  93}
  94
  95char *url_decode_parameter_name(const char **query)
  96{
  97        struct strbuf out = STRBUF_INIT;
  98        return url_decode_internal(query, -1, "&=", &out, 1);
  99}
 100
 101char *url_decode_parameter_value(const char **query)
 102{
 103        struct strbuf out = STRBUF_INIT;
 104        return url_decode_internal(query, -1, "&", &out, 1);
 105}
 106
 107void end_url_with_slash(struct strbuf *buf, const char *url)
 108{
 109        strbuf_addstr(buf, url);
 110        strbuf_complete(buf, '/');
 111}
 112
 113void str_end_url_with_slash(const char *url, char **dest)
 114{
 115        struct strbuf buf = STRBUF_INIT;
 116        end_url_with_slash(&buf, url);
 117        free(*dest);
 118        *dest = strbuf_detach(&buf, NULL);
 119}