comparison Core/HttpServer/HttpOutput.cpp @ 910:28a52982196e plugins

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 20 Jun 2014 13:38:19 +0200
parents e078ea944089
children 306afd58a0b3
comparison
equal deleted inserted replaced
909:ef71057d8b26 910:28a52982196e
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 class HttpOutput::StateMachine : public boost::noncopyable
46 {
47 protected:
48 enum State
49 {
50 State_WaitingHttpStatus,
51 State_WritingHeader,
52 State_WritingBody
53 };
54
55 private:
56 IHttpOutputStream& stream_;
57 State state_;
58
59 public:
60 HttpStateMachine() :
61 state_(State_WaitingHttpStatus)
62 {
63 }
64
65 void SendHttpStatus(HttpStatus status);
66
67 void SendHeaderData(const void* buffer, size_t length);
68
69 void SendHeaderString(const std::string& str);
70
71 void SendBodyData(const void* buffer, size_t length);
72
73 void SendBodyString(const std::string& str);
74 };
75
76
77 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status)
78 {
79 if (state_ != State_WaitingHttpStatus)
80 {
81 throw OrthancException(ErrorCode_BadSequenceOfCalls);
82 }
83
84 OnHttpStatusReceived(status);
85 state_ = State_WritingHeader;
86
87 std::string s = "HTTP/1.1 " +
88 boost::lexical_cast<std::string>(status) +
89 " " + std::string(EnumerationToString(status)) +
90 "\r\n";
91
92 Send(true, &s[0], s.size());
93 }
94
95 void HttpOutput::StateMachine::SendHeaderData(const void* buffer, size_t length)
96 {
97 if (state_ != State_WritingHeader)
98 {
99 throw OrthancException(ErrorCode_BadSequenceOfCalls);
100 }
101
102 Send(true, buffer, length);
103 }
104
105 void HttpOutput::StateMachine::SendHeaderString(const std::string& str)
106 {
107 if (str.size() > 0)
108 {
109 SendHeaderData(&str[0], str.size());
110 }
111 }
112
113 void HttpOutput::StateMachine::SendBodyData(const void* buffer, size_t length)
114 {
115 if (state_ == State_WaitingHttpStatus)
116 {
117 throw OrthancException(ErrorCode_BadSequenceOfCalls);
118 }
119
120 if (state_ == State_WritingHeader)
121 {
122 // Close the HTTP header before writing the body
123 Send(true, "\r\n", 2);
124 state_ = State_WritingBody;
125 }
126
127 if (length > 0)
128 {
129 Send(false, buffer, length);
130 }
131 }
132
133 void HttpOutput::StateMachine::SendBodyString(const std::string& str)
134 {
135 if (str.size() > 0)
136 {
137 SendBodyData(&str[0], str.size());
138 }
139 }
140
141
45 void HttpOutput::PrepareOkHeader(Header& header, 142 void HttpOutput::PrepareOkHeader(Header& header,
46 const char* contentType, 143 const char* contentType,
47 bool hasContentLength, 144 bool hasContentLength,
48 uint64_t contentLength, 145 uint64_t contentLength,
49 const char* contentFilename) 146 const char* contentFilename)