Skip to content

[Swoole PSR7] Fix ServerRequest header Nyholm PSR7 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2022

Conversation

pistej
Copy link
Contributor

@pistej pistej commented Oct 2, 2022

When using swoole app with PSR7, heades are not correctly set from Swoole\Http\Request to \Nyholm\Psr7\ServerRequest

@pistej pistej changed the title Fix ServerRequest header Nyholm PSR7 [Swoole PSR7] Fix ServerRequest header Nyholm PSR7 Oct 2, 2022
@pistej
Copy link
Contributor Author

pistej commented Oct 9, 2022

Hi @Nyholm, can you check this PR ? thx

@@ -39,7 +39,7 @@ public function handle(Request $request, Response $response): void
$psrRequest = (new \Nyholm\Psr7\ServerRequest(
$request->getMethod(),
$request->server['request_uri'] ?? '/',
array_change_key_case($request->server ?? [], CASE_UPPER),
$request->header ?? [],
Copy link
Member

Choose a reason for hiding this comment

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

The CASE_UPPER looks strange here. If I look at the the ServerRequest, it always converts uppercase to lowercase so CASE_LOWER would maybe make more sense: https://github.com/Nyholm/psr7/blob/f734364e38a876a23be4d906a2a089e1315be18a/src/MessageTrait.php#L147

It is also converted in other class to uppercase here:

array_change_key_case($request->server ?? [], CASE_UPPER),

@Nyholm what do you think?

If we change it we would need todo this in all classes to be consistent.

@pistej What type of error you are trying to solve. Can you try to create a test case? Without a test case things get lost over the time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the error is, that headers from any request send to swoole server are not available in Nyholm ServerRequest -> here is wrong assignmen, dont know why $request->server is set to $header parameter

Copy link
Member

@alexander-schranz alexander-schranz Oct 9, 2022

Choose a reason for hiding this comment

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

I see, why remove the array_change_key_case? can we not keep that then?

Copy link
Member

Choose a reason for hiding this comment

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

Still the same here:

array_change_key_case($request->server ?? [], CASE_UPPER),

Is there any difference between swoole and openswoole here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

think headers keys are case-insensitive besed on RFC 7230 and more validation is done in ServerRequest __construct, setHeader is called and then validateAndTrimHeader from MessageTrait, so just assign without modifying is ok
https://github.com/Nyholm/psr7/blob/master/src/MessageTrait.php#L138

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in SymfonyHttpBridge there is new SymfonyRequest and on this line 30 is set $server in construct, this assingnemt is ok, server to server, and ServeBag is created https://github.com/symfony/http-foundation/blob/6.1/Request.php#L274 where all upsercasse keys are used, which is good https://github.com/symfony/http-foundation/blob/6.1/ServerBag.php#L26

Copy link
Contributor Author

@pistej pistej Oct 9, 2022

Choose a reason for hiding this comment

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

and no, there's no diff. between swoole and openswoole in this case

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you

@Nyholm Nyholm merged commit 3f41435 into php-runtime:main Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants