Mercurial > hg > orthanc
comparison Plugins/Engine/OrthancPlugins.cpp @ 1446:8dc80ba768aa
refactoring: IHttpHandler does not use std::string to hold the request body
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 01 Jul 2015 13:16:12 +0200 |
parents | d26c8a93d05a |
children | 5ba7471780ae |
comparison
equal
deleted
inserted
replaced
1445:d26c8a93d05a | 1446:8dc80ba768aa |
---|---|
229 bool OrthancPlugins::Handle(HttpOutput& output, | 229 bool OrthancPlugins::Handle(HttpOutput& output, |
230 HttpMethod method, | 230 HttpMethod method, |
231 const UriComponents& uri, | 231 const UriComponents& uri, |
232 const Arguments& headers, | 232 const Arguments& headers, |
233 const GetArguments& getArguments, | 233 const GetArguments& getArguments, |
234 const std::string& postData) | 234 const char* bodyData, |
235 size_t bodySize) | |
235 { | 236 { |
236 std::string flatUri = Toolbox::FlattenUri(uri); | 237 std::string flatUri = Toolbox::FlattenUri(uri); |
237 OrthancPluginRestCallback callback = NULL; | 238 OrthancPluginRestCallback callback = NULL; |
238 | 239 |
239 std::vector<std::string> groups; | 240 std::vector<std::string> groups; |
306 | 307 |
307 | 308 |
308 request.groups = (cgroups.size() ? &cgroups[0] : NULL); | 309 request.groups = (cgroups.size() ? &cgroups[0] : NULL); |
309 request.groupsCount = cgroups.size(); | 310 request.groupsCount = cgroups.size(); |
310 request.getCount = getArguments.size(); | 311 request.getCount = getArguments.size(); |
311 request.body = (postData.size() ? &postData[0] : NULL); | 312 request.body = bodyData; |
312 request.bodySize = postData.size(); | 313 request.bodySize = bodySize; |
313 request.headersCount = headers.size(); | 314 request.headersCount = headers.size(); |
314 | 315 |
315 if (getArguments.size() > 0) | 316 if (getArguments.size() > 0) |
316 { | 317 { |
317 request.getKeys = &getKeys[0]; | 318 request.getKeys = &getKeys[0]; |
647 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT | 648 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT |
648 | 649 |
649 UriComponents uri; | 650 UriComponents uri; |
650 Toolbox::SplitUriComponents(uri, p.uri); | 651 Toolbox::SplitUriComponents(uri, p.uri); |
651 | 652 |
652 // TODO Avoid unecessary memcpy | |
653 std::string body(p.body, p.bodySize); | |
654 | |
655 StringHttpOutput stream; | 653 StringHttpOutput stream; |
656 HttpOutput http(stream, false /* no keep alive */); | 654 HttpOutput http(stream, false /* no keep alive */); |
657 | 655 |
658 HttpMethod method = (isPost ? HttpMethod_Post : HttpMethod_Put); | 656 HttpMethod method = (isPost ? HttpMethod_Post : HttpMethod_Put); |
659 LOG(INFO) << "Plugin making REST " << EnumerationToString(method) << " call on URI " << p.uri | 657 LOG(INFO) << "Plugin making REST " << EnumerationToString(method) << " call on URI " << p.uri |
662 bool ok = false; | 660 bool ok = false; |
663 std::string result; | 661 std::string result; |
664 | 662 |
665 if (afterPlugins) | 663 if (afterPlugins) |
666 { | 664 { |
667 ok = Handle(http, method, uri, headers, getArguments, body); | 665 ok = Handle(http, method, uri, headers, getArguments, p.body, p.bodySize); |
668 } | 666 } |
669 | 667 |
670 if (!ok) | 668 if (!ok) |
671 { | 669 { |
672 ok = (pimpl_->restApi_ != NULL && | 670 ok = (pimpl_->restApi_ != NULL && |
673 pimpl_->restApi_->Handle(http, method, uri, headers, getArguments, body)); | 671 pimpl_->restApi_->Handle(http, method, uri, headers, getArguments, p.body, p.bodySize)); |
674 } | 672 } |
675 | 673 |
676 if (ok) | 674 if (ok) |
677 { | 675 { |
678 stream.GetOutput(result); | 676 stream.GetOutput(result); |
694 | 692 |
695 // TODO : Use "HttpToolbox::SimpleDelete()" | 693 // TODO : Use "HttpToolbox::SimpleDelete()" |
696 | 694 |
697 IHttpHandler::Arguments headers; // No HTTP header | 695 IHttpHandler::Arguments headers; // No HTTP header |
698 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT | 696 IHttpHandler::GetArguments getArguments; // No GET argument for POST/PUT |
699 std::string body; // No body for DELETE | |
700 | 697 |
701 StringHttpOutput stream; | 698 StringHttpOutput stream; |
702 HttpOutput http(stream, false /* no keep alive */); | 699 HttpOutput http(stream, false /* no keep alive */); |
703 | 700 |
704 LOG(INFO) << "Plugin making REST DELETE call on URI " | 701 LOG(INFO) << "Plugin making REST DELETE call on URI " |
707 | 704 |
708 bool ok = false; | 705 bool ok = false; |
709 | 706 |
710 if (afterPlugins) | 707 if (afterPlugins) |
711 { | 708 { |
712 ok = Handle(http, HttpMethod_Delete, uri, headers, getArguments, body); | 709 ok = Handle(http, HttpMethod_Delete, uri, headers, getArguments, |
710 NULL /* no body for DELETE */, 0); | |
713 } | 711 } |
714 | 712 |
715 if (!ok) | 713 if (!ok) |
716 { | 714 { |
717 ok = (pimpl_->restApi_ != NULL && | 715 ok = (pimpl_->restApi_ != NULL && |
718 pimpl_->restApi_->Handle(http, HttpMethod_Delete, uri, headers, getArguments, body)); | 716 pimpl_->restApi_->Handle(http, HttpMethod_Delete, uri, headers, getArguments, |
717 NULL /* no body for DELETE */, 0)); | |
719 } | 718 } |
720 | 719 |
721 if (!ok) | 720 if (!ok) |
722 { | 721 { |
723 throw OrthancException(ErrorCode_BadRequest); | 722 throw OrthancException(ErrorCode_BadRequest); |