From 2ac5a6fce4afaf88b54b54b0337ef1755baab496 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Fri, 28 Jan 2011 10:00:18 +0000 Subject: [PATCH] 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 Signed-off-by: Andy Green --- lib/libwebsockets.c | 29 +++++++++++++++++++++++++++++ lib/libwebsockets.h | 5 ++++- lib/private-libwebsockets.h | 2 ++ libwebsockets-api-doc.html | 17 +++++++++++++++++ test-server/test-client.c | 6 ++---- test-server/test-ping.c | 6 +++--- 6 files changed, 57 insertions(+), 8 deletions(-) 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 @@ libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) 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 @@ libwebsocket_create_context(int port, 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 @@ libwebsocket_create_context(int port, 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 @@ libwebsocket_client_connect(struct libwebsocket_context *clients, 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 #include #include +#include #include #include @@ -169,6 +170,7 @@ struct libwebsocket_context { 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 @@ If the output side of a server process becomes choked, this allows flow control for the input side.
+

libwebsocket_canonical_hostname - returns this host's hostname

+const char * +libwebsocket_canonical_hostname +(struct libwebsocket_context * this) +

Arguments

+
+
this +
Websocket context +
+

Description

+
+

+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. +

+

libwebsocket_create_context - Create the websocket handler

struct libwebsocket_context * libwebsocket_create_context 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 #include - #include "../lib/libwebsockets.h" #include @@ -170,7 +169,6 @@ int main(int argc, char **argv) struct libwebsocket *wsi_dumb; struct libwebsocket *wsi_mirror; - fprintf(stderr, "libwebsockets test client\n" "(C) Copyright 2010 Andy Green " "licensed under LGPL2.1\n"); @@ -215,7 +213,7 @@ int main(int argc, char **argv) /* 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 @@ int main(int argc, char **argv) /* 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 @@ int main(int argc, char **argv) /* 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"); -- 2.7.4