Skip to content
Open
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
11 changes: 11 additions & 0 deletions ext/lexbor/lexbor/url/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,17 @@ lxb_url_parse_basic_h(lxb_url_parser_t *parser, lxb_url_t *url,
lxb_url_parse_return(orig_data, buf, status);
}

/* Encode only the space immediately before a query or fragment. */
if (p > begin && p[-1] == ' ') {
tmp_str.length--;
if (lexbor_str_append(&tmp_str, url->mraw,
(const lxb_char_t *) "%20", 3) == NULL)
{
lxb_url_parse_return(orig_data, buf,
LXB_STATUS_ERROR_MEMORY_ALLOCATION);
}
}

status = lxb_url_path_list_push(url, &tmp_str);
if (status != LXB_STATUS_OK) {
lxb_url_parse_return(orig_data, buf, status);
Expand Down
6 changes: 3 additions & 3 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -1404,12 +1404,12 @@ PHP_METHOD(Uri_WhatWg_UrlBuilder, setFragment)
PHP_METHOD(Uri_WhatWg_UrlBuilder, build)
{
zval *base_url_zv = NULL;
zval *errors = NULL;
zval *soft_errors = NULL;

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(base_url_zv, php_uri_ce_whatwg_url)
Z_PARAM_ZVAL(errors)
Z_PARAM_ZVAL(soft_errors)
ZEND_PARSE_PARAMETERS_END();

const zval *scheme = Z_WHATWG_URL_PROP_SCHEME_DEREF_P(ZEND_THIS);
Expand All @@ -1430,7 +1430,7 @@ PHP_METHOD(Uri_WhatWg_UrlBuilder, build)

lxb_url_t *lexbor_url = php_uri_parser_whatwg_build_from_zval(
base_url, scheme, username, password, host, port, path, query, fragment,
errors
soft_errors
);
if (lexbor_url == NULL) {
RETURN_THROWS();
Expand Down
4 changes: 2 additions & 2 deletions ext/uri/php_uri.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ public function setQuery(?string $query): static {}

public function setFragment(?string $fragment): static {}

/** @param array $errors */
public function build(?\Uri\WhatWg\Url $baseUrl = null, &$errors = null): \Uri\WhatWg\Url {}
/** @param array $softErrors */
public function build(?\Uri\WhatWg\Url $baseUrl = null, &$softErrors = null): \Uri\WhatWg\Url {}
}

/** @strict-properties */
Expand Down
4 changes: 2 additions & 2 deletions ext/uri/php_uri_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/uri/php_uri_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setHost() - error - initializes validation errors
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();

try {
$builder->setHost("example.com/path");
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors);
}

?>
--EXPECTF--
Uri\WhatWg\InvalidUrlException: The specified host is malformed (HostInvalidCodePoint)
array(1) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(0) ""
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::HostInvalidCodePoint)
["failure"]=>
bool(true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - initializes errors for invalid component combinations
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("foo");
$builder->setUsername("user");

try {
$builder->build();
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors);
}

?>
--EXPECT--
Uri\WhatWg\InvalidUrlException: The specified URL cannot have username
array(0) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - password with a host that normalizes to empty
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("foo");
$builder->setHost("\t\n");
$builder->setPassword("pass");
$softErrors = ["unchanged"];

try {
$builder->build(softErrors: $softErrors);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors);
}

var_dump($softErrors);

?>
--EXPECTF--
Uri\WhatWg\InvalidUrlException: The specified URL cannot have password
array(1) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(2) "
"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
}
array(1) {
[0]=>
string(9) "unchanged"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - port with a host that normalizes to empty
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("foo");
$builder->setHost("\t\n");
$builder->setPort(123);
$softErrors = ["unchanged"];

try {
$builder->build(softErrors: $softErrors);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors);
}

var_dump($softErrors);

?>
--EXPECTF--
Uri\WhatWg\InvalidUrlException: The specified URL cannot have port
array(1) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(2) "
"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
}
array(1) {
[0]=>
string(9) "unchanged"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - username with a host that normalizes to empty
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("foo");
$builder->setHost("\t\n");
$builder->setUsername("user");
$softErrors = ["unchanged"];

try {
$builder->build(softErrors: $softErrors);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors);
}

var_dump($softErrors);

?>
--EXPECTF--
Uri\WhatWg\InvalidUrlException: The specified URL cannot have username
array(1) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(2) "
"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
}
array(1) {
[0]=>
string(9) "unchanged"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - leaves soft errors unchanged
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("ht\ttps");
$builder->setHost(null);
$softErrors = ["unchanged"];

try {
$builder->build(softErrors: $softErrors);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors);
}

var_dump($softErrors);

?>
--EXPECTF--
Uri\WhatWg\InvalidUrlException: The specified host is malformed (HostMissing)
array(2) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(4) " tps"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
[1]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(0) ""
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::HostMissing)
["failure"]=>
bool(true)
}
}
array(1) {
[0]=>
string(9) "unchanged"
}
50 changes: 50 additions & 0 deletions ext/uri/tests/whatwg/builder/build_success_soft_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - returns soft errors
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setScheme("https");
$builder->setHost("example.com");
$builder->setFragment("a\tb");
$softErrors = [];
$url = $builder->build(softErrors: $softErrors);

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

?>
--EXPECTF--
string(23) "https://example.com/#ab"
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(2) "ab"
}
array(1) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(2) " b"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
}
bool(true)
Loading
Loading