neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
http.hpp
Go to the documentation of this file.
1// http.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <vector>
40#include <string>
41#include <deque>
42#include <optional>
43#include <variant>
44#include <chrono>
46#include <neolib/task/event.hpp>
49
50namespace neolib
51{
52 class NEOLIB_EXPORT http_packet : public string_packet
53 {
54 // construction
55 public:
56 http_packet(const contents_type& aContents = contents_type()) :
57 string_packet(aContents)
58 {
59 }
60 // implementation
61 private:
62 virtual bool has_delimiters() const
63 {
64 return false;
65 }
66 };
67
69
70 class http
71 {
72 // events
73 public:
75 define_event(Progress, progress)
76 define_event(Completed, completed)
77 define_event(Failure, failure)
78
79 // types
80 public:
81 typedef std::map<ci_string, std::string> headers_t;
82 typedef std::vector<char> body_t;
83 enum type_e { Get, Post };
84
85 // construction
86 public:
87 http(async_task& aIoTask);
88 http(const http& aOther);
89 virtual ~http();
90 http& operator=(const http& aOther);
91 bool operator==(const http&) const { return false; }
92
93 // operations
94 public:
95 void request(const std::string& aUrl, type_e aType = Get, const headers_t& aRequestHeaders = headers_t(), const std::variant<body_t, std::string>& aRequestBody = std::string());
96 void request(const std::string& aHost, const std::string& aResource, type_e aType = Get, unsigned short aPort = 80, bool aSecure = false, const headers_t& aRequestHeaders = headers_t(), const std::variant<body_t, std::string>& aRequestBody = std::string());
97 bool ok() const { return iOk; }
98 uint32_t status_code() const { return iStatusCode; }
99 uint64_t body_length() const { return iBodyLength ? *iBodyLength : iBody.size(); }
100 const std::string& response_status() const { return iResponseStatus; }
101 const headers_t& response_headers() const { return iResponseHeaders; }
102 const body_t& body() const { return iBody; }
103 std::string body_as_string() const { return std::string(iBody.begin(), iBody.end()); }
104 double percent_done() const;
105
106 // implementation
107 private:
108 http_stream& stream();
109 void reset();
110 void add_response_header(const std::string& aHeaderLine);
111 bool decode();
112 bool decode_chunked();
113 void connection_established();
114 void connection_failure(const boost::system::error_code& aError);
115 void packet_sent(const http_packet& aPacket);
116 void packet_arrived(const http_packet& aPacket);
117 void transfer_failure(const boost::system::error_code& aError);
118 void connection_closed();
119
120 // attributes
121 private:
122 async_task& iIoTask;
123 std::optional<http_stream> iPacketStream;
124 std::string iHost;
125 uint16_t iPort;
126 bool iSecure;
127 type_e iType;
128 std::string iResource;
129 headers_t iRequestHeaders;
130 body_t iRequestBody;
131 std::string iResponseLine;
132 std::string iResponseStatus;
133 headers_t iResponseHeaders;
134 headers_t::iterator iLastResponseHeader;
135 bool iOk;
136 uint32_t iStatusCode;
137 std::optional<uint64_t> iBodyLength;
138 body_t iBody;
139 enum state { ResponseStatus, ResponseHeaders, Body, Finished } iState;
140 bool iPreviousWasCRLF;
141 std::optional<std::chrono::time_point<std::chrono::steady_clock>> iLastPacketReceived;
142 };
143}
std::basic_string< CharType > contents_type
http_packet(const contents_type &aContents=contents_type())
Definition http.hpp:56
const std::string & response_status() const
Definition http.hpp:100
http(async_task &aIoTask)
double percent_done() const
bool operator==(const http &) const
Definition http.hpp:91
std::map< ci_string, std::string > headers_t
Definition http.hpp:81
const headers_t & response_headers() const
Definition http.hpp:101
std::string body_as_string() const
Definition http.hpp:103
uint64_t body_length() const
Definition http.hpp:99
define_event(Started, started) define_event(Progress
uint32_t status_code() const
Definition http.hpp:98
const body_t & body() const
Definition http.hpp:102
http(const http &aOther)
void request(const std::string &aHost, const std::string &aResource, type_e aType=Get, unsigned short aPort=80, bool aSecure=false, const headers_t &aRequestHeaders=headers_t(), const std::variant< body_t, std::string > &aRequestBody=std::string())
bool ok() const
Definition http.hpp:97
http & operator=(const http &aOther)
void request(const std::string &aUrl, type_e aType=Get, const headers_t &aRequestHeaders=headers_t(), const std::variant< body_t, std::string > &aRequestBody=std::string())
std::vector< char > body_t
Definition http.hpp:82
virtual ~http()
std::basic_string< char, ci_char_traits< std::char_traits< char > > > ci_string
Definition string_ci.hpp:89
packet_stream< http_packet, tcp_protocol > http_stream
Definition http.hpp:68
Definition plf_hive.h:79
#define define_event(name, declName,...)
Definition event.hpp:200