Skip to content

Commit

Permalink
Fixed there is no Content-Range response header when the range reques…
Browse files Browse the repository at this point in the history
…t returns all the contents of the file (#5203)
  • Loading branch information
Yurunsoft authored Dec 26, 2023
1 parent 0063406 commit 828de1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
18 changes: 10 additions & 8 deletions ext-src/swoole_http2_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,19 @@ static bool http2_server_is_static_file(Server *serv, HttpContext *ctx) {
ctx->response.status = handler.status_code;
auto tasks = handler.get_tasks();
if (1 == tasks.size()) {
if (0 == tasks[0].offset && tasks[0].length == handler.get_filesize()) {
ctx->set_header(ZEND_STRL("Accept-Ranges"), SW_STRL("bytes"), 0);
} else {
if (SW_HTTP_PARTIAL_CONTENT == handler.status_code) {
std::stringstream content_range;
content_range << "bytes";
if (tasks[0].length != handler.get_filesize()) {
content_range << " " << tasks[0].offset << "-" << (tasks[0].length + tasks[0].offset - 1) << "/"
<< handler.get_filesize();
}
content_range << "bytes "
<< tasks[0].offset
<< "-"
<< (tasks[0].length + tasks[0].offset - 1)
<< "/"
<< handler.get_filesize()
<< "\r\n";
auto content_range_str = content_range.str();
ctx->set_header(ZEND_STRL("Content-Range"), content_range_str.c_str(), content_range_str.length(), 0);
} else {
ctx->set_header(ZEND_STRL("Accept-Ranges"), SW_STRL("bytes"), 0);
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/protocol/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ bool Server::select_static_handler(http_server::Request *request, Connection *co

std::stringstream header_stream;
if (1 == tasks.size()) {
if (0 == tasks[0].offset && tasks[0].length == handler.get_filesize()) {
header_stream << "Accept-Ranges: bytes\r\n";
if (SW_HTTP_PARTIAL_CONTENT == handler.status_code) {
header_stream << "Content-Range: bytes "
<< tasks[0].offset
<< "-"
<< (tasks[0].length + tasks[0].offset - 1)
<< "/"
<< handler.get_filesize()
<< "\r\n";
} else {
header_stream << "Content-Range: bytes";
if (tasks[0].length != handler.get_filesize()) {
header_stream << " " << tasks[0].offset << "-" << (tasks[0].length + tasks[0].offset - 1) << "/"
<< handler.get_filesize();
}
header_stream << "\r\n";
header_stream << "Accept-Ranges: bytes\r\n";
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/swoole_http_server/range.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ $pm->parentFunc = function () use ($pm) {
$lastModified = $response['headers']['last-modified'] ?? null;
Assert::notNull($lastModified);
Assert::null($response['headers']['accept-ranges'] ?? null);
$response = httpRequest("http://127.0.0.1:{$pm->getFreePort()}/test.jpg", ['http2' => $http2, 'headers' => ['Range' => 'bytes=0-']]);
Assert::same($response['statusCode'], 206);
Assert::same('bytes 0-218786/218787', $response['headers']['content-range']);
Assert::same(bin2hex($response['body']), bin2hex($data2));
// exit;
$response = httpRequest("http://127.0.0.1:{$pm->getFreePort()}/test.jpg", ['http2' => $http2, 'headers' => ['Range' => 'bytes=16-31']]);
Assert::same($response['statusCode'], 206);
Assert::same('bytes 16-31/218787', $response['headers']['content-range']);
Expand Down

0 comments on commit 828de1d

Please sign in to comment.