summaryrefslogtreecommitdiffstats
path: root/nanohttp
diff options
context:
space:
mode:
Diffstat (limited to 'nanohttp')
-rw-r--r--nanohttp/Makefile.am10
-rw-r--r--nanohttp/nanohttp-client.c8
-rw-r--r--nanohttp/nanohttp-client.h12
-rw-r--r--nanohttp/nanohttp-server.c72
-rw-r--r--nanohttp/nanohttp-server.h14
-rw-r--r--nanohttp/nanohttp-socket.c178
-rw-r--r--nanohttp/nanohttp-socket.h9
-rw-r--r--nanohttp/nanohttp-ssl.c132
-rw-r--r--nanohttp/nanohttp-ssl.h8
-rwxr-xr-xnanohttp/nanohttp-stream.c10
10 files changed, 252 insertions, 201 deletions
diff --git a/nanohttp/Makefile.am b/nanohttp/Makefile.am
index dae7909..f52cce0 100644
--- a/nanohttp/Makefile.am
+++ b/nanohttp/Makefile.am
@@ -1,3 +1,6 @@
+#
+# $Revision: 1.15 $
+#
lib_LTLIBRARIES=libnanohttp.la
libnanohttp_ladir=$(includedir)/nanohttp-@nanohttp_release@/nanohttp
@@ -8,10 +11,9 @@ libnanohttp_la_SOURCES=nanohttp-common.c nanohttp-socket.c nanohttp-client.c
nanohttp-logging.c nanohttp-admin.c nanohttp-error.c
libnanohttp_la_HEADERS=nanohttp-common.h nanohttp-socket.h nanohttp-client.h \
- nanohttp-server.h nanohttp-stream.h nanohttp-mime.h \
- nanohttp-request.h nanohttp-response.h \
- nanohttp-base64.h nanohttp-logging.h nanohttp-admin.h \
- nanohttp-error.h
+ nanohttp-server.h nanohttp-stream.h nanohttp-mime.h \
+ nanohttp-request.h nanohttp-response.h nanohttp-logging.h \
+ nanohttp-error.h nanohttp-base64.h nanohttp-admin.h
if BUILD_WITH_SSL
libnanohttp_la_SOURCES+=nanohttp-ssl.c
diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c
index 2392d81..1c4c6d7 100644
--- a/nanohttp/nanohttp-client.c
+++ b/nanohttp/nanohttp-client.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-client.c,v 1.47 2006/11/28 23:45:57 m0gg Exp $
+* $Id: nanohttp-client.c,v 1.48 2006/11/30 14:23:59 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -318,12 +318,12 @@ httpc_send_header(httpc_conn_t * conn)
if (walker->key && walker->value)
{
sprintf(buffer, "%s: %s\r\n", walker->key, walker->value);
- if ((status = hsocket_send(conn->sock, buffer)) != H_OK)
+ if ((status = hsocket_send_string(conn->sock, buffer)) != H_OK)
return status;
}
}
- return hsocket_send(conn->sock, "\r\n");
+ return hsocket_send_string(conn->sock, "\r\n");
}
/*--------------------------------------------------
@@ -429,7 +429,7 @@ _httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn,
}
log_verbose1("Sending request...");
- if ((status = hsocket_send(conn->sock, buffer)) != H_OK)
+ if ((status = hsocket_send_string(conn->sock, buffer)) != H_OK)
{
log_error2("Cannot send request (%s)", herror_message(status));
hsocket_close(conn->sock);
diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h
index b74130d..6e5d4c9 100644
--- a/nanohttp/nanohttp-client.h
+++ b/nanohttp/nanohttp-client.h
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-client.h,v 1.27 2006/11/24 17:28:07 m0gg Exp $
+ * $Id: nanohttp-client.h,v 1.28 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -24,6 +24,16 @@
#ifndef __nanohttp_client_h
#define __nanohttp_client_h
+/* XXX: Clean up nanohttp to make this unnecessary */
+#ifndef __NHTTP_INTERNAL
+#include <nanohttp/nanohttp-error.h>
+#include <nanohttp/nanohttp-common.h>
+#include <nanohttp/nanohttp-stream.h>
+#include <nanohttp/nanohttp-request.h>
+#include <nanohttp/nanohttp-response.h>
+#include <nanohttp/nanohttp-logging.h>
+#endif
+
typedef struct httpc_conn
{
struct hsocket_t *sock;
diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c
index 4c31421..87d740c 100644
--- a/nanohttp/nanohttp-server.c
+++ b/nanohttp/nanohttp-server.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-server.c,v 1.71 2006/11/28 23:45:57 m0gg Exp $
+* $Id: nanohttp-server.c,v 1.72 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -122,7 +122,6 @@ static volatile int _httpd_run = 1;
static struct hsocket_t _httpd_socket;
static int _httpd_port = 10000;
static int _httpd_max_connections = 20;
-static int _httpd_timeout = 10;
static hservice_t *_httpd_services_default = NULL;
static hservice_t *_httpd_services_head = NULL;
@@ -143,20 +142,36 @@ static sigset_t thrsigset;
static pthread_mutex_t _httpd_connection_lock;
#endif
-/**
- *
- * Set Sleep function platform depended
- *
- */
#ifdef WIN32
-static void _sys_sleep(int secs)
+BOOL WINAPI
+_httpd_term(DWORD sig)
+{
+ /* log_debug2 ("Got signal %d", sig); */
+ if (sig == _httpd_terminate_signal)
+ _httpd_run = 0;
+
+ return TRUE;
+}
+
+static void _httpd_sys_sleep(int secs)
{
Sleep(secs*1000);
return;
}
#else
-static inline void _sys_sleep(int secs)
+static void
+_httpd_term(int sig)
+{
+ log_debug2("Got signal %d", sig);
+
+ if (sig == _httpd_terminate_signal)
+ _httpd_run = 0;
+
+ return;
+}
+
+static inline void _httpd_sys_sleep(int secs)
{
sleep(secs);
@@ -185,7 +200,7 @@ _httpd_parse_arguments(int argc, char **argv)
}
else if (!strcmp(argv[i - 1], NHTTPD_ARG_TIMEOUT))
{
- _httpd_timeout = atoi(argv[i]);
+ hsocket_set_timeout(atoi(argv[i]));
}
}
@@ -337,13 +352,13 @@ httpd_get_port(void)
int
httpd_get_timeout(void)
{
- return _httpd_timeout;
+ return hsocket_get_timeout();
}
void
-httpd_set_timeout(int t)
+httpd_set_timeout(int secs)
{
- _httpd_timeout = t;
+ hsocket_set_timeout(secs);
return;
}
@@ -447,7 +462,7 @@ httpd_send_header(httpd_conn_t * res, int code, const char *text)
strcat(header, "\r\n");
/* send header */
- if ((status = hsocket_nsend(res->sock, header, strlen(header))) != H_OK)
+ if ((status = hsocket_send(res->sock, header, strlen(header))) != H_OK)
return status;
res->out = http_output_stream_new(res->sock, res->header);
@@ -834,29 +849,6 @@ httpd_add_headers(httpd_conn_t * conn, const hpair_t * values)
return;
}
-#ifdef WIN32
-BOOL WINAPI
-httpd_term(DWORD sig)
-{
- /* log_debug2 ("Got signal %d", sig); */
- if (sig == _httpd_terminate_signal)
- _httpd_run = 0;
-
- return TRUE;
-}
-#else
-void
-httpd_term(int sig)
-{
- log_debug2("Got signal %d", sig);
-
- if (sig == _httpd_terminate_signal)
- _httpd_run = 0;
-
- return;
-}
-#endif
-
/*
* -----------------------------------------------------
* FUNCTION: _httpd_register_signal_handler
@@ -868,13 +860,13 @@ _httpd_register_signal_handler(void)
log_verbose2("registering termination signal handler (SIGNAL:%d)",
_httpd_terminate_signal);
#ifdef WIN32
- if (SetConsoleCtrlHandler((PHANDLER_ROUTINE) httpd_term, TRUE) == FALSE)
+ if (SetConsoleCtrlHandler((PHANDLER_ROUTINE) _httpd_term, TRUE) == FALSE)
{
log_error1("Unable to install console event handler!");
}
#else
- signal(_httpd_terminate_signal, httpd_term);
+ signal(_httpd_terminate_signal, _httpd_term);
#endif
return;
@@ -905,7 +897,7 @@ _httpd_wait_for_empty_conn(void)
if (i >= _httpd_max_connections)
{
- _sys_sleep(1);
+ _httpd_sys_sleep(1);
i = -1;
}
else if (_httpd_connection[i].flag == CONNECTION_FREE)
diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h
index e85c64a..1b74344 100644
--- a/nanohttp/nanohttp-server.h
+++ b/nanohttp/nanohttp-server.h
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-server.h,v 1.25 2006/11/28 23:45:57 m0gg Exp $
+ * $Id: nanohttp-server.h,v 1.26 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -24,6 +24,16 @@
#ifndef __nanohttp_server_h
#define __nanohttp_server_h
+/* XXX: Clean up nanohttp to make this unnecessary */
+#ifndef __NHTTP_INTERNAL
+#include <nanohttp/nanohttp-error.h>
+#include <nanohttp/nanohttp-common.h>
+#include <nanohttp/nanohttp-stream.h>
+#include <nanohttp/nanohttp-request.h>
+#include <nanohttp/nanohttp-response.h>
+#include <nanohttp/nanohttp-logging.h>
+#endif
+
/**
*
* nanohttp command line flags
@@ -110,7 +120,7 @@ extern "C"
extern short httpd_get_port(void);
extern int httpd_get_timeout(void);
- extern void httpd_set_timeout(int t);
+ extern void httpd_set_timeout(int secs);
extern const char *httpd_get_protocol(void);
extern int httpd_get_conncount(void);
diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c
index 4869cd7..e2ecf3a 100644
--- a/nanohttp/nanohttp-socket.c
+++ b/nanohttp/nanohttp-socket.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-socket.c,v 1.66 2006/11/27 12:47:27 m0gg Exp $
+* $Id: nanohttp-socket.c,v 1.68 2006/11/30 14:25:07 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -90,10 +90,51 @@ typedef int ssize_t;
#endif
#include "nanohttp-ssl.h"
#endif
-#include "nanohttp-request.h"
-#include "nanohttp-server.h"
+
+static int _hsocket_timeout = 10;
#ifdef WIN32
+static herror_t
+_hsocket_sys_accept(struct hsocket_t * sock, struct hsocket_t * dest)
+{
+ struct hsocket_t sockfd;
+ int asize;
+
+ asize = sizeof(struct sockaddr_in);
+ while (1)
+ {
+ sockfd.sock = accept(sock->sock, (struct sockaddr *) &(dest->addr), &asize);
+ if (sockfd.sock == INVALID_SOCKET)
+ {
+ if (WSAGetLastError() != WSAEWOULDBLOCK)
+ return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, "Socket error (%s)", strerror(errno));
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ dest->sock = sockfd.sock;
+
+ return H_OK;
+}
+
+static void
+_hsocket_sys_close(struct hsocket_t * sock)
+{
+ char junk[10];
+
+ /* shutdown(sock,SD_RECEIVE); */
+
+ shutdown(sock->sock, SD_SEND);
+ while (recv(sock->sock, junk, sizeof(junk), 0) > 0);
+ /* nothing */
+ closesocket(sock->sock);
+
+ return;
+}
+
static void
_hsocket_module_sys_init(int argc, char **argv)
{
@@ -116,11 +157,38 @@ _hsocket_module_sys_init(int argc, char **argv)
{
return;
}
+
static inline void
_hsocket_module_sys_destroy(void)
{
return;
}
+
+static herror_t
+_hsocket_sys_accept(struct hsocket_t * sock, struct hsocket_t * dest)
+{
+ socklen_t len;
+
+ len = sizeof(struct sockaddr_in);
+
+ if ((dest->sock = accept(sock->sock, (struct sockaddr *) &(dest->addr), &len)) == -1)
+ {
+ log_warn2("accept failed (%s)", strerror(errno));
+ return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, "Cannot accept network connection (%s)", strerror(errno));
+ }
+
+ return H_OK;
+}
+
+static inline void
+_hsocket_sys_close(struct hsocket_t * sock)
+{
+ shutdown(sock->sock, SHUT_RDWR);
+
+ close(sock->sock);
+
+ return;
+}
#endif
herror_t
@@ -246,53 +314,6 @@ hsocket_bind(struct hsocket_t *dsock, unsigned short port)
return H_OK;
}
-#ifdef WIN32
-static herror_t
-_hsocket_sys_accept(struct hsocket_t * sock, struct hsocket_t * dest)
-{
- int asize;
- struct hsocket_t sockfd;
-
- asize = sizeof(struct sockaddr_in);
- while (1)
- {
- sockfd.sock = accept(sock->sock, (struct sockaddr *) &(dest->addr), &asize);
- if (sockfd.sock == INVALID_SOCKET)
- {
- if (WSAGetLastError() != WSAEWOULDBLOCK)
- return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT,
- "Socket error (%s)", strerror(errno));
- }
- else
- {
- break;
- }
- }
-
- dest->sock = sockfd.sock;
-
- return H_OK;
-}
-#else
-static herror_t
-_hsocket_sys_accept(struct hsocket_t * sock, struct hsocket_t * dest)
-{
- socklen_t len;
-
- len = sizeof(struct sockaddr_in);
-
- if ((dest->sock = accept(sock->sock, (struct sockaddr *) &(dest->addr), &len)) == -1)
- {
- log_warn2("accept failed (%s)", strerror(errno));
- return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT,
- "Cannot accept network connection (%s)",
- strerror(errno));
- }
-
- return H_OK;
-}
-#endif
-
herror_t
hsocket_accept(struct hsocket_t * sock, struct hsocket_t * dest)
{
@@ -337,34 +358,6 @@ hsocket_listen(struct hsocket_t * sock)
return H_OK;
}
-#ifdef WIN32
-static void
-_hsocket_sys_close(struct hsocket_t * sock)
-{
- char junk[10];
-
- /* shutdown(sock,SD_RECEIVE); */
-
- shutdown(sock->sock, SD_SEND);
- while (recv(sock->sock, junk, sizeof(junk), 0) > 0);
- /* nothing */
- closesocket(sock->sock);
-
- return;
-}
-#else
-static inline void
-_hsocket_sys_close(struct hsocket_t * sock)
-{
-
- shutdown(sock->sock, SHUT_RDWR);
-
- close(sock->sock);
-
- return;
-}
-#endif
-
void
hsocket_close(struct hsocket_t * sock)
{
@@ -385,7 +378,7 @@ hsocket_close(struct hsocket_t * sock)
}
herror_t
-hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n)
+hsocket_send(struct hsocket_t * sock, const unsigned char * bytes, int n)
{
#ifdef HAVE_SSL
herror_t status;
@@ -393,9 +386,8 @@ hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n)
size_t total = 0;
size_t size;
- log_verbose2("Starting to send on sock=%p", &sock);
if (sock->sock < 0)
- return herror_new("hsocket_nsend", HSOCKET_ERROR_NOT_INITIALIZED,
+ return herror_new("hsocket_send", HSOCKET_ERROR_NOT_INITIALIZED,
"hsocket not initialized");
/* log_verbose2( "SENDING %s", bytes ); */
@@ -410,7 +402,7 @@ hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n)
}
#else
if ((size = send(sock->sock, bytes + total, n, 0)) == -1)
- return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, "send failed (%s)", strerror(errno));
+ return herror_new("hsocket_send", HSOCKET_ERROR_SEND, "send failed (%s)", strerror(errno));
#endif
sock->bytes_received += size;
@@ -424,9 +416,9 @@ hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n)
}
herror_t
-hsocket_send(struct hsocket_t * sock, const char *str)
+hsocket_send_string(struct hsocket_t * sock, const char *str)
{
- return hsocket_nsend(sock, str, strlen(str));
+ return hsocket_send(sock, str, strlen(str));
}
int
@@ -438,13 +430,13 @@ hsocket_select_recv(int sock, char *buf, size_t len)
FD_ZERO(&fds);
FD_SET(sock, &fds);
- timeout.tv_sec = httpd_get_timeout();
+ timeout.tv_sec = _hsocket_timeout;
timeout.tv_usec = 0;
if (select(sock + 1, &fds, NULL, NULL, &timeout) == 0)
{
errno = ETIMEDOUT;
- log_verbose2("Socket %d timeout", sock);
+ log_verbose2("Socket %d timed out", sock);
return -1;
}
@@ -498,3 +490,17 @@ hsocket_read(struct hsocket_t * sock, unsigned char * buffer, int total, int for
}
while (1);
}
+
+int
+hsocket_get_timeout(void)
+{
+ return _hsocket_timeout;
+}
+
+void
+hsocket_set_timeout(int secs)
+{
+ _hsocket_timeout = secs;
+
+ return;
+}
diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h
index db2bf31..b71f38d 100644
--- a/nanohttp/nanohttp-socket.h
+++ b/nanohttp/nanohttp-socket.h
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-socket.h,v 1.33 2006/11/26 20:13:06 m0gg Exp $
+ * $Id: nanohttp-socket.h,v 1.34 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -175,7 +175,7 @@ extern herror_t hsocket_accept(struct hsocket_t *sock, struct hsocket_t *dest);
* - HSOCKET_ERROR_NOT_INITIALIZED
* - HSOCKET_ERROR_SEND
*/
-extern herror_t hsocket_nsend(struct hsocket_t *sock, const unsigned char *bytes, int size);
+extern herror_t hsocket_send(struct hsocket_t *sock, const unsigned char *bytes, int size);
/**
*
@@ -189,7 +189,7 @@ extern herror_t hsocket_nsend(struct hsocket_t *sock, const unsigned char *bytes
* - HSOCKET_ERROR_SEND
*
*/
-extern herror_t hsocket_send(struct hsocket_t *sock, const char *str);
+extern herror_t hsocket_send_string(struct hsocket_t *sock, const char *str);
extern int hsocket_select_recv(int sock, char *buf, size_t len);
@@ -210,6 +210,9 @@ extern int hsocket_select_recv(int sock, char *buf, size_t len);
*/
extern herror_t hsocket_read(struct hsocket_t * sock, unsigned char *buffer, int size, int force, int *readed);
+extern int hsocket_get_timeout(void);
+extern void hsocket_set_timeout(int secs);
+
#ifdef __cplusplus
}
#endif
diff --git a/nanohttp/nanohttp-ssl.c b/nanohttp/nanohttp-ssl.c
index 8b8fb5d..a5f32e3 100644
--- a/nanohttp/nanohttp-ssl.c
+++ b/nanohttp/nanohttp-ssl.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-ssl.c,v 1.33 2006/11/27 12:47:27 m0gg Exp $
+* $Id: nanohttp-ssl.c,v 1.34 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2001-2005 Rochester Institute of Technology
@@ -84,12 +84,11 @@
#include "nanohttp-ssl.h"
-static char *certificate = NULL;
-static char *certpass = "";
-static char *ca_list = NULL;
-static SSL_CTX *context = NULL;
-
-static int enabled = 0;
+static char *_hssl_certificate = NULL;
+static char *_hssl_certpass = NULL;
+static char *_hssl_ca_list = NULL;
+static SSL_CTX *_hssl_context = NULL;
+static int _hssl_enabled = 0;
static int
_hssl_dummy_verify_cert(X509 * cert)
@@ -99,7 +98,7 @@ _hssl_dummy_verify_cert(X509 * cert)
/* connect to anyone */
- log_verbose1("Validating certificate.");
+ log_verbose1("_Not_ validating certificate.");
return 1;
}
@@ -176,12 +175,15 @@ _hssl_password_callback(char *buf, int num, int rwflag, void *userdata)
{
int ret;
- ret = strlen(certpass);
+ if (!_hssl_certpass)
+ return 0;
+
+ ret = strlen(_hssl_certpass);
if (num < ret + 1)
return 0;
- strcpy(buf, certpass);
+ strcpy(buf, _hssl_certpass);
return ret;
}
@@ -221,25 +223,34 @@ hssl_set_hssl_verify_cert(int func(X509 * cert))
}
void
-hssl_set_certificate(char *c)
+hssl_set_certificate(const char *filename)
{
- certificate = c;
+ if (_hssl_certificate)
+ free(_hssl_certificate);
+
+ _hssl_certificate = strdup(filename);
return;
}
void
-hssl_set_certpass(char *c)
+hssl_set_certpass(const char *password)
{
- certpass = c;
+ if (_hssl_certpass)
+ free(_hssl_certpass);
+
+ _hssl_certpass = strdup(password);
return;
}
void
-hssl_set_ca(char *c)
+hssl_set_ca_list(const char *filename)
{
- ca_list = c;
+ if (_hssl_ca_list)
+ free(_hssl_ca_list);
+
+ _hssl_ca_list = strdup(filename);
return;
}
@@ -247,7 +258,7 @@ hssl_set_ca(char *c)
void
hssl_enable(void)
{
- enabled = 1;
+ _hssl_enabled = 1;
return;
}
@@ -257,23 +268,23 @@ _hssl_parse_arguments(int argc, char **argv)
{
int i;
- for (i = 1; i < argc; i++)
+ for (i=1; i<argc; i++)
{
if (!strcmp(argv[i - 1], NHTTP_ARG_CERT))
{
- certificate = argv[i];
+ hssl_set_certificate(argv[i]);
}
else if (!strcmp(argv[i - 1], NHTTP_ARG_CERTPASS))
{
- certpass = argv[i];
+ hssl_set_certpass(argv[i]);
}
else if (!strcmp(argv[i - 1], NHTTP_ARG_CA))
{
- ca_list = argv[i];
+ hssl_set_ca_list(argv[i]);
}
else if (!strcmp(argv[i - 1], NHTTPD_ARG_HTTPS))
{
- enabled = 1;
+ hssl_enabled();
}
}
@@ -305,57 +316,57 @@ _hssl_library_init(void)
static herror_t
_hssl_server_context_init(void)
{
- log_verbose3("enabled=%i, certificate=%p", enabled, certificate);
+ log_verbose3("enabled=%i, certificate=%p", _hssl_enabled, _hssl_certificate);
- if (!enabled || !certificate)
+ if (!_hssl_enabled || !_hssl_certificate)
return H_OK;
- if (!(context = SSL_CTX_new(SSLv23_method())))
+ if (!(_hssl_context = SSL_CTX_new(SSLv23_method())))
{
log_error1("Cannot create SSL context");
return herror_new("_hssl_server_context_init", HSSL_ERROR_CONTEXT,
"Unable to create SSL context");
}
- if (!(SSL_CTX_use_certificate_file(context, certificate, SSL_FILETYPE_PEM)))
+ if (!(SSL_CTX_use_certificate_file(_hssl_context, _hssl_certificate, SSL_FILETYPE_PEM)))
{
- log_error2("Cannot read certificate file: \"%s\"", certificate);
- SSL_CTX_free(context);
+ log_error2("Cannot read certificate file: \"%s\"", _hssl_certificate);
+ SSL_CTX_free(_hssl_context);
return herror_new("_hssl_server_context_init", HSSL_ERROR_CERTIFICATE,
- "Unable to use SSL certificate \"%s\"", certificate);
+ "Unable to use SSL certificate \"%s\"", _hssl_certificate);
}
- SSL_CTX_set_default_passwd_cb(context, _hssl_password_callback);
+ SSL_CTX_set_default_passwd_cb(_hssl_context, _hssl_password_callback);
- if (!(SSL_CTX_use_PrivateKey_file(context, certificate, SSL_FILETYPE_PEM)))
+ if (!(SSL_CTX_use_PrivateKey_file(_hssl_context, _hssl_certificate, SSL_FILETYPE_PEM)))
{
- log_error2("Cannot read key file: \"%s\"", certificate);
- SSL_CTX_free(context);
+ log_error2("Cannot read key file: \"%s\"", _hssl_certificate);
+ SSL_CTX_free(_hssl_context);
return herror_new("_hssl_server_context_init", HSSL_ERROR_PEM,
"Unable to use private key");
}
- if (ca_list != NULL && *ca_list != '\0')
+ if (_hssl_ca_list != NULL && *_hssl_ca_list != '\0')
{
- if (!(SSL_CTX_load_verify_locations(context, ca_list, NULL)))
+ if (!(SSL_CTX_load_verify_locations(_hssl_context, _hssl_ca_list, NULL)))
{
- SSL_CTX_free(context);
- log_error2("Cannot read CA list: \"%s\"", ca_list);
+ SSL_CTX_free(_hssl_context);
+ log_error2("Cannot read CA list: \"%s\"", _hssl_ca_list);
return herror_new("_hssl_server_context_init", HSSL_ERROR_CA_LIST,
"Unable to read certification authorities \"%s\"");
}
- SSL_CTX_set_client_CA_list(context, SSL_load_client_CA_file(ca_list));
+ SSL_CTX_set_client_CA_list(_hssl_context, SSL_load_client_CA_file(_hssl_ca_list));
log_verbose1("Certification authority contacted");
}
- SSL_CTX_set_verify(context, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
+ SSL_CTX_set_verify(_hssl_context, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
_hssl_cert_verify_callback);
log_verbose1("Certificate verification callback registered");
- SSL_CTX_set_mode(context, SSL_MODE_AUTO_RETRY);
+ SSL_CTX_set_mode(_hssl_context, SSL_MODE_AUTO_RETRY);
- SSL_CTX_set_session_cache_mode(context, SSL_SESS_CACHE_OFF);
+ SSL_CTX_set_session_cache_mode(_hssl_context, SSL_SESS_CACHE_OFF);
_hssl_superseed();
@@ -365,10 +376,10 @@ _hssl_server_context_init(void)
static void
_hssl_server_context_destroy(void)
{
- if (context)
+ if (_hssl_context)
{
- SSL_CTX_free(context);
- context = NULL;
+ SSL_CTX_free(_hssl_context);
+ _hssl_context = NULL;
}
return;
}
@@ -378,7 +389,7 @@ hssl_module_init(int argc, char **argv)
{
_hssl_parse_arguments(argc, argv);
- if (enabled)
+ if (_hssl_enabled)
{
_hssl_library_init();
log_verbose1("SSL enabled");
@@ -396,13 +407,31 @@ hssl_module_destroy(void)
{
_hssl_server_context_destroy();
+ if (_hssl_certpass)
+ {
+ free(_hssl_certpass);
+ _hssl_certpass = NULL;
+ }
+
+ if (_hssl_ca_list)
+ {
+ free(_hssl_ca_list);
+ _hssl_ca_list = NULL;
+ }
+
+ if (_hssl_certificate)
+ {
+ free(_hssl_certificate);
+ _hssl_certificate = NULL;
+ }
+
return;
}
int
hssl_enabled(void)
{
- return enabled;
+ return _hssl_enabled;
}
herror_t
@@ -413,7 +442,7 @@ hssl_client_ssl(struct hsocket_t * sock)
log_verbose1("Starting SSL client initialization");
- if (!(ssl = SSL_new(context)))
+ if (!(ssl = SSL_new(_hssl_context)))
{
log_error1("Cannot create new SSL object");
return herror_new("hssl_client_ssl", HSSL_ERROR_CLIENT, "SSL_new failed");
@@ -452,7 +481,6 @@ hssl_client_ssl(struct hsocket_t * sock)
static int
_hssl_bio_read(BIO * b, char *out, int outl)
{
-
return hsocket_select_recv(b->num, out, outl);;
}
@@ -463,12 +491,12 @@ hssl_server_ssl(struct hsocket_t *sock)
int ret;
BIO *sbio;
- if (!enabled)
+ if (!_hssl_enabled)
return H_OK;
log_verbose2("Starting SSL initialization for socket %d", sock->sock);
- if (!(ssl = SSL_new(context)))
+ if (!(ssl = SSL_new(_hssl_context)))
{
log_warn1("SSL_new failed");
return herror_new("hssl_server_ssl", HSSL_ERROR_SERVER,
@@ -530,7 +558,7 @@ hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received)
if (sock->ssl)
{
if ((count = SSL_read(sock->ssl, buf, len)) < 1)
- return herror_new("SSL_read", HSOCKET_ERROR_RECEIVE,
+ return herror_new("hssl_read", HSOCKET_ERROR_RECEIVE,
"SSL_read failed (%s)", _hssl_get_error(sock->ssl,
count));
}
@@ -556,7 +584,7 @@ hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent)
if (sock->ssl)
{
if ((count = SSL_write(sock->ssl, buf, len)) == -1)
- return herror_new("SSL_write", HSOCKET_ERROR_SEND,
+ return herror_new("hssl_write", HSOCKET_ERROR_SEND,
"SSL_write failed (%s)", _hssl_get_error(sock->ssl,
count));
}
diff --git a/nanohttp/nanohttp-ssl.h b/nanohttp/nanohttp-ssl.h
index 6df53e7..8902ea3 100644
--- a/nanohttp/nanohttp-ssl.h
+++ b/nanohttp/nanohttp-ssl.h
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-ssl.h,v 1.22 2006/11/27 12:47:27 m0gg Exp $
+* $Id: nanohttp-ssl.h,v 1.23 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2001-2005 Rochester Institute of Technology
@@ -51,9 +51,9 @@ extern herror_t hssl_module_init(int argc, char **argv);
*/
extern void hssl_module_destroy(void);
-extern void hssl_set_certificate(char *c);
-extern void hssl_set_certpass(char *c);
-extern void hssl_set_ca(char *c);
+extern void hssl_set_certificate(const char *filename);
+extern void hssl_set_certpass(const char *password);
+extern void hssl_set_ca_list(const char *filename);
/**
*
diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c
index 59888d1..b121e57 100755
--- a/nanohttp/nanohttp-stream.c
+++ b/nanohttp/nanohttp-stream.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-stream.c,v 1.17 2006/11/25 15:06:58 m0gg Exp $
+* $Id: nanohttp-stream.c,v 1.18 2006/11/30 14:24:00 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003-2004 Ferhat Ayaz
@@ -570,19 +570,19 @@ http_output_stream_write(struct http_output_stream_t * stream,
if (stream->type == HTTP_TRANSFER_CHUNKED)
{
sprintf(chunked, "%x\r\n", size);
- if ((status = hsocket_send(stream->sock, chunked)) != H_OK)
+ if ((status = hsocket_send_string(stream->sock, chunked)) != H_OK)
return status;
}
if (size > 0)
{
- if ((status = hsocket_nsend(stream->sock, bytes, size)) != H_OK)
+ if ((status = hsocket_send(stream->sock, bytes, size)) != H_OK)
return status;
}
if (stream->type == HTTP_TRANSFER_CHUNKED)
{
- if ((status = hsocket_send(stream->sock, "\r\n")) != H_OK)
+ if ((status = hsocket_send_string(stream->sock, "\r\n")) != H_OK)
return status;
}
@@ -608,7 +608,7 @@ http_output_stream_flush(struct http_output_stream_t * stream)
if (stream->type == HTTP_TRANSFER_CHUNKED)
{
- if ((status = hsocket_send(stream->sock, "0\r\n\r\n")) != H_OK)
+ if ((status = hsocket_send_string(stream->sock, "0\r\n\r\n")) != H_OK)
return status;
}