void HandleRead(boost::system::error_code const &error, std::size_t bytes) { if (error) return; std::vector<boost::asio::const_buffer> Buffers; //Raw request std::string rawHeader( Buffer.data() ); parseHttpHeader(rawHeader); formAnswer(); std::cout<<"Raw Request:\n"<<rawHeader<<std::endl; std::cout<<"Formed answer:\n"<<httpMessage.message; Buffers.push_back(boost::asio::const_buffer(httpMessage.message.c_str(), httpMessage.message.size() )); boost::asio::async_write(Socket, Buffers, Strand.wrap( boost::bind(&Connection::HandleWrite, shared_from_this(), boost::asio::placeholders::error) )); }
void HTTPCallbackTcpConnection::handleHttpRequest(const error_code& error, size_t length) { if (!error) { target::TargetRegistry::instance->refreshClient(localSocket.remote_endpoint().address().to_v4().to_ulong()); if (length != 0) { httpMessage.append(string(receiveBuffer.data(), length)); } if (httpMessage.find("\r\n\r\n") == string::npos) { async_read(localSocket, buffer(receiveBuffer), transfer_at_least(1), bind(&HTTPCallbackTcpConnection::handleHttpRequest, shared_from_this(), placeholders::error, placeholders::bytes_transferred)); } else { if (parsedHttpRequest == NULL) { parsedHttpRequest = parseHttpHeader(httpMessage); } int bytesMissing = parsedHttpRequest->contentLength - (httpMessage.length() - httpMessage.find("\r\n\r\n")) - 4; if (bytesMissing > 0) { async_read(localSocket, buffer(receiveBuffer), transfer_at_least(bytesMissing), bind(&HTTPCallbackTcpConnection::handleHttpRequest, shared_from_this(), placeholders::error, placeholders::bytes_transferred)); return; } else if (parsedHttpRequest->method == "POST") { parsedHttpRequest->content = httpMessage.substr(httpMessage.find("\r\n\r\n")); } if (parsedHttpRequest->url.find("/poll") != string::npos) { stringstream answer; auto pendingRequest = target::TargetRegistry::instance->poll(localSocket.remote_endpoint().address().to_v4().to_ulong()); if(pendingRequest != NULL) { answer << "request(" << pendingRequest->id << ", '" << pendingRequest->url << "'," << ((pendingRequest->postData.empty()) ? ("null") : ("'" + pendingRequest->postData + "'")) << ", " << ((pendingRequest->authorization.empty()) ? ("null") : ("'" + pendingRequest->authorization + "'")) << ");"; } async_write(localSocket, buffer( string( "HTTP/1.0 200 OK\r\nContent-Type: text/javascript; charset=UTF-8\r\nContent-Length: ") + to_string(answer.str().length()) + string("\r\n\r\n") + answer.str()), bind(&HTTPCallbackTcpConnection::handleWrite, shared_from_this(), placeholders::error)); } else if (parsedHttpRequest->url.compare("/post") == 0) { int fileSize = boost::filesystem::file_size( CROSS_SITE_CALLBACK_HTML); async_write(localSocket, buffer( string( "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ") + to_string(fileSize) + string("\r\n\r\n")), bind(&HTTPCallbackTcpConnection::handlePostRequest, shared_from_this(), placeholders::error)); } else if (parsedHttpRequest->url.compare("/put") == 0) { target::TargetRegistry::instance->onTargetResponse( parsedHttpRequest, localSocket.remote_endpoint().address().to_v4().to_ulong()); async_write(localSocket, buffer( string( "HTTP/1.0 200 OK\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Length: 0") + string("\r\n\r\n")), bind(&HTTPCallbackTcpConnection::handleWrite, shared_from_this(), placeholders::error)); } } } else { cout << "Receive error: " << error << endl; } }