Serving static files using read() and asio::write() #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR would add the ability to serve static files.
Unlike my previous PR, this PR loads a file 16KB at a time then uses
asio::write
to push the 16KB through the socket, since it uses ASIO rather than thesendfile()
, it works with SSL.Additionally, there were some problems with large (4.4MB) files where
sendfile()
would not send the full file, instead hanging until the server times out.The performance difference is almost negligible (about 1ms for 700KB file and none for 4.4MB file). That's why I decided to use the 16KB method for both
http
andhttps
instead of the hybrid system withsendfile()
for http.I also wrote a test, though the test can only tell if the headers were proper and if the file was read, but not whether it was sent properly. This is due to the fact that a. I cannot access the socket and b. I wouldn't know what to do with it even if i could.
Requests to add arguments to the mime type file generator and returning a
404
if the file isn't found both are added here.Regarding windows support, I do not have a windows machine, but the only reason windows might be unsupported would be the
stat()
command, I admit I didn't look too far into whether or not it does support it or if it's just POSIX, so I kept the 'linux only' macros.Edit: I forgot to mention, for the test to work, a folder named
img
with a file namedcat.jpg
need to be placed inside thebuild
folder, I'm not sure how to get around this since the system I'm testing is about serving static files