fix-host-header-contents--introduce-canonical-hostname-api.patch

I?aki pointed out the dummy host field used in client test and ping
is not valid http.  This patch changes it to use the actual host
name and adds an api to collect that from the context cheaply.

Reported-by: I?aki Baz Castillo <ibc@aliax.net>
Signed-off-by: Andy Green <andy@warmcat.com>
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 2674aee..044a158 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -561,6 +561,24 @@
 	return 1;
 }
 
+/**
+ * libwebsocket_canonical_hostname() - returns this host's hostname
+ *
+ * This is typically used by client code to fill in the host parameter
+ * when making a client connection.  You can only call it after the context
+ * has been created.
+ *
+ * @this:	Websocket context
+ */
+
+
+extern const char *
+libwebsocket_canonical_hostname(struct libwebsocket_context *this)
+{
+	return (const char *)this->canonical_hostname;
+}
+
+
 static void sigpipe_handler(int x)
 {
 }
@@ -624,6 +642,8 @@
 	struct libwebsocket_context *this = NULL;
 	unsigned int slen;
 	char *p;
+	char hostname[1024];
+	struct hostent* he;
 
 #ifdef LWS_OPENSSL_SUPPORT
 	SSL_METHOD *method;
@@ -640,6 +660,15 @@
 	this->http_proxy_port = 0;
 	this->http_proxy_address[0] = '\0';
 
+	/* find canonical hostname */
+
+	hostname[(sizeof hostname) - 1] = '\0';
+	gethostname(hostname, (sizeof hostname) - 1);
+	he = gethostbyname(hostname);
+	strncpy(this->canonical_hostname, he->h_name,
+					   sizeof this->canonical_hostname - 1);
+	this->canonical_hostname[sizeof this->canonical_hostname - 1] = '\0';
+
 	/* split the proxy ads:port if given */
 
 	p = getenv("http_proxy");
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index ac28abf..f68a4a3 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -249,7 +249,10 @@
 			      const char *origin,
 			      const char *protocol);
 
-void
+extern const char *
+libwebsocket_canonical_hostname(struct libwebsocket_context *this);
+
+extern void
 libwebsocket_client_close(struct libwebsocket *wsi);
 
 #endif
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index 2cb3f2b..6a5df59 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -28,6 +28,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <netdb.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -169,6 +170,7 @@
 	int fds_count;
 	int listen_port;
 	char http_proxy_address[256];
+	char canonical_hostname[1024];
 	unsigned int http_proxy_port;
 #ifdef LWS_OPENSSL_SUPPORT
 	int use_ssl;
diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html
index d070343..f3ee10e 100644
--- a/libwebsockets-api-doc.html
+++ b/libwebsockets-api-doc.html
@@ -112,6 +112,23 @@
 control for the input side.
 </blockquote>
 <hr>
+<h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
+<i>const char *</i>
+<b>libwebsocket_canonical_hostname</b>
+(<i>struct libwebsocket_context *</i> <b>this</b>)
+<h3>Arguments</h3>
+<dl>
+<dt><b>this</b>
+<dd>Websocket context
+</dl>
+<h3>Description</h3>
+<blockquote>
+<p>
+This is typically used by client code to fill in the host parameter
+when making a client connection.  You can only call it after the context
+has been created.
+</blockquote>
+<hr>
 <h2>libwebsocket_create_context - Create the websocket handler</h2>
 <i>struct libwebsocket_context *</i>
 <b>libwebsocket_create_context</b>
diff --git a/test-server/test-client.c b/test-server/test-client.c
index 734aab2..289374a 100644
--- a/test-server/test-client.c
+++ b/test-server/test-client.c
@@ -25,7 +25,6 @@
 #include <getopt.h>
 #include <string.h>
 
-
 #include "../lib/libwebsockets.h"
 #include <poll.h>
 
@@ -170,7 +169,6 @@
 	struct libwebsocket *wsi_dumb;
 	struct libwebsocket *wsi_mirror;
 
-
 	fprintf(stderr, "libwebsockets test client\n"
 			"(C) Copyright 2010 Andy Green <andy@warmcat.com> "
 						    "licensed under LGPL2.1\n");
@@ -215,7 +213,7 @@
 	/* create a client websocket using dumb increment protocol */
 
 	wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
-					"/", "http://host", "origin",
+			"/", libwebsocket_canonical_hostname(context), "origin",
 				       protocols[PROTOCOL_DUMB_INCREMENT].name);
 
 	if (wsi_dumb == NULL) {
@@ -226,7 +224,7 @@
 	/* create a client websocket using mirror protocol */
 
 	wsi_mirror = libwebsocket_client_connect(context, address, port,
-					use_ssl,  "/", "http://host", "origin",
+	     use_ssl,  "/", libwebsocket_canonical_hostname(context), "origin",
 				       protocols[PROTOCOL_LWS_MIRROR].name);
 
 	if (wsi_mirror == NULL) {
diff --git a/test-server/test-ping.c b/test-server/test-ping.c
index 6911923..434e9d0 100644
--- a/test-server/test-ping.c
+++ b/test-server/test-ping.c
@@ -370,9 +370,9 @@
 
 	/* create a client websocket using dumb increment protocol */
 
-	wsi_mirror = libwebsocket_client_connect(context, address, port, use_ssl,
-					"/", "http://host", "origin",
-				       protocols[PROTOCOL_LWS_MIRROR].name);
+	wsi_mirror = libwebsocket_client_connect(context, address, port,
+			use_ssl, "/", libwebsocket_canonical_hostname(context),
+				 "origin", protocols[PROTOCOL_LWS_MIRROR].name);
 
 	if (wsi_mirror == NULL) {
 		fprintf(stderr, "libwebsocket connect failed\n");