Advertisement
Guest User

Untitled

a guest
Aug 12th, 2011
2,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <boost/asio.hpp>
  2. #include <boost/asio/ssl.hpp>
  3. #include <iostream>
  4. #include <istream>
  5. #include <ostream>
  6. #include <string>
  7.  
  8.  
  9. class client
  10. {
  11. public:
  12.   client(boost::asio::io_service& io_service, boost::asio::ssl::context& context, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
  13.     : socket_(io_service, context)
  14.   {
  15.     socket_.set_verify_mode(boost::asio::ssl::context::verify_none);
  16.     socket_.set_verify_callback(boost::bind(&client::verify_certificate, this, _1, _2));
  17.  
  18.     boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator, boost::bind(&client::handle_connect, this, boost::asio::placeholders::error));
  19.   }
  20.  
  21.   bool verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx)
  22.   {
  23.     char subject_name[256];
  24.     X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
  25.     X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
  26.     std::cout << "Verifying:\n" << subject_name << std::endl;
  27.  
  28.     return preverified;
  29.   }
  30.  
  31.   void handle_connect(const boost::system::error_code& error)
  32.   {
  33.     if(!error){
  34.       std::cout << "Connection OK!" << std::endl;
  35.       socket_.async_handshake(boost::asio::ssl::stream_base::client, boost::bind(&client::handle_handshake, this, boost::asio::placeholders::error));
  36.     }else{
  37.       std::cout << "Connect failed: " << error.message() << std::endl;
  38.     }
  39.   }
  40.  
  41.   void handle_handshake(const boost::system::error_code& error)
  42.   {
  43.     if(!error){
  44.       std::cout << "Sending request: " << std::endl;
  45.      
  46.       std::stringstream request_;
  47.  
  48.       request_ << "GET /api/0/data/ticker.php HTTP 1.1\r\n";
  49.       request_ << "Host: mtgox.com\r\n";
  50.       request_ << "Accept-Encoding: *\r\n";
  51.       request_ << "\r\n";
  52.  
  53.       std::cout << request_.str() << std::endl;
  54.  
  55.       boost::asio::async_write(socket_, boost::asio::buffer(request_.str()), boost::bind(&client::handle_write, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  56.     }else{
  57.       std::cout << "Handshake failed: " << error.message() << std::endl;
  58.     }
  59.   }
  60.  
  61.   void handle_write(const boost::system::error_code& error, size_t bytes_transferred)
  62.   {
  63.     if (!error){
  64.       std::cout << "Sending request OK!" << std::endl;
  65.       boost::asio::async_read(socket_, boost::asio::buffer(reply_, bytes_transferred), boost::bind(&client::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  66.     }else{
  67.       std::cout << "Write failed: " << error.message() << std::endl;
  68.     }
  69.   }
  70.  
  71.   void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
  72.   {
  73.     if (!error){
  74.       std::cout << "Reply: ";
  75.       std::cout.write(reply_, bytes_transferred);
  76.       std::cout << "\n";
  77.     }else{
  78.       std::cout << "Read failed: " << error.message() << std::endl;
  79.     }
  80.   }
  81.  
  82. private:
  83.   boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
  84.   char reply_[0x1 << 16];
  85. };
  86.  
  87. int main(int argc, char* argv[])
  88. {
  89.   try{
  90.     boost::asio::io_service io_service;
  91.  
  92.     boost::asio::ip::tcp::resolver resolver(io_service);
  93.     boost::asio::ip::tcp::resolver::query query("mtgox.com", "443");
  94.     boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
  95.  
  96.     boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
  97.     context.load_verify_file("key.pem");
  98.  
  99.     client c(io_service, context, iterator);
  100.  
  101.     io_service.run();
  102.   }catch (std::exception& e){
  103.     std::cerr << "Exception: " << e.what() << "\n";
  104.   }
  105.  
  106.   std::cin.get();
  107.   return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement