mirror of
https://github.com/lxsang/ant-http
synced 2025-02-23 00:22:47 +01:00
bug every where
This commit is contained in:
parent
8a6dcb0793
commit
aba40b16f5
25
Makefile
25
Makefile
@ -1,4 +1,5 @@
|
|||||||
USE_DB=TRUE
|
USE_DB=TRUE
|
||||||
|
USE_SSL = TRUE
|
||||||
CC=gcc
|
CC=gcc
|
||||||
EXT=dylib
|
EXT=dylib
|
||||||
|
|
||||||
@ -10,6 +11,8 @@ endif
|
|||||||
ifeq ($(UNAME_S),Darwin)
|
ifeq ($(UNAME_S),Darwin)
|
||||||
BUILDIRD=../ant-build
|
BUILDIRD=../ant-build
|
||||||
PF_FLAG= -DMACOS
|
PF_FLAG= -DMACOS
|
||||||
|
SSL_HEADER_PATH = -I/usr/local/opt/openssl/include
|
||||||
|
SSL_LIB_PATH = -L/usr/local/opt/openssl/lib
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(USE_DB),TRUE)
|
ifeq ($(USE_DB),TRUE)
|
||||||
@ -24,13 +27,25 @@ ifeq ($(USE_DB),FALSE)
|
|||||||
DB_FLAG=
|
DB_FLAG=
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(USE_SSL),TRUE)
|
||||||
|
SSL_LIB= $(SSL_LIB_PATH) -lssl -lcrypto
|
||||||
|
SSL_FLAG=-D USE_OPENSSL
|
||||||
|
endif
|
||||||
|
|
||||||
CFLAGS= -W -Wall -g -std=c99 -D DEBUG $(DB_FLAG) $(PF_FLAG)
|
ifeq ($(USE_SSL),FALSE)
|
||||||
|
SSL_LIB=
|
||||||
|
SSL_FLAG=
|
||||||
|
SSL_HEADER_PATH =
|
||||||
|
SSL_LIB_PATH =
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
CFLAGS= -W -Wall -g -std=c99 -D DEBUG $(DB_FLAG) $(PF_FLAG) $(SSL_FLAG) $(SSL_HEADER_PATH)
|
||||||
|
|
||||||
LIB_PATH=$(BUILDIRD)/plugins
|
LIB_PATH=$(BUILDIRD)/plugins
|
||||||
LIB_NAME=libantd
|
LIB_NAME=libantd
|
||||||
LIB_FLAG= $(LIB_NAME).$(EXT)
|
LIB_FLAG= $(LIB_NAME).$(EXT)
|
||||||
SERVERLIB= -ldl $(LIB_FLAG) $(DB_LIB) -l pthread
|
SERVERLIB= -ldl $(LIB_FLAG) $(DB_LIB) $(SSL_LIB) -l pthread
|
||||||
|
|
||||||
SERVER_O=plugin_manager.o \
|
SERVER_O=plugin_manager.o \
|
||||||
http_server.o
|
http_server.o
|
||||||
@ -58,7 +73,7 @@ httpd: lib $(SERVER_O)
|
|||||||
cp antd $(BUILDIRD)
|
cp antd $(BUILDIRD)
|
||||||
|
|
||||||
lib: $(LIBOBJS)
|
lib: $(LIBOBJS)
|
||||||
$(CC) $(CFLAGS) $(DB_LIB) -shared -o $(LIB_NAME).$(EXT) $(LIBOBJS)
|
$(CC) $(CFLAGS) $(DB_LIB) $(SSL_LIB) -shared -o $(LIB_NAME).$(EXT) $(LIBOBJS)
|
||||||
cp $(LIB_NAME).$(EXT) $(LIB_PATH$)/
|
cp $(LIB_NAME).$(EXT) $(LIB_PATH$)/
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
$(CC) -fPIC $(CFLAGS) -c $< -o $@
|
$(CC) -fPIC $(CFLAGS) -c $< -o $@
|
||||||
@ -79,8 +94,8 @@ sclean:
|
|||||||
-rm -f *.o $(BUILDIRD)/httpd
|
-rm -f *.o $(BUILDIRD)/httpd
|
||||||
-rm *.$(EXT)
|
-rm *.$(EXT)
|
||||||
pclean:
|
pclean:
|
||||||
-rm -rf $(BUILDIRD)/plugins/* plugins/*.o
|
-rm -rf $(BUILDIRD)/plugins/* libs/*.o
|
||||||
-for file in plugins/* ;do \
|
-for file in libs/* ;do \
|
||||||
if [ -d "$$file" ]; then \
|
if [ -d "$$file" ]; then \
|
||||||
rm "$$file"/*.o; \
|
rm "$$file"/*.o; \
|
||||||
fi \
|
fi \
|
||||||
|
15
config.h
15
config.h
@ -1,15 +0,0 @@
|
|||||||
#define CONFIG "config.ini"
|
|
||||||
#include "libs/dictionary.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int port;
|
|
||||||
char *plugins_dir;
|
|
||||||
char *plugins_ext;
|
|
||||||
char *db_path;
|
|
||||||
char* htdocs;
|
|
||||||
char* tmpdir;
|
|
||||||
dictionary rules;
|
|
||||||
int backlog;
|
|
||||||
}config_t;
|
|
||||||
|
|
||||||
extern config_t server_config;
|
|
@ -4,12 +4,12 @@
|
|||||||
* return. Process the request appropriately.
|
* return. Process the request appropriately.
|
||||||
* Parameters: the socket connected to the client */
|
* Parameters: the socket connected to the client */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
void accept_request(int client)
|
void accept_request(void* client)
|
||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
int numchars;
|
int numchars;
|
||||||
char method[255];
|
char method[255];
|
||||||
char url[4096];
|
char url[4096];
|
||||||
char path[1024];
|
char path[1024];
|
||||||
char* token;
|
char* token;
|
||||||
char *line;
|
char *line;
|
||||||
@ -17,8 +17,9 @@ void accept_request(int client)
|
|||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
//char *query_string = NULL;
|
//char *query_string = NULL;
|
||||||
|
LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock);
|
||||||
numchars = get_line(client, buf, sizeof(buf));
|
numchars = get_line(((antd_client_t*)client)->sock, buf, sizeof(buf));
|
||||||
|
printf("BUF: %s\n", buf);
|
||||||
i = 0; j = 0;
|
i = 0; j = 0;
|
||||||
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
|
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
|
||||||
{
|
{
|
||||||
@ -32,7 +33,7 @@ void accept_request(int client)
|
|||||||
// unimplemented
|
// unimplemented
|
||||||
//while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
|
//while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
|
||||||
unimplemented(client);
|
unimplemented(client);
|
||||||
close(client);
|
antd_close(client);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +104,7 @@ void accept_request(int client)
|
|||||||
end:
|
end:
|
||||||
if(oldurl) free(oldurl);
|
if(oldurl) free(oldurl);
|
||||||
if(rq) free(rq);
|
if(rq) free(rq);
|
||||||
close(client);
|
antd_close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rule_check(association it, const char* host, const char* _url, const char* _query, char* buf)
|
void rule_check(association it, const char* host, const char* _url, const char* _query, char* buf)
|
||||||
@ -168,7 +169,7 @@ void rule_check(association it, const char* host, const char* _url, const char*
|
|||||||
* Parameters: the client socket descriptor
|
* Parameters: the client socket descriptor
|
||||||
* FILE pointer for the file to cat */
|
* FILE pointer for the file to cat */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
void catb(int client, FILE* ptr)
|
void catb(void* client, FILE* ptr)
|
||||||
{
|
{
|
||||||
unsigned char buffer[BUFFLEN];
|
unsigned char buffer[BUFFLEN];
|
||||||
size_t size;
|
size_t size;
|
||||||
@ -180,14 +181,17 @@ void catb(int client, FILE* ptr)
|
|||||||
}
|
}
|
||||||
//fclose(ptr);
|
//fclose(ptr);
|
||||||
}
|
}
|
||||||
void cat(int client, FILE *resource)
|
void cat(void* client, FILE *resource)
|
||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
//fgets(buf, sizeof(buf), resource);
|
//fgets(buf, sizeof(buf), resource);
|
||||||
while (fgets(buf, sizeof(buf), resource) != NULL)
|
while (fgets(buf, sizeof(buf), resource) != NULL)
|
||||||
{
|
{
|
||||||
send(client, buf, strlen(buf), 0);
|
antd_send(client, buf, strlen(buf), _ssl);
|
||||||
//fgets(buf, sizeof(buf), resource);
|
//fgets(buf, sizeof(buf), resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,7 +202,7 @@ void cat(int client, FILE *resource)
|
|||||||
/* Inform the client that a CGI script could not be executed.
|
/* Inform the client that a CGI script could not be executed.
|
||||||
* Parameter: the client socket descriptor. */
|
* Parameter: the client socket descriptor. */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
void cannot_execute(int client)
|
void cannot_execute(void* client)
|
||||||
{
|
{
|
||||||
set_status(client,500,"Internal Server Error");
|
set_status(client,500,"Internal Server Error");
|
||||||
__t(client,SERVER_STRING);
|
__t(client,SERVER_STRING);
|
||||||
@ -231,6 +235,7 @@ void error_die(const char *sc)
|
|||||||
* the size of the buffer
|
* the size of the buffer
|
||||||
* Returns: the number of bytes stored (excluding null) */
|
* Returns: the number of bytes stored (excluding null) */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
|
//This function is deprecate
|
||||||
int get_line(int sock, char *buf, int size)
|
int get_line(int sock, char *buf, int size)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -240,13 +245,13 @@ int get_line(int sock, char *buf, int size)
|
|||||||
while ((i < size - 1) && (c != '\n'))
|
while ((i < size - 1) && (c != '\n'))
|
||||||
{
|
{
|
||||||
n = recv(sock, &c, 1, 0);
|
n = recv(sock, &c, 1, 0);
|
||||||
/* DEBUG printf("%02X\n", c); */
|
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
{
|
{
|
||||||
if (c == '\r')
|
if (c == '\r')
|
||||||
{
|
{
|
||||||
n = recv(sock, &c, 1, MSG_PEEK);
|
n = recv(sock, &c, 1, MSG_PEEK);
|
||||||
/* DEBUG printf("%02X\n", c); */
|
|
||||||
if ((n > 0) && (c == '\n'))
|
if ((n > 0) && (c == '\n'))
|
||||||
recv(sock, &c, 1, 0);
|
recv(sock, &c, 1, 0);
|
||||||
else
|
else
|
||||||
@ -267,7 +272,7 @@ int get_line(int sock, char *buf, int size)
|
|||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
/* Give a client a 404 not found status message. */
|
/* Give a client a 404 not found status message. */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
void not_found(int client)
|
void not_found(void* client)
|
||||||
{
|
{
|
||||||
set_status(client,404,"NOT FOUND");
|
set_status(client,404,"NOT FOUND");
|
||||||
__t(client,SERVER_STRING);
|
__t(client,SERVER_STRING);
|
||||||
@ -287,8 +292,9 @@ void not_found(int client)
|
|||||||
* file descriptor
|
* file descriptor
|
||||||
* the name of the file to serve */
|
* the name of the file to serve */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
void serve_file(int client, const char *filename)
|
void serve_file(void* client, const char *filename)
|
||||||
{
|
{
|
||||||
|
LOG("Serve file: %s\n", filename);
|
||||||
FILE *resource = NULL;
|
FILE *resource = NULL;
|
||||||
int numchars = 1;
|
int numchars = 1;
|
||||||
//char buf[1024];
|
//char buf[1024];
|
||||||
@ -350,7 +356,7 @@ int startup(unsigned *port)
|
|||||||
* implemented.
|
* implemented.
|
||||||
* Parameter: the client socket */
|
* Parameter: the client socket */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
void unimplemented(int client)
|
void unimplemented(void* client)
|
||||||
{
|
{
|
||||||
set_status(client,501,"Method Not Implemented");
|
set_status(client,501,"Method Not Implemented");
|
||||||
__t(client,SERVER_STRING);
|
__t(client,SERVER_STRING);
|
||||||
@ -362,7 +368,7 @@ void unimplemented(int client)
|
|||||||
__t(client, "</BODY></HTML>");
|
__t(client, "</BODY></HTML>");
|
||||||
}
|
}
|
||||||
|
|
||||||
void badrequest(int client)
|
void badrequest(void* client)
|
||||||
{
|
{
|
||||||
set_status(client,400,"Bad Request");
|
set_status(client,400,"Bad Request");
|
||||||
__t(client,SERVER_STRING);
|
__t(client,SERVER_STRING);
|
||||||
@ -378,11 +384,15 @@ void badrequest(int client)
|
|||||||
* @param len content length
|
* @param len content length
|
||||||
* @return query string
|
* @return query string
|
||||||
*/
|
*/
|
||||||
char* post_url_decode(int client,int len)
|
char* post_url_decode(void* client,int len)
|
||||||
{
|
{
|
||||||
char *query = (char*) malloc((len+1)*sizeof(char));
|
char *query = (char*) malloc((len+1)*sizeof(char));
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
recv(client, (query+i), 1, 0);
|
#ifdef USE_OPENSSL
|
||||||
|
antd_recv(client, (query+i), 1, server_config.usessl);
|
||||||
|
#else
|
||||||
|
antd_recv(client, (query+i), 1, 0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
query[len]='\0';
|
query[len]='\0';
|
||||||
//query = url_decode(query);
|
//query = url_decode(query);
|
||||||
@ -430,7 +440,7 @@ char* apply_rules(const char* host, char*url)
|
|||||||
* @param query query string in case of GET
|
* @param query query string in case of GET
|
||||||
* @return a dictionary of key- value
|
* @return a dictionary of key- value
|
||||||
*/
|
*/
|
||||||
dictionary decode_request(int client,const char* method, char* url)
|
dictionary decode_request(void* client,const char* method, char* url)
|
||||||
{
|
{
|
||||||
dictionary request = NULL;
|
dictionary request = NULL;
|
||||||
dictionary cookie = NULL;
|
dictionary cookie = NULL;
|
||||||
@ -452,6 +462,7 @@ dictionary decode_request(int client,const char* method, char* url)
|
|||||||
while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf))
|
while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf))
|
||||||
{
|
{
|
||||||
line = buf;
|
line = buf;
|
||||||
|
printf("LINE1: %s \n", line);
|
||||||
trim(line, '\n');
|
trim(line, '\n');
|
||||||
trim(line, '\r');
|
trim(line, '\r');
|
||||||
token = strsep(&line,":");
|
token = strsep(&line,":");
|
||||||
@ -563,7 +574,7 @@ void __px(const char* data,int size)
|
|||||||
* that the websocket is accepted by
|
* that the websocket is accepted by
|
||||||
* our server
|
* our server
|
||||||
*/
|
*/
|
||||||
void ws_confirm_request(int client, const char* key)
|
void ws_confirm_request(void* client, const char* key)
|
||||||
{
|
{
|
||||||
char buf[256];
|
char buf[256];
|
||||||
char rkey[128];
|
char rkey[128];
|
||||||
@ -572,23 +583,30 @@ void ws_confirm_request(int client, const char* key)
|
|||||||
strcpy(rkey,key);
|
strcpy(rkey,key);
|
||||||
strcat(rkey,WS_MAGIC_STRING);
|
strcat(rkey,WS_MAGIC_STRING);
|
||||||
//printf("RESPONDKEY '%s'\n", rkey);
|
//printf("RESPONDKEY '%s'\n", rkey);
|
||||||
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
SHA_CTX context;
|
||||||
|
#else
|
||||||
SHA1_CTX context;
|
SHA1_CTX context;
|
||||||
|
#endif
|
||||||
|
|
||||||
SHA1_Init(&context);
|
SHA1_Init(&context);
|
||||||
SHA1_Update(&context, rkey, strlen(rkey));
|
SHA1_Update(&context, rkey, strlen(rkey));
|
||||||
SHA1_Final(&context, sha_d);
|
SHA1_Final(sha_d, &context);
|
||||||
Base64encode(base64, sha_d, 20);
|
Base64encode(base64, sha_d, 20);
|
||||||
//printf("Base 64 '%s'\n", base64);
|
//printf("Base 64 '%s'\n", base64);
|
||||||
// send accept to client
|
// send accept to client
|
||||||
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
|
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
|
||||||
send(client, buf, strlen(buf), 0);
|
antd_send(client, buf, strlen(buf), _ssl);
|
||||||
sprintf(buf, "Upgrade: websocket\r\n");
|
sprintf(buf, "Upgrade: websocket\r\n");
|
||||||
send(client, buf, strlen(buf), 0);
|
antd_send(client, buf, strlen(buf), _ssl);
|
||||||
sprintf(buf, "Connection: Upgrade\r\n");
|
sprintf(buf, "Connection: Upgrade\r\n");
|
||||||
send(client, buf, strlen(buf), 0);
|
antd_send(client, buf, strlen(buf), _ssl);
|
||||||
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
|
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
|
||||||
send(client, buf, strlen(buf), 0);
|
antd_send(client, buf, strlen(buf), _ssl);
|
||||||
sprintf(buf, "\r\n");
|
sprintf(buf, "\r\n");
|
||||||
send(client, buf, strlen(buf), 0);
|
antd_send(client, buf, strlen(buf), _ssl);
|
||||||
|
|
||||||
LOG("%s\n", "Websocket is now enabled for plugin");
|
LOG("%s\n", "Websocket is now enabled for plugin");
|
||||||
}
|
}
|
||||||
@ -631,7 +649,7 @@ dictionary decode_cookie(const char* line)
|
|||||||
* @param clen Content length, but not used here
|
* @param clen Content length, but not used here
|
||||||
* @return a dictionary of key - value
|
* @return a dictionary of key - value
|
||||||
*/
|
*/
|
||||||
dictionary decode_multi_part_request(int client,const char* ctype)
|
dictionary decode_multi_part_request(void* client,const char* ctype)
|
||||||
{
|
{
|
||||||
char * boundary;
|
char * boundary;
|
||||||
char * boundend;
|
char * boundend;
|
||||||
@ -791,11 +809,15 @@ dictionary decode_url_request(const char* query)
|
|||||||
/**
|
/**
|
||||||
* Decode JSON query string to string
|
* Decode JSON query string to string
|
||||||
*/
|
*/
|
||||||
char* json_data_decode(int client,int len)
|
char* json_data_decode(void* client,int len)
|
||||||
{
|
{
|
||||||
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
char *query = (char*) malloc((len+1)*sizeof(char));
|
char *query = (char*) malloc((len+1)*sizeof(char));
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
recv(client, (query+i), 1, 0);
|
antd_recv(client, (query+i), 1, _ssl);
|
||||||
}
|
}
|
||||||
query[len]='\0';
|
query[len]='\0';
|
||||||
//query = url_decode(query);
|
//query = url_decode(query);
|
||||||
@ -817,11 +839,11 @@ char* json_data_decode(int client,int len)
|
|||||||
* @return -1 if failure
|
* @return -1 if failure
|
||||||
* 1 if sucess
|
* 1 if sucess
|
||||||
*/
|
*/
|
||||||
int execute_plugin(int client, const char *path, const char *method, dictionary dic)
|
int execute_plugin(void* client, const char *path, const char *method, dictionary dic)
|
||||||
{
|
{
|
||||||
char pname[255];
|
char pname[255];
|
||||||
char pfunc[255];
|
char pfunc[255];
|
||||||
void (*fn)(int, const char*,const char*, dictionary);
|
void (*fn)(void*, const char*,const char*, dictionary);
|
||||||
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));
|
||||||
@ -857,7 +879,7 @@ int execute_plugin(int client, const char *path, const char *method, dictionary
|
|||||||
if((plugin= plugin_load(pname)) == NULL)
|
if((plugin= plugin_load(pname)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
// load the function
|
// load the function
|
||||||
fn = (void (*)(int, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER);
|
fn = (void (*)(void*, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER);
|
||||||
if ((error = dlerror()) != NULL)
|
if ((error = dlerror()) != NULL)
|
||||||
{
|
{
|
||||||
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);
|
||||||
@ -869,3 +891,10 @@ int execute_plugin(int client, const char *path, const char *method, dictionary
|
|||||||
free(rpath);
|
free(rpath);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
int usessl()
|
||||||
|
{
|
||||||
|
return server_config.usessl;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -21,26 +21,29 @@
|
|||||||
|
|
||||||
#define SERVER_STRING "Server: ant-httpd"
|
#define SERVER_STRING "Server: ant-httpd"
|
||||||
|
|
||||||
void accept_request(int);
|
#define CONFIG "config.ini"
|
||||||
void cat(int, FILE *);
|
extern config_t server_config;
|
||||||
void cannot_execute(int);
|
|
||||||
|
void accept_request(void*);
|
||||||
|
void cat(void*, FILE *);
|
||||||
|
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(int);
|
void not_found(void*);
|
||||||
void serve_file(int, const char *);
|
void serve_file(void*, const char *);
|
||||||
int startup(unsigned *);
|
int startup(unsigned *);
|
||||||
void unimplemented(int);
|
void unimplemented(void*);
|
||||||
void badrequest(int);
|
void badrequest(void*);
|
||||||
void rule_check(association, const char* , const char* , const char* , char*);
|
void rule_check(association, const char* , const char* , const char* , char*);
|
||||||
void ws_confirm_request(int, const char*);
|
void ws_confirm_request(void*, const char*);
|
||||||
char* post_url_decode(int client,int len);
|
char* post_url_decode(void* client,int len);
|
||||||
dictionary decode_url_request(const char* query);
|
dictionary decode_url_request(const char* query);
|
||||||
dictionary decode_request(int client,const char* method, char* url);
|
dictionary decode_request(void* client,const char* method, char* url);
|
||||||
dictionary decode_multi_part_request(int,const char*);
|
dictionary decode_multi_part_request(void*,const char*);
|
||||||
dictionary decode_cookie(const char*);
|
dictionary decode_cookie(const char*);
|
||||||
char* json_data_decode(int,int);
|
char* json_data_decode(void*,int);
|
||||||
|
|
||||||
int execute_plugin(int client, const char *path,
|
int execute_plugin(void* client, const char *path,
|
||||||
const char *method, dictionary rq);
|
const char *method, dictionary rq);
|
||||||
|
|
||||||
#endif
|
#endif
|
101
httpd.c
101
httpd.c
@ -5,6 +5,53 @@
|
|||||||
|
|
||||||
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
||||||
|
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
void init_openssl()
|
||||||
|
{
|
||||||
|
SSL_load_error_strings();
|
||||||
|
OpenSSL_add_ssl_algorithms();
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_openssl()
|
||||||
|
{
|
||||||
|
EVP_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
SSL_CTX *create_context()
|
||||||
|
{
|
||||||
|
const SSL_METHOD *method;
|
||||||
|
SSL_CTX *ctx;
|
||||||
|
|
||||||
|
method = SSLv23_server_method();
|
||||||
|
|
||||||
|
ctx = SSL_CTX_new(method);
|
||||||
|
if (!ctx) {
|
||||||
|
perror("Unable to create SSL context");
|
||||||
|
ERR_print_errors_fp(stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
void configure_context(SSL_CTX *ctx)
|
||||||
|
{
|
||||||
|
SSL_CTX_set_ecdh_auto(ctx, 1);
|
||||||
|
|
||||||
|
/* Set the key and cert */
|
||||||
|
if (SSL_CTX_use_certificate_file(ctx, server_config.sslcert, SSL_FILETYPE_PEM) <= 0) {
|
||||||
|
ERR_print_errors_fp(stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SSL_CTX_use_PrivateKey_file(ctx, server_config.sslkey, SSL_FILETYPE_PEM) <= 0 ) {
|
||||||
|
ERR_print_errors_fp(stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
static int config_handler(void* conf, const char* section, const char* name,
|
static int config_handler(void* conf, const char* section, const char* name,
|
||||||
const char* value)
|
const char* value)
|
||||||
{
|
{
|
||||||
@ -23,9 +70,20 @@ static int config_handler(void* conf, const char* section, const char* name,
|
|||||||
} else if(MATCH("SERVER", "tmpdir")) {
|
} else if(MATCH("SERVER", "tmpdir")) {
|
||||||
pconfig->tmpdir = strdup(value);
|
pconfig->tmpdir = strdup(value);
|
||||||
}
|
}
|
||||||
else if(MATCH("SERVER", "backlog")) {
|
else if(MATCH("SERVER", "backlog")) {
|
||||||
pconfig->backlog = atoi(value);
|
pconfig->backlog = atoi(value);
|
||||||
}
|
}
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
else if(MATCH("SERVER", "ssl.enable")) {
|
||||||
|
pconfig->usessl = atoi(value);
|
||||||
|
}
|
||||||
|
else if(MATCH("SERVER", "ssl.cert")) {
|
||||||
|
pconfig->sslcert = strdup(value);
|
||||||
|
}
|
||||||
|
else if(MATCH("SERVER", "ssl.key")) {
|
||||||
|
pconfig->sslkey = strdup(value);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
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));
|
||||||
@ -66,16 +124,27 @@ 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();
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
server_config.usessl = 0;
|
||||||
|
server_config.sslcert = "cert.pem";
|
||||||
|
server_config.sslkey = "key.pem";
|
||||||
|
#endif
|
||||||
if (ini_parse(file, config_handler, &server_config) < 0) {
|
if (ini_parse(file, config_handler, &server_config) < 0) {
|
||||||
LOG("Can't load '%s'\n. Used defaut configuration", file);
|
LOG("Can't load '%s'\n. Used defaut configuration", file);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOG("Using configuration : %s\n", file);
|
LOG("Using configuration : %s\n", file);
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
LOG("Enable %d\n", server_config.usessl);
|
||||||
|
LOG("cert %s\n", server_config.sslcert);
|
||||||
|
LOG("key %s\n", server_config.sslkey);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
init_file_system();
|
init_file_system();
|
||||||
}
|
}
|
||||||
void stop_serve(int dummy) {
|
void stop_serve(int dummy) {
|
||||||
|
free(server_config.rules);
|
||||||
unload_all_plugin();
|
unload_all_plugin();
|
||||||
}
|
}
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
@ -98,11 +167,25 @@ int main(int argc, char* argv[])
|
|||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
signal(SIGABRT, SIG_IGN);
|
signal(SIGABRT, SIG_IGN);
|
||||||
signal(SIGINT, stop_serve);
|
signal(SIGINT, stop_serve);
|
||||||
|
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
SSL_CTX *ctx;
|
||||||
|
if( server_config.usessl == 1 )
|
||||||
|
{
|
||||||
|
init_openssl();
|
||||||
|
ctx = create_context();
|
||||||
|
|
||||||
|
configure_context(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
server_sock = startup(&port);
|
server_sock = startup(&port);
|
||||||
LOG("httpd running on port %d\n", port);
|
LOG("httpd running on port %d\n", port);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
antd_client_t client;
|
||||||
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
|
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
|
||||||
if (client_sock == -1)
|
if (client_sock == -1)
|
||||||
{
|
{
|
||||||
@ -111,7 +194,21 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
/* accept_request(client_sock); */
|
/* accept_request(client_sock); */
|
||||||
|
|
||||||
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client_sock) != 0)
|
#ifdef USE_OPENSSL
|
||||||
|
client.ssl = NULL;
|
||||||
|
if(server_config.usessl == 1)
|
||||||
|
{
|
||||||
|
client.ssl = (void*)SSL_new(ctx);
|
||||||
|
SSL_set_fd((SSL*)client.ssl, client_sock);
|
||||||
|
|
||||||
|
if (SSL_accept((SSL*)client.ssl) <= 0) {
|
||||||
|
ERR_print_errors_fp(stderr);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
client.sock = client_sock;
|
||||||
|
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)&client) != 0)
|
||||||
perror("pthread_create");
|
perror("pthread_create");
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -8,7 +8,7 @@ void init()
|
|||||||
printf("Finish init\n");
|
printf("Finish init\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute(int client,const char* method,dictionary rq)
|
void execute(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,7 +25,7 @@ void execute(int client,const char* method,dictionary rq)
|
|||||||
freedict(d);
|
freedict(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
void get(int client,const char* method,dictionary rq)
|
void get(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
html(client);
|
html(client);
|
||||||
if(rq)
|
if(rq)
|
||||||
@ -46,7 +46,7 @@ void get(int client,const char* method,dictionary rq)
|
|||||||
__t(client,"no request");
|
__t(client,"no request");
|
||||||
|
|
||||||
}
|
}
|
||||||
void handler(int client, const char* method, const char* rqpth, dictionary rq)
|
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||||
{
|
{
|
||||||
if(EQU(rqpth,"default"))
|
if(EQU(rqpth,"default"))
|
||||||
{
|
{
|
||||||
|
@ -19,7 +19,7 @@ void pexit()
|
|||||||
{
|
{
|
||||||
LOG("%s\n","Plugin DUMMY is exited");
|
LOG("%s\n","Plugin DUMMY is exited");
|
||||||
}
|
}
|
||||||
void execute(int client,const char* method,dictionary rq)
|
void execute(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
|
|
||||||
char * question;
|
char * question;
|
||||||
@ -56,7 +56,7 @@ void execute(int client,const char* method,dictionary rq)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// delete record
|
// delete record
|
||||||
void delete(int client,const char* method,dictionary rq)
|
void delete(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
char* id = dvalue(rq,"id");
|
char* id = dvalue(rq,"id");
|
||||||
html(client);
|
html(client);
|
||||||
@ -76,7 +76,7 @@ void delete(int client,const char* method,dictionary rq)
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void update(int client,const char* method,dictionary rq)
|
void update(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
char * id;
|
char * id;
|
||||||
html(client);
|
html(client);
|
||||||
@ -121,14 +121,14 @@ void update(int client,const char* method,dictionary rq)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void jsonex(int client,const char* method,dictionary rq)
|
void jsonex(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
//json(client);
|
//json(client);
|
||||||
//__t(client,"{name:\"%s\", age:%d}","Sang",30);
|
//__t(client,"{name:\"%s\", age:%d}","Sang",30);
|
||||||
jpeg(client);
|
jpeg(client);
|
||||||
__f(client,htdocs("images/ex.jpg"));
|
__f(client,htdocs("images/ex.jpg"));
|
||||||
}
|
}
|
||||||
void handler(int client, const char* method, const char* rqpth, dictionary rq)
|
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||||
{
|
{
|
||||||
if(EQU(rqpth,"default"))
|
if(EQU(rqpth,"default"))
|
||||||
{
|
{
|
||||||
|
@ -27,7 +27,7 @@ char* folder_list_from(const char* aPath)
|
|||||||
return flist;
|
return flist;
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute(int client,const char* method,dictionary rq)
|
void execute(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
DIR *d;
|
DIR *d;
|
||||||
struct dirent *dir;
|
struct dirent *dir;
|
||||||
@ -96,7 +96,7 @@ void execute(int client,const char* method,dictionary rq)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(int c, const char* m, dictionary rq)
|
void add(void* c, const char* m, dictionary rq)
|
||||||
{
|
{
|
||||||
json(c);
|
json(c);
|
||||||
if(IS_GET(m))
|
if(IS_GET(m))
|
||||||
@ -133,7 +133,7 @@ void add(int c, const char* m, dictionary rq)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mkfolder(int c, const char* m, dictionary rq)
|
void mkfolder(void* c, const char* m, dictionary rq)
|
||||||
{
|
{
|
||||||
json(c);
|
json(c);
|
||||||
if(IS_GET(m))
|
if(IS_GET(m))
|
||||||
@ -161,7 +161,7 @@ void mkfolder(int c, const char* m, dictionary rq)
|
|||||||
__t(c,__RESULT__,1,"OK");
|
__t(c,__RESULT__,1,"OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
void rmfolder(int c, const char* m, dictionary rq)
|
void rmfolder(void* c, const char* m, dictionary rq)
|
||||||
{
|
{
|
||||||
json(c);
|
json(c);
|
||||||
if(IS_GET(m))
|
if(IS_GET(m))
|
||||||
@ -190,7 +190,7 @@ void pexit()
|
|||||||
{
|
{
|
||||||
LOG("Exit file manager,plugins\n");
|
LOG("Exit file manager,plugins\n");
|
||||||
}
|
}
|
||||||
void handler(int client, const char* method, const char* rqpth, dictionary rq)
|
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||||
{
|
{
|
||||||
if(EQU(rqpth,"default"))
|
if(EQU(rqpth,"default"))
|
||||||
{
|
{
|
||||||
|
127
libs/handle.c
127
libs/handle.c
@ -1,33 +1,39 @@
|
|||||||
#include "handle.h"
|
#include "handle.h"
|
||||||
|
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
int usessl()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void set_status(int client,int code,const char* msg)
|
void set_status(void* client,int code,const char* msg)
|
||||||
{
|
{
|
||||||
response(client, __s("HTTP/1.1 %d %s", code, msg));
|
response(client, __s("HTTP/1.1 %d %s", code, msg));
|
||||||
response(client, __s("Server: %s ", SERVER_NAME));
|
response(client, __s("Server: %s ", SERVER_NAME));
|
||||||
}
|
}
|
||||||
void redirect(int client,const char*path)
|
void redirect(void* client,const char*path)
|
||||||
{
|
{
|
||||||
__t(client,"<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"></head><body></body></html>",path);
|
__t(client,"<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"></head><body></body></html>",path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void html(int client)
|
void html(void* client)
|
||||||
{
|
{
|
||||||
ctype(client,"text/html; charset=utf-8");
|
ctype(client,"text/html; charset=utf-8");
|
||||||
}
|
}
|
||||||
void text(int client)
|
void text(void* client)
|
||||||
{
|
{
|
||||||
ctype(client,"text/plain; charset=utf-8");
|
ctype(client,"text/plain; charset=utf-8");
|
||||||
}
|
}
|
||||||
void json(int client)
|
void json(void* client)
|
||||||
{
|
{
|
||||||
ctype(client,"application/json");
|
ctype(client,"application/json");
|
||||||
}
|
}
|
||||||
void textstream(int client)
|
void textstream(void* client)
|
||||||
{
|
{
|
||||||
ctype(client, "text/event-stream");
|
ctype(client, "text/event-stream");
|
||||||
}
|
}
|
||||||
void octstream(int client, char* name)
|
void octstream(void* client, char* name)
|
||||||
{
|
{
|
||||||
set_status(client,200,"OK");
|
set_status(client,200,"OK");
|
||||||
__t(client,"Content-Type: application/octet-stream");
|
__t(client,"Content-Type: application/octet-stream");
|
||||||
@ -35,18 +41,18 @@ void octstream(int client, char* name)
|
|||||||
response(client,"");
|
response(client,"");
|
||||||
//Content-Disposition: attachment; filename="fname.ext"
|
//Content-Disposition: attachment; filename="fname.ext"
|
||||||
}
|
}
|
||||||
void jpeg(int client)
|
void jpeg(void* client)
|
||||||
{
|
{
|
||||||
ctype(client,"image/jpeg");
|
ctype(client,"image/jpeg");
|
||||||
}
|
}
|
||||||
void ctype(int client, const char* type)
|
void ctype(void* client, const char* type)
|
||||||
{
|
{
|
||||||
set_status(client,200,"OK");
|
set_status(client,200,"OK");
|
||||||
__t(client,"Content-Type: %s",type);
|
__t(client,"Content-Type: %s",type);
|
||||||
response(client,"");
|
response(client,"");
|
||||||
}
|
}
|
||||||
|
|
||||||
int response(int client, const char* data)
|
int response(void* client, const char* data)
|
||||||
{
|
{
|
||||||
char buf[BUFFLEN+3];
|
char buf[BUFFLEN+3];
|
||||||
strcpy(buf, data);
|
strcpy(buf, data);
|
||||||
@ -54,18 +60,66 @@ int response(int client, const char* data)
|
|||||||
int size = strlen(data);
|
int size = strlen(data);
|
||||||
buf[size] = '\r';
|
buf[size] = '\r';
|
||||||
buf[size+1] = '\n';
|
buf[size+1] = '\n';
|
||||||
buf[size+2] = '\0';
|
buf[size+2] = '\0';
|
||||||
nbytes = send(client, buf, strlen(buf), 0);
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
|
nbytes = antd_send(client, buf, strlen(buf), _ssl);
|
||||||
return (nbytes ==-1?0:1);
|
return (nbytes ==-1?0:1);
|
||||||
}
|
}
|
||||||
int __ti(int client,int data)
|
int antd_send(const void *src, const void* data, int len, int _ssl)
|
||||||
|
{
|
||||||
|
antd_client_t * source = (antd_client_t *) src;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
if(_ssl)
|
||||||
|
{
|
||||||
|
return SSL_write((SSL*) source->ssl, data, len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
return send(source->sock, data, len, 0);
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
int antd_recv(const void *src, void* data, int len, int _ssl)
|
||||||
|
{
|
||||||
|
antd_client_t * source = (antd_client_t *) src;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
if(_ssl)
|
||||||
|
{
|
||||||
|
return SSL_read((SSL*) source->ssl, data, len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
return recv(((int) source->sock), data, len, 0);
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
int antd_close(void* src)
|
||||||
|
{
|
||||||
|
antd_client_t * source = (antd_client_t *) src;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
if(source->ssl && usessl()){
|
||||||
|
SSL_free((SSL*) source->ssl);
|
||||||
|
LOG("Freeing SSL\n");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
printf("Close sock %d\n", source->sock);
|
||||||
|
close(source->sock);
|
||||||
|
}
|
||||||
|
int __ti(void* client,int data)
|
||||||
{
|
{
|
||||||
char str[15];
|
char str[15];
|
||||||
sprintf(str, "%d", data);
|
sprintf(str, "%d", data);
|
||||||
return response(client,str);
|
return response(client,str);
|
||||||
}
|
}
|
||||||
|
|
||||||
int __t(int client, const char* fstring,...)
|
int __t(void* client, const char* fstring,...)
|
||||||
{
|
{
|
||||||
int nbytes;
|
int nbytes;
|
||||||
int dlen;
|
int dlen;
|
||||||
@ -77,6 +131,10 @@ int __t(int client, const char* fstring,...)
|
|||||||
va_start( arguments, fstring);
|
va_start( arguments, fstring);
|
||||||
dlen = vsnprintf(0,0,fstring,arguments) + 1;
|
dlen = vsnprintf(0,0,fstring,arguments) + 1;
|
||||||
va_end(arguments);
|
va_end(arguments);
|
||||||
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
||||||
{
|
{
|
||||||
va_start(arguments, fstring);
|
va_start(arguments, fstring);
|
||||||
@ -99,27 +157,31 @@ int __t(int client, const char* fstring,...)
|
|||||||
//chunk[buflen-1] = '\0';
|
//chunk[buflen-1] = '\0';
|
||||||
//response(client,chunk);
|
//response(client,chunk);
|
||||||
sent += buflen;
|
sent += buflen;
|
||||||
nbytes = send(client, chunk, buflen, 0);
|
nbytes = antd_send(client, chunk, buflen, _ssl);
|
||||||
free(chunk);
|
free(chunk);
|
||||||
if(nbytes == -1) return 0;
|
if(nbytes == -1) return 0;
|
||||||
}
|
}
|
||||||
chunk = "\r\n";
|
chunk = "\r\n";
|
||||||
send(client, chunk, strlen(chunk), 0);
|
antd_send(client, chunk, strlen(chunk), _ssl);
|
||||||
}
|
}
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
int __b(int client, const unsigned char* data, int size)
|
int __b(void* client, const unsigned char* data, int size)
|
||||||
{
|
{
|
||||||
char buf[BUFFLEN];
|
char buf[BUFFLEN];
|
||||||
int sent = 0;
|
int sent = 0;
|
||||||
int buflen = 0;
|
int buflen = 0;
|
||||||
int nbytes;
|
int nbytes;
|
||||||
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
if(size <= BUFFLEN)
|
if(size <= BUFFLEN)
|
||||||
{
|
{
|
||||||
nbytes = send(client,data,size,0);
|
nbytes = antd_send(client,data,size,_ssl);
|
||||||
return (nbytes==-1?0:1);
|
return (nbytes==-1?0:1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -131,14 +193,14 @@ int __b(int client, const unsigned char* data, int size)
|
|||||||
else
|
else
|
||||||
buflen = size - sent;
|
buflen = size - sent;
|
||||||
memcpy(buf,data+sent,buflen);
|
memcpy(buf,data+sent,buflen);
|
||||||
nbytes = send(client,buf,buflen,0);
|
nbytes = antd_send(client,buf,buflen,_ssl);
|
||||||
sent += buflen;
|
sent += buflen;
|
||||||
if(nbytes == -1) return 0;
|
if(nbytes == -1) return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int __fb(int client, const char* file)
|
int __fb(void* client, const char* file)
|
||||||
{
|
{
|
||||||
printf("Open file %s\n",file );
|
printf("Open file %s\n",file );
|
||||||
unsigned char buffer[BUFFLEN];
|
unsigned char buffer[BUFFLEN];
|
||||||
@ -158,7 +220,7 @@ int __fb(int client, const char* file)
|
|||||||
fclose(ptr);
|
fclose(ptr);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int __f(int client, const char* file)
|
int __f(void* client, const char* file)
|
||||||
{
|
{
|
||||||
unsigned char buf[BUFFLEN];
|
unsigned char buf[BUFFLEN];
|
||||||
FILE *ptr;
|
FILE *ptr;
|
||||||
@ -169,10 +231,13 @@ int __f(int client, const char* file)
|
|||||||
LOG("Cannot read : %s\n", file);
|
LOG("Cannot read : %s\n", file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
;
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
while(fgets(buf, sizeof(buf), ptr) != NULL)
|
while(fgets(buf, sizeof(buf), ptr) != NULL)
|
||||||
{
|
{
|
||||||
nbytes = send(client, buf, strlen(buf), 0);
|
nbytes = antd_send(client, buf, strlen(buf), _ssl);
|
||||||
if(nbytes == -1) return 0;
|
if(nbytes == -1) return 0;
|
||||||
//LOG("READ : %s\n", buf);
|
//LOG("READ : %s\n", buf);
|
||||||
//fgets(buf, sizeof(buf), ptr);
|
//fgets(buf, sizeof(buf), ptr);
|
||||||
@ -186,7 +251,7 @@ int upload(const char* tmp, const char* path)
|
|||||||
return !rename(tmp, path);
|
return !rename(tmp, path);
|
||||||
}
|
}
|
||||||
// __plugin__.name
|
// __plugin__.name
|
||||||
void set_cookie(int client,const char* type, dictionary dic, const char* name)
|
void set_cookie(void* client,const char* type, dictionary dic, const char* name)
|
||||||
{
|
{
|
||||||
set_status(client,200,"OK");
|
set_status(client,200,"OK");
|
||||||
__t(client,"Content-Type: %s",type);
|
__t(client,"Content-Type: %s",type);
|
||||||
@ -196,7 +261,7 @@ void set_cookie(int client,const char* type, dictionary dic, const char* name)
|
|||||||
}
|
}
|
||||||
response(client,"");
|
response(client,"");
|
||||||
}
|
}
|
||||||
void clear_cookie(int client, dictionary dic)
|
void clear_cookie(void* client, dictionary dic)
|
||||||
{
|
{
|
||||||
set_status(client,200,"OK");
|
set_status(client,200,"OK");
|
||||||
__t(client,"Content-Type: text/html; charset=utf-8");
|
__t(client,"Content-Type: text/html; charset=utf-8");
|
||||||
@ -206,7 +271,7 @@ void clear_cookie(int client, dictionary dic)
|
|||||||
}
|
}
|
||||||
response(client,"");
|
response(client,"");
|
||||||
}
|
}
|
||||||
void unknow(int client)
|
void unknow(void* client)
|
||||||
{
|
{
|
||||||
html(client);
|
html(client);
|
||||||
__t(client,"404 API not found");
|
__t(client,"404 API not found");
|
||||||
@ -220,7 +285,7 @@ int ws_enable(dictionary dic)
|
|||||||
* @param sock socket
|
* @param sock socket
|
||||||
* @return a request string
|
* @return a request string
|
||||||
*/
|
*/
|
||||||
char* read_line(int sock)
|
char* read_line(void* sock)
|
||||||
{
|
{
|
||||||
char buf[BUFFLEN];
|
char buf[BUFFLEN];
|
||||||
read_buf(sock,buf,sizeof(buf));
|
read_buf(sock,buf,sizeof(buf));
|
||||||
@ -235,14 +300,18 @@ char* read_line(int sock)
|
|||||||
* @param size size of buffer
|
* @param size size of buffer
|
||||||
* @return number of bytes read
|
* @return number of bytes read
|
||||||
*/
|
*/
|
||||||
int read_buf(int sock, char*buf,int size)
|
int read_buf(void* sock, char*buf,int size)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char c = '\0';
|
char c = '\0';
|
||||||
int n;
|
int n;
|
||||||
|
int _ssl = 0;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
_ssl = usessl();
|
||||||
|
#endif
|
||||||
while ((i < size - 1) && (c != '\n'))
|
while ((i < size - 1) && (c != '\n'))
|
||||||
{
|
{
|
||||||
n = recv(sock, &c, 1, 0);
|
n = antd_recv(sock, &c, 1, _ssl);
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
{
|
{
|
||||||
buf[i] = c;
|
buf[i] = c;
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
//open ssl
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
#include <openssl/ssl.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#endif
|
||||||
#ifdef USE_DB
|
#ifdef USE_DB
|
||||||
#include "dbhelper.h"
|
#include "dbhelper.h"
|
||||||
#endif
|
#endif
|
||||||
@ -20,30 +25,56 @@
|
|||||||
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
int __attribute__((weak)) usessl();
|
||||||
|
#endif
|
||||||
|
|
||||||
int response(int, const char*);
|
typedef struct {
|
||||||
void ctype(int,const char*);
|
int port;
|
||||||
void redirect(int,const char*);
|
char *plugins_dir;
|
||||||
void html(int);
|
char *plugins_ext;
|
||||||
void text(int);
|
char *db_path;
|
||||||
void json(int);
|
char* htdocs;
|
||||||
void jpeg(int);
|
char* tmpdir;
|
||||||
void octstream(int, char*);
|
dictionary rules;
|
||||||
void textstream(int);
|
int backlog;
|
||||||
int __ti(int,int);
|
#ifdef USE_OPENSSL
|
||||||
int __t(int, const char*,...);
|
int usessl;
|
||||||
int __b(int, const unsigned char*, int);
|
char* sslcert;
|
||||||
int __f(int, const char*);
|
char* sslkey;
|
||||||
int __fb(int, const char*);
|
#endif
|
||||||
|
}config_t;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
int sock;
|
||||||
|
void* ssl;
|
||||||
|
} antd_client_t;
|
||||||
|
|
||||||
|
int response(void*, const char*);
|
||||||
|
void ctype(void*,const char*);
|
||||||
|
void redirect(void*,const char*);
|
||||||
|
void html(void*);
|
||||||
|
void text(void*);
|
||||||
|
void json(void*);
|
||||||
|
void jpeg(void*);
|
||||||
|
void octstream(void*, char*);
|
||||||
|
void textstream(void*);
|
||||||
|
int __ti(void*,int);
|
||||||
|
int __t(void*, const char*,...);
|
||||||
|
int __b(void*, const unsigned char*, int);
|
||||||
|
int __f(void*, const char*);
|
||||||
|
int __fb(void*, const char*);
|
||||||
int upload(const char*, const char*);
|
int upload(const char*, const char*);
|
||||||
|
|
||||||
void set_cookie(int, const char*,dictionary,const char*);
|
void set_cookie(void*, const char*,dictionary,const char*);
|
||||||
void set_status(int,int,const char*);
|
void set_status(void*,int,const char*);
|
||||||
void clear_cookie(int, dictionary);
|
void clear_cookie(void*, dictionary);
|
||||||
/*Default function for plugin*/
|
/*Default function for plugin*/
|
||||||
void unknow(int);
|
void unknow(void*);
|
||||||
int ws_enable(dictionary);
|
int ws_enable(dictionary);
|
||||||
char* read_line(int sock);
|
char* read_line(void* sock);
|
||||||
int read_buf(int sock,char* buf,int i);
|
int read_buf(void* sock,char* buf,int i);
|
||||||
|
int antd_send(const void *source, const void* data, int len, int usessl);
|
||||||
|
int antd_recv(const void *source, void* data, int len, int usessl);
|
||||||
|
int antd_close(void* source);
|
||||||
#endif
|
#endif
|
||||||
|
@ -101,7 +101,7 @@ void pexit()
|
|||||||
{
|
{
|
||||||
LOG("%s","EXIT daemon");
|
LOG("%s","EXIT daemon");
|
||||||
}
|
}
|
||||||
void handler(int c, const char* m, const char* rqp, dictionary d)
|
void handler(void* c, const char* m, const char* rqp, dictionary d)
|
||||||
{
|
{
|
||||||
text(c);
|
text(c);
|
||||||
__t(c,"This is a system plugin. It cant be acessed from the web");
|
__t(c,"This is a system plugin. It cant be acessed from the web");
|
||||||
|
@ -3,13 +3,15 @@
|
|||||||
plugin_header __plugin__;
|
plugin_header __plugin__;
|
||||||
// private function
|
// private function
|
||||||
call __init__;
|
call __init__;
|
||||||
|
void __init_plugin__(const char* pl,config_t* conf){
|
||||||
void __init_plugin__(const char* pl,const char*ph,const char* htdocs, const char* pdir,int port){
|
|
||||||
__plugin__.name = strdup(pl);
|
__plugin__.name = strdup(pl);
|
||||||
__plugin__.dbpath= strdup(ph);
|
__plugin__.dbpath= strdup(conf->db_path);
|
||||||
__plugin__.htdocs = strdup(htdocs);
|
__plugin__.htdocs = strdup(conf->htdocs);
|
||||||
__plugin__.pdir = strdup(pdir);
|
__plugin__.pdir = strdup(conf->plugins_dir);
|
||||||
__plugin__.sport = port;
|
__plugin__.sport = conf->port;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
__plugin__.usessl = conf->usessl;
|
||||||
|
#endif
|
||||||
if(__init__ != NULL) __init__();
|
if(__init__ != NULL) __init__();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -32,6 +34,13 @@ sqldb getdb()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
int usessl()
|
||||||
|
{
|
||||||
|
return __plugin__.usessl;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
char* route(const char* repath)
|
char* route(const char* repath)
|
||||||
{
|
{
|
||||||
int len = strlen(__plugin__.name) + 2;
|
int len = strlen(__plugin__.name) + 2;
|
||||||
|
@ -12,6 +12,9 @@ typedef struct {
|
|||||||
char * htdocs;
|
char * htdocs;
|
||||||
char*pdir;
|
char*pdir;
|
||||||
int sport;
|
int sport;
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
int usessl;
|
||||||
|
#endif
|
||||||
} plugin_header;
|
} plugin_header;
|
||||||
|
|
||||||
|
|
||||||
@ -33,6 +36,6 @@ char* route(const char*);
|
|||||||
char* htdocs(const char*);
|
char* htdocs(const char*);
|
||||||
char* config_dir();
|
char* config_dir();
|
||||||
/*Default function for plugin*/
|
/*Default function for plugin*/
|
||||||
void handler(int, const char*,const char*,dictionary);
|
void handler(void*, const char*,const char*,dictionary);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#define PEXT "dylib"
|
#define PEXT "dylib"
|
||||||
#define MAXSIZE 500000
|
#define MAXSIZE 500000
|
||||||
|
|
||||||
void execute(int client,const char* method,dictionary rq)
|
void execute(void* client,const char* method,dictionary rq)
|
||||||
{
|
{
|
||||||
//all plugin file
|
//all plugin file
|
||||||
DIR *d;
|
DIR *d;
|
||||||
@ -36,7 +36,7 @@ void execute(int client,const char* method,dictionary rq)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void install(int c, const char* m, dictionary rq)
|
void install(void* c, const char* m, dictionary rq)
|
||||||
{
|
{
|
||||||
char * result = "{\"result\":%d,\"msg\":\"%s\"}";
|
char * result = "{\"result\":%d,\"msg\":\"%s\"}";
|
||||||
json(c);
|
json(c);
|
||||||
@ -74,7 +74,7 @@ void install(int c, const char* m, dictionary rq)
|
|||||||
|
|
||||||
__t(c,result,0,"This is not a plugin file");
|
__t(c,result,0,"This is not a plugin file");
|
||||||
}
|
}
|
||||||
void handler(int client, const char* method, const char* rqpth, dictionary rq)
|
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||||
{
|
{
|
||||||
if(EQU(rqpth,"default"))
|
if(EQU(rqpth,"default"))
|
||||||
{
|
{
|
||||||
|
@ -5,7 +5,7 @@ void pexit()
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
void handler(int cl, const char* m, const char* rqp, dictionary rq)
|
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
|
||||||
{
|
{
|
||||||
//html(cl);
|
//html(cl);
|
||||||
ws_msg_header_t* h = NULL;
|
ws_msg_header_t* h = NULL;
|
||||||
|
@ -218,7 +218,7 @@ void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len)
|
|||||||
|
|
||||||
|
|
||||||
/* Add padding and return the message digest. */
|
/* Add padding and return the message digest. */
|
||||||
void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE])
|
void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
uint8_t finalcount[8];
|
uint8_t finalcount[8];
|
||||||
|
@ -18,6 +18,6 @@ typedef struct {
|
|||||||
|
|
||||||
void SHA1_Init(SHA1_CTX* context);
|
void SHA1_Init(SHA1_CTX* context);
|
||||||
void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len);
|
void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len);
|
||||||
void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]);
|
void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context);
|
||||||
void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output);
|
void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output);
|
||||||
#endif /* __SHA1_H */
|
#endif /* __SHA1_H */
|
14
libs/utils.c
14
libs/utils.c
@ -444,9 +444,13 @@ void md5(uint8_t *initial_msg, size_t initial_len, char* buff) {
|
|||||||
void sha1(const char* text, char* out)
|
void sha1(const char* text, char* out)
|
||||||
{
|
{
|
||||||
uint8_t d [20];
|
uint8_t d [20];
|
||||||
SHA1_CTX context;
|
#ifdef USE_OPENSSL
|
||||||
SHA1_Init(&context);
|
SHA_CTX context;
|
||||||
SHA1_Update(&context, text, strlen(text));
|
#else
|
||||||
SHA1_Final(&context, d);
|
SHA1_CTX context;
|
||||||
digest_to_hex(d,out);
|
#endif
|
||||||
|
SHA1_Init(&context);
|
||||||
|
SHA1_Update(&context, text, strlen(text));
|
||||||
|
SHA1_Final(d, &context);
|
||||||
|
digest_to_hex(d,out);
|
||||||
}
|
}
|
@ -36,7 +36,11 @@ THE SOFTWARE.
|
|||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#ifdef USE_OPENSSL
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
#else
|
||||||
#include "sha1.h"
|
#include "sha1.h"
|
||||||
|
#endif
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
|
|
||||||
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
|
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
|
||||||
|
@ -6,7 +6,7 @@ void pexit()
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
void handler(int cl, const char* m, const char* rqp, dictionary rq)
|
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
|
||||||
{
|
{
|
||||||
char* path = NULL;
|
char* path = NULL;
|
||||||
int nimg = 19;
|
int nimg = 19;
|
||||||
|
@ -13,7 +13,7 @@ void pexit()
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
void handler(int cl, const char* m, const char* rqp, dictionary rq)
|
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
|
||||||
{
|
{
|
||||||
ws_msg_header_t* h = NULL;
|
ws_msg_header_t* h = NULL;
|
||||||
if(ws_enable(rq))
|
if(ws_enable(rq))
|
||||||
|
@ -55,7 +55,7 @@ void * plugin_from_file(char* name)
|
|||||||
void *lib_handle;
|
void *lib_handle;
|
||||||
char* error;
|
char* error;
|
||||||
char* path = __s("%s%s%s",server_config.plugins_dir,name,server_config.plugins_ext);
|
char* path = __s("%s%s%s",server_config.plugins_dir,name,server_config.plugins_ext);
|
||||||
void (*fn)(const char*,const char*,const char*,const char*,int);
|
void (*fn)(const char*, config_t*);
|
||||||
lib_handle = dlopen(path, RTLD_LAZY);
|
lib_handle = dlopen(path, RTLD_LAZY);
|
||||||
if (!lib_handle)
|
if (!lib_handle)
|
||||||
{
|
{
|
||||||
@ -63,11 +63,11 @@ void * plugin_from_file(char* name)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
// set database path
|
// set database path
|
||||||
fn = (void (*)(const char *, const char *, const char *, const char *,int))dlsym(lib_handle, "__init_plugin__");
|
fn = (void (*)(const char *, config_t*))dlsym(lib_handle, "__init_plugin__");
|
||||||
if ((error = dlerror()) != NULL)
|
if ((error = dlerror()) != NULL)
|
||||||
LOG("Problem when setting data path for %s : %s \n", name,error);
|
LOG("Problem when setting data path for %s : %s \n", name,error);
|
||||||
else
|
else
|
||||||
(*fn)(name,server_config.db_path, server_config.htdocs,server_config.plugins_dir,server_config.port);
|
(*fn)(name,&server_config);
|
||||||
if(path)
|
if(path)
|
||||||
free(path);
|
free(path);
|
||||||
return lib_handle;
|
return lib_handle;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#define PLUGIN_MANAGER_H
|
#define PLUGIN_MANAGER_H
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include "libs/utils.h"
|
#include "libs/utils.h"
|
||||||
#include "config.h"
|
#include "libs/handle.h"
|
||||||
|
|
||||||
struct plugin_entry {
|
struct plugin_entry {
|
||||||
struct plugin_entry *next;
|
struct plugin_entry *next;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user