Mercurial > hg > orthanc-stone
annotate Framework/Oracle/WebAssemblyOracle.cpp @ 1099:66e21ef2d657 broker
move comment elsewhere
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Oct 2019 14:58:06 +0200 |
parents | 008dbc4ceb62 |
children | b9f2a111c5b9 |
rev | line source |
---|---|
825 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "WebAssemblyOracle.h" | |
23 | |
24 #include "SleepOracleCommand.h" | |
995
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
25 #include "OracleCommandExceptionMessage.h" |
825 | 26 |
27 #include <Core/OrthancException.h> | |
28 #include <Core/Toolbox.h> | |
29 | |
30 #include <emscripten.h> | |
31 #include <emscripten/html5.h> | |
32 #include <emscripten/fetch.h> | |
33 | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
34 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
35 extern bool logbgo233; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
36 extern bool logbgo115; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
37 #endif |
825 | 38 |
39 namespace OrthancStone | |
40 { | |
41 class WebAssemblyOracle::TimeoutContext | |
42 { | |
43 private: | |
44 WebAssemblyOracle& oracle_; | |
45 const IObserver& receiver_; | |
46 std::auto_ptr<SleepOracleCommand> command_; | |
47 | |
48 public: | |
49 TimeoutContext(WebAssemblyOracle& oracle, | |
50 const IObserver& receiver, | |
51 IOracleCommand* command) : | |
52 oracle_(oracle), | |
53 receiver_(receiver) | |
54 { | |
55 if (command == NULL) | |
56 { | |
57 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
58 } | |
59 else | |
60 { | |
61 command_.reset(dynamic_cast<SleepOracleCommand*>(command)); | |
62 } | |
63 } | |
64 | |
65 void EmitMessage() | |
66 { | |
67 SleepOracleCommand::TimeoutMessage message(*command_); | |
68 oracle_.EmitMessage(receiver_, message); | |
69 } | |
70 | |
71 static void Callback(void *userData) | |
72 { | |
73 std::auto_ptr<TimeoutContext> context(reinterpret_cast<TimeoutContext*>(userData)); | |
74 context->EmitMessage(); | |
75 } | |
76 }; | |
77 | |
78 | |
79 class WebAssemblyOracle::Emitter : public IMessageEmitter | |
80 { | |
81 private: | |
82 WebAssemblyOracle& oracle_; | |
83 | |
84 public: | |
85 Emitter(WebAssemblyOracle& oracle) : | |
86 oracle_(oracle) | |
87 { | |
88 } | |
89 | |
90 virtual void EmitMessage(const IObserver& receiver, | |
91 const IMessage& message) | |
92 { | |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
93 LOG(TRACE) << "WebAssemblyOracle::Emitter::EmitMessage receiver = " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
94 << std::hex << &receiver << std::dec; |
825 | 95 oracle_.EmitMessage(receiver, message); |
96 } | |
97 }; | |
98 | |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
99 /** |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
100 This object is created on the heap for every http request. |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
101 It is deleted in the success (or error) callbacks. |
825 | 102 |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
103 This object references the receiver of the request. Since this is a raw |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
104 reference, we need additional checks to make sure we send the response to |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
105 the same object, for the object can be deleted and a new one recreated at the |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
106 same address (it often happens in the [single-threaded] browser context). |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
107 */ |
825 | 108 class WebAssemblyOracle::FetchContext : public boost::noncopyable |
109 { | |
110 private: | |
111 Emitter emitter_; | |
112 const IObserver& receiver_; | |
113 std::auto_ptr<IOracleCommand> command_; | |
114 std::string expectedContentType_; | |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
115 std::string receiverFingerprint_; |
825 | 116 |
117 public: | |
118 FetchContext(WebAssemblyOracle& oracle, | |
119 const IObserver& receiver, | |
120 IOracleCommand* command, | |
121 const std::string& expectedContentType) : | |
122 emitter_(oracle), | |
123 receiver_(receiver), | |
124 command_(command), | |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
125 expectedContentType_(expectedContentType), |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
126 receiverFingerprint_(receiver.GetFingerprint()) |
825 | 127 { |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
128 LOG(TRACE) << "WebAssemblyOracle::FetchContext::FetchContext() | " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
129 << "receiver address = " << std::hex << &receiver << std::dec |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
130 << " with fingerprint = " << receiverFingerprint_; |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
131 |
825 | 132 if (command == NULL) |
133 { | |
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
135 } | |
136 } | |
137 | |
138 const std::string& GetExpectedContentType() const | |
139 { | |
140 return expectedContentType_; | |
141 } | |
142 | |
143 void EmitMessage(const IMessage& message) | |
144 { | |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
145 LOG(TRACE) << "WebAssemblyOracle::FetchContext::EmitMessage receiver_ = " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
146 << std::hex << &receiver_ << std::dec; |
825 | 147 emitter_.EmitMessage(receiver_, message); |
148 } | |
149 | |
150 IMessageEmitter& GetEmitter() | |
151 { | |
152 return emitter_; | |
153 } | |
154 | |
155 const IObserver& GetReceiver() const | |
156 { | |
157 return receiver_; | |
158 } | |
159 | |
160 IOracleCommand& GetCommand() const | |
161 { | |
162 return *command_; | |
163 } | |
164 | |
165 template <typename T> | |
166 const T& GetTypedCommand() const | |
167 { | |
168 return dynamic_cast<T&>(*command_); | |
169 } | |
170 | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
171 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
172 static std::string ToString(Orthanc::HttpMethod method) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
173 { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
174 switch (method) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
175 case Orthanc::HttpMethod_Get: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
176 return "GET"; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
177 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
178 case Orthanc::HttpMethod_Post: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
179 return "POST"; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
180 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
181 default: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
182 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
183 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
184 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
185 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
186 static void DumpCommand(emscripten_fetch_t* fetch, std::string answer) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
187 { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
188 FetchContext* context = reinterpret_cast<FetchContext*>(fetch->userData); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
189 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
190 const auto& command = context->GetTypedCommand<OrthancRestApiCommand>(); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
191 auto commandStr = ToString(command.GetMethod()); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
192 LOG(TRACE) << "SuccessCallback for REST command. Method is : " << commandStr; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
193 switch (command.GetMethod()) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
194 case Orthanc::HttpMethod_Get: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
195 LOG(TRACE) << " * SuccessCallback GET URI = " << command.GetUri() << " timeout = " << command.GetTimeout(); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
196 LOG(TRACE) << " * SuccessCallback GET RESPONSE = " << answer; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
197 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
198 case Orthanc::HttpMethod_Post: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
199 LOG(TRACE) << " * SuccessCallback POST URI = " << command.GetUri() << " body = " << command.GetBody() << " timeout = " << command.GetTimeout(); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
200 LOG(TRACE) << " * SuccessCallback POST RESPONSE = " << answer; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
201 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
202 default: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
203 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
204 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
205 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
206 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
207 #endif |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
208 |
825 | 209 static void SuccessCallback(emscripten_fetch_t *fetch) |
210 { | |
211 /** | |
212 * Firstly, make a local copy of the fetched information, and | |
213 * free data associated with the fetch. | |
214 **/ | |
215 | |
216 std::auto_ptr<FetchContext> context(reinterpret_cast<FetchContext*>(fetch->userData)); | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
217 |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
218 // an UUID is 36 chars : 32 hex chars + 4 hyphens: char #0 --> char #35 |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
219 // char #36 is \0. |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
220 bool callHandler = true; |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
221 |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
222 // TODO: remove this line because we are NOT allowed to call methods on GetReceiver that is maybe a dangling ref |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
223 if (context->GetReceiver().DoesFingerprintLookGood()) |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
224 { |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
225 callHandler = true; |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
226 std::string currentFingerprint(context->GetReceiver().GetFingerprint()); |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
227 |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
228 LOG(TRACE) << "SuccessCallback for object at address (" << std::hex |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
229 << &(context->GetReceiver()) << std::dec |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
230 << " with current fingerprint = " << currentFingerprint |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
231 << ". Fingerprint looks OK"; |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
232 |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
233 if (currentFingerprint != context->receiverFingerprint_) |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
234 { |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
235 LOG(TRACE) << " ** SuccessCallback: BUT currentFingerprint != " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
236 << "receiverFingerprint_(" << context->receiverFingerprint_ << ")"; |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
237 callHandler = false; |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
238 } |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
239 else |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
240 { |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
241 LOG(TRACE) << " ** SuccessCallback: FetchContext-level " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
242 << "fingerprints are the same: " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
243 << context->receiverFingerprint_ |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
244 << " ---> oracle will dispatch the response to observer: " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
245 << std::hex << &(context->GetReceiver()) << std::dec; |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
246 } |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
247 } |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
248 else { |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
249 LOG(TRACE) << "SuccessCallback for object at address (" << std::hex << &(context->GetReceiver()) << std::dec << " with current fingerprint is XXXXX -- NOT A VALID FINGERPRINT! OBJECT IS READ ! CALLBACK WILL NOT BE CALLED!"; |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
250 callHandler = false; |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
251 } |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
252 |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
253 if (fetch->userData == NULL) |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
254 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
255 LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback fetch->userData is NULL!!!!!!!"; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
256 } |
825 | 257 |
258 std::string answer; | |
259 if (fetch->numBytes > 0) | |
260 { | |
261 answer.assign(fetch->data, fetch->numBytes); | |
262 } | |
263 | |
264 /** | |
265 * TODO - HACK - As of emscripten-1.38.31, the fetch API does | |
266 * not contain a way to retrieve the HTTP headers of the | |
267 * answer. We make the assumption that the "Content-Type" header | |
268 * of the response is the same as the "Accept" header of the | |
269 * query. This should be fixed in future versions of emscripten. | |
270 * https://github.com/emscripten-core/emscripten/pull/8486 | |
271 **/ | |
272 | |
273 HttpHeaders headers; | |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
274 if (fetch->userData != NULL) |
825 | 275 { |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
276 if (!context->GetExpectedContentType().empty()) |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
277 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
278 headers["Content-Type"] = context->GetExpectedContentType(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
279 } |
825 | 280 } |
281 | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
282 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
283 if (context->GetCommand().GetType() == IOracleCommand::Type_OrthancRestApi) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
284 //if (logbgo115) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
285 DumpCommand(fetch, answer); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
286 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
287 #endif |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
288 LOG(TRACE) << "About to call emscripten_fetch_close"; |
825 | 289 emscripten_fetch_close(fetch); |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
290 LOG(TRACE) << "Successfully called emscripten_fetch_close"; |
825 | 291 |
292 /** | |
293 * Secondly, use the retrieved data. | |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
294 * IMPORTANT NOTE: the receiver might be dead. This is prevented |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
295 * by the object responsible for zombie check, later on. |
825 | 296 **/ |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
297 try |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
298 { |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
299 if (context.get() == NULL) |
825 | 300 { |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
301 LOG(ERROR) << "WebAssemblyOracle::FetchContext::SuccessCallback: (context.get() == NULL)"; |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
302 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
303 } |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
304 else |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
305 { |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
306 if (callHandler) |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
307 { |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
308 switch (context->GetCommand().GetType()) |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
309 { |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
310 case IOracleCommand::Type_Http: |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
311 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
312 HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
313 context->EmitMessage(message); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
314 break; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
315 } |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
316 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
317 case IOracleCommand::Type_OrthancRestApi: |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
318 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
319 LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);"; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
320 OrthancRestApiCommand::SuccessMessage message |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
321 (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
322 context->EmitMessage(message); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
323 break; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
324 } |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
325 |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
326 case IOracleCommand::Type_GetOrthancImage: |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
327 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
328 context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
329 (context->GetEmitter(), context->GetReceiver(), answer, headers); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
330 break; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
331 } |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
332 |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
333 case IOracleCommand::Type_GetOrthancWebViewerJpeg: |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
334 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
335 context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
336 (context->GetEmitter(), context->GetReceiver(), answer); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
337 break; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
338 } |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
339 |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
340 default: |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
341 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
342 << context->GetCommand().GetType(); |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
971
diff
changeset
|
343 } |
825 | 344 } |
345 } | |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
346 } |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
347 catch (Orthanc::OrthancException& e) |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
348 { |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
349 LOG(ERROR) << "Error while processing a fetch answer in the oracle: " << e.What(); |
825 | 350 } |
351 } | |
352 | |
353 static void FailureCallback(emscripten_fetch_t *fetch) | |
354 { | |
355 std::auto_ptr<FetchContext> context(reinterpret_cast<FetchContext*>(fetch->userData)); | |
995
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
356 |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
357 { |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
358 const size_t kEmscriptenStatusTextSize = sizeof(emscripten_fetch_t::statusText); |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
359 char message[kEmscriptenStatusTextSize + 1]; |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
360 memcpy(message, fetch->statusText, kEmscriptenStatusTextSize); |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
361 message[kEmscriptenStatusTextSize] = 0; |
959
13e078adfb94
Better error log in fetch failure callback +
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
362 |
996
727f2007ca23
commenting out annoying error
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
363 /*LOG(ERROR) << "Fetching " << fetch->url |
995
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
364 << " failed, HTTP failure status code: " << fetch->status |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
365 << " | statusText = " << message |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
366 << " | numBytes = " << fetch->numBytes |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
367 << " | totalBytes = " << fetch->totalBytes |
996
727f2007ca23
commenting out annoying error
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
368 << " | readyState = " << fetch->readyState;*/ |
995
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
369 } |
825 | 370 |
995
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
371 { |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
372 OracleCommandExceptionMessage message |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
373 (context->GetCommand(), Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol)); |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
374 context->EmitMessage(message); |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
375 } |
9893fa8cd7a6
send error message for HTTP fetching in wasm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
992
diff
changeset
|
376 |
825 | 377 /** |
378 * TODO - The following code leads to an infinite recursion, at | |
379 * least with Firefox running on incognito mode => WHY? | |
380 **/ | |
959
13e078adfb94
Better error log in fetch failure callback +
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
381 emscripten_fetch_close(fetch); // Also free data on failure. |
825 | 382 } |
383 }; | |
384 | |
385 | |
386 | |
387 class WebAssemblyOracle::FetchCommand : public boost::noncopyable | |
388 { | |
389 private: | |
390 WebAssemblyOracle& oracle_; | |
391 const IObserver& receiver_; | |
392 std::auto_ptr<IOracleCommand> command_; | |
393 Orthanc::HttpMethod method_; | |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
394 std::string url_; |
825 | 395 std::string body_; |
396 HttpHeaders headers_; | |
397 unsigned int timeout_; | |
398 std::string expectedContentType_; | |
399 | |
400 public: | |
401 FetchCommand(WebAssemblyOracle& oracle, | |
402 const IObserver& receiver, | |
403 IOracleCommand* command) : | |
404 oracle_(oracle), | |
405 receiver_(receiver), | |
406 command_(command), | |
407 method_(Orthanc::HttpMethod_Get), | |
408 timeout_(0) | |
409 { | |
410 if (command == NULL) | |
411 { | |
412 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
413 } | |
414 } | |
415 | |
416 void SetMethod(Orthanc::HttpMethod method) | |
417 { | |
418 method_ = method; | |
419 } | |
420 | |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
421 void SetOrthancUri(const std::string& uri) |
825 | 422 { |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
423 url_ = oracle_.orthancRoot_ + uri; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
424 } |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
425 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
426 void SetUrl(const std::string& url) |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
427 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
428 url_ = url; |
825 | 429 } |
430 | |
431 void SetBody(std::string& body /* will be swapped */) | |
432 { | |
433 body_.swap(body); | |
434 } | |
435 | |
436 void SetHttpHeaders(const HttpHeaders& headers) | |
437 { | |
438 headers_ = headers; | |
439 } | |
440 | |
441 void SetTimeout(unsigned int timeout) | |
442 { | |
443 timeout_ = timeout; | |
444 } | |
445 | |
446 void Execute() | |
447 { | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
448 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
449 if (logbgo233) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
450 if (logbgo115) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
451 LOG(TRACE) << " WebAssemblyOracle::Execute () command addr " << |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
452 std::hex << command_.get() << std::dec; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
453 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
454 #endif |
825 | 455 if (command_.get() == NULL) |
456 { | |
457 // Cannot call Execute() twice | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
933
diff
changeset
|
458 LOG(ERROR) << "WebAssemblyOracle::Execute(): (command_.get() == NULL)"; |
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
933
diff
changeset
|
459 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
825 | 460 } |
461 | |
462 emscripten_fetch_attr_t attr; | |
463 emscripten_fetch_attr_init(&attr); | |
464 | |
465 const char* method; | |
466 | |
467 switch (method_) | |
468 { | |
469 case Orthanc::HttpMethod_Get: | |
470 method = "GET"; | |
471 break; | |
472 | |
473 case Orthanc::HttpMethod_Post: | |
474 method = "POST"; | |
475 break; | |
476 | |
477 case Orthanc::HttpMethod_Delete: | |
478 method = "DELETE"; | |
479 break; | |
480 | |
481 case Orthanc::HttpMethod_Put: | |
482 method = "PUT"; | |
483 break; | |
484 | |
485 default: | |
486 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
487 } | |
488 | |
489 strcpy(attr.requestMethod, method); | |
490 | |
971
bc7b249dfbd0
Added EMSCRIPTEN_FETCH_REPLACE flag to requests to prevent stale cache results
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
491 attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE; |
825 | 492 attr.onsuccess = FetchContext::SuccessCallback; |
493 attr.onerror = FetchContext::FailureCallback; | |
494 attr.timeoutMSecs = timeout_ * 1000; | |
495 | |
496 std::vector<const char*> headers; | |
497 headers.reserve(2 * headers_.size() + 1); | |
498 | |
499 std::string expectedContentType; | |
500 | |
501 for (HttpHeaders::const_iterator it = headers_.begin(); it != headers_.end(); ++it) | |
502 { | |
503 std::string key; | |
504 Orthanc::Toolbox::ToLowerCase(key, it->first); | |
505 | |
506 if (key == "accept") | |
507 { | |
508 expectedContentType = it->second; | |
509 } | |
510 | |
511 if (key != "accept-encoding") // Web browsers forbid the modification of this HTTP header | |
512 { | |
513 headers.push_back(it->first.c_str()); | |
514 headers.push_back(it->second.c_str()); | |
515 } | |
516 } | |
517 | |
518 headers.push_back(NULL); // Termination of the array of HTTP headers | |
519 | |
520 attr.requestHeaders = &headers[0]; | |
521 | |
841
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
522 char* requestData = NULL; |
825 | 523 if (!body_.empty()) |
841
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
524 requestData = reinterpret_cast<char*>(malloc(body_.size())); |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
525 |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
526 try |
825 | 527 { |
841
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
528 if (!body_.empty()) |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
529 { |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
530 memcpy(requestData, &(body_[0]), body_.size()); |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
531 attr.requestDataSize = body_.size(); |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
532 attr.requestData = requestData; |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
533 } |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
534 attr.userData = new FetchContext(oracle_, receiver_, command_.release(), expectedContentType); |
825 | 535 |
841
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
536 // Must be the last call to prevent memory leak on error |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
537 #if 0 |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
538 LOG(TRACE) << "Performing " << method << " request on URI: \"" << url_ << "\""; |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
539 #endif |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
540 emscripten_fetch(&attr, url_.c_str()); |
841
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
541 } |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
542 catch(...) |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
543 { |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
544 if(requestData != NULL) |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
545 free(requestData); |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
546 throw; |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
547 } |
266e2b0b9abc
better error reporting in DicomStructureSetLoader + fixed POST request logic
Benjamin Golinvaux <bgo@osimis.io>
parents:
831
diff
changeset
|
548 } |
825 | 549 }; |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
550 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
551 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
552 static void DumpCommand(OrthancRestApiCommand* pCommand) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
553 { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
554 OrthancRestApiCommand& command = *pCommand; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
555 LOG(TRACE) << "WebAssemblyOracle::Execute for REST command."; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
556 switch (command.GetMethod()) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
557 case Orthanc::HttpMethod_Get: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
558 LOG(TRACE) << " * WebAssemblyOracle::Execute GET URI = " << command.GetUri() << " timeout = " << command.GetTimeout(); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
559 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
560 case Orthanc::HttpMethod_Post: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
561 LOG(TRACE) << " * WebAssemblyOracle::Execute POST URI = " << command.GetUri() << " body = " << command.GetBody() << " timeout = " << command.GetTimeout(); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
562 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
563 default: |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
564 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
565 break; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
566 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
567 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
568 #endif |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
569 |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
570 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
571 void WebAssemblyOracle::Execute(const IObserver& receiver, |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
572 HttpCommand* command) |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
573 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
574 FetchCommand fetch(*this, receiver, command); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
575 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
576 fetch.SetMethod(command->GetMethod()); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
577 fetch.SetUrl(command->GetUrl()); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
578 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
579 fetch.SetTimeout(command->GetTimeout()); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
580 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
581 if (command->GetMethod() == Orthanc::HttpMethod_Post || |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
582 command->GetMethod() == Orthanc::HttpMethod_Put) |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
583 { |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
584 std::string body; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
585 command->SwapBody(body); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
586 fetch.SetBody(body); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
587 } |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
588 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
589 fetch.Execute(); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
590 } |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
591 |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
592 |
825 | 593 void WebAssemblyOracle::Execute(const IObserver& receiver, |
594 OrthancRestApiCommand* command) | |
595 { | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
596 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
597 DumpCommand(command); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
598 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
599 if (logbgo233) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
600 if (logbgo115) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
601 LOG(TRACE) << " WebAssemblyOracle::Execute (OrthancRestApiCommand) command addr " << |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
602 std::hex << command << std::dec; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
603 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
604 #endif |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
605 |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
606 try |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
607 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
608 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute."; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
609 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
610 FetchCommand fetch(*this, receiver, command); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
611 |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
612 fetch.SetMethod(command->GetMethod()); |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
613 fetch.SetOrthancUri(command->GetUri()); |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
614 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
615 fetch.SetTimeout(command->GetTimeout()); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
616 |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
617 if (command->GetMethod() == Orthanc::HttpMethod_Post || |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
618 command->GetMethod() == Orthanc::HttpMethod_Put) |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
619 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
620 std::string body; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
621 command->SwapBody(body); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
622 fetch.SetBody(body); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
623 } |
825 | 624 |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
625 fetch.Execute(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
626 //LOG(TRACE) << "*********** successful end of WebAssemblyOracle::Execute."; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
627 } |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
628 catch (const Orthanc::OrthancException& e) |
825 | 629 { |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
630 if (e.HasDetails()) |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
631 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
632 LOG(ERROR) << "OrthancException in WebAssemblyOracle::Execute: " << e.What() << " Details: " << e.GetDetails(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
633 } |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
634 else |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
635 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
636 LOG(ERROR) << "OrthancException in WebAssemblyOracle::Execute: " << e.What(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
637 } |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
638 //LOG(TRACE) << "*********** failing end of WebAssemblyOracle::Execute."; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
639 throw; |
825 | 640 } |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
641 catch (const std::exception& e) |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
642 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
643 LOG(ERROR) << "std::exception in WebAssemblyOracle::Execute: " << e.what(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
644 // LOG(TRACE) << "*********** failing end of WebAssemblyOracle::Execute."; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
645 throw; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
646 } |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
647 catch (...) |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
648 { |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
649 LOG(ERROR) << "Unknown exception in WebAssemblyOracle::Execute"; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
650 // LOG(TRACE) << "*********** failing end of WebAssemblyOracle::Execute."; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
651 throw; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
652 } |
825 | 653 } |
654 | |
655 | |
656 void WebAssemblyOracle::Execute(const IObserver& receiver, | |
657 GetOrthancImageCommand* command) | |
658 { | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
659 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
660 if (logbgo233) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
661 if (logbgo115) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
662 LOG(TRACE) << " WebAssemblyOracle::Execute (GetOrthancImageCommand) command addr " << |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
663 std::hex << command << std::dec; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
664 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
665 #endif |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
666 |
825 | 667 FetchCommand fetch(*this, receiver, command); |
668 | |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
669 fetch.SetOrthancUri(command->GetUri()); |
825 | 670 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
671 fetch.SetTimeout(command->GetTimeout()); | |
672 | |
673 fetch.Execute(); | |
674 } | |
675 | |
676 | |
677 void WebAssemblyOracle::Execute(const IObserver& receiver, | |
678 GetOrthancWebViewerJpegCommand* command) | |
679 { | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
680 #if 0 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
681 if (logbgo233) { |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
682 if (logbgo115) |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
683 LOG(TRACE) << " WebAssemblyOracle::Execute (GetOrthancWebViewerJpegCommand) command addr " << std::hex << command << std::dec; |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
684 } |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
685 #endif |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
959
diff
changeset
|
686 |
825 | 687 FetchCommand fetch(*this, receiver, command); |
688 | |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
689 fetch.SetOrthancUri(command->GetUri()); |
825 | 690 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
691 fetch.SetTimeout(command->GetTimeout()); | |
692 | |
693 fetch.Execute(); | |
694 } | |
695 | |
696 | |
697 | |
1076
008dbc4ceb62
removed LambdaCallable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
698 void WebAssemblyOracle::Schedule(boost::shared_ptr<IObserver>& receiver, |
825 | 699 IOracleCommand* command) |
700 { | |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
701 LOG(TRACE) << "WebAssemblyOracle::Schedule : receiver = " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
702 << std::hex << &receiver << std::dec |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
703 << " | Current fingerprint is " << receiver.GetFingerprint(); |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
975
diff
changeset
|
704 |
825 | 705 std::auto_ptr<IOracleCommand> protection(command); |
706 | |
707 if (command == NULL) | |
708 { | |
709 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
710 } | |
711 | |
712 switch (command->GetType()) | |
713 { | |
992
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
714 case IOracleCommand::Type_Http: |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
715 Execute(receiver, dynamic_cast<HttpCommand*>(protection.release())); |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
716 break; |
a9f5d0742e22
implementation of HttpCommand in WebAssemblyOracle
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
717 |
825 | 718 case IOracleCommand::Type_OrthancRestApi: |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
719 //// DIAGNOSTIC. PLEASE REMOVE IF IT HAS BEEN COMMITTED BY MISTAKE |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
720 //{ |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
721 // const IObserver* pReceiver = &receiver; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
722 // LOG(TRACE) << "WebAssemblyOracle::Schedule | pReceiver is " << pReceiver; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
723 // LOG(TRACE) << "WebAssemblyOracle::Schedule | command = " << command; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
724 // OrthancRestApiCommand* rac = dynamic_cast<OrthancRestApiCommand*>(protection.get()); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
725 // LOG(TRACE) << "WebAssemblyOracle::Schedule | typed command = " << rac; |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
726 // LOG(TRACE) << "WebAssemblyOracle::Schedule" << rac->GetUri(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
727 //} |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
728 //// END OF BLOCK TO REMOVE |
825 | 729 Execute(receiver, dynamic_cast<OrthancRestApiCommand*>(protection.release())); |
730 break; | |
731 | |
732 case IOracleCommand::Type_GetOrthancImage: | |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
733 //// DIAGNOSTIC. PLEASE REMOVE IF IT HAS BEEN COMMITTED BY MISTAKE |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
734 //{ |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
735 // GetOrthancImageCommand* rac = dynamic_cast<GetOrthancImageCommand*>(protection.get()); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
736 // LOG(TRACE) << "WebAssemblyOracle::Schedule" << rac->GetUri(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
737 //} |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
738 //// END OF BLOCK TO REMOVE |
825 | 739 Execute(receiver, dynamic_cast<GetOrthancImageCommand*>(protection.release())); |
740 break; | |
741 | |
742 case IOracleCommand::Type_GetOrthancWebViewerJpeg: | |
933
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
743 //// DIAGNOSTIC. PLEASE REMOVE IF IT HAS BEEN COMMITTED BY MISTAKE |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
744 //{ |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
745 // GetOrthancWebViewerJpegCommand* rac = dynamic_cast<GetOrthancWebViewerJpegCommand*>(protection.get()); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
746 // LOG(TRACE) << "WebAssemblyOracle::Schedule" << rac->GetUri(); |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
747 //} |
f75f6cb69c1b
Commented-out traces for debug
Benjamin Golinvaux <bgo@osimis.io>
parents:
843
diff
changeset
|
748 //// END OF BLOCK TO REMOVE |
825 | 749 Execute(receiver, dynamic_cast<GetOrthancWebViewerJpegCommand*>(protection.release())); |
750 break; | |
751 | |
752 case IOracleCommand::Type_Sleep: | |
753 { | |
754 unsigned int timeoutMS = dynamic_cast<SleepOracleCommand*>(command)->GetDelay(); | |
755 emscripten_set_timeout(TimeoutContext::Callback, timeoutMS, | |
756 new TimeoutContext(*this, receiver, protection.release())); | |
757 break; | |
758 } | |
759 | |
760 default: | |
761 LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: " << command->GetType(); | |
762 } | |
763 } | |
764 } |