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