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 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
28 char *abs
= estrdup(url
);
29 php_url
*purl
= php_url_parse(abs
);
34 http_build_url(purl
, NULL
, NULL
, &abs
, NULL
);
37 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", url
);
43 /* {{{ void http_build_url(const php_url *, const php_url *, php_url **, char **, size_t *) */
44 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
)
46 #ifdef HTTP_HAVE_NETDB
49 php_url
*url
= emalloc(sizeof(php_url
));
52 url->n = (new_url&&new_url->n) ? estrdup(new_url->n) : ((old_url&&old_url->n) ? estrdup(old_url->n) : NULL)
53 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
66 url
->scheme
= estrndup("https", lenof("https"));
69 #ifndef HTTP_HAVE_NETDB
73 url
->scheme
= estrndup("http", lenof("http"));
76 #ifdef HTTP_HAVE_NETDB
78 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
79 url
->scheme
= estrdup(se
->s_name
);
81 url
->scheme
= estrndup("http", lenof("http"));
91 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
92 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
93 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
95 url
->host
= estrndup("localhost", lenof("localhost"));
100 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
101 const char *q
= strchr(SG(request_info
).request_uri
, '?');
104 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
106 url
->path
= estrdup(SG(request_info
).request_uri
);
109 url
->path
= estrndup("/", 1);
111 } else if (*url
->path
!= '/') {
112 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
113 const char *q
= strchr(SG(request_info
).request_uri
, '?');
118 uri
= estrndup(SG(request_info
).request_uri
, len
= q
- SG(request_info
).request_uri
);
120 uri
= estrndup(SG(request_info
).request_uri
, len
= strlen(SG(request_info
).request_uri
));
123 php_dirname(uri
, len
);
124 spprintf(&path
, 0, "%s/%s", uri
, url
->path
);
126 STR_SET(url
->path
, path
);
130 spprintf(&uri
, 0, "/%s", url
->path
);
131 STR_SET(url
->path
, uri
);
136 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
137 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
138 #ifdef HTTP_HAVE_NETDB
139 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
140 (url
->port
== ntohs(se
->s_port
)))
150 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
153 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
154 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
156 if (url
->user
&& *url
->user
) {
157 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
158 if (url
->pass
&& *url
->pass
) {
159 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
160 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
162 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
165 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
168 char port_str
[6] = {0};
170 snprintf(port_str
, 5, "%d", (int) url
->port
);
171 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
172 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
175 if (*url
->path
!= '/') {
176 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
178 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
180 if (url
->query
&& *url
->query
) {
181 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
182 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
185 if (url
->fragment
&& *url
->fragment
) {
186 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
187 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
190 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
191 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
206 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
207 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
208 char *pre_encoded_data
, size_t pre_encoded_len
,
209 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
213 phpstr
*qstr
= phpstr_new();
215 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
216 arg_sep
= HTTP_URL_ARGSEP
;
217 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
220 if (pre_encoded_len
&& pre_encoded_data
) {
221 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
224 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
229 phpstr_data(qstr
, encoded_data
, encoded_len
);
236 /* {{{ http_urlencode_hash_recursive */
237 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
)
246 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
249 if (ht
->nApplyCount
> 0) {
253 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
258 if (!data
|| !*data
) {
263 if (len
&& key
[len
- 1] == '\0') {
266 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
269 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
273 phpstr_init(&new_prefix
);
274 if (prefix
&& prefix_len
) {
275 phpstr_append(&new_prefix
, prefix
, prefix_len
);
276 phpstr_appends(&new_prefix
, "[");
279 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
282 if (prefix
&& prefix_len
) {
283 phpstr_appends(&new_prefix
, "]");
285 phpstr_fix(&new_prefix
);
288 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
291 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
293 if (SUCCESS
!= status
) {
294 phpstr_dtor(&new_prefix
);
298 zval
*val
= zval_copy(IS_STRING
, *data
);
300 if (PHPSTR_LEN(str
)) {
301 phpstr_append(str
, arg_sep
, arg_sep_len
);
303 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
304 phpstr_appends(str
, "=");
306 if (Z_STRLEN_P(val
) && Z_STRVAL_P(val
)) {
310 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
311 phpstr_append(str
, encoded_val
, encoded_len
);
317 phpstr_dtor(&new_prefix
);
328 * vim600: noet sw=4 ts=4 fdm=marker
329 * vim<600: noet sw=4 ts=4