soapservice.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning( disable : 4786)
  14. #ifdef ESPHTTP_EXPORTS
  15. #define esp_http_decl DECL_EXPORT
  16. #else
  17. #define esp_http_decl DECL_IMPORT
  18. #endif
  19. //ESP Bindings
  20. #include "platform.h"
  21. #include <xpp/XmlPullParser.h>
  22. #include "SOAP/Platform/soapservice.hpp"
  23. #include "http/platform/httpservice.hpp"
  24. #include "bindutil.hpp"
  25. #include "authenticate.hpp"
  26. #include <memory>
  27. using namespace std;
  28. using namespace xpp;
  29. #ifdef _DEBUG
  30. //#define DEBUG_SOAP_
  31. #endif
  32. int CSoapService::processHeader(CHeader* header, IEspContext* ctx)
  33. {
  34. int num = header->getNumBlocks();
  35. if(ctx == NULL)
  36. return 0;
  37. int returnValue = 0;
  38. bool authenticated = !ctx->toBeAuthenticated();
  39. for (int i = 0; i < num; i++)
  40. {
  41. IRpcMessage* oneblock = header->getHeaderBlock(i);
  42. if(oneblock == NULL)
  43. continue;
  44. if(strcmp(oneblock->get_name(), "Security") == 0)
  45. {
  46. bool encodeXML = oneblock->getEncodeXml();
  47. oneblock->setEncodeXml(false);
  48. StringBuffer username, password,realm;
  49. oneblock->get_value("UsernameToken/Username", username);
  50. oneblock->get_value("UsernameToken/Password", password);
  51. oneblock->get_value("RealmToken/Realm", realm);
  52. oneblock->setEncodeXml(encodeXML);
  53. //DBGLOG("username=%s, password=%s", username.str(), password.str());
  54. if(username.length() > 0)
  55. {
  56. ctx->setUserID(username.str());
  57. ctx->setPassword(password.str());
  58. if(realm.length()>0)
  59. ctx->setRealm(realm.str());
  60. ISecManager* secmgr = ctx->querySecManager();
  61. if(secmgr != NULL)
  62. {
  63. ISecUser *user = ctx->queryUser();
  64. if(user==NULL)
  65. {
  66. user = secmgr->createUser(username.str());
  67. ctx->setUser(user);
  68. }
  69. if(user == NULL)
  70. {
  71. WARNLOG("Couldn't create ISecUser object for %s", username.str());
  72. }
  73. user->setName(username.str());
  74. user->credentials().setPassword(password.str());
  75. if(realm.length()>0)
  76. user->setRealm(realm.str());
  77. }
  78. if(ctx->toBeAuthenticated())
  79. {
  80. if(stricmp(m_soapbinding->getTransportType(), "http") == 0)
  81. {
  82. EspHttpBinding* httpbinding = dynamic_cast<EspHttpBinding*>(m_soapbinding.get());
  83. authenticated = httpbinding->doAuth(ctx);
  84. }
  85. else
  86. {
  87. authenticated = false;
  88. }
  89. if(!authenticated)
  90. returnValue = SOAP_AUTHENTICATION_ERROR;
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. if (returnValue == 0)
  97. {
  98. if (authenticated)
  99. return 0;
  100. returnValue = SOAP_AUTHENTICATION_REQUIRED;
  101. }
  102. StringBuffer peerStr;
  103. ctx->getPeer(peerStr);
  104. const char* userId = ctx->queryUserId();
  105. VStringBuffer msg("SOAP request from %s@%s.", (userId&&*userId)?userId:"unknown", (peerStr.length()>0)?peerStr.str():"unknown");
  106. if (returnValue == SOAP_AUTHENTICATION_ERROR)
  107. msg.append(" User authentication failed");
  108. else
  109. msg.append(" User authentication required");
  110. DBGLOG("%s", msg.str());
  111. return returnValue;
  112. }
  113. int CSoapService::processRequest(ISoapMessage &req, ISoapMessage& resp)
  114. {
  115. CSoapRequest& request = *(dynamic_cast<CSoapRequest*>(&req));
  116. CSoapResponse& response = *(dynamic_cast<CSoapResponse*>(&resp));
  117. IEspContext* ctx = req.queryContext();
  118. StringBuffer requeststr;
  119. Owned<CMimeMultiPart> multipart;
  120. if(Utils::strncasecmp(request.get_content_type(), HTTP_TYPE_SOAP, strlen(HTTP_TYPE_SOAP))==0 || Utils::strncasecmp(request.get_content_type(), HTTP_TYPE_TEXT_XML, strlen(HTTP_TYPE_TEXT_XML))==0)
  121. {
  122. requeststr.append(request.get_text());
  123. }
  124. else if(!Utils::strncasecmp(request.get_content_type(), HTTP_TYPE_MULTIPART_RELATED, strlen(HTTP_TYPE_MULTIPART_RELATED)))
  125. {
  126. multipart.setown(LINK(request.queryMultiPart()));
  127. CMimeBodyPart* rootpart = multipart->queryRootPart();
  128. if(rootpart != NULL)
  129. rootpart->getContent(requeststr);
  130. else
  131. throw MakeStringException(-1, "MultiPart root is NULL");
  132. }
  133. else
  134. {
  135. throw MakeStringException(-1, "Request type %s not supported", request.get_content_type());
  136. }
  137. //Parse the content
  138. std::unique_ptr<XmlPullParser> xpp(new XmlPullParser());
  139. int bufSize = requeststr.length();
  140. xpp->setSupportNamespaces(true);
  141. xpp->setInput(requeststr.str(), bufSize);
  142. int type;
  143. StartTag stag;
  144. EndTag etag;
  145. Owned<CEnvelope> req_envelope;
  146. req_envelope.setown(new CEnvelope);
  147. while((type = xpp->next()) != XmlPullParser::END_DOCUMENT)
  148. {
  149. if(type == XmlPullParser::START_TAG) {
  150. xpp->readStartTag(stag);
  151. if(!stricmp(stag.getLocalName(), SOAP_ENVELOPE_NAME))
  152. {
  153. req_envelope->unmarshall(xpp.get());
  154. break;
  155. }
  156. }
  157. }
  158. CHeader* req_header = req_envelope->get_header();
  159. if(req_header != NULL)
  160. {
  161. // As headers are normally for common uses like authentication and routing, let's process it here
  162. // instead of in binding.
  163. int ret = processHeader(req_header, ctx);
  164. if(ret != 0 )
  165. {
  166. response.set_status(ret);
  167. return 0;
  168. }
  169. }
  170. StringBuffer peerStr;
  171. ctx->getPeer(peerStr);
  172. const char* userId = ctx->queryUserId();
  173. CBody* req_body = req_envelope->get_body();
  174. Owned<CRpcResponse> rpc_response;
  175. rpc_response.setown(new CRpcResponse);
  176. rpc_response->setContext(req.queryContext());
  177. Owned<CRpcCall>rpc_call;
  178. rpc_call.setown(new CRpcCall);
  179. rpc_call->setContext(req.queryContext());
  180. try {
  181. req_body->nextRpcMessage(rpc_call.get());
  182. rpc_call->unmarshall(xpp.get(), multipart.get());
  183. } catch (XmlPullParserException& e) {
  184. response.set_status(SOAP_CLIENT_ERROR);
  185. response.set_err(e.getMessage().c_str());
  186. DBGLOG("SOAP request from %s@%s. Parsing xml error: %s. Offending XML: [%s]", (userId&&*userId)?userId:"unknown",
  187. (peerStr.length()>0)?peerStr.str():"unknown", e.getMessage().c_str(), requeststr.str());
  188. return 0;
  189. } catch (...) {
  190. response.set_status(SOAP_CLIENT_ERROR);
  191. response.set_err("Unknown error when parsing soap body XML");
  192. ERRLOG("SOAP request from %s@%s. Unknown error when parsing: %s", (userId&&*userId)?userId:"unknown",
  193. (peerStr.length()>0)?peerStr.str():"unknown", requeststr.str());
  194. return 0;
  195. }
  196. DBGLOG("SOAP method <%s> from %s@%s.", rpc_call->get_name(), (userId&&*userId)?userId:"unknown",
  197. (peerStr.length()>0)?peerStr.str():"unknown");
  198. ctx->setHTTPMethod("SOAP");
  199. ctx->setServiceMethod(rpc_call->get_name());
  200. // call the rpc and set the response
  201. if(m_soapbinding != NULL)
  202. m_soapbinding->processRequest(rpc_call, rpc_response);
  203. response.set_status(rpc_response->get_status());
  204. response.set_err(rpc_response->get_err());
  205. Owned<CBody> res_body = new CBody;
  206. res_body->add_rpcmessage(rpc_response.get());
  207. Owned<CEnvelope> res_envelope;
  208. res_envelope.setown(new CEnvelope(NULL, res_body.getLink()));
  209. Owned<CMimeMultiPart> resp_multipart;
  210. resp_multipart.setown(new CMimeMultiPart("1.0", "", "MIME_boundary", "text/xml", "soaproot"));
  211. res_envelope->marshall(resp_multipart);
  212. StringBuffer contenttype;
  213. StringBuffer responsestr;
  214. resp_multipart->serialize(contenttype, responsestr);
  215. response.set_content_type(contenttype.str());
  216. response.set_text(responsestr.str());
  217. return 0;
  218. }