2015-10-22 11:39:11 +02:00
|
|
|
#include "http_server.h"
|
|
|
|
/**********************************************************************/
|
|
|
|
/* 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 */
|
|
|
|
/**********************************************************************/
|
|
|
|
void accept_request(int client)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int numchars;
|
|
|
|
char method[255];
|
2015-11-24 17:58:32 +01:00
|
|
|
char url[4096];
|
2015-10-22 11:39:11 +02:00
|
|
|
char path[512];
|
|
|
|
size_t i, j;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
char *query_string = NULL;
|
|
|
|
|
|
|
|
numchars = get_line(client, buf, sizeof(buf));
|
2017-06-05 19:09:59 +02:00
|
|
|
//printf("%s\n", buf);
|
2015-10-22 11:39:11 +02:00
|
|
|
i = 0; j = 0;
|
|
|
|
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
|
|
|
|
{
|
|
|
|
method[i] = buf[j];
|
|
|
|
i++; j++;
|
|
|
|
}
|
|
|
|
method[i] = '\0';
|
|
|
|
if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
|
|
|
|
{
|
2017-06-05 19:09:59 +02:00
|
|
|
printf("METHOD NOT FOUND %s\n", method);
|
|
|
|
// unimplemented
|
2015-10-22 11:39:11 +02:00
|
|
|
unimplemented(client);
|
2017-06-05 19:09:59 +02:00
|
|
|
while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
|
|
|
|
close(client);
|
2015-10-22 11:39:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
if (strcasecmp(method, "GET") == 0)
|
|
|
|
{
|
|
|
|
query_string = url;
|
|
|
|
while ((*query_string != '?') && (*query_string != '\0'))
|
|
|
|
query_string++;
|
|
|
|
if (*query_string == '?')
|
|
|
|
{
|
|
|
|
*query_string = '\0';
|
|
|
|
query_string++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(path, server_config.htdocs);
|
|
|
|
strcat(path, url);
|
|
|
|
//LOG("Path %s\n", path);
|
|
|
|
if (path[strlen(path) - 1] == '/')
|
|
|
|
strcat(path, "index.html");
|
|
|
|
if (stat(path, &st) == -1) {
|
|
|
|
|
|
|
|
if(execute_plugin(client,url,method,query_string) < 0)
|
|
|
|
not_found(client);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
strcat(path, "/index.html");
|
2016-12-07 10:57:14 +01:00
|
|
|
// check if the mime is supported
|
|
|
|
// if the minme is not supported
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
sprintf(buf,"/%s-api%s",ext(path),url);
|
|
|
|
LOG("WARNING::::Access octetstream via handler %s\n", buf);
|
|
|
|
if(execute_plugin(client,buf,method,query_string) < 0)
|
|
|
|
cannot_execute(client);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
headers(client,mime_type);
|
|
|
|
// if the mime is supported, send the file
|
|
|
|
serve_file(client, path);
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Inform the client that a request it has made has a problem.
|
|
|
|
* Parameters: client socket */
|
|
|
|
/**********************************************************************/
|
|
|
|
void bad_request(int client)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n");
|
|
|
|
send(client, buf, sizeof(buf), 0);
|
|
|
|
sprintf(buf, "Content-type: text/html\r\n");
|
|
|
|
send(client, buf, sizeof(buf), 0);
|
|
|
|
sprintf(buf, "\r\n");
|
|
|
|
send(client, buf, sizeof(buf), 0);
|
|
|
|
sprintf(buf, "<P>Your browser sent a bad request, ");
|
|
|
|
send(client, buf, sizeof(buf), 0);
|
|
|
|
sprintf(buf, "such as a POST without a Content-Length.\r\n");
|
|
|
|
send(client, buf, sizeof(buf), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* 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 */
|
|
|
|
/**********************************************************************/
|
|
|
|
void __b(int client, const unsigned char* data, int size)
|
|
|
|
{
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
int sent = 0;
|
|
|
|
int buflen = 0;
|
|
|
|
if(size <= BUFFLEN)
|
|
|
|
send(client,data,size,0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(sent < size)
|
|
|
|
{
|
|
|
|
if(size - sent > BUFFLEN)
|
|
|
|
buflen = BUFFLEN;
|
|
|
|
else
|
|
|
|
buflen = size - sent;
|
|
|
|
memcpy(buf,data+sent,buflen);
|
|
|
|
send(client,buf,buflen,0);
|
|
|
|
sent += buflen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void catb(int client, FILE* ptr)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
void cat(int client, FILE *resource)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
send(client, buf, strlen(buf), 0);
|
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. */
|
|
|
|
/**********************************************************************/
|
|
|
|
void cannot_execute(int client)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "Content-type: text/html\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "<P>Error prohibited CGI execution.\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* 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) */
|
|
|
|
/**********************************************************************/
|
|
|
|
int get_line(int sock, char *buf, int size)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char c = '\0';
|
|
|
|
int n;
|
|
|
|
|
|
|
|
while ((i < size - 1) && (c != '\n'))
|
|
|
|
{
|
|
|
|
n = recv(sock, &c, 1, 0);
|
|
|
|
/* DEBUG printf("%02X\n", c); */
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
if (c == '\r')
|
|
|
|
{
|
|
|
|
n = recv(sock, &c, 1, MSG_PEEK);
|
|
|
|
/* DEBUG printf("%02X\n", c); */
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Return the informational HTTP headers about a file. */
|
|
|
|
/* Parameters: the socket to print the headers on
|
|
|
|
* the name of the file */
|
|
|
|
/**********************************************************************/
|
2016-12-07 10:57:14 +01:00
|
|
|
void headers(int client, const char *mime_type)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
//printf("Mime %s\n", mime(filename));
|
|
|
|
strcpy(buf, "HTTP/1.0 200 OK\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
strcpy(buf, SERVER_STRING);
|
|
|
|
send(client, buf, strlen(buf), 0);
|
2016-12-07 10:57:14 +01:00
|
|
|
sprintf(buf, "Content-Type: %s\r\n",mime_type);
|
2015-10-22 11:39:11 +02:00
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
strcpy(buf, "\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Give a client a 404 not found status message. */
|
|
|
|
/**********************************************************************/
|
|
|
|
void not_found(int client)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, SERVER_STRING);
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "Content-Type: text/html\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "<BODY><P>The server could not fulfill\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "your request because the resource specified\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "is unavailable or nonexistent.\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "</BODY></HTML>\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* 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 */
|
|
|
|
/**********************************************************************/
|
|
|
|
void serve_file(int client, const char *filename)
|
|
|
|
{
|
|
|
|
FILE *resource = NULL;
|
|
|
|
int numchars = 1;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
buf[0] = 'A'; buf[1] = '\0';
|
|
|
|
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
|
|
|
|
numchars = get_line(client, buf, sizeof(buf));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (listen(httpd, 5) < 0)
|
|
|
|
error_die("listen");
|
|
|
|
return(httpd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Inform the client that the requested web method has not been
|
|
|
|
* implemented.
|
|
|
|
* Parameter: the client socket */
|
|
|
|
/**********************************************************************/
|
|
|
|
void unimplemented(int client)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, SERVER_STRING);
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "Content-Type: text/html\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "</TITLE></HEAD>\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "</BODY></HTML>\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-29 22:00:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode the HTTP POST request with URL encode
|
|
|
|
* @param client socket client
|
|
|
|
* @param len content length
|
|
|
|
* @return query string
|
|
|
|
*/
|
|
|
|
char* post_url_decode(int client,int len)
|
|
|
|
{
|
|
|
|
char *query = (char*) malloc((len+1)*sizeof(char));
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
recv(client, (query+i), 1, 0);
|
|
|
|
}
|
|
|
|
query[len]='\0';
|
|
|
|
//query = url_decode(query);
|
|
|
|
LOG("POST Query %s\n", query);
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
dictionary decode_request(int client,const char* method,const char* query)
|
|
|
|
{
|
|
|
|
dictionary request = NULL;
|
|
|
|
dictionary cookie = NULL;
|
|
|
|
dictionary xheader = dict();
|
|
|
|
char* line;
|
|
|
|
char * token;
|
|
|
|
if(strcmp(method,"GET") == 0)
|
|
|
|
{
|
|
|
|
// this for check if web socket is enabled
|
|
|
|
int ws= 0;
|
|
|
|
char* ws_key = NULL;
|
|
|
|
while((line = read_line(client)) && strcmp("\r\n",line))
|
|
|
|
{
|
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
token = strsep(&line,":");
|
|
|
|
trim(token,' ');
|
|
|
|
trim(line,' ');
|
|
|
|
dput(xheader,token,line);
|
|
|
|
if(token != NULL &&strcasecmp(token,"Cookie") == 0)
|
|
|
|
{
|
|
|
|
if(!cookie) cookie = decode_cookie(line);
|
|
|
|
}
|
|
|
|
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,"Sec-WebSocket-Key") == 0)
|
|
|
|
{
|
|
|
|
// get the key from the client
|
|
|
|
trim(line, ' ');
|
|
|
|
ws_key = strdup(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request = decode_url_request(query);
|
|
|
|
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
|
|
|
|
if(!request) request = dict();
|
|
|
|
dput(request,"__web_socket__","1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,"Cookie") == 0)
|
|
|
|
{
|
|
|
|
if(!cookie) cookie = decode_cookie(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
line = read_line(client);
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
if(ctype == NULL || clen == -1)
|
|
|
|
{
|
|
|
|
LOG("Bad request\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// decide what to do with the data
|
|
|
|
if(strstr(ctype,FORM_URL_ENCODE) > 0)
|
|
|
|
{
|
|
|
|
request = decode_url_request(post_url_decode(client,clen));
|
|
|
|
} else if(strstr(ctype,FORM_MULTI_PART)> 0)
|
|
|
|
{
|
|
|
|
//printf("Multi part form : %s\n", ctype);
|
|
|
|
request = decode_multi_part_request(client,ctype);
|
|
|
|
}
|
|
|
|
else if(strstr(ctype,APP_JSON) > 0)
|
|
|
|
{
|
|
|
|
char* query = json_data_decode(client,clen);
|
|
|
|
request = dict();
|
|
|
|
dput(request,"json", strdup(query));
|
|
|
|
free(query);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("Un supported yet %s\n",ctype);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//if(cookie->key == NULL) {free(cookie);cookie= NULL;}
|
|
|
|
if(!request)
|
|
|
|
request = dict();
|
|
|
|
|
|
|
|
dput(request,"cookie",cookie);
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void ws_confirm_request(int client, const char* key)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
SHA1_CTX context;
|
|
|
|
SHA1_Init(&context);
|
|
|
|
SHA1_Update(&context, rkey, strlen(rkey));
|
|
|
|
SHA1_Final(&context, sha_d);
|
|
|
|
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");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "Upgrade: websocket\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "Connection: Upgrade\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
sprintf(buf, "\r\n");
|
|
|
|
send(client, buf, strlen(buf), 0);
|
|
|
|
|
|
|
|
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);
|
|
|
|
trim(cpstr,' ');
|
|
|
|
trim(cpstr,'\n');
|
|
|
|
trim(cpstr,'\r');
|
|
|
|
//printf("FUCKIT %s\n",cpstr );
|
|
|
|
dictionary dic = NULL;
|
|
|
|
while((token = strsep(&cpstr,";")))
|
|
|
|
{
|
|
|
|
trim(token,' ');
|
|
|
|
token1 = strsep(&token,"=");
|
|
|
|
if(token1)
|
|
|
|
{
|
|
|
|
if(dic == NULL)
|
|
|
|
dic = dict();
|
|
|
|
//LOG("Found cookie : %s = %s\n",token1,token);
|
|
|
|
dput(dic,token1,strdup(token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//}
|
|
|
|
return dic;
|
|
|
|
//free(cpstr);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
dictionary decode_multi_part_request(int client,const char* ctype)
|
|
|
|
{
|
|
|
|
char * boundary;
|
|
|
|
char * boundend;
|
|
|
|
char * line;
|
|
|
|
char * str_copy = strdup(ctype);
|
|
|
|
char* token;
|
|
|
|
char* keytoken ;
|
|
|
|
char* valtoken ;
|
|
|
|
char* part_name;
|
|
|
|
char* part_file;
|
|
|
|
char* file_path;
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
char* field;
|
|
|
|
dictionary dic = NULL;
|
|
|
|
FILE *fp = NULL;
|
|
|
|
boundary = strsep(&str_copy,"="); //discard first part
|
|
|
|
boundary = strsep(&str_copy,"=");
|
|
|
|
if(boundary && strlen(boundary)>0)
|
|
|
|
{
|
|
|
|
dic = dict();
|
|
|
|
trim(boundary,' ');
|
|
|
|
boundend = __s("%s--",boundary);
|
|
|
|
//find first boundary
|
|
|
|
while((line = read_line(client))&&strstr(line,boundary) <= 0);
|
|
|
|
// loop through each part separated by the boundary
|
|
|
|
while(line && strstr(line,boundary) > 0){
|
|
|
|
// search for content disposition:
|
|
|
|
while((line = read_line(client)) &&
|
|
|
|
strstr(line,"Content-Disposition:") <= 0);
|
|
|
|
if(strstr(line,"Content-Disposition:") <= 0) return NULL;
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
part_name = valtoken;
|
|
|
|
} else if(strcmp(keytoken,"filename") == 0)
|
|
|
|
{
|
|
|
|
part_file = valtoken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// get the binary data
|
|
|
|
if(part_name != NULL)
|
|
|
|
{
|
|
|
|
// go to the beginer of data bock
|
|
|
|
while((line = read_line(client)) && strcmp(line,"\r\n") != 0);
|
|
|
|
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
|
|
|
|
while((line = read_line(client)) && strstr(line,boundary) <= 0);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
line = buf;
|
|
|
|
|
|
|
|
field = __s("%s.file",part_name);
|
|
|
|
dput(dic,field,part_file);
|
|
|
|
field = __s("%s.tmp",part_name);
|
|
|
|
dput(dic,field,strdup(file_path));
|
|
|
|
field = __s("%s.size",part_name);
|
|
|
|
dput(dic,field,__s("%d",totalsize));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//printf("[Lines]:%s\n",line);
|
|
|
|
// check if end of request
|
|
|
|
if(line&&strstr(line,boundend)>0)
|
|
|
|
{
|
|
|
|
LOG("End request %s\n", boundend);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(str_copy);
|
|
|
|
return dic;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
dictionary decode_url_request(const char* query)
|
|
|
|
{
|
|
|
|
char* str_copy = strdup(query);
|
|
|
|
//str_copy = ;
|
|
|
|
char* token;
|
|
|
|
if(query == NULL) return NULL;
|
|
|
|
if(strlen(query) == 0) return NULL;
|
|
|
|
dictionary dic = dict();
|
|
|
|
while ((token = strsep(&str_copy, "&")))
|
|
|
|
{
|
|
|
|
char* key;
|
|
|
|
char* val;
|
|
|
|
if(strlen(token)>0)
|
|
|
|
{
|
|
|
|
key = strsep(&token,"=");
|
|
|
|
if(key && strlen(key)>0)
|
|
|
|
{
|
|
|
|
val = strsep(&token,"=");
|
|
|
|
dput(dic,key,url_decode(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(str_copy);
|
|
|
|
return dic;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Decode JSON query string to string
|
|
|
|
*/
|
|
|
|
char* json_data_decode(int client,int len)
|
|
|
|
{
|
|
|
|
char *query = (char*) malloc((len+1)*sizeof(char));
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
recv(client, (query+i), 1, 0);
|
|
|
|
}
|
|
|
|
query[len]='\0';
|
|
|
|
//query = url_decode(query);
|
|
|
|
LOG("JSON Query %s\n", query);
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read the request as a string line format
|
|
|
|
* @param sock socket
|
|
|
|
* @return a request string
|
|
|
|
*/
|
|
|
|
char* read_line(int sock)
|
|
|
|
{
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
read_buf(sock,buf,sizeof(buf));
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Read the socket request in to a buffer or size
|
|
|
|
* The data is read until the buffer is full or
|
|
|
|
* there are a carrier return character
|
|
|
|
* @param sock socket
|
|
|
|
* @param buf buffer
|
|
|
|
* @param size size of buffer
|
|
|
|
* @return number of bytes read
|
|
|
|
*/
|
|
|
|
int read_buf(int sock, char*buf,int size)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char c = '\0';
|
|
|
|
int n;
|
|
|
|
while ((i < size - 1) && (c != '\n'))
|
|
|
|
{
|
|
|
|
n = recv(sock, &c, 1, 0);
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
buf[i] = c;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c = '\n';
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
int execute_plugin(int client, const char *path, const char *method, const char *query_string)
|
|
|
|
{
|
|
|
|
char pname[255];
|
|
|
|
char pfunc[255];
|
|
|
|
void (*fn)(int, const char*,const char*, dictionary);
|
|
|
|
struct plugin_entry *plugin ;
|
|
|
|
int plen = strlen(path);
|
|
|
|
char * rpath = (char*) malloc((plen+1)*sizeof(char));
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
LOG("Client %d\n",client );
|
|
|
|
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);
|
|
|
|
|
|
|
|
//load the plugin
|
|
|
|
if((plugin = plugin_lookup(pname)) == NULL)
|
|
|
|
if((plugin= plugin_load(pname)) == NULL)
|
|
|
|
return -1;
|
|
|
|
// load the function
|
|
|
|
fn = (void (*)(int, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER);
|
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
LOG("Problem when finding %s method from %s : %s \n", PLUGIN_HANDLER, pname,error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dictionary dic = decode_request(client,method,query_string);
|
|
|
|
(*fn)(client,method,pfunc,dic);
|
|
|
|
free(dic);
|
|
|
|
free(rpath);
|
|
|
|
return 1;
|
|
|
|
}
|