2 +--------------------------------------------------------------------+
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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_NETDB
20 #include "php_output.h"
21 #include "ext/standard/php_string.h"
23 #include "php_http_api.h"
24 #include "php_http_querystring_api.h"
25 #include "php_http_url_api.h"
27 static inline char *localhostname(void)
29 char hostname
[1024] = {0};
32 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
33 return estrdup(hostname
);
35 #elif defined(HAVE_UNISTD_H)
36 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
37 size_t hlen
= strlen(hostname
);
39 if (hlen
<= lenof(hostname
) - lenof("(none)")) {
40 hostname
[hlen
++] = '.';
41 if (SUCCESS
== getdomainname(&hostname
[hlen
], lenof(hostname
) - hlen
)) {
42 if (!strcmp(&hostname
[hlen
], "(none)")) {
43 hostname
[hlen
- 1] = '\0';
45 return estrdup(hostname
);
50 return estrdup("localhost");
53 PHP_MINIT_FUNCTION(http_url
)
55 HTTP_LONG_CONSTANT("HTTP_URL_REPLACE", HTTP_URL_REPLACE
);
56 HTTP_LONG_CONSTANT("HTTP_URL_JOIN_PATH", HTTP_URL_JOIN_PATH
);
57 HTTP_LONG_CONSTANT("HTTP_URL_JOIN_QUERY", HTTP_URL_JOIN_QUERY
);
58 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_USER", HTTP_URL_STRIP_USER
);
59 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PASS", HTTP_URL_STRIP_PASS
);
60 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_AUTH", HTTP_URL_STRIP_AUTH
);
61 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PORT", HTTP_URL_STRIP_PORT
);
62 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PATH", HTTP_URL_STRIP_PATH
);
63 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_QUERY", HTTP_URL_STRIP_QUERY
);
64 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_FRAGMENT", HTTP_URL_STRIP_FRAGMENT
);
65 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_ALL", HTTP_URL_STRIP_ALL
);
69 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
75 purl
= php_url_parse(abs
= estrdup(url
));
78 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", url
);
83 http_build_url(0, purl
, NULL
, NULL
, &abs
, NULL
);
92 /* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */
93 PHP_HTTP_API
void _http_build_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
)
95 #ifdef HTTP_HAVE_NETDB
98 php_url
*url
= ecalloc(1, sizeof(php_url
));
100 #define __URLSET(u,n) \
102 #define __URLCPY(n) \
103 url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL)
105 if (!(flags
& HTTP_URL_STRIP_PORT
)) {
106 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
108 if (!(flags
& HTTP_URL_STRIP_USER
)) {
111 if (!(flags
& HTTP_URL_STRIP_PASS
)) {
118 if (!(flags
& HTTP_URL_STRIP_PATH
)) {
119 if ((flags
& HTTP_URL_JOIN_PATH
) && __URLSET(old_url
, path
) && __URLSET(new_url
, path
) && *new_url
->path
!= '/') {
120 size_t old_path_len
= strlen(old_url
->path
), new_path_len
= strlen(new_url
->path
);
122 url
->path
= ecalloc(1, old_path_len
+ new_path_len
+ 1 + 1);
124 strcat(url
->path
, old_url
->path
);
125 if (url
->path
[old_path_len
- 1] != '/') {
126 php_dirname(url
->path
, old_path_len
);
127 strcat(url
->path
, "/");
129 strcat(url
->path
, new_url
->path
);
134 if (!(flags
& HTTP_URL_STRIP_QUERY
)) {
135 if ((flags
& HTTP_URL_JOIN_QUERY
) && __URLSET(new_url
, query
) && __URLSET(old_url
, query
)) {
142 ZVAL_STRING(&qstr
, old_url
->query
, 0);
143 http_querystring_modify(&qarr
, &qstr
);
144 ZVAL_STRING(&qstr
, new_url
->query
, 0);
145 http_querystring_modify(&qarr
, &qstr
);
148 http_querystring_update(&qarr
, &qstr
);
149 url
->query
= Z_STRVAL(qstr
);
155 if (!(flags
& HTTP_URL_STRIP_FRAGMENT
)) {
160 zval
*https
= http_get_server_var("HTTPS");
161 if (https
&& !strcasecmp(Z_STRVAL_P(https
), "ON")) {
162 url
->scheme
= estrndup("https", lenof("https"));
163 } else switch (url
->port
) {
165 url
->scheme
= estrndup("https", lenof("https"));
168 #ifndef HTTP_HAVE_NETDB
172 url
->scheme
= estrndup("http", lenof("http"));
175 #ifdef HTTP_HAVE_NETDB
177 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
178 url
->scheme
= estrdup(se
->s_name
);
180 url
->scheme
= estrndup("http", lenof("http"));
190 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
191 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
192 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
194 url
->host
= localhostname();
199 if (SG(request_info
).request_uri
&& SG(request_info
).request_uri
[0]) {
200 const char *q
= strchr(SG(request_info
).request_uri
, '?');
203 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
205 url
->path
= estrdup(SG(request_info
).request_uri
);
208 url
->path
= estrndup("/", 1);
210 } else if (url
->path
[0] != '/') {
211 if (SG(request_info
).request_uri
&& SG(request_info
).request_uri
[0]) {
212 size_t ulen
= strlen(SG(request_info
).request_uri
);
213 size_t plen
= strlen(url
->path
);
216 if (SG(request_info
).request_uri
[ulen
-1] != '/') {
217 for (--ulen
; ulen
&& SG(request_info
).request_uri
[ulen
- 1] != '/'; --ulen
);
220 path
= emalloc(ulen
+ plen
+ 1);
221 memcpy(path
, SG(request_info
).request_uri
, ulen
);
222 memcpy(path
+ ulen
, url
->path
, plen
);
223 path
[ulen
+ plen
] = '\0';
224 STR_SET(url
->path
, path
);
226 size_t plen
= strlen(url
->path
);
227 char *path
= emalloc(plen
+ 1 + 1);
230 memcpy(&path
[1], url
->path
, plen
+ 1);
231 STR_SET(url
->path
, path
);
234 /* replace directory references if path is not a single slash */
235 if (url
->path
[0] && (url
->path
[0] != '/' || url
->path
[1])) {
236 char *ptr
, *end
= url
->path
+ strlen(url
->path
) + 1;
238 for (ptr
= strstr(url
->path
, "/."); ptr
; ptr
= strstr(ptr
, "/.")) {
245 memmove(&ptr
[1], &ptr
[3], end
- &ptr
[3]);
251 while (ptr
!= url
->path
) {
256 memmove(&ptr
[1], pos
, end
- pos
);
269 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
270 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
271 #ifdef HTTP_HAVE_NETDB
272 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
273 (url
->port
== ntohs(se
->s_port
)))
283 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
286 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
287 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
289 if (url
->user
&& *url
->user
) {
290 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
291 if (url
->pass
&& *url
->pass
) {
292 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
293 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
295 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
298 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
301 char port_str
[8] = {0};
303 snprintf(port_str
, lenof(port_str
), "%d", (int) url
->port
);
304 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
305 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
308 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
310 if (url
->query
&& *url
->query
) {
311 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
312 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
315 if (url
->fragment
&& *url
->fragment
) {
316 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
317 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
320 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
321 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
336 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
337 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
338 char *pre_encoded_data
, size_t pre_encoded_len
,
339 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
343 phpstr
*qstr
= phpstr_new();
345 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
346 arg_sep
= HTTP_URL_ARGSEP
;
347 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
350 if (pre_encoded_len
&& pre_encoded_data
) {
351 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
354 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
359 phpstr_data(qstr
, encoded_data
, encoded_len
);
366 /* {{{ http_urlencode_hash_recursive */
367 PHP_HTTP_API STATUS
_http_urlencode_hash_recursive(HashTable
*ht
, phpstr
*str
, const char *arg_sep
, size_t arg_sep_len
, const char *prefix
, size_t prefix_len TSRMLS_DC
)
376 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
379 if (ht
->nApplyCount
> 0) {
383 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
388 if (!data
|| !*data
) {
394 if (len
&& key
[len
- 1] == '\0') {
397 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
400 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
404 phpstr_init(&new_prefix
);
405 if (prefix
&& prefix_len
) {
406 phpstr_append(&new_prefix
, prefix
, prefix_len
);
407 phpstr_appends(&new_prefix
, "%5B");
410 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
413 if (prefix
&& prefix_len
) {
414 phpstr_appends(&new_prefix
, "%5D");
416 phpstr_fix(&new_prefix
);
419 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
422 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
424 if (SUCCESS
!= status
) {
425 phpstr_dtor(&new_prefix
);
430 zval
*val
= zval_copy(IS_STRING
, *data
);
432 if (PHPSTR_LEN(str
)) {
433 phpstr_append(str
, arg_sep
, arg_sep_len
);
435 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
436 phpstr_appends(str
, "=");
438 if (Z_STRLEN_P(val
) && Z_STRVAL_P(val
)) {
442 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
443 phpstr_append(str
, encoded_val
, encoded_len
);
449 phpstr_dtor(&new_prefix
);
460 * vim600: noet sw=4 ts=4 fdm=marker
461 * vim<600: noet sw=4 ts=4