Mercurial > hg > orthanc
annotate OrthancFramework/Sources/RestApi/RestApiOutput.cpp @ 5406:aaf7c49a9ddc am-http-compression
tentative to implement smart HTTP compression with detection of transfer syntax
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 04 Nov 2023 13:42:30 +0100 |
parents | 0ea402b4d901 |
children |
rev | line source |
---|---|
209 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1194
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
209 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
209 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
209 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
209 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
209 | 25 #include "RestApiOutput.h" |
26 | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1368
diff
changeset
|
27 #include "../Logging.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1368
diff
changeset
|
28 #include "../OrthancException.h" |
2142 | 29 #include "../Toolbox.h" |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
30 #include "../SystemToolbox.h" |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1368
diff
changeset
|
31 |
330 | 32 #include <boost/lexical_cast.hpp> |
33 | |
211 | 34 |
209 | 35 namespace Orthanc |
36 { | |
1368 | 37 RestApiOutput::RestApiOutput(HttpOutput& output, |
38 HttpMethod method) : | |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
39 output_(output), |
1368 | 40 method_(method), |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
41 convertJsonToXml_(false) |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
42 { |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
43 alreadySent_ = false; |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
44 } |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
45 |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
46 RestApiOutput::~RestApiOutput() |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
47 { |
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
48 } |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
49 |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
50 void RestApiOutput::Finalize() |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
51 { |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
52 if (!alreadySent_) |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
53 { |
1368 | 54 if (method_ == HttpMethod_Post) |
55 { | |
56 output_.SendStatus(HttpStatus_400_BadRequest); | |
57 } | |
58 else | |
59 { | |
60 output_.SendStatus(HttpStatus_404_NotFound); | |
61 } | |
1113
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
62 } |
ba5c0908600c
Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1100
diff
changeset
|
63 } |
211 | 64 |
65 void RestApiOutput::CheckStatus() | |
66 { | |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
67 if (alreadySent_) |
211 | 68 { |
69 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
70 } | |
71 } | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
209
diff
changeset
|
72 |
1514
d73a2178b319
support of deflate and gzip content-types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1511
diff
changeset
|
73 |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1518
diff
changeset
|
74 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream) |
209 | 75 { |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
76 AnswerStream(stream, ContentCompression_Unknown); |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
77 } |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
78 |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
79 void RestApiOutput::AnswerStream(IHttpStreamAnswer& stream, ContentCompression contentCompression) |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
80 { |
211 | 81 CheckStatus(); |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
82 output_.SetContentCompression(contentCompression); |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1518
diff
changeset
|
83 output_.Answer(stream); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
84 alreadySent_ = true; |
209 | 85 } |
86 | |
4672
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
87 |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
88 void RestApiOutput::AnswerWithoutBuffering(IHttpStreamAnswer& stream) |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
89 { |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
90 AnswerWithoutBuffering(stream, ContentCompression_Unknown); |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
91 } |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
92 |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
93 void RestApiOutput::AnswerWithoutBuffering(IHttpStreamAnswer& stream, ContentCompression contentCompression) |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
94 { |
4672
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
95 CheckStatus(); |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
96 output_.SetContentCompression(contentCompression); |
4672
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
97 output_.AnswerWithoutBuffering(stream); |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
98 alreadySent_ = true; |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
99 } |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
100 |
d9942d48fea7
ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
101 |
209 | 102 void RestApiOutput::AnswerJson(const Json::Value& value) |
103 { | |
211 | 104 CheckStatus(); |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
105 |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
106 if (convertJsonToXml_) |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
107 { |
2136
dd609a99d39a
uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2118
diff
changeset
|
108 #if ORTHANC_ENABLE_PUGIXML == 1 |
1517 | 109 std::string s; |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
110 Toolbox::JsonToXml(s, value); |
2905
ae20fccdd867
refactoring mime types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
111 |
2908
9d277f8ad698
new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2905
diff
changeset
|
112 output_.SetContentType(MIME_XML_UTF8); |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
113 output_.SetContentCompression(ContentCompression_NotCompressed); |
1521 | 114 output_.Answer(s); |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
115 #else |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2908
diff
changeset
|
116 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2908
diff
changeset
|
117 "Orthanc was compiled without XML support"); |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
118 #endif |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
119 } |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
120 else |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
121 { |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
122 std::string s; |
4394
f7104e9d044c
functions to read/write JSON in OrthancPluginCppWrapper.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4392
diff
changeset
|
123 Toolbox::WriteStyledJson(s, value); |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
124 output_.SetContentType(MIME_JSON_UTF8); |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
125 output_.SetContentCompression(ContentCompression_NotCompressed); |
2908
9d277f8ad698
new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2905
diff
changeset
|
126 output_.Answer(s); |
1046
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
127 } |
00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1042
diff
changeset
|
128 |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
129 alreadySent_ = true; |
209 | 130 } |
131 | |
132 void RestApiOutput::AnswerBuffer(const std::string& buffer, | |
2908
9d277f8ad698
new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2905
diff
changeset
|
133 MimeType contentType) |
209 | 134 { |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
135 AnswerBuffer(buffer, contentType, SystemToolbox::GuessContentCompression(contentType)); |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
136 } |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
137 |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
138 |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
139 void RestApiOutput::AnswerBuffer(const std::string& buffer, |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
140 MimeType contentType, |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
141 ContentCompression contentCompression) |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
142 { |
1514
d73a2178b319
support of deflate and gzip content-types
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1511
diff
changeset
|
143 AnswerBuffer(buffer.size() == 0 ? NULL : buffer.c_str(), |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
144 buffer.size(), contentType, contentCompression); |
209 | 145 } |
146 | |
339 | 147 void RestApiOutput::AnswerBuffer(const void* buffer, |
148 size_t length, | |
2908
9d277f8ad698
new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2905
diff
changeset
|
149 MimeType contentType) |
339 | 150 { |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
151 AnswerBuffer(buffer, length, contentType, SystemToolbox::GuessContentCompression(contentType)); |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
152 } |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
153 |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
154 void RestApiOutput::AnswerBuffer(const void* buffer, |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
155 size_t length, |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
156 MimeType contentType, |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
157 ContentCompression contentCompression) |
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
158 { |
339 | 159 CheckStatus(); |
3314
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
160 |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
161 if (convertJsonToXml_ && |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
162 contentType == MimeType_Json) |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
163 { |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
164 Json::Value json; |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
165 if (Toolbox::ReadJson(json, buffer, length)) |
3314
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
166 { |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
167 AnswerJson(json); |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
168 } |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
169 else |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
170 { |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
171 throw OrthancException(ErrorCode_BadFileFormat, |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
172 "The REST API tries and answers with an invalid JSON file"); |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
173 } |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
174 } |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
175 else |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
176 { |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
177 output_.SetContentType(contentType); |
5406
aaf7c49a9ddc
tentative to implement smart HTTP compression with detection of transfer syntax
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
178 output_.SetContentCompression(contentCompression); |
3314
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
179 output_.Answer(buffer, length); |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
180 alreadySent_ = true; |
7eb5405b7ed8
Enlarge the support of JSON-to-XML conversion in the REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
181 } |
339 | 182 } |
183 | |
215
c07170f3f4f7
refactoring of access to images in REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
211
diff
changeset
|
184 void RestApiOutput::Redirect(const std::string& path) |
209 | 185 { |
211 | 186 CheckStatus(); |
209 | 187 output_.Redirect(path); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
188 alreadySent_ = true; |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
189 } |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
190 |
1567
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
191 void RestApiOutput::SignalErrorInternal(HttpStatus status, |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
192 const char* message, |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
193 size_t messageSize) |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
194 { |
1194 | 195 if (status != HttpStatus_400_BadRequest && |
196 status != HttpStatus_403_Forbidden && | |
1165
0561f2087cc9
Fix reporting of errors in Orthanc Explorer when sending images to peers/modalities
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1113
diff
changeset
|
197 status != HttpStatus_500_InternalServerError && |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
198 status != HttpStatus_415_UnsupportedMediaType) |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
199 { |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1567
diff
changeset
|
200 throw OrthancException(ErrorCode_BadHttpStatusInRest); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
201 } |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
202 |
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
203 CheckStatus(); |
1567
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
204 output_.SendStatus(status, message, messageSize); |
216
e5d5d4a9a326
refactored upload of dicom through http
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
205 alreadySent_ = true; |
209 | 206 } |
330 | 207 |
1567
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
208 void RestApiOutput::SignalError(HttpStatus status) |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
209 { |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
210 SignalErrorInternal(status, NULL, 0); |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
211 } |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
212 |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
213 void RestApiOutput::SignalError(HttpStatus status, |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
214 const std::string& message) |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
215 { |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
216 SignalErrorInternal(status, message.c_str(), message.size()); |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
217 } |
9c5d93510414
If error while calling the REST API, the answer body contains an error description
jodogne
parents:
1521
diff
changeset
|
218 |
330 | 219 void RestApiOutput::SetCookie(const std::string& name, |
220 const std::string& value, | |
221 unsigned int maxAge) | |
222 { | |
223 if (name.find(";") != std::string::npos || | |
224 name.find(" ") != std::string::npos || | |
225 value.find(";") != std::string::npos || | |
226 value.find(" ") != std::string::npos) | |
227 { | |
228 throw OrthancException(ErrorCode_NotImplemented); | |
229 } | |
230 | |
231 CheckStatus(); | |
232 | |
233 std::string v = value + ";path=/"; | |
234 | |
235 if (maxAge != 0) | |
236 { | |
237 v += ";max-age=" + boost::lexical_cast<std::string>(maxAge); | |
238 } | |
239 | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
240 output_.SetCookie(name, v); |
330 | 241 } |
242 | |
243 void RestApiOutput::ResetCookie(const std::string& name) | |
244 { | |
245 // This marks the cookie to be deleted by the browser in 1 second, | |
246 // and before it actually gets deleted, its value is set to the | |
247 // empty string | |
248 SetCookie(name, "", 1); | |
249 } | |
4805
0a38000b086d
Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents:
4672
diff
changeset
|
250 |
0a38000b086d
Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents:
4672
diff
changeset
|
251 void RestApiOutput::SetContentFilename(const char* filename) |
0a38000b086d
Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents:
4672
diff
changeset
|
252 { |
0a38000b086d
Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents:
4672
diff
changeset
|
253 output_.SetContentFilename(filename); |
0a38000b086d
Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents:
4672
diff
changeset
|
254 } |
209 | 255 } |