Skip to content

Commit 2aac5f3

Browse files
committed
docs and tests
1 parent 63385f6 commit 2aac5f3

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
INSERT INTO
2+
sqlpage_functions (
3+
"name",
4+
"introduced_in_version",
5+
"icon",
6+
"description_md"
7+
)
8+
VALUES
9+
(
10+
'client_ip',
11+
'0.33.0',
12+
'network',
13+
'Returns the IP address of the client making the HTTP request.
14+
15+
### Example
16+
17+
```sql
18+
insert into connection_log (client_ip) values (sqlpage.client_ip());
19+
```
20+
21+
### Details
22+
23+
The function returns:
24+
- The IP address of the client as a string
25+
- `null` if the client IP cannot be determined (e.g., when serving through a Unix socket)
26+
27+
### ⚠️ Important Notes for Production Use
28+
29+
When running behind a reverse proxy (e.g., Nginx, Apache, Cloudflare):
30+
- This function will return the IP address of the reverse proxy, not the actual client
31+
- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header(''x-forwarded-for'')` or `sqlpage.header(''x-real-ip'')`
32+
- The exact header name depends on your reverse proxy configuration
33+
34+
Example with reverse proxy:
35+
```sql
36+
-- Choose the appropriate header based on your setup
37+
select coalesce(
38+
sqlpage.header(''x-forwarded-for''),
39+
sqlpage.header(''x-real-ip''),
40+
sqlpage.client_ip()
41+
) as real_client_ip;
42+
```
43+
44+
For security-critical applications, ensure your reverse proxy is properly configured to set and validate these headers.
45+
'
46+
);

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ super::function_definition_macro::sqlpage_functions! {
1616
basic_auth_password((&RequestInfo));
1717
basic_auth_username((&RequestInfo));
1818

19+
client_ip((&RequestInfo));
1920
cookie((&RequestInfo), name: Cow<str>);
2021
current_working_directory();
2122

@@ -634,3 +635,7 @@ async fn request_body_base64(request: &RequestInfo) -> Option<String> {
634635
async fn headers(request: &RequestInfo) -> String {
635636
serde_json::to_string(&request.headers).unwrap_or_default()
636637
}
638+
639+
async fn client_ip(request: &RequestInfo) -> Option<String> {
640+
Some(request.client_ip?.to_string())
641+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
select 'text' as component,
2+
case when sqlpage.client_ip() is null then 'It works !'
3+
else 'It failed ! Got: ' || sqlpage.client_ip()
4+
end as contents;

0 commit comments

Comments
 (0)