Mercurial > hg > orthanc
diff UnitTests/ServerIndex.cpp @ 208:de640de989b8
HttpFileSender
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 28 Nov 2012 17:00:46 +0100 |
parents | 7f74209ea0f8 |
children | 9960642f0f45 |
line wrap: on
line diff
--- a/UnitTests/ServerIndex.cpp Wed Nov 28 16:23:11 2012 +0100 +++ b/UnitTests/ServerIndex.cpp Wed Nov 28 17:00:46 2012 +0100 @@ -5,7 +5,6 @@ #include <ctype.h> #include <glog/logging.h> - using namespace Orthanc; namespace @@ -261,10 +260,16 @@ + +#include "../Core/HttpServer/FilesystemHttpSender.h" + #include "../Core/Toolbox.h" #include "../Core/HttpServer/HttpOutput.h" #include "../Core/HttpServer/HttpHandler.h" +#include "../Core/HttpServer/HttpFileSender.h" + + namespace Orthanc { class RestApiPath @@ -377,131 +382,6 @@ }; - class HttpFileSender - { - private: - std::string contentType_; - std::string filename_; - - void SendHeader(HttpOutput& output) - { - std::string header; - header += "Content-Length: " + boost::lexical_cast<std::string>(GetFileSize()) + "\r\n"; - - if (contentType_.size() > 0) - { - header += "Content-Type: " + contentType_ + "\r\n"; - } - - if (filename_.size() > 0) - { - header += "Content-Disposition: attachment; filename=\"" + filename_ + "\"\r\n"; - } - - output.SendCustomOkHeader(header); - } - - protected: - virtual uint64_t GetFileSize() = 0; - - virtual bool SendData(HttpOutput& output) = 0; - - public: - virtual ~HttpFileSender() - { - } - - void ResetContentType() - { - contentType_.clear(); - } - - void SetContentType(const std::string& contentType) - { - contentType_ = contentType; - } - - const std::string& GetContentType() const - { - return contentType_; - } - - void ResetFilename() - { - contentType_.clear(); - } - - void SetFilename(const std::string& filename) - { - filename_ = filename; - } - - const std::string& GetFilename() const - { - return filename_; - } - - void Send(HttpOutput& output) - { - SendHeader(output); - - if (!SendData(output)) - { - output.SendHeader(Orthanc_HttpStatus_500_InternalServerError); - } - } - }; - - - class FilesystemHttpSender : public HttpFileSender - { - private: - std::string path_; - - protected: - virtual uint64_t GetFileSize() - { - return Toolbox::GetFileSize(path_); - } - - virtual bool SendData(HttpOutput& output) - { - FILE* fp = fopen(path_.c_str(), "rb"); - if (!fp) - { - return false; - } - - std::vector<uint8_t> buffer(1024 * 1024); // Chunks of 1MB - - for (;;) - { - size_t nbytes = fread(&buffer[0], 1, buffer.size(), fp); - if (nbytes == 0) - { - break; - } - else - { - output.Send(&buffer[0], nbytes); - } - } - - fclose(fp); - - return true; - } - - public: - FilesystemHttpSender(const char* path) : path_(path) - { - boost::filesystem::path p(path); - SetFilename(p.filename().string()); - SetContentType(Toolbox::AutodetectMimeType(p.filename().string())); - } - }; - - class RestApiOutput { private: @@ -994,14 +874,3 @@ /*LOG(WARNING) << "REST has started"; Toolbox::ServerBarrier();*/ } - - -/** - - output.AnswerBufferWithContentType(s, "application/json"); - output.AnswerFile(storage_, fileUuid, contentType, filename.c_str()); - output.Redirect("app/explorer.html"); - output.SendHeader(Orthanc_HttpStatus_415_UnsupportedMediaType); - output.SendMethodNotAllowedError("GET"); - -**/