soapbind.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 WIN32
  15. #ifdef ESPHTTP_EXPORTS
  16. #define esp_http_decl __declspec(dllexport)
  17. #endif
  18. #endif
  19. //Jlib
  20. #include "jliball.hpp"
  21. //SCM Interfaces
  22. #include "esp.hpp"
  23. //ESP Core
  24. #include "espthread.hpp"
  25. //ESP Bindings
  26. #include "SOAP/Platform/soapbind.hpp"
  27. #include "SOAP/client/soapclient.hpp"
  28. #include "http/platform/httpprot.hpp"
  29. #include "http/platform/httpservice.hpp"
  30. #include "SOAP/Platform/soapservice.hpp"
  31. #ifdef WIN32
  32. #define ESP_FACTORY __declspec(dllexport)
  33. #else
  34. #define ESP_FACTORY
  35. #endif
  36. CSoapBinding::CSoapBinding()
  37. {
  38. }
  39. CSoapBinding::~CSoapBinding()
  40. {
  41. }
  42. //IEspRpcBinding
  43. const char * CSoapBinding::getRpcType()
  44. {
  45. return "soap";
  46. }
  47. const char * CSoapBinding::getTransportType()
  48. {
  49. return "unknown";
  50. }
  51. int CSoapBinding::processRequest(IRpcMessage* rpc_call, IRpcMessage* rpc_response)
  52. {
  53. return 0;
  54. }
  55. CHttpSoapBinding::CHttpSoapBinding():EspHttpBinding(NULL, NULL, NULL)
  56. {
  57. log_level_=hsl_none;
  58. }
  59. CHttpSoapBinding::CHttpSoapBinding(IPropertyTree* cfg, const char *bindname, const char *procname, http_soap_log_level level)
  60. : EspHttpBinding(cfg, bindname, procname)
  61. {
  62. log_level_=level;
  63. }
  64. CHttpSoapBinding::~CHttpSoapBinding()
  65. {
  66. }
  67. const char * CHttpSoapBinding::getTransportType()
  68. {
  69. return "http";
  70. }
  71. static CSoapFault* makeSoapFault(CHttpRequest* request, IMultiException* me, const char *ns)
  72. {
  73. const char* svcName = request->queryServiceName();
  74. if (svcName && *svcName)
  75. {
  76. const char* method = request->queryServiceMethod();
  77. StringBuffer host;
  78. const char* wsdlAddr = request->queryParameters()->queryProp("__wsdl_address");
  79. if (wsdlAddr && *wsdlAddr)
  80. host.append(wsdlAddr);
  81. else
  82. {
  83. host.append(request->queryHost());
  84. if (request->getPort()>0)
  85. host.append(":").append(request->getPort());
  86. }
  87. VStringBuffer ns_ext("xmlns=\"%s\""
  88. " xsi:schemaLocation=\"%s %s/%s/%s?xsd\"",
  89. ns, ns, host.str(), svcName, method ? method : "");
  90. return new CSoapFault(me, ns_ext);
  91. }
  92. return new CSoapFault(me);
  93. }
  94. int CHttpSoapBinding::onSoapRequest(CHttpRequest* request, CHttpResponse* response)
  95. {
  96. Owned<CSoapFault> soapFault;
  97. try
  98. {
  99. return HandleSoapRequest(request,response);
  100. }
  101. catch (IMultiException* mex)
  102. {
  103. StringBuffer ns;
  104. soapFault.setown(makeSoapFault(request,mex, generateNamespace(*request->queryContext(), request, request->queryServiceName(), request->queryServiceMethod(), ns).str()));
  105. //SetHTTPErrorStatus(mex->errorCode(),response);
  106. SetHTTPErrorStatus(500,response);
  107. mex->Release();
  108. }
  109. catch (IException* e)
  110. {
  111. StringBuffer ns;
  112. Owned<IMultiException> mex = MakeMultiException("Esp");
  113. mex->append(*e); // e is owned by mex
  114. soapFault.setown(makeSoapFault(request,mex, generateNamespace(*request->queryContext(), request, request->queryServiceName(), request->queryServiceMethod(), ns).str()));
  115. SetHTTPErrorStatus(500,response);
  116. }
  117. catch (...)
  118. {
  119. soapFault.setown(new CSoapFault(500,"Internal Server Error"));
  120. SetHTTPErrorStatus(500,response);
  121. }
  122. //response->setContentType(soapFault->get_content_type());
  123. response->setContentType(HTTP_TYPE_TEXT_XML_UTF8);
  124. response->setContent(soapFault->get_text());
  125. DBGLOG("Sending SOAP Fault(%u): %s", response->getContentLength(), response->queryContent());
  126. {
  127. EspTimeSection sendtime("send fault [CHttpSoapBinding::onSoapRequest]");
  128. response->send();
  129. }
  130. return -1;
  131. }
  132. int CHttpSoapBinding::HandleSoapRequest(CHttpRequest* request, CHttpResponse* response)
  133. {
  134. ESP_TIME_SECTION("CHttpSoapBinding::onSoapRequest");
  135. StringBuffer requeststr;
  136. request->getContent(requeststr);
  137. if(requeststr.length() == 0)
  138. throw MakeStringException(-1, "Content read is empty");
  139. Owned<CSoapService> soapservice;
  140. Owned<CSoapRequest> soaprequest;
  141. Owned<CSoapResponse> soapresponse;
  142. //soapservice.setown(new CSoapService((IEspSoapBinding*)(this)));
  143. soapservice.setown(new CSoapService(this));
  144. soaprequest.setown(new CSoapRequest);
  145. soaprequest->set_text(requeststr.str());
  146. StringBuffer contenttype;
  147. request->getContentType(contenttype);
  148. soaprequest->set_content_type(contenttype.str());
  149. soaprequest->setContext(request->queryContext());
  150. CMimeMultiPart* multipart = request->queryMultiPart();
  151. if(multipart != NULL)
  152. {
  153. soaprequest->setOwnMultiPart(LINK(multipart));
  154. }
  155. soapresponse.setown(new CSoapResponse);
  156. StringBuffer reqPath;
  157. request->getPath(reqPath);
  158. setRequestPath(reqPath.str());
  159. soapservice->processRequest(*soaprequest.get(), *soapresponse.get());
  160. response->setVersion(HTTP_VERSION);
  161. int status = soapresponse->get_status();
  162. if(status == SOAP_OK)
  163. response->setStatus(HTTP_STATUS_OK);
  164. else if(status == SOAP_SERVER_ERROR || status == SOAP_RPC_ERROR || status == SOAP_CONNECTION_ERROR)
  165. {
  166. StringBuffer msg("Internal Server Error");
  167. const char* detail = soapresponse->get_err();
  168. if (detail && *detail)
  169. msg.appendf(" [%s]", detail);
  170. throw MakeStringExceptionDirect(500, msg.str());
  171. }
  172. else if(status == SOAP_CLIENT_ERROR || status == SOAP_REQUEST_TYPE_ERROR)
  173. {
  174. StringBuffer msg("Bad Request");
  175. const char* detail = soapresponse->get_err();
  176. if (detail && *detail)
  177. msg.appendf(" [%s]", detail);
  178. throw MakeStringExceptionDirect(400, msg.str());
  179. }
  180. else if(status == SOAP_AUTHENTICATION_REQUIRED)
  181. response->sendBasicChallenge(m_challenge_realm.str(), false);
  182. else if(status == SOAP_AUTHENTICATION_ERROR)
  183. {
  184. throw MakeStringExceptionDirect(401,"Unauthorized Access");
  185. }
  186. else
  187. response->setStatus(HTTP_STATUS_OK);
  188. response->setContentType(soapresponse->get_content_type());
  189. response->setContent(soapresponse->get_text());
  190. DBGLOG("Sending SOAP Response(%u)", response->getContentLength());
  191. {
  192. EspTimeSection sendtime("send response [CHttpSoapBinding::HandleSoapRequest]");
  193. response->send();
  194. }
  195. return 0;
  196. }
  197. void CSoapRequestBinding::post(const char *proxy, const char* url, IRpcResponseBinding& response, const char *soapaction)
  198. {
  199. CRpcCall rpccall;
  200. CRpcResponse rpcresponse;
  201. rpccall.set_url(url);
  202. rpccall.setProxy(proxy);
  203. serialize(*static_cast<IRpcMessage*>(&rpccall));
  204. CSoapClient soapclient; //to add support for handling cookies soapclient(false);
  205. soapclient.setUsernameToken(getUserId(), getPassword(), getRealm());
  206. int result = soapclient.postRequest(soapaction, rpccall, rpcresponse);
  207. if(result == SOAP_OK)
  208. {
  209. response.setRpcState(RPC_MESSAGE_OK);
  210. response.unserialize(rpcresponse, NULL, NULL);
  211. }
  212. else if(result == SOAP_CONNECTION_ERROR)
  213. {
  214. response.setRpcState(RPC_MESSAGE_CONNECTION_ERROR);
  215. }
  216. else
  217. {
  218. response.setRpcState(RPC_MESSAGE_ERROR);
  219. }
  220. }
  221. void CSoapComplexType::appendContent(IEspContext* ctx, MemoryBuffer& buffer, StringBuffer& mimetype)
  222. {
  223. StringBuffer content;
  224. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  225. {
  226. const char *jsonp = ctx->queryRequestParameters()->queryProp("jsonp");
  227. if (jsonp && *jsonp)
  228. content.append(jsonp).append('(');
  229. content.append('{');
  230. serializeStruct(ctx, content, (const char *)NULL);
  231. content.append('}');
  232. if (jsonp && *jsonp)
  233. content.append(");");
  234. mimetype.set("application/json; charset=UTF-8");
  235. }
  236. else
  237. {
  238. buffer.append(38, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  239. serializeStruct(ctx, content, (const char *)NULL);
  240. mimetype.set("text/xml; charset=UTF-8");
  241. }
  242. buffer.append(content.length(), content.str());
  243. }
  244. inline void open_element(IEspContext *ctx, StringBuffer &xml, const char *name, const char *uri, const char *prefix)
  245. {
  246. if (!name || !*name)
  247. return;
  248. xml.append("<");
  249. if (prefix && *prefix)
  250. xml.append(prefix).append(':');
  251. xml.append(name);
  252. if (uri && *uri)
  253. {
  254. xml.append("xmlns");
  255. if (prefix && *prefix)
  256. xml.append(':').append(prefix);
  257. xml.append("=\"").append(uri).append('\"');
  258. }
  259. }
  260. inline void start_child_attributes(IEspContext *ctx, StringBuffer &xml, const char *name)
  261. {
  262. }
  263. inline void start_child_elements(IEspContext *ctx, StringBuffer &xml, const char *name)
  264. {
  265. if (!name || !*name)
  266. return;
  267. xml.append('>');
  268. }
  269. inline void close_element(IEspContext *ctx, StringBuffer &xml, const char *name, const char *prefix)
  270. {
  271. if (!name || !*name)
  272. return;
  273. xml.append("</");
  274. if (prefix && *prefix)
  275. xml.append(prefix).append(':');
  276. xml.append(name).append('>');
  277. }
  278. void CSoapComplexType::serializeJSONStruct(IEspContext* ctx, StringBuffer& s, const char *name)
  279. {
  280. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  281. {
  282. appendJSONNameOrDelimit(s, name);
  283. s.append("{");
  284. serializeContent(ctx, s);
  285. s.append("}");
  286. return;
  287. }
  288. }
  289. void CSoapComplexType::serializeStruct(IEspContext* ctx, StringBuffer& s, const char *name)
  290. {
  291. const char *tag = (name && *name) ? name : getRootName();
  292. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  293. return serializeJSONStruct(ctx, s, tag);
  294. open_element(ctx, s, tag, getNsURI(), getNsPrefix());
  295. start_child_attributes(ctx, s, name);
  296. serializeAttributes(ctx, s);
  297. start_child_elements(ctx, s, tag);
  298. serializeContent(ctx, s);
  299. close_element(ctx, s, tag, getNsPrefix());
  300. }
  301. void CSoapComplexType::serializeItem(IEspContext* ctx, StringBuffer& s, const char *name)
  302. {
  303. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  304. return serializeJSONStruct(ctx, s, NULL);
  305. serializeStruct(ctx, s, name);
  306. }
  307. void CSoapResponseBinding::handleExceptions(IMultiException *me, const char *serv, const char *meth)
  308. {
  309. if (me->ordinality() > 0)
  310. {
  311. StringBuffer text;
  312. me->errorMessage(text);
  313. text.append('\n');
  314. WARNLOG("Exception(s) in %s::%s - %s", serv, meth, text.str());
  315. IArrayOf<IException>& exceptions = me->getArray();
  316. ForEachItemIn(i, exceptions)
  317. noteException(*LINK(&exceptions.item(i)));
  318. }
  319. }
  320. void SetHTTPErrorStatus(int ErrorCode,CHttpResponse* response)
  321. {
  322. switch(ErrorCode)
  323. {
  324. case 204:
  325. response->setStatus(HTTP_STATUS_NO_CONTENT);
  326. break;
  327. case 301:
  328. response->setStatus(HTTP_STATUS_MOVED_PERMANENTLY);
  329. break;
  330. case 302:
  331. response->setStatus(HTTP_STATUS_REDIRECT);
  332. break;
  333. case 303:
  334. response->setStatus(HTTP_STATUS_REDIRECT_POST);
  335. break;
  336. case 400:
  337. response->setStatus(HTTP_STATUS_BAD_REQUEST);
  338. break;
  339. case 401:
  340. response->setStatus(HTTP_STATUS_UNAUTHORIZED);
  341. break;
  342. case 403:
  343. response->setStatus(HTTP_STATUS_FORBIDDEN);
  344. break;
  345. case 404:
  346. response->setStatus(HTTP_STATUS_NOT_FOUND);
  347. break;
  348. case 405:
  349. response->setStatus(HTTP_STATUS_NOT_ALLOWED);
  350. break;
  351. case 501:
  352. response->setStatus(HTTP_STATUS_NOT_IMPLEMENTED);
  353. break;
  354. default:
  355. response->setStatus(HTTP_STATUS_INTERNAL_SERVER_ERROR);
  356. }
  357. }