Commit ce01689c authored by octal's avatar octal

Experimental http client

parent 3a154192
This diff is collapsed.
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#define unsafe
#define TRY(...) \ #define TRY(...) \
do { \ do { \
auto ret = __VA_ARGS__; \ auto ret = __VA_ARGS__; \
......
...@@ -97,6 +97,7 @@ public: ...@@ -97,6 +97,7 @@ public:
friend class Private::Parser<Http::Request>; friend class Private::Parser<Http::Request>;
friend class RequestBuilder; friend class RequestBuilder;
friend class Client;
Request(const Request& other) = default; Request(const Request& other) = default;
Request& operator=(const Request& other) = default; Request& operator=(const Request& other) = default;
......
...@@ -314,7 +314,8 @@ public: ...@@ -314,7 +314,8 @@ public:
Host() 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) : host_(host)
, port_(port) , port_(port)
{ } { }
...@@ -364,7 +365,11 @@ public: ...@@ -364,7 +365,11 @@ public:
void parse(const std::string& data); void parse(const std::string& data);
void write(std::ostream& os) const; 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: private:
std::string ua_; std::string ua_;
......
...@@ -61,6 +61,14 @@ public: ...@@ -61,6 +61,14 @@ public:
return add(std::make_shared<H>(std::forward<Args>(args)...)); 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<const Header> get(const std::string& name) const;
std::shared_ptr<Header> get(const std::string& name); std::shared_ptr<Header> get(const std::string& name);
Raw getRaw(const std::string& name) const; Raw getRaw(const std::string& name) const;
...@@ -80,6 +88,8 @@ public: ...@@ -80,6 +88,8 @@ public:
std::vector<std::shared_ptr<Header>> list() const; std::vector<std::shared_ptr<Header>> list() const;
bool remove(const std::string& name);
void clear(); void clear();
private: private:
......
...@@ -339,22 +339,32 @@ int main(int argc, char *argv[]) { ...@@ -339,22 +339,32 @@ int main(int argc, char *argv[]) {
#if 1 #if 1
int main() { int main() {
Net::Http::Client client("http://www.foaas.com"); Net::Http::Client client("http://supnetwork.org:9080");
auto opts = Net::Http::Client::options() auto opts = Net::Http::Client::options()
.threads(1) .threads(1)
.maxConnections(1); .maxConnections(20);
using namespace Net::Http; using namespace Net::Http;
constexpr size_t Requests = 1000;
std::atomic<int> responsesReceived(0);
client.init(opts); client.init(opts);
client.get(client for (int i = 0; i < Requests; ++i) {
.request("/off/octal/nask") client.get(client
.header<Header::ContentType>(MIME(Text, Plain)) .request("/ping")
.cookie(Cookie("FOO", "bar"))) .cookie(Cookie("FOO", "bar")))
.then([](const Http::Response& response) { .then([&](const Http::Response& response) {
std::cout << "code = " << response.code() << std::endl; responsesReceived.fetch_add(1);
std::cout << "body = " << response.body() << std::endl; //std::cout << "code = " << response.code() << std::endl;
}, Async::NoExcept); // std::cout << "body = " << response.body() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1)); }, 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 #endif
This diff is collapsed.
...@@ -297,6 +297,10 @@ Expect::write(std::ostream& os) const { ...@@ -297,6 +297,10 @@ Expect::write(std::ostream& os) const {
} }
} }
Host::Host(const std::string& data) {
parse(data);
}
void void
Host::parse(const std::string& data) { Host::parse(const std::string& data) {
auto pos = data.find(':'); auto pos = data.find(':');
......
...@@ -157,6 +157,20 @@ Collection::list() const { ...@@ -157,6 +157,20 @@ Collection::list() const {
return ret; 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 void
Collection::clear() { Collection::clear() {
headers.clear(); headers.clear();
......
...@@ -225,7 +225,7 @@ TEST(headers_test, user_agent) { ...@@ -225,7 +225,7 @@ TEST(headers_test, user_agent) {
Header::UserAgent ua; Header::UserAgent ua;
ua.parse("CERN-LineMode/2.15 libwww/2.17b3"); 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) { 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