//start from the very beginning,and to create greatness
//@author: Chuangwei Lin
//@E-mail:979951191@qq.com
//@brief: 服务器SHTTPD请求方法解析
#include "lcw_shttpd.h"
/******************************************************
函数名: Method_DoGet(struct worker_ctl *wctl)
参数:
功能:GET方法
*******************************************************/
static int Method_DoGet(struct worker_ctl *wctl)
{
DBGPRINT("LCW==>Method_DoGet\n");
struct conn_response *res = &wctl->conn.con_res;
struct conn_request *req = &wctl->conn.con_req;
char path[URI_MAX];
memset(path, 0, URI_MAX);
size_t n;
unsigned long r1, r2;
char *fmt = "%a, %d %b %Y %H:%M:%S GMT";//时间格式
//需要确定的参数
size_t status = 200;//状态值,已确定
char *msg = "OK"; //状态信息,已确定
char date[64] = "";//时间
char lm[64] = ""; //请求文件最后修改信息
char etag[64] = "";//etag信息
big_int_t cl; //内容长度
char range[64] = "";//范围
struct mine_type *mine = NULL;
time_t t = time(NULL);//取得当前时间
//根据fmt指向字符串的命令将localtime(&t)中的时间信息储存在date中
(void)strftime(date, sizeof(date),fmt,localtime(&t));
//最后修改时间
(void) strftime(lm, sizeof(lm), fmt,localtime(&res->fsate.st_mtime));
//ETAG 将可变个参数(...)按照"%lx.%lx"格式化成字符串,然后将其复制到etag中
(void) snprintf(etag, sizeof(etag), "%lx.%lx",(unsigned long)res->fsate.st_mtime,(unsigned long)res->fsate.st_size);
//发送的MIME类型
mine = Mine_Type(req->uri, strlen(req->uri), wctl);
//内容长度
cl = (big_int_t) res->fsate.st_size;
//范围range
memset(range, 0, sizeof(range));
n = -1;
if (req->ch.range.v_vec.len > 0 )//取出请求范围
{
printf("request range:%d\n",req->ch.range.v_vec.len);
//从ptr里读进数据,依照第二个参数的格式将数据写入到后面的参数里
n = sscanf(req->ch.range.v_vec.ptr,"bytes=%lu-%lu",&r1, &r2);
}
printf("n:%d\n",n);
if(n > 0)
{
status = 206;
lseek(res->fd, r1, SEEK_SET);
//n==2取前一个
cl = n == 2 ? r2 - r1 + 1: cl - r1;
(void) snprintf(range, sizeof(range),"Content-Range: bytes %lu-%lu/%lu\r\n",r1, r1 + cl - 1, (unsigned long) res->fsate.st_size);
msg = "Partial Content";
}
//构建输出的头部
memset(res->res.ptr, 0, sizeof(wctl->conn.dres));
snprintf(res->res.ptr,//缓冲区
sizeof(wctl->conn.dres),//缓冲区长度
"HTTP/1.1 %d %s\r\n" //状态和状态信息
"Date: %s\r\n"//日期
"Last-Modified: %s\r\n"//最后修改时间
"Etag: \"%s\"\r\n"//Web资源标记号
"Content-Type: %.*s\r\n"//内容类型
"Content-Length: %lu\r\n"//内容长度
//"Connection:close\r\n"
"Accept-Ranges: bytes\r\n"//发送范围
"%s\r\n",//范围起始
status,msg,date,lm,etag,strlen(mine->mime_type),mine->mime_type,cl,range);
res->cl = cl;
res->status = status;
printf("content length:%d, status:%d\n",res->cl, res->status);
DBGPRINT("LCW<==Method_DoGet\n");
return 0;
}
/******************************************************
未实现的方法
*******************************************************/
static int Method_DoPost(struct worker_ctl *wctl)
{
return 0;
}
static int Method_DoHead(struct worker_ctl *wctl)
{
Method_DoGet(wctl);
close(wctl->conn.con_res.fd);
wctl->conn.con_res.fd = -1;
return 0;
}
static int Method_DoPut(struct worker_ctl *wctl)
{
return 0;
}
static int Method_DoDelete(struct worker_ctl *wctl)
{
return 0;
}
static int Method_DoCGI(struct worker_ctl *wctl)
{
return 0;
}
static int Method_DoList(struct worker_ctl *wctl)
{
return 0;
}
/******************************************************
函数名:Method_Do(struct worker_ctl *wctl)
参数:业务和线程状态结构
功能:匹配方法
*******************************************************/
void Method_Do(struct worker_ctl *wctl)
{
DBGPRINT("LCW==>Method_Do\n");
if(0)//????什么意思 不执行?
Method_DoCGI(wctl);
switch(wctl->conn.con_req.method)//匹配请求类型
{
case METHOD_PUT://PUT方法
Method_DoPut(wctl);
break;
case METHOD_DELETE://DELETE方法
Method_DoDelete(wctl);
break;
case METHOD_GET://GET方法(这里只实现GET方法)
Method_DoGet(wctl);
break;
case METHOD_POST://POST方法
Method_DoPost(wctl);
break;
case METHOD_HEAD://HEAD方法
Method_DoHead(wctl);
break;
default:
Method_DoList(wctl);
} DBGPRINT("LCW<==Method_Do\n");
}

一个简单的wed服务器SHTTPD(5)————服务器SHTTPD请求方法解析的更多相关文章

  1. 自己动手写一个简单的(IIS)小型服务器

    因为第一次在博客园发表随笔,不太会用,这个笔记是我之前在印象笔记中写好的,然后直接copy过来,有兴趣自己做一个IIS服务器的小伙伴们可以参照下面的流程做一次,也可以叫我要源代码,不过要做完,我觉得花 ...

  2. 一个简单的wed服务器SHTTPD(9)————main函数文件,Makefile,头文件

    主函数: #include "lcw_shttpd.h" //初始化时服务器的默认配置 extern struct conf_opts conf_para= { "/us ...

  3. 一个简单的wed服务器SHTTPD(1)————命令行和文件配置解析

    开始学习<LInux网络编程>中的综合案例,虽然代码书上有,还是自己打一下加深理解和印象. 主要有两个函数,完成命令行的解析,另一个实现配置文件的解析,注释还是比较丰富的哦. //star ...

  4. 一个简单的wed服务器SHTTPD(6)———— SHTTPD错误处理的实现

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

  5. 一个简单的wed服务器SHTTPD(4)————SHTTPD支持CGI的实现

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

  6. 一个简单的wed服务器SHTTPD(3)————SHTTPD多客户端支持的实现

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

  7. 一个简单的wed服务器SHTTPD(8)———— URI分析

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

  8. 一个简单的wed服务器SHTTPD(7)———— SHTTPD内容类型的实现

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

  9. 一个简单的wed服务器SHTTPD(2)———— 客户端请求分析

    //start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...

随机推荐

  1. Kitty-Cloud服务搭建过程剖析

    项目地址 https://github.com/yinjihuan/kitty-cloud 服务搭建 大家目前看到的都是我已经搭建好了的服务,如果让你从零开始自己搭建一个微服务的项目,要怎么做? 我们 ...

  2. 配置spark历史服务(spark二)

    1. 编辑spark-defaults.conf位置文件 添加spark.eventLog.enabled和spark.eventLog.dir的配置修改spark.eventLog.dir为我们之前 ...

  3. search(4)- elastic4s-ElasticDsl

    上次分析了一下elastic4s的运算框架.本来计划接着开始实质的函数调用示范,不过看过了Elastic4s的所有使用说明文档后感觉还是走的快了一点.主要原因是elasticsearch在7.0后有了 ...

  4. Anaconda下的juputer notebook 更改起始目录的方法【填坑】

    出来的结果是这样的,我们很不习惯,找文件.保存文件很麻烦 这里的快捷方式可以打开 jupyter notebook ,但是如果你没配置环境变量的话,在cmd 中 输入命令 jupyter notebo ...

  5. 视频图文教学 - 用最快的速度把 DotNet Core Blazor 程序安装到 树莓派中 并且用网页控制 GPIO 闪灯

    前言 dotnet core 在3.0时代已经发展得很好. 尤其是在跨平台方面更已经是达到了很实用的阶段. 作为 dotnet 程序员, 应该对 Linux 有充分的了解, 也可以在业余时间玩玩硬件, ...

  6. Daily Scrum 1/5/2015

    Process: Zhaoyang: Fix some crash bugs and increase the program stability. Yangdong: Complete some b ...

  7. stand up meeting 1/8/2016 & weekend 1/9/2016~1/10/2016 && sprint2扫尾

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云 跑通打印机功能,尝试与pdf读取部分结合     6 查漏补缺, ...

  8. mybatis配置的逻辑删除不好使了

    在使用mybatisplus中,可使用逻辑删除.案例中,使用mybatisplus逆向生成model,使用delete_status为识别逻辑删除字段. springboot 中配置启动逻辑删除 my ...

  9. 网站假死 重启NGINX无效 必须重启PHP 原因分析

    一.错误提示说明: Nginx 502 Bad Gateway:请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止. Nginx 504 G ...

  10. LeetCode#160-Intersection of Two Linked Lists-相交链表

    一.题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], l ...