-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpRequestGenerator.cc
More file actions
75 lines (62 loc) · 1.69 KB
/
HttpRequestGenerator.cc
File metadata and controls
75 lines (62 loc) · 1.69 KB
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
/*
* Copyright (C) 2021 Frank Mertens.
*
* Distribution and use is allowed under the terms of the GNU General Public License version 3
* (see CoreComponents/LICENSE-gpl-3.0).
*
*/
#include <cc/HttpRequestGenerator>
namespace cc {
struct HttpRequestGenerator::State: public HttpMessageGenerator::State
{
State(const Stream &stream):
HttpMessageGenerator::State{stream}
{}
void polishHeader() override
{
header_.insert("Connection", "keep-alive");
if (contentLength_ > 0) {
header_.remove("Transfer-Encoding");
header_.establish("Content-Length", str(contentLength_));
}
else if (contentLength_ < 0) {
header_.establish("Transfer-Encoding", "chunked");
}
}
void writeFirstLine(Format &sink) override
{
if (method_ == "") sink << "GET ";
else sink << method_ << " ";
if (path_ == "") sink << "/ ";
else sink << path_ << " ";
if (version_ == "") sink << "HTTP/1.1\r\n";
else sink << version_ << "\r\n";
}
String method_;
String path_;
String version_;
};
HttpRequestGenerator::HttpRequestGenerator(const Stream &stream):
HttpMessageGenerator{new State{stream}}
{}
void HttpRequestGenerator::setMethod(const String &method)
{
me().method_ = method;
}
void HttpRequestGenerator::setPath(const String &path)
{
me().path_ = path;
}
void HttpRequestGenerator::setVersion(const String &version)
{
me().version_ = version;
}
void HttpRequestGenerator::setHost(const String &host)
{
me().setHeader("Host", host);
}
HttpRequestGenerator::State &HttpRequestGenerator::me()
{
return Object::me.as<State>();
}
} // namespace cc