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