Skip to content
This repository
Newer
Older
100644 132 lines (117 sloc) 2.826 kb
638794cd » peff
2010-05-23 make url-related functions reusable
1 #include "cache.h"
c2e86add » bebarino
2011-03-22 Fix sparse warnings
2 #include "url.h"
638794cd » peff
2010-05-23 make url-related functions reusable
3
4 int 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
19 int is_url(const char *url)
20 {
b33a1b9f » gitster
2011-10-03 url.c: simplify is_url()
21 /* Is "scheme" part reasonable? */
22 if (!url || !is_urlschemechar(1, *url++))
638794cd » peff
2010-05-23 make url-related functions reusable
23 return 0;
b33a1b9f » gitster
2011-10-03 url.c: simplify is_url()
24 while (*url && *url != ':') {
25 if (!is_urlschemechar(0, *url++))
638794cd » peff
2010-05-23 make url-related functions reusable
26 return 0;
27 }
b33a1b9f » gitster
2011-10-03 url.c: simplify is_url()
28 /* We've seen "scheme"; we want colon-slash-slash */
29 return (url[0] == ':' && url[1] == '/' && url[2] == '/');
638794cd » peff
2010-05-23 make url-related functions reusable
30 }
31
32 static int url_decode_char(const char *q)
33 {
34 int i;
35 unsigned char val = 0;
36 for (i = 0; i < 2; i++) {
37 unsigned char c = *q++;
38 val <<= 4;
39 if (c >= '0' && c <= '9')
40 val += c - '0';
41 else if (c >= 'a' && c <= 'f')
42 val += c - 'a' + 10;
43 else if (c >= 'A' && c <= 'F')
44 val += c - 'A' + 10;
45 else
46 return -1;
47 }
48 return val;
49 }
50
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
51 static char *url_decode_internal(const char **query, int len,
52 const char *stop_at, struct strbuf *out,
53 int decode_plus)
638794cd » peff
2010-05-23 make url-related functions reusable
54 {
55 const char *q = *query;
56
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
57 while (len) {
638794cd » peff
2010-05-23 make url-related functions reusable
58 unsigned char c = *q;
59
60 if (!c)
61 break;
62 if (stop_at && strchr(stop_at, c)) {
63 q++;
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
64 len--;
638794cd » peff
2010-05-23 make url-related functions reusable
65 break;
66 }
67
68 if (c == '%') {
69 int val = url_decode_char(q + 1);
70 if (0 <= val) {
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
71 strbuf_addch(out, val);
638794cd » peff
2010-05-23 make url-related functions reusable
72 q += 3;
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
73 len -= 3;
638794cd » peff
2010-05-23 make url-related functions reusable
74 continue;
75 }
76 }
77
730220de » trast
2010-07-24 Do not unquote + into ' ' in URLs
78 if (decode_plus && c == '+')
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
79 strbuf_addch(out, ' ');
638794cd » peff
2010-05-23 make url-related functions reusable
80 else
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
81 strbuf_addch(out, c);
638794cd » peff
2010-05-23 make url-related functions reusable
82 q++;
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
83 len--;
84 }
638794cd » peff
2010-05-23 make url-related functions reusable
85 *query = q;
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
86 return strbuf_detach(out, NULL);
638794cd » peff
2010-05-23 make url-related functions reusable
87 }
88
89 char *url_decode(const char *url)
90 {
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
91 return url_decode_mem(url, strlen(url));
92 }
93
94 char *url_decode_mem(const char *url, int len)
95 {
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
96 struct strbuf out = STRBUF_INIT;
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
97 const char *colon = memchr(url, ':', len);
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
98
99 /* Skip protocol part if present */
3c73a1d5 » gitster
2010-06-24 url_decode: URL scheme ends with a colon and does not require a slash
100 if (colon && url < colon) {
101 strbuf_add(&out, url, colon - url);
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
102 len -= colon - url;
3c73a1d5 » gitster
2010-06-24 url_decode: URL scheme ends with a colon and does not require a slash
103 url = colon;
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
104 }
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
105 return url_decode_internal(&url, len, NULL, &out, 0);
638794cd » peff
2010-05-23 make url-related functions reusable
106 }
107
108 char *url_decode_parameter_name(const char **query)
109 {
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
110 struct strbuf out = STRBUF_INIT;
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
111 return url_decode_internal(query, -1, "&=", &out, 1);
638794cd » peff
2010-05-23 make url-related functions reusable
112 }
113
114 char *url_decode_parameter_value(const char **query)
115 {
ce83eda1 » gitster
2010-06-23 url.c: "<scheme>://" part at the beginning should not be URL decoded
116 struct strbuf out = STRBUF_INIT;
66c84485 » peff
2011-07-18 url: decode buffers that are not NUL-terminated
117 return url_decode_internal(query, -1, "&", &out, 1);
638794cd » peff
2010-05-23 make url-related functions reusable
118 }
1966d9f3 » rctay
2010-11-25 shift end_url_with_slash() from http.[ch] to url.[ch]
119
120 void end_url_with_slash(struct strbuf *buf, const char *url)
121 {
122 strbuf_addstr(buf, url);
123 if (buf->len && buf->buf[buf->len - 1] != '/')
124 strbuf_addstr(buf, "/");
125 }
3793a309 » rctay
2010-11-25 url: add str wrapper for end_url_with_slash()
126
127 void str_end_url_with_slash(const char *url, char **dest) {
128 struct strbuf buf = STRBUF_INIT;
129 end_url_with_slash(&buf, url);
130 free(*dest);
131 *dest = strbuf_detach(&buf, NULL);
132 }
Something went wrong with that request. Please try again.