在C中执行HTTP标头检查
我想在C中执行HTTP标头检查,这是我使用的代码来执行相同的代码。但是不幸的是,我没有得到所需的输出。 检查的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
const char *payload="GET / HTTP/1.1\r\n"
"Host: 192.168.43.135:12345\r\n"
"Connection: Upgrade\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"Upgrade: websocket\r\n"
"Origin: file://\r\n"
"Sec-WebSocket-Version: 13\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\r\n"
"Accept-Encoding: gzip, deflate, sdch\r\n"
"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
"Sec-WebSocket-Key: bKdPyn3u98cTfZJSh4TNeQ==\r\n"
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"\r\n"
"[Full request URI: http://192.168.43.135:12345/]"
"[HTTP request 1/1]"
"[Response in frame: 6]";
const char *token;
token = strtok(payload, "\r\n");
while (token)
{
if(strcmp(token,"GET / HTTP/1.1")==0)
{
printf("Current token: %s\n", token);
printf("Correct Version\n");
}
else if(strcmp(token,"Connection: Upgrade")==0)
{
printf("Current token: %s\n", token);
printf("Connection upgraded\n");
}
else if(strcmp(token,"Sec-WebSocket-Version: 13")==0)
{
printf("Current token: %s\n", token);
printf("Correct version\n");
}
token = strtok(NULL, "\n");
}
return 0;
}
这是我用来实现我尝试过的 。 sstatic.net/eleda.png“ rel =“ nofollow noreferrer”>使用strtok()时显示的输出。所需的输出应为
Current token: GET / HTTP/1.1
Correct version
Current token: Connection: Upgrade
Connection upgraded
Current token: Sec-WebSocket-Version: 13
Correct version
I want to perform HTTP header checks in C, here is the code that I am using to do the same. But I am unfortunately not getting the desired output. This is the code that I have used to implement the checks
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
const char *payload="GET / HTTP/1.1\r\n"
"Host: 192.168.43.135:12345\r\n"
"Connection: Upgrade\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"Upgrade: websocket\r\n"
"Origin: file://\r\n"
"Sec-WebSocket-Version: 13\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\r\n"
"Accept-Encoding: gzip, deflate, sdch\r\n"
"Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
"Sec-WebSocket-Key: bKdPyn3u98cTfZJSh4TNeQ==\r\n"
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"\r\n"
"[Full request URI: http://192.168.43.135:12345/]"
"[HTTP request 1/1]"
"[Response in frame: 6]";
const char *token;
token = strtok(payload, "\r\n");
while (token)
{
if(strcmp(token,"GET / HTTP/1.1")==0)
{
printf("Current token: %s\n", token);
printf("Correct Version\n");
}
else if(strcmp(token,"Connection: Upgrade")==0)
{
printf("Current token: %s\n", token);
printf("Connection upgraded\n");
}
else if(strcmp(token,"Sec-WebSocket-Version: 13")==0)
{
printf("Current token: %s\n", token);
printf("Correct version\n");
}
token = strtok(NULL, "\n");
}
return 0;
}
I have tried both with both strtok()
and strstr()
and I am getting the incorrect outputs in both the cases.The output shown while using strstr() The output shown while using strtok(). The output required should be
Current token: GET / HTTP/1.1
Correct version
Current token: Connection: Upgrade
Connection upgraded
Current token: Sec-WebSocket-Version: 13
Correct version
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论