This commit is contained in:
lxsang
2020-01-08 19:17:51 +01:00
parent cb98562585
commit 4608ae874f
12 changed files with 321 additions and 94 deletions

View File

@ -76,6 +76,34 @@ int require_plugin(const char* name)
return 0;
}
int compressable(char* ctype)
{
UNUSED(ctype);
return 0;
}
void htdocs(antd_request_t* rq, char* dest)
{
dictionary_t xheader = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
char* www = (char*)dvalue(xheader, "SERVER_WWW_ROOT");
if(www)
{
strcpy(dest,www);
}
}
void dbdir(char* dest)
{
UNUSED(dest);
}
void tmpdir(char* dest)
{
UNUSED(dest);
}
void plugindir(char* dest)
{
UNUSED(dest);
}
const char* get_status_str(int stat)
{
switch(stat)
@ -150,10 +178,72 @@ const char* get_status_str(int stat)
}
}
void antd_send_header(void* client, antd_response_header_t* res)
void antd_send_header(void* cl, antd_response_header_t* res)
{
if(!res->header)
res->header = dict();
antd_client_t* client = (antd_client_t*) cl;
#ifdef USE_ZLIB
char* str = dvalue(res->header,"Content-Encoding");
if(!str)
{
// check for compress
str = dvalue(res->header,"Content-Type");
if(str)
{
if(compressable(str))
{
switch (client->z_level)
{
case ANTD_CGZ:
client->zstream = (z_stream *) malloc(sizeof(z_stream));
if(client->zstream)
{
((z_stream*)client->zstream)->zalloc = Z_NULL;
((z_stream*)client->zstream)->zfree = Z_NULL;
((z_stream*)client->zstream)->opaque = Z_NULL;
if(deflateInit2(client->zstream,Z_BEST_COMPRESSION,Z_DEFLATED,15 | 16, 8,Z_DEFAULT_STRATEGY) != Z_OK)
{
ERROR("Cannot init gzip stream");
free(client->zstream);
client->zstream = NULL;
}
else
{
client->status = Z_NO_FLUSH;
dput(res->header,"Content-Encoding", strdup("gzip"));
}
}
break;
case ANTD_CDEFL:
client->zstream = (z_stream *) malloc(sizeof(z_stream));
if(client->zstream)
{
((z_stream*)client->zstream)->zalloc = Z_NULL;
((z_stream*)client->zstream)->zfree = Z_NULL;
((z_stream*)client->zstream)->opaque = Z_NULL;
if(deflateInit(client->zstream, Z_BEST_COMPRESSION) != Z_OK)
{
ERROR("Cannot init deflate stream");
free(client->zstream);
client->zstream = NULL;
}
else
{
client->status = Z_NO_FLUSH;
dput(res->header,"Content-Encoding", strdup("deflate"));
}
}
break;
default:
break;
}
}
}
}
#endif
dput(res->header,"Server", strdup(SERVER_NAME));
const char* stat_str = get_status_str(res->status);
__t(client, "HTTP/1.1 %d %s", res->status, stat_str);
@ -191,16 +281,57 @@ void octstream(void* client, char* name)
//Content-Disposition: attachment; filename="fname.ext"
}*/
int antd_send(void *src, const void* data, int len)
#ifdef USE_ZLIB
int zcompress(antd_client_t * cl, uint8_t* data, int len, uint8_t* dest)
{
if(!src || !data) return -1;
int written;
z_stream* zstream = (z_stream*) cl->zstream;
zstream->avail_in = (uInt)len;
zstream->next_in = (Bytef *)data;
zstream->avail_out = len;
zstream->next_out = dest;
if(deflate(zstream, cl->status) == Z_STREAM_ERROR)
{
free(dest);
return -1;
}
return zstream->total_out;
}
#endif
int antd_send(void *src, const void* data_in, int len_in)
{
uint8_t* data = (uint8_t*)data_in;
int len = len_in;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_ZLIB
if(source->zstream && source->z_level != ANTD_CNONE)
{
data = (uint8_t*) malloc(len);
if(data)
{
len = zcompress(source,data_in, len, data);
}
}
#endif
if(!src || !data)
{
#ifdef USE_ZLIB
if(source->zstream && source->z_level != ANTD_CNONE && data)
free(data);
#endif
return -1;
}
int written;
char* ptr;
int writelen = 0;
int count;
#ifdef USE_OPENSSL
if(source->port_config->usessl)
if(source->ssl)
{
//LOG("SSL WRITE\n");
//ret = SSL_write((SSL*) source->ssl, data, len);
@ -334,6 +465,10 @@ int antd_send(void *src, const void* data, int len)
{
antd_close(src);
}*/
#ifdef USE_ZLIB
if(source->zstream && source->z_level != ANTD_CNONE && data)
free(data);
#endif
return written;
}
int antd_recv(void *src, void* data, int len)
@ -345,7 +480,7 @@ int antd_recv(void *src, void* data, int len)
int readlen=0;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(source->port_config->usessl)
if(source->ssl)
{
ptr = (char* )data;
readlen = len > BUFFLEN?BUFFLEN:len;
@ -517,8 +652,32 @@ int antd_close(void* src)
{
if(!src) return -1;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_ZLIB
//TODO: send finish data to the socket before quit
if(source->zstream)
{
printf("Close the stream now\n");
if(source->status == Z_NO_FLUSH)
{
uint8_t buf[512];
// send finish data
z_stream* zstream = (z_stream*) cl->zstream;
source->status = Z_FINISH;
zstream->avail_in = 0;
zstream->next_in = "";
zstream->avail_out = 512;
zstream->next_out = buf;
antd_send(source,"", 0);
deflateEnd(source->zstream);
}
deflateEnd(source->zstream);
free(source->zstream);
}
#endif
#ifdef USE_OPENSSL
if(source->port_config->usessl && source->ssl){
if(source->ssl){
//printf("SSL:Shutdown ssl\n");
//SSL_shutdown((SSL*) source->ssl);
SSL_set_shutdown((SSL*) source->ssl, SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
@ -536,7 +695,6 @@ int antd_close(void* src)
#endif
//printf("Close sock %d\n", source->sock);
int ret = close(source->sock);
if(source->ip) free(source->ip);
free(src);
src = NULL;
return ret;

View File

@ -9,9 +9,13 @@
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#ifdef USE_ZLIB
#include <zlib.h>
#endif
#ifdef USE_DB
#include "dbhelper.h"
#endif
#include <fcntl.h>
#include <stdlib.h>
#include "dictionary.h"
@ -30,6 +34,9 @@
#define FORM_MULTI_PART "multipart/form-data"
#define MAX_IO_WAIT_TIME 5 // second
typedef enum {ANTD_CGZ, ANTD_CDEFL, ANTD_CNONE} antd_compress_t;
//extern config_t server_config;
typedef struct {
@ -43,12 +50,11 @@ typedef struct {
typedef struct{
int sock;
void* ssl;
char* ip;
//#ifdef USE_OPENSSL
int status;
//#endif
time_t last_io;
port_config_t* port_config;
// compress
antd_compress_t z_level;
void* zstream;
} antd_client_t;
typedef struct {
@ -79,31 +85,37 @@ typedef struct {
int connection;
int n_workers;
int max_upload_size;
// log
FILE* errorfp;
// #ifdef DEBUG
FILE* logfp;
// #endif
// #ifdef USE_OPENSSL
// ssl
int enable_ssl;
char* sslcert;
char* sslkey;
char* ssl_cipher;
int gzip_enable;
list_t gzip_types;
dictionary_t mimes;
dictionary_t ports;
// #endif
}config_t;
typedef struct {
char *name;
char *dbpath;
char *tmpdir;
char*pdir;
char name[128];
char dbpath[512];
char tmpdir[512];
char pdir[512];
int raw_body;
} plugin_header_t;
int __attribute__((weak)) require_plugin(const char*);
void __attribute__((weak)) htdocs(antd_request_t* rq, char* dest);
void __attribute__((weak)) dbdir(char* dest);
void __attribute__((weak)) tmpdir(char* dest);
void __attribute__((weak)) plugindir(char* dest);
int __attribute__((weak)) compressable(char* ctype);
void set_nonblock(int socket);
//void set_block(int socket);

View File

@ -136,9 +136,10 @@ list_t split(const char* str, const char* delim)
list_t l = list_init();
while((token = strsep(&str_cpy,delim)))
{
trim(token, ' ');
if(strlen(token) > 0)
{
list_put_special(&l,token);
list_put_special(&l, token);
}
}
if(l->type== LIST_TYPE_NIL)

View File

@ -20,8 +20,6 @@ sqldb getdb();
sqldb __getdb(char *name);
#endif
char* route(const char*);
char* htdocs(const char*);
char* config_dir();
/*Default function for plugin*/
// init the plugin
@ -37,11 +35,11 @@ STATIC PART, should be included in any plugin
#ifdef PLUGIN_IMPLEMENT
static plugin_header_t __plugin__;
// private function
void __init_plugin__(const char* pl,config_t* conf){
__plugin__.name = strdup(pl);
__plugin__.dbpath= conf->db_path;
__plugin__.pdir = conf->plugins_dir;
__plugin__.tmpdir = conf->tmpdir;
void __init_plugin__(const char* pl){
strcpy(__plugin__.name,pl);
dbdir(__plugin__.dbpath);
plugindir(__plugin__.pdir);
tmpdir(__plugin__.tmpdir);
__plugin__.raw_body = 0;
init();
};
@ -72,26 +70,6 @@ plugin_header_t* meta()
{
return &__plugin__;
}
char* route(const char* repath)
{
int len = strlen(__plugin__.name) + 2;
if(repath != NULL)
len += strlen(repath)+1;
char * path = (char*) malloc(len*sizeof(char));
strcpy(path,"/");
strcat(path,__plugin__.name);
if(repath != NULL)
{
strcat(path,"/");
strcat(path,repath);
}
return path;
}
const char* tmpdir()
{
return (const char*) __plugin__.tmpdir;
}
char* config_dir()
{
@ -105,11 +83,6 @@ char* config_dir()
void __release__()
{
destroy();
LOG("Releasing plugin\n");
if(__plugin__.name) free(__plugin__.name);
//if(__plugin__.dbpath) free(__plugin__.dbpath);
//if(__plugin__.htdocs) free(__plugin__.htdocs);
//if(__plugin__.pdir) free(__plugin__.pdir);
}
#endif
#endif

View File

@ -369,14 +369,10 @@ int request_socket(const char* ip, int port)
void ws_client_close(ws_client_t* wsclient)
{
int usessl = wsclient->antdsock->port_config->usessl;
port_config_t *ptr = wsclient->antdsock->port_config;
antd_close(wsclient->antdsock);
if(ptr)
free(ptr);
#ifdef USE_OPENSSL
if(usessl)
if(wsclient->ssl_ctx)
{
if(wsclient->ssl_ctx)
SSL_CTX_free(wsclient->ssl_ctx);
@ -389,34 +385,29 @@ void ws_client_close(ws_client_t* wsclient)
// DEPRECATED: ERR_remove_state(0);
ERR_free_strings();
}
#else
UNUSED(usessl);
#endif
}
//this is for the client side, not use for now
int ws_client_connect(ws_client_t* wsclient)
int ws_client_connect(ws_client_t* wsclient, port_config_t pcnf)
{
char ip[100];
int stat = ip_from_hostname(wsclient->host,ip);
if(stat == -1)
return -1;
int sock = request_socket(ip, wsclient->antdsock->port_config->port);
int sock = request_socket(ip, pcnf.port);
if(sock <= 0)
{
ERROR("Cannot request socket");
return -1;
}
// will be free
wsclient->antdsock->ip = strdup(ip);
wsclient->antdsock->sock = sock;
wsclient->antdsock->status = 0;
wsclient->antdsock->last_io = time(NULL);
wsclient->antdsock->port_config->sock = -1;
wsclient->antdsock->port_config->rules = NULL;
wsclient->antdsock->port_config->htdocs = NULL;
wsclient->antdsock->zstream = NULL;
#ifdef USE_OPENSSL
if(wsclient->antdsock->port_config->usessl)
if(pcnf.usessl)
{
SSL_library_init();
SSL_load_error_strings();

View File

@ -73,6 +73,6 @@ char* get_ip_address();
// client
void ws_client_close(ws_client_t* wsclient);
int ws_client_connect(ws_client_t* wsclient);
int ws_client_connect(ws_client_t* wsclient, port_config_t pcnf);
int ws_open_handshake(ws_client_t* client);
#endif