|
| 1 | +#include <string.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +extern void* GC_malloc_atomic(size_t sz); |
| 6 | +extern void* GC_malloc(size_t sz); |
| 7 | + |
| 8 | +static char* cs_strdup_gc(const char* s) { |
| 9 | + if (!s) return NULL; |
| 10 | + size_t len = strlen(s); |
| 11 | + char* out = (char*)GC_malloc_atomic(len + 1); |
| 12 | + memcpy(out, s, len + 1); |
| 13 | + return out; |
| 14 | +} |
| 15 | + |
| 16 | +static char* cs_strndup_gc(const char* s, size_t n) { |
| 17 | + char* out = (char*)GC_malloc_atomic(n + 1); |
| 18 | + memcpy(out, s, n); |
| 19 | + out[n] = '\0'; |
| 20 | + return out; |
| 21 | +} |
| 22 | + |
| 23 | +const char* cs_url_parse_protocol(const char* href) { |
| 24 | + if (!href) return cs_strdup_gc(""); |
| 25 | + const char* colon = strstr(href, "://"); |
| 26 | + if (!colon) return cs_strdup_gc(""); |
| 27 | + size_t len = colon - href + 1; |
| 28 | + char* out = (char*)GC_malloc_atomic(len + 1); |
| 29 | + memcpy(out, href, len); |
| 30 | + out[len] = '\0'; |
| 31 | + return out; |
| 32 | +} |
| 33 | + |
| 34 | +const char* cs_url_parse_hostname(const char* href) { |
| 35 | + if (!href) return cs_strdup_gc(""); |
| 36 | + const char* after = strstr(href, "://"); |
| 37 | + if (!after) return cs_strdup_gc(""); |
| 38 | + after += 3; |
| 39 | + const char* end = after; |
| 40 | + while (*end && *end != '/' && *end != '?' && *end != '#' && *end != ':') end++; |
| 41 | + return cs_strndup_gc(after, end - after); |
| 42 | +} |
| 43 | + |
| 44 | +const char* cs_url_parse_port(const char* href) { |
| 45 | + if (!href) return cs_strdup_gc(""); |
| 46 | + const char* after = strstr(href, "://"); |
| 47 | + if (!after) return cs_strdup_gc(""); |
| 48 | + after += 3; |
| 49 | + const char* colon = NULL; |
| 50 | + const char* p = after; |
| 51 | + while (*p && *p != '/' && *p != '?' && *p != '#') { |
| 52 | + if (*p == ':') { colon = p; break; } |
| 53 | + p++; |
| 54 | + } |
| 55 | + if (!colon) return cs_strdup_gc(""); |
| 56 | + colon++; |
| 57 | + const char* end = colon; |
| 58 | + while (*end && *end != '/' && *end != '?' && *end != '#') end++; |
| 59 | + return cs_strndup_gc(colon, end - colon); |
| 60 | +} |
| 61 | + |
| 62 | +const char* cs_url_parse_host(const char* href) { |
| 63 | + if (!href) return cs_strdup_gc(""); |
| 64 | + const char* after = strstr(href, "://"); |
| 65 | + if (!after) return cs_strdup_gc(""); |
| 66 | + after += 3; |
| 67 | + const char* end = after; |
| 68 | + while (*end && *end != '/' && *end != '?' && *end != '#') end++; |
| 69 | + return cs_strndup_gc(after, end - after); |
| 70 | +} |
| 71 | + |
| 72 | +const char* cs_url_parse_pathname(const char* href) { |
| 73 | + if (!href) return cs_strdup_gc("/"); |
| 74 | + const char* after = strstr(href, "://"); |
| 75 | + if (!after) return cs_strdup_gc("/"); |
| 76 | + after += 3; |
| 77 | + while (*after && *after != '/' && *after != '?' && *after != '#') after++; |
| 78 | + if (!*after || *after == '?' || *after == '#') return cs_strdup_gc("/"); |
| 79 | + const char* end = after; |
| 80 | + while (*end && *end != '?' && *end != '#') end++; |
| 81 | + return cs_strndup_gc(after, end - after); |
| 82 | +} |
| 83 | + |
| 84 | +const char* cs_url_parse_search(const char* href) { |
| 85 | + if (!href) return cs_strdup_gc(""); |
| 86 | + const char* q = strchr(href, '?'); |
| 87 | + if (!q) return cs_strdup_gc(""); |
| 88 | + const char* end = q; |
| 89 | + while (*end && *end != '#') end++; |
| 90 | + return cs_strndup_gc(q, end - q); |
| 91 | +} |
| 92 | + |
| 93 | +const char* cs_url_parse_hash(const char* href) { |
| 94 | + if (!href) return cs_strdup_gc(""); |
| 95 | + const char* h = strchr(href, '#'); |
| 96 | + if (!h) return cs_strdup_gc(""); |
| 97 | + return cs_strdup_gc(h); |
| 98 | +} |
| 99 | + |
| 100 | +const char* cs_url_parse_origin(const char* href) { |
| 101 | + if (!href) return cs_strdup_gc(""); |
| 102 | + const char* after = strstr(href, "://"); |
| 103 | + if (!after) return cs_strdup_gc(""); |
| 104 | + after += 3; |
| 105 | + const char* end = after; |
| 106 | + while (*end && *end != '/' && *end != '?' && *end != '#') end++; |
| 107 | + size_t proto_len = (after - 3) - href; |
| 108 | + size_t host_len = end - after; |
| 109 | + size_t total = proto_len + 3 + host_len; |
| 110 | + char* out = (char*)GC_malloc_atomic(total + 1); |
| 111 | + memcpy(out, href, proto_len + 3 + host_len); |
| 112 | + out[total] = '\0'; |
| 113 | + return out; |
| 114 | +} |
| 115 | + |
| 116 | +static const char* skip_qmark(const char* q) { |
| 117 | + if (q && *q == '?') return q + 1; |
| 118 | + return q ? q : ""; |
| 119 | +} |
| 120 | + |
| 121 | +const char* cs_urlsearch_get(const char* query, const char* key) { |
| 122 | + if (!query || !key) return NULL; |
| 123 | + const char* q = skip_qmark(query); |
| 124 | + size_t klen = strlen(key); |
| 125 | + const char* p = q; |
| 126 | + while (*p) { |
| 127 | + const char* eq = strchr(p, '='); |
| 128 | + if (!eq) break; |
| 129 | + size_t nlen = eq - p; |
| 130 | + if (nlen == klen && strncmp(p, key, klen) == 0) { |
| 131 | + eq++; |
| 132 | + const char* vend = strchr(eq, '&'); |
| 133 | + if (!vend) vend = eq + strlen(eq); |
| 134 | + return cs_strndup_gc(eq, vend - eq); |
| 135 | + } |
| 136 | + const char* amp = strchr(eq, '&'); |
| 137 | + if (!amp) break; |
| 138 | + p = amp + 1; |
| 139 | + } |
| 140 | + return NULL; |
| 141 | +} |
| 142 | + |
| 143 | +int cs_urlsearch_has(const char* query, const char* key) { |
| 144 | + if (!query || !key) return 0; |
| 145 | + const char* q = skip_qmark(query); |
| 146 | + size_t klen = strlen(key); |
| 147 | + const char* p = q; |
| 148 | + while (*p) { |
| 149 | + const char* eq = strchr(p, '='); |
| 150 | + if (!eq) { |
| 151 | + if (strlen(p) == klen && strncmp(p, key, klen) == 0) return 1; |
| 152 | + break; |
| 153 | + } |
| 154 | + size_t nlen = eq - p; |
| 155 | + if (nlen == klen && strncmp(p, key, klen) == 0) return 1; |
| 156 | + const char* amp = strchr(eq, '&'); |
| 157 | + if (!amp) break; |
| 158 | + p = amp + 1; |
| 159 | + } |
| 160 | + return 0; |
| 161 | +} |
| 162 | + |
| 163 | +const char* cs_urlsearch_set(const char* query, const char* key, const char* value) { |
| 164 | + if (!key || !value) return query ? cs_strdup_gc(query) : cs_strdup_gc(""); |
| 165 | + const char* q = skip_qmark(query ? query : ""); |
| 166 | + size_t klen = strlen(key); |
| 167 | + size_t buf_size = strlen(q) + strlen(key) + strlen(value) + 64; |
| 168 | + char* out = (char*)GC_malloc_atomic(buf_size); |
| 169 | + out[0] = '\0'; |
| 170 | + int found = 0; |
| 171 | + const char* p = q; |
| 172 | + int first = 1; |
| 173 | + while (*p) { |
| 174 | + const char* eq = strchr(p, '='); |
| 175 | + if (!eq) break; |
| 176 | + size_t nlen = eq - p; |
| 177 | + const char* amp = strchr(eq, '&'); |
| 178 | + const char* seg_end = amp ? amp : eq + strlen(eq); |
| 179 | + if (!first) strcat(out, "&"); |
| 180 | + first = 0; |
| 181 | + if (nlen == klen && strncmp(p, key, klen) == 0) { |
| 182 | + strncat(out, key, klen); |
| 183 | + strcat(out, "="); |
| 184 | + strcat(out, value); |
| 185 | + found = 1; |
| 186 | + } else { |
| 187 | + strncat(out, p, seg_end - p); |
| 188 | + } |
| 189 | + if (!amp) break; |
| 190 | + p = amp + 1; |
| 191 | + } |
| 192 | + if (!found) { |
| 193 | + if (!first) strcat(out, "&"); |
| 194 | + strcat(out, key); |
| 195 | + strcat(out, "="); |
| 196 | + strcat(out, value); |
| 197 | + } |
| 198 | + return out; |
| 199 | +} |
| 200 | + |
| 201 | +const char* cs_urlsearch_append(const char* query, const char* key, const char* value) { |
| 202 | + if (!key || !value) return query ? cs_strdup_gc(query) : cs_strdup_gc(""); |
| 203 | + const char* q = skip_qmark(query ? query : ""); |
| 204 | + size_t qlen = strlen(q); |
| 205 | + size_t klen = strlen(key); |
| 206 | + size_t vlen = strlen(value); |
| 207 | + size_t total = qlen + klen + vlen + 4; |
| 208 | + char* out = (char*)GC_malloc_atomic(total); |
| 209 | + if (qlen > 0) { |
| 210 | + memcpy(out, q, qlen); |
| 211 | + out[qlen] = '&'; |
| 212 | + memcpy(out + qlen + 1, key, klen); |
| 213 | + out[qlen + 1 + klen] = '='; |
| 214 | + memcpy(out + qlen + 1 + klen + 1, value, vlen); |
| 215 | + out[qlen + 1 + klen + 1 + vlen] = '\0'; |
| 216 | + } else { |
| 217 | + memcpy(out, key, klen); |
| 218 | + out[klen] = '='; |
| 219 | + memcpy(out + klen + 1, value, vlen); |
| 220 | + out[klen + 1 + vlen] = '\0'; |
| 221 | + } |
| 222 | + return out; |
| 223 | +} |
| 224 | + |
| 225 | +const char* cs_urlsearch_delete(const char* query, const char* key) { |
| 226 | + if (!query || !key) return cs_strdup_gc(""); |
| 227 | + const char* q = skip_qmark(query); |
| 228 | + size_t klen = strlen(key); |
| 229 | + size_t buf_size = strlen(q) + 2; |
| 230 | + char* out = (char*)GC_malloc_atomic(buf_size); |
| 231 | + out[0] = '\0'; |
| 232 | + const char* p = q; |
| 233 | + int first = 1; |
| 234 | + while (*p) { |
| 235 | + const char* eq = strchr(p, '='); |
| 236 | + if (!eq) break; |
| 237 | + size_t nlen = eq - p; |
| 238 | + const char* amp = strchr(eq, '&'); |
| 239 | + const char* seg_end = amp ? amp : eq + strlen(eq); |
| 240 | + if (!(nlen == klen && strncmp(p, key, klen) == 0)) { |
| 241 | + if (!first) strcat(out, "&"); |
| 242 | + first = 0; |
| 243 | + strncat(out, p, seg_end - p); |
| 244 | + } |
| 245 | + if (!amp) break; |
| 246 | + p = amp + 1; |
| 247 | + } |
| 248 | + return out; |
| 249 | +} |
| 250 | + |
| 251 | +const char* cs_urlsearch_tostring(const char* query) { |
| 252 | + if (!query) return cs_strdup_gc(""); |
| 253 | + return cs_strdup_gc(skip_qmark(query)); |
| 254 | +} |
0 commit comments