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

标签 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;
  }
}

Makefile

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.google.com/

输出

我收到了带有状态代码和所有内容的http回复消息,就在“

最佳答案

I get garbage characters: a zero at the end and "f43" just before the start of the html

欢迎来到 HTTP 的奇妙世界。请注意,HTTP 并不是一个简单的协议(protocol),尽管它可能看起来像这样。应该说最初在 RFC 2616 中发布的 HTTP/1.1 标准有 176 页文本。

您可能在这里看到的是内容的分块传输编码。在此编码中,内容不是作为一个单独的片段传输,而是以多个 block 的形式传输,每个 block 都以长度(十六进制)为前缀。 IE。像这样的东西:

 HTTP/1.1 200 ok
 Transfer-Encoding: chunked

 a
 0123456789
 12
 These are 18 bytes
 0

在您的具体情况下,初始 f43 “就在 html 开始之前” 是以下 block 的长度(十进制 3907 的十六进制 f43)和 “零位于end" 是最后一个 block 的长度 (0)。

有关这方面的更多信息,请参阅 section 3.6.1 in RFC 2616section 4.1 in RFC 7230

关于c - http 套接字请求返回尾随字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54025860/

相关文章:

linux - Linux 中的 IO 多路复用

http - 为不支持 HTTP2 的用户提供 HTTP 版本的网站

c - 使用数组和函数求平均值

c++ - 在 winsock2 中使用 C++ 创建套接字时出现问题

C语言: Write a program that would take 30 integers and prints the largest number and the smallest number

linux - IPV6绑定(bind)失败错误: Cannot assign requested address

python - 在 python 中解码 HTTP 数据包内容,如 wireshark 中所见

rest - 在具有应该唯一的指定属性的记录已经存在的情况下,可以使用 409 HTTP 代码吗?

c - 使用给出错误的文件权限

C 结构体中的字符到整数