X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_url_api.c;h=7bacffc3e73e26120c8ddbc58e7f52ec1df63eb4;hp=99c8af97542d61c164574b404839d39480da9bfc;hb=6e9ea0a647db55213dd230145c35a419d7abde4d;hpb=bf87f6e654235acb023ca052a5e71faeb2635a3f diff --git a/http_url_api.c b/http_url_api.c index 99c8af9..7bacffc 100644 --- a/http_url_api.c +++ b/http_url_api.c @@ -22,12 +22,11 @@ #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); @@ -38,6 +37,8 @@ PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC) 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; @@ -59,7 +60,6 @@ PHP_HTTP_API void _http_build_url(const php_url *old_url, const php_url *new_url __URLCPY(pass); __URLCPY(host); __URLCPY(path); - __URLCPY(path); __URLCPY(query); __URLCPY(fragment); @@ -100,9 +100,8 @@ PHP_HTTP_API void _http_build_url(const php_url *old_url, const php_url *new_url } } - /* 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) { @@ -111,7 +110,29 @@ PHP_HTTP_API void _http_build_url(const php_url *old_url, const php_url *new_url 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); } } @@ -278,9 +299,7 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c 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); @@ -288,15 +307,17 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c 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;