本文整理汇总了C++中HttpResponse::getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpResponse::getStatusCode方法的具体用法?C++ HttpResponse::getStatusCode怎么用?C++ HttpResponse::getStatusCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpResponse
的用法示例。
在下文中一共展示了HttpResponse::getStatusCode方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getUrl
void spider::PageDownloader::download() const {
using std::back_inserter;
using std::string;
using std::vector;
try {
Url const& url = getUrl();
std::cerr << "Downloading page: " << url << std::endl;
HttpResponse response = getResponse();
int statusCode = response.getStatusCode();
if (statusCode < 200 || statusCode >= 400) {
return; // ignore errors
}
if (statusCode >= 300 && statusCode < 400) {
handleRedirect(response);
return;
}
string content = getContent(response);
vector<Url> urls;
m_finder.getUrls(url, content, back_inserter(urls));
m_manager.download(url, urls.begin(), urls.end());
} catch (ConnectionException const& exception) {
std::cerr << exception.what() << std::endl;
}
}
示例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();
}
示例3: geneHttpResponse
void HttpProtocol::geneHttpResponse(const HttpResponse& http,string& response)
{
HttpHeader header;
http.getHeader(header);
char t[10];
sprintf(t, "%d", http.getStatusCode());
string code = t;
response = "" + http.getVersion() + " " + code + " " + http.getReason() + "\n";
geneHeader(response,header);
response += http.getContent();
}
示例4:
bool Http11Handler::writeResponse(void* req, void* res, void* context) {
HttpRequest* request = (HttpRequest*)req;
HttpResponse* response = (HttpResponse*)res;
if(isClosed()) {
if(request!=NULL) {
delete request;
}
delete response;
return true;
}
if(!response->isDone()) {
response->updateContent(request, chunkSize);
}
if(response->getHeader(HttpRequest::Connection)=="")
{
if(StringUtil::toLowerCopy(request->getHeader(HttpRequest::Connection))!="keep-alive"
|| CastUtil::lexical_cast<int>(response->getStatusCode())>307 || request->getHttpVers()<1.1)
{
response->addHeaderValue(HttpResponse::Connection, "close");
}
else
{
response->addHeaderValue(HttpResponse::Connection, "keep-alive");
}
}
std::string data = response->generateOnlyHeaderResponse(request);
if(!write(data)) {
bool isFirst = true;
while(response->hasContent && (data = response->getRemainingContent(request->getUrl(), isFirst)) != "") {
isFirst = false;
if(write(data)) {
break;
}
}
}
if(request!=NULL) {
delete request;
}
delete response;
return true;
}
示例5: handle
bool ControllerHandler::handle(HttpRequest* req, HttpResponse& res, map<string, string> urlpattMap, map<string, string> mappattMap, void* dlib,
string ext, resFuncMap rstCntMap, map<string, string> mapMap, map<string, string> urlMap, string pthwofile)
{
Logger logger = Logger::getLogger("ControllerHandler");
string claz;
bool isContrl = false;
if((urlpattMap[req->getCntxt_name()+"*.*"]!="" || urlMap[req->getCntxt_name()+ext]!=""))
{
//logger << "Controller requested for " << req->getCntxt_name() << " name " << urlMap[req->getCntxt_name()+ext] << endl;
if(urlpattMap[req->getCntxt_name()+"*.*"]!="")
claz = "getReflectionCIFor" + urlpattMap[req->getCntxt_name()+"*.*"];
else
claz = "getReflectionCIFor" + urlMap[req->getCntxt_name()+ext];
string libName = Constants::INTER_LIB_FILE;
if(dlib == NULL)
{
cerr << dlerror() << endl;
exit(-1);
}
void *mkr = dlsym(dlib, claz.c_str());
if(mkr!=NULL)
{
FunPtr f = (FunPtr)mkr;
ClassInfo srv = f();
args argus;
Constructor ctor = srv.getConstructor(argus);
Reflector ref;
void *_temp = ref.newInstanceGVP(ctor);
Controller *thrd = (Controller *)_temp;
try{
logger << "Controller called" << endl;
res = thrd->service(*req);
logger << res.getStatusCode() << endl;
logger << res.getContent_type() << endl;
logger << res.getContent_len() << endl;
if(res.getStatusCode()!="")
isContrl = true;
ext = AuthHandler::getFileExtension(req->getUrl());
//delete mkr;
}catch(...){ logger << "Controller exception" << endl;}
logger << "Controller called\n" << flush;
}
}
else if((mappattMap[req->getCntxt_name()+"*.*"]!="" || mapMap[req->getCntxt_name()+ext]!=""))
{
string file = req->getFile();
string fili = file.substr(0,file.find_last_of("."));
if(mappattMap[req->getCntxt_name()+"*.*"]!="")
{
req->setFile(fili+mappattMap[req->getCntxt_name()+"*.*"]);
logger << "URL mapped from * to " << mappattMap[req->getCntxt_name()+"*.*"] << endl;
}
else
{
req->setFile(fili+mapMap[req->getCntxt_name()+ext]);
logger << "URL mapped from " << ext << " to " << mapMap[req->getCntxt_name()+ext] << endl;
}
}
else
{
resFuncMap::iterator it;
RestFunction rft;
bool flag = false;
int prsiz = 0;
vector<string> valss;
//logger << pthwofile << endl;
for (it=rstCntMap.begin();it!=rstCntMap.end();it++)
{
valss.clear();
//logger << it->first << endl;
//if(pthwofile.find(it->first)!=string::npos)
{
RestFunction ft = it->second;
prsiz = ft.params.size();
string pthwofiletemp(pthwofile);
if(ft.baseUrl=="")
{
logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz <<
", against url: " << it->first << endl;
for (int var = 0; var < prsiz; var++)
{
//logger << "loop - " << pthwofiletemp << endl;
string valsvv(pthwofiletemp.substr(pthwofiletemp.find_last_of("/")+1));
pthwofiletemp = pthwofiletemp.substr(0, pthwofiletemp.find_last_of("/"));
valss.push_back(valsvv);
}
reverse(valss.begin(),valss.end());
//logger << "after - " << pthwofiletemp << endl;
/*if(pthwofiletemp.at(pthwofiletemp.length()-1)=='/')
{
pthwofiletemp = pthwofiletemp.substr(0, pthwofiletemp.length()-1);
}*/
//logger << "after - " << pthwofiletemp << endl;
logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz << ",vals: " << valss.size() <<
", against url: " << it->first << endl;
if(it->first==pthwofiletemp)
{
string lhs = StringUtil::toUpperCopy(ft.meth);
string rhs = StringUtil::toUpperCopy(req->getMethod());
logger << lhs << " <> " << rhs << endl;
//.........这里部分代码省略.........
示例6: handle_input
int ClientService::handle_input(ACE_HANDLE fd /* = ACE_INVALID_HANDLE */) {
if ( fd != get_handle() ) {
ND_DEBUG("[HTTP][Conn %07d] Received input for unmanaged handle.\n");
return 0;
}
ssize_t recv_cnt;
// ACE_Time_Value zeroTime(ACE_Time_Value::zero);
/*
while ( (recv_cnt = sock_.recv(buffer_, buflen_)) > 0 ) {
ND_DEBUG("[HTTP][Conn %07d] Received %d octets of request data.\n", _connectionNumber, recv_cnt);
req_.addData(buffer_, recv_cnt);
}
int closeErr = errno;
if (recv_cnt < 0 && closeErr != EWOULDBLOCK) {
ND_DEBUG("[HTTP][Conn %07d] Connection closed to %s. %p.\n", _connectionNumber, peer_name_, "Reason");
req_.clear();
return -1;
}
*/
if ( (recv_cnt = sock_.recv(buffer_, buflen_)) < 1 ) {
ND_DEBUG("[HTTP][Conn %07d] Connection closed to %s. %p.\n", _connectionNumber, peer_name_, "Reason");
req_.clear();
return -1;
}
ND_DEBUG("[HTTP][Conn %07d] Received %d octets of request data.\n", _connectionNumber, recv_cnt);
req_.addData(buffer_, recv_cnt);
// std::string reqStr(req_.getAsString());
try {
req_.parse();
}
catch (const nd_error& e) {
ND_CRITICAL("[HTTP][Conn %07d] %s\n", _connectionNumber, e.what());
req_.clear();
return 0;
}
// ND_DEBUG("[HTTP][Conn %07d] Received %d-octet request:\n%s\n", _connectionNumber, req_.getRequestLength(), reqStr.c_str());
if (req_.isComplete()) {
HttpResponse response;
httpManager::instance()->handleRequest(req_, response);
string responseStr = response.makeClientStr((req_.getMethod() != HttpRequest::MethodHEAD));
ND_DEBUG("[HTTP][Conn %07d] Sending response (%d %s, %s, %d octet contents).\n", _connectionNumber,
response.getStatusCode(),
response.getStatusStr().c_str(),
response.getContentType().c_str(),
response.getContent().length());
sock_.send(responseStr.c_str(), responseStr.length());
double ver = req_.getProtocolVersion();
req_.clear();
if ( ver == 1.0 ) {
ND_DEBUG("[HTTP][Conn %07d] Closing HTTP 1.0 connection.\n", _connectionNumber);
return -1;
}
}
else {
if ( req_.hasContent() )
ND_DEBUG("[HTTP][Conn %07d] Received %d octets of %d expected, waiting for more.\n", _connectionNumber,
req_.getContentLength(), req_.getReportedContentLength());
}
return 0;
}
示例7: handle
bool ControllerHandler::handle(HttpRequest* req, HttpResponse& res, ConfigurationData configData,
string ext, string pthwofile)
{
map<string, string> urlpattMap = configData.urlpattMap;
map<string, string> mappattMap = configData.mappattMap;
resFuncMap rstCntMap = configData.rstCntMap;
map<string, string> mapMap = configData.mapMap;
map<string, string> urlMap = configData.urlMap;
Logger logger = LoggerFactory::getLogger("ControllerHandler");
bool isContrl = false;
if((urlpattMap[req->getCntxt_name()+"*.*"]!="" || urlMap[req->getCntxt_name()+ext]!=""))
{
//logger << "Controller requested for " << req->getCntxt_name() << " name " << urlMap[req->getCntxt_name()+ext] << endl;
string controller;
if(urlpattMap[req->getCntxt_name()+"*.*"]!="")
controller = urlpattMap[req->getCntxt_name()+"*.*"];
else
controller = urlMap[req->getCntxt_name()+ext];
void *_temp = configData.ffeadContext->getBean("controller_"+req->getCntxt_name()+controller, req->getCntxt_name());
Controller* thrd = static_cast<Controller*>(_temp);
if(thrd!=NULL)
{
try{
logger << ("Controller " + controller + " called") << endl;
res = thrd->service(*req);
if(res.getStatusCode()!="")
isContrl = true;
ext = AuthHandler::getFileExtension(req->getUrl());
//delete mkr;
}catch(...){
logger << "Controller Exception occurred" << endl;
}
logger << "Controller call complete" << endl;
}
else
{
logger << "Invalid Controller" << endl;
res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
isContrl = true;
}
}
else if((mappattMap[req->getCntxt_name()+"*.*"]!="" || mapMap[req->getCntxt_name()+ext]!=""))
{
string file = req->getFile();
string fili = file.substr(0,file.find_last_of("."));
if(mappattMap[req->getCntxt_name()+"*.*"]!="")
{
req->setFile(fili+mappattMap[req->getCntxt_name()+"*.*"]);
logger << ("URL mapped from * to " + mappattMap[req->getCntxt_name()+"*.*"]) << endl;
}
else
{
req->setFile(fili+mapMap[req->getCntxt_name()+ext]);
logger << ("URL mapped from " + ext + " to " + mapMap[req->getCntxt_name()+ext]) << endl;
}
}
else
{
resFuncMap::iterator it;
RestFunction rft;
bool flag = false;
int prsiz = 0;
vector<string> valss;
map<string, string> mapOfValues;
//logger << pthwofile << endl;
for (it=rstCntMap.begin();it!=rstCntMap.end();it++)
{
valss.clear();
//logger << it->first << endl;
//if(pthwofile.find(it->first)!=string::npos)
{
RestFunction ft = it->second;
prsiz = ft.params.size();
string pthwofiletemp(pthwofile);
string baseUrl(it->first);
strVec resturlparts;
StringUtil::split(resturlparts, baseUrl, "/");
strVec urlparts;
StringUtil::split(urlparts, pthwofiletemp, "/");
if(urlparts.size()!=resturlparts.size())
{
flag = false;
//break;
}
else
{
flag = true;
}
if(flag)
{
bool fflag = true;
for (int var = 0; var < (int)resturlparts.size(); var++)
{
//logger << "resturlparts.at(var) = " << resturlparts.at(var) << endl;
//.........这里部分代码省略.........
示例8: 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;
//.........这里部分代码省略.........
示例9: 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();
}
}
}
示例10: main
//.........这里部分代码省略.........
timer.start();
if(isDebug) {
std::cout << "HTTP Request Is=>\n" << data << "\n\n" << std::endl;
}
client->connectionUnresolv(ip,port);
int bytes = client->sendData(data);
std::string tot = client->getTextData("\r\n","content-length");
long long millis = timer.elapsedMilliSeconds();
if(isDebug) {
std::cout << "HTTP Response Is=>\n" << tot << "\n\n" << std::endl;
}
HttpResponse res;
HttpResponseParser parser(tot, res);
if(res.getHeader("Set-Cookie")!="")
{
cookies = res.getHeader("Set-Cookie");
cookies = cookies.substr(0, cookies.find(";"));
}
std::string debugContentValue;
if(debugCont)
{
debugContentValue = ", Content => " + parser.getContent();
}
std::string ss;
bool passedFlag = false, done = false;
if(res.getStatusCode()==responseCode)
{
if(respCntType!="")
{
if(res.getHeader("Content-Type")==respCntType)
{
ss.clear();
ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " was Successfull, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue;
passedFlag = true;
}
else
{
ss.clear();
ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " Failed, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms"
+ ", Expected ContentType = " + respCntType + ", Actual ContentType = " + res.getHeader("Content-Type");
passedFlag = false;
}
done = true;
}
if(!done)
{
std::string cntlen = res.getHeader("Content-Length");
if(file!="")
{
std::ifstream myfile (&file[0], std::ios::binary | std::ios::ate);
if (myfile.is_open() && cntlen!="" && myfile.tellg()==CastUtil::lexical_cast<int>(cntlen))
{
ss.clear();
ss = "Test " + CastUtil::lexical_cast<std::string>(counter) + " " + request + " was Successfull, Response Time = " + CastUtil::lexical_cast<std::string>(millis) + "ms" + debugContentValue;
passedFlag = true;
}
else
{