2019-03-01 06:54:03 +01:00
|
|
|
/*
|
|
|
|
* IXHttpClient.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-08-13 19:59:18 +02:00
|
|
|
#include "IXHttp.h"
|
2019-05-30 17:46:50 +02:00
|
|
|
#include "IXSocket.h"
|
|
|
|
#include "IXWebSocketHttpHeaders.h"
|
2019-03-01 06:54:03 +01:00
|
|
|
#include <algorithm>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <atomic>
|
2019-06-06 02:04:24 +02:00
|
|
|
#include <condition_variable>
|
2019-03-01 06:54:03 +01:00
|
|
|
#include <functional>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2019-03-01 06:54:03 +01:00
|
|
|
#include <mutex>
|
2019-06-06 02:04:24 +02:00
|
|
|
#include <queue>
|
|
|
|
#include <thread>
|
2019-03-01 06:54:03 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-05-30 17:46:50 +02:00
|
|
|
class HttpClient
|
|
|
|
{
|
2019-03-01 06:54:03 +01:00
|
|
|
public:
|
2019-06-06 02:04:24 +02:00
|
|
|
HttpClient(bool async = false);
|
2019-03-01 06:54:03 +01:00
|
|
|
~HttpClient();
|
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
HttpResponsePtr get(const std::string& url, HttpRequestArgsPtr args);
|
|
|
|
HttpResponsePtr head(const std::string& url, HttpRequestArgsPtr args);
|
|
|
|
HttpResponsePtr del(const std::string& url, HttpRequestArgsPtr args);
|
|
|
|
|
|
|
|
HttpResponsePtr post(const std::string& url,
|
|
|
|
const HttpParameters& httpParameters,
|
|
|
|
HttpRequestArgsPtr args);
|
|
|
|
HttpResponsePtr post(const std::string& url,
|
2019-03-01 06:54:03 +01:00
|
|
|
const std::string& body,
|
2019-06-06 02:04:24 +02:00
|
|
|
HttpRequestArgsPtr args);
|
2019-03-01 06:54:03 +01:00
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
HttpResponsePtr put(const std::string& url,
|
|
|
|
const HttpParameters& httpParameters,
|
|
|
|
HttpRequestArgsPtr args);
|
|
|
|
HttpResponsePtr put(const std::string& url,
|
|
|
|
const std::string& body,
|
|
|
|
HttpRequestArgsPtr args);
|
2019-03-01 06:54:03 +01:00
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
HttpResponsePtr request(const std::string& url,
|
|
|
|
const std::string& verb,
|
|
|
|
const std::string& body,
|
|
|
|
HttpRequestArgsPtr args,
|
|
|
|
int redirects = 0);
|
2019-03-01 06:54:03 +01:00
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
// Async API
|
|
|
|
HttpRequestArgsPtr createRequest(const std::string& url = std::string(),
|
|
|
|
const std::string& verb = HttpClient::kGet);
|
2019-03-01 06:54:03 +01:00
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
bool performRequest(HttpRequestArgsPtr request,
|
|
|
|
const OnResponseCallback& onResponseCallback);
|
2019-03-01 06:54:03 +01:00
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
std::string serializeHttpParameters(const HttpParameters& httpParameters);
|
|
|
|
|
|
|
|
std::string urlEncode(const std::string& value);
|
2019-03-01 06:54:03 +01:00
|
|
|
|
|
|
|
const static std::string kPost;
|
|
|
|
const static std::string kGet;
|
|
|
|
const static std::string kHead;
|
2019-06-03 20:38:56 +02:00
|
|
|
const static std::string kDel;
|
|
|
|
const static std::string kPut;
|
2019-06-06 02:04:24 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void log(const std::string& msg, HttpRequestArgsPtr args);
|
|
|
|
|
|
|
|
bool gzipInflate(const std::string& in, std::string& out);
|
|
|
|
|
|
|
|
// Async API background thread runner
|
|
|
|
void run();
|
|
|
|
|
|
|
|
// Async API
|
|
|
|
bool _async;
|
|
|
|
std::queue<std::pair<HttpRequestArgsPtr, OnResponseCallback>> _queue;
|
|
|
|
mutable std::mutex _queueMutex;
|
|
|
|
std::condition_variable _condition;
|
|
|
|
std::atomic<bool> _stop;
|
|
|
|
std::thread _thread;
|
|
|
|
|
|
|
|
std::shared_ptr<Socket> _socket;
|
2019-06-06 03:47:48 +02:00
|
|
|
std::mutex _mutex; // to protect accessing the _socket (only one socket per client)
|
2019-03-01 06:54:03 +01:00
|
|
|
};
|
2019-05-30 17:46:50 +02:00
|
|
|
} // namespace ix
|