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