| 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 "websocket.h" |
| 14 |
#include "websocketprivate.h" |
| 15 |
|
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
/** |
| 20 |
* WebSocketState: |
| 21 |
* @WEB_SOCKET_STATE_CONNECTING: the WebSocket is not yet ready to send messages |
| 22 |
* @WEB_SOCKET_STATE_OPEN: the Websocket is ready to send messages |
| 23 |
* @WEB_SOCKET_STATE_CLOSING: the Websocket is in the process of closing down, no further messages sent |
| 24 |
* @WEB_SOCKET_STATE_CLOSED: the Websocket is completely closed down |
| 25 |
* |
| 26 |
* The WebSocket is in the %WEB_SOCKET_STATE_CONNECTING state during initial |
| 27 |
* connection setup, and handshaking. If the handshake or connection fails it |
| 28 |
* can go directly to the %WEB_SOCKET_STATE_CLOSED state from here. |
| 29 |
* |
| 30 |
* Once the WebSocket handshake completes successfully it will be in the |
| 31 |
* %WEB_SOCKET_STATE_OPEN state. During this state, and only during this state |
| 32 |
* can WebSocket messages be sent. |
| 33 |
* |
| 34 |
* WebSocket messages can be received during either the %WEB_SOCKET_STATE_OPEN |
| 35 |
* or %WEB_SOCKET_STATE_CLOSING states. |
| 36 |
* |
| 37 |
* The WebSocket goes into the %WEB_SOCKET_STATE_CLOSING state once it has |
| 38 |
* successfully sent a close request to the peer. If we had not yet received |
| 39 |
* an earlier close request from the peer, then the WebSocket waits for a |
| 40 |
* response to the close request (until a timeout). |
| 41 |
* |
| 42 |
* Once actually closed completely down the WebSocket state is |
| 43 |
* %WEB_SOCKET_STATE_CLOSED. No communication is possible during this state. |
| 44 |
*/ |
| 45 |
|
| 46 |
GQuark |
| 47 |
web_socket_error_get_quark (void) |
| 48 |
{ |
| 49 |
return g_quark_from_static_string ("web-socket-error-quark"); |
| 50 |
} |
| 51 |
|
| 52 |
static inline const gchar * |
| 53 |
strskip (const gchar *start, |
| 54 |
const gchar c, |
| 55 |
const gchar *end) |
| 56 |
{ |
| 57 |
while (start != end && start[0] == c) |
| 58 |
start++; |
| 59 |
return start; |
| 60 |
} |
| 61 |
|
| 62 |
static gsize |
| 63 |
parse_version (const gchar *data, |
| 64 |
gsize length, |
| 65 |
gchar **version) |
| 66 |
{ |
| 67 |
if (length < 8) |
| 68 |
return 0; |
| 69 |
if (memcmp (data, "HTTP/1.0", 8) != 0 && |
| 70 |
memcmp (data, "HTTP/1.1", 8) != 0) |
| 71 |
return 0; |
| 72 |
if (version) |
| 73 |
*version = g_strndup (data, 8); |
| 74 |
return 8; |
| 75 |
} |
| 76 |
|
| 77 |
gboolean |
| 78 |
_web_socket_util_parse_url (const gchar *url, |
| 79 |
gchar **out_scheme, |
| 80 |
gchar **out_host, |
| 81 |
gchar **out_path, |
| 82 |
GError **error) |
| 83 |
{ |
| 84 |
const gchar *colon; |
| 85 |
const gchar *host; |
| 86 |
const gchar *path; |
| 87 |
|
| 88 |
colon = strchr (url, ':'); |
| 89 |
if (colon == NULL || |
| 90 |
(colon[1] != '/' && colon[2] != '/')) |
| 91 |
{ |
| 92 |
/* The same error as g_network_address_parse_uri() */ |
| 93 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
| 94 |
"Invalid URI '%s'", url); |
| 95 |
return FALSE; |
| 96 |
} |
| 97 |
path = strchr (colon + 3, '/'); |
| 98 |
host = strchr (colon + 3, '@'); |
| 99 |
if (host && (!path || host < path)) |
| 100 |
{ |
| 101 |
host++; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
host = colon + 3; |
| 106 |
path = strchr (host, '/'); |
| 107 |
} |
| 108 |
|
| 109 |
if (host[0] == '\0' || host == path) |
| 110 |
{ |
| 111 |
/* The same error as g_network_address_parse_uri() */ |
| 112 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
| 113 |
"Invalid URI '%s'", url); |
| 114 |
return FALSE; |
| 115 |
} |
| 116 |
|
| 117 |
if (out_scheme) |
| 118 |
*out_scheme = g_strndup (url, colon - url); |
| 119 |
if (out_host) |
| 120 |
{ |
| 121 |
if (path) |
| 122 |
*out_host = g_strndup (host, path - host); |
| 123 |
else |
| 124 |
*out_host = g_strdup (host); |
| 125 |
} |
| 126 |
|
| 127 |
if (out_path) |
| 128 |
{ |
| 129 |
if (!path) |
| 130 |
path = "/"; |
| 131 |
*out_path = g_strdup (path); |
| 132 |
} |
| 133 |
|
| 134 |
return TRUE; |
| 135 |
} |
| 136 |
|
| 137 |
static gboolean |
| 138 |
is_valid_line (const gchar *string, |
| 139 |
gssize length) |
| 140 |
{ |
| 141 |
gint i; |
| 142 |
|
| 143 |
if (length < 0) |
| 144 |
length = strlen (string); |
| 145 |
|
| 146 |
for (i = 0; i < length; i++) |
| 147 |
{ |
| 148 |
if (string[i] != '\t') |
| 149 |
{ |
| 150 |
if (string[i] < ' ' || string[i] & 0x80) |
| 151 |
return FALSE; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return TRUE; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* web_socket_util_parse_req_line: |
| 160 |
* @data: (array length=length): the input data |
| 161 |
* @length: length of data |
| 162 |
* @method: (out): location to place HTTP method, or %NULL |
| 163 |
* @resource: (out): location to place HTTP resource path, or %NULL |
| 164 |
* |
| 165 |
* Parse an HTTP request line. |
| 166 |
* |
| 167 |
* The number of bytes parsed will be returned if parsing succeeds, including |
| 168 |
* the new line at the end of the request line. A negative value will be |
| 169 |
* returned if parsing fails. |
| 170 |
* |
| 171 |
* If the HTTP request line was truncated (ie: not all of it was present |
| 172 |
* within @length) then zero will be returned. |
| 173 |
* |
| 174 |
* The @method and @resource should point to string pointers. The values |
| 175 |
* returned should be freed by the caller using g_free(). |
| 176 |
* |
| 177 |
* Return value: zero if truncated, negative if fails, or number of |
| 178 |
* characters parsed |
| 179 |
*/ |
| 180 |
gssize |
| 181 |
web_socket_util_parse_req_line (const gchar *data, |
| 182 |
gsize length, |
| 183 |
gchar **method, |
| 184 |
gchar **resource) |
| 185 |
{ |
| 186 |
const gchar *end; |
| 187 |
const gchar *method_end; |
| 188 |
const gchar *path_beg; |
| 189 |
const gchar *path_end; |
| 190 |
const gchar *version; |
| 191 |
const gchar *last; |
| 192 |
gsize n; |
| 193 |
|
| 194 |
/* |
| 195 |
* Here we parse a line like: |
| 196 |
* |
| 197 |
* GET /path/to/file HTTP/1.1 |
| 198 |
*/ |
| 199 |
|
| 200 |
g_return_val_if_fail (data != NULL || length == 0, -1); |
| 201 |
|
| 202 |
if (length == 0) |
| 203 |
return 0; |
| 204 |
|
| 205 |
end = memchr (data, '\n', length); |
| 206 |
if (end == NULL) |
| 207 |
return 0; /* need more data */ |
| 208 |
|
| 209 |
if (data[0] == ' ') |
| 210 |
return -1; |
| 211 |
|
| 212 |
method_end = memchr (data, ' ', (end - data)); |
| 213 |
if (method_end == NULL) |
| 214 |
return -1; |
| 215 |
|
| 216 |
path_beg = strskip (method_end + 1, ' ', end); |
| 217 |
path_end = memchr (path_beg, ' ', (end - path_beg)); |
| 218 |
if (path_end == NULL) |
| 219 |
return -1; |
| 220 |
|
| 221 |
version = strskip (path_end + 1, ' ', end); |
| 222 |
|
| 223 |
/* Returns number of characters consumed */ |
| 224 |
n = parse_version (version, (end - version), NULL); |
| 225 |
if (n == 0) |
| 226 |
return -1; |
| 227 |
|
| 228 |
last = version + n; |
| 229 |
while (last != end) |
| 230 |
{ |
| 231 |
/* Acceptable trailing characters */ |
| 232 |
if (!strchr ("\r ", last[0])) |
| 233 |
return -1; |
| 234 |
last++; |
| 235 |
} |
| 236 |
|
| 237 |
if (!is_valid_line (data, (method_end - data)) || |
| 238 |
!is_valid_line (data, (path_end - path_beg))) |
| 239 |
return -1; |
| 240 |
|
| 241 |
if (method) |
| 242 |
*method = g_strndup (data, (method_end - data)); |
| 243 |
if (resource) |
| 244 |
*resource = g_strndup (path_beg, (path_end - path_beg)); |
| 245 |
return (end - data) + 1; |
| 246 |
} |
| 247 |
|
| 248 |
static guint |
| 249 |
str_case_hash (gconstpointer v) |
| 250 |
{ |
| 251 |
/* A case agnostic version of g_str_hash */ |
| 252 |
const signed char *p; |
| 253 |
guint32 h = 5381; |
| 254 |
for (p = v; *p != '\0'; p++) |
| 255 |
h = (h << 5) + h + g_ascii_tolower (*p); |
| 256 |
return h; |
| 257 |
} |
| 258 |
|
| 259 |
static gboolean |
| 260 |
str_case_equal (gconstpointer v1, |
| 261 |
gconstpointer v2) |
| 262 |
{ |
| 263 |
/* A case agnostic version of g_str_equal */ |
| 264 |
return g_ascii_strcasecmp (v1, v2) == 0; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* web_socket_util_new_headers: |
| 269 |
* |
| 270 |
* Create a new hashtable for HTTP headers. |
| 271 |
* |
| 272 |
* The GHashTable contains allocated null-terminated strings, as would |
| 273 |
* be returned by g_strdup(). The headers are indexed by the header names |
| 274 |
* in a case insensitive way. |
| 275 |
* |
| 276 |
* It is not necessary to worry about case headers in this GHashTable. |
| 277 |
* |
| 278 |
* Return value: (transfer full): a new header hashtable |
| 279 |
*/ |
| 280 |
GHashTable * |
| 281 |
web_socket_util_new_headers (void) |
| 282 |
{ |
| 283 |
return g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* web_socket_util_parse_headers: |
| 288 |
* @data: (array length=length): the input data |
| 289 |
* @length: length of data |
| 290 |
* @headers: (out): location to place HTTP header hash table |
| 291 |
* |
| 292 |
* Parse HTTP headers. |
| 293 |
* |
| 294 |
* The number of bytes parsed will be returned if parsing succeeds, including |
| 295 |
* the new line at the end of the request line. A negative value will be |
| 296 |
* returned if parsing fails. |
| 297 |
* |
| 298 |
* If the HTTP request line was truncated (ie: not all of it was present |
| 299 |
* within @length) then zero will be returned. |
| 300 |
* |
| 301 |
* The @headers returned will be allocated using web_socket_util_new_headers(), |
| 302 |
* and should be freed by the caller using g_free(). |
| 303 |
* |
| 304 |
* Return value: zero if truncated, negative if fails, or number of |
| 305 |
* characters parsed |
| 306 |
*/ |
| 307 |
gssize |
| 308 |
web_socket_util_parse_headers (const gchar *data, |
| 309 |
gsize length, |
| 310 |
GHashTable **headers) |
| 311 |
{ |
| 312 |
GHashTable *parsed_headers; |
| 313 |
const gchar *line; |
| 314 |
const gchar *colon; |
| 315 |
gsize consumed = 0; |
| 316 |
gboolean end = FALSE; |
| 317 |
gsize line_len; |
| 318 |
|
| 319 |
parsed_headers = web_socket_util_new_headers (); |
| 320 |
|
| 321 |
while (!end) |
| 322 |
{ |
| 323 |
line = memchr (data, '\n', length); |
| 324 |
|
| 325 |
/* No line ending: need more data */ |
| 326 |
if (line == NULL) |
| 327 |
{ |
| 328 |
consumed = 0; |
| 329 |
break; |
| 330 |
} |
| 331 |
|
| 332 |
line++; |
| 333 |
line_len = (line - data); |
| 334 |
|
| 335 |
/* An empty line, all done */ |
| 336 |
if ((data[0] == '\r' && data[1] == '\n') || data[0] == '\n') |
| 337 |
{ |
| 338 |
end = TRUE; |
| 339 |
} |
| 340 |
|
| 341 |
/* A header line */ |
| 342 |
else |
| 343 |
{ |
| 344 |
colon = memchr (data, ':', length); |
| 345 |
if (!colon || colon >= line) |
| 346 |
{ |
| 347 |
g_debug ("received invalid header line: %.*s", (gint)line_len, data); |
| 348 |
consumed = -1; |
| 349 |
break; |
| 350 |
} |
| 351 |
|
| 352 |
g_autofree gchar *name = g_strndup (data, colon - data); |
| 353 |
g_strstrip (name); |
| 354 |
g_autofree gchar *value = g_strndup (colon + 1, line - (colon + 1)); |
| 355 |
g_strstrip (value); |
| 356 |
|
| 357 |
if (!is_valid_line (name, -1) || !g_utf8_validate (value, -1, NULL)) |
| 358 |
{ |
| 359 |
g_debug ("received invalid header"); |
| 360 |
consumed = -1; |
| 361 |
break; |
| 362 |
} |
| 363 |
g_hash_table_insert (parsed_headers, g_steal_pointer (&name), g_steal_pointer (&value)); |
| 364 |
} |
| 365 |
|
| 366 |
consumed += line_len; |
| 367 |
data += line_len; |
| 368 |
length -= line_len; |
| 369 |
} |
| 370 |
|
| 371 |
if (consumed > 0) |
| 372 |
{ |
| 373 |
if (headers) |
| 374 |
*headers = g_hash_table_ref (parsed_headers); |
| 375 |
} |
| 376 |
|
| 377 |
g_hash_table_unref (parsed_headers); |
| 378 |
|
| 379 |
return consumed; |
| 380 |
} |
| 381 |
|
| 382 |
gboolean |
| 383 |
_web_socket_util_header_equals (GHashTable *headers, |
| 384 |
const gchar *name, |
| 385 |
const gchar *want) |
| 386 |
{ |
| 387 |
const gchar *value; |
| 388 |
|
| 389 |
value = g_hash_table_lookup (headers, name); |
| 390 |
if (value != NULL && g_ascii_strcasecmp (value, want) == 0) |
| 391 |
return TRUE; |
| 392 |
|
| 393 |
g_message ("received invalid or missing %s header: %s", name, value); |
| 394 |
return FALSE; |
| 395 |
} |
| 396 |
|
| 397 |
gboolean |
| 398 |
_web_socket_util_header_contains (GHashTable *headers, |
| 399 |
const gchar *name, |
| 400 |
const gchar *word) |
| 401 |
{ |
| 402 |
const gchar *value; |
| 403 |
const gchar *at; |
| 404 |
|
| 405 |
value = g_hash_table_lookup (headers, name); |
| 406 |
if (value != NULL) |
| 407 |
{ |
| 408 |
/* The word must be present, and not part of another word */ |
| 409 |
at = strcasestr (value, word); |
| 410 |
if (at != NULL && |
| 411 |
(at == value || !g_ascii_isalnum (*(at - 1))) && |
| 412 |
!g_ascii_isalnum (at[strlen (word)])) |
| 413 |
return TRUE; |
| 414 |
} |
| 415 |
|
| 416 |
g_message ("received invalid or missing %s header: %s", name, value); |
| 417 |
return FALSE; |
| 418 |
} |
| 419 |
|
| 420 |
gboolean |
| 421 |
_web_socket_util_header_empty (GHashTable *headers, |
| 422 |
const gchar *name) |
| 423 |
{ |
| 424 |
const gchar *value; |
| 425 |
|
| 426 |
value = g_hash_table_lookup (headers, name); |
| 427 |
if (value == NULL || value[0] == '\0') |
| 428 |
return TRUE; |
| 429 |
|
| 430 |
g_message ("received unsupported %s header: %s", name, value); |
| 431 |
return FALSE; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* web_socket_util_parse_status_line: |
| 436 |
* @data: (array length=length): the input data |
| 437 |
* @length: length of data |
| 438 |
* @version: (out): location to place HTTP version, or %NULL |
| 439 |
* @status: (out): location to place HTTP status, or %NULL |
| 440 |
* @reason: (out): location to place HTTP message, or %NULL |
| 441 |
* |
| 442 |
* Parse an HTTP status line. |
| 443 |
* |
| 444 |
* The number of bytes parsed will be returned if parsing succeeds, including |
| 445 |
* the new line at the end of the status line. A negative value will be |
| 446 |
* returned if parsing fails. |
| 447 |
* |
| 448 |
* If the HTTP request line was truncated (ie: not all of it was present |
| 449 |
* within @length) then zero will be returned. |
| 450 |
* |
| 451 |
* @reason should point to a string pointer. The value |
| 452 |
* returned should be freed by the caller using g_free(). |
| 453 |
* |
| 454 |
* Return value: zero if truncated, negative if fails, or number of |
| 455 |
* characters parsed |
| 456 |
*/ |
| 457 |
gssize |
| 458 |
web_socket_util_parse_status_line (const gchar *data, |
| 459 |
gsize length, |
| 460 |
gchar **version, |
| 461 |
guint *status, |
| 462 |
gchar **reason) |
| 463 |
{ |
| 464 |
const gchar *at; |
| 465 |
const gchar *end; |
| 466 |
gsize n; |
| 467 |
guint64 num; |
| 468 |
gchar *ep; |
| 469 |
|
| 470 |
/* |
| 471 |
* Here we parse a line like: |
| 472 |
* |
| 473 |
* HTTP/1.1 101 Switching protocols |
| 474 |
*/ |
| 475 |
|
| 476 |
at = data; |
| 477 |
end = memchr (at, '\n', length); |
| 478 |
if (end == NULL) |
| 479 |
return 0; /* need more data */ |
| 480 |
|
| 481 |
n = parse_version (at, (end - at), version); |
| 482 |
if (n == 0 || at[n] != ' ') |
| 483 |
return -1; |
| 484 |
at += n; |
| 485 |
|
| 486 |
/* Extra spaces */ |
| 487 |
at = strskip (at, ' ', end); |
| 488 |
|
| 489 |
/* First check for space after status */ |
| 490 |
if (memchr (at, ' ', (end - at)) == NULL) |
| 491 |
return -1; |
| 492 |
|
| 493 |
/* This will stop at above space */ |
| 494 |
num = g_ascii_strtoull (at, &ep, 10); |
| 495 |
if (num == 0 || num > G_MAXUINT || *ep != ' ') |
| 496 |
return -1; |
| 497 |
|
| 498 |
at = strskip (ep, ' ', end); |
| 499 |
|
| 500 |
if (reason) |
| 501 |
{ |
| 502 |
*reason = g_strndup (at, (end - at)); |
| 503 |
g_strstrip (*reason); |
| 504 |
} |
| 505 |
if (status) |
| 506 |
*status = (guint)num; |
| 507 |
return (end - data) + 1; |
| 508 |
} |