clean up test
[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_LIBIDN2
16 # include <idn2.h>
17 #endif
18 #if PHP_HTTP_HAVE_LIBIDN
19 # include <idna.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 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 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 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)
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 (!silent) {
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 return 0;
783 }
784
785 static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *ptr)
786 {
787 size_t mb;
788 const char *password = NULL, *end = state->ptr, *tmp = ptr;
789
790 state->url.user = &state->buffer[state->offset];
791
792 do {
793 switch (*ptr) {
794 case ':':
795 if (password) {
796 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
797 php_error_docref(NULL, E_WARNING,
798 "Failed to parse password; duplicate ':' at pos %u in '%s'",
799 (unsigned) (ptr - tmp), tmp);
800 }
801 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
802 return FAILURE;
803 }
804 state->buffer[state->offset++] = *ptr;
805 break;
806 }
807 password = ptr + 1;
808 state->buffer[state->offset++] = 0;
809 state->url.pass = &state->buffer[state->offset];
810 break;
811
812 case '%':
813 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
814 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
815 php_error_docref(NULL, E_WARNING,
816 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
817 (unsigned) (ptr - tmp), tmp);
818 }
819 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
820 return FAILURE;
821 }
822 state->buffer[state->offset++] = *ptr++;
823 break;
824 }
825 state->buffer[state->offset++] = *ptr++;
826 state->buffer[state->offset++] = *ptr++;
827 state->buffer[state->offset++] = *ptr;
828 break;
829
830 default:
831 if ((mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) {
832 ptr += mb - 1;
833 break;
834 }
835 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
836 return FAILURE;
837 }
838 /* no break */
839 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
840 case '+': case ',': case ';': case '=': /* sub-delims */
841 case '-': case '.': case '_': case '~': /* unreserved */
842 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
843 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
844 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
845 case 'V': case 'W': case 'X': case 'Y': case 'Z':
846 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
847 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
848 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
849 case 'v': case 'w': case 'x': case 'y': case 'z':
850 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
851 case '7': case '8': case '9':
852 /* allowed */
853 state->buffer[state->offset++] = *ptr;
854 break;
855
856 }
857 } while(++ptr < end);
858
859
860 state->buffer[state->offset++] = 0;
861
862 return SUCCESS;
863 }
864
865 #if PHP_WIN32 || HAVE_UIDNA_IDNTOASCII
866 typedef size_t (*parse_mb_func)(unsigned *wc, const char *ptr, const char *end);
867 static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len)
868 {
869 size_t offset = 0, u8_len = strlen(u8);
870
871 *u16 = ecalloc(4 * sizeof(uint16_t), u8_len + 1);
872 *len = 0;
873
874 while (offset < u8_len) {
875 unsigned wc;
876 uint16_t buf[2], *ptr = buf;
877 size_t consumed = fn(&wc, &u8[offset], &u8[u8_len]);
878
879 if (!consumed) {
880 efree(*u16);
881 php_error_docref(NULL, E_WARNING, "Failed to parse UTF-8 at pos %zu of '%s'", offset, u8);
882 return FAILURE;
883 } else {
884 offset += consumed;
885 }
886
887 switch (wctoutf16(buf, wc)) {
888 case 2:
889 (*u16)[(*len)++] = *ptr++;
890 /* no break */
891 case 1:
892 (*u16)[(*len)++] = *ptr++;
893 break;
894 case 0:
895 default:
896 efree(*u16);
897 php_error_docref(NULL, E_WARNING, "Failed to convert UTF-32 'U+%X' to UTF-16", wc);
898 return FAILURE;
899 }
900 }
901
902 return SUCCESS;
903 }
904 #endif
905
906 #if PHP_HTTP_HAVE_LIBIDN2
907 # if __GNUC__
908 __attribute__ ((unused))
909 # endif
910 static ZEND_RESULT_CODE parse_gidn_2008(struct parse_state *state, size_t prev_len)
911 {
912 char *idn = NULL;
913 int rv = -1;
914
915 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
916 rv = idn2_lookup_u8((const unsigned char *) state->url.host, (unsigned char **) &idn, IDN2_NFC_INPUT);
917 }
918 # if PHP_HTTP_HAVE_WCHAR
919 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
920 rv = idn2_lookup_ul(state->url.host, &idn, 0);
921 }
922 # endif
923 if (rv != IDN2_OK) {
924 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
925 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (IDNA2008); %s", idn2_strerror(rv));
926 }
927 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
928 return FAILURE;
929 }
930 } else {
931 size_t idnlen = strlen(idn);
932 memcpy(state->url.host, idn, idnlen + 1);
933 free(idn);
934 state->offset += idnlen - prev_len;
935 }
936 return SUCCESS;
937 }
938 #endif
939
940 #if PHP_HTTP_HAVE_LIBIDN
941 # if __GNUC__
942 __attribute__ ((unused))
943 # endif
944 static ZEND_RESULT_CODE parse_gidn_2003(struct parse_state *state, size_t prev_len)
945 {
946 char *idn = NULL;
947 int rv = -1;
948
949 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
950 rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED);
951 }
952 # if PHP_HTTP_HAVE_WCHAR
953 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
954 rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED);
955 }
956 # endif
957 if (rv != IDNA_SUCCESS) {
958 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
959 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (IDNA2003); %s", idna_strerror(rv));
960 }
961 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
962 return FAILURE;
963 }
964 } else {
965 size_t idnlen = strlen(idn);
966 memcpy(state->url.host, idn, idnlen + 1);
967 free(idn);
968 state->offset += idnlen - prev_len;
969 }
970 return SUCCESS;
971 }
972 #endif
973
974 #if HAVE_UIDNA_IDNTOASCII
975 # if !PHP_HTTP_HAVE_LIBICU
976 typedef uint16_t UChar;
977 typedef enum { U_ZERO_ERROR = 0 } UErrorCode;
978 int32_t uidna_IDNToASCII(const UChar *src, int32_t srcLength, UChar *dest, int32_t destCapacity, int32_t options, void *parseError, UErrorCode *status);
979 # endif
980 static ZEND_RESULT_CODE parse_uidn_2003(struct parse_state *state)
981 {
982 char *host_ptr = state->url.host, ebuf[64] = {0}, *error = NULL;
983 uint16_t *uhost_str, ahost_str[256], *ahost_ptr;
984 size_t uhost_len, ahost_len;
985 UErrorCode rc = U_ZERO_ERROR;
986
987 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
988 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
989 error = "failed to convert to UTF-16";
990 goto error;
991 }
992 #if PHP_HTTP_HAVE_WCHAR
993 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
994 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
995 error = "failed to convert to UTF-16";
996 goto error;
997 }
998 #endif
999 } else {
1000 error = "codepage not specified";
1001 goto error;
1002 }
1003
1004 # if __GNUC__ >= 5
1005 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1006 # endif
1007 ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, 256, 3, NULL, &rc);
1008 # if __GNUC__ >= 5
1009 # pragma GCC diagnostic pop
1010 # endif
1011
1012 efree(uhost_str);
1013 if (error > U_ZERO_ERROR) {
1014 goto error;
1015 }
1016
1017 ahost_ptr = ahost_str;
1018 PHP_HTTP_DUFF(ahost_len, *host_ptr++ = *ahost_ptr++);
1019 *host_ptr = '\0';
1020 state->offset += host_ptr - state->url.host;
1021
1022 return SUCCESS;
1023
1024 error:
1025 if (!error) {
1026 slprintf(ebuf, sizeof(ebuf)-1, "errorcode: %d", rc);
1027 error = ebuf;
1028 }
1029 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (ICU IDNA2003); %s", error);
1030
1031 return FAILURE;
1032 }
1033 #endif
1034
1035 #if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
1036 static ZEND_RESULT_CODE parse_uidn_2008(struct parse_state *state)
1037 {
1038 char *host_ptr, *error = NULL, ebuf[64] = {0};
1039 UErrorCode rc = U_ZERO_ERROR;
1040 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
1041 UIDNA *uidna = uidna_openUTS46(UIDNA_ALLOW_UNASSIGNED, &rc);
1042
1043 if (!uidna || U_FAILURE(rc)) {
1044 return FAILURE;
1045 }
1046
1047 host_ptr = state->url.host;
1048
1049 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
1050 char ahost_str[256], *ahost_ptr = &ahost_str[0];
1051 size_t ahost_len = uidna_nameToASCII_UTF8(uidna, host_ptr, -1, ahost_str, sizeof(ahost_str)-1, &info, &rc);
1052
1053 if (U_FAILURE(rc) || info.errors) {
1054 goto error;
1055 }
1056 PHP_HTTP_DUFF(ahost_len, *host_ptr++ = *ahost_ptr++);
1057 #if PHP_HTTP_HAVE_WCHAR
1058 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1059 uint16_t *uhost_str, whost_str[256], *whost_ptr = &whost_str[0];
1060 size_t uhost_len, whost_len;
1061
1062 if (SUCCESS != to_utf16(parse_mb_loc, host_ptr, &uhost_str, &uhost_len)) {
1063 error = "could not convert to UTF-16";
1064 goto error;
1065 }
1066
1067 whost_len = uidna_nameToASCII(uidna, uhost_str, uhost_len, whost_str, sizeof(whost_str)-1, &info, &rc);
1068 whost_ptr = whost_str;
1069 efree(uhost_str);
1070 if (U_FAILURE(rc) || info.errors) {
1071 goto error;
1072 }
1073 PHP_HTTP_DUFF(whost_len, *host_ptr++ = *whost_ptr++);
1074 #endif
1075 } else {
1076 error = "codepage not specified";
1077 goto error;
1078 }
1079
1080 *host_ptr = '\0';
1081 state->offset += host_ptr - state->url.host;
1082
1083 uidna_close(uidna);
1084 return SUCCESS;
1085
1086 error:
1087 if (!error) {
1088 if (U_FAILURE(rc)) {
1089 slprintf(ebuf, sizeof(ebuf)-1, "%s", u_errorName(rc));
1090 error = ebuf;
1091 } else if (info.errors) {
1092 slprintf(ebuf, sizeof(ebuf)-1, "ICU IDNA error codes: 0x%x", info.errors);
1093 error = ebuf;
1094 } else {
1095 error = "unknown error";
1096 }
1097 }
1098 php_error_docref(NULL, E_WARNING, "Failed to parse IDN (ICU IDNA2008); %s", error);
1099
1100 uidna_close(uidna);
1101 return FAILURE;
1102 }
1103 #endif
1104
1105 #if PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2
1106 # if __GNUC__
1107 __attribute__ ((unused))
1108 # endif
1109 static ZEND_RESULT_CODE parse_kidn(struct parse_state *state)
1110 {
1111 idn_result_t rc;
1112 #if PHP_HTTP_HAVE_LIBIDNKIT
1113 int actions = IDN_DELIMMAP|IDN_LOCALMAP|IDN_NAMEPREP|IDN_IDNCONV|IDN_LENCHECK;
1114 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1115 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;
1116 #endif
1117 char ahost_str[256] = {0}, *ahost_ptr = &ahost_str[0], *host_ptr = state->url.host;
1118
1119 if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1120 #if PHP_HTTP_HAVE_LIBIDNKIT
1121 actions |= IDN_LOCALCONV;
1122 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1123 actions |= IDN_UNICODECONV;
1124 #endif
1125 }
1126
1127 rc = idn_encodename(actions, state->url.host, ahost_str, 256);
1128 if (rc == idn_success) {
1129 PHP_HTTP_DUFF(strlen(ahost_str), *host_ptr++ = *ahost_ptr++);
1130
1131 *host_ptr = '\0';
1132 state->offset += host_ptr - state->url.host;
1133
1134 return SUCCESS;
1135 } else {
1136 php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idn_result_tostring(rc));
1137 return FAILURE;
1138 }
1139 }
1140 #endif
1141
1142 #if 0 && PHP_WIN32
1143 static ZEND_RESULT_CODE parse_widn_2003(struct parse_state *state)
1144 {
1145 char *host_ptr;
1146 uint16_t *uhost_str, ahost_str[256], *ahost_ptr;
1147 size_t uhost_len;
1148
1149 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
1150 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
1151 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1152 return FAILURE;
1153 }
1154 #if PHP_HTTP_HAVE_WCHAR
1155 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
1156 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
1157 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1158 return FAILURE;
1159 }
1160 #endif
1161 } else {
1162 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1163 return FAILURE;
1164 }
1165
1166 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED, uhost_str, uhost_len, ahost_str, 256)) {
1167 efree(uhost_str);
1168 php_error_docref(NULL, E_WARNING, "Failed to parse IDN");
1169 return FAILURE;
1170 }
1171
1172 efree(uhost_str);
1173 host_ptr = state->url.host;
1174 ahost_ptr = ahost_str;
1175 PHP_HTTP_DUFF(wcslen(ahost_str), *host_ptr++ = *ahost_ptr++);
1176
1177 *host_ptr = '\0';
1178 state->offset += host_ptr - state->url.host;
1179
1180 return SUCCESS;
1181 }
1182 #endif
1183
1184 static ZEND_RESULT_CODE parse_idna(struct parse_state *state, size_t len)
1185 {
1186 #if PHP_HTTP_HAVE_IDNA2008
1187 if ((state->flags & PHP_HTTP_URL_PARSE_TOIDN_2008) == PHP_HTTP_URL_PARSE_TOIDN_2008
1188 # if PHP_HTTP_HAVE_IDNA2003
1189 || (state->flags & PHP_HTTP_URL_PARSE_TOIDN_2003) != PHP_HTTP_URL_PARSE_TOIDN_2003
1190 # endif
1191 ) {
1192 #if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
1193 return parse_uidn_2008(state);
1194 #elif PHP_HTTP_HAVE_LIBIDN2
1195 return parse_gidn_2008(state, len);
1196 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1197 return parse_kidn(state);
1198 #endif
1199 }
1200 #endif
1201
1202 #if PHP_HTTP_HAVE_IDNA2003
1203 if ((state->flags & PHP_HTTP_URL_PARSE_TOIDN_2003) == PHP_HTTP_URL_PARSE_TOIDN_2003
1204 # if PHP_HTTP_HAVE_IDNA2008
1205 || (state->flags & PHP_HTTP_URL_PARSE_TOIDN_2008) != PHP_HTTP_URL_PARSE_TOIDN_2008
1206 #endif
1207 ) {
1208 #if HAVE_UIDNA_IDNTOASCII
1209 return parse_uidn_2003(state);
1210 #elif PHP_HTTP_HAVE_LIBIDN
1211 return parse_gidn_2003(state, len);
1212 #elif PHP_HTTP_HAVE_LIBIDNKIT
1213 return parse_kidn(state);
1214 #endif
1215 }
1216 #endif
1217
1218 #if 0 && PHP_WIN32
1219 return parse_widn_2003(state);
1220 #endif
1221
1222 #if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
1223 return parse_uidn_2008(state);
1224 #elif PHP_HTTP_HAVE_LIBIDN2
1225 return parse_gidn_2008(state, len);
1226 #elif PHP_HTTP_HAVE_LIBIDNKIT2
1227 return parse_kidn(state);
1228 #elif HAVE_UIDNA_IDNTOASCII
1229 return parse_uidn_2003(state);
1230 #elif PHP_HTTP_HAVE_LIBIDN
1231 return parse_gidn_2003(state, len);
1232 #elif PHP_HTTP_HAVE_LIBIDNKIT
1233 return parse_kidn(state);
1234 #endif
1235
1236 return SUCCESS;
1237 }
1238
1239 #if HAVE_INET_PTON
1240 static const char *parse_ip6(struct parse_state *state, const char *ptr)
1241 {
1242 unsigned pos = 0;
1243 const char *error = NULL, *end = state->ptr, *tmp = memchr(ptr, ']', end - ptr);
1244
1245 if (tmp) {
1246 size_t addrlen = tmp - ptr + 1;
1247 char buf[16], *addr = estrndup(ptr + 1, addrlen - 2);
1248 int rv = inet_pton(AF_INET6, addr, buf);
1249
1250 if (rv == 1) {
1251 state->buffer[state->offset] = '[';
1252 state->url.host = &state->buffer[state->offset];
1253 inet_ntop(AF_INET6, buf, state->url.host + 1, state->maxlen - state->offset);
1254 state->offset += strlen(state->url.host);
1255 state->buffer[state->offset++] = ']';
1256 state->buffer[state->offset++] = 0;
1257 ptr = tmp + 1;
1258 } else if (rv == -1) {
1259 pos = 1;
1260 error = strerror(errno);
1261 } else {
1262 error = "unexpected '['";
1263 }
1264 efree(addr);
1265 } else {
1266 pos = end - ptr;
1267 error = "expected ']'";
1268 }
1269
1270 if (error) {
1271 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1272 php_error_docref(NULL, E_WARNING, "Failed to parse hostinfo; %s at pos %u in '%s'", error, pos, ptr);
1273 }
1274 return NULL;
1275 }
1276
1277 return ptr;
1278 }
1279 #endif
1280
1281 static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *ptr)
1282 {
1283 size_t mb, len = state->offset;
1284 const char *end = state->ptr, *tmp = ptr, *port = NULL, *label = NULL;
1285
1286 #if HAVE_INET_PTON
1287 if (*ptr == '[' && !(ptr = parse_ip6(state, ptr))) {
1288 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1289 return FAILURE;
1290 }
1291 ptr = tmp;
1292 }
1293 #endif
1294
1295 if (ptr != end) do {
1296 switch (*ptr) {
1297 case ':':
1298 if (port) {
1299 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1300 php_error_docref(NULL, E_WARNING,
1301 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1302 (unsigned) (ptr - tmp), tmp);
1303 }
1304 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1305 return FAILURE;
1306 }
1307 }
1308 port = ptr + 1;
1309 break;
1310
1311 case '%':
1312 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
1313 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1314 php_error_docref(NULL, E_WARNING,
1315 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1316 (unsigned) (ptr - tmp), tmp);
1317 }
1318 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1319 return FAILURE;
1320 }
1321 state->buffer[state->offset++] = *ptr++;
1322 break;
1323 }
1324 state->buffer[state->offset++] = *ptr++;
1325 state->buffer[state->offset++] = *ptr++;
1326 state->buffer[state->offset++] = *ptr;
1327 break;
1328
1329 case '.':
1330 if (port || !label) {
1331 /* sort of a compromise, just ensure we don't end up
1332 * with a dot at the beginning or two consecutive dots
1333 */
1334 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1335 php_error_docref(NULL, E_WARNING,
1336 "Failed to parse %s; unexpected '%c' at pos %u in '%s'",
1337 port ? "port" : "host",
1338 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1339 }
1340 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1341 return FAILURE;
1342 }
1343 break;
1344 }
1345 state->buffer[state->offset++] = *ptr;
1346 label = NULL;
1347 break;
1348
1349 case '-':
1350 if (!label) {
1351 /* sort of a compromise, just ensure we don't end up
1352 * with a hyphen at the beginning
1353 */
1354 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1355 php_error_docref(NULL, E_WARNING,
1356 "Failed to parse %s; unexpected '%c' at pos %u in '%s'",
1357 port ? "port" : "host",
1358 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1359 }
1360 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1361 return FAILURE;
1362 }
1363 break;
1364 }
1365 /* no break */
1366 case '_': case '~': /* unreserved */
1367 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1368 case '+': case ',': case ';': case '=': /* sub-delims */
1369 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1370 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1371 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1372 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1373 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1374 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1375 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1376 case 'v': case 'w': case 'x': case 'y': case 'z':
1377 if (port) {
1378 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1379 php_error_docref(NULL, E_WARNING,
1380 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1381 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1382 }
1383 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1384 return FAILURE;
1385 }
1386 break;
1387 }
1388 /* no break */
1389 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1390 case '7': case '8': case '9':
1391 /* allowed */
1392 if (port) {
1393 state->url.port *= 10;
1394 state->url.port += *ptr - '0';
1395 } else {
1396 label = ptr;
1397 state->buffer[state->offset++] = *ptr;
1398 }
1399 break;
1400
1401 default:
1402 if (ptr == end) {
1403 break;
1404 } else if (port) {
1405 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1406 php_error_docref(NULL, E_WARNING,
1407 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1408 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1409 }
1410 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1411 return FAILURE;
1412 }
1413 break;
1414 } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) {
1415 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1416 return FAILURE;
1417 }
1418 break;
1419 }
1420 label = ptr;
1421 ptr += mb - 1;
1422 }
1423 } while (++ptr < end);
1424
1425 if (!state->url.host) {
1426 len = state->offset - len;
1427 state->url.host = &state->buffer[state->offset - len];
1428 state->buffer[state->offset++] = 0;
1429 }
1430
1431 if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
1432 return parse_idna(state, len);
1433 }
1434
1435 return SUCCESS;
1436 }
1437
1438 static const char *parse_authority(struct parse_state *state)
1439 {
1440 const char *tmp = state->ptr, *host = NULL;
1441
1442 do {
1443 switch (*state->ptr) {
1444 case '@':
1445 /* userinfo delimiter */
1446 if (host) {
1447 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1448 php_error_docref(NULL, E_WARNING,
1449 "Failed to parse userinfo; unexpected '@'");
1450 }
1451 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1452 return NULL;
1453 }
1454 break;
1455 }
1456 host = state->ptr + 1;
1457 if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) {
1458 return NULL;
1459 }
1460 tmp = state->ptr + 1;
1461 break;
1462
1463 case '/':
1464 case '?':
1465 case '#':
1466 case '\0':
1467 EOD:
1468 /* host delimiter */
1469 if (tmp != state->ptr && SUCCESS != parse_hostinfo(state, tmp)) {
1470 return NULL;
1471 }
1472 return state->ptr;
1473 }
1474 } while (++state->ptr <= state->end);
1475
1476 --state->ptr;
1477 goto EOD;
1478 }
1479
1480 static const char *parse_path(struct parse_state *state)
1481 {
1482 size_t mb;
1483 const char *tmp;
1484
1485 /* is there actually a path to parse? */
1486 if (!*state->ptr) {
1487 return state->ptr;
1488 }
1489 tmp = state->ptr;
1490 state->url.path = &state->buffer[state->offset];
1491
1492 do {
1493 switch (*state->ptr) {
1494 case '#':
1495 case '?':
1496 goto done;
1497
1498 case '%':
1499 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1500 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1501 php_error_docref(NULL, E_WARNING,
1502 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1503 (unsigned) (state->ptr - tmp), tmp);
1504 }
1505 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1506 return NULL;
1507 }
1508 state->buffer[state->offset++] = *state->ptr;
1509 break;
1510 }
1511 state->buffer[state->offset++] = *state->ptr++;
1512 state->buffer[state->offset++] = *state->ptr++;
1513 state->buffer[state->offset++] = *state->ptr;
1514 break;
1515
1516 case '/': /* yeah, well */
1517 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1518 case '+': case ',': case ';': case '=': /* sub-delims */
1519 case '-': case '.': case '_': case '~': /* unreserved */
1520 case ':': case '@': /* pchar */
1521 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1522 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1523 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1524 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1525 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1526 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1527 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1528 case 'v': case 'w': case 'x': case 'y': case 'z':
1529 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1530 case '7': case '8': case '9':
1531 /* allowed */
1532 state->buffer[state->offset++] = *state->ptr;
1533 break;
1534
1535 default:
1536 if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) {
1537 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1538 return NULL;
1539 }
1540 break;
1541 }
1542 state->ptr += mb - 1;
1543 }
1544 } while (++state->ptr < state->end);
1545
1546 done:
1547 /* did we have any path component ? */
1548 if (tmp != state->ptr) {
1549 state->buffer[state->offset++] = 0;
1550 } else {
1551 state->url.path = NULL;
1552 }
1553 return state->ptr;
1554 }
1555
1556 static const char *parse_query(struct parse_state *state)
1557 {
1558 size_t mb;
1559 const char *tmp = state->ptr + !!*state->ptr;
1560
1561 /* is there actually a query to parse? */
1562 if (*state->ptr != '?') {
1563 return state->ptr;
1564 }
1565
1566 /* skip initial '?' */
1567 tmp = ++state->ptr;
1568 state->url.query = &state->buffer[state->offset];
1569
1570 while (state->ptr < state->end) {
1571 switch (*state->ptr) {
1572 case '#':
1573 goto done;
1574
1575 case '%':
1576 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1577 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1578 php_error_docref(NULL, E_WARNING,
1579 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1580 (unsigned) (state->ptr - tmp), tmp);
1581 }
1582 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1583 return NULL;
1584 }
1585 /* fallthrough, pct-encode the percent sign */
1586 } else {
1587 state->buffer[state->offset++] = *state->ptr++;
1588 state->buffer[state->offset++] = *state->ptr++;
1589 state->buffer[state->offset++] = *state->ptr;
1590 break;
1591 }
1592 /* no break */
1593 case '{': case '}':
1594 case '<': case '>':
1595 case '[': case ']':
1596 case '|': case '\\': case '^': case '`': case '"': case ' ':
1597 /* RFC1738 unsafe */
1598 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1599 state->buffer[state->offset++] = '%';
1600 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1601 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1602 break;
1603 }
1604 /* no break */
1605
1606 case '?': case '/': /* yeah, well */
1607 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1608 case '+': case ',': case ';': case '=': /* sub-delims */
1609 case '-': case '.': case '_': case '~': /* unreserved */
1610 case ':': case '@': /* pchar */
1611 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1612 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1613 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1614 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1615 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1616 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1617 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1618 case 'v': case 'w': case 'x': case 'y': case 'z':
1619 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1620 case '7': case '8': case '9':
1621 /* allowed */
1622 state->buffer[state->offset++] = *state->ptr;
1623 break;
1624
1625 default:
1626 if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) {
1627 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1628 return NULL;
1629 }
1630 break;
1631 }
1632 state->ptr += mb - 1;
1633 }
1634
1635 ++state->ptr;
1636 }
1637
1638 done:
1639 state->buffer[state->offset++] = 0;
1640 return state->ptr;
1641 }
1642
1643 static const char *parse_fragment(struct parse_state *state)
1644 {
1645 size_t mb;
1646 const char *tmp;
1647
1648 /* is there actually a fragment to parse? */
1649 if (*state->ptr != '#') {
1650 return state->ptr;
1651 }
1652
1653 /* skip initial '#' */
1654 tmp = ++state->ptr;
1655 state->url.fragment = &state->buffer[state->offset];
1656
1657 do {
1658 switch (*state->ptr) {
1659 case '#':
1660 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1661 php_error_docref(NULL, E_WARNING,
1662 "Failed to parse fragment; invalid fragment identifier at pos %u in '%s'",
1663 (unsigned) (state->ptr - tmp), tmp);
1664 }
1665 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1666 return NULL;
1667 }
1668 state->buffer[state->offset++] = *state->ptr;
1669 break;
1670
1671 case '%':
1672 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1673 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1674 php_error_docref(NULL, E_WARNING,
1675 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1676 (unsigned) (state->ptr - tmp), tmp);
1677 }
1678 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1679 return NULL;
1680 }
1681 /* fallthrough */
1682 } else {
1683 state->buffer[state->offset++] = *state->ptr++;
1684 state->buffer[state->offset++] = *state->ptr++;
1685 state->buffer[state->offset++] = *state->ptr;
1686 break;
1687 }
1688 /* no break */
1689
1690 case '{': case '}':
1691 case '<': case '>':
1692 case '[': case ']':
1693 case '|': case '\\': case '^': case '`': case '"': case ' ':
1694 /* RFC1738 unsafe */
1695 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1696 state->buffer[state->offset++] = '%';
1697 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1698 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1699 break;
1700 }
1701 /* no break */
1702
1703 case '?': case '/':
1704 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1705 case '+': case ',': case ';': case '=': /* sub-delims */
1706 case '-': case '.': case '_': case '~': /* unreserved */
1707 case ':': case '@': /* pchar */
1708 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1709 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1710 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1711 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1712 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1713 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1714 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1715 case 'v': case 'w': case 'x': case 'y': case 'z':
1716 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1717 case '7': case '8': case '9':
1718 /* allowed */
1719 state->buffer[state->offset++] = *state->ptr;
1720 break;
1721
1722 default:
1723 if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) {
1724 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1725 return NULL;
1726 }
1727 break;
1728 }
1729 state->ptr += mb - 1;
1730 }
1731 } while (++state->ptr < state->end);
1732
1733 state->buffer[state->offset++] = 0;
1734 return state->ptr;
1735 }
1736
1737 static const char *parse_hier(struct parse_state *state)
1738 {
1739 if (*state->ptr == '/') {
1740 if (state->end - state->ptr > 1) {
1741 if (*(state->ptr + 1) == '/') {
1742 state->ptr += 2;
1743 if (!(state->ptr = parse_authority(state))) {
1744 return NULL;
1745 }
1746 }
1747 }
1748 }
1749 return parse_path(state);
1750 }
1751
1752 static const char *parse_scheme(struct parse_state *state)
1753 {
1754 size_t mb;
1755 const char *tmp = state->ptr;
1756
1757 do {
1758 switch (*state->ptr) {
1759 case ':':
1760 /* scheme delimiter */
1761 state->url.scheme = &state->buffer[0];
1762 state->buffer[state->offset++] = 0;
1763 return ++state->ptr;
1764
1765 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1766 case '7': case '8': case '9':
1767 case '+': case '-': case '.':
1768 if (state->ptr == tmp) {
1769 goto softfail;
1770 }
1771 /* no break */
1772 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1773 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1774 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1775 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1776 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1777 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1778 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1779 case 'v': case 'w': case 'x': case 'y': case 'z':
1780 /* scheme part */
1781 state->buffer[state->offset++] = *state->ptr;
1782 break;
1783
1784 default:
1785 if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
1786 goto softfail;
1787 }
1788 state->ptr += mb - 1;
1789 }
1790 } while (++state->ptr < state->end);
1791
1792 softfail:
1793 state->offset = 0;
1794 return state->ptr = tmp;
1795 }
1796
1797 php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags)
1798 {
1799 size_t maxlen = 3 * len + 8 /* null bytes for all components */;
1800 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1801
1802 state->end = str + len;
1803 state->ptr = str;
1804 state->flags = flags;
1805 state->maxlen = maxlen;
1806
1807 if (!parse_scheme(state)) {
1808 php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
1809 efree(state);
1810 return NULL;
1811 }
1812
1813 if (!parse_hier(state)) {
1814 efree(state);
1815 return NULL;
1816 }
1817
1818 if (!parse_query(state)) {
1819 php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
1820 efree(state);
1821 return NULL;
1822 }
1823
1824 if (!parse_fragment(state)) {
1825 php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
1826 efree(state);
1827 return NULL;
1828 }
1829
1830 return (php_http_url_t *) state;
1831 }
1832
1833 php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags)
1834 {
1835 size_t maxlen = 3 * len;
1836 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1837
1838 state->end = str + len;
1839 state->ptr = str;
1840 state->flags = flags;
1841 state->maxlen = maxlen;
1842
1843 if (!(state->ptr = parse_authority(state))) {
1844 efree(state);
1845 return NULL;
1846 }
1847
1848 if (state->ptr != state->end) {
1849 if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
1850 php_error_docref(NULL, E_WARNING,
1851 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1852 (unsigned) (state->ptr - str), str);
1853 }
1854 if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
1855 efree(state);
1856 return NULL;
1857 }
1858 }
1859
1860 return (php_http_url_t *) state;
1861 }
1862
1863 static zend_class_entry *php_http_url_class_entry;
1864 static zend_class_entry *php_http_env_url_class_entry;
1865
1866 zend_class_entry *php_http_url_get_class_entry(void)
1867 {
1868 return php_http_url_class_entry;
1869 }
1870
1871 zend_class_entry *php_http_get_env_url_class_entry(void)
1872 {
1873 return php_http_env_url_class_entry;
1874 }
1875
1876 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0)
1877 ZEND_ARG_INFO(0, old_url)
1878 ZEND_ARG_INFO(0, new_url)
1879 ZEND_ARG_INFO(0, flags)
1880 ZEND_END_ARG_INFO();
1881 PHP_METHOD(HttpUrl, __construct)
1882 {
1883 zval *new_url = NULL, *old_url = NULL;
1884 zend_long flags = 0;
1885 zend_error_handling zeh;
1886
1887 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return);
1888
1889 /* always set http\Url::FROM_ENV for instances of http\Env\Url */
1890 if (instanceof_function(Z_OBJCE_P(getThis()), php_http_env_url_class_entry)) {
1891 flags |= PHP_HTTP_URL_FROM_ENV;
1892 }
1893
1894 if (flags & PHP_HTTP_URL_SILENT_ERRORS) {
1895 zend_replace_error_handling(EH_SUPPRESS, NULL, &zeh);
1896 } else if (flags & PHP_HTTP_URL_IGNORE_ERRORS) {
1897 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
1898 } else {
1899 zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
1900 }
1901 {
1902 php_http_url_t *res_purl, *new_purl = NULL, *old_purl = NULL;
1903
1904 if (new_url) {
1905 new_purl = php_http_url_from_zval(new_url, flags);
1906 if (!new_purl) {
1907 zend_restore_error_handling(&zeh);
1908 return;
1909 }
1910 }
1911 if (old_url) {
1912 old_purl = php_http_url_from_zval(old_url, flags);
1913 if (!old_purl) {
1914 if (new_purl) {
1915 php_http_url_free(&new_purl);
1916 }
1917 zend_restore_error_handling(&zeh);
1918 return;
1919 }
1920 }
1921
1922 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1923 php_http_url_to_struct(res_purl, getThis());
1924
1925 php_http_url_free(&res_purl);
1926 if (old_purl) {
1927 php_http_url_free(&old_purl);
1928 }
1929 if (new_purl) {
1930 php_http_url_free(&new_purl);
1931 }
1932 }
1933 zend_restore_error_handling(&zeh);
1934 }
1935
1936 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod, 0, 0, 1)
1937 ZEND_ARG_INFO(0, more_url_parts)
1938 ZEND_ARG_INFO(0, flags)
1939 ZEND_END_ARG_INFO();
1940 PHP_METHOD(HttpUrl, mod)
1941 {
1942 zval *new_url = NULL;
1943 zend_long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY | PHP_HTTP_URL_SANITIZE_PATH;
1944 zend_error_handling zeh;
1945
1946 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url, &flags), invalid_arg, return);
1947
1948 if (flags & PHP_HTTP_URL_SILENT_ERRORS) {
1949 zend_replace_error_handling(EH_SUPPRESS, NULL, &zeh);
1950 } else if (flags & PHP_HTTP_URL_IGNORE_ERRORS) {
1951 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
1952 } else {
1953 zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
1954 }
1955 {
1956 php_http_url_t *new_purl = NULL, *old_purl = NULL;
1957
1958 if (new_url) {
1959 new_purl = php_http_url_from_zval(new_url, flags);
1960 if (!new_purl) {
1961 zend_restore_error_handling(&zeh);
1962 return;
1963 }
1964 }
1965
1966 if ((old_purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1967 php_http_url_t *res_purl;
1968
1969 ZVAL_OBJ(return_value, zend_objects_clone_obj(getThis()));
1970
1971 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1972 php_http_url_to_struct(res_purl, return_value);
1973
1974 php_http_url_free(&res_purl);
1975 php_http_url_free(&old_purl);
1976 }
1977 if (new_purl) {
1978 php_http_url_free(&new_purl);
1979 }
1980 }
1981 zend_restore_error_handling(&zeh);
1982 }
1983
1984 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString, 0, 0, 0)
1985 ZEND_END_ARG_INFO();
1986 PHP_METHOD(HttpUrl, toString)
1987 {
1988 if (SUCCESS == zend_parse_parameters_none()) {
1989 php_http_url_t *purl;
1990
1991 if ((purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1992 char *str;
1993 size_t len;
1994
1995 php_http_url_to_string(purl, &str, &len, 0);
1996 php_http_url_free(&purl);
1997 RETURN_STR(php_http_cs2zs(str, len));
1998 }
1999 }
2000 RETURN_EMPTY_STRING();
2001 }
2002
2003 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray, 0, 0, 0)
2004 ZEND_END_ARG_INFO();
2005 PHP_METHOD(HttpUrl, toArray)
2006 {
2007 php_http_url_t *purl;
2008
2009 if (SUCCESS != zend_parse_parameters_none()) {
2010 return;
2011 }
2012
2013 /* strip any non-URL properties */
2014 purl = php_http_url_from_struct(HASH_OF(getThis()));
2015 php_http_url_to_struct(purl, return_value);
2016 php_http_url_free(&purl);
2017 }
2018
2019 static zend_function_entry php_http_url_methods[] = {
2020 PHP_ME(HttpUrl, __construct, ai_HttpUrl___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
2021 PHP_ME(HttpUrl, mod, ai_HttpUrl_mod, ZEND_ACC_PUBLIC)
2022 PHP_ME(HttpUrl, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
2023 ZEND_MALIAS(HttpUrl, __toString, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
2024 PHP_ME(HttpUrl, toArray, ai_HttpUrl_toArray, ZEND_ACC_PUBLIC)
2025 EMPTY_FUNCTION_ENTRY
2026 };
2027
2028 PHP_MINIT_FUNCTION(http_url)
2029 {
2030 zend_class_entry ce = {0};
2031
2032 INIT_NS_CLASS_ENTRY(ce, "http", "Url", php_http_url_methods);
2033 php_http_url_class_entry = zend_register_internal_class(&ce);
2034
2035 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC);
2036 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC);
2037 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC);
2038 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC);
2039 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC);
2040 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("path"), ZEND_ACC_PUBLIC);
2041 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC);
2042 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC);
2043
2044 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE);
2045 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH);
2046 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY);
2047 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER);
2048 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS);
2049 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH);
2050 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT);
2051 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH);
2052 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY);
2053 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT);
2054 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL);
2055 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV);
2056 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH);
2057
2058 #if PHP_HTTP_HAVE_WCHAR
2059 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC);
2060 #endif
2061 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8);
2062 #if PHP_HTTP_HAVE_LIBIDN2 || PHP_HTTP_HAVE_LIBIDN || PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2 || HAVE_UIDNA_IDNTOASCII || HAVE_UIDNA_NAMETOASCII_UTF8
2063 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN);
2064 # if PHP_HTTP_HAVE_IDNA2003
2065 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN_2003"), PHP_HTTP_URL_PARSE_TOIDN_2003);
2066 # endif
2067 # if PHP_HTTP_HAVE_IDNA2008
2068 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN_2008"), PHP_HTTP_URL_PARSE_TOIDN_2008);
2069 # endif
2070 #endif
2071 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT);
2072
2073 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Url", php_http_url_methods);
2074 php_http_env_url_class_entry = zend_register_internal_class_ex(&ce, php_http_url_class_entry);
2075
2076 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("IGNORE_ERRORS"), PHP_HTTP_URL_IGNORE_ERRORS);
2077 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SILENT_ERRORS"), PHP_HTTP_URL_SILENT_ERRORS);
2078
2079 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STDFLAGS"), PHP_HTTP_URL_STDFLAGS);
2080
2081 return SUCCESS;
2082 }
2083
2084
2085 /*
2086 * Local variables:
2087 * tab-width: 4
2088 * c-basic-offset: 4
2089 * End:
2090 * vim600: noet sw=4 ts=4 fdm=marker
2091 * vim<600: noet sw=4 ts=4
2092 */
2093