diff Core/HttpServer/HttpOutput.cpp @ 155:93e1b0e3b83a

filenames when downloading json/dicom
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 23 Oct 2012 15:43:46 +0200
parents fe180eae201d
children 7f74209ea0f8
line wrap: on
line diff
--- a/Core/HttpServer/HttpOutput.cpp	Tue Oct 23 15:12:40 2012 +0200
+++ b/Core/HttpServer/HttpOutput.cpp	Tue Oct 23 15:43:46 2012 +0200
@@ -49,33 +49,17 @@
 
   void HttpOutput::SendOkHeader(const std::string& contentType)
   {
-    SendOkHeader(contentType.c_str(), false, 0);
-  }
-
-  void HttpOutput::SendOkHeader()
-  {
-    SendOkHeader(NULL, false, 0);
+    SendOkHeader(contentType.c_str(), false, 0, NULL);
   }
 
-  void HttpOutput::SendOkHeader(uint64_t contentLength)
-  {
-    SendOkHeader(NULL, true, contentLength);
-  }
-
-  void HttpOutput::SendOkHeader(const std::string& contentType,
-                                uint64_t contentLength)
-  {
-    SendOkHeader(contentType.c_str(), true, contentLength);
-  }
-
-
   void HttpOutput::SendOkHeader(const char* contentType,
                                 bool hasContentLength,
-                                uint64_t contentLength)
+                                uint64_t contentLength,
+                                const char* contentFilename)
   {
     std::string s = "HTTP/1.1 200 OK\r\n";
 
-    if (contentType)
+    if (contentType && contentType[0] != '\0')
     {
       s += "Content-Type: " + std::string(contentType) + "\r\n";
     }
@@ -85,6 +69,11 @@
       s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n";
     }
 
+    if (contentFilename && contentFilename[0] != '\0')
+    {
+      s += "Content-Disposition: attachment; filename=\"" + std::string(contentFilename) + "\"\r\n";
+    }
+
     s += "\r\n";
 
     Send(&s[0], s.size());
@@ -126,7 +115,7 @@
   void HttpOutput::AnswerBufferWithContentType(const std::string& buffer,
                                                const std::string& contentType)
   {
-    SendOkHeader(contentType.c_str(), true, buffer.size());
+    SendOkHeader(contentType.c_str(), true, buffer.size(), NULL);
     SendString(buffer);
   }
 
@@ -135,13 +124,14 @@
                                                size_t size,
                                                const std::string& contentType)
   {
-    SendOkHeader(contentType.c_str(), true, size);
+    SendOkHeader(contentType.c_str(), true, size, NULL);
     Send(buffer, size);
   }
 
 
   void HttpOutput::AnswerFileWithContentType(const std::string& path,
-                                             const std::string& contentType)
+                                             const std::string& contentType,
+                                             const char* filename)
   {
     uint64_t fileSize = Toolbox::GetFileSize(path);
   
@@ -152,7 +142,7 @@
       return;
     }
   
-    SendOkHeader(contentType.c_str(), true, fileSize);
+    SendOkHeader(contentType.c_str(), true, fileSize, filename);
 
     std::vector<uint8_t> buffer(1024 * 1024);  // Chunks of 1MB
 
@@ -173,18 +163,20 @@
   }
 
 
-  void HttpOutput::AnswerFileAutodetectContentType(const std::string& path)
+  void HttpOutput::AnswerFileAutodetectContentType(const std::string& path,
+                                                   const char* filename)
   {
-    AnswerFileWithContentType(path, Toolbox::AutodetectMimeType(path));
+    AnswerFileWithContentType(path, Toolbox::AutodetectMimeType(path), filename);
   }
 
 
   void HttpOutput::AnswerFile(const FileStorage& storage,
                               const std::string& uuid,
-                              const std::string& contentType)
+                              const std::string& contentType,
+                              const char* filename)
   {
     boost::filesystem::path p(storage.GetPath(uuid));
-    AnswerFileWithContentType(p.string(), contentType);
+    AnswerFileWithContentType(p.string(), contentType, filename);
   }