-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQuery.h
69 lines (57 loc) · 2.72 KB
/
Query.h
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
#pragma once
#include <boost/asio/spawn.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <string>
#include <system_error>
#include "Action.h"
#include "Exception.h"
#include "Candidate.h"
//TODO: PostQuery
struct GetQuery {
GetQuery(GetAction action, bool print_found, bool verbose) : print_found{print_found}, verbose{verbose},
action{std::move(action)} {}
template <typename Stream>
void execute(Stream& stream, const std::string_view& description, const boost::asio::yield_context& yield) {
boost::system::error_code ec;
boost::beast::flat_buffer buffer;
boost::beast::http::response<boost::beast::http::dynamic_body> response;
boost::beast::http::async_read(stream, buffer, response, yield[ec]);
if (ec) throw AbradeException{"get query", ec};
action.process(response.result_int(), response, description);
const auto status_code = response.result_int();
if (print_found && (status_code >= 200 && status_code < 300)) {
std::cout << "[+] Status of " << description << ": " << status_code << std::endl;
}
else if (verbose) { std::cout << "[-] Status of " << description << ": " << status_code << std::endl; }
}
template <typename Request>
void set_method(Request& request) const { request.method(boost::beast::http::verb::get); }
private:
bool print_found, verbose;
GetAction action;
};
struct HeadQuery {
HeadQuery(HeadAction action, bool print_found, bool verbose) : print_found{print_found}, verbose{verbose},
action {std::move(action)} { }
template <typename Stream>
void execute(Stream& stream, const std::string_view& description, const boost::asio::yield_context& yield) {
boost::system::error_code ec;
boost::beast::flat_buffer buffer;
boost::beast::http::response_parser<boost::beast::http::empty_body> parser;
boost::beast::http::async_read_header(stream, buffer, parser, yield[ec]);
if (ec) throw AbradeException{"head query", ec};
const auto& response = parser.release();
action.process(response.result_int(), description);
const auto status_code = response.result_int();
if (print_found && (status_code >= 200 && status_code < 300)) {
std::cout << "[+] Status of " << description << ": " << status_code << std::endl;
}
else if (verbose) { std::cout << "[-] Status of " << description << ": " << status_code << std::endl; }
}
template <typename Request>
void set_method(Request& request) const { request.method(boost::beast::http::verb::head); }
private:
bool print_found, verbose;
HeadAction action;
};