[Nodogsplash] [PATCH 3/3] Don't share buffers between http threads

Sven Eckelmann sven at open-mesh.com
Fri Nov 8 19:17:18 CET 2013


It is not save to share buffer between different threads when multiple users
read and write to it. Instead the own stack can be used to save the
information per thread.

Signed-off-by: Sven Eckelmann <sven at open-mesh.com>
---
 libhttpd/api.c      |  7 +++----
 libhttpd/httpd.h    |  2 +-
 libhttpd/protocol.c | 15 ++++++++-------
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/libhttpd/api.c b/libhttpd/api.c
index 558ae92..ab8844d 100644
--- a/libhttpd/api.c
+++ b/libhttpd/api.c
@@ -72,10 +72,8 @@ char *httpdUrlEncode(str)
 
 
 
-char *httpdRequestMethodName(request *r)
+char *httpdRequestMethodName(request *r, char tmpBuf[256])
 {
-	static	char	tmpBuf[255];
-
 	switch(r->request.method)
 	{
 		case HTTP_GET: return("GET");
@@ -83,6 +81,7 @@ char *httpdRequestMethodName(request *r)
 		default:
 			snprintf(tmpBuf,255,"Invalid method '%d'",
 				r->request.method);
+			tmpBuf[255] = '\0';
 			return(tmpBuf);
 	}
 }
@@ -377,7 +376,7 @@ request *httpdGetConnection(server, timeout)
 
 int httpdReadRequest(httpd *server, request *r)
 {
-	static	char	buf[HTTP_MAX_LEN];
+	char	buf[HTTP_MAX_LEN];
 	int	count,
 		inHeaders;
 	char	*cp, *cp2;
diff --git a/libhttpd/httpd.h b/libhttpd/httpd.h
index cf1bb0b..89b4b08 100644
--- a/libhttpd/httpd.h
+++ b/libhttpd/httpd.h
@@ -206,7 +206,7 @@ int httpdReadRequest __ANSI_PROTO((httpd*, request*));
 int httpdCheckAcl __ANSI_PROTO((httpd*, request *, httpAcl*));
 int httpdAddC404Content __ANSI_PROTO((httpd*,void(*)()));
 
-char *httpdRequestMethodName __ANSI_PROTO((request*));
+char *httpdRequestMethodName __ANSI_PROTO((request*, char *));
 char *httpdUrlEncode __ANSI_PROTO((const char *));
 
 void httpdAddHeader __ANSI_PROTO((request*, const char*));
diff --git a/libhttpd/protocol.c b/libhttpd/protocol.c
index 2415be4..81784e0 100644
--- a/libhttpd/protocol.c
+++ b/libhttpd/protocol.c
@@ -104,7 +104,7 @@ int _httpd_readLine(request *r, char *destBuf, int len)
 	int	count;
 	
 
-	count = 0;
+	count = 1;
 	dst = destBuf;
 	while(count < len)
 	{
@@ -157,6 +157,7 @@ void _httpd_writeAccessLog(httpd *server, request *r)
 	struct 	tm *timePtr;
 	time_t	clock;
 	int	responseCode;
+	char tmpbuf[256];
 
 
 	if (server->accessLog == NULL)
@@ -166,7 +167,7 @@ void _httpd_writeAccessLog(httpd *server, request *r)
 	strftime(dateBuf, 30, "%d/%b/%Y:%T %Z",  timePtr);
 	responseCode = atoi(r->response.response);
 	fprintf(server->accessLog, "%s - - [%s] %s \"%s\" %d %d\n", 
-		r->clientAddr, dateBuf, httpdRequestMethodName(r), 
+		r->clientAddr, dateBuf, httpdRequestMethodName(r, tmpbuf), 
 		httpdRequestPath(r), responseCode, 
 		r->response.responseLength);
 }
@@ -202,7 +203,7 @@ int _httpd_decode (bufcoded, bufplain, outbufsize)
 	char *		bufplain;
 	int		outbufsize;
 {
-	static char six2pr[64] = {
+	static const char six2pr[64] = {
     		'A','B','C','D','E','F','G','H','I','J','K','L','M',
     		'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
     		'a','b','c','d','e','f','g','h','i','j','k','l','m',
@@ -210,13 +211,13 @@ int _httpd_decode (bufcoded, bufplain, outbufsize)
     		'0','1','2','3','4','5','6','7','8','9','+','/'   
 	};
   
-	static unsigned char pr2six[256];
+	unsigned char pr2six[256];
 
 	/* single character decode */
 #	define DEC(c) pr2six[(int)c]
 #	define _DECODE_MAXVAL 63
 
-	static int first = 1;
+	int first = 1;
 
 	int nbytesdecoded, j;
 	register char *bufin = bufcoded;
@@ -665,7 +666,7 @@ int _httpd_checkLastModified(request *r, int modTime)
 }
 
 
-static unsigned char isAcceptable[96] =
+static const unsigned char isAcceptable[96] =
 
 /* Overencodes */
 #define URL_XALPHAS     (unsigned char) 1
@@ -685,7 +686,7 @@ static unsigned char isAcceptable[96] =
  
 #define ACCEPTABLE(a)   ( a!='&' && a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
 
-static char *hex = "0123456789ABCDEF";
+static const char *hex = "0123456789ABCDEF";
 
 
 char *_httpd_escape(str)
-- 
1.8.4.2




More information about the Nodogsplash mailing list