| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
/* This gets logged as part of the (more verbose) protocol logging */ |
| 9 |
#ifdef G_LOG_DOMAIN |
| 10 |
#undef G_LOG_DOMAIN |
| 11 |
#endif |
| 12 |
#define G_LOG_DOMAIN "cockpit-protocol" |
| 13 |
|
| 14 |
#include "cockpitwebresponse.h" |
| 15 |
#include "cockpitwebfilter.h" |
| 16 |
|
| 17 |
#include "common/cockpitconf.h" |
| 18 |
#include "cockpiterror.h" |
| 19 |
#include "cockpitflow.h" |
| 20 |
#include "cockpitlocale.h" |
| 21 |
#include "cockpittemplate.h" |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
/** |
| 28 |
* CockpitWebResponse: |
| 29 |
* |
| 30 |
* A response sent back to an HTTP client. You can use the high level one |
| 31 |
* shot APIs, like cockpit_web_response_content() and |
| 32 |
* cockpit_web_response_error() * or low level builder APIs: |
| 33 |
* |
| 34 |
* cockpit_web_response_headers() send the headers |
| 35 |
* cockpit_web_response_queue() send a block of data. |
| 36 |
* cockpit_web_response_complete() finish. |
| 37 |
*/ |
| 38 |
|
| 39 |
struct _CockpitWebResponse { |
| 40 |
GObject parent; |
| 41 |
GIOStream *io; |
| 42 |
const gchar *logname; |
| 43 |
const gchar *path; |
| 44 |
gchar *full_path; |
| 45 |
gchar *url_root; |
| 46 |
gchar *method; |
| 47 |
gchar *origin; |
| 48 |
|
| 49 |
gchar *protocol; |
| 50 |
CockpitCacheType cache_type; |
| 51 |
|
| 52 |
/* The output queue */ |
| 53 |
GPollableOutputStream *out; |
| 54 |
GQueue *queue; |
| 55 |
gsize out_queued; |
| 56 |
gsize out_queueable; |
| 57 |
gsize partial_offset; |
| 58 |
GSource *source; |
| 59 |
|
| 60 |
/* Status flags */ |
| 61 |
guint count; |
| 62 |
gboolean complete; |
| 63 |
gboolean failed; |
| 64 |
gboolean done; |
| 65 |
gboolean chunked; |
| 66 |
gboolean keep_alive; |
| 67 |
|
| 68 |
GList *filters; |
| 69 |
}; |
| 70 |
|
| 71 |
/* A megabyte is when we start to consider queue full enough */ |
| 72 |
#define QUEUE_PRESSURE 1024UL * 1024UL |
| 73 |
|
| 74 |
static guint signal__done; |
| 75 |
|
| 76 |
static void cockpit_web_response_flow_iface_init (CockpitFlowInterface *iface); |
| 77 |
|
| 78 |
G_DEFINE_TYPE_WITH_CODE (CockpitWebResponse, cockpit_web_response, G_TYPE_OBJECT, |
| 79 |
G_IMPLEMENT_INTERFACE (COCKPIT_TYPE_FLOW, cockpit_web_response_flow_iface_init)); |
| 80 |
|
| 81 |
static void |
| 82 |
cockpit_web_response_init (CockpitWebResponse *self) |
| 83 |
{ |
| 84 |
self->queue = g_queue_new (); |
| 85 |
self->out_queueable = G_MAXSIZE; |
| 86 |
self->cache_type = COCKPIT_WEB_RESPONSE_CACHE_UNSET; |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
cockpit_web_response_done (CockpitWebResponse *self) |
| 91 |
{ |
| 92 |
gboolean reusable = FALSE; |
| 93 |
|
| 94 |
g_object_ref (self); |
| 95 |
|
| 96 |
g_assert (!self->done); |
| 97 |
self->done = TRUE; |
| 98 |
|
| 99 |
if (self->source) |
| 100 |
{ |
| 101 |
g_source_destroy (self->source); |
| 102 |
g_source_unref (self->source); |
| 103 |
self->source = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
if (self->complete) |
| 107 |
{ |
| 108 |
reusable = !self->failed && self->keep_alive; |
| 109 |
g_object_unref (self); |
| 110 |
} |
| 111 |
else if (!self->failed) |
| 112 |
{ |
| 113 |
g_critical ("A CockpitWebResponse was freed without being completed properly. " |
| 114 |
"This is a programming error."); |
| 115 |
} |
| 116 |
|
| 117 |
g_signal_emit (self, signal__done, 0, reusable); |
| 118 |
|
| 119 |
g_object_unref (self->io); |
| 120 |
self->io = NULL; |
| 121 |
self->out = NULL; |
| 122 |
|
| 123 |
g_object_unref (self); |
| 124 |
} |
| 125 |
|
| 126 |
static void |
| 127 |
cockpit_web_response_dispose (GObject *object) |
| 128 |
{ |
| 129 |
CockpitWebResponse *self = COCKPIT_WEB_RESPONSE (object); |
| 130 |
|
| 131 |
if (!self->done) |
| 132 |
cockpit_web_response_done (self); |
| 133 |
g_list_free_full (self->filters, g_object_unref); |
| 134 |
self->filters = NULL; |
| 135 |
|
| 136 |
G_OBJECT_CLASS (cockpit_web_response_parent_class)->dispose (object); |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
cockpit_web_response_finalize (GObject *object) |
| 141 |
{ |
| 142 |
CockpitWebResponse *self = COCKPIT_WEB_RESPONSE (object); |
| 143 |
|
| 144 |
g_free (self->protocol); |
| 145 |
g_free (self->full_path); |
| 146 |
g_free (self->url_root); |
| 147 |
g_free (self->method); |
| 148 |
g_free (self->origin); |
| 149 |
g_assert (self->io == NULL); |
| 150 |
g_assert (self->out == NULL); |
| 151 |
g_queue_free_full (self->queue, (GDestroyNotify)g_bytes_unref); |
| 152 |
self->out_queued = 0; |
| 153 |
|
| 154 |
G_OBJECT_CLASS (cockpit_web_response_parent_class)->finalize (object); |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
cockpit_web_response_class_init (CockpitWebResponseClass *klass) |
| 159 |
{ |
| 160 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 161 |
|
| 162 |
gobject_class->dispose = cockpit_web_response_dispose; |
| 163 |
gobject_class->finalize = cockpit_web_response_finalize; |
| 164 |
|
| 165 |
signal__done = g_signal_new ("done", COCKPIT_TYPE_WEB_RESPONSE, |
| 166 |
G_SIGNAL_RUN_LAST, |
| 167 |
0, NULL, NULL, NULL, |
| 168 |
G_TYPE_NONE, 1, G_TYPE_BOOLEAN); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* cockpit_web_response_new: |
| 173 |
* @io: the stream to send on |
| 174 |
* @path: the path resource or NULL |
| 175 |
* @in_headers: input headers or NULL |
| 176 |
* @flags: in #COCKPIT_WEB_RESPONSE_FOR_TLS_PROXY mode, the origin is assumed to |
| 177 |
* be https://<host> even for a non-HTTPS connection. |
| 178 |
* |
| 179 |
* Create a new web response. |
| 180 |
* |
| 181 |
* The returned reference belongs to the caller. Additionally |
| 182 |
* once cockpit_web_response_complete() is called, an additional |
| 183 |
* reference is held until the response is sent and flushed. |
| 184 |
* |
| 185 |
* Returns: (transfer full): the new response, unref when done with it |
| 186 |
*/ |
| 187 |
CockpitWebResponse * |
| 188 |
cockpit_web_response_new (GIOStream *io, |
| 189 |
const gchar *original_path, |
| 190 |
const gchar *path, |
| 191 |
GHashTable *in_headers, |
| 192 |
const gchar *method, |
| 193 |
const gchar *protocol) |
| 194 |
{ |
| 195 |
CockpitWebResponse *self; |
| 196 |
GOutputStream *out; |
| 197 |
const gchar *connection; |
| 198 |
const gchar *host = NULL; |
| 199 |
gint offset; |
| 200 |
|
| 201 |
/* Trying to be a somewhat performant here, avoiding properties */ |
| 202 |
self = g_object_new (COCKPIT_TYPE_WEB_RESPONSE, NULL); |
| 203 |
self->io = g_object_ref (io); |
| 204 |
|
| 205 |
out = g_io_stream_get_output_stream (io); |
| 206 |
if (G_IS_POLLABLE_OUTPUT_STREAM (out)) |
| 207 |
{ |
| 208 |
self->out = (GPollableOutputStream *)out; |
| 209 |
} |
| 210 |
else if (out) |
| 211 |
{ |
| 212 |
g_critical ("Cannot send web response over non-pollable output stream: %s", |
| 213 |
G_OBJECT_TYPE_NAME (out)); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
g_critical ("Cannot send web response: no output stream available"); |
| 218 |
} |
| 219 |
|
| 220 |
self->url_root = NULL; |
| 221 |
self->full_path = g_strdup (path); |
| 222 |
self->path = self->full_path; |
| 223 |
|
| 224 |
g_assert (method != NULL); |
| 225 |
self->method = g_strdup (method); |
| 226 |
|
| 227 |
if (path && original_path) |
| 228 |
{ |
| 229 |
offset = strlen (original_path) - strlen (path); |
| 230 |
if (offset > 0 && g_strcmp0 (original_path + offset, path) == 0) |
| 231 |
self->url_root = g_strndup (original_path, offset); |
| 232 |
} |
| 233 |
|
| 234 |
if (self->path) |
| 235 |
self->logname = self->path; |
| 236 |
else |
| 237 |
self->logname = "response"; |
| 238 |
|
| 239 |
self->keep_alive = TRUE; |
| 240 |
if (in_headers) |
| 241 |
{ |
| 242 |
connection = g_hash_table_lookup (in_headers, "Connection"); |
| 243 |
if (connection) |
| 244 |
self->keep_alive = g_str_equal (connection, "keep-alive"); |
| 245 |
host = g_hash_table_lookup (in_headers, "Host"); |
| 246 |
} |
| 247 |
|
| 248 |
self->protocol = g_strdup (protocol ?: "http"); |
| 249 |
if (host) |
| 250 |
self->origin = g_strdup_printf ("%s://%s", self->protocol, host); |
| 251 |
|
| 252 |
return self; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* cockpit_web_response_get_path: |
| 257 |
* @self: the response |
| 258 |
* |
| 259 |
* Returns: the resource path for response |
| 260 |
*/ |
| 261 |
const gchar * |
| 262 |
cockpit_web_response_get_path (CockpitWebResponse *self) |
| 263 |
{ |
| 264 |
g_return_val_if_fail (COCKPIT_IS_WEB_RESPONSE (self), NULL); |
| 265 |
return self->path; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* cockpit_web_response_get_url_root: |
| 270 |
* @self: the response |
| 271 |
* |
| 272 |
* Returns: The url root portion of the original path that was removed |
| 273 |
*/ |
| 274 |
const gchar * |
| 275 |
cockpit_web_response_get_url_root (CockpitWebResponse *self) { |
| 276 |
return self->url_root; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* cockpit_web_response_get_stream: |
| 281 |
* @self: the response |
| 282 |
* |
| 283 |
* Returns: the stream we're sending on |
| 284 |
*/ |
| 285 |
GIOStream * |
| 286 |
cockpit_web_response_get_stream (CockpitWebResponse *self) |
| 287 |
{ |
| 288 |
g_return_val_if_fail (COCKPIT_IS_WEB_RESPONSE (self), NULL); |
| 289 |
return self->io; |
| 290 |
} |
| 291 |
|
| 292 |
#if !GLIB_CHECK_VERSION(2,43,2) |
| 293 |
#define G_IO_ERROR_CONNECTION_CLOSED G_IO_ERROR_BROKEN_PIPE |
| 294 |
#endif |
| 295 |
|
| 296 |
gboolean |
| 297 |
cockpit_web_should_suppress_output_error (const gchar *logname, |
| 298 |
GError *error) |
| 299 |
{ |
| 300 |
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED) || |
| 301 |
g_error_matches (error, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE)) |
| 302 |
{ |
| 303 |
g_debug ("%s: output error: %s", logname, error->message); |
| 304 |
return TRUE; |
| 305 |
} |
| 306 |
|
| 307 |
#if !GLIB_CHECK_VERSION(2,43,2) |
| 308 |
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED) && |
| 309 |
strstr (error->message, g_strerror (ECONNRESET))) |
| 310 |
{ |
| 311 |
g_debug ("%s: output error: %s", logname, error->message); |
| 312 |
return TRUE; |
| 313 |
} |
| 314 |
#endif |
| 315 |
|
| 316 |
return FALSE; |
| 317 |
} |
| 318 |
|
| 319 |
static void |
| 320 |
on_output_flushed (GObject *stream, |
| 321 |
GAsyncResult *result, |
| 322 |
gpointer user_data) |
| 323 |
{ |
| 324 |
CockpitWebResponse *self = COCKPIT_WEB_RESPONSE (user_data); |
| 325 |
GOutputStream *output = G_OUTPUT_STREAM (stream); |
| 326 |
GError *error = NULL; |
| 327 |
|
| 328 |
if (g_output_stream_flush_finish (output, result, &error)) |
| 329 |
{ |
| 330 |
g_debug ("%s: flushed output", self->logname); |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
if (!cockpit_web_should_suppress_output_error (self->logname, error)) |
| 335 |
g_message ("%s: couldn't flush web output: %s", self->logname, error->message); |
| 336 |
self->failed = TRUE; |
| 337 |
g_error_free (error); |
| 338 |
} |
| 339 |
|
| 340 |
cockpit_web_response_done (self); |
| 341 |
g_object_unref (self); |
| 342 |
} |
| 343 |
|
| 344 |
static gboolean |
| 345 |
on_response_output (GObject *pollable, |
| 346 |
gpointer user_data) |
| 347 |
{ |
| 348 |
CockpitWebResponse *self = user_data; |
| 349 |
GError *error = NULL; |
| 350 |
const guint8 *data; |
| 351 |
GBytes *block; |
| 352 |
gssize count; |
| 353 |
gsize before, size, len; |
| 354 |
|
| 355 |
block = g_queue_peek_head (self->queue); |
| 356 |
if (block) |
| 357 |
{ |
| 358 |
data = g_bytes_get_data (block, &len); |
| 359 |
g_assert (len == 0 || self->partial_offset < len); |
| 360 |
data += self->partial_offset; |
| 361 |
len -= self->partial_offset; |
| 362 |
|
| 363 |
before = self->out_queued; |
| 364 |
|
| 365 |
if (len > 0) |
| 366 |
{ |
| 367 |
count = g_pollable_output_stream_write_nonblocking (self->out, data, len, |
| 368 |
NULL, &error); |
| 369 |
} |
| 370 |
else |
| 371 |
{ |
| 372 |
count = 0; |
| 373 |
} |
| 374 |
|
| 375 |
if (count < 0) |
| 376 |
{ |
| 377 |
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) |
| 378 |
{ |
| 379 |
g_error_free (error); |
| 380 |
return TRUE; |
| 381 |
} |
| 382 |
|
| 383 |
if (!cockpit_web_should_suppress_output_error (self->logname, error)) |
| 384 |
g_message ("%s: couldn't write web output: %s", self->logname, error->message); |
| 385 |
|
| 386 |
self->failed = TRUE; |
| 387 |
cockpit_web_response_done (self); |
| 388 |
|
| 389 |
g_error_free (error); |
| 390 |
return FALSE; |
| 391 |
} |
| 392 |
|
| 393 |
if (count == len) |
| 394 |
{ |
| 395 |
g_debug ("%s: sent %d bytes", self->logname, (int)len); |
| 396 |
self->partial_offset = 0; |
| 397 |
block = g_queue_pop_head (self->queue); |
| 398 |
size = g_bytes_get_size (block); |
| 399 |
g_assert (size <= self->out_queued); |
| 400 |
self->out_queued -= size; |
| 401 |
g_bytes_unref (block); |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
g_debug ("%s: sent %d partial", self->logname, (int)count); |
| 406 |
g_assert (count < len); |
| 407 |
self->partial_offset += count; |
| 408 |
} |
| 409 |
|
| 410 |
/* |
| 411 |
* If we're controlling another flow, turn it on again when our output |
| 412 |
* buffer size becomes less than the low mark. |
| 413 |
*/ |
| 414 |
if (before >= QUEUE_PRESSURE && self->out_queued < QUEUE_PRESSURE) |
| 415 |
cockpit_flow_emit_pressure (COCKPIT_FLOW (self), FALSE); |
| 416 |
|
| 417 |
return TRUE; |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
g_source_destroy (self->source); |
| 422 |
g_source_unref (self->source); |
| 423 |
self->source = NULL; |
| 424 |
|
| 425 |
if (self->complete) |
| 426 |
{ |
| 427 |
g_debug ("%s: complete flushing output", self->logname); |
| 428 |
g_output_stream_flush_async (G_OUTPUT_STREAM (self->out), G_PRIORITY_DEFAULT, |
| 429 |
NULL, on_output_flushed, g_object_ref (self)); |
| 430 |
} |
| 431 |
|
| 432 |
return FALSE; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
static void |
| 437 |
queue_bytes (CockpitWebResponse *self, |
| 438 |
GBytes *block) |
| 439 |
{ |
| 440 |
gsize size, before; |
| 441 |
|
| 442 |
size = g_bytes_get_size (block); |
| 443 |
before = self->out_queued; |
| 444 |
g_return_if_fail (G_MAXSIZE - size > self->out_queued); |
| 445 |
self->out_queued += size; |
| 446 |
|
| 447 |
g_queue_push_tail (self->queue, g_bytes_ref (block)); |
| 448 |
|
| 449 |
self->count++; |
| 450 |
|
| 451 |
if (!self->source) |
| 452 |
{ |
| 453 |
self->source = g_pollable_output_stream_create_source (self->out, NULL); |
| 454 |
g_source_set_callback (self->source, (GSourceFunc)on_response_output, self, NULL); |
| 455 |
g_source_attach (self->source, NULL); |
| 456 |
} |
| 457 |
|
| 458 |
if (before < QUEUE_PRESSURE && self->out_queued >= QUEUE_PRESSURE) |
| 459 |
cockpit_flow_emit_pressure (COCKPIT_FLOW (self), TRUE); |
| 460 |
} |
| 461 |
|
| 462 |
static void |
| 463 |
queue_block (CockpitWebResponse *self, |
| 464 |
GBytes *block) |
| 465 |
{ |
| 466 |
gsize length = g_bytes_get_size (block); |
| 467 |
GBytes *bytes; |
| 468 |
gchar *data; |
| 469 |
|
| 470 |
/* |
| 471 |
* We cannot queue chunks of length zero. Besides being silly, this |
| 472 |
* messes with chunked encoding. The 0 length block means end of |
| 473 |
* response. |
| 474 |
*/ |
| 475 |
if (length == 0) |
| 476 |
return; |
| 477 |
|
| 478 |
if (self->out_queueable < length) |
| 479 |
{ |
| 480 |
g_critical ("Too much data queuing in HTTP response. This is a programmer error."); |
| 481 |
return; |
| 482 |
} |
| 483 |
|
| 484 |
self->out_queueable -= length; |
| 485 |
g_debug ("%s: queued %d bytes", self->logname, (int)length); |
| 486 |
|
| 487 |
if (!self->chunked) |
| 488 |
{ |
| 489 |
queue_bytes (self, block); |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
/* Required for chunked transfer encoding. */ |
| 494 |
data = g_strdup_printf ("%x\r\n", (unsigned int)length); |
| 495 |
bytes = g_bytes_new_take (data, strlen (data)); |
| 496 |
queue_bytes (self, bytes); |
| 497 |
g_bytes_unref (bytes); |
| 498 |
|
| 499 |
queue_bytes (self, block); |
| 500 |
|
| 501 |
bytes = g_bytes_new_static ("\r\n", 2); |
| 502 |
queue_bytes (self, bytes); |
| 503 |
g_bytes_unref (bytes); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
typedef struct { |
| 508 |
CockpitWebResponse *response; |
| 509 |
GList *filters; |
| 510 |
} QueueStep; |
| 511 |
|
| 512 |
static void |
| 513 |
queue_filter (gpointer data, |
| 514 |
GBytes *bytes) |
| 515 |
{ |
| 516 |
QueueStep *qs = data; |
| 517 |
QueueStep qn = { .response = qs->response }; |
| 518 |
|
| 519 |
g_return_if_fail (bytes != NULL); |
| 520 |
|
| 521 |
if (qs->filters) |
| 522 |
{ |
| 523 |
qn.filters = qs->filters->next; |
| 524 |
cockpit_web_filter_push (qs->filters->data, bytes, queue_filter, &qn); |
| 525 |
} |
| 526 |
else |
| 527 |
{ |
| 528 |
queue_block (qs->response, bytes); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* cockpit_web_response_queue: |
| 534 |
* @self: the response |
| 535 |
* @block: the block of data to queue |
| 536 |
* |
| 537 |
* Queue a single block of data on the response. Will be sent |
| 538 |
* during the main loop. |
| 539 |
* |
| 540 |
* See cockpit_web_response_content() for a simple way to |
| 541 |
* avoid queueing individual blocks. |
| 542 |
* |
| 543 |
* If this function returns %FALSE, then the response has failed |
| 544 |
* or has been completed elsewhere. The block was ignored and |
| 545 |
* queuing more blocks doesn't makes sense. |
| 546 |
* |
| 547 |
* After done queuing all your blocks call |
| 548 |
* cockpit_web_response_complete(). |
| 549 |
* |
| 550 |
* Returns: Whether queuing more blocks makes sense |
| 551 |
*/ |
| 552 |
gboolean |
| 553 |
cockpit_web_response_queue (CockpitWebResponse *self, |
| 554 |
GBytes *block) |
| 555 |
{ |
| 556 |
QueueStep qn = { .response = self }; |
| 557 |
|
| 558 |
g_return_val_if_fail (COCKPIT_IS_WEB_RESPONSE (self), FALSE); |
| 559 |
g_return_val_if_fail (block != NULL, FALSE); |
| 560 |
g_return_val_if_fail (self->complete == FALSE, FALSE); |
| 561 |
|
| 562 |
if (self->failed) |
| 563 |
{ |
| 564 |
g_debug ("%s: ignoring queued block after failure", self->logname); |
| 565 |
return FALSE; |
| 566 |
} |
| 567 |
|
| 568 |
if (g_str_equal (self->method, "HEAD")) |
| 569 |
{ |
| 570 |
g_debug ("%s: ignoring queued block for method HEAD", self->logname); |
| 571 |
return TRUE; |
| 572 |
} |
| 573 |
|
| 574 |
qn.filters = self->filters; |
| 575 |
queue_filter (&qn, block); |
| 576 |
return TRUE; |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* cockpit_web_response_complete: |
| 581 |
* @self: the response |
| 582 |
* |
| 583 |
* See cockpit_web_response_content() for easy to use stuff. |
| 584 |
* |
| 585 |
* Tell the response that all the data has been queued. |
| 586 |
* The response will hold a reference to itself until the |
| 587 |
* data is actually sent, so you can unref it. |
| 588 |
*/ |
| 589 |
void |
| 590 |
cockpit_web_response_complete (CockpitWebResponse *self) |
| 591 |
{ |
| 592 |
GBytes *bytes; |
| 593 |
|
| 594 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 595 |
g_return_if_fail (self->complete == FALSE); |
| 596 |
|
| 597 |
if (self->failed) |
| 598 |
return; |
| 599 |
|
| 600 |
/* Hold a reference until cockpit_web_response_done() */ |
| 601 |
g_object_ref (self); |
| 602 |
self->complete = TRUE; |
| 603 |
|
| 604 |
if (self->chunked && !g_str_equal(self->method, "HEAD")) |
| 605 |
{ |
| 606 |
bytes = g_bytes_new_static ("0\r\n\r\n", 5); |
| 607 |
queue_bytes (self, bytes); |
| 608 |
g_bytes_unref (bytes); |
| 609 |
} |
| 610 |
|
| 611 |
if (self->source) |
| 612 |
{ |
| 613 |
g_debug ("%s: queueing complete", self->logname); |
| 614 |
} |
| 615 |
else |
| 616 |
{ |
| 617 |
g_debug ("%s: complete closing io", self->logname); |
| 618 |
g_output_stream_flush_async (G_OUTPUT_STREAM (self->out), G_PRIORITY_DEFAULT, |
| 619 |
NULL, on_output_flushed, g_object_ref (self)); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* cockpit_web_response_abort: |
| 625 |
* @self: the response |
| 626 |
* |
| 627 |
* This function is used when streaming content, and at |
| 628 |
* some point we can't provide the remainder of the content |
| 629 |
* |
| 630 |
* This completes the response and terminates the connection. |
| 631 |
*/ |
| 632 |
void |
| 633 |
cockpit_web_response_abort (CockpitWebResponse *self) |
| 634 |
{ |
| 635 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 636 |
g_return_if_fail (self->complete == FALSE); |
| 637 |
|
| 638 |
if (self->failed) |
| 639 |
return; |
| 640 |
|
| 641 |
/* Hold a reference until cockpit_web_response_done() */ |
| 642 |
g_object_ref (self); |
| 643 |
|
| 644 |
self->complete = TRUE; |
| 645 |
self->failed = TRUE; |
| 646 |
|
| 647 |
g_debug ("%s: aborted", self->logname); |
| 648 |
cockpit_web_response_done (self); |
| 649 |
} |
| 650 |
|
| 651 |
/** |
| 652 |
* CockpitWebResponding: |
| 653 |
* @COCKPIT_WEB_RESPONSE_READY: nothing queued or sent yet |
| 654 |
* @COCKPIT_WEB_RESPONSE_QUEUING: started and still queuing data on response |
| 655 |
* @COCKPIT_WEB_RESPONSE_COMPLETE: all data is queued or aborted |
| 656 |
* @COCKPIT_WEB_RESPONSE_SENT: data is completely sent |
| 657 |
* |
| 658 |
* Various states of the web response. |
| 659 |
*/ |
| 660 |
|
| 661 |
/** |
| 662 |
* cockpit_web_response_get_state: |
| 663 |
* @self: the web response |
| 664 |
* |
| 665 |
* Return the state of the web response. |
| 666 |
*/ |
| 667 |
CockpitWebResponding |
| 668 |
cockpit_web_response_get_state (CockpitWebResponse *self) |
| 669 |
{ |
| 670 |
g_return_val_if_fail (COCKPIT_IS_WEB_RESPONSE (self), 0); |
| 671 |
|
| 672 |
if (self->done) |
| 673 |
return COCKPIT_WEB_RESPONSE_SENT; |
| 674 |
else if (self->complete) |
| 675 |
return COCKPIT_WEB_RESPONSE_COMPLETE; |
| 676 |
else if (self->count == 0) |
| 677 |
return COCKPIT_WEB_RESPONSE_READY; |
| 678 |
else |
| 679 |
return COCKPIT_WEB_RESPONSE_QUEUING; |
| 680 |
} |
| 681 |
|
| 682 |
static gboolean |
| 683 |
cockpit_web_response_is_simple_token (const gchar *string) |
| 684 |
{ |
| 685 |
string += strcspn (string, " \t\r\n\v"); |
| 686 |
return string[0] == '\0'; |
| 687 |
} |
| 688 |
|
| 689 |
gboolean |
| 690 |
cockpit_web_response_is_header_value (const gchar *string) |
| 691 |
{ |
| 692 |
string += strcspn (string, "\r\n\v"); |
| 693 |
return string[0] == '\0'; |
| 694 |
} |
| 695 |
|
| 696 |
enum { |
| 697 |
HEADER_CONTENT_TYPE = 1 << 0, |
| 698 |
HEADER_CONTENT_ENCODING = 1 << 1, |
| 699 |
HEADER_VARY = 1 << 2, |
| 700 |
HEADER_CACHE_CONTROL = 1 << 3, |
| 701 |
HEADER_DNS_PREFETCH_CONTROL = 1 << 4, |
| 702 |
HEADER_REFERRER_POLICY = 1 << 5, |
| 703 |
HEADER_CONTENT_TYPE_OPTIONS = 1 << 6, |
| 704 |
HEADER_CROSS_ORIGIN_RESOURCE_POLICY = 1 << 7, |
| 705 |
HEADER_X_FRAME_OPTIONS = 1 << 8, |
| 706 |
}; |
| 707 |
|
| 708 |
static GString * |
| 709 |
begin_headers (CockpitWebResponse *response, |
| 710 |
guint status, |
| 711 |
const gchar *reason) |
| 712 |
{ |
| 713 |
GString *string; |
| 714 |
|
| 715 |
string = g_string_sized_new (1024); |
| 716 |
g_string_printf (string, "HTTP/1.1 %d %s\r\n", status, reason); |
| 717 |
|
| 718 |
return string; |
| 719 |
} |
| 720 |
|
| 721 |
static guint |
| 722 |
append_header (GString *string, |
| 723 |
const gchar *name, |
| 724 |
const gchar *value) |
| 725 |
{ |
| 726 |
if (value) |
| 727 |
{ |
| 728 |
g_return_val_if_fail (cockpit_web_response_is_simple_token (name), 0); |
| 729 |
g_return_val_if_fail (cockpit_web_response_is_header_value (value), 0); |
| 730 |
g_string_append_printf (string, "%s: %s\r\n", name, value); |
| 731 |
} |
| 732 |
if (g_ascii_strcasecmp ("Content-Type", name) == 0) |
| 733 |
return HEADER_CONTENT_TYPE; |
| 734 |
if (g_ascii_strcasecmp ("Cache-Control", name) == 0) |
| 735 |
return HEADER_CACHE_CONTROL; |
| 736 |
if (g_ascii_strcasecmp ("Vary", name) == 0) |
| 737 |
return HEADER_VARY; |
| 738 |
if (g_ascii_strcasecmp ("Content-Encoding", name) == 0) |
| 739 |
return HEADER_CONTENT_ENCODING; |
| 740 |
if (g_ascii_strcasecmp ("X-DNS-Prefetch-Control", name) == 0) |
| 741 |
return HEADER_DNS_PREFETCH_CONTROL; |
| 742 |
if (g_ascii_strcasecmp ("Referrer-Policy", name) == 0) |
| 743 |
return HEADER_REFERRER_POLICY; |
| 744 |
if (g_ascii_strcasecmp ("X-Content-Type-Options", name) == 0) |
| 745 |
return HEADER_CONTENT_TYPE_OPTIONS; |
| 746 |
if (g_ascii_strcasecmp ("Cross-Origin-Resource-Policy", name) == 0) |
| 747 |
return HEADER_CROSS_ORIGIN_RESOURCE_POLICY; |
| 748 |
if (g_ascii_strcasecmp ("X-Frame-Options", name) == 0) |
| 749 |
return HEADER_X_FRAME_OPTIONS; |
| 750 |
if (g_ascii_strcasecmp ("Content-Length", name) == 0 || |
| 751 |
g_ascii_strcasecmp ("Transfer-Encoding", name) == 0 || |
| 752 |
g_ascii_strcasecmp ("Connection", name) == 0) |
| 753 |
{ |
| 754 |
g_critical ("Don't set %s header manually. This is a programmer error.", name); |
| 755 |
} |
| 756 |
return 0; |
| 757 |
} |
| 758 |
|
| 759 |
static guint |
| 760 |
append_table (GString *string, |
| 761 |
GHashTable *headers) |
| 762 |
{ |
| 763 |
GHashTableIter iter; |
| 764 |
gpointer key; |
| 765 |
gpointer value; |
| 766 |
guint seen = 0; |
| 767 |
|
| 768 |
if (headers) |
| 769 |
{ |
| 770 |
g_hash_table_iter_init (&iter, headers); |
| 771 |
while (g_hash_table_iter_next (&iter, &key, &value)) |
| 772 |
seen |= append_header (string, key, value); |
| 773 |
} |
| 774 |
|
| 775 |
return seen; |
| 776 |
} |
| 777 |
|
| 778 |
static guint |
| 779 |
append_va (GString *string, |
| 780 |
va_list va) |
| 781 |
{ |
| 782 |
const gchar *name; |
| 783 |
const gchar *value; |
| 784 |
guint seen = 0; |
| 785 |
|
| 786 |
for (;;) |
| 787 |
{ |
| 788 |
name = va_arg (va, const gchar *); |
| 789 |
if (!name) |
| 790 |
break; |
| 791 |
value = va_arg (va, const gchar *); |
| 792 |
seen |= append_header (string, name, value); |
| 793 |
} |
| 794 |
|
| 795 |
return seen; |
| 796 |
} |
| 797 |
|
| 798 |
static GBytes * |
| 799 |
finish_headers (CockpitWebResponse *self, |
| 800 |
GString *string, |
| 801 |
gssize length, |
| 802 |
gint status, |
| 803 |
guint seen) |
| 804 |
{ |
| 805 |
const gchar *content_type; |
| 806 |
|
| 807 |
/* Automatically figure out content type */ |
| 808 |
if ((seen & HEADER_CONTENT_TYPE) == 0 && |
| 809 |
self->full_path != NULL && status >= 200 && status <= 299) |
| 810 |
{ |
| 811 |
content_type = cockpit_web_response_content_type (self->full_path); |
| 812 |
if (content_type) |
| 813 |
g_string_append_printf (string, "Content-Type: %s\r\n", content_type); |
| 814 |
} |
| 815 |
|
| 816 |
if (status != 304) |
| 817 |
{ |
| 818 |
if (length < 0 || seen & HEADER_CONTENT_ENCODING || self->filters) |
| 819 |
{ |
| 820 |
self->chunked = TRUE; |
| 821 |
g_string_append_printf (string, "Transfer-Encoding: chunked\r\n"); |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
self->chunked = FALSE; |
| 826 |
g_string_append_printf (string, "Content-Length: %" G_GSSIZE_FORMAT "\r\n", length); |
| 827 |
self->out_queueable = length; |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
if ((seen & HEADER_CACHE_CONTROL) == 0 && status >= 200 && status <= 299) |
| 832 |
{ |
| 833 |
if (self->cache_type == COCKPIT_WEB_RESPONSE_NO_CACHE) |
| 834 |
g_string_append (string, "Cache-Control: no-cache, no-store\r\n"); |
| 835 |
else if (self->cache_type == COCKPIT_WEB_RESPONSE_CACHE) |
| 836 |
g_string_append (string, "Cache-Control: max-age=86400, private\r\n"); |
| 837 |
} |
| 838 |
|
| 839 |
if ((seen & HEADER_VARY) == 0 && status >= 200 && status <= 299 && |
| 840 |
self->cache_type == COCKPIT_WEB_RESPONSE_CACHE) |
| 841 |
{ |
| 842 |
g_string_append (string, "Vary: Cookie\r\n"); |
| 843 |
} |
| 844 |
|
| 845 |
if (!self->keep_alive) |
| 846 |
g_string_append (string, "Connection: close\r\n"); |
| 847 |
|
| 848 |
/* Some blanket security headers */ |
| 849 |
if ((seen & HEADER_DNS_PREFETCH_CONTROL) == 0) |
| 850 |
g_string_append (string, "X-DNS-Prefetch-Control: off\r\n"); |
| 851 |
if ((seen & HEADER_REFERRER_POLICY) == 0) |
| 852 |
g_string_append (string, "Referrer-Policy: no-referrer\r\n"); |
| 853 |
if ((seen & HEADER_CONTENT_TYPE_OPTIONS) == 0) |
| 854 |
g_string_append (string, "X-Content-Type-Options: nosniff\r\n"); |
| 855 |
/* Be very strict here -- there is no reason that external web sites should |
| 856 |
* be able to read any resource. This does *not* affect embedding with <iframe> */ |
| 857 |
if ((seen & HEADER_CROSS_ORIGIN_RESOURCE_POLICY) == 0) |
| 858 |
g_string_append (string, "Cross-Origin-Resource-Policy: same-origin\r\n"); |
| 859 |
/* This is the counterpart for iframe embedding, line of defence against clickjacking */ |
| 860 |
if ((seen & HEADER_X_FRAME_OPTIONS) == 0) |
| 861 |
g_string_append (string, "X-Frame-Options: sameorigin\r\n"); |
| 862 |
|
| 863 |
g_string_append (string, "\r\n"); |
| 864 |
return g_string_free_to_bytes (string); |
| 865 |
} |
| 866 |
|
| 867 |
/** |
| 868 |
* cockpit_web_response_set_cache_type: |
| 869 |
* @self: the response |
| 870 |
* @cache_type: Ensures the appropriate cache headers are returned for |
| 871 |
the given cache type. |
| 872 |
*/ |
| 873 |
void |
| 874 |
cockpit_web_response_set_cache_type (CockpitWebResponse *self, |
| 875 |
CockpitCacheType cache_type) |
| 876 |
{ |
| 877 |
self->cache_type = cache_type; |
| 878 |
} |
| 879 |
|
| 880 |
/** |
| 881 |
* cockpit_web_response_headers: |
| 882 |
* @self: the response |
| 883 |
* @status: the HTTP status code |
| 884 |
* @reason: the HTTP reason |
| 885 |
* @length: the combined length of data blocks to follow, or -1 |
| 886 |
* |
| 887 |
* See cockpit_web_response_content() for an easy to use function. |
| 888 |
* |
| 889 |
* Queue the headers of the response. No data blocks must yet be |
| 890 |
* queued on the response. |
| 891 |
* |
| 892 |
* Specify header name/value pairs in the var args, and end with |
| 893 |
* a NULL name. If value is NULL, then that header won't be sent. |
| 894 |
* |
| 895 |
* Don't specify Content-Length or Connection headers. |
| 896 |
* |
| 897 |
* If @length is zero or greater, then it must represent the |
| 898 |
* number of queued blocks to follow. |
| 899 |
*/ |
| 900 |
void |
| 901 |
cockpit_web_response_headers (CockpitWebResponse *self, |
| 902 |
guint status, |
| 903 |
const gchar *reason, |
| 904 |
gssize length, |
| 905 |
...) |
| 906 |
{ |
| 907 |
GString *string; |
| 908 |
GBytes *block; |
| 909 |
va_list va; |
| 910 |
|
| 911 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 912 |
|
| 913 |
if (self->count > 0) |
| 914 |
{ |
| 915 |
g_critical ("Headers should be sent first. This is a programmer error."); |
| 916 |
return; |
| 917 |
} |
| 918 |
|
| 919 |
string = begin_headers (self, status, reason); |
| 920 |
|
| 921 |
va_start (va, length); |
| 922 |
block = finish_headers (self, string, length, status, |
| 923 |
append_va (string, va)); |
| 924 |
va_end (va); |
| 925 |
|
| 926 |
queue_bytes (self, block); |
| 927 |
g_bytes_unref (block); |
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* cockpit_web_response_headers: |
| 932 |
* @self: the response |
| 933 |
* @status: the HTTP status code |
| 934 |
* @reason: the HTTP reason |
| 935 |
* @length: the combined length of data blocks to follow, or -1 |
| 936 |
* @headers: headers to include or NULL |
| 937 |
* |
| 938 |
* See cockpit_web_response_content() for an easy to use function. |
| 939 |
* |
| 940 |
* Queue the headers of the response. No data blocks must yet be |
| 941 |
* queued on the response. |
| 942 |
* |
| 943 |
* Don't put Content-Length or Connection in @headers. |
| 944 |
* |
| 945 |
* If @length is zero or greater, then it must represent the |
| 946 |
* number of queued blocks to follow. |
| 947 |
*/ |
| 948 |
void |
| 949 |
cockpit_web_response_headers_full (CockpitWebResponse *self, |
| 950 |
guint status, |
| 951 |
const gchar *reason, |
| 952 |
gssize length, |
| 953 |
GHashTable *headers) |
| 954 |
{ |
| 955 |
GString *string; |
| 956 |
GBytes *block; |
| 957 |
|
| 958 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 959 |
|
| 960 |
if (self->count > 0) |
| 961 |
{ |
| 962 |
g_critical ("Headers should be sent first. This is a programmer error."); |
| 963 |
return; |
| 964 |
} |
| 965 |
|
| 966 |
string = begin_headers (self, status, reason); |
| 967 |
|
| 968 |
block = finish_headers (self, string, length, status, |
| 969 |
append_table (string, headers)); |
| 970 |
|
| 971 |
queue_bytes (self, block); |
| 972 |
g_bytes_unref (block); |
| 973 |
} |
| 974 |
|
| 975 |
/** |
| 976 |
* cockpit_web_response_content: |
| 977 |
* @self: the response |
| 978 |
* @headers: headers to include or NULL |
| 979 |
* @block: first block to send |
| 980 |
* |
| 981 |
* This is a simple way to send an HTTP response as a single |
| 982 |
* call. The response will be complete after this call, and will |
| 983 |
* send in the main-loop. |
| 984 |
* |
| 985 |
* The var args are additional GBytes* blocks to send, followed by |
| 986 |
* a trailing NULL. |
| 987 |
* |
| 988 |
* Don't include Content-Length or Connection in @headers. |
| 989 |
* |
| 990 |
* This calls cockpit_web_response_headers_full(), |
| 991 |
* cockpit_web_response_queue() and cockpit_web_response_complete() |
| 992 |
* internally. |
| 993 |
*/ |
| 994 |
void |
| 995 |
cockpit_web_response_content (CockpitWebResponse *self, |
| 996 |
GHashTable *headers, |
| 997 |
GBytes *block, |
| 998 |
...) |
| 999 |
{ |
| 1000 |
GBytes *first; |
| 1001 |
gsize length = 0; |
| 1002 |
va_list va; |
| 1003 |
va_list va2; |
| 1004 |
|
| 1005 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 1006 |
|
| 1007 |
first = block; |
| 1008 |
va_start (va, block); |
| 1009 |
va_copy (va2, va); |
| 1010 |
|
| 1011 |
while (block) |
| 1012 |
{ |
| 1013 |
length += g_bytes_get_size (block); |
| 1014 |
block = va_arg (va, GBytes *); |
| 1015 |
} |
| 1016 |
va_end (va); |
| 1017 |
|
| 1018 |
cockpit_web_response_headers_full (self, 200, "OK", length, headers); |
| 1019 |
|
| 1020 |
block = first; |
| 1021 |
for (;;) |
| 1022 |
{ |
| 1023 |
if (!block) |
| 1024 |
{ |
| 1025 |
cockpit_web_response_complete (self); |
| 1026 |
break; |
| 1027 |
} |
| 1028 |
if (!cockpit_web_response_queue (self, block)) |
| 1029 |
break; |
| 1030 |
block = va_arg (va2, GBytes *); |
| 1031 |
} |
| 1032 |
va_end (va2); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static GBytes * |
| 1036 |
substitute_hash_value (const gchar *variable, |
| 1037 |
gpointer user_data) |
| 1038 |
{ |
| 1039 |
GHashTable *data = user_data; |
| 1040 |
gchar *value = g_hash_table_lookup (data, variable); |
| 1041 |
if (value) |
| 1042 |
return g_bytes_new (value, strlen (value)); |
| 1043 |
return g_bytes_new ("", 0); |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** |
| 1047 |
* cockpit_web_response_error: |
| 1048 |
* @self: the response |
| 1049 |
* @status: the HTTP status code |
| 1050 |
* @headers: headers to include or NULL |
| 1051 |
* @format: printf format of error message |
| 1052 |
* |
| 1053 |
* Send an error message with a basic HTML page containing |
| 1054 |
* the error. |
| 1055 |
*/ |
| 1056 |
void |
| 1057 |
cockpit_web_response_error (CockpitWebResponse *self, |
| 1058 |
guint code, |
| 1059 |
GHashTable *headers, |
| 1060 |
const gchar *format, |
| 1061 |
...) |
| 1062 |
{ |
| 1063 |
g_autofree gchar *reason = NULL; |
| 1064 |
va_list ap; |
| 1065 |
|
| 1066 |
if (format) { |
| 1067 |
va_start (ap, format); |
| 1068 |
reason = g_strdup_vprintf (format, ap); |
| 1069 |
va_end (ap); |
| 1070 |
} |
| 1071 |
|
| 1072 |
cockpit_web_response_error_with_body (self, code, reason, headers, NULL); |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
/** |
| 1077 |
* cockpit_web_response_error_with_Body: |
| 1078 |
* @self: the response |
| 1079 |
* @status: the HTTP status code |
| 1080 |
* @reason: the HTTP status next (or NULL to choose one by @code) |
| 1081 |
* @headers: headers to include or NULL |
| 1082 |
* |
| 1083 |
* Send an error response with an optional body. If no body is |
| 1084 |
* provided, sends basic HTML page containing the error. |
| 1085 |
*/ |
| 1086 |
void |
| 1087 |
cockpit_web_response_error_with_body (CockpitWebResponse *self, |
| 1088 |
guint code, |
| 1089 |
const gchar *reason, |
| 1090 |
GHashTable *headers, |
| 1091 |
GBytes *body) |
| 1092 |
{ |
| 1093 |
g_autofree gchar *escaped = NULL; |
| 1094 |
g_autofree gchar *reason_local = NULL; |
| 1095 |
const gchar *message; |
| 1096 |
|
| 1097 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 1098 |
|
| 1099 |
if (reason) |
| 1100 |
{ |
| 1101 |
/* If sending arbitrary messages, make sure they're escaped */ |
| 1102 |
escaped = g_uri_escape_string (reason, " :", FALSE); |
| 1103 |
message = escaped; |
| 1104 |
} |
| 1105 |
else |
| 1106 |
{ |
| 1107 |
switch (code) |
| 1108 |
{ |
| 1109 |
case 400: |
| 1110 |
message = "Bad request"; |
| 1111 |
break; |
| 1112 |
case 401: |
| 1113 |
message = "Not Authorized"; |
| 1114 |
break; |
| 1115 |
case 403: |
| 1116 |
message = "Forbidden"; |
| 1117 |
break; |
| 1118 |
case 404: |
| 1119 |
message = "Not Found"; |
| 1120 |
break; |
| 1121 |
case 405: |
| 1122 |
message = "Method Not Allowed"; |
| 1123 |
break; |
| 1124 |
case 413: |
| 1125 |
message = "Request Entity Too Large"; |
| 1126 |
break; |
| 1127 |
case 502: |
| 1128 |
message = "Remote Page is Unavailable"; |
| 1129 |
break; |
| 1130 |
case 500: |
| 1131 |
message = "Internal Server Error"; |
| 1132 |
break; |
| 1133 |
default: |
| 1134 |
if (code < 100) |
| 1135 |
reason_local = g_strdup_printf ("%u Continue", code); |
| 1136 |
else if (code < 200) |
| 1137 |
reason_local = g_strdup_printf ("%u OK", code); |
| 1138 |
else if (code < 300) |
| 1139 |
reason_local = g_strdup_printf ("%u Moved", code); |
| 1140 |
else |
| 1141 |
reason_local = g_strdup_printf ("%u Failed", code); |
| 1142 |
message = reason_local; |
| 1143 |
break; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
g_debug ("%s: returning error: %u %s", self->logname, code, message); |
| 1148 |
|
| 1149 |
if (headers) |
| 1150 |
{ |
| 1151 |
if (!g_hash_table_lookup (headers, "Content-Type")) |
| 1152 |
g_hash_table_replace (headers, g_strdup ("Content-Type"), g_strdup ("text/html; charset=utf8")); |
| 1153 |
cockpit_web_response_headers_full (self, code, message, -1, headers); |
| 1154 |
} |
| 1155 |
else |
| 1156 |
{ |
| 1157 |
cockpit_web_response_headers (self, code, message, -1, "Content-Type", "text/html; charset=utf8", NULL); |
| 1158 |
} |
| 1159 |
|
| 1160 |
if (!g_str_equal (self->method, "HEAD")) |
| 1161 |
{ |
| 1162 |
if (body != NULL) |
| 1163 |
{ |
| 1164 |
// If the caller had content, send it... |
| 1165 |
cockpit_web_response_queue (self, body); |
| 1166 |
} |
| 1167 |
else |
| 1168 |
{ |
| 1169 |
// Otherwise, show our fail.html page... |
| 1170 |
extern const char *cockpit_webresponse_fail_html_text; |
| 1171 |
GString *html = g_string_new (cockpit_webresponse_fail_html_text); |
| 1172 |
g_string_replace (html, "@@message@@", message, 0); |
| 1173 |
g_autoptr(GBytes) bytes = g_string_free_to_bytes (html); |
| 1174 |
if (!cockpit_web_response_queue (self, bytes)) |
| 1175 |
return; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
cockpit_web_response_complete (self); |
| 1180 |
} |
| 1181 |
|
| 1182 |
/** |
| 1183 |
* cockpit_web_response_error: |
| 1184 |
* @self: the response |
| 1185 |
* @headers: headers to include or NULL |
| 1186 |
* @error: the error |
| 1187 |
* |
| 1188 |
* Send an error message with a basic HTML page containing |
| 1189 |
* the error. |
| 1190 |
*/ |
| 1191 |
void |
| 1192 |
cockpit_web_response_gerror (CockpitWebResponse *self, |
| 1193 |
GHashTable *headers, |
| 1194 |
GBytes *body, |
| 1195 |
GError *error) |
| 1196 |
{ |
| 1197 |
int code; |
| 1198 |
|
| 1199 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 1200 |
|
| 1201 |
if (g_error_matches (error, |
| 1202 |
COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED)) |
| 1203 |
code = 401; |
| 1204 |
else if (g_error_matches (error, |
| 1205 |
COCKPIT_ERROR, COCKPIT_ERROR_PERMISSION_DENIED)) |
| 1206 |
code = 403; |
| 1207 |
else if (g_error_matches (error, |
| 1208 |
G_IO_ERROR, G_IO_ERROR_INVALID_DATA)) |
| 1209 |
code = 400; |
| 1210 |
else if (g_error_matches (error, |
| 1211 |
G_IO_ERROR, G_IO_ERROR_NO_SPACE)) |
| 1212 |
code = 413; |
| 1213 |
else |
| 1214 |
code = 500; |
| 1215 |
|
| 1216 |
cockpit_web_response_error_with_body (self, code, error->message, headers, body); |
| 1217 |
} |
| 1218 |
|
| 1219 |
static gboolean |
| 1220 |
path_has_prefix (const gchar *path, |
| 1221 |
const gchar *prefix) |
| 1222 |
{ |
| 1223 |
gsize len; |
| 1224 |
if (prefix == NULL) |
| 1225 |
return FALSE; |
| 1226 |
len = strlen (prefix); |
| 1227 |
if (len == 0) |
| 1228 |
return FALSE; |
| 1229 |
if (!g_str_has_prefix (path, prefix)) |
| 1230 |
return FALSE; |
| 1231 |
if (prefix[len - 1] == '/' || |
| 1232 |
path[len] == '/') |
| 1233 |
return TRUE; |
| 1234 |
return FALSE; |
| 1235 |
} |
| 1236 |
|
| 1237 |
gchar ** |
| 1238 |
cockpit_web_response_resolve_roots (const gchar **input) |
| 1239 |
{ |
| 1240 |
GPtrArray *roots; |
| 1241 |
char *path; |
| 1242 |
gint i; |
| 1243 |
|
| 1244 |
roots = g_ptr_array_new (); |
| 1245 |
for (i = 0; input && input[i]; i++) |
| 1246 |
{ |
| 1247 |
path = realpath (input[i], NULL); |
| 1248 |
if (path == NULL) |
| 1249 |
g_debug ("couldn't resolve document root: %s: %m", input[i]); |
| 1250 |
else |
| 1251 |
g_ptr_array_add (roots, path); |
| 1252 |
} |
| 1253 |
g_ptr_array_add (roots, NULL); |
| 1254 |
return (gchar **)g_ptr_array_free (roots, FALSE); |
| 1255 |
} |
| 1256 |
|
| 1257 |
static void |
| 1258 |
web_response_file (CockpitWebResponse *response, |
| 1259 |
const gchar *escaped, |
| 1260 |
const gchar **roots, |
| 1261 |
gboolean search_gzip, |
| 1262 |
gboolean accept_gzip, |
| 1263 |
CockpitTemplateFunc template_func, |
| 1264 |
gpointer user_data) |
| 1265 |
{ |
| 1266 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (response)); |
| 1267 |
g_return_if_fail (!search_gzip || template_func == NULL); |
| 1268 |
g_return_if_fail (search_gzip == accept_gzip); |
| 1269 |
|
| 1270 |
if (!escaped) |
| 1271 |
escaped = cockpit_web_response_get_path (response); |
| 1272 |
|
| 1273 |
g_return_if_fail (escaped != NULL); |
| 1274 |
|
| 1275 |
/* Someone is trying to escape the root directory, or access hidden files? */ |
| 1276 |
g_autofree gchar *unescaped = g_uri_unescape_string (escaped, "/"); |
| 1277 |
if (!unescaped || strstr (unescaped, "/.") || strstr (unescaped, "../") || strstr (unescaped, "//")) |
| 1278 |
{ |
| 1279 |
g_debug ("%s: invalid path request", escaped); |
| 1280 |
cockpit_web_response_error (response, 404, NULL, "Not Found"); |
| 1281 |
return; |
| 1282 |
} |
| 1283 |
|
| 1284 |
gboolean is_gzip = FALSE; |
| 1285 |
g_autoptr(GMappedFile) file = NULL; |
| 1286 |
for (gint i = 0; roots[i]; i++) |
| 1287 |
{ |
| 1288 |
const gchar *root = roots[i]; |
| 1289 |
g_autofree gchar *path = g_build_filename (root, unescaped, NULL); |
| 1290 |
|
| 1291 |
if (g_file_test (path, G_FILE_TEST_IS_DIR)) |
| 1292 |
{ |
| 1293 |
cockpit_web_response_error (response, 403, NULL, "Directory Listing Denied"); |
| 1294 |
return; |
| 1295 |
} |
| 1296 |
|
| 1297 |
/* As a double check of above behavior */ |
| 1298 |
g_assert (path_has_prefix (path, root)); |
| 1299 |
|
| 1300 |
g_autoptr(GError) error = NULL; |
| 1301 |
file = g_mapped_file_new (path, FALSE, &error); |
| 1302 |
|
| 1303 |
if (file == NULL && search_gzip && |
| 1304 |
g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) |
| 1305 |
{ |
| 1306 |
g_debug ("%s: file not found in root: %s, looking for .gz", escaped, root); |
| 1307 |
g_clear_error (&error); |
| 1308 |
g_autofree gchar *old_path = g_steal_pointer (&path); |
| 1309 |
path = g_strconcat (old_path, ".gz", NULL); |
| 1310 |
file = g_mapped_file_new (path, FALSE, &error); |
| 1311 |
is_gzip = file != NULL; |
| 1312 |
} |
| 1313 |
|
| 1314 |
if (file != NULL) |
| 1315 |
break; |
| 1316 |
|
| 1317 |
if (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT) || |
| 1318 |
g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NAMETOOLONG)) |
| 1319 |
{ |
| 1320 |
g_debug ("%s: file not found in root: %s", escaped, root); |
| 1321 |
} |
| 1322 |
else if (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_PERM) || |
| 1323 |
g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_ACCES) || |
| 1324 |
g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_ISDIR)) |
| 1325 |
{ |
| 1326 |
cockpit_web_response_error (response, 403, NULL, "Access denied"); |
| 1327 |
return; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
{ |
| 1331 |
g_warning ("%s: %s", path, error->message); |
| 1332 |
cockpit_web_response_error (response, 500, NULL, "Internal server error"); |
| 1333 |
return; |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
if (file == NULL) |
| 1338 |
{ |
| 1339 |
cockpit_web_response_error (response, 404, NULL, "Not Found"); |
| 1340 |
return; |
| 1341 |
} |
| 1342 |
|
| 1343 |
g_autoptr(GBytes) body = g_mapped_file_get_bytes (file); |
| 1344 |
|
| 1345 |
GList *output; |
| 1346 |
gint content_length = -1; |
| 1347 |
if (template_func) |
| 1348 |
{ |
| 1349 |
output = cockpit_template_expand (body, template_func, user_data); |
| 1350 |
} |
| 1351 |
else |
| 1352 |
{ |
| 1353 |
output = g_list_prepend (NULL, g_bytes_ref (body)); |
| 1354 |
content_length = g_bytes_get_size (body); |
| 1355 |
} |
| 1356 |
|
| 1357 |
GString *string = begin_headers (response, 200, "OK"); |
| 1358 |
guint seen = 0; |
| 1359 |
|
| 1360 |
if (response->origin) |
| 1361 |
seen |= append_header (string, "Access-Control-Allow-Origin", response->origin); |
| 1362 |
|
| 1363 |
/* |
| 1364 |
* The default Content-Security-Policy for .html files allows |
| 1365 |
* the site to have inline <script> and <style> tags. This code |
| 1366 |
* is only used for static resources that do not use the session. |
| 1367 |
*/ |
| 1368 |
if (g_str_has_suffix (unescaped, ".html")) |
| 1369 |
{ |
| 1370 |
const gchar *default_policy = "default-src 'self' 'unsafe-inline';"; |
| 1371 |
g_autofree gchar *policy = cockpit_web_response_security_policy (default_policy, response->origin); |
| 1372 |
seen |= append_header (string, "Content-Security-Policy", policy); |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (is_gzip) |
| 1376 |
seen |= append_header (string, "Content-Encoding", "gzip"); |
| 1377 |
|
| 1378 |
g_autoptr(GBytes) headers_block = finish_headers (response, string, content_length, 200, seen); |
| 1379 |
queue_bytes (response, headers_block); |
| 1380 |
|
| 1381 |
GList *l; |
| 1382 |
for (l = output; l != NULL; l = g_list_next (l)) |
| 1383 |
{ |
| 1384 |
if (!cockpit_web_response_queue (response, l->data)) |
| 1385 |
break; |
| 1386 |
} |
| 1387 |
if (l == NULL) |
| 1388 |
cockpit_web_response_complete (response); |
| 1389 |
|
| 1390 |
g_list_free_full (output, (GDestroyNotify)g_bytes_unref); |
| 1391 |
} |
| 1392 |
|
| 1393 |
/** |
| 1394 |
* cockpit_web_response_file: |
| 1395 |
* @response: the response |
| 1396 |
* @path: escaped path, or NULL to get from response |
| 1397 |
* @roots: directories to look for file in |
| 1398 |
* |
| 1399 |
* Serve a file from disk as an HTTP response. |
| 1400 |
*/ |
| 1401 |
void |
| 1402 |
cockpit_web_response_file (CockpitWebResponse *response, |
| 1403 |
const gchar *escaped, |
| 1404 |
const gchar **roots) |
| 1405 |
{ |
| 1406 |
web_response_file (response, escaped, roots, FALSE, FALSE, NULL, NULL); |
| 1407 |
} |
| 1408 |
|
| 1409 |
void |
| 1410 |
cockpit_web_response_template (CockpitWebResponse *response, |
| 1411 |
const gchar *escaped, |
| 1412 |
const gchar **roots, |
| 1413 |
GHashTable *values) |
| 1414 |
{ |
| 1415 |
web_response_file (response, escaped, roots, FALSE, FALSE, substitute_hash_value, values); |
| 1416 |
} |
| 1417 |
|
| 1418 |
void |
| 1419 |
cockpit_web_response_file_or_gz (CockpitWebResponse *response, |
| 1420 |
const gchar *escaped, |
| 1421 |
const gchar **roots) |
| 1422 |
{ |
| 1423 |
web_response_file (response, escaped, roots, TRUE, TRUE, NULL, NULL); |
| 1424 |
} |
| 1425 |
|
| 1426 |
static gboolean |
| 1427 |
response_next_path (CockpitWebResponse *self, |
| 1428 |
gchar **component) |
| 1429 |
{ |
| 1430 |
const gchar *beg = NULL; |
| 1431 |
const gchar *path; |
| 1432 |
|
| 1433 |
g_return_val_if_fail (COCKPIT_IS_WEB_RESPONSE (self), FALSE); |
| 1434 |
|
| 1435 |
path = self->path; |
| 1436 |
|
| 1437 |
if (path && path[0] == '/') |
| 1438 |
{ |
| 1439 |
beg = path + 1; |
| 1440 |
path = strchr (beg, '/'); |
| 1441 |
} |
| 1442 |
else |
| 1443 |
{ |
| 1444 |
path = NULL; |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (!beg || path == beg) |
| 1448 |
return FALSE; |
| 1449 |
|
| 1450 |
self->path = path; |
| 1451 |
|
| 1452 |
if (self->path) |
| 1453 |
{ |
| 1454 |
if (component) |
| 1455 |
*component = g_strndup (beg, path - beg); |
| 1456 |
} |
| 1457 |
else if (beg && beg[0]) |
| 1458 |
{ |
| 1459 |
if (component) |
| 1460 |
*component = g_strdup (beg); |
| 1461 |
} |
| 1462 |
else |
| 1463 |
{ |
| 1464 |
return FALSE; |
| 1465 |
} |
| 1466 |
|
| 1467 |
return TRUE; |
| 1468 |
} |
| 1469 |
|
| 1470 |
gboolean |
| 1471 |
cockpit_web_response_skip_path (CockpitWebResponse *self) |
| 1472 |
{ |
| 1473 |
return response_next_path (self, NULL); |
| 1474 |
} |
| 1475 |
|
| 1476 |
gchar * |
| 1477 |
cockpit_web_response_pop_path (CockpitWebResponse *self) |
| 1478 |
{ |
| 1479 |
gchar *component = NULL; |
| 1480 |
if (!response_next_path (self, &component)) |
| 1481 |
return NULL; |
| 1482 |
return component; |
| 1483 |
} |
| 1484 |
|
| 1485 |
void |
| 1486 |
cockpit_web_response_add_filter (CockpitWebResponse *self, |
| 1487 |
CockpitWebFilter *filter) |
| 1488 |
{ |
| 1489 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (self)); |
| 1490 |
g_return_if_fail (COCKPIT_IS_WEB_FILTER (filter)); |
| 1491 |
g_return_if_fail (self->count == 0); |
| 1492 |
self->filters = g_list_append (self->filters, g_object_ref (filter)); |
| 1493 |
} |
| 1494 |
|
| 1495 |
static const gchar * |
| 1496 |
find_extension (const gchar *path) |
| 1497 |
{ |
| 1498 |
const gchar *dot; |
| 1499 |
const gchar *slash; |
| 1500 |
|
| 1501 |
dot = strrchr (path, '.'); |
| 1502 |
slash = strrchr (path, '/'); |
| 1503 |
|
| 1504 |
/* Dots before the last slash don't count */ |
| 1505 |
if (dot && slash && dot < slash) |
| 1506 |
dot = NULL; |
| 1507 |
|
| 1508 |
/* Leading dots on the filename don't count */ |
| 1509 |
if (dot && (dot == path || dot == slash + 1)) |
| 1510 |
dot = NULL; |
| 1511 |
|
| 1512 |
return dot; |
| 1513 |
} |
| 1514 |
|
| 1515 |
static GBytes * |
| 1516 |
load_file (const gchar *filename, |
| 1517 |
GError **error) |
| 1518 |
{ |
| 1519 |
GError *local_error = NULL; |
| 1520 |
|
| 1521 |
g_autoptr(GMappedFile) mapped = g_mapped_file_new (filename, FALSE, &local_error); |
| 1522 |
|
| 1523 |
if (mapped) |
| 1524 |
/* success! */ |
| 1525 |
return g_mapped_file_get_bytes (mapped); |
| 1526 |
|
| 1527 |
if (g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NOENT) || |
| 1528 |
g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_ISDIR) || |
| 1529 |
g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NAMETOOLONG) || |
| 1530 |
g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_LOOP) || |
| 1531 |
g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_INVAL)) |
| 1532 |
{ |
| 1533 |
g_clear_error (&local_error); |
| 1534 |
} |
| 1535 |
|
| 1536 |
/* A real error to stop on */ |
| 1537 |
else |
| 1538 |
{ |
| 1539 |
g_propagate_error (error, local_error); |
| 1540 |
} |
| 1541 |
|
| 1542 |
return NULL; |
| 1543 |
} |
| 1544 |
|
| 1545 |
/** |
| 1546 |
* cockpit_web_response_negotiation: |
| 1547 |
* @path: likely filesystem path |
| 1548 |
* @existing: a table of existing files |
| 1549 |
* @language: requested client language |
| 1550 |
* @out_is_language_specific: a pointer to a gboolean whether the actual file is specific to @language |
| 1551 |
* @out_is_compressed: a pointer to a gboolean whether the actual file is compressed |
| 1552 |
* @error: a failure |
| 1553 |
* |
| 1554 |
* Find a file to serve based on the suffixes. We prune off extra |
| 1555 |
* extensions while looking for a file that's present. We append |
| 1556 |
* .min and .gz when looking for files. We also check for the language |
| 1557 |
* before the extensions if set. |
| 1558 |
* |
| 1559 |
* The @existing may be NULL, if non-null it'll be used to check if |
| 1560 |
* files exist. |
| 1561 |
*/ |
| 1562 |
GBytes * |
| 1563 |
cockpit_web_response_negotiation (const gchar *path, |
| 1564 |
GHashTable *existing, |
| 1565 |
const gchar *language, |
| 1566 |
gboolean *out_is_language_specific, |
| 1567 |
gboolean *out_is_compressed, |
| 1568 |
GError **error) |
| 1569 |
{ |
| 1570 |
gchar *base = NULL; |
| 1571 |
const gchar *ext; |
| 1572 |
gchar *dot; |
| 1573 |
gchar *name = NULL; |
| 1574 |
GBytes *bytes = NULL; |
| 1575 |
GError *local_error = NULL; |
| 1576 |
gchar *locale = NULL; |
| 1577 |
gchar *shorter = NULL; |
| 1578 |
gchar *lang = NULL; |
| 1579 |
gchar *lang_region = NULL; |
| 1580 |
gboolean is_language_specific, is_compressed; |
| 1581 |
|
| 1582 |
gint i; |
| 1583 |
|
| 1584 |
if (language) |
| 1585 |
locale = cockpit_locale_from_language (language, NULL, &shorter); |
| 1586 |
|
| 1587 |
ext = find_extension (path); |
| 1588 |
if (ext) |
| 1589 |
{ |
| 1590 |
base = g_strndup (path, ext - path); |
| 1591 |
} |
| 1592 |
else |
| 1593 |
{ |
| 1594 |
ext = ""; |
| 1595 |
base = g_strdup (path); |
| 1596 |
} |
| 1597 |
|
| 1598 |
while (!bytes) |
| 1599 |
{ |
| 1600 |
/* For a request for a file named "base.ext" and locale "lang_REGION", We try the following variants, in |
| 1601 |
order, and serve the first that is found: |
| 1602 |
|
| 1603 |
base.lang_REGION.ext |
| 1604 |
base.lang_REGION.ext.gz |
| 1605 |
base.lang.ext |
| 1606 |
base.lang.ext.gz |
| 1607 |
base.ext |
| 1608 |
base.min.ext |
| 1609 |
base.ext.gz |
| 1610 |
base.ext.min.gz |
| 1611 |
|
| 1612 |
If no locale is requested, or a locale without region, those variants are left out by starting |
| 1613 |
further down in the list. |
| 1614 |
|
| 1615 |
If none of the variants are found, and the base of the file name has internal dots, these internal |
| 1616 |
extensions are dropped one by one from the right. For example, for a file named "foo.bar.js", we |
| 1617 |
first try "foo.bar" with extension ".js", and then "foo" with extension ".js". |
| 1618 |
*/ |
| 1619 |
|
| 1620 |
if (locale && shorter && g_strcmp0 (locale, shorter) != 0) { |
| 1621 |
lang = shorter; |
| 1622 |
lang_region = locale; |
| 1623 |
i = 0; |
| 1624 |
} else if (locale) { |
| 1625 |
lang = locale; |
| 1626 |
i = 2; |
| 1627 |
} else { |
| 1628 |
i = 4; |
| 1629 |
} |
| 1630 |
|
| 1631 |
for (; i < 8; i++) |
| 1632 |
{ |
| 1633 |
g_free (name); |
| 1634 |
switch (i) |
| 1635 |
{ |
| 1636 |
case 0: |
| 1637 |
name = g_strconcat (base, ".", lang_region, ext, NULL); |
| 1638 |
is_language_specific = TRUE; |
| 1639 |
is_compressed = FALSE; |
| 1640 |
break; |
| 1641 |
case 1: |
| 1642 |
name = g_strconcat (base, ".", lang_region, ext, ".gz", NULL); |
| 1643 |
is_language_specific = TRUE; |
| 1644 |
is_compressed = TRUE; |
| 1645 |
break; |
| 1646 |
case 2: |
| 1647 |
name = g_strconcat (base, ".", lang, ext, NULL); |
| 1648 |
is_language_specific = TRUE; |
| 1649 |
is_compressed = FALSE; |
| 1650 |
break; |
| 1651 |
case 3: |
| 1652 |
name = g_strconcat (base, ".", lang, ext, ".gz", NULL); |
| 1653 |
is_language_specific = TRUE; |
| 1654 |
is_compressed = TRUE; |
| 1655 |
break; |
| 1656 |
case 4: |
| 1657 |
name = g_strconcat (base, ext, NULL); |
| 1658 |
is_language_specific = FALSE; |
| 1659 |
is_compressed = FALSE; |
| 1660 |
break; |
| 1661 |
case 5: |
| 1662 |
name = g_strconcat (base, ".min", ext, NULL); |
| 1663 |
is_language_specific = FALSE; |
| 1664 |
is_compressed = FALSE; |
| 1665 |
break; |
| 1666 |
case 6: |
| 1667 |
name = g_strconcat (base, ext, ".gz", NULL); |
| 1668 |
is_language_specific = FALSE; |
| 1669 |
is_compressed = TRUE; |
| 1670 |
break; |
| 1671 |
case 7: |
| 1672 |
name = g_strconcat (base, ".min", ext, ".gz", NULL); |
| 1673 |
is_language_specific = FALSE; |
| 1674 |
is_compressed = TRUE; |
| 1675 |
break; |
| 1676 |
default: |
| 1677 |
g_assert_not_reached (); |
| 1678 |
} |
| 1679 |
|
| 1680 |
if (existing) |
| 1681 |
{ |
| 1682 |
if (!g_hash_table_lookup (existing, name)) |
| 1683 |
continue; |
| 1684 |
} |
| 1685 |
|
| 1686 |
bytes = load_file (name, &local_error); |
| 1687 |
if (bytes) |
| 1688 |
break; |
| 1689 |
if (local_error) |
| 1690 |
goto out; |
| 1691 |
} |
| 1692 |
|
| 1693 |
/* Pop one level off the file name */ |
| 1694 |
dot = (gchar *)find_extension (base); |
| 1695 |
if (!dot) |
| 1696 |
break; |
| 1697 |
|
| 1698 |
dot[0] = '\0'; |
| 1699 |
} |
| 1700 |
|
| 1701 |
out: |
| 1702 |
if (local_error) |
| 1703 |
g_propagate_error (error, local_error); |
| 1704 |
if (bytes) |
| 1705 |
{ |
| 1706 |
if (out_is_language_specific) |
| 1707 |
*out_is_language_specific = is_language_specific; |
| 1708 |
if (out_is_compressed) |
| 1709 |
*out_is_compressed = is_compressed; |
| 1710 |
} |
| 1711 |
g_free (name); |
| 1712 |
g_free (base); |
| 1713 |
g_free (locale); |
| 1714 |
g_free (shorter); |
| 1715 |
return bytes; |
| 1716 |
} |
| 1717 |
|
| 1718 |
const gchar * |
| 1719 |
cockpit_web_response_content_type (const gchar *path) |
| 1720 |
{ |
| 1721 |
static const struct { |
| 1722 |
const gchar *extension; |
| 1723 |
const gchar *content_type; |
| 1724 |
} content_types[] = { |
| 1725 |
{ ".css", "text/css" }, |
| 1726 |
{ ".gif", "image/gif" }, |
| 1727 |
{ ".eot", "application/vnd.ms-fontobject" }, |
| 1728 |
{ ".html", "text/html" }, |
| 1729 |
/* { ".ico", "image/vnd.microsoft.icon" }, */ |
| 1730 |
{ ".jpg", "image/jpg" }, |
| 1731 |
{ ".js", "application/javascript" }, |
| 1732 |
{ ".json", "application/json" }, |
| 1733 |
{ ".otf", "font/opentype" }, |
| 1734 |
{ ".png", "image/png" }, |
| 1735 |
{ ".svg", "image/svg+xml" }, |
| 1736 |
{ ".ttf", "application/octet-stream" }, /* unassigned */ |
| 1737 |
{ ".txt", "text/plain" }, |
| 1738 |
{ ".wasm", "application/wasm" }, |
| 1739 |
{ ".woff", "application/font-woff" }, |
| 1740 |
{ ".xml", "text/xml" }, |
| 1741 |
}; |
| 1742 |
|
| 1743 |
gint i; |
| 1744 |
|
| 1745 |
for (i = 0; i < G_N_ELEMENTS (content_types); i++) |
| 1746 |
{ |
| 1747 |
if (g_str_has_suffix (path, content_types[i].extension)) |
| 1748 |
return content_types[i].content_type; |
| 1749 |
} |
| 1750 |
|
| 1751 |
return NULL; |
| 1752 |
} |
| 1753 |
|
| 1754 |
static gboolean |
| 1755 |
strv_have_prefix (gchar **strv, |
| 1756 |
const gchar *prefix) |
| 1757 |
{ |
| 1758 |
gint i; |
| 1759 |
|
| 1760 |
for (i = 0; strv && strv[i] != NULL; i++) |
| 1761 |
{ |
| 1762 |
if (g_str_has_prefix (strv[i], prefix)) |
| 1763 |
return TRUE; |
| 1764 |
} |
| 1765 |
return FALSE; |
| 1766 |
} |
| 1767 |
|
| 1768 |
static void |
| 1769 |
string_inject_origin (GString *string, |
| 1770 |
const gchar *origin) |
| 1771 |
{ |
| 1772 |
const gchar *found; |
| 1773 |
gsize pos = 0; |
| 1774 |
|
| 1775 |
for (;;) |
| 1776 |
{ |
| 1777 |
found = strstr (string->str + pos, "'self'"); |
| 1778 |
if (!found) |
| 1779 |
break; |
| 1780 |
|
| 1781 |
pos = (found - string->str) + 6; |
| 1782 |
g_string_insert (string, pos, " "); |
| 1783 |
g_string_insert (string, pos + 1, origin); |
| 1784 |
pos += strlen (origin) + 1; |
| 1785 |
} |
| 1786 |
} |
| 1787 |
|
| 1788 |
/** |
| 1789 |
* cockpit_web_response_content_security_policy: |
| 1790 |
* @content_security_policy: the raw security policy or %NULL for a default |
| 1791 |
* @self_origin: our own web origin or %NULL |
| 1792 |
* |
| 1793 |
* Calculates the security policy. |
| 1794 |
* |
| 1795 |
* Returns: A calculated security policy, filled with defaults if necessary. |
| 1796 |
*/ |
| 1797 |
gchar * |
| 1798 |
cockpit_web_response_security_policy (const gchar *content_security_policy, |
| 1799 |
const gchar *self_origin) |
| 1800 |
{ |
| 1801 |
const gchar *default_src = "default-src 'self'"; |
| 1802 |
const gchar *form_action = "form-action 'self'"; |
| 1803 |
const gchar *base_uri = "base-uri 'self'"; |
| 1804 |
const gchar *object_src = "object-src 'none'"; |
| 1805 |
const gchar *font_src = "font-src 'self' data:"; |
| 1806 |
const gchar *img_src = "img-src 'self' data:"; |
| 1807 |
const gchar *block_all_mixed_content = "block-all-mixed-content"; |
| 1808 |
gchar **parts = NULL; |
| 1809 |
GString *result; |
| 1810 |
gint i; |
| 1811 |
|
| 1812 |
result = g_string_sized_new (128); |
| 1813 |
|
| 1814 |
/* |
| 1815 |
* Note that browsers need to be explicitly told they can connect |
| 1816 |
* to a WebSocket. This is non-obvious, but it stems from the fact |
| 1817 |
* that some browsers treat 'https' and 'wss' as different protocols. |
| 1818 |
* |
| 1819 |
* Since each component could establish a WebSocket connection back to |
| 1820 |
* cockpit-ws, we need to insert that into the policy. |
| 1821 |
*/ |
| 1822 |
|
| 1823 |
if (content_security_policy) |
| 1824 |
parts = g_strsplit (content_security_policy, ";", -1); |
| 1825 |
|
| 1826 |
for (i = 0; parts && parts[i] != NULL; i++) |
| 1827 |
g_strstrip (parts[i]); |
| 1828 |
|
| 1829 |
if (!strv_have_prefix (parts, "default-src ")) |
| 1830 |
g_string_append_printf (result, "%s; ", default_src); |
| 1831 |
if (!strv_have_prefix (parts, "connect-src ")) |
| 1832 |
{ |
| 1833 |
g_string_append (result, "connect-src 'self'"); |
| 1834 |
if (self_origin && g_str_has_prefix (self_origin, "http")) |
| 1835 |
g_string_append_printf (result, " ws%s", self_origin + 4); |
| 1836 |
g_string_append (result, "; "); |
| 1837 |
} |
| 1838 |
if (!strv_have_prefix (parts, "form-action ")) |
| 1839 |
g_string_append_printf (result, "%s; ", form_action); |
| 1840 |
if (!strv_have_prefix (parts, "base-uri ")) |
| 1841 |
g_string_append_printf (result, "%s; ", base_uri); |
| 1842 |
if (!strv_have_prefix (parts, "object-src ")) |
| 1843 |
g_string_append_printf (result, "%s; ", object_src); |
| 1844 |
if (!strv_have_prefix (parts, "font-src ")) |
| 1845 |
g_string_append_printf (result, "%s; ", font_src); |
| 1846 |
if (!strv_have_prefix (parts, "img-src ")) |
| 1847 |
g_string_append_printf (result, "%s; ", img_src); |
| 1848 |
if (!strv_have_prefix (parts, "block-all-mixed-content")) |
| 1849 |
g_string_append_printf (result, "%s; ", block_all_mixed_content); |
| 1850 |
|
| 1851 |
for (i = 0; parts && parts[i] != NULL; i++) |
| 1852 |
g_string_append_printf (result, "%s; ", parts[i]); |
| 1853 |
|
| 1854 |
g_strfreev (parts); |
| 1855 |
|
| 1856 |
/* Remove trailing semicolon */ |
| 1857 |
g_string_set_size (result, result->len - 2); |
| 1858 |
|
| 1859 |
/* Put in our own origin */ |
| 1860 |
if (self_origin) |
| 1861 |
string_inject_origin (result, self_origin); |
| 1862 |
|
| 1863 |
return g_string_free (result, FALSE); |
| 1864 |
} |
| 1865 |
|
| 1866 |
const gchar * |
| 1867 |
cockpit_web_response_get_origin (CockpitWebResponse *self) |
| 1868 |
{ |
| 1869 |
g_return_val_if_fail (COCKPIT_IS_WEB_RESPONSE (self), NULL); |
| 1870 |
return self->origin; |
| 1871 |
} |
| 1872 |
|
| 1873 |
const gchar * |
| 1874 |
cockpit_web_response_get_protocol (CockpitWebResponse *self) |
| 1875 |
{ |
| 1876 |
return self->protocol; |
| 1877 |
} |
| 1878 |
|
| 1879 |
static void |
| 1880 |
cockpit_web_response_flow_iface_init (CockpitFlowInterface *iface) |
| 1881 |
{ |
| 1882 |
/* No implementation */ |
| 1883 |
} |