zend_function_entry http_functions[] = {
PHP_FE(http_test, NULL)
PHP_FE(http_date, NULL)
- PHP_FE(http_build_url, http_arg_pass_ref_3)
+ PHP_FE(http_build_url, http_arg_pass_ref_4)
PHP_FE(http_negotiate_language, http_arg_pass_ref_2)
PHP_FE(http_negotiate_charset, http_arg_pass_ref_2)
PHP_FE(http_negotiate_content_type, http_arg_pass_ref_2)
if ( (SUCCESS != PHP_MINIT_CALL(http_support)) ||
(SUCCESS != PHP_MINIT_CALL(http_send)) ||
+ (SUCCESS != PHP_MINIT_CALL(http_url)) ||
#ifdef HTTP_HAVE_CURL
(SUCCESS != PHP_MINIT_CALL(http_request)) ||
#endif /* HTTP_HAVE_CURL */
}
/* }}} */
-/* {{{ proto string http_build_url(mixed url[, mixed parts[, array &new_url]])
+/* {{{ proto string http_build_url(mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]])
*
* Returns the new URL as string on success or FALSE on failure.
*/
{
char *url_str = NULL;
size_t url_len = 0;
+ long flags = HTTP_URL_REPLACE;
zval *z_old_url = NULL, *z_new_url = NULL, *z_composed_url = NULL;
php_url *old_url = NULL, *new_url = NULL, *composed_url = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|z/z", &z_old_url, &z_new_url, &z_composed_url) != SUCCESS) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|z/lz", &z_old_url, &z_new_url, &flags, &z_composed_url) != SUCCESS) {
RETURN_FALSE;
}
}
if (z_composed_url) {
- http_build_url(old_url, new_url, &composed_url, &url_str, &url_len);
+ http_build_url(flags, old_url, new_url, &composed_url, &url_str, &url_len);
zval_dtor(z_composed_url);
array_init(z_composed_url);
}
php_url_free(composed_url);
} else {
- http_build_url(old_url, new_url, NULL, &url_str, &url_len);
+ http_build_url(flags, old_url, new_url, NULL, &url_str, &url_len);
}
if (new_url) {
parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
}
- http_build_url(url, &parts, NULL, &uri, NULL);
+ http_build_url(HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
php_url_free(url);
efree(parts.host);
} else {
memset(&hurl, 0, sizeof(php_url));
hurl.host = host ? Z_STRVAL_P(host) : NULL;
- http_build_url(purl, &hurl, NULL, &url, NULL);
+ http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
php_url_free(purl);
add_assoc_string(array, "url", url, 0);
#include "php_http_api.h"
#include "php_http_url_api.h"
+PHP_MINIT_FUNCTION(http_url)
+{
+ HTTP_LONG_CONSTANT("HTTP_URL_REPLACE", HTTP_URL_REPLACE);
+ HTTP_LONG_CONSTANT("HTTP_URL_JOIN_PATH", HTTP_URL_JOIN_PATH);
+ HTTP_LONG_CONSTANT("HTTP_URL_JOIN_QUERY", HTTP_URL_JOIN_QUERY);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_USER", HTTP_URL_STRIP_USER);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PASS", HTTP_URL_STRIP_PASS);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_AUTH", HTTP_URL_STRIP_AUTH);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PORT", HTTP_URL_STRIP_PORT);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PATH", HTTP_URL_STRIP_PATH);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_QUERY", HTTP_URL_STRIP_QUERY);
+ HTTP_LONG_CONSTANT("HTTP_URL_STRIP_FRAGMENT", HTTP_URL_STRIP_FRAGMENT);
+ return SUCCESS;
+}
+
PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC)
{
char *abs = estrdup(url);
STR_SET(abs, NULL);
if (purl) {
- http_build_url(purl, NULL, NULL, &abs, NULL);
+ http_build_url(0, 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)
+/* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */
+PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC)
{
#ifdef HTTP_HAVE_NETDB
struct servent *se;
#endif
- php_url *url = emalloc(sizeof(php_url));
+ php_url *url = ecalloc(1, sizeof(php_url));
+#define __URLSET(u,n) \
+ ((u)&&(u)->n)
#define __URLCPY(n) \
- url->n = (new_url&&new_url->n) ? estrdup(new_url->n) : ((old_url&&old_url->n) ? estrdup(old_url->n) : NULL)
- url->port = (new_url&&new_url->port) ? new_url->port : ((old_url) ? old_url->port : 0);
+ url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL)
+
+ if (!(flags & HTTP_URL_STRIP_PORT)) {
+ url->port = (new_url&&new_url->port) ? new_url->port : ((old_url) ? old_url->port : 0);
+ }
+ if ((!(flags & HTTP_URL_STRIP_AUTH)) && (!(flags & HTTP_URL_STRIP_USER))) {
+ __URLCPY(user);
+ }
+ if ((!(flags & HTTP_URL_STRIP_AUTH)) && (!(flags & HTTP_URL_STRIP_PASS))) {
+ __URLCPY(pass);
+ }
+
__URLCPY(scheme);
- __URLCPY(user);
- __URLCPY(pass);
__URLCPY(host);
- __URLCPY(path);
- __URLCPY(query);
- __URLCPY(fragment);
+
+ if (!(flags & HTTP_URL_STRIP_PATH)) {
+ if ((flags & HTTP_URL_JOIN_PATH) && __URLSET(old_url, path) && __URLSET(new_url, path) && *new_url->path != '/') {
+ size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path);
+
+ url->path = ecalloc(1, old_path_len + new_path_len + 1 + 1);
+
+ strcat(url->path, old_url->path);
+ if (url->path[old_path_len - 1] != '/') {
+ php_dirname(url->path, old_path_len);
+ strcat(url->path, "/");
+ }
+ strcat(url->path, new_url->path);
+ } else {
+ __URLCPY(path);
+ }
+ }
+ if (!(flags & HTTP_URL_STRIP_QUERY)) {
+ if ((flags & HTTP_URL_JOIN_QUERY) && __URLSET(new_url, query) && __URLSET(old_url, query)) {
+ url->query = ecalloc(1, strlen(new_url->query) + strlen(old_url->query) + 1 + 1);
+ strcat(url->query, old_url->query);
+ strcat(url->query, "&");
+ strcat(url->query, new_url->query);
+ } else {
+ __URLCPY(query);
+ }
+ }
+ if (!(flags & HTTP_URL_STRIP_FRAGMENT)) {
+ __URLCPY(fragment);
+ }
if (!url->scheme) {
switch (url->port)
STR_SET(url->path, uri);
}
}
+ if (url->path) {
+ char *ptr, *end = url->path + strlen(url->path) + 1;
+
+ for (ptr = strstr(url->path, "/."); ptr; ptr = strstr(ptr, "/.")) {
+ switch (ptr[2])
+ {
+ case '\0':
+ ptr[1] = '\0';
+ break;
+
+ case '/':
+ memmove(&ptr[1], &ptr[3], end - &ptr[3]);
+ break;
+
+ case '.':
+ if (ptr[3] == '/') {
+ char *pos = &ptr[4];
+ while (ptr != url->path) {
+ if (*--ptr == '/') {
+ break;
+ }
+ }
+ memmove(&ptr[1], pos, end - pos);
+ }
+ break;
+ }
+ }
+ }
if (url->port) {
if ( ((url->port == 80) && !strcmp(url->scheme, "http"))
#ifdef ZEND_ENGINE_2
+#include "ext/standard/php_http.h"
+
#include "php_http_util_object.h"
#define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpUtil, method, 0, req_args)
HTTP_ARG_VAL(timestamp, 0)
HTTP_END_ARGS;
+HTTP_BEGIN_ARGS(buildQuery, 1)
+ HTTP_ARG_VAL(query, 0)
+HTTP_END_ARGS;
+
HTTP_BEGIN_ARGS(buildUrl, 1)
HTTP_ARG_VAL(url, 0)
HTTP_ARG_VAL(parts, 0)
+ HTTP_ARG_VAL(flags, 0)
HTTP_ARG_VAL(composed, 1)
HTTP_END_ARGS;
zend_function_entry http_util_object_fe[] = {
HTTP_UTIL_ALIAS(date, http_date)
HTTP_UTIL_ALIAS(buildUrl, http_build_url)
+ HTTP_UTIL_ALIAS(buildQuery, http_build_query)
HTTP_UTIL_ALIAS(negotiateLanguage, http_negotiate_language)
HTTP_UTIL_ALIAS(negotiateCharset, http_negotiate_charset)
HTTP_UTIL_ALIAS(negotiateContentType, http_negotiate_content_type)
ZEND_END_ARG_INFO(); \
\
static \
- ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \
+ ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \
+ ZEND_ARG_PASS_INFO(0) \
ZEND_ARG_PASS_INFO(0) \
ZEND_ARG_PASS_INFO(0) \
ZEND_ARG_PASS_INFO(1) \
ZEND_END_ARG_INFO();
# else
# define HTTP_DECLARE_ARG_PASS_INFO() \
- static unsigned char http_arg_pass_ref_2[] = {2, BYREF_NONE, BYREF_FORCE};\
- static unsigned char http_arg_pass_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
+ static unsigned char http_arg_pass_ref_2[] = {2, BYREF_NONE, BYREF_FORCE}; \
+ static unsigned char http_arg_pass_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
# endif /* ZEND_ENGINE_2 */
#endif /* HTTP_HAVE_CURL */
#include "ext/standard/url.h"
+extern PHP_MINIT_FUNCTION(http_url);
+
#define http_absolute_url(u) _http_absolute_url((u) TSRMLS_CC)
PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC);
-#define http_build_url(o, n, p, s, l) _http_build_url((o), (n), (p), (s), (l) TSRMLS_CC)
-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);
+#define HTTP_URL_REPLACE 0x000
+#define HTTP_URL_JOIN_PATH 0x001
+#define HTTP_URL_JOIN_QUERY 0x002
+#define HTTP_URL_STRIP_USER 0x004
+#define HTTP_URL_STRIP_PASS 0x008
+#define HTTP_URL_STRIP_AUTH 0x010
+#define HTTP_URL_STRIP_PORT 0x020
+#define HTTP_URL_STRIP_PATH 0x040
+#define HTTP_URL_STRIP_QUERY 0x080
+#define HTTP_URL_STRIP_FRAGMENT 0x100
+
+#define http_build_url(f, o, n, p, s, l) _http_build_url((f), (o), (n), (p), (s), (l) TSRMLS_CC)
+PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC);
#define http_urlencode_hash(h, q) _http_urlencode_hash_ex((h), 1, NULL, 0, (q), NULL TSRMLS_CC)
#define http_urlencode_hash_ex(h, o, p, pl, q, ql) _http_urlencode_hash_ex((h), (o), (p), (pl), (q), (ql) TSRMLS_CC)
PHP_METHOD(HttpUtil, date);
PHP_METHOD(HttpUtil, buildUrl);
+PHP_METHOD(HttpUtil, buildQuery);
PHP_METHOD(HttpUtil, negotiateLanguage);
PHP_METHOD(HttpUtil, negotiateCharset);
PHP_METHOD(HttpUtil, negotiateContentType);