File size: 1,878 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// -*- c++ -*-
// Adapted by Ulrich Germann from: 
// async_client.cpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff 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)
//
#pragma once
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <sstream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

namespace Moses
{
using boost::asio::ip::tcp;

std::string uri_encode(std::string const& in);

class http_client
{
  std::ostringstream m_content;
  std::vector<std::string> m_header;
  std::string m_http_version;
  unsigned int m_status_code;
  std::string m_status_message;
  std::ostringstream m_error;

public:
  http_client(boost::asio::io_service& io_service, std::string url, std::ostream* log);
  http_client(boost::asio::io_service& io_service,
	      std::string const& server, 
	      std::string const& port, 
	      std::string const& path);
private:

  void init(std::string const& server, 
	    std::string const& port, 
	    std::string const& path);

  void handle_resolve(const boost::system::error_code& err,
		      tcp::resolver::iterator endpoint_iterator);
  void handle_connect(const boost::system::error_code& err,
		      tcp::resolver::iterator endpoint_iterator);
  void handle_write_request(const boost::system::error_code& err);
  void handle_read_status_line(const boost::system::error_code& err);
  void handle_read_headers(const boost::system::error_code& err);
  void handle_read_content(const boost::system::error_code& err);
  tcp::resolver resolver_;
  tcp::socket socket_;
  boost::asio::streambuf request_;
  boost::asio::streambuf response_;
public:
  std::string content() const;
  std::string error_msg() const { return m_error.str(); }
};

}