一个简单的wed服务器SHTTPD(1)————命令行和文件配置解析
开始学习《LInux网络编程》中的综合案例,虽然代码书上有,还是自己打一下加深理解和印象。
主要有两个函数,完成命令行的解析,另一个实现配置文件的解析,注释还是比较丰富的哦。
//start from the very beginning,and to create greatness
//@author: Chuangwei Lin
//@E-mail:979951191@qq.com
//@brief: 命令行解析代码和配置文件解析的实现
#include "lcw_shttpd.h"
//短选项的配置为c:d:f:h:o:l:m:t:
static char* shortopts="c:d:f:h:o:l:m:t:";
//长选项的配置
//option是getop_long的一个结构体参数p531
static struct option longopts[]=
{
{"CGIRoot",required_argument,NULL,'c'},
{"ConfigFile",required_argument,NULL,'f'},
{"DefaultFile",required_argument,NULL,'d'},
{"DocumentRoot",required_argument,NULL,'o'},
{"ListenPort",required_argument,NULL,'l'},
{"MaxClient",required_argument,NULL,'m'},
{"TimeOut",required_argument,NULL,'t'},
{"Help",no_argument,NULL,'h'},
{0,0,0,0},
}
//初始化时服务器的默认配置
extern struct conf_opts conf_para=
{
"/usr/local/var/www/cgi-bin/",//CGI根目录
"index.html",//默认文件名称
"/usr/local/var/www/",//根文件目录
"/etc/SHTTPD.conf",//配置文件路径和名称
8080, //监听端口
4, //最大客户端数量
3,//超时时间
2//初始化线程数量
};
/******************************************************
函数名:display_usage(void)
参数:无
功能:显示参数输入方法
*******************************************************/
void display_usage(void)
{
printf("*******************Chuangwei Lin*******************\n");
printf("sHTTPD -l number -m number -o path -c path -d filename -t seconds -o filename\n");
printf("sHTTPD --ListenPort number\n");
printf(" --MaxClient number\n");
printf(" --DocumentRoot) path\n");
printf(" --DefaultFile) filename\n");
printf(" --CGIRoot path \n");
printf(" --DefaultFile filename\n");
printf(" --TimeOut seconds\n");
printf(" --ConfigFile filename\n");
}
/******************************************************
函数名:conf_readline(int fd, char *buff, int len)
参数:文件描述符,缓冲区,长度
功能:读取配置文件的一行
*******************************************************/
static int conf_readline(int fd, char *buff, int len)
{
int n = -1;
int i = 0;
int begin = 0;
memset(buff, 0, len);//清缓冲区
for(i =0; i<len;begin?i++:i)//当开头部分不为'\r'或者'\n'时i计数
{ //begin真则i++
n = read(fd, buff+i, 1);//读一个字符
if(n == 0)//文件末尾
{
*(buff+i) = '\0';
break;
}
else if(*(buff+i) == '\r' ||*(buff+i) == '\n')
{//是回车换行
if(begin)
{//为一行
*(buff+i) = '\0';
break;
}
}
else
{
begin = 1;
}
}
return i;
}
static char* l_opt_arg;//存输入参数
/******************************************************
函数名:Para_CmdParse(int argc,char* argv[])
参数:argc:参数个数 ,argv:参数的字符串数组,两个参数一般是从main()函数的输入参数中直接传来
功能:命令行解析函数,利用getopt_long函数实现
*******************************************************/
static int Para_CmdParse(int argc,char* argv[])
{
int c;
int len;
int value;
//遍历输入参数,设置配置参数
while((c=getopt_long(argc,argv,shortopts,longopts,NULL))!=-1)
{
switch(c)
{ //getopt_long()如果有输入参数,则输入参数为optarg
case:'c'//CGI跟路径
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
memcpy(conf_para.CGIRoot,l_opt_arg,len+1);//更新
}
break;
case:'d'//默认文件名称
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
memcpy(conf_para.DefaultFile,l_opt_arg,len+1);
}
break;
case:'f'//配置文件名称和路径
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
memcpy(conf_para.ConfigFile,l_opt_arg,len+1);
}
break;
case:'o'//根文件路径
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
memcpy(conf_para.DocumentRoot,l_opt_arg,len+1);
}
break;
case:'l'//侦听端口
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
value = strtol(l_opt_arg,NULL,10);//转化字符串为整形
if (value != LONG_MAX && value != LONG_MIN)
{
conf_para.ListenPort = value;//更新
}
}
break;
case:'m'//最大客户端数量
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
value = strtol(l_opt_arg,NULL,10);//转化字符串为整形
if (value != LONG_MAX && value != LONG_MIN)
{
conf_para.MaxClient = value;//更新
}
}
break;
case:'t'//超时时间
l_opt_arg = optarg;
if (l_opt_arg && l_opt_arg[0] != ':')
{
len = strlen(l_opt_arg);
value = strtol(l_opt_arg,NULL,10);//转化字符串为整形
if (value != LONG_MAX && value != LONG_MIN)
{
conf_para.TimeOut = value;//更新
}
}
break;
case:'?'
printf("Invalid para \n");
case:'h'
display_usage();
break;
}
}
}
/******************************************************
函数名:Para_FileParse(char* file)
参数:文件
功能:文件配置解析函数
*******************************************************/
void Para_FileParse(char* file)
{
#define LINELENGTH 256
char line[LINELENGTH];//读取缓冲区
char *name = NULL,*value = NULL;//用于获取关键字和值
int fd = -1;//文件描述符
int n = 0;
fd = open(file,O_RDONLY);//只读方式打开配置文件
if (-1 == fd)//错误检查
{
goto EXITPara_FileParse;//退出
}
//命令格式如下
//[#注释|[空格]关键字[空格]=[空格]value]
//
while((n = conf_readline(fd,line,LINELENGTH))!= 0)//每次读取一行
{
char *pos = line;//文件位置指针
while(isspace(*pos))
{
pos++;//跳过一行开头部分的空格
}
if(*pos == '#')//如果是注释
{
continue;//那就读取下一行
}
name = pos;//此时的位置就是关键字的开头
while(!isspace(*pos) && *pos != '=')//不是空格也不是’=‘,则继续读直到读完关键字
{
pos++;
}
*pos = '\0';//得到关键字
while(isspace(*pos))//再次跳过值前面的空格
{
pos++;
}
value = pos;
while(!isspace(*pos) && *pos != '\r' && *pos != '\n')//读到结束
{
pos++;
}
pos = '\0';//得到值
//根据关键字,将值赋给配置文件的结构
int ivalue;
if(strncmp("CGIRoot",name,7))
{
memcpy(conf_para.CGIRoot,value,strlen(value)+1);
}
else if(strncmp("DefaultFile",name,11))
{
memcpy(conf_para.DefaultFile,value,strlen(value)+1);
}
else if(strncmp("DocumentRoot",name,12))
{
memcpy(conf_para.DocumentRoot,value,strlen(value)+1);
}
else if(strncmp("ListenPort",name,10))
{
ivalue = strtol(value,NULL,10);//转化字符串为整形
conf_para.ListenPort = ivalue;
}
else if(strncmp("MaxClient",name,9))
{
ivalue = strtol(value,NULL,10);//转化字符串为整形
conf_para.MaxClient = ivalue;
}
else if(strncmp("TimeOut",name,7))
{
ivalue = strtol(value,NULL,10);//转化字符串为整形
conf_para.TimeOut = ivalue;
}
}
close(fd);//关闭文件
EXITPara_FileParse:
return ;
}
/******************************************************
函数名:display_para()
参数:无
功能:显示配置的参数
*******************************************************/
static void display_para()
{
printf("*******************Chuangwei Lin*******************\n");
printf("sHTTPD ListenPort: %d\n",conf_para.ListenPort);
printf(" MaxClient: %d\n", conf_para.MaxClient);
printf(" DocumentRoot: %s\n",conf_para.DocumentRoot);
printf(" DefaultFile:%s\n",conf_para.DefaultFile);
printf(" CGIRoot:%s \n",conf_para.CGIRoot);
printf(" DefaultFile:%s\n",conf_para.DefaultFile);
printf(" TimeOut:%d\n",conf_para.TimeOut);
printf(" ConfigFile:%s\n",conf_para.ConfigFile);
}
/******************************************************
函数名:Para_Init(int argc, char *argv[])
参数:参数个数,和参数字符串
功能:初始化配置
*******************************************************/
void Para_Init(int argc, char *argv[])
{
//解析命令行输入参数
Para_CmdParse(argc, argv);
//解析配置文件配置参数
if(strlen(conf_para.ConfigFile))
{
Para_FileParse(conf_para.ConfigFile);
}
display_para();
return ;//返回配置参数
}
一个简单的wed服务器SHTTPD(1)————命令行和文件配置解析的更多相关文章
- 一个简单的wed服务器SHTTPD(5)————服务器SHTTPD请求方法解析
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- 一个简单的wed服务器SHTTPD(9)————main函数文件,Makefile,头文件
主函数: #include "lcw_shttpd.h" //初始化时服务器的默认配置 extern struct conf_opts conf_para= { "/us ...
- 一个简单的wed服务器SHTTPD(4)————SHTTPD支持CGI的实现
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- 一个简单的wed服务器SHTTPD(3)————SHTTPD多客户端支持的实现
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- 一个简单的wed服务器SHTTPD(6)———— SHTTPD错误处理的实现
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- 一个简单的wed服务器SHTTPD(8)———— URI分析
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- 一个简单的wed服务器SHTTPD(7)———— SHTTPD内容类型的实现
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- 一个简单的wed服务器SHTTPD(2)———— 客户端请求分析
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- Tomcat剖析(一):一个简单的Web服务器
Tomcat剖析(一):一个简单的Web服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器 ...
随机推荐
- C语言学生管理系统完善版
#include<stdio.h>#include<string.h>#include <stdlib.h>#define M 100struct score ...
- Python高级特性-迭代器和生成器
迭代器 Python中可迭代对象(iterable)通俗指可直接作用与For循环的数据对象,如Python中的集合数据类型,字符串(str),列表(list),元组(tuple),集合(set),字典 ...
- Python Requests-学习笔记(5)-响应状态码
我们可以检测响应状态码: r = requests.get('http://httpbin.org/get') r.status_code 为方便引用,Requests还附带了一个内置的状态码查询对象 ...
- 树状数组模板--Color the ball
Color the ball HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电 ...
- matlab创建HDF5文件
一.例子 1.创建写入 testdata = uint8(magic(5)); h5create('my_example.h5','/dataset1',size(testdata)); %创建 h5 ...
- JAVA—SQL注入
之前看到的一道java面试题,Statement与PreparedStatement的区别,什么是SQL注入,如何防止SQL注入 前面部分比较好回答 1.PreparedStatement支持动态设置 ...
- niuke --abc
链接:https://ac.nowcoder.com/acm/contest/1083/A来源:牛客网 给出一个字符串s,你需要做的是统计s中子串”abc”的个数.子串的定义就是存在任意下标a< ...
- calculator.py
代码如下: #计算器类 class Count: def __init__(self, a, b): self.a = int(a) self.b = int(b) #计算器加法 def add(se ...
- [php代码审计]bluecms v1.6 sp1
一.环境搭建 bluecms v1.6 sp1源码 windows 7 phpstudy2016(php 5.4.45) seay源代码审计系统 源码在网上很容易下载,很多教程说访问地址 http:/ ...
- SpringCloud(四)学习笔记之Feign
Feign是一个声明式的Web服务客户端,可帮助我们更加便捷.优雅地调用HTTP API Feign可以与Eureka和Ribbon组合使用以支持负载均衡 一.构建Eureka Server [基于第 ...