Skip to content

(feature) add sqlpage.query_string() #486

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions src/webserver/database/sqlpage_functions/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ super::function_definition_macro::sqlpage_functions! {
header((&RequestInfo), name: Cow<str>);

path((&RequestInfo));
query_string((&RequestInfo));
persist_uploaded_file((&RequestInfo), field_name: Cow<str>, folder: Option<Cow<str>>, allowed_extensions: Option<Cow<str>>);
protocol((&RequestInfo));

Expand Down Expand Up @@ -190,6 +191,10 @@ async fn path(request: &RequestInfo) -> &str {
&request.path
}

async fn query_string(request: &RequestInfo) -> &str {
&request.query_string
}

const DEFAULT_ALLOWED_EXTENSIONS: &str =
"jpg,jpeg,png,gif,bmp,webp,pdf,txt,doc,docx,xls,xlsx,csv,mp3,mp4,wav,avi,mov";

Expand Down
4 changes: 4 additions & 0 deletions src/webserver/http_request_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use super::request_variables::ParamMap;
pub struct RequestInfo {
pub method: actix_web::http::Method,
pub path: String,
pub query_string: String,
pub protocol: String,
pub get_variables: ParamMap,
pub post_variables: ParamMap,
Expand All @@ -45,6 +46,7 @@ impl Clone for RequestInfo {
Self {
method: self.method.clone(),
path: self.path.clone(),
query_string: self.query_string.clone(),
protocol: self.protocol.clone(),
get_variables: self.get_variables.clone(),
post_variables: self.post_variables.clone(),
Expand Down Expand Up @@ -74,6 +76,7 @@ pub(crate) async fn extract_request_info(
String::from_utf8_lossy(value.as_bytes()).to_string(),
)
});
let query_string = req.query_string().to_string();
let get_variables = web::Query::<Vec<(String, String)>>::from_query(req.query_string())
.map(web::Query::into_inner)
.unwrap_or_default();
Expand All @@ -92,6 +95,7 @@ pub(crate) async fn extract_request_info(
Ok(RequestInfo {
method,
path: req.path().to_string(),
query_string,
headers: param_map(headers),
get_variables: param_map(get_variables),
post_variables: param_map(post_variables),
Expand Down
16 changes: 16 additions & 0 deletions tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ async fn test_overwrite_variable() -> actix_web::Result<()> {
Ok(())
}

#[actix_web::test]
async fn test_query() -> actix_web::Result<()> {
let query_string = "source=web&q=sqlpage+github";
let url = format!("/tests/sql_test_files/it_works_query_string.sql?{}", query_string);
let req = get_request_to(url.as_str()).await?.to_srv_request();
let resp = main_handler(req).await?;

assert_eq!(resp.status(), StatusCode::FOUND);
let redirect = resp.headers().get("Location").unwrap().to_str().unwrap();
assert!(
redirect.contains("login.sql?source=web&q=sqlpage+github"),
"{redirect}\nexpected to contain: [login.sql?source=web&q=sqlpage+github]"
);
Ok(())
}

async fn test_file_upload(target: &str) -> actix_web::Result<()> {
let req = get_request_to(target)
.await?
Expand Down
9 changes: 9 additions & 0 deletions tests/sql_test_files/it_works_query_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT
'dynamic' AS component,
CASE sqlpage.query_string()
WHEN 'x=1' THEN
'[{"component":"text"}, {"contents":"It works !"}]'
ELSE
'[{"component":"redirect", "link":"login.sql?' || sqlpage.query_string() || '"}]'
END AS properties
;
Loading