Пример #1
0
uint64_t RequestHandler :: compute_hash( HttpMessage& request ){
	if( cache_hash_function != NULL ){
		return cache_hash_function( request );
		}
	else{
		std::string ckey = request.get_method() + ":" + request.get_path();
		return fnv1a_hash( ckey );
		}
	}
Пример #2
0
HttpMessage RequestHandler :: handleRequest( HttpMessage& request ){
	HttpMessage response;
	std::string body, path, ckey;
	handler_function func = NULL;
	size_t pattern_length = 0;
	uint64_t cache_key;
	std::unordered_map<uint64_t, CachedEntry>::iterator cache_it;
	
	path = request.get_path();
	response.set_version( "HTTP/1.1" );
	response.set_header_field( "Server", SERVER_VERSION );
	response.set_header_field( "Content-Type", "text/html" );
	response.set_header_field( "Date", get_gmt_time(0) );
	
	/*
	ckey = request.get_method() + ":" + path;
	cache_key = fnv1a_hash( ckey );
	*/
	cache_key = compute_hash( request );
	
	pthread_rwlock_rdlock( &RequestHandler::cache_lock );
	cache_it = cache.find( cache_key );
	if( cache_it != cache.end() ){
		if( (cache_it->second).is_valid() ){
			pthread_rwlock_unlock( &RequestHandler::cache_lock );
			//std::cerr << "Found @ cache" << std::endl;
			return (cache_it->second).get_content();
			}
		}
	pthread_rwlock_unlock( &RequestHandler::cache_lock );
					
	for( std::map< std::string, handler_function >::iterator it = handlers.begin() ; it != handlers.end() ; it++ ){
		if( string_startswith( it->first, path ) ){
			if( (it->first).size() > pattern_length ){
				pattern_length = (it->first).size();
				func = it->second;
				}
			}
		}
	
	if( func == NULL ){
		response.set_code( 404 );
		response.set_code_string( "Not found." );
		response.set_version( "HTTP/1.1" );
		response.set_header_field( "Connection", "close" );
		response.set_header_field( "Date", get_gmt_time(0) );
		response.set_header_field( "Server", SERVER_VERSION );
		response.set_header_field( "Last-Modified", get_gmt_time(0) );
		response.set_header_field( "Content-Type", "text/plain" );
		response.set_body( "Error 404 - Not found!\n" );
		}
	else{
		response_options_t result;
		try{
			result = func( request, response );
			} catch( std::string error ){
				response.set_code( 500 );
				response.set_code_string( "Internal server error." );
				
				}
		if( !result.ok ){
			response.set_code( 404 );
			response.set_code_string( "Not found." );
			response.set_version( "HTTP/1.1" );
			response.set_header_field( "Connection", "close" );
			response.set_header_field( "Date", get_gmt_time(0) );
			response.set_header_field( "Server", SERVER_VERSION );
			response.set_header_field( "Last-Modified", get_gmt_time(0) );
			response.set_header_field( "Content-Type", "text/plain" );
			response.set_body( "Error 404 - Not found!\n" );
			}
		else{
			if( request.has_header_field( "Accept-Encoding" ) ){
				if( request.get_header_field( "Accept-Encoding" ).find( "gzip" ) != std::string::npos ){
					response.set_body( zlib_gzip_deflate( response.get_body() ) );
					response.set_header_field( "Content-Encoding", "gzip" );
					} 
				else{
					if( request.get_header_field( "Accept-Encoding" ).find( "deflate" ) != std::string::npos ){
						response.set_body( zlib_deflate( response.get_body() ) );
						response.set_header_field( "Content-Encoding", "deflate" );
						}
					}
				}
			
			if( result.cached ){
				add_cache( cache_key, response, result.expires );
				}
			
			}
		}
	
	return response;
	}