提升Asio收到未知号码?

时间:2012-08-30 15:58:44

标签: c++ http boost boost-asio

我写了一个Http / Rest客户端。

主要问题是我收到了所请求数据中的一些未知数字。 我真的不知道他们来自哪里..

e0b
<html>
<head>
[...]
</body>
</html>
0 

您最后会看到 e0b 0 。 例如,在大的xml文件中,我有这样的东西:

<sometag id="somei
2000
d"><child>
...
</child></some
2000
tag>

我不可复制。

我的代码:

  // read the response status code
  boost::asio::streambuf httpStreamBufferResponse;
  boost::asio::read_until(httpSocket, httpStreamBufferResponse, "\r\n");

  // check status code and validate
  istream httpResponseIStream(&httpStreamBufferResponse);

  // temp var for version
  string sHttpVersion;
  httpResponseIStream >> sHttpVersion;

  // temp var for status code
  unsigned int uiStatusCode;
  httpResponseIStream >> uiStatusCode;

  // fetch status message and switch it
  string sStatusMessage;
  getline(httpResponseIStream, sStatusMessage);
  if(!httpResponseIStream || sHttpVersion.substr(0, 5) != "HTTP/"){
    new Note(eNotesType(ERROR), "Request Interrupt", "Invalid Request Response");
    Log::write("ERROR: Request Interrupt: Invalid Request Response");
  }
  // != 200 even means that something is not OK
  if(uiStatusCode != 200){
    this -> sHttpStatusCode = uiStatusCode;
    new Note(eNotesType(WARNING), "Request Response " 
      + boost::lexical_cast<string>(uiStatusCode), httpErrorToString.at(uiStatusCode));
    Log::write("WARNING: Request Response " 
      + boost::lexical_cast<string>(uiStatusCode) + ": " + httpErrorToString.at(uiStatusCode));
  }

  // Read the response headers, which are terminated by a blank line.
  boost::asio::read_until(httpSocket, httpStreamBufferResponse, "\r\n\r\n");

  // Process the response header
  stringstream responseSStream;
  string responseSHeader;
  while (getline( httpResponseIStream, responseSHeader ) && responseSHeader != "\r" ) {
    responseSStream << responseSHeader;
  }
  // store header in member variable
  this -> sHttpResponseHeader = sHttpVersion + " " + boost::lexical_cast<string>(uiStatusCode) + " " 
    + httpErrorToString.at(uiStatusCode) + "\n" + responseSStream.str();

  // read until EOF and writing data to output as we go.
  ostringstream responseOSStream;
  while(boost::asio::read(httpSocket, httpStreamBufferResponse, boost::asio::transfer_at_least(1), error)){
    responseOSStream << &httpStreamBufferResponse;
  }

  // store content in member variable
  this -> sHttpResponseContent = responseOSStream.str();

  // if there is no EOF
  if(error != boost::asio::error::eof){ 
    new Note(eNotesType(ERROR), "Request Interrupt", "Invalid Response End");
    Log::write("ERROR: Request Interrupt: Invalid  Response End");    
  }

// catch not known exceptions properly
} catch (exception& e){
  string exceptionMessage = e.what();
  new Note(eNotesType(ERROR), "Exception", exceptionMessage);
  Log::write("ERROR: Exception: " + exceptionMessage);    
}

// log http standby
Log::write("http status: standby");

如果有人任何想法来自哪里,那将是一件非常愉快的事情。?!

我的神经紧张......

1 个答案:

答案 0 :(得分:3)

您的代码声称符合HTTP / 1.1并且实际上并不符合HTTP / 1.1的要求。要么不要求HTTP / 1.1合规性,要么确保您的代码执行标准所说客户必须做的所有事情。

  

所有HTTP / 1.1应用程序必须能够接收和解码“分块”传输编码,并且必须忽略他们不理解的块扩展扩展。 - HTTP/1.1 specification, section 3.6.1