Mercurial > hg > orthanc
comparison Core/HttpServer/HttpOutput.cpp @ 908:e078ea944089 plugins
refactoring HttpOutput
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jun 2014 17:47:39 +0200 |
parents | a811bdf8b8eb |
children | 28a52982196e |
comparison
equal
deleted
inserted
replaced
907:9b8298234254 | 908:e078ea944089 |
---|---|
40 #include "../OrthancException.h" | 40 #include "../OrthancException.h" |
41 #include "../Toolbox.h" | 41 #include "../Toolbox.h" |
42 | 42 |
43 namespace Orthanc | 43 namespace Orthanc |
44 { | 44 { |
45 void HttpOutput::SendString(const std::string& s) | |
46 { | |
47 if (s.size() > 0) | |
48 { | |
49 Send(&s[0], s.size()); | |
50 } | |
51 } | |
52 | |
53 void HttpOutput::PrepareOkHeader(Header& header, | 45 void HttpOutput::PrepareOkHeader(Header& header, |
54 const char* contentType, | 46 const char* contentType, |
55 bool hasContentLength, | 47 bool hasContentLength, |
56 uint64_t contentLength, | 48 uint64_t contentLength, |
57 const char* contentFilename) | 49 const char* contentFilename) |
85 SendOkHeader(header); | 77 SendOkHeader(header); |
86 } | 78 } |
87 | 79 |
88 void HttpOutput::SendOkHeader(const Header& header) | 80 void HttpOutput::SendOkHeader(const Header& header) |
89 { | 81 { |
90 std::string s = "HTTP/1.1 200 OK\r\n"; | 82 stream_.SendHttpStatus(HttpStatus_200_Ok); |
91 | 83 |
84 std::string s; | |
92 for (Header::const_iterator | 85 for (Header::const_iterator |
93 it = header.begin(); it != header.end(); ++it) | 86 it = header.begin(); it != header.end(); ++it) |
94 { | 87 { |
95 s += it->first + ": " + it->second + "\r\n"; | 88 s += it->first + ": " + it->second + "\r\n"; |
96 } | 89 } |
97 | 90 |
98 s += "\r\n"; | 91 stream_.SendHeaderString(s); |
99 | |
100 Send(&s[0], s.size()); | |
101 } | 92 } |
102 | 93 |
103 | 94 |
104 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) | 95 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) |
105 { | 96 { |
106 std::string s = | 97 stream_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); |
107 "HTTP/1.1 405 " + std::string(EnumerationToString(HttpStatus_405_MethodNotAllowed)) + | 98 stream_.SendHeaderString("Allow: " + allowed + "\r\n"); |
108 "\r\nAllow: " + allowed + | |
109 "\r\n\r\n"; | |
110 Send(&s[0], s.size()); | |
111 } | 99 } |
112 | 100 |
113 | 101 |
114 void HttpOutput::SendHeader(HttpStatus status) | 102 void HttpOutput::SendHeader(HttpStatus status) |
115 { | 103 { |
116 if (status == HttpStatus_200_Ok || | 104 if (status == HttpStatus_200_Ok || |
105 status == HttpStatus_301_MovedPermanently || | |
106 status == HttpStatus_401_Unauthorized || | |
117 status == HttpStatus_405_MethodNotAllowed) | 107 status == HttpStatus_405_MethodNotAllowed) |
118 { | 108 { |
119 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); | 109 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
120 } | 110 } |
121 | 111 |
122 SendHeaderInternal(status); | 112 stream_.SendHttpStatus(status); |
123 } | |
124 | |
125 | |
126 void HttpOutput::SendHeaderInternal(HttpStatus status) | |
127 { | |
128 std::string s = "HTTP/1.1 " + | |
129 boost::lexical_cast<std::string>(status) + | |
130 " " + std::string(EnumerationToString(status)) + | |
131 "\r\n\r\n"; | |
132 Send(&s[0], s.size()); | |
133 } | 113 } |
134 | 114 |
135 | 115 |
136 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | 116 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, |
137 const std::string& contentType) | 117 const std::string& contentType) |
138 { | 118 { |
139 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); | 119 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
140 SendString(buffer); | 120 SendBodyString(buffer); |
141 } | 121 } |
142 | 122 |
143 | 123 |
144 void HttpOutput::PrepareCookies(Header& header, | 124 void HttpOutput::PrepareCookies(Header& header, |
145 const HttpHandler::Arguments& cookies) | 125 const HttpHandler::Arguments& cookies) |
158 { | 138 { |
159 Header header; | 139 Header header; |
160 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); | 140 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); |
161 PrepareCookies(header, cookies); | 141 PrepareCookies(header, cookies); |
162 SendOkHeader(header); | 142 SendOkHeader(header); |
163 SendString(buffer); | 143 SendBodyString(buffer); |
164 } | 144 } |
165 | 145 |
166 | 146 |
167 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | 147 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
168 size_t size, | 148 size_t size, |
169 const std::string& contentType) | 149 const std::string& contentType) |
170 { | 150 { |
171 SendOkHeader(contentType.c_str(), true, size, NULL); | 151 SendOkHeader(contentType.c_str(), true, size, NULL); |
172 Send(buffer, size); | 152 SendBodyData(buffer, size); |
173 } | 153 } |
174 | 154 |
175 | 155 |
176 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | 156 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
177 size_t size, | 157 size_t size, |
180 { | 160 { |
181 Header header; | 161 Header header; |
182 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); | 162 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); |
183 PrepareCookies(header, cookies); | 163 PrepareCookies(header, cookies); |
184 SendOkHeader(header); | 164 SendOkHeader(header); |
185 Send(buffer, size); | 165 SendBodyData(buffer, size); |
186 } | 166 } |
187 | |
188 | 167 |
189 | 168 |
190 void HttpOutput::Redirect(const std::string& path) | 169 void HttpOutput::Redirect(const std::string& path) |
191 { | 170 { |
192 std::string s = | 171 stream_.SendHttpStatus(HttpStatus_301_MovedPermanently); |
193 "HTTP/1.1 301 " + std::string(EnumerationToString(HttpStatus_301_MovedPermanently)) + | 172 stream_.SendHeaderString("Location: " + path + "\r\n"); |
194 "\r\nLocation: " + path + | |
195 "\r\n\r\n"; | |
196 Send(&s[0], s.size()); | |
197 } | 173 } |
174 | |
175 | |
176 void HttpOutput::SendUnauthorized(const std::string& realm) | |
177 { | |
178 stream_.SendHttpStatus(HttpStatus_401_Unauthorized); | |
179 stream_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n"); | |
180 } | |
181 | |
198 } | 182 } |