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