add brotli encoding
[m6w6/ext-http] / src / 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 #if PHP_HTTP_HAVE_LIBIDN
16 # include <idna.h>
17 #endif
18 #if PHP_HTTP_HAVE_LIBIDN2
19 # include <idn2.h>
20 #endif
21 #if PHP_HTTP_HAVE_LIBICU
22 # include <unicode/uidna.h>
23 #endif
24 #if PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2
25 # include <idn/api.h>
26 # include <idn/result.h>
27 #endif
28
29 #if PHP_HTTP_HAVE_WCHAR
30 # include <wchar.h>
31 # include <wctype.h>
32 #endif
33
34 #if HAVE_ARPA_INET_H
35 # include <arpa/inet.h>
36 #endif
37
38 #include "php_http_utf8.h"
39
40 static inline char *localhostname(void)
41 {
42 char hostname[1024] = {0};
43
44 #if PHP_WIN32
45 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
46 return estrdup(hostname);
47 }
48 #elif HAVE_GETHOSTNAME
49 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
50 # if HAVE_GETDOMAINNAME
51 size_t hlen = strlen(hostname);
52 if (hlen <= lenof(hostname) - lenof("(none)")) {
53 hostname[hlen++] = '.';
54 if (SUCCESS == getdomainname(&hostname[hlen], lenof(hostname) - hlen)) {
55 if (!strcmp(&hostname[hlen], "(none)")) {
56 hostname[hlen - 1] = '\0';
57 }
58 return estrdup(hostname);
59 }
60 }
61 # endif
62 if (strcmp(hostname, "(none)")) {
63 return estrdup(hostname);
64 }
65 }
66 #endif
67 return estrndup("localhost", lenof("localhost"));
68 }
69
70 #define url(buf) ((php_http_url_t *) (buf).data)
71
72 static php_http_url_t *php_http_url_from_env(void)
73 {
74 zval *https, *zhost, *zport;
75 long port;
76 php_http_buffer_t buf;
77
78 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
79 php_http_buffer_account(&buf, sizeof(php_http_url_t));
80 memset(buf.data, 0, buf.used);
81
82 /* scheme */
83 url(buf)->scheme = &buf.data[buf.used];
84 https = php_http_env_get_server_var(ZEND_STRL("HTTPS"), 1);
85 if (https && !strcasecmp(Z_STRVAL_P(https), "ON")) {
86 php_http_buffer_append(&buf, "https", sizeof("https"));
87 } else {
88 php_http_buffer_append(&buf, "http", sizeof("http"));
89 }
90
91 /* host */
92 url(buf)->host = &buf.data[buf.used];
93 if ((((zhost = php_http_env_get_server_var(ZEND_STRL("HTTP_HOST"), 1)) ||
94 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1)) ||
95 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1)))) && Z_STRLEN_P(zhost)) {
96 size_t stop_at = strspn(Z_STRVAL_P(zhost), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.");
97
98 php_http_buffer_append(&buf, Z_STRVAL_P(zhost), stop_at);
99 php_http_buffer_append(&buf, "", 1);
100 } else {
101 char *host_str = localhostname();
102
103 php_http_buffer_append(&buf, host_str, strlen(host_str) + 1);
104 efree(host_str);
105 }
106
107 /* port */
108 zport = php_http_env_get_server_var(ZEND_STRL("SERVER_PORT"), 1);
109 if (zport && IS_LONG == is_numeric_string(Z_STRVAL_P(zport), Z_STRLEN_P(zport), &port, NULL, 0)) {
110 url(buf)->port = port;
111 }
112
113 /* path */
114 if (SG(request_info).request_uri && SG(request_info).request_uri[0]) {
115 const char *q = strchr(SG(request_info).request_uri, '?');
116
117 url(buf)->path = &buf.data[buf.used];
118
119 if (q) {
120 php_http_buffer_append(&buf, SG(request_info).request_uri, q - SG(request_info).request_uri);
121 php_http_buffer_append(&buf, "", 1);
122 } else {
123 php_http_buffer_append(&buf, SG(request_info).request_uri, strlen(SG(request_info).request_uri) + 1);
124 }
125 }
126
127 /* query */
128 if (SG(request_info).query_string && SG(request_info).query_string[0]) {
129 url(buf)->query = &buf.data[buf.used];
130 php_http_buffer_append(&buf, SG(request_info).query_string, strlen(SG(request_info).query_string) + 1);
131 }
132
133 return url(buf);
134 }
135
136 #define url_isset(u,n) \
137 ((u)&&(u)->n)
138 #define url_append(buf, append) do { \
139 char *_ptr = (buf)->data; \
140 php_http_url_t *_url = (php_http_url_t *) _ptr, _mem = *_url; \
141 append; \
142 /* relocate */ \
143 if (_ptr != (buf)->data) { \
144 ptrdiff_t diff = (buf)->data - _ptr; \
145 _url = (php_http_url_t *) (buf)->data; \
146 if (_mem.scheme) _url->scheme += diff; \
147 if (_mem.user) _url->user += diff; \
148 if (_mem.pass) _url->pass += diff; \
149 if (_mem.host) _url->host += diff; \
150 if (_mem.path) _url->path += diff; \
151 if (_mem.query) _url->query += diff; \
152 if (_mem.fragment) _url->fragment += diff; \
153 } \
154 } while (0)
155 #define url_copy(n) do { \
156 if (url_isset(new_url, n)) { \
157 url(buf)->n = &buf.data[buf.used]; \
158 url_append(&buf, php_http_buffer_append(&buf, new_url->n, strlen(new_url->n) + 1)); \
159 } else if (url_isset(old_url, n)) { \
160 url(buf)->n = &buf.data[buf.used]; \
161 url_append(&buf, php_http_buffer_append(&buf, old_url->n, strlen(old_url->n) + 1)); \
162 } \
163 } while (0)
164
165 php_http_url_t *php_http_url_mod(const php_http_url_t *old_url, const php_http_url_t *new_url, unsigned flags)
166 {
167 php_http_url_t *tmp_url = NULL;
168 php_http_buffer_t buf;
169
170 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
171 php_http_buffer_account(&buf, sizeof(php_http_url_t));
172 memset(buf.data, 0, buf.used);
173
174 /* set from env if requested */
175 if (flags & PHP_HTTP_URL_FROM_ENV) {
176 php_http_url_t *env_url = php_http_url_from_env();
177
178 old_url = tmp_url = php_http_url_mod(env_url, old_url, flags ^ PHP_HTTP_URL_FROM_ENV);
179 php_http_url_free(&env_url);
180 }
181
182 url_copy(scheme);
183
184 if (!(flags & PHP_HTTP_URL_STRIP_USER)) {
185 url_copy(user);
186 }
187
188 if (!(flags & PHP_HTTP_URL_STRIP_PASS)) {
189 url_copy(pass);
190 }
191
192 url_copy(host);
193
194 if (!(flags & PHP_HTTP_URL_STRIP_PORT)) {
195 url(buf)->port = url_isset(new_url, port) ? new_url->port : ((old_url) ? old_url->port : 0);
196 }
197
198 if (!(flags & PHP_HTTP_URL_STRIP_PATH)) {
199 if ((flags & PHP_HTTP_URL_JOIN_PATH) && url_isset(old_url, path) && url_isset(new_url, path) && *new_url->path != '/') {
200 size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path);
201 char *path = ecalloc(1, old_path_len + new_path_len + 1 + 1);
202
203 strcat(path, old_url->path);
204 if (path[old_path_len - 1] != '/') {
205 php_dirname(path, old_path_len);
206 strcat(path, "/");
207 }
208 strcat(path, new_url->path);
209
210 url(buf)->path = &buf.data[buf.used];
211 if (path[0] != '/') {
212 url_append(&buf, php_http_buffer_append(&buf, "/", 1));
213 }
214 url_append(&buf, php_http_buffer_append(&buf, path, strlen(path) + 1));
215 efree(path);
216 } else {
217 const char *path = NULL;
218
219 if (url_isset(new_url, path)) {
220 path = new_url->path;
221 } else if (url_isset(old_url, path)) {
222 path = old_url->path;
223 }
224
225 if (path) {
226 url(buf)->path = &buf.data[buf.used];
227
228 url_append(&buf, php_http_buffer_append(&buf, path, strlen(path) + 1));
229 }
230
231
232 }
233 }
234
235 if (!(flags & PHP_HTTP_URL_STRIP_QUERY)) {
236 if ((flags & PHP_HTTP_URL_JOIN_QUERY) && url_isset(new_url, query) && url_isset(old_url, query)) {
237 zval qarr, qstr;
238
239 array_init(&qarr);
240
241 ZVAL_STRING(&qstr, old_url->query);
242 php_http_querystring_update(&qarr, &qstr, NULL);
243 zval_ptr_dtor(&qstr);
244 ZVAL_STRING(&qstr, new_url->query);
245 php_http_querystring_update(&qarr, &qstr, NULL);
246 zval_ptr_dtor(&qstr);
247
248 ZVAL_NULL(&qstr);
249 php_http_querystring_update(&qarr, NULL, &qstr);
250
251 url(buf)->query = &buf.data[buf.used];
252 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL(qstr), Z_STRLEN(qstr) + 1));
253
254 zval_dtor(&qstr);
255 zval_dtor(&qarr);
256 } else {
257 url_copy(query);
258 }
259 }
260
261 if (!(flags & PHP_HTTP_URL_STRIP_FRAGMENT)) {
262 url_copy(fragment);
263 }
264
265 /* done with copy & combine & strip */
266
267 if (flags & PHP_HTTP_URL_FROM_ENV) {
268 /* free old_url we tainted above */
269 php_http_url_free(&tmp_url);
270 }
271
272 /* replace directory references if path is not a single slash */
273 if ((flags & PHP_HTTP_URL_SANITIZE_PATH)
274 && url(buf)->path
275 && url(buf)->path[0] && url(buf)->path[1]) {
276 char *ptr, *end = url(buf)->path + strlen(url(buf)->path) + 1;
277
278 for (ptr = strchr(url(buf)->path, '/'); ptr; ptr = strchr(ptr, '/')) {
279 switch (ptr[1]) {
280 case '/':
281 memmove(&ptr[1], &ptr[2], end - &ptr[2]);
282 break;
283
284 case '.':
285 switch (ptr[2]) {
286 case '\0':
287 ptr[1] = '\0';
288 break;
289
290 case '/':
291 memmove(&ptr[1], &ptr[3], end - &ptr[3]);
292 break;
293
294 case '.':
295 if (ptr[3] == '/') {
296 char *pos = &ptr[4];
297 while (ptr != url(buf)->path) {
298 if (*--ptr == '/') {
299 break;
300 }
301 }
302 memmove(&ptr[1], pos, end - pos);
303 break;
304 } else if (!ptr[3]) {
305 /* .. at the end */
306 ptr[1] = '\0';
307 }
308 /* no break */
309
310 default:
311 /* something else */
312 ++ptr;
313 break;
314 }
315 break;
316
317 default:
318 ++ptr;
319 break;
320 }
321 }
322 }
323 /* unset default ports */
324 if (url(buf)->port) {
325 if ( ((url(buf)->port == 80) && url(buf)->scheme && !strcmp(url(buf)->scheme, "http"))
326 || ((url(buf)->port ==443) && url(buf)->scheme && !strcmp(url(buf)->scheme, "https"))
327 ) {
328 url(buf)->port = 0;
329 }
330 }
331
332 return url(buf);
333 }
334
335 char *php_http_url_to_string(const php_http_url_t *url, char **url_str, size_t *url_len, zend_bool persistent)
336 {
337 php_http_buffer_t buf;
338
339 php_http_buffer_init_ex(&buf, PHP_HTTP_BUFFER_DEFAULT_SIZE, persistent ?
340 PHP_HTTP_BUFFER_INIT_PERSISTENT : 0);
341
342 if (url->scheme && *url->scheme) {
343 php_http_buffer_appendl(&buf, url->scheme);
344 php_http_buffer_appends(&buf, "://");
345 } else if ((url->user && *url->user) || (url->host && *url->host)) {
346 php_http_buffer_appends(&buf, "//");
347 }
348
349 if (url->user && *url->user) {
350 php_http_buffer_appendl(&buf, url->user);
351 if (url->pass && *url->pass) {
352 php_http_buffer_appends(&buf, ":");
353 php_http_buffer_appendl(&buf, url->pass);
354 }
355 php_http_buffer_appends(&buf, "@");
356 }
357
358 if (url->host && *url->host) {
359 php_http_buffer_appendl(&buf, url->host);
360 if (url->port) {
361 php_http_buffer_appendf(&buf, ":%hu", url->port);
362 }
363 }
364
365 if (url->path && *url->path) {
366 if (*url->path != '/') {
367 php_http_buffer_appends(&buf, "/");
368 }
369 php_http_buffer_appendl(&buf, url->path);
370 } else if (buf.used) {
371 php_http_buffer_appends(&buf, "/");
372 }
373
374 if (url->query && *url->query) {
375 php_http_buffer_appends(&buf, "?");
376 php_http_buffer_appendl(&buf, url->query);
377 }
378
379 if (url->fragment && *url->fragment) {
380 php_http_buffer_appends(&buf, "#");
381 php_http_buffer_appendl(&buf, url->fragment);
382 }
383
384 php_http_buffer_shrink(&buf);
385 php_http_buffer_fix(&buf);
386
387 if (url_len) {
388 *url_len = buf.used;
389 }
390
391 if (url_str) {
392 *url_str = buf.data;
393 }
394
395 return buf.data;
396 }
397
398 char *php_http_url_authority_to_string(const php_http_url_t *url, char **url_str, size_t *url_len)
399 {
400 php_http_buffer_t buf;
401
402 php_http_buffer_init(&buf);
403
404 if (url->user && *url->user) {
405 php_http_buffer_appendl(&buf, url->user);
406 if (url->pass && *url->pass) {
407 php_http_buffer_appends(&buf, ":");
408 php_http_buffer_appendl(&buf, url->pass);
409 }
410 php_http_buffer_appends(&buf, "@");
411 }
412
413 if (url->host && *url->host) {
414 php_http_buffer_appendl(&buf, url->host);
415 if (url->port) {
416 php_http_buffer_appendf(&buf, ":%hu", url->port);
417 }
418 }
419
420 php_http_buffer_shrink(&buf);
421 php_http_buffer_fix(&buf);
422
423 if (url_len) {
424 *url_len = buf.used;
425 }
426
427 if (url_str) {
428 *url_str = buf.data;
429 }
430
431 return buf.data;
432 }
433
434 php_http_url_t *php_http_url_from_zval(zval *value, unsigned flags)
435 {
436 zend_string *zs;
437 php_http_url_t *purl;
438
439 switch (Z_TYPE_P(value)) {
440 case IS_ARRAY:
441 case IS_OBJECT:
442 purl = php_http_url_from_struct(HASH_OF(value));
443 break;
444
445 default:
446 zs = zval_get_string(value);
447 purl = php_http_url_parse(zs->val, zs->len, flags);
448 zend_string_release(zs);
449 }
450
451 return purl;
452 }
453
454 php_http_url_t *php_http_url_from_struct(HashTable *ht)
455 {
456 zval *e;
457 php_http_buffer_t buf;
458
459 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
460 php_http_buffer_account(&buf, sizeof(php_http_url_t));
461 memset(buf.data, 0, buf.used);
462
463 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("scheme")))) {
464 zend_string *zs = zval_get_string(e);
465 url(buf)->scheme = &buf.data[buf.used];
466 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
467 zend_string_release(zs);
468 }
469 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("user")))) {
470 zend_string *zs = zval_get_string(e);
471 url(buf)->user = &buf.data[buf.used];
472 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
473 zend_string_release(zs);
474 }
475 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("pass")))) {
476 zend_string *zs = zval_get_string(e);
477 url(buf)->pass = &buf.data[buf.used];
478 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
479 zend_string_release(zs);
480 }
481 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("host")))) {
482 zend_string *zs = zval_get_string(e);
483 url(buf)->host = &buf.data[buf.used];
484 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
485 zend_string_release(zs);
486 }
487 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("port")))) {
488 url(buf)->port = (unsigned short) zval_get_long(e);
489 }
490 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("path")))) {
491 zend_string *zs = zval_get_string(e);
492 url(buf)->path = &buf.data[buf.used];
493 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
494 zend_string_release(zs);
495 }
496 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("query")))) {
497 zend_string *zs = zval_get_string(e);
498 url(buf)->query = &buf.data[buf.used];
499 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
500 zend_string_release(zs);
501 }
502 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("fragment")))) {
503 zend_string *zs = zval_get_string(e);
504 url(buf)->fragment = &buf.data[buf.used];
505 url_append(&buf, php_http_buffer_append(&buf, zs->val, zs->len + 1));
506 zend_string_release(zs);
507 }
508
509 return url(buf);
510 }
511
512 HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct)
513 {
514 HashTable *ht = NULL;
515 zval tmp;
516
517 if (strct) {
518 switch (Z_TYPE_P(strct)) {
519 default:
520 zval_dtor(strct);
521 array_init(strct);
522 /* no break */
523 case IS_ARRAY:
524 case IS_OBJECT:
525 ht = HASH_OF(strct);
526 break;
527 }
528 } else {
529 ALLOC_HASHTABLE(ht);
530 zend_hash_init(ht, 8, NULL, ZVAL_PTR_DTOR, 0);
531 }
532
533 #define url_struct_add(part) \
534 if (!strct || Z_TYPE_P(strct) == IS_ARRAY) { \
535 zend_hash_str_update(ht, part, lenof(part), &tmp); \
536 } else { \
537 zend_update_property(Z_OBJCE_P(strct), strct, part, lenof(part), &tmp); \
538 zval_ptr_dtor(&tmp); \
539 }
540
541 if (url) {
542 if (url->scheme) {
543 ZVAL_STRING(&tmp, url->scheme);
544 url_struct_add("scheme");
545 }
546 if (url->user) {
547 ZVAL_STRING(&tmp, url->user);
548 url_struct_add("user");
549 }
550 if (url->pass) {
551 ZVAL_STRING(&tmp, url->pass);
552 url_struct_add("pass");
553 }
554 if (url->host) {
555 ZVAL_STRING(&tmp, url->host);
556 url_struct_add("host");
557 }
558 if (url->port) {
559 ZVAL_LONG(&tmp, url->port);
560 url_struct_add("port");
561 }
562 if (url->path) {
563 ZVAL_STRING(&tmp, url->path);
564 url_struct_add("path");
565 }
566 if (url->query) {
567 ZVAL_STRING(&tmp, url->query);
568 url_struct_add("query");
569 }
570 if (url->fragment) {
571 ZVAL_STRING(&tmp, url->fragment);
572 url_struct_add("fragment");
573 }
574 }
575
576 return ht;
577 }
578
579 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)
580 {
581 const char *arg_sep_str = "&";
582 size_t arg_sep_len = 1;
583 php_http_buffer_t *qstr = php_http_buffer_new();
584
585 php_http_url_argsep(&arg_sep_str, &arg_sep_len);
586
587 if (SUCCESS != php_http_url_encode_hash_ex(hash, qstr, arg_sep_str, arg_sep_len, "=", 1, pre_encoded_str, pre_encoded_len)) {
588 php_http_buffer_free(&qstr);
589 return FAILURE;
590 }
591
592 php_http_buffer_data(qstr, encoded_str, encoded_len);
593 php_http_buffer_free(&qstr);
594
595 return SUCCESS;
596 }
597
598 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)
599 {
600 if (pre_encoded_len && pre_encoded_str) {
601 php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len);
602 }
603
604 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)) {
605 return FAILURE;
606 }
607
608 return SUCCESS;
609 }
610
611 struct parse_state {
612 php_http_url_t url;
613 const char *ptr;
614 const char *end;
615 size_t maxlen;
616 off_t offset;
617 unsigned flags;
618 char buffer[1]; /* last member */
619 };
620
621 void php_http_url_free(php_http_url_t **url)
622 {
623 if (*url) {
624 efree(*url);
625 *url = NULL;
626 }
627 }
628
629 php_http_url_t *php_http_url_copy(const php_http_url_t *url, zend_bool persistent)
630 {
631 php_http_url_t *cpy;
632 const char *end = NULL, *url_ptr = (const char *) url;
633 char *cpy_ptr;
634
635 end = MAX(url->scheme, end);
636 end = MAX(url->pass, end);
637 end = MAX(url->user, end);
638 end = MAX(url->host, end);
639 end = MAX(url->path, end);
640 end = MAX(url->query, end);
641 end = MAX(url->fragment, end);
642
643 if (end) {
644 end += strlen(end) + 1;
645 cpy_ptr = pecalloc(1, end - url_ptr, persistent);
646 cpy = (php_http_url_t *) cpy_ptr;
647
648 memcpy(cpy_ptr + sizeof(*cpy), url_ptr + sizeof(*url), end - url_ptr - sizeof(*url));
649
650 cpy->scheme = url->scheme ? cpy_ptr + (url->scheme - url_ptr) : NULL;
651 cpy->pass = url->pass ? cpy_ptr + (url->pass - url_ptr) : NULL;
652 cpy->user = url->user ? cpy_ptr + (url->user - url_ptr) : NULL;
653 cpy->host = url->host ? cpy_ptr + (url->host - url_ptr) : NULL;
654 cpy->path = url->path ? cpy_ptr + (url->path - url_ptr) : NULL;
655 cpy->query = url->query ? cpy_ptr + (url->query - url_ptr) : NULL;
656 cpy->fragment = url->fragment ? cpy_ptr + (url->fragment - url_ptr) : NULL;
657 } else {
658 cpy = ecalloc(1, sizeof(*url));
659 }
660
661 cpy->port = url->port;
662
663 return cpy;
664 }
665
666 static inline size_t parse_mb_utf8(unsigned *wc, const char *ptr, const char *end)
667 {
668 unsigned wchar;
669 size_t consumed = utf8towc(&wchar, (const unsigned char *) ptr, end - ptr);
670
671 if (!consumed || consumed == (size_t) -1) {
672 return 0;
673 }
674
675 if (wc) {
676 *wc = wchar;
677 }
678 return consumed;
679 }
680
681 #if PHP_HTTP_HAVE_WCHAR
682 static inline size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
683 {
684 wchar_t wchar;
685 size_t consumed = 0;
686 #if HAVE_MBRTOWC
687 mbstate_t ps;
688
689 memset(&ps, 0, sizeof(ps));
690 consumed = mbrtowc(&wchar, ptr, end - ptr, &ps);
691 #elif HAVE_MBTOWC
692 consumed = mbtowc(&wchar, ptr, end - ptr);
693 #endif
694
695 if (!consumed || consumed == (size_t) -1) {
696 return 0;
697 }
698
699 if (wc) {
700 *wc = wchar;
701 }
702 return consumed;
703 }
704 #endif
705
706 typedef enum parse_mb_what {
707 PARSE_SCHEME,
708 PARSE_USERINFO,
709 PARSE_HOSTINFO,
710 PARSE_PATH,
711 PARSE_QUERY,
712 PARSE_FRAGMENT
713 } parse_mb_what_t;
714
715 static const char * const parse_what[] = {
716 "scheme",
717 "userinfo",
718 "hostinfo",
719 "path",
720 "query",
721 "fragment"
722 };
723
724 static const char parse_xdigits[] = "0123456789ABCDEF";
725
726 static inline size_t parse_mb(struct parse_state *state, parse_mb_what_t what, const char *ptr, const char *end, const char *begin, zend_bool force_silent)
727 {
728 unsigned wchar;
729 size_t consumed = 0;
730
731 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
732 consumed = parse_mb_utf8(&wchar, ptr, end);
733 }
734 #if PHP_HTTP_HAVE_WCHAR
735 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
736 consumed = parse_mb_loc(&wchar, ptr, end);
737 }
738 #endif
739
740 while (consumed) {
741 if (!(state->flags & PHP_HTTP_URL_PARSE_TOPCT) || what == PARSE_HOSTINFO || what == PARSE_SCHEME) {
742 if (what == PARSE_HOSTINFO && (state->flags & PHP_HTTP_URL_PARSE_TOIDN)) {
743 /* idna */
744 } else if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
745 if (!isualnum(wchar)) {
746 break;
747 }
748 #if PHP_HTTP_HAVE_WCHAR
749 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
750 if (!iswalnum(wchar)) {
751 break;
752 }
753 #endif
754 }
755
756 memcpy(&state->buffer[state->offset], ptr, consumed);
757 state->offset += consumed;
758 } else {
759 size_t i;
760
761 for (i = 0; i < consumed; ++i) {
762 state->buffer[state->offset++] = '%';
763 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) >> 4];
764 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) & 0xf];
765 }
766 }
767
768 return consumed;
769 }
770
771 if (!force_silent && !(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
772 if (consumed) {
773 php_error_docref(NULL, E_WARNING,
774 "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
775 parse_what[what], wchar, (unsigned) (ptr - begin), begin);
776 } else {
777 php_error_docref(NULL, E_WARNING,
778 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
779 parse_what[what], (unsigned char) *ptr, (unsigned) (ptr - begin), begin);
780 }
781 }
782
783 if (state->flags & PHP_HTTP_URL_IGNORE_ERRORS) {
784 state->buffer[state->offset++] = *ptr;
785 return 1;
786 }
787
788 return 0;
789 }
790
791 static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *ptr)
792 {
793 size_t mb;
794 const char *password = NULL, *end = state->ptr, *tmp = ptr;
795
796 state->url.user = &state->buffer[state->offset];
797
798 do {
799 switch (*ptr) {
800 case ':':
801 if (password) {
802 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
803 php_error_docref(NULL, E_WARNING,
804 "Failed to parse password; duplicate ':' at pos %u in '%s'",
805 (unsigned) (ptr - tmp), tmp);
806 }
807 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
808 return FAILURE;
809 }
810 state->buffer[state->offset++] = *ptr;
811 break;
812 }
813 password = ptr + 1;
814 state->buffer[state->offset++] = 0;
815 state->url.pass = &state->buffer[state->offset];
816 break;
817
818 case '%':
819 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
820 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
821 php_error_docref(NULL, E_WARNING,
822 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
823 (unsigned) (ptr - tmp), tmp);
824 }
825 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
826 return FAILURE;
827 }
828 state->buffer[state->offset++] = *ptr++;
829 break;
830 }
831 state->buffer[state->offset++] = *ptr++;
832 state->buffer[state->offset++] = *ptr++;
833 state->buffer[state->offset++] = *ptr;
834 break;
835
836 default:
837 if ((mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, 0))) {
838 ptr += mb - 1;
839 break;
840 }
841 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
842 return FAILURE;
843 }
844 /* no break */
845 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
846 case '+': case ',': case ';': case '=': /* sub-delims */
847 case '-': case '.': case '_': case '~': /* unreserved */
848 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
849 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
850 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
851 case 'V': case 'W': case 'X': case 'Y': case 'Z':
852 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
853 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
854 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
855 case 'v': case 'w': case 'x': case 'y': case 'z':
856 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
857 case '7': case '8': case '9':
858 /* allowed */
859 state->buffer[state->offset++] = *ptr;
860 break;
861
862 }
863 } while(++ptr < end);
864
865
866 state->buffer[state->offset++] = 0;
867
868 return SUCCESS;
869 }
870
871 #if PHP_WIN32 || HAVE_UIDNA_IDNTOASCII
872 typedef size_t (*parse_mb_func)(unsigned *wc, const char *ptr, const char *end);
873 static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len)
874 {
875 size_t offset = 0, u8_len = strlen(u8);
876
877 *u16 = ecalloc(4 * sizeof(uint16_t), u8_len + 1);
878 *len = 0;
879
880 while (offset < u8_len) {
881 unsigned wc;
882 uint16_t buf[2], *ptr = buf;
883 size_t consumed = fn(&wc, &u8[offset], &u8[u8_len]);
884
885 if (!consumed) {
886 efree(*u16);
887 php_error_docref(NULL, E_WARNING, "Failed to parse UTF-8 at pos %zu of '%s'", offset, u8);
888 return FAILURE;
889 } else {
890 offset += consumed;
891 }
892
893 switch (wctoutf16(buf, wc)) {
894 case 2:
895 (*u16)[(*len)++] = *ptr++;
896 /* no break */
897 case 1:
898 (*u16)[(*len)++] = *ptr++;
899 break;
900 case 0:
901 default:
902 efree(*u16);
903 php_error_docref(NULL, E_WARNING, "Failed to convert UTF-32 'U+%X' to UTF-16", wc);
904 return FAILURE;
905 }
906 }
907
908 return SUCCESS;
909 }
910 #endif
911
912 #if PHP_HTTP_HAVE_LIBIDN2
913 # if __GNUC__
914 __attribute__ ((unused))
915 # endif
916 static ZEND_RESULT_CODE parse_gidn_2008(struct parse_state *state, size_t prev_len)
917 {
918 char *idn = NULL;
919 int rv = -1;
920
921 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
922 rv = idn2_lookup_u8((const unsigned char *) state->url.host, (unsigned char **) &idn, IDN2_NFC_INPUT);
923 }
924 # if PHP_HTTP_HAVE_WCHAR
925 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
926 rv = idn2_lookup_ul(state->url.host, &idn, 0);
927 }
928 # endif
929 if (rv != IDN2_OK) {
930 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
931 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (IDNA2008); %s", idn2_strerror(rv));
932 }
933 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
934 return FAILURE;
935 }
936 } else {
937 size_t idnlen = strlen(idn);
938 memcpy(state->url.host, idn, idnlen + 1);
939 free(idn);
940 state->offset += idnlen - prev_len;
941 }
942 return SUCCESS;
943 }
944 #endif
945
946 #if PHP_HTTP_HAVE_LIBIDN
947 # if __GNUC__
948 __attribute__ ((unused))
949 # endif
950 static ZEND_RESULT_CODE parse_gidn_2003(struct parse_state *state, size_t prev_len)
951 {
952 char *idn = NULL;
953 int rv = -1;
954
955 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
956 rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED);
957 }
958 # if PHP_HTTP_HAVE_WCHAR
959 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
960 rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED);
961 }
962 # endif
963 if (rv != IDNA_SUCCESS) {
964 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
965 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (IDNA2003); %s", idna_strerror(rv));
966 }
967 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
968 return FAILURE;
969 }
970 } else {
971 size_t idnlen = strlen(idn);
972 memcpy(state->url.host, idn, idnlen + 1);
973 free(idn);
974 state->offset += idnlen - prev_len;
975 }
976 return SUCCESS;
977 }
978 #endif
979
980 #if HAVE_UIDNA_IDNTOASCII
981 # if !PHP_HTTP_HAVE_LIBICU
982 typedef uint16_t UChar;
983 typedef enum { U_ZERO_ERROR = 0 } UErrorCode;
984 int32_t uidna_IDNToASCII(const UChar *src, int32_t srcLength, UChar *dest, int32_t destCapacity, int32_t options, void *parseError, UErrorCode *status);
985 # endif
986 static ZEND_RESULT_CODE parse_uidn_2003(struct parse_state *state, size_t prev_len)
987 {
988 char ebuf[64] = {0}, *error = NULL;
989 uint16_t *uhost_str, ahost_str[256];
990 size_t uhost_len, ahost_len;
991 UErrorCode rc = U_ZERO_ERROR;
992
993 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
994 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
995 error = "failed to convert to UTF-16";
996 goto error;
997 }
998 #if PHP_HTTP_HAVE_WCHAR
999 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1000 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
1001 error = "failed to convert to UTF-16";
1002 goto error;
1003 }
1004 #endif
1005 } else {
1006 error = "codepage not specified";
1007 goto error;
1008 }
1009
1010 # if __GNUC__ >= 5
1011 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1012 # endif
1013 ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, 256, 3, NULL, &rc);
1014 # if __GNUC__ >= 5
1015 # pragma GCC diagnostic pop
1016 # endif
1017
1018 efree(uhost_str);
1019 if (error > U_ZERO_ERROR) {
1020 goto error;
1021 }
1022
1023 state->url.host[ahost_len] = '\0';
1024 state->offset += ahost_len - prev_len;
1025 while (ahost_len--) {
1026 state->url.host[ahost_len] = ahost_str[ahost_len];
1027 }
1028
1029 return SUCCESS;
1030
1031 error:
1032 if (!error) {
1033 slprintf(ebuf, sizeof(ebuf)-1, "errorcode: %d", rc);
1034 error = ebuf;
1035 }
1036 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (ICU IDNA2003); %s", error);
1037
1038 return FAILURE;
1039 }
1040 #endif
1041
1042 #if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
1043 static ZEND_RESULT_CODE parse_uidn_2008(struct parse_state *state, size_t prev_len)
1044 {
1045 char *error = NULL, ebuf[64] = {0};
1046 UErrorCode rc = U_ZERO_ERROR;
1047 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
1048 UIDNA *uidna = uidna_openUTS46(UIDNA_ALLOW_UNASSIGNED, &rc);
1049
1050 if (!uidna || U_FAILURE(rc)) {
1051 return FAILURE;
1052 }
1053
1054 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
1055 char ahost_str[256];
1056 size_t ahost_len = uidna_nameToASCII_UTF8(uidna, state->url.host, -1, ahost_str, sizeof(ahost_str)-1, &info, &rc);
1057
1058 if (U_FAILURE(rc) || info.errors) {
1059 goto error;
1060 }
1061
1062 memcpy(state->url.host, ahost_str, ahost_len);
1063 state->url.host[ahost_len] = '\0';
1064 state->offset += ahost_len - prev_len;
1065
1066 #if PHP_HTTP_HAVE_WCHAR
1067 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1068 uint16_t *uhost_str, whost_str[256];
1069 size_t uhost_len, whost_len;
1070
1071 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
1072 error = "could not convert to UTF-16";
1073 goto error;
1074 }
1075
1076 whost_len = uidna_nameToASCII(uidna, uhost_str, uhost_len, whost_str, sizeof(whost_str)-1, &info, &rc);
1077 efree(uhost_str);
1078
1079 if (U_FAILURE(rc) || info.errors) {
1080 goto error;
1081 }
1082
1083 state->url.host[whost_len] = '\0';
1084 state->offset += whost_len - prev_len;
1085 while (whost_len--) {
1086 state->url.host[whost_len] = whost_str[whost_len];
1087 }
1088 #endif
1089 } else {
1090 error = "codepage not specified";
1091 goto error;
1092 }
1093
1094 uidna_close(uidna);
1095 return SUCCESS;
1096
1097 error:
1098 if (!error) {
1099 if (U_FAILURE(rc)) {
1100 slprintf(ebuf, sizeof(ebuf)-1, "%s", u_errorName(rc));
1101 error = ebuf;
1102 } else if (info.errors) {
1103 slprintf(ebuf, sizeof(ebuf)-1, "ICU IDNA error codes: 0x%x", info.errors);
1104 error = ebuf;
1105 } else {
1106 error = "unknown error";
1107 }
1108 }
1109 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (ICU IDNA2008); %s", error);
1110
1111 uidna_close(uidna);
1112 return FAILURE;
1113 }
1114 #endif
1115
1116 #if PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2
1117 # if __GNUC__
1118 __attribute__ ((unused))
1119 # endif
1120 static ZEND_RESULT_CODE parse_kidn(struct parse_state *state, size_t prev_len)
1121 {
1122 idn_result_t rc;
1123 #if PHP_HTTP_HAVE_LIBIDNKIT
1124 int actions = IDN_DELIMMAP|IDN_LOCALMAP|IDN_NAMEPREP|IDN_IDNCONV|IDN_LENCHECK;
1125 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1126 int actions = IDN_MAP|IDN_ASCLOWER|IDN_RTCONV|IDN_PROHCHECK|IDN_NFCCHECK|IDN_PREFCHECK|IDN_COMBCHECK|IDN_CTXOLITECHECK|IDN_BIDICHECK|IDN_LOCALCHECK|IDN_IDNCONV|IDN_LENCHECK|IDN_RTCHECK;
1127 #endif
1128 char ahost_str[256] = {0};
1129
1130 if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1131 #if PHP_HTTP_HAVE_LIBIDNKIT
1132 actions |= IDN_LOCALCONV;
1133 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1134 actions |= IDN_UNICODECONV;
1135 #endif
1136 }
1137
1138 rc = idn_encodename(actions, state->url.host, ahost_str, 256);
1139 if (rc == idn_success) {
1140 size_t ahost_len = strlen(ahost_str);
1141
1142 memcpy(state->url.host, ahost_str, ahost_len + 1);
1143 state->offset += ahost_len - prev_len;
1144
1145 return SUCCESS;
1146 } else {
1147 php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idn_result_tostring(rc));
1148 return FAILURE;
1149 }
1150 }
1151 #endif
1152
1153 #if 0 && PHP_WIN32
1154 static ZEND_RESULT_CODE parse_widn_2003(struct parse_state *state, size_t prev_len)
1155 {
1156 char *host_ptr;
1157 uint16_t *uhost_str, ahost_str[256];
1158 size_t uhost_len, ahost_len;
1159
1160 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
1161 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
1162 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1163 return FAILURE;
1164 }
1165 #if PHP_HTTP_HAVE_WCHAR
1166 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1167 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
1168 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1169 return FAILURE;
1170 }
1171 #endif
1172 } else {
1173 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1174 return FAILURE;
1175 }
1176
1177 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED, uhost_str, uhost_len, ahost_str, 256)) {
1178 efree(uhost_str);
1179 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1180 return FAILURE;
1181 }
1182
1183 efree(uhost_str);
1184 ahost_len = wcslen(ahost_str);
1185 state->url.host[ahost_len] = '\0';
1186 state->offset += ahost_len - prev_len;
1187 while (ahost_len--) {
1188 state->url.host[ahost_len] = ahost_str[ahost_len];
1189 }
1190
1191 return SUCCESS;
1192 }
1193 #endif
1194
1195 static ZEND_RESULT_CODE parse_idna(struct parse_state *state, size_t len)
1196 {
1197 #if PHP_HTTP_HAVE_IDNA2008
1198 if ((state->flags & PHP_HTTP_URL_PARSE_TOIDN_2008) == PHP_HTTP_URL_PARSE_TOIDN_2008
1199 # if PHP_HTTP_HAVE_IDNA2003
1200 || (state->flags & PHP_HTTP_URL_PARSE_TOIDN_2003) != PHP_HTTP_URL_PARSE_TOIDN_2003
1201 # endif
1202 ) {
1203 #if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
1204 return parse_uidn_2008(state, len);
1205 #elif PHP_HTTP_HAVE_LIBIDN2
1206 return parse_gidn_2008(state, len);
1207 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1208 return parse_kidn(state, len);
1209 #endif
1210 }
1211 #endif
1212
1213 #if PHP_HTTP_HAVE_IDNA2003
1214 if ((state->flags & PHP_HTTP_URL_PARSE_TOIDN_2003) == PHP_HTTP_URL_PARSE_TOIDN_2003
1215 # if PHP_HTTP_HAVE_IDNA2008
1216 || (state->flags & PHP_HTTP_URL_PARSE_TOIDN_2008) != PHP_HTTP_URL_PARSE_TOIDN_2008
1217 #endif
1218 ) {
1219 #if HAVE_UIDNA_IDNTOASCII
1220 return parse_uidn_2003(state, len);
1221 #elif PHP_HTTP_HAVE_LIBIDN
1222 return parse_gidn_2003(state, len);
1223 #elif PHP_HTTP_HAVE_LIBIDNKIT
1224 return parse_kidn(state, len);
1225 #endif
1226 }
1227 #endif
1228
1229 #if 0 && PHP_WIN32
1230 return parse_widn_2003(state, len);
1231 #endif
1232
1233 #if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
1234 return parse_uidn_2008(state, len);
1235 #elif PHP_HTTP_HAVE_LIBIDN2
1236 return parse_gidn_2008(state, len);
1237 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1238 return parse_kidn(state, len);
1239 #elif HAVE_UIDNA_IDNTOASCII
1240 return parse_uidn_2003(state, len);
1241 #elif PHP_HTTP_HAVE_LIBIDN
1242 return parse_gidn_2003(state, len);
1243 #elif PHP_HTTP_HAVE_LIBIDNKIT
1244 return parse_kidn(state, len);
1245 #endif
1246
1247 return SUCCESS;
1248 }
1249
1250 #if HAVE_INET_PTON
1251 static const char *parse_ip6(struct parse_state *state, const char *ptr)
1252 {
1253 unsigned pos = 0;
1254 const char *error = NULL, *end = state->ptr, *tmp = memchr(ptr, ']', end - ptr);
1255
1256 if (tmp) {
1257 size_t addrlen = tmp - ptr + 1;
1258 char buf[16], *addr = estrndup(ptr + 1, addrlen - 2);
1259 int rv = inet_pton(AF_INET6, addr, buf);
1260
1261 if (rv == 1) {
1262 state->buffer[state->offset] = '[';
1263 state->url.host = &state->buffer[state->offset];
1264 inet_ntop(AF_INET6, buf, state->url.host + 1, state->maxlen - state->offset);
1265 state->offset += strlen(state->url.host);
1266 state->buffer[state->offset++] = ']';
1267 state->buffer[state->offset++] = 0;
1268 ptr = tmp + 1;
1269 } else if (rv == -1) {
1270 pos = 1;
1271 error = strerror(errno);
1272 } else {
1273 error = "unexpected '['";
1274 }
1275 efree(addr);
1276 } else {
1277 pos = end - ptr;
1278 error = "expected ']'";
1279 }
1280
1281 if (error) {
1282 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1283 php_error_docref(NULL, E_WARNING, "Failed to parse hostinfo; %s at pos %u in '%s'", error, pos, ptr);
1284 }
1285 return NULL;
1286 }
1287
1288 return ptr;
1289 }
1290 #endif
1291
1292 static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *ptr)
1293 {
1294 size_t mb, len = state->offset;
1295 const char *end = state->ptr, *tmp = ptr, *port = NULL, *label = NULL;
1296
1297 #if HAVE_INET_PTON
1298 if (*ptr == '[' && !(ptr = parse_ip6(state, ptr))) {
1299 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1300 return FAILURE;
1301 }
1302 ptr = tmp;
1303 }
1304 #endif
1305
1306 if (ptr != end) do {
1307 switch (*ptr) {
1308 case ':':
1309 if (port) {
1310 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1311 php_error_docref(NULL, E_WARNING,
1312 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1313 (unsigned) (ptr - tmp), tmp);
1314 }
1315 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1316 return FAILURE;
1317 }
1318 }
1319 port = ptr + 1;
1320 break;
1321
1322 case '%':
1323 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
1324 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1325 php_error_docref(NULL, E_WARNING,
1326 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1327 (unsigned) (ptr - tmp), tmp);
1328 }
1329 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1330 return FAILURE;
1331 }
1332 state->buffer[state->offset++] = *ptr++;
1333 break;
1334 }
1335 state->buffer[state->offset++] = *ptr++;
1336 state->buffer[state->offset++] = *ptr++;
1337 state->buffer[state->offset++] = *ptr;
1338 break;
1339
1340 case '.':
1341 if (port || !label) {
1342 /* sort of a compromise, just ensure we don't end up
1343 * with a dot at the beginning or two consecutive dots
1344 */
1345 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1346 php_error_docref(NULL, E_WARNING,
1347 "Failed to parse %s; unexpected '%c' at pos %u in '%s'",
1348 port ? "port" : "host",
1349 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1350 }
1351 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1352 return FAILURE;
1353 }
1354 break;
1355 }
1356 state->buffer[state->offset++] = *ptr;
1357 label = NULL;
1358 break;
1359
1360 case '-':
1361 if (!label) {
1362 /* sort of a compromise, just ensure we don't end up
1363 * with a hyphen at the beginning
1364 */
1365 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1366 php_error_docref(NULL, E_WARNING,
1367 "Failed to parse %s; unexpected '%c' at pos %u in '%s'",
1368 port ? "port" : "host",
1369 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1370 }
1371 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1372 return FAILURE;
1373 }
1374 break;
1375 }
1376 /* no break */
1377 case '_': case '~': /* unreserved */
1378 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1379 case '+': case ',': case ';': case '=': /* sub-delims */
1380 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1381 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1382 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1383 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1384 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1385 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1386 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1387 case 'v': case 'w': case 'x': case 'y': case 'z':
1388 if (port) {
1389 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1390 php_error_docref(NULL, E_WARNING,
1391 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1392 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1393 }
1394 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1395 return FAILURE;
1396 }
1397 break;
1398 }
1399 /* no break */
1400 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1401 case '7': case '8': case '9':
1402 /* allowed */
1403 if (port) {
1404 state->url.port *= 10;
1405 state->url.port += *ptr - '0';
1406 } else {
1407 label = ptr;
1408 state->buffer[state->offset++] = *ptr;
1409 }
1410 break;
1411
1412 default:
1413 if (ptr == end) {
1414 break;
1415 } else if (port) {
1416 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1417 php_error_docref(NULL, E_WARNING,
1418 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1419 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1420 }
1421 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1422 return FAILURE;
1423 }
1424 break;
1425 } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, 0))) {
1426 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1427 return FAILURE;
1428 }
1429 break;
1430 }
1431 label = ptr;
1432 ptr += mb - 1;
1433 }
1434 } while (++ptr < end);
1435
1436 if (!state->url.host) {
1437 len = state->offset - len;
1438 state->url.host = &state->buffer[state->offset - len];
1439 state->buffer[state->offset++] = 0;
1440 }
1441
1442 if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
1443 return parse_idna(state, len);
1444 }
1445
1446 return SUCCESS;
1447 }
1448
1449 static const char *parse_authority(struct parse_state *state)
1450 {
1451 const char *tmp = state->ptr, *host = NULL;
1452
1453 do {
1454 switch (*state->ptr) {
1455 case '@':
1456 /* userinfo delimiter */
1457 if (host) {
1458 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1459 php_error_docref(NULL, E_WARNING,
1460 "Failed to parse userinfo; unexpected '@'");
1461 }
1462 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1463 return NULL;
1464 }
1465 break;
1466 }
1467 host = state->ptr + 1;
1468 if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) {
1469 return NULL;
1470 }
1471 tmp = state->ptr + 1;
1472 break;
1473
1474 case '/':
1475 case '?':
1476 case '#':
1477 case '\0':
1478 EOD:
1479 /* host delimiter */
1480 if (tmp != state->ptr && SUCCESS != parse_hostinfo(state, tmp)) {
1481 return NULL;
1482 }
1483 return state->ptr;
1484 }
1485 } while (++state->ptr <= state->end);
1486
1487 --state->ptr;
1488 goto EOD;
1489 }
1490
1491 static const char *parse_path(struct parse_state *state)
1492 {
1493 size_t mb;
1494 const char *tmp;
1495
1496 /* is there actually a path to parse? */
1497 if (!*state->ptr) {
1498 return state->ptr;
1499 }
1500 tmp = state->ptr;
1501 state->url.path = &state->buffer[state->offset];
1502
1503 do {
1504 switch (*state->ptr) {
1505 case '#':
1506 case '?':
1507 goto done;
1508
1509 case '%':
1510 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1511 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1512 php_error_docref(NULL, E_WARNING,
1513 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1514 (unsigned) (state->ptr - tmp), tmp);
1515 }
1516 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1517 return NULL;
1518 }
1519 state->buffer[state->offset++] = *state->ptr;
1520 break;
1521 }
1522 state->buffer[state->offset++] = *state->ptr++;
1523 state->buffer[state->offset++] = *state->ptr++;
1524 state->buffer[state->offset++] = *state->ptr;
1525 break;
1526
1527 case '/': /* yeah, well */
1528 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1529 case '+': case ',': case ';': case '=': /* sub-delims */
1530 case '-': case '.': case '_': case '~': /* unreserved */
1531 case ':': case '@': /* pchar */
1532 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1533 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1534 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1535 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1536 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1537 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1538 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1539 case 'v': case 'w': case 'x': case 'y': case 'z':
1540 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1541 case '7': case '8': case '9':
1542 /* allowed */
1543 state->buffer[state->offset++] = *state->ptr;
1544 break;
1545
1546 default:
1547 if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) {
1548 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1549 return NULL;
1550 }
1551 break;
1552 }
1553 state->ptr += mb - 1;
1554 }
1555 } while (++state->ptr < state->end);
1556
1557 done:
1558 /* did we have any path component ? */
1559 if (tmp != state->ptr) {
1560 state->buffer[state->offset++] = 0;
1561 } else {
1562 state->url.path = NULL;
1563 }
1564 return state->ptr;
1565 }
1566
1567 static const char *parse_query(struct parse_state *state)
1568 {
1569 size_t mb;
1570 const char *tmp = state->ptr + !!*state->ptr;
1571
1572 /* is there actually a query to parse? */
1573 if (*state->ptr != '?') {
1574 return state->ptr;
1575 }
1576
1577 /* skip initial '?' */
1578 tmp = ++state->ptr;
1579 state->url.query = &state->buffer[state->offset];
1580
1581 while (state->ptr < state->end) {
1582 switch (*state->ptr) {
1583 case '#':
1584 goto done;
1585
1586 case '%':
1587 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1588 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1589 php_error_docref(NULL, E_WARNING,
1590 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1591 (unsigned) (state->ptr - tmp), tmp);
1592 }
1593 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1594 return NULL;
1595 }
1596 /* fallthrough, pct-encode the percent sign */
1597 } else {
1598 state->buffer[state->offset++] = *state->ptr++;
1599 state->buffer[state->offset++] = *state->ptr++;
1600 state->buffer[state->offset++] = *state->ptr;
1601 break;
1602 }
1603 /* no break */
1604 case '{': case '}':
1605 case '<': case '>':
1606 case '[': case ']':
1607 case '|': case '\\': case '^': case '`': case '"': case ' ':
1608 /* RFC1738 unsafe */
1609 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1610 state->buffer[state->offset++] = '%';
1611 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1612 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1613 break;
1614 }
1615 /* no break */
1616
1617 case '?': case '/': /* yeah, well */
1618 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1619 case '+': case ',': case ';': case '=': /* sub-delims */
1620 case '-': case '.': case '_': case '~': /* unreserved */
1621 case ':': case '@': /* pchar */
1622 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1623 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1624 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1625 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1626 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1627 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1628 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1629 case 'v': case 'w': case 'x': case 'y': case 'z':
1630 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1631 case '7': case '8': case '9':
1632 /* allowed */
1633 state->buffer[state->offset++] = *state->ptr;
1634 break;
1635
1636 default:
1637 if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) {
1638 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1639 return NULL;
1640 }
1641 break;
1642 }
1643 state->ptr += mb - 1;
1644 }
1645
1646 ++state->ptr;
1647 }
1648
1649 done:
1650 state->buffer[state->offset++] = 0;
1651 return state->ptr;
1652 }
1653
1654 static const char *parse_fragment(struct parse_state *state)
1655 {
1656 size_t mb;
1657 const char *tmp;
1658
1659 /* is there actually a fragment to parse? */
1660 if (*state->ptr != '#') {
1661 return state->ptr;
1662 }
1663
1664 /* skip initial '#' */
1665 tmp = ++state->ptr;
1666 state->url.fragment = &state->buffer[state->offset];
1667
1668 do {
1669 switch (*state->ptr) {
1670 case '#':
1671 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1672 php_error_docref(NULL, E_WARNING,
1673 "Failed to parse fragment; invalid fragment identifier at pos %u in '%s'",
1674 (unsigned) (state->ptr - tmp), tmp);
1675 }
1676 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1677 return NULL;
1678 }
1679 state->buffer[state->offset++] = *state->ptr;
1680 break;
1681
1682 case '%':
1683 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1684 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1685 php_error_docref(NULL, E_WARNING,
1686 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1687 (unsigned) (state->ptr - tmp), tmp);
1688 }
1689 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1690 return NULL;
1691 }
1692 /* fallthrough */
1693 } else {
1694 state->buffer[state->offset++] = *state->ptr++;
1695 state->buffer[state->offset++] = *state->ptr++;
1696 state->buffer[state->offset++] = *state->ptr;
1697 break;
1698 }
1699 /* no break */
1700
1701 case '{': case '}':
1702 case '<': case '>':
1703 case '[': case ']':
1704 case '|': case '\\': case '^': case '`': case '"': case ' ':
1705 /* RFC1738 unsafe */
1706 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1707 state->buffer[state->offset++] = '%';
1708 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1709 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1710 break;
1711 }
1712 /* no break */
1713
1714 case '?': case '/':
1715 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1716 case '+': case ',': case ';': case '=': /* sub-delims */
1717 case '-': case '.': case '_': case '~': /* unreserved */
1718 case ':': case '@': /* pchar */
1719 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1720 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1721 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1722 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1723 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1724 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1725 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1726 case 'v': case 'w': case 'x': case 'y': case 'z':
1727 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1728 case '7': case '8': case '9':
1729 /* allowed */
1730 state->buffer[state->offset++] = *state->ptr;
1731 break;
1732
1733 default:
1734 if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) {
1735 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1736 return NULL;
1737 }
1738 break;
1739 }
1740 state->ptr += mb - 1;
1741 }
1742 } while (++state->ptr < state->end);
1743
1744 state->buffer[state->offset++] = 0;
1745 return state->ptr;
1746 }
1747
1748 static const char *parse_hier(struct parse_state *state)
1749 {
1750 if (*state->ptr == '/') {
1751 if (state->end - state->ptr > 1) {
1752 if (*(state->ptr + 1) == '/') {
1753 state->ptr += 2;
1754 if (!(state->ptr = parse_authority(state))) {
1755 return NULL;
1756 }
1757 }
1758 }
1759 }
1760 return parse_path(state);
1761 }
1762
1763 static const char *parse_scheme(struct parse_state *state)
1764 {
1765 size_t mb;
1766 const char *tmp = state->ptr;
1767
1768 do {
1769 switch (*state->ptr) {
1770 case ':':
1771 /* scheme delimiter */
1772 state->url.scheme = &state->buffer[0];
1773 state->buffer[state->offset++] = 0;
1774 return ++state->ptr;
1775
1776 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1777 case '7': case '8': case '9':
1778 case '+': case '-': case '.':
1779 if (state->ptr == tmp) {
1780 goto softfail;
1781 }
1782 /* no break */
1783 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1784 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1785 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1786 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1787 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1788 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1789 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1790 case 'v': case 'w': case 'x': case 'y': case 'z':
1791 /* scheme part */
1792 state->buffer[state->offset++] = *state->ptr;
1793 break;
1794
1795 default:
1796 if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
1797 goto softfail;
1798 }
1799 state->ptr += mb - 1;
1800 }
1801 } while (++state->ptr < state->end);
1802
1803 softfail:
1804 state->offset = 0;
1805 return state->ptr = tmp;
1806 }
1807
1808 php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags)
1809 {
1810 size_t maxlen = 3 * len + 8 /* null bytes for all components */;
1811 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1812
1813 state->end = str + len;
1814 state->ptr = str;
1815 state->flags = flags;
1816 state->maxlen = maxlen;
1817
1818 if (!parse_scheme(state)) {
1819 if (!(flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1820 php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
1821 }
1822 efree(state);
1823 return NULL;
1824 }
1825
1826 if (!parse_hier(state)) {
1827 efree(state);
1828 return NULL;
1829 }
1830
1831 if (!parse_query(state)) {
1832 if (!(flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1833 php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
1834 }
1835 efree(state);
1836 return NULL;
1837 }
1838
1839 if (!parse_fragment(state)) {
1840 if (!(flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1841 php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
1842 }
1843 efree(state);
1844 return NULL;
1845 }
1846
1847 return (php_http_url_t *) state;
1848 }
1849
1850 php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags)
1851 {
1852 size_t maxlen = 3 * len;
1853 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1854
1855 state->end = str + len;
1856 state->ptr = str;
1857 state->flags = flags;
1858 state->maxlen = maxlen;
1859
1860 if (!(state->ptr = parse_authority(state))) {
1861 efree(state);
1862 return NULL;
1863 }
1864
1865 if (state->ptr != state->end) {
1866 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1867 php_error_docref(NULL, E_WARNING,
1868 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1869 (unsigned) (state->ptr - str), str);
1870 }
1871 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1872 efree(state);
1873 return NULL;
1874 }
1875 }
1876
1877 return (php_http_url_t *) state;
1878 }
1879
1880 static zend_class_entry *php_http_url_class_entry;
1881 static zend_class_entry *php_http_env_url_class_entry;
1882
1883 zend_class_entry *php_http_url_get_class_entry(void)
1884 {
1885 return php_http_url_class_entry;
1886 }
1887
1888 zend_class_entry *php_http_get_env_url_class_entry(void)
1889 {
1890 return php_http_env_url_class_entry;
1891 }
1892
1893 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0)
1894 ZEND_ARG_INFO(0, old_url)
1895 ZEND_ARG_INFO(0, new_url)
1896 ZEND_ARG_INFO(0, flags)
1897 ZEND_END_ARG_INFO();
1898 PHP_METHOD(HttpUrl, __construct)
1899 {
1900 zval *new_url = NULL, *old_url = NULL;
1901 zend_long flags = 0;
1902 zend_error_handling zeh;
1903
1904 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return);
1905
1906 /* always set http\Url::FROM_ENV for instances of http\Env\Url */
1907 if (instanceof_function(Z_OBJCE_P(getThis()), php_http_env_url_class_entry)) {
1908 flags |= PHP_HTTP_URL_FROM_ENV;
1909 }
1910
1911 if (flags & (PHP_HTTP_URL_SILENT_ERRORS|PHP_HTTP_URL_IGNORE_ERRORS)) {
1912 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
1913 } else {
1914 zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
1915 }
1916 {
1917 php_http_url_t *res_purl, *new_purl = NULL, *old_purl = NULL;
1918
1919 if (new_url) {
1920 new_purl = php_http_url_from_zval(new_url, flags);
1921 if (!new_purl) {
1922 zend_restore_error_handling(&zeh);
1923 return;
1924 }
1925 }
1926 if (old_url) {
1927 old_purl = php_http_url_from_zval(old_url, flags);
1928 if (!old_purl) {
1929 if (new_purl) {
1930 php_http_url_free(&new_purl);
1931 }
1932 zend_restore_error_handling(&zeh);
1933 return;
1934 }
1935 }
1936
1937 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1938 php_http_url_to_struct(res_purl, getThis());
1939
1940 php_http_url_free(&res_purl);
1941 if (old_purl) {
1942 php_http_url_free(&old_purl);
1943 }
1944 if (new_purl) {
1945 php_http_url_free(&new_purl);
1946 }
1947 }
1948 zend_restore_error_handling(&zeh);
1949 }
1950
1951 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod, 0, 0, 1)
1952 ZEND_ARG_INFO(0, more_url_parts)
1953 ZEND_ARG_INFO(0, flags)
1954 ZEND_END_ARG_INFO();
1955 PHP_METHOD(HttpUrl, mod)
1956 {
1957 zval *new_url = NULL;
1958 zend_long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY | PHP_HTTP_URL_SANITIZE_PATH;
1959 zend_error_handling zeh;
1960
1961 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url, &flags), invalid_arg, return);
1962
1963 if (flags & (PHP_HTTP_URL_SILENT_ERRORS|PHP_HTTP_URL_IGNORE_ERRORS)) {
1964 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
1965 } else {
1966 zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
1967 }
1968 {
1969 php_http_url_t *new_purl = NULL, *old_purl = NULL;
1970
1971 if (new_url) {
1972 new_purl = php_http_url_from_zval(new_url, flags);
1973 if (!new_purl) {
1974 zend_restore_error_handling(&zeh);
1975 return;
1976 }
1977 }
1978
1979 if ((old_purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1980 php_http_url_t *res_purl;
1981
1982 ZVAL_OBJ(return_value, zend_objects_clone_obj(getThis()));
1983
1984 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1985 php_http_url_to_struct(res_purl, return_value);
1986
1987 php_http_url_free(&res_purl);
1988 php_http_url_free(&old_purl);
1989 }
1990 if (new_purl) {
1991 php_http_url_free(&new_purl);
1992 }
1993 }
1994 zend_restore_error_handling(&zeh);
1995 }
1996
1997 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString, 0, 0, 0)
1998 ZEND_END_ARG_INFO();
1999 PHP_METHOD(HttpUrl, toString)
2000 {
2001 if (SUCCESS == zend_parse_parameters_none()) {
2002 php_http_url_t *purl;
2003
2004 if ((purl = php_http_url_from_struct(HASH_OF(getThis())))) {
2005 char *str;
2006 size_t len;
2007
2008 php_http_url_to_string(purl, &str, &len, 0);
2009 php_http_url_free(&purl);
2010 RETURN_STR(php_http_cs2zs(str, len));
2011 }
2012 }
2013 RETURN_EMPTY_STRING();
2014 }
2015
2016 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray, 0, 0, 0)
2017 ZEND_END_ARG_INFO();
2018 PHP_METHOD(HttpUrl, toArray)
2019 {
2020 php_http_url_t *purl;
2021
2022 if (SUCCESS != zend_parse_parameters_none()) {
2023 return;
2024 }
2025
2026 /* strip any non-URL properties */
2027 purl = php_http_url_from_struct(HASH_OF(getThis()));
2028 php_http_url_to_struct(purl, return_value);
2029 php_http_url_free(&purl);
2030 }
2031
2032 static zend_function_entry php_http_url_methods[] = {
2033 PHP_ME(HttpUrl, __construct, ai_HttpUrl___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
2034 PHP_ME(HttpUrl, mod, ai_HttpUrl_mod, ZEND_ACC_PUBLIC)
2035 PHP_ME(HttpUrl, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
2036 ZEND_MALIAS(HttpUrl, __toString, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
2037 PHP_ME(HttpUrl, toArray, ai_HttpUrl_toArray, ZEND_ACC_PUBLIC)
2038 EMPTY_FUNCTION_ENTRY
2039 };
2040
2041 PHP_MINIT_FUNCTION(http_url)
2042 {
2043 zend_class_entry ce = {0};
2044
2045 INIT_NS_CLASS_ENTRY(ce, "http", "Url", php_http_url_methods);
2046 php_http_url_class_entry = zend_register_internal_class(&ce);
2047
2048 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC);
2049 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC);
2050 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC);
2051 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC);
2052 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC);
2053 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("path"), ZEND_ACC_PUBLIC);
2054 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC);
2055 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC);
2056
2057 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE);
2058 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH);
2059 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY);
2060 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER);
2061 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS);
2062 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH);
2063 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT);
2064 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH);
2065 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY);
2066 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT);
2067 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL);
2068 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV);
2069 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH);
2070
2071 #if PHP_HTTP_HAVE_WCHAR
2072 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC);
2073 #endif
2074 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8);
2075 #if PHP_HTTP_HAVE_LIBIDN2 || PHP_HTTP_HAVE_LIBIDN || PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2 || HAVE_UIDNA_IDNTOASCII || HAVE_UIDNA_NAMETOASCII_UTF8
2076 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN);
2077 # if PHP_HTTP_HAVE_IDNA2003
2078 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN_2003"), PHP_HTTP_URL_PARSE_TOIDN_2003);
2079 # endif
2080 # if PHP_HTTP_HAVE_IDNA2008
2081 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN_2008"), PHP_HTTP_URL_PARSE_TOIDN_2008);
2082 # endif
2083 #endif
2084 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT);
2085
2086 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Url", php_http_url_methods);
2087 php_http_env_url_class_entry = zend_register_internal_class_ex(&ce, php_http_url_class_entry);
2088
2089 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("IGNORE_ERRORS"), PHP_HTTP_URL_IGNORE_ERRORS);
2090 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SILENT_ERRORS"), PHP_HTTP_URL_SILENT_ERRORS);
2091
2092 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STDFLAGS"), PHP_HTTP_URL_STDFLAGS);
2093
2094 return SUCCESS;
2095 }
2096
2097
2098 /*
2099 * Local variables:
2100 * tab-width: 4
2101 * c-basic-offset: 4
2102 * End:
2103 * vim600: noet sw=4 ts=4 fdm=marker
2104 * vim<600: noet sw=4 ts=4
2105 */
2106