Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECTF--
string(21) "https://example.com/#"
string(20) "https://example.com/"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
Expand All @@ -33,6 +33,6 @@ object(Uri\WhatWg\Url)#%d (%d) {
["query"]=>
NULL
["fragment"]=>
string(0) ""
NULL
}
bool(true)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - replaces the base fragment with an empty fragment
Test Uri\WhatWg\UrlBuilder::build() - success - clears the base fragment with an empty string
--FILE--
<?php

Expand All @@ -13,5 +13,5 @@ var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECT--
string(26) "https://example.com/a?old#"
string(25) "https://example.com/a?old"
bool(true)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - resolves an empty fragment against an opaque base
Test Uri\WhatWg\UrlBuilder::build() - success - clears the fragment of an opaque base with an empty string
--FILE--
<?php

Expand All @@ -15,7 +15,7 @@ var_dump($base->toAsciiString());

?>
--EXPECT--
string(15) "foo:opaque?old#"
string(14) "foo:opaque?old"
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
bool(true)
string(18) "foo:opaque?old#old"
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setFragment() - success - hashmark only
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("https");
$builder->setHost("example.com");
$builder->setFragment("#");
$url = $builder->build();

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString()), Uri\UriComparisonMode::IncludeFragment));

?>
--EXPECTF--
string(21) "https://example.com/#"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
string(11) "example.com"
["port"]=>
NULL
["path"]=>
string(1) "/"
["query"]=>
NULL
["fragment"]=>
string(0) ""
}
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setFragment() - success - hashmark only with base URL
--FILE--
<?php

$base = new Uri\WhatWg\Url('https://example.com/base/path?oldQuery#oldFragment');

$url = new Uri\WhatWg\UrlBuilder()
->setFragment('#')
->build($base);

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
var_dump($url->equals(new Uri\WhatWg\Url('#', $base), Uri\UriComparisonMode::IncludeFragment));

?>
--EXPECTF--
string(39) "https://example.com/base/path?oldQuery#"
Comment thread
kocsismate marked this conversation as resolved.
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
string(11) "example.com"
["port"]=>
NULL
["path"]=>
string(10) "/base/path"
["query"]=>
string(8) "oldQuery"
["fragment"]=>
string(0) ""
}
bool(true)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setPath() - success - trailing spaces with empty query and fragment inputs
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("foo");
$builder->setPath("abc ");
$builder->setQuery("");
$builder->setFragment("");
$url = $builder->build(softErrors: $softErrors);

var_dump($url->toAsciiString());
var_dump($url);
var_dump($softErrors);

?>
--EXPECTF--
string(8) "foo:abc "
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(3) "foo"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
NULL
["port"]=>
NULL
["path"]=>
string(4) "abc "
["query"]=>
NULL
["fragment"]=>
NULL
}
array(1) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(1) " "
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECTF--
string(21) "https://example.com/?"
string(20) "https://example.com/"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
Expand All @@ -31,7 +31,7 @@ object(Uri\WhatWg\Url)#%d (%d) {
["path"]=>
string(1) "/"
["query"]=>
string(0) ""
NULL
["fragment"]=>
NULL
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - replaces the base query with an empty query
Test Uri\WhatWg\UrlBuilder::build() - success - clears the base query with an empty string
--FILE--
<?php

Expand All @@ -13,5 +13,5 @@ var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECT--
string(22) "https://example.com/a?"
string(21) "https://example.com/a"
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setQuery() - success - question mark only
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("https");
$builder->setHost("example.com");
$builder->setQuery("?");
$url = $builder->build();

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECTF--
string(21) "https://example.com/?"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
string(11) "example.com"
["port"]=>
NULL
["path"]=>
string(1) "/"
["query"]=>
string(0) ""
["fragment"]=>
NULL
}
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setQuery() - success - question mark only with base URL
--FILE--
<?php

$base = new Uri\WhatWg\Url('https://example.com/base/path?oldQuery#oldFragment');

$url = new Uri\WhatWg\UrlBuilder()
->setQuery('?')
->build($base);

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
var_dump($url->equals(new Uri\WhatWg\Url('?', $base), Uri\UriComparisonMode::IncludeFragment));

?>
--EXPECTF--
string(30) "https://example.com/base/path?"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
string(11) "example.com"
["port"]=>
NULL
["path"]=>
string(10) "/base/path"
["query"]=>
string(0) ""
["fragment"]=>
NULL
}
bool(true)
bool(true)
56 changes: 16 additions & 40 deletions ext/uri/uri_parser_whatwg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,27 +1136,17 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser
}

if (Z_TYPE_P(query) == IS_STRING) {
lxb_url_query_set_null(lexbor_url);
lxb_url_parser_clean(&lexbor_parser);
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
(lxb_char_t *) Z_STRVAL_P(query), Z_STRLEN_P(query),
LXB_URL_STATE_QUERY_STATE, LXB_ENCODING_AUTO
);
php_uri_parser_whatwg_build_errors_and_throw(status, "query", Z_ARRVAL(errors));
if (status != LXB_STATUS_OK) {
zend_result result = php_uri_parser_whatwg_query_write(lexbor_url, query, NULL);
php_uri_parser_whatwg_build_errors(Z_ARRVAL(errors));
if (result == FAILURE) {
goto failure;
}
}

if (Z_TYPE_P(fragment) == IS_STRING) {
lxb_url_fragment_set_null(lexbor_url);
lxb_url_parser_clean(&lexbor_parser);
status = lxb_url_parse_basic(&lexbor_parser, lexbor_url, lexbor_base_url,
(lxb_char_t *) Z_STRVAL_P(fragment), Z_STRLEN_P(fragment),
LXB_URL_STATE_FRAGMENT_STATE, LXB_ENCODING_AUTO
);
php_uri_parser_whatwg_build_errors_and_throw(status, "fragment", Z_ARRVAL(errors));
if (status != LXB_STATUS_OK) {
zend_result result = php_uri_parser_whatwg_fragment_write(lexbor_url, fragment, NULL);
php_uri_parser_whatwg_build_errors(Z_ARRVAL(errors));
if (result == FAILURE) {
goto failure;
}
}
Expand Down Expand Up @@ -1202,9 +1192,9 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_whatwg_build_path(

/* The opaque path parser needs the following delimiter to encode the
* immediately preceding space, while still reporting all space errors. */
if (Z_TYPE_P(query) != IS_NULL) {
if (Z_TYPE_P(query) == IS_STRING && Z_STRLEN_P(query) > 0) {
smart_str_appendc(&opaque_path, '?');
} else if (Z_TYPE_P(fragment) != IS_NULL) {
} else if (Z_TYPE_P(fragment) == IS_STRING && Z_STRLEN_P(fragment) > 0) {
smart_str_appendc(&opaque_path, '#');
}

Expand Down Expand Up @@ -1320,30 +1310,16 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(2, 3, 4, 5, 6, 7, 8, 9) lxb_url_t *php_uri_parser_wh
goto failure;
}

if (Z_TYPE_P(query) == IS_STRING && Z_STRLEN_P(query) == 0) {
/* The URL API setter treats an empty string as removal. The builder
* distinguishes an empty component from an absent one. */
lexbor_str_destroy(&lexbor_url->query, lexbor_url->mraw, false);
lexbor_str_init(&lexbor_url->query, lexbor_url->mraw, 1);
} else {
result = php_uri_parser_whatwg_query_write(lexbor_url, query, NULL);
php_uri_parser_whatwg_build_errors(Z_ARRVAL(errors));
if (result == FAILURE) {
goto failure;
}
result = php_uri_parser_whatwg_query_write(lexbor_url, query, NULL);
php_uri_parser_whatwg_build_errors(Z_ARRVAL(errors));
if (result == FAILURE) {
goto failure;
}

if (Z_TYPE_P(fragment) == IS_STRING && Z_STRLEN_P(fragment) == 0) {
/* The URL API setter treats an empty string as removal. The builder
* distinguishes an empty component from an absent one. */
lexbor_str_destroy(&lexbor_url->fragment, lexbor_url->mraw, false);
lexbor_str_init(&lexbor_url->fragment, lexbor_url->mraw, 1);
} else {
result = php_uri_parser_whatwg_fragment_write(lexbor_url, fragment, NULL);
php_uri_parser_whatwg_build_errors(Z_ARRVAL(errors));
if (result == FAILURE) {
goto failure;
}
result = php_uri_parser_whatwg_fragment_write(lexbor_url, fragment, NULL);
php_uri_parser_whatwg_build_errors(Z_ARRVAL(errors));
if (result == FAILURE) {
goto failure;
}

if (php_uri_pass_errors_by_ref_and_free(soft_errors_zv, Z_ARRVAL(errors)) == FAILURE) {
Expand Down
Loading