为什么我的程序在HTTP / 1.1中读取额外的字节

时间:2013-02-01 20:28:04

标签: c++ http sockets

我正在尝试使用套接字并尝试构建一个非常简单的webbot。

这是我的代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <cstring>
#include <iostream>
#include <string>

#define HTTP_PORT "80"

#define HOST "www.taringa.net"
#define PORT HTTP_PORT

#define IN_BUFFSIZE 1024
#define OUT_BUFFSIZE 1024

#define REQUEST "GET /Taringa/posts HTTP/1.0\r\nHost: www.taringa.net\r\nUser-Agent: foo\r\n\r\n"

using namespace std;

int main(int argc, char **argv) {

  struct addrinfo hints, *res;
  struct sockaddr_in servAddress;
  int sockfd;

  char addrstr[100];
  char buff_msg_out[OUT_BUFFSIZE], buff_msg_in[IN_BUFFSIZE];

  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ( getaddrinfo(HOST, HTTP_PORT, &hints, &res) != 0) {
     cerr << "Error en getaddrinfo" << endl;
     return -1;
  }


  // Crear socket
  if ( ( sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol) ) < 0 )
  {
   cerr << "Error en socket()" << endl;
   return -1;
  }


  // Iniciar conexion
  if ( connect(sockfd, res->ai_addr, res->ai_addrlen) == -1 )
  {
   cerr << "Error en connect()" << endl;
   cerr << "Error: " << strerror(errno) << endl;
   return -1;
  }

  cout << "Conectado con éxito" << endl;

  // Enviar datos
  strncpy(buff_msg_out, REQUEST, strlen(REQUEST));

  if ( send(sockfd, buff_msg_out, strlen(buff_msg_out), 0) <= 0 )
  {
   cerr << "Error en write()" << endl;
   return -1;
  }

  cout << "Mensaje enviado:" << endl << buff_msg_out << endl << endl;

  int bytes_recv = 0;

  while ( ( bytes_recv = recv(sockfd, buff_msg_in, IN_BUFFSIZE-1, 0)) > 0 )
  {
    buff_msg_in[bytes_recv] = '\0';
    cout << buff_msg_in << endl;        
  }

  freeaddrinfo(res);
  close(sockfd);

  return 0;
}

这是请求具有HTTP / 1.0

时的输出
HTTP/1.1 200 OK
Server: n0
Date: Fri, 01 Feb 2013 19:57:26 GMT
Content-Type: text/html; charset=utf8
Connection: close
Set-Cookie: trngssn=06359673; path=/
Set-Cookie: trngssn=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: taringa_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: lastNick=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: fbs=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: tws=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: iB-friendfind=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es" >
        <head profile="http://purl.org/NET/erdf/profile" prefix="og: http://ogp.me/ns
# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv=”X-Frame-Options” content=”Deny” />

                <link rel="alternate" type="application/atom+xml" title="Últimos Posts de Taringa" href="/rss/Taringa/posts/" />
                <link rel="alternate" type="application/atom+xml" title="Últimos Temas de Taringa" href="/rss/Taringa/tem
as/" />

                <title>Posts de Taringa!  - Taringa!</title>

...

    </body>
</html>

但是当我指定HTTP / 1.1时,回复是

HTTP/1.1 200 OK
Server: n0
Date: Fri, 01 Feb 2013 20:03:54 GMT
Content-Type: text/html; charset=utf8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: trngssn=81047255; path=/
Set-Cookie: trngssn=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: taringa_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: lastNick=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: fbs=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: tws=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: iB-friendfind=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net

d0f
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es" >
        <head profile="http://purl.org/NET/erdf
/profile" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv=”X-Frame-Options” content=”Deny” />

                <link rel="alternate" type="application/atom+xml" title="Últimos Posts de Taringa" href="/rss/Taringa/posts/" />
                <link rel="alternate" type="application/atom+xml" title="Últimos Te
mas de Taringa" href="/rss/Taringa/temas/" />

                <title>Posts de Taringa!  - Taringa!</title>

...

,这是问题

    </body>
</html>

0

并且在关闭通信之前等了几秒钟。

我刚尝试使用stackoverflow.com/about,它运行正常。除了服务器在网页后发送给我的以下文本

</html>HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>

我错过了什么吗?

2 个答案:

答案 0 :(得分:5)

服务器正在使用chunked encoding。 d0f是八位字节中以“十六进制”表示的“块”的长度。 0是下一个块的长度(即没有)。

答案 1 :(得分:3)

  

服务器:n0
  日期:2013年2月1日星期五20:03:54 GMT
  内容类型:text / html;字符集= UTF8
  转移编码:分块
  连接:保持活力

如果不这样做,请不要说你支持HTTP 1.1。

  

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