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
)
73 purl
= php_url_parse(abs
= estrdup(url
));
76 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", url
);
81 http_build_url(0, purl
, NULL
, NULL
, &abs
, NULL
);
90 /* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */
91 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
)
93 #ifdef HTTP_HAVE_NETDB
96 php_url
*url
= ecalloc(1, sizeof(php_url
));
98 #define __URLSET(u,n) \
100 #define __URLCPY(n) \
101 url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL)
103 if (!(flags
& HTTP_URL_STRIP_PORT
)) {
104 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
106 if ((!(flags
& HTTP_URL_STRIP_AUTH
)) && (!(flags
& HTTP_URL_STRIP_USER
))) {
109 if ((!(flags
& HTTP_URL_STRIP_AUTH
)) && (!(flags
& HTTP_URL_STRIP_PASS
))) {
116 if (!(flags
& HTTP_URL_STRIP_PATH
)) {
117 if ((flags
& HTTP_URL_JOIN_PATH
) && __URLSET(old_url
, path
) && __URLSET(new_url
, path
) && *new_url
->path
!= '/') {
118 size_t old_path_len
= strlen(old_url
->path
), new_path_len
= strlen(new_url
->path
);
120 url
->path
= ecalloc(1, old_path_len
+ new_path_len
+ 1 + 1);
122 strcat(url
->path
, old_url
->path
);
123 if (url
->path
[old_path_len
- 1] != '/') {
124 php_dirname(url
->path
, old_path_len
);
125 strcat(url
->path
, "/");
127 strcat(url
->path
, new_url
->path
);
132 if (!(flags
& HTTP_URL_STRIP_QUERY
)) {
133 if ((flags
& HTTP_URL_JOIN_QUERY
) && __URLSET(new_url
, query
) && __URLSET(old_url
, query
)) {
134 url
->query
= ecalloc(1, strlen(new_url
->query
) + strlen(old_url
->query
) + 1 + 1);
135 strcat(url
->query
, old_url
->query
);
136 strcat(url
->query
, "&");
137 strcat(url
->query
, new_url
->query
);
142 if (!(flags
& HTTP_URL_STRIP_FRAGMENT
)) {
150 url
->scheme
= estrndup("https", lenof("https"));
153 #ifndef HTTP_HAVE_NETDB
157 url
->scheme
= estrndup("http", lenof("http"));
160 #ifdef HTTP_HAVE_NETDB
162 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
163 url
->scheme
= estrdup(se
->s_name
);
165 url
->scheme
= estrndup("http", lenof("http"));
175 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
176 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
177 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
179 url
->host
= localhostname();
184 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
185 const char *q
= strchr(SG(request_info
).request_uri
, '?');
188 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
190 url
->path
= estrdup(SG(request_info
).request_uri
);
193 url
->path
= estrndup("/", 1);
195 } else if (*url
->path
!= '/') {
196 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
197 const char *q
= strchr(SG(request_info
).request_uri
, '?');
202 uri
= estrndup(SG(request_info
).request_uri
, len
= q
- SG(request_info
).request_uri
);
204 uri
= estrndup(SG(request_info
).request_uri
, len
= strlen(SG(request_info
).request_uri
));
207 php_dirname(uri
, len
);
208 spprintf(&path
, 0, "%s/%s", uri
, url
->path
);
210 STR_SET(url
->path
, path
);
214 spprintf(&uri
, 0, "/%s", url
->path
);
215 STR_SET(url
->path
, uri
);
219 char *ptr
, *end
= url
->path
+ strlen(url
->path
) + 1;
221 for (ptr
= strstr(url
->path
, "/."); ptr
; ptr
= strstr(ptr
, "/.")) {
229 memmove(&ptr
[1], &ptr
[3], end
- &ptr
[3]);
235 while (ptr
!= url
->path
) {
240 memmove(&ptr
[1], pos
, end
- pos
);
253 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
254 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
255 #ifdef HTTP_HAVE_NETDB
256 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
257 (url
->port
== ntohs(se
->s_port
)))
267 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
270 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
271 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
273 if (url
->user
&& *url
->user
) {
274 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
275 if (url
->pass
&& *url
->pass
) {
276 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
277 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
279 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
282 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
285 char port_str
[8] = {0};
287 snprintf(port_str
, lenof(port_str
), "%d", (int) url
->port
);
288 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
289 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
292 if (*url
->path
!= '/') {
293 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
295 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
297 if (url
->query
&& *url
->query
) {
298 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
299 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
302 if (url
->fragment
&& *url
->fragment
) {
303 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
304 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
307 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
308 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
323 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
324 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
325 char *pre_encoded_data
, size_t pre_encoded_len
,
326 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
330 phpstr
*qstr
= phpstr_new();
332 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
333 arg_sep
= HTTP_URL_ARGSEP
;
334 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
337 if (pre_encoded_len
&& pre_encoded_data
) {
338 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
341 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
346 phpstr_data(qstr
, encoded_data
, encoded_len
);
353 /* {{{ http_urlencode_hash_recursive */
354 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
)
363 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
366 if (ht
->nApplyCount
> 0) {
370 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
375 if (!data
|| !*data
) {
381 if (len
&& key
[len
- 1] == '\0') {
384 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
387 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
391 phpstr_init(&new_prefix
);
392 if (prefix
&& prefix_len
) {
393 phpstr_append(&new_prefix
, prefix
, prefix_len
);
394 phpstr_appends(&new_prefix
, "%5B");
397 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
400 if (prefix
&& prefix_len
) {
401 phpstr_appends(&new_prefix
, "%5D");
403 phpstr_fix(&new_prefix
);
406 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
409 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
411 if (SUCCESS
!= status
) {
412 phpstr_dtor(&new_prefix
);
417 zval
*val
= zval_copy(IS_STRING
, *data
);
419 if (PHPSTR_LEN(str
)) {
420 phpstr_append(str
, arg_sep
, arg_sep_len
);
422 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
423 phpstr_appends(str
, "=");
425 if (Z_STRLEN_P(val
) && Z_STRVAL_P(val
)) {
429 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
430 phpstr_append(str
, encoded_val
, encoded_len
);
436 phpstr_dtor(&new_prefix
);
447 * vim600: noet sw=4 ts=4 fdm=marker
448 * vim<600: noet sw=4 ts=4