http套接字请求返回尾随字符

时间:2019-01-03 16:04:09

标签: c sockets http

我正在学习C语言中的网络编程,并尝试创建玩具版本的wget。

但是,当我运行程序时,页面的开头和结尾都有一些尾随字符(在这种情况下为0和f43)。

程序包含两个.c和两个.h文件。

一个用于(天真地)解析地址,另一个用于发出网络请求 并转储数据。

以下是用于解析输入的文件:

url.h

#ifndef URL_H
#define URL_H

/* information of an URL*/
struct url_info
{
    char* url; //full url
    char* protocol; // protocol type: http, ftp, etc...
    char* host; // host name
    int port;   //port number
    char* path; //path
};
typedef struct url_info url_info;

static const char P_HTTP[] = "http";

void parse_url(char* url, url_info *info);

void exit_with_error(char* message);

void print_url_info(url_info info);

#endif //URL_H

url.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"url.h"


void parse_url(char* url, url_info *info)
{
    // url format: [http://]<hostname>[:<port>]/<path>

    char *full_url = malloc((strlen(url) + 1) * sizeof(char));
  char *protocol;
  char *path;
  char *host;
  int port;

  strcpy(full_url, url);
    info->url = full_url;

  char *protocol_token = strstr(url, "://");
  if (protocol_token){
    protocol = url;
    *protocol_token = '\0';
    url = protocol_token + 3;
  } else {
    protocol = "http";
  }
    info->protocol = protocol;

  char *port_token = strstr(url, ":");
  char *path_token = strstr(url, "/");

  if (port_token && port_token < path_token){
        port = atoi(port_token + 1);
        *port_token = '\0';
  } else {
    port = 80;
  }
    info->port = port;

  if (path_token){
    *path_token = '\0';
    host = url;
    path = path_token + 1;
        info->host = host;
        info->path = path;
  } else {
    exit_with_error("No trailing /.");
  }
}


void print_url_info(url_info info){
    printf("The URL contains following information: \n");
    printf("Full url:\t%s\n", info.url);
    printf("Protocol type:\t%s\n", info.protocol);
    printf("Host name:\t%s\n", info.host);
    printf("Port No.:\t%d\n", info.port);
    printf("Path:\t\t%s\n", info.path);
}


void exit_with_error(char *message)
{
    fprintf(stderr, "%s\n", message);
    exit(EXIT_FAILURE);
}

以下是发出请求的文件

wgetX.h

#ifndef WGETX_H_
#define WGETX_H_

#define B_SIZE 1024 * 5000

void write_data(const char *path, const char *data);

char* download_page(url_info info, char *buff);

char* http_get_request(char* path, char* host);

char* read_http_reply(char* recv_buf_t);

unsigned long ipfromhost(const char *host);

#endif

wgetX.c

 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>

#include "url.h"
#include "wgetX.h"

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

    url_info info;


    if (argc != 2) {
        exit_with_error("The wgetX must have exactly 1 parameter as input. \n");
    }
    char *url = argv[1];
    parse_url(url, &info);

    char *buf;
    buf = malloc(sizeof(char)*B_SIZE);
    bzero(buf, B_SIZE);

    download_page(info, buf);
  printf("%s", buf);

    free(buf);
    return (EXIT_SUCCESS);
}

char* download_page(url_info info, char *buf)
{
    struct sockaddr_in dest;
    int len, sz, mysocket;
    char *request = http_get_request(info.path, info.host);

    mysocket = socket(AF_INET, SOCK_STREAM, 0);
    memset(&dest, 0, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = ipfromhost(info.host);
    dest.sin_port = htons(info.port);
    connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
    send(mysocket, request, strlen(request), 0);

    len = 0;
    sz = 0;
    do {
        len = recv(mysocket, buf + sz, B_SIZE - sz, 0);
        if (len == -1) {continue;}
        sz += len;
    } while (len > 0);

    *(buf + sz) = '\0';

  close(mysocket);
    return buf;
}


char* http_get_request(char* path, char* host) {
    char * request_buffer = (char *) malloc(1024);
    memset(request_buffer, 0, sizeof(*request_buffer));
    snprintf(request_buffer, 1024, "GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
            path, host);
    return request_buffer;
}

unsigned long ipfromhost(const char *host){
  struct in_addr **addr_list;
  struct hostent *he;
  if ((he = gethostbyname(host)) != NULL){
    addr_list = (struct in_addr **) he->h_addr_list;
    int i;
    for (i = 0; addr_list[i] != NULL; i++){
      return addr_list[i]->s_addr;
    }
    exit_with_error("Couldn't resolve host to ip adress\n");
    return 0;
  } else {
    exit_with_error("Couldn't resolve host to ip adress\n");
    return 0;
  }
}

制作文件

LINK_TARGET = wgetX
OBJS = \
    wgetX.o \
    url.o
REBUILDABLES = $(OBJS) $(LINK_TARGET)
all : $(LINK_TARGET)
clean:
    rm -f $(REBUILDABLES)

$(LINK_TARGET) : $(OBJS)
    cc -g -o $@ $^

%.o : %.c
    cc -g  -Wall -o $@ -c $<

wgetX.o : wgetX.h url.h
url.o : url.h

在一个特定的url上执行程序时,我得到的html输出与源代码不同(在Chrome中找到)。我得到了垃圾字符:结尾处为零,而html开头之前为“ f43”

命令

make clean
make
./wgetX http://www.polytechnique.fr/

输出

我收到了带有状态码和全部的http回复消息,并且在“

1 个答案:

答案 0 :(得分:1)

  

我得到了垃圾字符:结尾处为零,而html开头之前为“ f43”

欢迎来到HTTP的美好世界。请注意,即使看起来像这样,HTTP也不是简单的协议。应该说的是,最初在RFC 2616中发布的HTTP / 1.1标准具有176页文本。

您可能会在这里看到内容的分块传输编码。在这种编码中,内容不是作为一个整体传送,而是分成多个块,每个块都以长度为前缀(十六进制)。即像这样的东西:

 HTTP/1.1 200 ok
 Transfer-Encoding: chunked

 a
 0123456789
 12
 These are 18 bytes
 0

在您的特定情况下,初始f43 “恰好在html开头之前” 是以下块的长度(f43,十六进制为3907,十进制为10),“零为end” 是最后一块(0)的长度。

有关更多信息,请参见section 3.6.1 in RFC 2616section 4.1 in RFC 7230