Skip to content

Commit

Permalink
[WIP] HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Sep 22, 2016
1 parent 2f9a844 commit 88b485d
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bin/
bin64/
.DS_store
17 changes: 17 additions & 0 deletions include/beast/http/get.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BEAST_HTTP_GET_HPP
#define BEAST_HTTP_GET_HPP

namespace beast {
namespace http {

} // http
} // beast

#endif
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ unit-test http-tests :
http/body_type.cpp
http/concepts.cpp
http/empty_body.cpp
http/get.cpp
http/headers.cpp
http/message.cpp
http/message_v1.cpp
Expand Down
1 change: 1 addition & 0 deletions test/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_executable (http-tests
body_type.cpp
concepts.cpp
empty_body.cpp
get.cpp
headers.cpp
message.cpp
message_v1.cpp
Expand Down
137 changes: 137 additions & 0 deletions test/http/get.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

// Test that header file is self-contained.
#include <beast/http/get.hpp>

#include <beast/unit_test/suite.hpp>
#include <boost/utility/string_ref.hpp>

#include <beast/http.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>

namespace beast {
namespace http {

using resolver_type =
boost::asio::ip::tcp::resolver;

using query_type =
resolver_type::query;

using socket_type =
boost::asio::ip::tcp::socket;

using endpoint_type = socket_type::endpoint_type;

class url
{
public:
explicit
url(std::string const& s)
{
parse(s);
}

std::string protocol;
std::string host;
std::string path;
std::string query;

private:
void
parse(std::string const& s)
{
using namespace std;
const string prot_end("://");
string::const_iterator prot_i = search(
s.begin(), s.end(), prot_end.begin(), prot_end.end());
protocol.reserve(distance(s.begin(), prot_i));
transform(s.begin(), prot_i,
back_inserter(protocol),
ptr_fun<int,int>(tolower)); // protocol is icase
if(prot_i == s.end() )
return;
advance(prot_i, prot_end.length());
string::const_iterator path_i = find(prot_i, s.end(), '/');
host.reserve(distance(prot_i, path_i));
transform(prot_i, path_i,
back_inserter(host),
ptr_fun<int,int>(tolower)); // host is icase
string::const_iterator query_i = find(path_i, s.end(), '?');
path.assign(path_i, query_i);
if(query_i != s.end() )
++query_i;
query.assign(query_i, s.end());
}
};

class basic_client
{
public:
/** Fetch a resource using a HTTP/1 GET request.
*/
void
get(boost::string_ref url_str)
{
boost::asio::io_service ios;
resolver_type r{ios};

url u{url_str.to_string()};

auto it = r.resolve(
query_type{u.host, "http"});
socket_type sock{ios};
boost::asio::connect(sock, it);

// Send HTTP request using beast
beast::http::request_v1<
beast::http::empty_body> req;
req.method = "GET";
req.url = "/";
req.version = 11;
req.headers.insert("Host", u.host);
req.headers.insert("User-Agent", "Beast");
beast::http::prepare(req);
beast::http::write(sock, req);

// Receive and print HTTP response using beast
beast::streambuf sb;
beast::http::response_v1<
beast::http::streambuf_body> resp;
beast::http::read(sock, sb, resp);
std::cout << resp;
}
};

using client = basic_client;

class get_test : public beast::unit_test::suite
{
public:
void testGet()
{
client c;
c.get("http://boost.org");

pass();
}
void run() override
{
testGet();
}
};

BEAST_DEFINE_TESTSUITE(get,http,beast);

} // http
} // beast

0 comments on commit 88b485d

Please sign in to comment.