Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync updates #139

Merged
merged 8 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
826 changes: 469 additions & 357 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions casper-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version = "0.1.0"

[dependencies]
anyhow = "1"
async-trait = "0.1"
base64 = "0.21"
bitflags = "1.3.2"
blake3 = "1.0"
Expand All @@ -20,12 +19,12 @@ futures = "0.3"
futures-util = "0.3"
hex = "0.4.3"
hyper = { version = "0.14.27", features = ["client", "http1", "tcp"] }
itertools = "0.11"
itertools = "0.12"
linked-hash-map = "0.5.4"
log = "0.4"
mini-moka = "0.10"
moka = { version = "0.12", features = ["future"] }
ntex = { version = "0.7.2", features = ["tokio", "openssl"] }
ntex = { version = "1.0.0", features = ["tokio", "openssl"] }
num_cpus = "1.13"
num_threads = "0.1"
once_cell = "1"
Expand Down Expand Up @@ -60,16 +59,16 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
zstd = "0.13"

[dev-dependencies]
serial_test = "2.0"
serial_test = "3.0"
tokio-stream = { version = "0.1", features = ["time"] }

[dependencies.mlua]
features = ["luau-jit", "async", "serialize", "macros", "unstable"]
version = "0.9.1"
version = "0.9.4"

[dependencies.fred]
features = ["enable-native-tls"]
version = "7.0"
version = "7.1.2"

[target.'cfg(target_os = "linux")'.dependencies]
tikv-jemallocator = "0.5"
1 change: 0 additions & 1 deletion casper-server/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ pub async fn buffer_body(mut body: impl MessageBody) -> Result<Bytes, Box<dyn St
}

pub(crate) mod proxy;
pub(crate) mod serde;
pub(crate) mod trace;
pub(crate) mod websocket;
176 changes: 0 additions & 176 deletions casper-server/src/http/serde.rs

This file was deleted.

1 change: 1 addition & 0 deletions casper-server/src/lua/http/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ impl<'lua> FromLua<'lua> for LuaBody {
}
}

#[allow(clippy::await_holding_refcell_ref)]
impl UserData for LuaBody {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
// Static constructor
Expand Down
23 changes: 17 additions & 6 deletions casper-server/src/lua/http/headers.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::collections::{HashMap, HashSet};
use std::ops::{Deref, DerefMut};

use itertools::Itertools;
use mlua::{
AnyUserData, ExternalError, ExternalResult, FromLua, Function, IntoLua, Lua, MetaMethod,
RegistryKey, Result as LuaResult, String as LuaString, Table, UserData, UserDataMethods,
UserDataRef, UserDataRefMut, Value, Variadic,
};
use ntex::http::header::{HeaderMap, HeaderName, HeaderValue};
use serde::ser::Serializer;

use super::super::Regex;
use crate::http::serde::header_map::serialize_sorted as serialize_headers;

pub trait LuaHttpHeadersExt {
/// Returns a first value of the header of Nil if not found.
Expand Down Expand Up @@ -85,8 +86,7 @@ impl<'lua> FromLua<'lua> for LuaHttpHeaders {
Value::Nil => Ok(Self::new()),
Value::Table(table) => {
let mut headers = Self::with_capacity(table.raw_len());
for kv in table.pairs::<String, Value>() {
let (name, value) = kv?;
table.for_each::<String, Value>(|name, value| {
// Maybe `value` is a list of header values
if let Value::Table(values) = value {
let name = HeaderName::from_bytes(name.as_bytes()).into_lua_err()?;
Expand All @@ -103,7 +103,8 @@ impl<'lua> FromLua<'lua> for LuaHttpHeaders {
HeaderValue::from_bytes(value.as_bytes()).into_lua_err()?,
);
}
}
Ok(())
})?;
Ok(headers)
}
Value::UserData(ud) => {
Expand Down Expand Up @@ -336,10 +337,10 @@ impl LuaHttpHeadersExt for HeaderMap {
let mut writer = Vec::new();
if pretty.unwrap_or_default() {
let mut serializer = serde_json::Serializer::pretty(&mut writer);
lua_try!(serialize_headers(self, &mut serializer));
lua_try!(serialize_headers_sorted(self, &mut serializer));
} else {
let mut serializer = serde_json::Serializer::new(&mut writer);
lua_try!(serialize_headers(self, &mut serializer));
lua_try!(serialize_headers_sorted(self, &mut serializer));
}
Ok(Ok(lua.create_string(writer)?))
}
Expand Down Expand Up @@ -404,6 +405,16 @@ fn set_headers_metatable(lua: &Lua, headers: Table) -> LuaResult<()> {
metatable_helper.call(headers)
}

/// Serialize headers sorting them by name.
fn serialize_headers_sorted<S: Serializer>(headers: &HeaderMap, ser: S) -> Result<S::Ok, S::Error> {
ser.collect_map(
headers
.iter_inner()
.map(|(k, v)| (k.as_str(), v))
.sorted_by_key(|(k, _)| *k),
)
}

#[cfg(test)]
mod tests {
use mlua::{chunk, Lua, Result};
Expand Down
12 changes: 7 additions & 5 deletions casper-server/src/lua/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::mem;
use std::net::SocketAddr;
use std::time::Duration;

use futures::future::{self, Ready};
use mlua::{
AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, LuaSerdeExt,
Result as LuaResult, String as LuaString, Table, UserData, UserDataFields, UserDataMethods,
Expand Down Expand Up @@ -168,9 +167,11 @@ impl LuaRequest {
/// Provides an Extractor to make LuaRequest from ntex request
impl<Err> FromRequest<Err> for LuaRequest {
type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>;

fn from_request(request: &HttpRequest, payload: &mut Payload) -> Self::Future {
async fn from_request(
request: &HttpRequest,
payload: &mut Payload,
) -> Result<Self, Self::Error> {
let content_length = request
.headers()
.get(CONTENT_LENGTH)
Expand All @@ -187,7 +188,7 @@ impl<Err> FromRequest<Err> for LuaRequest {
}
};

future::ready(Ok(LuaRequest {
Ok(LuaRequest {
orig_req: Some(request.clone()),
uri: request.uri().clone(),
method: request.method().clone(),
Expand All @@ -196,7 +197,7 @@ impl<Err> FromRequest<Err> for LuaRequest {
body: EitherBody::Body(body),
remote_addr: request.peer_addr(),
timeout: None,
}))
})
}
}

Expand Down Expand Up @@ -249,6 +250,7 @@ impl<'lua> FromLua<'lua> for LuaRequest {
}
}

#[allow(clippy::await_holding_refcell_ref)]
impl UserData for LuaRequest {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("method", |lua, this| this.method().as_str().into_lua(lua));
Expand Down
7 changes: 2 additions & 5 deletions casper-server/src/lua/http/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::future::{ready, Ready};
use std::mem;

use mlua::{
Expand Down Expand Up @@ -168,9 +167,7 @@ where
}

impl Responder for LuaResponse {
type Future = Ready<Response>;

fn respond_to(self, req: &HttpRequest) -> Self::Future {
async fn respond_to(self, req: &HttpRequest) -> Response {
let Self {
status,
headers,
Expand All @@ -191,7 +188,7 @@ impl Responder for LuaResponse {
_ => {}
}

ready(resp.set_body(body.into()))
resp.set_body(body.into())
}
}

Expand Down
Loading
Loading