aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/portal/http_connection.cpp
blob: 97a5f6082c944e5fb92e421e81022eef202c1e39 (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
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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "http_connection.h"
#include <vespa/vespalib/data/output_writer.h>
#include <cassert>

namespace vespalib::portal {

namespace {

constexpr size_t CHUNK_SIZE = 4096;

enum class ReadRes { OK, END, FAIL };
enum class WriteRes { OK, BLOCKED, FAIL };

bool is_blocked(int res) {
    return ((res == -1) && ((errno == EWOULDBLOCK) || (errno == EAGAIN)));
}

ReadRes drain(CryptoSocket &socket, SmartBuffer &buffer) {
    size_t chunk_size = std::max(CHUNK_SIZE, socket.min_read_buffer_size());
    for (;;) {
        auto chunk = buffer.reserve(chunk_size);
        auto res = socket.drain(chunk.data, chunk.size);
        if (res > 0) {
            buffer.commit(res);
        } else if (res == 0) {
            return ReadRes::OK;
        } else {
            return ReadRes::FAIL;
        }
    }
}

ReadRes read(CryptoSocket &socket, SmartBuffer &buffer) {
    size_t chunk_size = std::max(CHUNK_SIZE, socket.min_read_buffer_size());
    auto chunk = buffer.reserve(chunk_size);
    auto res = socket.read(chunk.data, chunk.size);
    if (res > 0) {
        buffer.commit(res);
    } else if (res == 0) {
        return ReadRes::END;
    } else if (is_blocked(res)) {
        return ReadRes::OK;
    } else {
        return ReadRes::FAIL;
    }
    return drain(socket, buffer);
}

WriteRes flush(CryptoSocket &socket) {
    for (;;) {
        auto res = socket.flush();
        if (res > 0) {
            // flush more
        } else if (res == 0) {
            return WriteRes::OK;
        } else if (is_blocked(res)) {
            return WriteRes::BLOCKED;
        } else {
            return WriteRes::FAIL;
        }
    }
}

WriteRes write(CryptoSocket &socket, SmartBuffer &buffer) {
    auto chunk = buffer.obtain();
    if (chunk.size > 0) {
        auto res = socket.write(chunk.data, chunk.size);
        if (res > 0) {
            buffer.evict(res);
        } else if (is_blocked(res)) {
            return WriteRes::BLOCKED;
        } else {
            assert(res < 0);
            return WriteRes::FAIL;
        }
    }
    return flush(socket);
}

WriteRes half_close(CryptoSocket &socket) {
    auto res = socket.half_close();
    if (res == 0) {
        return WriteRes::OK;
    } else if (is_blocked(res)) {
        return WriteRes::BLOCKED;
    } else {
        return WriteRes::FAIL;
    }
}

} // namespace vespalib::portal::<unnamed>

void
HttpConnection::set_state(State state, bool read, bool write)
{
    _token->update(read, write);
    _state = state;
}

void
HttpConnection::do_handshake()
{
    for (;;) {
        switch (_socket->handshake()) {
        case vespalib::CryptoSocket::HandshakeResult::FAIL:       return set_state(State::NOTIFY,       false, false);
        case vespalib::CryptoSocket::HandshakeResult::DONE:       return set_state(State::READ_REQUEST,  true, false);
        case vespalib::CryptoSocket::HandshakeResult::NEED_READ:  return set_state(State::HANDSHAKE,     true, false);
        case vespalib::CryptoSocket::HandshakeResult::NEED_WRITE: return set_state(State::HANDSHAKE,    false,  true);
        case vespalib::CryptoSocket::HandshakeResult::NEED_WORK:  _socket->do_handshake_work();
        }
    }
}

void
HttpConnection::do_read_request()
{
    if (read(*_socket, _input) != ReadRes::OK) {
        return set_state(State::NOTIFY, false, false);
    }
    auto data = _input.obtain();
    auto consumed = _request.handle_data(data.data, data.size);
    _input.evict(consumed);
    if (!_request.need_more_data()) {
        set_state(State::DISPATCH, false, false);
    }
}

void
HttpConnection::do_dispatch()
{
    set_state(State::WAIT, false, false);
    return _handler(this); // callback is final touch
}

void
HttpConnection::do_wait()
{
    if (_reply_ready) {
        set_state(State::WRITE_REPLY, false, true);
    }
}

void
HttpConnection::do_write_reply()
{
    if (write(*_socket, _output) == WriteRes::FAIL) {
        return set_state(State::NOTIFY, false, false);
    }
    if (_output.obtain().size == 0) {
        set_state(State::CLOSE, false, true);
    }
}

void
HttpConnection::do_close()
{
    if (half_close(*_socket) != WriteRes::BLOCKED) {
        set_state(State::NOTIFY, false, false);
    }
}

void
HttpConnection::do_notify()
{
    set_state(State::END, false, false);
    return _handler(this); // callback is final touch
}

HttpConnection::HttpConnection(HandleGuard guard, Reactor &reactor, CryptoSocket::UP socket, handler_fun_t handler)
    : _guard(std::move(guard)),
      _state(State::HANDSHAKE),
      _socket(std::move(socket)),
      _input(CHUNK_SIZE * 2),
      _output(CHUNK_SIZE * 2),
      _request(),
      _handler(std::move(handler)),
      _reply_ready(false),
      _token()
{
    _token = reactor.attach(*this, _socket->get_fd(), true, true);
}

HttpConnection::~HttpConnection()
{
    _token.reset();
}

void
HttpConnection::handle_event(bool, bool)
{
    if (_state == State::HANDSHAKE) {
        do_handshake();
    }
    if (_state == State::READ_REQUEST) {
        do_read_request();
    }
    if (_state == State::DISPATCH) {
        return do_dispatch(); // callback is final touch
    }
    if (_state == State::WAIT) {
        do_wait();
    }
    if (_state == State::WRITE_REPLY) { 
        do_write_reply();
    }
    if (_state == State::CLOSE) {
        do_close();
    }
    if (_state == State::NOTIFY) {
        return do_notify(); // callback is final touch
    }
}

void
HttpConnection::respond_with_content(const vespalib::string &content_type,
                                     const vespalib::string &content)
{
    {
        OutputWriter dst(_output, CHUNK_SIZE);
        dst.printf("HTTP/1.1 200 OK\r\n");
        dst.printf("Connection: close\r\n");
        dst.printf("Content-Type: %s\r\n", content_type.c_str());
        dst.printf("Content-Length: %zu\r\n", content.size());
        dst.printf("\r\n");
        dst.write(content.data(), content.size());
    }
    _reply_ready = true;
    _token->update(false, true);
}

void
HttpConnection::respond_with_error(int code, const vespalib::string &msg)
{
    {
        OutputWriter dst(_output, CHUNK_SIZE);
        dst.printf("HTTP/1.1 %d %s\r\n", code, msg.c_str());
        dst.printf("Connection: close\r\n");
        dst.printf("\r\n");
    }
    _reply_ready = true;
    _token->update(false, true);
}

} // namespace vespalib::portal