Skip to content

Commit 8cff7bf

Browse files
committed
new function: sqlpage.request_method
1 parent 1bef2e9 commit 8cff7bf

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CHANGELOG.md
22

3-
## unreleased
3+
## 0.20.6 (unreleased)
44

55
- `sqlpage.hash_password(NULL)` now returns `NULL` instead of throwing an error. This behavior was changed unintentionally in 0.20.5 and could have broken existing SQLPage websites.
66
- The [dynamic](https://sql.ophir.dev/documentation.sql?component=dynamic#component) component now supports multiple `properties` attributes. The following is now possible:
@@ -12,6 +12,7 @@
1212
- Casting values from one type to another using the `::` operator is only supported by PostgreSQL. SQLPage versions before 0.20.5 would silently convert all casts to the `CAST(... AS ...)` syntax, which is supported by all databases. Since 0.20.5, SQLPage started to respect the original `::` syntax, and pass it as-is to the database. This broke existing SQLPage websites that used the `::` syntax with databases other than PostgreSQL. For backward compatibility, this version of SQLPage re-establishes the previous behavior, converts `::` casts on non-PostgreSQL databases to the `CAST(... AS ...)` syntax, but will display a warning in the logs.
1313
- In short, if you saw an error like `Error: unrecognized token ":"` after upgrading to 0.20.5, this version should fix it.
1414
- The `dynamic` component now properly displays error messages when its properties are invalid. There used to be a bug where errors would be silently ignored, making it hard to debug invalid dynamic components.
15+
- New [`sqlpage.request_method`](https://sql.ophir.dev/functions.sql?function=request_method#function) function to get the HTTP method used to access the current page. This is useful to create pages that behave differently depending on whether they are accessed with a GET request (to display a form, for instance) or a POST request (to process the form).
1516

1617
## 0.20.5 (2024-05-07)
1718

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
INSERT INTO
2+
sqlpage_functions (
3+
"name",
4+
"introduced_in_version",
5+
"icon",
6+
"description_md"
7+
)
8+
VALUES
9+
(
10+
'request_method',
11+
'0.20.6',
12+
'http-get',
13+
'Returns the HTTP request method (GET, POST, etc.) used to access the page.
14+
15+
# HTTP request methods
16+
17+
HTTP request methods (also known as verbs) are used to indicate the desired action to be performed on the identified resource. The most common methods are:
18+
- **GET**: retrieve information from the server. This is the default method used by browsers when you click on a link.
19+
- **POST**: submit data to be processed by the server. This is the default method used by browsers when you submit a form.
20+
- **PUT**: replace the current representation of the target resource with the request payload. Most commonly used in REST APIs.
21+
- **DELETE**: remove the target resource.
22+
- **PATCH**, **HEAD**, **OPTIONS**, **CONNECT**, **TRACE**: less common methods that are used in specific situations.
23+
24+
# Example
25+
26+
```sql
27+
select ''redirect'' as component,
28+
''/error?msg=expected+a+PUT+request'' as link,
29+
where sqlpage.request_method() != ''PUT'';
30+
31+
insert into my_table (column1, column2) values (:value1, :value2);
32+
```
33+
'
34+
);

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ super::function_definition_macro::sqlpage_functions! {
2626
random_string(string_length: SqlPageFunctionParam<usize>);
2727
read_file_as_data_url((&RequestInfo), file_path: Option<Cow<str>>);
2828
read_file_as_text((&RequestInfo), file_path: Option<Cow<str>>);
29+
request_method((&RequestInfo));
2930
run_sql((&RequestInfo), sql_file_path: Option<Cow<str>>);
3031

3132
uploaded_file_mime_type((&RequestInfo), upload_name: Cow<str>);
@@ -340,6 +341,10 @@ fn mime_guess_from_filename(filename: &str) -> mime_guess::Mime {
340341
maybe_mime.unwrap_or(mime_guess::mime::APPLICATION_OCTET_STREAM)
341342
}
342343

344+
async fn request_method(request: &RequestInfo) -> String {
345+
request.method.to_string()
346+
}
347+
343348
async fn run_sql<'a>(
344349
request: &'a RequestInfo,
345350
sql_file_path: Option<Cow<'a, str>>,

src/webserver/http_request_info.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tokio_stream::StreamExt;
2424

2525
#[derive(Debug)]
2626
pub struct RequestInfo {
27+
pub method: actix_web::http::Method,
2728
pub path: String,
2829
pub protocol: String,
2930
pub get_variables: ParamMap,
@@ -40,6 +41,7 @@ pub struct RequestInfo {
4041
impl Clone for RequestInfo {
4142
fn clone(&self) -> Self {
4243
Self {
44+
method: self.method.clone(),
4345
path: self.path.clone(),
4446
protocol: self.protocol.clone(),
4547
get_variables: self.get_variables.clone(),
@@ -61,6 +63,7 @@ pub(crate) async fn extract_request_info(
6163
app_state: Arc<AppState>,
6264
) -> anyhow::Result<RequestInfo> {
6365
let (http_req, payload) = req.parts_mut();
66+
let method = http_req.method().clone();
6467
let protocol = http_req.connection_info().scheme().to_string();
6568
let config = &app_state.config;
6669
let (post_variables, uploaded_files) = extract_post_data(http_req, payload, config).await?;
@@ -86,6 +89,7 @@ pub(crate) async fn extract_request_info(
8689
.map(Authorization::into_scheme);
8790

8891
Ok(RequestInfo {
92+
method,
8993
path: req.path().to_string(),
9094
headers: param_map(headers),
9195
get_variables: param_map(get_variables),
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set actual = sqlpage.request_method()
2+
set expected = 'GET';
3+
4+
select 'text' as component,
5+
case $actual
6+
when $expected
7+
then 'It works !'
8+
else
9+
'Failed.
10+
Expected: ' || $expected ||
11+
'Got: ' || $actual
12+
end as contents;

0 commit comments

Comments
 (0)