From: Andy Green Date: Sun, 13 Feb 2011 08:54:05 +0000 (+0000) Subject: add-example-protocol-handshake-filter-callback.patch X-Git-Tag: 1.2~183 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=38c4f0c556bb72504174f4c663b90868355b39a5;p=profile%2Fivi%2Flibwebsockets.git add-example-protocol-handshake-filter-callback.patch Signed-off-by: Andy Green --- diff --git a/test-server/test-server-extpoll.c b/test-server/test-server-extpoll.c index 725fb95..450e5f6 100644 --- a/test-server/test-server-extpoll.c +++ b/test-server/test-server-extpoll.c @@ -157,6 +157,50 @@ static int callback_http(struct libwebsocket *wsi, return 0; } +/* + * this is just an example of parsing handshake headers, you don't need this + * in your code unless you will filter allowing connections by the header + * content + */ + +static void +dump_handshake_info(struct lws_tokens *lwst) +{ + int n; + static const char *token_names[] = { + [WSI_TOKEN_GET_URI] = "GET URI", + [WSI_TOKEN_HOST] = "Host", + [WSI_TOKEN_CONNECTION] = "Connection", + [WSI_TOKEN_KEY1] = "key 1", + [WSI_TOKEN_KEY2] = "key 2", + [WSI_TOKEN_PROTOCOL] = "Protocol", + [WSI_TOKEN_UPGRADE] = "Upgrade", + [WSI_TOKEN_ORIGIN] = "Origin", + [WSI_TOKEN_DRAFT] = "Draft", + [WSI_TOKEN_CHALLENGE] = "Challenge", + + /* new for 04 */ + [WSI_TOKEN_KEY] = "Key", + [WSI_TOKEN_VERSION] = "Version", + [WSI_TOKEN_SWORIGIN] = "Sworigin", + + /* new for 05 */ + [WSI_TOKEN_EXTENSIONS] = "Extensions", + + /* client receives these */ + [WSI_TOKEN_ACCEPT] = "Accept", + [WSI_TOKEN_NONCE] = "Nonce", + [WSI_TOKEN_HTTP] = "Http", + }; + + for (n = 0; n < WSI_TOKEN_COUNT; n++) { + if (lwst[n].token == NULL) + continue; + + fprintf(stderr, " %s = %s\n", token_names[n], lwst[n].token); + } +} + /* dumb_increment protocol */ /* @@ -211,6 +255,17 @@ callback_dumb_increment(struct libwebsocket *wsi, pss->number = 0; break; + /* + * this just demonstrates how to use the protocol filter. If you won't + * study and reject connections based on header content, you don't need + * to handle this callback + */ + + case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: + dump_handshake_info((struct lws_tokens *)(long)user); + /* you could return non-zero here and kill the connection */ + break; + default: break; } @@ -312,6 +367,17 @@ callback_lws_mirror(struct libwebsocket *wsi, libwebsockets_get_protocol(wsi)); break; + /* + * this just demonstrates how to use the protocol filter. If you won't + * study and reject connections based on header content, you don't need + * to handle this callback + */ + + case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: + dump_handshake_info((struct lws_tokens *)(long)user); + /* you could return non-zero here and kill the connection */ + break; + default: break; } diff --git a/test-server/test-server.c b/test-server/test-server.c index f4995c7..9143c03 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -111,6 +111,51 @@ static int callback_http(struct libwebsocket *wsi, return 0; } + +/* + * this is just an example of parsing handshake headers, you don't need this + * in your code unless you will filter allowing connections by the header + * content + */ + +static void +dump_handshake_info(struct lws_tokens *lwst) +{ + int n; + static const char *token_names[] = { + [WSI_TOKEN_GET_URI] = "GET URI", + [WSI_TOKEN_HOST] = "Host", + [WSI_TOKEN_CONNECTION] = "Connection", + [WSI_TOKEN_KEY1] = "key 1", + [WSI_TOKEN_KEY2] = "key 2", + [WSI_TOKEN_PROTOCOL] = "Protocol", + [WSI_TOKEN_UPGRADE] = "Upgrade", + [WSI_TOKEN_ORIGIN] = "Origin", + [WSI_TOKEN_DRAFT] = "Draft", + [WSI_TOKEN_CHALLENGE] = "Challenge", + + /* new for 04 */ + [WSI_TOKEN_KEY] = "Key", + [WSI_TOKEN_VERSION] = "Version", + [WSI_TOKEN_SWORIGIN] = "Sworigin", + + /* new for 05 */ + [WSI_TOKEN_EXTENSIONS] = "Extensions", + + /* client receives these */ + [WSI_TOKEN_ACCEPT] = "Accept", + [WSI_TOKEN_NONCE] = "Nonce", + [WSI_TOKEN_HTTP] = "Http", + }; + + for (n = 0; n < WSI_TOKEN_COUNT; n++) { + if (lwst[n].token == NULL) + continue; + + fprintf(stderr, " %s = %s\n", token_names[n], lwst[n].token); + } +} + /* dumb_increment protocol */ /* @@ -164,6 +209,16 @@ callback_dumb_increment(struct libwebsocket *wsi, if (strcmp(in, "reset\n") == 0) pss->number = 0; break; + /* + * this just demonstrates how to use the protocol filter. If you won't + * study and reject connections based on header content, you don't need + * to handle this callback + */ + + case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: + dump_handshake_info((struct lws_tokens *)(long)user); + /* you could return non-zero here and kill the connection */ + break; default: break; @@ -262,6 +317,16 @@ callback_lws_mirror(struct libwebsocket *wsi, libwebsocket_callback_on_writable_all_protocol( libwebsockets_get_protocol(wsi)); break; + /* + * this just demonstrates how to use the protocol filter. If you won't + * study and reject connections based on header content, you don't need + * to handle this callback + */ + + case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: + dump_handshake_info((struct lws_tokens *)(long)user); + /* you could return non-zero here and kill the connection */ + break; default: break;