style cleaning

Signed-off-by: Andy Green <andy.green@linaro.org>
diff --git a/lib/base64-decode.c b/lib/base64-decode.c
index 428b9c6..95d4ba4 100644
--- a/lib/base64-decode.c
+++ b/lib/base64-decode.c
@@ -66,33 +66,21 @@
 			} else
 				triple[i] = 0;
 		}
-		if (len) {
+		if (!len)
+			continue;
 
-			if (done + 4 >= out_size)
-				return -1;
+		if (done + 4 >= out_size)
+			return -1;
 
-			*out++ = encode[triple[0] >> 2];
-			*out++ = encode[((triple[0] & 0x03) << 4) |
-						     ((triple[1] & 0xf0) >> 4)];
-			*out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
+		*out++ = encode[triple[0] >> 2];
+		*out++ = encode[((triple[0] & 0x03) << 4) |
+					     ((triple[1] & 0xf0) >> 4)];
+		*out++ = (len > 1 ? encode[((triple[1] & 0x0f) << 2) |
 					     ((triple[2] & 0xc0) >> 6)] : '=');
-			*out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
+		*out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
 
-			done += 4;
-			line += 4;
-		}
-#if 0
-		if (line >= 72) {
-
-			if (done + 2 >= out_size)
-				return -1;
-
-			*out++ = '\r';
-			*out++ = '\n';
-			done += 2;
-			line = 0;
-		}
-#endif
+		done += 4;
+		line += 4;
 	}
 
 	if (done + 1 >= out_size)
@@ -168,10 +156,10 @@
 	char buf[64];
 	int n;
 	int test;
-	static const char *plaintext[] = {
+	static const char * const plaintext[] = {
 		"sanity check base 64"
 	};
-	static const char *coded[] = {
+	static const char * const coded[] = {
 		"c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
 	};
 
diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 18f2220..58761ca 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -2,7 +2,7 @@
 #include <netdb.h>
 
 
-struct libwebsocket * __libwebsocket_client_connect_2(
+struct libwebsocket *__libwebsocket_client_connect_2(
 	struct libwebsocket_context *context,
 	struct libwebsocket *wsi
 ) {
@@ -15,7 +15,7 @@
 	char pkt[512];
 	int opt = 1;
 #if defined(__APPLE__)
-    struct protoent* tcp_proto;
+	struct protoent *tcp_proto;
 #endif
 
 	debug("__libwebsocket_client_connect_2\n");
@@ -66,10 +66,12 @@
 
 	/* Disable Nagle */
 #if !defined(__APPLE__)
-	setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY, (const void *)&opt, sizeof(opt));
+	setsockopt(wsi->sock, SOL_TCP, TCP_NODELAY,
+					      (const void *)&opt, sizeof(opt));
 #else
-    tcp_proto = getprotobyname("TCP");
-    setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY, &opt, sizeof(opt));
+	tcp_proto = getprotobyname("TCP");
+	setsockopt(wsi->sock, tcp_proto->p_proto, TCP_NODELAY,
+							    &opt, sizeof(opt));
 #endif
 
 	/* Set receiving timeout */
@@ -78,7 +80,7 @@
 	setsockopt(wsi->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv);
 
 	if (connect(wsi->sock, (struct sockaddr *)&server_addr,
-					      sizeof(struct sockaddr)) == -1)  {
+					     sizeof(struct sockaddr)) == -1)  {
 		fprintf(stderr, "Connect failed\n");
 		goto oom4;
 	}
@@ -167,7 +169,7 @@
  *		the server, or just one.  The server will pick the one it
  *		likes best.
  * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
- * 		protocol supported, or the specific protocol ordinal
+ *		protocol supported, or the specific protocol ordinal
  *
  *	This function creates a connection to a remote server
  */
@@ -227,10 +229,12 @@
 	if (wsi->c_path == NULL)
 		goto bail1;
 	strcpy(wsi->c_path, path);
+
 	wsi->c_host = malloc(strlen(host) + 1);
 	if (wsi->c_host == NULL)
 		goto oom1;
 	strcpy(wsi->c_host, host);
+
 	if (origin) {
 		wsi->c_origin = malloc(strlen(origin) + 1);
 		strcpy(wsi->c_origin, origin);
@@ -238,6 +242,7 @@
 			goto oom2;
 	} else
 		wsi->c_origin = NULL;
+
 	if (protocol) {
 		wsi->c_protocol = malloc(strlen(protocol) + 1);
 		if (wsi->c_protocol == NULL)
@@ -246,7 +251,6 @@
 	} else
 		wsi->c_protocol = NULL;
 
-
 	/* set up appropriate masking */
 
 	wsi->xor_mask = xor_no_mask;
diff --git a/lib/handshake.c b/lib/handshake.c
index 6809be4..3455b39 100644
--- a/lib/handshake.c
+++ b/lib/handshake.c
@@ -21,6 +21,9 @@
 
 #include "private-libwebsockets.h"
 
+#define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
+#define LWS_CPYAPP_TOKEN(ptr, tok) { strcpy(p, wsi->utf8_token[tok].token); \
+		p += wsi->utf8_token[tok].token_len; }
 
 static int
 interpret_key(const char *key, unsigned long *result)
@@ -33,12 +36,13 @@
 	int rem = 0;
 
 	while (*p) {
-		if (isdigit(*p)) {
-			if (digit_pos == sizeof(digits) - 1)
-				return -1;
-			digits[digit_pos++] = *p;
+		if (!isdigit(*p)) {
+			p++;
+			continue;
 		}
-		p++;
+		if (digit_pos == sizeof(digits) - 1)
+			return -1;
+		digits[digit_pos++] = *p++;
 	}
 	digits[digit_pos] = '\0';
 	if (!digit_pos)
@@ -91,8 +95,9 @@
 		goto bail;
 
 	/* allocate the per-connection user memory (if any) */
-	if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
-          goto bail;
+	if (wsi->protocol->per_session_data_size &&
+					  !libwebsocket_ensure_user_space(wsi))
+		goto bail;
 
 	/* create the response packet */
 
@@ -111,41 +116,28 @@
 	}
 
 	p = response;
-	strcpy(p,   "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
-					  "Upgrade: WebSocket\x0d\x0a");
-	p += strlen("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
-					  "Upgrade: WebSocket\x0d\x0a");
-	strcpy(p,   "Connection: Upgrade\x0d\x0a"
-		    "Sec-WebSocket-Origin: ");
-	p += strlen("Connection: Upgrade\x0d\x0a"
-		    "Sec-WebSocket-Origin: ");
+	LWS_CPYAPP(p, "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
+		      "Upgrade: WebSocket\x0d\x0a"
+		      "Connection: Upgrade\x0d\x0a"
+		      "Sec-WebSocket-Origin: ");
 	strcpy(p, wsi->utf8_token[WSI_TOKEN_ORIGIN].token);
 	p += wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len;
 #ifdef LWS_OPENSSL_SUPPORT
-	if (wsi->ssl) {
-		strcpy(p,   "\x0d\x0aSec-WebSocket-Location: wss://");
-		p += strlen("\x0d\x0aSec-WebSocket-Location: wss://");
-	} else {
+	if (wsi->ssl)
+		LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Location: wss://");
+	else
 #endif
-		strcpy(p,   "\x0d\x0aSec-WebSocket-Location: ws://");
-		p += strlen("\x0d\x0aSec-WebSocket-Location: ws://");
-#ifdef LWS_OPENSSL_SUPPORT
-	}
-#endif
-	strcpy(p, wsi->utf8_token[WSI_TOKEN_HOST].token);
-	p += wsi->utf8_token[WSI_TOKEN_HOST].token_len;
-	strcpy(p, wsi->utf8_token[WSI_TOKEN_GET_URI].token);
-	p += wsi->utf8_token[WSI_TOKEN_GET_URI].token_len;
+		LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Location: ws://");
+
+	LWS_CPYAPP_TOKEN(p, WSI_TOKEN_HOST);
+	LWS_CPYAPP_TOKEN(p, WSI_TOKEN_GET_URI);
 
 	if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) {
-		strcpy(p,   "\x0d\x0aSec-WebSocket-Protocol: ");
-		p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
-		strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
-		p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len;
+		LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
+		LWS_CPYAPP_TOKEN(p, WSI_TOKEN_PROTOCOL);
 	}
 
-	strcpy(p,   "\x0d\x0a\x0d\x0a");
-	p += strlen("\x0d\x0a\x0d\x0a");
+	LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
 
 	/* convert the two keys into 32-bit integers */
 
@@ -232,7 +224,7 @@
 	int accept_len;
 	char *c;
 	char ext_name[128];
-	struct libwebsocket_extension * ext;
+	struct libwebsocket_extension *ext;
 	int ext_count = 0;
 	int more = 1;
 
@@ -266,8 +258,9 @@
 	}
 
 	/* allocate the per-connection user memory (if any) */
-	if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
-          goto bail;
+	if (wsi->protocol->per_session_data_size &&
+					  !libwebsocket_ensure_user_space(wsi))
+		goto bail;
 
 	/* create the response packet */
 
@@ -283,20 +276,15 @@
 	}
 
 	p = response;
-	strcpy(p,   "HTTP/1.1 101 Switching Protocols\x0d\x0a"
-					  "Upgrade: WebSocket\x0d\x0a");
-	p += strlen("HTTP/1.1 101 Switching Protocols\x0d\x0a"
-					  "Upgrade: WebSocket\x0d\x0a");
-	strcpy(p,   "Connection: Upgrade\x0d\x0a"
-		    "Sec-WebSocket-Accept: ");
-	p += strlen("Connection: Upgrade\x0d\x0a"
-		    "Sec-WebSocket-Accept: ");
+	LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
+		      "Upgrade: WebSocket\x0d\x0a"
+		      "Connection: Upgrade\x0d\x0a"
+		      "Sec-WebSocket-Accept: ");
 	strcpy(p, accept_buf);
 	p += accept_len;
 
 	if (wsi->ietf_spec_revision == 4) {
-		strcpy(p,   "\x0d\x0aSec-WebSocket-Nonce: ");
-		p += strlen("\x0d\x0aSec-WebSocket-Nonce: ");
+		LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Nonce: ");
 
 		/* select the nonce */
 
@@ -328,10 +316,8 @@
 	}
 
 	if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) {
-		strcpy(p,   "\x0d\x0aSec-WebSocket-Protocol: ");
-		p += strlen("\x0d\x0aSec-WebSocket-Protocol: ");
-		strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
-		p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len;
+		LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
+		LWS_CPYAPP_TOKEN(p, WSI_TOKEN_PROTOCOL);
 	}
 
 	/*
@@ -347,11 +333,12 @@
 		 */
 
 		c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
-		debug("wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token = %s\n", wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token);
+		debug("wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token = %s\n",
+				  wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token);
 		wsi->count_active_extensions = 0;
 		n = 0;
 		while (more) {
-		
+
 			if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
 				ext_name[n] = *c++;
 				if (n < sizeof(ext_name) - 1)
@@ -405,13 +392,12 @@
 				}
 
 				/* apply it */
-				
+
 				if (ext_count)
 					*p++ = ',';
-				else {
-			                strcpy(p,   "\x0d\x0aSec-WebSocket-Extensions: ");
-			                p += strlen("\x0d\x0aSec-WebSocket-Extensions: ");
-				}
+				else
+					LWS_CPYAPP(p,
+					 "\x0d\x0aSec-WebSocket-Extensions: ");
 				p += sprintf(p, "%s", ext_name);
 				ext_count++;
 
@@ -423,7 +409,7 @@
 				memset(wsi->active_extensions_user[
 					wsi->count_active_extensions], 0,
 						    ext->per_session_data_size);
-							
+
 				wsi->active_extensions[
 					  wsi->count_active_extensions] = ext;
 
@@ -448,8 +434,7 @@
 
 	/* end of response packet */
 
-	strcpy(p,   "\x0d\x0a\x0d\x0a");
-	p += strlen("\x0d\x0a\x0d\x0a");
+	LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
 
 	if (wsi->ietf_spec_revision == 4) {
 
@@ -477,7 +462,7 @@
 
 	if (!lws_any_extension_handled(context, wsi,
 			LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
-								response, p - response)) {
+						     response, p - response)) {
 
 		/* okay send the handshake response accepting the connection */
 
@@ -549,8 +534,8 @@
  */
 
 int
-libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi,
-						unsigned char * buf, size_t len)
+libwebsocket_read(struct libwebsocket_context *context,
+		     struct libwebsocket *wsi, unsigned char * buf, size_t len)
 {
 	size_t n;
 
@@ -579,7 +564,7 @@
 		default:
 			break;
 		}
-		
+
 		/* LWS_CONNMODE_WS_SERVING */
 
 		for (n = 0; n < len; n++)
@@ -605,7 +590,7 @@
 		}
 
 		if (!wsi->protocol)
-			fprintf(stderr, "NULL protocol coming on libwebsocket_read\n");
+			fprintf(stderr, "NULL protocol at libwebsocket_read\n");
 
 		/*
 		 * It's websocket
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index b05f79e..ebd54ba 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -114,11 +114,11 @@
 
 
 static int
-interface_to_sa(const char* ifname, struct sockaddr_in *addr, size_t addrlen)
+interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
 {
 	int rc = -1;
 #ifdef WIN32
-	// TODO
+	/* TODO */
 #else
 	struct ifaddrs *ifr;
 	struct ifaddrs *ifc;
@@ -134,7 +134,7 @@
 		if (sin->sin_family != AF_INET)
 			continue;
 		memcpy(addr, sin, addrlen);
-		rc = 0; 
+		rc = 0;
 	}
 
 	freeifaddrs(ifr);
@@ -313,16 +313,14 @@
 	/* tell the user it's all over for this guy */
 
 	if (wsi->protocol && wsi->protocol->callback &&
-				((old_state == WSI_STATE_ESTABLISHED) ||
-				 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
-				 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
+			((old_state == WSI_STATE_ESTABLISHED) ||
+			 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
+			 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
 		debug("calling back CLOSED\n");
 		wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
 						      wsi->user_space, NULL, 0);
 	} else
-		debug("not calling back closed due to old_state=%d\n",
-								     old_state);
-
+		debug("not calling back closed, old_state=%d\n", old_state);
 
 	/* deallocate any active extension contexts */
 
@@ -393,7 +391,7 @@
 
 /**
  * libwebsockets_hangup_on_client() - Server calls to terminate client
- * 					connection
+ *					connection
  * @context:	libwebsockets context
  * @fd:		Connection socket descriptor
  */
@@ -420,9 +418,9 @@
  * @rip_len:	Length of client address IP buffer
  *
  *	This function fills in @name and @rip with the name and IP of
- * 	the client connected with socket descriptor @fd.  Names may be
- * 	truncated if there is not enough room.  If either cannot be
- * 	determined, they will be returned as valid zero-length strings.
+ *	the client connected with socket descriptor @fd.  Names may be
+ *	truncated if there is not enough room.  If either cannot be
+ *	determined, they will be returned as valid zero-length strings.
  */
 
 void
@@ -446,7 +444,7 @@
 		perror("getpeername");
 		return;
 	}
-		
+
 	host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
 								       AF_INET);
 	if (host == NULL) {
@@ -478,7 +476,7 @@
 #ifdef AF_LOCAL
 		else {
 			un = (struct sockaddr_un *)p;
-			strncpy(ip, un->sun_path, sizeof(ip) -1);
+			strncpy(ip, un->sun_path, sizeof(ip) - 1);
 			ip[sizeof(ip) - 1] = '\0';
 		}
 #endif
@@ -513,13 +511,13 @@
 void libwebsockets_00_spaceout(char *key, int spaces, int seed)
 {
 	char *p;
-	
+
 	key++;
 	while (spaces--) {
 		if (*key && (seed & 1))
 			key++;
 		seed >>= 1;
-		
+
 		p = key + strlen(key);
 		while (p >= key) {
 			p[1] = p[0];
@@ -535,7 +533,7 @@
 
 	key++;
 	while (count--) {
-		
+
 		if (*key && (seed & 1))
 			key++;
 		seed >>= 1;
@@ -624,7 +622,7 @@
 					LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
 				   wsi->active_extensions_user[n], &eff_buf, 0);
 			if (m < 0) {
-				fprintf(stderr, "extension reports fatal error\n");
+				fprintf(stderr, "ext reports fatal error\n");
 				return -1;
 			}
 			if (m)
@@ -717,7 +715,7 @@
 
 	if (!wsi->pending_timeout)
 		return;
-			  
+
 	/*
 	 * if we went beyond the allowed time, kill the
 	 * connection
@@ -742,7 +740,7 @@
 		return NULL;
 	}
 
-	memset(new_wsi, 0, sizeof (struct libwebsocket));
+	memset(new_wsi, 0, sizeof(struct libwebsocket));
 	new_wsi->count_active_extensions = 0;
 	new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
 
@@ -787,8 +785,8 @@
 	struct libwebsocket_extension *ext;
 	struct libwebsocket_extension *ext1;
 	int ext_count = 0;
-	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
-						  LWS_SEND_BUFFER_POST_PADDING];
+	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
+			 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
 	static const char magic_websocket_guid[] =
 					 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
 
@@ -853,10 +851,8 @@
 		unsigned int count;
 		char challenge[16];
 
-		libwebsockets_get_random(context, &spaces_1,
-							  sizeof(char));
-		libwebsockets_get_random(context, &spaces_2,
-							  sizeof(char));
+		libwebsockets_get_random(context, &spaces_1, sizeof(char));
+		libwebsockets_get_random(context, &spaces_2, sizeof(char));
 
 		spaces_1 = (spaces_1 % 12) + 1;
 		spaces_2 = (spaces_2 % 12) + 1;
@@ -904,17 +900,14 @@
 			"Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
 			wsi->c_host);
 		if (wsi->c_origin)
-			p += sprintf(p, "Origin: %s\x0d\x0a",
-			wsi->c_origin);
+			p += sprintf(p, "Origin: %s\x0d\x0a", wsi->c_origin);
 
 		if (wsi->c_protocol)
 			p += sprintf(p, "Sec-WebSocket-Protocol: %s"
 					 "\x0d\x0a", wsi->c_protocol);
 
-		p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a",
-			key_1);
-		p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a",
-			key_2);
+		p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", key_1);
+		p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", key_2);
 
 		/* give userland a chance to append, eg, cookies */
 
@@ -955,13 +948,13 @@
 
 	p += sprintf(p, "Sec-WebSocket-Extensions: ");
 
-	ext =context->extensions;
+	ext = context->extensions;
 	while (ext && ext->callback) {
 
 		n = 0;
 		ext1 = context->extensions;
-		while (ext1 && ext1->callback) {
 
+		while (ext1 && ext1->callback) {
 			n |= ext1->callback(context, ext1, wsi,
 				LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
 					NULL, (char *)ext->name, 0);
@@ -969,9 +962,7 @@
 			ext1++;
 		}
 
-		if (n) {
-
-			/* an extension vetos us */
+		if (n) { /* an extension vetos us */
 			debug("ext %s vetoed\n", (char *)ext->name);
 			ext++;
 			continue;
@@ -1030,7 +1021,9 @@
 
 issue_hdr:
 
-//	puts(pkt);
+#if 0
+	puts(pkt);
+#endif
 
 	/* done with these now */
 
@@ -1046,8 +1039,8 @@
 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
 		struct libwebsocket *wsi)
 {
-	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
-						  LWS_SEND_BUFFER_POST_PADDING];
+	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
+			MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
 	char pkt[1024];
 	char *p = &pkt[0];
 	const char *pc;
@@ -1089,16 +1082,14 @@
 		}
 
 		strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
-		if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
-			                                    "101", 3)) {
+		if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
 			fprintf(stderr, "libwebsocket_client_handshake "
 				"server sent bad HTTP response '%s'\n",
 				wsi->utf8_token[WSI_TOKEN_HTTP].token);
 			goto bail3;
 		}
 
-		if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len <
-								   16) {
+		if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) {
 			fprintf(stderr, "libwebsocket_client_handshake "
 				"challenge reply too short %d\n",
 				wsi->utf8_token[
@@ -1117,22 +1108,28 @@
 	 * Now let's confirm it sent all the necessary headers
 	 */
 #if 0
-	fprintf(stderr, "WSI_TOKEN_HTTP: %d\n", wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
-	fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n", wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
-	fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n", wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
-	fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n", wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
-	fprintf(stderr, "WSI_TOKEN_NONCE: %d\n", wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
-	fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n", wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
+	fprintf(stderr, "WSI_TOKEN_HTTP: %d\n",
+				    wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
+	fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n",
+				 wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
+	fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n",
+			      wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
+	fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n",
+				  wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
+	fprintf(stderr, "WSI_TOKEN_NONCE: %d\n",
+				   wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
+	fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n",
+				wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
 #endif
-	 if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
-		!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
-		!wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
-		!wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
-		(!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
+	if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
+	    !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
+	    !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
+	    !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
+	    (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
 				   wsi->ietf_spec_revision == 4) ||
-		(!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
-						 wsi->c_protocol != NULL)) {
-		 debug("libwebsocket_client_handshake "
+	    (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
+						    wsi->c_protocol != NULL)) {
+		debug("libwebsocket_client_handshake "
 					"missing required header(s)\n");
 		pkt[len] = '\0';
 		debug("%s", pkt);
@@ -1173,9 +1170,11 @@
 select_protocol:
 	pc = wsi->c_protocol;
 	if (pc == NULL)
-		fprintf(stderr, "lws_client_interpret_server_handshake: NULL c_protocol\n");
+		fprintf(stderr, "lws_client_interpret_server_handshake: "
+							  "NULL c_protocol\n");
 	else
-		debug("lws_client_interpret_server_handshake: cPprotocol='%s'\n", pc);
+		debug("lws_client_interpret_server_handshake: "
+						      "cPprotocol='%s'\n", pc);
 
 	/*
 	 * confirm the protocol the server wants to talk was in the list
@@ -1184,7 +1183,8 @@
 
 	if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
 
-		fprintf(stderr, "lws_client_interpret_server_handshake WSI_TOKEN_PROTOCOL is null\n");
+		fprintf(stderr, "lws_client_interpret_server_handshake "
+					       "WSI_TOKEN_PROTOCOL is null\n");
 		/*
 		 * no protocol name to work from,
 		 * default to first protocol
@@ -1197,11 +1197,10 @@
 	}
 
 	while (*pc && !okay) {
-		if ((!strncmp(pc,
-			wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
-		   wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
-	 (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
-	   pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
+		if ((!strncmp(pc, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
+		 wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
+		 (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
+		  pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
 			okay = 1;
 			continue;
 		}
@@ -1216,7 +1215,6 @@
 	if (wsi->c_protocol)
 		free(wsi->c_protocol);
 
-
 	if (!okay) {
 		fprintf(stderr, "libwebsocket_client_handshake server "
 					"sent bad protocol '%s'\n",
@@ -1248,7 +1246,7 @@
 	/* instantiate the accepted extensions */
 
 	if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
-		debug("no client extenstions allowed by server \n");
+		debug("no client extenstions allowed by server\n");
 		goto check_accept;
 	}
 
@@ -1370,11 +1368,12 @@
 		strcpy(p, magic_websocket_04_masking_guid);
 		SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
 	}
-	accept_ok:
+accept_ok:
 
 	/* allocate the per-connection user memory (if any) */
-	if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
-	  goto bail2;
+	if (wsi->protocol->per_session_data_size &&
+					  !libwebsocket_ensure_user_space(wsi))
+		goto bail2;
 
 	/* clear his proxy connection timeout */
 
@@ -1385,15 +1384,13 @@
 	wsi->state = WSI_STATE_ESTABLISHED;
 	wsi->mode = LWS_CONNMODE_WS_CLIENT;
 
-	fprintf(stderr, "handshake OK for protocol %s\n",
-						   wsi->protocol->name);
+	fprintf(stderr, "handshake OK for protocol %s\n", wsi->protocol->name);
 
 	/* call him back to inform him he is up */
 
 	wsi->protocol->callback(context, wsi,
-			 LWS_CALLBACK_CLIENT_ESTABLISHED,
-			 wsi->user_space,
-			 NULL, 0);
+				LWS_CALLBACK_CLIENT_ESTABLISHED,
+						     wsi->user_space, NULL, 0);
 
 	/*
 	 * inform all extensions, not just active ones since they
@@ -1431,7 +1428,7 @@
  * libwebsocket_service_fd() - Service polled socket with something waiting
  * @context:	Websocket context
  * @pollfd:	The pollfd entry describing the socket fd and which events
- * 		happened.
+ *		happened.
  *
  *	This function closes any active connections and then frees the
  *	context.  After calling this, any further use of the context is
@@ -1442,8 +1439,8 @@
 libwebsocket_service_fd(struct libwebsocket_context *context,
 							  struct pollfd *pollfd)
 {
-	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
-						  LWS_SEND_BUFFER_POST_PADDING];
+	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
+			 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
 	struct libwebsocket *wsi;
 	struct libwebsocket *new_wsi;
 	int n;
@@ -1521,8 +1518,8 @@
 
 		/* Disable Nagle */
 		opt = 1;
-        setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt,
-				sizeof(opt));
+		setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY,
+					      (const void *)&opt, sizeof(opt));
 
 		/*
 		 * look at who we connected to and give user code a chance
@@ -1532,7 +1529,7 @@
 
 		if ((context->protocols[0].callback)(context, wsi,
 				LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
-					     (void*)(long)accept_fd, NULL, 0)) {
+					   (void *)(long)accept_fd, NULL, 0)) {
 			debug("Callback denied network connection\n");
 #ifdef WIN32
 			closesocket(accept_fd);
@@ -1584,7 +1581,7 @@
 				free(new_wsi);
 				break;
 			}
-			
+
 			debug("accepted new SSL conn  "
 			      "port %u on fd=%d SSL ver %s\n",
 				ntohs(cli_addr.sin_port), accept_fd,
@@ -1617,7 +1614,7 @@
 	case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
 
 		/* as we are listening, POLLIN means accept() is needed */
-	
+
 		if (!pollfd->revents & POLLIN)
 			break;
 
@@ -1645,7 +1642,7 @@
 		/* create a dummy wsi for the connection and add it */
 
 		new_wsi = malloc(sizeof(struct libwebsocket));
-		memset(new_wsi, 0, sizeof (struct libwebsocket));
+		memset(new_wsi, 0, sizeof(struct libwebsocket));
 		new_wsi->sock = accept_fd;
 		new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY;
 		new_wsi->state = WSI_STATE_ESTABLISHED;
@@ -1688,9 +1685,10 @@
 		 */
 
 		if (pollfd->revents & POLLOUT)
-			if (lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
-				libwebsocket_close_and_free_session(context, wsi,
-						       LWS_CLOSE_STATUS_NORMAL);
+			if (lws_handle_POLLOUT_event(context, wsi,
+								 pollfd) < 0) {
+				libwebsocket_close_and_free_session(
+					context, wsi, LWS_CLOSE_STATUS_NORMAL);
 				return 1;
 			}
 
@@ -1802,8 +1800,8 @@
 				fprintf(stderr, "SSL connect error %s\n",
 					ERR_error_string(ERR_get_error(),
 								  ssl_err_buf));
-				libwebsocket_close_and_free_session(context, wsi,
-						     LWS_CLOSE_STATUS_NOSTATUS);
+				libwebsocket_close_and_free_session(context,
+					       wsi, LWS_CLOSE_STATUS_NOSTATUS);
 				return 1;
 			}
 
@@ -1828,7 +1826,7 @@
 	#endif
 
 		p = libwebsockets_generate_client_handshake(context, wsi, p);
-		if (p ==NULL)
+		if (p == NULL)
 			return 1;
 
 		/* send our request to the server */
@@ -1900,7 +1898,7 @@
 		}
 
 		/*
-		 * hs may also be coming in multiple packets, there is a 5-second
+		 * hs may also be coming in multiple packets, there is a 5-sec
 		 * libwebsocket timeout still active here too, so if parsing did
 		 * not complete just wait for next packet coming in this state
 		 */
@@ -1920,15 +1918,17 @@
 		if (wsi->c_protocol)
 			free(wsi->c_protocol);
 		libwebsocket_close_and_free_session(context, wsi,
-								 LWS_CLOSE_STATUS_NOSTATUS);
+						    LWS_CLOSE_STATUS_NOSTATUS);
 		return 1;
 
 	case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
-		fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
+		fprintf(stderr,
+			 "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
 		break;
 
 	case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
-		fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
+		fprintf(stderr,
+			   "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
 		break;
 
 
@@ -1976,13 +1976,13 @@
 			fprintf(stderr, "Socket read returned %d\n",
 							    eff_buf.token_len);
 			if (errno != EINTR)
-				libwebsocket_close_and_free_session(context, wsi,
-								 LWS_CLOSE_STATUS_NOSTATUS);
+				libwebsocket_close_and_free_session(context,
+					       wsi, LWS_CLOSE_STATUS_NOSTATUS);
 			return 1;
 		}
 		if (!eff_buf.token_len) {
 			libwebsocket_close_and_free_session(context, wsi,
-						     LWS_CLOSE_STATUS_NOSTATUS);
+						    LWS_CLOSE_STATUS_NOSTATUS);
 			return 1;
 		}
 
@@ -2012,9 +2012,11 @@
 					wsi->active_extensions_user[n],
 								   &eff_buf, 0);
 				if (m < 0) {
-					fprintf(stderr, "Extension reports fatal error\n");
-					libwebsocket_close_and_free_session(context, wsi,
-							     LWS_CLOSE_STATUS_NOSTATUS);
+					fprintf(stderr,
+					    "Extension reports fatal error\n");
+					libwebsocket_close_and_free_session(
+						context, wsi,
+						    LWS_CLOSE_STATUS_NOSTATUS);
 					return 1;
 				}
 				if (m)
@@ -2025,7 +2027,8 @@
 
 			if (eff_buf.token_len) {
 				n = libwebsocket_read(context, wsi,
-				     (unsigned char *)eff_buf.token, eff_buf.token_len);
+					(unsigned char *)eff_buf.token,
+							    eff_buf.token_len);
 				if (n < 0)
 					/* we closed wsi */
 					return 1;
@@ -2167,8 +2170,8 @@
 
 int
 lws_any_extension_handled(struct libwebsocket_context *context,
-						       struct libwebsocket *wsi,
-						       enum libwebsocket_extension_callback_reasons r,
+			  struct libwebsocket *wsi,
+			  enum libwebsocket_extension_callback_reasons r,
 						       void *v, size_t len)
 {
 	int n;
@@ -2191,7 +2194,7 @@
 
 void *
 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
-							struct libwebsocket_extension * ext)
+					   struct libwebsocket_extension *ext)
 {
 	int n = 0;
 
@@ -2220,7 +2223,7 @@
 
 int
 libwebsocket_callback_on_writable(struct libwebsocket_context *context,
-						       struct libwebsocket *wsi)
+						      struct libwebsocket *wsi)
 {
 	int n;
 	int handled = 0;
@@ -2247,7 +2250,8 @@
 		}
 
 	if (n == context->fds_count)
-		fprintf(stderr, "libwebsocket_callback_on_writable: failed to find socket %d\n", wsi->sock);
+		fprintf(stderr, "libwebsocket_callback_on_writable: "
+				      "failed to find socket %d\n", wsi->sock);
 
 	/* external POLL support via protocol 0 */
 	context->protocols[0].callback(context, wsi,
@@ -2410,7 +2414,7 @@
 	 * static
 	 */
 	context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
-	
+
 	n = context->protocols[0].callback(NULL, NULL,
 		LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
 						   x509_ctx, ssl, preverify_ok);
@@ -2439,7 +2443,7 @@
  *		entry that has a NULL callback pointer.
  *		It's not const because we write the owning_server member
  * @extensions: NULL or array of libwebsocket_extension structs listing the
- * 		extensions this context supports
+ *		extensions this context supports
  * @ssl_cert_filepath:	If libwebsockets was compiled to use ssl, and you want
  *			to listen using SSL, set to the filepath to fetch the
  *			server cert from, otherwise NULL for unencrypted
@@ -2506,7 +2510,7 @@
 		WORD wVersionRequested;
 		WSADATA wsaData;
 		int err;
-        HMODULE wsdll;
+		HMODULE wsdll;
 
 		/* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
 		wVersionRequested = MAKEWORD(2, 2);
@@ -2520,16 +2524,13 @@
 			return NULL;
 		}
 
-        wsdll = GetModuleHandle("Ws2_32.dll");
-        if (wsdll)
-        {
-            poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
-        }
+		/* default to a poll() made out of select() */
+		poll = emulated_poll;
 
-        if (!poll)
-        {
-            poll = emulated_poll;
-        }
+		/* if windows socket lib available, use his WSAPoll */
+		wsdll = GetModuleHandle("Ws2_32.dll");
+		if (wsdll)
+			poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
 	}
 #endif
 
@@ -2583,7 +2584,7 @@
 	p = getenv("http_proxy");
 	if (p) {
 		strncpy(context->http_proxy_address, p,
-				        sizeof context->http_proxy_address - 1);
+				       sizeof context->http_proxy_address - 1);
 		context->http_proxy_address[
 				 sizeof context->http_proxy_address - 1] = '\0';
 
@@ -2661,8 +2662,8 @@
 	}
 
 	/* client context */
-	if (port == CONTEXT_PORT_NO_LISTEN)
-	{
+
+	if (port == CONTEXT_PORT_NO_LISTEN) {
 		method = (SSL_METHOD *)SSLv23_client_method();
 		if (!method) {
 			fprintf(stderr, "problem creating ssl method: %s\n",
@@ -2697,12 +2698,13 @@
 			LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
 			context->ssl_client_ctx, NULL, 0);
 	}
+
 	/* as a server, are we requiring clients to identify themselves? */
 
 	if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
 
 		/* absolutely require the client cert */
-		
+
 		SSL_CTX_set_verify(context->ssl_ctx,
 		       SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
 						       OpenSSL_verify_callback);
@@ -2769,12 +2771,13 @@
 		}
 
 		/* allow us to restart even if old sockets in TIME_WAIT */
-		setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));
-
+		setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
+					      (const void *)&opt, sizeof(opt));
 
 		/* Disable Nagle */
 		opt = 1;
-		setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt, sizeof(opt));
+		setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
+					      (const void *)&opt, sizeof(opt));
 
 		bzero((char *) &serv_addr, sizeof(serv_addr));
 		serv_addr.sin_family = AF_INET;
@@ -2794,7 +2797,7 @@
 		}
 
 		wsi = malloc(sizeof(struct libwebsocket));
-		memset(wsi, 0, sizeof (struct libwebsocket));
+		memset(wsi, 0, sizeof(struct libwebsocket));
 		wsi->sock = sockfd;
 		wsi->count_active_extensions = 0;
 		wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
@@ -2804,7 +2807,7 @@
 		fprintf(stderr, " Listening on port %d\n", port);
 
 		/* list in the internal poll array */
-		
+
 		context->fds[context->fds_count].fd = sockfd;
 		context->fds[context->fds_count++].events = POLLIN;
 
@@ -2815,7 +2818,11 @@
 
 	}
 
-	/* drop any root privs for this process */
+	/*
+	 * drop any root privs for this process
+	 * to listen on port < 1023 we would have needed root, but now we are
+	 * listening, we don't want the power for anything else
+	 */
 #ifdef WIN32
 #else
 	if (gid != -1)
@@ -2832,7 +2839,8 @@
 			protocols[context->count_protocols].callback;
 						   context->count_protocols++) {
 
-		fprintf(stderr, "  Protocol: %s\n", protocols[context->count_protocols].name);
+		fprintf(stderr, "  Protocol: %s\n",
+				     protocols[context->count_protocols].name);
 
 		protocols[context->count_protocols].owning_server = context;
 		protocols[context->count_protocols].protocol_index =
@@ -2845,7 +2853,8 @@
 		}
 
 		/* allow us to restart even if old sockets in TIME_WAIT */
-		setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));
+		setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt,
+								  sizeof(opt));
 
 		bzero((char *) &serv_addr, sizeof(serv_addr));
 		serv_addr.sin_family = AF_INET;
@@ -2876,7 +2885,7 @@
 		/* dummy wsi per broadcast proxy socket */
 
 		wsi = malloc(sizeof(struct libwebsocket));
-		memset(wsi, 0, sizeof (struct libwebsocket));
+		memset(wsi, 0, sizeof(struct libwebsocket));
 		wsi->sock = fd;
 		wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
 		wsi->count_active_extensions = 0;
@@ -3034,7 +3043,7 @@
 	struct libwebsocket_context *context = protocol->owning_server;
 	int n;
 	int m;
-	struct libwebsocket * wsi;
+	struct libwebsocket *wsi;
 
 	if (!protocol->broadcast_socket_user_fd) {
 		/*
@@ -3115,7 +3124,8 @@
 						   "conn user space\n");
 			return NULL;
 		}
-		memset(wsi->user_space, 0, wsi->protocol->per_session_data_size);
+		memset(wsi->user_space, 0,
+					 wsi->protocol->per_session_data_size);
 	}
 	return wsi->user_space;
 }
diff --git a/lib/parsers.c b/lib/parsers.c
index c3bf7ae..61f3045 100644
--- a/lib/parsers.c
+++ b/lib/parsers.c
@@ -200,7 +200,7 @@
 
 			wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
 			wsi->utf8_token[wsi->parser_state].token =
-							 malloc(wsi->current_alloc_len);
+						 malloc(wsi->current_alloc_len);
 			wsi->utf8_token[wsi->parser_state].token_len = 0;
 		}
 
@@ -304,7 +304,9 @@
 	int handled;
 	int m;
 
-//	fprintf(stderr, "RX: %02X ", c);
+#if 0
+	fprintf(stderr, "RX: %02X ", c);
+#endif
 
 	switch (wsi->lws_rx_parse_state) {
 	case LWS_RXPS_NEW:
@@ -704,7 +706,8 @@
 
 
 	case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
-		if (wsi->ietf_spec_revision < 4 || (wsi->all_zero_nonce && wsi->ietf_spec_revision >= 5))
+		if (wsi->ietf_spec_revision < 4 ||
+			 (wsi->all_zero_nonce && wsi->ietf_spec_revision >= 5))
 			wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
 			       (wsi->rx_user_buffer_head++)] = c;
 		else
@@ -922,7 +925,8 @@
 			if (wsi->ietf_spec_revision < 7)
 				switch (c & 0xf) {
 				case LWS_WS_OPCODE_04__CONTINUATION:
-					wsi->opcode = LWS_WS_OPCODE_07__CONTINUATION;
+					wsi->opcode =
+						LWS_WS_OPCODE_07__CONTINUATION;
 					break;
 				case LWS_WS_OPCODE_04__CLOSE:
 					wsi->opcode = LWS_WS_OPCODE_07__CLOSE;
@@ -934,14 +938,16 @@
 					wsi->opcode = LWS_WS_OPCODE_07__PONG;
 					break;
 				case LWS_WS_OPCODE_04__TEXT_FRAME:
-					wsi->opcode = LWS_WS_OPCODE_07__TEXT_FRAME;
+					wsi->opcode =
+						  LWS_WS_OPCODE_07__TEXT_FRAME;
 					break;
 				case LWS_WS_OPCODE_04__BINARY_FRAME:
-					wsi->opcode = LWS_WS_OPCODE_07__BINARY_FRAME;
+					wsi->opcode =
+						LWS_WS_OPCODE_07__BINARY_FRAME;
 					break;
 				default:
 					fprintf(stderr, "reserved opcodes not "
-							    "usable pre v7 protocol\n");
+						   "usable pre v7 protocol\n");
 					return -1;
 				}
 			else
@@ -993,7 +999,7 @@
 			else {
 				if (c)
 					wsi->lws_rx_parse_state =
-							LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
+					LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
 				else {
 					wsi->lws_rx_parse_state = LWS_RXPS_NEW;
 					goto spill;
@@ -1016,7 +1022,7 @@
 		else {
 			if (wsi->rx_packet_length)
 				wsi->lws_rx_parse_state =
-						LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
+					LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
 			else {
 				wsi->lws_rx_parse_state = LWS_RXPS_NEW;
 				goto spill;
@@ -1082,7 +1088,7 @@
 		else {
 			if (wsi->rx_packet_length)
 				wsi->lws_rx_parse_state =
-						LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
+					LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
 			else {
 				wsi->lws_rx_parse_state = LWS_RXPS_NEW;
 				goto spill;
@@ -1200,12 +1206,14 @@
 				debug("seen server's close ack\n");
 				return -1;
 			}
-			fprintf(stderr, "client sees server close packet len = %d\n", wsi->rx_user_buffer_head);
+			fprintf(stderr, "client sees server close packet "
+				       "len = %d\n", wsi->rx_user_buffer_head);
 			/* parrot the close packet payload back */
 			n = libwebsocket_write(wsi, (unsigned char *)
 			   &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
 				     wsi->rx_user_buffer_head, LWS_WRITE_CLOSE);
-			fprintf(stderr, "client writing close ack returned %d\n", n);
+			fprintf(stderr, "client writing close "
+						       "ack returned %d\n", n);
 			wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
 			/* close the connection */
 			return -1;
@@ -1351,7 +1359,7 @@
 	 */
 
 	memcpy(buf, wsi->frame_masking_nonce_04, 4);
-	
+
 	memcpy(buf + 4, wsi->masking_key_04, 20);
 
 	/* concatenate the nonce with the connection key then hash it */
@@ -1373,7 +1381,7 @@
 		start = n;
 
 		fprintf(stderr, "%04X: ", start);
-		
+
 		for (m = 0; m < 16 && n < len; m++)
 			fprintf(stderr, "%02X ", buf[n++]);
 		while (m++ < 16)
@@ -1419,14 +1427,13 @@
 			return -1;
 		}
 		if (m) /* handled */ {
-//			fprintf(stderr, "ext sent it\n");
+/*			fprintf(stderr, "ext sent it\n"); */
 			return 0;
 		}
 	}
 
-	if (!wsi->sock) {
+	if (!wsi->sock)
 		fprintf(stderr, "** error 0 sock but expected to send\n");
-	}
 
 	/*
 	 * nope, send it on the socket directly
@@ -1492,7 +1499,7 @@
 					LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
 				   wsi->active_extensions_user[n], &eff_buf, 0);
 			if (m < 0) {
-				fprintf(stderr, "Extension reports fatal error\n");
+				fprintf(stderr, "Extension: fatal error\n");
 				return -1;
 			}
 			if (m)
@@ -1507,7 +1514,7 @@
 
 		if (eff_buf.token_len)
 			if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
-							     eff_buf.token_len))
+							    eff_buf.token_len))
 				return -1;
 
 		/* we used up what we had */
@@ -1572,7 +1579,8 @@
 	int pre = 0;
 	int post = 0;
 	int shift = 7;
-	int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT && wsi->xor_mask != xor_no_mask;
+	int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT &&
+						  wsi->xor_mask != xor_no_mask;
 	unsigned char *dropmask = NULL;
 	unsigned char is_masked_bit = 0;
 
@@ -1665,7 +1673,7 @@
 			 * v5 mandates the first byte of close packet
 			 * in both client and server directions
 			 */
-			
+
 			switch (wsi->ietf_spec_revision) {
 			case 0:
 			case 4:
@@ -1675,7 +1683,7 @@
 
 				if (len < 1)
 					len = 1;
-				
+
 				switch (wsi->mode) {
 				case LWS_CONNMODE_WS_SERVING:
 					/*
@@ -1780,7 +1788,8 @@
 		 * to control the raw packet payload content
 		 */
 
-		if (!(protocol & LWS_WRITE_CLIENT_IGNORE_XOR_MASK) && wsi->xor_mask != xor_no_mask) {
+		if (!(protocol & LWS_WRITE_CLIENT_IGNORE_XOR_MASK) &&
+						wsi->xor_mask != xor_no_mask) {
 
 			if (libwebsocket_0405_frame_mask_generate(wsi)) {
 				fprintf(stderr, "libwebsocket_write: "
@@ -1801,7 +1810,8 @@
 				 * in v7, just mask the payload
 				 */
 				for (n = 0; n < (int)len; n++)
-					dropmask[n + 4] = wsi->xor_mask(wsi, dropmask[n + 4]);
+					dropmask[n + 4] =
+					   wsi->xor_mask(wsi, dropmask[n + 4]);
 
 
 			if (wsi->ietf_spec_revision < 7) {
@@ -1813,7 +1823,8 @@
 
 			if (dropmask)
 				/* copy the frame nonce into place */
-				memcpy(dropmask, wsi->frame_masking_nonce_04, 4);
+				memcpy(dropmask,
+					       wsi->frame_masking_nonce_04, 4);
 
 		} else {
 			if (wsi->ietf_spec_revision < 7) {
@@ -1914,7 +1925,8 @@
 			"Server: libwebsockets\x0d\x0a"
 			"Content-Type: %s\x0d\x0a"
 			"Content-Length: %u\x0d\x0a"
-			"\x0d\x0a", content_type, (unsigned int)stat_buf.st_size);
+			"\x0d\x0a", content_type,
+					(unsigned int)stat_buf.st_size);
 
 	libwebsocket_write(wsi, (unsigned char *)buf, p - buf, LWS_WRITE_HTTP);
 
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index b4b63ac..b4779e6 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -100,7 +100,7 @@
  * but happily have something equivalent in the SO_NOSIGPIPE flag.
  */
 #ifdef __APPLE__
-#define MSG_NOSIGNAL SO_NOSIGPIPE 
+#define MSG_NOSIGNAL SO_NOSIGPIPE
 #endif
 
 
@@ -236,7 +236,7 @@
 	unsigned long last_timeout_check_s;
 
 	int fd_random;
-	
+
 #ifdef LWS_OPENSSL_SUPPORT
 	int use_ssl;
 	SSL_CTX *ssl_ctx;
@@ -269,7 +269,7 @@
 	const struct libwebsocket_protocols *protocol;
 	struct libwebsocket_extension *
 				   active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
-	void * active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
+	void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
 	int count_active_extensions;
 
 	enum lws_connection_states state;
@@ -347,8 +347,9 @@
 						unsigned char *buf, size_t len);
 
 extern int
-libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi,
-					       unsigned char * buf, size_t len);
+libwebsocket_read(struct libwebsocket_context *context,
+				struct libwebsocket *wsi,
+					       unsigned char *buf, size_t len);
 
 extern int
 lws_b64_selftest(void);
@@ -383,8 +384,8 @@
 libwebsocket_service_timeout_check(struct libwebsocket_context *context,
 				    struct libwebsocket *wsi, unsigned int sec);
 
-extern struct libwebsocket * __libwebsocket_client_connect_2(
-	struct libwebsocket_context *context,
+extern struct libwebsocket *
+__libwebsocket_client_connect_2(struct libwebsocket_context *context,
 	struct libwebsocket *wsi);
 
 extern struct libwebsocket *
@@ -400,13 +401,13 @@
 
 extern int
 lws_any_extension_handled(struct libwebsocket_context *context,
-						       struct libwebsocket *wsi,
-						       enum libwebsocket_extension_callback_reasons r,
-						       void *v, size_t len);
+			  struct libwebsocket *wsi,
+			  enum libwebsocket_extension_callback_reasons r,
+			  void *v, size_t len);
 
 extern void *
 lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
-							struct libwebsocket_extension * ext);
+			  struct libwebsocket_extension *ext);
 
 extern int
 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
diff --git a/lib/sha-1.c b/lib/sha-1.c
index 2bd2494..62d4f6f 100644
--- a/lib/sha-1.c
+++ b/lib/sha-1.c
@@ -45,10 +45,7 @@
 #define BYTE_ORDER LITTLE_ENDIAN
 #endif
 
-typedef unsigned char u_int8_t;
-typedef unsigned int u_int32_t;
 typedef unsigned __int64 u_int64_t;
-typedef void* caddr_t;
 
 #undef __P
 #ifndef __P
@@ -59,7 +56,7 @@
 #endif
 #endif
 
-#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
+#define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
 
 #else
 #include <sys/cdefs.h>
@@ -70,18 +67,18 @@
 
 struct sha1_ctxt {
 	union {
-		u_int8_t	b8[20];
-		u_int32_t	b32[5];
+		unsigned char		b8[20];
+		unsigned int		b32[5];
 	} h;
 	union {
-		u_int8_t	b8[8];
-		u_int64_t	b64[1];
+		unsigned char		b8[8];
+		u_int64_t		b64[1];
 	} c;
 	union {
-		u_int8_t	b8[64];
-		u_int32_t	b32[16];
+		unsigned char		b8[64];
+		unsigned int		b32[16];
 	} m;
-	u_int8_t	count;
+	unsigned char			count;
 };
 
 /* sanity check */
@@ -94,7 +91,7 @@
 #ifndef unsupported
 
 /* constant table */
-static u_int32_t _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
+static unsigned int _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
 #define	K(t)	_K[(t) / 20]
 
 #define	F0(b, c, d)	(((b) & (c)) | ((~(b)) & (d)))
@@ -116,7 +113,7 @@
 	ctxt->c.b64[0] += 8;			\
 	if (COUNT % 64 == 0)			\
 		sha1_step(ctxt);		\
-     }
+	}
 
 #define	PUTPAD(x)	{ \
 	ctxt->m.b8[(COUNT % 64)] = (x);		\
@@ -124,16 +121,15 @@
 	COUNT %= 64;				\
 	if (COUNT % 64 == 0)			\
 		sha1_step(ctxt);		\
-     }
+	}
 
 static void sha1_step __P((struct sha1_ctxt *));
 
 static void
 sha1_step(struct sha1_ctxt *ctxt)
 {
-	u_int32_t	a, b, c, d, e;
+	unsigned int	a, b, c, d, e, tmp;
 	size_t t, s;
-	u_int32_t	tmp;
 
 #if BYTE_ORDER == LITTLE_ENDIAN
 	struct sha1_ctxt tctxt;
@@ -263,7 +259,7 @@
 }
 
 void
-sha1_loop(struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len)
+sha1_loop(struct sha1_ctxt *ctxt, const unsigned char *input, size_t len)
 {
 	size_t gaplen;
 	size_t gapstart;
@@ -288,11 +284,11 @@
 }
 
 void
-sha1_result(struct sha1_ctxt *ctxt, caddr_t digest0)
+sha1_result(struct sha1_ctxt *ctxt, void *digest0)
 {
-	u_int8_t *digest;
+	unsigned char *digest;
 
-	digest = (u_int8_t *)digest0;
+	digest = (unsigned char *)digest0;
 	sha1_pad(ctxt);
 #if BYTE_ORDER == BIG_ENDIAN
 	memcpy(digest, &ctxt->h.b8[0], 20);
@@ -321,7 +317,7 @@
 
 	sha1_init(&ctx);
 	sha1_loop(&ctx, d, n);
-	sha1_result(&ctx, (caddr_t)md);
+	sha1_result(&ctx, (void *)md);
 
 	return md;
 }
diff --git a/test-server/test-client.c b/test-server/test-client.c
index 3fecd47..9d1d9ef 100644
--- a/test-server/test-client.c
+++ b/test-server/test-client.c
@@ -59,7 +59,7 @@
 /* dumb_increment protocol */
 
 static int
-callback_dumb_increment(struct libwebsocket_context * this,
+callback_dumb_increment(struct libwebsocket_context *this,
 			struct libwebsocket *wsi,
 			enum libwebsocket_callback_reasons reason,
 					       void *user, void *in, size_t len)
@@ -102,7 +102,7 @@
 
 
 static int
-callback_lws_mirror(struct libwebsocket_context * this,
+callback_lws_mirror(struct libwebsocket_context *this,
 			struct libwebsocket *wsi,
 			enum libwebsocket_callback_reasons reason,
 					       void *user, void *in, size_t len)
@@ -291,18 +291,22 @@
 
 			/* create a client websocket using mirror protocol */
 
-			wsi_mirror = libwebsocket_client_connect(context, address, port,
-			     use_ssl,  "/", argv[optind], argv[optind],
-					     protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
+			wsi_mirror = libwebsocket_client_connect(context,
+				address, port, use_ssl,  "/",
+				argv[optind], argv[optind],
+				protocols[PROTOCOL_LWS_MIRROR].name,
+								 ietf_version);
 
 			if (wsi_mirror == NULL) {
-				fprintf(stderr, "libwebsocket dumb connect failed\n");
+				fprintf(stderr, "libwebsocket "
+						      "dumb connect failed\n");
 				return -1;
 			}
 
 			mirror_lifetime = 10 + (random() & 1023);
 
-			fprintf(stderr, "opened mirror connection with %d lifetime\n", mirror_lifetime);
+			fprintf(stderr, "opened mirror connection with "
+					     "%d lifetime\n", mirror_lifetime);
 
 		} else {
 
diff --git a/test-server/test-fraggle.c b/test-server/test-fraggle.c
index cdb4f56..873bee3 100644
--- a/test-server/test-fraggle.c
+++ b/test-server/test-fraggle.c
@@ -56,7 +56,7 @@
 };
 
 static int
-callback_fraggle(struct libwebsocket_context * context,
+callback_fraggle(struct libwebsocket_context *context,
 			struct libwebsocket *wsi,
 			enum libwebsocket_callback_reasons reason,
 					       void *user, void *in, size_t len)
@@ -179,7 +179,7 @@
 			bp[1] = psf->sum >> 16;
 			bp[2] = psf->sum >> 8;
 			bp[3] = psf->sum;
-							
+
 			n = libwebsocket_write(wsi, (unsigned char *)bp,
 							   4, LWS_WRITE_BINARY);
 
@@ -231,8 +231,8 @@
 	{ "port",	required_argument,	NULL, 'p' },
 	{ "ssl",	no_argument,		NULL, 's' },
 	{ "killmask",	no_argument,		NULL, 'k' },
-	{ "interface",  required_argument, 	NULL, 'i' },
-	{ "client", 	no_argument,		NULL, 'c' },
+	{ "interface",  required_argument,	NULL, 'i' },
+	{ "client",	no_argument,		NULL, 'c' },
 	{ NULL, 0, 0, 0 }
 };
 
@@ -248,7 +248,7 @@
 	struct libwebsocket_context *context;
 	int opts = 0;
 	char interface_name[128] = "";
-	const char * interface = NULL;
+	const char *interface = NULL;
 	struct libwebsocket *wsi;
 	const char *address;
 	int server_port = port;
diff --git a/test-server/test-server.c b/test-server/test-server.c
index c1cfab8..f054b16 100644
--- a/test-server/test-server.c
+++ b/test-server/test-server.c
@@ -61,7 +61,7 @@
 
 /* this protocol server (always the first one) just knows how to do HTTP */
 
-static int callback_http(struct libwebsocket_context * context,
+static int callback_http(struct libwebsocket_context *context,
 		struct libwebsocket *wsi,
 		enum libwebsocket_callback_reasons reason, void *user,
 							   void *in, size_t len)
@@ -150,7 +150,7 @@
 		/*[WSI_TOKEN_HTTP]		=*/ "Http",
 		/*[WSI_TOKEN_MUXURL]	=*/ "MuxURL",
 	};
-	
+
 	for (n = 0; n < WSI_TOKEN_COUNT; n++) {
 		if (lwst[n].token == NULL)
 			continue;
@@ -174,7 +174,7 @@
 };
 
 static int
-callback_dumb_increment(struct libwebsocket_context * context,
+callback_dumb_increment(struct libwebsocket_context *context,
 			struct libwebsocket *wsi,
 			enum libwebsocket_callback_reasons reason,
 					       void *user, void *in, size_t len)
@@ -188,7 +188,8 @@
 	switch (reason) {
 
 	case LWS_CALLBACK_ESTABLISHED:
-		fprintf(stderr, "callback_dumb_increment: LWS_CALLBACK_ESTABLISHED\n");
+		fprintf(stderr, "callback_dumb_increment: "
+						 "LWS_CALLBACK_ESTABLISHED\n");
 		pss->number = 0;
 		break;
 
@@ -257,7 +258,7 @@
 
 
 static int
-callback_lws_mirror(struct libwebsocket_context * context,
+callback_lws_mirror(struct libwebsocket_context *context,
 			struct libwebsocket *wsi,
 			enum libwebsocket_callback_reasons reason,
 					       void *user, void *in, size_t len)
@@ -268,7 +269,8 @@
 	switch (reason) {
 
 	case LWS_CALLBACK_ESTABLISHED:
-		fprintf(stderr, "callback_lws_mirror: LWS_CALLBACK_ESTABLISHED\n");
+		fprintf(stderr, "callback_lws_mirror: "
+						 "LWS_CALLBACK_ESTABLISHED\n");
 		pss->ringbuffer_tail = ringbuffer_head;
 		pss->wsi = wsi;
 		break;
@@ -380,7 +382,7 @@
 	{ "port",	required_argument,	NULL, 'p' },
 	{ "ssl",	no_argument,		NULL, 's' },
 	{ "killmask",	no_argument,		NULL, 'k' },
-	{ "interface",  required_argument, 	NULL, 'i' },
+	{ "interface",  required_argument,	NULL, 'i' },
 	{ "closetest",  no_argument,		NULL, 'c' },
 	{ NULL, 0, 0, 0 }
 };
@@ -399,7 +401,7 @@
 	struct libwebsocket_context *context;
 	int opts = 0;
 	char interface_name[128] = "";
-	const char * interface = NULL;
+	const char *interface = NULL;
 #ifdef LWS_NO_FORK
 	unsigned int oldus = 0;
 #endif