fix warning, and place bet on the safe side
[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 #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(TSRMLS_D)
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 TSRMLS_CC);
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 TSRMLS_CC)) ||
86 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_NAME"), 1 TSRMLS_CC)) ||
87 (zhost = php_http_env_get_server_var(ZEND_STRL("SERVER_ADDR"), 1 TSRMLS_CC)))) && 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 TSRMLS_CC);
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 TSRMLS_DC)
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(TSRMLS_C);
169
170 old_url = tmp_url = php_http_url_mod(env_url, old_url, flags ^ PHP_HTTP_URL_FROM_ENV TSRMLS_CC);
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 INIT_PZVAL(&qstr);
232 INIT_PZVAL(&qarr);
233 array_init(&qarr);
234
235 ZVAL_STRING(&qstr, old_url->query, 0);
236 php_http_querystring_update(&qarr, &qstr, NULL TSRMLS_CC);
237 ZVAL_STRING(&qstr, new_url->query, 0);
238 php_http_querystring_update(&qarr, &qstr, NULL TSRMLS_CC);
239
240 ZVAL_NULL(&qstr);
241 php_http_querystring_update(&qarr, NULL, &qstr TSRMLS_CC);
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[0] && url(buf)->path[1]) {
267 char *ptr, *end = url(buf)->path + strlen(url(buf)->path) + 1;
268
269 for (ptr = strchr(url(buf)->path, '/'); ptr; ptr = strchr(ptr, '/')) {
270 switch (ptr[1]) {
271 case '/':
272 memmove(&ptr[1], &ptr[2], end - &ptr[2]);
273 break;
274
275 case '.':
276 switch (ptr[2]) {
277 case '\0':
278 ptr[1] = '\0';
279 break;
280
281 case '/':
282 memmove(&ptr[1], &ptr[3], end - &ptr[3]);
283 break;
284
285 case '.':
286 if (ptr[3] == '/') {
287 char *pos = &ptr[4];
288 while (ptr != url(buf)->path) {
289 if (*--ptr == '/') {
290 break;
291 }
292 }
293 memmove(&ptr[1], pos, end - pos);
294 break;
295 } else if (!ptr[3]) {
296 /* .. at the end */
297 ptr[1] = '\0';
298 }
299 /* no break */
300
301 default:
302 /* something else */
303 ++ptr;
304 break;
305 }
306 break;
307
308 default:
309 ++ptr;
310 break;
311 }
312 }
313 }
314 /* unset default ports */
315 if (url(buf)->port) {
316 if ( ((url(buf)->port == 80) && url(buf)->scheme && !strcmp(url(buf)->scheme, "http"))
317 || ((url(buf)->port ==443) && url(buf)->scheme && !strcmp(url(buf)->scheme, "https"))
318 ) {
319 url(buf)->port = 0;
320 }
321 }
322
323 return url(buf);
324 }
325
326 char *php_http_url_to_string(const php_http_url_t *url, char **url_str, size_t *url_len, zend_bool persistent)
327 {
328 php_http_buffer_t buf;
329
330 php_http_buffer_init_ex(&buf, PHP_HTTP_BUFFER_DEFAULT_SIZE, persistent ?
331 PHP_HTTP_BUFFER_INIT_PERSISTENT : 0);
332
333 if (url->scheme && *url->scheme) {
334 php_http_buffer_appendl(&buf, url->scheme);
335 php_http_buffer_appends(&buf, "://");
336 } else if ((url->user && *url->user) || (url->host && *url->host)) {
337 php_http_buffer_appends(&buf, "//");
338 }
339
340 if (url->user && *url->user) {
341 php_http_buffer_appendl(&buf, url->user);
342 if (url->pass && *url->pass) {
343 php_http_buffer_appends(&buf, ":");
344 php_http_buffer_appendl(&buf, url->pass);
345 }
346 php_http_buffer_appends(&buf, "@");
347 }
348
349 if (url->host && *url->host) {
350 php_http_buffer_appendl(&buf, url->host);
351 if (url->port) {
352 php_http_buffer_appendf(&buf, ":%hu", url->port);
353 }
354 }
355
356 if (url->path && *url->path) {
357 if (*url->path != '/') {
358 php_http_buffer_appends(&buf, "/");
359 }
360 php_http_buffer_appendl(&buf, url->path);
361 } else if (buf.used) {
362 php_http_buffer_appends(&buf, "/");
363 }
364
365 if (url->query && *url->query) {
366 php_http_buffer_appends(&buf, "?");
367 php_http_buffer_appendl(&buf, url->query);
368 }
369
370 if (url->fragment && *url->fragment) {
371 php_http_buffer_appends(&buf, "#");
372 php_http_buffer_appendl(&buf, url->fragment);
373 }
374
375 php_http_buffer_shrink(&buf);
376 php_http_buffer_fix(&buf);
377
378 if (url_len) {
379 *url_len = buf.used;
380 }
381
382 if (url_str) {
383 *url_str = buf.data;
384 }
385
386 return buf.data;
387 }
388
389 char *php_http_url_authority_to_string(const php_http_url_t *url, char **url_str, size_t *url_len)
390 {
391 php_http_buffer_t buf;
392
393 php_http_buffer_init(&buf);
394
395 if (url->user && *url->user) {
396 php_http_buffer_appendl(&buf, url->user);
397 if (url->pass && *url->pass) {
398 php_http_buffer_appends(&buf, ":");
399 php_http_buffer_appendl(&buf, url->pass);
400 }
401 php_http_buffer_appends(&buf, "@");
402 }
403
404 if (url->host && *url->host) {
405 php_http_buffer_appendl(&buf, url->host);
406 if (url->port) {
407 php_http_buffer_appendf(&buf, ":%hu", url->port);
408 }
409 }
410
411 php_http_buffer_shrink(&buf);
412 php_http_buffer_fix(&buf);
413
414 if (url_len) {
415 *url_len = buf.used;
416 }
417
418 if (url_str) {
419 *url_str = buf.data;
420 }
421
422 return buf.data;
423 }
424
425 php_http_url_t *php_http_url_from_zval(zval *value, unsigned flags TSRMLS_DC)
426 {
427 zval *zcpy;
428 php_http_url_t *purl;
429
430 switch (Z_TYPE_P(value)) {
431 case IS_ARRAY:
432 case IS_OBJECT:
433 purl = php_http_url_from_struct(HASH_OF(value));
434 break;
435
436 default:
437 zcpy = php_http_ztyp(IS_STRING, value);
438 purl = php_http_url_parse(Z_STRVAL_P(zcpy), Z_STRLEN_P(zcpy), flags TSRMLS_CC);
439 zval_ptr_dtor(&zcpy);
440 }
441
442 return purl;
443 }
444
445 php_http_url_t *php_http_url_from_struct(HashTable *ht)
446 {
447 zval **e;
448 php_http_buffer_t buf;
449
450 php_http_buffer_init_ex(&buf, MAX(PHP_HTTP_BUFFER_DEFAULT_SIZE, sizeof(php_http_url_t)<<2), PHP_HTTP_BUFFER_INIT_PREALLOC);
451 php_http_buffer_account(&buf, sizeof(php_http_url_t));
452 memset(buf.data, 0, buf.used);
453
454 if (SUCCESS == zend_hash_find(ht, "scheme", sizeof("scheme"), (void *) &e)) {
455 zval *cpy = php_http_ztyp(IS_STRING, *e);
456 url(buf)->scheme = &buf.data[buf.used];
457 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
458 zval_ptr_dtor(&cpy);
459 }
460 if (SUCCESS == zend_hash_find(ht, "user", sizeof("user"), (void *) &e)) {
461 zval *cpy = php_http_ztyp(IS_STRING, *e);
462 url(buf)->user = &buf.data[buf.used];
463 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
464 zval_ptr_dtor(&cpy);
465 }
466 if (SUCCESS == zend_hash_find(ht, "pass", sizeof("pass"), (void *) &e)) {
467 zval *cpy = php_http_ztyp(IS_STRING, *e);
468 url(buf)->pass = &buf.data[buf.used];
469 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
470 zval_ptr_dtor(&cpy);
471 }
472 if (SUCCESS == zend_hash_find(ht, "host", sizeof("host"), (void *) &e)) {
473 zval *cpy = php_http_ztyp(IS_STRING, *e);
474 url(buf)->host = &buf.data[buf.used];
475 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
476 zval_ptr_dtor(&cpy);
477 }
478 if (SUCCESS == zend_hash_find(ht, "port", sizeof("port"), (void *) &e)) {
479 zval *cpy = php_http_ztyp(IS_LONG, *e);
480 url(buf)->port = (unsigned short) Z_LVAL_P(cpy);
481 zval_ptr_dtor(&cpy);
482 }
483 if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &e)) {
484 zval *cpy = php_http_ztyp(IS_STRING, *e);
485 url(buf)->path = &buf.data[buf.used];
486 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
487 zval_ptr_dtor(&cpy);
488 }
489 if (SUCCESS == zend_hash_find(ht, "query", sizeof("query"), (void *) &e)) {
490 zval *cpy = php_http_ztyp(IS_STRING, *e);
491 url(buf)->query = &buf.data[buf.used];
492 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
493 zval_ptr_dtor(&cpy);
494 }
495 if (SUCCESS == zend_hash_find(ht, "fragment", sizeof("fragment"), (void *) &e)) {
496 zval *cpy = php_http_ztyp(IS_STRING, *e);
497 url(buf)->fragment = &buf.data[buf.used];
498 url_append(&buf, php_http_buffer_append(&buf, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy) + 1));
499 zval_ptr_dtor(&cpy);
500 }
501
502 return url(buf);
503 }
504
505 HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct TSRMLS_DC)
506 {
507 zval arr;
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 INIT_PZVAL_ARRAY((&arr), HASH_OF(strct));
518 break;
519 }
520 } else {
521 INIT_PZVAL(&arr);
522 array_init(&arr);
523 }
524
525 if (url) {
526 if (url->scheme) {
527 add_assoc_string(&arr, "scheme", url->scheme, 1);
528 }
529 if (url->user) {
530 add_assoc_string(&arr, "user", url->user, 1);
531 }
532 if (url->pass) {
533 add_assoc_string(&arr, "pass", url->pass, 1);
534 }
535 if (url->host) {
536 add_assoc_string(&arr, "host", url->host, 1);
537 }
538 if (url->port) {
539 add_assoc_long(&arr, "port", (long) url->port);
540 }
541 if (url->path) {
542 add_assoc_string(&arr, "path", url->path, 1);
543 }
544 if (url->query) {
545 add_assoc_string(&arr, "query", url->query, 1);
546 }
547 if (url->fragment) {
548 add_assoc_string(&arr, "fragment", url->fragment, 1);
549 }
550 }
551
552 return Z_ARRVAL(arr);
553 }
554
555 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 TSRMLS_DC)
556 {
557 const char *arg_sep_str = "&";
558 size_t arg_sep_len = 1;
559 php_http_buffer_t *qstr = php_http_buffer_new();
560
561 php_http_url_argsep(&arg_sep_str, &arg_sep_len TSRMLS_CC);
562
563 if (SUCCESS != php_http_url_encode_hash_ex(hash, qstr, arg_sep_str, arg_sep_len, "=", 1, pre_encoded_str, pre_encoded_len TSRMLS_CC)) {
564 php_http_buffer_free(&qstr);
565 return FAILURE;
566 }
567
568 php_http_buffer_data(qstr, encoded_str, encoded_len);
569 php_http_buffer_free(&qstr);
570
571 return SUCCESS;
572 }
573
574 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 TSRMLS_DC)
575 {
576 if (pre_encoded_len && pre_encoded_str) {
577 php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len);
578 }
579
580 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 TSRMLS_CC)) {
581 return FAILURE;
582 }
583
584 return SUCCESS;
585 }
586
587 struct parse_state {
588 php_http_url_t url;
589 #ifdef ZTS
590 void ***ts;
591 #endif
592 const char *ptr;
593 const char *end;
594 size_t maxlen;
595 off_t offset;
596 unsigned flags;
597 char buffer[1]; /* last member */
598 };
599
600 void php_http_url_free(php_http_url_t **url)
601 {
602 if (*url) {
603 efree(*url);
604 *url = NULL;
605 }
606 }
607
608 php_http_url_t *php_http_url_copy(const php_http_url_t *url, zend_bool persistent)
609 {
610 php_http_url_t *cpy;
611 const char *end = NULL, *url_ptr = (const char *) url;
612 char *cpy_ptr;
613
614 end = MAX(url->scheme, end);
615 end = MAX(url->pass, end);
616 end = MAX(url->user, end);
617 end = MAX(url->host, end);
618 end = MAX(url->path, end);
619 end = MAX(url->query, end);
620 end = MAX(url->fragment, end);
621
622 if (end) {
623 end += strlen(end) + 1;
624 cpy_ptr = pecalloc(1, end - url_ptr, persistent);
625 cpy = (php_http_url_t *) cpy_ptr;
626
627 memcpy(cpy_ptr + sizeof(*cpy), url_ptr + sizeof(*url), end - url_ptr - sizeof(*url));
628
629 cpy->scheme = url->scheme ? cpy_ptr + (url->scheme - url_ptr) : NULL;
630 cpy->pass = url->pass ? cpy_ptr + (url->pass - url_ptr) : NULL;
631 cpy->user = url->user ? cpy_ptr + (url->user - url_ptr) : NULL;
632 cpy->host = url->host ? cpy_ptr + (url->host - url_ptr) : NULL;
633 cpy->path = url->path ? cpy_ptr + (url->path - url_ptr) : NULL;
634 cpy->query = url->query ? cpy_ptr + (url->query - url_ptr) : NULL;
635 cpy->fragment = url->fragment ? cpy_ptr + (url->fragment - url_ptr) : NULL;
636 } else {
637 cpy = ecalloc(1, sizeof(*url));
638 }
639
640 cpy->port = url->port;
641
642 return cpy;
643 }
644
645 static size_t parse_mb_utf8(unsigned *wc, const char *ptr, const char *end)
646 {
647 unsigned wchar;
648 size_t consumed = utf8towc(&wchar, (const unsigned char *) ptr, end - ptr);
649
650 if (!consumed || consumed == (size_t) -1) {
651 return 0;
652 }
653
654 if (wc) {
655 *wc = wchar;
656 }
657 return consumed;
658 }
659
660 #ifdef PHP_HTTP_HAVE_WCHAR
661 static size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
662 {
663 wchar_t wchar;
664 size_t consumed = 0;
665 #if defined(HAVE_MBRTOWC)
666 mbstate_t ps;
667
668 memset(&ps, 0, sizeof(ps));
669 consumed = mbrtowc(&wchar, ptr, end - ptr, &ps);
670 #elif defined(HAVE_MBTOWC)
671 consumed = mbtowc(&wchar, ptr, end - ptr);
672 #endif
673
674 if (!consumed || consumed == (size_t) -1) {
675 return 0;
676 }
677
678 if (wc) {
679 *wc = wchar;
680 }
681 return consumed;
682 }
683 #endif
684
685 typedef enum parse_mb_what {
686 PARSE_SCHEME,
687 PARSE_USERINFO,
688 PARSE_HOSTINFO,
689 PARSE_PATH,
690 PARSE_QUERY,
691 PARSE_FRAGMENT
692 } parse_mb_what_t;
693
694 static const char * const parse_what[] = {
695 "scheme",
696 "userinfo",
697 "hostinfo",
698 "path",
699 "query",
700 "fragment"
701 };
702
703 static const char parse_xdigits[] = "0123456789ABCDEF";
704
705 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)
706 {
707 unsigned wchar;
708 size_t consumed = 0;
709
710 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
711 consumed = parse_mb_utf8(&wchar, ptr, end);
712 }
713 #ifdef PHP_HTTP_HAVE_WCHAR
714 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
715 consumed = parse_mb_loc(&wchar, ptr, end);
716 }
717 #endif
718
719 while (consumed) {
720 if (!(state->flags & PHP_HTTP_URL_PARSE_TOPCT) || what == PARSE_HOSTINFO || what == PARSE_SCHEME) {
721 if (what == PARSE_HOSTINFO && (state->flags & PHP_HTTP_URL_PARSE_TOIDN)) {
722 /* idna */
723 } else if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
724 if (!isualnum(wchar)) {
725 break;
726 }
727 #ifdef PHP_HTTP_HAVE_WCHAR
728 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
729 if (!iswalnum(wchar)) {
730 break;
731 }
732 #endif
733 }
734 PHP_HTTP_DUFF(consumed, state->buffer[state->offset++] = *ptr++);
735 } else {
736 int i = 0;
737
738 PHP_HTTP_DUFF(consumed,
739 state->buffer[state->offset++] = '%';
740 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) >> 4];
741 state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) & 0xf];
742 ++i;
743 );
744 }
745
746 return consumed;
747 }
748
749 if (!silent) {
750 TSRMLS_FETCH_FROM_CTX(state->ts);
751 if (consumed) {
752 php_error_docref(NULL TSRMLS_CC, E_WARNING,
753 "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
754 parse_what[what], wchar, (unsigned) (ptr - begin), begin);
755 } else {
756 php_error_docref(NULL TSRMLS_CC, E_WARNING,
757 "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
758 parse_what[what], (unsigned char) *ptr, (unsigned) (ptr - begin), begin);
759 }
760 }
761
762 return 0;
763 }
764
765 static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *ptr)
766 {
767 size_t mb;
768 const char *password = NULL, *end = state->ptr, *tmp = ptr;
769 TSRMLS_FETCH_FROM_CTX(state->ts);
770
771 state->url.user = &state->buffer[state->offset];
772
773 do {
774 switch (*ptr) {
775 case ':':
776 if (password) {
777 php_error_docref(NULL TSRMLS_CC, E_WARNING,
778 "Failed to parse password; duplicate ':' at pos %u in '%s'",
779 (unsigned) (ptr - tmp), tmp);
780 return FAILURE;
781 }
782 password = ptr + 1;
783 state->buffer[state->offset++] = 0;
784 state->url.pass = &state->buffer[state->offset];
785 break;
786
787 case '%':
788 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
789 php_error_docref(NULL TSRMLS_CC, E_WARNING,
790 "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'",
791 (unsigned) (ptr - tmp), tmp);
792 return FAILURE;
793 }
794 state->buffer[state->offset++] = *ptr++;
795 state->buffer[state->offset++] = *ptr++;
796 state->buffer[state->offset++] = *ptr;
797 break;
798
799 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
800 case '+': case ',': case ';': case '=': /* sub-delims */
801 case '-': case '.': case '_': case '~': /* unreserved */
802 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
803 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
804 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
805 case 'V': case 'W': case 'X': case 'Y': case 'Z':
806 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
807 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
808 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
809 case 'v': case 'w': case 'x': case 'y': case 'z':
810 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
811 case '7': case '8': case '9':
812 /* allowed */
813 state->buffer[state->offset++] = *ptr;
814 break;
815
816 default:
817 if (!(mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, 0))) {
818 return FAILURE;
819 }
820 ptr += mb - 1;
821 }
822 } while(++ptr != end);
823
824
825 state->buffer[state->offset++] = 0;
826
827 return SUCCESS;
828 }
829
830 #if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
831 typedef size_t (*parse_mb_func)(unsigned *wc, const char *ptr, const char *end);
832 static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len TSRMLS_DC)
833 {
834 size_t offset = 0, u8_len = strlen(u8);
835
836 *u16 = ecalloc(4 * sizeof(uint16_t), u8_len + 1);
837 *len = 0;
838
839 while (offset < u8_len) {
840 unsigned wc;
841 uint16_t buf[2], *ptr = buf;
842 size_t consumed = fn(&wc, &u8[offset], &u8[u8_len]);
843
844 if (!consumed) {
845 efree(*u16);
846 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse UTF-8 at pos %zu of '%s'", offset, u8);
847 return FAILURE;
848 } else {
849 offset += consumed;
850 }
851
852 switch (wctoutf16(buf, wc)) {
853 case 2:
854 (*u16)[(*len)++] = *ptr++;
855 /* no break */
856 case 1:
857 (*u16)[(*len)++] = *ptr++;
858 break;
859 case 0:
860 default:
861 efree(*u16);
862 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert UTF-32 'U+%X' to UTF-16", wc);
863 return FAILURE;
864 }
865 }
866
867 return SUCCESS;
868 }
869 #endif
870
871 #ifndef MAXHOSTNAMELEN
872 # define MAXHOSTNAMELEN 256
873 #endif
874
875 #if PHP_HTTP_HAVE_IDN2
876 static ZEND_RESULT_CODE parse_idn2(struct parse_state *state, size_t prev_len)
877 {
878 char *idn = NULL;
879 int rv = -1;
880 TSRMLS_FETCH_FROM_CTX(state->ts);
881
882 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
883 rv = idn2_lookup_u8((const unsigned char *) state->url.host, (unsigned char **) &idn, IDN2_NFC_INPUT);
884 }
885 # ifdef PHP_HTTP_HAVE_WCHAR
886 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
887 rv = idn2_lookup_ul(state->url.host, &idn, 0);
888 }
889 # endif
890 if (rv != IDN2_OK) {
891 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; %s", idn2_strerror(rv));
892 return FAILURE;
893 } else {
894 size_t idnlen = strlen(idn);
895 memcpy(state->url.host, idn, idnlen + 1);
896 free(idn);
897 state->offset += idnlen - prev_len;
898 return SUCCESS;
899 }
900 }
901 #elif PHP_HTTP_HAVE_IDN
902 static ZEND_RESULT_CODE parse_idn(struct parse_state *state, size_t prev_len)
903 {
904 char *idn = NULL;
905 int rv = -1;
906 TSRMLS_FETCH_FROM_CTX(state->ts);
907
908 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
909 rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
910 }
911 # ifdef PHP_HTTP_HAVE_WCHAR
912 else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
913 rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
914 }
915 # endif
916 if (rv != IDNA_SUCCESS) {
917 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv));
918 return FAILURE;
919 } else {
920 size_t idnlen = strlen(idn);
921 memcpy(state->url.host, idn, idnlen + 1);
922 free(idn);
923 state->offset += idnlen - prev_len;
924 return SUCCESS;
925 }
926 }
927 #endif
928
929 #ifdef HAVE_UIDNA_IDNTOASCII
930 # if HAVE_UNICODE_UIDNA_H
931 # include <unicode/uidna.h>
932 # else
933 typedef uint16_t UChar;
934 typedef enum { U_ZERO_ERROR = 0 } UErrorCode;
935 int32_t uidna_IDNToASCII(const UChar *src, int32_t srcLength, UChar *dest, int32_t destCapacity, int32_t options, void *parseError, UErrorCode *status);
936 # endif
937 static ZEND_RESULT_CODE parse_uidn(struct parse_state *state)
938 {
939 char *host_ptr;
940 uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
941 size_t uhost_len, ahost_len;
942 UErrorCode error = U_ZERO_ERROR;
943 TSRMLS_FETCH_FROM_CTX(state->ts);
944
945 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
946 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len TSRMLS_CC)) {
947 return FAILURE;
948 }
949 #ifdef PHP_HTTP_HAVE_WCHAR
950 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
951 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len TSRMLS_CC)) {
952 return FAILURE;
953 }
954 #endif
955 } else {
956 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; codepage not specified");
957 return FAILURE;
958 }
959
960 ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN, 3, NULL, &error);
961 efree(uhost_str);
962
963 if (error != U_ZERO_ERROR) {
964 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; ICU error %d", error);
965 return FAILURE;
966 }
967
968 host_ptr = state->url.host;
969 ahost_ptr = ahost_str;
970 PHP_HTTP_DUFF(ahost_len, *host_ptr++ = *ahost_ptr++);
971
972 *host_ptr = '\0';
973 state->offset += host_ptr - state->url.host;
974
975 return SUCCESS;
976 }
977 #endif
978
979 #if 0 && defined(PHP_WIN32)
980 static ZEND_RESULT_CODE parse_widn(struct parse_state *state)
981 {
982 char *host_ptr;
983 uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
984 size_t uhost_len;
985 TSRMLS_FETCH_FROM_CTX(state->ts);
986
987 if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
988 if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
989 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
990 return FAILURE;
991 }
992 #ifdef PHP_HTTP_HAVE_WCHAR
993 } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
994 if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
995 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
996 return FAILURE;
997 }
998 #endif
999 } else {
1000 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
1001 return FAILURE;
1002 }
1003
1004 if (!IdnToAscii(IDN_ALLOW_UNASSIGNED|IDN_USE_STD3_ASCII_RULES, uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN)) {
1005 efree(uhost_str);
1006 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
1007 return FAILURE;
1008 }
1009
1010 efree(uhost_str);
1011 host_ptr = state->url.host;
1012 ahost_ptr = ahost_str;
1013 PHP_HTTP_DUFF(wcslen(ahost_str), *host_ptr++ = *ahost_ptr++);
1014 efree(ahost_str);
1015
1016 *host_ptr = '\0';
1017 state->offset += host_ptr - state->url.host;
1018
1019 return SUCCESS;
1020 }
1021 #endif
1022
1023 static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *ptr)
1024 {
1025 size_t mb, len;
1026 const char *end = state->ptr, *tmp = ptr, *port = NULL;
1027 TSRMLS_FETCH_FROM_CTX(state->ts);
1028
1029 #ifdef HAVE_INET_PTON
1030 if (*ptr == '[') {
1031 char *error = NULL, *tmp = memchr(ptr, ']', end - ptr);
1032
1033 if (tmp) {
1034 size_t addrlen = tmp - ptr + 1;
1035 char buf[16], *addr = estrndup(ptr + 1, addrlen - 2);
1036 int rv = inet_pton(AF_INET6, addr, buf);
1037
1038 efree(addr);
1039 if (rv == 1) {
1040 state->buffer[state->offset] = '[';
1041 state->url.host = &state->buffer[state->offset];
1042 inet_ntop(AF_INET6, buf, state->url.host + 1, state->maxlen - state->offset);
1043 state->offset += strlen(state->url.host);
1044 state->buffer[state->offset++] = ']';
1045 state->buffer[state->offset++] = 0;
1046 ptr = tmp + 1;
1047 } else if (rv == -1) {
1048 error = strerror(errno);
1049 } else {
1050 error = "unexpected '['";
1051 }
1052 } else {
1053 error = "expected ']'";
1054 }
1055
1056 if (error) {
1057 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse hostinfo; %s", error);
1058 return FAILURE;
1059 }
1060 }
1061 #endif
1062 if (ptr != end) do {
1063 switch (*ptr) {
1064 case ':':
1065 if (port) {
1066 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1067 "Failed to parse port; unexpected ':' at pos %u in '%s'",
1068 (unsigned) (ptr - tmp), tmp);
1069 return FAILURE;
1070 }
1071 port = ptr + 1;
1072 break;
1073
1074 case '%':
1075 if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) {
1076 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1077 "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'",
1078 (unsigned) (ptr - tmp), tmp);
1079 return FAILURE;
1080 }
1081 state->buffer[state->offset++] = *ptr++;
1082 state->buffer[state->offset++] = *ptr++;
1083 state->buffer[state->offset++] = *ptr;
1084 break;
1085
1086 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1087 case '+': case ',': case ';': case '=': /* sub-delims */
1088 case '-': case '.': case '_': case '~': /* unreserved */
1089 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1090 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1091 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1092 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1093 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1094 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1095 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1096 case 'v': case 'w': case 'x': case 'y': case 'z':
1097 if (port) {
1098 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1099 "Failed to parse port; unexpected char '%c' at pos %u in '%s'",
1100 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1101 return FAILURE;
1102 }
1103 /* no break */
1104 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1105 case '7': case '8': case '9':
1106 /* allowed */
1107 if (port) {
1108 state->url.port *= 10;
1109 state->url.port += *ptr - '0';
1110 } else {
1111 state->buffer[state->offset++] = *ptr;
1112 }
1113 break;
1114
1115 default:
1116 if (ptr == end) {
1117 break;
1118 } else if (port) {
1119 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1120 "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'",
1121 (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
1122 return FAILURE;
1123 } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, 0))) {
1124 return FAILURE;
1125 }
1126 ptr += mb - 1;
1127 }
1128 } while (++ptr != end);
1129
1130 if (!state->url.host) {
1131 len = (port ? port - tmp - 1 : end - tmp);
1132 state->url.host = &state->buffer[state->offset - len];
1133 state->buffer[state->offset++] = 0;
1134 }
1135
1136 if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
1137 #if PHP_HTTP_HAVE_IDN2
1138 return parse_idn2(state, len);
1139 #elif PHP_HTTP_HAVE_IDN
1140 return parse_idn(state, len);
1141 #endif
1142 #ifdef HAVE_UIDNA_IDNTOASCII
1143 return parse_uidn(state);
1144 #endif
1145 #if 0 && defined(PHP_WIN32)
1146 return parse_widn(state);
1147 #endif
1148 }
1149
1150 return SUCCESS;
1151 }
1152
1153 static const char *parse_authority(struct parse_state *state)
1154 {
1155 const char *tmp = state->ptr, *host = NULL;
1156
1157 do {
1158 switch (*state->ptr) {
1159 case '@':
1160 /* userinfo delimiter */
1161 if (host) {
1162 TSRMLS_FETCH_FROM_CTX(state->ts);
1163 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1164 "Failed to parse userinfo; unexpected '@'");
1165 return NULL;
1166 }
1167 host = state->ptr + 1;
1168 if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) {
1169 return NULL;
1170 }
1171 tmp = state->ptr + 1;
1172 break;
1173
1174 case '/':
1175 case '?':
1176 case '#':
1177 case '\0':
1178 EOD:
1179 /* host delimiter */
1180 if (tmp != state->ptr && SUCCESS != parse_hostinfo(state, tmp)) {
1181 return NULL;
1182 }
1183 return state->ptr;
1184 }
1185 } while (++state->ptr <= state->end);
1186
1187 --state->ptr;
1188 goto EOD;
1189 }
1190
1191 static const char *parse_path(struct parse_state *state)
1192 {
1193 size_t mb;
1194 const char *tmp;
1195 TSRMLS_FETCH_FROM_CTX(state->ts);
1196
1197 /* is there actually a path to parse? */
1198 if (!*state->ptr) {
1199 return state->ptr;
1200 }
1201 tmp = state->ptr;
1202 state->url.path = &state->buffer[state->offset];
1203
1204 do {
1205 switch (*state->ptr) {
1206 case '#':
1207 case '?':
1208 goto done;
1209
1210 case '%':
1211 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1212 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1213 "Failed to parse path; invalid percent encoding at pos %u in '%s'",
1214 (unsigned) (state->ptr - tmp), tmp);
1215 return NULL;
1216 }
1217 state->buffer[state->offset++] = *state->ptr++;
1218 state->buffer[state->offset++] = *state->ptr++;
1219 state->buffer[state->offset++] = *state->ptr;
1220 break;
1221
1222 case '/': /* yeah, well */
1223 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1224 case '+': case ',': case ';': case '=': /* sub-delims */
1225 case '-': case '.': case '_': case '~': /* unreserved */
1226 case ':': case '@': /* pchar */
1227 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1228 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1229 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1230 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1231 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1232 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1233 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1234 case 'v': case 'w': case 'x': case 'y': case 'z':
1235 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1236 case '7': case '8': case '9':
1237 /* allowed */
1238 state->buffer[state->offset++] = *state->ptr;
1239 break;
1240
1241 default:
1242 if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) {
1243 return NULL;
1244 }
1245 state->ptr += mb - 1;
1246 }
1247 } while (++state->ptr < state->end);
1248
1249 done:
1250 /* did we have any path component ? */
1251 if (tmp != state->ptr) {
1252 state->buffer[state->offset++] = 0;
1253 } else {
1254 state->url.path = NULL;
1255 }
1256 return state->ptr;
1257 }
1258
1259 static const char *parse_query(struct parse_state *state)
1260 {
1261 size_t mb;
1262 const char *tmp = state->ptr + !!*state->ptr;
1263 TSRMLS_FETCH_FROM_CTX(state->ts);
1264
1265 /* is there actually a query to parse? */
1266 if (*state->ptr != '?') {
1267 return state->ptr;
1268 }
1269
1270 /* skip initial '?' */
1271 tmp = ++state->ptr;
1272 state->url.query = &state->buffer[state->offset];
1273
1274 while (state->ptr < state->end) {
1275 switch (*state->ptr) {
1276 case '#':
1277 goto done;
1278
1279 case '%':
1280 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1281 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1282 "Failed to parse query; invalid percent encoding at pos %u in '%s'",
1283 (unsigned) (state->ptr - tmp), tmp);
1284 return NULL;
1285 }
1286 state->buffer[state->offset++] = *state->ptr++;
1287 state->buffer[state->offset++] = *state->ptr++;
1288 state->buffer[state->offset++] = *state->ptr;
1289 break;
1290
1291 case ']':
1292 case '[':
1293 if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) {
1294 state->buffer[state->offset++] = '%';
1295 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4];
1296 state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) & 0xf];
1297 break;
1298 }
1299 /* no break */
1300
1301 case '?': case '/': /* yeah, well */
1302 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1303 case '+': case ',': case ';': case '=': /* sub-delims */
1304 case '-': case '.': case '_': case '~': /* unreserved */
1305 case ':': case '@': /* pchar */
1306 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1307 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1308 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1309 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1310 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1311 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1312 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1313 case 'v': case 'w': case 'x': case 'y': case 'z':
1314 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1315 case '7': case '8': case '9':
1316 /* allowed */
1317 state->buffer[state->offset++] = *state->ptr;
1318 break;
1319
1320 default:
1321 if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) {
1322 return NULL;
1323 }
1324 state->ptr += mb - 1;
1325 }
1326
1327 ++state->ptr;
1328 }
1329
1330 done:
1331 state->buffer[state->offset++] = 0;
1332 return state->ptr;
1333 }
1334
1335 static const char *parse_fragment(struct parse_state *state)
1336 {
1337 size_t mb;
1338 const char *tmp;
1339 TSRMLS_FETCH_FROM_CTX(state->ts);
1340
1341 /* is there actually a fragment to parse? */
1342 if (*state->ptr != '#') {
1343 return state->ptr;
1344 }
1345
1346 /* skip initial '#' */
1347 tmp = ++state->ptr;
1348 state->url.fragment = &state->buffer[state->offset];
1349
1350 do {
1351 switch (*state->ptr) {
1352 case '%':
1353 if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) {
1354 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1355 "Failed to parse fragment; invalid percent encoding at pos %u in '%s'",
1356 (unsigned) (state->ptr - tmp), tmp);
1357 return NULL;
1358 }
1359 state->buffer[state->offset++] = *state->ptr++;
1360 state->buffer[state->offset++] = *state->ptr++;
1361 state->buffer[state->offset++] = *state->ptr;
1362 break;
1363
1364 case '?': case '/':
1365 case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
1366 case '+': case ',': case ';': case '=': /* sub-delims */
1367 case '-': case '.': case '_': case '~': /* unreserved */
1368 case ':': case '@': /* pchar */
1369 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1370 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1371 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1372 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1373 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1374 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1375 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1376 case 'v': case 'w': case 'x': case 'y': case 'z':
1377 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1378 case '7': case '8': case '9':
1379 /* allowed */
1380 state->buffer[state->offset++] = *state->ptr;
1381 break;
1382
1383 default:
1384 if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) {
1385 return NULL;
1386 }
1387 state->ptr += mb - 1;
1388 }
1389 } while (++state->ptr < state->end);
1390
1391 state->buffer[state->offset++] = 0;
1392 return state->ptr;
1393 }
1394
1395 static const char *parse_hier(struct parse_state *state)
1396 {
1397 if (*state->ptr == '/') {
1398 if (state->end - state->ptr > 1) {
1399 if (*(state->ptr + 1) == '/') {
1400 state->ptr += 2;
1401 if (!(state->ptr = parse_authority(state))) {
1402 return NULL;
1403 }
1404 }
1405 }
1406 }
1407 return parse_path(state);
1408 }
1409
1410 static const char *parse_scheme(struct parse_state *state)
1411 {
1412 size_t mb;
1413 const char *tmp = state->ptr;
1414
1415 do {
1416 switch (*state->ptr) {
1417 case ':':
1418 /* scheme delimiter */
1419 state->url.scheme = &state->buffer[0];
1420 state->buffer[state->offset++] = 0;
1421 return ++state->ptr;
1422
1423 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
1424 case '7': case '8': case '9':
1425 case '+': case '-': case '.':
1426 if (state->ptr == tmp) {
1427 return tmp;
1428 }
1429 /* no break */
1430 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1431 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1432 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1433 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1434 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1435 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1436 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1437 case 'v': case 'w': case 'x': case 'y': case 'z':
1438 /* scheme part */
1439 state->buffer[state->offset++] = *state->ptr;
1440 break;
1441
1442 default:
1443 if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
1444 /* soft fail; parse path next */
1445 return tmp;
1446 }
1447 state->ptr += mb - 1;
1448 }
1449 } while (++state->ptr != state->end);
1450
1451 return state->ptr = tmp;
1452 }
1453
1454 php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags TSRMLS_DC)
1455 {
1456 size_t maxlen = 3 * len;
1457 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1458
1459 state->end = str + len;
1460 state->ptr = str;
1461 state->flags = flags;
1462 state->maxlen = maxlen;
1463 TSRMLS_SET_CTX(state->ts);
1464
1465 if (!parse_scheme(state)) {
1466 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
1467 efree(state);
1468 return NULL;
1469 }
1470
1471 if (!parse_hier(state)) {
1472 efree(state);
1473 return NULL;
1474 }
1475
1476 if (!parse_query(state)) {
1477 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
1478 efree(state);
1479 return NULL;
1480 }
1481
1482 if (!parse_fragment(state)) {
1483 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
1484 efree(state);
1485 return NULL;
1486 }
1487
1488 return (php_http_url_t *) state;
1489 }
1490
1491 php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags TSRMLS_DC)
1492 {
1493 size_t maxlen = 3 * len;
1494 struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
1495
1496 state->end = str + len;
1497 state->ptr = str;
1498 state->flags = flags;
1499 state->maxlen = maxlen;
1500 TSRMLS_SET_CTX(state->ts);
1501
1502 if (!(state->ptr = parse_authority(state))) {
1503 efree(state);
1504 return NULL;
1505 }
1506
1507 if (state->ptr != state->end) {
1508 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1509 "Failed to parse URL authority, unexpected character at pos %u in '%s'",
1510 (unsigned) (state->ptr - str), str);
1511 efree(state);
1512 return NULL;
1513 }
1514
1515 return (php_http_url_t *) state;
1516 }
1517
1518 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0)
1519 ZEND_ARG_INFO(0, old_url)
1520 ZEND_ARG_INFO(0, new_url)
1521 ZEND_ARG_INFO(0, flags)
1522 ZEND_END_ARG_INFO();
1523 PHP_METHOD(HttpUrl, __construct)
1524 {
1525 zval *new_url = NULL, *old_url = NULL;
1526 long flags = PHP_HTTP_URL_FROM_ENV;
1527 zend_error_handling zeh;
1528
1529 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return);
1530
1531 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC);
1532 {
1533 php_http_url_t *res_purl, *new_purl = NULL, *old_purl = NULL;
1534
1535 if (new_url) {
1536 new_purl = php_http_url_from_zval(new_url, flags TSRMLS_CC);
1537 if (!new_purl) {
1538 zend_restore_error_handling(&zeh TSRMLS_CC);
1539 return;
1540 }
1541 }
1542 if (old_url) {
1543 old_purl = php_http_url_from_zval(old_url, flags TSRMLS_CC);
1544 if (!old_purl) {
1545 if (new_purl) {
1546 php_http_url_free(&new_purl);
1547 }
1548 zend_restore_error_handling(&zeh TSRMLS_CC);
1549 return;
1550 }
1551 }
1552
1553 res_purl = php_http_url_mod(old_purl, new_purl, flags TSRMLS_CC);
1554 php_http_url_to_struct(res_purl, getThis() TSRMLS_CC);
1555
1556 php_http_url_free(&res_purl);
1557 if (old_purl) {
1558 php_http_url_free(&old_purl);
1559 }
1560 if (new_purl) {
1561 php_http_url_free(&new_purl);
1562 }
1563 }
1564 zend_restore_error_handling(&zeh TSRMLS_CC);
1565 }
1566
1567 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod, 0, 0, 1)
1568 ZEND_ARG_INFO(0, more_url_parts)
1569 ZEND_ARG_INFO(0, flags)
1570 ZEND_END_ARG_INFO();
1571 PHP_METHOD(HttpUrl, mod)
1572 {
1573 zval *new_url = NULL;
1574 long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY | PHP_HTTP_URL_SANITIZE_PATH;
1575 zend_error_handling zeh;
1576
1577 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!|l", &new_url, &flags), invalid_arg, return);
1578
1579 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC);
1580 {
1581 php_http_url_t *new_purl = NULL, *old_purl = NULL;
1582
1583 if (new_url) {
1584 new_purl = php_http_url_from_zval(new_url, flags TSRMLS_CC);
1585 if (!new_purl) {
1586 zend_restore_error_handling(&zeh TSRMLS_CC);
1587 return;
1588 }
1589 }
1590
1591 if ((old_purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1592 php_http_url_t *res_purl;
1593
1594 ZVAL_OBJVAL(return_value, zend_objects_clone_obj(getThis() TSRMLS_CC), 0);
1595
1596 res_purl = php_http_url_mod(old_purl, new_purl, flags TSRMLS_CC);
1597 php_http_url_to_struct(res_purl, return_value TSRMLS_CC);
1598
1599 php_http_url_free(&res_purl);
1600 php_http_url_free(&old_purl);
1601 }
1602 if (new_purl) {
1603 php_http_url_free(&new_purl);
1604 }
1605 }
1606 zend_restore_error_handling(&zeh TSRMLS_CC);
1607 }
1608
1609 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString, 0, 0, 0)
1610 ZEND_END_ARG_INFO();
1611 PHP_METHOD(HttpUrl, toString)
1612 {
1613 if (SUCCESS == zend_parse_parameters_none()) {
1614 php_http_url_t *purl;
1615
1616 if ((purl = php_http_url_from_struct(HASH_OF(getThis())))) {
1617 char *str;
1618 size_t len;
1619
1620 php_http_url_to_string(purl, &str, &len, 0);
1621 php_http_url_free(&purl);
1622 RETURN_STRINGL(str, len, 0);
1623 }
1624 }
1625 RETURN_EMPTY_STRING();
1626 }
1627
1628 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray, 0, 0, 0)
1629 ZEND_END_ARG_INFO();
1630 PHP_METHOD(HttpUrl, toArray)
1631 {
1632 php_http_url_t *purl;
1633
1634 if (SUCCESS != zend_parse_parameters_none()) {
1635 return;
1636 }
1637
1638 /* strip any non-URL properties */
1639 purl = php_http_url_from_struct(HASH_OF(getThis()));
1640 php_http_url_to_struct(purl, return_value TSRMLS_CC);
1641 php_http_url_free(&purl);
1642 }
1643
1644 static zend_function_entry php_http_url_methods[] = {
1645 PHP_ME(HttpUrl, __construct, ai_HttpUrl___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1646 PHP_ME(HttpUrl, mod, ai_HttpUrl_mod, ZEND_ACC_PUBLIC)
1647 PHP_ME(HttpUrl, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
1648 ZEND_MALIAS(HttpUrl, __toString, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC)
1649 PHP_ME(HttpUrl, toArray, ai_HttpUrl_toArray, ZEND_ACC_PUBLIC)
1650 EMPTY_FUNCTION_ENTRY
1651 };
1652
1653 zend_class_entry *php_http_url_class_entry;
1654
1655 PHP_MINIT_FUNCTION(http_url)
1656 {
1657 zend_class_entry ce = {0};
1658
1659 INIT_NS_CLASS_ENTRY(ce, "http", "Url", php_http_url_methods);
1660 php_http_url_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
1661
1662 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC TSRMLS_CC);
1663 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC);
1664 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC);
1665 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC);
1666 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC);
1667 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("path"), ZEND_ACC_PUBLIC TSRMLS_CC);
1668 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC TSRMLS_CC);
1669 zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("fragment"), ZEND_ACC_PUBLIC TSRMLS_CC);
1670
1671 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("REPLACE"), PHP_HTTP_URL_REPLACE TSRMLS_CC);
1672 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_PATH"), PHP_HTTP_URL_JOIN_PATH TSRMLS_CC);
1673 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("JOIN_QUERY"), PHP_HTTP_URL_JOIN_QUERY TSRMLS_CC);
1674 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_USER"), PHP_HTTP_URL_STRIP_USER TSRMLS_CC);
1675 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PASS"), PHP_HTTP_URL_STRIP_PASS TSRMLS_CC);
1676 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_AUTH"), PHP_HTTP_URL_STRIP_AUTH TSRMLS_CC);
1677 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PORT"), PHP_HTTP_URL_STRIP_PORT TSRMLS_CC);
1678 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_PATH"), PHP_HTTP_URL_STRIP_PATH TSRMLS_CC);
1679 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_QUERY"), PHP_HTTP_URL_STRIP_QUERY TSRMLS_CC);
1680 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_FRAGMENT"), PHP_HTTP_URL_STRIP_FRAGMENT TSRMLS_CC);
1681 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STRIP_ALL"), PHP_HTTP_URL_STRIP_ALL TSRMLS_CC);
1682 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV TSRMLS_CC);
1683 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH TSRMLS_CC);
1684
1685 #ifdef PHP_HTTP_HAVE_WCHAR
1686 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC TSRMLS_CC);
1687 #endif
1688 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8 TSRMLS_CC);
1689 #if defined(PHP_HTTP_HAVE_IDN2) || defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
1690 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN TSRMLS_CC);
1691 #endif
1692 zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT TSRMLS_CC);
1693
1694 return SUCCESS;
1695 }
1696
1697
1698 /*
1699 * Local variables:
1700 * tab-width: 4
1701 * c-basic-offset: 4
1702 * End:
1703 * vim600: noet sw=4 ts=4 fdm=marker
1704 * vim<600: noet sw=4 ts=4
1705 */
1706