Skip to content
Open
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
34 changes: 28 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ impl DurableObject for Repository {
Ok(selection) => selection,
Err(resp) => return resp,
};
let ref_name = parts.get(3).unwrap_or(&"main");
let ref_name = &decode_path_segment(parts.get(3).unwrap_or(&"main"));
let sub_path = if parts.len() > 4 {
parts[4..].join("/")
decode_path_segment(&parts[4..].join("/"))
} else {
String::new()
};
Expand Down Expand Up @@ -420,9 +420,9 @@ impl DurableObject for Repository {
Ok(selection) => selection,
Err(resp) => return resp,
};
let ref_name = parts.get(3).unwrap_or(&"main");
let ref_name = &decode_path_segment(parts.get(3).unwrap_or(&"main"));
let sub_path = if parts.len() > 4 {
parts[4..].join("/")
decode_path_segment(&parts[4..].join("/"))
} else {
String::new()
};
Expand Down Expand Up @@ -496,9 +496,9 @@ impl DurableObject for Repository {
.await
}
(Method::Get, "raw") => {
let ref_name = parts.get(3).unwrap_or(&"main");
let ref_name = &decode_path_segment(parts.get(3).unwrap_or(&"main"));
let sub_path = if parts.len() > 4 {
parts[4..].join("/")
decode_path_segment(&parts[4..].join("/"))
} else {
String::new()
};
Expand Down Expand Up @@ -1148,6 +1148,28 @@ fn pkt_line(buf: &mut Vec<u8>, data: &str) {
buf.extend_from_slice(data.as_bytes());
}

/// Percent-decode a URL path segment (e.g. `my%20file.txt` -> `my file.txt`).
/// Unlike form decoding, `+` is left as-is. Invalid escapes pass through unchanged.
fn decode_path_segment(s: &str) -> String {
let bytes = s.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
let hi = (bytes[i + 1] as char).to_digit(16);
let lo = (bytes[i + 2] as char).to_digit(16);
if let (Some(h), Some(l)) = (hi, lo) {
out.push((h * 16 + l) as u8);
i += 3;
continue;
}
}
out.push(bytes[i]);
i += 1;
}
String::from_utf8_lossy(&out).into_owned()
}

/// Check if a string is a 40-character hex SHA-1 hash.
/// Used to distinguish API calls (by hash) from web UI calls (by ref + path).
fn is_hex40(s: &str) -> bool {
Expand Down