comparison Core/HttpClient.cpp @ 2954:d924f9bb61cc

taking advantage of details in OrthancException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Dec 2018 14:35:34 +0100
parents 807169f85ba9
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2953:210d5afd8f2b 2954:d924f9bb61cc
75 static CURLcode OrthancHttpClientPerformSSL(CURL* curl, long* status) 75 static CURLcode OrthancHttpClientPerformSSL(CURL* curl, long* status)
76 { 76 {
77 #if ORTHANC_ENABLE_SSL == 1 77 #if ORTHANC_ENABLE_SSL == 1
78 return GetHttpStatus(curl_easy_perform(curl), curl, status); 78 return GetHttpStatus(curl_easy_perform(curl), curl, status);
79 #else 79 #else
80 LOG(ERROR) << "Orthanc was compiled without SSL support, cannot make HTTPS request"; 80 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 81 "Orthanc was compiled without SSL support, "
82 "cannot make HTTPS request");
82 #endif 83 #endif
83 } 84 }
84 } 85 }
85 86
86 87
218 219
219 static CURLcode CheckCode(CURLcode code) 220 static CURLcode CheckCode(CURLcode code)
220 { 221 {
221 if (code == CURLE_NOT_BUILT_IN) 222 if (code == CURLE_NOT_BUILT_IN)
222 { 223 {
223 LOG(ERROR) << "Your libcurl does not contain a required feature, " 224 throw OrthancException(ErrorCode_InternalError,
224 << "please recompile Orthanc with -DUSE_SYSTEM_CURL=OFF"; 225 "Your libcurl does not contain a required feature, "
225 throw OrthancException(ErrorCode_InternalError); 226 "please recompile Orthanc with -DUSE_SYSTEM_CURL=OFF");
226 } 227 }
227 228
228 if (code != CURLE_OK) 229 if (code != CURLE_OK)
229 { 230 {
230 LOG(ERROR) << "libCURL error: " + std::string(curl_easy_strerror(code)); 231 throw OrthancException(ErrorCode_NetworkProtocol,
231 throw OrthancException(ErrorCode_NetworkProtocol); 232 "libCURL error: " + std::string(curl_easy_strerror(code)));
232 } 233 }
233 234
234 return code; 235 return code;
235 } 236 }
236 237
500 501
501 // Setup the HTTPS client certificate 502 // Setup the HTTPS client certificate
502 if (!clientCertificateFile_.empty() && 503 if (!clientCertificateFile_.empty() &&
503 pkcs11Enabled_) 504 pkcs11Enabled_)
504 { 505 {
505 LOG(ERROR) << "Cannot enable both client certificates and PKCS#11 authentication"; 506 throw OrthancException(ErrorCode_ParameterOutOfRange,
506 throw OrthancException(ErrorCode_ParameterOutOfRange); 507 "Cannot enable both client certificates and PKCS#11 authentication");
507 } 508 }
508 509
509 if (pkcs11Enabled_) 510 if (pkcs11Enabled_)
510 { 511 {
511 #if ORTHANC_ENABLE_PKCS11 == 1 512 #if ORTHANC_ENABLE_PKCS11 == 1
515 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEYTYPE, "ENG")); 516 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEYTYPE, "ENG"));
516 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLCERTTYPE, "ENG")); 517 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLCERTTYPE, "ENG"));
517 } 518 }
518 else 519 else
519 { 520 {
520 LOG(ERROR) << "Cannot use PKCS#11 for a HTTPS request, because it has not been initialized"; 521 throw OrthancException(ErrorCode_BadSequenceOfCalls,
521 throw OrthancException(ErrorCode_BadSequenceOfCalls); 522 "Cannot use PKCS#11 for a HTTPS request, "
523 "because it has not been initialized");
522 } 524 }
523 #else 525 #else
524 LOG(ERROR) << "This version of Orthanc is compiled without support for PKCS#11"; 526 throw OrthancException(ErrorCode_InternalError,
525 throw OrthancException(ErrorCode_InternalError); 527 "This version of Orthanc is compiled without support for PKCS#11");
526 #endif 528 #endif
527 } 529 }
528 else if (!clientCertificateFile_.empty()) 530 else if (!clientCertificateFile_.empty())
529 { 531 {
530 #if ORTHANC_ENABLE_SSL == 1 532 #if ORTHANC_ENABLE_SSL == 1
542 { 544 {
543 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEYTYPE, "PEM")); 545 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEYTYPE, "PEM"));
544 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEY, clientCertificateKeyFile_.c_str())); 546 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEY, clientCertificateKeyFile_.c_str()));
545 } 547 }
546 #else 548 #else
547 LOG(ERROR) << "This version of Orthanc is compiled without OpenSSL support, cannot use HTTPS client authentication"; 549 throw OrthancException(ErrorCode_InternalError,
548 throw OrthancException(ErrorCode_InternalError); 550 "This version of Orthanc is compiled without OpenSSL support, "
551 "cannot use HTTPS client authentication");
549 #endif 552 #endif
550 } 553 }
551 554
552 // Reset the parameters from previous calls to Apply() 555 // Reset the parameters from previous calls to Apply()
553 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->userHeaders_)); 556 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->userHeaders_));
834 throw OrthancException(ErrorCode_ParameterOutOfRange); 837 throw OrthancException(ErrorCode_ParameterOutOfRange);
835 } 838 }
836 839
837 if (!SystemToolbox::IsRegularFile(certificateFile)) 840 if (!SystemToolbox::IsRegularFile(certificateFile))
838 { 841 {
839 LOG(ERROR) << "Cannot open certificate file: " << certificateFile; 842 throw OrthancException(ErrorCode_InexistentFile,
840 throw OrthancException(ErrorCode_InexistentFile); 843 "Cannot open certificate file: " + certificateFile);
841 } 844 }
842 845
843 if (!certificateKeyFile.empty() && 846 if (!certificateKeyFile.empty() &&
844 !SystemToolbox::IsRegularFile(certificateKeyFile)) 847 !SystemToolbox::IsRegularFile(certificateKeyFile))
845 { 848 {
846 LOG(ERROR) << "Cannot open key file: " << certificateKeyFile; 849 throw OrthancException(ErrorCode_InexistentFile,
847 throw OrthancException(ErrorCode_InexistentFile); 850 "Cannot open key file: " + certificateKeyFile);
848 } 851 }
849 852
850 clientCertificateFile_ = certificateFile; 853 clientCertificateFile_ = certificateFile;
851 clientCertificateKeyFile_ = certificateKeyFile; 854 clientCertificateKeyFile_ = certificateKeyFile;
852 clientCertificateKeyPassword_ = certificateKeyPassword; 855 clientCertificateKeyPassword_ = certificateKeyPassword;
860 #if ORTHANC_ENABLE_PKCS11 == 1 863 #if ORTHANC_ENABLE_PKCS11 == 1
861 LOG(INFO) << "Initializing PKCS#11 using " << module 864 LOG(INFO) << "Initializing PKCS#11 using " << module
862 << (pin.empty() ? " (no PIN provided)" : " (PIN is provided)"); 865 << (pin.empty() ? " (no PIN provided)" : " (PIN is provided)");
863 GlobalParameters::GetInstance().InitializePkcs11(module, pin, verbose); 866 GlobalParameters::GetInstance().InitializePkcs11(module, pin, verbose);
864 #else 867 #else
865 LOG(ERROR) << "This version of Orthanc is compiled without support for PKCS#11"; 868 throw OrthancException(ErrorCode_InternalError,
866 throw OrthancException(ErrorCode_InternalError); 869 "This version of Orthanc is compiled without support for PKCS#11");
867 #endif 870 #endif
868 } 871 }
869 } 872 }