Beispiel #1
0
 /**
  * protected constructor restricts creation of objects (use create())
  *
  * @param tcp_conn TCP connection containing a new message to parse
  * @param handler function called after the message has been parsed
  */
 request_reader(tcp::connection_ptr& tcp_conn, finished_handler_t handler)
     : http::reader(true, tcp_conn), m_http_msg(new http::request),
     m_finished(handler)
 {
     m_http_msg->set_remote_ip(tcp_conn->get_remote_ip());
     set_logger(PION_GET_LOGGER("pion.http.request_reader"));
 }
Beispiel #2
0
void forum_server::handle_request(http::request_ptr& http_request_ptr, tcp::connection_ptr& tcp_conn, const boost::system::error_code& ec)
{
	std::string path = http_request_ptr->get_resource();
	boost::asio::ip::address ip = tcp_conn->get_remote_ip();
	log("Received new request from '" + ip.to_string() + "' who is trying to access '" + path + "'");
	std::string route;

	// Is this conditional really needed..?
	if (path.size() > 1)
	{
		std::string::iterator it;
		for (it = path.begin() + 1; it != path.end(); ++it)
		{
			if (*it == '/') break;
		}

		if (it != path.end())
		{
			try
			{
				int index = std::distance(path.begin() + 1, it);
				routes[path.substr(1, index)](path.substr(index + 2), http_request_ptr, tcp_conn);
			}
			catch (std::exception& e)
			{
				ferror(e.what());
				http::response_writer_ptr writer(
					http::response_writer::create(tcp_conn, *http_request_ptr, boost::bind(&tcp::connection::finish, tcp_conn)));

				http::response res = writer->get_response();
				res.set_content_type(http::types::CONTENT_TYPE_HTML);
				res.set_status_code(500);
				res.set_status_message("Internal Server Error");

				// Push the custom error page..
				writer->write("Invalid route");
				writer->send();
			}
		}
		else routes["home"](path.substr(1), http_request_ptr, tcp_conn);
	}
	// Home is the default route..
	else routes["home"]("", http_request_ptr, tcp_conn);
}