Loading...
Searching...
No Matches
client.h
1/*
2* <one line to give the program's name and a brief idea of what it does.>
3* Copyright (C) 2021 <copyright holder> <email>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef NAF_HTTP_CLIENT
20#define NAF_HTTP_CLIENT
21
22
23#include <functional>
24#include <vector>
25#include <optional>
26
27#include "Poco/Net/HTTPSClientSession.h"
28#include "Poco/Net/HTTPClientSession.h"
29#include "Poco/Net/HTTPRequest.h"
30#include "Poco/Net/HTTPResponse.h"
31#include "Poco/Net/HTTPCredentials.h"
32#include "Poco/Net/AcceptCertificateHandler.h"
33#include <Poco/Net/Context.h>
34#include <Poco/Net/NetException.h>
35#include <Poco/Net/NetSSL.h>
36#include <Poco/Net/SSLManager.h>
37#include "Poco/StreamCopier.h"
38#include "Poco/NullStream.h"
39#include "Poco/Path.h"
40#include "Poco/URI.h"
41#include "Poco/Exception.h"
42#include <Poco/JSON/Object.h>
43#include <Poco/Net/NameValueCollection.h>
44#include <Poco/Net/SSLException.h>
45
46#include "http/methods.h"
47#include "http/request.h"
48#include "tools/output_logger.h"
49
50
51namespace NAF
52{
53 namespace HTTP
54 {
55 class Client;
56 }
57}
58
59using namespace Poco;
60using namespace Poco::Net;
61
62
64{
65 public:
66 using ClientResponseFunction = std::function<void(std::stringstream& response, Net::HTTPRequest& http_request, Net::HTTPResponse& http_response)>;
67
68 Client(std::string uri, const std::string method);
69
70 bool get_use_ssl() const { return use_ssl_; }
71 std::string get_uri() const { return uri_; }
72 std::string get_method() const { return method_; }
73 std::string get_username() const { return username_; }
74 std::string get_password() const { return password_; }
75 bool get_use_credentials() const { return use_credentials_; }
76 std::vector<HTTP::Header>& get_headers()
77 {
78 auto& var = headers_;
79 return var;
80 }
81 std::vector<HTTP::Cookie>& get_cookies()
82 {
83 auto& var = cookies_;
84 return var;
85 }
86 ClientResponseFunction get_response_handler() const { return response_handler_; }
87
88 void set_use_ssl(bool use_ssl) { use_ssl_ = use_ssl; }
89 void set_uri(std::string uri) { uri_ = uri; }
90 void set_method(std::string method) { method_ = method; }
91 void set_username(std::string username) { username_ = username; }
92 void set_password(std::string password) { password_ = password; }
93 void set_use_credentials(bool use_credentials) { use_credentials_ = use_credentials; }
94 void set_response_handler(ClientResponseFunction response_handler) { response_handler_ = response_handler; }
95
96 void UseCredentials_(std::string username, std::string password);
97 void SetupSSL_(std::string key, std::string cert);
98 void SetupSSL_(std::string rootcert);
99 void AddHeader_(std::string name, std::string value);
100 void AddCookie_(std::string name, std::string value);
101 void SendRequest_();
102
103 protected:
104 void SendNormalRequest_();
105 void SendSSLRequest_();
106 void SetupHeaders_(Net::HTTPRequest& http_request);
107 void SetupCookies_(Net::HTTPRequest& http_request);
108
109 private:
110 bool use_ssl_;
111 std::string uri_;
112 std::string method_;
113 std::string username_;
114 std::string password_;
115 bool use_credentials_;
116 std::vector<HTTP::Header> headers_;
117 std::vector<HTTP::Cookie> cookies_;
118 ClientResponseFunction response_handler_;
119 Context::Ptr ssl_context_;
120};
121
122#endif // NAF_HTTP_CLIENT
Definition client.h:64