Example #1
0
 void connection::send(const reply& r)
 {
   // send!
   boost::asio::async_write(socket_, r.to_buffers(),
     strand_.wrap(
       boost::bind(&connection::handle_write, shared_from_this(),
         boost::asio::placeholders::error)));
 }
Example #2
0
void handle_jquery(server_ptr serv, connection_ptr conn, const request& req, const std::string& path,
                   const std::string&query, reply&rep)
{
#include "jquery/jquery.i"
    static const std::string jqst(JQUERY_STR, sizeof(JQUERY_STR));
    //this redirect the browser so it thinks the stream url is unique
    rep.status = reply::ok;
    rep.headers.clear();
    rep.content = jqst;
    rep.headers.push_back(header("Content-Length", boost::lexical_cast<std::string>(sizeof(JQUERY_STR))));
    rep.headers.push_back(header("Content-Type", mime_types::extension_to_type("js")));
    conn->async_write(rep.to_buffers());
}
	inline void connection::handle_read(const boost::system::error_code& e,
					    std::size_t bytes_transferred) {
	    if (!e) {
		boost::tribool result;
		boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
		  *request_, buffer_.data(), buffer_.data() + bytes_transferred);

		if (result) {
		    try {
			request_handler_.handle_request(*request_, reply_);
		    } catch (webserver::reply rep) {
			reply_ = rep;
		    }
		    std::cerr << reply_.content << std::endl;
		    boost::asio::async_write(socket_, reply_.to_buffers(),
		     strand_.wrap(
			  boost::bind(&connection::handle_write, shared_from_this(),
				      boost::asio::placeholders::error)));
		} else if (!result) {
		    reply_ = reply::stock_reply(reply::bad_request);
		    boost::asio::async_write(socket_, reply_.to_buffers(),
		     strand_.wrap(
			  boost::bind(&connection::handle_write, shared_from_this(),
				      boost::asio::placeholders::error)));
		} else {
		    socket_.async_read_some(boost::asio::buffer(buffer_),
			    strand_.wrap(
				 boost::bind(&connection::handle_read, shared_from_this(),
					     boost::asio::placeholders::error,
					     boost::asio::placeholders::bytes_transferred)));
		}
	    }

	    // If an error occurs then no new asynchronous operations are started. This
	    // means that all shared_ptr references to the connection object will
	    // disappear and the object will be destroyed automatically after this
	    // handler returns. The connection class's destructor closes the socket.
	}
Example #4
0
		void to(reply&r)override{
			unique_ptr<chunky>y(/*takes*/r.reply_chunky());
			y->p("HTTP/1.1 200\r\nTransfer-Encoding:chunked\r\nContent-Type:text/plain;charset=utf-8\r\n\r\n");
			y->send_response_header();//? send_session_id


			xprinter&x=*y;
			for(auto i=0;i<4*1024;i++){
				x.p("chunked response ");
			}


			y->finish();
		}
void request_handler::produre_reply(const request& req, reply& rep)
{
	string request_path;
	//
	request_path = req.url;
	//Request path must be absolute and not contain ".."
	if(request_path.empty() || request_path[0]!='/' 
		|| request_path.find("..")!=string::npos)
	{
		rep.set_status(reply::bad_request);
		return;
	}

	if(request_path[request_path.size() -1] == '/')
	{
		request_path += "index.html";
	}

	std::size_t last_slash_pos = request_path.find_last_of("/");
	std::size_t last_dot_pos = request_path.find_last_of(".");
	string extension;
	if( last_dot_pos!=string::npos && last_dot_pos> last_slash_pos)
	{
		extension = request_path.substr(last_dot_pos+1);
	}

	std::string full_path = this->m_docroot + request_path;
	ifstream in(full_path.c_str(), std::ios::in | std::ios::binary);
	if( !in)
	{
		rep.set_status(reply::not_found);
		return;
	}

	rep.set_status(reply::ok);
	string content;
	char buf[512];
	while(in.read(buf, sizeof(buf)).gcount() > 0)
			content.append(buf, in.gcount());
	rep.set_content(content);
	
	vector<header> headers;
	headers.resize(2);
	headers[0].name = "Content-Length";
	char len[8] = {0};
	sprintf(len, "%d", static_cast<int>(rep.get_content().size()) );
	headers[0].value = len;
	headers[1].name = "Content-Type";
	headers[1].value = maper.extension_to_type(extension); //Check the extension! 	
	rep.set_headers(headers);
}
Example #6
0
	void reset()
	{
		request_.clear();
		reply_.clear();
		request_parser_.reset();
	}