Skip to content

Commit

Permalink
Add support for HTTP/HTTPS client
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Aug 11, 2019
1 parent 16a77cf commit 8bfa5f9
Show file tree
Hide file tree
Showing 12 changed files with 1,723 additions and 0 deletions.
9 changes: 9 additions & 0 deletions build/pomdog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ set(POMDOG_SOURCES_CORE
${POMDOG_DIR}/include/Pomdog/Math/detail/ForwardDeclarations.hpp
${POMDOG_DIR}/include/Pomdog/Math/detail/TaggedArithmetic.hpp
${POMDOG_DIR}/include/Pomdog/Network/ArrayView.hpp
${POMDOG_DIR}/include/Pomdog/Network/HTTPClient.hpp
${POMDOG_DIR}/include/Pomdog/Network/HTTPMethod.hpp
${POMDOG_DIR}/include/Pomdog/Network/HTTPRequest.hpp
${POMDOG_DIR}/include/Pomdog/Network/HTTPResponse.hpp
${POMDOG_DIR}/include/Pomdog/Network/IOService.hpp
${POMDOG_DIR}/include/Pomdog/Network/TCPStream.hpp
${POMDOG_DIR}/include/Pomdog/Network/TLSStream.hpp
Expand Down Expand Up @@ -334,6 +338,11 @@ set(POMDOG_SOURCES_CORE
${POMDOG_DIR}/src/Network/EndPoint.hpp
${POMDOG_DIR}/src/Network/ErrorHelper.cpp
${POMDOG_DIR}/src/Network/ErrorHelper.hpp
${POMDOG_DIR}/src/Network/HTTPClient.cpp
${POMDOG_DIR}/src/Network/HTTPParser.cpp
${POMDOG_DIR}/src/Network/HTTPParser.hpp
${POMDOG_DIR}/src/Network/HTTPRequest.cpp
${POMDOG_DIR}/src/Network/HTTPResponse.cpp
${POMDOG_DIR}/src/Network/IOService.cpp
${POMDOG_DIR}/src/Network/SocketProtocol.hpp
${POMDOG_DIR}/src/Network/TCPStream.cpp
Expand Down
50 changes: 50 additions & 0 deletions include/Pomdog/Network/HTTPClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2013-2019 mogemimi. Distributed under the MIT license.

#pragma once

#include "Pomdog/Basic/Export.hpp"
#include "Pomdog/Network/detail/ForwardDeclarations.hpp"
#include "Pomdog/Signals/detail/ForwardDeclarations.hpp"
#include "Pomdog/Utility/Errors.hpp"
#include <functional>
#include <memory>
#include <string>
#include <tuple>

namespace Pomdog {

class POMDOG_EXPORT HTTPClient final {
public:
explicit HTTPClient(IOService* service);

HTTPClient(const HTTPClient&) = delete;
HTTPClient& operator=(const HTTPClient&) = delete;

~HTTPClient();

/// Sends an HTTP request and returns an HTTP response.
[[nodiscard]] std::shared_ptr<Error>
Do(const std::shared_ptr<HTTPRequest>& req);

/// Sends a GET request to the specified URL.
[[nodiscard]] std::tuple<Connection, std::shared_ptr<Error>>
Get(const std::string& url,
std::function<void(std::unique_ptr<HTTPResponse>&&, const std::shared_ptr<Error>&)>&& callback);

/// Sends a POST request to the specified URL.
[[nodiscard]] std::tuple<Connection, std::shared_ptr<Error>>
Post(const std::string& url,
const std::string& contentType,
std::vector<char>&& body,
std::function<void(std::unique_ptr<HTTPResponse>&&, const std::shared_ptr<Error>&)>&& callback);

/// Aborts the specified request immediately.
[[nodiscard]] std::shared_ptr<Error>
CancelRequest(const std::shared_ptr<HTTPRequest>& req);

private:
struct Impl;
std::unique_ptr<Impl> impl;
};

} // namespace Pomdog
19 changes: 19 additions & 0 deletions include/Pomdog/Network/HTTPMethod.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2013-2019 mogemimi. Distributed under the MIT license.

#pragma once

#include <cstdint>

namespace Pomdog {

enum class HTTPMethod : std::int8_t {
Get,
Head,
Post,
Put,
Delete,
Connect,
Trace,
};

} // namespace Pomdog
43 changes: 43 additions & 0 deletions include/Pomdog/Network/HTTPRequest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2013-2019 mogemimi. Distributed under the MIT license.

#pragma once

#include "Pomdog/Basic/Export.hpp"
#include "Pomdog/Network/detail/ForwardDeclarations.hpp"
#include "Pomdog/Network/HTTPMethod.hpp"
#include "Pomdog/Signals/Delegate.hpp"
#include "Pomdog/Utility/Errors.hpp"
#include <functional>
#include <memory>
#include <string>
#include <vector>

namespace Pomdog {

class POMDOG_EXPORT HTTPRequest final {
public:
static std::shared_ptr<HTTPRequest>
Create(HTTPMethod method, const std::string& url);

void AddHeader(const std::string& key, const std::string& value);

public:
/// The URL for the request (e.g. "https://example.com")
std::string URL;

/// The request headers
std::vector<std::pair<std::string, std::string>> Headers;

/// The request body
std::vector<char> Body;

/// Callback function that will be called on request completion or error caused
Delegate<void(std::unique_ptr<HTTPResponse>&&, const std::shared_ptr<Error>&)> OnCompleted;

/// The method for the request
HTTPMethod Method;

bool PersistentConnection = true;
};

} // namespace Pomdog
38 changes: 38 additions & 0 deletions include/Pomdog/Network/HTTPResponse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2013-2019 mogemimi. Distributed under the MIT license.

#pragma once

#include "Pomdog/Basic/Export.hpp"
#include "Pomdog/Network/detail/ForwardDeclarations.hpp"
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <vector>

namespace Pomdog {

class POMDOG_EXPORT HTTPResponse final {
public:
/// e.g. "200 OK"
std::string Status;

/// e.g. 200
int StatusCode;

/// e.g. HTTP/1.1
std::string Protocol;

std::vector<std::pair<std::string, std::string>> Header;

std::vector<char> Body;

std::size_t ContentLength = 0;

/// True if "Transfer-Encoding" has a "chunked" directive, false otherwise.
bool ChunkedTransferEncoding = false;

std::shared_ptr<HTTPRequest> Request;
};

} // namespace Pomdog
Loading

0 comments on commit 8bfa5f9

Please sign in to comment.