Mercurial > hg > orthanc-stone
comparison Framework/Oracle/WebAssemblyOracle.cpp @ 964:91f827272c1f toa2019082701
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 27 Aug 2019 14:28:19 +0200 |
parents | 13e078adfb94 |
children | bc7b249dfbd0 |
comparison
equal
deleted
inserted
replaced
962:e70d75f199c5 | 964:91f827272c1f |
---|---|
28 | 28 |
29 #include <emscripten.h> | 29 #include <emscripten.h> |
30 #include <emscripten/html5.h> | 30 #include <emscripten/html5.h> |
31 #include <emscripten/fetch.h> | 31 #include <emscripten/fetch.h> |
32 | 32 |
33 #if 0 | |
34 extern bool logbgo233; | |
35 extern bool logbgo115; | |
36 #endif | |
33 | 37 |
34 namespace OrthancStone | 38 namespace OrthancStone |
35 { | 39 { |
36 class WebAssemblyOracle::TimeoutContext | 40 class WebAssemblyOracle::TimeoutContext |
37 { | 41 { |
143 const T& GetTypedCommand() const | 147 const T& GetTypedCommand() const |
144 { | 148 { |
145 return dynamic_cast<T&>(*command_); | 149 return dynamic_cast<T&>(*command_); |
146 } | 150 } |
147 | 151 |
152 #if 0 | |
153 static std::string ToString(Orthanc::HttpMethod method) | |
154 { | |
155 switch (method) { | |
156 case Orthanc::HttpMethod_Get: | |
157 return "GET"; | |
158 break; | |
159 case Orthanc::HttpMethod_Post: | |
160 return "POST"; | |
161 break; | |
162 default: | |
163 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
164 break; | |
165 } | |
166 } | |
167 static void DumpCommand(emscripten_fetch_t* fetch, std::string answer) | |
168 { | |
169 FetchContext* context = reinterpret_cast<FetchContext*>(fetch->userData); | |
170 | |
171 const auto& command = context->GetTypedCommand<OrthancRestApiCommand>(); | |
172 auto commandStr = ToString(command.GetMethod()); | |
173 LOG(TRACE) << "SuccessCallback for REST command. Method is : " << commandStr; | |
174 switch (command.GetMethod()) { | |
175 case Orthanc::HttpMethod_Get: | |
176 LOG(TRACE) << " * SuccessCallback GET URI = " << command.GetUri() << " timeout = " << command.GetTimeout(); | |
177 LOG(TRACE) << " * SuccessCallback GET RESPONSE = " << answer; | |
178 break; | |
179 case Orthanc::HttpMethod_Post: | |
180 LOG(TRACE) << " * SuccessCallback POST URI = " << command.GetUri() << " body = " << command.GetBody() << " timeout = " << command.GetTimeout(); | |
181 LOG(TRACE) << " * SuccessCallback POST RESPONSE = " << answer; | |
182 break; | |
183 default: | |
184 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
185 break; | |
186 } | |
187 } | |
188 #endif | |
189 | |
148 static void SuccessCallback(emscripten_fetch_t *fetch) | 190 static void SuccessCallback(emscripten_fetch_t *fetch) |
149 { | 191 { |
150 /** | 192 /** |
151 * Firstly, make a local copy of the fetched information, and | 193 * Firstly, make a local copy of the fetched information, and |
152 * free data associated with the fetch. | 194 * free data associated with the fetch. |
180 { | 222 { |
181 headers["Content-Type"] = context->GetExpectedContentType(); | 223 headers["Content-Type"] = context->GetExpectedContentType(); |
182 } | 224 } |
183 } | 225 } |
184 | 226 |
227 #if 0 | |
228 if (context->GetCommand().GetType() == IOracleCommand::Type_OrthancRestApi) { | |
229 //if (logbgo115) | |
230 DumpCommand(fetch, answer); | |
231 } | |
232 #endif | |
185 emscripten_fetch_close(fetch); | 233 emscripten_fetch_close(fetch); |
186 | |
187 | 234 |
188 /** | 235 /** |
189 * Secondly, use the retrieved data. | 236 * Secondly, use the retrieved data. |
190 **/ | 237 **/ |
191 | 238 |
313 timeout_ = timeout; | 360 timeout_ = timeout; |
314 } | 361 } |
315 | 362 |
316 void Execute() | 363 void Execute() |
317 { | 364 { |
365 #if 0 | |
366 if (logbgo233) { | |
367 if (logbgo115) | |
368 LOG(TRACE) << " WebAssemblyOracle::Execute () command addr " << | |
369 std::hex << command_.get() << std::dec; | |
370 } | |
371 #endif | |
318 if (command_.get() == NULL) | 372 if (command_.get() == NULL) |
319 { | 373 { |
320 // Cannot call Execute() twice | 374 // Cannot call Execute() twice |
321 LOG(ERROR) << "WebAssemblyOracle::Execute(): (command_.get() == NULL)"; | 375 LOG(ERROR) << "WebAssemblyOracle::Execute(): (command_.get() == NULL)"; |
322 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | 376 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
395 attr.requestData = requestData; | 449 attr.requestData = requestData; |
396 } | 450 } |
397 attr.userData = new FetchContext(oracle_, receiver_, command_.release(), expectedContentType); | 451 attr.userData = new FetchContext(oracle_, receiver_, command_.release(), expectedContentType); |
398 | 452 |
399 // Must be the last call to prevent memory leak on error | 453 // Must be the last call to prevent memory leak on error |
454 #if 0 | |
455 LOG(TRACE) << "Performing " << method << " request on URI: \"" << uri_ << "\""; | |
456 #endif | |
400 emscripten_fetch(&attr, uri_.c_str()); | 457 emscripten_fetch(&attr, uri_.c_str()); |
401 } | 458 } |
402 catch(...) | 459 catch(...) |
403 { | 460 { |
404 if(requestData != NULL) | 461 if(requestData != NULL) |
405 free(requestData); | 462 free(requestData); |
406 throw; | 463 throw; |
407 } | 464 } |
408 } | 465 } |
409 }; | 466 }; |
410 | 467 |
468 #if 0 | |
469 static void DumpCommand(OrthancRestApiCommand* pCommand) | |
470 { | |
471 OrthancRestApiCommand& command = *pCommand; | |
472 LOG(TRACE) << "WebAssemblyOracle::Execute for REST command."; | |
473 switch (command.GetMethod()) { | |
474 case Orthanc::HttpMethod_Get: | |
475 LOG(TRACE) << " * WebAssemblyOracle::Execute GET URI = " << command.GetUri() << " timeout = " << command.GetTimeout(); | |
476 break; | |
477 case Orthanc::HttpMethod_Post: | |
478 LOG(TRACE) << " * WebAssemblyOracle::Execute POST URI = " << command.GetUri() << " body = " << command.GetBody() << " timeout = " << command.GetTimeout(); | |
479 break; | |
480 default: | |
481 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
482 break; | |
483 } | |
484 } | |
485 #endif | |
486 | |
411 void WebAssemblyOracle::Execute(const IObserver& receiver, | 487 void WebAssemblyOracle::Execute(const IObserver& receiver, |
412 OrthancRestApiCommand* command) | 488 OrthancRestApiCommand* command) |
413 { | 489 { |
490 #if 0 | |
491 DumpCommand(command); | |
492 | |
493 if (logbgo233) { | |
494 if (logbgo115) | |
495 LOG(TRACE) << " WebAssemblyOracle::Execute (OrthancRestApiCommand) command addr " << | |
496 std::hex << command << std::dec; | |
497 } | |
498 #endif | |
499 | |
414 try | 500 try |
415 { | 501 { |
416 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute."; | 502 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute."; |
417 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command; | 503 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command; |
418 FetchCommand fetch(*this, receiver, command); | 504 FetchCommand fetch(*this, receiver, command); |
462 | 548 |
463 | 549 |
464 void WebAssemblyOracle::Execute(const IObserver& receiver, | 550 void WebAssemblyOracle::Execute(const IObserver& receiver, |
465 GetOrthancImageCommand* command) | 551 GetOrthancImageCommand* command) |
466 { | 552 { |
553 #if 0 | |
554 if (logbgo233) { | |
555 if (logbgo115) | |
556 LOG(TRACE) << " WebAssemblyOracle::Execute (GetOrthancImageCommand) command addr " << | |
557 std::hex << command << std::dec; | |
558 } | |
559 #endif | |
560 | |
467 FetchCommand fetch(*this, receiver, command); | 561 FetchCommand fetch(*this, receiver, command); |
468 | 562 |
469 fetch.SetUri(command->GetUri()); | 563 fetch.SetUri(command->GetUri()); |
470 fetch.SetHttpHeaders(command->GetHttpHeaders()); | 564 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
471 fetch.SetTimeout(command->GetTimeout()); | 565 fetch.SetTimeout(command->GetTimeout()); |
475 | 569 |
476 | 570 |
477 void WebAssemblyOracle::Execute(const IObserver& receiver, | 571 void WebAssemblyOracle::Execute(const IObserver& receiver, |
478 GetOrthancWebViewerJpegCommand* command) | 572 GetOrthancWebViewerJpegCommand* command) |
479 { | 573 { |
574 #if 0 | |
575 if (logbgo233) { | |
576 if (logbgo115) | |
577 LOG(TRACE) << " WebAssemblyOracle::Execute (GetOrthancWebViewerJpegCommand) command addr " << std::hex << command << std::dec; | |
578 } | |
579 #endif | |
580 | |
480 FetchCommand fetch(*this, receiver, command); | 581 FetchCommand fetch(*this, receiver, command); |
481 | 582 |
482 fetch.SetUri(command->GetUri()); | 583 fetch.SetUri(command->GetUri()); |
483 fetch.SetHttpHeaders(command->GetHttpHeaders()); | 584 fetch.SetHttpHeaders(command->GetHttpHeaders()); |
484 fetch.SetTimeout(command->GetTimeout()); | 585 fetch.SetTimeout(command->GetTimeout()); |
489 | 590 |
490 | 591 |
491 void WebAssemblyOracle::Schedule(const IObserver& receiver, | 592 void WebAssemblyOracle::Schedule(const IObserver& receiver, |
492 IOracleCommand* command) | 593 IOracleCommand* command) |
493 { | 594 { |
595 #if 0 | |
596 if (logbgo233) { | |
597 if (logbgo115) | |
598 LOG(TRACE) << " WebAssemblyOracle::Schedule command addr " << | |
599 std::hex << command << std::dec; | |
600 } | |
601 #endif | |
494 std::auto_ptr<IOracleCommand> protection(command); | 602 std::auto_ptr<IOracleCommand> protection(command); |
495 | 603 |
496 if (command == NULL) | 604 if (command == NULL) |
497 { | 605 { |
498 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | 606 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |