当前位置: 首页>>代码示例>>C++>>正文


C++ HttpMessage::set_code方法代码示例

本文整理汇总了C++中HttpMessage::set_code方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpMessage::set_code方法的具体用法?C++ HttpMessage::set_code怎么用?C++ HttpMessage::set_code使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpMessage的用法示例。


在下文中一共展示了HttpMessage::set_code方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: handleRequest

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;
	}
开发者ID:mgronhol,项目名称:Saiga,代码行数:94,代码来源:libHerd.cpp


注:本文中的HttpMessage::set_code方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。