diff --git a/tinyweb.c b/tinyweb.c index 17bbfa0..e889906 100644 --- a/tinyweb.c +++ b/tinyweb.c @@ -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); } @@ -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) @@ -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" @@ -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 diff --git a/tinyweb.h b/tinyweb.h index 487f1d2..b7c61d3 100644 --- a/tinyweb.h +++ b/tinyweb.h @@ -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 diff --git a/tools.c b/tools.c index 0cda029..4e49a41 100644 --- a/tools.c +++ b/tools.c @@ -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; @@ -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; @@ -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" diff --git a/tools.h b/tools.h index 4c55376..aa8ece1 100644 --- a/tools.h +++ b/tools.h @@ -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 @@ -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);