Skip to content

standard: various nit and refactorings for standard stream wrappers - #22875

Open
Girgias wants to merge 3 commits into
php:masterfrom
Girgias:2026-07-std-stream-wrapper-nits
Open

standard: various nit and refactorings for standard stream wrappers#22875
Girgias wants to merge 3 commits into
php:masterfrom
Girgias:2026-07-std-stream-wrapper-nits

Conversation

@Girgias

@Girgias Girgias commented Jul 23, 2026

Copy link
Copy Markdown
Member

No description provided.

@Girgias
Girgias marked this pull request as ready for review July 25, 2026 10:36
@Girgias
Girgias requested a review from bukka as a code owner July 25, 2026 10:36
@Girgias
Girgias requested a review from devnexen July 28, 2026 08:39
@@ -1128,7 +1125,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
} \
}
/* check for control characters in login, password & path */
if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
if (zend_string_starts_with_literal_ci(new_path, "http://") || zend_string_starts_with_literal_ci(new_path, "https://")) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems the old condition was always true regardless ? I would either drop the "migrated" version of it or

if (!zend_string_starts_with_literal_ci(new_path, "http://") && !zend_string_starts_with_literal_ci(new_path, "https://"))
```. wdyt ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm... not even sure what the semantics are. Reading the rest of the code it does seem to me that it expects the path to start with an HTTP protocol schema....

As we are passing new_path to php_stream_url_wrap_http_ex

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you re right.

Comment thread ext/standard/http_fopen_wrapper.c Outdated
@devnexen

Copy link
Copy Markdown
Member

Seems your PR resolves a relative Location (e.g. Location: baz on /foo/bar) to /foo/baz instead of master's /foo//baz, because it no longer inserts a / separator after a path prefix that already ends in one.

@Girgias

Girgias commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

Seems your PR resolves a relative Location (e.g. Location: baz on /foo/bar) to /foo/baz instead of master's /foo//baz, because it no longer inserts a / separator after a path prefix that already ends in one.

So I guess this PR fixes another pre-existing bug as well, and clearly this part of the stream wrapper is barely used and badly tested :/

/* check for control characters in login, password & path */
if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
if (zend_string_starts_with_literal_ci(new_path, "http://") || zend_string_starts_with_literal_ci(new_path, "https://")) {
CHECK_FOR_CNTRL_CHARS(resource->user);

@Sjord Sjord Sep 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for control characters in the output from php_uri_parse_to_struct. When using the URL default parser, the control characters have already been replaced by underscores when we get here (php_replace_controlchars in php_url_parse_ex2). When setting the parser to Uri\Rfc3986\Uri, the URL fails to parse. With Uri\WhatWg\Url, it is possible to get control characters in the URL.

The check used to be performed for all URLs, but now is only performed for http(s) URLs. So this makes it possible to redirect to e.g. FTP URLs that contain control characters.

But perhaps it should be up to the URL parser to decide how to handle control characters? If WhatWg thinks URLs can have control characters, should we really discard them here?

Edit: I tested this with this on the server:

<?php
header("Location: ftp://u\vser:pass@localhost:41303/");

And this as the client:

<?php
$context = stream_context_create([
    "http" => [
        "uri_parser_class" => \Uri\WhatWg\Url::class,
    ]
]);
file_get_contents("http://localhost:41303/", false, $context);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this makes sense, but might leave this for a follow-up PR and do this after 8.6 is branched as this might be contentious?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. There's no need to change that in this PR.

@Sjord

Sjord commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Looks good to me. But it indeed can use some more tests.

@Sjord

Sjord commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

#23557

@Girgias
Girgias force-pushed the 2026-07-std-stream-wrapper-nits branch from 766bdd8 to bc1aa5e Compare September 6, 2026 14:16
@Girgias

Girgias commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

@Sjord do you know why I get a different result for the redirection than your test? Not sure where I went wrong during the rebase.

@Sjord

Sjord commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Because you accidentally fixed the bug described in #23532? My test tests for the "wrong" behavior of the double slash. Is that what you meant?

@Girgias

Girgias commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

Because you accidentally fixed the bug described in #23532? My test tests for the "wrong" behavior of the double slash. Is that what you meant?

Right, I'll update the test then :)

- Use newer zend_string APIs
- Make logic more explicit and understandable
- Prevent some strlen() recomputations
@Girgias
Girgias force-pushed the 2026-07-std-stream-wrapper-nits branch from bc1aa5e to 3aaadc6 Compare September 6, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants