/**
 * Parse filepath URL from incoming HTTP request
 */
std::string HttpServer::parse_request(tcp::socket &socket, boost::system::error_code &error_code)
{
    std::regex url_path_regex("(\\/([^\\s]+)|\\/)");
    std::smatch url_path_match;

    std::vector<char> request_buffer(1024);
    socket.receive(boost::asio::buffer(request_buffer), {}, error_code);
    std::string request_str(request_buffer.data());
    std::regex_search(request_str, url_path_match, url_path_regex);

    return url_path_match[0];
}
Example #2
0
bool decodeContent(UINT* content_size, Byte** cont_buf)
{
	Byte* bytes = *cont_buf;
	UINT64 signature = *((UINT64*)(bytes + 4));
	if (signature == kRequestSignature || signature == kResponseSignature)
	{
		return true;		//nothing need to do
	}
	else
	{
		//may be xml struct
		string line((const char*)bytes, 100);
		if (line.find("RequestVersion") != string::npos)
		{
			//xml cell request
			string request_str((const char*)bytes, *content_size);
			size_t subrequestdata_index = request_str.find("<SubRequestData");
			size_t data_start_flag = request_str.find(">", subrequestdata_index);
			data_start_flag += 1;
			size_t data_end_flag = request_str.find("</SubRequestData>", data_start_flag);
			size_t b64_len = data_end_flag - data_start_flag;
			if (subrequestdata_index == string::npos || data_end_flag == string::npos || b64_len < 8)
				goto FAILED_END;
			request_str = request_str.substr(data_start_flag, b64_len);
			*content_size = base64_decode(request_str.c_str(), *cont_buf);
		}
		else if (line.find("ResponseVersion") != string::npos)
		{
			//xml cell response
			string response_str((const char*)bytes, *content_size);
			size_t subresponsedata_index = response_str.find("<SubResponseData");
			size_t data_start_flag = response_str.find(">", subresponsedata_index);
			data_start_flag += 1;
			size_t data_end_flag = response_str.find("</SubResponseData>", data_start_flag);
			size_t b64_len = data_end_flag - data_start_flag;
			if (subresponsedata_index == string::npos || data_end_flag == string::npos || b64_len < 8)
				goto FAILED_END;
			response_str = response_str.substr(data_start_flag, b64_len);

			*content_size = base64_decode(response_str.c_str(), *cont_buf);
		}
		else
		{
			goto FAILED_END;
		}
	}

	return true;

FAILED_END:
	return false;
}
Example #3
0
void
statistics_collector::process_remote_connection() {
	// create zmq resp socket
	zmq::socket_t socket(*zmq_context_, ZMQ_REP);
	LT::port port = config()->remote_statistics_port();
	std::string port_str = boost::lexical_cast<std::string>(port);
	socket.bind(("tcp://*:" + port_str).c_str());

	while (is_running_) {
		// poll for request
		zmq_pollitem_t poll_items[1];
		poll_items[0].socket = socket;
		poll_items[0].fd = 0;
		poll_items[0].events = ZMQ_POLLIN;
		poll_items[0].revents = 0;

		int socket_response = zmq_poll(poll_items, 1, 0);
		if (socket_response <= 0) {
			continue;
		}

		// in case we received control message
	    if ((ZMQ_POLLIN & poll_items[0].revents) != ZMQ_POLLIN) {
	    	continue;
	    }

    	zmq::message_t request;

    	// see if we're still running
    	if (!is_running_) {
    		continue;
    	}

    	std::string response_json;

    	try {
    		if(!socket.recv(&request)) {
    			logger()->log("recv failed at " + std::string(BOOST_CURRENT_FUNCTION));
    			continue;
    		}
    		else {
				std::string request_str((char*)request.data(), request.size());
				response_json = process_request_json(request_str);
    		}
    	}
    	catch (const std::exception& ex) {
			std::string error_msg = "some very ugly shit happend while recv on socket at ";
			error_msg += std::string(BOOST_CURRENT_FUNCTION);
			error_msg += " details: " + std::string(ex.what());
			throw error(error_msg);
    	}

    	// see if we're still running
    	if (!is_running_ || response_json.empty()) {
    		continue;
    	}

    	// send response
		try {
			// send response
			size_t data_len = response_json.length();

			zmq::message_t reply(data_len);
			memcpy((void*)reply.data(), response_json.c_str(), data_len);

			if(!socket.send(reply)) {
				logger()->log("sending failed at " + std::string(BOOST_CURRENT_FUNCTION));
			}
		}
		catch (const std::exception& ex) {
			std::string error_msg = "some very ugly shit happend while send on socket at ";
			error_msg += std::string(BOOST_CURRENT_FUNCTION);
			error_msg += " details: " + std::string(ex.what());
			throw error(error_msg);
		}
	}
}