| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#ifdef G_LOG_DOMAIN |
| 9 |
#undef G_LOG_DOMAIN |
| 10 |
#endif |
| 11 |
#define G_LOG_DOMAIN "WebSocket" |
| 12 |
|
| 13 |
#include "websocketserver.h" |
| 14 |
#include "websocketprivate.h" |
| 15 |
|
| 16 |
#include <string.h> |
| 17 |
#include <fnmatch.h> |
| 18 |
|
| 19 |
enum { |
| 20 |
PROP_0, |
| 21 |
PROP_ORIGINS, |
| 22 |
PROP_PROTOCOLS, |
| 23 |
PROP_REQUEST_HEADERS, |
| 24 |
PROP_INPUT_BUFFER, |
| 25 |
}; |
| 26 |
|
| 27 |
struct _WebSocketServer |
| 28 |
{ |
| 29 |
WebSocketConnection parent; |
| 30 |
|
| 31 |
gboolean protocol_chosen; |
| 32 |
gchar **allowed_origins; |
| 33 |
gchar **allowed_protocols; |
| 34 |
GHashTable *request_headers; |
| 35 |
}; |
| 36 |
|
| 37 |
struct _WebSocketServerClass |
| 38 |
{ |
| 39 |
WebSocketConnectionClass parent; |
| 40 |
}; |
| 41 |
|
| 42 |
G_DEFINE_TYPE (WebSocketServer, web_socket_server, WEB_SOCKET_TYPE_CONNECTION); |
| 43 |
|
| 44 |
static void |
| 45 |
web_socket_server_init (WebSocketServer *self) |
| 46 |
{ |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
respond_handshake_forbidden (WebSocketConnection *conn) |
| 52 |
{ |
| 53 |
GError *error; |
| 54 |
|
| 55 |
const gchar *bad_request = "HTTP/1.1 403 Forbidden\r\n" |
| 56 |
"Connection: close\r\n" |
| 57 |
"\r\n" |
| 58 |
"<html><head><title>403 Forbidden</title></head>\r\n" |
| 59 |
"<body>Received invalid WebSocket request</body></html>\r\n"; |
| 60 |
|
| 61 |
_web_socket_connection_queue (conn, WEB_SOCKET_QUEUE_URGENT | WEB_SOCKET_QUEUE_LAST, |
| 62 |
g_strdup (bad_request), strlen (bad_request), 0); |
| 63 |
g_debug ("queued: forbidden request response"); |
| 64 |
|
| 65 |
error = g_error_new_literal (WEB_SOCKET_ERROR, |
| 66 |
WEB_SOCKET_CLOSE_PROTOCOL, |
| 67 |
"Received invalid handshake request from the client"); |
| 68 |
_web_socket_connection_error (conn, error); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
respond_handshake_bad (WebSocketConnection *conn) |
| 73 |
{ |
| 74 |
GError *error; |
| 75 |
|
| 76 |
const gchar *bad_request = "HTTP/1.1 400 Bad Request\r\n" |
| 77 |
"Connection: close\r\n" |
| 78 |
"\r\n" |
| 79 |
"<html><head><title>400 Bad Request</title></head>\r\n" |
| 80 |
"<body>Received invalid WebSocket request</body></html>\r\n"; |
| 81 |
|
| 82 |
_web_socket_connection_queue (conn, WEB_SOCKET_QUEUE_URGENT | WEB_SOCKET_QUEUE_LAST, |
| 83 |
g_strdup (bad_request), strlen (bad_request), 0); |
| 84 |
g_debug ("queued: bad request response"); |
| 85 |
|
| 86 |
error = g_error_new_literal (WEB_SOCKET_ERROR, |
| 87 |
WEB_SOCKET_CLOSE_PROTOCOL, |
| 88 |
"Received invalid handshake request from the client"); |
| 89 |
_web_socket_connection_error (conn, error); |
| 90 |
} |
| 91 |
|
| 92 |
gchar * |
| 93 |
_web_socket_complete_accept_key_rfc6455 (const gchar *key) |
| 94 |
{ |
| 95 |
gsize digest_len = 20; |
| 96 |
guchar digest[digest_len]; |
| 97 |
GChecksum *checksum; |
| 98 |
|
| 99 |
checksum = g_checksum_new (G_CHECKSUM_SHA1); |
| 100 |
g_return_val_if_fail (checksum != NULL, NULL); |
| 101 |
|
| 102 |
g_checksum_update (checksum, (guchar *)key, -1); |
| 103 |
|
| 104 |
/* magic from: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17 */ |
| 105 |
g_checksum_update (checksum, (guchar *)"258EAFA5-E914-47DA-95CA-C5AB0DC85B11", -1); |
| 106 |
|
| 107 |
g_checksum_get_digest (checksum, digest, &digest_len); |
| 108 |
g_checksum_free (checksum); |
| 109 |
|
| 110 |
g_assert (digest_len == 20); |
| 111 |
|
| 112 |
return g_base64_encode (digest, digest_len); |
| 113 |
} |
| 114 |
|
| 115 |
static gboolean |
| 116 |
validate_rfc6455_websocket_key (const gchar *key) |
| 117 |
{ |
| 118 |
/* The key must be 16 bytes base64 encoded */ |
| 119 |
guchar *decoded; |
| 120 |
gsize length, len; |
| 121 |
len = strlen (key); |
| 122 |
if (len == 0 || len > 1024) |
| 123 |
return FALSE; |
| 124 |
decoded = g_base64_decode (key, &length); |
| 125 |
if (!decoded) |
| 126 |
return FALSE; |
| 127 |
g_free (decoded); |
| 128 |
return length == 16; |
| 129 |
} |
| 130 |
|
| 131 |
static gboolean |
| 132 |
respond_handshake_rfc6455 (WebSocketServer *self, |
| 133 |
WebSocketConnection *conn, |
| 134 |
GHashTable *headers) |
| 135 |
{ |
| 136 |
const gchar *protocol; |
| 137 |
const gchar *origin; |
| 138 |
const gchar *host; |
| 139 |
gchar *accept_key; |
| 140 |
gchar *key; |
| 141 |
GString *handshake; |
| 142 |
gsize len; |
| 143 |
guint i; |
| 144 |
|
| 145 |
if (!_web_socket_util_header_equals (headers, "Upgrade", "websocket") || |
| 146 |
!_web_socket_util_header_contains (headers, "Connection", "upgrade") || |
| 147 |
!_web_socket_util_header_equals (headers, "Sec-WebSocket-Version", "13") || |
| 148 |
!_web_socket_connection_choose_protocol (conn, (const gchar **)self->allowed_protocols, |
| 149 |
g_hash_table_lookup (headers, "Sec-WebSocket-Protocol"))) |
| 150 |
{ |
| 151 |
respond_handshake_bad (conn); |
| 152 |
return FALSE; |
| 153 |
} |
| 154 |
|
| 155 |
self->protocol_chosen = TRUE; |
| 156 |
|
| 157 |
key = g_hash_table_lookup (headers, "Sec-WebSocket-Key"); |
| 158 |
if (key == NULL) |
| 159 |
{ |
| 160 |
g_message ("received missing Sec-WebSocket-Key header"); |
| 161 |
respond_handshake_bad (conn); |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
if (!validate_rfc6455_websocket_key (key)) |
| 165 |
{ |
| 166 |
g_message ("received invalid Sec-WebSocket-Key header: %s", key); |
| 167 |
respond_handshake_bad (conn); |
| 168 |
return FALSE; |
| 169 |
} |
| 170 |
|
| 171 |
host = g_hash_table_lookup (headers, "Host"); |
| 172 |
if (host == NULL) |
| 173 |
{ |
| 174 |
g_message ("received request without Host"); |
| 175 |
respond_handshake_bad (conn); |
| 176 |
return FALSE; |
| 177 |
} |
| 178 |
|
| 179 |
if (self->allowed_origins) |
| 180 |
{ |
| 181 |
origin = g_hash_table_lookup (headers, "Origin"); |
| 182 |
if (!origin) |
| 183 |
{ |
| 184 |
g_message ("received request without Origin"); |
| 185 |
respond_handshake_forbidden (conn); |
| 186 |
return FALSE; |
| 187 |
} |
| 188 |
for (i = 0; self->allowed_origins[i] != NULL; i++) |
| 189 |
{ |
| 190 |
if (fnmatch (self->allowed_origins[i], origin, FNM_CASEFOLD) == 0) |
| 191 |
break; |
| 192 |
} |
| 193 |
if (self->allowed_origins[i] == NULL) |
| 194 |
{ |
| 195 |
g_message ("received request from bad Origin: %s", origin); |
| 196 |
respond_handshake_forbidden (conn); |
| 197 |
return FALSE; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
accept_key = _web_socket_complete_accept_key_rfc6455 (key); |
| 202 |
|
| 203 |
handshake = g_string_new (""); |
| 204 |
g_string_printf (handshake, "HTTP/1.1 101 Switching Protocols\r\n" |
| 205 |
"Upgrade: websocket\r\n" |
| 206 |
"Connection: Upgrade\r\n" |
| 207 |
"Sec-WebSocket-Accept: %s\r\n", |
| 208 |
(gchar *)accept_key); |
| 209 |
|
| 210 |
g_free (accept_key); |
| 211 |
|
| 212 |
protocol = web_socket_connection_get_protocol (conn); |
| 213 |
if (protocol) |
| 214 |
g_string_append_printf (handshake, "Sec-WebSocket-Protocol: %s\r\n", protocol); |
| 215 |
|
| 216 |
g_string_append (handshake, "\r\n"); |
| 217 |
|
| 218 |
len = handshake->len; |
| 219 |
_web_socket_connection_queue (conn, WEB_SOCKET_QUEUE_URGENT, |
| 220 |
g_string_free (handshake, FALSE), len, 0); |
| 221 |
g_debug ("queued response to rfc6455 handshake"); |
| 222 |
|
| 223 |
return TRUE; |
| 224 |
} |
| 225 |
|
| 226 |
static gboolean |
| 227 |
parse_handshake_request (WebSocketServer *self, |
| 228 |
WebSocketConnection *conn, |
| 229 |
GByteArray *incoming) |
| 230 |
{ |
| 231 |
GHashTable *headers; |
| 232 |
g_autofree gchar *method = NULL; |
| 233 |
g_autofree gchar *resource = NULL; |
| 234 |
gboolean valid; |
| 235 |
gssize in1, in2; |
| 236 |
gssize consumed; |
| 237 |
|
| 238 |
/* Headers already passed from caller */ |
| 239 |
if (self->request_headers) |
| 240 |
{ |
| 241 |
headers = self->request_headers; |
| 242 |
self->request_headers = NULL; |
| 243 |
method = g_strdup ("GET"); |
| 244 |
consumed = 0; |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
/* Parse the handshake response received from the server */ |
| 249 |
in1 = web_socket_util_parse_req_line ((const gchar *)incoming->data, |
| 250 |
incoming->len, &method, &resource); |
| 251 |
if (in1 < 0) |
| 252 |
{ |
| 253 |
g_message ("received invalid request line"); |
| 254 |
respond_handshake_bad (conn); |
| 255 |
} |
| 256 |
else if (in1 == 0) |
| 257 |
g_debug ("waiting for more handshake data"); |
| 258 |
if (in1 <= 0) |
| 259 |
return FALSE; |
| 260 |
|
| 261 |
/* Read in the handshake request from the client */ |
| 262 |
in2 = web_socket_util_parse_headers ((const gchar *)incoming->data + in1, |
| 263 |
incoming->len - in1, &headers); |
| 264 |
|
| 265 |
if (in2 < 0) |
| 266 |
{ |
| 267 |
g_message ("received invalid response headers"); |
| 268 |
respond_handshake_bad (conn); |
| 269 |
} |
| 270 |
else if (in2 == 0) |
| 271 |
g_debug ("waiting for more handshake data"); |
| 272 |
if (in2 <= 0) |
| 273 |
return FALSE; |
| 274 |
|
| 275 |
consumed = in1 + in2; |
| 276 |
} |
| 277 |
|
| 278 |
if (!g_str_equal (method, "GET")) |
| 279 |
{ |
| 280 |
g_message ("received unexpected method: %s", method); |
| 281 |
valid = FALSE; |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
valid = respond_handshake_rfc6455 (self, conn, headers); |
| 286 |
} |
| 287 |
|
| 288 |
if (valid) |
| 289 |
{ |
| 290 |
/* Handshake is successful */ |
| 291 |
g_debug ("open: responded to handshake"); |
| 292 |
} |
| 293 |
|
| 294 |
if (consumed > 0) |
| 295 |
g_byte_array_remove_range (incoming, 0, consumed); |
| 296 |
g_hash_table_unref (headers); |
| 297 |
|
| 298 |
return valid; |
| 299 |
} |
| 300 |
|
| 301 |
static gboolean |
| 302 |
web_socket_server_handshake (WebSocketConnection *conn, |
| 303 |
GByteArray *incoming) |
| 304 |
{ |
| 305 |
WebSocketServer *self = WEB_SOCKET_SERVER (conn); |
| 306 |
return parse_handshake_request (self, conn, incoming); |
| 307 |
} |
| 308 |
|
| 309 |
static void |
| 310 |
web_socket_server_constructed (GObject *object) |
| 311 |
{ |
| 312 |
WebSocketConnection *conn = WEB_SOCKET_CONNECTION (object); |
| 313 |
GIOStream *io_stream; |
| 314 |
|
| 315 |
G_OBJECT_CLASS (web_socket_server_parent_class)->constructed (object); |
| 316 |
|
| 317 |
io_stream = web_socket_connection_get_io_stream (conn); |
| 318 |
if (io_stream == NULL) |
| 319 |
{ |
| 320 |
g_critical ("server-side WebSocketConnection must be created " |
| 321 |
"with a io-stream property"); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
web_socket_server_set_property (GObject *object, |
| 327 |
guint prop_id, |
| 328 |
const GValue *value, |
| 329 |
GParamSpec *pspec) |
| 330 |
{ |
| 331 |
WebSocketServer *self = WEB_SOCKET_SERVER (object); |
| 332 |
|
| 333 |
switch (prop_id) |
| 334 |
{ |
| 335 |
case PROP_ORIGINS: |
| 336 |
g_return_if_fail (self->allowed_origins == FALSE); |
| 337 |
self->allowed_origins = g_value_dup_boxed (value); |
| 338 |
break; |
| 339 |
|
| 340 |
case PROP_PROTOCOLS: |
| 341 |
g_return_if_fail (self->protocol_chosen == FALSE); |
| 342 |
g_strfreev (self->allowed_protocols); |
| 343 |
self->allowed_protocols = g_value_dup_boxed (value); |
| 344 |
break; |
| 345 |
|
| 346 |
case PROP_REQUEST_HEADERS: |
| 347 |
g_return_if_fail (self->request_headers == NULL); |
| 348 |
self->request_headers = g_value_dup_boxed (value); |
| 349 |
break; |
| 350 |
|
| 351 |
case PROP_INPUT_BUFFER: |
| 352 |
_web_socket_connection_take_incoming (WEB_SOCKET_CONNECTION (self), |
| 353 |
g_value_dup_boxed (value)); |
| 354 |
break; |
| 355 |
|
| 356 |
default: |
| 357 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 358 |
break; |
| 359 |
|
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
static void |
| 364 |
web_socket_server_finalize (GObject *object) |
| 365 |
{ |
| 366 |
WebSocketServer *self = WEB_SOCKET_SERVER (object); |
| 367 |
|
| 368 |
g_strfreev (self->allowed_origins); |
| 369 |
g_strfreev (self->allowed_protocols); |
| 370 |
if (self->request_headers) |
| 371 |
g_hash_table_unref (self->request_headers); |
| 372 |
|
| 373 |
G_OBJECT_CLASS (web_socket_server_parent_class)->finalize (object); |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
web_socket_server_class_init (WebSocketServerClass *klass) |
| 378 |
{ |
| 379 |
WebSocketConnectionClass *conn_class = WEB_SOCKET_CONNECTION_CLASS (klass); |
| 380 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 381 |
|
| 382 |
object_class->constructed = web_socket_server_constructed; |
| 383 |
object_class->set_property = web_socket_server_set_property; |
| 384 |
object_class->finalize = web_socket_server_finalize; |
| 385 |
|
| 386 |
conn_class->server_behavior = TRUE; |
| 387 |
conn_class->handshake = web_socket_server_handshake; |
| 388 |
|
| 389 |
/** |
| 390 |
* WebSocketServer:origins: |
| 391 |
* |
| 392 |
* The allowed origins to receive client requests from. |
| 393 |
*/ |
| 394 |
g_object_class_install_property (object_class, PROP_ORIGINS, |
| 395 |
g_param_spec_boxed ("origins", "Possible Origins", "The possible HTTP origins", G_TYPE_STRV, |
| 396 |
G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS)); |
| 397 |
/** |
| 398 |
* WebSocketServer:protocols: |
| 399 |
* |
| 400 |
* The allowed protocols to negotiate with the client. |
| 401 |
*/ |
| 402 |
g_object_class_install_property (object_class, PROP_PROTOCOLS, |
| 403 |
g_param_spec_boxed ("protocols", "Possible Protocol", "The possible WebSocket protocols", G_TYPE_STRV, |
| 404 |
G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS)); |
| 405 |
|
| 406 |
/** |
| 407 |
* WebSocketServer:request-headers: |
| 408 |
* |
| 409 |
* If headers have already been parsed, passed in here. |
| 410 |
*/ |
| 411 |
g_object_class_install_property (object_class, PROP_REQUEST_HEADERS, |
| 412 |
g_param_spec_boxed ("request-headers", "Request Headers", "Already parsed headers", G_TYPE_HASH_TABLE, |
| 413 |
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 414 |
|
| 415 |
/** |
| 416 |
* WebSocketServer:input-buffer: |
| 417 |
* |
| 418 |
* When specifying a #WebSocketConnection:io-stream during construction, |
| 419 |
* if you've already read bytes (ie: containing an HTTP header) out of the |
| 420 |
* input stream, then you must pass in a buffer containing those initial bytes, |
| 421 |
* so the WebSocket can consume them. |
| 422 |
* |
| 423 |
* This is usually only useful for WebSocket server connections. See |
| 424 |
* web_socket_server_new_for_stream() |
| 425 |
*/ |
| 426 |
g_object_class_install_property (object_class, PROP_INPUT_BUFFER, |
| 427 |
g_param_spec_boxed ("input-buffer", "Input buffer", "Input buffer with seed data", G_TYPE_BYTE_ARRAY, |
| 428 |
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* web_socket_server_new_for_stream: |
| 434 |
* @origins: (allow-none): the origin to expect the client to report |
| 435 |
* @protocols: (allow-none): possible protocols for the client to |
| 436 |
* @io_stream: the IO stream to communicate over |
| 437 |
* @request_headers: (allow-none): already parsed headers, or %NULL |
| 438 |
* @input_buffer: (allow-none): initial bytes already read from the input stream, or %NULL |
| 439 |
* |
| 440 |
* Create a new server side WebSocket connection to communicate with a client. |
| 441 |
* |
| 442 |
* Since callers may have already read some bytes from the inputstream (ie: |
| 443 |
* the HTTP header Request-Line) those bytes should be included in the |
| 444 |
* @input_buffer argument so that the WebSocket can consume them. |
| 445 |
* |
| 446 |
* If @protocols are specified then these are used to negotiate a protocol |
| 447 |
* with the client. |
| 448 |
* |
| 449 |
* The input and output streams of the @io_stream must be pollable. |
| 450 |
* |
| 451 |
* If the input stream on the @io_stream has already been read, those |
| 452 |
* read bytes should be passed in the @input_buffer byte array. |
| 453 |
* |
| 454 |
* In addition if the HTTP headers have already been parsed, they should be |
| 455 |
* passed in using the @request_headers hash table. This should be a hash table |
| 456 |
* setup for case-insensitive lookups, as created by web_socket_util_new_headers(). |
| 457 |
* When passing in headers, fill in @input_buffer with any of the HTTP body |
| 458 |
* read from the input stream (ie: after the \r\n\r\n). |
| 459 |
* |
| 460 |
* Returns: (transfer full): a new WebSocket |
| 461 |
*/ |
| 462 |
WebSocketConnection * |
| 463 |
web_socket_server_new_for_stream (const gchar * const *origins, |
| 464 |
const gchar * const *protocols, |
| 465 |
GIOStream *io_stream, |
| 466 |
GHashTable *request_headers, |
| 467 |
GByteArray *input_buffer) |
| 468 |
{ |
| 469 |
return g_object_new (WEB_SOCKET_TYPE_SERVER, |
| 470 |
"origins", origins, |
| 471 |
"protocols", protocols, |
| 472 |
"io-stream", io_stream, |
| 473 |
"request-headers", request_headers, |
| 474 |
"input-buffer", input_buffer, |
| 475 |
NULL); |
| 476 |
} |