1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-01 12:59:47 +02:00
ant-http/http_server.c

1016 lines
25 KiB
C
Raw Normal View History

2015-10-22 11:39:11 +02:00
#include "http_server.h"
static pthread_mutex_t server_mux = PTHREAD_MUTEX_INITIALIZER;
2015-10-22 11:39:11 +02:00
/**********************************************************************/
/* A request has caused a call to accept() on the server port to
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
void accept_request(void* client)
2015-10-22 11:39:11 +02:00
{
char buf[1024];
int numchars;
char method[255];
2018-02-10 11:22:41 +01:00
char url[4096];
2018-02-03 18:50:07 +01:00
char path[1024];
char* token;
char *line;
2018-03-14 18:49:25 +01:00
char* oldurl = NULL;
char* tmp = NULL;
dictionary rq = NULL;
2015-10-22 11:39:11 +02:00
size_t i, j;
struct stat st;
//char *query_string = NULL;
2018-02-10 12:24:01 +01:00
//LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock);
numchars = read_buf(client, buf, sizeof(buf));
2018-03-14 16:47:39 +01:00
if(numchars <= 0)
2018-03-14 11:55:15 +01:00
{
unknow(client);
goto end;
}
2015-10-22 11:39:11 +02:00
i = 0; j = 0;
2018-03-14 11:55:15 +01:00
while (j < numchars && !ISspace(buf[j]) && (i < sizeof(method) - 1))
2015-10-22 11:39:11 +02:00
{
method[i] = buf[j];
i++; j++;
}
method[i] = '\0';
if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
{
LOG("METHOD NOT FOUND %s\n", method);
2017-06-05 19:09:59 +02:00
// unimplemented
2018-02-03 18:50:07 +01:00
//while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
2015-10-22 11:39:11 +02:00
unimplemented(client);
2018-04-14 17:32:44 +02:00
//antd_close(client);
goto end;
2015-10-22 11:39:11 +02:00
}
i = 0;
while (ISspace(buf[j]) && (j < sizeof(buf)))
j++;
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)))
{
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
2018-02-03 18:50:07 +01:00
2018-03-14 18:49:25 +01:00
oldurl = strdup(url);
tmp = strchr(oldurl,'?');
if(tmp)
*tmp = '\0';
2018-02-03 18:50:07 +01:00
2018-03-14 18:49:25 +01:00
rq = decode_request(client, method, url);
if(rq == NULL)
{
badrequest(client);
goto end;
}
2015-10-22 11:39:11 +02:00
sprintf(path, server_config.htdocs);
strcat(path, url);
2018-02-05 19:06:03 +01:00
LOG("Path is : %s \n", path);
2018-02-03 18:50:07 +01:00
//if (path[strlen(path) - 1] == '/')
// strcat(path, "index.html");
2015-10-22 11:39:11 +02:00
if (stat(path, &st) == -1) {
if(execute_plugin(client,oldurl,method,rq) < 0)
2015-10-22 11:39:11 +02:00
not_found(client);
}
else
{
if (S_ISDIR(st.st_mode))
{
2018-02-20 19:02:31 +01:00
int l = strlen(path);
int ul = strlen(url);
2015-10-22 11:39:11 +02:00
strcat(path, "/index.html");
if(stat(path, &st) == -1)
{
2018-02-20 19:02:31 +01:00
association it;
for_each_assoc(it, server_config.handlers)
{
path[l] = '\0';
url[ul] = '\0';
strcat(url,"/index.");
strcat(path, "/index.");
strcat(url,it->key);
strcat(path, it->key);
if(stat(path, &st) == 0)
{
l = -1;
i = HASHSIZE;
break;
}
}
if(l!= -1)
{
not_found(client);
goto end;
}
}
}
2016-12-07 10:57:14 +01:00
// check if the mime is supported
// if the mime is not supported
2016-12-07 10:57:14 +01:00
// find an handler plugin to process it
// if the plugin is not found, forbidden access to the file should be sent
char* mime_type = mime(path);
if(strcmp(mime_type,"application/octet-stream") == 0)
{
2018-03-14 11:55:15 +01:00
char * ex = ext(path);
char* h = dvalue(server_config.handlers,ex);
if(ex) free(ex);
2018-02-20 19:02:31 +01:00
if(h)
{
sprintf(buf,"/%s%s",h,url);
LOG("WARNING::::Access octetstream via handler %s\n", buf);
if(execute_plugin(client,buf,method,rq) < 0)
cannot_execute(client);
}
else
unknow(client);
2016-12-07 10:57:14 +01:00
}
else
{
2017-07-29 23:54:43 +02:00
ctype(client,mime_type);
// if the mime is supported, send the file
2016-12-07 10:57:14 +01:00
serve_file(client, path);
//response(client,"this is the file");
2016-12-07 10:57:14 +01:00
}
2015-10-22 11:39:11 +02:00
}
end:
if(oldurl) free(oldurl);
2018-03-02 19:04:00 +01:00
if(rq)
{
dictionary subdict;
subdict = (dictionary)dvalue(rq, "__xheader__");
if(subdict)
{
freedict(subdict);
dput(rq, "__xheader__", NULL);
}
subdict = (dictionary)dvalue(rq, "cookie");
if(subdict)
{
freedict(subdict);
dput(rq, "cookie", NULL);
}
freedict(rq);
}
2018-02-10 11:22:41 +01:00
antd_close(client);
2015-10-22 11:39:11 +02:00
}
2018-10-03 23:42:42 +02:00
void* finish_request(void* data)
{
return NULL;
}
2018-02-23 19:54:16 +01:00
int rule_check(const char*k, const char* v, const char* host, const char* _url, const char* _query, char* buf)
{
// first perfom rule check on host, if not success, perform on url
regmatch_t key_matches[10];
regmatch_t val_matches[2];
char* query = strdup(_query);
char* url = strdup(_url);
int ret;
char* target;
char* tmp, rep[10];
int idx = 0;
memset(rep,0,10);
// 1 group
2018-02-23 19:54:16 +01:00
if(!host || !(ret = regex_match(k,host, 10, key_matches)) )
{
target = url;
2018-02-23 19:54:16 +01:00
ret = regex_match(k,url, 10, key_matches);
}
else
target = host;
2018-03-02 19:04:00 +01:00
if(!ret)
{
free(url);
free(query);
return 0;
}
2018-02-23 19:54:16 +01:00
tmp = (char*) v;
char * search = "<([a-zA-Z0-9]+)>";
//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);
idx += val_matches[1].rm_so - 1;
memcpy(rep, tmp + val_matches[1].rm_so, val_matches[1].rm_eo - val_matches[1].rm_so);
if(strcasecmp(rep,"url") == 0)
{
memcpy(buf+idx, url, strlen(url));
idx += strlen(url);
} else if(strcasecmp(rep,"query") == 0)
{
memcpy(buf+idx, query, strlen(query));
idx += strlen(query);
} else if(match_int(rep))
{
int i = atoi(rep);
memcpy(buf+idx, target + key_matches[i].rm_so, key_matches[i].rm_eo - key_matches[i].rm_so);
idx += key_matches[i].rm_eo - key_matches[i].rm_so;
} else { // just keep it
memcpy(buf+idx, tmp + val_matches[1].rm_so-1, val_matches[1].rm_eo + 2 - val_matches[1].rm_so);
idx+= val_matches[1].rm_eo + 2 - val_matches[1].rm_so;
}
tmp += val_matches[1].rm_eo + 1;
//break;
}
// now modify the match 2 group
2018-02-23 19:54:16 +01:00
if(idx > 0)
{
if(tmp)
{
// copy the remainning of tmp
memcpy(buf+idx, tmp, strlen(tmp));
idx += strlen(tmp);
}
buf[idx] = '\0';
}
free(url);
free(query);
2018-02-23 19:54:16 +01:00
return 1;
}
2015-10-22 11:39:11 +02:00
/**********************************************************************/
/* Put the entire contents of a file out on a socket. This function
* is named after the UNIX "cat" command, because it might have been
* easier just to do something like pipe, fork, and exec("cat").
* Parameters: the client socket descriptor
* FILE pointer for the file to cat */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
void catb(void* client, FILE* ptr)
2015-10-22 11:39:11 +02:00
{
unsigned char buffer[BUFFLEN];
2016-12-07 10:57:14 +01:00
size_t size;
2015-10-22 11:39:11 +02:00
while(!feof(ptr))
{
2016-12-07 10:57:14 +01:00
size = fread(buffer,1,BUFFLEN,ptr);
__b(client,buffer,size);
//if(!__b(client,buffer,size)) return;
2015-10-22 11:39:11 +02:00
}
2015-10-22 14:04:57 +02:00
//fclose(ptr);
2015-10-22 11:39:11 +02:00
}
2018-02-10 11:22:41 +01:00
void cat(void* client, FILE *resource)
2015-10-22 11:39:11 +02:00
{
char buf[1024];
2016-11-23 23:02:10 +01:00
//fgets(buf, sizeof(buf), resource);
while (fgets(buf, sizeof(buf), resource) != NULL)
2015-10-22 11:39:11 +02:00
{
2018-02-10 13:44:25 +01:00
antd_send(client, buf, strlen(buf));
2016-11-23 23:02:10 +01:00
//fgets(buf, sizeof(buf), resource);
2015-10-22 11:39:11 +02:00
}
}
/**********************************************************************/
/* Inform the client that a CGI script could not be executed.
* Parameter: the client socket descriptor. */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
void cannot_execute(void* client)
2015-10-22 11:39:11 +02:00
{
2017-07-29 23:54:43 +02:00
set_status(client,500,"Internal Server Error");
__t(client,SERVER_STRING);
__t(client,"Content-Type: text/html");
response(client,"");
__t(client, "<P>Error prohibited CGI execution.");
2015-10-22 11:39:11 +02:00
}
/**********************************************************************/
/* Print out an error message with perror() (for system errors; based
* on value of errno, which indicates system call errors) and exit the
* program indicating an error. */
/**********************************************************************/
void error_die(const char *sc)
{
perror(sc);
exit(1);
}
/**********************************************************************/
/* Get a line from a socket, whether the line ends in a newline,
* carriage return, or a CRLF combination. Terminates the string read
* with a null character. If no newline indicator is found before the
* end of the buffer, the string is terminated with a null. If any of
* the above three line terminators is read, the last character of the
* string will be a linefeed and the string will be terminated with a
* null character.
* Parameters: the socket descriptor
* the buffer to save the data in
* the size of the buffer
* Returns: the number of bytes stored (excluding null) */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
//This function is deprecate
2018-02-10 12:24:01 +01:00
/*int get_line(int sock, char *buf, int size)
2015-10-22 11:39:11 +02:00
{
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
2018-02-10 11:22:41 +01:00
2015-10-22 11:39:11 +02:00
if (n > 0)
{
if (c == '\r')
{
n = recv(sock, &c, 1, MSG_PEEK);
2018-02-10 11:22:41 +01:00
2015-10-22 11:39:11 +02:00
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
c = '\n';
}
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0';
return(i);
2018-02-10 12:24:01 +01:00
}*/
2015-10-22 11:39:11 +02:00
/**********************************************************************/
/* Give a client a 404 not found status message. */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
void not_found(void* client)
2015-10-22 11:39:11 +02:00
{
2017-07-29 23:54:43 +02:00
set_status(client,404,"NOT FOUND");
__t(client,SERVER_STRING);
__t(client,"Content-Type: text/html");
response(client,"");
__t(client, "<HTML><TITLE>Not Found</TITLE>");
__t(client, "<BODY><P>The server could not fulfill");
__t(client, "your request because the resource specified");
__t(client, "is unavailable or nonexistent.");
__t(client, "</BODY></HTML>");
2015-10-22 11:39:11 +02:00
}
/**********************************************************************/
/* Send a regular file to the client. Use headers, and report
* errors to client if they occur.
* Parameters: a pointer to a file structure produced from the socket
* file descriptor
* the name of the file to serve */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
void serve_file(void* client, const char *filename)
2015-10-22 11:39:11 +02:00
{
2018-02-10 11:22:41 +01:00
LOG("Serve file: %s\n", filename);
2015-10-22 11:39:11 +02:00
FILE *resource = NULL;
int numchars = 1;
//char buf[1024];
2015-10-22 11:39:11 +02:00
//buf[0] = 'A'; buf[1] = '\0';
//while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
// numchars = get_line(client, buf, sizeof(buf));
2015-10-22 11:39:11 +02:00
resource = fopen(filename, "rb");
if (resource == NULL)
not_found(client);
else
{
if(is_bin(filename))
catb(client,resource);
else
cat(client, resource);
}
fclose(resource);
}
/**********************************************************************/
/* This function starts the process of listening for web connections
* on a specified port. If the port is 0, then dynamically allocate a
* port and modify the original port variable to reflect the actual
* port.
* Parameters: pointer to variable containing the port to connect on
* Returns: the socket */
/**********************************************************************/
int startup(unsigned *port)
{
int httpd = 0;
struct sockaddr_in name;
httpd = socket(PF_INET, SOCK_STREAM, 0);
if (httpd == -1)
error_die("socket");
memset(&name, 0, sizeof(name));
name.sin_family = AF_INET;
name.sin_port = htons(*port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)
error_die("bind");
if (*port == 0) /* if dynamically allocating a port */
{
socklen_t namelen = sizeof(name);
if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
error_die("getsockname");
*port = ntohs(name.sin_port);
}
2018-02-05 23:04:02 +01:00
printf("back log is %d\n", server_config.backlog);
if (listen(httpd, server_config.backlog) < 0)
2015-10-22 11:39:11 +02:00
error_die("listen");
return(httpd);
}
/**********************************************************************/
/* Inform the client that the requested web method has not been
* implemented.
* Parameter: the client socket */
/**********************************************************************/
2018-02-10 11:22:41 +01:00
void unimplemented(void* client)
2015-10-22 11:39:11 +02:00
{
2017-07-29 23:54:43 +02:00
set_status(client,501,"Method Not Implemented");
__t(client,SERVER_STRING);
__t(client,"Content-Type: text/html");
response(client,"");
__t(client, "<HTML><HEAD><TITLE>Method Not Implemented");
__t(client, "</TITLE></HEAD>");
__t(client, "<BODY><P>HTTP request method not supported.");
__t(client, "</BODY></HTML>");
2015-10-22 11:39:11 +02:00
}
2018-02-10 11:22:41 +01:00
void badrequest(void* client)
2018-02-03 18:50:07 +01:00
{
set_status(client,400,"Bad Request");
__t(client,SERVER_STRING);
__t(client,"Content-Type: text/html");
response(client,"");
__t(client,"The request could not be understood by the server due to malformed syntax.");
}
2015-10-22 11:39:11 +02:00
char* apply_rules(const char* host, char*url)
{
association it;
// rule check
char* query_string = url;
while ((*query_string != '?') && (*query_string != '\0'))
query_string++;
if (*query_string == '?')
{
*query_string = '\0';
query_string++;
}
//char* oldurl = strdup(url);
2018-02-23 19:54:16 +01:00
int size = list_size(server_config.rules);
for(int i = 0; i < size; i+= 2)
{
2018-02-23 19:54:16 +01:00
char *k, *v;
k = list_at(server_config.rules, i)->value.s;
v = list_at(server_config.rules, i+1)->value.s;
// 1 group
2018-02-23 19:54:16 +01:00
if(rule_check(k, v,host, url, query_string, url)){
query_string = url;
while ((*query_string != '?') && (*query_string != '\0'))
query_string++;
if (*query_string == '?')
{
*query_string = '\0';
query_string++;
}
}
}
2018-02-23 19:54:16 +01:00
return strdup(query_string);
}
2017-07-29 22:00:34 +02:00
/**
* Decode the HTTP request
* Get the cookie values
* if it is the GET request, decode the query string into a dictionary
* if it is a POST, check the content type of the request
* - if it is a POST request with URL encoded : decode the url encode
* - if it is a POST request with multipart form data: de code the multipart
* - if other - UNIMPLEMENTED
* @param client socket client
* @param method HTTP method
* @param query query string in case of GET
* @return a dictionary of key- value
*/
2018-02-10 11:22:41 +01:00
dictionary decode_request(void* client,const char* method, char* url)
2017-07-29 22:00:34 +02:00
{
2018-09-05 23:26:21 +02:00
dictionary request = dict();
2017-07-29 22:00:34 +02:00
dictionary cookie = NULL;
dictionary xheader = dict();
char* line;
char * token;
char* query = NULL;
char* ctype = NULL;
2018-02-09 01:59:49 +01:00
char* host = NULL;
int clen = -1;
2018-02-09 09:21:10 +01:00
// first real all header
// this for check if web socket is enabled
int ws= 0;
char* ws_key = NULL;
2018-02-09 09:21:10 +01:00
char buf[BUFFLEN];
// ip address
dput(xheader,"REMOTE_ADDR", (void*)strdup(((antd_client_t*)client)->ip ));
2018-02-09 09:21:10 +01:00
//while((line = read_line(client)) && strcmp("\r\n",line))
while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf))
{
2018-02-09 09:21:10 +01:00
line = buf;
trim(line, '\n');
trim(line, '\r');
token = strsep(&line,":");
trim(token,' ');
trim(line,' ');
2018-03-17 20:54:38 +01:00
if(token && line && strlen(line) > 0)
dput(xheader,token,strdup(line));
if(token != NULL &&strcasecmp(token,"Cookie") == 0)
{
if(!cookie) cookie = decode_cookie(line);
}
else if(token != NULL &&strcasecmp(token,"Content-Type") == 0)
{
2018-02-09 09:21:10 +01:00
ctype = strdup(line); //strsep(&line,":");
trim(ctype,' ');
} else if(token != NULL &&strcasecmp(token,"Content-Length") == 0)
2017-07-29 22:00:34 +02:00
{
2018-02-09 01:59:49 +01:00
token = line; //strsep(&line,":");
2017-07-29 22:00:34 +02:00
trim(token,' ');
clen = atoi(token);
2017-07-29 22:00:34 +02:00
}
else if(token != NULL && strcasecmp(token,"Upgrade") == 0)
{
// verify that the connection is upgrade to websocket
trim(line, ' ');
if(line != NULL && strcasecmp(line,"websocket") == 0)
ws = 1;
}else if(token != NULL && strcasecmp(token,"Host") == 0)
{
2018-02-09 09:21:10 +01:00
host = strdup(line);
}
else if(token != NULL && strcasecmp(token,"Sec-WebSocket-Key") == 0)
{
// get the key from the client
trim(line, ' ');
ws_key = strdup(line);
}
}
2018-02-09 09:21:10 +01:00
//if(line) free(line);
2018-02-23 19:54:16 +01:00
query = apply_rules(host, url);
2018-09-05 23:26:21 +02:00
if(query)
{
LOG("Query: %s\n", query);
decode_url_request(query, request);
free(query);
}
2018-03-02 19:04:00 +01:00
if(host) free(host);
if(strcmp(method,"GET") == 0)
2018-03-02 19:04:00 +01:00
{
2018-09-05 23:26:21 +02:00
//if(ctype) free(ctype);
2017-07-29 22:00:34 +02:00
if(ws && ws_key != NULL)
{
ws_confirm_request(client, ws_key);
free(ws_key);
// insert wsocket flag to request
// plugin should handle this ugraded connection
// not the server
2018-09-05 23:26:21 +02:00
//if(!request) request = dict();
2018-03-02 19:04:00 +01:00
dput(request,"__web_socket__",strdup("1"));
2017-07-29 22:00:34 +02:00
}
}
else
{
2018-03-02 19:04:00 +01:00
if(ws_key)
free(ws_key);
2017-07-29 22:00:34 +02:00
if(ctype == NULL || clen == -1)
{
LOG("Bad request\n");
2018-02-09 09:21:10 +01:00
if(ctype) free(ctype);
2018-03-02 19:04:00 +01:00
if(cookie) freedict(cookie);
2018-09-05 23:26:21 +02:00
freedict(request);
freedict(xheader);
2017-07-29 22:00:34 +02:00
return NULL;
}
2017-08-28 22:26:00 +02:00
LOG("ContentType %s\n", ctype);
2017-07-29 22:00:34 +02:00
// decide what to do with the data
if(strstr(ctype,FORM_URL_ENCODE) > 0)
{
2018-09-11 11:06:01 +02:00
char* pquery = post_data_decode(client,clen);
decode_url_request(pquery, request);
free(pquery);
2017-07-29 22:00:34 +02:00
} else if(strstr(ctype,FORM_MULTI_PART)> 0)
{
//printf("Multi part form : %s\n", ctype);
2018-09-05 23:26:21 +02:00
decode_multi_part_request(client,ctype,request);
2017-07-29 22:00:34 +02:00
}
2018-02-19 17:44:45 +01:00
else
2017-07-29 22:00:34 +02:00
{
2018-09-05 19:08:26 +02:00
char* pquery = post_data_decode(client,clen);
2018-02-19 17:44:45 +01:00
char* key = strstr(ctype,"/");
if(key)
key++;
else
key = ctype;
2018-09-05 19:08:26 +02:00
dput(request,key, strdup(pquery));
free(pquery);
2017-07-29 22:00:34 +02:00
}
}
2018-02-09 09:21:10 +01:00
if(ctype) free(ctype);
2017-07-29 22:00:34 +02:00
//if(cookie->key == NULL) {free(cookie);cookie= NULL;}
2018-09-05 23:26:21 +02:00
//if(!request)
// request = dict();
2018-03-02 19:04:00 +01:00
if(cookie)
dput(request,"cookie",cookie);
2017-07-29 22:00:34 +02:00
dput(request,"__xheader__",xheader);
return request;
}
void __px(const char* data,int size)
{
for (int i = 0; i < size; ++i)
printf(" %02x", data[i]);
printf("\n");
}
/**
* Send header to the client to confirm
* that the websocket is accepted by
* our server
*/
2018-02-10 11:22:41 +01:00
void ws_confirm_request(void* client, const char* key)
2017-07-29 22:00:34 +02:00
{
char buf[256];
char rkey[128];
char sha_d[20];
char base64[64];
strcpy(rkey,key);
strcat(rkey,WS_MAGIC_STRING);
//printf("RESPONDKEY '%s'\n", rkey);
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
SHA_CTX context;
#else
2017-07-29 22:00:34 +02:00
SHA1_CTX context;
2018-02-10 11:22:41 +01:00
#endif
2017-07-29 22:00:34 +02:00
SHA1_Init(&context);
SHA1_Update(&context, rkey, strlen(rkey));
2018-02-10 11:22:41 +01:00
SHA1_Final(sha_d, &context);
2017-07-29 22:00:34 +02:00
Base64encode(base64, sha_d, 20);
//printf("Base 64 '%s'\n", base64);
// send accept to client
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
2018-02-10 13:44:25 +01:00
antd_send(client, buf, strlen(buf));
2017-07-29 22:00:34 +02:00
sprintf(buf, "Upgrade: websocket\r\n");
2018-02-10 13:44:25 +01:00
antd_send(client, buf, strlen(buf));
2017-07-29 22:00:34 +02:00
sprintf(buf, "Connection: Upgrade\r\n");
2018-02-10 13:44:25 +01:00
antd_send(client, buf, strlen(buf));
2017-07-29 22:00:34 +02:00
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
2018-02-10 13:44:25 +01:00
antd_send(client, buf, strlen(buf));
2017-07-29 22:00:34 +02:00
sprintf(buf, "\r\n");
2018-02-10 13:44:25 +01:00
antd_send(client, buf, strlen(buf));
2017-07-29 22:00:34 +02:00
LOG("%s\n", "Websocket is now enabled for plugin");
}
/**
* Decode the cookie header to a dictionary
* @param client The client socket
* @return The Dictionary socket or NULL
*/
dictionary decode_cookie(const char* line)
{
char *token,*token1;
char *cpstr = strdup(line);
2018-03-02 19:04:00 +01:00
char *orgcpy = cpstr;
2017-07-29 22:00:34 +02:00
trim(cpstr,' ');
trim(cpstr,'\n');
trim(cpstr,'\r');
2018-03-02 19:04:00 +01:00
2017-07-29 22:00:34 +02:00
dictionary dic = NULL;
while((token = strsep(&cpstr,";")))
{
trim(token,' ');
token1 = strsep(&token,"=");
2018-03-14 15:10:59 +01:00
if(token1 && token && strlen(token) > 0)
2017-07-29 22:00:34 +02:00
{
if(dic == NULL)
dic = dict();
dput(dic,token1,strdup(token));
}
}
//}
2018-03-02 19:04:00 +01:00
free(orgcpy);
2017-07-29 22:00:34 +02:00
return dic;
}
/**
* Decode the multi-part form data from the POST request
* If it is a file upload, copy the file to tmp dir
* and generate the metadata for the server-side
* @param client the socket client
* @param ctype Content-Type of the request
* @param clen Content length, but not used here
* @return a dictionary of key - value
*/
2018-09-05 23:26:21 +02:00
void decode_multi_part_request(void* client,const char* ctype, dictionary dic)
2017-07-29 22:00:34 +02:00
{
char * boundary;
char * boundend;
char * line;
2018-03-02 19:04:00 +01:00
char * orgline;
2017-07-29 22:00:34 +02:00
char * str_copy = strdup(ctype);
2018-03-02 19:04:00 +01:00
char* orgcpy = str_copy;
2017-07-29 22:00:34 +02:00
char* token;
char* keytoken ;
char* valtoken ;
char* part_name;
char* part_file;
char* file_path;
char buf[BUFFLEN];
char* field;
2018-09-05 23:26:21 +02:00
//dictionary dic = NULL;
2017-07-29 22:00:34 +02:00
FILE *fp = NULL;
boundary = strsep(&str_copy,"="); //discard first part
boundary = strsep(&str_copy,"=");
if(boundary && strlen(boundary)>0)
{
2018-09-05 23:26:21 +02:00
//dic = dict();
2017-07-29 22:00:34 +02:00
trim(boundary,' ');
boundend = __s("%s--",boundary);
//find first boundary
2018-03-02 19:04:00 +01:00
while((line = read_line(client))&&strstr(line,boundary) <= 0)
{
if(line) free(line);
}
2017-07-29 22:00:34 +02:00
// loop through each part separated by the boundary
while(line && strstr(line,boundary) > 0){
2018-09-05 23:26:21 +02:00
if(line)
{
free(line);
line = NULL;
}
2017-07-29 22:00:34 +02:00
// search for content disposition:
while((line = read_line(client)) &&
2018-03-02 19:04:00 +01:00
strstr(line,"Content-Disposition:") <= 0)
{
free(line);
line = NULL;
}
if(!line || strstr(line,"Content-Disposition:") <= 0)
{
if(line)
free(line);
free(orgcpy);
free(boundend);
2018-09-05 23:26:21 +02:00
return;
2018-03-02 19:04:00 +01:00
}
orgline = line;
2017-07-29 22:00:34 +02:00
// extract parameters from header
part_name = NULL;
part_file = NULL;
while((token = strsep(&line,";")))
{
keytoken = strsep(&token,"=");
if(keytoken && strlen(keytoken)>0)
{
trim(keytoken,' ');
valtoken = strsep(&token,"=");
if(valtoken)
{
trim(valtoken,' ');
trim(valtoken,'\n');
trim(valtoken,'\r');
trim(valtoken,'\"');
if(strcmp(keytoken,"name") == 0)
{
2018-03-02 19:04:00 +01:00
part_name = strdup(valtoken);
2017-07-29 22:00:34 +02:00
} else if(strcmp(keytoken,"filename") == 0)
{
2018-03-02 19:04:00 +01:00
part_file = strdup(valtoken);
2017-07-29 22:00:34 +02:00
}
}
}
}
2018-03-02 19:04:00 +01:00
free(orgline);
2018-09-05 23:26:21 +02:00
line = NULL;
2017-07-29 22:00:34 +02:00
// get the binary data
if(part_name != NULL)
{
// go to the beginer of data bock
2018-03-02 19:04:00 +01:00
while((line = read_line(client)) && strcmp(line,"\r\n") != 0)
{
free(line);
2018-09-05 23:26:21 +02:00
line = NULL;
}
if(line)
{
free(line);
line = NULL;
2018-03-02 19:04:00 +01:00
}
2017-07-29 22:00:34 +02:00
if(part_file == NULL)
{
/**
* This allow only 1024 bytes of data (max),
* out of this range, the data is cut out.
* Need an efficient way to handle this
*/
line = read_line(client);
trim(line,'\n');
trim(line,'\r');
trim(line,' ');
dput(dic,part_name,line);
// find the next boundary
2018-03-02 19:04:00 +01:00
while((line = read_line(client)) && strstr(line,boundary) <= 0)
{
free(line);
2018-09-05 23:26:21 +02:00
line = NULL;
2018-03-02 19:04:00 +01:00
}
2017-07-29 22:00:34 +02:00
}
else
{
file_path = __s("%s%s.%u",server_config.tmpdir,part_file,(unsigned)time(NULL));
fp=fopen(file_path, "wb");
if(fp)
{
int totalsize=0,len=0;
//read until the next boundary
while((len = read_buf(client,buf,sizeof(buf))) > 0 && strstr(buf,boundary) <= 0)
{
fwrite(buf, len, 1, fp);
totalsize += len;
}
//remove \r\n at the end
fseek(fp,-2, SEEK_CUR);
totalsize -= 2;
fclose(fp);
2018-09-05 23:26:21 +02:00
line = strdup(buf);
2017-07-29 22:00:34 +02:00
field = __s("%s.file",part_name);
2018-03-02 19:04:00 +01:00
dput(dic,field, strdup(part_file));
free(field);
2017-07-29 22:00:34 +02:00
field = __s("%s.tmp",part_name);
dput(dic,field,strdup(file_path));
2018-03-02 19:04:00 +01:00
free(field);
2017-07-29 22:00:34 +02:00
field = __s("%s.size",part_name);
dput(dic,field,__s("%d",totalsize));
2018-03-02 19:04:00 +01:00
free(field);
2017-07-29 22:00:34 +02:00
field = __s("%s.ext",part_name);
dput(dic,field,ext(part_file));
free(field);
}
else
{
LOG("Cannot wirte file to :%s\n", file_path );
}
free(file_path);
2018-03-02 19:04:00 +01:00
free(part_file);
2017-07-29 22:00:34 +02:00
}
2018-03-02 19:04:00 +01:00
free(part_name);
2017-07-29 22:00:34 +02:00
}
//printf("[Lines]:%s\n",line);
// check if end of request
if(line&&strstr(line,boundend)>0)
{
LOG("End request %s\n", boundend);
2018-09-05 23:26:21 +02:00
free(line);
2017-07-29 22:00:34 +02:00
break;
}
2018-03-02 19:04:00 +01:00
}
free(boundend);
2017-07-29 22:00:34 +02:00
}
2018-03-02 19:04:00 +01:00
free(orgcpy);
2018-09-05 23:26:21 +02:00
//return dic;
2017-07-29 22:00:34 +02:00
}
/**
* Decode a query string (GET request or POST URL encoded) to
* a dictionary of key-value
* @param query : the query string
* @return a dictionary of key-value
*/
2018-09-05 23:26:21 +02:00
void decode_url_request(const char* query, dictionary dic)
2017-07-29 22:00:34 +02:00
{
2018-09-05 23:26:21 +02:00
if(query == NULL) return;
2017-07-29 22:00:34 +02:00
//str_copy = ;
char* token;
2018-09-05 23:26:21 +02:00
if(strlen(query) == 0) return;
2018-03-02 19:04:00 +01:00
char* str_copy = strdup(query);
char* org_copy = str_copy;
2018-09-05 23:26:21 +02:00
//dictionary dic = dict();
2017-07-29 22:00:34 +02:00
while ((token = strsep(&str_copy, "&")))
{
char* key;
2018-03-17 15:49:37 +01:00
char* val = NULL;
2017-07-29 22:00:34 +02:00
if(strlen(token)>0)
{
key = strsep(&token,"=");
if(key && strlen(key)>0)
{
2018-03-17 15:49:37 +01:00
val = strsep(&token,"=");
if(!val)
val = "";
2017-07-29 22:00:34 +02:00
dput(dic,key,url_decode(val));
}
}
}
2018-03-02 19:04:00 +01:00
free(org_copy);
2018-09-05 23:26:21 +02:00
//return dic;
2017-07-29 22:00:34 +02:00
}
/**
2018-02-19 17:44:45 +01:00
* Decode post query string to string
2017-07-29 22:00:34 +02:00
*/
2018-02-19 17:44:45 +01:00
char* post_data_decode(void* client,int len)
2017-07-29 22:00:34 +02:00
{
char *query = (char*) malloc((len+1)*sizeof(char));
for (int i = 0; i < len; i++) {
2018-02-10 13:44:25 +01:00
antd_recv(client, (query+i), 1);
2017-07-29 22:00:34 +02:00
}
query[len]='\0';
//query = url_decode(query);
2018-02-19 17:44:45 +01:00
//LOG("JSON Query %s\n", query);
2017-07-29 22:00:34 +02:00
return query;
}
/**
* Execute a plugin based on the http requeset
* First decode the http request header to find the correct plugin
* and the correct function on the plugin
* Second, decode all parameters necessary of the request and pass it
* to the callback function.
* Execute the callback function if sucess
* @param client soket client
* @param path request path
* @param method request method
* @param query_string GET query string
* @return -1 if failure
* 1 if sucess
*/
2018-02-10 11:22:41 +01:00
int execute_plugin(void* client, const char *path, const char *method, dictionary dic)
2017-07-29 22:00:34 +02:00
{
char pname[255];
char pfunc[255];
2018-02-10 11:22:41 +01:00
void (*fn)(void*, const char*,const char*, dictionary);
2017-07-29 22:00:34 +02:00
struct plugin_entry *plugin ;
int plen = strlen(path);
char * rpath = (char*) malloc((plen+1)*sizeof(char));
2018-03-02 19:04:00 +01:00
char* orgs = rpath;
2017-07-29 22:00:34 +02:00
char *error;
memcpy(rpath,path+1,plen);
rpath[plen] = '\0';
trim(rpath,'/');
char * delim = strchr(rpath,'/');
if(delim == NULL)
{
strcpy(pname,rpath);
strcpy(pfunc,"default");
}
else
{
int npos,fpos;
npos = delim - rpath;
fpos = strlen(rpath) - npos ;
memcpy(pname,rpath,npos);
pname[npos] = '\0';
memcpy(pfunc,rpath+npos+1,fpos);
pfunc[fpos-1]='\0';
}
2018-02-10 12:24:01 +01:00
LOG("Client %d\n",((antd_client_t*)client)->sock );
2017-07-29 22:00:34 +02:00
LOG("Path : '%s'\n", rpath);
LOG("Method:%s\n", method);
LOG("Plugin name '%s'\n",pname);
LOG("Query path. '%s'\n", pfunc);
//LOG("query :%s\n", query_string);
2017-07-29 22:00:34 +02:00
//load the plugin
if((plugin = plugin_lookup(pname)) == NULL)
{
pthread_mutex_lock(&server_mux);
plugin= plugin_load(pname);
pthread_mutex_unlock(&server_mux);
if( plugin == NULL)
2018-03-14 10:51:46 +01:00
{
if(orgs) free(orgs);
2017-07-29 22:00:34 +02:00
return -1;
2018-03-14 10:51:46 +01:00
}
}
2017-07-29 22:00:34 +02:00
// load the function
2018-02-10 11:22:41 +01:00
fn = (void (*)(void*, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER);
2017-07-29 22:00:34 +02:00
if ((error = dlerror()) != NULL)
{
2018-03-14 10:51:46 +01:00
if(orgs) free(orgs);
2017-07-29 22:00:34 +02:00
LOG("Problem when finding %s method from %s : %s \n", PLUGIN_HANDLER, pname,error);
return -1;
}
//dictionary dic = decode_request(client,method,query_string);
2017-07-29 22:00:34 +02:00
(*fn)(client,method,pfunc,dic);
//free(dic);
2018-03-02 19:04:00 +01:00
free(orgs);
2017-07-29 22:00:34 +02:00
return 1;
}
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
int usessl()
{
return server_config.usessl;
}
#endif