UniSet  2.8.0
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 // -------------------------------------------------------------------------
72 // -------------------------------------------------------------------------
73 namespace uniset
74 {
75  namespace UHttp
76  {
77  // текущая версия API
78  const std::string UHTTP_API_VERSION = "v01";
79 
82  {
83  public:
84  IHttpRequest() {}
85  virtual ~IHttpRequest() {}
86 
87  // throw SystemError
88  virtual Poco::JSON::Object::Ptr httpGet( const Poco::URI::QueryParameters& p ) = 0;
89  virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) = 0;
90 
91  // не обязательная функция.
92  virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p );
93  };
94  // -------------------------------------------------------------------------
97  {
98  public:
100  virtual ~IHttpRequestRegistry() {}
101 
102  // throw SystemError, NameNotFound
103  virtual Poco::JSON::Object::Ptr httpGetByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
104 
105  // throw SystemError
106  virtual Poco::JSON::Array::Ptr httpGetObjectsList( const Poco::URI::QueryParameters& p ) = 0;
107  virtual Poco::JSON::Object::Ptr httpHelpByName( const std::string& name, const Poco::URI::QueryParameters& p ) = 0;
108  virtual Poco::JSON::Object::Ptr httpRequestByName( const std::string& name, const std::string& req, const Poco::URI::QueryParameters& p ) = 0;
109  };
110 
111  // -------------------------------------------------------------------------
113  public Poco::Net::HTTPRequestHandler
114  {
115  public:
116  UHttpRequestHandler( std::shared_ptr<IHttpRequestRegistry> _registry, const std::string& httpCORS_allow = "*");
117 
118  virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp ) override;
119 
120  private:
121 
122  std::shared_ptr<IHttpRequestRegistry> registry;
123  std::shared_ptr<DebugStream> log;
124  const std::string httpCORS_allow = { "*" };
125  };
126  // -------------------------------------------------------------------------
128  public Poco::Net::HTTPRequestHandlerFactory
129  {
130  public:
131 
132  UHttpRequestHandlerFactory( std::shared_ptr<IHttpRequestRegistry>& _registry );
133 
134  virtual Poco::Net::HTTPRequestHandler* createRequestHandler( const Poco::Net::HTTPServerRequest& ) override;
135 
136  // (CORS): Access-Control-Allow-Origin. Default: *
137  void setCORS_allow( const std::string& allow );
138  private:
139  std::shared_ptr<IHttpRequestRegistry> registry;
140  std::string httpCORS_allow = { "*" };
141  };
142  }
143  // -------------------------------------------------------------------------
144 } // end of uniset namespace
145 // -------------------------------------------------------------------------
146 #endif // UHttpRequesrHandler_H_
147 // -------------------------------------------------------------------------
148 #endif
Definition: CommonEventLoop.h:14
Definition: UHttpRequestHandler.h:96
Definition: UHttpRequestHandler.h:81
Definition: UHttpRequestHandler.h:112
Definition: UHttpRequestHandler.h:127