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 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
32 char *abs
= estrdup(url
);
33 php_url
*purl
= php_url_parse(abs
);
38 http_build_url(purl
, NULL
, NULL
, &abs
, NULL
);
45 /* {{{ void http_build_url(const php_url *, const php_url *, php_url **, char **, size_t *) */
46 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
)
48 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
51 php_url
*url
= emalloc(sizeof(php_url
));
54 url->n = (new_url&&new_url->n) ? estrdup(new_url->n) : ((old_url&&old_url->n) ? estrdup(old_url->n) : NULL)
55 url
->port
= (new_url
&&new_url
->port
) ? new_url
->port
: ((old_url
) ? old_url
->port
: 0);
68 url
->scheme
= estrndup("https", lenof("https"));
71 #if !defined(PHP_WIN32) && !defined(HAVE_NETDB_H)
75 url
->scheme
= estrndup("http", lenof("http"));
78 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
80 if ((se
= getservbyport(htons(url
->port
), "tcp")) && se
->s_name
) {
81 url
->scheme
= estrdup(se
->s_name
);
83 url
->scheme
= estrndup("http", lenof("http"));
93 if ((((zhost
= http_get_server_var("HTTP_HOST")) ||
94 (zhost
= http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost
)) {
95 url
->host
= estrndup(Z_STRVAL_P(zhost
), Z_STRLEN_P(zhost
));
97 url
->host
= estrndup("localhost", lenof("localhost"));
102 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
103 const char *q
= strchr(SG(request_info
).request_uri
, '?');
106 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
108 url
->path
= estrdup(SG(request_info
).request_uri
);
111 url
->path
= estrndup("/", 1);
113 } else if (*url
->path
!= '/') {
114 if (SG(request_info
).request_uri
&& *SG(request_info
).request_uri
) {
115 const char *q
= strchr(SG(request_info
).request_uri
, '?');
120 uri
= estrndup(SG(request_info
).request_uri
, len
= q
- SG(request_info
).request_uri
);
122 uri
= estrndup(SG(request_info
).request_uri
, len
= strlen(SG(request_info
).request_uri
));
125 php_dirname(uri
, len
);
126 spprintf(&path
, 0, "%s/%s", uri
, url
->path
);
128 STR_SET(url
->path
, path
);
132 spprintf(&uri
, 0, "/%s", url
->path
);
133 STR_SET(url
->path
, uri
);
138 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
139 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
140 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
141 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
142 (url
->port
== ntohs(se
->s_port
)))
152 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
155 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
156 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
158 if (url
->user
&& *url
->user
) {
159 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
160 if (url
->pass
&& *url
->pass
) {
161 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
162 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
164 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
167 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
170 char port_str
[6] = {0};
172 snprintf(port_str
, 5, "%d", (int) url
->port
);
173 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
174 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
177 if (*url
->path
!= '/') {
178 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
180 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
182 if (url
->query
&& *url
->query
) {
183 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
184 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
187 if (url
->fragment
&& *url
->fragment
) {
188 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
189 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
192 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
193 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
208 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
209 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
210 char *pre_encoded_data
, size_t pre_encoded_len
,
211 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
215 phpstr
*qstr
= phpstr_new();
217 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
218 arg_sep
= HTTP_URL_ARGSEP
;
219 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
222 if (pre_encoded_len
&& pre_encoded_data
) {
223 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
226 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
231 phpstr_data(qstr
, encoded_data
, encoded_len
);
238 /* {{{ http_urlencode_hash_recursive */
239 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
)
248 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
251 if (ht
->nApplyCount
> 0) {
255 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
260 if (!data
|| !*data
) {
265 if (len
&& key
[len
- 1] == '\0') {
268 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
271 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
275 phpstr_init(&new_prefix
);
276 if (prefix
&& prefix_len
) {
277 phpstr_append(&new_prefix
, prefix
, prefix_len
);
278 phpstr_appends(&new_prefix
, "[");
281 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
284 if (prefix
&& prefix_len
) {
285 phpstr_appends(&new_prefix
, "]");
287 phpstr_fix(&new_prefix
);
290 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
293 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
295 if (SUCCESS
!= status
) {
296 phpstr_dtor(&new_prefix
);
306 convert_to_string(val
);
308 if (PHPSTR_LEN(str
)) {
309 phpstr_append(str
, arg_sep
, arg_sep_len
);
311 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
312 phpstr_appends(str
, "=");
314 if (Z_STRLEN_P(val
) && Z_STRVAL_P(val
)) {
318 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
319 phpstr_append(str
, encoded_val
, encoded_len
);
325 phpstr_dtor(&new_prefix
);
336 * vim600: noet sw=4 ts=4 fdm=marker
337 * vim<600: noet sw=4 ts=4