Skip to content

Commit

Permalink
Fix accessing params from a request with content-type, but without co…
Browse files Browse the repository at this point in the history
…ntent
  • Loading branch information
thekid committed Aug 26, 2024
1 parent ffb305e commit 2a47a61
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Web change log

## ?.?.? / ????-??-??

* Fixed accessing params from a request with content-type, but without
content; following the *be liberal in what you accept* paradigm.
(@thekid)

## 4.4.1 / 2024-07-07

* Fixed request dispatching inside development webserver - @thekid
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/web/Request.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ private function parse($peek= true) {
// Merge parameters from URL and urlencoded payload.
$query= $this->uri->query(false) ?? '';
$type= Headers::parameterized()->parse($this->header('Content-Type', ''));
if ('application/x-www-form-urlencoded' === $type->value()) {
$data= Streams::readAll($this->input->incoming());
if ('application/x-www-form-urlencoded' === $type->value() && $stream= $this->input->incoming()) {
$data= Streams::readAll($stream);
$this->stream= new MemoryInputStream($data);
$query.= '&'.$data;
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/php/web/unittest/RequestTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,13 @@ public function hash_code() {
$req= new Request(new TestInput('GET', '/', ['Host' => 'localhost', 'Connection' => 'close']));
Assert::equals(spl_object_hash($req), $req->hashCode());
}

#[Test]
public function form_encoded_without_payload() {
$headers= ['Content-Type' => 'application/x-www-form-urlencoded'];
Assert::equals(
['source' => 'query'],
(new Request(new TestInput('DELETE', '/?source=query', $headers)))->params()
);
}
}

0 comments on commit 2a47a61

Please sign in to comment.