From 1233923c3a0f079a971f1513ccf094f746b4dac4 Mon Sep 17 00:00:00 2001 From: Stas Kelvich Date: Sat, 15 Apr 2023 02:18:52 +0300 Subject: [PATCH] works with browser --- Cargo.toml | 2 +- proxy/Cargo.toml | 2 +- proxy/src/http/websocket.rs | 16 +++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90271aedc2..9811bc1d2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ hmac = "0.12.1" hostname = "0.3.1" humantime = "2.1" humantime-serde = "1.1.1" -hyper = "0.14" +hyper = { version = "0.14", features = ["http2", "tcp", "runtime", "http1"]} hyper-tungstenite = "0.9" itertools = "0.10" jsonwebtoken = "8" diff --git a/proxy/Cargo.toml b/proxy/Cargo.toml index e3907da1f0..14dff6aa76 100644 --- a/proxy/Cargo.toml +++ b/proxy/Cargo.toml @@ -23,7 +23,7 @@ hmac.workspace = true hostname.workspace = true humantime.workspace = true hyper-tungstenite.workspace = true -hyper.workspace = true +hyper = { workspace = true, features = ["http2", "http1", "tcp", "runtime"] } itertools.workspace = true md5.workspace = true metrics.workspace = true diff --git a/proxy/src/http/websocket.rs b/proxy/src/http/websocket.rs index c5ab0045d0..5579b79bf5 100644 --- a/proxy/src/http/websocket.rs +++ b/proxy/src/http/websocket.rs @@ -15,6 +15,7 @@ use hyper_tungstenite::{tungstenite::Message, HyperWebsocket, WebSocketStream}; use pin_project_lite::pin_project; use pq_proto::StartupMessageParams; +use percent_encoding::percent_decode; use std::{ convert::Infallible, future::ready, @@ -31,7 +32,6 @@ use tokio_util::sync::CancellationToken; use tracing::{error, info, info_span, warn, Instrument}; use url::form_urlencoded; use utils::http::{error::ApiError, json::json_response}; -use percent_encoding::percent_decode; // TODO: use `std::sync::Exclusive` once it's stabilized. // Tracking issue: https://github.com/rust-lang/rust/issues/98407. @@ -190,7 +190,13 @@ async fn ws_handler( Ok(response) } else if request.uri().path() == "/sql" { match handle_sql(config, request).await { - Ok(resp) => json_response(StatusCode::OK, resp), + Ok(resp) => json_response(StatusCode::OK, resp).map(|mut r| { + r.headers_mut().insert( + "Access-Control-Allow-Origin", + hyper::http::HeaderValue::from_static("*"), + ); + r + }), Err(e) => json_response(StatusCode::BAD_REQUEST, format!("error: {e:?}")), } } else { @@ -272,11 +278,7 @@ async fn handle_sql( info!("!!!! connecting to: {}", conn_string); - let (client, connection) = tokio_postgres::connect( - conn_string, - tokio_postgres::NoTls, - ) - .await?; + let (client, connection) = tokio_postgres::connect(conn_string, tokio_postgres::NoTls).await?; tokio::spawn(async move { if let Err(e) = connection.await {