comparison Framework/Oracle/WebAssemblyOracle.cpp @ 1242:b9b5d4378874 broker

working of WebAssemblyOracle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 06 Jan 2020 18:08:05 +0100
parents b9f2a111c5b9
children 608983cc2512
comparison
equal deleted inserted replaced
1241:a4bb8c2dd211 1242:b9b5d4378874
336 std::string url_; 336 std::string url_;
337 std::string body_; 337 std::string body_;
338 HttpHeaders headers_; 338 HttpHeaders headers_;
339 unsigned int timeout_; 339 unsigned int timeout_;
340 std::string expectedContentType_; 340 std::string expectedContentType_;
341 bool hasCredentials_;
342 std::string username_;
343 std::string password_;
341 344
342 public: 345 public:
343 FetchCommand(WebAssemblyOracle& oracle, 346 FetchCommand(WebAssemblyOracle& oracle,
344 boost::weak_ptr<IObserver> receiver, 347 boost::weak_ptr<IObserver> receiver,
345 IOracleCommand* command) : 348 IOracleCommand* command) :
346 oracle_(oracle), 349 oracle_(oracle),
347 receiver_(receiver), 350 receiver_(receiver),
348 command_(command), 351 command_(command),
349 method_(Orthanc::HttpMethod_Get), 352 method_(Orthanc::HttpMethod_Get),
350 timeout_(0) 353 timeout_(0),
354 hasCredentials_(false)
351 { 355 {
352 if (command == NULL) 356 if (command == NULL)
353 { 357 {
354 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 358 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
355 } 359 }
358 void SetMethod(Orthanc::HttpMethod method) 362 void SetMethod(Orthanc::HttpMethod method)
359 { 363 {
360 method_ = method; 364 method_ = method;
361 } 365 }
362 366
363 void SetOrthancUri(const std::string& uri)
364 {
365 url_ = oracle_.orthancRoot_ + uri;
366 }
367
368 void SetUrl(const std::string& url) 367 void SetUrl(const std::string& url)
369 { 368 {
370 url_ = url; 369 url_ = url;
371 } 370 }
372 371
373 void SetBody(std::string& body /* will be swapped */) 372 void SetBody(std::string& body /* will be swapped */)
374 { 373 {
375 body_.swap(body); 374 body_.swap(body);
376 } 375 }
377 376
378 void SetHttpHeaders(const HttpHeaders& headers) 377 void AddHttpHeaders(const HttpHeaders& headers)
379 { 378 {
380 headers_ = headers; 379 for (HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); ++it)
380 {
381 headers_[it->first] = it->second;
382 }
381 } 383 }
382 384
383 void SetTimeout(unsigned int timeout) 385 void SetTimeout(unsigned int timeout)
384 { 386 {
385 timeout_ = timeout; 387 timeout_ = timeout;
388 }
389
390 void SetCredentials(const std::string& username,
391 const std::string& password)
392 {
393 hasCredentials_ = true;
394 username_ = username;
395 password_ = password;
386 } 396 }
387 397
388 void Execute() 398 void Execute()
389 { 399 {
390 #if 0 400 #if 0
433 attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE; 443 attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE;
434 attr.onsuccess = FetchContext::SuccessCallback; 444 attr.onsuccess = FetchContext::SuccessCallback;
435 attr.onerror = FetchContext::FailureCallback; 445 attr.onerror = FetchContext::FailureCallback;
436 attr.timeoutMSecs = timeout_ * 1000; 446 attr.timeoutMSecs = timeout_ * 1000;
437 447
448 if (hasCredentials_)
449 {
450 attr.withCredentials = EM_TRUE;
451 attr.userName = username_.c_str();
452 attr.password = password_.c_str();
453 }
454
438 std::vector<const char*> headers; 455 std::vector<const char*> headers;
439 headers.reserve(2 * headers_.size() + 1); 456 headers.reserve(2 * headers_.size() + 1);
440 457
441 std::string expectedContentType; 458 std::string expectedContentType;
442 459
508 } 525 }
509 } 526 }
510 #endif 527 #endif
511 528
512 529
530 void WebAssemblyOracle::SetOrthancUrl(FetchCommand& command,
531 const std::string& uri) const
532 {
533 if (isLocalOrthanc_)
534 {
535 command.SetUrl(localOrthancRoot_ + uri);
536 }
537 else
538 {
539 command.SetUrl(remoteOrthanc_.GetUrl() + uri);
540 command.AddHttpHeaders(remoteOrthanc_.GetHttpHeaders());
541
542 if (!remoteOrthanc_.GetUsername().empty())
543 {
544 command.SetCredentials(remoteOrthanc_.GetUsername(), remoteOrthanc_.GetPassword());
545 }
546 }
547 }
548
549
513 void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver, 550 void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
514 HttpCommand* command) 551 HttpCommand* command)
515 { 552 {
516 FetchCommand fetch(*this, receiver, command); 553 FetchCommand fetch(*this, receiver, command);
517 554
518 fetch.SetMethod(command->GetMethod()); 555 fetch.SetMethod(command->GetMethod());
519 fetch.SetUrl(command->GetUrl()); 556 fetch.SetUrl(command->GetUrl());
520 fetch.SetHttpHeaders(command->GetHttpHeaders()); 557 fetch.AddHttpHeaders(command->GetHttpHeaders());
521 fetch.SetTimeout(command->GetTimeout()); 558 fetch.SetTimeout(command->GetTimeout());
522 559
523 if (command->GetMethod() == Orthanc::HttpMethod_Post || 560 if (command->GetMethod() == Orthanc::HttpMethod_Post ||
524 command->GetMethod() == Orthanc::HttpMethod_Put) 561 command->GetMethod() == Orthanc::HttpMethod_Put)
525 { 562 {
550 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute."; 587 //LOG(TRACE) << "*********** WebAssemblyOracle::Execute.";
551 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command; 588 //LOG(TRACE) << "WebAssemblyOracle::Execute | command = " << command;
552 FetchCommand fetch(*this, receiver, command); 589 FetchCommand fetch(*this, receiver, command);
553 590
554 fetch.SetMethod(command->GetMethod()); 591 fetch.SetMethod(command->GetMethod());
555 fetch.SetOrthancUri(command->GetUri()); 592 SetOrthancUrl(fetch, command->GetUri());
556 fetch.SetHttpHeaders(command->GetHttpHeaders()); 593 fetch.AddHttpHeaders(command->GetHttpHeaders());
557 fetch.SetTimeout(command->GetTimeout()); 594 fetch.SetTimeout(command->GetTimeout());
558 595
559 if (command->GetMethod() == Orthanc::HttpMethod_Post || 596 if (command->GetMethod() == Orthanc::HttpMethod_Post ||
560 command->GetMethod() == Orthanc::HttpMethod_Put) 597 command->GetMethod() == Orthanc::HttpMethod_Put)
561 { 598 {
606 } 643 }
607 #endif 644 #endif
608 645
609 FetchCommand fetch(*this, receiver, command); 646 FetchCommand fetch(*this, receiver, command);
610 647
611 fetch.SetOrthancUri(command->GetUri()); 648 SetOrthancUrl(fetch, command->GetUri());
612 fetch.SetHttpHeaders(command->GetHttpHeaders()); 649 fetch.AddHttpHeaders(command->GetHttpHeaders());
613 fetch.SetTimeout(command->GetTimeout()); 650 fetch.SetTimeout(command->GetTimeout());
614 651
615 fetch.Execute(); 652 fetch.Execute();
616 } 653 }
617 654
626 } 663 }
627 #endif 664 #endif
628 665
629 FetchCommand fetch(*this, receiver, command); 666 FetchCommand fetch(*this, receiver, command);
630 667
631 fetch.SetOrthancUri(command->GetUri()); 668 SetOrthancUrl(fetch, command->GetUri());
632 fetch.SetHttpHeaders(command->GetHttpHeaders()); 669 fetch.AddHttpHeaders(command->GetHttpHeaders());
633 fetch.SetTimeout(command->GetTimeout()); 670 fetch.SetTimeout(command->GetTimeout());
634 671
635 fetch.Execute(); 672 fetch.Execute();
636 } 673 }
637 674