2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #if PHP_HTTP_HAVE_IDN2
17 #elif PHP_HTTP_HAVE_IDN
21 #ifdef PHP_HTTP_HAVE_WCHAR
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
30 #include "php_http_utf8.h"
32 static inline char *localhostname(void)
34 char hostname
[1024] = {0};
37 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
38 return estrdup(hostname
);
40 #elif defined(HAVE_GETHOSTNAME)
41 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
42 # if defined(HAVE_GETDOMAINNAME)
43 size_t hlen
= strlen(hostname
);
44 if (hlen
<= lenof(hostname
) - lenof("(none)")) {
45 hostname
[hlen
++] = '.';
46 if (SUCCESS
== getdomainname(&hostname
[hlen
], lenof(hostname
) - hlen
)) {
47 if (!strcmp(&hostname
[hlen
], "(none)")) {
48 hostname
[hlen
- 1] = '\0';
50 return estrdup(hostname
);
54 if (strcmp(hostname
, "(none)")) {
55 return estrdup(hostname
);
59 return estrndup("localhost", lenof("localhost"));
62 #define url(buf) ((php_http_url_t *) (buf).data)
64 static php_http_url_t
*php_http_url_from_env(void)
66 zval
*https
, *zhost
, *zport
;
68 php_http_buffer_t buf
;
70 php_http_buffer_init_ex(&buf
, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE
, sizeof(php_http_url_t
)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC
);
71 php_http_buffer_account(&buf
, sizeof(php_http_url_t
));
72 memset(buf
.data
, 0, buf
.used
);
75 url(buf
)->scheme
= &buf
.data
[buf
.used
];
76 https
= php_http_env_get_server_var(ZEND_STRL("HTTPS"), 1);
77 if (https
&& !strcasecmp(Z_STRVAL_P(https
), "ON")) {
78 php_http_buffer_append(&buf
, "https", sizeof("https"));
80 php_http_buffer_append(&buf
, "http", sizeof("http"));
84 url(buf
)->host
= &buf
.data
[buf
.used
];
85 if ((((zhost
= php_http_env_get_server_var(ZEND_STRL("HTTP_HOST"), 1)) ||
86 (zhost
= php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1)) ||
87 (zhost
= php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1)))) && Z_STRLEN_P(zhost
)) {
88 size_t stop_at
= strspn(Z_STRVAL_P(zhost
), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.");
90 php_http_buffer_append(&buf
, Z_STRVAL_P(zhost
), stop_at
);
91 php_http_buffer_append(&buf
, "", 1);
93 char *host_str
= localhostname();
95 php_http_buffer_append(&buf
, host_str
, strlen(host_str
) + 1);
100 zport
= php_http_env_get_server_var(ZEND_STRL("SERVER_PORT"), 1);
101 if (zport
&& IS_LONG
== is_numeric_string(Z_STRVAL_P(zport
), Z_STRLEN_P(zport
), &port
, NULL
, 0)) {
102 url(buf
)->port
= port
;
106 if (SG(request_info
).request_uri
&& SG(request_info
).request_uri
[0]) {
107 const char *q
= strchr(SG(request_info
).request_uri
, '?');
109 url(buf
)->path
= &buf
.data
[buf
.used
];
112 php_http_buffer_append(&buf
, SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
113 php_http_buffer_append(&buf
, "", 1);
115 php_http_buffer_append(&buf
, SG(request_info
).request_uri
, strlen(SG(request_info
).request_uri
) + 1);
120 if (SG(request_info
).query_string
&& SG(request_info
).query_string
[0]) {
121 url(buf
)->query
= &buf
.data
[buf
.used
];
122 php_http_buffer_append(&buf
, SG(request_info
).query_string
, strlen(SG(request_info
).query_string
) + 1);
128 #define url_isset(u,n) \
130 #define url_append(buf, append) do { \
131 char *_ptr = (buf)->data; \
132 php_http_url_t *_url = (php_http_url_t *) _ptr, _mem = *_url; \
135 if (_ptr != (buf)->data) { \
136 ptrdiff_t diff = (buf)->data - _ptr; \
137 _url = (php_http_url_t *) (buf)->data; \
138 if (_mem.scheme) _url->scheme += diff; \
139 if (_mem.user) _url->user += diff; \
140 if (_mem.pass) _url->pass += diff; \
141 if (_mem.host) _url->host += diff; \
142 if (_mem.path) _url->path += diff; \
143 if (_mem.query) _url->query += diff; \
144 if (_mem.fragment) _url->fragment += diff; \
147 #define url_copy(n) do { \
148 if (url_isset(new_url, n)) { \
149 url(buf)->n = &buf.data[buf.used]; \
150 url_append(&buf, php_http_buffer_append(&buf, new_url->n, strlen(new_url->n) + 1)); \
151 } else if (url_isset(old_url, n)) { \
152 url(buf)->n = &buf.data[buf.used]; \
153 url_append(&buf, php_http_buffer_append(&buf, old_url->n, strlen(old_url->n) + 1)); \
157 php_http_url_t
*php_http_url_mod(const php_http_url_t
*old_url
, const php_http_url_t
*new_url
, unsigned flags
)
159 php_http_url_t
*tmp_url
= NULL
;
160 php_http_buffer_t buf
;
162 php_http_buffer_init_ex(&buf
, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE
, sizeof(php_http_url_t
)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC
);
163 php_http_buffer_account(&buf
, sizeof(php_http_url_t
));
164 memset(buf
.data
, 0, buf
.used
);
166 /* set from env if requested */
167 if (flags
& PHP_HTTP_URL_FROM_ENV
) {
168 php_http_url_t
*env_url
= php_http_url_from_env();
170 old_url
= tmp_url
= php_http_url_mod(env_url
, old_url
, flags
^ PHP_HTTP_URL_FROM_ENV
);
171 php_http_url_free(&env_url
);
176 if (!(flags
& PHP_HTTP_URL_STRIP_USER
)) {
180 if (!(flags
& PHP_HTTP_URL_STRIP_PASS
)) {
186 if (!(flags
& PHP_HTTP_URL_STRIP_PORT
)) {
187 url(buf
)->port
= url_isset(new_url
, port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
190 if (!(flags
& PHP_HTTP_URL_STRIP_PATH
)) {
191 if ((flags
& PHP_HTTP_URL_JOIN_PATH
) && url_isset(old_url
, path
) && url_isset(new_url
, path
) && *new_url
->path
!= '/') {
192 size_t old_path_len
= strlen(old_url
->path
), new_path_len
= strlen(new_url
->path
);
193 char *path
= ecalloc(1, old_path_len
+ new_path_len
+ 1 + 1);
195 strcat(path
, old_url
->path
);
196 if (path
[old_path_len
- 1] != '/') {
197 php_dirname(path
, old_path_len
);
200 strcat(path
, new_url
->path
);
202 url(buf
)->path
= &buf
.data
[buf
.used
];
203 if (path
[0] != '/') {
204 url_append(&buf
, php_http_buffer_append(&buf
, "/", 1));
206 url_append(&buf
, php_http_buffer_append(&buf
, path
, strlen(path
) + 1));
209 const char *path
= NULL
;
211 if (url_isset(new_url
, path
)) {
212 path
= new_url
->path
;
213 } else if (url_isset(old_url
, path
)) {
214 path
= old_url
->path
;
218 url(buf
)->path
= &buf
.data
[buf
.used
];
220 url_append(&buf
, php_http_buffer_append(&buf
, path
, strlen(path
) + 1));
227 if (!(flags
& PHP_HTTP_URL_STRIP_QUERY
)) {
228 if ((flags
& PHP_HTTP_URL_JOIN_QUERY
) && url_isset(new_url
, query
) && url_isset(old_url
, query
)) {
233 ZVAL_STRING(&qstr
, old_url
->query
);
234 php_http_querystring_update(&qarr
, &qstr
, NULL
);
235 zval_ptr_dtor(&qstr
);
236 ZVAL_STRING(&qstr
, new_url
->query
);
237 php_http_querystring_update(&qarr
, &qstr
, NULL
);
238 zval_ptr_dtor(&qstr
);
241 php_http_querystring_update(&qarr
, NULL
, &qstr
);
243 url(buf
)->query
= &buf
.data
[buf
.used
];
244 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL(qstr
), Z_STRLEN(qstr
) + 1));
253 if (!(flags
& PHP_HTTP_URL_STRIP_FRAGMENT
)) {
257 /* done with copy & combine & strip */
259 if (flags
& PHP_HTTP_URL_FROM_ENV
) {
260 /* free old_url we tainted above */
261 php_http_url_free(&tmp_url
);
264 /* replace directory references if path is not a single slash */
265 if ((flags
& PHP_HTTP_URL_SANITIZE_PATH
)
266 && url(buf
)->path
[0] && url(buf
)->path
[1]) {
267 char *ptr
, *end
= url(buf
)->path
+ strlen(url(buf
)->path
) + 1;
269 for (ptr
= strchr(url(buf
)->path
, '/'); ptr
; ptr
= strchr(ptr
, '/')) {
272 memmove(&ptr
[1], &ptr
[2], end
- &ptr
[2]);
282 memmove(&ptr
[1], &ptr
[3], end
- &ptr
[3]);
288 while (ptr
!= url(buf
)->path
) {
293 memmove(&ptr
[1], pos
, end
- pos
);
295 } else if (!ptr
[3]) {
314 /* unset default ports */
315 if (url(buf
)->port
) {
316 if ( ((url(buf
)->port
== 80) && url(buf
)->scheme
&& !strcmp(url(buf
)->scheme
, "http"))
317 || ((url(buf
)->port
==443) && url(buf
)->scheme
&& !strcmp(url(buf
)->scheme
, "https"))
326 char *php_http_url_to_string(const php_http_url_t
*url
, char **url_str
, size_t *url_len
, zend_bool persistent
)
328 php_http_buffer_t buf
;
330 php_http_buffer_init_ex(&buf
, PHP_HTTP_BUFFER_DEFAULT_SIZE
, persistent
?
331 PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0);
333 if (url
->scheme
&& *url
->scheme
) {
334 php_http_buffer_appendl(&buf
, url
->scheme
);
335 php_http_buffer_appends(&buf
, "://");
336 } else if ((url
->user
&& *url
->user
) || (url
->host
&& *url
->host
)) {
337 php_http_buffer_appends(&buf
, "//");
340 if (url
->user
&& *url
->user
) {
341 php_http_buffer_appendl(&buf
, url
->user
);
342 if (url
->pass
&& *url
->pass
) {
343 php_http_buffer_appends(&buf
, ":");
344 php_http_buffer_appendl(&buf
, url
->pass
);
346 php_http_buffer_appends(&buf
, "@");
349 if (url
->host
&& *url
->host
) {
350 php_http_buffer_appendl(&buf
, url
->host
);
352 php_http_buffer_appendf(&buf
, ":%hu", url
->port
);
356 if (url
->path
&& *url
->path
) {
357 if (*url
->path
!= '/') {
358 php_http_buffer_appends(&buf
, "/");
360 php_http_buffer_appendl(&buf
, url
->path
);
361 } else if (buf
.used
) {
362 php_http_buffer_appends(&buf
, "/");
365 if (url
->query
&& *url
->query
) {
366 php_http_buffer_appends(&buf
, "?");
367 php_http_buffer_appendl(&buf
, url
->query
);
370 if (url
->fragment
&& *url
->fragment
) {
371 php_http_buffer_appends(&buf
, "#");
372 php_http_buffer_appendl(&buf
, url
->fragment
);
375 php_http_buffer_shrink(&buf
);
376 php_http_buffer_fix(&buf
);
389 char *php_http_url_authority_to_string(const php_http_url_t
*url
, char **url_str
, size_t *url_len
)
391 php_http_buffer_t buf
;
393 php_http_buffer_init(&buf
);
395 if (url
->user
&& *url
->user
) {
396 php_http_buffer_appendl(&buf
, url
->user
);
397 if (url
->pass
&& *url
->pass
) {
398 php_http_buffer_appends(&buf
, ":");
399 php_http_buffer_appendl(&buf
, url
->pass
);
401 php_http_buffer_appends(&buf
, "@");
404 if (url
->host
&& *url
->host
) {
405 php_http_buffer_appendl(&buf
, url
->host
);
407 php_http_buffer_appendf(&buf
, ":%hu", url
->port
);
411 php_http_buffer_shrink(&buf
);
412 php_http_buffer_fix(&buf
);
425 php_http_url_t
*php_http_url_from_zval(zval
*value
, unsigned flags
)
428 php_http_url_t
*purl
;
430 switch (Z_TYPE_P(value
)) {
433 purl
= php_http_url_from_struct(HASH_OF(value
));
437 zs
= zval_get_string(value
);
438 purl
= php_http_url_parse(zs
->val
, zs
->len
, flags
);
439 zend_string_release(zs
);
445 php_http_url_t
*php_http_url_from_struct(HashTable
*ht
)
448 php_http_buffer_t buf
;
450 php_http_buffer_init_ex(&buf
, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE
, sizeof(php_http_url_t
)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC
);
451 php_http_buffer_account(&buf
, sizeof(php_http_url_t
));
452 memset(buf
.data
, 0, buf
.used
);
454 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("scheme")))) {
455 zend_string
*zs
= zval_get_string(e
);
456 url(buf
)->scheme
= &buf
.data
[buf
.used
];
457 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
458 zend_string_release(zs
);
460 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("user")))) {
461 zend_string
*zs
= zval_get_string(e
);
462 url(buf
)->user
= &buf
.data
[buf
.used
];
463 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
464 zend_string_release(zs
);
466 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("pass")))) {
467 zend_string
*zs
= zval_get_string(e
);
468 url(buf
)->pass
= &buf
.data
[buf
.used
];
469 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
470 zend_string_release(zs
);
472 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("host")))) {
473 zend_string
*zs
= zval_get_string(e
);
474 url(buf
)->host
= &buf
.data
[buf
.used
];
475 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
476 zend_string_release(zs
);
478 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("port")))) {
479 url(buf
)->port
= (unsigned short) zval_get_long(e
);
481 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("path")))) {
482 zend_string
*zs
= zval_get_string(e
);
483 url(buf
)->path
= &buf
.data
[buf
.used
];
484 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
485 zend_string_release(zs
);
487 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("query")))) {
488 zend_string
*zs
= zval_get_string(e
);
489 url(buf
)->query
= &buf
.data
[buf
.used
];
490 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
491 zend_string_release(zs
);
493 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("fragment")))) {
494 zend_string
*zs
= zval_get_string(e
);
495 url(buf
)->fragment
= &buf
.data
[buf
.used
];
496 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
497 zend_string_release(zs
);
503 HashTable
*php_http_url_to_struct(const php_http_url_t
*url
, zval
*strct
)
509 switch (Z_TYPE_P(strct
)) {
521 zend_hash_init(ht
, 8, NULL
, ZVAL_PTR_DTOR
, 0);
524 #define url_struct_add(part) \
525 if (Z_TYPE_P(strct) == IS_ARRAY) { \
526 zend_hash_str_update(Z_ARRVAL_P(strct), part, lenof(part), &tmp); \
528 zend_update_property(Z_OBJCE_P(strct), strct, part, lenof(part), &tmp); \
529 zval_ptr_dtor(&tmp); \
534 ZVAL_STRING(&tmp
, url
->scheme
);
535 url_struct_add("scheme");
538 ZVAL_STRING(&tmp
, url
->user
);
539 url_struct_add("user");
542 ZVAL_STRING(&tmp
, url
->pass
);
543 url_struct_add("pass");
546 ZVAL_STRING(&tmp
, url
->host
);
547 url_struct_add("host");
550 ZVAL_LONG(&tmp
, url
->port
);
551 url_struct_add("port");
554 ZVAL_STRING(&tmp
, url
->path
);
555 url_struct_add("path");
558 ZVAL_STRING(&tmp
, url
->query
);
559 url_struct_add("query");
562 ZVAL_STRING(&tmp
, url
->fragment
);
563 url_struct_add("fragment");
570 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
)
572 const char *arg_sep_str
= "&";
573 size_t arg_sep_len
= 1;
574 php_http_buffer_t
*qstr
= php_http_buffer_new();
576 php_http_url_argsep(&arg_sep_str
, &arg_sep_len
);
578 if (SUCCESS
!= php_http_url_encode_hash_ex(hash
, qstr
, arg_sep_str
, arg_sep_len
, "=", 1, pre_encoded_str
, pre_encoded_len
)) {
579 php_http_buffer_free(&qstr
);
583 php_http_buffer_data(qstr
, encoded_str
, encoded_len
);
584 php_http_buffer_free(&qstr
);
589 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
)
591 if (pre_encoded_len
&& pre_encoded_str
) {
592 php_http_buffer_append(qstr
, pre_encoded_str
, pre_encoded_len
);
595 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
)) {
609 char buffer
[1]; /* last member */
612 void php_http_url_free(php_http_url_t
**url
)
620 php_http_url_t
*php_http_url_copy(const php_http_url_t
*url
, zend_bool persistent
)
623 const char *end
= NULL
, *url_ptr
= (const char *) url
;
626 end
= MAX(url
->scheme
, end
);
627 end
= MAX(url
->pass
, end
);
628 end
= MAX(url
->user
, end
);
629 end
= MAX(url
->host
, end
);
630 end
= MAX(url
->path
, end
);
631 end
= MAX(url
->query
, end
);
632 end
= MAX(url
->fragment
, end
);
635 end
+= strlen(end
) + 1;
636 cpy_ptr
= pecalloc(1, end
- url_ptr
, persistent
);
637 cpy
= (php_http_url_t
*) cpy_ptr
;
639 memcpy(cpy_ptr
+ sizeof(*cpy
), url_ptr
+ sizeof(*url
), end
- url_ptr
- sizeof(*url
));
641 cpy
->scheme
= url
->scheme
? cpy_ptr
+ (url
->scheme
- url_ptr
) : NULL
;
642 cpy
->pass
= url
->pass
? cpy_ptr
+ (url
->pass
- url_ptr
) : NULL
;
643 cpy
->user
= url
->user
? cpy_ptr
+ (url
->user
- url_ptr
) : NULL
;
644 cpy
->host
= url
->host
? cpy_ptr
+ (url
->host
- url_ptr
) : NULL
;
645 cpy
->path
= url
->path
? cpy_ptr
+ (url
->path
- url_ptr
) : NULL
;
646 cpy
->query
= url
->query
? cpy_ptr
+ (url
->query
- url_ptr
) : NULL
;
647 cpy
->fragment
= url
->fragment
? cpy_ptr
+ (url
->fragment
- url_ptr
) : NULL
;
649 cpy
= ecalloc(1, sizeof(*url
));
652 cpy
->port
= url
->port
;
657 static size_t parse_mb_utf8(unsigned *wc
, const char *ptr
, const char *end
)
660 size_t consumed
= utf8towc(&wchar
, (const unsigned char *) ptr
, end
- ptr
);
662 if (!consumed
|| consumed
== (size_t) -1) {
672 #ifdef PHP_HTTP_HAVE_WCHAR
673 static size_t parse_mb_loc(unsigned *wc
, const char *ptr
, const char *end
)
677 #if defined(HAVE_MBRTOWC)
680 memset(&ps
, 0, sizeof(ps
));
681 consumed
= mbrtowc(&wchar
, ptr
, end
- ptr
, &ps
);
682 #elif defined(HAVE_MBTOWC)
683 consumed
= mbtowc(&wchar
, ptr
, end
- ptr
);
686 if (!consumed
|| consumed
== (size_t) -1) {
697 typedef enum parse_mb_what
{
706 static const char * const parse_what
[] = {
715 static const char parse_xdigits
[] = "0123456789ABCDEF";
717 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
)
722 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
723 consumed
= parse_mb_utf8(&wchar
, ptr
, end
);
725 #ifdef PHP_HTTP_HAVE_WCHAR
726 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
727 consumed
= parse_mb_loc(&wchar
, ptr
, end
);
732 if (!(state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) || what
== PARSE_HOSTINFO
|| what
== PARSE_SCHEME
) {
733 if (what
== PARSE_HOSTINFO
&& (state
->flags
& PHP_HTTP_URL_PARSE_TOIDN
)) {
735 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
736 if (!isualnum(wchar
)) {
739 #ifdef PHP_HTTP_HAVE_WCHAR
740 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
741 if (!iswalnum(wchar
)) {
746 PHP_HTTP_DUFF(consumed
, state
->buffer
[state
->offset
++] = *ptr
++);
750 PHP_HTTP_DUFF(consumed
,
751 state
->buffer
[state
->offset
++] = '%';
752 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) ptr
[i
]) >> 4];
753 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) ptr
[i
]) & 0xf];
763 php_error_docref(NULL
, E_WARNING
,
764 "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
765 parse_what
[what
], wchar
, (unsigned) (ptr
- begin
), begin
);
767 php_error_docref(NULL
, E_WARNING
,
768 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
769 parse_what
[what
], (unsigned char) *ptr
, (unsigned) (ptr
- begin
), begin
);
776 static ZEND_RESULT_CODE
parse_userinfo(struct parse_state
*state
, const char *ptr
)
779 const char *password
= NULL
, *end
= state
->ptr
, *tmp
= ptr
;
781 state
->url
.user
= &state
->buffer
[state
->offset
];
787 php_error_docref(NULL
, E_WARNING
,
788 "Failed to parse password; duplicate ':' at pos %u in '%s'",
789 (unsigned) (ptr
- tmp
), tmp
);
793 state
->buffer
[state
->offset
++] = 0;
794 state
->url
.pass
= &state
->buffer
[state
->offset
];
798 if (ptr
[1] != '%' && (end
- ptr
<= 2 || !isxdigit(*(ptr
+1)) || !isxdigit(*(ptr
+2)))) {
799 php_error_docref(NULL
, E_WARNING
,
800 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
801 (unsigned) (ptr
- tmp
), tmp
);
804 state
->buffer
[state
->offset
++] = *ptr
++;
805 state
->buffer
[state
->offset
++] = *ptr
++;
806 state
->buffer
[state
->offset
++] = *ptr
;
809 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
810 case '+': case ',': case ';': case '=': /* sub-delims */
811 case '-': case '.': case '_': case '~': /* unreserved */
812 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
813 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
814 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
815 case 'V': case 'W': case 'X': case 'Y': case 'Z':
816 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
817 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
818 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
819 case 'v': case 'w': case 'x': case 'y': case 'z':
820 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
821 case '7': case '8': case '9':
823 state
->buffer
[state
->offset
++] = *ptr
;
827 if (!(mb
= parse_mb(state
, PARSE_USERINFO
, ptr
, end
, tmp
, 0))) {
832 } while(++ptr
!= end
);
835 state
->buffer
[state
->offset
++] = 0;
840 #if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
841 typedef size_t (*parse_mb_func
)(unsigned *wc
, const char *ptr
, const char *end
);
842 static ZEND_RESULT_CODE
to_utf16(parse_mb_func fn
, const char *u8
, uint16_t **u16
, size_t *len
)
844 size_t offset
= 0, u8_len
= strlen(u8
);
846 *u16
= ecalloc(4 * sizeof(uint16_t), u8_len
+ 1);
849 while (offset
< u8_len
) {
851 uint16_t buf
[2], *ptr
= buf
;
852 size_t consumed
= fn(&wc
, &u8
[offset
], &u8
[u8_len
]);
856 php_error_docref(NULL
, E_WARNING
, "Failed to parse UTF-8 at pos %zu of '%s'", offset
, u8
);
862 switch (wctoutf16(buf
, wc
)) {
864 (*u16
)[(*len
)++] = *ptr
++;
867 (*u16
)[(*len
)++] = *ptr
++;
872 php_error_docref(NULL
, E_WARNING
, "Failed to convert UTF-32 'U+%X' to UTF-16", wc
);
881 #ifndef MAXHOSTNAMELEN
882 # define MAXHOSTNAMELEN 256
885 #if PHP_HTTP_HAVE_IDN2
886 static ZEND_RESULT_CODE
parse_idn2(struct parse_state
*state
, size_t prev_len
)
891 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
892 rv
= idn2_lookup_u8((const unsigned char *) state
->url
.host
, (unsigned char **) &idn
, IDN2_NFC_INPUT
);
894 # ifdef PHP_HTTP_HAVE_WCHAR
895 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
896 rv
= idn2_lookup_ul(state
->url
.host
, &idn
, 0);
900 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; %s", idn2_strerror(rv
));
903 size_t idnlen
= strlen(idn
);
904 memcpy(state
->url
.host
, idn
, idnlen
+ 1);
906 state
->offset
+= idnlen
- prev_len
;
910 #elif PHP_HTTP_HAVE_IDN
911 static ZEND_RESULT_CODE
parse_idn(struct parse_state
*state
, size_t prev_len
)
916 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
917 rv
= idna_to_ascii_8z(state
->url
.host
, &idn
, IDNA_ALLOW_UNASSIGNED
|IDNA_USE_STD3_ASCII_RULES
);
919 # ifdef PHP_HTTP_HAVE_WCHAR
920 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
921 rv
= idna_to_ascii_lz(state
->url
.host
, &idn
, IDNA_ALLOW_UNASSIGNED
|IDNA_USE_STD3_ASCII_RULES
);
924 if (rv
!= IDNA_SUCCESS
) {
925 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; %s", idna_strerror(rv
));
928 size_t idnlen
= strlen(idn
);
929 memcpy(state
->url
.host
, idn
, idnlen
+ 1);
931 state
->offset
+= idnlen
- prev_len
;
937 #ifdef HAVE_UIDNA_IDNTOASCII
938 # if HAVE_UNICODE_UIDNA_H
939 # include <unicode/uidna.h>
941 typedef uint16_t UChar
;
942 typedef enum { U_ZERO_ERROR
= 0 } UErrorCode
;
943 int32_t uidna_IDNToASCII(const UChar
*src
, int32_t srcLength
, UChar
*dest
, int32_t destCapacity
, int32_t options
, void *parseError
, UErrorCode
*status
);
945 static ZEND_RESULT_CODE
parse_uidn(struct parse_state
*state
)
948 uint16_t *uhost_str
, ahost_str
[MAXHOSTNAMELEN
], *ahost_ptr
;
949 size_t uhost_len
, ahost_len
;
950 UErrorCode error
= U_ZERO_ERROR
;
952 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
953 if (SUCCESS
!= to_utf16(parse_mb_utf8
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
956 #ifdef PHP_HTTP_HAVE_WCHAR
957 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
958 if (SUCCESS
!= to_utf16(parse_mb_loc
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
963 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; codepage not specified");
967 ahost_len
= uidna_IDNToASCII(uhost_str
, uhost_len
, ahost_str
, MAXHOSTNAMELEN
, 3, NULL
, &error
);
970 if (error
!= U_ZERO_ERROR
) {
971 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; ICU error %d", error
);
975 host_ptr
= state
->url
.host
;
976 ahost_ptr
= ahost_str
;
977 PHP_HTTP_DUFF(ahost_len
, *host_ptr
++ = *ahost_ptr
++);
980 state
->offset
+= host_ptr
- state
->url
.host
;
986 #if 0 && defined(PHP_WIN32)
987 static ZEND_RESULT_CODE
parse_widn(struct parse_state
*state
)
990 uint16_t *uhost_str
, ahost_str
[MAXHOSTNAMELEN
], *ahost_ptr
;
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 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
998 #ifdef 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 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
1006 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
1010 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED
|IDN_USE_STD3_ASCII_RULES
, uhost_str
, uhost_len
, ahost_str
, MAXHOSTNAMELEN
)) {
1012 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
1017 host_ptr
= state
->url
.host
;
1018 ahost_ptr
= ahost_str
;
1019 PHP_HTTP_DUFF(wcslen(ahost_str
), *host_ptr
++ = *ahost_ptr
++);
1023 state
->offset
+= host_ptr
- state
->url
.host
;
1029 #ifdef HAVE_INET_PTON
1030 static const char *parse_ip6(struct parse_state
*state
, const char *ptr
)
1032 const char *error
= NULL
, *end
= state
->ptr
, *tmp
= memchr(ptr
, ']', end
- ptr
);
1035 size_t addrlen
= tmp
- ptr
+ 1;
1036 char buf
[16], *addr
= estrndup(ptr
+ 1, addrlen
- 2);
1037 int rv
= inet_pton(AF_INET6
, addr
, buf
);
1040 state
->buffer
[state
->offset
] = '[';
1041 state
->url
.host
= &state
->buffer
[state
->offset
];
1042 inet_ntop(AF_INET6
, buf
, state
->url
.host
+ 1, state
->maxlen
- state
->offset
);
1043 state
->offset
+= strlen(state
->url
.host
);
1044 state
->buffer
[state
->offset
++] = ']';
1045 state
->buffer
[state
->offset
++] = 0;
1047 } else if (rv
== -1) {
1048 error
= strerror(errno
);
1050 error
= "unexpected '['";
1054 error
= "expected ']'";
1058 php_error_docref(NULL
, E_WARNING
, "Failed to parse hostinfo; %s", error
);
1066 static ZEND_RESULT_CODE
parse_hostinfo(struct parse_state
*state
, const char *ptr
)
1069 const char *end
= state
->ptr
, *tmp
= ptr
, *port
= NULL
, *label
= NULL
;
1071 #ifdef HAVE_INET_PTON
1072 if (*ptr
== '[' && !(ptr
= parse_ip6(state
, ptr
))) {
1077 if (ptr
!= end
) do {
1081 php_error_docref(NULL
, E_WARNING
,
1082 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1083 (unsigned) (ptr
- tmp
), tmp
);
1090 if (ptr
[1] != '%' && (end
- ptr
<= 2 || !isxdigit(*(ptr
+1)) || !isxdigit(*(ptr
+2)))) {
1091 php_error_docref(NULL
, E_WARNING
,
1092 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1093 (unsigned) (ptr
- tmp
), tmp
);
1096 state
->buffer
[state
->offset
++] = *ptr
++;
1097 state
->buffer
[state
->offset
++] = *ptr
++;
1098 state
->buffer
[state
->offset
++] = *ptr
;
1101 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1102 case '+': case ',': case ';': case '=': /* sub-delims */
1103 case '-': case '.': case '_': case '~': /* unreserved */
1104 if (port
|| !label
) {
1105 /* sort of a compromise, just ensure we don't end up
1106 * with a dot at the beginning or two consecutive dots
1108 php_error_docref(NULL
, E_WARNING
,
1109 "Failed to parse %s; unexpected '%c' at pos %u in '%s'",
1110 port
? "port" : "host",
1111 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1114 state
->buffer
[state
->offset
++] = *ptr
;
1118 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1119 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1120 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1121 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1122 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1123 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1124 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1125 case 'v': case 'w': case 'x': case 'y': case 'z':
1127 php_error_docref(NULL
, E_WARNING
,
1128 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1129 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1133 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1134 case '7': case '8': case '9':
1137 state
->url
.port
*= 10;
1138 state
->url
.port
+= *ptr
- '0';
1141 state
->buffer
[state
->offset
++] = *ptr
;
1149 php_error_docref(NULL
, E_WARNING
,
1150 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1151 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1153 } else if (!(mb
= parse_mb(state
, PARSE_HOSTINFO
, ptr
, end
, tmp
, 0))) {
1159 } while (++ptr
!= end
);
1161 if (!state
->url
.host
) {
1162 len
= (port
? port
- tmp
- 1 : end
- tmp
);
1163 state
->url
.host
= &state
->buffer
[state
->offset
- len
];
1164 state
->buffer
[state
->offset
++] = 0;
1167 if (state
->flags
& PHP_HTTP_URL_PARSE_TOIDN
) {
1168 #if PHP_HTTP_HAVE_IDN2
1169 return parse_idn2(state
, len
);
1170 #elif PHP_HTTP_HAVE_IDN
1171 return parse_idn(state
, len
);
1173 #ifdef HAVE_UIDNA_IDNTOASCII
1174 return parse_uidn(state
);
1176 #if 0 && defined(PHP_WIN32)
1177 return parse_widn(state
);
1184 static const char *parse_authority(struct parse_state
*state
)
1186 const char *tmp
= state
->ptr
, *host
= NULL
;
1189 switch (*state
->ptr
) {
1191 /* userinfo delimiter */
1193 php_error_docref(NULL
, E_WARNING
,
1194 "Failed to parse userinfo; unexpected '@'");
1197 host
= state
->ptr
+ 1;
1198 if (tmp
!= state
->ptr
&& SUCCESS
!= parse_userinfo(state
, tmp
)) {
1201 tmp
= state
->ptr
+ 1;
1209 /* host delimiter */
1210 if (tmp
!= state
->ptr
&& SUCCESS
!= parse_hostinfo(state
, tmp
)) {
1215 } while (++state
->ptr
<= state
->end
);
1221 static const char *parse_path(struct parse_state
*state
)
1226 /* is there actually a path to parse? */
1231 state
->url
.path
= &state
->buffer
[state
->offset
];
1234 switch (*state
->ptr
) {
1240 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1241 php_error_docref(NULL
, E_WARNING
,
1242 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1243 (unsigned) (state
->ptr
- tmp
), tmp
);
1246 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1247 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1248 state
->buffer
[state
->offset
++] = *state
->ptr
;
1251 case '/': /* yeah, well */
1252 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1253 case '+': case ',': case ';': case '=': /* sub-delims */
1254 case '-': case '.': case '_': case '~': /* unreserved */
1255 case ':': case '@': /* pchar */
1256 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1257 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1258 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1259 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1260 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1261 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1262 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1263 case 'v': case 'w': case 'x': case 'y': case 'z':
1264 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1265 case '7': case '8': case '9':
1267 state
->buffer
[state
->offset
++] = *state
->ptr
;
1271 if (!(mb
= parse_mb(state
, PARSE_PATH
, state
->ptr
, state
->end
, tmp
, 0))) {
1274 state
->ptr
+= mb
- 1;
1276 } while (++state
->ptr
< state
->end
);
1279 /* did we have any path component ? */
1280 if (tmp
!= state
->ptr
) {
1281 state
->buffer
[state
->offset
++] = 0;
1283 state
->url
.path
= NULL
;
1288 static const char *parse_query(struct parse_state
*state
)
1291 const char *tmp
= state
->ptr
+ !!*state
->ptr
;
1293 /* is there actually a query to parse? */
1294 if (*state
->ptr
!= '?') {
1298 /* skip initial '?' */
1300 state
->url
.query
= &state
->buffer
[state
->offset
];
1302 while (state
->ptr
< state
->end
) {
1303 switch (*state
->ptr
) {
1308 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1309 php_error_docref(NULL
, E_WARNING
,
1310 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1311 (unsigned) (state
->ptr
- tmp
), tmp
);
1314 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1315 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1316 state
->buffer
[state
->offset
++] = *state
->ptr
;
1319 /* RFC1738 unsafe */
1323 case '|': case '\\': case '^': case '`': case '"': case ' ':
1324 if (state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) {
1325 state
->buffer
[state
->offset
++] = '%';
1326 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) >> 4];
1327 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) & 0xf];
1332 case '?': case '/': /* yeah, well */
1333 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1334 case '+': case ',': case ';': case '=': /* sub-delims */
1335 case '-': case '.': case '_': case '~': /* unreserved */
1336 case ':': case '@': /* pchar */
1337 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1338 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1339 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1340 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1341 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1342 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1343 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1344 case 'v': case 'w': case 'x': case 'y': case 'z':
1345 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1346 case '7': case '8': case '9':
1348 state
->buffer
[state
->offset
++] = *state
->ptr
;
1352 if (!(mb
= parse_mb(state
, PARSE_QUERY
, state
->ptr
, state
->end
, tmp
, 0))) {
1355 state
->ptr
+= mb
- 1;
1362 state
->buffer
[state
->offset
++] = 0;
1366 static const char *parse_fragment(struct parse_state
*state
)
1371 /* is there actually a fragment to parse? */
1372 if (*state
->ptr
!= '#') {
1376 /* skip initial '#' */
1378 state
->url
.fragment
= &state
->buffer
[state
->offset
];
1381 switch (*state
->ptr
) {
1383 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1384 php_error_docref(NULL
, E_WARNING
,
1385 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1386 (unsigned) (state
->ptr
- tmp
), tmp
);
1389 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1390 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1391 state
->buffer
[state
->offset
++] = *state
->ptr
;
1394 /* RFC1738 unsafe */
1398 case '|': case '\\': case '^': case '`': case '"': case ' ':
1399 if (state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) {
1400 state
->buffer
[state
->offset
++] = '%';
1401 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) >> 4];
1402 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) & 0xf];
1408 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1409 case '+': case ',': case ';': case '=': /* sub-delims */
1410 case '-': case '.': case '_': case '~': /* unreserved */
1411 case ':': case '@': /* pchar */
1412 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1413 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1414 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1415 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1416 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1417 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1418 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1419 case 'v': case 'w': case 'x': case 'y': case 'z':
1420 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1421 case '7': case '8': case '9':
1423 state
->buffer
[state
->offset
++] = *state
->ptr
;
1427 if (!(mb
= parse_mb(state
, PARSE_FRAGMENT
, state
->ptr
, state
->end
, tmp
, 0))) {
1430 state
->ptr
+= mb
- 1;
1432 } while (++state
->ptr
< state
->end
);
1434 state
->buffer
[state
->offset
++] = 0;
1438 static const char *parse_hier(struct parse_state
*state
)
1440 if (*state
->ptr
== '/') {
1441 if (state
->end
- state
->ptr
> 1) {
1442 if (*(state
->ptr
+ 1) == '/') {
1444 if (!(state
->ptr
= parse_authority(state
))) {
1450 return parse_path(state
);
1453 static const char *parse_scheme(struct parse_state
*state
)
1456 const char *tmp
= state
->ptr
;
1459 switch (*state
->ptr
) {
1461 /* scheme delimiter */
1462 state
->url
.scheme
= &state
->buffer
[0];
1463 state
->buffer
[state
->offset
++] = 0;
1464 return ++state
->ptr
;
1466 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1467 case '7': case '8': case '9':
1468 case '+': case '-': case '.':
1469 if (state
->ptr
== tmp
) {
1473 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1474 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1475 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1476 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1477 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1478 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1479 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1480 case 'v': case 'w': case 'x': case 'y': case 'z':
1482 state
->buffer
[state
->offset
++] = *state
->ptr
;
1486 if (!(mb
= parse_mb(state
, PARSE_SCHEME
, state
->ptr
, state
->end
, tmp
, 1))) {
1487 /* soft fail; parse path next */
1490 state
->ptr
+= mb
- 1;
1492 } while (++state
->ptr
!= state
->end
);
1494 return state
->ptr
= tmp
;
1497 php_http_url_t
*php_http_url_parse(const char *str
, size_t len
, unsigned flags
)
1499 size_t maxlen
= 3 * len
;
1500 struct parse_state
*state
= ecalloc(1, sizeof(*state
) + maxlen
);
1502 state
->end
= str
+ len
;
1504 state
->flags
= flags
;
1505 state
->maxlen
= maxlen
;
1507 if (!parse_scheme(state
)) {
1508 php_error_docref(NULL
, E_WARNING
, "Failed to parse URL scheme: '%s'", state
->ptr
);
1513 if (!parse_hier(state
)) {
1518 if (!parse_query(state
)) {
1519 php_error_docref(NULL
, E_WARNING
, "Failed to parse URL query: '%s'", state
->ptr
);
1524 if (!parse_fragment(state
)) {
1525 php_error_docref(NULL
, E_WARNING
, "Failed to parse URL fragment: '%s'", state
->ptr
);
1530 return (php_http_url_t
*) state
;
1533 php_http_url_t
*php_http_url_parse_authority(const char *str
, size_t len
, unsigned flags
)
1535 size_t maxlen
= 3 * len
;
1536 struct parse_state
*state
= ecalloc(1, sizeof(*state
) + maxlen
);
1538 state
->end
= str
+ len
;
1540 state
->flags
= flags
;
1541 state
->maxlen
= maxlen
;
1543 if (!(state
->ptr
= parse_authority(state
))) {
1548 if (state
->ptr
!= state
->end
) {
1549 php_error_docref(NULL
, E_WARNING
,
1550 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1551 (unsigned) (state
->ptr
- str
), str
);
1556 return (php_http_url_t
*) state
;
1559 static zend_class_entry
*php_http_url_class_entry
;
1560 static zend_class_entry
*php_http_env_url_class_entry
;
1562 zend_class_entry
*php_http_url_get_class_entry(void)
1564 return php_http_url_class_entry
;
1567 zend_class_entry
*php_http_get_env_url_class_entry(void)
1569 return php_http_env_url_class_entry
;
1572 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct
, 0, 0, 0)
1573 ZEND_ARG_INFO(0, old_url
)
1574 ZEND_ARG_INFO(0, new_url
)
1575 ZEND_ARG_INFO(0, flags
)
1576 ZEND_END_ARG_INFO();
1577 PHP_METHOD(HttpUrl
, __construct
)
1579 zval
*new_url
= NULL
, *old_url
= NULL
;
1580 zend_long flags
= 0;
1581 zend_error_handling zeh
;
1583 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url
, &new_url
, &flags
), invalid_arg
, return);
1585 /* always set http\Url::FROM_ENV for instances of http\Env\Url */
1586 if (instanceof_function(Z_OBJCE_P(getThis()), php_http_env_url_class_entry
)) {
1587 flags
|= PHP_HTTP_URL_FROM_ENV
;
1590 zend_replace_error_handling(EH_THROW
, php_http_get_exception_bad_url_class_entry(), &zeh
);
1592 php_http_url_t
*res_purl
, *new_purl
= NULL
, *old_purl
= NULL
;
1595 new_purl
= php_http_url_from_zval(new_url
, flags
);
1597 zend_restore_error_handling(&zeh
);
1602 old_purl
= php_http_url_from_zval(old_url
, flags
);
1605 php_http_url_free(&new_purl
);
1607 zend_restore_error_handling(&zeh
);
1612 res_purl
= php_http_url_mod(old_purl
, new_purl
, flags
);
1613 php_http_url_to_struct(res_purl
, getThis());
1615 php_http_url_free(&res_purl
);
1617 php_http_url_free(&old_purl
);
1620 php_http_url_free(&new_purl
);
1623 zend_restore_error_handling(&zeh
);
1626 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod
, 0, 0, 1)
1627 ZEND_ARG_INFO(0, more_url_parts
)
1628 ZEND_ARG_INFO(0, flags
)
1629 ZEND_END_ARG_INFO();
1630 PHP_METHOD(HttpUrl
, mod
)
1632 zval
*new_url
= NULL
;
1633 zend_long flags
= PHP_HTTP_URL_JOIN_PATH
| PHP_HTTP_URL_JOIN_QUERY
| PHP_HTTP_URL_SANITIZE_PATH
;
1634 zend_error_handling zeh
;
1636 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url
, &flags
), invalid_arg
, return);
1638 zend_replace_error_handling(EH_THROW
, php_http_get_exception_bad_url_class_entry(), &zeh
);
1640 php_http_url_t
*new_purl
= NULL
, *old_purl
= NULL
;
1643 new_purl
= php_http_url_from_zval(new_url
, flags
);
1645 zend_restore_error_handling(&zeh
);
1650 if ((old_purl
= php_http_url_from_struct(HASH_OF(getThis())))) {
1651 php_http_url_t
*res_purl
;
1653 ZVAL_OBJ(return_value
, zend_objects_clone_obj(getThis()));
1655 res_purl
= php_http_url_mod(old_purl
, new_purl
, flags
);
1656 php_http_url_to_struct(res_purl
, return_value
);
1658 php_http_url_free(&res_purl
);
1659 php_http_url_free(&old_purl
);
1662 php_http_url_free(&new_purl
);
1665 zend_restore_error_handling(&zeh
);
1668 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString
, 0, 0, 0)
1669 ZEND_END_ARG_INFO();
1670 PHP_METHOD(HttpUrl
, toString
)
1672 if (SUCCESS
== zend_parse_parameters_none()) {
1673 php_http_url_t
*purl
;
1675 if ((purl
= php_http_url_from_struct(HASH_OF(getThis())))) {
1679 php_http_url_to_string(purl
, &str
, &len
, 0);
1680 php_http_url_free(&purl
);
1681 RETURN_STR(php_http_cs2zs(str
, len
));
1684 RETURN_EMPTY_STRING();
1687 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray
, 0, 0, 0)
1688 ZEND_END_ARG_INFO();
1689 PHP_METHOD(HttpUrl
, toArray
)
1691 php_http_url_t
*purl
;
1693 if (SUCCESS
!= zend_parse_parameters_none()) {
1697 /* strip any non-URL properties */
1698 purl
= php_http_url_from_struct(HASH_OF(getThis()));
1699 php_http_url_to_struct(purl
, return_value
);
1700 php_http_url_free(&purl
);
1703 static zend_function_entry php_http_url_methods
[] = {
1704 PHP_ME(HttpUrl
, __construct
, ai_HttpUrl___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1705 PHP_ME(HttpUrl
, mod
, ai_HttpUrl_mod
, ZEND_ACC_PUBLIC
)
1706 PHP_ME(HttpUrl
, toString
, ai_HttpUrl_toString
, ZEND_ACC_PUBLIC
)
1707 ZEND_MALIAS(HttpUrl
, __toString
, toString
, ai_HttpUrl_toString
, ZEND_ACC_PUBLIC
)
1708 PHP_ME(HttpUrl
, toArray
, ai_HttpUrl_toArray
, ZEND_ACC_PUBLIC
)
1709 EMPTY_FUNCTION_ENTRY
1712 PHP_MINIT_FUNCTION(http_url
)
1714 zend_class_entry ce
= {0};
1716 INIT_NS_CLASS_ENTRY(ce
, "http", "Url", php_http_url_methods
);
1717 php_http_url_class_entry
= zend_register_internal_class(&ce
);
1719 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC
);
1720 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
1721 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
1722 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
1723 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
1724 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("path"), ZEND_ACC_PUBLIC
);
1725 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("query"), ZEND_ACC_PUBLIC
);
1726 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC
);
1728 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE
);
1729 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH
);
1730 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY
);
1731 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER
);
1732 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS
);
1733 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH
);
1734 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT
);
1735 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH
);
1736 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY
);
1737 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT
);
1738 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL
);
1739 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV
);
1740 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH
);
1742 #ifdef PHP_HTTP_HAVE_WCHAR
1743 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC
);
1745 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8
);
1746 #if defined(PHP_HTTP_HAVE_IDN2) || defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
1747 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN
);
1749 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT
);
1751 INIT_NS_CLASS_ENTRY(ce
, "http\\Env", "Url", php_http_url_methods
);
1752 php_http_env_url_class_entry
= zend_register_internal_class_ex(&ce
, php_http_url_class_entry
);
1763 * vim600: noet sw=4 ts=4 fdm=marker
1764 * vim<600: noet sw=4 ts=4