Ejemplo n.º 1
0
void WebProcessor::Dispatch()
{
	if (*m_szUrl != '/')
	{
		SendErrorResponse(ERR_HTTP_BAD_REQUEST);
		return;
	}

	if (XmlRpcProcessor::IsRpcRequest(m_szUrl))
	{
		XmlRpcProcessor processor;
		processor.SetRequest(m_szRequest);
		processor.SetHttpMethod(m_eHttpMethod == hmGet ? XmlRpcProcessor::hmGet : XmlRpcProcessor::hmPost);
		processor.SetUserAccess((XmlRpcProcessor::EUserAccess)m_eUserAccess);
		processor.SetUrl(m_szUrl);
		processor.Execute();
		SendBodyResponse(processor.GetResponse(), strlen(processor.GetResponse()), processor.GetContentType()); 
		return;
	}

	if (Util::EmptyStr(g_pOptions->GetWebDir()))
	{
		SendErrorResponse(ERR_HTTP_SERVICE_UNAVAILABLE);
		return;
	}
	
	if (m_eHttpMethod != hmGet)
	{
		SendErrorResponse(ERR_HTTP_BAD_REQUEST);
		return;
	}
	
	// for security reasons we allow only characters "0..9 A..Z a..z . - _ /" in the URLs
	// we also don't allow ".." in the URLs
	for (char *p = m_szUrl; *p; p++)
	{
		if (!((*p >= '0' && *p <= '9') || (*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') ||
			*p == '.' || *p == '-' || *p == '_' || *p == '/') || (*p == '.' && p[1] == '.'))
		{
			SendErrorResponse(ERR_HTTP_NOT_FOUND);
			return;
		}
	}

	const char *szDefRes = "";
	if (m_szUrl[strlen(m_szUrl)-1] == '/')
	{
		// default file in directory (if not specified) is "index.html"
		szDefRes = "index.html";
	}

	char disk_filename[1024];
	snprintf(disk_filename, sizeof(disk_filename), "%s%s%s", g_pOptions->GetWebDir(), m_szUrl + 1, szDefRes);

	disk_filename[sizeof(disk_filename)-1] = '\0';

	SendFileResponse(disk_filename);
}
Ejemplo n.º 2
0
void *serving_thread(void *argp) 
{
	char 	buffer[BUFSIZE];
	char	*clientip,*clientport;
	char 	*type,*data=NULL;
	int 	rval;
	int err;
	char 	*filename,*md5sum="xxx",*filesize="12324";
	int 	socket = *(int*)argp;
	fileptr tempnode;
	FILE 	*fp;
	char 	prints[45];
	
	printf("in thread:%d\n",socket);
	
	
	sprintf(prints,"../serverLog.%u",(unsigned int)pthread_self());
	if ( (fp = fopen(prints,"wb")) == NULL )
	{
		perror("fopen");
		return NULL;
	}
	if	(err=pthread_detach (pthread_self ()))	
	{
		// Detach thread  
		perror2("pthread_detach ",err);
		return NULL; 
	}
	////////////////////////////////////////////////////////////////


	if ((rval=myread(socket, buffer, BUFSIZE)) <= 0)
	{
		perror("read"); 
		return NULL;
	}
	if ( GetType(buffer,fp) == JOINREQUEST)
	{ 
		parse_JoinRequest(buffer,&clientip,&clientport);
		JoinResponse(buffer);
		fprintf(fp,"JoinResponse to client with ip: %s\n",clientip);
		if(mywrite(socket,buffer,BUFSIZE) <= 0 )
		{
			perror("write");
			return NULL;
		}
				
	}	
	while (runflag)
	{
	
		///Ask FileList///
		int rval;
		if ( (rval=myread(socket, buffer , BUFSIZE)) < 0)
		{	
			perror("read"); 
			break;
		}
		//an to socket exei kleisei
		else if ( rval == 0 )
		{
			close(socket);	
			break;
		}
		else if (GetType(buffer,fp) == ASKFILELIST) 
		{	
			//if (err = pthread_mutex_lock(&serverlist_mux) ){ 
			//	perror2(" pthread_mutex_lock ",err ); exit(1); }
			fprintf(fp,"upadate server's list\n");	
			initialize("./",&metadata);
			printf("initialization:\n");
			printfile(metadata);
			//chekarw ti lista me ta arxeia pou diatiro kai gia kathe komvo tha ftiaxno 2 orismata pou tou 
			//antistixoun filename kai md5 na mi ksexasw teleuteoorisma NULL
			FileList(buffer,metadata);
			fprintf(fp,"Send FileList to client with ip: %s\n",clientip);
			//if (err = pthread_mutex_unlock(&serverlist_mux) ){ 
			//	perror2(" pthread_mutex_unlock ",err ); exit(1); }
			if(mywrite(socket,buffer,BUFSIZE) <= 0 )
			{
				perror("write");
				break;
			}		
		}
		///AskFile///
		else if (GetType(buffer,fp) == ASKFILE)
		{ 
			parse_AskFile(buffer,&filename);
			fprintf(fp,"client with ip: %s asked for file:%s\n",clientip,filename);
			//lock list gia anazitisi tou arxeiou
			if (err = pthread_mutex_lock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_lock ",err ); exit(1); }
			tempnode=filenode(metadata,filename,NULL);
			if (err = pthread_mutex_unlock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_unlock ",err ); exit(1); }
			AskFileResponse(buffer,filename,myip,myport);
			fprintf(fp,"server is going to send the file to client \n");
			if(mywrite(socket,buffer,BUFSIZE) <= 0 )
			{	
				perror("write");
				break;
			}	
		}
		///GetFile///	
		else if (GetType(buffer,fp) == GETFILE)
		{
			parse_GetFile(buffer,&filename,&md5sum);
			//stelnei to arxeio pou tou zita kai sti lista tou oti autos o client exei pleon to arxeio
			//lock list gia na ton prosthesei sti lista
			GetFileResponse(socket,filename,md5sum);	
			if (err = pthread_mutex_lock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_lock ",err ); exit(1); }
			tempnode=filenode(metadata,filename,md5sum);
			add_peer(&tempnode,clientip,clientport);
			if (err = pthread_mutex_unlock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_unlock ",err ); exit(1); }
			fprintf(fp,"send  File %s to client with ip: %s\n",filename,clientip);		
		}
		///SendFile	
		else if (GetType(buffer,fp) == SENDFILE)
		{ 
			parse_SendFile(buffer,&filename,&md5sum,&filesize,socket);
			//lock list gia na prosthesei to arxeio
			if (err = pthread_mutex_lock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_lock ",err ); exit(1); }
			add_file(&metadata,filename,md5sum);
			tempnode=filenode(metadata,filename,md5sum);
			add_peer(&tempnode,clientip,clientport);
			if (err = pthread_mutex_unlock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_unlock ",err ); exit(1); }
			SendFileResponse(buffer,filename,md5sum);
			fprintf(fp,"new File %s from client with ip: %s\n",filename,clientip);
			if(mywrite(socket,buffer,BUFSIZE) <= 0 )
			{
				perror("write");
				break;
			}	
		
		}
		///DeleteFile	
		else if (GetType(buffer,fp) == DELETEFILE)
		{ 
			printf("DeleteFile\n");
			parse_DeleteFile(buffer,&filename,&md5sum);
			fprintf(fp,"File to delete: %s \n",filename);
			//lock list mux
			if (err = pthread_mutex_lock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_lock ",err ); exit(1); }
			tempnode=filenode(metadata,filename,NULL);
			if (err = pthread_mutex_unlock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_unlock ",err ); exit(1); }
			//lock file mux
			if (err = pthread_mutex_lock(&tempnode->file_mux) ){ 
				perror2(" pthread_mutex_lock ",err ); exit(1); }
			if( remove(filename) != 0 )
    				perror( "Error deleting file" );
  			else
    				printf("File  %s successfully deleted\n",filename);
			if (err = pthread_mutex_unlock(&tempnode->file_mux) ){ 
				perror2(" pthread_mutex_unlock ",err ); exit(1); }
			//lock list mux
			if (err = pthread_mutex_lock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_lock ",err ); exit(1); }
			delete_file(&metadata,filename,md5sum);
			if (err = pthread_mutex_unlock(&serverlist_mux) ){ 
				perror2(" pthread_mutex_unlock ",err ); exit(1); }
			DeleteFileResponse(buffer,filename,md5sum);
			fprintf(fp,"file deleted %s\n",filename);
			if(mywrite(socket,buffer,BUFSIZE) <= 0 )
			{
				perror("write");
				break;
			}	
		}	
	}
	close(socket);
	fprintf(fp,"Closing connection.\n");
	fclose(fp);
	free(argp);
}