0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 07:54:05 +01:00

modules/root: Use regular encoding for static content w/ known size.

This commit is contained in:
Jason Volk 2018-04-25 18:17:29 -07:00
parent 7e32d3cbaa
commit 4d3999b7b8

View file

@ -51,15 +51,20 @@ try
if(it == end(files)) if(it == end(files))
throw http::error{http::NOT_FOUND}; throw http::error{http::NOT_FOUND};
const unique_buffer<mutable_buffer> chunk_buffer const unique_buffer<mutable_buffer> buffer
{ {
48_KiB 24_KiB
}; };
const auto &filename{it->second}; const auto &file_name{it->second};
const string_view first_chunk const size_t file_size
{ {
fs::read(filename, chunk_buffer) fs::size(file_name)
};
string_view chunk
{
fs::read(file_name, buffer)
}; };
char content_type_buf[64]; char content_type_buf[64];
@ -67,52 +72,36 @@ try
{ {
client, client,
http::OK, http::OK,
content_type(content_type_buf, filename, first_chunk), content_type(content_type_buf, file_name, chunk),
size(first_chunk) < size(chunk_buffer)? size(first_chunk) : -1 file_size
}; };
if(size(first_chunk) < size(chunk_buffer)) const unwind::exceptional terminate{[&client]
{ {
client.write_all(first_chunk);
return {};
}
char headbuf[64];
const unwind::exceptional terminate{[&headbuf, &client]
{
//TODO: find out if it's not a client problem so we don't have to eject
client.close(net::dc::RST, net::close_ignore); client.close(net::dc::RST, net::close_ignore);
}}; }};
client.write_all(http::writechunk(headbuf, size(first_chunk))); size_t written
client.write_all(first_chunk);
client.write_all("\r\n"_sv);
for(size_t offset(size(first_chunk));;)
{ {
const string_view chunk client.write_all(chunk)
{ };
fs::read(filename, chunk_buffer, offset)
};
if(empty(chunk)) size_t offset
break; {
size(chunk)
};
const string_view head while(offset < file_size)
{ {
http::writechunk(headbuf, size(chunk)) chunk = fs::read(file_name, buffer, offset);
}; assert(!empty(chunk));
written += client.write_all(chunk);
client.write_all(head);
client.write_all(chunk);
client.write_all("\r\n"_sv);
offset += size(chunk); offset += size(chunk);
if(size(chunk) < size(chunk_buffer)) assert(written == offset);
break;
} }
client.write_all(http::writechunk(headbuf, 0)); assert(offset == file_size);
client.write_all("\r\n"_sv); assert(written == offset);
return {}; return {};
} }
catch(const fs::filesystem_error &e) catch(const fs::filesystem_error &e)