espcontext.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. #include "jliball.hpp"
  20. #include "espcontext.hpp"
  21. #include "txsummary.hpp"
  22. #include "http/platform/httptransport.ipp"
  23. #include "sechandler.hpp"
  24. #include "espprotocol.hpp"
  25. #include "espsecurecontext.hpp"
  26. class CEspContext : public CInterface, implements IEspContext
  27. {
  28. private:
  29. StringAttr m_userid;
  30. StringAttr m_password;
  31. StringAttr m_realm;
  32. StringAttr m_path;
  33. StringAttr m_peer;
  34. StringAttr m_useragent;
  35. StringAttr m_acceptLanguage;
  36. StringAttr httpMethod;
  37. StringAttr servMethod;
  38. StringBuffer m_servName;
  39. StringBuffer m_servHost;
  40. short m_servPort;
  41. Owned<ISecUser> m_user;
  42. Owned<ISecResourceList> m_resources;
  43. Owned<ISecManager> m_secmgr;
  44. Owned<IAuthMap> m_feature_authmap;
  45. Owned<ISecPropertyList> m_sec_settings;
  46. void *m_bindingValue;
  47. void *m_serviceValue;
  48. bool m_toBeAuthenticated;
  49. double m_clientVer;
  50. Owned<IProperties> m_queryparams;
  51. Owned<IProperties> xslParameters;
  52. Owned<IMapInfo> m_mapinfo;
  53. StringArray m_custom_headers;
  54. unsigned options;
  55. SecHandler m_SecurityHandler;
  56. BoolHash m_optGroups;
  57. Owned<CTxSummary> m_txSummary;
  58. unsigned m_active;
  59. unsigned m_creationTime;
  60. unsigned m_processingTime;
  61. unsigned m_exceptionTime;
  62. bool m_hasException;
  63. int m_exceptionCode;
  64. ESPSerializationFormat respSerializationFormat;
  65. Owned<IEspSecureContext> m_secureContext;
  66. StringAttr m_transactionID;
  67. public:
  68. IMPLEMENT_IINTERFACE;
  69. CEspContext(IEspSecureContext* secureContext)
  70. : m_servPort(0)
  71. , m_bindingValue(0)
  72. , m_serviceValue(0)
  73. , m_toBeAuthenticated(false)
  74. , m_clientVer(-1)
  75. , options(0)
  76. , m_active(ActiveRequests::getCount())
  77. , m_creationTime(msTick())
  78. , m_processingTime(0)
  79. , m_exceptionTime(0)
  80. , m_hasException(false)
  81. , m_exceptionCode(0)
  82. , respSerializationFormat(ESPSerializationANY)
  83. {
  84. m_txSummary.setown(new CTxSummary(m_creationTime));
  85. updateTraceSummaryHeader();
  86. m_secureContext.setown(secureContext);
  87. m_SecurityHandler.setSecureContext(secureContext);
  88. }
  89. ~CEspContext()
  90. {
  91. flushTraceSummary();
  92. }
  93. virtual void addOptions(unsigned opts){options|=opts;}
  94. virtual void removeOptions(unsigned opts){opts&=~opts;}
  95. virtual unsigned queryOptions(){return options;}
  96. // versioning
  97. virtual double getClientVersion(){return m_clientVer;}
  98. virtual void setClientVersion(double ver){m_clientVer=ver;}
  99. virtual bool checkMinVer(double minVer) { return m_clientVer<0 || m_clientVer >= minVer; }
  100. virtual bool checkMaxVer(double maxVer) { return m_clientVer<0 || m_clientVer <= maxVer; }
  101. virtual bool checkMinMaxVer(double minVer, double maxVer) { return m_clientVer<0 || m_clientVer>= minVer || m_clientVer <= maxVer; }
  102. virtual bool checkOptional(const char* option) { return m_queryparams.get() && m_queryparams->hasProp(option); }
  103. virtual bool isMethodAllowed(double version, const char* optional, const char* security, double maxver, double minver);
  104. virtual IMapInfo& queryMapInfo()
  105. {
  106. if (!m_mapinfo.get())
  107. m_mapinfo.setown(createMapInfo());
  108. return *m_mapinfo.get();
  109. }
  110. virtual bool suppressed(const char* structName, const char* fieldName);
  111. virtual void addOptGroup(const char* optGroup) { if (optGroup) m_optGroups.setValue(optGroup,true); }
  112. virtual BoolHash& queryOptGroups() { return m_optGroups; }
  113. virtual void setUserID(const char* userid)
  114. {
  115. m_userid.set(userid);
  116. }
  117. virtual StringBuffer& getUserID(StringBuffer& userid)
  118. {
  119. userid.append(m_userid.get());
  120. return userid;
  121. }
  122. virtual const char * queryUserId()
  123. {
  124. return m_userid.get();
  125. }
  126. virtual void setPassword(const char* password)
  127. {
  128. m_password.set(password);
  129. }
  130. virtual StringBuffer& getPassword(StringBuffer& password)
  131. {
  132. password.append(m_password.get());
  133. return password;
  134. }
  135. virtual const char * queryPassword()
  136. {
  137. return m_password.get();
  138. }
  139. virtual void setRealm(const char* realm)
  140. {
  141. m_realm.set(realm);
  142. }
  143. virtual StringBuffer& getRealm(StringBuffer& realm)
  144. {
  145. realm.append(m_realm.get());
  146. return realm;
  147. }
  148. virtual const char * queryRealm()
  149. {
  150. return m_realm.get();
  151. }
  152. virtual void setContextPath(const char* path)
  153. {
  154. m_path.set(path);
  155. }
  156. virtual const char * getContextPath()
  157. {
  158. return m_path.get();
  159. }
  160. virtual void setUser(ISecUser* user)
  161. {
  162. m_user.setown(user);
  163. m_SecurityHandler.setUser(user);
  164. }
  165. virtual ISecUser* queryUser()
  166. {
  167. return m_user.get();
  168. }
  169. virtual void setServiceName(const char *name)
  170. {
  171. m_servName.clear().append(name).toLowerCase();
  172. }
  173. virtual const char * queryServiceName(const char *name)
  174. {
  175. return m_servName.str();
  176. }
  177. virtual const unsigned queryCreationTime()
  178. {
  179. return m_creationTime;
  180. }
  181. virtual void setProcessingTime()
  182. {
  183. m_processingTime = msTick() - m_creationTime;
  184. }
  185. virtual const unsigned queryProcessingTime()
  186. {
  187. return m_processingTime;
  188. }
  189. virtual void setException(int exceptionCode)
  190. {
  191. m_hasException = true;
  192. m_exceptionCode = exceptionCode;
  193. m_exceptionTime = msTick() - m_creationTime;
  194. }
  195. virtual const bool queryException(int& exceptionCode, unsigned& exceptionTime)
  196. {
  197. if (m_hasException)
  198. {
  199. exceptionCode = m_exceptionCode;
  200. exceptionTime = m_exceptionTime;
  201. }
  202. return m_hasException;
  203. }
  204. virtual const bool queryHasException()
  205. {
  206. return m_hasException;
  207. }
  208. virtual void setResources(ISecResourceList* rlist)
  209. {
  210. m_resources.setown(rlist);
  211. m_SecurityHandler.setResources(rlist);
  212. }
  213. virtual ISecResourceList* queryResources()
  214. {
  215. return m_resources.get();
  216. }
  217. virtual void setSecManger(ISecManager* mgr)
  218. {
  219. m_secmgr.setown(mgr);
  220. m_SecurityHandler.setSecManger(mgr);
  221. }
  222. virtual ISecManager* querySecManager()
  223. {
  224. return m_secmgr.get();
  225. }
  226. virtual void setBindingValue(void * value)
  227. {
  228. m_bindingValue=value;
  229. }
  230. virtual void * getBindingValue()
  231. {
  232. return m_bindingValue;
  233. }
  234. virtual void setServiceValue(void * value)
  235. {
  236. m_serviceValue=value;
  237. }
  238. virtual void * getServiceValue()
  239. {
  240. return m_serviceValue;
  241. }
  242. virtual void setToBeAuthenticated(bool val)
  243. {
  244. m_toBeAuthenticated = val;
  245. }
  246. virtual bool toBeAuthenticated()
  247. {
  248. return m_toBeAuthenticated;
  249. }
  250. virtual void setPeer(const char* peer)
  251. {
  252. m_peer.set(peer);
  253. }
  254. virtual StringBuffer& getPeer(StringBuffer& peer)
  255. {
  256. peer.append(m_peer.get());
  257. return peer;
  258. }
  259. virtual void setUseragent(const char* useragent)
  260. {
  261. if(useragent && *useragent)
  262. m_useragent.set(useragent);
  263. }
  264. virtual StringBuffer& getUseragent(StringBuffer& useragent)
  265. {
  266. const char* agent = m_useragent.get();
  267. if(agent && *agent)
  268. useragent.append(m_useragent.get());
  269. return useragent;
  270. }
  271. virtual void setAcceptLanguage(const char* acceptLanguage)
  272. {
  273. if(acceptLanguage && *acceptLanguage)
  274. m_acceptLanguage.set(acceptLanguage);
  275. }
  276. virtual StringBuffer& getAcceptLanguage(StringBuffer& acceptLanguage)
  277. {
  278. const char* acceptLang = m_acceptLanguage.get();
  279. if(acceptLang && *acceptLang)
  280. acceptLanguage.set(m_acceptLanguage.get());
  281. return acceptLanguage;
  282. }
  283. virtual IProperties * queryRequestParameters()
  284. {
  285. if (!m_queryparams)
  286. m_queryparams.setown(createProperties(false));
  287. return m_queryparams.get();
  288. }
  289. virtual void setRequestParameters(IProperties * Parameters)
  290. {
  291. m_queryparams.set(Parameters);
  292. }
  293. virtual void setServAddress(const char * host, short port)
  294. {
  295. m_servHost.clear().append(host);
  296. m_servPort = port;
  297. }
  298. virtual void getServAddress(StringBuffer & host, short & port)
  299. {
  300. host.append(m_servHost);
  301. port = m_servPort;
  302. }
  303. virtual void setFeatureAuthMap(IAuthMap * map)
  304. {
  305. if(map != NULL)
  306. {
  307. m_feature_authmap.setown(map);
  308. m_SecurityHandler.setFeatureAuthMap(map);
  309. }
  310. }
  311. virtual IAuthMap * queryAuthMap()
  312. {
  313. return m_feature_authmap.get();
  314. }
  315. virtual void setSecuritySettings(ISecPropertyList* slist)
  316. {
  317. m_sec_settings.setown(slist);
  318. }
  319. virtual ISecPropertyList* querySecuritySettings()
  320. {
  321. return m_sec_settings.get();
  322. }
  323. virtual bool authorizeFeatures(StringArray & features, IEspStringIntMap & pmap)
  324. {
  325. return m_SecurityHandler.authorizeSecReqFeatures(features, pmap, NULL);
  326. }
  327. virtual bool authorizeFeature(const char * pszFeatureUrl, const char* UserID, const char* CompanyID, SecAccessFlags & access)
  328. {
  329. SecUserStatus user_status;
  330. return m_SecurityHandler.authorizeSecFeature(pszFeatureUrl, UserID, CompanyID, access,false,0, user_status);
  331. }
  332. virtual bool authorizeFeature(const char * pszFeatureUrl, const char* UserID, const char* CompanyID, SecAccessFlags & access,bool bCheckTrial,int DebitUnits, SecUserStatus& user_status)
  333. {
  334. return m_SecurityHandler.authorizeSecFeature(pszFeatureUrl, UserID, CompanyID, access,bCheckTrial, DebitUnits, user_status);
  335. }
  336. virtual bool authorizeFeature(const char* pszFeatureUrl, SecAccessFlags& access)
  337. {
  338. return m_SecurityHandler.authorizeSecFeature(pszFeatureUrl, access);
  339. }
  340. virtual bool validateFeaturesAccess(MapStringTo<SecAccessFlags> & pmap, bool throwExcpt)
  341. {
  342. return m_SecurityHandler.validateSecFeaturesAccess(pmap, throwExcpt);
  343. }
  344. virtual bool validateFeatureAccess(const char* pszFeatureUrl, unsigned required, bool throwExcpt)
  345. {
  346. return m_SecurityHandler.validateSecFeatureAccess(pszFeatureUrl, required, throwExcpt);
  347. }
  348. void AuditMessage(AuditType type, const char *filterType, const char *title, const char *parms, ...) __attribute__((format(printf, 5, 6)));
  349. void AuditMessage(AuditType type, const char *filterType, const char *title);
  350. IProperties * queryXslParameters()
  351. {
  352. if (!xslParameters)
  353. xslParameters.setown(createProperties(false));
  354. return xslParameters.get();
  355. }
  356. StringArray& queryCustomHeaders()
  357. {
  358. return m_custom_headers;
  359. }
  360. void addCustomerHeader(const char* name, const char* val)
  361. {
  362. if(!name || !*name)
  363. throw MakeStringException(-1, "Header name can't be empty");
  364. m_custom_headers.append(StringBuffer(name).appendf(": %s", val?val:"").str());
  365. }
  366. virtual void setHTTPMethod(const char *method)
  367. {
  368. httpMethod.set(method);
  369. }
  370. virtual void setServiceMethod(const char *method)
  371. {
  372. servMethod.set(method);
  373. }
  374. virtual CTxSummary* queryTxSummary()
  375. {
  376. return m_txSummary.get();
  377. }
  378. virtual void addTraceSummaryValue(LogLevel logLevel, const char *name, const char *value)
  379. {
  380. if (m_txSummary && (getTxSummaryLevel() >= logLevel))
  381. m_txSummary->append(name, value);
  382. }
  383. virtual void addTraceSummaryValue(LogLevel logLevel, const char *name, __int64 value)
  384. {
  385. if (m_txSummary && (getTxSummaryLevel() >= logLevel))
  386. m_txSummary->append(name, value);
  387. }
  388. virtual void addTraceSummaryTimeStamp(LogLevel logLevel, const char *name)
  389. {
  390. if (m_txSummary && (getTxSummaryLevel() >= logLevel) && name && *name)
  391. m_txSummary->append(name, m_txSummary->getElapsedTime(), "ms");
  392. }
  393. virtual void flushTraceSummary()
  394. {
  395. updateTraceSummaryHeader();
  396. if (m_txSummary && (getTxSummaryLevel() >= LogMin))
  397. m_txSummary->append("total", m_processingTime, "ms");
  398. }
  399. virtual void addTraceSummaryCumulativeTime(LogLevel logLevel, const char* name, unsigned __int64 time)
  400. {
  401. if (m_txSummary && (getTxSummaryLevel() >= logLevel))
  402. m_txSummary->updateTimer(name, time);
  403. }
  404. virtual CumulativeTimer* queryTraceSummaryCumulativeTimer(const char* name)
  405. {
  406. return (m_txSummary ? m_txSummary->queryTimer(name) : NULL);
  407. }
  408. virtual void cancelTxSummary()
  409. {
  410. if (!m_txSummary)
  411. return;
  412. m_txSummary->clear();
  413. m_txSummary.clear();
  414. }
  415. virtual ESPSerializationFormat getResponseFormat(){return respSerializationFormat;}
  416. virtual void setResponseFormat(ESPSerializationFormat fmt){respSerializationFormat = fmt;}
  417. void updateTraceSummaryHeader();
  418. IEspSecureContext* querySecureContext() override
  419. {
  420. return m_secureContext.get();
  421. }
  422. virtual void setTransactionID(const char * trxid)
  423. {
  424. m_transactionID.set(trxid);
  425. }
  426. virtual const char * queryTransactionID()
  427. {
  428. return m_transactionID.get();
  429. }
  430. };
  431. //---------------------------------------------------------
  432. // implementations
  433. void CEspContext::AuditMessage(AuditType type, const char *filterType, const char *title, const char *parms, ...)
  434. {
  435. va_list args;
  436. va_start(args, parms);
  437. StringBuffer msg(title);
  438. msg.appendf("\n\tProcess: esp\n\tService: %s\n\tUser: %s", m_servName.str(), queryUserId());
  439. if (parms)
  440. msg.append("\n\t").valist_appendf(parms, args);
  441. va_end(args);
  442. AUDIT(type, msg.str());
  443. }
  444. void CEspContext::AuditMessage(AuditType type, const char *filterType, const char *title)
  445. {
  446. VStringBuffer msg("%s\n\tProcess: esp\n\tService: %s\n\tUser: %s", title, m_servName.str(), queryUserId());
  447. AUDIT(type, msg.str());
  448. }
  449. bool CEspContext::suppressed(const char* structName, const char* fieldName)
  450. {
  451. if (!m_mapinfo)
  452. return false;
  453. double ver = getClientVersion();
  454. double minver = m_mapinfo->getMinVersion(structName,fieldName);
  455. if (minver>0 && ver<minver)
  456. return true;
  457. double deprver = m_mapinfo->getDeprVersion(structName,fieldName);
  458. if (deprver>0)
  459. {
  460. if (ver>=deprver)
  461. return true;
  462. }
  463. else
  464. {
  465. double maxver = m_mapinfo->getMaxVersion(structName,fieldName);
  466. if (maxver>0 && ver>maxver)
  467. return true;
  468. }
  469. const char* optional = m_mapinfo->getOptional(structName,fieldName);
  470. if (optional)
  471. return !queryRequestParameters()->hasProp(optional);
  472. return false;
  473. }
  474. bool CEspContext::isMethodAllowed(double version, const char* optional, const char* security, double minver, double maxver)
  475. {
  476. if (optional)
  477. {
  478. IProperties *props = queryRequestParameters();
  479. if (props && !props->hasProp(optional))
  480. return false;
  481. }
  482. if (security)
  483. {
  484. SecAccessFlags acc;
  485. if (!authorizeFeature(security, acc) || (acc==SecAccess_None))
  486. return false;
  487. }
  488. if (minver>0 && version<minver)
  489. return false;
  490. if (maxver>0 && version>maxver)
  491. return false;
  492. return true;
  493. }
  494. void CEspContext::updateTraceSummaryHeader()
  495. {
  496. if (m_txSummary && (getTxSummaryLevel() >= LogMin))
  497. {
  498. m_txSummary->set("activeReqs", m_active);
  499. VStringBuffer user("%s%s%s", (queryUserId() ? queryUserId() : ""), (m_peer.length() ? "@" : ""), m_peer.str());
  500. if (!user.isEmpty())
  501. m_txSummary->set("user", user.str());
  502. VStringBuffer reqSummary("%s", httpMethod.isEmpty() ? "" : httpMethod.get());
  503. if (!m_servName.isEmpty() || !servMethod.isEmpty())
  504. {
  505. if (!reqSummary.isEmpty())
  506. reqSummary.append(" ");
  507. if (!m_servName.isEmpty())
  508. reqSummary.append(m_servName.str());
  509. if (!servMethod.isEmpty())
  510. reqSummary.append(".").append(servMethod.str());
  511. }
  512. if (m_clientVer > 0)
  513. {
  514. if (!reqSummary.isEmpty())
  515. reqSummary.append(" ");
  516. reqSummary.append("v").append(m_clientVer);
  517. }
  518. if (!reqSummary.isEmpty())
  519. m_txSummary->set("req", reqSummary.str());
  520. if (m_hasException)
  521. m_txSummary->set(VStringBuffer("exception@%ums", m_exceptionTime), m_exceptionCode);
  522. }
  523. }
  524. IEspContext* createEspContext(IEspSecureContext* secureContext)
  525. {
  526. return new CEspContext(secureContext);
  527. }
  528. bool getUrlParams(IProperties *props, StringBuffer& params)
  529. {
  530. bool hasVersion = false;
  531. if (props) {
  532. Owned<IPropertyIterator> it = props->getIterator();
  533. for (it->first(); it->isValid(); it->next()) {
  534. const char* key = it->getPropKey();
  535. if (!key || !*key || stricmp(key,"form")==0 || stricmp(key,"__querystring")==0)
  536. continue;
  537. if (params.length()==0)
  538. params.append("?");
  539. else
  540. params.append("&");
  541. params.append(key);
  542. if (stricmp(key,"ver_")==0)
  543. hasVersion = true;
  544. const char* v = props->queryProp(key);
  545. if (v && *v)
  546. params.appendf("=%s",v);
  547. }
  548. }
  549. return hasVersion;
  550. }
  551. void getEspUrlParams(IEspContext& ctx, StringBuffer& params, const char* excludeParams[])
  552. {
  553. bool hasVersion = false, addAmpersand = false;
  554. int excludes = 0;
  555. if (excludeParams)
  556. while (excludeParams[excludes]) excludes++;
  557. IProperties* props = ctx.queryRequestParameters();
  558. if (props)
  559. {
  560. const char* querystr = props->queryProp("__querystring");
  561. if (querystr)
  562. {
  563. StringArray ps;
  564. ps.appendListUniq(querystr, "&");
  565. for (unsigned int i=0; i<ps.ordinality(); i++)
  566. {
  567. const char* item = ps.item(i);
  568. const char* eq = strchr(item,'=');
  569. StringAttr key;
  570. if (eq)
  571. key.set(item, eq-item);
  572. else
  573. key.set(item);
  574. bool excluded = false;
  575. if (*key.get()=='.')
  576. excluded = true;
  577. else for (int i=0; i<excludes; i++)
  578. {
  579. if (stricmp(excludeParams[i],key.get())==0)
  580. {
  581. excluded = true;
  582. break;
  583. }
  584. }
  585. if (!excluded)
  586. {
  587. if (addAmpersand)
  588. params.append('&');
  589. else
  590. addAmpersand = true;
  591. params.append(item);
  592. }
  593. if (stricmp(key,"ver_")==0)
  594. hasVersion = true;
  595. }
  596. }
  597. }
  598. if (!hasVersion)
  599. params.appendf("%sver_=%g", addAmpersand?"&":"", ctx.getClientVersion());
  600. }
  601. void addEspNativeArray(StringBuffer& schema, const char* xsdType, const char* arrayType)
  602. {
  603. schema.appendf("<xsd:complexType name=\"%s\">"
  604. "<xsd:sequence>"
  605. "<xsd:element name=\"Item\" type=\"xsd:%s\" minOccurs=\"0\" maxOccurs=\"unbounded\" />"
  606. "</xsd:sequence>"
  607. "</xsd:complexType>\n", arrayType, xsdType);
  608. }
  609. void checkRequest(IEspContext& ctx)
  610. {
  611. #ifdef ENABLE_NEW_SECURITY
  612. ISecUser* user = ctx.queryUser();
  613. if (user && user->getStatus()!=SecUserStatus_Unknown) // no user means security is not configured
  614. {
  615. BoolHash& groups = ctx.queryOptGroups();
  616. if (groups.find("internal"))
  617. {
  618. if(user->getStatus()!=SecUserStatus_Inhouse)
  619. {
  620. ERRLOG("User %s trying to access unauthorized feature: internal", user->getName() ? user->getName() : ctx.queryUserId());
  621. throw MakeStringException(400,"Bad request");
  622. }
  623. }
  624. }
  625. #elif !defined(DISABLE_NEW_SECURITY)
  626. #error Please include esphttp.hpp in this file.
  627. #endif
  628. }
  629. //--------------------------------
  630. // log level
  631. static IEspContainer*& getContainer()
  632. {
  633. static IEspContainer* gContainer = NULL;
  634. // printf("Container: %p\n", gContainer);
  635. return gContainer;
  636. }
  637. LogLevel getEspLogLevel() { return getEspLogLevel(NULL); }
  638. LogLevel getEspLogLevel(IEspContext* ctx)
  639. {
  640. if (ctx)
  641. {
  642. ISecPropertyList* properties = ctx->querySecuritySettings();
  643. if (properties)
  644. {
  645. ISecProperty* sec = properties->findProperty("DebugMode");
  646. if (sec)
  647. {
  648. const char* mode = sec->getValue();
  649. if ( mode && (streq(mode,"1") || streq(mode, "true")) )
  650. return LogMax;
  651. }
  652. }
  653. }
  654. if (getContainer())
  655. return getContainer()->getLogLevel();
  656. return LogMin;
  657. }
  658. LogLevel getTxSummaryLevel()
  659. {
  660. if (getContainer())
  661. return getContainer()->getTxSummaryLevel();
  662. return LogMin;
  663. }
  664. bool getTxSummaryResourceReq()
  665. {
  666. if (getContainer())
  667. return getContainer()->getTxSummaryResourceReq();
  668. return false;
  669. }
  670. bool getEspLogRequests()
  671. {
  672. if (getContainer())
  673. return getContainer()->getLogRequests();
  674. return false;
  675. }
  676. bool getEspLogResponses()
  677. {
  678. if (getContainer())
  679. return getContainer()->getLogResponses();
  680. return false;
  681. }
  682. unsigned getSlowProcessingTime()
  683. {
  684. if (getContainer())
  685. return getContainer()->getSlowProcessingTime();
  686. return false;
  687. }
  688. void ESPLOG(LogLevel level, const char* fmt, ...)
  689. {
  690. if (getEspLogLevel(NULL)>=level)
  691. {
  692. va_list args;
  693. va_start(args,fmt);
  694. VALOG(MCdebugInfo, unknownJob, fmt, args);
  695. va_end(args);
  696. }
  697. }
  698. void ESPLOG(IEspContext* ctx, LogLevel level, const char* fmt, ...)
  699. {
  700. if (getEspLogLevel(ctx)>=level)
  701. {
  702. va_list args;
  703. va_start(args,fmt);
  704. VALOG(MCdebugInfo, unknownJob, fmt, args);
  705. va_end(args);
  706. }
  707. }
  708. void setEspContainer(IEspContainer* container)
  709. {
  710. getContainer() = container;
  711. }
  712. IEspContainer* getESPContainer()
  713. {
  714. return getContainer();
  715. }
  716. static StringBuffer g_cfd;
  717. void setCFD(const char* cfd)
  718. {
  719. g_cfd.clear();
  720. if(cfd&&*cfd)
  721. g_cfd.append(cfd);
  722. g_cfd.trim();
  723. if (g_cfd.length())
  724. makeAbsolutePath(g_cfd, true);
  725. if (g_cfd.length())
  726. {
  727. char lastChar = g_cfd.charAt(g_cfd.length() - 1);
  728. if(lastChar != PATHSEPCHAR && lastChar != '/')
  729. g_cfd.append(PATHSEPCHAR);
  730. }
  731. }
  732. const char* getCFD()
  733. {
  734. return g_cfd.str();
  735. }
  736. static StringBuffer g_buildVersion;
  737. void setBuildVersion(const char* buildVersion)
  738. {
  739. g_buildVersion.clear();
  740. if(buildVersion&&*buildVersion)
  741. g_buildVersion.append(buildVersion);
  742. g_buildVersion.trim();
  743. }
  744. const char* getBuildVersion()
  745. {
  746. return g_buildVersion.str();
  747. }
  748. static StringBuffer g_buildLevel;
  749. void setBuildLevel(const char* buildLevel)
  750. {
  751. g_buildLevel.clear();
  752. if(buildLevel&&*buildLevel)
  753. g_buildLevel.append(buildLevel);
  754. g_buildLevel.trim();
  755. }
  756. const char* getBuildLevel()
  757. {
  758. return g_buildLevel.str();
  759. }