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


C++ HttpResponse::getHeaders方法代码示例

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


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

示例1: print_response

void print_response(HttpResponse& response, TcpClientSocket& socket) {
	std::cout << narrow(widen(response.getStatusLine(), CP_UTF8), CP_OEMCP) << std::endl;
	auto headers = response.getHeaders();
	for (auto it = std::begin(headers), eit = std::end(headers); it != eit; ++it) {
		for (auto vit = std::begin(it->second), veit = std::end(it->second); vit != veit; ++vit) {
			std::cout << it->first << ": " << *vit << std::endl;
		}
	}
	std::cout << "======END OF HEADERS======" << std::endl;

	auto& buffer = response.getBuffer();
	if (response.getIsChunked()) {
		std::cout << "[[CHUNKED ENCODING]]" << std::endl;
		for (auto chunk = buffer.getchunk(); !chunk.empty(); chunk = buffer.getchunk()) {
			std::cout << chunk;
		}
		std::cout << "[[TRAILING HEADERS]]" << std::endl;
		for (auto th = buffer.getline(); !th.empty(); th = buffer.getline()) {
			std::cout << th << std::endl;
		}
	}
	else if (HttpResponse::nlen != response.getContentLength()) {
		std::cout << "[[EXACTLY " << response.getContentLength() << " BYTES]]" << std::endl;
		std::cout << buffer.getcount(response.getContentLength()) << std::endl;
	}
	else {
		std::vector<BYTE> buff(8 * 1024, 0);
		for (auto last = socket.recv_upto(std::begin(buff), std::end(buff));
			last != std::begin(buff); last = socket.recv_upto(std::begin(buff), std::end(buff))) {
				std::cout << std::string(std::begin(buff), last);
		}
	}
	std::cout << std::endl << "======END OF RESPONSE======" << std::endl;
}
开发者ID:Joker-vD,项目名称:sntlm,代码行数:34,代码来源:main.cpp

示例2: onReadComplete

void WebClientApp::onReadComplete()
{
	mText.push_back( "Read complete" );
	
	console() << "HTTP version: ";
	switch ( mHttpResponse.getHttpVersion() ) {
		case HttpVersion::HTTP_0_9:
			console() << "0.9";
			break;
		case HttpVersion::HTTP_1_0:
			console() << "1.0";
			break;
		case HttpVersion::HTTP_1_1:
			console() << "1.1";
			break;
		case HttpVersion::HTTP_2_0:
			console() << "2.0";
			break;
	}
	console() << endl;
	console() << "Status code: " << mHttpResponse.getStatusCode() << endl;
	console() << "Reason: " << mHttpResponse.getReason() << endl;
	
	console() << "Header fields: " << endl;
	for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) {
		console() << ">> " << kvp.first << ": " << kvp.second << endl;
	}
	console() << endl;
	
	console() << "Body:" << endl;
	console() << HttpResponse::bufferToString( mHttpResponse.getBody() ) << endl;

	mSession->close();
}
开发者ID:BanTheRewind,项目名称:Cinder-Protocol,代码行数:34,代码来源:WebClientApp.cpp

示例3: getReferrer

void spider::PageDownloader::handleRedirect(HttpResponse & response) const {
    using std::string;
    using std::vector;
    using boost::optional;

    HeaderCollection const& headers = response.getHeaders();
    if (!headers.hasHeader("Location")) {        
        return;
    }
    string urlString = headers.getHeader("Location").getValue(0);
    optional<Url> referrer = getReferrer();
    vector<Url> redirectUrls { Url::parse(urlString) };
    m_manager.download(
       referrer, 
       redirectUrls.begin(), redirectUrls.end());
}
开发者ID:jehugaleahsa,项目名称:spider-cpp,代码行数:16,代码来源:page_downloader.cpp

示例4: ngx_http_ffeadcpp_module_handler_post_read

static ngx_int_t ngx_http_ffeadcpp_module_handler_post_read(ngx_http_request_t *r)
{
string cntpath = "";
	cntpath.append(ffeadcpp_path.data, ffeadcpp_path.len);
	cntpath += "/web/";
	HttpRequest* req = new HttpRequest(cntpath);

	//cerr << "FFEAD in ngx_http_ffeadcpp_module_handler " << cntpath << r->uri.data << endl;
    ngx_int_t    rc;
    ngx_buf_t   *b;
    ngx_chain_t  out;

	ngx_list_part_t            *part;
    ngx_table_elt_t            *h;
    ngx_uint_t                  i;

    /*
    Get the first part of the list. There is usual only one part.
    */
    part = &r->headers_in.headers.part;
    h = part->elts;

    /*
    Headers list array may consist of more than one part,
    so loop through all of it
    */
    for (i = 0; /* void */ ; i++) {
        if (i >= part->nelts) {
            if (part->next == NULL) {
                /* The last part, search is done. */
                break;
            }

            part = part->next;
            h = part->elts;
            i = 0;
        }
//	cerr << "header -> " << string(h[i].key.data, h[i].key.len) << " = " << string(h[i].value.data, h[i].value.len) << endl;
	req->buildRequest(string(h[i].key.data, h[i].key.len), string(h[i].value.data, h[i].value.len));
    }

    string content;
	ngx_http_read_input_data(r, content);
	//cerr << "Input Request Data\n " << content << "\n======================\n" << endl;
	//cerr << "URL -> " << string(r->uri.data,r->uri.len) << endl;
	//cerr << "Method -> " << string(r->main->method_name.data, r->main->method_name.len) << endl;
	if(content!="")
	{
		req->buildRequest("Content", content.c_str());
	}
	req->buildRequest("URL",  string(r->uri.data,r->uri.len));
	req->buildRequest("Method", string(r->main->method_name.data, r->main->method_name.len));
	if(r->args.len > 0)
	{
		req->buildRequest("GetArguments", string(r->args.data,r->args.len));
	}
	req->buildRequest("HttpVersion", CastUtil::lexical_cast<string>(r->http_version));

	HttpResponse* respo = new HttpResponse;
	ServiceTask* task = new ServiceTask;
	task->handle(req, respo);
	delete task;
cerr << req->toString() << endl;
	if(respo->isDone()) {
		for (int var = 0; var < (int)respo->getCookies().size(); var++)
		{
			set_custom_header_in_headers_out(r, "Set-Cookie", respo->getCookies().at(var));
		}

		/* allocate a buffer for your response body */
		b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
		if (b == NULL) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}

		/* attach this buffer to the buffer chain */
		out.buf = b;
		out.next = NULL;

		string data = respo->generateResponse(false);
		//cerr << "OUT -> " << data << endl;
		map<string,string>::const_iterator it;
		for(it=respo->getHeaders().begin();it!=respo->getHeaders().end();it++) {
			if(StringUtil::toLowerCopy(it->first)==StringUtil::toLowerCopy(HttpResponse::ContentLength)) {
				r->headers_out.content_length_n = CastUtil::lexical_cast<int>(it->second);
			} else {
				set_custom_header_in_headers_out(r, it->first, it->second);
			}
		}
		//cerr << "done writing headers" << endl;		
/* adjust the pointers of the buffer */
		b->pos = data.c_str();
		b->last = data.length();
		b->memory = 1;    /* this buffer is in memory */
		b->last_buf = 1;  /* this is the last buffer in the buffer chain */

		/* set the status line */
		r->headers_out.status = CastUtil::lexical_cast<int>(respo->getStatusCode());

		delete respo;
//.........这里部分代码省略.........
开发者ID:sumeetchhetri,项目名称:ffead-cpp,代码行数:101,代码来源:works_ngx_http_ffeadcpp_module.cpp

示例5: onRead

void HttpClientApp::onRead( ci::BufferRef buffer )
{
	size_t sz	= buffer->getSize();
	mBytesRead	+= sz;
	mText.push_back( toString( sz ) + " bytes read" );
	
	if ( !mHttpResponse.hasHeader() ) {
		
		// Parse header
		mHttpResponse.parseHeader( HttpResponse::bufferToString( buffer ) );
		buffer = HttpResponse::removeHeader( buffer );
		
		// Get content-length
		for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) {
			if ( kvp.first == "Content-Length" ) {
				mContentLength = fromString<size_t>( kvp.second );
				break;
			}
		}
	}
	
	// Append buffer to body
	mHttpResponse.append( buffer );
	
	if ( mBytesRead < mContentLength ) {
		
		// Keep reading until we hit the content length
		mSession->read();
	} else {

		mText.push_back( "Read complete" );
		mText.push_back( toString( mHttpResponse.getStatusCode() ) + " " + mHttpResponse.getReason() );
		
		if ( mHttpResponse.getStatusCode() == 200 ) {
			for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) {
				
				// Choose file extension based on MIME type
				if ( kvp.first == "Content-Type" ) {
					string mime = kvp.second;
					
					if ( mime == "audio/mp3" ) {
						mFilename += ".mp3";
					} else if ( mime == "image/jpeg" ) {
						mFilename += ".jpg";
					} else if ( mime == "image/png" ) {
						mFilename += ".png";
					}
				} else if ( kvp.first == "Connection" ) {
					
					// Close connection if requested by server
					if ( kvp.second == "close" ) {
						mSession->close();
					}
				}
			}

			// Save the file
			fs::path path = getAppPath();
#if !defined ( CINDER_MSW )
			path = path.parent_path();
#endif
			path = path / mFilename;
			OStreamFileRef file = writeFileStream( path );
			file->write( mHttpResponse.getBody() );
			
			mText.push_back( mFilename + " downloaded" );
		} else {
			
			// Write error
			mText.push_back( "Response: " +  HttpResponse::bufferToString( mHttpResponse.getBody() ) );
			
			mSession->close();
		}
	}
}
开发者ID:BanTheRewind,项目名称:Cinder-Protocol,代码行数:75,代码来源:HttpClientApp.cpp


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