add-Sec-WebSocket-Draft-and-protocol-autodetect.patch

Signed-off-by: Andy Green <andy@warmcat.com>
This commit is contained in:
Andy Green 2010-11-11 12:48:13 +00:00
parent ccbcef3931
commit ce510c6beb
7 changed files with 37 additions and 66 deletions

View file

@ -112,6 +112,24 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len)
/* completed header processing, but missing some bits */
goto bail;
/* are we happy about the draft version client side wants? */
if (wsi->utf8_token[WSI_TOKEN_DRAFT].token) {
wsi->ietf_spec_revision =
atoi(wsi->utf8_token[WSI_TOKEN_DRAFT].token);
switch (wsi->ietf_spec_revision) {
case 76:
break;
case 2:
break;
default:
fprintf(stderr, "Rejecting handshake on seeing "
"unsupported draft request %d\n",
wsi->ietf_spec_revision);
goto bail;
}
}
/* Make sure user side is happy about protocol */
if (wsi->callback)

View file

@ -69,7 +69,6 @@ libwebsocket_close_and_free_session(struct libwebsocket *wsi)
* libwebsocket_create_server() - Create the listening websockets server
* @port: Port to listen on
* @callback: The callback in user code to perform actual serving
* @protocol: Which version of the websockets protocol (currently 76)
* @user_area_size: How much memory to allocate per connection session
* which will be used by the user application to store
* per-session data. A pointer to this space is given
@ -99,13 +98,13 @@ libwebsocket_close_and_free_session(struct libwebsocket *wsi)
*/
int libwebsocket_create_server(int port,
int (*callback)(struct libwebsocket *,
enum libwebsocket_callback_reasons,
void *, void *, size_t),
int protocol, size_t user_area_size,
const char * ssl_cert_filepath,
const char * ssl_private_key_filepath,
int gid, int uid)
int (*callback)(struct libwebsocket *,
enum libwebsocket_callback_reasons,
void *, void *, size_t),
size_t user_area_size,
const char * ssl_cert_filepath,
const char * ssl_private_key_filepath,
int gid, int uid)
{
int n;
int client;
@ -186,20 +185,6 @@ int libwebsocket_create_server(int port,
/* SSL is happy and has a cert it's content with */
}
#endif
/* sanity check */
switch (protocol) {
case 0:
case 2:
case 76:
fprintf(stderr, " Using protocol v%d\n", protocol);
break;
default:
fprintf(stderr, "protocol %d not supported (try 0 2 or 76)\n",
protocol);
return -1;
}
if (!callback) {
fprintf(stderr, "callback is not optional!\n");
@ -346,7 +331,12 @@ int libwebsocket_create_server(int port,
}
wsi[fds_count]->callback = callback;
wsi[fds_count]->ietf_spec_revision = protocol;
/*
* Default protocol is 76
* After 76, there's a header specified to inform which
* draft the client wants
*/
wsi[fds_count]->ietf_spec_revision = 76;
fds[fds_count].events = POLLIN;
fds[fds_count++].fd = fd;

View file

@ -44,7 +44,7 @@ extern int libwebsocket_create_server(int port,
int (*callback)(struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len),
int protocol, size_t user_space,
size_t user_space,
const char * ssl_cert_filepath,
const char * ssl_private_key_filepath,
int gid, int uid);

View file

@ -30,6 +30,7 @@ const struct lws_tokens lws_tokens[WSI_TOKEN_COUNT] = {
{ "Sec-WebSocket-Protocol:", 23 },
{ "Upgrade:", 8 },
{ "Origin:", 7 },
{ "Sec-WebSocket-Draft:", 20 },
{ "\x0d\x0a", 2 },
};
@ -46,6 +47,7 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
case WSI_TOKEN_PROTOCOL:
case WSI_TOKEN_UPGRADE:
case WSI_TOKEN_ORIGIN:
case WSI_TOKEN_DRAFT:
case WSI_TOKEN_CHALLENGE:
debug("WSI_TOKEN_(%d) '%c'\n", wsi->parser_state, c);

View file

@ -85,6 +85,7 @@ enum lws_token_indexes {
WSI_TOKEN_PROTOCOL,
WSI_TOKEN_UPGRADE,
WSI_TOKEN_ORIGIN,
WSI_TOKEN_DRAFT,
WSI_TOKEN_CHALLENGE,
/* always last real token index*/

View file

@ -2,8 +2,7 @@
<i>int</i>
<b>libwebsocket_create_server</b>
(<i>int</i> <b>port</b>,
<i>int (*</i><b>callback</b>) <i>(struct libwebsocket *, enum libwebsocket_callback_reasons, void *, void *, size_t)</i>,
<i>int</i> <b>protocol</b>,
<i>int (*</i><b>callback</b>) <i>(struct libwebsocket *, enum libwebsocket_callback_reasons, void *, void *, size_t)</i>,
<i>size_t</i> <b>user_area_size</b>,
<i>const char *</i> <b>ssl_cert_filepath</b>,
<i>const char *</i> <b>ssl_private_key_filepath</b>,
@ -15,8 +14,6 @@
<dd>Port to listen on
<dt><b>callback</b>
<dd>The callback in user code to perform actual serving
<dt><b>protocol</b>
<dd>Which version of the websockets protocol (currently 76)
<dt><b>user_area_size</b>
<dd>How much memory to allocate per connection session
which will be used by the user application to store
@ -52,39 +49,6 @@ images or whatever over http and dynamic data over websockets all in
one place; they're all handled in the user callback.
</blockquote>
<hr>
<h2>libwebsocket_get_uri - Return the URI path being requested</h2>
<i>const char *</i>
<b>libwebsocket_get_uri</b>
(<i>struct libwebsocket *</i> <b>wsi</b>)
<h3>Arguments</h3>
<dl>
<dt><b>wsi</b>
<dd>Websocket instance
</dl>
<h3>Description</h3>
<blockquote>
The user code can find out the local path being opened from this
call, it's valid on HTTP or established websocket connections.
If the client opened the connection with "http://127.0.0.1/xyz/abc.d"
then this call will return a pointer to "/xyz/abc.d"
</blockquote>
<hr>
<h2>libwebsocket_get_protocol - Return the list of protocols being requested</h2>
<i>const char *</i>
<b>libwebsocket_get_protocol</b>
(<i>struct libwebsocket *</i> <b>wsi</b>)
<h3>Arguments</h3>
<dl>
<dt><b>wsi</b>
<dd>Websocket instance
</dl>
<h3>Description</h3>
<blockquote>
The user code can find out which protocols the client is asking to
work with by calling this. It may return NULL if there was no
protocol header specified.
</blockquote>
<hr>
<h2>libwebsocket_write - Apply protocol then write data to client</h2>
<i>int</i>
<b>libwebsocket_write</b>

View file

@ -29,7 +29,6 @@
#define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server"
static int port = 7681;
static int ws_protocol = 76;
static int use_ssl = 0;
struct per_session_data {
@ -207,9 +206,6 @@ int main(int argc, char **argv)
case 'p':
port = atoi(optarg);
break;
case 'r':
ws_protocol = atoi(optarg);
break;
case 'h':
fprintf(stderr, "Usage: test-server "
"[--port=<p>] [--protocol=<v>]\n");
@ -220,7 +216,7 @@ int main(int argc, char **argv)
if (!use_ssl)
cert_path = key_path = NULL;
if (libwebsocket_create_server(port, websocket_callback, ws_protocol,
if (libwebsocket_create_server(port, websocket_callback,
sizeof(struct per_session_data),
cert_path, key_path, -1, -1) < 0) {
fprintf(stderr, "libwebsocket init failed\n");