espprotocol.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef _ESPPROTOCOL_HPP__
  14. #define _ESPPROTOCOL_HPP__
  15. #include "esphttp.hpp"
  16. //Jlib
  17. #include "jliball.hpp"
  18. //SCM Interfaces
  19. #include "esp.hpp"
  20. #include "xslprocessor.hpp"
  21. #include "persistent.hpp"
  22. //STL
  23. #include <algorithm>
  24. #include <string>
  25. #include <map>
  26. using namespace std;
  27. class ActiveRequests
  28. {
  29. public:
  30. ActiveRequests() { inc(); }
  31. ~ActiveRequests() { dec(); }
  32. void inc();
  33. void dec();
  34. static long getCount();
  35. };
  36. class CEspBindingEntry : public CInterface, implements IInterface
  37. {
  38. private:
  39. Owned<ISocket> sock_;
  40. Owned<IEspRpcBinding> binding_;
  41. public:
  42. IMPLEMENT_IINTERFACE;
  43. CEspBindingEntry(ISocket *sock, IEspRpcBinding *binding)
  44. {
  45. sock->Link();
  46. sock_.set(sock);
  47. binding_.set(binding);
  48. }
  49. virtual ~CEspBindingEntry()
  50. {
  51. sock_.clear();
  52. binding_.clear();
  53. }
  54. unsigned getId()
  55. {
  56. if (sock_)
  57. return sock_->OShandle();
  58. return 0;
  59. }
  60. IEspRpcBinding *queryBinding()
  61. {
  62. return binding_.get();
  63. }
  64. };
  65. class CEspProtocol;
  66. class CEspApplicationPort
  67. {
  68. IArrayOf<CEspBindingEntry> bindings;
  69. CEspBindingEntry* defBinding = nullptr;
  70. StringBuffer titleBarHtml;
  71. StringBuffer appFrameHtml;
  72. const char *build_ver;
  73. bool viewConfig;
  74. bool rootAuth;
  75. bool navResize;
  76. bool navScroll;
  77. int navWidth;
  78. HINSTANCE hxsl;
  79. Owned<IXslProcessor> xslp;
  80. CEspProtocol* protocol = nullptr;
  81. ReadWriteLock rwLock;
  82. public:
  83. CEspApplicationPort(bool viewcfg, CEspProtocol* prot);
  84. ~CEspApplicationPort()
  85. {
  86. if (hxsl)
  87. FreeSharedObject(hxsl);
  88. }
  89. const StringBuffer &getAppFrameHtml(time_t &modified, const char *inner_url, StringBuffer &html, IEspContext* ctx);
  90. const StringBuffer &getTitleBarHtml(IEspContext& ctx, bool rawXml);
  91. const StringBuffer &getNavBarContent(IEspContext &context, StringBuffer &content, StringBuffer &contentType, bool xml);
  92. const StringBuffer &getDynNavData(IEspContext &context, IProperties *params, StringBuffer &content,
  93. StringBuffer &contentType, bool& bVolatile);
  94. void buildNavTreeXML(IPropertyTree* navtree, StringBuffer& xmlBuf, bool insideFolder = false);
  95. int onGetNavEvent(IEspContext &context, IHttpMessage* request, IHttpMessage* response);
  96. int onBuildSoapRequest(IEspContext &context, IHttpMessage* request, IHttpMessage* response);
  97. int getBindingCount(){return bindings.length();}
  98. void appendBinding(CEspBindingEntry* entry, bool isdefault);
  99. void removeBinding(IEspRpcBinding* binding);
  100. bool rootAuthRequired(){return rootAuth;}
  101. CEspBindingEntry* queryBindingItem(int item)
  102. {
  103. ReadLockBlock rblock(rwLock);
  104. return (item<bindings.length()) ? &bindings.item(item) : nullptr;
  105. }
  106. CEspBindingEntry* getDefaultBinding()
  107. {
  108. ReadLockBlock rblock(rwLock);
  109. if (defBinding)
  110. return defBinding;
  111. if (bindings.length() > 0)
  112. return &bindings.item(0);
  113. return nullptr;
  114. }
  115. CEspProtocol* queryProtocol() { return protocol; }
  116. #ifdef _USE_OPENLDAP
  117. unsigned updatePassword(IEspContext &context, IHttpMessage* request, StringBuffer& message);
  118. void onUpdatePasswordInput(IEspContext &context, StringBuffer &html);
  119. unsigned onUpdatePassword(IEspContext &context, IHttpMessage* request, StringBuffer& html);
  120. #endif
  121. };
  122. typedef map<int, CEspApplicationPort*> CApplicationPortMap;
  123. #define DEFAULT_MAX_REQUEST_ENTITY_LENGTH 8000000
  124. class esp_http_decl CEspProtocol : public CInterface,
  125. implements IEspProtocol,
  126. implements ISocketSelectNotify,
  127. implements IPersistentSelectNotify
  128. {
  129. private:
  130. //map between socket port and one or more bindings
  131. CApplicationPortMap m_portmap;
  132. bool m_viewConfig;
  133. int m_MaxRequestEntityLength;
  134. IEspContainer *m_container;
  135. unsigned m_nextSeq;
  136. Owned<IPersistentHandler> m_persistentHandler;
  137. ReadWriteLock rwLock;
  138. public:
  139. IMPLEMENT_IINTERFACE;
  140. void beforeDispose()
  141. {
  142. }
  143. CEspProtocol();
  144. virtual ~CEspProtocol();
  145. void clear()
  146. {
  147. WriteLockBlock wblock(rwLock);
  148. map<int, CEspApplicationPort*>::iterator bndi = m_portmap.begin();
  149. for(;bndi!=m_portmap.end();bndi++)
  150. if(bndi->second)
  151. delete bndi->second;
  152. m_portmap.clear();
  153. }
  154. void clearBindingMap()
  155. {
  156. clear();
  157. }
  158. void setViewConfig(bool viewConfig){m_viewConfig=viewConfig;}
  159. bool getViewConfig(){return m_viewConfig;}
  160. virtual bool notifySelected(ISocket *sock,unsigned selected);
  161. virtual bool notifySelected(ISocket *sock,unsigned selected, IPersistentHandler* persistentHandler, bool shouldClose) override { return false; };
  162. //IEspProtocol
  163. virtual const char * getProtocolName();
  164. virtual void addBindingMap(ISocket *sock, IEspRpcBinding* binding, bool isdefault);
  165. virtual int removeBindingMap(int port, IEspRpcBinding* binding);
  166. virtual CEspApplicationPort* queryApplicationPort(int handle);
  167. virtual void setMaxRequestEntityLength(int len) {m_MaxRequestEntityLength = len;};
  168. virtual int getMaxRequestEntityLength() { return m_MaxRequestEntityLength; }
  169. virtual void setContainer(IEspContainer* container) { m_container = container; }
  170. virtual void initPersistentHandler(IPropertyTree * proc_cfg);
  171. virtual bool persistentEnabled() { return m_persistentHandler != nullptr; }
  172. virtual void addPersistent(ISocket* sock);
  173. virtual int countBindings(int port);
  174. };
  175. #endif