sptk2 logo
SPTK Home Page
HttpReader.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 <sptk5/Buffer.h>
30#include <sptk5/CaseInsensitiveCompare.h>
31#include <sptk5/RegularExpression.h>
32#include <sptk5/net/TCPSocket.h>
33
34#include <mutex>
35
36namespace sptk {
37
41using HttpHeaders = std::map<String, String, CaseInsensitiveCompare>;
42
48class SP_EXPORT HttpReader
49{
50public:
54 enum class State : unsigned
55 {
56 READY = 0,
57 READING_HEADERS = 1,
58 READING_DATA = 2,
59 COMPLETED = 4,
60 READ_ERROR = 8
61 };
62
66 enum class ReadMode
67 {
68 REQUEST,
69 RESPONSE
70 };
71
76
80 HttpHeaders& getHttpHeaders();
81
86 String httpHeader(const String& headerName) const;
87
93 HttpReader(TCPSocket& socket, Buffer& output, ReadMode readMode);
94
100
106
110 void read();
111
116
121
128 int readAll(std::chrono::milliseconds timeout);
129
134 int getStatusCode() const;
135
140 const String& getStatusText() const;
141
142 String getRequestType() const;
143 String getRequestURL() const;
144
145 void close();
146
147private:
148 TCPSocket& m_socket;
149 ReadMode m_readMode;
150 State m_readerState {State::READY};
151 mutable std::mutex m_mutex;
152 String m_statusText;
153 int m_statusCode {0};
154 size_t m_contentLength {0};
155 size_t m_contentReceivedLength {0};
156 bool m_contentIsChunked {false};
157 HttpHeaders m_httpHeaders;
158 RegularExpression m_matchProtocolAndResponseCode {"^(HTTP\\S+)\\s+(\\d+)\\s+(.*)?\r?"};
159 Buffer& m_output;
160 Buffer m_read_buffer;
161 String m_requestType;
162 String m_requestURL;
163
167 void reset();
168
173 bool readStatus();
174
178 bool readData();
179
180 void readDataChunk(bool& done);
181};
182
183} // namespace sptk
Definition: Buffer.h:51
Definition: HttpReader.h:49
ReadMode
Definition: HttpReader.h:67
State getReaderState() const
void readHttpHeaders()
bool readHttpRequest()
TCPSocket & socket()
State
Definition: HttpReader.h:55
int readAll(std::chrono::milliseconds timeout)
const String & getStatusText() const
HttpReader(TCPSocket &socket, Buffer &output, ReadMode readMode)
Buffer & output()
HttpHeaders & getHttpHeaders()
String httpHeader(const String &headerName) const
int getStatusCode() const
Definition: String.h:49
Definition: TCPSocket.h:165

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