changeset 217:1ac3aacd10a5

simplifications
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2012 12:22:23 +0100
parents e5d5d4a9a326
children 0200cd330582
files Core/HttpServer/FilesystemHttpHandler.cpp Core/HttpServer/FilesystemHttpSender.cpp Core/HttpServer/FilesystemHttpSender.h Core/HttpServer/HttpFileSender.cpp Core/HttpServer/HttpFileSender.h Core/HttpServer/HttpOutput.cpp Core/HttpServer/HttpOutput.h Core/HttpServer/MongooseServer.cpp Core/HttpServer/MongooseServer.h Core/RestApi/RestApi.h OrthancServer/OrthancRestApi.cpp OrthancServer/OrthancRestApi2.cpp
diffstat 12 files changed, 58 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/FilesystemHttpHandler.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/FilesystemHttpHandler.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -33,6 +33,7 @@
 #include "FilesystemHttpHandler.h"
 
 #include "../OrthancException.h"
+#include "FilesystemHttpSender.h"
 
 #include <boost/filesystem.hpp>
 
@@ -53,7 +54,7 @@
   {
     namespace fs = boost::filesystem;
 
-    output.SendOkHeader("text/html");
+    output.SendCustomOkHeader("Content-Type: text/html\r\n");
     output.SendString("<html>");
     output.SendString("  <body>");
     output.SendString("    <h1>Subdirectories</h1>");
@@ -148,7 +149,9 @@
 
     if (fs::exists(p) && fs::is_regular_file(p))
     {
-      output.AnswerFileAutodetectContentType(p.string());
+      FilesystemHttpSender(p).Send(output);
+
+      //output.AnswerFileAutodetectContentType(p.string());
     }
     else if (listDirectoryContent_ &&
              fs::exists(p) && 
--- a/Core/HttpServer/FilesystemHttpSender.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/FilesystemHttpSender.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -39,14 +39,13 @@
 {
   void FilesystemHttpSender::Setup()
   {
-    boost::filesystem::path p(path_);
-    SetFilename(p.filename().string());
-    SetContentType(Toolbox::AutodetectMimeType(p.filename().string()));
+    //SetDownloadFilename(path_.filename().string());
+    SetContentType(Toolbox::AutodetectMimeType(path_.filename().string()));
   }
 
   uint64_t FilesystemHttpSender::GetFileSize()
   {
-    return Toolbox::GetFileSize(path_);
+    return Toolbox::GetFileSize(path_.string());
   }
 
   bool FilesystemHttpSender::SendData(HttpOutput& output)
@@ -77,8 +76,15 @@
     return true;
   }
 
-  FilesystemHttpSender::FilesystemHttpSender(const char* path) : path_(path)
+  FilesystemHttpSender::FilesystemHttpSender(const char* path)
   {
+    path_ = std::string(path);
+    Setup();
+  }
+
+  FilesystemHttpSender::FilesystemHttpSender(const boost::filesystem::path& path)
+  {
+    path_ = path;
     Setup();
   }
 
--- a/Core/HttpServer/FilesystemHttpSender.h	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/FilesystemHttpSender.h	Thu Nov 29 12:22:23 2012 +0100
@@ -39,7 +39,7 @@
   class FilesystemHttpSender : public HttpFileSender
   {
   private:
-    std::string path_;
+    boost::filesystem::path path_;
 
     void Setup();
 
@@ -51,6 +51,8 @@
   public:
     FilesystemHttpSender(const char* path);
 
+    FilesystemHttpSender(const boost::filesystem::path& path);
+
     FilesystemHttpSender(const FileStorage& storage,
                          const std::string& uuid);
   };
--- a/Core/HttpServer/HttpFileSender.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/HttpFileSender.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -46,9 +46,9 @@
       header += "Content-Type: " + contentType_ + "\r\n";
     }
 
-    if (filename_.size() > 0)
+    if (downloadFilename_.size() > 0)
     {
-      header += "Content-Disposition: attachment; filename=\"" + filename_ + "\"\r\n";
+      header += "Content-Disposition: attachment; filename=\"" + downloadFilename_ + "\"\r\n";
     }
   
     output.SendCustomOkHeader(header);
--- a/Core/HttpServer/HttpFileSender.h	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/HttpFileSender.h	Thu Nov 29 12:22:23 2012 +0100
@@ -40,7 +40,7 @@
   {
   private:
     std::string contentType_;
-    std::string filename_;
+    std::string downloadFilename_;
 
     void SendHeader(HttpOutput& output);
 
@@ -69,19 +69,19 @@
       return contentType_;
     }
 
-    void ResetFilename()
+    void ResetDownloadFilename()
     {
-      contentType_.clear();
+      downloadFilename_.clear();
     }
 
-    void SetFilename(const std::string& filename)
+    void SetDownloadFilename(const std::string& filename)
     {
-      filename_ = filename;
+      downloadFilename_ = filename;
     }
 
-    const std::string& GetFilename() const
+    const std::string& GetDownloadFilename() const
     {
-      return filename_;
+      return downloadFilename_;
     }
 
     void Send(HttpOutput& output);
--- a/Core/HttpServer/HttpOutput.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/HttpOutput.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -44,12 +44,9 @@
   void HttpOutput::SendString(const std::string& s)
   {
     if (s.size() > 0)
+    {
       Send(&s[0], s.size());
-  }
-
-  void HttpOutput::SendOkHeader(const std::string& contentType)
-  {
-    SendOkHeader(contentType.c_str(), false, 0, NULL);
+    }
   }
 
   void HttpOutput::SendOkHeader(const char* contentType,
@@ -135,59 +132,6 @@
     Send(buffer, size);
   }
 
-
-  void HttpOutput::AnswerFileWithContentType(const std::string& path,
-                                             const std::string& contentType,
-                                             const char* filename)
-  {
-    uint64_t fileSize = Toolbox::GetFileSize(path);
-  
-    FILE* fp = fopen(path.c_str(), "rb");
-    if (!fp)
-    {
-      SendHeaderInternal(Orthanc_HttpStatus_500_InternalServerError);
-      return;
-    }
-  
-    SendOkHeader(contentType.c_str(), true, fileSize, filename);
-
-    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
-      {
-        Send(&buffer[0], nbytes);
-      }
-    }
-
-    fclose(fp);
-  }
-
-
-  void HttpOutput::AnswerFileAutodetectContentType(const std::string& path,
-                                                   const char* filename)
-  {
-    AnswerFileWithContentType(path, Toolbox::AutodetectMimeType(path), filename);
-  }
-
-
-  void HttpOutput::AnswerFile(const FileStorage& storage,
-                              const std::string& uuid,
-                              const std::string& contentType,
-                              const char* filename)
-  {
-    boost::filesystem::path p(storage.GetPath(uuid));
-    AnswerFileWithContentType(p.string(), contentType, filename);
-  }
-
-
-
   void HttpOutput::Redirect(const std::string& path)
   {
     std::string s = 
--- a/Core/HttpServer/HttpOutput.h	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/HttpOutput.h	Thu Nov 29 12:22:23 2012 +0100
@@ -35,7 +35,6 @@
 #include <string>
 #include <stdint.h>
 #include "../Enumerations.h"
-#include "../FileStorage.h"
 
 namespace Orthanc
 {
@@ -44,6 +43,11 @@
   private:
     void SendHeaderInternal(Orthanc_HttpStatus status);
 
+    void SendOkHeader(const char* contentType,
+                      bool hasContentLength,
+                      uint64_t contentLength,
+                      const char* contentFilename);
+
   public:
     virtual ~HttpOutput()
     {
@@ -51,13 +55,6 @@
 
     virtual void Send(const void* buffer, size_t length) = 0;
 
-    void SendOkHeader(const std::string& contentType);
-
-    void SendOkHeader(const char* contentType,
-                      bool hasContentLength,
-                      uint64_t contentLength,
-                      const char* contentFilename);
-
     void SendCustomOkHeader(const std::string& customHeader);
 
     void SendString(const std::string& s);
@@ -66,8 +63,9 @@
 
     void SendHeader(Orthanc_HttpStatus status);
 
+    void Redirect(const std::string& path);
 
-    // Higher-level constructs to send entire files or buffers -------------------
+    // Higher-level constructs to send entire buffers ----------------------------
 
     void AnswerBufferWithContentType(const std::string& buffer,
                                      const std::string& contentType);
@@ -75,19 +73,5 @@
     void AnswerBufferWithContentType(const void* buffer,
                                      size_t size,
                                      const std::string& contentType);
-
-    void AnswerFileWithContentType(const std::string& path,
-                                   const std::string& contentType,
-                                   const char* filename = NULL);
-
-    void AnswerFileAutodetectContentType(const std::string& path,
-                                         const char* filename = NULL); 
-
-    void AnswerFile(const FileStorage& storage,
-                    const std::string& uuid,
-                    const std::string& contentType,
-                    const char* filename = NULL);
-
-    void Redirect(const std::string& path);
   };
 }
--- a/Core/HttpServer/MongooseServer.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/MongooseServer.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -75,7 +75,10 @@
 
       virtual void Send(const void* buffer, size_t length)
       {
-        mg_write(connection_, buffer, length);
+        if (length > 0)
+        {
+          mg_write(connection_, buffer, length);
+        }
       }
     };
 
--- a/Core/HttpServer/MongooseServer.h	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/HttpServer/MongooseServer.h	Thu Nov 29 12:22:23 2012 +0100
@@ -36,6 +36,7 @@
 
 #include <list>
 #include <map>
+#include <set>
 #include <stdint.h>
 #include <boost/shared_ptr.hpp>
 
--- a/Core/RestApi/RestApi.h	Thu Nov 29 11:57:35 2012 +0100
+++ b/Core/RestApi/RestApi.h	Thu Nov 29 12:22:23 2012 +0100
@@ -36,6 +36,8 @@
 #include "RestApiPath.h"
 #include "RestApiOutput.h"
 
+#include <list>
+
 namespace Orthanc
 {
   class RestApi : public HttpHandler
--- a/OrthancServer/OrthancRestApi.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -53,7 +53,7 @@
   }
 
   void OrthancRestApi::ConnectToModality(DicomUserConnection& c,
-                                           const std::string& name)
+                                         const std::string& name)
   {
     std::string aet, address;
     int port;
@@ -66,7 +66,7 @@
   }
 
   bool OrthancRestApi::MergeQueryAndTemplate(DicomMap& result,
-                                               const std::string& postData)
+                                             const std::string& postData)
   {
     Json::Value query;
     Json::Reader reader;
@@ -88,8 +88,8 @@
   }
 
   bool OrthancRestApi::DicomFindPatient(Json::Value& result,
-                                          DicomUserConnection& c,
-                                          const std::string& postData)
+                                        DicomUserConnection& c,
+                                        const std::string& postData)
   {
     DicomMap m;
     DicomMap::SetupFindPatientTemplate(m);
@@ -105,8 +105,8 @@
   }
 
   bool OrthancRestApi::DicomFindStudy(Json::Value& result,
-                                        DicomUserConnection& c,
-                                        const std::string& postData)
+                                      DicomUserConnection& c,
+                                      const std::string& postData)
   {
     DicomMap m;
     DicomMap::SetupFindStudyTemplate(m);
@@ -128,8 +128,8 @@
   }
 
   bool OrthancRestApi::DicomFindSeries(Json::Value& result,
-                                         DicomUserConnection& c,
-                                         const std::string& postData)
+                                       DicomUserConnection& c,
+                                       const std::string& postData)
   {
     DicomMap m;
     DicomMap::SetupFindSeriesTemplate(m);
@@ -152,8 +152,8 @@
   }
 
   bool OrthancRestApi::DicomFind(Json::Value& result,
-                                   DicomUserConnection& c,
-                                   const std::string& postData)
+                                 DicomUserConnection& c,
+                                 const std::string& postData)
   {
     DicomMap m;
     DicomMap::SetupFindPatientTemplate(m);
@@ -222,8 +222,8 @@
 
 
   bool OrthancRestApi::DicomStore(Json::Value& result,
-                                    DicomUserConnection& c,
-                                    const std::string& postData)
+                                  DicomUserConnection& c,
+                                  const std::string& postData)
   {
     Json::Value found(Json::objectValue);
 
@@ -268,7 +268,7 @@
 
 
   OrthancRestApi::OrthancRestApi(ServerIndex& index,
-                                     const std::string& path) :
+                                 const std::string& path) :
     index_(index),
     storage_(path)
   {
--- a/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 11:57:35 2012 +0100
+++ b/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 12:22:23 2012 +0100
@@ -158,7 +158,7 @@
       assert(compressionType == CompressionType_None);
 
       FilesystemHttpSender sender(context.GetFileStorage(), fileUuid);
-      sender.SetFilename(fileUuid + ".dcm");
+      sender.SetDownloadFilename(fileUuid + ".dcm");
       sender.SetContentType("application/dicom");
       call.GetOutput().AnswerFile(sender);
     }