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 7d3b77a commit 8277ef6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 56 deletions.
11 changes: 6 additions & 5 deletions Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ char on_socket_data(void* data, uv_stream_t* client, tw_peerAddr* pa, membuf_t*

free(gb);


len = buf->size;
l = len;
uc = enc_u82u(buf->data, &l);
Expand All @@ -104,11 +103,13 @@ char on_socket_data(void* data, uv_stream_t* client, tw_peerAddr* pa, membuf_t*
printf("-------------------------------------------ws1:len=%zd\n%s\n-------------------------------------------\n", len, gb);
free(uc);
free(gb);
}else
//linux 下,系统和源代码文件编码都是是utf8的,就不需要转换
#endif // _MSC_VER
}
else {
//linux 下,系统和源代码文件编码都是是utf8的,就不需要转换
printf("-------------------------------------------ws:len=%zd\n%s\n-------------------------------------------\n", buf->size, buf->data);
size_t len = buf->size;
}
#endif // _MSC_VER
ulong len = buf->size;
char* p = WebSocketMakeFrame(buf->data, &len, 1);//文本帧
tw_send_data(client, p, len, 0, 1);
} else { //Socket
Expand Down
92 changes: 45 additions & 47 deletions tinyweb.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ void tw_send_data(uv_stream_t* client, const void* data, size_t len, char need_c

//获取头部 SetCookie 字段值
//setCookie: 缓存区(至少 110+strlen(domain)=strlen(path) ),外部传入
//expires: 多少秒后过期
//domain: Domain, 可以是 heads->host,外部传入
//path: Path, 可以是 heads->path,外部传入
void tw_make_cookie(char* set_cookie,int expires,char* domain,char* path)
{
char val[30];
char szDate[30];
getGmtTime(szDate);
getGmtTime(szDate,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"
Expand Down Expand Up @@ -166,7 +167,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);
getGmtTime(szDate,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 @@ -198,7 +199,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);
getGmtTime(szDate,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 @@ -249,7 +250,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);
getGmtTime(szDate,0);
respone = (char*)malloc(300 + 1);
int respone_size;
if (heads->Range_frm == 0) //200 OK
Expand Down Expand Up @@ -476,28 +477,25 @@ static char* tw_get_http_heads(const uv_buf_t* buf, tw_reqHeads* heads) {
while (isspace(*heads->path)) heads->path++;
start = strchr(heads->path, ' ');
if (start) *start = 0;
//
url_decode(heads->path);
//url含有转义编码字符
if (strstr(heads->path, "%") != 0)
{
url_decode(heads->path);
#ifdef _MSC_VER //Windows下需要转换编码,因为windows系统的编码是GB2312
size_t len = strlen(heads->path);
char *gb = U82GB(heads->path, &(unsigned int)len);
strncpy(heads->path, gb, len);
heads->path[len] = 0;
free(gb);
//linux 下,系统和源代码文件编码都是是utf8的,就不需要转换
size_t len = strlen(heads->path);
char *gb = U82GB(heads->path, &(unsigned int)len);
strncpy(heads->path, gb, len);
heads->path[len] = 0;
free(gb);
//linux 下,系统和源代码文件编码都是是utf8的,就不需要转换
#endif // _MSC_VER
//query param
if (1 == heads->method) { //GET
p = strchr(heads->path, '?');
if (p) {
heads->query = p + 1;
*p = 0;
}
}
else { //POST
heads->query = heads->data;
//query param
p = strchr(heads->path, '?');
if (p) {
heads->query = p + 1;
*p = 0;
}
////------------尽可能的合并 "../" "/./"
//确保开头为'/'
if (*heads->path != '/') {
heads->path--;
Expand All @@ -510,31 +508,34 @@ static char* tw_get_http_heads(const uv_buf_t* buf, tw_reqHeads* heads) {
*(p + 1) = '/';
*(p + 2) = 0;
}
//去掉"/./"
while ((p = strstr(heads->path, "/./"))) {
memmove(p, p + 2, strlen(p + 2) + 1);
}
//尽可能的合并"../"
while ((p = strstr(heads->path, "/.."))) {//存在 ..
if ((p - heads->path) <= 1) {
if ((start = strchr(heads->path + 2, '/')))
heads->path = start;
////------------尽可能的合并 "../" "/./"
if (strstr(heads->path, "./") != 0)
{
//去掉"/./"
while ((p = strstr(heads->path, "/./"))) {
memmove(p, p + 2, strlen(p + 2) + 1);
}
//尽可能的合并"../"
while ((p = strstr(heads->path, "/.."))) {//存在 ..
if ((p - heads->path) <= 1) {
if ((start = strchr(heads->path + 2, '/')))
heads->path = start;
else
*p = 0;
continue;
}
*(p - 1) = 0;
start = strrchr(heads->path, '/');
if (start == NULL)
start = heads->path;
key = strchr(p + 2, '/');
if (key)
p = key;
else
*p = 0;
continue;
break;
memmove(start, p, strlen(p) + 1);
}
*(p - 1) = 0;
start = strrchr(heads->path, '/');
if (start == NULL)
start = heads->path;
key = strchr(p + 2, '/');
if (key)
p = key;
else
break;
memmove(start, p, strlen(p) + 1);
}
////------------end 尽可能的合并 "../" "/./"

key = NULL;
//从第二行开始循环处理 头部
Expand All @@ -546,13 +547,11 @@ static char* tw_get_http_heads(const uv_buf_t* buf, tw_reqHeads* heads) {
if (key = strstr(head, "Sec-WebSocket-Key: "))
{
key += 19;
while (isspace(*key)) key++;
}
//search host
else if (heads->host = strstr(head, "Host: "))
{
heads->host += 6;
while (isspace(*heads->host)) heads->host++;
}
//Range: bytes=sizeFrom-[sizeTo] (sizeTo 可能没有,或不正确,表示整个文件大小)
// (sizeFrom 为负数,表示从文件末尾反过来的位置,即fileSize-sizeFrom)
Expand Down Expand Up @@ -581,7 +580,6 @@ static char* tw_get_http_heads(const uv_buf_t* buf, tw_reqHeads* heads) {
else if (heads->cookie = strstr(head, "Cookie: "))
{
heads->cookie += 8;
while (isspace(*heads->cookie)) heads->cookie++;
}
//下一行 头部
head = strtok(NULL, delims);
Expand Down
2 changes: 1 addition & 1 deletion tinyweb.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void tinyweb_stop(uv_loop_t* loop);

//获取头部 SetCookie 字段值
//set_cookie: 缓存区(至少 110+strlen(domain)=strlen(path) ),外部传入
//expires: 过期时间,秒
//expires: 多少秒后过期
//domain: Domain, 可以是 heads->host,外部传入
//path: Path, 可以是 heads->path,外部传入
void tw_make_cookie(char* set_cookie, int expires, char* domain, char* path);
Expand Down
6 changes: 4 additions & 2 deletions tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ char* listDir(const char* fullpath, const char* reqPath)
snprintf(szFind, 255, "%s\\*", fullpath);
hFind = _findfirsti64(szFind, &fdt);
//[name:"file1.txt",mtime:"2016-11-28 16:25:46",size:123],\r\n
while (hFind != INVALID_HANDLE_VALUE)//一次查找循环
while (hFind != -1)//一次查找循环
{
//最后修改时间
struct tm *t = localtime(&fdt.time_write);//年月日 时分秒
Expand Down Expand Up @@ -1462,11 +1462,13 @@ inline int day_of_year(int y, int m, int d)

//获取格林制(GMT)时间: "Wed, 18 Jul 2018 06:02:42 GMT"
//szDate: 存放GMT时间的缓存区(至少 char[30]),外部传入
void getGmtTime(char* szDate)
//addSecond: 当前时间加上多少秒
void getGmtTime(char* szDate,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);
}
Expand Down
3 changes: 2 additions & 1 deletion tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ extern "C" {

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

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

0 comments on commit 8277ef6

Please sign in to comment.