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(TSRMLS_D
)
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 TSRMLS_CC
);
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 TSRMLS_CC
)) ||
86 (zhost
= php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1 TSRMLS_CC
)) ||
87 (zhost
= php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1 TSRMLS_CC
)))) && 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 TSRMLS_CC
);
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 TSRMLS_DC
)
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(TSRMLS_C
);
170 old_url
= tmp_url
= php_http_url_mod(env_url
, old_url
, flags
^ PHP_HTTP_URL_FROM_ENV TSRMLS_CC
);
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
)) {
235 ZVAL_STRING(&qstr
, old_url
->query
, 0);
236 php_http_querystring_update(&qarr
, &qstr
, NULL TSRMLS_CC
);
237 ZVAL_STRING(&qstr
, new_url
->query
, 0);
238 php_http_querystring_update(&qarr
, &qstr
, NULL TSRMLS_CC
);
241 php_http_querystring_update(&qarr
, NULL
, &qstr TSRMLS_CC
);
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 TSRMLS_DC
)
428 php_http_url_t
*purl
;
430 switch (Z_TYPE_P(value
)) {
433 purl
= php_http_url_from_struct(HASH_OF(value
));
437 zcpy
= php_http_ztyp(IS_STRING
, value
);
438 purl
= php_http_url_parse(Z_STRVAL_P(zcpy
), Z_STRLEN_P(zcpy
), flags TSRMLS_CC
);
439 zval_ptr_dtor(&zcpy
);
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 (SUCCESS
== zend_hash_find(ht
, "scheme", sizeof("scheme"), (void *) &e
)) {
455 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
456 url(buf
)->scheme
= &buf
.data
[buf
.used
];
457 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
460 if (SUCCESS
== zend_hash_find(ht
, "user", sizeof("user"), (void *) &e
)) {
461 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
462 url(buf
)->user
= &buf
.data
[buf
.used
];
463 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
466 if (SUCCESS
== zend_hash_find(ht
, "pass", sizeof("pass"), (void *) &e
)) {
467 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
468 url(buf
)->pass
= &buf
.data
[buf
.used
];
469 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
472 if (SUCCESS
== zend_hash_find(ht
, "host", sizeof("host"), (void *) &e
)) {
473 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
474 url(buf
)->host
= &buf
.data
[buf
.used
];
475 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
478 if (SUCCESS
== zend_hash_find(ht
, "port", sizeof("port"), (void *) &e
)) {
479 zval
*cpy
= php_http_ztyp(IS_LONG
, *e
);
480 url(buf
)->port
= (unsigned short) Z_LVAL_P(cpy
);
483 if (SUCCESS
== zend_hash_find(ht
, "path", sizeof("path"), (void *) &e
)) {
484 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
485 url(buf
)->path
= &buf
.data
[buf
.used
];
486 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
489 if (SUCCESS
== zend_hash_find(ht
, "query", sizeof("query"), (void *) &e
)) {
490 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
491 url(buf
)->query
= &buf
.data
[buf
.used
];
492 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
495 if (SUCCESS
== zend_hash_find(ht
, "fragment", sizeof("fragment"), (void *) &e
)) {
496 zval
*cpy
= php_http_ztyp(IS_STRING
, *e
);
497 url(buf
)->fragment
= &buf
.data
[buf
.used
];
498 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
) + 1));
505 HashTable
*php_http_url_to_struct(const php_http_url_t
*url
, zval
*strct TSRMLS_DC
)
510 switch (Z_TYPE_P(strct
)) {
517 INIT_PZVAL_ARRAY((&arr
), HASH_OF(strct
));
527 add_assoc_string(&arr
, "scheme", url
->scheme
, 1);
530 add_assoc_string(&arr
, "user", url
->user
, 1);
533 add_assoc_string(&arr
, "pass", url
->pass
, 1);
536 add_assoc_string(&arr
, "host", url
->host
, 1);
539 add_assoc_long(&arr
, "port", (long) url
->port
);
542 add_assoc_string(&arr
, "path", url
->path
, 1);
545 add_assoc_string(&arr
, "query", url
->query
, 1);
548 add_assoc_string(&arr
, "fragment", url
->fragment
, 1);
552 return Z_ARRVAL(arr
);
555 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 TSRMLS_DC
)
557 const char *arg_sep_str
;
559 php_http_buffer_t
*qstr
= php_http_buffer_new();
561 php_http_url_argsep(&arg_sep_str
, &arg_sep_len TSRMLS_CC
);
563 if (SUCCESS
!= php_http_url_encode_hash_ex(hash
, qstr
, arg_sep_str
, arg_sep_len
, "=", 1, pre_encoded_str
, pre_encoded_len TSRMLS_CC
)) {
564 php_http_buffer_free(&qstr
);
568 php_http_buffer_data(qstr
, encoded_str
, encoded_len
);
569 php_http_buffer_free(&qstr
);
574 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 TSRMLS_DC
)
576 if (pre_encoded_len
&& pre_encoded_str
) {
577 php_http_buffer_append(qstr
, pre_encoded_str
, pre_encoded_len
);
580 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 TSRMLS_CC
)) {
597 char buffer
[1]; /* last member */
600 void php_http_url_free(php_http_url_t
**url
)
608 php_http_url_t
*php_http_url_copy(const php_http_url_t
*url
, zend_bool persistent
)
611 const char *end
= NULL
, *url_ptr
= (const char *) url
;
614 end
= MAX(url
->scheme
, end
);
615 end
= MAX(url
->pass
, end
);
616 end
= MAX(url
->user
, end
);
617 end
= MAX(url
->host
, end
);
618 end
= MAX(url
->path
, end
);
619 end
= MAX(url
->query
, end
);
620 end
= MAX(url
->fragment
, end
);
623 end
+= strlen(end
) + 1;
624 cpy_ptr
= pecalloc(1, end
- url_ptr
, persistent
);
625 cpy
= (php_http_url_t
*) cpy_ptr
;
627 memcpy(cpy_ptr
+ sizeof(*cpy
), url_ptr
+ sizeof(*url
), end
- url_ptr
- sizeof(*url
));
629 cpy
->scheme
= url
->scheme
? cpy_ptr
+ (url
->scheme
- url_ptr
) : NULL
;
630 cpy
->pass
= url
->pass
? cpy_ptr
+ (url
->pass
- url_ptr
) : NULL
;
631 cpy
->user
= url
->user
? cpy_ptr
+ (url
->user
- url_ptr
) : NULL
;
632 cpy
->host
= url
->host
? cpy_ptr
+ (url
->host
- url_ptr
) : NULL
;
633 cpy
->path
= url
->path
? cpy_ptr
+ (url
->path
- url_ptr
) : NULL
;
634 cpy
->query
= url
->query
? cpy_ptr
+ (url
->query
- url_ptr
) : NULL
;
635 cpy
->fragment
= url
->fragment
? cpy_ptr
+ (url
->fragment
- url_ptr
) : NULL
;
637 cpy
= ecalloc(1, sizeof(*url
));
640 cpy
->port
= url
->port
;
645 static size_t parse_mb_utf8(unsigned *wc
, const char *ptr
, const char *end
)
648 size_t consumed
= utf8towc(&wchar
, (const unsigned char *) ptr
, end
- ptr
);
650 if (!consumed
|| consumed
== (size_t) -1) {
660 #ifdef PHP_HTTP_HAVE_WCHAR
661 static size_t parse_mb_loc(unsigned *wc
, const char *ptr
, const char *end
)
665 #if defined(HAVE_MBRTOWC)
668 memset(&ps
, 0, sizeof(ps
));
669 consumed
= mbrtowc(&wchar
, ptr
, end
- ptr
, &ps
);
670 #elif defined(HAVE_MBTOWC)
671 consumed
= mbtowc(&wchar
, ptr
, end
- ptr
);
674 if (!consumed
|| consumed
== (size_t) -1) {
685 typedef enum parse_mb_what
{
694 static const char * const parse_what
[] = {
703 static const char parse_xdigits
[] = "0123456789ABCDEF";
705 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
)
710 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
711 consumed
= parse_mb_utf8(&wchar
, ptr
, end
);
713 #ifdef PHP_HTTP_HAVE_WCHAR
714 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
715 consumed
= parse_mb_loc(&wchar
, ptr
, end
);
720 if (!(state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) || what
== PARSE_HOSTINFO
|| what
== PARSE_SCHEME
) {
721 if (what
== PARSE_HOSTINFO
&& (state
->flags
& PHP_HTTP_URL_PARSE_TOIDN
)) {
723 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
724 if (!isualnum(wchar
)) {
727 #ifdef PHP_HTTP_HAVE_WCHAR
728 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
729 if (!iswalnum(wchar
)) {
734 PHP_HTTP_DUFF(consumed
, state
->buffer
[state
->offset
++] = *ptr
++);
738 PHP_HTTP_DUFF(consumed
,
739 state
->buffer
[state
->offset
++] = '%';
740 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) ptr
[i
]) >> 4];
741 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) ptr
[i
]) & 0xf];
750 TSRMLS_FETCH_FROM_CTX(state
->ts
);
752 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
753 "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
754 parse_what
[what
], wchar
, (unsigned) (ptr
- begin
), begin
);
756 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
757 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
758 parse_what
[what
], (unsigned char) *ptr
, (unsigned) (ptr
- begin
), begin
);
765 static ZEND_RESULT_CODE
parse_userinfo(struct parse_state
*state
, const char *ptr
)
768 const char *password
= NULL
, *end
= state
->ptr
, *tmp
= ptr
;
769 TSRMLS_FETCH_FROM_CTX(state
->ts
);
771 state
->url
.user
= &state
->buffer
[state
->offset
];
777 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
778 "Failed to parse password; duplicate ':' at pos %u in '%s'",
779 (unsigned) (ptr
- tmp
), tmp
);
783 state
->buffer
[state
->offset
++] = 0;
784 state
->url
.pass
= &state
->buffer
[state
->offset
];
788 if (ptr
[1] != '%' && (end
- ptr
<= 2 || !isxdigit(*(ptr
+1)) || !isxdigit(*(ptr
+2)))) {
789 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
790 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
791 (unsigned) (ptr
- tmp
), tmp
);
794 state
->buffer
[state
->offset
++] = *ptr
++;
795 state
->buffer
[state
->offset
++] = *ptr
++;
796 state
->buffer
[state
->offset
++] = *ptr
;
799 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
800 case '+': case ',': case ';': case '=': /* sub-delims */
801 case '-': case '.': case '_': case '~': /* unreserved */
802 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
803 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
804 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
805 case 'V': case 'W': case 'X': case 'Y': case 'Z':
806 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
807 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
808 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
809 case 'v': case 'w': case 'x': case 'y': case 'z':
810 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
811 case '7': case '8': case '9':
813 state
->buffer
[state
->offset
++] = *ptr
;
817 if (!(mb
= parse_mb(state
, PARSE_USERINFO
, ptr
, end
, tmp
, 0))) {
822 } while(++ptr
!= end
);
825 state
->buffer
[state
->offset
++] = 0;
830 #if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
831 typedef size_t (*parse_mb_func
)(unsigned *wc
, const char *ptr
, const char *end
);
832 static ZEND_RESULT_CODE
to_utf16(parse_mb_func fn
, const char *u8
, uint16_t **u16
, size_t *len TSRMLS_DC
)
834 size_t offset
= 0, u8_len
= strlen(u8
);
836 *u16
= ecalloc(4 * sizeof(uint16_t), u8_len
+ 1);
839 while (offset
< u8_len
) {
841 uint16_t buf
[2], *ptr
= buf
;
842 size_t consumed
= fn(&wc
, &u8
[offset
], &u8
[u8_len
]);
846 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse UTF-8 at pos %zu of '%s'", offset
, u8
);
852 switch (wctoutf16(buf
, wc
)) {
854 (*u16
)[(*len
)++] = *ptr
++;
857 (*u16
)[(*len
)++] = *ptr
++;
862 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to convert UTF-32 'U+%X' to UTF-16", wc
);
871 #ifndef MAXHOSTNAMELEN
872 # define MAXHOSTNAMELEN 256
875 #if PHP_HTTP_HAVE_IDN2
876 static ZEND_RESULT_CODE
parse_idn2(struct parse_state
*state
, size_t prev_len
)
880 TSRMLS_FETCH_FROM_CTX(state
->ts
);
882 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
883 rv
= idn2_lookup_u8((const unsigned char *) state
->url
.host
, (unsigned char **) &idn
, IDN2_NFC_INPUT
);
885 # ifdef PHP_HTTP_HAVE_WCHAR
886 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
887 rv
= idn2_lookup_ul(state
->url
.host
, &idn
, 0);
891 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN; %s", idn2_strerror(rv
));
894 size_t idnlen
= strlen(idn
);
895 memcpy(state
->url
.host
, idn
, idnlen
+ 1);
897 state
->offset
+= idnlen
- prev_len
;
901 #elif PHP_HTTP_HAVE_IDN
902 static ZEND_RESULT_CODE
parse_idn(struct parse_state
*state
, size_t prev_len
)
906 TSRMLS_FETCH_FROM_CTX(state
->ts
);
908 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
909 rv
= idna_to_ascii_8z(state
->url
.host
, &idn
, IDNA_ALLOW_UNASSIGNED
|IDNA_USE_STD3_ASCII_RULES
);
911 # ifdef PHP_HTTP_HAVE_WCHAR
912 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
913 rv
= idna_to_ascii_lz(state
->url
.host
, &idn
, IDNA_ALLOW_UNASSIGNED
|IDNA_USE_STD3_ASCII_RULES
);
916 if (rv
!= IDNA_SUCCESS
) {
917 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN; %s", idna_strerror(rv
));
920 size_t idnlen
= strlen(idn
);
921 memcpy(state
->url
.host
, idn
, idnlen
+ 1);
923 state
->offset
+= idnlen
- prev_len
;
929 #ifdef HAVE_UIDNA_IDNTOASCII
930 # if HAVE_UNICODE_UIDNA_H
931 # include <unicode/uidna.h>
933 typedef uint16_t UChar
;
934 typedef enum { U_ZERO_ERROR
= 0 } UErrorCode
;
935 int32_t uidna_IDNToASCII(const UChar
*src
, int32_t srcLength
, UChar
*dest
, int32_t destCapacity
, int32_t options
, void *parseError
, UErrorCode
*status
);
937 static ZEND_RESULT_CODE
parse_uidn(struct parse_state
*state
)
940 uint16_t *uhost_str
, ahost_str
[MAXHOSTNAMELEN
], *ahost_ptr
;
941 size_t uhost_len
, ahost_len
;
942 UErrorCode error
= U_ZERO_ERROR
;
943 TSRMLS_FETCH_FROM_CTX(state
->ts
);
945 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
946 if (SUCCESS
!= to_utf16(parse_mb_utf8
, state
->url
.host
, &uhost_str
, &uhost_len TSRMLS_CC
)) {
949 #ifdef PHP_HTTP_HAVE_WCHAR
950 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
951 if (SUCCESS
!= to_utf16(parse_mb_loc
, state
->url
.host
, &uhost_str
, &uhost_len TSRMLS_CC
)) {
956 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN; codepage not specified");
960 ahost_len
= uidna_IDNToASCII(uhost_str
, uhost_len
, ahost_str
, MAXHOSTNAMELEN
, 3, NULL
, &error
);
963 if (error
!= U_ZERO_ERROR
) {
964 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN; ICU error %d", error
);
968 host_ptr
= state
->url
.host
;
969 ahost_ptr
= ahost_str
;
970 PHP_HTTP_DUFF(ahost_len
, *host_ptr
++ = *ahost_ptr
++);
973 state
->offset
+= host_ptr
- state
->url
.host
;
979 #if 0 && defined(PHP_WIN32)
980 static ZEND_RESULT_CODE
parse_widn(struct parse_state
*state
)
983 uint16_t *uhost_str
, ahost_str
[MAXHOSTNAMELEN
], *ahost_ptr
;
985 TSRMLS_FETCH_FROM_CTX(state
->ts
);
987 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
988 if (SUCCESS
!= to_utf16(parse_mb_utf8
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
989 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN");
992 #ifdef PHP_HTTP_HAVE_WCHAR
993 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
994 if (SUCCESS
!= to_utf16(parse_mb_loc
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
995 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN");
1000 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN");
1004 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED
|IDN_USE_STD3_ASCII_RULES
, uhost_str
, uhost_len
, ahost_str
, MAXHOSTNAMELEN
)) {
1006 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse IDN");
1011 host_ptr
= state
->url
.host
;
1012 ahost_ptr
= ahost_str
;
1013 PHP_HTTP_DUFF(wcslen(ahost_str
), *host_ptr
++ = *ahost_ptr
++);
1017 state
->offset
+= host_ptr
- state
->url
.host
;
1023 static ZEND_RESULT_CODE
parse_hostinfo(struct parse_state
*state
, const char *ptr
)
1026 const char *end
= state
->ptr
, *tmp
= ptr
, *port
= NULL
;
1027 TSRMLS_FETCH_FROM_CTX(state
->ts
);
1029 #ifdef HAVE_INET_PTON
1031 char *error
= NULL
, *tmp
= memchr(ptr
, ']', end
- ptr
);
1034 size_t addrlen
= tmp
- ptr
+ 1;
1035 char buf
[16], *addr
= estrndup(ptr
+ 1, addrlen
- 2);
1036 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 '['";
1053 error
= "expected ']'";
1057 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse hostinfo; %s", error
);
1062 if (ptr
!= end
) do {
1066 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1067 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1068 (unsigned) (ptr
- tmp
), tmp
);
1075 if (ptr
[1] != '%' && (end
- ptr
<= 2 || !isxdigit(*(ptr
+1)) || !isxdigit(*(ptr
+2)))) {
1076 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1077 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1078 (unsigned) (ptr
- tmp
), tmp
);
1081 state
->buffer
[state
->offset
++] = *ptr
++;
1082 state
->buffer
[state
->offset
++] = *ptr
++;
1083 state
->buffer
[state
->offset
++] = *ptr
;
1086 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1087 case '+': case ',': case ';': case '=': /* sub-delims */
1088 case '-': case '.': case '_': case '~': /* unreserved */
1089 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1090 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1091 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1092 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1093 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1094 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1095 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1096 case 'v': case 'w': case 'x': case 'y': case 'z':
1098 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1099 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1100 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1104 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1105 case '7': case '8': case '9':
1108 state
->url
.port
*= 10;
1109 state
->url
.port
+= *ptr
- '0';
1111 state
->buffer
[state
->offset
++] = *ptr
;
1119 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1120 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1121 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1123 } else if (!(mb
= parse_mb(state
, PARSE_HOSTINFO
, ptr
, end
, tmp
, 0))) {
1128 } while (++ptr
!= end
);
1130 if (!state
->url
.host
) {
1131 len
= (port
? port
- tmp
- 1 : end
- tmp
);
1132 state
->url
.host
= &state
->buffer
[state
->offset
- len
];
1133 state
->buffer
[state
->offset
++] = 0;
1136 if (state
->flags
& PHP_HTTP_URL_PARSE_TOIDN
) {
1137 #if PHP_HTTP_HAVE_IDN2
1138 return parse_idn2(state
, len
);
1139 #elif PHP_HTTP_HAVE_IDN
1140 return parse_idn(state
, len
);
1142 #ifdef HAVE_UIDNA_IDNTOASCII
1143 return parse_uidn(state
);
1145 #if 0 && defined(PHP_WIN32)
1146 return parse_widn(state
);
1153 static const char *parse_authority(struct parse_state
*state
)
1155 const char *tmp
= state
->ptr
, *host
= NULL
;
1158 switch (*state
->ptr
) {
1160 /* userinfo delimiter */
1162 TSRMLS_FETCH_FROM_CTX(state
->ts
);
1163 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1164 "Failed to parse userinfo; unexpected '@'");
1167 host
= state
->ptr
+ 1;
1168 if (tmp
!= state
->ptr
&& SUCCESS
!= parse_userinfo(state
, tmp
)) {
1171 tmp
= state
->ptr
+ 1;
1179 /* host delimiter */
1180 if (tmp
!= state
->ptr
&& SUCCESS
!= parse_hostinfo(state
, tmp
)) {
1185 } while (++state
->ptr
<= state
->end
);
1191 static const char *parse_path(struct parse_state
*state
)
1195 TSRMLS_FETCH_FROM_CTX(state
->ts
);
1197 /* is there actually a path to parse? */
1202 state
->url
.path
= &state
->buffer
[state
->offset
];
1205 switch (*state
->ptr
) {
1211 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1212 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1213 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1214 (unsigned) (state
->ptr
- tmp
), tmp
);
1217 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1218 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1219 state
->buffer
[state
->offset
++] = *state
->ptr
;
1222 case '/': /* yeah, well */
1223 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1224 case '+': case ',': case ';': case '=': /* sub-delims */
1225 case '-': case '.': case '_': case '~': /* unreserved */
1226 case ':': case '@': /* pchar */
1227 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1228 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1229 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1230 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1231 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1232 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1233 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1234 case 'v': case 'w': case 'x': case 'y': case 'z':
1235 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1236 case '7': case '8': case '9':
1238 state
->buffer
[state
->offset
++] = *state
->ptr
;
1242 if (!(mb
= parse_mb(state
, PARSE_PATH
, state
->ptr
, state
->end
, tmp
, 0))) {
1245 state
->ptr
+= mb
- 1;
1247 } while (++state
->ptr
< state
->end
);
1250 /* did we have any path component ? */
1251 if (tmp
!= state
->ptr
) {
1252 state
->buffer
[state
->offset
++] = 0;
1254 state
->url
.path
= NULL
;
1259 static const char *parse_query(struct parse_state
*state
)
1262 const char *tmp
= state
->ptr
+ !!*state
->ptr
;
1263 TSRMLS_FETCH_FROM_CTX(state
->ts
);
1265 /* is there actually a query to parse? */
1266 if (*state
->ptr
!= '?') {
1270 /* skip initial '?' */
1272 state
->url
.query
= &state
->buffer
[state
->offset
];
1275 switch (*state
->ptr
) {
1280 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1281 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1282 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1283 (unsigned) (state
->ptr
- tmp
), tmp
);
1286 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1287 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1288 state
->buffer
[state
->offset
++] = *state
->ptr
;
1293 if (state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) {
1294 state
->buffer
[state
->offset
++] = '%';
1295 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) >> 4];
1296 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) & 0xf];
1301 case '?': case '/': /* yeah, well */
1302 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1303 case '+': case ',': case ';': case '=': /* sub-delims */
1304 case '-': case '.': case '_': case '~': /* unreserved */
1305 case ':': case '@': /* pchar */
1306 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1307 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1308 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1309 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1310 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1311 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1312 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1313 case 'v': case 'w': case 'x': case 'y': case 'z':
1314 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1315 case '7': case '8': case '9':
1317 state
->buffer
[state
->offset
++] = *state
->ptr
;
1321 if (!(mb
= parse_mb(state
, PARSE_QUERY
, state
->ptr
, state
->end
, tmp
, 0))) {
1324 state
->ptr
+= mb
- 1;
1326 } while (++state
->ptr
< state
->end
);
1329 state
->buffer
[state
->offset
++] = 0;
1333 static const char *parse_fragment(struct parse_state
*state
)
1337 TSRMLS_FETCH_FROM_CTX(state
->ts
);
1339 /* is there actually a fragment to parse? */
1340 if (*state
->ptr
!= '#') {
1344 /* skip initial '#' */
1346 state
->url
.fragment
= &state
->buffer
[state
->offset
];
1349 switch (*state
->ptr
) {
1351 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1352 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1353 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1354 (unsigned) (state
->ptr
- tmp
), tmp
);
1357 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1358 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1359 state
->buffer
[state
->offset
++] = *state
->ptr
;
1363 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1364 case '+': case ',': case ';': case '=': /* sub-delims */
1365 case '-': case '.': case '_': case '~': /* unreserved */
1366 case ':': case '@': /* pchar */
1367 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1368 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1369 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1370 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1371 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1372 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1373 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1374 case 'v': case 'w': case 'x': case 'y': case 'z':
1375 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1376 case '7': case '8': case '9':
1378 state
->buffer
[state
->offset
++] = *state
->ptr
;
1382 if (!(mb
= parse_mb(state
, PARSE_FRAGMENT
, state
->ptr
, state
->end
, tmp
, 0))) {
1385 state
->ptr
+= mb
- 1;
1387 } while (++state
->ptr
< state
->end
);
1389 state
->buffer
[state
->offset
++] = 0;
1393 static const char *parse_hier(struct parse_state
*state
)
1395 if (*state
->ptr
== '/') {
1396 if (state
->end
- state
->ptr
> 1) {
1397 if (*(state
->ptr
+ 1) == '/') {
1399 if (!(state
->ptr
= parse_authority(state
))) {
1405 return parse_path(state
);
1408 static const char *parse_scheme(struct parse_state
*state
)
1411 const char *tmp
= state
->ptr
;
1414 switch (*state
->ptr
) {
1416 /* scheme delimiter */
1417 state
->url
.scheme
= &state
->buffer
[0];
1418 state
->buffer
[state
->offset
++] = 0;
1419 return ++state
->ptr
;
1421 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1422 case '7': case '8': case '9':
1423 case '+': case '-': case '.':
1424 if (state
->ptr
== tmp
) {
1428 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1429 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1430 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1431 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1432 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1433 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1434 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1435 case 'v': case 'w': case 'x': case 'y': case 'z':
1437 state
->buffer
[state
->offset
++] = *state
->ptr
;
1441 if (!(mb
= parse_mb(state
, PARSE_SCHEME
, state
->ptr
, state
->end
, tmp
, 1))) {
1442 /* soft fail; parse path next */
1445 state
->ptr
+= mb
- 1;
1447 } while (++state
->ptr
!= state
->end
);
1449 return state
->ptr
= tmp
;
1452 php_http_url_t
*php_http_url_parse(const char *str
, size_t len
, unsigned flags TSRMLS_DC
)
1454 size_t maxlen
= 3 * len
;
1455 struct parse_state
*state
= ecalloc(1, sizeof(*state
) + maxlen
);
1457 state
->end
= str
+ len
;
1459 state
->flags
= flags
;
1460 state
->maxlen
= maxlen
;
1461 TSRMLS_SET_CTX(state
->ts
);
1463 if (!parse_scheme(state
)) {
1464 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse URL scheme: '%s'", state
->ptr
);
1469 if (!parse_hier(state
)) {
1474 if (!parse_query(state
)) {
1475 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse URL query: '%s'", state
->ptr
);
1480 if (!parse_fragment(state
)) {
1481 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to parse URL fragment: '%s'", state
->ptr
);
1486 return (php_http_url_t
*) state
;
1489 php_http_url_t
*php_http_url_parse_authority(const char *str
, size_t len
, unsigned flags TSRMLS_DC
)
1491 size_t maxlen
= 3 * len
;
1492 struct parse_state
*state
= ecalloc(1, sizeof(*state
) + maxlen
);
1494 state
->end
= str
+ len
;
1496 state
->flags
= flags
;
1497 state
->maxlen
= maxlen
;
1498 TSRMLS_SET_CTX(state
->ts
);
1500 if (!(state
->ptr
= parse_authority(state
))) {
1505 if (state
->ptr
!= state
->end
) {
1506 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1507 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1508 (unsigned) (state
->ptr
- str
), str
);
1513 return (php_http_url_t
*) state
;
1516 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct
, 0, 0, 0)
1517 ZEND_ARG_INFO(0, old_url
)
1518 ZEND_ARG_INFO(0, new_url
)
1519 ZEND_ARG_INFO(0, flags
)
1520 ZEND_END_ARG_INFO();
1521 PHP_METHOD(HttpUrl
, __construct
)
1523 zval
*new_url
= NULL
, *old_url
= NULL
;
1524 long flags
= PHP_HTTP_URL_FROM_ENV
;
1525 zend_error_handling zeh
;
1527 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z!z!l", &old_url
, &new_url
, &flags
), invalid_arg
, return);
1529 zend_replace_error_handling(EH_THROW
, php_http_exception_bad_url_class_entry
, &zeh TSRMLS_CC
);
1531 php_http_url_t
*res_purl
, *new_purl
= NULL
, *old_purl
= NULL
;
1534 new_purl
= php_http_url_from_zval(new_url
, flags TSRMLS_CC
);
1536 zend_restore_error_handling(&zeh TSRMLS_CC
);
1541 old_purl
= php_http_url_from_zval(old_url
, flags TSRMLS_CC
);
1544 php_http_url_free(&new_purl
);
1546 zend_restore_error_handling(&zeh TSRMLS_CC
);
1551 res_purl
= php_http_url_mod(old_purl
, new_purl
, flags TSRMLS_CC
);
1552 php_http_url_to_struct(res_purl
, getThis() TSRMLS_CC
);
1554 php_http_url_free(&res_purl
);
1556 php_http_url_free(&old_purl
);
1559 php_http_url_free(&new_purl
);
1562 zend_restore_error_handling(&zeh TSRMLS_CC
);
1565 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod
, 0, 0, 1)
1566 ZEND_ARG_INFO(0, more_url_parts
)
1567 ZEND_ARG_INFO(0, flags
)
1568 ZEND_END_ARG_INFO();
1569 PHP_METHOD(HttpUrl
, mod
)
1571 zval
*new_url
= NULL
;
1572 long flags
= PHP_HTTP_URL_JOIN_PATH
| PHP_HTTP_URL_JOIN_QUERY
;
1573 zend_error_handling zeh
;
1575 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z!|l", &new_url
, &flags
), invalid_arg
, return);
1577 zend_replace_error_handling(EH_THROW
, php_http_exception_bad_url_class_entry
, &zeh TSRMLS_CC
);
1579 php_http_url_t
*new_purl
= NULL
, *old_purl
= NULL
;
1582 new_purl
= php_http_url_from_zval(new_url
, flags TSRMLS_CC
);
1584 zend_restore_error_handling(&zeh TSRMLS_CC
);
1589 if ((old_purl
= php_http_url_from_struct(HASH_OF(getThis())))) {
1590 php_http_url_t
*res_purl
;
1592 ZVAL_OBJVAL(return_value
, zend_objects_clone_obj(getThis() TSRMLS_CC
), 0);
1594 res_purl
= php_http_url_mod(old_purl
, new_purl
, flags TSRMLS_CC
);
1595 php_http_url_to_struct(res_purl
, return_value TSRMLS_CC
);
1597 php_http_url_free(&res_purl
);
1598 php_http_url_free(&old_purl
);
1601 php_http_url_free(&new_purl
);
1604 zend_restore_error_handling(&zeh TSRMLS_CC
);
1607 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString
, 0, 0, 0)
1608 ZEND_END_ARG_INFO();
1609 PHP_METHOD(HttpUrl
, toString
)
1611 if (SUCCESS
== zend_parse_parameters_none()) {
1612 php_http_url_t
*purl
;
1614 if ((purl
= php_http_url_from_struct(HASH_OF(getThis())))) {
1618 php_http_url_to_string(purl
, &str
, &len
, 0);
1619 php_http_url_free(&purl
);
1620 RETURN_STRINGL(str
, len
, 0);
1623 RETURN_EMPTY_STRING();
1626 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray
, 0, 0, 0)
1627 ZEND_END_ARG_INFO();
1628 PHP_METHOD(HttpUrl
, toArray
)
1630 php_http_url_t
*purl
;
1632 if (SUCCESS
!= zend_parse_parameters_none()) {
1636 /* strip any non-URL properties */
1637 purl
= php_http_url_from_struct(HASH_OF(getThis()));
1638 php_http_url_to_struct(purl
, return_value TSRMLS_CC
);
1639 php_http_url_free(&purl
);
1642 static zend_function_entry php_http_url_methods
[] = {
1643 PHP_ME(HttpUrl
, __construct
, ai_HttpUrl___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1644 PHP_ME(HttpUrl
, mod
, ai_HttpUrl_mod
, ZEND_ACC_PUBLIC
)
1645 PHP_ME(HttpUrl
, toString
, ai_HttpUrl_toString
, ZEND_ACC_PUBLIC
)
1646 ZEND_MALIAS(HttpUrl
, __toString
, toString
, ai_HttpUrl_toString
, ZEND_ACC_PUBLIC
)
1647 PHP_ME(HttpUrl
, toArray
, ai_HttpUrl_toArray
, ZEND_ACC_PUBLIC
)
1648 EMPTY_FUNCTION_ENTRY
1651 zend_class_entry
*php_http_url_class_entry
;
1653 PHP_MINIT_FUNCTION(http_url
)
1655 zend_class_entry ce
= {0};
1657 INIT_NS_CLASS_ENTRY(ce
, "http", "Url", php_http_url_methods
);
1658 php_http_url_class_entry
= zend_register_internal_class(&ce TSRMLS_CC
);
1660 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1661 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1662 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1663 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1664 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1665 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("path"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1666 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("query"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1667 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1669 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE TSRMLS_CC
);
1670 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH TSRMLS_CC
);
1671 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY TSRMLS_CC
);
1672 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER TSRMLS_CC
);
1673 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS TSRMLS_CC
);
1674 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH TSRMLS_CC
);
1675 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT TSRMLS_CC
);
1676 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH TSRMLS_CC
);
1677 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY TSRMLS_CC
);
1678 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT TSRMLS_CC
);
1679 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL TSRMLS_CC
);
1680 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV TSRMLS_CC
);
1681 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH TSRMLS_CC
);
1683 #ifdef PHP_HTTP_HAVE_WCHAR
1684 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC TSRMLS_CC
);
1686 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8 TSRMLS_CC
);
1687 #if defined(PHP_HTTP_HAVE_IDN2) || defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
1688 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN TSRMLS_CC
);
1690 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT TSRMLS_CC
);
1701 * vim600: noet sw=4 ts=4 fdm=marker
1702 * vim<600: noet sw=4 ts=4