Fix response content-type on /rpc endpoint (#3918)

Co-authored-by: Raphael Darley <raphael@raphaeldarley.com>
Co-authored-by: Tobie Morgan Hitchcock <tobie@surrealdb.com>
This commit is contained in:
David Bottiau 2024-04-19 21:13:51 +02:00 committed by GitHub
parent 02faf73fef
commit 854e6e6df2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 78 additions and 18 deletions

26
Cargo.lock generated
View file

@ -774,7 +774,7 @@ dependencies = [
"http-body 0.4.6",
"hyper 0.14.28",
"pin-project-lite",
"rustls 0.21.10",
"rustls 0.21.11",
"rustls-pemfile",
"tokio",
"tokio-rustls",
@ -2740,7 +2740,7 @@ dependencies = [
"futures-util",
"http 0.2.12",
"hyper 0.14.28",
"rustls 0.21.10",
"rustls 0.21.11",
"tokio",
"tokio-rustls",
]
@ -3109,7 +3109,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if",
"windows-targets 0.52.4",
"windows-targets 0.48.5",
]
[[package]]
@ -4662,7 +4662,7 @@ dependencies = [
"once_cell",
"percent-encoding",
"pin-project-lite",
"rustls 0.21.10",
"rustls 0.21.11",
"rustls-pemfile",
"serde",
"serde_json",
@ -5117,9 +5117,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.21.10"
version = "0.21.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba"
checksum = "7fecbfb7b1444f477b345853b1fce097a2c6fb637b2bfb87e6bc5db0f043fae4"
dependencies = [
"log",
"ring 0.17.8",
@ -5129,9 +5129,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.22.3"
version = "0.22.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99008d7ad0bbbea527ec27bddbc0e432c5b87d8175178cee68d2eec9c4a1813c"
checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432"
dependencies = [
"log",
"ring 0.17.8",
@ -5858,7 +5858,7 @@ dependencies = [
"revision",
"ring 0.17.8",
"rust_decimal",
"rustls 0.21.10",
"rustls 0.21.11",
"semver",
"serde",
"serde_json",
@ -6418,7 +6418,7 @@ version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081"
dependencies = [
"rustls 0.21.10",
"rustls 0.21.11",
"tokio",
]
@ -6442,7 +6442,7 @@ dependencies = [
"futures-util",
"log",
"native-tls",
"rustls 0.21.10",
"rustls 0.21.11",
"tokio",
"tokio-native-tls",
"tokio-rustls",
@ -6776,7 +6776,7 @@ dependencies = [
"log",
"native-tls",
"rand 0.8.5",
"rustls 0.21.10",
"rustls 0.21.11",
"sha1",
"thiserror",
"url",
@ -6905,7 +6905,7 @@ dependencies = [
"base64 0.21.7",
"log",
"once_cell",
"rustls 0.22.3",
"rustls 0.22.4",
"rustls-pki-types",
"rustls-webpki 0.102.2",
"url",

View file

@ -100,7 +100,7 @@ revision = { version = "0.7.0", features = [
"uuid",
] }
rust_decimal = { version = "1.33.1", features = ["maths", "serde-str"] }
rustls = { version = "0.21.10", optional = true }
rustls = { version = "0.21.11", optional = true }
semver = { version = "1.0.20", features = ["serde"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"

View file

@ -5,7 +5,7 @@ use axum::extract::ws::Message;
use axum::response::IntoResponse;
use axum::response::Response as AxumResponse;
use bytes::Bytes;
use http::StatusCode;
use http::header::{HeaderValue, CONTENT_TYPE};
use surrealdb::rpc::format::Format;
use surrealdb::rpc::request::Request;
use surrealdb::rpc::RpcError;
@ -36,6 +36,19 @@ impl From<&ContentType> for Format {
}
}
impl From<&Format> for ContentType {
fn from(format: &Format) -> Self {
match format {
Format::Json => ContentType::ApplicationJson,
Format::Cbor => ContentType::ApplicationCbor,
Format::Msgpack => ContentType::ApplicationPack,
Format::Unsupported => ContentType::ApplicationOctetStream,
Format::Bincode => ContentType::Surrealdb,
_ => ContentType::TextPlain,
}
}
}
pub trait WsFormat {
fn req_ws(&self, msg: Message) -> Result<Request, Failure>;
fn res_ws(&self, res: Response) -> Result<(usize, Message), Failure>;
@ -75,9 +88,13 @@ impl HttpFormat for Format {
if matches!(self, Format::Json) {
// If this has significant performance overhead it could be replaced with unsafe { String::from_utf8_unchecked(res) }
// This would be safe as in the case of JSON res come from a call to Into::<Vec<u8>> for String
Ok((StatusCode::OK, String::from_utf8(res).unwrap()).into_response())
Ok((
[(CONTENT_TYPE, HeaderValue::from(ContentType::ApplicationJson))],
String::from_utf8(res).unwrap(),
)
.into_response())
} else {
Ok((StatusCode::OK, res).into_response())
Ok(([(CONTENT_TYPE, HeaderValue::from(ContentType::from(self)))], res).into_response())
}
}
}

View file

@ -1,7 +1,15 @@
# cargo-vet audits file
[audits]
[[audits.rustls]]
who = "Tobie Morgan Hitchcock <tobie@surrealdb.com>"
criteria = "safe-to-deploy"
delta = "0.21.10 -> 0.21.11"
[[audits.rustls]]
who = "Tobie Morgan Hitchcock <tobie@surrealdb.com>"
criteria = "safe-to-deploy"
delta = "0.22.3 -> 0.22.4"
[[trusted.addr]]
criteria = "safe-to-deploy"

View file

@ -65,6 +65,13 @@ user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.echodb]]
version = "0.6.0"
when = "2024-04-05"
user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.encoding_rs]]
version = "0.8.33"
when = "2023-08-23"
@ -100,6 +107,13 @@ user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.revision]]
version = "0.7.0"
when = "2024-04-17"
user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.revision-derive]]
version = "0.5.0"
when = "2023-08-29"
@ -107,6 +121,13 @@ user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.revision-derive]]
version = "0.7.0"
when = "2024-04-17"
user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.storekey]]
version = "0.5.0"
when = "2023-04-28"
@ -149,6 +170,13 @@ user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.surrealkv]]
version = "0.1.4"
when = "2024-04-10"
user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.surrealml-core]]
version = "0.1.2"
when = "2024-04-02"
@ -191,6 +219,13 @@ user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[publisher.vart]]
version = "0.2.0"
when = "2024-04-04"
user-id = 145457
user-login = "tobiemh"
user-name = "Tobie Morgan Hitchcock"
[[audits.bytecode-alliance.wildcard-audits.arbitrary]]
who = "Nick Fitzgerald <fitzgen@gmail.com>"
criteria = "safe-to-deploy"