fix regex problem and refactory request decode

This commit is contained in:
Xuan Sang LE 2018-02-05 11:42:01 +01:00
parent ff3c68ff64
commit 73adf50d41
2 changed files with 68 additions and 68 deletions

View File

@ -55,8 +55,14 @@ void accept_request(int client)
*tmp = '\0'; *tmp = '\0';
dictionary rq = decode_request(client, method, url); dictionary rq = decode_request(client, method, url);
if(rq == NULL)
{
badrequest(client);
goto end;
}
sprintf(path, server_config.htdocs); sprintf(path, server_config.htdocs);
strcat(path, url); strcat(path, url);
//printf("path is : %s \n", path);
//if (path[strlen(path) - 1] == '/') //if (path[strlen(path) - 1] == '/')
// strcat(path, "index.html"); // strcat(path, "index.html");
if (stat(path, &st) == -1) { if (stat(path, &st) == -1) {
@ -66,7 +72,14 @@ void accept_request(int client)
else else
{ {
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
{
strcat(path, "/index.html"); strcat(path, "/index.html");
if(stat(path, &st) == -1)
{
not_found(client);
goto end;
}
}
// check if the mime is supported // check if the mime is supported
// if the mime is not supported // if the mime is not supported
// find an handler plugin to process it // find an handler plugin to process it
@ -87,8 +100,9 @@ void accept_request(int client)
//response(client,"this is the file"); //response(client,"this is the file");
} }
} }
free(oldurl); end:
free(rq); if(oldurl) free(oldurl);
if(rq) free(rq);
close(client); close(client);
} }
@ -115,8 +129,9 @@ void rule_check(association it, const char* host, const char* _url, const char*
if(!ret) return; if(!ret) return;
tmp = (char*) it->value; tmp = (char*) it->value;
char * search = "<([a-zA-Z0-9]+)>";
while((ret = regex_match( "{([a-zA-Z0-9]+)}",tmp, 2, val_matches))) //printf("match again %s\n",tmp);
while((ret = regex_match( search,tmp, 2, val_matches)))
{ {
memcpy(buf + idx, tmp, val_matches[1].rm_so - 1); memcpy(buf + idx, tmp, val_matches[1].rm_so - 1);
idx += val_matches[1].rm_so - 1; idx += val_matches[1].rm_so - 1;
@ -422,9 +437,10 @@ dictionary decode_request(int client,const char* method, char* url)
char* line; char* line;
char * token; char * token;
char* query = NULL; char* query = NULL;
if(strcmp(method,"GET") == 0) char* ctype = NULL;
{ int clen = -1;
// this for check if web socket is enabled // first real all header
// this for check if web socket is enabled
int ws= 0; int ws= 0;
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))
@ -439,6 +455,16 @@ dictionary decode_request(int client,const char* method, char* url)
{ {
if(!cookie) cookie = decode_cookie(line); if(!cookie) cookie = decode_cookie(line);
} }
else if(token != NULL &&strcasecmp(token,"Content-Type") == 0)
{
ctype = strsep(&line,":");
trim(ctype,' ');
} else if(token != NULL &&strcasecmp(token,"Content-Length") == 0)
{
token = strsep(&line,":");
trim(token,' ');
clen = atoi(token);
}
else if(token != NULL && strcasecmp(token,"Upgrade") == 0) else if(token != NULL && strcasecmp(token,"Upgrade") == 0)
{ {
// verify that the connection is upgrade to websocket // verify that the connection is upgrade to websocket
@ -456,6 +482,11 @@ dictionary decode_request(int client,const char* method, char* url)
ws_key = strdup(line); ws_key = strdup(line);
} }
} }
if(line) free(line);
if(strcmp(method,"GET") == 0)
{
request = decode_url_request(query); request = decode_url_request(query);
if(query) free(query); if(query) free(query);
if(ws && ws_key != NULL) if(ws && ws_key != NULL)
@ -471,39 +502,6 @@ dictionary decode_request(int client,const char* method, char* url)
} }
else else
{ {
char* ctype = NULL;
int clen = -1;
line = read_line(client);
while (line && strcmp("\r\n",line))
{
//printf("%s\n",line);
trim(line, '\n');
trim(line, '\r');
token = strsep(&line,":");
trim(token,' ');
trim(line, ' ');
dput(xheader,token,line);
if(token != NULL &&strcasecmp(token,"Content-Type") == 0)
{
ctype = strsep(&line,":");
trim(ctype,' ');
} else if(token != NULL &&strcasecmp(token,"Content-Length") == 0)
{
token = strsep(&line,":");
trim(token,' ');
clen = atoi(token);
}else if(token != NULL && strcasecmp(token,"Host") == 0)
{
apply_rules(line, url);
}
else if(token != NULL &&strcasecmp(token,"Cookie") == 0)
{
if(!cookie) cookie = decode_cookie(line);
}
line = read_line(client);
}
free(line);
if(ctype == NULL || clen == -1) if(ctype == NULL || clen == -1)
{ {
LOG("Bad request\n"); LOG("Bad request\n");

View File

@ -216,6 +216,8 @@ int regex_match(const char* expr,const char* search, int msize, regmatch_t* matc
reti = regcomp(&regex, expr, REG_ICASE | REG_EXTENDED); reti = regcomp(&regex, expr, REG_ICASE | REG_EXTENDED);
if( reti ){ if( reti ){
LOG("Could not compile regex: %s\n",expr); LOG("Could not compile regex: %s\n",expr);
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
LOG("Regex match failed: %s\n", msgbuf);
return 0; return 0;
} }