zend_string updates
[m6w6/ext-http] / 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 #ifdef PHP_HTTP_HAVE_IDN
16 # include <idna.h>
17 #endif
18
19 #ifdef PHP_HTTP_HAVE_WCHAR
20 # include <wchar.h>
21 # include <wctype.h>
22 #endif
23
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
26 #endif
27
28 #include "php_http_utf8.h"
29
30 static inline char *localhostname(void)
31 {
32 char hostname[1024] = {0};
33
34 #ifdef PHP_WIN32
35 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
36 return estrdup(hostname);
37 }
38 #elif defined(HAVE_GETHOSTNAME)
39 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
40 # if defined(HAVE_GETDOMAINNAME)
41 size_t hlen = strlen(hostname);
42 if (hlen <= lenof(hostname) - lenof("(none)")) {
43 hostname[hlen++] = '.';
44 if (SUCCESS == getdomainname(&hostname[hlen], lenof(hostname) - hlen)) {
45 if (!strcmp(&hostname[hlen], "(none)")) {
46 hostname[hlen - 1] = '\0';
47 }
48 return estrdup(hostname);
49 }
50 }
51 # endif
52 if (strcmp(hostname, "(none)")) {
53 return estrdup(hostname);
54 }
55 }
56 #endif
57 return estrndup("localhost", lenof("localhost"));
58 }
59
60 #define url(buf) ((php_http_url_t *) buf.data)
61
62 static php_http_url_t *php_http_url_from_env(void)
63 {
64 zval *https, *zhost, *zport;
65 long port;
66 php_http_buffer_t buf;
67
68 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
69 php_http_buffer_account(&buf, sizeof(php_http_url_t));
70 memset(buf.data, 0, buf.used);
71
72 /* scheme */
73 url(buf)->scheme = &buf.data[buf.used];
74 https = php_http_env_get_server_var(ZEND_STRL("HTTPS"), 1);
75 if (https && !strcasecmp(Z_STRVAL_P(https), "ON")) {
76 php_http_buffer_append(&buf, "https", sizeof("https"));
77 } else {
78 php_http_buffer_append(&buf, "http", sizeof("http"));
79 }
80
81 /* host */
82 url(buf)->host = &buf.data[buf.used];
83 if ((((zhost = php_http_env_get_server_var(ZEND_STRL("HTTP_HOST"), 1)) ||
84 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1)) ||
85 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1)))) && Z_STRLEN_P(zhost)) {
86 size_t stop_at = strspn(Z_STRVAL_P(zhost), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.");
87
88 php_http_buffer_append(&buf, Z_STRVAL_P(zhost), stop_at);
89 php_http_buffer_append(&buf, "", 1);
90 } else {
91 char *host_str = localhostname();
92
93 php_http_buffer_append(&buf, host_str, strlen(host_str) + 1);
94 efree(host_str);
95 }
96
97 /* port */
98 zport = php_http_env_get_server_var(ZEND_STRL("SERVER_PORT"), 1);
99 if (zport && IS_LONG == is_numeric_string(Z_STRVAL_P(zport), Z_STRLEN_P(zport), &port, NULL, 0)) {
100 url(buf)->port = port;
101 }
102
103 /* path */
104 if (SG(request_info).request_uri && SG(request_info).request_uri[0]) {
105 const char *q = strchr(SG(request_info).request_uri, '?');
106
107 url(buf)->path = &buf.data[buf.used];
108
109 if (q) {
110 php_http_buffer_append(&buf, SG(request_info).request_uri, q - SG(request_info).request_uri);
111 php_http_buffer_append(&buf, "", 1);
112 } else {
113 php_http_buffer_append(&buf, SG(request_info).request_uri, strlen(SG(request_info).request_uri) + 1);
114 }
115 }
116
117 /* query */
118 if (SG(request_info).query_string && SG(request_info).query_string[0]) {
119 url(buf)->query = &buf.data[buf.used];
120 php_http_buffer_append(&buf, SG(request_info).query_string, strlen(SG(request_info).query_string) + 1);
121 }
122
123 return url(buf);
124 }
125
126 #define url_isset(u,n) \
127 ((u)&&(u)->n)
128 #define url_copy(n) do { \
129 if (url_isset(new_url, n)) { \
130 url(buf)->n = &buf.data[buf.used]; \
131 php_http_buffer_append(&buf, new_url->n, strlen(new_url->n) + 1); \
132 } else if (url_isset(old_url, n)) { \
133 url(buf)->n = &buf.data[buf.used]; \
134 php_http_buffer_append(&buf, old_url->n, strlen(old_url->n) + 1); \
135 } \
136 } while (0)
137
138 php_http_url_t *php_http_url_mod(const php_http_url_t *old_url, const php_http_url_t *new_url, unsigned flags)
139 {
140 php_http_url_t *tmp_url = NULL;
141 php_http_buffer_t buf;
142
143 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
144 php_http_buffer_account(&buf, sizeof(php_http_url_t));
145 memset(buf.data, 0, buf.used);
146
147 /* set from env if requested */
148 if (flags & PHP_HTTP_URL_FROM_ENV) {
149 php_http_url_t *env_url = php_http_url_from_env();
150
151 old_url = tmp_url = php_http_url_mod(env_url, old_url, flags ^ PHP_HTTP_URL_FROM_ENV);
152 php_http_url_free(&env_url);
153 }
154
155 url_copy(scheme);
156
157 if (!(flags & PHP_HTTP_URL_STRIP_USER)) {
158 url_copy(user);
159 }
160
161 if (!(flags & PHP_HTTP_URL_STRIP_PASS)) {
162 url_copy(pass);
163 }
164
165 url_copy(host);
166
167 if (!(flags & PHP_HTTP_URL_STRIP_PORT)) {
168 url(buf)->port = url_isset(new_url, port) ? new_url->port : ((old_url) ? old_url->port : 0);
169 }
170
171 if (!(flags & PHP_HTTP_URL_STRIP_PATH)) {
172 if ((flags & PHP_HTTP_URL_JOIN_PATH) && url_isset(old_url, path) && url_isset(new_url, path) && *new_url->path != '/') {
173 size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path);
174 char *path = ecalloc(1, old_path_len + new_path_len + 1 + 1);
175
176 strcat(path, old_url->path);
177 if (path[old_path_len - 1] != '/') {
178 php_dirname(path, old_path_len);
179 strcat(path, "/");
180 }
181 strcat(path, new_url->path);
182
183 url(buf)->path = &buf.data[buf.used];
184 if (path[0] != '/') {
185 php_http_buffer_append(&buf, "/", 1);
186 }
187 php_http_buffer_append(&buf, path, strlen(path) + 1);
188 efree(path);
189 } else {
190 const char *path = NULL;
191
192 if (url_isset(new_url, path)) {
193 path = new_url->path;
194 } else if (url_isset(old_url, path)) {
195 path = old_url->path;
196 }
197
198 if (path) {
199 url(buf)->path = &buf.data[buf.used];
200
201 php_http_buffer_append(&buf, path, strlen(path) + 1);
202 }
203
204
205 }
206 }
207
208 if (!(flags & PHP_HTTP_URL_STRIP_QUERY)) {
209 if ((flags & PHP_HTTP_URL_JOIN_QUERY) && url_isset(new_url, query) && url_isset(old_url, query)) {
210 zval qarr, qstr;
211
212 array_init(&qarr);
213
214 ZVAL_STRING(&qstr, old_url->query);
215 php_http_querystring_update(&qarr, &qstr, NULL);
216 zval_ptr_dtor(&qstr);
217 ZVAL_STRING(&qstr, new_url->query);
218 php_http_querystring_update(&qarr, &qstr, NULL);
219 zval_ptr_dtor(&qstr);
220
221 ZVAL_NULL(&qstr);
222 php_http_querystring_update(&qarr, NULL, &qstr);
223
224 url(buf)->query = &buf.data[buf.used];
225 php_http_buffer_append(&buf, Z_STRVAL(qstr), Z_STRLEN(qstr) + 1);
226
227 zval_dtor(&qstr);
228 zval_dtor(&qarr);
229 } else {
230 url_copy(query);
231 }
232 }
233
234 if (!(flags & PHP_HTTP_URL_STRIP_FRAGMENT)) {
235 url_copy(fragment);
236 }
237
238 /* done with copy & combine & strip */
239
240 if (flags & PHP_HTTP_URL_FROM_ENV) {
241 /* free old_url we tainted above */
242 php_http_url_free(&tmp_url);
243 }
244
245 /* replace directory references if path is not a single slash */
246 if ((flags & PHP_HTTP_URL_SANITIZE_PATH)
247 && url(buf)->path[0] && url(buf)->path[1]) {
248 char *ptr, *end = url(buf)->path + strlen(url(buf)->path) + 1;
249
250 for (ptr = strchr(url(buf)->path, '/'); ptr; ptr = strchr(ptr, '/')) {
251 switch (ptr[1]) {
252 case '/':
253 memmove(&ptr[1], &ptr[2], end - &ptr[2]);
254 break;
255
256 case '.':
257 switch (ptr[2]) {
258 case '\0':
259 ptr[1] = '\0';
260 break;
261
262 case '/':
263 memmove(&ptr[1], &ptr[3], end - &ptr[3]);
264 break;
265
266 case '.':
267 if (ptr[3] == '/') {
268 char *pos = &ptr[4];
269 while (ptr != url(buf)->path) {
270 if (*--ptr == '/') {
271 break;
272 }
273 }
274 memmove(&ptr[1], pos, end - pos);
275 break;
276 } else if (!ptr[3]) {
277 /* .. at the end */
278 ptr[1] = '\0';
279 }
280 /* no break */
281
282 default:
283 /* something else */
284 ++ptr;
285 break;
286 }
287 break;
288
289 default:
290 ++ptr;
291 break;
292 }
293 }
294 }
295 /* unset default ports */
296 if (url(buf)->port) {
297 if ( ((url(buf)->port == 80) && !strcmp(url(buf)->scheme, "http"))
298 || ((url(buf)->port ==443) && !strcmp(url(buf)->scheme, "https"))
299 ) {
300 url(buf)->port = 0;
301 }
302 }
303
304 return url(buf);
305 }
306
307 char *php_http_url_to_string(const php_http_url_t *url, char **url_str, size_t *url_len, zend_bool persistent)
308 {
309 php_http_buffer_t buf;
310
311 php_http_buffer_init_ex(&buf, PHP_HTTP_BUFFER_DEFAULT_SIZE, persistent ?
312 PHP_HTTP_BUFFER_INIT_PERSISTENT : 0);
313
314 if (url->scheme && *url->scheme) {
315 php_http_buffer_appendl(&buf, url->scheme);
316 php_http_buffer_appends(&buf, "://");
317 } else if ((url->user && *url->user) || (url->host && *url->host)) {
318 php_http_buffer_appends(&buf, "//");
319 }
320
321 if (url->user && *url->user) {
322 php_http_buffer_appendl(&buf, url->user);
323 if (url->pass && *url->pass) {
324 php_http_buffer_appends(&buf, ":");
325 php_http_buffer_appendl(&buf, url->pass);
326 }
327 php_http_buffer_appends(&buf, "@");
328 }
329
330 if (url->host && *url->host) {
331 php_http_buffer_appendl(&buf, url->host);
332 if (url->port) {
333 php_http_buffer_appendf(&buf, ":%hu", url->port);
334 }
335 }
336
337 if (url->path && *url->path) {
338 if (*url->path != '/') {
339 php_http_buffer_appends(&buf, "/");
340 }
341 php_http_buffer_appendl(&buf, url->path);
342 } else if (buf.used) {
343 php_http_buffer_appends(&buf, "/");
344 }
345
346 if (url->query && *url->query) {
347 php_http_buffer_appends(&buf, "?");
348 php_http_buffer_appendl(&buf, url->query);
349 }
350
351 if (url->fragment && *url->fragment) {
352 php_http_buffer_appends(&buf, "#");
353 php_http_buffer_appendl(&buf, url->fragment);
354 }
355
356 php_http_buffer_shrink(&buf);
357 php_http_buffer_fix(&buf);
358
359 if (url_len) {
360 *url_len = buf.used;
361 }
362
363 if (url_str) {
364 *url_str = buf.data;
365 }
366
367 return buf.data;
368 }
369
370 php_http_url_t *php_http_url_from_zval(zval *value, unsigned flags)
371 {
372 zend_string *zs;
373 php_http_url_t *purl;
374
375 switch (Z_TYPE_P(value)) {
376 case IS_ARRAY:
377 case IS_OBJECT:
378 purl = php_http_url_from_struct(HASH_OF(value));
379 break;
380
381 default:
382 zs = zval_get_string(value);
383 purl = php_http_url_parse(zs->val, zs->len, flags);
384 zend_string_release(zs);
385 }
386
387 return purl;
388 }
389
390 php_http_url_t *php_http_url_from_struct(HashTable *ht)
391 {
392 zval *e;
393 php_http_buffer_t buf;
394
395 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
396 php_http_buffer_account(&buf, sizeof(php_http_url_t));
397 memset(buf.data, 0, buf.used);
398
399 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("scheme")))) {
400 zend_string *zs = zval_get_string(e);
401 url(buf)->scheme = &buf.data[buf.used];
402 php_http_buffer_append(&buf, zs->val, zs->len + 1);
403 zend_string_release(zs);
404 }
405 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("user")))) {
406 zend_string *zs = zval_get_string(e);
407 url(buf)->user = &buf.data[buf.used];
408 php_http_buffer_append(&buf, zs->val, zs->len + 1);
409 zend_string_release(zs);
410 }
411 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("pass")))) {
412 zend_string *zs = zval_get_string(e);
413 url(buf)->pass = &buf.data[buf.used];
414 php_http_buffer_append(&buf, zs->val, zs->len + 1);
415 zend_string_release(zs);
416 }
417 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("host")))) {
418 zend_string *zs = zval_get_string(e);
419 url(buf)->host = &buf.data[buf.used];
420 php_http_buffer_append(&buf, zs->val, zs->len + 1);
421 zend_string_release(zs);
422 }
423 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("port")))) {
424 url(buf)->port = (unsigned short) zval_get_long(e);
425 }
426 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("path")))) {
427 zend_string *zs = zval_get_string(e);
428 url(buf)->path = &buf.data[buf.used];
429 php_http_buffer_append(&buf, zs->val, zs->len + 1);
430 zend_string_release(zs);
431 }
432 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("query")))) {
433 zend_string *zs = zval_get_string(e);
434 url(buf)->query = &buf.data[buf.used];
435 php_http_buffer_append(&buf, zs->val, zs->len + 1);
436 zend_string_release(zs);
437 }
438 if ((e = zend_hash_str_find_ind(ht, ZEND_STRL("fragment")))) {
439 zend_string *zs = zval_get_string(e);
440 url(buf)->fragment = &buf.data[buf.used];
441 php_http_buffer_append(&buf, zs->val, zs->len + 1);
442 zend_string_release(zs);
443 }
444
445 return url(buf);
446 }
447
448 HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct)
449 {
450 HashTable *ht;
451 zval tmp;
452
453 if (strct) {
454 switch (Z_TYPE_P(strct)) {
455 default:
456 zval_dtor(strct);
457 array_init(strct);
458 /* no break */
459 case IS_ARRAY:
460 case IS_OBJECT:
461 ht = HASH_OF(strct);
462 break;
463 }
464 } else {
465 ALLOC_HASHTABLE(ht);
466 zend_hash_init(ht, 8, NULL, ZVAL_PTR_DTOR, 0);
467 }
468
469 #define url_struct_add(part) \
470 if (Z_TYPE_P(strct) == IS_ARRAY) { \
471 zend_hash_str_update(Z_ARRVAL_P(strct), part, lenof(part), &tmp); \
472 } else { \
473 zend_update_property(Z_OBJCE_P(strct), strct, part, lenof(part), &tmp); \
474 zval_ptr_dtor(&tmp); \
475 }
476
477 if (url) {
478 if (url->scheme) {
479 ZVAL_STRING(&tmp, url->scheme);
480 url_struct_add("scheme");
481 }
482 if (url->user) {
483 ZVAL_STRING(&tmp, url->user);
484 url_struct_add("user");
485 }
486 if (url->pass) {
487 ZVAL_STRING(&tmp, url->pass);
488 url_struct_add("pass");
489 }
490 if (url->host) {
491 ZVAL_STRING(&tmp, url->host);
492 url_struct_add("host");
493 }
494 if (url->port) {
495 ZVAL_LONG(&tmp, url->port);
496 url_struct_add("port");
497 }
498 if (url->path) {
499 ZVAL_STRING(&tmp, url->path);
500 url_struct_add("path");
501 }
502 if (url->query) {
503 ZVAL_STRING(&tmp, url->query);
504 url_struct_add("query");
505 }
506 if (url->fragment) {
507 ZVAL_STRING(&tmp, url->fragment);
508 url_struct_add("fragment");
509 }
510 }
511
512 return ht;
513 }
514
515 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)
516 {
517 const char *arg_sep_str;
518 size_t arg_sep_len;
519 php_http_buffer_t *qstr = php_http_buffer_new();
520
521 php_http_url_argsep(&arg_sep_str, &arg_sep_len);
522
523 if (SUCCESS != php_http_url_encode_hash_ex(hash, qstr, arg_sep_str, arg_sep_len, "=", 1, pre_encoded_str, pre_encoded_len)) {
524 php_http_buffer_free(&qstr);
525 return FAILURE;
526 }
527
528 php_http_buffer_data(qstr, encoded_str, encoded_len);
529 php_http_buffer_free(&qstr);
530
531 return SUCCESS;
532 }
533
534 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)
535 {
536 if (pre_encoded_len && pre_encoded_str) {
537 php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len);
538 }
539
540 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)) {
541 return FAILURE;
542 }
543
544 return SUCCESS;
545 }
546
547 struct parse_state {
548 php_http_url_t url;
549 const char *ptr;
550 const char *end;
551 size_t maxlen;
552 off_t offset;
553 unsigned flags;
554 char buffer[1]; /* last member */
555 };
556
557 void php_http_url_free(php_http_url_t **url)
558 {
559 if (*url) {
560 efree(*url);
561 *url = NULL;
562 }
563 }
564
565 php_http_url_t *php_http_url_copy(const php_http_url_t *url, zend_bool persistent)
566 {
567 php_http_url_t *cpy;
568 const char *end = NULL, *url_ptr = (const char *) url;
569 char *cpy_ptr;
570
571 end = MAX(url->scheme, end);
572 end = MAX(url->pass, end);
573 end = MAX(url->user, end);
574 end = MAX(url->host, end);
575 end = MAX(url->path, end);
576 end = MAX(url->query, end);
577 end = MAX(url->fragment, end);
578
579 if (end) {
580 end += strlen(end) + 1;
581 cpy_ptr = pecalloc(1, end - url_ptr, persistent);
582 cpy = (php_http_url_t *) cpy_ptr;
583
584 memcpy(cpy_ptr + sizeof(*cpy), url_ptr + sizeof(*url), end - url_ptr - sizeof(*url));
585
586 cpy->scheme = url->scheme ? cpy_ptr + (url->scheme - url_ptr) : NULL;
587 cpy->pass = url->pass ? cpy_ptr + (url->pass - url_ptr) : NULL;
588 cpy->user = url->user ? cpy_ptr + (url->user - url_ptr) : NULL;
589 cpy->host = url->host ? cpy_ptr + (url->host - url_ptr) : NULL;
590 cpy->path = url->path ? cpy_ptr + (url->path - url_ptr) : NULL;
591 cpy->query = url->query ? cpy_ptr + (url->query - url_ptr) : NULL;
592 cpy->fragment = url->fragment ? cpy_ptr + (url->fragment - url_ptr) : NULL;
593 } else {
594 cpy = ecalloc(1, sizeof(*url));
595 }
596
597 cpy->port = url->port;
598
599 return cpy;
600 }
601
602 static size_t parse_mb_utf8(unsigned *wc, const char *ptr, const char *end)
603 {
604 unsigned wchar;
605 size_t consumed = utf8towc(&wchar, (const unsigned char *) ptr, end - ptr);
606
607 if (!consumed || consumed == (size_t) -1) {
608 return 0;
609 }
610
611 if (wc) {
612 *wc = wchar;
613 }
614 return consumed;
615 }
616
617 #ifdef PHP_HTTP_HAVE_WCHAR
618 static size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
619 {
620 wchar_t wchar;
621 size_t consumed = 0;
622 #if defined(HAVE_MBRTOWC)
623 mbstate_t ps = {0};
624
625 consumed = mbrtowc(&wchar, ptr, end - ptr, &ps);
626 #elif defined(HAVE_MBTOWC)
627 consumed = mbtowc(&wchar, ptr, end - ptr);
628 #endif
629
630 if (!consumed || consumed == (size_t) -1) {
631 return 0;
632 }
633
634 if (wc) {
635 *wc = wchar;
636 }
637 return consumed;
638 }
639 #endif
640
641 typedef enum parse_mb_what {
642 PARSE_SCHEME,
643 PARSE_USERINFO,
644 PARSE_HOSTINFO,
645 PARSE_PATH,
646 PARSE_QUERY,
647 PARSE_FRAGMENT
648 } parse_mb_what_t;
649
650 static const char * const parse_what[] = {
651 "scheme",
652 "userinfo",
653 "hostinfo",
654 "path",
655 "query",
656 "fragment"
657 };
658
659 static const char parse_xdigits[] = "0123456789ABCDEF";
660
661 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)
662 {
663 unsigned wchar;
664 size_t consumed = 0;
665
666 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
667 consumed = parse_mb_utf8(&wchar, ptr, end);
668 }
669 #ifdef PHP_HTTP_HAVE_WCHAR
670 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
671 consumed = parse_mb_loc(&wchar, ptr, end);
672 }
673 #endif
674
675 while (consumed) {
676 if (!(state->flags & PHP_HTTP_URL_PARSE_TOPCT) || what == PARSE_HOSTINFO || what == PARSE_SCHEME) {
677 if (what == PARSE_HOSTINFO && (state->flags & PHP_HTTP_URL_PARSE_TOIDN)) {
678 /* idna */
679 } else if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
680 if (!isualnum(wchar)) {
681 break;
682 }
683 #ifdef PHP_HTTP_HAVE_WCHAR
684 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
685 if (!iswalnum(wchar)) {
686 break;
687 }
688 #endif
689 }
690 PHP_HTTP_DUFF(consumed, state->buffer[state->offset++] = *ptr++);
691 } else {
692 int i = 0;
693
694 PHP_HTTP_DUFF(consumed,
695 state->buffer[state->offset++] = '%';
696 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) >> 4];
697 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) & 0xf];
698 ++i;
699 );
700 }
701
702 return consumed;
703 }
704
705 if (!silent) {
706 php_error_docref(NULL, E_WARNING,
707 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
708 parse_what[what], (unsigned char) *ptr, (unsigned) (ptr - begin), begin);
709 }
710
711 return 0;
712 }
713
714 static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *ptr)
715 {
716 size_t mb;
717 const char *password = NULL, *end = state->ptr, *tmp = ptr;
718
719 state->url.user = &state->buffer[state->offset];
720
721 do {
722 switch (*ptr) {
723 case ':':
724 if (password) {
725 php_error_docref(NULL, E_WARNING,
726 "Failed to parse password; duplicate ':' at pos %u in '%s'",
727 (unsigned) (ptr - tmp), tmp);
728 return FAILURE;
729 }
730 password = ptr + 1;
731 state->buffer[state->offset++] = 0;
732 state->url.pass = &state->buffer[state->offset];
733 break;
734
735 case '%':
736 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
737 php_error_docref(NULL, E_WARNING,
738 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
739 (unsigned) (ptr - tmp), tmp);
740 return FAILURE;
741 }
742 state->buffer[state->offset++] = *ptr++;
743 state->buffer[state->offset++] = *ptr++;
744 state->buffer[state->offset++] = *ptr;
745 break;
746
747 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
748 case '+': case ',': case ';': case '=': /* sub-delims */
749 case '-': case '.': case '_': case '~': /* unreserved */
750 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
751 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
752 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
753 case 'V': case 'W': case 'X': case 'Y': case 'Z':
754 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
755 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
756 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
757 case 'v': case 'w': case 'x': case 'y': case 'z':
758 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
759 case '7': case '8': case '9':
760 /* allowed */
761 state->buffer[state->offset++] = *ptr;
762 break;
763
764 default:
765 if (!(mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, 0))) {
766 return FAILURE;
767 }
768 ptr += mb - 1;
769 }
770 } while(++ptr != end);
771
772
773 state->buffer[state->offset++] = 0;
774
775 return SUCCESS;
776 }
777
778 static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *ptr)
779 {
780 size_t mb, len;
781 const char *end = state->ptr, *tmp = ptr, *port = NULL;
782
783
784 #ifdef HAVE_INET_PTON
785 if (*ptr == '[') {
786 char *error = NULL, *tmp = memchr(ptr, ']', end - ptr);
787
788 if (tmp) {
789 size_t addrlen = tmp - ptr + 1;
790 char buf[16], *addr = estrndup(ptr + 1, addrlen - 2);
791 int rv = inet_pton(AF_INET6, addr, buf);
792
793 efree(addr);
794 if (rv == 1) {
795 state->buffer[state->offset] = '[';
796 state->url.host = &state->buffer[state->offset];
797 inet_ntop(AF_INET6, buf, state->url.host + 1, state->maxlen - state->offset);
798 state->offset += strlen(state->url.host);
799 state->buffer[state->offset++] = ']';
800 state->buffer[state->offset++] = 0;
801 ptr = tmp + 1;
802 } else if (rv == -1) {
803 error = strerror(errno);
804 } else {
805 error = "unexpected '['";
806 }
807 } else {
808 error = "expected ']'";
809 }
810
811 if (error) {
812 php_error_docref(NULL, E_WARNING, "Failed to parse hostinfo; %s", error);
813 return FAILURE;
814 }
815 }
816 #endif
817 if (ptr != end) do {
818 switch (*ptr) {
819 case ':':
820 if (port) {
821 php_error_docref(NULL, E_WARNING,
822 "Failed to parse port; unexpected ':' at pos %u in '%s'",
823 (unsigned) (ptr - tmp), tmp);
824 return FAILURE;
825 }
826 port = ptr + 1;
827 break;
828
829 case '%':
830 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
831 php_error_docref(NULL, E_WARNING,
832 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
833 (unsigned) (ptr - tmp), tmp);
834 return FAILURE;
835 }
836 state->buffer[state->offset++] = *ptr++;
837 state->buffer[state->offset++] = *ptr++;
838 state->buffer[state->offset++] = *ptr;
839 break;
840
841 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
842 case '+': case ',': case ';': case '=': /* sub-delims */
843 case '-': case '.': case '_': case '~': /* unreserved */
844 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
845 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
846 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
847 case 'V': case 'W': case 'X': case 'Y': case 'Z':
848 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
849 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
850 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
851 case 'v': case 'w': case 'x': case 'y': case 'z':
852 if (port) {
853 php_error_docref(NULL, E_WARNING,
854 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
855 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
856 return FAILURE;
857 }
858 /* no break */
859 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
860 case '7': case '8': case '9':
861 /* allowed */
862 if (port) {
863 state->url.port *= 10;
864 state->url.port += *ptr - '0';
865 } else {
866 state->buffer[state->offset++] = *ptr;
867 }
868 break;
869
870 default:
871 if (port) {
872 php_error_docref(NULL, E_WARNING,
873 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
874 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
875 return FAILURE;
876 } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, 0))) {
877 return FAILURE;
878 }
879 ptr += mb - 1;
880 }
881 } while (++ptr != end);
882
883 if (!state->url.host) {
884 len = (port ? port - tmp - 1 : end - tmp);
885 state->url.host = &state->buffer[state->offset - len];
886 state->buffer[state->offset++] = 0;
887 }
888
889 #ifdef PHP_HTTP_HAVE_IDN
890 if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
891 char *idn = NULL;
892 int rv = -1;
893
894 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
895 rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
896 }
897 # ifdef PHP_HTTP_HAVE_WCHAR
898 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
899 rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
900 }
901 # endif
902 if (rv != IDNA_SUCCESS) {
903 php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv));
904 return FAILURE;
905 } else {
906 size_t idnlen = strlen(idn);
907 memcpy(state->url.host, idn, idnlen + 1);
908 free(idn);
909 state->offset += idnlen - len;
910 }
911 }
912 #endif
913
914 return SUCCESS;
915 }
916
917 static const char *parse_authority(struct parse_state *state)
918 {
919 const char *tmp = state->ptr, *host = NULL;
920
921 do {
922 switch (*state->ptr) {
923 case '@':
924 /* userinfo delimiter */
925 if (host) {
926 php_error_docref(NULL, E_WARNING,
927 "Failed to parse userinfo; unexpected '@'");
928 return NULL;
929 }
930 host = state->ptr + 1;
931 if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) {
932 return NULL;
933 }
934 tmp = state->ptr + 1;
935 break;
936
937 case '/':
938 case '?':
939 case '#':
940 case '\0':
941 /* host delimiter */
942 if (tmp != state->ptr && SUCCESS != parse_hostinfo(state, tmp)) {
943 return NULL;
944 }
945 return state->ptr;
946 }
947 } while (++state->ptr <= state->end);
948
949 return NULL;
950 }
951
952 static const char *parse_path(struct parse_state *state)
953 {
954 size_t mb;
955 const char *tmp;
956
957 /* is there actually a path to parse? */
958 if (!*state->ptr) {
959 return state->ptr;
960 }
961 tmp = state->ptr;
962 state->url.path = &state->buffer[state->offset];
963
964 do {
965 switch (*state->ptr) {
966 case '#':
967 case '?':
968 goto done;
969
970 case '%':
971 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
972 php_error_docref(NULL, E_WARNING,
973 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
974 (unsigned) (state->ptr - tmp), tmp);
975 return NULL;
976 }
977 state->buffer[state->offset++] = *state->ptr++;
978 state->buffer[state->offset++] = *state->ptr++;
979 state->buffer[state->offset++] = *state->ptr;
980 break;
981
982 case '/': /* yeah, well */
983 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
984 case '+': case ',': case ';': case '=': /* sub-delims */
985 case '-': case '.': case '_': case '~': /* unreserved */
986 case ':': case '@': /* pchar */
987 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
988 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
989 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
990 case 'V': case 'W': case 'X': case 'Y': case 'Z':
991 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
992 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
993 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
994 case 'v': case 'w': case 'x': case 'y': case 'z':
995 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
996 case '7': case '8': case '9':
997 /* allowed */
998 state->buffer[state->offset++] = *state->ptr;
999 break;
1000
1001 default:
1002 if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) {
1003 return NULL;
1004 }
1005 state->ptr += mb - 1;
1006 }
1007 } while (++state->ptr < state->end);
1008
1009 done:
1010 /* did we have any path component ? */
1011 if (tmp != state->ptr) {
1012 state->buffer[state->offset++] = 0;
1013 } else {
1014 state->url.path = NULL;
1015 }
1016 return state->ptr;
1017 }
1018
1019 static const char *parse_query(struct parse_state *state)
1020 {
1021 size_t mb;
1022 const char *tmp = state->ptr + !!*state->ptr;
1023
1024 /* is there actually a query to parse? */
1025 if (*state->ptr != '?') {
1026 return state->ptr;
1027 }
1028
1029 /* skip initial '?' */
1030 tmp = ++state->ptr;
1031 state->url.query = &state->buffer[state->offset];
1032
1033 do {
1034 switch (*state->ptr) {
1035 case '#':
1036 goto done;
1037
1038 case '%':
1039 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1040 php_error_docref(NULL, E_WARNING,
1041 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1042 (unsigned) (state->ptr - tmp), tmp);
1043 return NULL;
1044 }
1045 state->buffer[state->offset++] = *state->ptr++;
1046 state->buffer[state->offset++] = *state->ptr++;
1047 state->buffer[state->offset++] = *state->ptr;
1048 break;
1049
1050 case ']':
1051 case '[':
1052 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1053 state->buffer[state->offset++] = '%';
1054 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1055 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1056 break;
1057 }
1058 /* no break */
1059
1060 case '?': case '/': /* yeah, well */
1061 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1062 case '+': case ',': case ';': case '=': /* sub-delims */
1063 case '-': case '.': case '_': case '~': /* unreserved */
1064 case ':': case '@': /* pchar */
1065 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1066 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1067 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1068 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1069 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1070 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1071 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1072 case 'v': case 'w': case 'x': case 'y': case 'z':
1073 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1074 case '7': case '8': case '9':
1075 /* allowed */
1076 state->buffer[state->offset++] = *state->ptr;
1077 break;
1078
1079 default:
1080 if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) {
1081 return NULL;
1082 }
1083 state->ptr += mb - 1;
1084 }
1085 } while (++state->ptr < state->end);
1086
1087 done:
1088 state->buffer[state->offset++] = 0;
1089 return state->ptr;
1090 }
1091
1092 static const char *parse_fragment(struct parse_state *state)
1093 {
1094 size_t mb;
1095 const char *tmp;
1096
1097 /* is there actually a fragment to parse? */
1098 if (*state->ptr != '#') {
1099 return state->ptr;
1100 }
1101
1102 /* skip initial '#' */
1103 tmp = ++state->ptr;
1104 state->url.fragment = &state->buffer[state->offset];
1105
1106 do {
1107 switch (*state->ptr) {
1108 case '%':
1109 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1110 php_error_docref(NULL, E_WARNING,
1111 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1112 (unsigned) (state->ptr - tmp), tmp);
1113 return NULL;
1114 }
1115 state->buffer[state->offset++] = *state->ptr++;
1116 state->buffer[state->offset++] = *state->ptr++;
1117 state->buffer[state->offset++] = *state->ptr;
1118 break;
1119
1120 case '?': case '/':
1121 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1122 case '+': case ',': case ';': case '=': /* sub-delims */
1123 case '-': case '.': case '_': case '~': /* unreserved */
1124 case ':': case '@': /* pchar */
1125 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1126 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1127 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1128 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1129 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1130 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1131 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1132 case 'v': case 'w': case 'x': case 'y': case 'z':
1133 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1134 case '7': case '8': case '9':
1135 /* allowed */
1136 state->buffer[state->offset++] = *state->ptr;
1137 break;
1138
1139 default:
1140 if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) {
1141 return NULL;
1142 }
1143 state->ptr += mb - 1;
1144 }
1145 } while (++state->ptr < state->end);
1146
1147 state->buffer[state->offset++] = 0;
1148 return state->ptr;
1149 }
1150
1151 static const char *parse_hier(struct parse_state *state)
1152 {
1153 if (*state->ptr == '/') {
1154 if (state->end - state->ptr > 1) {
1155 if (*(state->ptr + 1) == '/') {
1156 state->ptr += 2;
1157 if (!(state->ptr = parse_authority(state))) {
1158 return NULL;
1159 }
1160 }
1161 }
1162 }
1163 return parse_path(state);
1164 }
1165
1166 static const char *parse_scheme(struct parse_state *state)
1167 {
1168 size_t mb;
1169 const char *tmp = state->ptr;
1170
1171 do {
1172 switch (*state->ptr) {
1173 case ':':
1174 /* scheme delimiter */
1175 state->url.scheme = &state->buffer[0];
1176 state->buffer[state->offset++] = 0;
1177 return ++state->ptr;
1178
1179 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1180 case '7': case '8': case '9':
1181 case '+': case '-': case '.':
1182 if (state->ptr == tmp) {
1183 return tmp;
1184 }
1185 /* no break */
1186 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1187 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1188 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1189 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1190 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1191 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1192 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1193 case 'v': case 'w': case 'x': case 'y': case 'z':
1194 /* scheme part */
1195 state->buffer[state->offset++] = *state->ptr;
1196 break;
1197
1198 default:
1199 if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
1200 /* soft fail; parse path next */
1201 return tmp;
1202 }
1203 state->ptr += mb - 1;
1204 }
1205 } while (++state->ptr != state->end);
1206
1207 return state->ptr = tmp;
1208 }
1209
1210 php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags)
1211 {
1212 size_t maxlen = 3 * len;
1213 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1214
1215 state->end = str + len;
1216 state->ptr = str;
1217 state->flags = flags;
1218 state->maxlen = maxlen;
1219
1220 if (!parse_scheme(state)) {
1221 php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
1222 efree(state);
1223 return NULL;
1224 }
1225
1226 if (!parse_hier(state)) {
1227 efree(state);
1228 return NULL;
1229 }
1230
1231 if (!parse_query(state)) {
1232 php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
1233 efree(state);
1234 return NULL;
1235 }
1236
1237 if (!parse_fragment(state)) {
1238 php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
1239 efree(state);
1240 return NULL;
1241 }
1242
1243 return (php_http_url_t *) state;
1244 }
1245
1246 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0)
1247 ZEND_ARG_INFO(0, old_url)
1248 ZEND_ARG_INFO(0, new_url)
1249 ZEND_ARG_INFO(0, flags)
1250 ZEND_END_ARG_INFO();
1251 PHP_METHOD(HttpUrl, __construct)
1252 {
1253 zval *new_url = NULL, *old_url = NULL;
1254 zend_long flags = PHP_HTTP_URL_FROM_ENV;
1255 zend_error_handling zeh;
1256
1257 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return);
1258
1259 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
1260 {
1261 php_http_url_t *res_purl, *new_purl = NULL, *old_purl = NULL;
1262
1263 if (new_url) {
1264 new_purl = php_http_url_from_zval(new_url, flags);
1265 if (!new_purl) {
1266 zend_restore_error_handling(&zeh);
1267 return;
1268 }
1269 }
1270 if (old_url) {
1271 old_purl = php_http_url_from_zval(old_url, flags);
1272 if (!old_purl) {
1273 if (new_purl) {
1274 php_http_url_free(&new_purl);
1275 }
1276 zend_restore_error_handling(&zeh);
1277 return;
1278 }
1279 }
1280
1281 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1282 php_http_url_to_struct(res_purl, getThis());
1283
1284 php_http_url_free(&res_purl);
1285 if (old_purl) {
1286 php_http_url_free(&old_purl);
1287 }
1288 if (new_purl) {
1289 php_http_url_free(&new_purl);
1290 }
1291 }
1292 zend_restore_error_handling(&zeh);
1293 }
1294
1295 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod, 0, 0, 1)
1296 ZEND_ARG_INFO(0, more_url_parts)
1297 ZEND_ARG_INFO(0, flags)
1298 ZEND_END_ARG_INFO();
1299 PHP_METHOD(HttpUrl, mod)
1300 {
1301 zval *new_url = NULL;
1302 zend_long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY;
1303 zend_error_handling zeh;
1304
1305 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url, &flags), invalid_arg, return);
1306
1307 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
1308 {
1309 php_http_url_t *new_purl = NULL, *old_purl = NULL;
1310
1311 if (new_url) {
1312 new_purl = php_http_url_from_zval(new_url, flags);
1313 if (!new_purl) {
1314 zend_restore_error_handling(&zeh);
1315 return;
1316 }
1317 }
1318
1319 if ((old_purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1320 php_http_url_t *res_purl;
1321
1322 ZVAL_OBJ(return_value, zend_objects_clone_obj(getThis()));
1323
1324 res_purl = php_http_url_mod(old_purl, new_purl, flags);
1325 php_http_url_to_struct(res_purl, return_value);
1326
1327 php_http_url_free(&res_purl);
1328 php_http_url_free(&old_purl);
1329 }
1330 if (new_purl) {
1331 php_http_url_free(&new_purl);
1332 }
1333 }
1334 zend_restore_error_handling(&zeh);
1335 }
1336
1337 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString, 0, 0, 0)
1338 ZEND_END_ARG_INFO();
1339 PHP_METHOD(HttpUrl, toString)
1340 {
1341 if (SUCCESS == zend_parse_parameters_none()) {
1342 php_http_url_t *purl;
1343
1344 if ((purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1345 char *str;
1346 size_t len;
1347
1348 php_http_url_to_string(purl, &str, &len, 0);
1349 php_http_url_free(&purl);
1350 RETURN_STR(php_http_cs2zs(str, len));
1351 }
1352 }
1353 RETURN_EMPTY_STRING();
1354 }
1355
1356 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray, 0, 0, 0)
1357 ZEND_END_ARG_INFO();
1358 PHP_METHOD(HttpUrl, toArray)
1359 {
1360 php_http_url_t *purl;
1361
1362 if (SUCCESS != zend_parse_parameters_none()) {
1363 return;
1364 }
1365
1366 /* strip any non-URL properties */
1367 purl = php_http_url_from_struct(HASH_OF(getThis()));
1368 php_http_url_to_struct(purl, return_value);
1369 php_http_url_free(&purl);
1370 }
1371
1372 static zend_function_entry php_http_url_methods[] = {
1373 PHP_ME(HttpUrl, __construct, ai_HttpUrl___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1374 PHP_ME(HttpUrl, mod, ai_HttpUrl_mod, ZEND_ACC_PUBLIC)
1375 PHP_ME(HttpUrl, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
1376 ZEND_MALIAS(HttpUrl, __toString, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
1377 PHP_ME(HttpUrl, toArray, ai_HttpUrl_toArray, ZEND_ACC_PUBLIC)
1378 EMPTY_FUNCTION_ENTRY
1379 };
1380
1381 zend_class_entry *php_http_url_class_entry;
1382
1383 PHP_MINIT_FUNCTION(http_url)
1384 {
1385 zend_class_entry ce = {0};
1386
1387 INIT_NS_CLASS_ENTRY(ce, "http", "Url", php_http_url_methods);
1388 php_http_url_class_entry = zend_register_internal_class(&ce);
1389
1390 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC);
1391 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC);
1392 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC);
1393 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC);
1394 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC);
1395 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("path"), ZEND_ACC_PUBLIC);
1396 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC);
1397 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC);
1398
1399 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE);
1400 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH);
1401 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY);
1402 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER);
1403 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS);
1404 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH);
1405 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT);
1406 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH);
1407 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY);
1408 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT);
1409 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL);
1410 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV);
1411 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH);
1412
1413 #ifdef PHP_HTTP_HAVE_WCHAR
1414 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC);
1415 #endif
1416 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8);
1417 #ifdef PHP_HTTP_HAVE_IDN
1418 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN);
1419 #endif
1420 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT);
1421
1422 return SUCCESS;
1423 }
1424
1425
1426 /*
1427 * Local variables:
1428 * tab-width: 4
1429 * c-basic-offset: 4
1430 * End:
1431 * vim600: noet sw=4 ts=4 fdm=marker
1432 * vim<600: noet sw=4 ts=4
1433 */
1434