自己动手学TCP/IP–http协议(http报文头)
在前面的一篇文章中,简单了介绍了HTTP报文格式,详情参考http://www.firefoxbug.net/?cat=47。
这里大概介绍下基本的,常见的HTTP包头格式。
POST /report/getComment.jsp HTTP/1.1
Host: yeeg.com
Connection: keep-alive
Content-Length: 161
Origin: http://www.1g1g.com
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
content-type: application/x-www-form-urlencoded
Accept: */*
Referer: http://www.1g1g.com/player/loader.swf?uid=0.8106261373031884
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=C3F105F72E3602D6292D3E4561E8E400
上面是一个POST包的包头,其中Content-Length字段里面的值就是POST包数据段的长度。可以用
wireshark抓取POST包,会发现,post包是把报文头和数据内容分开来发的,会被TCP分片,然后重组。
具体这里不详细讨论。
GET /enclosure/2010-09-10T02_51_05-07_00.mp3 HTTP/1.1
Host: 805665086.podomatic.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
Accept: */*
Referer: http://www.1g1g.com/player/loader.swf?uid=0.8106261373031884
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
上面是一个GET包,GET包请求的资源都是在URL里面的,所以数据段也就没有了,可以通过抓包分析。
HTTP/1.1 200 OK
Date: Tue, 10 Jul 2012 09:12:52 GMT
Server: Apache/2.2.14 (Ubuntu)
Last-Modified: Thu, 23 Dec 2010 19:29:26 GMT
ETag: "960fcf-4a6459-49818e3486374"
Accept-Ranges: bytes
Content-Length: 487
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: audio/mpeg
上面是一个http响应包,Content-Length指明了数据段的大小。
下面是我今天用C写了解析HTTP报文头的程序。注意:下面代码只主要用libcap实现实现了部分功能,具体
是解析GET包头,POST包头,还有HTTP相应包头,可能存在一些不足,希望大家多多交流。
- /*
- capture http packet by firefoxbug
- */
- #include <pcap.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <net/ethernet.h>
- #include <netinet/ip_icmp.h> //Provides declarations for icmp header
- #include <netinet/udp.h> //Provides declarations for udp header
- #include <netinet/tcp.h> //Provides declarations for tcp header
- #include <netinet/ip.h> //Provides declarations for ip header
- #define BUFFSIZE 1500
- typedef struct port{
- unsigned int src_port;
- unsigned int des_port;
- }Port;
- Port port;
- int tcp=0;
- FILE *logfile;
- int size;
- void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer);
- char *print_tcp_packet(const char *Buffer);
- void get_line(char *data,char *buff,int length);
- void print_http_req_packet(char *data);
- void print_http_ans_packet(char *data);
- int main(int argc,char *argv[])
- {
- pcap_if_t *alldevsp , *device;
- pcap_t *handle; //Handle of the device that shall be sniffed
- char errbuf[100] , *devname , devs[100][100];
- int count = 1 , n;
- //First get the list of available devices
- printf("Finding available devices ... ");
- if( pcap_findalldevs( &alldevsp , errbuf) )
- {
- printf("Error finding devices : %s" , errbuf);
- exit(1);
- }
- printf("Done");
- //Print the available devices
- printf("\nAvailable Devices are :\n");
- for(device = alldevsp ; device != NULL ; device = device->next)
- {
- printf("%d. %s - %s\n" , count , device->name , device->description);
- if(device->name != NULL)
- {
- strcpy(devs[count] , device->name);
- }
- count++;
- }
- //Ask user which device to sniff
- printf("Enter the number of the device you want to sniff : ");
- scanf("%d" , &n);
- devname = devs[n];
- //Open the device for sniffing
- printf("Opening device %s for sniffing ... " , devname);
- handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
- if (handle == NULL)
- {
- fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
- exit(1);
- }
- printf("Done\n");
- logfile=fopen("log.txt","w");
- if(logfile==NULL)
- {
- printf("Unable to create file.");
- }
- //Put the device in sniff loop
- pcap_loop(handle , -1 , process_packet , NULL);
- return 0;
- }
- void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer)
- {
- size = header->len;
- // fprintf(logfile,"length of packet : %d \n",size);
- //Get the IP Header part of this packet , excluding the ethernet header
- struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
- switch (iph->protocol) //Check the Protocol and do accordingly...
- {
- case 6: //TCP Protocol
- ++tcp;
- // printf("TCP : %d \n", tcp);
- unsigned char *data = print_tcp_packet(buffer);
- if (size <= 0)
- break;
- if (port.des_port == 80)
- {
- print_http_req_packet(data);
- }
- else if ( port.src_port == 80 )
- {
- print_http_ans_packet(data);
- }
- break;
- }
- }
- char *print_tcp_packet(const char *Buffer)
- {
- //IP header
- struct iphdr *iph = (struct iphdr *)( Buffer + sizeof(struct ethhdr) );
- unsigned int iphdrlen = iph->ihl*4;
- //TCP header
- struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen + sizeof(struct ethhdr));
- port.src_port = ntohs(tcph->source);
- port.des_port = ntohs(tcph->dest);
- // mac_header + ip_header + tcp_header
- int header_size = sizeof(struct ethhdr) + iphdrlen + tcph->doff*4;
- size = size - header_size;
- // fprintf(logfile,"length of header : %d \n",header_size );
- return (char *)(Buffer + header_size);
- }
- void print_http_req_packet(char *data)
- {
- if( strncmp(data,"GET",3) == 0 || strncmp(data,"POST",4) == 0 )
- {
- fprintf(logfile,"\n/***********************length of data : %d**********************/ \n",size);
- fprintf(logfile,"From %d To %d \n",port.src_port,port.des_port);
- int i = 0;
- for( ; i < size ; ++i)
- {
- fprintf(logfile,"%c",*(data + i));
- }
- fprintf(logfile,"/***************************** end *******************************/ \n\n");
- }
- return ;
- }
- void print_http_ans_packet(char *data)
- {
- if( strncmp(data,"HTTP",4) == 0 )
- {
- fprintf(logfile,"\n/***********************length of data : %d**********************/ \n",size);
- fprintf(logfile,"From %d To %d \n",port.src_port,port.des_port);
- char buff[BUFFSIZE] = {'\0'};
- get_line(data,buff,size);
- fprintf(logfile,"%s",buff);
- unsigned int off = strlen(buff);
- size = size - off;
- while(strcmp(buff,"\r\n") != 0)
- {
- memset(buff,'\0',sizeof(buff));
- get_line(data + off,buff,size);
- fprintf(logfile,"%s",buff);
- off = off + strlen(buff);
- size = size - off;
- }
- fprintf(logfile,"/***************************** end *******************************/ \n\n");
- }
- }
- void get_line(char *data,char *buff,int length)
- {
- int i = 0;
- char ch;
- for ( ; i < length;++i)
- {
- ch = *(data + i);
- *(buff + i) = ch;
- if(ch == '\n')
- break;
- }
- }
自己动手学TCP/IP–http协议(http报文头)的更多相关文章
- 跟我学TCP/IP系列
最近在微信公众号“java与Android开发专栏”,看到系列文章“跟我学TCP/IP系列”,共7章,文章很赞. 系列文章在CSDN上也有分发,下列出地址以备以后查看(版权问题不转载内容). http ...
- 对TCP/IP网络协议的深入浅出归纳
前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没弄清楚.后来 我花了一些时间去了解这些网络协议,现在对TCP/IP网 ...
- 转:对TCP/IP网络协议的深入浅出归纳
转自:http://blog.jobbole.com/74795/ 前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没 ...
- 对TCP/IP网络协议的深入浅出归纳(转)
前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没弄清楚.后来 我花了一些时间去了解这些网络协议,现在对TCP/IP网 ...
- TCP/IP网络协议的通俗理解,SOCKET,HTTP,SOAP
TCP/IP,HTTP,SOAP等协议之区别 术语TCP/IP代表传输控制协议/网际协议,指的是一系列协议.“IP”代表网际协议,TCP和UDP使用该协议从一个网络传送数据包到另一个网络.把IP想 ...
- Linux学习(1)- TCP/IP网络协议基础
Linux学习(1)- TCP/IP网络协议基础 一.TCP/IP 简介 学习内容 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输 ...
- 计算机网络【1】—— OSI七层协议和TCP/IP四层协议
新开一贴,专门用来记录计算机网络相关知识. 一.OSI七层协议 物理层.数据链路层.网络层.传输层.会话层.表示层.应用层 二.TCP/IP四层协议 网络接口层.网际层.运输层.应用层 三.五层协议 ...
- TCP/IP四层协议模型与ISO七层模型
TCP/IP四层协议模型与ISO七层模型 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他 ...
- TCP/IP网络协议基础
实验楼学习网络协议传送门 一.TCP/IP简介 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输控制协议和网络协议的简称,它定义了电 ...
随机推荐
- jdbc插入修改clob类型的两种方式
方法一: Connection con = dbl.loadConnection(); strSql = "insert into table1(id,a) values (1,EMPTY_ ...
- Android之发送短信的两种方式
SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...
- Day9 - Python 多线程、进程
Python之路,Day9, 进程.线程.协程篇 本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线 ...
- codevs 1515 跳
/* 画矩阵找规律发现是杨辉三角 Cg (i,j)= C (i+j,i); 贪心走的话 沿着0行(列)一直走然后拐直角 拐弯后每个格子都累加 Cg (n,0) + Cg (n,1) + Cg (n,2 ...
- 构建可比较的对象(IComparable)
IComparable接口 System.IComparable接口指定了一种允许一个对象可基于某些特定键值进行排序的行为. namespace System { [ComVisible(true)] ...
- js页面加载事件
<body onload="myfunction()" > </body> <script type="text/javascript&qu ...
- The requested URL ***** was not found on this serve
Wamp的Alias具体是干什么用的,后面要研究一下!!!! 我是之前创建了一个站点用Alias,后来把站点文件移到了www下后,除了首页都访问不了了.显示“The requested URL *** ...
- css.day.05.eg
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】block一点也不神秘————如何利用block进行回调
我们在开发中常常会用到函数回调,你可以用通知来替代回调,但是大多数时候回调是比通知方便的,所以何乐而不为呢?如果你不知道回调使用的场景,我们来假设一下: 1.我现在玩手机 2.突然手机没有电了 3.我 ...
- /root/.bashrc与/etc/profile的异同
要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是loginshell 和non-loginshell. 交互式模式就是shell等待你的输入,并 ...