-
Notifications
You must be signed in to change notification settings - Fork 125
Implements #89 by adding a FileResponse class #94
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
base: master
Are you sure you want to change the base?
Changes from all commits
7b305a1
14132ed
dcf0b38
0c473a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
|
|
||
| #pragma once | ||
|
|
||
| #include "seasocks/Request.h" | ||
| #include "seasocks/ResponseCode.h" | ||
|
|
||
| #include <map> | ||
|
|
@@ -52,6 +53,7 @@ class Response { | |
| static std::shared_ptr<Response> textResponse(const std::string& response); | ||
| static std::shared_ptr<Response> jsonResponse(const std::string& response); | ||
| static std::shared_ptr<Response> htmlResponse(const std::string& response); | ||
| static std::shared_ptr<Response> fileResponse(const Request &request, const std::string &filePath, const std::string &contentType, bool allowCompression, bool allowCaching); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a forward declaration possible here (to avoid the inclusion of |
||
| }; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,8 @@ void replace(std::string& string, const std::string& find, const std::string& re | |
| bool caseInsensitiveSame(const std::string &lhs, const std::string &rhs); | ||
|
|
||
| std::string webtime(time_t time); | ||
| // Returns -1 on error | ||
| time_t webtime(std::string time); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it reasonable to throw an exception in case of an error here? |
||
|
|
||
| std::string now(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) 2018, Joe Balough | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @scallopedllama / @mattgodbolt does this require an user to handle two licenses – the default text and this one?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm no lawyer, but I don't think the copyright affects the license. In other projects I have the author put their name, as long as the license remains intact below |
||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are met: | ||
| // | ||
| // Redistributions of source code must retain the above copyright notice, this | ||
| // list of conditions and the following disclaimer. | ||
| // | ||
| // Redistributions in binary form must reproduce the above copyright notice, | ||
| // this list of conditions and the following disclaimer in the documentation | ||
| // and/or other materials provided with the distribution. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| // POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "seasocks/Response.h" | ||
| #include "seasocks/ResponseWriter.h" | ||
| #include "seasocks/Request.h" | ||
| #include "seasocks/util/PathHandler.h" | ||
|
|
||
| namespace seasocks { | ||
|
|
||
| class FileResponse : public Response | ||
| { | ||
| public: | ||
| /// Supports returning file data back to a client supporting deflate compression on the fly. | ||
| /// If compression is disabled, file resuming is supported. | ||
| /// If compression is enabled, files resumption is disabled and file size is not provided to the client. | ||
| /// These are only determinable if the file is compressed before transmission, which this Response does not do. | ||
| /// @param request Incoming request | ||
| /// @param filePath Path to file to return to client in filesystem | ||
| /// @param contentType Content-Type header value for file | ||
| /// @param allowCompression Whether to support compressing the file on the fly with deflate during transmission (disables resuming and content length) | ||
| /// @param allowCaching Whether the file contents are allowed to be cached | ||
| explicit FileResponse(const Request &request, const std::string &filePath, const std::string &contentType, bool allowCompression, bool allowCaching); | ||
|
|
||
| virtual void handle(std::shared_ptr<ResponseWriter> writer) override; | ||
|
|
||
| /// Same as handle but does not spawn a thread | ||
| void respond(std::shared_ptr<ResponseWriter> writer); | ||
|
|
||
| virtual void cancel() override; | ||
|
|
||
|
|
||
| private: | ||
|
|
||
| ResponseCode parseHeaders(const off_t fileSize, const time_t fileLastModified, bool sendCompressedData, off_t& fileTransferStart, off_t& fileTransferEnd) const; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible avoid out-parameters and eg. return a tuple. |
||
|
|
||
| const Request &_request; | ||
| std::string _path; | ||
| std::string _contentType; | ||
| bool _allowCompression; | ||
| bool _allowCaching; | ||
| bool _cancelled; | ||
| }; | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Const-Ref here?