comparison Core/HttpServer/HttpOutput.cpp @ 1042:8d1845feb277

set cookies, not allowed methods, unauthorized in plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 17 Jul 2014 15:55:40 +0200
parents 306afd58a0b3
children ba5c0908600c
comparison
equal deleted inserted replaced
1041:2c49b7dffcec 1042:8d1845feb277
34 #include "HttpOutput.h" 34 #include "HttpOutput.h"
35 35
36 #include <iostream> 36 #include <iostream>
37 #include <vector> 37 #include <vector>
38 #include <stdio.h> 38 #include <stdio.h>
39 #include <glog/logging.h>
39 #include <boost/lexical_cast.hpp> 40 #include <boost/lexical_cast.hpp>
40 #include "../OrthancException.h" 41 #include "../OrthancException.h"
41 #include "../Toolbox.h" 42 #include "../Toolbox.h"
42 43
43 namespace Orthanc 44 namespace Orthanc
44 { 45 {
45 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status) 46 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status)
46 { 47 {
47 if (state_ != State_WaitingHttpStatus) 48 if (state_ != State_WaitingHttpStatus)
48 { 49 {
49 throw OrthancException(ErrorCode_BadSequenceOfCalls); 50 LOG(ERROR) << "Sending twice an HTTP status";
51 return;
50 } 52 }
51 53
52 stream_.OnHttpStatusReceived(status); 54 stream_.OnHttpStatusReceived(status);
53 state_ = State_WritingHeader; 55 state_ = State_WritingHeader;
54 56
151 it = header.begin(); it != header.end(); ++it) 153 it = header.begin(); it != header.end(); ++it)
152 { 154 {
153 s += it->first + ": " + it->second + "\r\n"; 155 s += it->first + ": " + it->second + "\r\n";
154 } 156 }
155 157
158 for (HttpHandler::Arguments::const_iterator
159 it = cookies_.begin(); it != cookies_.end(); ++it)
160 {
161 s += "Set-Cookie: " + it->first + "=" + it->second + "\r\n";
162 }
163
156 stateMachine_.SendHeaderString(s); 164 stateMachine_.SendHeaderString(s);
157 } 165 }
158 166
159 167
160 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) 168 void HttpOutput::SendMethodNotAllowed(const std::string& allowed)
161 { 169 {
162 stateMachine_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); 170 stateMachine_.SendHttpStatus(HttpStatus_405_MethodNotAllowed);
163 stateMachine_.SendHeaderString("Allow: " + allowed + "\r\n"); 171 stateMachine_.SendHeaderString("Allow: " + allowed + "\r\n");
164 } 172 }
165 173
179 187
180 188
181 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, 189 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer,
182 const std::string& contentType) 190 const std::string& contentType)
183 { 191 {
184 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL);
185 SendBodyString(buffer);
186 }
187
188
189 void HttpOutput::PrepareCookies(Header& header,
190 const HttpHandler::Arguments& cookies)
191 {
192 for (HttpHandler::Arguments::const_iterator it = cookies.begin();
193 it != cookies.end(); ++it)
194 {
195 header.push_back(std::make_pair("Set-Cookie", it->first + "=" + it->second));
196 }
197 }
198
199
200 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer,
201 const std::string& contentType,
202 const HttpHandler::Arguments& cookies)
203 {
204 Header header; 192 Header header;
205 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); 193 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL);
206 PrepareCookies(header, cookies);
207 SendOkHeader(header); 194 SendOkHeader(header);
208 SendBodyString(buffer); 195 SendBodyString(buffer);
209 } 196 }
210 197
211 198
212 void HttpOutput::AnswerBufferWithContentType(const void* buffer, 199 void HttpOutput::AnswerBufferWithContentType(const void* buffer,
213 size_t size, 200 size_t size,
214 const std::string& contentType) 201 const std::string& contentType)
215 { 202 {
216 SendOkHeader(contentType.c_str(), true, size, NULL);
217 SendBodyData(buffer, size);
218 }
219
220
221 void HttpOutput::AnswerBufferWithContentType(const void* buffer,
222 size_t size,
223 const std::string& contentType,
224 const HttpHandler::Arguments& cookies)
225 {
226 Header header; 203 Header header;
227 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); 204 PrepareOkHeader(header, contentType.c_str(), true, size, NULL);
228 PrepareCookies(header, cookies);
229 SendOkHeader(header); 205 SendOkHeader(header);
230 SendBodyData(buffer, size); 206 SendBodyData(buffer, size);
231 } 207 }
232 208
233 209