UniSet  2.24.2
UHttpRequestHandler.h
1 #ifndef DISABLE_REST_API
2 /*
3  * Copyright (c) 2015 Pavel Vainerman.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as
7  * published by the Free Software Foundation, version 2.1.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Lesser Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 // -------------------------------------------------------------------------
18 #ifndef UHttpRequesrHandler_H_
19 #define UHttpRequesrHandler_H_
20 // -------------------------------------------------------------------------
21 #include <memory>
22 #include <Poco/Net/HTTPRequestHandler.h>
23 #include <Poco/Net/HTTPRequestHandlerFactory.h>
24 #include <Poco/Net/HTTPServerRequest.h>
25 #include <Poco/Net/HTTPServerResponse.h>
26 #include <Poco/URI.h>
27 #include <Poco/JSON/Object.h>
28 #include "ujson.h"
29 #include "DebugStream.h"
30 // -------------------------------------------------------------------------
85 // -------------------------------------------------------------------------
86 namespace uniset
87 {
88  namespace UHttp
89  {
90  // текущая версия API
91  const std::string UHTTP_API_VERSION = "v01";
92 
95  {
96  public:
97  IHttpRequest() {}
98  virtual ~IHttpRequest() {}
99 
100  // throw SystemError
101  virtual Poco::JSON::Object::Ptr httpGet( const Poco::URI::QueryParameters& p ) = 0;
102  virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) = 0;
103 
104  // не обязательная функция.
105  virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p );
106  };
107  // -------------------------------------------------------------------------
110  {
111  public:
113  virtual ~IHttpRequestRegistry() {}
114 
115  // throw SystemError, NameNotFound
116  virtual Poco::JSON::Object::Ptr httpGetByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
117 
118  // throw SystemError
119  virtual Poco::JSON::Array::Ptr httpGetObjectsList( const Poco::URI::QueryParameters& p ) = 0;
120  virtual Poco::JSON::Object::Ptr httpHelpByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
121  virtual Poco::JSON::Object::Ptr httpRequestByName( const std::string& name, const std::string& req, const Poco::URI::QueryParameters& p ) = 0;
122  };
123 
124  // -------------------------------------------------------------------------
126  public Poco::Net::HTTPRequestHandler
127  {
128  public:
129  UHttpRequestHandler( std::shared_ptr<IHttpRequestRegistry> _registry, const std::string& httpCORS_allow = "*");
130 
131  virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp ) override;
132 
133  private:
134 
135  std::shared_ptr<IHttpRequestRegistry> registry;
136  std::shared_ptr<DebugStream> log;
137  const std::string httpCORS_allow = { "*" };
138  };
139  // -------------------------------------------------------------------------
141  public Poco::Net::HTTPRequestHandlerFactory
142  {
143  public:
144 
145  UHttpRequestHandlerFactory( std::shared_ptr<IHttpRequestRegistry>& _registry );
146 
147  virtual Poco::Net::HTTPRequestHandler* createRequestHandler( const Poco::Net::HTTPServerRequest& ) override;
148 
149  // (CORS): Access-Control-Allow-Origin. Default: *
150  void setCORS_allow( const std::string& allow );
151  private:
152  std::shared_ptr<IHttpRequestRegistry> registry;
153  std::string httpCORS_allow = { "*" };
154  };
155  }
156  // -------------------------------------------------------------------------
157 } // end of uniset namespace
158 // -------------------------------------------------------------------------
159 #endif // UHttpRequesrHandler_H_
160 // -------------------------------------------------------------------------
161 #endif
Definition: UHttpRequestHandler.h:95
Definition: UHttpRequestHandler.h:110
Definition: UHttpRequestHandler.h:142
Definition: UHttpRequestHandler.h:127
Definition: CommonEventLoop.h:15