sptk2 logo
SPTK Home Page
HttpConnect.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <memory>
30#include <sptk5/RegularExpression.h>
31#include <sptk5/Strings.h>
32#include <sptk5/net/HttpParams.h>
33#include <sptk5/net/HttpReader.h>
34#include <sptk5/net/TCPSocket.h>
35
36namespace sptk {
37
49class SP_EXPORT HttpConnect
50{
51public:
56 {
57 public:
62 Authorization(const Authorization& other) = default;
63
68 Authorization& operator=(const Authorization& other) = default;
69
74 String method() const
75 {
76 return m_method;
77 }
78
83 String value() const
84 {
85 return m_value;
86 }
87
88 protected:
92 Authorization() = delete;
93
101 explicit Authorization(const String& method, const String& username, const String& password, const String& jwtToken);
102
103 private:
104 String m_method;
105 String m_value;
106 };
107
109 {
110 public:
116 BasicAuthorization(const String& username, const String& password)
117 : Authorization("basic", username, password, "")
118 {
119 }
120 };
121
123 {
124 public:
129 explicit BearerAuthorization(const String& jwtToken)
130 : Authorization("bearer", "", "", jwtToken)
131 {
132 }
133 };
134
142 explicit HttpConnect(TCPSocket& socket);
143
150 HttpHeaders& requestHeaders()
151 {
152 return m_requestHeaders;
153 }
154
162 [[nodiscard]] const HttpHeaders& responseHeaders() const
163 {
164 return m_reader->getHttpHeaders();
165 }
166
178 [[nodiscard]] int cmd_get(const String& pageName, const HttpParams& parameters, Buffer& output,
179 const Authorization* authorization = nullptr,
180 std::chrono::milliseconds timeout = std::chrono::seconds(60));
181
195 [[nodiscard]] int cmd_post(const String& pageName, const HttpParams& parameters, const Buffer& content, Buffer& output,
196 const sptk::Strings& possibleContentEncodings, const Authorization* authorization = nullptr,
197 std::chrono::milliseconds timeout = std::chrono::seconds(60));
198
211 [[nodiscard]] int cmd_put(const String& pageName, const HttpParams& parameters, const Buffer& content, Buffer& output,
212 const Authorization* authorization = nullptr,
213 std::chrono::milliseconds timeout = std::chrono::seconds(60));
214
226 [[nodiscard]] int cmd_delete(const String& pageName, const HttpParams& parameters, Buffer& output,
227 const Authorization* authorization = nullptr,
228 std::chrono::milliseconds timeout = std::chrono::seconds(60));
229
235 [[nodiscard]] String responseHeader(const String& headerName) const;
236
241 [[nodiscard]] int statusCode() const;
242
247 [[nodiscard]] String statusText() const;
248
249protected:
253 Strings makeHeaders(const String& httpCommand, const String& pageName, const HttpParams& requestParameters,
254 const Authorization* authorization) const;
255
262 void sendCommand(const String& cmd);
263
270 void sendCommand(const Buffer& cmd);
271
280 int getResponse(Buffer& output, std::chrono::milliseconds timeout);
281
282private:
286 std::shared_ptr<HttpReader> m_reader;
287
291 TCPSocket& m_socket;
292
296 HttpHeaders m_requestHeaders;
297};
298
302} // namespace sptk
Definition: Buffer.h:51
Definition: HttpConnect.h:56
Authorization & operator=(const Authorization &other)=default
String method() const
Definition: HttpConnect.h:74
Authorization(const String &method, const String &username, const String &password, const String &jwtToken)
String value() const
Definition: HttpConnect.h:83
Authorization(const Authorization &other)=default
Definition: HttpConnect.h:109
BasicAuthorization(const String &username, const String &password)
Definition: HttpConnect.h:116
Definition: HttpConnect.h:123
BearerAuthorization(const String &jwtToken)
Definition: HttpConnect.h:129
HTTP socket.
Definition: HttpConnect.h:50
int cmd_get(const String &pageName, const HttpParams &parameters, Buffer &output, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
Sends the GET command to the server.
int cmd_delete(const String &pageName, const HttpParams &parameters, Buffer &output, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
Sends the DELETE command to the server.
int cmd_post(const String &pageName, const HttpParams &parameters, const Buffer &content, Buffer &output, const sptk::Strings &possibleContentEncodings, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
Sends the POST command to the server.
HttpHeaders & requestHeaders()
Returns the HTTP request headers.
Definition: HttpConnect.h:150
const HttpHeaders & responseHeaders() const
Returns the HTTP headers.
Definition: HttpConnect.h:162
Strings makeHeaders(const String &httpCommand, const String &pageName, const HttpParams &requestParameters, const Authorization *authorization) const
int statusCode() const
Get the request execution status code.
String responseHeader(const String &headerName) const
Get value of response header.
void sendCommand(const Buffer &cmd)
Sends a single command to HTTP server.
String statusText() const
Get the request execution status text.
int cmd_put(const String &pageName, const HttpParams &parameters, const Buffer &content, Buffer &output, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
Sends the PUT command to the server.
void sendCommand(const String &cmd)
Sends a single command to HTTP server.
int getResponse(Buffer &output, std::chrono::milliseconds timeout)
Retrieves the server response on the command.
HttpConnect(TCPSocket &socket)
Constructor.
Definition: HttpParams.h:69
Definition: String.h:49
Definition: Strings.h:52
Definition: TCPSocket.h:165

Fri Oct 14 2022 09:58:32: SPTK 5.4.1