Skip to content

Commit

Permalink
Percent decode paths in scopes (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro authored Jan 3, 2024
1 parent 1d66ef2 commit f3acfc6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ hyper = { version = "=0.14", features = ["http1", "http2", "server", "stream", "
log = "0.4"
mimalloc = { version = "0.1.34", default-features = false, features = ["local_dynamic_tls"] }
once_cell = "1.5"
percent-encoding = "=2.3"
pin-project = "1.0"
pyo3 = { version = "=0.20", features = ["extension-module", "generate-import-lib"] }
pyo3-asyncio = { version = "=0.20", features = ["tokio-runtime"], git = "https://github.com/gi0baro/pyo3-asyncio.git", rev = "6e3a309" }
Expand Down
7 changes: 4 additions & 3 deletions src/asgi/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use hyper::{header::HeaderMap, Uri, Version};
use once_cell::sync::OnceCell;
use percent_encoding::percent_decode_str;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyList, PyString};
use std::net::{IpAddr, SocketAddr};
Expand Down Expand Up @@ -114,7 +115,7 @@ impl ASGIScope {
.path_and_query()
.map_or_else(|| ("", ""), |pq| (pq.path(), pq.query().unwrap_or("")));
(
path,
percent_decode_str(path).decode_utf8().unwrap(),
query_string,
self.py_proto(),
self.py_http_version(),
Expand Down Expand Up @@ -152,10 +153,10 @@ impl ASGIScope {
dict.set_item(pyo3::intern!(py, "scheme"), scheme)?;
dict.set_item(pyo3::intern!(py, "method"), method)?;
dict.set_item(pyo3::intern!(py, "root_path"), url_path_prefix)?;
dict.set_item(pyo3::intern!(py, "path"), path)?;
dict.set_item(pyo3::intern!(py, "path"), &path)?;
dict.set_item(
pyo3::intern!(py, "raw_path"),
PyString::new(py, path).call_method1(pyo3::intern!(py, "encode"), (pyo3::intern!(py, "ascii"),))?,
PyString::new(py, &path).call_method1(pyo3::intern!(py, "encode"), (pyo3::intern!(py, "ascii"),))?,
)?;
dict.set_item(
pyo3::intern!(py, "query_string"),
Expand Down
5 changes: 3 additions & 2 deletions src/rsgi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use hyper::{
header::{HeaderMap, HeaderName, HeaderValue, SERVER as HK_SERVER},
Body, Uri, Version,
};
use percent_encoding::percent_decode_str;
use pyo3::prelude::*;
use pyo3::types::PyString;
use std::{borrow::Cow, net::SocketAddr};
Expand Down Expand Up @@ -128,8 +129,8 @@ impl RSGIScope {
}

#[getter(path)]
fn get_path(&self) -> &str {
self.uri.path()
fn get_path(&self) -> Cow<str> {
percent_decode_str(self.uri.path()).decode_utf8().unwrap()
}

#[getter(query_string)]
Expand Down
3 changes: 2 additions & 1 deletion src/wsgi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hyper::{
header::{HeaderMap, CONTENT_LENGTH, CONTENT_TYPE},
Body, Method, Request, Uri, Version,
};
use percent_encoding::percent_decode_str;
use pyo3::types::{PyBytes, PyDict, PyList};
use pyo3::{prelude::*, types::IntoPyDict};
use std::{
Expand Down Expand Up @@ -166,7 +167,7 @@ impl WSGIScope {
}

(
path,
percent_decode_str(path).decode_utf8().unwrap(),
query_string,
self.py_http_version(),
(self.server_ip.to_string(), self.server_port),
Expand Down

0 comments on commit f3acfc6

Please sign in to comment.