comparison Core/RestApi/RestApiOutput.cpp @ 1113:ba5c0908600c

Refactoring of HttpOutput ("Content-Length" header is now always sent)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Sep 2014 15:51:20 +0200
parents f21f7783f934
children 0561f2087cc9
comparison
equal deleted inserted replaced
1112:a119f9ae3640 1113:ba5c0908600c
48 } 48 }
49 49
50 RestApiOutput::~RestApiOutput() 50 RestApiOutput::~RestApiOutput()
51 { 51 {
52 } 52 }
53
54 void RestApiOutput::Finalize()
55 {
56 if (!alreadySent_)
57 {
58 output_.SendStatus(HttpStatus_404_NotFound);
59 }
60 }
53 61
54 void RestApiOutput::CheckStatus() 62 void RestApiOutput::CheckStatus()
55 { 63 {
56 if (alreadySent_) 64 if (alreadySent_)
57 { 65 {
73 if (convertJsonToXml_) 81 if (convertJsonToXml_)
74 { 82 {
75 #if ORTHANC_PUGIXML_ENABLED == 1 83 #if ORTHANC_PUGIXML_ENABLED == 1
76 std::string s; 84 std::string s;
77 Toolbox::JsonToXml(s, value); 85 Toolbox::JsonToXml(s, value);
78 output_.AnswerBufferWithContentType(s, "application/xml"); 86 output_.SetContentType("application/xml");
87 output_.SendBody(s);
79 #else 88 #else
80 LOG(ERROR) << "Orthanc was compiled without XML support"; 89 LOG(ERROR) << "Orthanc was compiled without XML support";
81 throw OrthancException(ErrorCode_InternalError); 90 throw OrthancException(ErrorCode_InternalError);
82 #endif 91 #endif
83 } 92 }
84 else 93 else
85 { 94 {
86 Json::StyledWriter writer; 95 Json::StyledWriter writer;
87 std::string s = writer.write(value); 96 output_.SetContentType("application/json");
88 output_.AnswerBufferWithContentType(s, "application/json"); 97 output_.SendBody(writer.write(value));
89 } 98 }
90 99
91 alreadySent_ = true; 100 alreadySent_ = true;
92 } 101 }
93 102
94 void RestApiOutput::AnswerBuffer(const std::string& buffer, 103 void RestApiOutput::AnswerBuffer(const std::string& buffer,
95 const std::string& contentType) 104 const std::string& contentType)
96 { 105 {
97 CheckStatus(); 106 CheckStatus();
98 output_.AnswerBufferWithContentType(buffer, contentType); 107 output_.SetContentType(contentType.c_str());
108 output_.SendBody(buffer);
99 alreadySent_ = true; 109 alreadySent_ = true;
100 } 110 }
101 111
102 void RestApiOutput::AnswerBuffer(const void* buffer, 112 void RestApiOutput::AnswerBuffer(const void* buffer,
103 size_t length, 113 size_t length,
104 const std::string& contentType) 114 const std::string& contentType)
105 { 115 {
106 CheckStatus(); 116 CheckStatus();
107 output_.AnswerBufferWithContentType(buffer, length, contentType); 117 output_.SetContentType(contentType.c_str());
118 output_.SendBody(buffer, length);
108 alreadySent_ = true; 119 alreadySent_ = true;
109 } 120 }
110 121
111 void RestApiOutput::Redirect(const std::string& path) 122 void RestApiOutput::Redirect(const std::string& path)
112 { 123 {
122 { 133 {
123 throw OrthancException("This HTTP status is not allowed in a REST API"); 134 throw OrthancException("This HTTP status is not allowed in a REST API");
124 } 135 }
125 136
126 CheckStatus(); 137 CheckStatus();
127 output_.SendHeader(status); 138 output_.SendStatus(status);
128 alreadySent_ = true; 139 alreadySent_ = true;
129 } 140 }
130 141
131 void RestApiOutput::SetCookie(const std::string& name, 142 void RestApiOutput::SetCookie(const std::string& name,
132 const std::string& value, 143 const std::string& value,