Skip to content

Commit

Permalink
11.支持cookie/setcookie
Browse files Browse the repository at this point in the history
12.支持添加自定义头部信息
  • Loading branch information
lzpong committed Jul 18, 2018
1 parent 7d72e41 commit 868446b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
13 changes: 7 additions & 6 deletions tinyweb.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,18 @@ void tw_send_data(uv_stream_t* client, const void* data, size_t len, char need_c

//获取头部 SetCookie 字段值
//setCookie: 缓存区(至少 110+strlen(domain)=strlen(path) ),外部传入
//ckLen: set_cookie的长度
//expires: 多少秒后过期
//domain: Domain, 可以是 heads->host,外部传入
//path: Path, 可以是 heads->path,外部传入
void tw_make_cookie(char* set_cookie,int expires,char* domain,char* path)
void tw_make_cookie(char* set_cookie,int ckLen,int expires,char* domain,char* path)
{
char val[30];
char szDate[30];
getGmtTime(szDate,expires);
getGmtTime(szDate,30,expires);
path == NULL ? path = "/" : 0;
snprintf(val, 100, "Tiny%lld", str2stmp(NULL));
snprintf(set_cookie, sizeof(set_cookie), "SetCookie: TINY_SSID=%s; Expires=%s; Path=%s; Domain=%s;\r\n"
snprintf(set_cookie, ckLen, "SetCookie: TINY_SSID=%s; Expires=%s; Path=%s; Domain=%s;\r\n"
, val, szDate, path, domain);
}

Expand Down Expand Up @@ -167,7 +168,7 @@ char* tw_format_http_respone(uv_stream_t* client, const char* status, const char
size_t totalsize, header_size;
char* respone;
char szDate[30];
getGmtTime(szDate,0);
getGmtTime(szDate,30,0);
ext_heads == NULL ? ext_heads = "" : 0;
tw_config* tw_conf = (tw_config*)(client->loop->data);
if (content_length == 0 || content_length == (size_t)-1)
Expand Down Expand Up @@ -199,7 +200,7 @@ static void tw_301_Moved(uv_stream_t* client, tw_reqHeads* heads, const char* ex
size_t len = 76 + strlen(heads->path);
char buffer[10245];
char szDate[30];
getGmtTime(szDate,0);
getGmtTime(szDate,30,0);
tw_config* tw_conf = (tw_config*)(client->loop->data);
snprintf(buffer, sizeof(buffer), "HTTP/1.1 301 Moved Permanently\r\nDate: %s\r\n"
"Server: TinyWeb\r\nLocation: http://%s%s/%s%s\r\nConnection: close\r\n"
Expand Down Expand Up @@ -250,7 +251,7 @@ static char tw_http_send_file(uv_stream_t* client, const char* content_type, con
tw_404_not_found(client, heads->path, ext_heads);
return 0;
}
getGmtTime(szDate,0);
getGmtTime(szDate,30,0);
respone = (char*)malloc(300 + 1);
int respone_size;
if (heads->Range_frm == 0) //200 OK
Expand Down
3 changes: 2 additions & 1 deletion tinyweb.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ void tinyweb_stop(uv_loop_t* loop);

//获取头部 SetCookie 字段值
//set_cookie: 缓存区(至少 110+strlen(domain)=strlen(path) ),外部传入
//ckLen: set_cookie的长度
//expires: 多少秒后过期
//domain: Domain, 可以是 heads->host,外部传入
//path: Path, 可以是 heads->path,外部传入
void tw_make_cookie(char* set_cookie, int expires, char* domain, char* path);
void tw_make_cookie(char* set_cookie, int ckLen, int expires, char* domain, char* path);

//处理客户端请求
//invoked by tinyweb when GET request comes in
Expand Down
10 changes: 6 additions & 4 deletions tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ char isDir(const char* path)
return (fd.name[0] && (fd.attrib & FILE_ATTRIBUTE_DIRECTORY));
}

//列表目录
//返回列表目录Json字符串,need free the return
char* listDir(const char* fullpath, const char* reqPath)
{
int fnum = 0;
Expand Down Expand Up @@ -375,7 +375,7 @@ char isFile(const char* path)
return 0;
}

//列表目录
//返回列表目录Json字符串,need free the return
char* listDir(const char* fullpath, const char* reqPath)
{
int fnum = 0;
Expand Down Expand Up @@ -1462,15 +1462,17 @@ inline int day_of_year(int y, int m, int d)

//获取格林制(GMT)时间: "Wed, 18 Jul 2018 06:02:42 GMT"
//szDate: 存放GMT时间的缓存区(至少 char[30]),外部传入
//szLen: szDate的长度大小
//addSecond: 当前时间加上多少秒
void getGmtTime(char* szDate,int addSecond)
char* getGmtTime(char* szDate,int szLen,int addSecond)
{
time_t rawTime;
struct tm* timeInfo;
time(&rawTime);
rawTime += addSecond;
timeInfo = gmtime(&rawTime);
strftime(szDate, sizeof(szDate), "%a, %d %b %Y %H:%M:%S GMT", timeInfo);
strftime(szDate, szLen, "%a, %d %b %Y %H:%M:%S GMT", timeInfo);
return szDate;
}

//字符串转换成时间戳(秒),字符串格式为:"2016-08-03 06:56:36"
Expand Down
6 changes: 4 additions & 2 deletions tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ extern "C" {
//是否目录(1:是目录 0;非目录/不存在)
char isDir(const char* path);

//网页,列表目录,need free the return
//返回列表目录Json字符串,need free the return
//{"path":"/","files":[{"name":"file.txt","mtime":"2014-04-18 23:24:05","size":463,"type":"F"}]}
char* listDir(const char* fullpath, const char* reqPath);

//-----------------------------------------------------------------------------------编码转换 win/unix
Expand Down Expand Up @@ -228,8 +229,9 @@ extern "C" {

//获取格林制(GMT)时间: "Wed, 18 Jul 2018 06:02:42 GMT"
//szDate: 存放GMT时间的缓存区(至少 char[30]),外部传入
//szLen: szDate的长度大小
//addSecond: 当前时间加上多少秒
void getGmtTime(char* szDate, int addSecond);
char* getGmtTime(char* szDate, int szLen, int addSecond);

//字符串转换成时间戳(秒),字符串格式为:"2016-08-03 06:56:36"
llong str2stmp(const char *strTime);
Expand Down

0 comments on commit 868446b

Please sign in to comment.