Exemple #1
0
void
Proxy::
handleProxyTransaction( Socket & browserSocket )
{
    // Recieve HTTP header from browser
    std::string browserHeader = "";
    if ( ! browserSocket.recieveHttpHeader( browserHeader ) )
    {
        std::cout << "\n\n**** Error recieving HTTP header from browser ****\n\n";
        return;
    }
    std::cout << browserHeader << std::endl;
    
    if (searchStringForIllegalContents( pickOutJumbledURL( browserHeader ), mIllegalContents ))
    {
        std::cout << "\n\n**** We found some illegal content in the URL! ****\n\n";
        sendBadUrlRedirectToBrowser( browserSocket );
        
    }
    
    std::string hostName = extractHost( browserHeader );
    if (hostName == "")
    {
        std::cout << "\n\n**** Empty browser bessage recieved ****\n\n";
        return;
    }
    
    // Create clientsocket
    Socket clientSocket;
    clientSocket.createAndConnect( hostName.c_str() );
    
    std::string toServerHeader = correctConnectionInfo( browserHeader );
    std::cout << "Message to send by client :" << std::endl << toServerHeader;
    
    clientSocket.sendString( toServerHeader );
    if ( headerComesWithData( toServerHeader ) )
    {
        pipeDataTrough( browserSocket, clientSocket );
    }
    
    
    std::string responseHeader = "";
    if ( ! clientSocket.recieveHttpHeader( responseHeader ) )
    {
        std::cout << "\n\n**** Error recieving HTTP header from web server ****\n\n";
        return;
    }
    std::cout << responseHeader << std::endl << std::endl;
    
    if ( filteringPossible( responseHeader ) )
    {
        filterContent(responseHeader, clientSocket, browserSocket );
    }
    else
    {
        browserSocket.sendString( responseHeader );
        pipeDataTrough( clientSocket, browserSocket );
    }
    
    
    browserSocket.close();
}
// . returns -1 on error, 0 on success
// . reads HTTP reply from filename given as argument, filters it, 
//   and then writes it to stdout
// . originally, we read from stdin, but popen was causing problems when called
//   from a thread on linux 2.4.17 with the old linux threads
int main ( int argc , char *argv[] ) {

	// should have one and only 1 arg (excluding filename)
	if ( argc != 2 ) {
		fprintf(stderr,"gbfilter: usage: gbfilter <inputfilename>\n");
		return -1;
	}

	// . read HTTP reply in from file, gigablast will give it to us there
	// . this should be the HTTP mime followed by the content
	char *buf = (char *)malloc ( MAX_READ_SIZE );
	if ( ! buf ) {
		fprintf(stderr,"gbfilter:malloc:%s: %s: %s\n",
			argv[1],strerror(errno)); 
		return -1;
	}

	// first and only arg is the input file to read from
	int fd = open ( argv[1] , O_RDONLY );
	if ( fd < 0 ) {
		fprintf(stderr,"gbfilter:open: %s: %s\n",
			argv[1],strerror(errno)); 
		free ( buf );
		return -1;
	}

	int n = read ( fd , buf , MAX_READ_SIZE );

	close ( fd );

	// return -1 on read error
	if ( n < 0 ) {
		fprintf(stderr,"gbfilter:fread: %s\n",strerror(errno)); 
		free ( buf );
		return -1;
	}

	// warn if the doc was bigger than expected
	if ( n >= MAX_READ_SIZE ) 
		fprintf(stderr,"gbfilter: WARNING: MAX_READ_SIZE "
			"needs boost\n");

	//sleep(45);

	//srand(time(NULL));
	//int32_t i = rand() % 30;
	//fprintf(stderr,"sleep(%"INT32")\n",i);
	//sleep(i);

	// if nothing came in then nothing goes out, we're done
	if ( n == 0 ) { free ( buf ) ; return 0; }

	// get the end of the mime of this HTTP reply
	int32_t mimeLen = getMimeLen ( buf , n );

	// if it is -1, no mime boundary was found, so return an error
	if ( mimeLen < 0 ) {
		fprintf(stderr,"gbfilter: no mime boundary\n");
		free ( buf );
		return -1;
	}

	// . get the id from the input filename
	// . use that for out tmp files as well so parent caller can remove
	//   our cruft if we core
	int32_t id ;
	char *p = argv[1];
	// get id in the file
	while ( *p && ! isdigit(*p) ) p++;
	id = atol ( p );

	// ... begin filter logic here ...

	// get the content type (the various types are #define'd above)
	char ctype = getContentType ( buf , mimeLen );
	bool filter = false;
	if ( ctype == CT_PDF ) filter = true ;
	if ( ctype == CT_DOC ) filter = true ;
	if ( ctype == CT_XLS ) filter = true ;
	if ( ctype == CT_PPT ) filter = true ;
	if ( ctype == CT_PS  ) filter = true ;
	if ( filter ) {
		int status = filterContent ( buf, n, mimeLen, ctype, id );
		free ( buf );
		return status;
	}

	// ... end filter logic here ...

	// if not filtered, write the input to stdout unaltered
	// no! make it 0 bytes!
	//int32_t w = fwrite ( buf , 1 , n , stdout );
	//if ( w == n ) { free ( buf ) ; return 0; }
	free ( buf );
	return 0;
	// note any errors
	fprintf(stderr,"gbfilter: fwrite: %s\n",strerror(errno)); 
	free ( buf );
	return -1;
}