<commit_before>/*
 * session.cpp
 *
 *  Created on: Aug 21, 2015
 *      Author: zmij
 */

#include <tip/http/client/session.hpp>

#include <tip/http/common/request.hpp>
#include <tip/http/common/response.hpp>

#include <tip/ssl_context_service.hpp>

#include <tip/util/misc_algorithm.hpp>
#include <tip/log.hpp>

#include <boost/asio.hpp>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>

#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/euml/operator.hpp>

namespace tip {
namespace http {
namespace client {

LOCAL_LOGGING_FACILITY(HTTPSSN, OFF);

struct tcp_transport {
	typedef boost::asio::io_service io_service;
	typedef boost::asio::ip::tcp tcp;
	typedef boost::system::error_code error_code;
	typedef std::function<void(error_code const&)> connect_callback;
	typedef tcp::socket socket_type;

	tcp::resolver resolver_;
	socket_type socket_;

	tcp_transport(io_service& io_service, request::iri_type const& iri, connect_callback cb) :
			resolver_(io_service), socket_(io_service)
	{
		std::string host = static_cast<std::string const&>(iri.authority.host);
		std::string svc = iri.authority.port.empty() ?
				static_cast<std::string const&>(iri.scheme) :
				static_cast<std::string const&>(iri.authority.port);
		tcp::resolver::query qry(host, svc);
		resolver_.async_resolve(qry, std::bind(
			&tcp_transport::handle_resolve, this,
				std::placeholders::_1, std::placeholders::_2, cb
		));
	}

	void
	handle_resolve(error_code const& ec,
			tcp::resolver::iterator endpoint_iterator,
			connect_callback cb)
	{
		if (!ec) {
			boost::asio::async_connect(socket_, endpoint_iterator, std::bind(
				&tcp_transport::handle_connect, this,
					std::placeholders::_1, cb
			));
		} else if (cb) {
			cb(ec);
		}
	}

	void
	handle_connect(error_code const& ec, connect_callback cb)
	{
		if (cb) {
			cb(ec);
		}
	}
	void
	disconnect()
	{
		if (socket_.is_open()) {
			socket_.close();
		}
	}
};

struct ssl_transport {
	typedef boost::asio::io_service io_service;
	typedef boost::asio::ip::tcp tcp;
	typedef boost::system::error_code error_code;
	typedef std::function<void(error_code const&)> connect_callback;
	typedef boost::asio::ssl::stream< tcp::socket > socket_type;

	tcp::resolver resolver_;
	socket_type socket_;

	ssl_transport(io_service& io_service, request::iri_type const& iri, connect_callback cb) :
		resolver_(io_service),
		socket_( io_service,
				boost::asio::use_service<tip::ssl::ssl_context_service>(io_service).context() )
	{
		std::string host = static_cast<std::string const&>(iri.authority.host);
		std::string svc = iri.authority.port.empty() ?
				static_cast<std::string const&>(iri.scheme) :
				static_cast<std::string const&>(iri.authority.port);
		tcp::resolver::query qry(host, svc);
		resolver_.async_resolve(qry, std::bind(
			&ssl_transport::handle_resolve, this,
				std::placeholders::_1, std::placeholders::_2, cb
		));
	}

	void
	handle_resolve(error_code const& ec,
			tcp::resolver::iterator endpoint_iterator,
			connect_callback cb)
	{
		if (!ec) {
			socket_.set_verify_mode(boost::asio::ssl::verify_peer);
			socket_.set_verify_callback(std::bind(
				&ssl_transport::verify_certificate,
				this, std::placeholders::_1, std::placeholders::_2
			));
			boost::asio::async_connect(socket_.lowest_layer(),
				endpoint_iterator, std::bind(
				&ssl_transport::handle_connect, this,
					std::placeholders::_1, cb
			));
		} else if (cb) {
			cb(ec);
		}
	}

	bool
	verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx)
	{
	    // The verify callback can be used to check whether the certificate that is
	    // being presented is valid for the peer. For example, RFC 2818 describes
	    // the steps involved in doing this for HTTPS. Consult the OpenSSL
	    // documentation for more details. Note that the callback is called once
	    // for each certificate in the certificate chain, starting from the root
	    // certificate authority.

	    // In this example we will simply print the certificate's subject name.
	    char subject_name[256];
	    X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
	    X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
	    local_log() << "Verifying " << subject_name;
	    return preverified;
	}
	void
	handle_connect(error_code const& ec, connect_callback cb)
	{
		if (cb) {
			cb(ec);
		}
	}
	void
	disconnect()<commit_msg>AWM-575 Skip sertificate check because of fatal bug<commit_after>/*
 * session.cpp
 *
 *  Created on: Aug 21, 2015
 *      Author: zmij
 */

#include <tip/http/client/session.hpp>

#include <tip/http/common/request.hpp>
#include <tip/http/common/response.hpp>

#include <tip/ssl_context_service.hpp>

#include <tip/util/misc_algorithm.hpp>
#include <tip/log.hpp>

#include <boost/asio.hpp>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>

#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/euml/operator.hpp>

namespace tip {
namespace http {
namespace client {

LOCAL_LOGGING_FACILITY(HTTPSSN, TRACE);

struct tcp_transport {
	typedef boost::asio::io_service io_service;
	typedef boost::asio::ip::tcp tcp;
	typedef boost::system::error_code error_code;
	typedef std::function<void(error_code const&)> connect_callback;
	typedef tcp::socket socket_type;

	tcp::resolver resolver_;
	socket_type socket_;

	tcp_transport(io_service& io_service, request::iri_type const& iri, connect_callback cb) :
			resolver_(io_service), socket_(io_service)
	{
		std::string host = static_cast<std::string const&>(iri.authority.host);
		std::string svc = iri.authority.port.empty() ?
				static_cast<std::string const&>(iri.scheme) :
				static_cast<std::string const&>(iri.authority.port);
		tcp::resolver::query qry(host, svc);
		resolver_.async_resolve(qry, std::bind(
			&tcp_transport::handle_resolve, this,
				std::placeholders::_1, std::placeholders::_2, cb
		));
	}

	void
	handle_resolve(error_code const& ec,
			tcp::resolver::iterator endpoint_iterator,
			connect_callback cb)
	{
		if (!ec) {
			boost::asio::async_connect(socket_, endpoint_iterator, std::bind(
				&tcp_transport::handle_connect, this,
					std::placeholders::_1, cb
			));
		} else if (cb) {
			cb(ec);
		}
	}

	void
	handle_connect(error_code const& ec, connect_callback cb)
	{
		if (cb) {
			cb(ec);
		}
	}
	void
	disconnect()
	{
		if (socket_.is_open()) {
			socket_.close();
		}
	}
};

struct ssl_transport {
	typedef boost::asio::io_service io_service;
	typedef boost::asio::ip::tcp tcp;
	typedef boost::system::error_code error_code;
	typedef std::function<void(error_code const&)> connect_callback;
	typedef boost::asio::ssl::stream< tcp::socket > socket_type;

	tcp::resolver resolver_;
	socket_type socket_;

	ssl_transport(io_service& io_service, request::iri_type const& iri, connect_callback cb) :
		resolver_(io_service),
		socket_( io_service,
				boost::asio::use_service<tip::ssl::ssl_context_service>(io_service).context() )
	{
		std::string host = static_cast<std::string const&>(iri.authority.host);
		std::string svc = iri.authority.port.empty() ?
				static_cast<std::string const&>(iri.scheme) :
				static_cast<std::string const&>(iri.authority.port);
		tcp::resolver::query qry(host, svc);
		resolver_.async_resolve(qry, std::bind(
			&ssl_transport::handle_resolve, this,
				std::placeholders::_1, std::placeholders::_2, cb
		));
	}

	void
	handle_resolve(error_code const& ec,
			tcp::resolver::iterator endpoint_iterator,
			connect_callback cb)
	{
		if (!ec) {
			socket_.set_verify_mode(boost::asio::ssl::verify_peer);
			socket_.set_verify_callback(std::bind(
				&ssl_transport::verify_certificate,
				this, std::placeholders::_1, std::placeholders::_2
			));
			boost::asio::async_connect(socket_.lowest_layer(),
				endpoint_iterator, std::bind(
				&ssl_transport::handle_connect, this,
					std::placeholders::_1, cb
			));
		} else if (cb) {
			cb(ec);
		}
	}

	bool
	verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx)
	{
	    // The verify callback can be used to check whether the certificate that is
	    // being presented is valid for the peer. For example, RFC 2818 describes
	    // the steps involved in doing this for HTTPS. Consult the OpenSSL
	    // documentation for more details. Note that the callback is called once
	    // for each certificate in the certificate chain, starting from the root
	    // certificate authority.

	    // In this example we will simply print the certificate's subject name.
	    char subject_name[256];
	    X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
	    X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
	    local_log() << "Verifying " << subject_name;
	    return true;
	}
	void
	handle_connect(error_code const& ec, connect_callback cb)
	{
		if (!ec) {
			socket_.async_handshake(boost::asio::ssl::stream_base::client,
				std::bind(&ssl_transport::handle_handshake, this,
					std::placeholders::_1, cb));
		} else if (cb) {
			cb(ec);
		}
	}
	void
	handle_handshake(error_code const& ec, connect_callback cb)
	{
		if (cb) {
			cb(ec);
		}
	}
	void
	disconnect()
{'fraction_non_alphanumeric': 0.13465430915095344, 'fraction_numerical': 0.008096303398316821, 'mean_word_length': 4.85286783042394, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '103070', 'n_tokens_mistral': 3557, 'n_tokens_neox': 3194, 'n_words': 647}