协议分析器分析 HTTP C++ 编写过程的代码
时间: 2024-02-29 20:53:31 浏览: 27
好的,以下是一个简单的HTTP协议分析器的C++代码,它可以解析HTTP请求和响应报文的头部信息并将其打印到控制台上:
```c++
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
#define BUFFER_SIZE 65536
struct http_header
{
string method; // HTTP方法
string uri; // URI
string http_version; // HTTP版本
map<string, string> headers; // 头部字段
};
bool parse_http_request(const char* buffer, ssize_t packet_size, http_header& header)
{
const char* end_of_line = strstr(buffer, "\r\n");
if(end_of_line == NULL)
{
return false;
}
const char* end_of_request_line = strstr(buffer, " ");
if(end_of_request_line == NULL)
{
return false;
}
header.method = string(buffer, end_of_request_line - buffer);
buffer = end_of_request_line + 1;
end_of_request_line = strstr(buffer, " ");
if(end_of_request_line == NULL)
{
return false;
}
header.uri = string(buffer, end_of_request_line - buffer);
buffer = end_of_request_line + 1;
header.http_version = string(buffer, end_of_line - buffer);
buffer = end_of_line + 2;
while(buffer - packet_size < end_of_line)
{
end_of_line = strstr(buffer, "\r\n");
if(end_of_line == NULL)
{
return false;
}
const char* end_of_header_name = strstr(buffer, ":");
if(end_of_header_name == NULL)
{
return false;
}
string header_name(buffer, end_of_header_name - buffer);
buffer = end_of_header_name + 2;
string header_value(buffer, end_of_line - buffer);
buffer = end_of_line + 2;
header.headers[header_name] = header_value;
}
return true;
}
bool parse_http_response(const char* buffer, ssize_t packet_size, http_header& header)
{
const char* end_of_line = strstr(buffer, "\r\n");
if(end_of_line == NULL)
{
return false;
}
const char* end_of_status_line = strstr(buffer, " ");
if(end_of_status_line == NULL)
{
return false;
}
buffer = end_of_status_line + 1;
end_of_status_line = strstr(buffer, " ");
if(end_of_status_line == NULL)
{
return false;
}
header.http_version = string(buffer, end_of_status_line - buffer);
buffer = end_of_status_line + 1;
header.uri = string(buffer, end_of_line - buffer);
buffer = end_of_line + 2;
while(buffer - packet_size < end_of_line)
{
end_of_line = strstr(buffer, "\r\n");
if(end_of_line == NULL)
{
return false;
}
const char* end_of_header_name = strstr(buffer, ":");
if(end_of_header_name == NULL)
{
return false;
}
string header_name(buffer, end_of_header_name - buffer);
buffer = end_of_header_name + 2;
string header_value(buffer, end_of_line - buffer);
buffer = end_of_line + 2;
header.headers[header_name] = header_value;
}
return true;
}
int main()
{
int sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if(sock_raw < 0)
{
cout << "Error creating raw socket: " << strerror(errno) << endl;
return 1;
}
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
while(true)
{
struct sockaddr_in src_addr;
socklen_t src_addr_len = sizeof(src_addr);
ssize_t packet_size = recvfrom(sock_raw , buffer , BUFFER_SIZE , 0 , (struct sockaddr*)&src_addr , &src_addr_len);
if(packet_size < 0)
{
cout << "Error receiving packet: " << strerror(errno) << endl;
return 1;
}
struct iphdr* ip_header = (struct iphdr*)buffer;
if(ip_header->protocol != IPPROTO_TCP)
{
cout << "Error: Not a TCP packet!" << endl;
continue;
}
struct tcphdr* tcp_header = (struct tcphdr*)(buffer + ip_header->ihl*4);
if(tcp_header->dest != htons(80) && tcp_header->source != htons(80))
{
cout << "Error: Not an HTTP packet!" << endl;
continue;
}
const char* payload = buffer + ip_header->ihl*4 + tcp_header->doff*4;
ssize_t payload_size = packet_size - ip_header->ihl*4 - tcp_header->doff*4;
http_header header;
if(tcp_header->dest == htons(80) && parse_http_request(payload, payload_size, header))
{
cout << "HTTP Request:" << endl;
cout << "Method: " << header.method << endl;
cout << "URI: " << header.uri << endl;
cout << "HTTP Version: " << header.http_version << endl;
for(auto& field : header.headers)
{
cout << field.first << ": " << field.second << endl;
}
}
else if(tcp_header->source == htons(80) && parse_http_response(payload, payload_size, header))
{
cout << "HTTP Response:" << endl;
cout << "URI: " << header.uri << endl;
cout << "HTTP Version: " << header.http_version << endl;
for(auto& field : header.headers)
{
cout << field.first << ": " << field.second << endl;
}
}
}
close(sock_raw);
return 0;
}
```
这个程序创建了一个原始套接字,并使用recvfrom函数从网络上接收数据包。然后,它根据TCP端口号过滤出HTTP请求和响应报文,并使用parse_http_request和parse_http_response函数解析HTTP头部信息,并将其打印到控制台上。需要注意的是,为了解析HTTP头部,我们使用了一个名为http_header的结构体,并使用map来存储头部字段。另外,我们还使用了strstr函数来寻找特定的字符串,并使用string构造函数来构造字符串对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)