Add PHP_HTTP_HAVE_CLIENT, by Jan Erhardt
[m6w6/ext-http] / php_http_url.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #ifdef PHP_HTTP_HAVE_IDN
16 # include <idna.h>
17 #endif
18
19 #ifdef PHP_HTTP_HAVE_WCHAR
20 # include <wchar.h>
21 # include <wctype.h>
22 #endif
23
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
26 #endif
27
28 #include "php_http_utf8.h"
29
30 static inline char *localhostname(void)
31 {
32 char hostname[1024] = {0};
33
34 #ifdef PHP_WIN32
35 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
36 return estrdup(hostname);
37 }
38 #elif defined(HAVE_GETHOSTNAME)
39 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
40 # if defined(HAVE_GETDOMAINNAME)
41 size_t hlen = strlen(hostname);
42 if (hlen <= lenof(hostname) - lenof("(none)")) {
43 hostname[hlen++] = '.';
44 if (SUCCESS == getdomainname(&hostname[hlen], lenof(hostname) - hlen)) {
45 if (!strcmp(&hostname[hlen], "(none)")) {
46 hostname[hlen - 1] = '\0';
47 }
48 return estrdup(hostname);
49 }
50 }
51 # endif
52 if (strcmp(hostname, "(none)")) {
53 return estrdup(hostname);
54 }
55 }
56 #endif
57 return estrndup("localhost", lenof("localhost"));
58 }
59
60 #define url(buf) ((php_http_url_t *) (buf).data)
61
62 static php_http_url_t *php_http_url_from_env(void)
63 {
64 zval *https, *zhost, *zport;
65 long port;
66 php_http_buffer_t buf;
67
68 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
69 php_http_buffer_account(&buf, sizeof(php_http_url_t));
70 memset(buf.data, 0, buf.used);
71
72 /* scheme */
73 url(buf)->scheme = &buf.data[buf.used];
74 https = php_http_env_get_server_var(ZEND_STRL("HTTPS"), 1);
75 if (https && !strcasecmp(Z_STRVAL_P(https), "ON")) {
76 php_http_buffer_append(&buf, "https", sizeof("https"));
77 } else {
78 php_http_buffer_append(&buf, "http", sizeof("http"));
79 }
80
81 /* host */
82 url(buf)->host = &buf.data[buf.used];
83 if ((((zhost = php_http_env_get_server_var(ZEND_STRL("HTTP_HOST"), 1)) ||
84 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1)) ||
85 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1)))) && Z_STRLEN_P(zhost)) {
86 size_t stop_at = strspn(Z_STRVAL_P(zhost), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.");
87
88 php_http_buffer_append(&buf, Z_STRVAL_P(zhost), stop_at);
89 php_http_buffer_append(&buf, "", 1);
90 } else {
91 char *host_str = localhostname();
92
93 php_http_buffer_append(&buf, host_str, strlen(host_str) + 1);
94 efree(host_str);
95 }
96
97 /* port */
98 zport = php_http_env_get_server_var(ZEND_STRL("SERVER_PORT"), 1);
99 if (zport && IS_LONG == is_numeric_string(Z_STRVAL_P(zport), Z_STRLEN_P(zport), &port, NULL, 0)) {
100 url(buf)->port = port;
101 }
102
103 /* path */
104 if (SG(request_info).request_uri && SG(request_info).request_uri[0]) {
105 const char *q = strchr(SG(request_info).request_uri, '?');
106
107 url(buf)->path = &buf.data[buf.used];
108
109 if (q) {
110 php_http_buffer_append(&buf, SG(request_info).request_uri, q - SG(request_info).request_uri);
111 php_http_buffer_append(&buf, "", 1);
112 } else {
113 php_http_buffer_append(&buf, SG(request_info).request_uri, strlen(SG(request_info).request_uri) + 1);
114 }
115 }
116
117 /* query */
118 if (SG(request_info).query_string && SG(request_info).query_string[0]) {
119 url(buf)->query = &buf.data[buf.used];
120 php_http_buffer_append(&buf, SG(request_info).query_string, strlen(SG(request_info).query_string) + 1);
121 }
122
123 return url(buf);
124 }
125
126 #define url_isset(u,n) \
127 ((u)&&(u)->n)
128 #define url_append(buf, append) do { \
129 char *_ptr = (buf)->data; \
130 php_http_url_t *_url = (php_http_url_t *) _ptr, _mem = *_url; \
131 append; \
132 /* relocate */ \
133 if (_ptr != (buf)->data) { \
134 ptrdiff_t diff = (buf)->data - _ptr; \
135 _url = (php_http_url_t *) (buf)->data; \
136 if (_mem.scheme) _url->scheme += diff; \
137 if (_mem.user) _url->user += diff; \
138 if (_mem.pass) _url->pass += diff; \
139 if (_mem.host) _url->host += diff; \
140 if (_mem.path) _url->path += diff; \
141 if (_mem.query) _url->query += diff; \
142 if (_mem.fragment) _url->fragment += diff; \
143 } \
144 } while (0)
145 #define url_copy(n) do { \
146 if (url_isset(new_url, n)) { \
147 url(buf)->n = &buf.data[buf.used]; \
148 url_append(&buf, php_http_buffer_append(&buf, new_url->n, strlen(new_url->n) + 1)); \
149 } else if (url_isset(old_url, n)) { \
150 url(buf)->n = &buf.data[buf.used]; \
151 url_append(&buf, php_http_buffer_append(&buf, old_url->n, strlen(old_url->n) + 1)); \
152 } \
153 } while (0)
154
155 php_http_url_t *php_http_url_mod(const php_http_url_t *old_url, const php_http_url_t *new_url, unsigned flags)
156 {
157 php_http_url_t *tmp_url = NULL;
158 php_http_buffer_t buf;
159
160 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
161 php_http_buffer_account(&buf, sizeof(php_http_url_t));
162 memset(buf.data, 0, buf.used);
163
164 /* set from env if requested */
165 if (flags & PHP_HTTP_URL_FROM_ENV) {
166 php_http_url_t *env_url = php_http_url_from_env();
167
168 old_url = tmp_url = php_http_url_mod(env_url, old_url, flags ^ PHP_HTTP_URL_FROM_ENV);
169 php_http_url_free(&env_url);
170 }
171
172 url_copy(scheme);
173
174 if (!(flags & PHP_HTTP_URL_STRIP_USER)) {
175 url_copy(user);
176 }
177
178 if (!(flags & PHP_HTTP_URL_STRIP_PASS)) {
179 url_copy(pass);
180 }
181
182 url_copy(host);
183
184 if (!(flags & PHP_HTTP_URL_STRIP_PORT)) {
185 url(buf)->port = url_isset(new_url, port) ? new_url->port : ((old_url) ? old_url->port : 0);
186 }
187
188 if (!(flags & PHP_HTTP_URL_STRIP_PATH)) {
189 if ((flags & PHP_HTTP_URL_JOIN_PATH) && url_isset(old_url, path) && url_isset(new_url, path) && *new_url->path != '/') {
190 size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path);
191 char *path = ecalloc(1, old_path_len + new_path_len + 1 + 1);
192
193 strcat(path, old_url->path);
194 if (path[old_path_len - 1] != '/') {
195 php_dirname(path, old_path_len);
196 strcat(path, "/");
197 }
198 strcat(path, new_url->path);
199
200 url(buf)->path = &buf.data[buf.used];
201 if (path[0] != '/') {
202 url_append(&buf, php_http_buffer_append(&buf, "/", 1));
203 }
204 url_append(&buf, php_http_buffer_append(&buf, path, strlen(path) + 1));
205 efree(path);
206 } else {
207 const char *path = NULL;
208
209 if (url_isset(new_url, path)) {
210 path = new_url->path;
211 } else if (url_isset(old_url, path)) {
212 path = old_url->path;
213 }
214
215 if (path) {
216 url(buf)->path = &buf.data[buf.used];
217
218 url_append(&buf, php_http_buffer_append(&buf, path, strlen(path) + 1));
219 }
220
221
222 }
223 }
224
225 if (!(flags & PHP_HTTP_URL_STRIP_QUERY)) {
226 if ((flags & PHP_HTTP_URL_JOIN_QUERY) && url_isset(new_url, query) && url_isset(old_url, query)) {
227 zval qarr, qstr;
228
229 array_init(&qarr);
230
231 ZVAL_STRING(&qstr, old_url->query);
232 php_http_querystring_update(&qarr, &qstr, NULL);
233 zval_ptr_dtor(&qstr);
234 ZVAL_STRING(&qstr, new_url->query);
235 php_http_querystring_update(&qarr, &qstr, NULL);
236 zval_ptr_dtor(&qstr);
237
238 ZVAL_NULL(&qstr);
239 php_http_querystring_update(&qarr, NULL, &qstr);
240
241 url(buf)->query = &buf.data[buf.used];
242 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL(qstr), Z_STRLEN(qstr) + 1));
243
244 zval_dtor(&qstr);
245 zval_dtor(&qarr);
246 } else {
247 url_copy(query);
248 }
249 }
250
251 if (!(flags & PHP_HTTP_URL_STRIP_FRAGMENT)) {
252 url_copy(fragment);
253 }
254
255 /* done with copy & combine & strip */
256
257 if (flags & PHP_HTTP_URL_FROM_ENV) {
258 /* free old_url we tainted above */
259 php_http_url_free(&tmp_url);
260 }
261
262 /* replace directory references if path is not a single slash */
263 if ((flags & PHP_HTTP_URL_SANITIZE_PATH)
264 && url(buf)->path[0] && url(buf)->path[1]) {
265 char *ptr, *end = url(buf)->path + strlen(url(buf)->path) + 1;
266
267 for (ptr = strchr(url(buf)->path, '/'); ptr; ptr = strchr(ptr, '/')) {
268 switch (ptr[1]) {
269 case '/':
270 memmove(&ptr[1], &ptr[2], end - &ptr[2]);
271 break;
272
273 case '.':
274 switch (ptr[2]) {
275 case '\0':
276 ptr[1] = '\0';
277 break;
278
279 case '/':
280 memmove(&ptr[1], &ptr[3], end - &ptr[3]);
281 break;
282
283 case '.':
284 if (ptr[3] == '/') {
285 char *pos = &ptr[4];
286 while (ptr != url(buf)->path) {
287 if (*--ptr == '/') {
288 break;
289 }
290 }
291 memmove(&ptr[1], pos, end - pos);
292 break;
293 } else if (!ptr[3]) {
294 /* .. at the end */
295 ptr[1] = '\0';
296 }
297 /* no break */
298
299 default:
300 /* something else */
301 ++ptr;
302 break;
303 }
304 break;
305
306 default:
307 ++ptr;
308 break;
309 }
310 }
311 }
312 /* unset default ports */
313 if (url(buf)->port) {
314 if ( ((url(buf)->port == 80) && url(buf)->scheme && !strcmp(url(buf)->scheme, "http"))
315 || ((url(buf)->port ==443) && url(buf)->scheme && !strcmp(url(buf)->scheme, "https"))
316 ) {
317 url(buf)->port = 0;
318 }
319 }
320
321 return url(buf);
322 }
323
324 char *php_http_url_to_string(const php_http_url_t *url, char **url_str, size_t *url_len, zend_bool persistent)
325 {
326 php_http_buffer_t buf;
327
328 php_http_buffer_init_ex(&buf, PHP_HTTP_BUFFER_DEFAULT_SIZE, persistent ?
329 PHP_HTTP_BUFFER_INIT_PERSISTENT : 0);
330
331 if (url->scheme && *url->scheme) {
332 php_http_buffer_appendl(&buf, url->scheme);
333 php_http_buffer_appends(&buf, "://");
334 } else if ((url->user && *url->user) || (url->host && *url->host)) {
335 php_http_buffer_appends(&buf, "//");
336 }
337
338 if (url->user && *url->user) {
339 php_http_buffer_appendl(&buf, url->user);
340 if (url->pass && *url->pass) {
341 php_http_buffer_appends(&buf, ":");
342 php_http_buffer_appendl(&buf, url->pass);
343 }
344 php_http_buffer_appends(&buf, "@");
345 }
346
347 if (url->host && *url->host) {
348 php_http_buffer_appendl(&buf, url->host);
349 if (url->port) {
350 php_http_buffer_appendf(&buf, ":%hu", url->port);
351 }
352 }
353
354 if (url->path && *url->path) {
355 if (*url->path != '/') {
356 php_http_buffer_appends(&buf, "/");
357 }
358 php_http_buffer_appendl(&buf, url->path);
359 } else if (buf.used) {
360 php_http_buffer_appends(&buf, "/");
361 }
362
363 if (url->query && *url->query) {
364 php_http_buffer_appends(&buf, "?");
365 php_http_buffer_appendl(&buf, url->query);
366 }
367
368 if (url->fragment && *url->fragment) {
369 php_http_buffer_appends(&buf, "#");
370 php_http_buffer_appendl(&buf, url->fragment);
371 }
372
373 php_http_buffer_shrink(&buf);
374 php_http_buffer_fix(&buf);
375
376 if (url_len) {
377 *url_len = buf.used;
378 }
379
380 if (url_str) {
381 *url_str = buf.data;
382 }
383
384 return buf.data;
385 }
386
387 char *php_http_url_authority_to_string(const php_http_url_t *url, char **url_str, size_t *url_len)
388 {
389 php_http_buffer_t buf;
390
391 php_http_buffer_init(&buf);
392
393 if (url->user && *url->user) {
394 php_http_buffer_appendl(&buf, url->user);
395 if (url->pass && *url->pass) {
396 php_http_buffer_appends(&buf, ":");
397 php_http_buffer_appendl(&buf, url->pass);
398 }
399 php_http_buffer_appends(&buf, "@");
400 }
401
402 if (url->host && *url->host) {
403 php_http_buffer_appendl(&buf, url->host);
404 if (url->port) {
405 php_http_buffer_appendf(&buf, ":%hu", url->port);
406 }
407 }
408
409 php_http_buffer_shrink(&buf);
410 php_http_buffer_fix(&buf);
411
412 if (url_len) {
413 *url_len = buf.used;
414 }
415
416 if (url_str) {
417 *url_str = buf.data;
418 }
419
420 return buf.data;
421 }
422
423 php_http_url_t *php_http_url_from_zval(zval *value, unsigned flags)
424 {
425 zend_string *zs;
426 php_http_url_t *purl;
427
428 switch (Z_TYPE_P(value)) {
429 case IS_ARRAY:
430 case IS_OBJECT:
431 purl = php_http_url_from_struct(HASH_OF(value));
432 break;
433
434 default:
435 zs = zval_get_string(value);
436 purl = php_http_url_parse(zs->val, zs->len, flags);
437 zend_string_release(zs);
438 }
439
440 return purl;
441 }
442
443 php_http_url_t *php_http_url_from_struct(HashTable *ht)
444 {
445 zval *e;
446 php_http_buffer_t buf;
447
448 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
449 php_http_buffer_account(&buf, sizeof(php_http_url_t));
450 memset(buf.data, 0, buf.used);
451
452 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("scheme")))) {
453 zend_string *zs = zval_get_string(e);
454 url(buf)->scheme = &buf.data[buf.used];
455 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
456 zend_string_release(zs);
457 }
458 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("user")))) {
459 zend_string *zs = zval_get_string(e);
460 url(buf)->user = &buf.data[buf.used];
461 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
462 zend_string_release(zs);
463 }
464 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("pass")))) {
465 zend_string *zs = zval_get_string(e);
466 url(buf)->pass = &buf.data[buf.used];
467 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
468 zend_string_release(zs);
469 }
470 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("host")))) {
471 zend_string *zs = zval_get_string(e);
472 url(buf)->host = &buf.data[buf.used];
473 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
474 zend_string_release(zs);
475 }
476 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("port")))) {
477 url(buf)->port = (unsigned short) zval_get_long(e);
478 }
479 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("path")))) {
480 zend_string *zs = zval_get_string(e);
481 url(buf)->path = &buf.data[buf.used];
482 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
483 zend_string_release(zs);
484 }
485 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("query")))) {
486 zend_string *zs = zval_get_string(e);
487 url(buf)->query = &buf.data[buf.used];
488 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
489 zend_string_release(zs);
490 }
491 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("fragment")))) {
492 zend_string *zs = zval_get_string(e);
493 url(buf)->fragment = &buf.data[buf.used];
494 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
495 zend_string_release(zs);
496 }
497
498 return url(buf);
499 }
500
501 HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct)
502 {
503 HashTable *ht;
504 zval tmp;
505
506 if (strct) {
507 switch (Z_TYPE_P(strct)) {
508 default:
509 zval_dtor(strct);
510 array_init(strct);
511 /* no break */
512 case IS_ARRAY:
513 case IS_OBJECT:
514 ht = HASH_OF(strct);
515 break;
516 }
517 } else {
518 ALLOC_HASHTABLE(ht);
519 zend_hash_init(ht, 8, NULL, ZVAL_PTR_DTOR, 0);
520 }
521
522 #define url_struct_add(part) \
523 if (Z_TYPE_P(strct) == IS_ARRAY) { \
524 zend_hash_str_update(Z_ARRVAL_P(strct), part, lenof(part), &tmp); \
525 } else { \
526 zend_update_property(Z_OBJCE_P(strct), strct, part, lenof(part), &tmp); \
527 zval_ptr_dtor(&tmp); \
528 }
529
530 if (url) {
531 if (url->scheme) {
532 ZVAL_STRING(&tmp, url->scheme);
533 url_struct_add("scheme");
534 }
535 if (url->user) {
536 ZVAL_STRING(&tmp, url->user);
537 url_struct_add("user");
538 }
539 if (url->pass) {
540 ZVAL_STRING(&tmp, url->pass);
541 url_struct_add("pass");
542 }
543 if (url->host) {
544 ZVAL_STRING(&tmp, url->host);
545 url_struct_add("host");
546 }
547 if (url->port) {
548 ZVAL_LONG(&tmp, url->port);
549 url_struct_add("port");
550 }
551 if (url->path) {
552 ZVAL_STRING(&tmp, url->path);
553 url_struct_add("path");
554 }
555 if (url->query) {
556 ZVAL_STRING(&tmp, url->query);
557 url_struct_add("query");
558 }
559 if (url->fragment) {
560 ZVAL_STRING(&tmp, url->fragment);
561 url_struct_add("fragment");
562 }
563 }
564
565 return ht;
566 }
567
568 ZEND_RESULT_CODE php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len)
569 {
570 const char *arg_sep_str;
571 size_t arg_sep_len;
572 php_http_buffer_t *qstr = php_http_buffer_new();
573
574 php_http_url_argsep(&arg_sep_str, &arg_sep_len);
575
576 if (SUCCESS != php_http_url_encode_hash_ex(hash, qstr, arg_sep_str, arg_sep_len, "=", 1, pre_encoded_str, pre_encoded_len)) {
577 php_http_buffer_free(&qstr);
578 return FAILURE;
579 }
580
581 php_http_buffer_data(qstr, encoded_str, encoded_len);
582 php_http_buffer_free(&qstr);
583
584 return SUCCESS;
585 }
586
587 ZEND_RESULT_CODE php_http_url_encode_hash_ex(HashTable *hash, php_http_buffer_t *qstr, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *pre_encoded_str, size_t pre_encoded_len)
588 {
589 if (pre_encoded_len && pre_encoded_str) {
590 php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len);
591 }
592
593 if (!php_http_params_to_string(qstr, hash, arg_sep_str, arg_sep_len, "", 0, val_sep_str, val_sep_len, PHP_HTTP_PARAMS_QUERY)) {
594 return FAILURE;
595 }
596
597 return SUCCESS;
598 }
599
600 struct parse_state {
601 php_http_url_t url;
602 const char *ptr;
603 const char *end;
604 size_t maxlen;
605 off_t offset;
606 unsigned flags;
607 char buffer[1]; /* last member */
608 };
609
610 void php_http_url_free(php_http_url_t **url)
611 {
612 if (*url) {
613 efree(*url);
614 *url = NULL;
615 }
616 }
617
618 php_http_url_t *php_http_url_copy(const php_http_url_t *url, zend_bool persistent)
619 {
620 php_http_url_t *cpy;
621 const char *end = NULL, *url_ptr = (const char *) url;
622 char *cpy_ptr;
623
624 end = MAX(url->scheme, end);
625 end = MAX(url->pass, end);
626 end = MAX(url->user, end);
627 end = MAX(url->host, end);
628 end = MAX(url->path, end);
629 end = MAX(url->query, end);
630 end = MAX(url->fragment, end);
631
632 if (end) {
633 end += strlen(end) + 1;
634 cpy_ptr = pecalloc(1, end - url_ptr, persistent);
635 cpy = (php_http_url_t *) cpy_ptr;
636
637 memcpy(cpy_ptr + sizeof(*cpy), url_ptr + sizeof(*url), end - url_ptr - sizeof(*url));
638
639 cpy->scheme = url->scheme ? cpy_ptr + (url->scheme - url_ptr) : NULL;
640 cpy->pass = url->pass ? cpy_ptr + (url->pass - url_ptr) : NULL;
641 cpy->user = url->user ? cpy_ptr + (url->user - url_ptr) : NULL;
642 cpy->host = url->host ? cpy_ptr + (url->host - url_ptr) : NULL;
643 cpy->path = url->path ? cpy_ptr + (url->path - url_ptr) : NULL;
644 cpy->query = url->query ? cpy_ptr + (url->query - url_ptr) : NULL;
645 cpy->fragment = url->fragment ? cpy_ptr + (url->fragment - url_ptr) : NULL;
646 } else {
647 cpy = ecalloc(1, sizeof(*url));
648 }
649
650 cpy->port = url->port;
651
652 return cpy;
653 }
654
655 static size_t parse_mb_utf8(unsigned *wc, const char *ptr, const char *end)
656 {
657 unsigned wchar;
658 size_t consumed = utf8towc(&wchar, (const unsigned char *) ptr, end - ptr);
659
660 if (!consumed || consumed == (size_t) -1) {
661 return 0;
662 }
663
664 if (wc) {
665 *wc = wchar;
666 }
667 return consumed;
668 }
669
670 #ifdef PHP_HTTP_HAVE_WCHAR
671 static size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
672 {
673 wchar_t wchar;
674 size_t consumed = 0;
675 #if defined(HAVE_MBRTOWC)
676 mbstate_t ps;
677
678 memset(&ps, 0, sizeof(ps));
679 consumed = mbrtowc(&wchar, ptr, end - ptr, &ps);
680 #elif defined(HAVE_MBTOWC)
681 consumed = mbtowc(&wchar, ptr, end - ptr);
682 #endif
683
684 if (!consumed || consumed == (size_t) -1) {
685 return 0;
686 }
687
688 if (wc) {
689 *wc = wchar;
690 }
691 return consumed;
692 }
693 #endif
694
695 typedef enum parse_mb_what {
696 PARSE_SCHEME,
697 PARSE_USERINFO,
698 PARSE_HOSTINFO,
699 PARSE_PATH,
700 PARSE_QUERY,
701 PARSE_FRAGMENT
702 } parse_mb_what_t;
703
704 static const char * const parse_what[] = {
705 "scheme",
706 "userinfo",
707 "hostinfo",
708 "path",
709 "query",
710 "fragment"
711 };
712
713 static const char parse_xdigits[] = "0123456789ABCDEF";
714
715 static size_t parse_mb(struct parse_state *state, parse_mb_what_t what, const char *ptr, const char *end, const char *begin, zend_bool silent)
716 {
717 unsigned wchar;
718 size_t consumed = 0;
719
720 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
721 consumed = parse_mb_utf8(&wchar, ptr, end);
722 }
723 #ifdef PHP_HTTP_HAVE_WCHAR
724 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
725 consumed = parse_mb_loc(&wchar, ptr, end);
726 }
727 #endif
728
729 while (consumed) {
730 if (!(state->flags & PHP_HTTP_URL_PARSE_TOPCT) || what == PARSE_HOSTINFO || what == PARSE_SCHEME) {
731 if (what == PARSE_HOSTINFO && (state->flags & PHP_HTTP_URL_PARSE_TOIDN)) {
732 /* idna */
733 } else if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
734 if (!isualnum(wchar)) {
735 break;
736 }
737 #ifdef PHP_HTTP_HAVE_WCHAR
738 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
739 if (!iswalnum(wchar)) {
740 break;
741 }
742 #endif
743 }
744 PHP_HTTP_DUFF(consumed, state->buffer[state->offset++] = *ptr++);
745 } else {
746 int i = 0;
747
748 PHP_HTTP_DUFF(consumed,
749 state->buffer[state->offset++] = '%';
750 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) >> 4];
751 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) & 0xf];
752 ++i;
753 );
754 }
755
756 return consumed;
757 }
758
759 if (!silent) {
760 if (consumed) {
761 php_error_docref(NULL, E_WARNING,
762 "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
763 parse_what[what], wchar, (unsigned) (ptr - begin), begin);
764 } else {
765 php_error_docref(NULL, E_WARNING,
766 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
767 parse_what[what], (unsigned char) *ptr, (unsigned) (ptr - begin), begin);
768 }
769 }
770
771 return 0;
772 }
773
774 static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *ptr)
775 {
776 size_t mb;
777 const char *password = NULL, *end = state->ptr, *tmp = ptr;
778
779 state->url.user = &state->buffer[state->offset];
780
781 do {
782 switch (*ptr) {
783 case ':':
784 if (password) {
785 php_error_docref(NULL, E_WARNING,
786 "Failed to parse password; duplicate ':' at pos %u in '%s'",
787 (unsigned) (ptr - tmp), tmp);
788 return FAILURE;
789 }
790 password = ptr + 1;
791 state->buffer[state->offset++] = 0;
792 state->url.pass = &state->buffer[state->offset];
793 break;
794
795 case '%':
796 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
797 php_error_docref(NULL, E_WARNING,
798 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
799 (unsigned) (ptr - tmp), tmp);
800 return FAILURE;
801 }
802 state->buffer[state->offset++] = *ptr++;
803 state->buffer[state->offset++] = *ptr++;
804 state->buffer[state->offset++] = *ptr;
805 break;
806
807 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
808 case '+': case ',': case ';': case '=': /* sub-delims */
809 case '-': case '.': case '_': case '~': /* unreserved */
810 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
811 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
812 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
813 case 'V': case 'W': case 'X': case 'Y': case 'Z':
814 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
815 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
816 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
817 case 'v': case 'w': case 'x': case 'y': case 'z':
818 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
819 case '7': case '8': case '9':
820 /* allowed */
821 state->buffer[state->offset++] = *ptr;
822 break;
823
824 default:
825 if (!(mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, 0))) {
826 return FAILURE;
827 }
828 ptr += mb - 1;
829 }
830 } while(++ptr != end);
831
832
833 state->buffer[state->offset++] = 0;
834
835 return SUCCESS;
836 }
837
838 #if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
839 typedef size_t (*parse_mb_func)(unsigned *wc, const char *ptr, const char *end);
840 static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len)
841 {
842 size_t offset = 0, u8_len = strlen(u8);
843
844 *u16 = ecalloc(4 * sizeof(uint16_t), u8_len + 1);
845 *len = 0;
846
847 while (offset < u8_len) {
848 unsigned wc;
849 uint16_t buf[2], *ptr = buf;
850 size_t consumed = fn(&wc, &u8[offset], &u8[u8_len]);
851
852 if (!consumed) {
853 efree(*u16);
854 php_error_docref(NULL, E_WARNING, "Failed to parse UTF-8 at pos %zu of '%s'", offset, u8);
855 return FAILURE;
856 } else {
857 offset += consumed;
858 }
859
860 switch (wctoutf16(buf, wc)) {
861 case 2:
862 (*u16)[(*len)++] = *ptr++;
863 /* no break */
864 case 1:
865 (*u16)[(*len)++] = *ptr++;
866 break;
867 case 0:
868 default:
869 efree(*u16);
870 php_error_docref(NULL, E_WARNING, "Failed to convert UTF-32 'U+%X' to UTF-16", wc);
871 return FAILURE;
872 }
873 }
874
875 return SUCCESS;
876 }
877 #endif
878
879 #ifndef MAXHOSTNAMELEN
880 # define MAXHOSTNAMELEN 256
881 #endif
882
883 #ifdef PHP_HTTP_HAVE_IDN
884 static ZEND_RESULT_CODE parse_idn(struct parse_state *state, size_t prev_len)
885 {
886 char *idn = NULL;
887 int rv = -1;
888
889 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
890 rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
891 }
892 # ifdef PHP_HTTP_HAVE_WCHAR
893 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
894 rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
895 }
896 # endif
897 if (rv != IDNA_SUCCESS) {
898 php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv));
899 return FAILURE;
900 } else {
901 size_t idnlen = strlen(idn);
902 memcpy(state->url.host, idn, idnlen + 1);
903 free(idn);
904 state->offset += idnlen - prev_len;
905 return SUCCESS;
906 }
907 }
908 #endif
909
910 #ifdef HAVE_UIDNA_IDNTOASCII
911 # if HAVE_UNICODE_UIDNA_H
912 # include <unicode/uidna.h>
913 # else
914 typedef uint16_t UChar;
915 typedef enum { U_ZERO_ERROR = 0 } UErrorCode;
916 int32_t uidna_IDNToASCII(const UChar *src, int32_t srcLength, UChar *dest, int32_t destCapacity, int32_t options, void *parseError, UErrorCode *status);
917 # endif
918 static ZEND_RESULT_CODE parse_uidn(struct parse_state *state)
919 {
920 char *host_ptr;
921 uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
922 size_t uhost_len, ahost_len;
923 UErrorCode error = U_ZERO_ERROR;
924 TSRMLS_FETCH_FROM_CTX(state->ts);
925
926 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
927 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
928 return FAILURE;
929 }
930 #ifdef PHP_HTTP_HAVE_WCHAR
931 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
932 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
933 return FAILURE;
934 }
935 #endif
936 } else {
937 php_error_docref(NULL, E_WARNING, "Failed to parse IDN; codepage not specified");
938 return FAILURE;
939 }
940
941 ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN, 3, NULL, &error);
942 efree(uhost_str);
943
944 if (error != U_ZERO_ERROR) {
945 php_error_docref(NULL, E_WARNING, "Failed to parse IDN; ICU error %d", error);
946 return FAILURE;
947 }
948
949 host_ptr = state->url.host;
950 ahost_ptr = ahost_str;
951 PHP_HTTP_DUFF(ahost_len, *host_ptr++ = *ahost_ptr++);
952
953 *host_ptr = '\0';
954 state->offset += host_ptr - state->url.host;
955
956 return SUCCESS;
957 }
958 #endif
959
960 #if 0 && defined(PHP_WIN32)
961 static ZEND_RESULT_CODE parse_widn(struct parse_state *state)
962 {
963 char *host_ptr;
964 uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
965 size_t uhost_len;
966
967 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
968 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
969 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
970 return FAILURE;
971 }
972 #ifdef PHP_HTTP_HAVE_WCHAR
973 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
974 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
975 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
976 return FAILURE;
977 }
978 #endif
979 } else {
980 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
981 return FAILURE;
982 }
983
984 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED|IDN_USE_STD3_ASCII_RULES, uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN)) {
985 efree(uhost_str);
986 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
987 return FAILURE;
988 }
989
990 efree(uhost_str);
991 host_ptr = state->url.host;
992 ahost_ptr = ahost_str;
993 PHP_HTTP_DUFF(wcslen(ahost_str), *host_ptr++ = *ahost_ptr++);
994 efree(ahost_str);
995
996 *host_ptr = '\0';
997 state->offset += host_ptr - state->url.host;
998
999 return SUCCESS;
1000 }
1001 #endif
1002
1003 static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *ptr)
1004 {
1005 size_t mb, len;
1006 const char *end = state->ptr, *tmp = ptr, *port = NULL;
1007
1008 #ifdef HAVE_INET_PTON
1009 if (*ptr == '[') {
1010 char *error = NULL, *tmp = memchr(ptr, ']', end - ptr);
1011
1012 if (tmp) {
1013 size_t addrlen = tmp - ptr + 1;
1014 char buf[16], *addr = estrndup(ptr + 1, addrlen - 2);
1015 int rv = inet_pton(AF_INET6, addr, buf);
1016
1017 efree(addr);
1018 if (rv == 1) {
1019 state->buffer[state->offset] = '[';
1020 state->url.host = &state->buffer[state->offset];
1021 inet_ntop(AF_INET6, buf, state->url.host + 1, state->maxlen - state->offset);
1022 state->offset += strlen(state->url.host);
1023 state->buffer[state->offset++] = ']';
1024 state->buffer[state->offset++] = 0;
1025 ptr = tmp + 1;
1026 } else if (rv == -1) {
1027 error = strerror(errno);
1028 } else {
1029 error = "unexpected '['";
1030 }
1031 } else {
1032 error = "expected ']'";
1033 }
1034
1035 if (error) {
1036 php_error_docref(NULL, E_WARNING, "Failed to parse hostinfo; %s", error);
1037 return FAILURE;
1038 }
1039 }
1040 #endif
1041 if (ptr != end) do {
1042 switch (*ptr) {
1043 case ':':
1044 if (port) {
1045 php_error_docref(NULL, E_WARNING,
1046 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1047 (unsigned) (ptr - tmp), tmp);
1048 return FAILURE;
1049 }
1050 port = ptr + 1;
1051 break;
1052
1053 case '%':
1054 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
1055 php_error_docref(NULL, E_WARNING,
1056 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1057 (unsigned) (ptr - tmp), tmp);
1058 return FAILURE;
1059 }
1060 state->buffer[state->offset++] = *ptr++;
1061 state->buffer[state->offset++] = *ptr++;
1062 state->buffer[state->offset++] = *ptr;
1063 break;
1064
1065 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1066 case '+': case ',': case ';': case '=': /* sub-delims */
1067 case '-': case '.': case '_': case '~': /* unreserved */
1068 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1069 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1070 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1071 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1072 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1073 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1074 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1075 case 'v': case 'w': case 'x': case 'y': case 'z':
1076 if (port) {
1077 php_error_docref(NULL, E_WARNING,
1078 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1079 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1080 return FAILURE;
1081 }
1082 /* no break */
1083 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1084 case '7': case '8': case '9':
1085 /* allowed */
1086 if (port) {
1087 state->url.port *= 10;
1088 state->url.port += *ptr - '0';
1089 } else {
1090 state->buffer[state->offset++] = *ptr;
1091 }
1092 break;
1093
1094 default:
1095 if (ptr == end) {
1096 break;
1097 } else if (port) {
1098 php_error_docref(NULL, E_WARNING,
1099 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1100 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1101 return FAILURE;
1102 } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, 0))) {
1103 return FAILURE;
1104 }
1105 ptr += mb - 1;
1106 }
1107 } while (++ptr != end);
1108
1109 if (!state->url.host) {
1110 len = (port ? port - tmp - 1 : end - tmp);
1111 state->url.host = &state->buffer[state->offset - len];
1112 state->buffer[state->offset++] = 0;
1113 }
1114
1115 if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
1116 #ifdef PHP_HTTP_HAVE_IDN
1117 return parse_idn(state, len);
1118 #endif
1119 #ifdef HAVE_UIDNA_IDNTOASCII
1120 return parse_uidn(state);
1121 #endif
1122 #if 0 && defined(PHP_WIN32)
1123 return parse_widn(state);
1124 #endif
1125 }
1126
1127 return SUCCESS;
1128 }
1129
1130 static const char *parse_authority(struct parse_state *state)
1131 {
1132 const char *tmp = state->ptr, *host = NULL;
1133
1134 do {
1135 switch (*state->ptr) {
1136 case '@':
1137 /* userinfo delimiter */
1138 if (host) {
1139 php_error_docref(NULL, E_WARNING,
1140 "Failed to parse userinfo; unexpected '@'");
1141 return NULL;
1142 }
1143 host = state->ptr + 1;
1144 if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) {
1145 return NULL;
1146 }
1147 tmp = state->ptr + 1;
1148 break;
1149
1150 case '/':
1151 case '?':
1152 case '#':
1153 case '\0':
1154 EOD:
1155 /* host delimiter */
1156 if (tmp != state->ptr && SUCCESS != parse_hostinfo(state, tmp)) {
1157 return NULL;
1158 }
1159 return state->ptr;
1160 }
1161 } while (++state->ptr <= state->end);
1162
1163 --state->ptr;
1164 goto EOD;
1165 }
1166
1167 static const char *parse_path(struct parse_state *state)
1168 {
1169 size_t mb;
1170 const char *tmp;
1171
1172 /* is there actually a path to parse? */
1173 if (!*state->ptr) {
1174 return state->ptr;
1175 }
1176 tmp = state->ptr;
1177 state->url.path = &state->buffer[state->offset];
1178
1179 do {
1180 switch (*state->ptr) {
1181 case '#':
1182 case '?':
1183 goto done;
1184
1185 case '%':
1186 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1187 php_error_docref(NULL, E_WARNING,
1188 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1189 (unsigned) (state->ptr - tmp), tmp);
1190 return NULL;
1191 }
1192 state->buffer[state->offset++] = *state->ptr++;
1193 state->buffer[state->offset++] = *state->ptr++;
1194 state->buffer[state->offset++] = *state->ptr;
1195 break;
1196
1197 case '/': /* yeah, well */
1198 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1199 case '+': case ',': case ';': case '=': /* sub-delims */
1200 case '-': case '.': case '_': case '~': /* unreserved */
1201 case ':': case '@': /* pchar */
1202 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1203 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1204 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1205 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1206 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1207 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1208 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1209 case 'v': case 'w': case 'x': case 'y': case 'z':
1210 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1211 case '7': case '8': case '9':
1212 /* allowed */
1213 state->buffer[state->offset++] = *state->ptr;
1214 break;
1215
1216 default:
1217 if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) {
1218 return NULL;
1219 }
1220 state->ptr += mb - 1;
1221 }
1222 } while (++state->ptr < state->end);
1223
1224 done:
1225 /* did we have any path component ? */
1226 if (tmp != state->ptr) {
1227 state->buffer[state->offset++] = 0;
1228 } else {
1229 state->url.path = NULL;
1230 }
1231 return state->ptr;
1232 }
1233
1234 static const char *parse_query(struct parse_state *state)
1235 {
1236 size_t mb;
1237 const char *tmp = state->ptr + !!*state->ptr;
1238
1239 /* is there actually a query to parse? */
1240 if (*state->ptr != '?') {
1241 return state->ptr;
1242 }
1243
1244 /* skip initial '?' */
1245 tmp = ++state->ptr;
1246 state->url.query = &state->buffer[state->offset];
1247
1248 do {
1249 switch (*state->ptr) {
1250 case '#':
1251 goto done;
1252
1253 case '%':
1254 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1255 php_error_docref(NULL, E_WARNING,
1256 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1257 (unsigned) (state->ptr - tmp), tmp);
1258 return NULL;
1259 }
1260 state->buffer[state->offset++] = *state->ptr++;
1261 state->buffer[state->offset++] = *state->ptr++;
1262 state->buffer[state->offset++] = *state->ptr;
1263 break;
1264
1265 case ']':
1266 case '[':
1267 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1268 state->buffer[state->offset++] = '%';
1269 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1270 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1271 break;
1272 }
1273 /* no break */
1274
1275 case '?': case '/': /* yeah, well */
1276 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1277 case '+': case ',': case ';': case '=': /* sub-delims */
1278 case '-': case '.': case '_': case '~': /* unreserved */
1279 case ':': case '@': /* pchar */
1280 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1281 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1282 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1283 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1284 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1285 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1286 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1287 case 'v': case 'w': case 'x': case 'y': case 'z':
1288 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1289 case '7': case '8': case '9':
1290 /* allowed */
1291 state->buffer[state->offset++] = *state->ptr;
1292 break;
1293
1294 default:
1295 if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) {
1296 return NULL;
1297 }
1298 state->ptr += mb - 1;
1299 }
1300 } while (++state->ptr < state->end);
1301
1302 done:
1303 state->buffer[state->offset++] = 0;
1304 return state->ptr;
1305 }
1306
1307 static const char *parse_fragment(struct parse_state *state)
1308 {
1309 size_t mb;
1310 const char *tmp;
1311
1312 /* is there actually a fragment to parse? */
1313 if (*state->ptr != '#') {
1314 return state->ptr;
1315 }
1316
1317 /* skip initial '#' */
1318 tmp = ++state->ptr;
1319 state->url.fragment = &state->buffer[state->offset];
1320
1321 do {
1322 switch (*state->ptr) {
1323 case '%':
1324 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1325 php_error_docref(NULL, E_WARNING,
1326 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1327 (unsigned) (state->ptr - tmp), tmp);
1328 return NULL;
1329 }
1330 state->buffer[state->offset++] = *state->ptr++;
1331 state->buffer[state->offset++] = *state->ptr++;
1332 state->buffer[state->offset++] = *state->ptr;
1333 break;
1334
1335 case '?': case '/':
1336 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1337 case '+': case ',': case ';': case '=': /* sub-delims */
1338 case '-': case '.': case '_': case '~': /* unreserved */
1339 case ':': case '@': /* pchar */
1340 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1341 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1342 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1343 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1344 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1345 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1346 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1347 case 'v': case 'w': case 'x': case 'y': case 'z':
1348 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1349 case '7': case '8': case '9':
1350 /* allowed */
1351 state->buffer[state->offset++] = *state->ptr;
1352 break;
1353
1354 default:
1355 if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) {
1356 return NULL;
1357 }
1358 state->ptr += mb - 1;
1359 }
1360 } while (++state->ptr < state->end);
1361
1362 state->buffer[state->offset++] = 0;
1363 return state->ptr;
1364 }
1365
1366 static const char *parse_hier(struct parse_state *state)
1367 {
1368 if (*state->ptr == '/') {
1369 if (state->end - state->ptr > 1) {
1370 if (*(state->ptr + 1) == '/') {
1371 state->ptr += 2;
1372 if (!(state->ptr = parse_authority(state))) {
1373 return NULL;
1374 }
1375 }
1376 }
1377 }
1378 return parse_path(state);
1379 }
1380
1381 static const char *parse_scheme(struct parse_state *state)
1382 {
1383 size_t mb;
1384 const char *tmp = state->ptr;
1385
1386 do {
1387 switch (*state->ptr) {
1388 case ':':
1389 /* scheme delimiter */
1390 state->url.scheme = &state->buffer[0];
1391 state->buffer[state->offset++] = 0;
1392 return ++state->ptr;
1393
1394 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1395 case '7': case '8': case '9':
1396 case '+': case '-': case '.':
1397 if (state->ptr == tmp) {
1398 return tmp;
1399 }
1400 /* no break */
1401 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1402 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1403 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1404 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1405 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1406 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1407 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1408 case 'v': case 'w': case 'x': case 'y': case 'z':
1409 /* scheme part */
1410 state->buffer[state->offset++] = *state->ptr;
1411 break;
1412
1413 default:
1414 if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
1415 /* soft fail; parse path next */
1416 return tmp;
1417 }
1418 state->ptr += mb - 1;
1419 }
1420 } while (++state->ptr != state->end);
1421
1422 return state->ptr = tmp;
1423 }
1424
1425 php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags)
1426 {
1427 size_t maxlen = 3 * len;
1428 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1429
1430 state->end = str + len;
1431 state->ptr = str;
1432 state->flags = flags;
1433 state->maxlen = maxlen;
1434
1435 if (!parse_scheme(state)) {
1436 php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
1437 efree(state);
1438 return NULL;
1439 }
1440
1441 if (!parse_hier(state)) {
1442 efree(state);
1443 return NULL;
1444 }
1445
1446 if (!parse_query(state)) {
1447 php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
1448 efree(state);
1449 return NULL;
1450 }
1451
1452 if (!parse_fragment(state)) {
1453 php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
1454 efree(state);
1455 return NULL;
1456 }
1457
1458 return (php_http_url_t *) state;
1459 }
1460
1461 php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags)
1462 {
1463 size_t maxlen = 3 * len;
1464 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1465
1466 state->end = str + len;
1467 state->ptr = str;
1468 state->flags = flags;
1469 state->maxlen = maxlen;
1470 TSRMLS_SET_CTX(state->ts);
1471
1472 if (!(state->ptr = parse_authority(state))) {
1473 efree(state);
1474 return NULL;
1475 }
1476
1477 if (state->ptr != state->end) {
1478 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1479 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1480 (unsigned) (state->ptr - str), str);
1481 efree(state);
1482 return NULL;
1483 }
1484
1485 return (php_http_url_t *) state;
1486 }
1487
1488 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0)
1489 ZEND_ARG_INFO(0, old_url)
1490 ZEND_ARG_INFO(0, new_url)
1491 ZEND_ARG_INFO(0, flags)
1492 ZEND_END_ARG_INFO();
1493 PHP_METHOD(HttpUrl, __construct)
1494 {
1495 zval *new_url = NULL, *old_url = NULL;
1496 zend_long flags = PHP_HTTP_URL_FROM_ENV;
1497 zend_error_handling zeh;
1498
1499 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return);
1500
1501 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
1502 {
1503 php_http_url_t *res_purl, *new_purl = NULL, *old_purl = NULL;
1504
1505 if (new_url) {
1506 new_purl = php_http_url_from_zval(new_url, flags);
1507 if (!new_purl) {
1508 zend_restore_error_handling(&zeh);
1509 return;
1510 }
1511 }
1512 if (old_url) {
1513 old_purl = php_http_url_from_zval(old_url, flags);
1514 if (!old_purl) {
1515 if (new_purl) {
1516 php_http_url_free(&new_purl);
1517 }
1518 zend_restore_error_handling(&zeh);
1519 return;
1520 }
1521 }
1522
1523 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1524 php_http_url_to_struct(res_purl, getThis());
1525
1526 php_http_url_free(&res_purl);
1527 if (old_purl) {
1528 php_http_url_free(&old_purl);
1529 }
1530 if (new_purl) {
1531 php_http_url_free(&new_purl);
1532 }
1533 }
1534 zend_restore_error_handling(&zeh);
1535 }
1536
1537 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod, 0, 0, 1)
1538 ZEND_ARG_INFO(0, more_url_parts)
1539 ZEND_ARG_INFO(0, flags)
1540 ZEND_END_ARG_INFO();
1541 PHP_METHOD(HttpUrl, mod)
1542 {
1543 zval *new_url = NULL;
1544 zend_long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY;
1545 zend_error_handling zeh;
1546
1547 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url, &flags), invalid_arg, return);
1548
1549 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
1550 {
1551 php_http_url_t *new_purl = NULL, *old_purl = NULL;
1552
1553 if (new_url) {
1554 new_purl = php_http_url_from_zval(new_url, flags);
1555 if (!new_purl) {
1556 zend_restore_error_handling(&zeh);
1557 return;
1558 }
1559 }
1560
1561 if ((old_purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1562 php_http_url_t *res_purl;
1563
1564 ZVAL_OBJ(return_value, zend_objects_clone_obj(getThis()));
1565
1566 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1567 php_http_url_to_struct(res_purl, return_value);
1568
1569 php_http_url_free(&res_purl);
1570 php_http_url_free(&old_purl);
1571 }
1572 if (new_purl) {
1573 php_http_url_free(&new_purl);
1574 }
1575 }
1576 zend_restore_error_handling(&zeh);
1577 }
1578
1579 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString, 0, 0, 0)
1580 ZEND_END_ARG_INFO();
1581 PHP_METHOD(HttpUrl, toString)
1582 {
1583 if (SUCCESS == zend_parse_parameters_none()) {
1584 php_http_url_t *purl;
1585
1586 if ((purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1587 char *str;
1588 size_t len;
1589
1590 php_http_url_to_string(purl, &str, &len, 0);
1591 php_http_url_free(&purl);
1592 RETURN_STR(php_http_cs2zs(str, len));
1593 }
1594 }
1595 RETURN_EMPTY_STRING();
1596 }
1597
1598 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray, 0, 0, 0)
1599 ZEND_END_ARG_INFO();
1600 PHP_METHOD(HttpUrl, toArray)
1601 {
1602 php_http_url_t *purl;
1603
1604 if (SUCCESS != zend_parse_parameters_none()) {
1605 return;
1606 }
1607
1608 /* strip any non-URL properties */
1609 purl = php_http_url_from_struct(HASH_OF(getThis()));
1610 php_http_url_to_struct(purl, return_value);
1611 php_http_url_free(&purl);
1612 }
1613
1614 static zend_function_entry php_http_url_methods[] = {
1615 PHP_ME(HttpUrl, __construct, ai_HttpUrl___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1616 PHP_ME(HttpUrl, mod, ai_HttpUrl_mod, ZEND_ACC_PUBLIC)
1617 PHP_ME(HttpUrl, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
1618 ZEND_MALIAS(HttpUrl, __toString, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
1619 PHP_ME(HttpUrl, toArray, ai_HttpUrl_toArray, ZEND_ACC_PUBLIC)
1620 EMPTY_FUNCTION_ENTRY
1621 };
1622
1623 zend_class_entry *php_http_url_class_entry;
1624
1625 PHP_MINIT_FUNCTION(http_url)
1626 {
1627 zend_class_entry ce = {0};
1628
1629 INIT_NS_CLASS_ENTRY(ce, "http", "Url", php_http_url_methods);
1630 php_http_url_class_entry = zend_register_internal_class(&ce);
1631
1632 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC);
1633 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC);
1634 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC);
1635 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC);
1636 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC);
1637 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("path"), ZEND_ACC_PUBLIC);
1638 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC);
1639 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC);
1640
1641 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE);
1642 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH);
1643 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY);
1644 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER);
1645 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS);
1646 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH);
1647 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT);
1648 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH);
1649 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY);
1650 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT);
1651 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL);
1652 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV);
1653 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH);
1654
1655 #ifdef PHP_HTTP_HAVE_WCHAR
1656 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC);
1657 #endif
1658 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8);
1659 #if defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
1660 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN);
1661 #endif
1662 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT);
1663
1664 return SUCCESS;
1665 }
1666
1667
1668 /*
1669 * Local variables:
1670 * tab-width: 4
1671 * c-basic-offset: 4
1672 * End:
1673 * vim600: noet sw=4 ts=4 fdm=marker
1674 * vim<600: noet sw=4 ts=4
1675 */
1676