// ---------------< read the contents of the message body, if it is a normal message >----------
HttpMessage ClientHandler::readBody(HttpMessage msg, Socket& socket) {
	size_t numBytes = 0;
	size_t pos = msg.findAttribute("content-length");
	HttpMessage message = msg;
	if (pos < msg.attributes().size()) {
		numBytes = Converter<size_t>::toValue(msg.attributes()[pos].second);
		Socket::byte* buffer = new Socket::byte[numBytes + 1];
		socket.recv(numBytes, buffer);
		buffer[numBytes] = '\0';
		std::string msgBody(buffer);
		message.addBody(msgBody);
		delete[] buffer;
	}
		return message;
}
Exemple #2
0
HttpMessage	MsgClient::readMessage(Socket& socket) {
	Show::write("Client readMessage \n");
	//connectionClosed_ = false;
	HttpMessage msg;
	//read attribute
	while (true)
	{
		std::string attribString = socket.recvString('\n');
		if (attribString.size() > 1)
		{
			HttpMessage::Attribute attrib = HttpMessage::parseAttribute(attribString);
			msg.addAttribute(attrib);
		}
		else
		{
			break;
		}
	}

	if (msg.attributes().size() == 0)
	{
		return msg;
	}

	if (msg.attributes()[0].first == "POST")
	{
		
		std::string filePath = msg.findValue("filePath");
		if (filePath != "")
		{
			Show::write("Test for get FolderList");
			size_t numBytes = 0;
			size_t pos = msg.findAttribute("content-length");
			if (pos < msg.attributes().size())
			{
				numBytes = Converter<size_t>::toValue(msg.attributes()[pos].second);
				Socket::byte* buffer = new Socket::byte[numBytes + 1];
				socket.recv(numBytes, buffer);
				buffer[numBytes] = '\0';
				std::string msgBody(buffer);
				msg.addBody(msgBody);
				Show::write("getFolderList:" + msgBody + "\n");
			}

		}
	}
	return msg;
}
// ------------------------------------ Starts Receiver for the this component ---------------------------
void Server::startReceiverThread()
{
	BlockingQueue<HttpMessage> msgQ;
	try
	{
		SocketSystem ss;
		SocketListener sl(getServerPort(), Socket::IP6);
		Receiver cp(msgQ,"Server");
		sl.start(cp);
		/*
		* Since this is a server the loop below never terminates.
		* We could easily change that by sending a distinguished
		* message for shutdown.
		*/
		while (true)
		{			
			HttpMessage msg = msgQ.deQ();
			std::string cPort = msg.findValue("From-Port");
			if (msg.attributes()[0].first == "GET") {												// Checks if its an file extraction request
				std::string filename = msg.findValue("file");				
				Show::write("\n\n Client @: "+cPort+ " => requested file :: " + filename);
				sendServerFiles(msg);
			}
			else
			Show::write("\n\n Message from client @:"+cPort +"  =>  " + msg.bodyString());
		}
	}
	catch (std::exception& exc)
	{
		Show::write("\n  Exeception caught: ");
		std::string exMsg = "\n  " + std::string(exc.what()) + "\n\n";
		Show::write(exMsg);
	}
}
//----< this defines processing to frame messages >------------------
HttpMessage ClientHandler::readMessage(Socket& socket) {
	connectionClosed_ = false;
	HttpMessage msg;
	while (true) { // read message attributes
		std::string attribString = socket.recvString('\n');
		if (attribString.size() > 1) {
			HttpMessage::Attribute attrib = HttpMessage::parseAttribute(attribString);
			msg.addAttribute(attrib);
		}
		else break;
	}
	if (msg.attributes().size() == 0) { // If client is done, connection breaks
		connectionClosed_ = true;					// and recvString returns empty string
		return msg;
	}
	if (msg.attributes()[0].first == "POST") // read body if POST
	{
		if (msg.attributes()[0].second == "Message") msg = readBody(msg, socket); // case 0 - normal message
		else if (msg.attributes()[0].second == "File") { // case 1 - client sending file to server
			saveFileServer(msg, socket);
			msg = constructMessage(msg);
		}
		else if (msg.attributes()[0].second == "closePackage") msg = closePackage(msg); // case 7 - close a package
		else if (msg.attributes()[0].second == "returnFile") { // case 4 - server sends files to client
			saveFileClient(msg, socket);
			msg = constructMessage(msg);
		}
	}
	else if (msg.attributes()[0].first == "GET") { // read message if GET
		msg = readBody(msg, socket);
		if (msg.attributes()[0].second == "getFileList")  msg = getFileList(msg); // case 5 - request list of packages
		else if (msg.attributes()[0].second == "getFileNameList")  msg = getFileNameList(msg); // case 6 - request list of files in a packages
		else if (msg.attributes()[0].second == "File" || msg.attributes()[0].second == "FileWithDeps")
			msg = getFiles(msg, msg.attributes()[0].second);
	}
	else {
		msg.removeAttribute("content-length");
		std::string bodyString = "<msg>Error message</msg>";
		std::string sizeString = Converter<size_t>::toString(bodyString.size());
		msg.addAttribute(HttpMessage::Attribute("content-length", sizeString));
		msg.addBody(bodyString);
	}
	return msg;
}
//----< receiver functionality is defined by this function >---------
void ClientHandler::operator()(Socket socket) {
	std::function<void()> threadProc = [&]() {
		while (true) {
			HttpMessage msg = readMessage(socket); // read a message from socket
			if (msg.attributes().size() == 0 || msg.bodyString() == "quit") break;
			if (msg.findValue("type") == "returnList") {
				sendMessage(msg);
				continue;
			}
			else if (msg.findValue("type") == "returnFiles") {
				Sender sndr(msg.findValue("fromAddr"));
				sndr.start();
				std::vector<std::string> files = splitString(msg.bodyString());
				for (std::string file : files) {
					std::string fileSpec = searchFile(file);
					FileSystem::FileInfo fi(fileSpec);
					FileSystem::File fileIn(fileSpec);
					fileIn.open(FileSystem::File::in, FileSystem::File::binary);
					if (!fileIn.isGood()) {
						std::cout << "\n  could not open file " << file;
						continue;
					}
					size_t fileSize = fi.size();
					std::string sizeString = Converter<size_t>::toString(fileSize);
					HttpMessage message = sndr.makeMessage(4, "", msg.findValue("toAddr"));
					message.addAttribute(HttpMessage::Attribute("file", fileSpec));
					message.addAttribute(HttpMessage::Attribute("content-length", sizeString));
					sndr.postMessage(message);
				}
				sndr.postMessage(sndr.makeMessage(0, "closeServer", msg.findValue("toAddr")));
				sndr.postMessage(sndr.makeMessage(0, "quit", msg.findValue("toAddr")));
				sndr.wait();
				continue;
			}
			else if (msg.attributes()[0].second == "error") {
				sendMessage(msg);
				continue;
			}
			msgQ_.enQ(msg);
		}
	};
	std::thread receiveThread(threadProc);
	receiveThread.join();
}