在前面的一篇文章中,简单了介绍了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相应包头,可能存在一些不足,希望大家多多交流。

  1. /*
  2. capture http packet by firefoxbug
  3. */
  4. #include <pcap.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10. #include <net/ethernet.h>
  11. #include <netinet/ip_icmp.h>  //Provides declarations for icmp header
  12. #include <netinet/udp.h>      //Provides declarations for udp header
  13. #include <netinet/tcp.h>      //Provides declarations for tcp header
  14. #include <netinet/ip.h>       //Provides declarations for ip header
  15. #define BUFFSIZE 1500
  16. typedef struct port{
  17. unsigned int src_port;
  18. unsigned int des_port;
  19. }Port;
  20. Port port;
  21. int tcp=0;
  22. FILE *logfile;
  23. int size;
  24. void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer);
  25. char *print_tcp_packet(const char *Buffer);
  26. void get_line(char *data,char *buff,int length);
  27. void print_http_req_packet(char *data);
  28. void print_http_ans_packet(char *data);
  29. int main(int argc,char *argv[])
  30. {
  31. pcap_if_t *alldevsp , *device;
  32. pcap_t *handle; //Handle of the device that shall be sniffed
  33. char errbuf[100] , *devname , devs[100][100];
  34. int count = 1 , n;
  35. //First get the list of available devices
  36. printf("Finding available devices ... ");
  37. if( pcap_findalldevs( &alldevsp , errbuf) )
  38. {
  39. printf("Error finding devices : %s" , errbuf);
  40. exit(1);
  41. }
  42. printf("Done");
  43. //Print the available devices
  44. printf("\nAvailable Devices are :\n");
  45. for(device = alldevsp ; device != NULL ; device = device->next)
  46. {
  47. printf("%d. %s - %s\n" , count , device->name , device->description);
  48. if(device->name != NULL)
  49. {
  50. strcpy(devs[count] , device->name);
  51. }
  52. count++;
  53. }
  54. //Ask user which device to sniff
  55. printf("Enter the number of the device you want to sniff : ");
  56. scanf("%d" , &n);
  57. devname = devs[n];
  58. //Open the device for sniffing
  59. printf("Opening device %s for sniffing ... " , devname);
  60. handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
  61. if (handle == NULL)
  62. {
  63. fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
  64. exit(1);
  65. }
  66. printf("Done\n");
  67. logfile=fopen("log.txt","w");
  68. if(logfile==NULL)
  69. {
  70. printf("Unable to create file.");
  71. }
  72. //Put the device in sniff loop
  73. pcap_loop(handle , -1 , process_packet , NULL);
  74. return 0;
  75. }
  76. void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer)
  77. {
  78. size = header->len;
  79. //  fprintf(logfile,"length of packet : %d \n",size);
  80. //Get the IP Header part of this packet , excluding the ethernet header
  81. struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
  82. switch (iph->protocol) //Check the Protocol and do accordingly...
  83. {
  84. case 6:  //TCP Protocol
  85. ++tcp;
  86. //  printf("TCP : %d \n", tcp);
  87. unsigned char *data = print_tcp_packet(buffer);
  88. if (size <= 0)
  89. break;
  90. if (port.des_port == 80)
  91. {
  92. print_http_req_packet(data);
  93. }
  94. else if ( port.src_port == 80 )
  95. {
  96. print_http_ans_packet(data);
  97. }
  98. break;
  99. }
  100. }
  101. char *print_tcp_packet(const char *Buffer)
  102. {
  103. //IP header
  104. struct iphdr *iph = (struct iphdr *)( Buffer  + sizeof(struct ethhdr) );
  105. unsigned int iphdrlen = iph->ihl*4;
  106. //TCP header
  107. struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen + sizeof(struct ethhdr));
  108. port.src_port = ntohs(tcph->source);
  109. port.des_port = ntohs(tcph->dest);
  110. // mac_header + ip_header + tcp_header
  111. int header_size =  sizeof(struct ethhdr) + iphdrlen + tcph->doff*4;
  112. size = size - header_size;
  113. //  fprintf(logfile,"length of header : %d \n",header_size );
  114. return (char *)(Buffer + header_size);
  115. }
  116. void print_http_req_packet(char *data)
  117. {
  118. if( strncmp(data,"GET",3) == 0 || strncmp(data,"POST",4) == 0 )
  119. {
  120. fprintf(logfile,"\n/***********************length of data : %d**********************/ \n",size);
  121. fprintf(logfile,"From %d To %d \n",port.src_port,port.des_port);
  122. int i = 0;
  123. for( ; i < size ; ++i)
  124. {
  125. fprintf(logfile,"%c",*(data + i));
  126. }
  127. fprintf(logfile,"/***************************** end *******************************/ \n\n");
  128. }
  129. return ;
  130. }
  131. void print_http_ans_packet(char *data)
  132. {
  133. if( strncmp(data,"HTTP",4) == 0 )
  134. {
  135. fprintf(logfile,"\n/***********************length of data : %d**********************/ \n",size);
  136. fprintf(logfile,"From %d To %d \n",port.src_port,port.des_port);
  137. char buff[BUFFSIZE] = {'\0'};
  138. get_line(data,buff,size);
  139. fprintf(logfile,"%s",buff);
  140. unsigned int off = strlen(buff);
  141. size = size - off;
  142. while(strcmp(buff,"\r\n") != 0)
  143. {
  144. memset(buff,'\0',sizeof(buff));
  145. get_line(data + off,buff,size);
  146. fprintf(logfile,"%s",buff);
  147. off = off + strlen(buff);
  148. size = size - off;
  149. }
  150. fprintf(logfile,"/***************************** end *******************************/ \n\n");
  151. }
  152. }
  153. void get_line(char *data,char *buff,int length)
  154. {
  155. int i = 0;
  156. char ch;
  157. for ( ; i < length;++i)
  158. {
  159. ch = *(data + i);
  160. *(buff + i) = ch;
  161. if(ch == '\n')
  162. break;
  163. }
  164. }

自己动手学TCP/IP–http协议(http报文头)的更多相关文章

  1. 跟我学TCP/IP系列

    最近在微信公众号“java与Android开发专栏”,看到系列文章“跟我学TCP/IP系列”,共7章,文章很赞. 系列文章在CSDN上也有分发,下列出地址以备以后查看(版权问题不转载内容). http ...

  2. 对TCP/IP网络协议的深入浅出归纳

    前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没弄清楚.后来 我花了一些时间去了解这些网络协议,现在对TCP/IP网 ...

  3. 转:对TCP/IP网络协议的深入浅出归纳

    转自:http://blog.jobbole.com/74795/ 前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没 ...

  4. 对TCP/IP网络协议的深入浅出归纳(转)

    前段时间做了一个开发,涉及到网络编程,开发过程比较顺利,但任务完成后始终觉得有一些疑惑.主要是因为对网络协议不太熟悉,对一些概念也没弄清楚.后来 我花了一些时间去了解这些网络协议,现在对TCP/IP网 ...

  5. TCP/IP网络协议的通俗理解,SOCKET,HTTP,SOAP

    TCP/IP,HTTP,SOAP等协议之区别   术语TCP/IP代表传输控制协议/网际协议,指的是一系列协议.“IP”代表网际协议,TCP和UDP使用该协议从一个网络传送数据包到另一个网络.把IP想 ...

  6. Linux学习(1)- TCP/IP网络协议基础

    Linux学习(1)- TCP/IP网络协议基础 一.TCP/IP 简介 学习内容 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输 ...

  7. 计算机网络【1】—— OSI七层协议和TCP/IP四层协议

    新开一贴,专门用来记录计算机网络相关知识. 一.OSI七层协议 物理层.数据链路层.网络层.传输层.会话层.表示层.应用层 二.TCP/IP四层协议 网络接口层.网际层.运输层.应用层 三.五层协议 ...

  8. TCP/IP四层协议模型与ISO七层模型

    TCP/IP四层协议模型与ISO七层模型 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他 ...

  9. TCP/IP网络协议基础

    实验楼学习网络协议传送门 一.TCP/IP简介 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输控制协议和网络协议的简称,它定义了电 ...

随机推荐

  1. android中Canvas使用drawBitmap绘制图片

    1.主要的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置     drawBitmap(Bitmap bitmap, float left, float ...

  2. qt优点

    (1)优良的跨平台特性.  Qt支持下列操作系统:Microsoft Windows 95/98.Microsoft Windows NT.Linux.Solaris.SunOS.HP-UX.Digi ...

  3. The builder launch configuration could not be found

    Export Wizard Error      Errors occurred during the build Problems occured when invoking code from p ...

  4. js获取当前url地址及参数

    介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...

  5. vim 设定一个新的snippets

    目录 ~/.vim/bundle/vim-snippets/snippets 编辑 _.snippets 在最后添加一行自己需要的,比如想要个自己的名字,然后按tab,即出现全称,可以添加如下内容 s ...

  6. JavaBean学习--练习示例

    初识Javabean,没感觉这鸟东西有什么好用的,一定是我太笨了 自己用jsp测试了下,这里用application作用域做个示例 <%@ page language="java&qu ...

  7. display:inline、block、inline-block的区别(转)

    display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...

  8. 谷歌插件postman如果不能用,就用git命令发送post请求

     curl -X POST --data '{"name":"zfpx"}' -H 'Content-Type:application/json' http:/ ...

  9. web.xml配置详解 (及<context-param>配置作用 )

    http://blog.csdn.net/guihaijinfen/article/details/8363839 <context-param>配置作用 http://blog.csdn ...

  10. OC - 21.CALayer核心要点及实例解析

    CALayer基础 CALayer是每一个UI控件的核心,一个UI控件之所以能显示可以说是CALayer的功劳 每一个UI控件默认都为自己创建一个CALayer对象,通过drawRect方法将内容绘制 ...