1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-05 22:19:47 +02:00

first working but buggy version

This commit is contained in:
lxsang 2018-10-04 19:47:31 +02:00
parent 3447b07fc6
commit 5477f54f60
5 changed files with 272 additions and 382 deletions

View File

@ -1,120 +1,121 @@
#include "http_server.h" #include "http_server.h"
static pthread_mutex_t server_mux = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t server_mux = PTHREAD_MUTEX_INITIALIZER;
/**********************************************************************/
/* 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(void* client)
{
char buf[1024];
int numchars;
char method[255];
char url[4096];
char path[1024];
char* token;
char *line;
char* oldurl = NULL;
char* tmp = NULL;
dictionary rq = NULL;
size_t i, j;
struct stat st;
//char *query_string = NULL; void* accept_request(void* client)
//LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock); {
numchars = read_buf(client, buf, sizeof(buf)); int count;
if(numchars <= 0) char buf[BUFFLEN];
antd_request_t* request;
antd_task_t* task;
char* token = NULL;
char* line = NULL;
request = (antd_request_t*)malloc(sizeof(*request));
request->client = client;
request->request = dict();
count = read_buf(client, buf, sizeof(buf));
task = antd_create_task(NULL,(void*)request,NULL);
task->priority++;
if(count <= 0)
{ {
unknow(client); unknow(client);
goto end; return task;
} }
i = 0; j = 0; line = buf;
while (j < numchars && !ISspace(buf[j]) && (i < sizeof(method) - 1)) // get the method string
token = strsep(&line," ");
if(!line)
{ {
method[i] = buf[j]; unknow(client);
i++; j++; return task;
} }
method[i] = '\0'; trim(token,' ');
if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) trim(line,' ');
dput(request->request, "METHOD", strdup(token));
// get the request
token = strsep(&line, " ");
if(!line)
{ {
LOG("METHOD NOT FOUND %s\n", method); unknow(client);
// unimplemented return task;
//while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf ); }
unimplemented(client); trim(token,' ');
//antd_close(client); trim(line,' ');
goto end; trim(line, '\n');
trim(line, '\r');
dput(request->request, "PROTOCOL", strdup(line));
dput(request->request, "REQUEST_QUERY", strdup(token));
line = token;
token = strsep(&line, "?");
dput(request->request, "REQUEST_PATH", strdup(token));
// decode request
// now return the task
task->handle = decode_request_header;
return task;
} }
void* resolve_request(void* data)
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]; struct stat st;
i++; j++; char path[2*BUFFLEN];
} antd_request_t* rq = (antd_request_t*) data;
url[i] = '\0'; antd_task_t* task = antd_create_task(NULL,(void*)rq,NULL);
task->priority++;
char* url = (char*)dvalue(rq->request, "RESOURCE_PATH");
char* newurl = NULL;
oldurl = strdup(url); char* rqp = (char*)dvalue(rq->request, "REQUEST_PATH");
tmp = strchr(oldurl,'?');
if(tmp)
*tmp = '\0';
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);
LOG("Path is : %s \n", path); LOG("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) {
if(execute_plugin(client,oldurl,method,rq) < 0) //if(execute_plugin(rq->client,rqp,method,rq) < 0)
not_found(client); // not_found(client);
LOG("execute plugin \n");
free(task);
return execute_plugin(rq, rqp);
} }
else else
{ {
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
{ {
int l = strlen(path);
int ul = strlen(url);
strcat(path, "/index.html"); strcat(path, "/index.html");
if(stat(path, &st) == -1) if(stat(path, &st) == -1)
{ {
association it; association it;
for_each_assoc(it, server_config.handlers) for_each_assoc(it, server_config.handlers)
{ {
path[l] = '\0'; newurl = __s("%s/index.%s", url, it->key);
url[ul] = '\0'; memset(path, 0, sizeof(path));
strcat(url,"/index."); strcat(path, server_config.htdocs);
strcat(path, "/index."); strcat(path, newurl);
strcat(url,it->key); if(stat(path, &st) != 0)
strcat(path, it->key); {
if(stat(path, &st) == 0) free(newurl);
newurl = NULL;
}
else
{ {
l = -1;
i = HASHSIZE;
break; break;
} }
} }
if(l!= -1) if(!newurl)
{ {
not_found(client); notfound(rq->client);
goto end; return task;
} }
if(url) free(url);
url = newurl;
dput(rq->request, "RESOURCE_PATH", url);
} }
} }
dput(rq->request, "ABS_RESOURCE_PATH", strdup(path));
// 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
// if the plugin is not found, forbidden access to the file should be sent // if the plugin is not found, forbidden access to the file should be sent
char* mime_type = mime(path); char* mime_type = mime(path);
dput(rq->request, "RESOURCE_MIME", strdup(mime_type));
if(strcmp(mime_type,"application/octet-stream") == 0) if(strcmp(mime_type,"application/octet-stream") == 0)
{ {
char * ex = ext(path); char * ex = ext(path);
@ -122,48 +123,35 @@ void accept_request(void* client)
if(ex) free(ex); if(ex) free(ex);
if(h) if(h)
{ {
sprintf(buf,"/%s%s",h,url); sprintf(path,"/%s%s",h,url);
LOG("WARNING::::Access octetstream via handler %s\n", buf); LOG("WARNING::::Access octetstream via handler %s\n", path);
if(execute_plugin(client,buf,method,rq) < 0) //if(execute_plugin(client,buf,method,rq) < 0)
cannot_execute(client); // cannot_execute(client);
free(task);
return execute_plugin(rq, path);
} }
else else
unknow(client); unknow(rq->client);
} }
else else
{ {
ctype(client,mime_type); task->type = HEAVY;
// if the mime is supported, send the file task->handle = serve_file;
serve_file(client, path);
//response(client,"this is the file");
} }
return task;
} }
end:
if(oldurl) free(oldurl);
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);
}
antd_close(client);
} }
void* finish_request(void* data) void* finish_request(void* data)
{ {
if(!data) return NULL;
LOG("Close request\n");
antd_request_t* rq = (antd_request_t*)data;
// free all other thing
if(rq->request) freedict(rq->request);
antd_close(rq->client);
free(rq);
return NULL; return NULL;
} }
@ -237,51 +225,6 @@ int rule_check(const char*k, const char* v, const char* host, const char* _url,
free(query); free(query);
return 1; return 1;
} }
/**********************************************************************/
/* 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 catb(void* client, FILE* ptr)
{
unsigned char buffer[BUFFLEN];
size_t size;
while(!feof(ptr))
{
size = fread(buffer,1,BUFFLEN,ptr);
__b(client,buffer,size);
//if(!__b(client,buffer,size)) return;
}
//fclose(ptr);
}
void cat(void* client, FILE *resource)
{
char buf[1024];
//fgets(buf, sizeof(buf), resource);
while (fgets(buf, sizeof(buf), resource) != NULL)
{
antd_send(client, buf, strlen(buf));
//fgets(buf, sizeof(buf), resource);
}
}
/**********************************************************************/
/* Inform the client that a CGI script could not be executed.
* Parameter: the client socket descriptor. */
/**********************************************************************/
void cannot_execute(void* client)
{
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.");
}
/**********************************************************************/ /**********************************************************************/
/* Print out an error message with perror() (for system errors; based /* Print out an error message with perror() (for system errors; based
* on value of errno, which indicates system call errors) and exit the * on value of errno, which indicates system call errors) and exit the
@ -292,99 +235,20 @@ void error_die(const char *sc)
perror(sc); perror(sc);
exit(1); exit(1);
} }
void* serve_file(void* data)
/**********************************************************************/
/* 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) */
/**********************************************************************/
//This function is deprecate
/*int get_line(int sock, char *buf, int size)
{ {
int i = 0; antd_request_t* rq = (antd_request_t*) data;
char c = '\0'; antd_task_t* task = antd_create_task(NULL,(void*)rq,NULL);
int n; task->priority++;
char* path = (char*)dvalue(rq->request, "ABS_RESOURCE_PATH");
while ((i < size - 1) && (c != '\n')) char* newurl = NULL;
{ char* mime_type = (char*)dvalue(rq->request, "RESOURCE_MIME");
n = recv(sock, &c, 1, 0); ctype(rq->client,mime_type);
if(is_bin(path))
if (n > 0) __fb(rq->client, path);
{
if (c == '\r')
{
n = recv(sock, &c, 1, MSG_PEEK);
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else else
c = '\n'; __f(rq->client, path);
} return task;
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0';
return(i);
}*/
/**********************************************************************/
/* Give a client a 404 not found status message. */
/**********************************************************************/
void not_found(void* client)
{
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>");
}
/**********************************************************************/
/* 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(void* client, const char *filename)
{
LOG("Serve file: %s\n", 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);
} }
/**********************************************************************/ /**********************************************************************/
@ -422,32 +286,6 @@ int startup(unsigned *port)
return(httpd); return(httpd);
} }
/**********************************************************************/
/* Inform the client that the requested web method has not been
* implemented.
* Parameter: the client socket */
/**********************************************************************/
void unimplemented(void* client)
{
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>");
}
void badrequest(void* client)
{
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.");
}
char* apply_rules(const char* host, char*url) char* apply_rules(const char* host, char*url)
{ {
association it; association it;
@ -491,32 +329,30 @@ char* apply_rules(const char* host, char*url)
* - if it is a POST request with URL encoded : decode the url encode * - 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 it is a POST request with multipart form data: de code the multipart
* - if other - UNIMPLEMENTED * - if other - UNIMPLEMENTED
* @param client socket client * @param an antd_request_t structure
* @param method HTTP method * @return a task
* @param query query string in case of GET
* @return a dictionary of key- value
*/ */
dictionary decode_request(void* client,const char* method, char* url)
void* decode_request_header(void* data)
{ {
dictionary request = dict(); antd_request_t* rq = (antd_request_t*) data;
dictionary cookie = NULL; dictionary cookie = NULL;
dictionary xheader = dict();
char* line; char* line;
char * token; char * token;
char* query = NULL; char* query = NULL;
char* ctype = NULL;
char* host = NULL; char* host = NULL;
int clen = -1; char buf[2*BUFFLEN];
char* url = (char*)dvalue(rq->request, "REQUEST_QUERY");
dictionary xheader = dict();
dictionary request = dict();
dput(rq->request,"REQUEST_HEADER",xheader);
dput(rq->request,"REQUEST_DATA",request);
// first real all header // first real all header
// this for check if web socket is enabled // this for check if web socket is enabled
int ws= 0;
char* ws_key = NULL;
char buf[BUFFLEN];
// ip address // ip address
dput(xheader,"REMOTE_ADDR", (void*)strdup(((antd_client_t*)client)->ip )); dput(xheader,"REMOTE_ADDR", (void*)strdup(((antd_client_t*)rq->client)->ip ));
//while((line = read_line(client)) && strcmp("\r\n",line)) //while((line = read_line(client)) && strcmp("\r\n",line))
while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf)) while((read_buf(rq->client,buf,sizeof(buf))) && strcmp("\r\n",buf))
{ {
line = buf; line = buf;
trim(line, '\n'); trim(line, '\n');
@ -530,84 +366,113 @@ dictionary decode_request(void* 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) else if(token != NULL && strcasecmp(token,"Host") == 0)
{
ctype = strdup(line); //strsep(&line,":");
trim(ctype,' ');
} else if(token != NULL &&strcasecmp(token,"Content-Length") == 0)
{
token = line; //strsep(&line,":");
trim(token,' ');
clen = atoi(token);
}
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)
{ {
host = strdup(line); 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);
}
} }
//if(line) free(line); //if(line) free(line);
query = apply_rules(host, url); memset(buf, 0, sizeof(buf));
strcat(buf,url);
query = apply_rules(host, buf);
LOG("BUGFGGGG is %s\n", buf);
dput(rq->request,"RESOURCE_PATH",strdup(buf));
if(query) if(query)
{ {
LOG("Query: %s\n", query); LOG("Query: %s\n", query);
decode_url_request(query, request); decode_url_request(query, request);
free(query); free(query);
} }
if(cookie)
dput(request,"cookie",cookie);
if(host) free(host); if(host) free(host);
if(strcmp(method,"GET") == 0) // header ok, now checkmethod
antd_task_t* task = antd_create_task(decode_request,(void*)rq, NULL);
task->priority++;
return task;
}
void* decode_request(void* data)
{
antd_request_t* rq = (antd_request_t*) data;
dictionary request = dvalue(rq->request, "REQUEST_DATA");
dictionary headers = dvalue(rq->request, "REQUEST_HEADER");
int ws = 0;
char*ws_key = NULL;
char* method = NULL;
char* tmp;
antd_task_t* task = NULL;
ws_key = (char*) dvalue(headers, "Sec-WebSocket-Key");
tmp = (char*)dvalue(headers, "Upgrade");
if(tmp && strcasecmp(tmp, "websocket") == 0) ws = 1;
method = (char*) dvalue(rq->request, "METHOD");
task = antd_create_task(NULL,(void*)rq, NULL);
task->priority++;
if(strcmp(method,"GET") == 0 || strcmp(method,"PUT") == 0)
{ {
//if(ctype) free(ctype); //if(ctype) free(ctype);
if(ws && ws_key != NULL) if(ws && ws_key != NULL)
{ {
ws_confirm_request(client, ws_key); ws_confirm_request(rq->client, ws_key);
free(ws_key); free(ws_key);
// insert wsocket flag to request // insert wsocket flag to request
// plugin should handle this ugraded connection // plugin should handle this ugraded connection
// not the server // not the server
//if(!request) request = dict();
dput(request,"__web_socket__",strdup("1")); dput(request,"__web_socket__",strdup("1"));
} }
// resolve task
task->handle = resolve_request;
return task;
}
else if(strcmp(method,"POST") == 0)
{
task->handle = decode_post_request;
task->type = HEAVY;
return task;
} }
else else
{ {
if(ws_key) unimplemented(rq->client);
free(ws_key); return task;
}
}
void* decode_post_request(void* data)
{
antd_request_t* rq = (antd_request_t*) data;
dictionary request = dvalue(rq->request, "REQUEST_DATA");
dictionary headers = dvalue(rq->request, "REQUEST_HEADER");
char* ctype = NULL;
int clen = -1;
char* tmp;
antd_task_t* task = NULL;
ctype = (char*) dvalue(headers, "Content-Type");
tmp = (char*)dvalue(headers, "Content-Length");
if(tmp)
clen = atoi(tmp);
task = antd_create_task(NULL,(void*)rq, NULL);
if(ctype == NULL || clen == -1) if(ctype == NULL || clen == -1)
{ {
LOG("Bad request\n"); LOG("Bad request\n");
if(ctype) free(ctype); badrequest(rq->client);
if(cookie) freedict(cookie); return task;
freedict(request);
freedict(xheader);
return NULL;
} }
LOG("ContentType %s\n", ctype); LOG("ContentType %s\n", ctype);
// decide what to do with the data // decide what to do with the data
if(strstr(ctype,FORM_URL_ENCODE) > 0) if(strstr(ctype,FORM_URL_ENCODE) > 0)
{ {
char* pquery = post_data_decode(client,clen); char* pquery = post_data_decode(rq->client,clen);
decode_url_request(pquery, request); decode_url_request(pquery, request);
free(pquery); free(pquery);
} else if(strstr(ctype,FORM_MULTI_PART)> 0) } else if(strstr(ctype,FORM_MULTI_PART)> 0)
{ {
//printf("Multi part form : %s\n", ctype); //printf("Multi part form : %s\n", ctype);
decode_multi_part_request(client,ctype,request); // TODO: split this to multiple task
decode_multi_part_request(rq->client,ctype,request);
} }
else else
{ {
char* pquery = post_data_decode(client,clen); char* pquery = post_data_decode(rq->client,clen);
char* key = strstr(ctype,"/"); char* key = strstr(ctype,"/");
if(key) if(key)
key++; key++;
@ -616,23 +481,10 @@ dictionary decode_request(void* client,const char* method, char* url)
dput(request,key, strdup(pquery)); dput(request,key, strdup(pquery));
free(pquery); free(pquery);
} }
task->handle = resolve_request;
return task;
} }
if(ctype) free(ctype);
//if(cookie->key == NULL) {free(cookie);cookie= NULL;}
//if(!request)
// request = dict();
if(cookie)
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 * Send header to the client to confirm
* that the websocket is accepted by * that the websocket is accepted by
@ -944,11 +796,11 @@ char* post_data_decode(void* client,int len)
* @return -1 if failure * @return -1 if failure
* 1 if sucess * 1 if sucess
*/ */
int execute_plugin(void* client, const char *path, const char *method, dictionary dic) void* execute_plugin(void* data, const char *path)
{ {
char pname[255]; char pname[255];
char pfunc[255]; char pfunc[255];
void (*fn)(void*, const char*,const char*, dictionary); void* (*fn)(void*);
struct plugin_entry *plugin ; struct plugin_entry *plugin ;
int plen = strlen(path); int plen = strlen(path);
char * rpath = (char*) malloc((plen+1)*sizeof(char)); char * rpath = (char*) malloc((plen+1)*sizeof(char));
@ -958,6 +810,9 @@ int execute_plugin(void* client, const char *path, const char *method, dictionar
rpath[plen] = '\0'; rpath[plen] = '\0';
trim(rpath,'/'); trim(rpath,'/');
char * delim = strchr(rpath,'/'); char * delim = strchr(rpath,'/');
antd_request_t* rq = (antd_request_t*) data;
antd_task_t* task = antd_create_task(NULL, (void*)rq, NULL);
task->priority++;
if(delim == NULL) if(delim == NULL)
{ {
strcpy(pname,rpath); strcpy(pname,rpath);
@ -973,9 +828,8 @@ int execute_plugin(void* client, const char *path, const char *method, dictionar
memcpy(pfunc,rpath+npos+1,fpos); memcpy(pfunc,rpath+npos+1,fpos);
pfunc[fpos-1]='\0'; pfunc[fpos-1]='\0';
} }
LOG("Client %d\n",((antd_client_t*)client)->sock ); LOG("Client %d\n",((antd_client_t*)rq->client)->sock );
LOG("Path : '%s'\n", rpath); LOG("Path : '%s'\n", rpath);
LOG("Method:%s\n", method);
LOG("Plugin name '%s'\n",pname); LOG("Plugin name '%s'\n",pname);
LOG("Query path. '%s'\n", pfunc); LOG("Query path. '%s'\n", pfunc);
//LOG("query :%s\n", query_string); //LOG("query :%s\n", query_string);
@ -989,22 +843,23 @@ int execute_plugin(void* client, const char *path, const char *method, dictionar
if( plugin == NULL) if( plugin == NULL)
{ {
if(orgs) free(orgs); if(orgs) free(orgs);
return -1; unknow(rq->client);
return task;
} }
} }
// load the function // load the function
fn = (void (*)(void*, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER); fn = (void* (*)(void*))dlsym(plugin->handle, PLUGIN_HANDLER);
if ((error = dlerror()) != NULL) if ((error = dlerror()) != NULL)
{ {
if(orgs) free(orgs); if(orgs) free(orgs);
LOG("Problem when finding %s method from %s : %s \n", PLUGIN_HANDLER, pname,error); LOG("Problem when finding %s method from %s : %s \n", PLUGIN_HANDLER, pname,error);
return -1; unknow(rq->client);
return task;
} }
//dictionary dic = decode_request(client,method,query_string); task->type = HEAVY;
(*fn)(client,method,pfunc,dic); task->handle = fn;
//free(dic);
free(orgs); free(orgs);
return 1; return task;
} }
#ifdef USE_OPENSSL #ifdef USE_OPENSSL

View File

@ -8,6 +8,7 @@
#include <signal.h> #include <signal.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "libs/handle.h" #include "libs/handle.h"
#include "libs/scheduler.h"
#include "plugin_manager.h" #include "plugin_manager.h"
#define FORM_URL_ENCODE "application/x-www-form-urlencoded" #define FORM_URL_ENCODE "application/x-www-form-urlencoded"
@ -15,22 +16,19 @@
#define PLUGIN_HANDLER "handle" #define PLUGIN_HANDLER "handle"
#define WS_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" #define WS_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
#define ISspace(x) isspace((int)(x))
#define SERVER_STRING "Server: ant-httpd" #define SERVER_STRING "Server: ant-httpd"
#define CONFIG "config.ini" #define CONFIG "config.ini"
extern config_t server_config; extern config_t server_config;
void accept_request(void*); void* accept_request(void*);
void* finish_request(void*); void* finish_request(void*);
void cat(void*, FILE *); void cat(void*, FILE *);
void cannot_execute(void*); void cannot_execute(void*);
void error_die(const char *); void error_die(const char *);
//int get_line(int, char *, int); //int get_line(int, char *, int);
void not_found(void*); void not_found(void*);
void serve_file(void*, const char *); void* serve_file(void*);
int startup(unsigned *); int startup(unsigned *);
void unimplemented(void*); void unimplemented(void*);
void badrequest(void*); void badrequest(void*);
@ -38,12 +36,14 @@ int rule_check(const char*, const char*, const char* , const char* , const char*
void ws_confirm_request(void*, const char*); void ws_confirm_request(void*, const char*);
char* post_url_decode(void* client,int len); char* post_url_decode(void* client,int len);
void decode_url_request(const char* query, dictionary); void decode_url_request(const char* query, dictionary);
dictionary decode_request(void* client,const char* method, char* url); void* decode_request_header(void* data);
void* decode_request(void* data);
void* decode_post_request(void* data);
void* resolve_request(void* data);
void decode_multi_part_request(void*,const char*, dictionary); void decode_multi_part_request(void*,const char*, dictionary);
dictionary decode_cookie(const char*); dictionary decode_cookie(const char*);
char* post_data_decode(void*,int); char* post_data_decode(void*,int);
int execute_plugin(void* client, const char *path, void* execute_plugin(void* data, const char *path);
const char *method, dictionary rq);
#endif #endif

View File

@ -2,7 +2,6 @@
#include <dirent.h> #include <dirent.h>
#include "http_server.h" #include "http_server.h"
#include "libs/ini.h" #include "libs/ini.h"
#include "libs/scheduler.h"
#include <fcntl.h> #include <fcntl.h>
static antd_scheduler_t scheduler; static antd_scheduler_t scheduler;
@ -179,11 +178,12 @@ void load_config(const char* file)
} }
void stop_serve(int dummy) { void stop_serve(int dummy) {
UNUSED(dummy); UNUSED(dummy);
LOG("Shuting down server \n");
antd_scheduler_destroy(&scheduler);
list_free(&(server_config.rules)); list_free(&(server_config.rules));
freedict(server_config.handlers); freedict(server_config.handlers);
LOG("Unclosed connection: %d\n", server_config.connection); LOG("Unclosed connection: %d\n", server_config.connection);
unload_all_plugin(); unload_all_plugin();
antd_scheduler_destroy(&scheduler);
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
SSL_CTX_free(ctx); SSL_CTX_free(ctx);
#endif #endif
@ -200,7 +200,6 @@ int main(int argc, char* argv[])
int client_sock = -1; int client_sock = -1;
struct sockaddr_in client_name; struct sockaddr_in client_name;
socklen_t client_name_len = sizeof(client_name); socklen_t client_name_len = sizeof(client_name);
pthread_t newthread;
char* client_ip = NULL; char* client_ip = NULL;
// ignore the broken PIPE error when writing // ignore the broken PIPE error when writing
//or reading to/from a closed socked connection //or reading to/from a closed socked connection

View File

@ -300,8 +300,41 @@ void clear_cookie(void* client, dictionary dic)
} }
void unknow(void* client) void unknow(void* client)
{ {
html(client); set_status(client,520,"Unknown Error");
__t(client,"404 API not found"); __t(client,"Content-Type: text/html; charset=utf-8");
response(client,"");
__t(client,"520 Unknow request");
}
void notfound(void* client)
{
set_status(client,404,"Not found");
__t(client,"Content-Type: text/html; charset=utf-8");
response(client,"");
__t(client,"Resource not found");
}
void badrequest(void* client)
{
set_status(client,400,"Bad request");
__t(client,"Content-Type: text/html; charset=utf-8");
response(client,"");
__t(client,"400 Bad request");
}
void unimplemented(void* client)
{
set_status(client,501,"Method Not Implemented");
__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>");
}
void cannot_execute(void* client)
{
set_status(client,500,"Internal Server Error");
__t(client,"Content-Type: text/html");
response(client,"");
__t(client, "<P>Error prohibited CGI execution.");
} }
int ws_enable(dictionary dic) int ws_enable(dictionary dic)
{ {

View File

@ -80,6 +80,9 @@ void set_status(void*,int,const char*);
void clear_cookie(void*, dictionary); void clear_cookie(void*, dictionary);
/*Default function for plugin*/ /*Default function for plugin*/
void unknow(void*); void unknow(void*);
void badrequest(void* client);
void unimplemented(void* client);
void notfound(void* client);
int ws_enable(dictionary); int ws_enable(dictionary);
char* read_line(void* sock); char* read_line(void* sock);
int read_buf(void* sock,char* buf,int i); int read_buf(void* sock,char* buf,int i);