94 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| 
 | |
| #pragma once
 | |
| 
 | |
| #if !defined(MIJIN_NET_HTTP_HPP_INCLUDED)
 | |
| #define MIJIN_NET_HTTP_HPP_INCLUDED 1
 | |
| 
 | |
| #include <memory>
 | |
| #include <string>
 | |
| #include <map>
 | |
| #include "./socket.hpp"
 | |
| #include "./url.hpp"
 | |
| #include "../container/boxed_object.hpp"
 | |
| #include "../container/typeless_buffer.hpp"
 | |
| #include "../internal/common.hpp"
 | |
| #include "../io/stream.hpp"
 | |
| 
 | |
| namespace mijin
 | |
| {
 | |
| namespace http_method
 | |
| {
 | |
| inline const std::string GET = "GET";
 | |
| inline const std::string POST = "POST";
 | |
| inline const std::string HEAD = "HEAD";
 | |
| inline const std::string PUT = "PUT";
 | |
| inline const std::string DELETE = "DELETE";
 | |
| }
 | |
| 
 | |
| struct HTTPVersion
 | |
| {
 | |
|     unsigned major;
 | |
|     unsigned minor;
 | |
| };
 | |
| 
 | |
| struct HTTPRequest
 | |
| {
 | |
|     HTTPVersion version = {1, 1};
 | |
|     std::string address;
 | |
|     std::string method = "GET";
 | |
|     std::multimap<std::string, std::string> headers;
 | |
|     std::string body;
 | |
| };
 | |
| 
 | |
| struct HTTPRequestOptions
 | |
| {
 | |
|     std::string method = "GET";
 | |
|     std::multimap<std::string, std::string> headers;
 | |
|     TypelessBuffer body;
 | |
| };
 | |
| 
 | |
| struct HTTPResponse
 | |
| {
 | |
|     HTTPVersion version;
 | |
|     unsigned status;
 | |
|     std::string statusMessage;
 | |
|     std::multimap<std::string, std::string> headers;
 | |
|     TypelessBuffer body;
 | |
| };
 | |
| 
 | |
| class HTTPStream
 | |
| {
 | |
| private:
 | |
|     Stream* base_;
 | |
| public:
 | |
|     HTTPStream(Stream& base) MIJIN_NOEXCEPT : base_(&base)
 | |
|     {
 | |
|         MIJIN_ASSERT(base_ != nullptr, "Invalid parameter for base.");
 | |
|     }
 | |
|     Task<StreamResult<HTTPResponse>> c_request(HTTPRequest request) MIJIN_NOEXCEPT;
 | |
| private:
 | |
|     Task<StreamError> c_writeRequest(const HTTPRequest& request) MIJIN_NOEXCEPT;
 | |
|     Task<StreamResult<HTTPResponse>> c_readResponse() MIJIN_NOEXCEPT;
 | |
| };
 | |
| 
 | |
| class HTTPClient
 | |
| {
 | |
| private:
 | |
|     std::unique_ptr<Socket> socket_;
 | |
|     std::unique_ptr<Stream> sslStream_;
 | |
|     mijin::BoxedObject<HTTPStream> stream_;
 | |
|     ip_address_t lastIP_;
 | |
|     std::uint16_t lastPort_ = 0;
 | |
|     bool lastWasHttps_ = false;
 | |
| public:
 | |
|     ~HTTPClient() MIJIN_NOEXCEPT { disconnect(); }
 | |
|     Task<StreamResult<HTTPResponse>> c_request(ip_address_t address, std::uint16_t port, bool https, HTTPRequest request) MIJIN_NOEXCEPT;
 | |
|     Task<StreamResult<HTTPResponse>> c_request(const URL& url, HTTPRequest request = {}) MIJIN_NOEXCEPT;
 | |
|     void disconnect() MIJIN_NOEXCEPT;
 | |
| private:
 | |
|     StreamError createSocket(ip_address_t address, const std::string& hostname, std::uint16_t port, bool https) MIJIN_NOEXCEPT;
 | |
| };
 | |
| }
 | |
| 
 | |
| #endif // !defined(MIJIN_NET_HTTP_HPP_INCLUDED)
 |