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>
This commit is contained in:
Andy Green 2011-01-28 10:00:18 +00:00
parent 5b9a4c0d43
commit 2ac5a6fce4
6 changed files with 57 additions and 8 deletions

View file

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

View file

@ -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

View file

@ -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 @@ 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;

View file

@ -112,6 +112,23 @@ If the output side of a server process becomes choked, this allows flow
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>

View file

@ -25,7 +25,6 @@
#include <getopt.h>
#include <string.h>
#include "../lib/libwebsockets.h"
#include <poll.h>
@ -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 <andy@warmcat.com> "
"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) {

View file

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