comparison Core/HttpServer/HttpOutput.cpp @ 217:1ac3aacd10a5

simplifications
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2012 12:22:23 +0100
parents 7f74209ea0f8
children 64925c94825c
comparison
equal deleted inserted replaced
216:e5d5d4a9a326 217:1ac3aacd10a5
42 namespace Orthanc 42 namespace Orthanc
43 { 43 {
44 void HttpOutput::SendString(const std::string& s) 44 void HttpOutput::SendString(const std::string& s)
45 { 45 {
46 if (s.size() > 0) 46 if (s.size() > 0)
47 {
47 Send(&s[0], s.size()); 48 Send(&s[0], s.size());
48 } 49 }
49
50 void HttpOutput::SendOkHeader(const std::string& contentType)
51 {
52 SendOkHeader(contentType.c_str(), false, 0, NULL);
53 } 50 }
54 51
55 void HttpOutput::SendOkHeader(const char* contentType, 52 void HttpOutput::SendOkHeader(const char* contentType,
56 bool hasContentLength, 53 bool hasContentLength,
57 uint64_t contentLength, 54 uint64_t contentLength,
133 { 130 {
134 SendOkHeader(contentType.c_str(), true, size, NULL); 131 SendOkHeader(contentType.c_str(), true, size, NULL);
135 Send(buffer, size); 132 Send(buffer, size);
136 } 133 }
137 134
138
139 void HttpOutput::AnswerFileWithContentType(const std::string& path,
140 const std::string& contentType,
141 const char* filename)
142 {
143 uint64_t fileSize = Toolbox::GetFileSize(path);
144
145 FILE* fp = fopen(path.c_str(), "rb");
146 if (!fp)
147 {
148 SendHeaderInternal(Orthanc_HttpStatus_500_InternalServerError);
149 return;
150 }
151
152 SendOkHeader(contentType.c_str(), true, fileSize, filename);
153
154 std::vector<uint8_t> buffer(1024 * 1024); // Chunks of 1MB
155
156 for (;;)
157 {
158 size_t nbytes = fread(&buffer[0], 1, buffer.size(), fp);
159 if (nbytes == 0)
160 {
161 break;
162 }
163 else
164 {
165 Send(&buffer[0], nbytes);
166 }
167 }
168
169 fclose(fp);
170 }
171
172
173 void HttpOutput::AnswerFileAutodetectContentType(const std::string& path,
174 const char* filename)
175 {
176 AnswerFileWithContentType(path, Toolbox::AutodetectMimeType(path), filename);
177 }
178
179
180 void HttpOutput::AnswerFile(const FileStorage& storage,
181 const std::string& uuid,
182 const std::string& contentType,
183 const char* filename)
184 {
185 boost::filesystem::path p(storage.GetPath(uuid));
186 AnswerFileWithContentType(p.string(), contentType, filename);
187 }
188
189
190
191 void HttpOutput::Redirect(const std::string& path) 135 void HttpOutput::Redirect(const std::string& path)
192 { 136 {
193 std::string s = 137 std::string s =
194 "HTTP/1.1 301 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_301_MovedPermanently)) + 138 "HTTP/1.1 301 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_301_MovedPermanently)) +
195 "\r\nLocation: " + path + 139 "\r\nLocation: " + path +