1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/**
* Orthanc - A Lightweight, RESTful DICOM Store
* Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
* Belgium
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* In addition, as a special exception, the copyright holders of this
* program give permission to link the code of its release with the
* OpenSSL project's "OpenSSL" library (or with modified versions of it
* that use the same license as the "OpenSSL" library), and distribute
* the linked executables. You must obey the GNU General Public License
* in all respects for all of the code used other than "OpenSSL". If you
* modify file(s) with this exception, you may extend this exception to
* your version of the file(s), but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source files
* in the program, then also delete it here.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include "../PrecompiledHeaders.h"
#include "HttpOutput.h"
#include <iostream>
#include <vector>
#include <stdio.h>
#include <glog/logging.h>
#include <boost/lexical_cast.hpp>
#include "../OrthancException.h"
#include "../Toolbox.h"
namespace Orthanc
{
HttpOutput::StateMachine::StateMachine(IHttpOutputStream& stream,
bool isKeepAlive) :
stream_(stream),
state_(State_WritingHeader),
status_(HttpStatus_200_Ok),
hasContentLength_(false),
contentPosition_(0),
keepAlive_(isKeepAlive)
{
}
HttpOutput::StateMachine::~StateMachine()
{
if (state_ != State_Done)
{
//asm volatile ("int3;");
LOG(ERROR) << "This HTTP answer does not contain any body";
}
if (hasContentLength_ && contentPosition_ != contentLength_)
{
LOG(ERROR) << "This HTTP answer has not sent the proper number of bytes in its body";
}
}
void HttpOutput::StateMachine::SetHttpStatus(HttpStatus status)
{
if (state_ != State_WritingHeader)
{
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
status_ = status;
}
void HttpOutput::StateMachine::SetContentLength(uint64_t length)
{
if (state_ != State_WritingHeader)
{
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
hasContentLength_ = true;
contentLength_ = length;
}
void HttpOutput::StateMachine::SetContentType(const char* contentType)
{
AddHeader("Content-Type", contentType);
}
void HttpOutput::StateMachine::SetContentFilename(const char* filename)
{
// TODO Escape double quotes
AddHeader("Content-Disposition", "filename=\"" + std::string(filename) + "\"");
}
void HttpOutput::StateMachine::SetCookie(const std::string& cookie,
const std::string& value)
{
if (state_ != State_WritingHeader)
{
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
// TODO Escape "=" characters
AddHeader("Set-Cookie", cookie + "=" + value);
}
void HttpOutput::StateMachine::AddHeader(const std::string& header,
const std::string& value)
{
if (state_ != State_WritingHeader)
{
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
headers_.push_back(header + ": " + value + "\r\n");
}
void HttpOutput::StateMachine::ClearHeaders()
{
if (state_ != State_WritingHeader)
{
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
headers_.clear();
}
void HttpOutput::StateMachine::SendBody(const void* buffer, size_t length)
{
if (state_ == State_Done)
{
if (length == 0)
{
return;
}
else
{
LOG(ERROR) << "Because of keep-alive connections, the entire body must be sent at once or Content-Length must be given";
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
}
if (state_ == State_WritingHeader)
{
// Send the HTTP header before writing the body
stream_.OnHttpStatusReceived(status_);
std::string s = "HTTP/1.1 " +
boost::lexical_cast<std::string>(status_) +
" " + std::string(EnumerationToString(status_)) +
"\r\n";
if (keepAlive_)
{
s += "Connection: keep-alive\r\n";
}
for (std::list<std::string>::const_iterator
it = headers_.begin(); it != headers_.end(); ++it)
{
s += *it;
}
if (status_ != HttpStatus_200_Ok)
{
hasContentLength_ = false;
}
uint64_t contentLength = (hasContentLength_ ? contentLength_ : length);
s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n\r\n";
stream_.Send(true, s.c_str(), s.size());
state_ = State_WritingBody;
}
if (hasContentLength_ &&
contentPosition_ + length > contentLength_)
{
LOG(ERROR) << "The body size exceeds what was declared with SetContentSize()";
throw OrthancException(ErrorCode_BadSequenceOfCalls);
}
if (length > 0)
{
stream_.Send(false, buffer, length);
contentPosition_ += length;
}
if (!hasContentLength_ ||
contentPosition_ == contentLength_)
{
state_ = State_Done;
}
}
void HttpOutput::SendMethodNotAllowed(const std::string& allowed)
{
stateMachine_.ClearHeaders();
stateMachine_.SetHttpStatus(HttpStatus_405_MethodNotAllowed);
stateMachine_.AddHeader("Allow", allowed);
stateMachine_.SendBody(NULL, 0);
}
void HttpOutput::SendStatus(HttpStatus status)
{
if (status == HttpStatus_200_Ok ||
status == HttpStatus_301_MovedPermanently ||
status == HttpStatus_401_Unauthorized ||
status == HttpStatus_405_MethodNotAllowed)
{
LOG(ERROR) << "Please use the dedicated methods to this HTTP status code in HttpOutput";
throw OrthancException(ErrorCode_ParameterOutOfRange);
}
stateMachine_.ClearHeaders();
stateMachine_.SetHttpStatus(status);
stateMachine_.SendBody(NULL, 0);
}
void HttpOutput::Redirect(const std::string& path)
{
stateMachine_.ClearHeaders();
stateMachine_.SetHttpStatus(HttpStatus_301_MovedPermanently);
stateMachine_.AddHeader("Location", path);
stateMachine_.SendBody(NULL, 0);
}
void HttpOutput::SendUnauthorized(const std::string& realm)
{
stateMachine_.ClearHeaders();
stateMachine_.SetHttpStatus(HttpStatus_401_Unauthorized);
stateMachine_.AddHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
stateMachine_.SendBody(NULL, 0);
}
void HttpOutput::SendBody(const void* buffer, size_t length)
{
stateMachine_.SendBody(buffer, length);
}
void HttpOutput::SendBody(const std::string& str)
{
if (str.size() == 0)
{
stateMachine_.SendBody(NULL, 0);
}
else
{
stateMachine_.SendBody(str.c_str(), str.size());
}
}
void HttpOutput::SendBody()
{
stateMachine_.SendBody(NULL, 0);
}
}
|