2020-09-17 23:02:40 +03:00
|
|
|
// Copyright (c) Vitaliy Filippov, 2019+
|
2021-02-06 01:26:07 +03:00
|
|
|
// License: VNPL-1.1 or GNU GPL-2.0+ (see README.md for details)
|
2020-09-17 23:02:40 +03:00
|
|
|
|
2020-04-15 15:47:06 +03:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
2020-05-21 11:39:01 +03:00
|
|
|
#include <arpa/inet.h>
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2020-04-16 23:22:32 +03:00
|
|
|
#include <ctype.h>
|
2020-05-21 11:39:01 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2020-04-16 23:22:32 +03:00
|
|
|
|
2020-10-06 02:35:11 +03:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2021-12-19 01:14:20 +03:00
|
|
|
#include "addr_util.h"
|
2020-04-25 23:11:50 +03:00
|
|
|
#include "json11/json11.hpp"
|
2020-05-21 11:39:01 +03:00
|
|
|
#include "http_client.h"
|
|
|
|
#include "timerfd_manager.h"
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2020-04-25 23:11:50 +03:00
|
|
|
#define READ_BUFFER_SIZE 9000
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2020-04-26 01:33:50 +03:00
|
|
|
static std::string trim(const std::string & in);
|
2020-04-25 23:11:50 +03:00
|
|
|
static std::string ws_format_frame(int type, uint64_t size);
|
|
|
|
static bool ws_parse_frame(std::string & buf, int & type, std::string & res);
|
2022-01-02 20:03:51 +03:00
|
|
|
static void parse_http_headers(std::string & res, http_response_t *parsed);
|
2020-04-15 15:47:06 +03:00
|
|
|
|
|
|
|
struct http_co_t
|
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
timerfd_manager_t *tfd;
|
2022-01-02 20:03:51 +03:00
|
|
|
std::function<void(const http_response_t*)> response_callback;
|
2020-04-25 23:11:50 +03:00
|
|
|
|
|
|
|
int request_timeout = 0;
|
2020-04-15 15:47:06 +03:00
|
|
|
std::string host;
|
|
|
|
std::string request;
|
2020-04-26 01:33:50 +03:00
|
|
|
std::string ws_outbox;
|
2020-04-15 15:47:06 +03:00
|
|
|
std::string response;
|
2020-04-25 23:11:50 +03:00
|
|
|
bool want_streaming;
|
2022-01-02 20:03:51 +03:00
|
|
|
bool keepalive;
|
2020-04-25 01:29:31 +03:00
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
std::vector<std::function<void()>> keepalive_queue;
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2020-04-25 23:11:50 +03:00
|
|
|
int state = 0;
|
2022-01-02 20:03:51 +03:00
|
|
|
std::string connected_host;
|
2020-04-15 15:47:06 +03:00
|
|
|
int peer_fd = -1;
|
2020-04-20 15:43:07 +03:00
|
|
|
int timeout_id = -1;
|
2020-04-15 15:47:06 +03:00
|
|
|
int epoll_events = 0;
|
2020-04-25 01:29:31 +03:00
|
|
|
int sent = 0;
|
2020-04-25 23:11:50 +03:00
|
|
|
std::vector<char> rbuf;
|
|
|
|
iovec read_iov, send_iov;
|
|
|
|
msghdr read_msg = { 0 }, send_msg = { 0 };
|
2022-01-02 20:03:51 +03:00
|
|
|
http_response_t parsed;
|
|
|
|
uint64_t target_response_size = 0;
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2020-06-01 00:47:49 +03:00
|
|
|
int onstack = 0;
|
|
|
|
bool ended = false;
|
|
|
|
|
2020-04-15 15:47:06 +03:00
|
|
|
~http_co_t();
|
2020-06-01 00:47:49 +03:00
|
|
|
inline void stackin() { onstack++; }
|
|
|
|
inline void stackout() { onstack--; if (!onstack && ended) end(); }
|
|
|
|
inline void end() { ended = true; if (!onstack) { delete this; } }
|
2020-04-26 01:33:50 +03:00
|
|
|
void start_connection();
|
2022-01-02 20:03:51 +03:00
|
|
|
void close_connection();
|
2020-05-31 15:03:27 +03:00
|
|
|
void handle_events();
|
2020-04-25 23:11:50 +03:00
|
|
|
void handle_connect_result();
|
|
|
|
void submit_read();
|
|
|
|
void submit_send();
|
2020-04-30 02:15:49 +03:00
|
|
|
bool handle_read();
|
2020-04-25 23:11:50 +03:00
|
|
|
void post_message(int type, const std::string & msg);
|
2022-01-02 20:03:51 +03:00
|
|
|
void send_request(const std::string & host, const std::string & request,
|
|
|
|
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback);
|
2020-04-15 15:47:06 +03:00
|
|
|
};
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
#define HTTP_CO_CLOSED 0
|
2020-04-25 23:11:50 +03:00
|
|
|
#define HTTP_CO_CONNECTING 1
|
|
|
|
#define HTTP_CO_SENDING_REQUEST 2
|
|
|
|
#define HTTP_CO_REQUEST_SENT 3
|
|
|
|
#define HTTP_CO_HEADERS_RECEIVED 4
|
|
|
|
#define HTTP_CO_WEBSOCKET 5
|
2020-04-26 01:33:50 +03:00
|
|
|
#define HTTP_CO_CHUNKED 6
|
2022-01-02 20:03:51 +03:00
|
|
|
#define HTTP_CO_KEEPALIVE 7
|
2020-04-25 23:11:50 +03:00
|
|
|
|
2020-04-27 14:32:59 +03:00
|
|
|
#define DEFAULT_TIMEOUT 5000
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
http_co_t *http_init(timerfd_manager_t *tfd)
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
|
|
|
http_co_t *handler = new http_co_t();
|
2020-04-25 23:11:50 +03:00
|
|
|
handler->tfd = tfd;
|
2022-01-02 20:03:51 +03:00
|
|
|
handler->state = HTTP_CO_CLOSED;
|
|
|
|
return handler;
|
2020-04-16 23:22:32 +03:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
http_co_t* open_websocket(timerfd_manager_t *tfd, const std::string & host, const std::string & path,
|
|
|
|
int timeout, std::function<void(const http_response_t *msg)> response_callback)
|
2020-04-16 23:22:32 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
std::string request = "GET "+path+" HTTP/1.1\r\n"
|
|
|
|
"Host: "+host+"\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Connection: upgrade\r\n"
|
|
|
|
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
|
|
|
"\r\n";
|
|
|
|
http_co_t *handler = new http_co_t();
|
2020-04-27 14:32:59 +03:00
|
|
|
handler->tfd = tfd;
|
2022-01-02 20:03:51 +03:00
|
|
|
handler->state = HTTP_CO_CLOSED;
|
2020-04-25 23:11:50 +03:00
|
|
|
handler->host = host;
|
2022-01-02 20:03:51 +03:00
|
|
|
handler->request_timeout = timeout < 0 ? -1 : (timeout == 0 ? DEFAULT_TIMEOUT : timeout);
|
|
|
|
handler->want_streaming = false;
|
|
|
|
handler->keepalive = false;
|
2020-04-25 23:11:50 +03:00
|
|
|
handler->request = request;
|
2022-01-02 20:03:51 +03:00
|
|
|
handler->response_callback = response_callback;
|
2020-04-26 01:33:50 +03:00
|
|
|
handler->start_connection();
|
2022-01-02 20:03:51 +03:00
|
|
|
return handler;
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
void http_request(http_co_t *handler, const std::string & host, const std::string & request,
|
|
|
|
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
handler->send_request(host, request, options, response_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_co_t::send_request(const std::string & host, const std::string & request,
|
|
|
|
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback)
|
|
|
|
{
|
|
|
|
stackin();
|
|
|
|
if (state == HTTP_CO_WEBSOCKET)
|
|
|
|
{
|
|
|
|
stackout();
|
|
|
|
throw std::runtime_error("Attempt to send HTTP request into a websocket or chunked stream");
|
|
|
|
}
|
|
|
|
else if (state != HTTP_CO_KEEPALIVE && state != HTTP_CO_CLOSED)
|
|
|
|
{
|
|
|
|
keepalive_queue.push_back([this, host, request, options, response_callback]()
|
|
|
|
{
|
|
|
|
this->send_request(host, request, options, response_callback);
|
|
|
|
});
|
|
|
|
stackout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->request_timeout = options.timeout < 0 ? 0 : (options.timeout == 0 ? DEFAULT_TIMEOUT : options.timeout);
|
|
|
|
this->want_streaming = options.want_streaming;
|
|
|
|
this->keepalive = options.keepalive;
|
|
|
|
this->host = host;
|
|
|
|
this->request = request;
|
|
|
|
this->response = "";
|
|
|
|
this->sent = 0;
|
|
|
|
this->response_callback = response_callback;
|
|
|
|
if (state == HTTP_CO_KEEPALIVE && connected_host != host)
|
|
|
|
{
|
|
|
|
close_connection();
|
|
|
|
}
|
|
|
|
if (request_timeout > 0)
|
|
|
|
{
|
|
|
|
timeout_id = tfd->set_timer(request_timeout, false, [this](int timer_id)
|
|
|
|
{
|
|
|
|
stackin();
|
|
|
|
close_connection();
|
|
|
|
parsed = { .error = "HTTP request timed out" };
|
|
|
|
this->response_callback(&parsed);
|
|
|
|
this->response_callback = NULL;
|
|
|
|
stackout();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (state == HTTP_CO_KEEPALIVE)
|
|
|
|
{
|
|
|
|
state = HTTP_CO_SENDING_REQUEST;
|
|
|
|
submit_send();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_connection();
|
|
|
|
}
|
|
|
|
stackout();
|
2020-04-16 23:22:32 +03:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
void http_post_message(http_co_t *handler, int type, const std::string & msg)
|
2020-04-26 01:33:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
handler->post_message(type, msg);
|
2020-04-26 01:33:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
void http_co_t::post_message(int type, const std::string & msg)
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
stackin();
|
|
|
|
if (state == HTTP_CO_WEBSOCKET)
|
2020-04-20 15:43:07 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
request += ws_format_frame(type, msg.size());
|
|
|
|
request += msg;
|
|
|
|
submit_send();
|
2020-04-20 15:43:07 +03:00
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
else if (state == HTTP_CO_KEEPALIVE || state == HTTP_CO_CHUNKED)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
throw std::runtime_error("Attempt to send websocket message on a regular HTTP connection");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws_outbox += ws_format_frame(type, msg.size());
|
|
|
|
ws_outbox += msg;
|
|
|
|
}
|
|
|
|
stackout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_close(http_co_t *handler)
|
|
|
|
{
|
|
|
|
handler->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_response_t::parse_json_response(std::string & error, json11::Json & r) const
|
|
|
|
{
|
|
|
|
if (this->error != "")
|
|
|
|
{
|
|
|
|
error = this->error;
|
|
|
|
r = json11::Json();
|
|
|
|
}
|
|
|
|
else if (status_code != 200)
|
|
|
|
{
|
|
|
|
error = "HTTP "+std::to_string(status_code)+" "+status_line+" body: "+trim(body);
|
|
|
|
r = json11::Json();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string json_err;
|
|
|
|
json11::Json data = json11::Json::parse(body, json_err);
|
|
|
|
if (json_err != "")
|
|
|
|
{
|
|
|
|
error = "Bad JSON: "+json_err+" (response: "+trim(body)+")";
|
|
|
|
r = json11::Json();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = "";
|
|
|
|
r = data;
|
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
http_co_t::~http_co_t()
|
|
|
|
{
|
|
|
|
close_connection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_co_t::close_connection()
|
|
|
|
{
|
|
|
|
if (timeout_id >= 0)
|
|
|
|
{
|
|
|
|
tfd->clear_timer(timeout_id);
|
|
|
|
timeout_id = -1;
|
|
|
|
}
|
|
|
|
if (peer_fd >= 0)
|
|
|
|
{
|
|
|
|
tfd->set_fd_handler(peer_fd, false, NULL);
|
|
|
|
close(peer_fd);
|
|
|
|
peer_fd = -1;
|
|
|
|
}
|
|
|
|
state = HTTP_CO_CLOSED;
|
|
|
|
connected_host = "";
|
|
|
|
response = "";
|
2020-04-15 15:47:06 +03:00
|
|
|
}
|
|
|
|
|
2020-04-26 01:33:50 +03:00
|
|
|
void http_co_t::start_connection()
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
2020-06-01 00:47:49 +03:00
|
|
|
stackin();
|
2021-12-19 01:14:20 +03:00
|
|
|
struct sockaddr addr;
|
|
|
|
if (!string_to_addr(host.c_str(), 1, 80, &addr))
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
parsed = { .error = "Invalid address: "+host };
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-04-25 23:11:50 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-12-19 01:14:20 +03:00
|
|
|
peer_fd = socket(addr.sa_family, SOCK_STREAM, 0);
|
2020-04-25 23:11:50 +03:00
|
|
|
if (peer_fd < 0)
|
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
parsed = { .error = std::string("socket: ")+strerror(errno) };
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-04-25 23:11:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fcntl(peer_fd, F_SETFL, fcntl(peer_fd, F_GETFL, 0) | O_NONBLOCK);
|
2020-05-31 15:03:27 +03:00
|
|
|
epoll_events = 0;
|
|
|
|
// Finally call connect
|
2021-12-19 01:14:20 +03:00
|
|
|
int r = ::connect(peer_fd, (sockaddr*)&addr, sizeof(addr));
|
2020-05-31 15:03:27 +03:00
|
|
|
if (r < 0 && errno != EINPROGRESS)
|
|
|
|
{
|
2022-01-20 00:44:18 +03:00
|
|
|
close_connection();
|
2022-01-02 20:03:51 +03:00
|
|
|
parsed = { .error = std::string("connect: ")+strerror(errno) };
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-05-31 15:03:27 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-06-23 20:18:04 +03:00
|
|
|
tfd->set_fd_handler(peer_fd, true, [this](int peer_fd, int epoll_events)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
|
|
|
this->epoll_events |= epoll_events;
|
2020-05-31 15:03:27 +03:00
|
|
|
handle_events();
|
|
|
|
});
|
2022-01-02 20:03:51 +03:00
|
|
|
connected_host = host;
|
2020-05-31 15:03:27 +03:00
|
|
|
state = HTTP_CO_CONNECTING;
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-05-31 15:03:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_co_t::handle_events()
|
|
|
|
{
|
2020-06-01 00:47:49 +03:00
|
|
|
stackin();
|
2020-05-31 15:03:27 +03:00
|
|
|
while (epoll_events)
|
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
if (state == HTTP_CO_CONNECTING)
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
handle_connect_result();
|
2020-04-15 15:47:06 +03:00
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
else
|
2020-04-20 15:43:07 +03:00
|
|
|
{
|
2020-05-31 15:03:27 +03:00
|
|
|
epoll_events &= ~EPOLLOUT;
|
|
|
|
if (epoll_events & EPOLLIN)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
|
|
|
submit_read();
|
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
else if (epoll_events & (EPOLLRDHUP|EPOLLERR))
|
2020-04-20 15:43:07 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
close_connection();
|
|
|
|
if (response_callback != NULL)
|
|
|
|
{
|
|
|
|
parsed.eof = true;
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
|
|
|
parsed = {};
|
|
|
|
}
|
2020-06-01 00:47:49 +03:00
|
|
|
break;
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
2020-04-20 15:43:07 +03:00
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_co_t::handle_connect_result()
|
|
|
|
{
|
2020-06-01 00:47:49 +03:00
|
|
|
stackin();
|
2020-05-31 15:03:27 +03:00
|
|
|
int result = 0;
|
|
|
|
socklen_t result_len = sizeof(result);
|
|
|
|
if (getsockopt(peer_fd, SOL_SOCKET, SO_ERROR, &result, &result_len) < 0)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2020-05-31 15:03:27 +03:00
|
|
|
result = errno;
|
2020-04-15 15:47:06 +03:00
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
if (result != 0)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
close_connection();
|
2022-01-20 00:44:18 +03:00
|
|
|
if (response_callback != NULL)
|
|
|
|
{
|
|
|
|
parsed = { .error = std::string("connect: ")+strerror(result) };
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
|
|
|
}
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-05-31 15:03:27 +03:00
|
|
|
return;
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
2020-05-31 15:03:27 +03:00
|
|
|
int one = 1;
|
|
|
|
setsockopt(peer_fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
|
2020-06-23 20:18:04 +03:00
|
|
|
tfd->set_fd_handler(peer_fd, false, [this](int peer_fd, int epoll_events)
|
|
|
|
{
|
|
|
|
this->epoll_events |= epoll_events;
|
|
|
|
handle_events();
|
|
|
|
});
|
2020-05-31 15:03:27 +03:00
|
|
|
state = HTTP_CO_SENDING_REQUEST;
|
|
|
|
submit_send();
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void http_co_t::submit_send()
|
|
|
|
{
|
2020-06-01 00:47:49 +03:00
|
|
|
stackin();
|
2020-05-21 01:23:43 +03:00
|
|
|
int res;
|
|
|
|
again:
|
|
|
|
if (sent < request.size())
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
send_iov = (iovec){ .iov_base = (void*)(request.c_str()+sent), .iov_len = request.size()-sent };
|
|
|
|
send_msg.msg_iov = &send_iov;
|
|
|
|
send_msg.msg_iovlen = 1;
|
2020-06-02 11:41:08 +03:00
|
|
|
res = sendmsg(peer_fd, &send_msg, MSG_NOSIGNAL);
|
2020-05-21 01:23:43 +03:00
|
|
|
if (res < 0)
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
2020-05-21 01:23:43 +03:00
|
|
|
res = -errno;
|
|
|
|
}
|
|
|
|
if (res == -EAGAIN)
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
else if (res < 0)
|
|
|
|
{
|
2022-01-20 00:44:18 +03:00
|
|
|
close_connection();
|
|
|
|
if (response_callback != NULL)
|
|
|
|
{
|
|
|
|
parsed = { .error = std::string("sendmsg: ")+strerror(errno) };
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
|
|
|
}
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-05-21 01:23:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sent += res;
|
|
|
|
if (state == HTTP_CO_SENDING_REQUEST)
|
|
|
|
{
|
|
|
|
if (sent >= request.size())
|
|
|
|
state = HTTP_CO_REQUEST_SENT;
|
|
|
|
else
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
else if (state == HTTP_CO_WEBSOCKET)
|
|
|
|
{
|
|
|
|
request = request.substr(sent);
|
|
|
|
sent = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
2020-04-15 15:47:06 +03:00
|
|
|
}
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
void http_co_t::submit_read()
|
|
|
|
{
|
|
|
|
stackin();
|
|
|
|
int res;
|
|
|
|
if (rbuf.size() != READ_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
rbuf.resize(READ_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
read_iov = { .iov_base = rbuf.data(), .iov_len = READ_BUFFER_SIZE };
|
|
|
|
read_msg.msg_iov = &read_iov;
|
|
|
|
read_msg.msg_iovlen = 1;
|
|
|
|
res = recvmsg(peer_fd, &read_msg, 0);
|
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
res = -errno;
|
|
|
|
}
|
|
|
|
if (res == -EAGAIN)
|
|
|
|
{
|
|
|
|
epoll_events = epoll_events & ~EPOLLIN;
|
|
|
|
}
|
|
|
|
else if (res <= 0)
|
|
|
|
{
|
|
|
|
// < 0 means error, 0 means EOF
|
2022-01-20 00:44:18 +03:00
|
|
|
epoll_events = epoll_events & ~EPOLLIN;
|
|
|
|
close_connection();
|
|
|
|
if (res < 0)
|
|
|
|
parsed = { .error = std::string("recvmsg: ")+strerror(-res) };
|
|
|
|
else
|
|
|
|
parsed.eof = true;
|
|
|
|
if (response_callback != NULL)
|
|
|
|
{
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response += std::string(rbuf.data(), res);
|
|
|
|
handle_read();
|
|
|
|
}
|
|
|
|
stackout();
|
|
|
|
}
|
|
|
|
|
2020-04-30 02:15:49 +03:00
|
|
|
bool http_co_t::handle_read()
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2020-06-01 00:47:49 +03:00
|
|
|
stackin();
|
2020-04-25 23:11:50 +03:00
|
|
|
if (state == HTTP_CO_REQUEST_SENT)
|
2020-04-15 15:47:06 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
int pos = response.find("\r\n\r\n");
|
|
|
|
if (pos >= 0)
|
2020-04-25 01:29:31 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
if (timeout_id >= 0)
|
2020-04-25 01:29:31 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
// Timeout is cleared when headers are received
|
2020-04-25 23:11:50 +03:00
|
|
|
tfd->clear_timer(timeout_id);
|
|
|
|
timeout_id = -1;
|
2020-04-25 01:29:31 +03:00
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
state = HTTP_CO_HEADERS_RECEIVED;
|
|
|
|
parse_http_headers(response, &parsed);
|
|
|
|
if (parsed.status_code == 101 &&
|
|
|
|
parsed.headers.find("sec-websocket-accept") != parsed.headers.end() &&
|
|
|
|
parsed.headers["upgrade"] == "websocket" &&
|
|
|
|
parsed.headers["connection"] == "upgrade")
|
2020-04-25 01:29:31 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
// Don't care about validating the key
|
|
|
|
state = HTTP_CO_WEBSOCKET;
|
2020-04-26 01:33:50 +03:00
|
|
|
request = ws_outbox;
|
|
|
|
ws_outbox = "";
|
2020-04-25 23:11:50 +03:00
|
|
|
sent = 0;
|
2020-04-26 01:33:50 +03:00
|
|
|
submit_send();
|
2020-04-25 01:29:31 +03:00
|
|
|
}
|
2020-04-26 01:33:50 +03:00
|
|
|
else if (parsed.headers["transfer-encoding"] == "chunked")
|
2020-04-25 01:29:31 +03:00
|
|
|
{
|
2020-04-26 01:33:50 +03:00
|
|
|
state = HTTP_CO_CHUNKED;
|
|
|
|
}
|
|
|
|
else if (parsed.headers["connection"] != "close")
|
|
|
|
{
|
|
|
|
target_response_size = stoull_full(parsed.headers["content-length"]);
|
|
|
|
if (!target_response_size)
|
|
|
|
{
|
|
|
|
// Sorry, unsupported response
|
2022-01-02 20:03:51 +03:00
|
|
|
close_connection();
|
|
|
|
parsed = { .error = "Response has neither Connection: close, nor Transfer-Encoding: chunked nor Content-Length headers" };
|
|
|
|
response_callback(&parsed);
|
|
|
|
response_callback = NULL;
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2020-04-30 02:15:49 +03:00
|
|
|
return false;
|
2020-04-26 01:33:50 +03:00
|
|
|
}
|
2020-04-25 01:29:31 +03:00
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
keepalive = false;
|
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-26 01:33:50 +03:00
|
|
|
if (state == HTTP_CO_HEADERS_RECEIVED && target_response_size > 0 && response.size() >= target_response_size)
|
|
|
|
{
|
2022-01-03 20:08:26 +03:00
|
|
|
std::swap(parsed.body, response);
|
2022-01-02 20:03:51 +03:00
|
|
|
response_callback(&parsed);
|
|
|
|
parsed.eof = true;
|
2020-04-26 01:33:50 +03:00
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
else if (state == HTTP_CO_CHUNKED && response.size() > 0)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
|
|
|
int prev = 0, pos = 0;
|
|
|
|
while ((pos = response.find("\r\n", prev)) >= prev)
|
|
|
|
{
|
|
|
|
uint64_t len = strtoull(response.c_str()+prev, NULL, 16);
|
2020-04-26 01:33:50 +03:00
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
// Zero length chunk indicates EOF
|
2020-04-27 14:32:59 +03:00
|
|
|
parsed.eof = true;
|
|
|
|
break;
|
2020-04-26 01:33:50 +03:00
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
if (response.size() < pos+2+len+2)
|
2020-04-25 01:29:31 +03:00
|
|
|
{
|
2020-04-25 23:11:50 +03:00
|
|
|
break;
|
2020-04-25 01:29:31 +03:00
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
parsed.body += response.substr(pos+2, len);
|
|
|
|
prev = pos+2+len+2;
|
|
|
|
}
|
|
|
|
if (prev > 0)
|
|
|
|
{
|
|
|
|
response = response.substr(prev);
|
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
if (want_streaming)
|
2020-04-27 14:32:59 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
// Streaming response
|
|
|
|
response_callback(&parsed);
|
|
|
|
parsed.body = "";
|
2020-04-27 14:32:59 +03:00
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
if (parsed.eof && !want_streaming)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
// Normal response
|
|
|
|
response_callback(&parsed);
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
else if (state == HTTP_CO_WEBSOCKET && response.size() > 0)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
|
|
|
while (ws_parse_frame(response, parsed.ws_msg_type, parsed.body))
|
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
response_callback(&parsed);
|
2020-04-25 23:11:50 +03:00
|
|
|
parsed.body = "";
|
2020-04-25 01:29:31 +03:00
|
|
|
}
|
2020-04-15 15:47:06 +03:00
|
|
|
}
|
2022-01-02 20:03:51 +03:00
|
|
|
if (parsed.eof)
|
2020-04-26 01:33:50 +03:00
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
response_callback = NULL;
|
|
|
|
parsed = {};
|
|
|
|
if (!keepalive)
|
|
|
|
{
|
|
|
|
close_connection();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = HTTP_CO_KEEPALIVE;
|
|
|
|
if (keepalive_queue.size() > 0)
|
|
|
|
{
|
|
|
|
auto next = keepalive_queue[0];
|
|
|
|
keepalive_queue.erase(keepalive_queue.begin(), keepalive_queue.begin()+1);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 01:33:50 +03:00
|
|
|
}
|
2020-06-01 00:47:49 +03:00
|
|
|
stackout();
|
2022-01-02 20:03:51 +03:00
|
|
|
return true;
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 17:44:03 +03:00
|
|
|
uint64_t stoull_full(const std::string & str, int base)
|
2020-04-20 15:43:07 +03:00
|
|
|
{
|
|
|
|
if (isspace(str[0]))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-20 17:44:03 +03:00
|
|
|
char *end = NULL;
|
|
|
|
uint64_t r = strtoull(str.c_str(), &end, base);
|
|
|
|
if (end != str.c_str()+str.length())
|
2020-04-20 15:43:07 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2020-04-25 23:11:50 +03:00
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
static void parse_http_headers(std::string & res, http_response_t *parsed)
|
2020-04-25 23:11:50 +03:00
|
|
|
{
|
|
|
|
int pos = res.find("\r\n");
|
|
|
|
pos = pos < 0 ? res.length() : pos+2;
|
|
|
|
std::string status_line = res.substr(0, pos);
|
|
|
|
int http_version;
|
|
|
|
char *status_text = NULL;
|
|
|
|
sscanf(status_line.c_str(), "HTTP/1.%d %d %ms", &http_version, &parsed->status_code, &status_text);
|
|
|
|
if (status_text)
|
|
|
|
{
|
|
|
|
parsed->status_line = status_text;
|
|
|
|
// %ms = allocate a buffer
|
|
|
|
free(status_text);
|
|
|
|
status_text = NULL;
|
|
|
|
}
|
|
|
|
int prev = pos;
|
|
|
|
while ((pos = res.find("\r\n", prev)) >= prev)
|
|
|
|
{
|
|
|
|
if (pos == prev)
|
|
|
|
{
|
|
|
|
res = res.substr(pos+2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string header = res.substr(prev, pos-prev);
|
|
|
|
int p2 = header.find(":");
|
|
|
|
if (p2 >= 0)
|
|
|
|
{
|
2020-04-26 01:33:50 +03:00
|
|
|
std::string key = strtolower(header.substr(0, p2));
|
2020-04-25 23:11:50 +03:00
|
|
|
int p3 = p2+1;
|
|
|
|
while (p3 < header.length() && isblank(header[p3]))
|
|
|
|
p3++;
|
2020-04-26 01:33:50 +03:00
|
|
|
parsed->headers[key] = key == "connection" || key == "upgrade" || key == "transfer-encoding"
|
|
|
|
? strtolower(header.substr(p3)) : header.substr(p3);
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
prev = pos+2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string ws_format_frame(int type, uint64_t size)
|
|
|
|
{
|
|
|
|
// Always zero mask
|
|
|
|
std::string res;
|
|
|
|
int p = 0;
|
|
|
|
res.resize(2 + (size >= 126 ? 2 : 0) + (size >= 65536 ? 6 : 0) + /*mask*/4);
|
|
|
|
res[p++] = 0x80 | type;
|
|
|
|
if (size < 126)
|
|
|
|
res[p++] = size | /*mask*/0x80;
|
|
|
|
else if (size < 65536)
|
|
|
|
{
|
2022-01-15 23:55:10 +03:00
|
|
|
res[p++] = (char)(126 | /*mask*/0x80);
|
2020-04-25 23:11:50 +03:00
|
|
|
res[p++] = (size >> 8) & 0xFF;
|
|
|
|
res[p++] = (size >> 0) & 0xFF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-15 23:55:10 +03:00
|
|
|
res[p++] = (char)(127 | /*mask*/0x80);
|
2020-04-25 23:11:50 +03:00
|
|
|
res[p++] = (size >> 56) & 0xFF;
|
|
|
|
res[p++] = (size >> 48) & 0xFF;
|
|
|
|
res[p++] = (size >> 40) & 0xFF;
|
|
|
|
res[p++] = (size >> 32) & 0xFF;
|
|
|
|
res[p++] = (size >> 24) & 0xFF;
|
|
|
|
res[p++] = (size >> 16) & 0xFF;
|
|
|
|
res[p++] = (size >> 8) & 0xFF;
|
|
|
|
res[p++] = (size >> 0) & 0xFF;
|
|
|
|
}
|
|
|
|
res[p++] = 0;
|
|
|
|
res[p++] = 0;
|
|
|
|
res[p++] = 0;
|
|
|
|
res[p++] = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ws_parse_frame(std::string & buf, int & type, std::string & res)
|
|
|
|
{
|
|
|
|
uint64_t hdr = 2;
|
|
|
|
if (buf.size() < hdr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
type = buf[0] & ~0x80;
|
2020-04-26 01:33:50 +03:00
|
|
|
bool mask = !!(buf[1] & 0x80);
|
2020-04-25 23:11:50 +03:00
|
|
|
hdr += mask ? 4 : 0;
|
2020-04-26 01:33:50 +03:00
|
|
|
uint64_t len = ((uint8_t)buf[1] & ~0x80);
|
2020-04-25 23:11:50 +03:00
|
|
|
if (len == 126)
|
|
|
|
{
|
|
|
|
hdr += 2;
|
|
|
|
if (buf.size() < hdr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-26 01:33:50 +03:00
|
|
|
len = ((uint64_t)(uint8_t)buf[2] << 8) | ((uint64_t)(uint8_t)buf[3] << 0);
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
else if (len == 127)
|
|
|
|
{
|
|
|
|
hdr += 8;
|
|
|
|
if (buf.size() < hdr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-26 01:33:50 +03:00
|
|
|
len = ((uint64_t)(uint8_t)buf[2] << 56) |
|
|
|
|
((uint64_t)(uint8_t)buf[3] << 48) |
|
|
|
|
((uint64_t)(uint8_t)buf[4] << 40) |
|
|
|
|
((uint64_t)(uint8_t)buf[5] << 32) |
|
|
|
|
((uint64_t)(uint8_t)buf[6] << 24) |
|
|
|
|
((uint64_t)(uint8_t)buf[7] << 16) |
|
|
|
|
((uint64_t)(uint8_t)buf[8] << 8) |
|
|
|
|
((uint64_t)(uint8_t)buf[9] << 0);
|
2020-04-25 23:11:50 +03:00
|
|
|
}
|
|
|
|
if (buf.size() < hdr+len)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mask)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
buf[hdr+i] ^= buf[hdr-4+(i & 3)];
|
|
|
|
}
|
|
|
|
res += buf.substr(hdr, len);
|
|
|
|
buf = buf.substr(hdr+len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-13 23:45:16 +03:00
|
|
|
std::string strtolower(const std::string & in)
|
2020-04-26 01:33:50 +03:00
|
|
|
{
|
|
|
|
std::string s = in;
|
|
|
|
for (int i = 0; i < s.length(); i++)
|
|
|
|
{
|
|
|
|
s[i] = tolower(s[i]);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string trim(const std::string & in)
|
|
|
|
{
|
|
|
|
int begin = in.find_first_not_of(" \n\r\t");
|
|
|
|
if (begin == -1)
|
|
|
|
return "";
|
|
|
|
int end = in.find_last_not_of(" \n\r\t");
|
|
|
|
return in.substr(begin, end+1-begin);
|
|
|
|
}
|