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

Serving static files using read() and asio::write() #10

Merged
merged 15 commits into from
Oct 13, 2020

Conversation

The-EDev
Copy link
Member

@The-EDev The-EDev commented Oct 12, 2020

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 the sendfile(), 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 and https instead of the hybrid system with sendfile() 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 named cat.jpg need to be placed inside the build folder, I'm not sure how to get around this since the system I'm testing is about serving static files

Farook and others added 15 commits October 4, 2020 15:05
using with instead of open and close
removed while loop
using one line to write the new file
using unordered map instead of ordered map
added indentation to the generated file
…le command would just hang until timeout without fully sending the file).

Added 404 and fixed some issues with windows error message
sendfile still incompatible with windows due to the stat function
also changed the buffer size to 16KB
Test cannot actually test whether the file was sent or not since the test has no access to the socket itself (also idk how to get data that was passed through a socket)
@mrozigor mrozigor merged commit 562c9a3 into CrowCpp:master Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants