1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
#pragma once
#include "httpagent.h"
#include "httpparser.h"
#include "http_digest.h"
#include <util/system/compat.h>
#include <util/string/vector.h>
#include <util/network/ip.h>
#include <library/cpp/uri/http_url.h>
#include <library/cpp/http/misc/httpcodes.h>
/********************************************************/
// Section 1: socket handlers
/********************************************************/
// The following classes allows to adopt template scheme
// THttpAgent for work with socket by flexible
// object-style scheme.
/********************************************************/
// This class is used as a base one for flexible
// socket handling
class socketAbstractHandler {
public:
virtual bool Good() = 0;
virtual int Connect(const TAddrList& addrs, TDuration Timeout) = 0;
virtual void Disconnect() = 0;
virtual void shutdown() = 0;
virtual bool send(const char* message, ssize_t messlen) = 0;
virtual bool peek() = 0;
virtual ssize_t read(void* buffer, ssize_t buflen) = 0;
virtual ~socketAbstractHandler() {
}
protected:
socketAbstractHandler() {
}
};
/********************************************************/
// This class is used as a proxy between THttpAgent and
// socketAbstractHandler
// (it is used by template scheme,
// so it does not have virtual methods)
class TSocketHandlerPtr {
protected:
socketAbstractHandler* Handler_;
public:
TSocketHandlerPtr()
: Handler_(nullptr)
{
}
virtual ~TSocketHandlerPtr() {
delete Handler_;
}
int Good() {
return (Handler_ && Handler_->Good());
}
int Connect(const TAddrList& addrs, TDuration Timeout) {
return (Handler_) ? Handler_->Connect(addrs, Timeout) : 1;
}
void Disconnect() {
if (Handler_)
Handler_->Disconnect();
}
void shutdown() {
if (Handler_)
Handler_->shutdown();
}
bool send(const char* message, ssize_t messlen) {
return (Handler_) ? Handler_->send(message, messlen) : false;
}
virtual bool peek() {
return (Handler_) ? Handler_->peek() : false;
}
virtual ssize_t read(void* buffer, ssize_t buflen) {
return (Handler_) ? Handler_->read(buffer, buflen) : 0;
}
void setHandler(socketAbstractHandler* handler) {
if (Handler_)
delete Handler_;
Handler_ = handler;
}
};
/********************************************************/
// Here is httpAgent that uses socketAbstractHandler class
// ant its derivatives
using httpSpecialAgent = THttpAgent<TSocketHandlerPtr>;
/********************************************************/
// Regular handler is used as implementation of
// socketAbstractHandler for work through HTTP protocol
class socketRegularHandler: public socketAbstractHandler {
protected:
TSimpleSocketHandler Socket_;
public:
socketRegularHandler()
: Socket_()
{
}
bool Good() override {
return Socket_.Good();
}
int Connect(const TAddrList& addrs, TDuration Timeout) override {
return Socket_.Connect(addrs, Timeout);
}
void Disconnect() override {
Socket_.Disconnect();
}
void shutdown() override {
//Do not block writing to socket
//There are servers that works in a bad way with this
//mSocket.shutdown();
}
bool send(const char* message, ssize_t messlen) override {
return Socket_.send(message, messlen);
}
bool peek() override {
return Socket_.peek();
}
ssize_t read(void* buffer, ssize_t buflen) override {
return Socket_.read(buffer, buflen);
}
};
/********************************************************/
// The base factory that allows to choose an appropriate
// socketAbstractHandler implementation by url schema
class socketHandlerFactory {
public:
virtual ~socketHandlerFactory() {
}
//returns mHandler_HTTP for correct HTTP-based url
virtual socketAbstractHandler* chooseHandler(const THttpURL& url);
static socketHandlerFactory sInstance;
};
/********************************************************/
// Section 2: the configurates tool to parse an HTTP-response
/********************************************************/
class httpAgentReader: public THttpParserGeneric<1> {
protected:
THttpAuthHeader Header_;
httpSpecialAgent& Agent_;
char* Buffer_;
void* BufPtr_;
int BufSize_;
long BufRest_;
void readBuf();
bool step() {
if (BufRest_ == 0)
readBuf();
if (eof())
return false;
return true;
}
public:
httpAgentReader(httpSpecialAgent& agent,
const char* baseUrl,
bool assumeConnectionClosed,
bool use_auth = false,
int bufSize = 0x1000);
~httpAgentReader();
bool eof() {
return BufRest_ < 0;
}
int error() {
return Header_.error;
}
void setError(int errCode) {
Header_.error = errCode;
}
const THttpAuthHeader* getAuthHeader() {
return &Header_;
}
const THttpHeader* readHeader();
long readPortion(void*& buf);
bool skipTheRest();
};
/********************************************************/
// Section 3: the main class
/********************************************************/
class httpLoadAgent: public httpSpecialAgent {
protected:
socketHandlerFactory& Factory_;
bool HandleAuthorization_;
THttpURL URL_;
bool PersistentConn_;
httpAgentReader* Reader_;
TVector<TString> Headers_;
int ErrCode_;
char* RealHost_;
httpDigestHandler Digest_;
void clearReader();
bool doSetHost(const TAddrList& addrs);
bool doStartRequest();
public:
httpLoadAgent(bool handleAuthorization = false,
socketHandlerFactory& factory = socketHandlerFactory::sInstance);
~httpLoadAgent();
void setRealHost(const char* host);
void setIMS(const char* ifModifiedSince);
void addHeaderInstruction(const char* instr);
void dropHeaderInstructions();
bool startRequest(const char* url,
const char* url_to_merge = nullptr,
bool persistent = false,
const TAddrList& addrs = TAddrList());
// deprecated v4-only
bool startRequest(const char* url,
const char* url_to_merge,
bool persistent,
ui32 ip);
bool startRequest(const THttpURL& url,
bool persistent = false,
const TAddrList& addrs = TAddrList());
bool setHost(const char* host_url,
const TAddrList& addrs = TAddrList());
bool startOneRequest(const char* local_url);
const THttpAuthHeader* getAuthHeader() {
if (Reader_ && Reader_->getAuthHeader()->use_auth)
return Reader_->getAuthHeader();
return nullptr;
}
const THttpHeader* getHeader() {
if (Reader_)
return Reader_->getAuthHeader();
return nullptr;
}
const THttpURL& getURL() {
return URL_;
}
bool eof() {
if (Reader_)
return Reader_->eof();
return true;
}
int error() {
if (ErrCode_)
return ErrCode_;
if (Reader_)
return Reader_->error();
return HTTP_BAD_URL;
}
long readPortion(void*& buf) {
if (Reader_)
return Reader_->readPortion(buf);
return -1;
}
};
/********************************************************/
|