summaryrefslogtreecommitdiffstats
path: root/nanohttp
diff options
context:
space:
mode:
authorGravatar m0gg2006-11-27 12:47:27 +0000
committerGravatar m0gg2006-11-27 12:47:27 +0000
commit17d9b98c8525c74cd83a421f151062825fe66890 (patch)
tree615dc9207798af270a8a0d957e427cc59a29166c /nanohttp
parent504da9682a1e739b41cfdbb25721f518c83ebf83 (diff)
downloadcsoap-17d9b98c8525c74cd83a421f151062825fe66890.tar.gz
csoap-17d9b98c8525c74cd83a421f151062825fe66890.tar.bz2
Code cleanup
Diffstat (limited to 'nanohttp')
-rw-r--r--nanohttp/Makefile.am16
-rw-r--r--nanohttp/nanohttp-server.c9
-rw-r--r--nanohttp/nanohttp-socket.c52
-rw-r--r--nanohttp/nanohttp-ssl.c45
-rw-r--r--nanohttp/nanohttp-ssl.h65
5 files changed, 70 insertions, 117 deletions
diff --git a/nanohttp/Makefile.am b/nanohttp/Makefile.am
index e960f50..dae7909 100644
--- a/nanohttp/Makefile.am
+++ b/nanohttp/Makefile.am
@@ -3,16 +3,20 @@ lib_LTLIBRARIES=libnanohttp.la
libnanohttp_ladir=$(includedir)/nanohttp-@nanohttp_release@/nanohttp
libnanohttp_la_SOURCES=nanohttp-common.c nanohttp-socket.c nanohttp-client.c \
- nanohttp-server.c nanohttp-stream.c nanohttp-mime.c \
- nanohttp-request.c nanohttp-response.c \
- nanohttp-base64.c nanohttp-ssl.c nanohttp-logging.c \
- nanohttp-admin.c nanohttp-error.c
+ nanohttp-server.c nanohttp-stream.c nanohttp-mime.c \
+ nanohttp-request.c nanohttp-response.c nanohttp-base64.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-ssl.h nanohttp-logging.h \
- nanohttp-admin.h nanohttp-error.h
+ nanohttp-base64.h nanohttp-logging.h nanohttp-admin.h \
+ nanohttp-error.h
+
+if BUILD_WITH_SSL
+libnanohttp_la_SOURCES+=nanohttp-ssl.c
+libnanohttp_la_HEADERS+=nanohttp-ssl.h
+endif
libnanohttp_la_LDFLAGS= -version-info @nanohttp_version@ -release @nanohttp_release@
libnanohttp_la_CFLAGS=-I${top_srcdir} -D__NHTTP_INTERNAL=1
diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c
index b0e650d..8c16e5f 100644
--- a/nanohttp/nanohttp-server.c
+++ b/nanohttp/nanohttp-server.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-server.c,v 1.69 2006/11/26 20:13:06 m0gg Exp $
+* $Id: nanohttp-server.c,v 1.70 2006/11/27 12:47:27 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -86,7 +86,14 @@
#include "nanohttp-response.h"
#include "nanohttp-server.h"
#include "nanohttp-base64.h"
+#ifdef HAVE_SSL
+#ifdef HAVE_OPENSSL_SSL_H
+#include <openssl/ssl.h>
+#endif
#include "nanohttp-ssl.h"
+#else
+static inline int hssl_enabled(void) { return 0; }
+#endif
#include "nanohttp-admin.h"
typedef struct _conndata
diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c
index 3fd600b..4869cd7 100644
--- a/nanohttp/nanohttp-socket.c
+++ b/nanohttp/nanohttp-socket.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-socket.c,v 1.65 2006/11/26 20:13:06 m0gg Exp $
+* $Id: nanohttp-socket.c,v 1.66 2006/11/27 12:47:27 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -84,7 +84,12 @@ typedef int ssize_t;
#include "nanohttp-common.h"
#include "nanohttp-socket.h"
+#ifdef HAVE_SSL
+#ifdef HAVE_OPENSSL_SSL_H
+#include <openssl/ssl.h>
+#endif
#include "nanohttp-ssl.h"
+#endif
#include "nanohttp-request.h"
#include "nanohttp-server.h"
@@ -121,9 +126,21 @@ _hsocket_module_sys_destroy(void)
herror_t
hsocket_module_init(int argc, char **argv)
{
+#ifdef HAVE_SSL
+ herror_t status;
+#endif
+
_hsocket_module_sys_init(argc, argv);
- return hssl_module_init(argc, argv);
+#ifdef HAVE_SSL
+ if ((status = hssl_module_init(argc, argv)) != H_OK)
+ {
+ log_error2("hssl_module_init failed (%s)", herror_message(status));
+ return status;
+ }
+#endif
+
+ return H_OK;
}
void
@@ -177,13 +194,12 @@ hsocket_open(struct hsocket_t * dsock, const char *hostname, int port, int ssl)
log_verbose4("Opening %s://%s:%i", ssl ? "https" : "http", hostname, port);
/* connect to the server */
- if (connect(dsock->sock, (struct sockaddr *) &address, sizeof(address)) !=
- 0)
- return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT,
- "Socket error (%s)", strerror(errno));
+ if (connect(dsock->sock, (struct sockaddr *) &address, sizeof(address)) != 0)
+ return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT, "Socket error (%s)", strerror(errno));
if (ssl)
{
+#ifdef HAVE_SSL
herror_t status;
if ((status = hssl_client_ssl(dsock)) != H_OK)
@@ -191,8 +207,10 @@ hsocket_open(struct hsocket_t * dsock, const char *hostname, int port, int ssl)
log_error2("hssl_client_ssl failed (%s)", herror_message(status));
return status;
}
+#else
+ return herror_new("hssl_client_ssl", 0, "SSL wasn't enabled at compile time");
+#endif
}
-
return H_OK;
}
@@ -287,11 +305,13 @@ hsocket_accept(struct hsocket_t * sock, struct hsocket_t * dest)
if ((status = _hsocket_sys_accept(sock, dest)) != H_OK)
return status;
+#ifdef HAVE_SSL
if ((status = hssl_server_ssl(dest)) != H_OK)
{
log_warn2("SSL startup failed (%s)", herror_message(status));
return status;
}
+#endif
log_verbose3("accepting connection from '%s' socket=%d",
SAVE_STR(((char *) inet_ntoa(dest->addr.sin_addr))),
@@ -350,7 +370,9 @@ hsocket_close(struct hsocket_t * sock)
{
log_verbose3("closing socket %p (%d)...", sock, sock->sock);
+#ifdef HAVE_SSL
hssl_cleanup(sock);
+#endif
_hsocket_sys_close(sock);
@@ -365,7 +387,9 @@ hsocket_close(struct hsocket_t * sock)
herror_t
hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n)
{
+#ifdef HAVE_SSL
herror_t status;
+#endif
size_t total = 0;
size_t size;
@@ -378,11 +402,17 @@ hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n)
while (1)
{
+#ifdef HAVE_SSL
if ((status = hssl_write(sock, bytes + total, n, &size)) != H_OK)
{
log_warn2("hssl_write failed (%s)", herror_message(status));
return status;
}
+#else
+ if ((size = send(sock->sock, bytes + total, n, 0)) == -1)
+ return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, "send failed (%s)", strerror(errno));
+#endif
+ sock->bytes_received += size;
n -= size;
total += size;
@@ -434,11 +464,17 @@ hsocket_read(struct hsocket_t * sock, unsigned char * buffer, int total, int for
do
{
- if ((status = hssl_read(sock, &buffer[totalRead], (size_t) total - totalRead, &count)) != H_OK)
+#ifdef HAVE_SSL
+ if ((status = hssl_read(sock, buffer + totalRead, (size_t) total - totalRead, &count)) != H_OK)
{
log_warn2("hssl_read failed (%s)", herror_message(status));
return status;
}
+#else
+ if ((count = hsocket_select_recv(sock->sock, buffer + totalRead, (size_t) total - totalRead)) == -1)
+ return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, "recv failed (%s)", strerror(errno));
+#endif
+ sock->bytes_received += count;
if (!force)
{
diff --git a/nanohttp/nanohttp-ssl.c b/nanohttp/nanohttp-ssl.c
index 1a98ed5..8b8fb5d 100644
--- a/nanohttp/nanohttp-ssl.c
+++ b/nanohttp/nanohttp-ssl.c
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-ssl.c,v 1.32 2006/11/26 20:13:06 m0gg Exp $
+* $Id: nanohttp-ssl.c,v 1.33 2006/11/27 12:47:27 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2001-2005 Rochester Institute of Technology
@@ -65,7 +65,10 @@
#include <io.h>
#endif
-#ifdef HAVE_SSL
+#ifdef HAVE_OPENSSL_SSL_H
+#include <openssl/ssl.h>
+#endif
+
#ifdef HAVE_OPENSSL_RAND_H
#include <openssl/rand.h>
#endif
@@ -73,7 +76,6 @@
#ifdef HAVE_OPENSSL_ERR_H
#include <openssl/err.h>
#endif
-#endif
#include "nanohttp-error.h"
#include "nanohttp-common.h"
@@ -82,8 +84,6 @@
#include "nanohttp-ssl.h"
-#ifdef HAVE_SSL
-
static char *certificate = NULL;
static char *certpass = "";
static char *ca_list = NULL;
@@ -540,7 +540,6 @@ hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received)
return herror_new("hssl_read", HSOCKET_ERROR_RECEIVE,
"recv failed (%s)", strerror(errno));
}
- sock->bytes_received += count;
*received = count;
return H_OK;
@@ -567,41 +566,7 @@ hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent)
return herror_new("hssl_write", HSOCKET_ERROR_SEND, "send failed (%s)",
strerror(errno));
}
- sock->bytes_transmitted += count;
- *sent = count;
-
- return H_OK;
-}
-
-#else
-
-herror_t
-hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received)
-{
- int count;
-
- if ((count = hsocket_select_recv(sock->sock, buf, len)) == -1)
- return herror_new("hssl_read", HSOCKET_ERROR_RECEIVE, "recv failed (%s)",
- strerror(errno));
- sock->bytes_received += count;
- *received = count;
-
- return H_OK;
-}
-
-
-herror_t
-hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent)
-{
- int count;
-
- if ((count = send(sock->sock, buf, len, 0)) == -1)
- return herror_new("hssl_write", HSOCKET_ERROR_SEND, "send failed (%s)",
- strerror(errno));
- sock->bytes_received += count;
*sent = count;
return H_OK;
}
-
-#endif
diff --git a/nanohttp/nanohttp-ssl.h b/nanohttp/nanohttp-ssl.h
index 1b23be8..6df53e7 100644
--- a/nanohttp/nanohttp-ssl.h
+++ b/nanohttp/nanohttp-ssl.h
@@ -1,5 +1,5 @@
/******************************************************************
-* $Id: nanohttp-ssl.h,v 1.21 2006/11/24 17:28:07 m0gg Exp $
+* $Id: nanohttp-ssl.h,v 1.22 2006/11/27 12:47:27 m0gg Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2001-2005 Rochester Institute of Technology
@@ -24,16 +24,6 @@
#ifndef __nanohttp_ssl_h
#define __nanohttp_ssl_h
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#ifdef HAVE_SSL
-
-#ifdef HAVE_OPENSSL_SSL_H
-#include <openssl/ssl.h>
-#endif
-
/**
*
* Commandline argument to enabled SSL in the nanoHTTP server.
@@ -42,8 +32,7 @@
#define NHTTPD_ARG_HTTPS "-NHTTPS"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/**
@@ -127,56 +116,8 @@ extern int verify_sn(X509 * cert, int who, int nid, char *str);
*/
extern void hssl_set_user_verify(int func(X509 * cert));
-#ifdef __cplusplus
-}
-#endif
-
-#else /* HAVE_SSL */
-
-static inline herror_t
-hssl_module_init(int argc, char **argv)
-{
- return H_OK;
-}
-
-static inline void
-hssl_module_destroy(void)
-{
- return;
-}
-
-static inline int
-hssl_enabled(void)
-{
- return 0;
-}
-
-static inline herror_t
-hssl_client_ssl(struct hsocket_t *sock)
-{
- return H_OK;
-}
-
-static inline herror_t
-hssl_server_ssl(struct hsocket_t *sock)
-{
- return H_OK;
-}
-
-static inline void
-hssl_cleanup(struct hsocket_t *sock)
-{
- return;
-}
-
-#endif /* HAVE_SSL */
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
extern herror_t hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received);
+
extern herror_t hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent);
#ifdef __cplusplus