Skip to content
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

fix: handle icap responses with no encapsulated headers #349

Merged
merged 1 commit into from
Jun 28, 2024
Merged
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
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"
Loading