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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_NETDB
24 #include "php_output.h"
25 #include "ext/standard/php_string.h"
27 #include "php_http_api.h"
28 #include "php_http_url_api.h"
30 ZEND_EXTERN_MODULE_GLOBALS(http
);
32 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
34 char *abs
= estrdup(url
);
35 php_url
*purl
= php_url_parse(abs
);
40 http_build_url(purl
, NULL
, NULL
, &abs
, NULL
);
47 /* {{{ void http_build_url(const php_url *, const php_url *, php_url **, char **, size_t *) */
48 PHP_HTTP_API
void _http_build_url(const php_url
*old_url
, const php_url
*new_url
, php_url
**url_ptr
, char **url_str
, size_t *url_len TSRMLS_DC
)
50 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
53 php_url
*url
= emalloc(sizeof(php_url
));
56 url->n = (new_url&&new_url->n) ? estrdup(new_url->n) : ((old_url&&old_url->n) ? estrdup(old_url->n) : NULL)
57 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
70 url
->scheme
= estrndup("https", lenof("https"));
73 #if !defined(PHP_WIN32) && !defined(HAVE_NETDB_H)
77 url
->scheme
= estrndup("http", lenof("http"));
80 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
82 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
83 url
->scheme
= estrdup(se
->s_name
);
85 url
->scheme
= estrndup("http", lenof("http"));
95 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
96 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
97 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
99 url
->host
= estrndup("localhost", lenof("localhost"));
104 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
105 const char *q
= strchr(SG(request_info
).request_uri
, '?');
108 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
110 url
->path
= estrdup(SG(request_info
).request_uri
);
113 url
->path
= estrndup("/", 1);
115 } else if (*url
->path
!= '/') {
116 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
117 const char *q
= strchr(SG(request_info
).request_uri
, '?');
122 uri
= estrndup(SG(request_info
).request_uri
, len
= q
- SG(request_info
).request_uri
);
124 uri
= estrndup(SG(request_info
).request_uri
, len
= strlen(SG(request_info
).request_uri
));
127 php_dirname(uri
, len
);
128 spprintf(&path
, 0, "%s/%s", uri
, url
->path
);
130 STR_SET(url
->path
, path
);
134 spprintf(&uri
, 0, "/%s", url
->path
);
135 STR_SET(url
->path
, uri
);
140 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
141 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
142 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
143 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
144 (url
->port
== ntohs(se
->s_port
)))
154 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
157 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
158 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
160 if (url
->user
&& *url
->user
) {
161 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
162 if (url
->pass
&& *url
->pass
) {
163 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
164 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
166 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
169 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
172 char port_str
[6] = {0};
174 snprintf(port_str
, 5, "%d", (int) url
->port
);
175 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
176 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
179 if (*url
->path
!= '/') {
180 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
182 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
184 if (url
->query
&& *url
->query
) {
185 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
186 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
189 if (url
->fragment
&& *url
->fragment
) {
190 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
191 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
194 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
195 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
210 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
211 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
212 char *pre_encoded_data
, size_t pre_encoded_len
,
213 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
217 phpstr
*qstr
= phpstr_new();
219 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
220 arg_sep
= HTTP_URL_ARGSEP
;
221 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
224 if (pre_encoded_len
&& pre_encoded_data
) {
225 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
228 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
233 phpstr_data(qstr
, encoded_data
, encoded_len
);
240 /* {{{ http_urlencode_hash_recursive */
241 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
)
250 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
253 if (ht
->nApplyCount
> 0) {
257 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
262 if (!data
|| !*data
) {
267 if (len
&& key
[len
- 1] == '\0') {
270 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
273 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
277 phpstr_init(&new_prefix
);
278 if (prefix
&& prefix_len
) {
279 phpstr_append(&new_prefix
, prefix
, prefix_len
);
280 phpstr_appends(&new_prefix
, "[");
283 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
286 if (prefix
&& prefix_len
) {
287 phpstr_appends(&new_prefix
, "]");
289 phpstr_fix(&new_prefix
);
292 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
295 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
297 if (SUCCESS
!= status
) {
298 phpstr_dtor(&new_prefix
);
302 zval
*cpy
, *val
= convert_to_type_ex(IS_STRING
, *data
, &cpy
);
304 if (PHPSTR_LEN(str
)) {
305 phpstr_append(str
, arg_sep
, arg_sep_len
);
307 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
308 phpstr_appends(str
, "=");
310 if (Z_STRLEN_P(val
)) {
314 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
315 phpstr_append(str
, encoded_val
, encoded_len
);
324 phpstr_dtor(&new_prefix
);
335 * vim600: noet sw=4 ts=4 fdm=marker
336 * vim<600: noet sw=4 ts=4