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_url_api.h"
26 static inline char *localhostname(void)
28 char hostname
[1024] = {0};
31 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
32 return estrdup(hostname
);
34 #elif defined(HAVE_UNISTD_H)
35 if (SUCCESS
== gethostname(hostname
, lenof(hostname
))) {
36 size_t hlen
= strlen(hostname
);
38 if (hlen
<= lenof(hostname
) - lenof("(none)")) {
39 hostname
[hlen
++] = '.';
40 if (SUCCESS
== getdomainname(&hostname
[hlen
], lenof(hostname
) - hlen
)) {
41 if (!strcmp(&hostname
[hlen
], "(none)")) {
42 hostname
[hlen
- 1] = '\0';
44 return estrdup(hostname
);
49 return estrdup("localhost");
52 PHP_MINIT_FUNCTION(http_url
)
54 HTTP_LONG_CONSTANT("HTTP_URL_REPLACE", HTTP_URL_REPLACE
);
55 HTTP_LONG_CONSTANT("HTTP_URL_JOIN_PATH", HTTP_URL_JOIN_PATH
);
56 HTTP_LONG_CONSTANT("HTTP_URL_JOIN_QUERY", HTTP_URL_JOIN_QUERY
);
57 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_USER", HTTP_URL_STRIP_USER
);
58 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PASS", HTTP_URL_STRIP_PASS
);
59 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_AUTH", HTTP_URL_STRIP_AUTH
);
60 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PORT", HTTP_URL_STRIP_PORT
);
61 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PATH", HTTP_URL_STRIP_PATH
);
62 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_QUERY", HTTP_URL_STRIP_QUERY
);
63 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_FRAGMENT", HTTP_URL_STRIP_FRAGMENT
);
67 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
69 char *abs
= estrdup(url
);
70 php_url
*purl
= php_url_parse(abs
);
75 http_build_url(0, purl
, NULL
, NULL
, &abs
, NULL
);
78 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", url
);
84 /* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */
85 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
)
87 #ifdef HTTP_HAVE_NETDB
90 php_url
*url
= ecalloc(1, sizeof(php_url
));
92 #define __URLSET(u,n) \
95 url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL)
97 if (!(flags
& HTTP_URL_STRIP_PORT
)) {
98 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
100 if ((!(flags
& HTTP_URL_STRIP_AUTH
)) && (!(flags
& HTTP_URL_STRIP_USER
))) {
103 if ((!(flags
& HTTP_URL_STRIP_AUTH
)) && (!(flags
& HTTP_URL_STRIP_PASS
))) {
110 if (!(flags
& HTTP_URL_STRIP_PATH
)) {
111 if ((flags
& HTTP_URL_JOIN_PATH
) && __URLSET(old_url
, path
) && __URLSET(new_url
, path
) && *new_url
->path
!= '/') {
112 size_t old_path_len
= strlen(old_url
->path
), new_path_len
= strlen(new_url
->path
);
114 url
->path
= ecalloc(1, old_path_len
+ new_path_len
+ 1 + 1);
116 strcat(url
->path
, old_url
->path
);
117 if (url
->path
[old_path_len
- 1] != '/') {
118 php_dirname(url
->path
, old_path_len
);
119 strcat(url
->path
, "/");
121 strcat(url
->path
, new_url
->path
);
126 if (!(flags
& HTTP_URL_STRIP_QUERY
)) {
127 if ((flags
& HTTP_URL_JOIN_QUERY
) && __URLSET(new_url
, query
) && __URLSET(old_url
, query
)) {
128 url
->query
= ecalloc(1, strlen(new_url
->query
) + strlen(old_url
->query
) + 1 + 1);
129 strcat(url
->query
, old_url
->query
);
130 strcat(url
->query
, "&");
131 strcat(url
->query
, new_url
->query
);
136 if (!(flags
& HTTP_URL_STRIP_FRAGMENT
)) {
144 url
->scheme
= estrndup("https", lenof("https"));
147 #ifndef HTTP_HAVE_NETDB
151 url
->scheme
= estrndup("http", lenof("http"));
154 #ifdef HTTP_HAVE_NETDB
156 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
157 url
->scheme
= estrdup(se
->s_name
);
159 url
->scheme
= estrndup("http", lenof("http"));
169 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
170 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
171 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
173 url
->host
= localhostname();
178 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
179 const char *q
= strchr(SG(request_info
).request_uri
, '?');
182 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
184 url
->path
= estrdup(SG(request_info
).request_uri
);
187 url
->path
= estrndup("/", 1);
189 } else if (*url
->path
!= '/') {
190 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
191 const char *q
= strchr(SG(request_info
).request_uri
, '?');
196 uri
= estrndup(SG(request_info
).request_uri
, len
= q
- SG(request_info
).request_uri
);
198 uri
= estrndup(SG(request_info
).request_uri
, len
= strlen(SG(request_info
).request_uri
));
201 php_dirname(uri
, len
);
202 spprintf(&path
, 0, "%s/%s", uri
, url
->path
);
204 STR_SET(url
->path
, path
);
208 spprintf(&uri
, 0, "/%s", url
->path
);
209 STR_SET(url
->path
, uri
);
213 char *ptr
, *end
= url
->path
+ strlen(url
->path
) + 1;
215 for (ptr
= strstr(url
->path
, "/."); ptr
; ptr
= strstr(ptr
, "/.")) {
223 memmove(&ptr
[1], &ptr
[3], end
- &ptr
[3]);
229 while (ptr
!= url
->path
) {
234 memmove(&ptr
[1], pos
, end
- pos
);
247 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
248 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
249 #ifdef HTTP_HAVE_NETDB
250 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
251 (url
->port
== ntohs(se
->s_port
)))
261 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
264 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
265 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
267 if (url
->user
&& *url
->user
) {
268 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
269 if (url
->pass
&& *url
->pass
) {
270 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
271 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
273 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
276 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
279 char port_str
[6] = {0};
281 snprintf(port_str
, 5, "%d", (int) url
->port
);
282 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
283 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
286 if (*url
->path
!= '/') {
287 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
289 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
291 if (url
->query
&& *url
->query
) {
292 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
293 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
296 if (url
->fragment
&& *url
->fragment
) {
297 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
298 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
301 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
302 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
317 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
318 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
319 char *pre_encoded_data
, size_t pre_encoded_len
,
320 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
324 phpstr
*qstr
= phpstr_new();
326 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
327 arg_sep
= HTTP_URL_ARGSEP
;
328 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
331 if (pre_encoded_len
&& pre_encoded_data
) {
332 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
335 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
340 phpstr_data(qstr
, encoded_data
, encoded_len
);
347 /* {{{ http_urlencode_hash_recursive */
348 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
)
357 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
360 if (ht
->nApplyCount
> 0) {
364 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
369 if (!data
|| !*data
) {
375 if (len
&& key
[len
- 1] == '\0') {
378 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
381 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
385 phpstr_init(&new_prefix
);
386 if (prefix
&& prefix_len
) {
387 phpstr_append(&new_prefix
, prefix
, prefix_len
);
388 phpstr_appends(&new_prefix
, "[");
391 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
394 if (prefix
&& prefix_len
) {
395 phpstr_appends(&new_prefix
, "]");
397 phpstr_fix(&new_prefix
);
400 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
403 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
405 if (SUCCESS
!= status
) {
406 phpstr_dtor(&new_prefix
);
411 zval
*val
= zval_copy(IS_STRING
, *data
);
413 if (PHPSTR_LEN(str
)) {
414 phpstr_append(str
, arg_sep
, arg_sep_len
);
416 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
417 phpstr_appends(str
, "=");
419 if (Z_STRLEN_P(val
) && Z_STRVAL_P(val
)) {
423 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
424 phpstr_append(str
, encoded_val
, encoded_len
);
430 phpstr_dtor(&new_prefix
);
441 * vim600: noet sw=4 ts=4 fdm=marker
442 * vim<600: noet sw=4 ts=4