aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/http/http_client.cpp
blob: c3326a95c4e68ae768288e7c2d4874c720ccb96d (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
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "http_client.h"
#include "hex_number.h"
#include <vbench/core/line_reader.h>
#include <vespa/vespalib/data/output_writer.h>

namespace vbench {

using OutputWriter = vespalib::OutputWriter;

HttpClient::~HttpClient() {}

void
HttpClient::writeRequest() {
    OutputWriter dst(_conn->stream(), WRITE_SIZE);
    dst.printf("GET %s HTTP/1.1\r\n", _url.c_str());
    dst.printf("Host: %s\r\n", _conn->server().host.c_str());
    dst.write("User-Agent: vbench\r\n");
    dst.write("X-Yahoo-Vespa-Benchmarkdata: true\r\n");
    dst.write("X-Yahoo-Vespa-Benchmarkdata-Coverage: true\r\n");
    dst.write("\r\n");
}

bool
HttpClient::readStatus()
{
    LineReader reader(_conn->stream());
    if (reader.readLine(_line) && (splitstr(_line, "\t ", _split) >= 2)) {
        if (_split[0] == "HTTP/1.0") {
            _header.version = 0;
        } else if (_split[0] == "HTTP/1.1") {
            _header.version = 1;
        } else {
            _handler.handleFailure(strfmt("unknown HTTP version: '%s'", _split[0].c_str()));
            return false;
        }
        _header.status = atoi(_split[1].c_str());
        if (_header.status != 200) {
            _handler.handleFailure(strfmt("HTTP status not 200: '%s'", _split[1].c_str()));
            return false;
        }
        return true;
    }
    if (_conn->stream().tainted()) {
        _handler.handleFailure(strfmt("Connection error: %s",
                                      _conn->stream().tainted().reason().c_str()));
    } else {
        _handler.handleFailure(strfmt("could not parse HTTP status line: '%s'", _line.c_str()));
    }
    return false;
}

bool
HttpClient::readHeaders()
{
    LineReader reader(_conn->stream());
    while (reader.readLine(_line)) {
        if (_line.empty()) {
            return true;
        }
        if ((_line[0] == ' ') || (_line[0] == '\t')) {
            // ignore continuation headers
        } else if (_line.find("X-Yahoo-Vespa-") == 0) {
            if (splitstr(_line, ":\t ", _split) == 2) {
                _handler.handleHeader(_split[0], _split[1]);
            }
        } else {
            if (splitstr(_line, ":\t ", _split) > 1) {
                if (strcasecmp(_split[0].c_str(), "connection") == 0) {
                    for (size_t i = 1; i < _split.size(); ++i) {
                        if (strcasecmp(_split[i].c_str(), "keep-alive") == 0) {
                            _handler.handleHeader(_split[0], _split[i]);
                            _header.keepAliveGiven = true;
                        } else if (strcasecmp(_split[i].c_str(), "close") == 0) {
                            _handler.handleHeader(_split[0], _split[i]);
                            _header.connectionCloseGiven = true;
                        }
                    }
                } else if (strcasecmp(_split[0].c_str(), "content-length") == 0 &&
                           _split.size() == 2)
                {
                    _handler.handleHeader(_split[0], _split[1]);
                    _header.contentLengthGiven = true;
                    _header.contentLength = atoi(_split[1].c_str());
                } else if (strcasecmp(_split[0].c_str(), "transfer-encoding") == 0 &&
                           strcasecmp(_split[1].c_str(), "chunked") == 0)
                {
                    _handler.handleHeader(_split[0], _split[1]);
                    _header.chunkedEncodingGiven = true;
                }
            }
        }
    }
    _handler.handleFailure("HTTP header did not end in empty line");
    return false;
}

bool
HttpClient::readContent(size_t len) {
    Input &input = _conn->stream();
    while (len > 0) {
        Memory mem = input.obtain();
        mem.size = std::min(len, mem.size);
        if (mem.size == 0) {
            _handler.handleFailure(strfmt("short read: missing %zu bytes", len));
            return false;
        }
        _handler.handleContent(mem);
        input.evict(mem.size);
        len -= mem.size;
    }
    return true;
}

bool
HttpClient::readChunkSize(bool first, size_t &size)
{
    LineReader reader(_conn->stream());
    if (!first && (!reader.readLine(_line) || !_line.empty())) {
        return false;
    }
    if (!reader.readLine(_line)) {
        return false;
    }
    HexNumber hex(_line.c_str());
    size = hex.value();
    return (hex.length() > 0);
}

bool
HttpClient::skipTrailers()
{
    LineReader reader(_conn->stream());
    while (reader.readLine(_line)) {
        if (_line.empty()) {
            return true;
        }
    }
    return false;
}

bool
HttpClient::readContent()
{
    if (_header.contentLengthGiven) {
        return readContent(_header.contentLength);
    } else if (_header.chunkedEncodingGiven) {
        size_t chunkSize = 0;
        for (bool first = true; readChunkSize(first, chunkSize); first = false) {
            if (chunkSize == 0) {
                return skipTrailers();
            }
            if (!readContent(chunkSize)) {
                return false;
            }
        }
        _handler.handleFailure("error reading HTTP chunk size");
        return false;
    } else { // data terminated by eof
        if (serverKeepAlive()) {
            _handler.handleFailure("server indicated keep-alive, "
                                   "but we need eof to terminate data");
            return false;
        }
        Input &input = _conn->stream();
        for (;;) {
            Memory mem = input.obtain();
            if (mem.size == 0) {
                if (_conn->stream().tainted()) {
                    _handler.handleFailure(strfmt("read error: '%s'",
                                                  _conn->stream().tainted().reason().c_str()));
                }
                return true;
            }
            _handler.handleContent(mem);
            input.evict(mem.size);
        }
    }
}

bool
HttpClient::perform(CryptoEngine &crypto)
{
    writeRequest();
    if (!_conn->fresh() && (_conn->stream().obtain().size == 0)) {
        _conn.reset(new HttpConnection(crypto, _conn->server()));
        writeRequest();
    }
    return (readStatus() && readHeaders() && readContent());
}

} // namespace vbench