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;
|
|
|
|
};
|
|
|
|
|
2020-04-16 23:22:32 +03:00
|
|
|
struct http_response_t
|
|
|
|
{
|
2020-04-25 01:29:31 +03:00
|
|
|
bool eof = false;
|
|
|
|
int error_code = 0;
|
|
|
|
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
|
|
|
|
2020-04-25 23:11:50 +03:00
|
|
|
struct http_co_t;
|
|
|
|
|
|
|
|
struct websocket_t
|
|
|
|
{
|
|
|
|
http_co_t *co;
|
|
|
|
void post_message(int type, const std::string & msg);
|
2020-04-26 01:33:50 +03:00
|
|
|
void close();
|
2020-04-25 23:11:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void parse_http_headers(std::string & res, http_response_t *parsed);
|
2020-05-21 11:39:01 +03:00
|
|
|
|
2021-12-19 01:14:20 +03:00
|
|
|
std::vector<std::string> getifaddr_list(json11::Json mask_cfg = json11::Json(), bool include_v6 = true);
|
2020-05-21 11:39:01 +03:00
|
|
|
|
2020-04-20 17:44:03 +03:00
|
|
|
uint64_t stoull_full(const std::string & str, int base = 10);
|
2020-05-21 11:39:01 +03:00
|
|
|
|
2021-03-13 23:45:16 +03:00
|
|
|
std::string strtolower(const std::string & in);
|
|
|
|
|
2020-05-21 11:39:01 +03:00
|
|
|
void http_request(timerfd_manager_t *tfd, const std::string & host, const std::string & request,
|
|
|
|
const http_options_t & options, std::function<void(const http_response_t *response)> callback);
|
|
|
|
|
|
|
|
void http_request_json(timerfd_manager_t *tfd, const std::string & host, const std::string & request,
|
|
|
|
int timeout, std::function<void(std::string, json11::Json r)> callback);
|
|
|
|
|
|
|
|
websocket_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)> callback);
|