| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
- | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
+ | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
+--------------------------------------------------------------------+
*/
/* $Id$ */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
+#define HTTP_WANT_SAPI
#define HTTP_WANT_NETDB
#include "php_http.h"
-#include "SAPI.h"
#include "zend_ini.h"
#include "php_output.h"
+#include "ext/standard/php_string.h"
#include "php_http_api.h"
#include "php_http_url_api.h"
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC)
{
char *abs = estrdup(url);
if (purl) {
http_build_url(purl, NULL, NULL, &abs, NULL);
php_url_free(purl);
+ } else {
+ http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", url);
}
return abs;
/* {{{ void http_build_url(const php_url *, const php_url *, php_url **, char **, size_t *) */
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)
{
-#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
+#ifdef HTTP_HAVE_NETDB
struct servent *se;
#endif
php_url *url = emalloc(sizeof(php_url));
__URLCPY(pass);
__URLCPY(host);
__URLCPY(path);
- __URLCPY(path);
__URLCPY(query);
__URLCPY(fragment);
url->scheme = estrndup("https", lenof("https"));
break;
-#if !defined(PHP_WIN32) && !defined(HAVE_NETDB_H)
+#ifndef HTTP_HAVE_NETDB
default:
#endif
case 80:
url->scheme = estrndup("http", lenof("http"));
break;
-#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
+#ifdef HTTP_HAVE_NETDB
default:
if ((se = getservbyport(htons(url->port), "tcp")) && se->s_name) {
url->scheme = estrdup(se->s_name);
}
}
- /* FIXXME: dirname(REQUEST_URI) if path is relative */
if (!url->path) {
- if (SG(request_info).request_uri) {
+ if (SG(request_info).request_uri && *SG(request_info).request_uri) {
const char *q = strchr(SG(request_info).request_uri, '?');
if (q) {
url->path = estrdup(SG(request_info).request_uri);
}
} else {
- url->path = ecalloc(1, 1);
+ url->path = estrndup("/", 1);
+ }
+ } else if (*url->path != '/') {
+ if (SG(request_info).request_uri && *SG(request_info).request_uri) {
+ const char *q = strchr(SG(request_info).request_uri, '?');
+ char *uri, *path;
+ size_t len;
+
+ if (q) {
+ uri = estrndup(SG(request_info).request_uri, len = q - SG(request_info).request_uri);
+ } else {
+ uri = estrndup(SG(request_info).request_uri, len = strlen(SG(request_info).request_uri));
+ }
+
+ php_dirname(uri, len);
+ spprintf(&path, 0, "%s/%s", uri, url->path);
+ efree(uri);
+ STR_SET(url->path, path);
+ } else {
+ char *uri;
+
+ spprintf(&uri, 0, "/%s", url->path);
+ STR_SET(url->path, uri);
}
}
if (url->port) {
if ( ((url->port == 80) && !strcmp(url->scheme, "http"))
|| ((url->port ==443) && !strcmp(url->scheme, "https"))
-#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
+#ifdef HTTP_HAVE_NETDB
|| ((se = getservbyname(url->scheme, "tcp")) && se->s_port &&
(url->port == ntohs(se->s_port)))
#endif
return FAILURE;
}
} else {
- char *encoded_val;
- int encoded_len;
- zval *cpy, *val = convert_to_type_ex(IS_STRING, *data, &cpy);
+ zval *val = zval_copy(IS_STRING, *data);
if (PHPSTR_LEN(str)) {
phpstr_append(str, arg_sep, arg_sep_len);
phpstr_append(str, PHPSTR_VAL(&new_prefix), PHPSTR_LEN(&new_prefix));
phpstr_appends(str, "=");
- encoded_val = php_url_encode(Z_STRVAL_P(val), Z_STRLEN_P(val), &encoded_len);
- phpstr_append(str, encoded_val, encoded_len);
- efree(encoded_val);
-
- if (cpy) {
- zval_ptr_dtor(&cpy);
+ if (Z_STRLEN_P(val) && Z_STRVAL_P(val)) {
+ char *encoded_val;
+ int encoded_len;
+
+ encoded_val = php_url_encode(Z_STRVAL_P(val), Z_STRLEN_P(val), &encoded_len);
+ phpstr_append(str, encoded_val, encoded_len);
+ efree(encoded_val);
}
+
+ zval_free(&val);
}
-
phpstr_dtor(&new_prefix);
}
return SUCCESS;