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");