xinetd网络(2) 协议头解析
1:在/etc/xinetd.d下添加配置文件xhttpd
service xhttpd
{
socket_type = stream //每行“=”前后各最多只能有一个空格
protocol= tcp
wait = no
user =nobody
server =/home/username/xhttpd/xhttpd //在xhttpd目录下有个xhttpd可执行文件
server_args = /home/username/dir //资源访问的目录
disable = no
flags = IPv4
}
2:添加监听端口
vim /etc/service
添加两行:
xhttpd 10086/tcp
xhttpd 10086/udp
3:重启xinetd服务
sudo service xinetd restart
4:在浏览器中输入访问地址:127.0.0.1:10086/hello.txt
- 然后xinetd会启动xhttpd程序,并且传入两个默认参数:
- argv[0] = xhttpd
- argv[1] = /home/username/dir
- 分析http头信息
- GET /hello.txt HTTP/1.1
5:xhttpd.c源码分析
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h> #define N 4096 void send_headers(char* type)
{
printf("HTTP/1.1 200 OK\r\n");
printf("Content-Type:%s; charset=utf-8\r\n",type);
printf("Connection:close\r\n");
printf("\r\n");
} void send_err()
{
//http protcol
//printf("HTTP/1.1 200 OK\r\n");
send_headers("text/html");
printf("<html>\r\n");
printf("<head><title>404 Can Not Find Page!</title></head>\r\n");
printf("<body>\r\n");
printf("Cann't Find Page!!!!\r\n");
printf("</body>\r\n");
printf("</html>\r\n");
exit();
} int main(int argc, char *argv[])
{
char line[N];
char method[N],path[N],protocol[N]; char *file;
struct stat sbuf;
FILE *fp;
int ich; // send_err();
// return 0; if(argc != )
{
send_err();
} if(chdir(argv[]) == -)
{
send_err();
} if(fgets(line,N,stdin) == NULL)
{
send_err();
} char headerInfo[];
strcpy(headerInfo,line); if(sscanf(line,"%[^ ] %[^ ] %[^ ]",method,path,protocol) != )
{
send_err(); //GET /hello.c HTTP/1.1
} while(fgets(line, N, stdin) != NULL)
if(strcmp(line,"\r\n"))
break; if(strcmp(method,"GET") != )
send_err(); if(path[] != '/')
send_err(); file = path+; if(stat(file,&sbuf) < )
send_err(); fp = fopen(file, "r");
if(fp == NULL)
send_err(); send_headers("text/plain");
//send_headers("audio/mpeg"); printf("method:%s\n",method);
printf("path:%s\n",path);
printf("protocol:%s\n",protocol);
printf("argv[0]:%s\n",argv[]);
printf("argv[1]:%s\n",argv[]);
printf("headerInfo:%s\n",headerInfo); while((ich = getc(fp)) != EOF)
putchar(ich); fflush(stdout);
fclose(fp);
return ;
}
xinetd网络(2) 协议头解析的更多相关文章
- [php]HTTP协议头解析
(Request-Line) GET /static/imgs/bqpd.jpg HTTP/1.1 Accept image/png, image/svg+xml, image/*;q=0.8, */ ...
- c++ 常见网络协议头
//NTP协议 typedef struct _NTP_HEADER { uint8_t _flags;//Flags 0xdb uint8_t _pcs;//Peer Clock Stratum u ...
- GJM : Unity3D 常用网络框架与实战解析 【笔记】
Unity常用网络框架与实战解析 1.Http协议 Http协议 存在TCP 之上 有时候 TLS\SSL 之上 默认端口80 https 默认端口 ...
- HTTP协议详细解析
HTTP协议详解 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. HTTP是一个基 ...
- HTTP报文头解析
HTTP报文头解析 本篇博客我们就来详细的聊一下HTTP协议的常用头部字段,当然我们将其分为请求头和响应头进行阐述.下方是报文头每个字段的格式,首先是头部字段的名称,如Accept,冒号后方紧跟的是该 ...
- 基于byte[]的HTTP协议头分析代码
smark 专注于高并发网络和大型网站架规划设计,提供.NET平台下高吞吐的网络通讯应用技术咨询和支持 基于byte[]的HTTP协议头分析代码 最近需要为组件实现一个HTTP的扩展包,所以简单地实现 ...
- C# 串口操作系列(4) -- 协议篇,文本协议数据解析
C# 串口操作系列(4) -- 协议篇,文本协议数据解析 标签: c#uiobjectstringbyte 2010-06-09 01:50 19739人阅读 评论(26) 收藏 举报 分类: 通讯 ...
- Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据
版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...
- xinetd网络
简单Web服务器 基本的HTTP协议 请求服务器数据 GET /文件或目录 HTTP/1.1 协议头部分(可选) /r/n(协议头结束) 服务器应答浏览器 HTTP/1.1 200 OK conten ...
随机推荐
- Laya播放unity特效
杭州-fun 2017/12/5 20:47:12 其实网上就有你搜下就有了现成的脚本,设置帧数和截取时间它会截屏并保存成贴图导入laya生成atlas就能用了 就是unity的截屏功能 就 ...
- HTTP redirect 重定向到 HTTPS
最近帮一个顾客做网站, 需要HTTPS. 之前接触的SSL certificate直接上传到plesk 上面勾选重定向即可. 在此先吐槽下godaddy 服务贵功能还少. 用代码从HTTP来重定向到 ...
- html5 视频播放插件
HTML5中加入了浏览器非常友好的标签 <video> ,这个标签非常的厉害,它可以不依靠falsh播放器,在网页中播放视频,目前W3C提供的video只支持mp4,ogg,webm三种视 ...
- less 引用阿里巴巴字体图标的线上地址
@import url("//at.alicdn.com/t/font_546826_wghayhobtn.css");
- vue-router 与 react-router 设计理念上的区别
vue-router 与 react-router 设计理念上的区别: 区别 vue-router react-router 改成history mode: 'history' 直接使用 react- ...
- python5-10 检查用户名
检查用户名5-10 current_users = ['Tom', 'bob', 'Alice', 'zhangsan', 'Lisi', 'John'] new_users = ['zhangsan ...
- java Collections.sort()
sort()是Collections中的静态方法,用于对List容器中的元素排序. 如容器list中存储的是Integer对象 List<Integer> list =Arrays.asL ...
- 深入理解ASP.NET MVC(3)
系列目录 URL是如何通过路由表生成的(outbound) 通常我们被推荐在view设计时使用Html.ActionLink(…)产生链接,这样做的优势就是,url可以根据路由表生成.路由机制的另一个 ...
- hadoop发行版本之间的区别
Hadoop是一个能够对大量数据进行分布式处理的软件框架. Hadoop 以一种可靠.高效.可伸缩的方式进行数据处理.Hadoop的发行版除了有Apache hadoop外cloudera,horto ...
- R3注入的四种方式
DLL注入 1.首先要获取想要注入的进程句柄(OpenProcess) 2.从要注入的进程的地址空间中分配一段内存(VirtualAllocEx) 3.往分配的内存位置写入要注入的DLL名称(Writ ...