1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-03 13:39:46 +02:00

minor fix

This commit is contained in:
Xuan Sang LE 2017-07-22 17:26:05 +02:00
parent e07e91ac9c
commit 8b7ce33944
4 changed files with 18 additions and 16 deletions

Binary file not shown.

View File

@ -38,6 +38,7 @@ dictionary decode_request(int client,const char* method,const char* query)
{ {
dictionary request = NULL; dictionary request = NULL;
dictionary cookie = NULL; dictionary cookie = NULL;
dictionary xheader = dict();
char* line; char* line;
char * token; char * token;
if(strcmp(method,"GET") == 0) if(strcmp(method,"GET") == 0)
@ -47,8 +48,12 @@ dictionary decode_request(int client,const char* method,const char* query)
char* ws_key = NULL; char* ws_key = NULL;
while((line = read_line(client)) && strcmp("\r\n",line)) while((line = read_line(client)) && strcmp("\r\n",line))
{ {
trim(line, '\n');
trim(line, '\r');
token = strsep(&line,":"); token = strsep(&line,":");
trim(token,' '); trim(token,' ');
trim(line,' ');
dput(xheader,token,line);
if(token != NULL &&strcasecmp(token,"Cookie") == 0) if(token != NULL &&strcasecmp(token,"Cookie") == 0)
{ {
if(!cookie) cookie = decode_cookie(line); if(!cookie) cookie = decode_cookie(line);
@ -57,16 +62,12 @@ dictionary decode_request(int client,const char* method,const char* query)
{ {
// verify that the connection is upgrade to websocket // verify that the connection is upgrade to websocket
trim(line, ' '); trim(line, ' ');
trim(line, '\n');
trim(line, '\r');
if(line != NULL && strcasecmp(line,"websocket") == 0) if(line != NULL && strcasecmp(line,"websocket") == 0)
ws = 1; ws = 1;
} else if(token != NULL && strcasecmp(token,"Sec-WebSocket-Key") == 0) } else if(token != NULL && strcasecmp(token,"Sec-WebSocket-Key") == 0)
{ {
// get the key from the client // get the key from the client
trim(line, ' '); trim(line, ' ');
trim(line, '\n');
trim(line, '\r');
ws_key = strdup(line); ws_key = strdup(line);
} }
} }
@ -90,14 +91,16 @@ dictionary decode_request(int client,const char* method,const char* query)
while (line && strcmp("\r\n",line)) while (line && strcmp("\r\n",line))
{ {
//printf("%s\n",line); //printf("%s\n",line);
trim(line, '\n');
trim(line, '\r');
token = strsep(&line,":"); token = strsep(&line,":");
trim(token,' '); trim(token,' ');
trim(line, ' ');
dput(xheader,token,line);
if(token != NULL &&strcasecmp(token,"Content-Type") == 0) if(token != NULL &&strcasecmp(token,"Content-Type") == 0)
{ {
ctype = strsep(&line,":"); ctype = strsep(&line,":");
trim(ctype,' '); trim(ctype,' ');
trim(ctype,'\n');
trim(ctype,'\r');
} else if(token != NULL &&strcasecmp(token,"Content-Length") == 0) } else if(token != NULL &&strcasecmp(token,"Content-Length") == 0)
{ {
token = strsep(&line,":"); token = strsep(&line,":");
@ -140,10 +143,11 @@ dictionary decode_request(int client,const char* method,const char* query)
} }
} }
//if(cookie->key == NULL) {free(cookie);cookie= NULL;} //if(cookie->key == NULL) {free(cookie);cookie= NULL;}
if(cookie && !request) if(!request)
request = dict(); request = dict();
dput(request,"cookie",cookie); dput(request,"cookie",cookie);
dput(request,"__xheader__",xheader);
return request; return request;
} }
void __px(const char* data,int size) void __px(const char* data,int size)

View File

@ -30,12 +30,10 @@ sqldb getdb()
return getdb(__plugin__.name); return getdb(__plugin__.name);
} }
#endif #endif
void header_base(int client) void set_status(int client,int code,const char* msg)
{ {
response(client, __s("HTTP/1.0 %d %s", code, msg));
response(client, "HTTP/1.0 200 OK");
response(client, __s("Server: %s ", SERVER_NAME)); response(client, __s("Server: %s ", SERVER_NAME));
} }
void redirect(int client,const char*path) void redirect(int client,const char*path)
{ {
@ -60,7 +58,7 @@ void textstream(int client)
} }
void octstream(int client, char* name) void octstream(int client, char* name)
{ {
header_base(client); set_status(client,200,"OK");
__t(client,"Content-Type: application/octet-stream"); __t(client,"Content-Type: application/octet-stream");
__t(client,"Content-Disposition: attachment; filename=\"%s\"", name); __t(client,"Content-Disposition: attachment; filename=\"%s\"", name);
response(client,""); response(client,"");
@ -72,7 +70,7 @@ void jpeg(int client)
} }
void header(int client, const char* type) void header(int client, const char* type)
{ {
header_base(client); set_status(client,200,"OK");
__t(client,"Content-Type: %s",type); __t(client,"Content-Type: %s",type);
response(client,""); response(client,"");
} }
@ -240,7 +238,7 @@ int upload(const char* tmp, const char* path)
} }
void set_cookie(int client,const char* type, dictionary dic) void set_cookie(int client,const char* type, dictionary dic)
{ {
header_base(client); set_status(client,200,"OK");
__t(client,"Content-Type: %s",type); __t(client,"Content-Type: %s",type);
association assoc; association assoc;
for_each_assoc(assoc,dic){ for_each_assoc(assoc,dic){
@ -250,7 +248,7 @@ void set_cookie(int client,const char* type, dictionary dic)
} }
void clear_cookie(int client, dictionary dic) void clear_cookie(int client, dictionary dic)
{ {
header_base(client); set_status(client,200,"OK");
__t(client,"Content-Type: text/html; charset=utf-8"); __t(client,"Content-Type: text/html; charset=utf-8");
association assoc; association assoc;
for_each_assoc(assoc,dic){ for_each_assoc(assoc,dic){

View File

@ -36,7 +36,6 @@ extern plugin_header __plugin__;
extern call __init__; extern call __init__;
int response(int, const char*); int response(int, const char*);
void header_base(int);
void header(int,const char*); void header(int,const char*);
void redirect(int,const char*); void redirect(int,const char*);
void html(int); void html(int);
@ -59,6 +58,7 @@ sqldb getdb();
sqldb __getdb(char *name); sqldb __getdb(char *name);
#endif #endif
void set_cookie(int, const char*,dictionary); void set_cookie(int, const char*,dictionary);
void set_status(int,int,const char*);
void clear_cookie(int, dictionary); void clear_cookie(int, dictionary);
/*Default function for plugin*/ /*Default function for plugin*/
void handler(int, const char*,const char*,dictionary); void handler(int, const char*,const char*,dictionary);