RequestResponse.cpp109 lines · 2.5 KiB
| 1 | #include "RequestResponse.hpp" |
| 2 | |
| 3 | RequestResponse::RequestResponse(char const * const uri_path, struct evhttp_request * const req) |
| 4 | : m_UriPath(uri_path), m_Request(req), m_InputHeader(nullptr), m_OutputHeader(nullptr) |
| 5 | { |
| 6 | } |
| 7 | |
| 8 | RequestResponse::~RequestResponse() |
| 9 | { |
| 10 | evhttp_clear_headers(&m_Query); |
| 11 | } |
| 12 | |
| 13 | void RequestResponse::UseInputHeader() |
| 14 | { |
| 15 | m_InputHeader = evhttp_request_get_input_headers(m_Request); |
| 16 | } |
| 17 | |
| 18 | void RequestResponse::UseOutputHeader() |
| 19 | { |
| 20 | m_OutputHeader = evhttp_request_get_output_headers(m_Request); |
| 21 | } |
| 22 | |
| 23 | bool RequestResponse::UseUri() |
| 24 | { |
| 25 | struct evhttp_uri const * const uri = evhttp_request_get_evhttp_uri(m_Request); |
| 26 | if (uri == nullptr) |
| 27 | { |
| 28 | return false; |
| 29 | } |
| 30 | char const * const query = evhttp_uri_get_query(uri); |
| 31 | evhttp_parse_query_str(query, &m_Query); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool RequestResponse::AddOutputHeaderByRef(std::string const & key, std::string const & value) |
| 36 | { |
| 37 | return evhttp_add_header(m_OutputHeader, key.c_str(), value.c_str()) == 0; |
| 38 | } |
| 39 | |
| 40 | bool RequestResponse::AddOutputHeader(std::string const key, std::string const value) |
| 41 | { |
| 42 | return AddOutputHeaderByRef(key, value); |
| 43 | } |
| 44 | |
| 45 | bool RequestResponse::RemoveOutputHeaderByRef(std::string const & key) |
| 46 | { |
| 47 | return evhttp_remove_header(m_OutputHeader, key.c_str()) == 0; |
| 48 | } |
| 49 | |
| 50 | bool RequestResponse::RemoveOutputHeader(std::string const key) |
| 51 | { |
| 52 | return RemoveOutputHeaderByRef(key); |
| 53 | } |
| 54 | |
| 55 | bool RequestResponse::GetInputHeaderByRef(std::string const & key, std::string & value) |
| 56 | { |
| 57 | char const * const v = evhttp_find_header(m_InputHeader, key.c_str()); |
| 58 | |
| 59 | if (v == nullptr) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | value = v; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool RequestResponse::GetInputHeader(std::string const key, std::string value) |
| 69 | { |
| 70 | return GetInputHeaderByRef(key, value); |
| 71 | } |
| 72 | |
| 73 | bool RequestResponse::GetQueryValue(std::string & key, std::string & value) |
| 74 | { |
| 75 | std::string const k = key; |
| 76 | return GetQueryValue(k, value); |
| 77 | } |
| 78 | |
| 79 | bool RequestResponse::GetQueryValue(std::string const key, std::string & value) |
| 80 | { |
| 81 | char const * const v = evhttp_find_header(&m_Query, key.c_str()); |
| 82 | |
| 83 | if (v == nullptr) |
| 84 | { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | value = v; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool RequestResponse::QueryValueEquals(std::string & key, std::string value) |
| 93 | { |
| 94 | std::string const k = key; |
| 95 | return QueryValueEquals(k, value); |
| 96 | } |
| 97 | |
| 98 | bool RequestResponse::QueryValueEquals(std::string const key, std::string value) |
| 99 | { |
| 100 | char const * const v = evhttp_find_header(&m_Query, key.c_str()); |
| 101 | |
| 102 | if (v == nullptr) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return value == std::string(v); |
| 108 | } |
| 109 |