解析HTTP标头的子集以标识主机Web地址 [英] parse a subset of HTTP header to identify host web address

查看:212
本文介绍了解析HTTP标头的子集以标识主机Web地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP协议全部包含在TCP数据包的数据部分中。

The HTTP protocol is all contained in the data portion of TCP packets.

作为一项任务,我需要解析HTTP头域(出于这个问题的目的,我们只考虑主机网址)只使用字符串解析功能,我不能使用任何现有的库来做到这一点。我试图找到HTTP标头的按位分段,但失败了。我真的不知道现在该做什么。有什么建议么?

As an assignment I need to parse HTTP headerfields (for purposes of this question lets only consider host web address) only using string parsing functions, I cannot use any existing libraries to do that. I tried to find a bitwise segmentation of HTTP header, but failed. I really do not know what to do now. Any suggestions?

事先谢谢

我已经完成的是提取以太网,IP和TCP头信息并进行了解析十六进制形式的数据,即

What I have already done is extracted Ethernet, IP and TCP header information and have parsed data in hexadecimal form, i.e.

数据

    48 54 54 50 2F 31 2E 31 20 33 30 34 20 4E 6F 74         HTTP/1.1 304 Not
    20 4D 6F 64 69 66 69 65 64 0D 0A 58 2D 43 6F 6E          Modified..X-Con
    74 65 6E 74 2D 54 79 70 65 2D 4F 70 74 69 6F 6E         tent-Type-Option
    73 3A 20 6E 6F 73 6E 69 66 66 0D 0A 44 61 74 65         s: nosniff..Date
    3A 20 54 68 75 2C 20 30 31 20 44 65 63 20 32 30         : Thu, 01 Dec 20
    31 31 20 31 33 3A 31 36 3A 34 30 20 47 4D 54 0D         11 13:16:40 GMT.
    0A 53 65 72 76 65 72 3A 20 73 66 66 65 0D 0A 58         .Server: sffe..X
    2D 58 53 53 2D 50 72 6F 74 65 63 74 69 6F 6E 3A         -XSS-Protection:
    20 31 3B 20 6D 6F 64 65 3D 62 6C 6F 63 6B 0D 0A          1; mode=block..
    0D 0A                     
                          ..

MeNa已展示分隔HTTP标头字段的技巧。但要做到这一点,我需要将我的数据有效负载转换为字符串。我尝试按以下方式执行:

MeNa has showed the trick to separate HTTP header fields. But to do that I need to convert my data payload to string. I tried to do the following way:

unsigned char * data;
data = (unsigned char *)(packet + ETHERNET_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE);
int length = header_length - (ETHERNET_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE);

char string[length];
for (i = 0; i < length; i++) {  
    string[i] = (char)data[i];
}
printf("%s ", string);

这打印出一个字符串,但主要是小方块,而不是字符串:(

this prints out a string, but mostly of little squares, rather then charachters :(

推荐答案

我认为HTTP标头没有按位分段。在HTTP中,每个标题都以\\\\ n结尾。所以,你只需要寻找下一个\\\\ n然后选择它。

I think there is no bitwise segmentation of HTTP header. in the HTTP, each header ends with "\r\n". so, all you need is to look for the next "\r\n" and pick it out.

类似的东西:

char httpRe[] ="GET / HTTP/1.1\r\nHost: http://stackoverflow.com/\r\nReferer: https://www.google.com/\r\n\r\n";
char * parser = strtok (httpRe,"\r\n");
while (parser != NULL){
   printf ("%s\n",parser);
   parser = strtok (NULL, "\r\n");
}

这是您要找的?

这篇关于解析HTTP标头的子集以标识主机Web地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆