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
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2020-04-16 23:22:32 +03:00
|
|
|
#include <map>
|
2020-05-21 11:39:01 +03:00
|
|
|
#include <functional>
|
|
|
|
#include "json11/json11.hpp"
|
2020-04-25 23:11:50 +03:00
|
|
|
|
|
|
|
#define WS_CONTINUATION 0
|
|
|
|
#define WS_TEXT 1
|
|
|
|
#define WS_BINARY 2
|
|
|
|
#define WS_CLOSE 8
|
|
|
|
#define WS_PING 9
|
|
|
|
#define WS_PONG 10
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2020-05-21 11:39:01 +03:00
|
|
|
class timerfd_manager_t;
|
|
|
|
|
2020-04-27 14:32:59 +03:00
|
|
|
struct http_options_t
|
|
|
|
{
|
|
|
|
int timeout;
|
|
|
|
bool want_streaming;
|
2022-01-02 20:03:51 +03:00
|
|
|
bool keepalive;
|
2020-04-27 14:32:59 +03:00
|
|
|
};
|
|
|
|
|
2020-04-16 23:22:32 +03:00
|
|
|
struct http_response_t
|
|
|
|
{
|
2022-01-02 20:03:51 +03:00
|
|
|
std::string error;
|
|
|
|
|
2020-04-25 01:29:31 +03:00
|
|
|
bool eof = false;
|
|
|
|
int status_code = 0;
|
2020-04-16 23:22:32 +03:00
|
|
|
std::string status_line;
|
|
|
|
std::map<std::string, std::string> headers;
|
2020-04-25 23:11:50 +03:00
|
|
|
int ws_msg_type = -1;
|
2020-04-16 23:22:32 +03:00
|
|
|
std::string body;
|
2020-04-15 15:47:06 +03:00
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
void parse_json_response(std::string & error, json11::Json & r) const;
|
2020-04-25 23:11:50 +03:00
|
|
|
};
|
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
// Opened websocket or keepalive HTTP connection
|
|
|
|
struct http_co_t;
|
2020-05-21 11:39:01 +03:00
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
http_co_t* http_init(timerfd_manager_t *tfd);
|
|
|
|
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)> on_message);
|
|
|
|
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);
|
|
|
|
void http_post_message(http_co_t *handler, int type, const std::string & msg);
|
|
|
|
void http_close(http_co_t *co);
|
2020-05-21 11:39:01 +03:00
|
|
|
|
2022-01-02 20:03:51 +03:00
|
|
|
// Utils
|
2020-04-20 17:44:03 +03:00
|
|
|
uint64_t stoull_full(const std::string & str, int base = 10);
|
2021-03-13 23:45:16 +03:00
|
|
|
std::string strtolower(const std::string & in);
|