| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitchannelresponse.h" |
| 9 |
|
| 10 |
#include "cockpitchannel.h" |
| 11 |
#include "common/cockpitconf.h" |
| 12 |
#include "cockpitflow.h" |
| 13 |
#include "cockpitwebinject.h" |
| 14 |
#include "cockpitwebserver.h" |
| 15 |
#include "cockpitwebresponse.h" |
| 16 |
|
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
typedef struct { |
| 20 |
CockpitWebService *service; |
| 21 |
gchar *base_path; |
| 22 |
gchar *host; |
| 23 |
} CockpitChannelInject; |
| 24 |
|
| 25 |
static void |
| 26 |
cockpit_channel_inject_free (gpointer data) |
| 27 |
{ |
| 28 |
CockpitChannelInject *inject = data; |
| 29 |
|
| 30 |
if (inject) |
| 31 |
{ |
| 32 |
if (inject->service) |
| 33 |
g_object_remove_weak_pointer (G_OBJECT (inject->service), (gpointer *)&inject->service); |
| 34 |
g_free (inject->base_path); |
| 35 |
g_free (inject->host); |
| 36 |
g_free (inject); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static CockpitChannelInject * |
| 41 |
cockpit_channel_inject_new (CockpitWebService *service, |
| 42 |
const gchar *path, |
| 43 |
const gchar *host) |
| 44 |
{ |
| 45 |
CockpitChannelInject *inject = g_new (CockpitChannelInject, 1); |
| 46 |
inject->service = service; |
| 47 |
g_object_add_weak_pointer (G_OBJECT (inject->service), (gpointer *)&inject->service); |
| 48 |
inject->base_path = g_strdup (path); |
| 49 |
inject->host = g_strdup (host); |
| 50 |
return inject; |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
cockpit_channel_inject_update_checksum (CockpitChannelInject *inject, |
| 55 |
GHashTable *headers) |
| 56 |
{ |
| 57 |
const gchar *checksum = g_hash_table_lookup (headers, COCKPIT_CHECKSUM_HEADER); |
| 58 |
|
| 59 |
if (checksum) |
| 60 |
cockpit_web_service_set_host_checksum (inject->service, inject->host, checksum); |
| 61 |
|
| 62 |
/* No need to send our custom header outside of cockpit */ |
| 63 |
g_hash_table_remove (headers, COCKPIT_CHECKSUM_HEADER); |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
cockpit_channel_inject_perform (CockpitChannelInject *inject, |
| 68 |
CockpitWebResponse *response, |
| 69 |
CockpitTransport *transport) |
| 70 |
{ |
| 71 |
static const gchar *marker = "<head>"; |
| 72 |
g_autofree gchar *prefixed_application = NULL; |
| 73 |
|
| 74 |
const gchar *url_root = cockpit_web_response_get_url_root (response); |
| 75 |
|
| 76 |
if (!url_root && !inject->base_path) |
| 77 |
return; |
| 78 |
|
| 79 |
g_autoptr(GString) str = g_string_new (""); |
| 80 |
CockpitCreds *creds = cockpit_web_service_get_creds (inject->service); |
| 81 |
if (url_root) |
| 82 |
{ |
| 83 |
g_string_append_printf (str, "\n <meta name=\"url-root\" content=\"%s\">", url_root); |
| 84 |
prefixed_application = g_strdup_printf ("%s/%s", |
| 85 |
url_root, |
| 86 |
cockpit_creds_get_application (creds)); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
prefixed_application = g_strdup_printf ("/%s", cockpit_creds_get_application (creds)); |
| 91 |
} |
| 92 |
|
| 93 |
{ |
| 94 |
const gboolean allow_multihost = cockpit_conf_bool ("WebService", "AllowMultiHost", |
| 95 |
ALLOW_MULTIHOST_DEFAULT); |
| 96 |
g_string_append_printf (str, "\n <meta name=\"allow-multihost\" content=\"%s\">", |
| 97 |
allow_multihost ? "yes" : "no"); |
| 98 |
} |
| 99 |
|
| 100 |
if (inject->base_path) |
| 101 |
{ |
| 102 |
const gchar *checksum = cockpit_web_service_get_checksum (inject->service, inject->host); |
| 103 |
if (checksum) |
| 104 |
{ |
| 105 |
g_string_append_printf (str, "\n <base href=\"%s/$%s%s\">", |
| 106 |
prefixed_application, |
| 107 |
checksum, inject->base_path); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
g_string_append_printf (str, "\n <base href=\"%s/@%s%s\">", |
| 112 |
prefixed_application, inject->host, inject->base_path); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
g_autoptr(GBytes) content = g_string_free_to_bytes (g_steal_pointer(&str)); |
| 117 |
g_autoptr(CockpitWebFilter) filter = cockpit_web_inject_new (marker, content, 1); |
| 118 |
cockpit_web_response_add_filter (response, filter); |
| 119 |
} |
| 120 |
|
| 121 |
#define COCKPIT_TYPE_CHANNEL_RESPONSE (cockpit_channel_response_get_type ()) |
| 122 |
#define COCKPIT_CHANNEL_RESPONSE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), COCKPIT_TYPE_CHANNEL_RESPONSE, CockpitChannelResponse)) |
| 123 |
#define COCKPIT_IS_CHANNEL_RESPONSE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), COCKPIT_TYPE_CHANNEL_RESPONSE)) |
| 124 |
|
| 125 |
typedef struct { |
| 126 |
CockpitChannel parent; |
| 127 |
|
| 128 |
const gchar *logname; |
| 129 |
CockpitWebResponse *response; |
| 130 |
GHashTable *headers; |
| 131 |
|
| 132 |
/* We can handle http-stream1 and http-stream2 */ |
| 133 |
gboolean http_stream1_prefix; |
| 134 |
gboolean http_stream2; |
| 135 |
|
| 136 |
/* Set when injecting data into response */ |
| 137 |
CockpitChannelInject *inject; |
| 138 |
} CockpitChannelResponse; |
| 139 |
|
| 140 |
typedef struct { |
| 141 |
CockpitChannelClass parent; |
| 142 |
} CockpitChannelResponseClass; |
| 143 |
|
| 144 |
GType cockpit_channel_response_get_type (void); |
| 145 |
|
| 146 |
G_DEFINE_TYPE (CockpitChannelResponse, cockpit_channel_response, COCKPIT_TYPE_CHANNEL); |
| 147 |
|
| 148 |
static void |
| 149 |
cockpit_channel_response_init (CockpitChannelResponse *self) |
| 150 |
{ |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
cockpit_channel_response_finalize (GObject *object) |
| 156 |
{ |
| 157 |
CockpitChannelResponse *self = COCKPIT_CHANNEL_RESPONSE (object); |
| 158 |
|
| 159 |
g_object_unref (self->response); |
| 160 |
g_hash_table_unref (self->headers); |
| 161 |
cockpit_channel_inject_free (self->inject); |
| 162 |
|
| 163 |
G_OBJECT_CLASS (cockpit_channel_response_parent_class)->finalize (object); |
| 164 |
} |
| 165 |
|
| 166 |
static gboolean |
| 167 |
ensure_headers (CockpitChannelResponse *self, |
| 168 |
guint status, |
| 169 |
const gchar *reason, |
| 170 |
gsize length) |
| 171 |
{ |
| 172 |
|
| 173 |
if (cockpit_web_response_get_state (self->response) == COCKPIT_WEB_RESPONSE_READY) |
| 174 |
{ |
| 175 |
if (self->inject && self->inject->service) |
| 176 |
{ |
| 177 |
cockpit_channel_inject_update_checksum (self->inject, self->headers); |
| 178 |
cockpit_channel_inject_perform (self->inject, self->response, |
| 179 |
cockpit_channel_get_transport (COCKPIT_CHANNEL (self))); |
| 180 |
} |
| 181 |
cockpit_web_response_headers_full (self->response, status, reason, length, self->headers); |
| 182 |
return TRUE; |
| 183 |
} |
| 184 |
|
| 185 |
return FALSE; |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
cockpit_channel_response_close (CockpitChannel *channel, |
| 190 |
const gchar *problem) |
| 191 |
{ |
| 192 |
CockpitChannelResponse *self = COCKPIT_CHANNEL_RESPONSE (channel); |
| 193 |
CockpitWebResponding state; |
| 194 |
|
| 195 |
/* The web response should not yet be complete */ |
| 196 |
state = cockpit_web_response_get_state (self->response); |
| 197 |
|
| 198 |
if (problem == NULL) |
| 199 |
{ |
| 200 |
/* Closed without any data */ |
| 201 |
if (state == COCKPIT_WEB_RESPONSE_READY) |
| 202 |
{ |
| 203 |
ensure_headers (self, 204, "OK", 0); |
| 204 |
cockpit_web_response_complete (self->response); |
| 205 |
g_debug ("%s: no content in external channel", self->logname); |
| 206 |
} |
| 207 |
else if (state < COCKPIT_WEB_RESPONSE_COMPLETE) |
| 208 |
{ |
| 209 |
g_message ("%s: truncated data in external channel", self->logname); |
| 210 |
cockpit_web_response_abort (self->response); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
g_debug ("%s: completed serving external channel", self->logname); |
| 215 |
} |
| 216 |
} |
| 217 |
else if (state == COCKPIT_WEB_RESPONSE_READY) |
| 218 |
{ |
| 219 |
if (g_str_equal (problem, "not-found")) |
| 220 |
{ |
| 221 |
g_debug ("%s: not found", self->logname); |
| 222 |
cockpit_web_response_error (self->response, 404, NULL, NULL); |
| 223 |
} |
| 224 |
else if (g_str_equal (problem, "access-denied")) |
| 225 |
{ |
| 226 |
g_debug ("%s: forbidden", self->logname); |
| 227 |
cockpit_web_response_error (self->response, 403, NULL, NULL); |
| 228 |
} |
| 229 |
else if (g_str_equal (problem, "no-host") || |
| 230 |
g_str_equal (problem, "no-cockpit") || |
| 231 |
g_str_equal (problem, "unknown-hostkey") || |
| 232 |
g_str_equal (problem, "unknown-host") || |
| 233 |
g_str_equal (problem, "authentication-failed") || |
| 234 |
g_str_equal (problem, "disconnected")) |
| 235 |
{ |
| 236 |
g_debug ("%s: remote server unavailable: %s", self->logname, problem); |
| 237 |
cockpit_web_response_error (self->response, 502, NULL, "%s", problem); |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
g_message ("%s: external channel failed: %s", self->logname, problem); |
| 242 |
cockpit_web_response_error (self->response, 500, NULL, "%s", problem); |
| 243 |
} |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
if (g_str_equal (problem, "disconnected") || g_str_equal (problem, "terminated")) |
| 248 |
g_debug ("%s: failure while serving external channel: %s", self->logname, problem); |
| 249 |
else |
| 250 |
g_message ("%s: failure while serving external channel: %s", self->logname, problem); |
| 251 |
if (state < COCKPIT_WEB_RESPONSE_COMPLETE) |
| 252 |
cockpit_web_response_abort (self->response); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
object_to_headers (JsonObject *object, |
| 258 |
const gchar *header, |
| 259 |
JsonNode *node, |
| 260 |
gpointer user_data) |
| 261 |
{ |
| 262 |
GHashTable *headers = user_data; |
| 263 |
const gchar *value = json_node_get_string (node); |
| 264 |
|
| 265 |
g_return_if_fail (value != NULL); |
| 266 |
|
| 267 |
/* Remove hop-by-hop headers. See RFC 2068 */ |
| 268 |
if (g_ascii_strcasecmp (header, "Connection") == 0 || |
| 269 |
g_ascii_strcasecmp (header, "Keep-Alive") == 0 || |
| 270 |
g_ascii_strcasecmp (header, "Public") == 0 || |
| 271 |
g_ascii_strcasecmp (header, "Proxy-Authenticate") == 0 || |
| 272 |
g_ascii_strcasecmp (header, "Transfer-Encoding") == 0 || |
| 273 |
g_ascii_strcasecmp (header, "Upgrade") == 0) |
| 274 |
return; |
| 275 |
|
| 276 |
g_hash_table_insert (headers, g_strdup (header), g_strdup (value)); |
| 277 |
} |
| 278 |
|
| 279 |
static gboolean |
| 280 |
parse_httpstream_response (CockpitChannelResponse *self, |
| 281 |
JsonObject *object, |
| 282 |
gint64 *status, |
| 283 |
const gchar **reason, |
| 284 |
gssize *length) |
| 285 |
{ |
| 286 |
const gchar *content_length = NULL; |
| 287 |
JsonNode *node; |
| 288 |
|
| 289 |
if (!cockpit_json_get_int (object, "status", 200, status) || |
| 290 |
!cockpit_json_get_string (object, "reason", NULL, reason)) |
| 291 |
{ |
| 292 |
g_warning ("%s: received invalid httpstream response", self->logname); |
| 293 |
return FALSE; |
| 294 |
} |
| 295 |
|
| 296 |
node = json_object_get_member (object, "headers"); |
| 297 |
if (node) |
| 298 |
{ |
| 299 |
if (!JSON_NODE_HOLDS_OBJECT (node)) |
| 300 |
{ |
| 301 |
g_warning ("%s: received invalid httpstream headers", self->logname); |
| 302 |
return FALSE; |
| 303 |
} |
| 304 |
json_object_foreach_member (json_node_get_object (node), object_to_headers, self->headers); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/* Default to unknown length */ |
| 309 |
*length = -1; |
| 310 |
|
| 311 |
content_length = g_hash_table_lookup (self->headers, "Content-Length"); |
| 312 |
if (content_length) |
| 313 |
{ |
| 314 |
gchar *endptr = NULL; |
| 315 |
gint64 result = g_ascii_strtoll (content_length, &endptr, 10); |
| 316 |
if (result > 0 && result <= G_MAXSIZE && endptr && *endptr == '\0') |
| 317 |
*length = (gssize)result; |
| 318 |
|
| 319 |
/* We don't relay Content-Length directly, but expect CockpitWebResponse to set it again */ |
| 320 |
g_hash_table_remove (self->headers, "Content-Length"); |
| 321 |
} |
| 322 |
|
| 323 |
return TRUE; |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
process_httpstream1_recv (CockpitChannelResponse *self, |
| 328 |
GBytes *payload) |
| 329 |
{ |
| 330 |
GError *error = NULL; |
| 331 |
JsonObject *object; |
| 332 |
gint64 status; |
| 333 |
const gchar *reason; |
| 334 |
gssize length; |
| 335 |
|
| 336 |
g_return_if_fail (cockpit_web_response_get_state (self->response) == COCKPIT_WEB_RESPONSE_READY); |
| 337 |
|
| 338 |
object = cockpit_json_parse_bytes (payload, &error); |
| 339 |
if (error) |
| 340 |
{ |
| 341 |
g_warning ("%s: couldn't parse http-stream1 header payload: %s", self->logname, error->message); |
| 342 |
cockpit_web_response_error (self->response, 500, NULL, NULL); |
| 343 |
g_error_free (error); |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
if (parse_httpstream_response (self, object, &status, &reason, &length)) |
| 348 |
{ |
| 349 |
if (!ensure_headers (self, status, reason, length)) |
| 350 |
g_return_if_reached (); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
cockpit_web_response_error (self->response, 500, NULL, NULL); |
| 355 |
} |
| 356 |
|
| 357 |
json_object_unref (object); |
| 358 |
} |
| 359 |
|
| 360 |
static void |
| 361 |
cockpit_channel_response_recv (CockpitChannel *channel, |
| 362 |
GBytes *payload) |
| 363 |
{ |
| 364 |
CockpitChannelResponse *self = COCKPIT_CHANNEL_RESPONSE (channel); |
| 365 |
|
| 366 |
/* First response payload message is meta data, then switch to actual data */ |
| 367 |
if (self->http_stream1_prefix) |
| 368 |
{ |
| 369 |
process_httpstream1_recv (self, payload); |
| 370 |
self->http_stream1_prefix = FALSE; |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
ensure_headers (self, 200, "OK", -1); |
| 375 |
cockpit_web_response_queue (self->response, payload); |
| 376 |
} |
| 377 |
|
| 378 |
static gboolean |
| 379 |
cockpit_channel_response_control (CockpitChannel *channel, |
| 380 |
const gchar *command, |
| 381 |
JsonObject *options) |
| 382 |
{ |
| 383 |
CockpitChannelResponse *self = COCKPIT_CHANNEL_RESPONSE (channel); |
| 384 |
gint64 status; |
| 385 |
const gchar *reason; |
| 386 |
gssize length; |
| 387 |
|
| 388 |
if (self->http_stream2) |
| 389 |
{ |
| 390 |
if (g_str_equal (command, "response")) |
| 391 |
{ |
| 392 |
if (parse_httpstream_response (self, options, &status, &reason, &length)) |
| 393 |
{ |
| 394 |
if (!ensure_headers (self, status, reason, length)) |
| 395 |
g_return_val_if_reached (FALSE); |
| 396 |
} |
| 397 |
else |
| 398 |
{ |
| 399 |
cockpit_web_response_error (self->response, 500, NULL, NULL); |
| 400 |
} |
| 401 |
return TRUE; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if (g_str_equal (command, "ready")) |
| 406 |
{ |
| 407 |
gint64 content_length; |
| 408 |
if (cockpit_json_get_int (options, "size-hint", -1, &content_length) && content_length != -1) |
| 409 |
ensure_headers (self, 200, "OK", content_length); |
| 410 |
|
| 411 |
return TRUE; |
| 412 |
} |
| 413 |
|
| 414 |
if (g_str_equal (command, "done")) |
| 415 |
{ |
| 416 |
ensure_headers (self, 200, "OK", 0); |
| 417 |
cockpit_web_response_complete (self->response); |
| 418 |
return TRUE; |
| 419 |
} |
| 420 |
|
| 421 |
return FALSE; |
| 422 |
} |
| 423 |
|
| 424 |
static void |
| 425 |
cockpit_channel_response_prepare (CockpitChannel *channel) |
| 426 |
{ |
| 427 |
CockpitChannelResponse *self = COCKPIT_CHANNEL_RESPONSE (channel); |
| 428 |
const gchar *payload; |
| 429 |
JsonObject *open; |
| 430 |
|
| 431 |
COCKPIT_CHANNEL_CLASS (cockpit_channel_response_parent_class)->prepare (channel); |
| 432 |
|
| 433 |
/* |
| 434 |
* Tell the transport to throttle incoming flow on the given channel based on |
| 435 |
* output pressure in the web response. |
| 436 |
*/ |
| 437 |
cockpit_flow_throttle (COCKPIT_FLOW (channel), COCKPIT_FLOW (self->response)); |
| 438 |
|
| 439 |
open = cockpit_channel_get_options (channel); |
| 440 |
cockpit_json_get_string (open, "path", NULL, &self->logname); |
| 441 |
if (!self->logname) |
| 442 |
self->logname = cockpit_channel_get_id (channel); |
| 443 |
|
| 444 |
payload = json_object_get_string_member (open, "payload"); |
| 445 |
|
| 446 |
/* Special handling for http-stream1, splice in headers, handle injection */ |
| 447 |
if (g_strcmp0 (payload, "http-stream1") == 0) |
| 448 |
self->http_stream1_prefix = TRUE; |
| 449 |
|
| 450 |
/* Special handling for http-stream2, splice in headers, handle injection */ |
| 451 |
if (g_strcmp0 (payload, "http-stream2") == 0) |
| 452 |
self->http_stream2 = TRUE; |
| 453 |
|
| 454 |
/* Send the open message across the transport */ |
| 455 |
cockpit_channel_control (channel, "open", open); |
| 456 |
|
| 457 |
/* Tell the channel we're ready */ |
| 458 |
cockpit_channel_ready (channel, NULL); |
| 459 |
|
| 460 |
/* Indicate we are done sending input, we support no POST or PUT */ |
| 461 |
cockpit_channel_control (channel, "done", NULL); |
| 462 |
} |
| 463 |
|
| 464 |
static void |
| 465 |
cockpit_channel_response_class_init (CockpitChannelResponseClass *klass) |
| 466 |
{ |
| 467 |
CockpitChannelClass *channel_class = COCKPIT_CHANNEL_CLASS (klass); |
| 468 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 469 |
|
| 470 |
object_class->finalize = cockpit_channel_response_finalize; |
| 471 |
|
| 472 |
channel_class->prepare = cockpit_channel_response_prepare; |
| 473 |
channel_class->recv = cockpit_channel_response_recv; |
| 474 |
channel_class->control = cockpit_channel_response_control; |
| 475 |
channel_class->close = cockpit_channel_response_close; |
| 476 |
} |
| 477 |
|
| 478 |
static CockpitChannelResponse * |
| 479 |
cockpit_channel_response_new (CockpitWebService *service, |
| 480 |
CockpitWebResponse *response, |
| 481 |
CockpitTransport *transport, |
| 482 |
GHashTable *headers, |
| 483 |
JsonObject *options) |
| 484 |
{ |
| 485 |
CockpitChannelResponse *self; |
| 486 |
gchar *id; |
| 487 |
|
| 488 |
id = cockpit_web_service_unique_channel (service); |
| 489 |
self = g_object_new (COCKPIT_TYPE_CHANNEL_RESPONSE, |
| 490 |
"transport", transport, |
| 491 |
"options", options, |
| 492 |
"id", id, |
| 493 |
NULL); |
| 494 |
|
| 495 |
self->response = g_object_ref (response); |
| 496 |
self->headers = g_hash_table_ref (headers); |
| 497 |
|
| 498 |
g_free (id); |
| 499 |
return self; |
| 500 |
} |
| 501 |
|
| 502 |
static gboolean |
| 503 |
is_resource_a_package_file (const gchar *path) |
| 504 |
{ |
| 505 |
return path && path[0] && strchr (path + 1, '/') != NULL; |
| 506 |
} |
| 507 |
|
| 508 |
static gboolean |
| 509 |
parse_host_and_etag (CockpitWebService *service, |
| 510 |
GHashTable *headers, |
| 511 |
const gchar *where, |
| 512 |
const gchar *path, |
| 513 |
const gchar **host, |
| 514 |
gchar **etag) |
| 515 |
{ |
| 516 |
const gchar *accept = NULL; |
| 517 |
gchar **languages = NULL; |
| 518 |
gboolean translatable; |
| 519 |
gchar *language; |
| 520 |
|
| 521 |
/* Parse the language out of the CockpitLang cookie and set Accept-Language */ |
| 522 |
language = cockpit_web_server_parse_cookie (headers, "CockpitLang"); |
| 523 |
if (language) |
| 524 |
g_hash_table_replace (headers, g_strdup ("Accept-Language"), language); |
| 525 |
|
| 526 |
if (!where) |
| 527 |
{ |
| 528 |
*host = "localhost"; |
| 529 |
*etag = NULL; |
| 530 |
return TRUE; |
| 531 |
} |
| 532 |
if (where[0] == '@') |
| 533 |
{ |
| 534 |
*host = where + 1; |
| 535 |
*etag = NULL; |
| 536 |
return TRUE; |
| 537 |
} |
| 538 |
|
| 539 |
if (!where || where[0] != '$') |
| 540 |
return FALSE; |
| 541 |
|
| 542 |
*host = cockpit_web_service_get_host (service, where + 1); |
| 543 |
if (!*host) |
| 544 |
return FALSE; |
| 545 |
|
| 546 |
/* Top level resources (like the /manifests) are not translatable */ |
| 547 |
translatable = is_resource_a_package_file (path); |
| 548 |
|
| 549 |
/* The ETag contains the language setting */ |
| 550 |
if (translatable) |
| 551 |
{ |
| 552 |
accept = g_hash_table_lookup (headers, "Accept-Language"); |
| 553 |
languages = cockpit_web_server_parse_accept_list (accept, "C"); |
| 554 |
*etag = g_strdup_printf ("\"%s-%s\"", where, languages[0]); |
| 555 |
g_strfreev (languages); |
| 556 |
} |
| 557 |
else |
| 558 |
{ |
| 559 |
*etag = g_strdup_printf ("\"%s\"", where); |
| 560 |
} |
| 561 |
|
| 562 |
return TRUE; |
| 563 |
} |
| 564 |
|
| 565 |
void |
| 566 |
cockpit_channel_response_serve (CockpitWebService *service, |
| 567 |
GHashTable *in_headers, |
| 568 |
CockpitWebResponse *response, |
| 569 |
const gchar *where, |
| 570 |
const gchar *path) |
| 571 |
{ |
| 572 |
CockpitChannelResponse *self = NULL; |
| 573 |
CockpitTransport *transport = NULL; |
| 574 |
CockpitCacheType cache_type = COCKPIT_WEB_RESPONSE_CACHE; |
| 575 |
const gchar *injecting_base_path = NULL; |
| 576 |
const gchar *host = NULL; |
| 577 |
const gchar *pragma; |
| 578 |
gchar *quoted_etag = NULL; |
| 579 |
GHashTable *out_headers = NULL; |
| 580 |
gchar *val = NULL; |
| 581 |
gboolean handled = FALSE; |
| 582 |
GHashTableIter iter; |
| 583 |
JsonObject *object = NULL; |
| 584 |
JsonObject *heads; |
| 585 |
const gchar *protocol; |
| 586 |
const gchar *http_host = "localhost"; |
| 587 |
gchar *channel = NULL; |
| 588 |
gpointer key; |
| 589 |
gpointer value; |
| 590 |
gboolean allow_multihost; |
| 591 |
|
| 592 |
g_return_if_fail (COCKPIT_IS_WEB_SERVICE (service)); |
| 593 |
g_return_if_fail (in_headers != NULL); |
| 594 |
g_return_if_fail (COCKPIT_IS_WEB_RESPONSE (response)); |
| 595 |
g_return_if_fail (path != NULL); |
| 596 |
|
| 597 |
/* Where might be NULL, but that's still valid */ |
| 598 |
if (!parse_host_and_etag (service, in_headers, where, path, &host, "ed_etag)) |
| 599 |
{ |
| 600 |
/* Did not recognize the where */ |
| 601 |
goto out; |
| 602 |
} |
| 603 |
|
| 604 |
allow_multihost = cockpit_conf_bool ("WebService", "AllowMultiHost", ALLOW_MULTIHOST_DEFAULT); |
| 605 |
if (!allow_multihost && g_strcmp0 (host, "localhost") != 0) |
| 606 |
{ |
| 607 |
cockpit_web_response_error (response, 403, NULL, NULL); |
| 608 |
handled = TRUE; |
| 609 |
goto out; |
| 610 |
} |
| 611 |
|
| 612 |
if (quoted_etag) |
| 613 |
{ |
| 614 |
cache_type = COCKPIT_WEB_RESPONSE_CACHE; |
| 615 |
pragma = g_hash_table_lookup (in_headers, "Pragma"); |
| 616 |
|
| 617 |
if ((!pragma || !strstr (pragma, "no-cache")) && |
| 618 |
g_strcmp0 (g_hash_table_lookup (in_headers, "If-None-Match"), quoted_etag) == 0) |
| 619 |
{ |
| 620 |
cockpit_web_response_headers (response, 304, "Not Modified", 0, "ETag", quoted_etag, NULL); |
| 621 |
cockpit_web_response_complete (response); |
| 622 |
handled = TRUE; |
| 623 |
goto out; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
cockpit_web_response_set_cache_type (response, cache_type); |
| 628 |
object = cockpit_transport_build_json ("command", "open", |
| 629 |
"payload", "http-stream1", |
| 630 |
"internal", "packages", |
| 631 |
"method", "GET", |
| 632 |
"host", host, |
| 633 |
"path", path, |
| 634 |
"binary", "raw", |
| 635 |
NULL); |
| 636 |
|
| 637 |
transport = cockpit_web_service_get_transport (service); |
| 638 |
if (!transport) |
| 639 |
goto out; |
| 640 |
|
| 641 |
out_headers = cockpit_web_server_new_table (); |
| 642 |
|
| 643 |
channel = cockpit_web_service_unique_channel (service); |
| 644 |
json_object_set_string_member (object, "channel", channel); |
| 645 |
json_object_set_boolean_member (object, "flow-control", TRUE); |
| 646 |
|
| 647 |
if (quoted_etag) |
| 648 |
{ |
| 649 |
/* |
| 650 |
* If we have a checksum, then use it as an ETag. It is intentional that |
| 651 |
* a cockpit-bridge version could (in the future) override this. |
| 652 |
*/ |
| 653 |
g_hash_table_insert (out_headers, g_strdup ("ETag"), quoted_etag); |
| 654 |
quoted_etag = NULL; |
| 655 |
} |
| 656 |
|
| 657 |
heads = json_object_new (); |
| 658 |
|
| 659 |
g_hash_table_iter_init (&iter, in_headers); |
| 660 |
while (g_hash_table_iter_next (&iter, &key, &value)) |
| 661 |
{ |
| 662 |
val = NULL; |
| 663 |
|
| 664 |
if (g_ascii_strcasecmp (key, "Cookie") == 0 || |
| 665 |
g_ascii_strcasecmp (key, "Referer") == 0 || |
| 666 |
g_ascii_strcasecmp (key, "Connection") == 0 || |
| 667 |
g_ascii_strcasecmp (key, "Pragma") == 0 || |
| 668 |
g_ascii_strcasecmp (key, "Cache-Control") == 0 || |
| 669 |
g_ascii_strcasecmp (key, "User-Agent") == 0 || |
| 670 |
g_ascii_strcasecmp (key, "Accept-Charset") == 0 || |
| 671 |
g_ascii_strcasecmp (key, "Accept-Ranges") == 0 || |
| 672 |
g_ascii_strcasecmp (key, "Content-Length") == 0 || |
| 673 |
g_ascii_strcasecmp (key, "Content-MD5") == 0 || |
| 674 |
g_ascii_strcasecmp (key, "Content-Range") == 0 || |
| 675 |
g_ascii_strcasecmp (key, "Range") == 0 || |
| 676 |
g_ascii_strcasecmp (key, "TE") == 0 || |
| 677 |
g_ascii_strcasecmp (key, "Trailer") == 0 || |
| 678 |
g_ascii_strcasecmp (key, "Upgrade") == 0 || |
| 679 |
g_ascii_strcasecmp (key, "Transfer-Encoding") == 0 || |
| 680 |
g_ascii_strcasecmp (key, "X-Forwarded-For") == 0 || |
| 681 |
g_ascii_strcasecmp (key, "X-Forwarded-Host") == 0 || |
| 682 |
g_ascii_strcasecmp (key, "X-Forwarded-Protocol") == 0) |
| 683 |
continue; |
| 684 |
|
| 685 |
if (g_ascii_strcasecmp (key, "Host") == 0) |
| 686 |
http_host = (gchar *) value; |
| 687 |
else |
| 688 |
json_object_set_string_member (heads, key, value); |
| 689 |
|
| 690 |
g_free (val); |
| 691 |
} |
| 692 |
|
| 693 |
/* Send along the HTTP scheme the package should assume is accessing things */ |
| 694 |
protocol = cockpit_web_response_get_protocol (response); |
| 695 |
|
| 696 |
json_object_set_string_member (heads, "Host", host); |
| 697 |
json_object_set_string_member (heads, "X-Forwarded-Proto", protocol); |
| 698 |
json_object_set_string_member (heads, "X-Forwarded-Host", http_host); |
| 699 |
|
| 700 |
/* We only inject a <base> if root level request */ |
| 701 |
injecting_base_path = where ? NULL : path; |
| 702 |
if (injecting_base_path) |
| 703 |
{ |
| 704 |
/* If we are injecting a <base> element, then we don't allow gzip compression */ |
| 705 |
json_object_set_string_member (heads, "Accept-Encoding", "identity"); |
| 706 |
} |
| 707 |
|
| 708 |
json_object_set_object_member (object, "headers", heads); |
| 709 |
|
| 710 |
self = cockpit_channel_response_new (service, response, transport, |
| 711 |
out_headers, object); |
| 712 |
|
| 713 |
self->inject = cockpit_channel_inject_new (service, injecting_base_path, host); |
| 714 |
handled = TRUE; |
| 715 |
|
| 716 |
/* Unref when the channel closes */ |
| 717 |
g_signal_connect_after (self, "closed", G_CALLBACK (g_object_unref), NULL); |
| 718 |
|
| 719 |
out: |
| 720 |
if (object) |
| 721 |
json_object_unref (object); |
| 722 |
g_free (quoted_etag); |
| 723 |
if (out_headers) |
| 724 |
g_hash_table_unref (out_headers); |
| 725 |
g_free (channel); |
| 726 |
|
| 727 |
if (!handled) |
| 728 |
cockpit_web_response_error (response, 404, NULL, NULL); |
| 729 |
} |
| 730 |
|
| 731 |
void |
| 732 |
cockpit_channel_response_open (CockpitWebService *service, |
| 733 |
CockpitWebRequest *request, |
| 734 |
JsonObject *open) |
| 735 |
{ |
| 736 |
CockpitChannelResponse *self; |
| 737 |
CockpitTransport *transport; |
| 738 |
WebSocketDataType data_type; |
| 739 |
GHashTable *headers; |
| 740 |
const gchar *content_type; |
| 741 |
const gchar *content_encoding; |
| 742 |
const gchar *content_disposition; |
| 743 |
|
| 744 |
g_autoptr(CockpitWebResponse) response = cockpit_web_request_respond (request); |
| 745 |
|
| 746 |
/* Parse the external */ |
| 747 |
if (!cockpit_web_service_parse_external (open, &content_type, &content_encoding, &content_disposition, NULL)) |
| 748 |
{ |
| 749 |
cockpit_web_response_error (response, 400, NULL, "Bad channel request"); |
| 750 |
return; |
| 751 |
} |
| 752 |
|
| 753 |
transport = cockpit_web_service_get_transport (service); |
| 754 |
if (!transport) |
| 755 |
{ |
| 756 |
cockpit_web_response_error (response, 502, NULL, "Failed to open channel transport"); |
| 757 |
return; |
| 758 |
} |
| 759 |
|
| 760 |
headers = cockpit_web_server_new_table (); |
| 761 |
|
| 762 |
if (content_disposition) |
| 763 |
g_hash_table_insert (headers, g_strdup ("Content-Disposition"), g_strdup (content_disposition)); |
| 764 |
|
| 765 |
if (!json_object_has_member (open, "binary")) |
| 766 |
json_object_set_string_member (open, "binary", "raw"); |
| 767 |
|
| 768 |
json_object_set_boolean_member (open, "flow-control", TRUE); |
| 769 |
|
| 770 |
if (!content_type) |
| 771 |
{ |
| 772 |
if (!cockpit_web_service_parse_binary (open, &data_type)) |
| 773 |
{ |
| 774 |
cockpit_web_response_error (response, 400, NULL, "Bad channel request"); |
| 775 |
g_hash_table_unref (headers); |
| 776 |
return; |
| 777 |
} |
| 778 |
if (data_type == WEB_SOCKET_DATA_TEXT) |
| 779 |
content_type = "text/plain"; |
| 780 |
else |
| 781 |
content_type = "application/octet-stream"; |
| 782 |
} |
| 783 |
g_hash_table_insert (headers, g_strdup ("Content-Type"), g_strdup (content_type)); |
| 784 |
|
| 785 |
if (content_encoding) |
| 786 |
g_hash_table_insert (headers, g_strdup ("Content-Encoding"), g_strdup (content_encoding)); |
| 787 |
|
| 788 |
/* We shouldn't need to send this part further */ |
| 789 |
json_object_remove_member (open, "external"); |
| 790 |
|
| 791 |
self = cockpit_channel_response_new (service, response, transport, headers, open); |
| 792 |
g_hash_table_unref (headers); |
| 793 |
|
| 794 |
/* Unref when the channel closes */ |
| 795 |
g_signal_connect_after (self, "closed", G_CALLBACK (g_object_unref), NULL); |
| 796 |
} |