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 #ifdef PHP_HTTP_HAVE_IDN
19 #ifdef PHP_HTTP_HAVE_WCHAR
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
28 #include "php_http_utf8.h"
30 static inline char *localhostname(void)
32 char hostname
[1024] = {0};
35 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
36 return estrdup(hostname
);
38 #elif defined(HAVE_GETHOSTNAME)
39 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
40 # if defined(HAVE_GETDOMAINNAME)
41 size_t hlen
= strlen(hostname
);
42 if (hlen
<= lenof(hostname
) - lenof("(none)")) {
43 hostname
[hlen
++] = '.';
44 if (SUCCESS
== getdomainname(&hostname
[hlen
], lenof(hostname
) - hlen
)) {
45 if (!strcmp(&hostname
[hlen
], "(none)")) {
46 hostname
[hlen
- 1] = '\0';
48 return estrdup(hostname
);
52 if (strcmp(hostname
, "(none)")) {
53 return estrdup(hostname
);
57 return estrndup("localhost", lenof("localhost"));
60 #define url(buf) ((php_http_url_t *) (buf).data)
62 static php_http_url_t
*php_http_url_from_env(void)
64 zval
*https
, *zhost
, *zport
;
66 php_http_buffer_t buf
;
68 php_http_buffer_init_ex(&buf
, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE
, sizeof(php_http_url_t
)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC
);
69 php_http_buffer_account(&buf
, sizeof(php_http_url_t
));
70 memset(buf
.data
, 0, buf
.used
);
73 url(buf
)->scheme
= &buf
.data
[buf
.used
];
74 https
= php_http_env_get_server_var(ZEND_STRL("HTTPS"), 1);
75 if (https
&& !strcasecmp(Z_STRVAL_P(https
), "ON")) {
76 php_http_buffer_append(&buf
, "https", sizeof("https"));
78 php_http_buffer_append(&buf
, "http", sizeof("http"));
82 url(buf
)->host
= &buf
.data
[buf
.used
];
83 if ((((zhost
= php_http_env_get_server_var(ZEND_STRL("HTTP_HOST"), 1)) ||
84 (zhost
= php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1)) ||
85 (zhost
= php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1)))) && Z_STRLEN_P(zhost
)) {
86 size_t stop_at
= strspn(Z_STRVAL_P(zhost
), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.");
88 php_http_buffer_append(&buf
, Z_STRVAL_P(zhost
), stop_at
);
89 php_http_buffer_append(&buf
, "", 1);
91 char *host_str
= localhostname();
93 php_http_buffer_append(&buf
, host_str
, strlen(host_str
) + 1);
98 zport
= php_http_env_get_server_var(ZEND_STRL("SERVER_PORT"), 1);
99 if (zport
&& IS_LONG
== is_numeric_string(Z_STRVAL_P(zport
), Z_STRLEN_P(zport
), &port
, NULL
, 0)) {
100 url(buf
)->port
= port
;
104 if (SG(request_info
).request_uri
&& SG(request_info
).request_uri
[0]) {
105 const char *q
= strchr(SG(request_info
).request_uri
, '?');
107 url(buf
)->path
= &buf
.data
[buf
.used
];
110 php_http_buffer_append(&buf
, SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
111 php_http_buffer_append(&buf
, "", 1);
113 php_http_buffer_append(&buf
, SG(request_info
).request_uri
, strlen(SG(request_info
).request_uri
) + 1);
118 if (SG(request_info
).query_string
&& SG(request_info
).query_string
[0]) {
119 url(buf
)->query
= &buf
.data
[buf
.used
];
120 php_http_buffer_append(&buf
, SG(request_info
).query_string
, strlen(SG(request_info
).query_string
) + 1);
126 #define url_isset(u,n) \
128 #define url_append(buf, append) do { \
129 char *_ptr = (buf)->data; \
130 php_http_url_t *_url = (php_http_url_t *) _ptr, _mem = *_url; \
133 if (_ptr != (buf)->data) { \
134 ptrdiff_t diff = (buf)->data - _ptr; \
135 _url = (php_http_url_t *) (buf)->data; \
136 if (_mem.scheme) _url->scheme += diff; \
137 if (_mem.user) _url->user += diff; \
138 if (_mem.pass) _url->pass += diff; \
139 if (_mem.host) _url->host += diff; \
140 if (_mem.path) _url->path += diff; \
141 if (_mem.query) _url->query += diff; \
142 if (_mem.fragment) _url->fragment += diff; \
145 #define url_copy(n) do { \
146 if (url_isset(new_url, n)) { \
147 url(buf)->n = &buf.data[buf.used]; \
148 url_append(&buf, php_http_buffer_append(&buf, new_url->n, strlen(new_url->n) + 1)); \
149 } else if (url_isset(old_url, n)) { \
150 url(buf)->n = &buf.data[buf.used]; \
151 url_append(&buf, php_http_buffer_append(&buf, old_url->n, strlen(old_url->n) + 1)); \
155 php_http_url_t
*php_http_url_mod(const php_http_url_t
*old_url
, const php_http_url_t
*new_url
, unsigned flags
)
157 php_http_url_t
*tmp_url
= NULL
;
158 php_http_buffer_t buf
;
160 php_http_buffer_init_ex(&buf
, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE
, sizeof(php_http_url_t
)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC
);
161 php_http_buffer_account(&buf
, sizeof(php_http_url_t
));
162 memset(buf
.data
, 0, buf
.used
);
164 /* set from env if requested */
165 if (flags
& PHP_HTTP_URL_FROM_ENV
) {
166 php_http_url_t
*env_url
= php_http_url_from_env();
168 old_url
= tmp_url
= php_http_url_mod(env_url
, old_url
, flags
^ PHP_HTTP_URL_FROM_ENV
);
169 php_http_url_free(&env_url
);
174 if (!(flags
& PHP_HTTP_URL_STRIP_USER
)) {
178 if (!(flags
& PHP_HTTP_URL_STRIP_PASS
)) {
184 if (!(flags
& PHP_HTTP_URL_STRIP_PORT
)) {
185 url(buf
)->port
= url_isset(new_url
, port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
188 if (!(flags
& PHP_HTTP_URL_STRIP_PATH
)) {
189 if ((flags
& PHP_HTTP_URL_JOIN_PATH
) && url_isset(old_url
, path
) && url_isset(new_url
, path
) && *new_url
->path
!= '/') {
190 size_t old_path_len
= strlen(old_url
->path
), new_path_len
= strlen(new_url
->path
);
191 char *path
= ecalloc(1, old_path_len
+ new_path_len
+ 1 + 1);
193 strcat(path
, old_url
->path
);
194 if (path
[old_path_len
- 1] != '/') {
195 php_dirname(path
, old_path_len
);
198 strcat(path
, new_url
->path
);
200 url(buf
)->path
= &buf
.data
[buf
.used
];
201 if (path
[0] != '/') {
202 url_append(&buf
, php_http_buffer_append(&buf
, "/", 1));
204 url_append(&buf
, php_http_buffer_append(&buf
, path
, strlen(path
) + 1));
207 const char *path
= NULL
;
209 if (url_isset(new_url
, path
)) {
210 path
= new_url
->path
;
211 } else if (url_isset(old_url
, path
)) {
212 path
= old_url
->path
;
216 url(buf
)->path
= &buf
.data
[buf
.used
];
218 url_append(&buf
, php_http_buffer_append(&buf
, path
, strlen(path
) + 1));
225 if (!(flags
& PHP_HTTP_URL_STRIP_QUERY
)) {
226 if ((flags
& PHP_HTTP_URL_JOIN_QUERY
) && url_isset(new_url
, query
) && url_isset(old_url
, query
)) {
231 ZVAL_STRING(&qstr
, old_url
->query
);
232 php_http_querystring_update(&qarr
, &qstr
, NULL
);
233 zval_ptr_dtor(&qstr
);
234 ZVAL_STRING(&qstr
, new_url
->query
);
235 php_http_querystring_update(&qarr
, &qstr
, NULL
);
236 zval_ptr_dtor(&qstr
);
239 php_http_querystring_update(&qarr
, NULL
, &qstr
);
241 url(buf
)->query
= &buf
.data
[buf
.used
];
242 url_append(&buf
, php_http_buffer_append(&buf
, Z_STRVAL(qstr
), Z_STRLEN(qstr
) + 1));
251 if (!(flags
& PHP_HTTP_URL_STRIP_FRAGMENT
)) {
255 /* done with copy & combine & strip */
257 if (flags
& PHP_HTTP_URL_FROM_ENV
) {
258 /* free old_url we tainted above */
259 php_http_url_free(&tmp_url
);
262 /* replace directory references if path is not a single slash */
263 if ((flags
& PHP_HTTP_URL_SANITIZE_PATH
)
264 && url(buf
)->path
[0] && url(buf
)->path
[1]) {
265 char *ptr
, *end
= url(buf
)->path
+ strlen(url(buf
)->path
) + 1;
267 for (ptr
= strchr(url(buf
)->path
, '/'); ptr
; ptr
= strchr(ptr
, '/')) {
270 memmove(&ptr
[1], &ptr
[2], end
- &ptr
[2]);
280 memmove(&ptr
[1], &ptr
[3], end
- &ptr
[3]);
286 while (ptr
!= url(buf
)->path
) {
291 memmove(&ptr
[1], pos
, end
- pos
);
293 } else if (!ptr
[3]) {
312 /* unset default ports */
313 if (url(buf
)->port
) {
314 if ( ((url(buf
)->port
== 80) && url(buf
)->scheme
&& !strcmp(url(buf
)->scheme
, "http"))
315 || ((url(buf
)->port
==443) && url(buf
)->scheme
&& !strcmp(url(buf
)->scheme
, "https"))
324 char *php_http_url_to_string(const php_http_url_t
*url
, char **url_str
, size_t *url_len
, zend_bool persistent
)
326 php_http_buffer_t buf
;
328 php_http_buffer_init_ex(&buf
, PHP_HTTP_BUFFER_DEFAULT_SIZE
, persistent
?
329 PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0);
331 if (url
->scheme
&& *url
->scheme
) {
332 php_http_buffer_appendl(&buf
, url
->scheme
);
333 php_http_buffer_appends(&buf
, "://");
334 } else if ((url
->user
&& *url
->user
) || (url
->host
&& *url
->host
)) {
335 php_http_buffer_appends(&buf
, "//");
338 if (url
->user
&& *url
->user
) {
339 php_http_buffer_appendl(&buf
, url
->user
);
340 if (url
->pass
&& *url
->pass
) {
341 php_http_buffer_appends(&buf
, ":");
342 php_http_buffer_appendl(&buf
, url
->pass
);
344 php_http_buffer_appends(&buf
, "@");
347 if (url
->host
&& *url
->host
) {
348 php_http_buffer_appendl(&buf
, url
->host
);
350 php_http_buffer_appendf(&buf
, ":%hu", url
->port
);
354 if (url
->path
&& *url
->path
) {
355 if (*url
->path
!= '/') {
356 php_http_buffer_appends(&buf
, "/");
358 php_http_buffer_appendl(&buf
, url
->path
);
359 } else if (buf
.used
) {
360 php_http_buffer_appends(&buf
, "/");
363 if (url
->query
&& *url
->query
) {
364 php_http_buffer_appends(&buf
, "?");
365 php_http_buffer_appendl(&buf
, url
->query
);
368 if (url
->fragment
&& *url
->fragment
) {
369 php_http_buffer_appends(&buf
, "#");
370 php_http_buffer_appendl(&buf
, url
->fragment
);
373 php_http_buffer_shrink(&buf
);
374 php_http_buffer_fix(&buf
);
387 char *php_http_url_authority_to_string(const php_http_url_t
*url
, char **url_str
, size_t *url_len
)
389 php_http_buffer_t buf
;
391 php_http_buffer_init(&buf
);
393 if (url
->user
&& *url
->user
) {
394 php_http_buffer_appendl(&buf
, url
->user
);
395 if (url
->pass
&& *url
->pass
) {
396 php_http_buffer_appends(&buf
, ":");
397 php_http_buffer_appendl(&buf
, url
->pass
);
399 php_http_buffer_appends(&buf
, "@");
402 if (url
->host
&& *url
->host
) {
403 php_http_buffer_appendl(&buf
, url
->host
);
405 php_http_buffer_appendf(&buf
, ":%hu", url
->port
);
409 php_http_buffer_shrink(&buf
);
410 php_http_buffer_fix(&buf
);
423 php_http_url_t
*php_http_url_from_zval(zval
*value
, unsigned flags
)
426 php_http_url_t
*purl
;
428 switch (Z_TYPE_P(value
)) {
431 purl
= php_http_url_from_struct(HASH_OF(value
));
435 zs
= zval_get_string(value
);
436 purl
= php_http_url_parse(zs
->val
, zs
->len
, flags
);
437 zend_string_release(zs
);
443 php_http_url_t
*php_http_url_from_struct(HashTable
*ht
)
446 php_http_buffer_t buf
;
448 php_http_buffer_init_ex(&buf
, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE
, sizeof(php_http_url_t
)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC
);
449 php_http_buffer_account(&buf
, sizeof(php_http_url_t
));
450 memset(buf
.data
, 0, buf
.used
);
452 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("scheme")))) {
453 zend_string
*zs
= zval_get_string(e
);
454 url(buf
)->scheme
= &buf
.data
[buf
.used
];
455 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
456 zend_string_release(zs
);
458 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("user")))) {
459 zend_string
*zs
= zval_get_string(e
);
460 url(buf
)->user
= &buf
.data
[buf
.used
];
461 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
462 zend_string_release(zs
);
464 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("pass")))) {
465 zend_string
*zs
= zval_get_string(e
);
466 url(buf
)->pass
= &buf
.data
[buf
.used
];
467 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
468 zend_string_release(zs
);
470 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("host")))) {
471 zend_string
*zs
= zval_get_string(e
);
472 url(buf
)->host
= &buf
.data
[buf
.used
];
473 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
474 zend_string_release(zs
);
476 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("port")))) {
477 url(buf
)->port
= (unsigned short) zval_get_long(e
);
479 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("path")))) {
480 zend_string
*zs
= zval_get_string(e
);
481 url(buf
)->path
= &buf
.data
[buf
.used
];
482 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
483 zend_string_release(zs
);
485 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("query")))) {
486 zend_string
*zs
= zval_get_string(e
);
487 url(buf
)->query
= &buf
.data
[buf
.used
];
488 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
489 zend_string_release(zs
);
491 if ((e
= zend_hash_str_find_ind(ht
, ZEND_STRL("fragment")))) {
492 zend_string
*zs
= zval_get_string(e
);
493 url(buf
)->fragment
= &buf
.data
[buf
.used
];
494 url_append(&buf
, php_http_buffer_append(&buf
, zs
->val
, zs
->len
+ 1));
495 zend_string_release(zs
);
501 HashTable
*php_http_url_to_struct(const php_http_url_t
*url
, zval
*strct
)
507 switch (Z_TYPE_P(strct
)) {
519 zend_hash_init(ht
, 8, NULL
, ZVAL_PTR_DTOR
, 0);
522 #define url_struct_add(part) \
523 if (Z_TYPE_P(strct) == IS_ARRAY) { \
524 zend_hash_str_update(Z_ARRVAL_P(strct), part, lenof(part), &tmp); \
526 zend_update_property(Z_OBJCE_P(strct), strct, part, lenof(part), &tmp); \
527 zval_ptr_dtor(&tmp); \
532 ZVAL_STRING(&tmp
, url
->scheme
);
533 url_struct_add("scheme");
536 ZVAL_STRING(&tmp
, url
->user
);
537 url_struct_add("user");
540 ZVAL_STRING(&tmp
, url
->pass
);
541 url_struct_add("pass");
544 ZVAL_STRING(&tmp
, url
->host
);
545 url_struct_add("host");
548 ZVAL_LONG(&tmp
, url
->port
);
549 url_struct_add("port");
552 ZVAL_STRING(&tmp
, url
->path
);
553 url_struct_add("path");
556 ZVAL_STRING(&tmp
, url
->query
);
557 url_struct_add("query");
560 ZVAL_STRING(&tmp
, url
->fragment
);
561 url_struct_add("fragment");
568 ZEND_RESULT_CODE
php_http_url_encode_hash(HashTable
*hash
, const char *pre_encoded_str
, size_t pre_encoded_len
, char **encoded_str
, size_t *encoded_len
)
570 const char *arg_sep_str
;
572 php_http_buffer_t
*qstr
= php_http_buffer_new();
574 php_http_url_argsep(&arg_sep_str
, &arg_sep_len
);
576 if (SUCCESS
!= php_http_url_encode_hash_ex(hash
, qstr
, arg_sep_str
, arg_sep_len
, "=", 1, pre_encoded_str
, pre_encoded_len
)) {
577 php_http_buffer_free(&qstr
);
581 php_http_buffer_data(qstr
, encoded_str
, encoded_len
);
582 php_http_buffer_free(&qstr
);
587 ZEND_RESULT_CODE
php_http_url_encode_hash_ex(HashTable
*hash
, php_http_buffer_t
*qstr
, const char *arg_sep_str
, size_t arg_sep_len
, const char *val_sep_str
, size_t val_sep_len
, const char *pre_encoded_str
, size_t pre_encoded_len
)
589 if (pre_encoded_len
&& pre_encoded_str
) {
590 php_http_buffer_append(qstr
, pre_encoded_str
, pre_encoded_len
);
593 if (!php_http_params_to_string(qstr
, hash
, arg_sep_str
, arg_sep_len
, "", 0, val_sep_str
, val_sep_len
, PHP_HTTP_PARAMS_QUERY
)) {
607 char buffer
[1]; /* last member */
610 void php_http_url_free(php_http_url_t
**url
)
618 php_http_url_t
*php_http_url_copy(const php_http_url_t
*url
, zend_bool persistent
)
621 const char *end
= NULL
, *url_ptr
= (const char *) url
;
624 end
= MAX(url
->scheme
, end
);
625 end
= MAX(url
->pass
, end
);
626 end
= MAX(url
->user
, end
);
627 end
= MAX(url
->host
, end
);
628 end
= MAX(url
->path
, end
);
629 end
= MAX(url
->query
, end
);
630 end
= MAX(url
->fragment
, end
);
633 end
+= strlen(end
) + 1;
634 cpy_ptr
= pecalloc(1, end
- url_ptr
, persistent
);
635 cpy
= (php_http_url_t
*) cpy_ptr
;
637 memcpy(cpy_ptr
+ sizeof(*cpy
), url_ptr
+ sizeof(*url
), end
- url_ptr
- sizeof(*url
));
639 cpy
->scheme
= url
->scheme
? cpy_ptr
+ (url
->scheme
- url_ptr
) : NULL
;
640 cpy
->pass
= url
->pass
? cpy_ptr
+ (url
->pass
- url_ptr
) : NULL
;
641 cpy
->user
= url
->user
? cpy_ptr
+ (url
->user
- url_ptr
) : NULL
;
642 cpy
->host
= url
->host
? cpy_ptr
+ (url
->host
- url_ptr
) : NULL
;
643 cpy
->path
= url
->path
? cpy_ptr
+ (url
->path
- url_ptr
) : NULL
;
644 cpy
->query
= url
->query
? cpy_ptr
+ (url
->query
- url_ptr
) : NULL
;
645 cpy
->fragment
= url
->fragment
? cpy_ptr
+ (url
->fragment
- url_ptr
) : NULL
;
647 cpy
= ecalloc(1, sizeof(*url
));
650 cpy
->port
= url
->port
;
655 static size_t parse_mb_utf8(unsigned *wc
, const char *ptr
, const char *end
)
658 size_t consumed
= utf8towc(&wchar
, (const unsigned char *) ptr
, end
- ptr
);
660 if (!consumed
|| consumed
== (size_t) -1) {
670 #ifdef PHP_HTTP_HAVE_WCHAR
671 static size_t parse_mb_loc(unsigned *wc
, const char *ptr
, const char *end
)
675 #if defined(HAVE_MBRTOWC)
678 memset(&ps
, 0, sizeof(ps
));
679 consumed
= mbrtowc(&wchar
, ptr
, end
- ptr
, &ps
);
680 #elif defined(HAVE_MBTOWC)
681 consumed
= mbtowc(&wchar
, ptr
, end
- ptr
);
684 if (!consumed
|| consumed
== (size_t) -1) {
695 typedef enum parse_mb_what
{
704 static const char * const parse_what
[] = {
713 static const char parse_xdigits
[] = "0123456789ABCDEF";
715 static size_t parse_mb(struct parse_state
*state
, parse_mb_what_t what
, const char *ptr
, const char *end
, const char *begin
, zend_bool silent
)
720 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
721 consumed
= parse_mb_utf8(&wchar
, ptr
, end
);
723 #ifdef PHP_HTTP_HAVE_WCHAR
724 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
725 consumed
= parse_mb_loc(&wchar
, ptr
, end
);
730 if (!(state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) || what
== PARSE_HOSTINFO
|| what
== PARSE_SCHEME
) {
731 if (what
== PARSE_HOSTINFO
&& (state
->flags
& PHP_HTTP_URL_PARSE_TOIDN
)) {
733 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
734 if (!isualnum(wchar
)) {
737 #ifdef PHP_HTTP_HAVE_WCHAR
738 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
739 if (!iswalnum(wchar
)) {
744 PHP_HTTP_DUFF(consumed
, state
->buffer
[state
->offset
++] = *ptr
++);
748 PHP_HTTP_DUFF(consumed
,
749 state
->buffer
[state
->offset
++] = '%';
750 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) ptr
[i
]) >> 4];
751 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) ptr
[i
]) & 0xf];
761 php_error_docref(NULL
, E_WARNING
,
762 "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
763 parse_what
[what
], wchar
, (unsigned) (ptr
- begin
), begin
);
765 php_error_docref(NULL
, E_WARNING
,
766 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
767 parse_what
[what
], (unsigned char) *ptr
, (unsigned) (ptr
- begin
), begin
);
774 static ZEND_RESULT_CODE
parse_userinfo(struct parse_state
*state
, const char *ptr
)
777 const char *password
= NULL
, *end
= state
->ptr
, *tmp
= ptr
;
779 state
->url
.user
= &state
->buffer
[state
->offset
];
785 php_error_docref(NULL
, E_WARNING
,
786 "Failed to parse password; duplicate ':' at pos %u in '%s'",
787 (unsigned) (ptr
- tmp
), tmp
);
791 state
->buffer
[state
->offset
++] = 0;
792 state
->url
.pass
= &state
->buffer
[state
->offset
];
796 if (ptr
[1] != '%' && (end
- ptr
<= 2 || !isxdigit(*(ptr
+1)) || !isxdigit(*(ptr
+2)))) {
797 php_error_docref(NULL
, E_WARNING
,
798 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
799 (unsigned) (ptr
- tmp
), tmp
);
802 state
->buffer
[state
->offset
++] = *ptr
++;
803 state
->buffer
[state
->offset
++] = *ptr
++;
804 state
->buffer
[state
->offset
++] = *ptr
;
807 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
808 case '+': case ',': case ';': case '=': /* sub-delims */
809 case '-': case '.': case '_': case '~': /* unreserved */
810 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
811 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
812 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
813 case 'V': case 'W': case 'X': case 'Y': case 'Z':
814 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
815 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
816 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
817 case 'v': case 'w': case 'x': case 'y': case 'z':
818 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
819 case '7': case '8': case '9':
821 state
->buffer
[state
->offset
++] = *ptr
;
825 if (!(mb
= parse_mb(state
, PARSE_USERINFO
, ptr
, end
, tmp
, 0))) {
830 } while(++ptr
!= end
);
833 state
->buffer
[state
->offset
++] = 0;
838 #if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
839 typedef size_t (*parse_mb_func
)(unsigned *wc
, const char *ptr
, const char *end
);
840 static ZEND_RESULT_CODE
to_utf16(parse_mb_func fn
, const char *u8
, uint16_t **u16
, size_t *len
)
842 size_t offset
= 0, u8_len
= strlen(u8
);
844 *u16
= ecalloc(4 * sizeof(uint16_t), u8_len
+ 1);
847 while (offset
< u8_len
) {
849 uint16_t buf
[2], *ptr
= buf
;
850 size_t consumed
= fn(&wc
, &u8
[offset
], &u8
[u8_len
]);
854 php_error_docref(NULL
, E_WARNING
, "Failed to parse UTF-8 at pos %zu of '%s'", offset
, u8
);
860 switch (wctoutf16(buf
, wc
)) {
862 (*u16
)[(*len
)++] = *ptr
++;
865 (*u16
)[(*len
)++] = *ptr
++;
870 php_error_docref(NULL
, E_WARNING
, "Failed to convert UTF-32 'U+%X' to UTF-16", wc
);
879 #ifndef MAXHOSTNAMELEN
880 # define MAXHOSTNAMELEN 256
883 #ifdef PHP_HTTP_HAVE_IDN
884 static ZEND_RESULT_CODE
parse_idn(struct parse_state
*state
, size_t prev_len
)
889 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
890 rv
= idna_to_ascii_8z(state
->url
.host
, &idn
, IDNA_ALLOW_UNASSIGNED
|IDNA_USE_STD3_ASCII_RULES
);
892 # ifdef PHP_HTTP_HAVE_WCHAR
893 else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
894 rv
= idna_to_ascii_lz(state
->url
.host
, &idn
, IDNA_ALLOW_UNASSIGNED
|IDNA_USE_STD3_ASCII_RULES
);
897 if (rv
!= IDNA_SUCCESS
) {
898 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; %s", idna_strerror(rv
));
901 size_t idnlen
= strlen(idn
);
902 memcpy(state
->url
.host
, idn
, idnlen
+ 1);
904 state
->offset
+= idnlen
- prev_len
;
910 #ifdef HAVE_UIDNA_IDNTOASCII
911 # if HAVE_UNICODE_UIDNA_H
912 # include <unicode/uidna.h>
914 typedef uint16_t UChar
;
915 typedef enum { U_ZERO_ERROR
= 0 } UErrorCode
;
916 int32_t uidna_IDNToASCII(const UChar
*src
, int32_t srcLength
, UChar
*dest
, int32_t destCapacity
, int32_t options
, void *parseError
, UErrorCode
*status
);
918 static ZEND_RESULT_CODE
parse_uidn(struct parse_state
*state
)
921 uint16_t *uhost_str
, ahost_str
[MAXHOSTNAMELEN
], *ahost_ptr
;
922 size_t uhost_len
, ahost_len
;
923 UErrorCode error
= U_ZERO_ERROR
;
924 TSRMLS_FETCH_FROM_CTX(state
->ts
);
926 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
927 if (SUCCESS
!= to_utf16(parse_mb_utf8
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
930 #ifdef PHP_HTTP_HAVE_WCHAR
931 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
932 if (SUCCESS
!= to_utf16(parse_mb_loc
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
937 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; codepage not specified");
941 ahost_len
= uidna_IDNToASCII(uhost_str
, uhost_len
, ahost_str
, MAXHOSTNAMELEN
, 3, NULL
, &error
);
944 if (error
!= U_ZERO_ERROR
) {
945 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN; ICU error %d", error
);
949 host_ptr
= state
->url
.host
;
950 ahost_ptr
= ahost_str
;
951 PHP_HTTP_DUFF(ahost_len
, *host_ptr
++ = *ahost_ptr
++);
954 state
->offset
+= host_ptr
- state
->url
.host
;
960 #if 0 && defined(PHP_WIN32)
961 static ZEND_RESULT_CODE
parse_widn(struct parse_state
*state
)
964 uint16_t *uhost_str
, ahost_str
[MAXHOSTNAMELEN
], *ahost_ptr
;
967 if (state
->flags
& PHP_HTTP_URL_PARSE_MBUTF8
) {
968 if (SUCCESS
!= to_utf16(parse_mb_utf8
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
969 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
972 #ifdef PHP_HTTP_HAVE_WCHAR
973 } else if (state
->flags
& PHP_HTTP_URL_PARSE_MBLOC
) {
974 if (SUCCESS
!= to_utf16(parse_mb_loc
, state
->url
.host
, &uhost_str
, &uhost_len
)) {
975 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
980 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
984 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED
|IDN_USE_STD3_ASCII_RULES
, uhost_str
, uhost_len
, ahost_str
, MAXHOSTNAMELEN
)) {
986 php_error_docref(NULL
, E_WARNING
, "Failed to parse IDN");
991 host_ptr
= state
->url
.host
;
992 ahost_ptr
= ahost_str
;
993 PHP_HTTP_DUFF(wcslen(ahost_str
), *host_ptr
++ = *ahost_ptr
++);
997 state
->offset
+= host_ptr
- state
->url
.host
;
1003 static ZEND_RESULT_CODE
parse_hostinfo(struct parse_state
*state
, const char *ptr
)
1006 const char *end
= state
->ptr
, *tmp
= ptr
, *port
= NULL
;
1008 #ifdef HAVE_INET_PTON
1010 char *error
= NULL
, *tmp
= memchr(ptr
, ']', end
- ptr
);
1013 size_t addrlen
= tmp
- ptr
+ 1;
1014 char buf
[16], *addr
= estrndup(ptr
+ 1, addrlen
- 2);
1015 int rv
= inet_pton(AF_INET6
, addr
, buf
);
1019 state
->buffer
[state
->offset
] = '[';
1020 state
->url
.host
= &state
->buffer
[state
->offset
];
1021 inet_ntop(AF_INET6
, buf
, state
->url
.host
+ 1, state
->maxlen
- state
->offset
);
1022 state
->offset
+= strlen(state
->url
.host
);
1023 state
->buffer
[state
->offset
++] = ']';
1024 state
->buffer
[state
->offset
++] = 0;
1026 } else if (rv
== -1) {
1027 error
= strerror(errno
);
1029 error
= "unexpected '['";
1032 error
= "expected ']'";
1036 php_error_docref(NULL
, E_WARNING
, "Failed to parse hostinfo; %s", error
);
1041 if (ptr
!= end
) do {
1045 php_error_docref(NULL
, E_WARNING
,
1046 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1047 (unsigned) (ptr
- tmp
), tmp
);
1054 if (ptr
[1] != '%' && (end
- ptr
<= 2 || !isxdigit(*(ptr
+1)) || !isxdigit(*(ptr
+2)))) {
1055 php_error_docref(NULL
, E_WARNING
,
1056 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1057 (unsigned) (ptr
- tmp
), tmp
);
1060 state
->buffer
[state
->offset
++] = *ptr
++;
1061 state
->buffer
[state
->offset
++] = *ptr
++;
1062 state
->buffer
[state
->offset
++] = *ptr
;
1065 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1066 case '+': case ',': case ';': case '=': /* sub-delims */
1067 case '-': case '.': case '_': case '~': /* unreserved */
1068 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1069 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1070 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1071 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1072 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1073 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1074 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1075 case 'v': case 'w': case 'x': case 'y': case 'z':
1077 php_error_docref(NULL
, E_WARNING
,
1078 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1079 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1083 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1084 case '7': case '8': case '9':
1087 state
->url
.port
*= 10;
1088 state
->url
.port
+= *ptr
- '0';
1090 state
->buffer
[state
->offset
++] = *ptr
;
1098 php_error_docref(NULL
, E_WARNING
,
1099 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1100 (unsigned char) *ptr
, (unsigned) (ptr
- tmp
), tmp
);
1102 } else if (!(mb
= parse_mb(state
, PARSE_HOSTINFO
, ptr
, end
, tmp
, 0))) {
1107 } while (++ptr
!= end
);
1109 if (!state
->url
.host
) {
1110 len
= (port
? port
- tmp
- 1 : end
- tmp
);
1111 state
->url
.host
= &state
->buffer
[state
->offset
- len
];
1112 state
->buffer
[state
->offset
++] = 0;
1115 if (state
->flags
& PHP_HTTP_URL_PARSE_TOIDN
) {
1116 #ifdef PHP_HTTP_HAVE_IDN
1117 return parse_idn(state
, len
);
1119 #ifdef HAVE_UIDNA_IDNTOASCII
1120 return parse_uidn(state
);
1122 #if 0 && defined(PHP_WIN32)
1123 return parse_widn(state
);
1130 static const char *parse_authority(struct parse_state
*state
)
1132 const char *tmp
= state
->ptr
, *host
= NULL
;
1135 switch (*state
->ptr
) {
1137 /* userinfo delimiter */
1139 php_error_docref(NULL
, E_WARNING
,
1140 "Failed to parse userinfo; unexpected '@'");
1143 host
= state
->ptr
+ 1;
1144 if (tmp
!= state
->ptr
&& SUCCESS
!= parse_userinfo(state
, tmp
)) {
1147 tmp
= state
->ptr
+ 1;
1155 /* host delimiter */
1156 if (tmp
!= state
->ptr
&& SUCCESS
!= parse_hostinfo(state
, tmp
)) {
1161 } while (++state
->ptr
<= state
->end
);
1167 static const char *parse_path(struct parse_state
*state
)
1172 /* is there actually a path to parse? */
1177 state
->url
.path
= &state
->buffer
[state
->offset
];
1180 switch (*state
->ptr
) {
1186 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1187 php_error_docref(NULL
, E_WARNING
,
1188 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1189 (unsigned) (state
->ptr
- tmp
), tmp
);
1192 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1193 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1194 state
->buffer
[state
->offset
++] = *state
->ptr
;
1197 case '/': /* yeah, well */
1198 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1199 case '+': case ',': case ';': case '=': /* sub-delims */
1200 case '-': case '.': case '_': case '~': /* unreserved */
1201 case ':': case '@': /* pchar */
1202 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1203 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1204 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1205 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1206 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1207 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1208 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1209 case 'v': case 'w': case 'x': case 'y': case 'z':
1210 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1211 case '7': case '8': case '9':
1213 state
->buffer
[state
->offset
++] = *state
->ptr
;
1217 if (!(mb
= parse_mb(state
, PARSE_PATH
, state
->ptr
, state
->end
, tmp
, 0))) {
1220 state
->ptr
+= mb
- 1;
1222 } while (++state
->ptr
< state
->end
);
1225 /* did we have any path component ? */
1226 if (tmp
!= state
->ptr
) {
1227 state
->buffer
[state
->offset
++] = 0;
1229 state
->url
.path
= NULL
;
1234 static const char *parse_query(struct parse_state
*state
)
1237 const char *tmp
= state
->ptr
+ !!*state
->ptr
;
1239 /* is there actually a query to parse? */
1240 if (*state
->ptr
!= '?') {
1244 /* skip initial '?' */
1246 state
->url
.query
= &state
->buffer
[state
->offset
];
1249 switch (*state
->ptr
) {
1254 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1255 php_error_docref(NULL
, E_WARNING
,
1256 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1257 (unsigned) (state
->ptr
- tmp
), tmp
);
1260 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1261 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1262 state
->buffer
[state
->offset
++] = *state
->ptr
;
1267 if (state
->flags
& PHP_HTTP_URL_PARSE_TOPCT
) {
1268 state
->buffer
[state
->offset
++] = '%';
1269 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) >> 4];
1270 state
->buffer
[state
->offset
++] = parse_xdigits
[((unsigned char) *state
->ptr
) & 0xf];
1275 case '?': case '/': /* yeah, well */
1276 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1277 case '+': case ',': case ';': case '=': /* sub-delims */
1278 case '-': case '.': case '_': case '~': /* unreserved */
1279 case ':': case '@': /* pchar */
1280 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1281 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1282 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1283 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1284 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1285 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1286 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1287 case 'v': case 'w': case 'x': case 'y': case 'z':
1288 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1289 case '7': case '8': case '9':
1291 state
->buffer
[state
->offset
++] = *state
->ptr
;
1295 if (!(mb
= parse_mb(state
, PARSE_QUERY
, state
->ptr
, state
->end
, tmp
, 0))) {
1298 state
->ptr
+= mb
- 1;
1300 } while (++state
->ptr
< state
->end
);
1303 state
->buffer
[state
->offset
++] = 0;
1307 static const char *parse_fragment(struct parse_state
*state
)
1312 /* is there actually a fragment to parse? */
1313 if (*state
->ptr
!= '#') {
1317 /* skip initial '#' */
1319 state
->url
.fragment
= &state
->buffer
[state
->offset
];
1322 switch (*state
->ptr
) {
1324 if (state
->ptr
[1] != '%' && (state
->end
- state
->ptr
<= 2 || !isxdigit(*(state
->ptr
+1)) || !isxdigit(*(state
->ptr
+2)))) {
1325 php_error_docref(NULL
, E_WARNING
,
1326 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1327 (unsigned) (state
->ptr
- tmp
), tmp
);
1330 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1331 state
->buffer
[state
->offset
++] = *state
->ptr
++;
1332 state
->buffer
[state
->offset
++] = *state
->ptr
;
1336 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1337 case '+': case ',': case ';': case '=': /* sub-delims */
1338 case '-': case '.': case '_': case '~': /* unreserved */
1339 case ':': case '@': /* pchar */
1340 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1341 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1342 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1343 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1344 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1345 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1346 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1347 case 'v': case 'w': case 'x': case 'y': case 'z':
1348 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1349 case '7': case '8': case '9':
1351 state
->buffer
[state
->offset
++] = *state
->ptr
;
1355 if (!(mb
= parse_mb(state
, PARSE_FRAGMENT
, state
->ptr
, state
->end
, tmp
, 0))) {
1358 state
->ptr
+= mb
- 1;
1360 } while (++state
->ptr
< state
->end
);
1362 state
->buffer
[state
->offset
++] = 0;
1366 static const char *parse_hier(struct parse_state
*state
)
1368 if (*state
->ptr
== '/') {
1369 if (state
->end
- state
->ptr
> 1) {
1370 if (*(state
->ptr
+ 1) == '/') {
1372 if (!(state
->ptr
= parse_authority(state
))) {
1378 return parse_path(state
);
1381 static const char *parse_scheme(struct parse_state
*state
)
1384 const char *tmp
= state
->ptr
;
1387 switch (*state
->ptr
) {
1389 /* scheme delimiter */
1390 state
->url
.scheme
= &state
->buffer
[0];
1391 state
->buffer
[state
->offset
++] = 0;
1392 return ++state
->ptr
;
1394 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1395 case '7': case '8': case '9':
1396 case '+': case '-': case '.':
1397 if (state
->ptr
== tmp
) {
1401 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1402 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1403 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1404 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1405 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1406 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1407 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1408 case 'v': case 'w': case 'x': case 'y': case 'z':
1410 state
->buffer
[state
->offset
++] = *state
->ptr
;
1414 if (!(mb
= parse_mb(state
, PARSE_SCHEME
, state
->ptr
, state
->end
, tmp
, 1))) {
1415 /* soft fail; parse path next */
1418 state
->ptr
+= mb
- 1;
1420 } while (++state
->ptr
!= state
->end
);
1422 return state
->ptr
= tmp
;
1425 php_http_url_t
*php_http_url_parse(const char *str
, size_t len
, unsigned flags
)
1427 size_t maxlen
= 3 * len
;
1428 struct parse_state
*state
= ecalloc(1, sizeof(*state
) + maxlen
);
1430 state
->end
= str
+ len
;
1432 state
->flags
= flags
;
1433 state
->maxlen
= maxlen
;
1435 if (!parse_scheme(state
)) {
1436 php_error_docref(NULL
, E_WARNING
, "Failed to parse URL scheme: '%s'", state
->ptr
);
1441 if (!parse_hier(state
)) {
1446 if (!parse_query(state
)) {
1447 php_error_docref(NULL
, E_WARNING
, "Failed to parse URL query: '%s'", state
->ptr
);
1452 if (!parse_fragment(state
)) {
1453 php_error_docref(NULL
, E_WARNING
, "Failed to parse URL fragment: '%s'", state
->ptr
);
1458 return (php_http_url_t
*) state
;
1461 php_http_url_t
*php_http_url_parse_authority(const char *str
, size_t len
, unsigned flags
)
1463 size_t maxlen
= 3 * len
;
1464 struct parse_state
*state
= ecalloc(1, sizeof(*state
) + maxlen
);
1466 state
->end
= str
+ len
;
1468 state
->flags
= flags
;
1469 state
->maxlen
= maxlen
;
1470 TSRMLS_SET_CTX(state
->ts
);
1472 if (!(state
->ptr
= parse_authority(state
))) {
1477 if (state
->ptr
!= state
->end
) {
1478 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1479 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1480 (unsigned) (state
->ptr
- str
), str
);
1485 return (php_http_url_t
*) state
;
1488 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct
, 0, 0, 0)
1489 ZEND_ARG_INFO(0, old_url
)
1490 ZEND_ARG_INFO(0, new_url
)
1491 ZEND_ARG_INFO(0, flags
)
1492 ZEND_END_ARG_INFO();
1493 PHP_METHOD(HttpUrl
, __construct
)
1495 zval
*new_url
= NULL
, *old_url
= NULL
;
1496 zend_long flags
= PHP_HTTP_URL_FROM_ENV
;
1497 zend_error_handling zeh
;
1499 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url
, &new_url
, &flags
), invalid_arg
, return);
1501 zend_replace_error_handling(EH_THROW
, php_http_exception_bad_url_class_entry
, &zeh
);
1503 php_http_url_t
*res_purl
, *new_purl
= NULL
, *old_purl
= NULL
;
1506 new_purl
= php_http_url_from_zval(new_url
, flags
);
1508 zend_restore_error_handling(&zeh
);
1513 old_purl
= php_http_url_from_zval(old_url
, flags
);
1516 php_http_url_free(&new_purl
);
1518 zend_restore_error_handling(&zeh
);
1523 res_purl
= php_http_url_mod(old_purl
, new_purl
, flags
);
1524 php_http_url_to_struct(res_purl
, getThis());
1526 php_http_url_free(&res_purl
);
1528 php_http_url_free(&old_purl
);
1531 php_http_url_free(&new_purl
);
1534 zend_restore_error_handling(&zeh
);
1537 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod
, 0, 0, 1)
1538 ZEND_ARG_INFO(0, more_url_parts
)
1539 ZEND_ARG_INFO(0, flags
)
1540 ZEND_END_ARG_INFO();
1541 PHP_METHOD(HttpUrl
, mod
)
1543 zval
*new_url
= NULL
;
1544 zend_long flags
= PHP_HTTP_URL_JOIN_PATH
| PHP_HTTP_URL_JOIN_QUERY
;
1545 zend_error_handling zeh
;
1547 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url
, &flags
), invalid_arg
, return);
1549 zend_replace_error_handling(EH_THROW
, php_http_exception_bad_url_class_entry
, &zeh
);
1551 php_http_url_t
*new_purl
= NULL
, *old_purl
= NULL
;
1554 new_purl
= php_http_url_from_zval(new_url
, flags
);
1556 zend_restore_error_handling(&zeh
);
1561 if ((old_purl
= php_http_url_from_struct(HASH_OF(getThis())))) {
1562 php_http_url_t
*res_purl
;
1564 ZVAL_OBJ(return_value
, zend_objects_clone_obj(getThis()));
1566 res_purl
= php_http_url_mod(old_purl
, new_purl
, flags
);
1567 php_http_url_to_struct(res_purl
, return_value
);
1569 php_http_url_free(&res_purl
);
1570 php_http_url_free(&old_purl
);
1573 php_http_url_free(&new_purl
);
1576 zend_restore_error_handling(&zeh
);
1579 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString
, 0, 0, 0)
1580 ZEND_END_ARG_INFO();
1581 PHP_METHOD(HttpUrl
, toString
)
1583 if (SUCCESS
== zend_parse_parameters_none()) {
1584 php_http_url_t
*purl
;
1586 if ((purl
= php_http_url_from_struct(HASH_OF(getThis())))) {
1590 php_http_url_to_string(purl
, &str
, &len
, 0);
1591 php_http_url_free(&purl
);
1592 RETURN_STR(php_http_cs2zs(str
, len
));
1595 RETURN_EMPTY_STRING();
1598 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray
, 0, 0, 0)
1599 ZEND_END_ARG_INFO();
1600 PHP_METHOD(HttpUrl
, toArray
)
1602 php_http_url_t
*purl
;
1604 if (SUCCESS
!= zend_parse_parameters_none()) {
1608 /* strip any non-URL properties */
1609 purl
= php_http_url_from_struct(HASH_OF(getThis()));
1610 php_http_url_to_struct(purl
, return_value
);
1611 php_http_url_free(&purl
);
1614 static zend_function_entry php_http_url_methods
[] = {
1615 PHP_ME(HttpUrl
, __construct
, ai_HttpUrl___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1616 PHP_ME(HttpUrl
, mod
, ai_HttpUrl_mod
, ZEND_ACC_PUBLIC
)
1617 PHP_ME(HttpUrl
, toString
, ai_HttpUrl_toString
, ZEND_ACC_PUBLIC
)
1618 ZEND_MALIAS(HttpUrl
, __toString
, toString
, ai_HttpUrl_toString
, ZEND_ACC_PUBLIC
)
1619 PHP_ME(HttpUrl
, toArray
, ai_HttpUrl_toArray
, ZEND_ACC_PUBLIC
)
1620 EMPTY_FUNCTION_ENTRY
1623 zend_class_entry
*php_http_url_class_entry
;
1625 PHP_MINIT_FUNCTION(http_url
)
1627 zend_class_entry ce
= {0};
1629 INIT_NS_CLASS_ENTRY(ce
, "http", "Url", php_http_url_methods
);
1630 php_http_url_class_entry
= zend_register_internal_class(&ce
);
1632 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC
);
1633 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
1634 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
1635 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
1636 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
1637 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("path"), ZEND_ACC_PUBLIC
);
1638 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("query"), ZEND_ACC_PUBLIC
);
1639 zend_declare_property_null(php_http_url_class_entry
, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC
);
1641 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE
);
1642 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH
);
1643 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY
);
1644 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER
);
1645 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS
);
1646 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH
);
1647 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT
);
1648 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH
);
1649 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY
);
1650 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT
);
1651 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL
);
1652 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV
);
1653 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH
);
1655 #ifdef PHP_HTTP_HAVE_WCHAR
1656 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC
);
1658 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8
);
1659 #if defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
1660 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN
);
1662 zend_declare_class_constant_long(php_http_url_class_entry
, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT
);
1673 * vim600: noet sw=4 ts=4 fdm=marker
1674 * vim<600: noet sw=4 ts=4