make shortlinks case insensitive

This commit is contained in:
2026-05-09 23:00:15 +01:00
parent c270565f96
commit aebd192f25
2 changed files with 25 additions and 3 deletions

View File

@@ -33,6 +33,27 @@ a _really_ simple url shortener: less than 200 lines of Rust and one dependency
a working [dockerfile](./Dockerfile) and [compose file](./compose.yaml) are provided.
## a note on case insensitivity
all shortlinks are case insenitive,
due to the lack of [urlencoding/decoding](#a-note-on-urlencoded-characters).
## a note on urlencoded characters
this program does not decode urlencoded strings before checking if strings match.
this is a deliberate choice to keep things as simple as possible.
avoid using links that may be encoded.
for links that will necessarily get encoded (still discouraged) (such as emojis),
put the urlencoded string.
for example, for the link <http://pls.cx/🤬> the following line is in my urls.txt:
```
/🤬 https://en.wikipedia.org/wiki/Controversies_of_Nestl%C3%A9 you may want the non-encoded version in case a client does not encode it? idk
/%f0%9f%a4%ac https://en.wikipedia.org/wiki/Controversies_of_Nestl%C3%A9
```
## why?
all url shorteners i've come across are too complicated:
@@ -47,7 +68,7 @@ updating it.
good question.
i'm not sure, but i think i want to separate my concerns?
you might just want to configure your reverse proxy instead.
you might just want to configure your reverse proxy instead:
#### caddy

View File

@@ -46,6 +46,7 @@ async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<St
simple_response(socket, ResponseCode::BadRequest).await;
return;
};
let path = path.to_lowercase();
if http_method != "GET" {
println!("process_request: forbidden method");
@@ -53,7 +54,7 @@ async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<St
return;
}
let Some(location) = urlmap.get(path) else {
let Some(location) = urlmap.get(&path) else {
println!("process_request: not found");
simple_response(socket, ResponseCode::NotFound).await;
return;
@@ -114,7 +115,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Err(format!("Unable to parse line: '{}'", url_pair))?
};
urlmap.insert(slug.to_string(), redirect.to_string());
urlmap.insert(slug.to_lowercase(), redirect.to_string());
}
let urlmap = Arc::new(urlmap);