Commit ce01689c authored by octal's avatar octal

Experimental http client

parent 3a154192
This diff is collapsed.
......@@ -15,6 +15,8 @@
#include <sys/socket.h>
#include <netdb.h>
#define unsafe
#define TRY(...) \
do { \
auto ret = __VA_ARGS__; \
......
......@@ -97,6 +97,7 @@ public:
friend class Private::Parser<Http::Request>;
friend class RequestBuilder;
friend class Client;
Request(const Request& other) = default;
Request& operator=(const Request& other) = default;
......
......@@ -314,7 +314,8 @@ public:
Host()
{ }
explicit Host(const std::string& host, Net::Port port = 80)
explicit Host(const std::string& host);
explicit Host(const std::string& host, Net::Port port)
: host_(host)
, port_(port)
{ }
......@@ -364,7 +365,11 @@ public:
void parse(const std::string& data);
void write(std::ostream& os) const;
std::string ua() const { return ua_; }
void setAgent(std::string ua) {
ua_ = std::move(ua);
}
std::string agent() const { return ua_; }
private:
std::string ua_;
......
......@@ -61,6 +61,14 @@ public:
return add(std::make_shared<H>(std::forward<Args>(args)...));
}
template<typename H>
typename std::enable_if<
IsHeader<H>::value, bool
>::type
remove() {
return remove(H::Name);
}
std::shared_ptr<const Header> get(const std::string& name) const;
std::shared_ptr<Header> get(const std::string& name);
Raw getRaw(const std::string& name) const;
......@@ -80,6 +88,8 @@ public:
std::vector<std::shared_ptr<Header>> list() const;
bool remove(const std::string& name);
void clear();
private:
......
......@@ -339,22 +339,32 @@ int main(int argc, char *argv[]) {
#if 1
int main() {
Net::Http::Client client("http://www.foaas.com");
Net::Http::Client client("http://supnetwork.org:9080");
auto opts = Net::Http::Client::options()
.threads(1)
.maxConnections(1);
.maxConnections(20);
using namespace Net::Http;
constexpr size_t Requests = 1000;
std::atomic<int> responsesReceived(0);
client.init(opts);
client.get(client
.request("/off/octal/nask")
.header<Header::ContentType>(MIME(Text, Plain))
.cookie(Cookie("FOO", "bar")))
.then([](const Http::Response& response) {
std::cout << "code = " << response.code() << std::endl;
std::cout << "body = " << response.body() << std::endl;
}, Async::NoExcept);
std::this_thread::sleep_for(std::chrono::seconds(1));
for (int i = 0; i < Requests; ++i) {
client.get(client
.request("/ping")
.cookie(Cookie("FOO", "bar")))
.then([&](const Http::Response& response) {
responsesReceived.fetch_add(1);
//std::cout << "code = " << response.code() << std::endl;
// std::cout << "body = " << response.body() << std::endl;
}, Async::NoExcept);
}
std::cout << "Sent " << Requests << " requests" << std::endl;
for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Received " << responsesReceived.load() << " responses" << std::endl;
}
client.shutdown();
}
#endif
This diff is collapsed.
......@@ -297,6 +297,10 @@ Expect::write(std::ostream& os) const {
}
}
Host::Host(const std::string& data) {
parse(data);
}
void
Host::parse(const std::string& data) {
auto pos = data.find(':');
......
......@@ -157,6 +157,20 @@ Collection::list() const {
return ret;
}
bool
Collection::remove(const std::string& name) {
auto tit = headers.find(name);
if (tit == std::end(headers)) {
auto rit = rawHeaders.find(name);
if (rit == std::end(rawHeaders)) return false;
rawHeaders.erase(rit);
return true;
}
headers.erase(tit);
return true;
}
void
Collection::clear() {
headers.clear();
......
......@@ -225,7 +225,7 @@ TEST(headers_test, user_agent) {
Header::UserAgent ua;
ua.parse("CERN-LineMode/2.15 libwww/2.17b3");
ASSERT_EQ(ua.ua(), "CERN-LineMode/2.15 libwww/2.17b3");
ASSERT_EQ(ua.agent(), "CERN-LineMode/2.15 libwww/2.17b3");
}
TEST(headers_test, content_encoding) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment