blob: 5d55230c41021c8858f83b93333532a5a35bd4d1 (
plain) (
blame)
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
|
#include "test_http_server.h"
#include <library/cpp/http/misc/httpcodes.h>
#include <library/cpp/http/server/http_ex.h>
#include <util/generic/yexception.h>
namespace NYql {
class TTestHttpServer::TImpl : public THttpServer::ICallBack {
class TRequestProcessor : public THttpClientRequestEx {
public:
explicit TRequestProcessor(TImpl* parent)
: Parent_(parent)
{
Y_UNUSED(Parent_);
}
bool Reply(void* /*tsr*/) override {
if (!ProcessHeaders()) {
return true;
}
if (!RequestString.StartsWith("GET ")) {
return true;
}
TRequest r;
for (auto& p : ParsedHeaders) {
if (p.first == "Authorization" && p.second.StartsWith("OAuth ")) {
r.OAuthToken = p.second.substr(strlen("OAuth "));
continue;
}
if (p.first == "If-None-Match") {
r.IfNoneMatch = p.second;
continue;
}
if (p.first == "If-Modified-Since") {
r.IfModifiedSince = p.second;
continue;
}
}
auto reply = Parent_->ProcessNextRequest(r);
switch (reply.Code) {
case HTTP_OK:
Output() << "HTTP/1.1 200 Ok\r\n";
break;
case HTTP_NOT_MODIFIED:
Output() << "HTTP/1.1 304 Not modified\r\n";
break;
case HTTP_FORBIDDEN:
Output() << "HTTP/1.1 403 Forbidden\r\n";
break;
default:
return true;
}
if (reply.ETag) {
Output() << "ETag: " + reply.ETag + "\r\n";
}
if (reply.LastModified) {
Output() << "Last-Modified: " + reply.LastModified + "\r\n";
}
if (reply.Content || reply.ContentLength) {
const int length = reply.ContentLength.GetOrElse(reply.Content.length());
Output() << "Content-Length: " << length << "\r\n";
}
Output() << "\r\n";
if (reply.Content) {
Output() << reply.Content;
}
Output().Finish();
return true;
}
private:
TImpl* Parent_ = nullptr;
};
public:
explicit TImpl(int port)
: HttpServer_(this, THttpServer::TOptions(port))
, Port_(port)
{
}
TClientRequest* CreateClient() override {
return new TRequestProcessor(this);
}
void Start() {
Y_ENSURE(HttpServer_.Start());
}
void Stop() {
HttpServer_.Stop();
}
TString GetUrl() const {
return "http://localhost:" + ToString(Port_);
}
void SetRequestHandler(TRequestHandler handler) {
RequestHandler_ = std::move(handler);
}
private:
TReply ProcessNextRequest(const TRequest& request) {
return RequestHandler_(request);
}
private:
THttpServer HttpServer_;
const int Port_;
TRequestHandler RequestHandler_;
};
TTestHttpServer::TTestHttpServer(int port)
: Impl_(new TImpl(port)) {
}
TTestHttpServer::~TTestHttpServer() {
Impl_->Stop();
}
void TTestHttpServer::Start() {
Impl_->Start();
}
TString TTestHttpServer::GetUrl() const {
return Impl_->GetUrl();
}
void TTestHttpServer::SetRequestHandler(TRequestHandler handler) {
return Impl_->SetRequestHandler(std::move(handler));
}
}
|