add configurable file handler

This commit is contained in:
Xuan Sang LE 2018-02-20 19:02:31 +01:00
parent 6847a1a141
commit 957a9f719f
3 changed files with 41 additions and 6 deletions

View File

@ -73,11 +73,32 @@ void accept_request(void* client)
{ {
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)
{ {
not_found(client); association it;
goto end; 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;
}
} }
} }
// check if the mime is supported // check if the mime is supported
@ -87,10 +108,17 @@ void accept_request(void* client)
char* mime_type = mime(path); char* mime_type = mime(path);
if(strcmp(mime_type,"application/octet-stream") == 0) if(strcmp(mime_type,"application/octet-stream") == 0)
{ {
sprintf(buf,"/%s-api%s",ext(path),url); char* h = dvalue(server_config.handlers,ext(path));
LOG("WARNING::::Access octetstream via handler %s\n", buf); if(h)
if(execute_plugin(client,buf,method,rq) < 0) {
cannot_execute(client); 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);
} }
else else
{ {

View File

@ -99,6 +99,10 @@ static int config_handler(void* conf, const char* section, const char* name,
else if (strcmp(section, "RULES") == 0) else if (strcmp(section, "RULES") == 0)
{ {
dput( pconfig->rules, strdup(name),strdup(value)); dput( pconfig->rules, strdup(name),strdup(value));
}
else if (strcmp(section, "FILEHANDLER") == 0)
{
dput( pconfig->handlers, strdup(name),strdup(value));
} }
else if(strcmp(section,"AUTOSTART")==0){ else if(strcmp(section,"AUTOSTART")==0){
// The server section must be added before the autostart section // The server section must be added before the autostart section
@ -136,6 +140,7 @@ void load_config(const char* file)
server_config.tmpdir = "tmp"; server_config.tmpdir = "tmp";
server_config.backlog = 100; server_config.backlog = 100;
server_config.rules = dict(); server_config.rules = dict();
server_config.handlers = dict();
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
server_config.usessl = 0; server_config.usessl = 0;
server_config.sslcert = "cert.pem"; server_config.sslcert = "cert.pem";
@ -157,6 +162,7 @@ void load_config(const char* file)
} }
void stop_serve(int dummy) { void stop_serve(int dummy) {
free(server_config.rules); free(server_config.rules);
free(server_config.handlers);
unload_all_plugin(); unload_all_plugin();
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])

View File

@ -37,6 +37,7 @@ typedef struct {
char* htdocs; char* htdocs;
char* tmpdir; char* tmpdir;
dictionary rules; dictionary rules;
dictionary handlers;
int backlog; int backlog;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
int usessl; int usessl;