1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-06 06:29:46 +02:00

ssl alpha support

This commit is contained in:
Xuan Sang LE 2018-02-10 12:24:01 +01:00
parent aba40b16f5
commit 708e54989d
6 changed files with 31 additions and 23 deletions

View File

@ -17,9 +17,8 @@ void accept_request(void* 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); //LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock);
numchars = get_line(((antd_client_t*)client)->sock, buf, sizeof(buf)); numchars = read_buf(client, 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))
{ {
@ -236,7 +235,7 @@ void error_die(const char *sc)
* Returns: the number of bytes stored (excluding null) */ * Returns: the number of bytes stored (excluding null) */
/**********************************************************************/ /**********************************************************************/
//This function is deprecate //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;
char c = '\0'; char c = '\0';
@ -266,7 +265,7 @@ int get_line(int sock, char *buf, int size)
buf[i] = '\0'; buf[i] = '\0';
return(i); return(i);
} }*/
/**********************************************************************/ /**********************************************************************/
@ -462,7 +461,6 @@ dictionary decode_request(void* 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,":");
@ -867,7 +865,7 @@ 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",client ); LOG("Client %d\n",((antd_client_t*)client)->sock );
LOG("Path : '%s'\n", rpath); LOG("Path : '%s'\n", rpath);
LOG("Method:%s\n", method); LOG("Method:%s\n", method);
LOG("Plugin name '%s'\n",pname); LOG("Plugin name '%s'\n",pname);

View File

@ -28,7 +28,7 @@ void accept_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*, const char *);
int startup(unsigned *); int startup(unsigned *);

21
httpd.c
View File

@ -136,9 +136,9 @@ void load_config(const char* file)
{ {
LOG("Using configuration : %s\n", file); LOG("Using configuration : %s\n", file);
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
LOG("Enable %d\n", server_config.usessl); LOG("SSL enable %d\n", server_config.usessl);
LOG("cert %s\n", server_config.sslcert); LOG("SSL cert %s\n", server_config.sslcert);
LOG("key %s\n", server_config.sslkey); LOG("SSL key %s\n", server_config.sslkey);
#endif #endif
} }
init_file_system(); init_file_system();
@ -185,7 +185,7 @@ int main(int argc, char* argv[])
while (1) while (1)
{ {
antd_client_t client; antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
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)
{ {
@ -195,26 +195,27 @@ int main(int argc, char* argv[])
/* accept_request(client_sock); */ /* accept_request(client_sock); */
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
client.ssl = NULL; client->ssl = NULL;
if(server_config.usessl == 1) if(server_config.usessl == 1)
{ {
client.ssl = (void*)SSL_new(ctx); client->ssl = (void*)SSL_new(ctx);
SSL_set_fd((SSL*)client.ssl, client_sock); SSL_set_fd((SSL*)client->ssl, client_sock);
if (SSL_accept((SSL*)client.ssl) <= 0) { if (SSL_accept((SSL*)client->ssl) <= 0) {
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
continue; continue;
} }
} }
#endif #endif
client.sock = client_sock; client->sock = client_sock;
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)&client) != 0) if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0)
perror("pthread_create"); perror("pthread_create");
else else
{ {
//reclaim the stack data when thread finish //reclaim the stack data when thread finish
pthread_detach(newthread) ; pthread_detach(newthread) ;
} }
//accept_request(&client);
} }
close(server_sock); close(server_sock);

View File

@ -70,10 +70,12 @@ int response(void* client, const char* data)
} }
int antd_send(const void *src, const void* data, int len, int _ssl) int antd_send(const void *src, const void* data, int len, int _ssl)
{ {
if(!src) return -1;
antd_client_t * source = (antd_client_t *) src; antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
if(_ssl) if(_ssl)
{ {
//LOG("SSL WRITE\n");
return SSL_write((SSL*) source->ssl, data, len); return SSL_write((SSL*) source->ssl, data, len);
} }
else else
@ -86,10 +88,12 @@ int antd_send(const void *src, const void* data, int len, int _ssl)
} }
int antd_recv(const void *src, void* data, int len, int _ssl) int antd_recv(const void *src, void* data, int len, int _ssl)
{ {
if(!src) return -1;
antd_client_t * source = (antd_client_t *) src; antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
if(_ssl) if(_ssl)
{ {
//LOG("SSL READ\n");
return SSL_read((SSL*) source->ssl, data, len); return SSL_read((SSL*) source->ssl, data, len);
} }
else else
@ -102,15 +106,18 @@ int antd_recv(const void *src, void* data, int len, int _ssl)
} }
int antd_close(void* src) int antd_close(void* src)
{ {
if(!src) return -1;
antd_client_t * source = (antd_client_t *) src; antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
if(source->ssl && usessl()){ if(source->ssl && usessl()){
SSL_free((SSL*) source->ssl); SSL_free((SSL*) source->ssl);
LOG("Freeing SSL\n"); //LOG("Freeing SSL\n");
} }
#endif #endif
printf("Close sock %d\n", source->sock); //printf("Close sock %d\n", source->sock);
close(source->sock); int ret = close(source->sock);
free(src);
return ret;
} }
int __ti(void* client,int data) int __ti(void* client,int data)
{ {

View File

@ -34,12 +34,13 @@ sqldb getdb()
} }
#endif #endif
#ifdef USE_OPENSSL /*#ifdef USE_OPENSSL
int usessl() int usessl()
{ {
LOG("CALLED from plugin \n");
return __plugin__.usessl; return __plugin__.usessl;
} }
#endif #endif*/
char* route(const char* repath) char* route(const char* repath)
{ {

View File

@ -81,6 +81,7 @@ char* __s(const char* fstring,...)
*/ */
void trim(char* str, const char delim) void trim(char* str, const char delim)
{ {
if(!str) return;
char * p = str; char * p = str;
int l = strlen(p); int l = strlen(p);