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"
26 #include "php_http_api.h"
27 #include "php_http_url_api.h"
29 ZEND_EXTERN_MODULE_GLOBALS(http
);
31 PHP_HTTP_API
char *_http_absolute_url(const char *url TSRMLS_DC
)
33 char *abs
= estrdup(url
);
34 php_url
*purl
= php_url_parse(abs
);
39 http_build_url(purl
, NULL
, NULL
, &abs
, NULL
);
46 /* {{{ void http_build_url(const php_url *, const php_url *, php_url **, char **, size_t *) */
47 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
)
49 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
52 php_url
*url
= emalloc(sizeof(php_url
));
55 url->n = (new_url&&new_url->n) ? estrdup(new_url->n) : ((old_url&&old_url->n) ? estrdup(old_url->n) : NULL)
56 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"));
103 /* FIXXME: dirname(REQUEST_URI) if path is relative */
105 if (SG(request_info
).request_uri
) {
106 const char *q
= strchr(SG(request_info
).request_uri
, '?');
109 url
->path
= estrndup(SG(request_info
).request_uri
, q
- SG(request_info
).request_uri
);
111 url
->path
= estrdup(SG(request_info
).request_uri
);
114 url
->path
= ecalloc(1, 1);
119 if ( ((url
->port
== 80) && !strcmp(url
->scheme
, "http"))
120 || ((url
->port
==443) && !strcmp(url
->scheme
, "https"))
121 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
122 || ((se
= getservbyname(url
->scheme
, "tcp")) && se
->s_port
&&
123 (url
->port
== ntohs(se
->s_port
)))
133 *url_str
= emalloc(HTTP_URL_MAXLEN
+ 1);
136 strlcat(*url_str
, url
->scheme
, HTTP_URL_MAXLEN
);
137 strlcat(*url_str
, "://", HTTP_URL_MAXLEN
);
139 if (url
->user
&& *url
->user
) {
140 strlcat(*url_str
, url
->user
, HTTP_URL_MAXLEN
);
141 if (url
->pass
&& *url
->pass
) {
142 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
143 strlcat(*url_str
, url
->pass
, HTTP_URL_MAXLEN
);
145 strlcat(*url_str
, "@", HTTP_URL_MAXLEN
);
148 strlcat(*url_str
, url
->host
, HTTP_URL_MAXLEN
);
151 char port_str
[6] = {0};
153 snprintf(port_str
, 5, "%d", (int) url
->port
);
154 strlcat(*url_str
, ":", HTTP_URL_MAXLEN
);
155 strlcat(*url_str
, port_str
, HTTP_URL_MAXLEN
);
158 if (*url
->path
!= '/') {
159 strlcat(*url_str
, "/", HTTP_URL_MAXLEN
);
161 strlcat(*url_str
, url
->path
, HTTP_URL_MAXLEN
);
163 if (url
->query
&& *url
->query
) {
164 strlcat(*url_str
, "?", HTTP_URL_MAXLEN
);
165 strlcat(*url_str
, url
->query
, HTTP_URL_MAXLEN
);
168 if (url
->fragment
&& *url
->fragment
) {
169 strlcat(*url_str
, "#", HTTP_URL_MAXLEN
);
170 strlcat(*url_str
, url
->fragment
, HTTP_URL_MAXLEN
);
173 if (HTTP_URL_MAXLEN
== (len
= strlen(*url_str
))) {
174 http_error(HE_NOTICE
, HTTP_E_URL
, "Length of URL exceeds HTTP_URL_MAXLEN");
189 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
190 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, zend_bool override_argsep
,
191 char *pre_encoded_data
, size_t pre_encoded_len
,
192 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
196 phpstr
*qstr
= phpstr_new();
198 if (override_argsep
|| !(arg_sep_len
= strlen(arg_sep
= INI_STR("arg_separator.output")))) {
199 arg_sep
= HTTP_URL_ARGSEP
;
200 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
203 if (pre_encoded_len
&& pre_encoded_data
) {
204 phpstr_append(qstr
, pre_encoded_data
, pre_encoded_len
);
207 if (SUCCESS
!= http_urlencode_hash_recursive(hash
, qstr
, arg_sep
, arg_sep_len
, NULL
, 0)) {
212 phpstr_data(qstr
, encoded_data
, encoded_len
);
219 /* {{{ http_urlencode_hash_recursive */
220 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
)
229 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid parameters");
232 if (ht
->nApplyCount
> 0) {
236 FOREACH_HASH_KEYLENVAL(pos
, ht
, key
, len
, idx
, data
) {
241 if (!data
|| !*data
) {
246 if (len
&& key
[len
- 1] == '\0') {
249 encoded_key
= php_url_encode(key
, len
, &encoded_len
);
252 encoded_len
= spprintf(&encoded_key
, 0, "%ld", idx
);
256 phpstr_init(&new_prefix
);
257 if (prefix
&& prefix_len
) {
258 phpstr_append(&new_prefix
, prefix
, prefix_len
);
259 phpstr_appends(&new_prefix
, "[");
262 phpstr_append(&new_prefix
, encoded_key
, encoded_len
);
265 if (prefix
&& prefix_len
) {
266 phpstr_appends(&new_prefix
, "]");
268 phpstr_fix(&new_prefix
);
271 if (Z_TYPE_PP(data
) == IS_ARRAY
) {
274 status
= http_urlencode_hash_recursive(Z_ARRVAL_PP(data
), str
, arg_sep
, arg_sep_len
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
276 if (SUCCESS
!= status
) {
277 phpstr_dtor(&new_prefix
);
283 zval
*cpy
, *val
= convert_to_type_ex(IS_STRING
, *data
, &cpy
);
285 if (PHPSTR_LEN(str
)) {
286 phpstr_append(str
, arg_sep
, arg_sep_len
);
288 phpstr_append(str
, PHPSTR_VAL(&new_prefix
), PHPSTR_LEN(&new_prefix
));
289 phpstr_appends(str
, "=");
291 encoded_val
= php_url_encode(Z_STRVAL_P(val
), Z_STRLEN_P(val
), &encoded_len
);
292 phpstr_append(str
, encoded_val
, encoded_len
);
300 phpstr_dtor(&new_prefix
);
311 * vim600: noet sw=4 ts=4 fdm=marker
312 * vim<600: noet sw=4 ts=4