Пример #1
0
//Service Client Function Definition
void msl::webserver::service_client(msl::socket& client,const std::string& message)
{
	//Get Requests
	if(msl::starts_with(message,"GET"))
	{
		//Create Parser
		std::istringstream istr(msl::http_to_ascii(message));

		//Parse the Request
		std::string request;
		istr>>request;
		istr>>request;

		//If User Options Fail
		if(_user_service_client==NULL||!_user_service_client(client,msl::http_to_ascii(message)))
		{
			//Web Root Variable (Where your web files are)
			std::string web_root="web";

			//Check for Index
			if(request=="/")
				request="/index.html";

			//Mime Type Variable (Default plain text)
			std::string mime_type="text/plain";

			//Check for Code Mime Type
			if(msl::ends_with(request,".js"))
				mime_type="application/x-javascript";

			//Check for Images Mime Type
			else if(msl::ends_with(request,".gif"))
				mime_type="image/gif";
			else if(msl::ends_with(request,".jpeg"))
				mime_type="image/jpeg";
			else if(msl::ends_with(request,".png"))
				mime_type="image/png";
			else if(msl::ends_with(request,".tiff"))
				mime_type="image/tiff";
			else if(msl::ends_with(request,".svg"))
				mime_type="image/svg+xml";
			else if(msl::ends_with(request,".ico"))
				mime_type="image/vnd.microsoft.icon";

			//Check for Text Mime Type
			else if(msl::ends_with(request,".css"))
				mime_type="text/css";
			else if(msl::ends_with(request,".htm")||msl::ends_with(request,".html"))
				mime_type="text/html";

			//File Data Variable
			std::string file;

			//Load File
			if(msl::file_to_string(web_root+request,file,true))
				client<<msl::http_pack_string(file,mime_type,false);

			//Bad File
			else if(msl::file_to_string(web_root+"/not_found.html",file,true))
				client<<msl::http_pack_string(file);
		}

		//Close Connection
		client.close();
	}
Пример #2
0
//Service Client Function Definition
void msl::webserver::service_client(msl::socket& client,const std::string& message)
{
	//Get Requests
	if(msl::starts_with(message,"GET"))
	{
		//Create Parser
		std::istringstream istr(message);

		//Parse the Request
		std::string request;
		istr>>request;
		istr>>request;

		//Translate Request
		request=msl::http_to_ascii(request);

		//If User Options Fail
		if(_user_service_client==NULL||!_user_service_client(client,message))
		{
			//Check for Index
			if(request=="/")
				request="/index.html";

			//Mime Type Variable (Default plain text)
			std::string mime_type="text/plain";

			//Check for Code Mime Type
			if(msl::ends_with(request,".js"))
				mime_type="application/x-javascript";

			//Check for Images Mime Type
			else if(msl::ends_with(request,".gif"))
				mime_type="image/gif";
			else if(msl::ends_with(request,".jpeg"))
				mime_type="image/jpeg";
			else if(msl::ends_with(request,".png"))
				mime_type="image/png";
			else if(msl::ends_with(request,".tiff"))
				mime_type="image/tiff";
			else if(msl::ends_with(request,".svg"))
				mime_type="image/svg+xml";
			else if(msl::ends_with(request,".ico"))
				mime_type="image/vnd.microsoft.icon";

			//Check for Text Mime Type
			else if(msl::ends_with(request,".css"))
				mime_type="text/css";
			else if(msl::ends_with(request,".htm")||msl::ends_with(request,".html"))
				mime_type="text/html";

			//File Data Variable
			std::string file;

			//Load File
			if(msl::file_to_string(_web_directory+request,file,true))
			{
				std::string response_str=msl::http_pack_string(file,mime_type,false);
				client.write(response_str.c_str(),response_str.size());
			}

			//Bad File
			else if(msl::file_to_string(_web_directory+"/not_found.html",file,true))
			{
				std::string response_str=msl::http_pack_string(file);
				client.write(response_str.c_str(),response_str.size());
			}

			//No Bad File
			else
			{
				std::string response_str=msl::http_pack_string("sorry...");
				client.write(response_str.c_str(),response_str.size());
			}
		}
	}