Skip to content

Commit

Permalink
Merge pull request #349 from nextcloud/handle-no-enc-headers
Browse files Browse the repository at this point in the history
fix: handle icap responses with no encapsulated headers
  • Loading branch information
icewind1991 authored Jun 28, 2024
2 parents e3b798c + 956eb07 commit 45f4858
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/ICAP/ResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,28 @@ private function readIcapStatusLine($stream): IcapResponseStatus {
return new IcapResponseStatus("$v1.$v2", (int)$code, $status);
}

private function parseResHdr($stream, string $headerValue): array {
private function parseEncapsulated(string $headerValue): array {
$result = [];
$encapsulatedParts = \explode(",", $headerValue);
foreach ($encapsulatedParts as $encapsulatedPart) {
$pieces = \explode("=", \trim($encapsulatedPart));
if ($pieces[0] === "res-hdr") {
$offset = (int)$pieces[1];
if ($offset > 0) {
fseek($stream, $offset);
}
break;
$result[$pieces[0]] = (int)$pieces[1];
}
return $result;
}

private function parseResHdr($stream, string $headerValue): array {
$encapsulated = $this->parseEncapsulated($headerValue);
if (isset($encapsulated['res-hdr'])) {
if ($encapsulated['res-hdr'] > 0) {
fseek($stream, $encapsulated['res-hdr'], SEEK_CUR);
}
} elseif (isset($encapsulated['req-hdr'])) {
if ($encapsulated['req-hdr'] > 0) {
fseek($stream, $encapsulated['req-hdr'], SEEK_CUR);
}
} else {
return [];
}

$status = trim(\fgets($stream));
Expand Down
5 changes: 5 additions & 0 deletions tests/ICAP/ResponseParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function testParse403() {
$response = $this->parseResponse(__DIR__ . '/../data/icap/403-response.txt');
$this->assertEquals('HTTP/1.1 403 Forbidden', $response->getResponseHeaders()['HTTP_STATUS']);
}

public function testParseNullBody() {
$response = $this->parseResponse(__DIR__ . '/../data/icap/null-body.txt');
$this->assertEquals([], $response->getResponseHeaders());
}
}
3 changes: 3 additions & 0 deletions tests/data/icap/null-body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ICAP/1.0 200 OK
Encapsulated: null-body=0
ISTag: "b9eb4f031598bb0b-1716440776"

0 comments on commit 45f4858

Please sign in to comment.