comparison Core/RestApi/RestApiOutput.cpp @ 330:78a8eaa5f30b

cookies
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Jan 2013 11:41:13 +0100
parents e5d5d4a9a326
children 639272ef7615
comparison
equal deleted inserted replaced
329:f579d50fdf8f 330:78a8eaa5f30b
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. 29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/ 30 **/
31 31
32 32
33 #include "RestApiOutput.h" 33 #include "RestApiOutput.h"
34
35 #include <boost/lexical_cast.hpp>
34 36
35 #include "../OrthancException.h" 37 #include "../OrthancException.h"
36 38
37 namespace Orthanc 39 namespace Orthanc
38 { 40 {
68 void RestApiOutput::AnswerJson(const Json::Value& value) 70 void RestApiOutput::AnswerJson(const Json::Value& value)
69 { 71 {
70 CheckStatus(); 72 CheckStatus();
71 Json::StyledWriter writer; 73 Json::StyledWriter writer;
72 std::string s = writer.write(value); 74 std::string s = writer.write(value);
73 output_.AnswerBufferWithContentType(s, "application/json"); 75 output_.AnswerBufferWithContentType(s, "application/json", cookies_);
74 alreadySent_ = true; 76 alreadySent_ = true;
75 } 77 }
76 78
77 void RestApiOutput::AnswerBuffer(const std::string& buffer, 79 void RestApiOutput::AnswerBuffer(const std::string& buffer,
78 const std::string& contentType) 80 const std::string& contentType)
79 { 81 {
80 CheckStatus(); 82 CheckStatus();
81 output_.AnswerBufferWithContentType(buffer, contentType); 83 output_.AnswerBufferWithContentType(buffer, contentType, cookies_);
82 alreadySent_ = true; 84 alreadySent_ = true;
83 } 85 }
84 86
85 void RestApiOutput::Redirect(const std::string& path) 87 void RestApiOutput::Redirect(const std::string& path)
86 { 88 {
89 alreadySent_ = true; 91 alreadySent_ = true;
90 } 92 }
91 93
92 void RestApiOutput::SignalError(Orthanc_HttpStatus status) 94 void RestApiOutput::SignalError(Orthanc_HttpStatus status)
93 { 95 {
94 if (status != Orthanc_HttpStatus_415_UnsupportedMediaType) 96 if (status != Orthanc_HttpStatus_403_Forbidden &&
97 status != Orthanc_HttpStatus_415_UnsupportedMediaType)
95 { 98 {
96 throw OrthancException("This HTTP status is not allowed in a REST API"); 99 throw OrthancException("This HTTP status is not allowed in a REST API");
97 } 100 }
98 101
99 CheckStatus(); 102 CheckStatus();
100 output_.SendHeader(status); 103 output_.SendHeader(status);
101 alreadySent_ = true; 104 alreadySent_ = true;
102 } 105 }
106
107 void RestApiOutput::SetCookie(const std::string& name,
108 const std::string& value,
109 unsigned int maxAge)
110 {
111 if (name.find(";") != std::string::npos ||
112 name.find(" ") != std::string::npos ||
113 value.find(";") != std::string::npos ||
114 value.find(" ") != std::string::npos)
115 {
116 throw OrthancException(ErrorCode_NotImplemented);
117 }
118
119 CheckStatus();
120
121 std::string v = value + ";path=/";
122
123 if (maxAge != 0)
124 {
125 v += ";max-age=" + boost::lexical_cast<std::string>(maxAge);
126 }
127
128 cookies_[name] = v;
129 }
130
131 void RestApiOutput::ResetCookie(const std::string& name)
132 {
133 // This marks the cookie to be deleted by the browser in 1 second,
134 // and before it actually gets deleted, its value is set to the
135 // empty string
136 SetCookie(name, "", 1);
137 }
103 } 138 }