Mercurial > hg > orthanc
comparison OrthancServer/Plugins/Engine/OrthancPlugins.cpp @ 4606:d01702fb29a9 db-changes
integration mainline->db-changes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Mar 2021 16:34:23 +0200 |
parents | 2c702cfae274 c8f444e8556d |
children | f75c63aa9de0 |
comparison
equal
deleted
inserted
replaced
4604:2c702cfae274 | 4606:d01702fb29a9 |
---|---|
151 { | 151 { |
152 memcpy(result, &str[0], str.size() + 1); | 152 memcpy(result, &str[0], str.size() + 1); |
153 } | 153 } |
154 | 154 |
155 return result; | 155 return result; |
156 } | |
157 | |
158 | |
159 static void CopyDictionary(OrthancPluginMemoryBuffer& target, | |
160 const std::map<std::string, std::string>& dictionary) | |
161 { | |
162 Json::Value json = Json::objectValue; | |
163 | |
164 for (HttpClient::HttpHeaders::const_iterator | |
165 it = dictionary.begin(); it != dictionary.end(); ++it) | |
166 { | |
167 json[it->first] = it->second; | |
168 } | |
169 | |
170 std::string s = json.toStyledString(); | |
171 CopyToMemoryBuffer(target, s); | |
156 } | 172 } |
157 | 173 |
158 | 174 |
159 namespace | 175 namespace |
160 { | 176 { |
2678 } | 2694 } |
2679 | 2695 |
2680 std::map<std::string, std::string> httpHeaders; | 2696 std::map<std::string, std::string> httpHeaders; |
2681 | 2697 |
2682 std::string result; | 2698 std::string result; |
2683 if (IHttpHandler::SimpleGet(result, *handler, RequestOrigin_Plugins, p.uri, httpHeaders)) | 2699 if (IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, httpHeaders) == HttpStatus_200_Ok) |
2684 { | 2700 { |
2685 CopyToMemoryBuffer(*p.target, result); | 2701 CopyToMemoryBuffer(*p.target, result); |
2686 } | 2702 } |
2687 else | 2703 else |
2688 { | 2704 { |
2714 PImpl::ServerContextLock lock(*pimpl_); | 2730 PImpl::ServerContextLock lock(*pimpl_); |
2715 handler = &lock.GetContext().GetHttpHandler().RestrictToOrthancRestApi(!p.afterPlugins); | 2731 handler = &lock.GetContext().GetHttpHandler().RestrictToOrthancRestApi(!p.afterPlugins); |
2716 } | 2732 } |
2717 | 2733 |
2718 std::string result; | 2734 std::string result; |
2719 if (IHttpHandler::SimpleGet(result, *handler, RequestOrigin_Plugins, p.uri, headers)) | 2735 if (IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, headers) == HttpStatus_200_Ok) |
2720 { | 2736 { |
2721 CopyToMemoryBuffer(*p.target, result); | 2737 CopyToMemoryBuffer(*p.target, result); |
2722 } | 2738 } |
2723 else | 2739 else |
2724 { | 2740 { |
2746 | 2762 |
2747 std::map<std::string, std::string> httpHeaders; | 2763 std::map<std::string, std::string> httpHeaders; |
2748 | 2764 |
2749 std::string result; | 2765 std::string result; |
2750 if (isPost ? | 2766 if (isPost ? |
2751 IHttpHandler::SimplePost(result, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, httpHeaders) : | 2767 IHttpHandler::SimplePost(result, NULL, *handler, RequestOrigin_Plugins, p.uri, |
2752 IHttpHandler::SimplePut (result, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, httpHeaders)) | 2768 p.body, p.bodySize, httpHeaders) == HttpStatus_200_Ok : |
2769 IHttpHandler::SimplePut(result, NULL, *handler, RequestOrigin_Plugins, p.uri, | |
2770 p.body, p.bodySize, httpHeaders) == HttpStatus_200_Ok) | |
2753 { | 2771 { |
2754 CopyToMemoryBuffer(*p.target, result); | 2772 CopyToMemoryBuffer(*p.target, result); |
2755 } | 2773 } |
2756 else | 2774 else |
2757 { | 2775 { |
2774 handler = &lock.GetContext().GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins); | 2792 handler = &lock.GetContext().GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins); |
2775 } | 2793 } |
2776 | 2794 |
2777 std::map<std::string, std::string> httpHeaders; | 2795 std::map<std::string, std::string> httpHeaders; |
2778 | 2796 |
2779 if (!IHttpHandler::SimpleDelete(*handler, RequestOrigin_Plugins, uri, httpHeaders)) | 2797 if (IHttpHandler::SimpleDelete(NULL, *handler, RequestOrigin_Plugins, uri, httpHeaders) != HttpStatus_200_Ok) |
2780 { | 2798 { |
2781 throw OrthancException(ErrorCode_UnknownResource); | 2799 throw OrthancException(ErrorCode_UnknownResource); |
2782 } | 2800 } |
2783 } | 2801 } |
2784 | 2802 |
3333 static void ExecuteHttpClientWithoutChunkedBody(uint16_t& httpStatus, | 3351 static void ExecuteHttpClientWithoutChunkedBody(uint16_t& httpStatus, |
3334 OrthancPluginMemoryBuffer* answerBody, | 3352 OrthancPluginMemoryBuffer* answerBody, |
3335 OrthancPluginMemoryBuffer* answerHeaders, | 3353 OrthancPluginMemoryBuffer* answerHeaders, |
3336 HttpClient& client) | 3354 HttpClient& client) |
3337 { | 3355 { |
3338 if (answerBody == NULL) | |
3339 { | |
3340 throw OrthancException(ErrorCode_NullPointer); | |
3341 } | |
3342 | |
3343 std::string body; | 3356 std::string body; |
3344 HttpClient::HttpHeaders headers; | 3357 HttpClient::HttpHeaders headers; |
3345 | 3358 |
3346 bool success = client.Apply(body, headers); | 3359 bool success = client.Apply(body, headers); |
3347 | 3360 |
3354 } | 3367 } |
3355 | 3368 |
3356 // Copy the HTTP headers of the answer, if the plugin requested them | 3369 // Copy the HTTP headers of the answer, if the plugin requested them |
3357 if (answerHeaders != NULL) | 3370 if (answerHeaders != NULL) |
3358 { | 3371 { |
3359 Json::Value json = Json::objectValue; | 3372 CopyDictionary(*answerHeaders, headers); |
3360 | |
3361 for (HttpClient::HttpHeaders::const_iterator | |
3362 it = headers.begin(); it != headers.end(); ++it) | |
3363 { | |
3364 json[it->first] = it->second; | |
3365 } | |
3366 | |
3367 std::string s = json.toStyledString(); | |
3368 CopyToMemoryBuffer(*answerHeaders, s); | |
3369 } | 3373 } |
3370 | 3374 |
3371 // Copy the body of the answer if it makes sense | 3375 // Copy the body of the answer if it makes sense |
3372 if (client.GetMethod() != HttpMethod_Delete) | 3376 if (client.GetMethod() != HttpMethod_Delete) |
3373 { | 3377 { |
3374 CopyToMemoryBuffer(*answerBody, body); | 3378 try |
3379 { | |
3380 if (answerBody != NULL) | |
3381 { | |
3382 CopyToMemoryBuffer(*answerBody, body); | |
3383 } | |
3384 } | |
3385 catch (OrthancException&) | |
3386 { | |
3387 if (answerHeaders != NULL) | |
3388 { | |
3389 free(answerHeaders->data); | |
3390 } | |
3391 throw; | |
3392 } | |
3375 } | 3393 } |
3376 } | 3394 } |
3377 | 3395 |
3378 | 3396 |
3379 void OrthancPlugins::CallHttpClient(const void* parameters) | 3397 void OrthancPlugins::CallHttpClient(const void* parameters) |
3485 HttpClient::ThrowException(client.GetLastStatus()); | 3503 HttpClient::ThrowException(client.GetLastStatus()); |
3486 } | 3504 } |
3487 } | 3505 } |
3488 | 3506 |
3489 | 3507 |
3508 void OrthancPlugins::CallRestApi(const void* parameters) | |
3509 { | |
3510 const _OrthancPluginCallRestApi& p = *reinterpret_cast<const _OrthancPluginCallRestApi*>(parameters); | |
3511 | |
3512 if (p.httpStatus == NULL) | |
3513 { | |
3514 throw OrthancException(ErrorCode_NullPointer); | |
3515 } | |
3516 | |
3517 const char* methodString; | |
3518 switch (p.method) | |
3519 { | |
3520 case OrthancPluginHttpMethod_Get: | |
3521 methodString = "GET"; | |
3522 break; | |
3523 | |
3524 case OrthancPluginHttpMethod_Post: | |
3525 methodString = "POST"; | |
3526 break; | |
3527 | |
3528 case OrthancPluginHttpMethod_Put: | |
3529 methodString = "PUT"; | |
3530 break; | |
3531 | |
3532 case OrthancPluginHttpMethod_Delete: | |
3533 methodString = "DELETE"; | |
3534 break; | |
3535 | |
3536 default: | |
3537 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
3538 } | |
3539 | |
3540 CLOG(INFO, PLUGINS) << "Plugin making REST " << methodString << " call to URI " << p.uri | |
3541 << (p.afterPlugins ? " (after plugins)" : " (built-in API)"); | |
3542 | |
3543 HttpToolbox::Arguments headers; | |
3544 | |
3545 for (uint32_t i = 0; i < p.headersCount; i++) | |
3546 { | |
3547 std::string name(p.headersKeys[i]); | |
3548 std::transform(name.begin(), name.end(), name.begin(), ::tolower); | |
3549 headers[name] = p.headersValues[i]; | |
3550 } | |
3551 | |
3552 IHttpHandler* handler; | |
3553 | |
3554 { | |
3555 PImpl::ServerContextLock lock(*pimpl_); | |
3556 handler = &lock.GetContext().GetHttpHandler().RestrictToOrthancRestApi(!p.afterPlugins); | |
3557 } | |
3558 | |
3559 std::string answerBody; | |
3560 std::map<std::string, std::string> answerHeaders; | |
3561 HttpStatus status; | |
3562 | |
3563 switch (p.method) | |
3564 { | |
3565 case OrthancPluginHttpMethod_Get: | |
3566 status = IHttpHandler::SimpleGet( | |
3567 answerBody, &answerHeaders, *handler, RequestOrigin_Plugins, p.uri, headers); | |
3568 break; | |
3569 | |
3570 case OrthancPluginHttpMethod_Post: | |
3571 status = IHttpHandler::SimplePost( | |
3572 answerBody, &answerHeaders, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, headers); | |
3573 break; | |
3574 | |
3575 case OrthancPluginHttpMethod_Put: | |
3576 status = IHttpHandler::SimplePut( | |
3577 answerBody, &answerHeaders, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, headers); | |
3578 break; | |
3579 | |
3580 case OrthancPluginHttpMethod_Delete: | |
3581 status = IHttpHandler::SimpleDelete( | |
3582 &answerHeaders, *handler, RequestOrigin_Plugins, p.uri, headers); | |
3583 break; | |
3584 | |
3585 default: | |
3586 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
3587 } | |
3588 | |
3589 *p.httpStatus = static_cast<uint16_t>(status); | |
3590 | |
3591 if (p.answerHeaders != NULL) | |
3592 { | |
3593 CopyDictionary(*p.answerHeaders, answerHeaders); | |
3594 } | |
3595 | |
3596 try | |
3597 { | |
3598 if (p.answerBody != NULL) | |
3599 { | |
3600 CopyToMemoryBuffer(*p.answerBody, answerBody); | |
3601 } | |
3602 } | |
3603 catch (OrthancException&) | |
3604 { | |
3605 if (p.answerHeaders != NULL) | |
3606 { | |
3607 free(p.answerHeaders->data); | |
3608 } | |
3609 throw; | |
3610 } | |
3611 } | |
3612 | |
3613 | |
3490 void OrthancPlugins::CallPeerApi(const void* parameters) | 3614 void OrthancPlugins::CallPeerApi(const void* parameters) |
3491 { | 3615 { |
3492 const _OrthancPluginCallPeerApi& p = *reinterpret_cast<const _OrthancPluginCallPeerApi*>(parameters); | 3616 const _OrthancPluginCallPeerApi& p = *reinterpret_cast<const _OrthancPluginCallPeerApi*>(parameters); |
3493 const OrthancPeers& peers = *reinterpret_cast<const OrthancPeers*>(p.peers); | 3617 const OrthancPeers& peers = *reinterpret_cast<const OrthancPeers*>(p.peers); |
3494 | 3618 |
3549 } | 3673 } |
3550 | 3674 |
3551 // Copy the HTTP headers of the answer, if the plugin requested them | 3675 // Copy the HTTP headers of the answer, if the plugin requested them |
3552 if (p.answerHeaders != NULL) | 3676 if (p.answerHeaders != NULL) |
3553 { | 3677 { |
3554 Json::Value json = Json::objectValue; | 3678 CopyDictionary(*p.answerHeaders, headers); |
3555 | |
3556 for (HttpClient::HttpHeaders::const_iterator | |
3557 it = headers.begin(); it != headers.end(); ++it) | |
3558 { | |
3559 json[it->first] = it->second; | |
3560 } | |
3561 | |
3562 std::string s = json.toStyledString(); | |
3563 CopyToMemoryBuffer(*p.answerHeaders, s); | |
3564 } | 3679 } |
3565 | 3680 |
3566 // Copy the body of the answer if it makes sense | 3681 // Copy the body of the answer if it makes sense |
3567 if (p.method != OrthancPluginHttpMethod_Delete) | 3682 if (p.method != OrthancPluginHttpMethod_Delete) |
3568 { | 3683 { |
3569 CopyToMemoryBuffer(*p.answerBody, body); | 3684 try |
3685 { | |
3686 if (p.answerBody != NULL) | |
3687 { | |
3688 CopyToMemoryBuffer(*p.answerBody, body); | |
3689 } | |
3690 } | |
3691 catch (OrthancException&) | |
3692 { | |
3693 if (p.answerHeaders != NULL) | |
3694 { | |
3695 free(p.answerHeaders->data); | |
3696 } | |
3697 throw; | |
3698 } | |
3570 } | 3699 } |
3571 } | 3700 } |
3572 | 3701 |
3573 | 3702 |
3574 void OrthancPlugins::ConvertPixelFormat(const void* parameters) | 3703 void OrthancPlugins::ConvertPixelFormat(const void* parameters) |
4220 CallHttpClient2(parameters); | 4349 CallHttpClient2(parameters); |
4221 return true; | 4350 return true; |
4222 | 4351 |
4223 case _OrthancPluginService_ChunkedHttpClient: | 4352 case _OrthancPluginService_ChunkedHttpClient: |
4224 ChunkedHttpClient(parameters); | 4353 ChunkedHttpClient(parameters); |
4354 return true; | |
4355 | |
4356 case _OrthancPluginService_CallRestApi: | |
4357 CallRestApi(parameters); | |
4225 return true; | 4358 return true; |
4226 | 4359 |
4227 case _OrthancPluginService_ConvertPixelFormat: | 4360 case _OrthancPluginService_ConvertPixelFormat: |
4228 ConvertPixelFormat(parameters); | 4361 ConvertPixelFormat(parameters); |
4229 return true; | 4362 return true; |