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
);
68 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
74 purl
= php_url_parse(abs
= estrdup(url
));
77 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", url
);
82 http_build_url(0, purl
, NULL
, NULL
, &abs
, NULL
);
91 /* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */
92 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
)
94 #ifdef HTTP_HAVE_NETDB
97 php_url
*url
= ecalloc(1, sizeof(php_url
));
99 #define __URLSET(u,n) \
101 #define __URLCPY(n) \
102 url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL)
104 if (!(flags
& HTTP_URL_STRIP_PORT
)) {
105 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
107 if ((!(flags
& HTTP_URL_STRIP_AUTH
)) && (!(flags
& HTTP_URL_STRIP_USER
))) {
110 if ((!(flags
& HTTP_URL_STRIP_AUTH
)) && (!(flags
& HTTP_URL_STRIP_PASS
))) {
117 if (!(flags
& HTTP_URL_STRIP_PATH
)) {
118 if ((flags
& HTTP_URL_JOIN_PATH
) && __URLSET(old_url
, path
) && __URLSET(new_url
, path
) && *new_url
->path
!= '/') {
119 size_t old_path_len
= strlen(old_url
->path
), new_path_len
= strlen(new_url
->path
);
121 url
->path
= ecalloc(1, old_path_len
+ new_path_len
+ 1 + 1);
123 strcat(url
->path
, old_url
->path
);
124 if (url
->path
[old_path_len
- 1] != '/') {
125 php_dirname(url
->path
, old_path_len
);
126 strcat(url
->path
, "/");
128 strcat(url
->path
, new_url
->path
);
133 if (!(flags
& HTTP_URL_STRIP_QUERY
)) {
134 if ((flags
& HTTP_URL_JOIN_QUERY
) && __URLSET(new_url
, query
) && __URLSET(old_url
, query
)) {
141 ZVAL_STRING(&qstr
, old_url
->query
, 0);
142 http_querystring_modify(&qarr
, &qstr
);
143 ZVAL_STRING(&qstr
, new_url
->query
, 0);
144 http_querystring_modify(&qarr
, &qstr
);
147 http_querystring_update(&qarr
, &qstr
);
148 url
->query
= Z_STRVAL(qstr
);
154 if (!(flags
& HTTP_URL_STRIP_FRAGMENT
)) {
162 url
->scheme
= estrndup("https", lenof("https"));
165 #ifndef HTTP_HAVE_NETDB
169 url
->scheme
= estrndup("http", lenof("http"));
172 #ifdef HTTP_HAVE_NETDB
174 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
175 url
->scheme
= estrdup(se
->s_name
);
177 url
->scheme
= estrndup("http", lenof("http"));
187 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
188 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
189 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
191 url
->host
= localhostname();
196 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
197 const char *q
= strchr(SG(request_info
).request_uri
, '?');
200 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
202 url
->path
= estrdup(SG(request_info
).request_uri
);
205 url
->path
= estrndup("/", 1);
207 } else if (*url
->path
!= '/') {
208 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
209 const char *q
= strchr(SG(request_info
).request_uri
, '?');
214 uri
= estrndup(SG(request_info
).request_uri
, len
= q
- SG(request_info
).request_uri
);
216 uri
= estrndup(SG(request_info
).request_uri
, len
= strlen(SG(request_info
).request_uri
));
219 php_dirname(uri
, len
);
220 spprintf(&path
, 0, "%s/%s", uri
, url
->path
);
222 STR_SET(url
->path
, path
);
226 spprintf(&uri
, 0, "/%s", url
->path
);
227 STR_SET(url
->path
, uri
);
231 char *ptr
, *end
= url
->path
+ strlen(url
->path
) + 1;
233 for (ptr
= strstr(url
->path
, "/."); ptr
; ptr
= strstr(ptr
, "/.")) {
241 memmove(&ptr
[1], &ptr
[3], end
- &ptr
[3]);
247 while (ptr
!= url
->path
) {
252 memmove(&ptr
[1], pos
, end
- pos
);
265 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
266 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
267 #ifdef HTTP_HAVE_NETDB
268 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
269 (url
->port
== ntohs(se
->s_port
)))
279 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
282 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
283 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
285 if (url
->user
&& *url
->user
) {
286 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
287 if (url
->pass
&& *url
->pass
) {
288 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
289 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
291 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
294 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
297 char port_str
[8] = {0};
299 snprintf(port_str
, lenof(port_str
), "%d", (int) url
->port
);
300 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
301 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
304 if (*url
->path
!= '/') {
305 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
307 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
309 if (url
->query
&& *url
->query
) {
310 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
311 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
314 if (url
->fragment
&& *url
->fragment
) {
315 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
316 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
319 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
320 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
335 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
336 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
337 char *pre_encoded_data
, size_t pre_encoded_len
,
338 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
342 phpstr
*qstr
= phpstr_new();
344 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
345 arg_sep
= HTTP_URL_ARGSEP
;
346 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
349 if (pre_encoded_len
&& pre_encoded_data
) {
350 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
353 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
358 phpstr_data(qstr
, encoded_data
, encoded_len
);
365 /* {{{ http_urlencode_hash_recursive */
366 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
)
375 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
378 if (ht
->nApplyCount
> 0) {
382 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
387 if (!data
|| !*data
) {
393 if (len
&& key
[len
- 1] == '\0') {
396 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
399 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
403 phpstr_init(&new_prefix
);
404 if (prefix
&& prefix_len
) {
405 phpstr_append(&new_prefix
, prefix
, prefix_len
);
406 phpstr_appends(&new_prefix
, "%5B");
409 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
412 if (prefix
&& prefix_len
) {
413 phpstr_appends(&new_prefix
, "%5D");
415 phpstr_fix(&new_prefix
);
418 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
421 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
423 if (SUCCESS
!= status
) {
424 phpstr_dtor(&new_prefix
);
429 zval
*val
= zval_copy(IS_STRING
, *data
);
431 if (PHPSTR_LEN(str
)) {
432 phpstr_append(str
, arg_sep
, arg_sep_len
);
434 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
435 phpstr_appends(str
, "=");
437 if (Z_STRLEN_P(val
) && Z_STRVAL_P(val
)) {
441 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
442 phpstr_append(str
, encoded_val
, encoded_len
);
448 phpstr_dtor(&new_prefix
);
459 * vim600: noet sw=4 ts=4 fdm=marker
460 * vim<600: noet sw=4 ts=4