Skip to content

Commit

Permalink
fix(junowen): url encode
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed Dec 9, 2023
1 parent 03ed7be commit 9d3f3d3
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 29 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions junowen-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ time = "0.3.29"
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros", "time"] }
tracing.workspace = true
tracing-subscriber.workspace = true
urlencoding = "2.1.3"
uuid = "1.5.0"

[target.x86_64-unknown-linux-gnu.dependencies]
Expand Down
31 changes: 26 additions & 5 deletions junowen-server/src/routes/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
};

use super::{
room_utils::{from_post_room_keep_response, from_put_room_response},
room_utils::{decode_room_name, from_post_room_keep_response, from_put_room_response},
to_response, try_parse,
};

Expand Down Expand Up @@ -163,14 +163,21 @@ pub async fn route(
) -> Result<Response<Body>> {
let regex = Regex::new(r"^([^/]+)$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::PUT => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = put_room(db, &c[1], body).await?;
let res = put_room(db, &room_name, body).await?;
from_put_room_response(res)
}
},
Expand All @@ -180,7 +187,7 @@ pub async fn route(
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = delete_room(db, &c[1], body).await?;
let res = delete_room(db, &room_name, body).await?;
to_response(res.status_code(), Body::Empty)
}
},
Expand All @@ -189,14 +196,21 @@ pub async fn route(
}
let regex = Regex::new(r"^([^/]+)/join$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::POST => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = post_room_join(db, &c[1], body).await?;
let res = post_room_join(db, &room_name, body).await?;
to_response(res.status_code_old(), Body::Empty)
}
},
Expand All @@ -205,14 +219,21 @@ pub async fn route(
}
let regex = Regex::new(r"^([^/]+)/keep$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::POST => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = post_room_keep(db, &c[1], body).await?;
let res = post_room_keep(db, &room_name, body).await?;
from_post_room_keep_response(res)
}
},
Expand Down
42 changes: 35 additions & 7 deletions junowen-server/src/routes/reserved_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use self::{
};

use super::{
room_utils::{from_post_room_keep_response, from_put_room_response},
room_utils::{decode_room_name, from_post_room_keep_response, from_put_room_response},
to_response, try_parse,
};

Expand All @@ -32,19 +32,26 @@ pub async fn route(
) -> Result<Response<Body>> {
let regex = Regex::new(r"^([^/]+)$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::PUT => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = put_room(db, &c[1], body).await?;
let res = put_room(db, &room_name, body).await?;
from_put_room_response(res)
}
},
Method::GET => {
let res = get_room(db, &c[1]).await?;
let res = get_room(db, &room_name).await?;
to_response(
res.status_code(),
res.to_body().map(Body::Text).unwrap_or_else(|| Body::Empty),
Expand All @@ -56,7 +63,7 @@ pub async fn route(
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = delete_room(db, &c[1], body).await?;
let res = delete_room(db, &room_name, body).await?;
to_response(res.status_code(), Body::Empty)
}
},
Expand All @@ -65,14 +72,21 @@ pub async fn route(
}
let regex = Regex::new(r"^([^/]+)/join$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::POST => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = post_room_join(db, &c[1], body).await?;
let res = post_room_join(db, &room_name, body).await?;
to_response(res.status_code_old(), Body::Empty)
}
},
Expand All @@ -81,14 +95,21 @@ pub async fn route(
}
let regex = Regex::new(r"^([^/]+)/spectate$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::POST => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = post_room_spectate(db, &c[1], body).await?;
let res = post_room_spectate(db, &room_name, body).await?;
to_response(res.status_code_old(), Body::Empty)
}
},
Expand All @@ -97,14 +118,21 @@ pub async fn route(
}
let regex = Regex::new(r"^([^/]+)/keep$").unwrap();
if let Some(c) = regex.captures(relative_uri) {
let room_name = match decode_room_name(&c[1]) {
Ok(room_name) => room_name,
Err(err) => {
debug!("{:?}", err);
return Ok(to_response(StatusCode::NOT_FOUND, Body::Empty));
}
};
return Ok(match *req.method() {
Method::POST => match try_parse(req.body()) {
Err(err) => {
debug!("{:?}", err);
to_response(StatusCode::BAD_REQUEST, Body::Empty)
}
Ok(body) => {
let res = post_room_keep(db, &c[1], body).await?;
let res = post_room_keep(db, &room_name, body).await?;
from_post_room_keep_response(res)
}
},
Expand Down
9 changes: 8 additions & 1 deletion junowen-server/src/routes/room_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
string::FromUtf8Error,
time::{SystemTime, UNIX_EPOCH},
};

use junowen_lib::signaling_server::room::{PostRoomKeepResponse, PutRoomResponse};
use lambda_http::{Body, Response};
Expand Down Expand Up @@ -47,3 +50,7 @@ where
};
to_response(status_code, body)
}

pub fn decode_room_name(encoded_room_name: &str) -> Result<String, FromUtf8Error> {
urlencoding::decode(&encoded_room_name.replace('+', "%20")).map(|x| x.to_string())
}
1 change: 1 addition & 0 deletions junowen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ toml_edit = "0.21.0"
tracing.workspace = true
tracing-appender = "0.2.2"
tracing-subscriber.workspace = true
urlencoding = "2.1.3"
webrtc = "0.9.0"
windows = { version = "0.52.0", features = [
"Win32_Foundation",
Expand Down
4 changes: 4 additions & 0 deletions junowen/src/signaling/waiting_for_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub use waiting_in_room::{
WaitingForSpectatorHostInReservedRoom, WaitingInRoom,
};

fn encode_room_name(room_name: &str) -> String {
urlencoding::encode(room_name).replace("%20", "+")
}

#[derive(new)]
pub struct WaitingForPureP2pOpponent {
battle_session_rx: mpsc::Receiver<BattleSession>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use junowen_lib::{
use tokio::sync::watch;
use tracing::info;

use super::socket::{retry_after, sleep_or_abort_and_delete_room};
use super::{
encode_room_name,
socket::{retry_after, sleep_or_abort_and_delete_room},
};

pub struct SignalingServerReservedRoomOpponentSocket {
client: reqwest::Client,
Expand All @@ -28,10 +31,11 @@ pub struct SignalingServerReservedRoomOpponentSocket {
}

impl SignalingServerReservedRoomOpponentSocket {
pub fn new(origin: String, room_name: String, abort_rx: watch::Receiver<bool>) -> Self {
pub fn new(origin: String, room_name: &str, abort_rx: watch::Receiver<bool>) -> Self {
let encoded_room_name = encode_room_name(room_name);
Self {
client: reqwest::Client::new(),
resource_url: format!("{}/reserved-room/{}", origin, room_name),
resource_url: format!("{}/reserved-room/{}", origin, encoded_room_name),
key: None,
abort_rx,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tracing::info;

use crate::signaling::waiting_for_match::socket::retry_after;

use super::socket::sleep_or_abort_and_delete_room;
use super::{socket::sleep_or_abort_and_delete_room, encode_room_name};

pub struct SignalingServerReservedRoomSpectatorHostSocket {
client: reqwest::Client,
Expand All @@ -30,13 +30,14 @@ pub struct SignalingServerReservedRoomSpectatorHostSocket {
impl SignalingServerReservedRoomSpectatorHostSocket {
pub fn new(
origin: String,
room_name: String,
room_name: &str,
key: String,
abort_rx: watch::Receiver<bool>,
) -> Self {
let encoded_room_name = encode_room_name(room_name);
Self {
client: reqwest::Client::new(),
resource_url: format!("{}/reserved-room/{}", origin, room_name),
resource_url: format!("{}/reserved-room/{}", origin, encoded_room_name),
key,
abort_rx,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use thiserror::Error;
use tokio::sync::watch;
use tracing::info;

use super::socket::{retry_after, sleep_or_abort};
use super::{
encode_room_name,
socket::{retry_after, sleep_or_abort},
};

#[derive(Error, Debug)]
pub enum SignalingServerReservedRoomSpectatorSocketError {
Expand All @@ -33,10 +36,11 @@ pub struct SignalingServerReservedRoomSpectatorSocket {
}

impl SignalingServerReservedRoomSpectatorSocket {
pub fn new(origin: String, room_name: String, abort_rx: watch::Receiver<bool>) -> Self {
pub fn new(origin: String, room_name: &str, abort_rx: watch::Receiver<bool>) -> Self {
let encoded_room_name = encode_room_name(room_name);
Self {
client: reqwest::Client::new(),
resource_url: format!("{}/reserved-room/{}", origin, room_name),
resource_url: format!("{}/reserved-room/{}", origin, encoded_room_name),
abort_rx,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use junowen_lib::{
use tokio::sync::watch;
use tracing::info;

use super::socket::{retry_after, sleep_or_abort_and_delete_room};
use super::{
encode_room_name,
socket::{retry_after, sleep_or_abort_and_delete_room},
};

pub struct SignalingServerSharedRoomOpponentSocket {
client: reqwest::Client,
Expand All @@ -26,10 +29,11 @@ pub struct SignalingServerSharedRoomOpponentSocket {
}

impl SignalingServerSharedRoomOpponentSocket {
pub fn new(origin: String, room_name: String, abort_rx: watch::Receiver<bool>) -> Self {
pub fn new(origin: String, room_name: &str, abort_rx: watch::Receiver<bool>) -> Self {
let encoded_room_name = encode_room_name(room_name);
Self {
client: reqwest::Client::new(),
resource_url: format!("{}/custom/{}", origin, room_name),
resource_url: format!("{}/custom/{}", origin, encoded_room_name),
abort_rx,
}
}
Expand Down
4 changes: 2 additions & 2 deletions junowen/src/signaling/waiting_for_match/waiting_in_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
TSession: Send + 'static,
{
fn internal_new<T>(
create_socket: impl FnOnce(String, String, watch::Receiver<bool>) -> T + Send + 'static,
create_socket: impl FnOnce(String, &str, watch::Receiver<bool>) -> T + Send + 'static,
create_session: fn(
conn: PeerConnection,
data_channel: DataChannel,
Expand All @@ -78,7 +78,7 @@ where
} else {
"https://wxvo3rgklveqwyig4b3q5qupbq0mgvik.lambda-url.ap-northeast-1.on.aws"
};
let mut socket = create_socket(origin.into(), room_name, abort_rx.clone());
let mut socket = create_socket(origin.into(), &room_name, abort_rx.clone());
let (conn, dc, host) = loop {
match socket.receive_signaling().await {
Ok(ok) => break ok,
Expand Down

0 comments on commit 9d3f3d3

Please sign in to comment.