2021-05-03 23:31:05 +02:00
|
|
|
//! Atward implements TVL's redirection service, living at
|
|
|
|
//! atward.tvl.fyi
|
|
|
|
//!
|
|
|
|
//! This service is designed to be added as a search engine to web
|
|
|
|
//! browsers and attempts to send users to useful locations based on
|
|
|
|
//! their search query (falling back to another search engine).
|
|
|
|
use regex::Regex;
|
2021-05-12 13:54:01 +02:00
|
|
|
use rouille::input::cookies;
|
|
|
|
use rouille::{Request, Response};
|
2021-05-03 23:31:05 +02:00
|
|
|
|
2022-03-26 17:27:21 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-05-06 15:54:24 +02:00
|
|
|
/// A query handler supported by atward. It consists of a pattern on
|
2021-05-03 23:31:05 +02:00
|
|
|
/// which to match and trigger the query, and a function to execute
|
|
|
|
/// that returns the target URL.
|
2021-05-06 15:54:24 +02:00
|
|
|
struct Handler {
|
2021-05-03 23:31:05 +02:00
|
|
|
/// Regular expression on which to match the query string.
|
|
|
|
pattern: Regex,
|
|
|
|
|
|
|
|
/// Function to construct the target URL. If the pattern matches,
|
|
|
|
/// this is invoked with the captured matches and the entire URI.
|
|
|
|
///
|
|
|
|
/// Returning `None` causes atward to fall through to the next
|
|
|
|
/// query (and eventually to the default search engine).
|
2021-05-06 16:11:45 +02:00
|
|
|
target: for<'s> fn(&Query, regex::Captures<'s>) -> Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An Atward query supplied by a user.
|
2021-05-12 13:54:01 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
2021-05-06 16:11:45 +02:00
|
|
|
struct Query {
|
|
|
|
/// Query string itself.
|
|
|
|
query: String,
|
2021-05-06 16:36:50 +02:00
|
|
|
|
|
|
|
/// Should Sourcegraph be used instead of cgit?
|
|
|
|
cs: bool,
|
2021-05-06 16:11:45 +02:00
|
|
|
}
|
|
|
|
|
2021-05-12 13:54:01 +02:00
|
|
|
/// Helper function for setting a parameter based on a query
|
|
|
|
/// parameter.
|
|
|
|
fn query_setting(req: &Request, config: &mut bool, param: &str) {
|
|
|
|
match req.get_param(param) {
|
|
|
|
Some(s) if s == "true" => *config = true,
|
|
|
|
Some(s) if s == "false" => *config = false,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 16:11:45 +02:00
|
|
|
impl Query {
|
2021-05-12 13:54:01 +02:00
|
|
|
fn from_request(req: &Request) -> Option<Query> {
|
|
|
|
// First extract the actual search query ...
|
|
|
|
let mut query = match req.get_param("q") {
|
|
|
|
Some(query) => Query { query, cs: false },
|
2021-05-06 16:11:45 +02:00
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
2021-05-12 13:54:01 +02:00
|
|
|
// ... then apply settings to it. Settings in query parameters
|
|
|
|
// take precedence over cookies.
|
|
|
|
for cookie in cookies(req) {
|
|
|
|
match cookie {
|
|
|
|
("cs", "true") => {
|
|
|
|
query.cs = true;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 16:36:50 +02:00
|
|
|
|
2021-05-12 13:54:01 +02:00
|
|
|
query_setting(req, &mut query.cs, "cs");
|
|
|
|
|
|
|
|
Some(query)
|
2021-05-06 16:11:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
impl From<&str> for Query {
|
|
|
|
fn from(query: &str) -> Query {
|
|
|
|
Query {
|
|
|
|
query: query.to_string(),
|
2021-05-06 16:36:50 +02:00
|
|
|
cs: false,
|
2021-05-06 16:11:45 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 23:31:05 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:25:52 +02:00
|
|
|
/// Create a URL to a file (and, optionally, specific line) in cgit.
|
|
|
|
fn cgit_url(path: &str) -> String {
|
|
|
|
if path.ends_with(".md") {
|
|
|
|
format!("https://code.tvl.fyi/about/{}", path)
|
|
|
|
} else {
|
|
|
|
format!("https://code.tvl.fyi/tree/{}", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 16:36:50 +02:00
|
|
|
/// Create a URL to a path in Sourcegraph.
|
|
|
|
fn sourcegraph_path_url(path: &str) -> String {
|
|
|
|
format!("https://cs.tvl.fyi/depot/-/tree/{}", path)
|
|
|
|
}
|
2021-05-06 15:54:24 +02:00
|
|
|
/// Definition of all supported query handlers in atward.
|
|
|
|
fn handlers() -> Vec<Handler> {
|
2021-05-03 23:31:05 +02:00
|
|
|
vec![
|
|
|
|
// Bug IDs (e.g. b/123)
|
2021-05-06 15:54:24 +02:00
|
|
|
Handler {
|
2021-05-03 23:31:05 +02:00
|
|
|
pattern: Regex::new("^b/(?P<bug>\\d+)$").unwrap(),
|
|
|
|
target: |_, captures| Some(format!("https://b.tvl.fyi/{}", &captures["bug"])),
|
|
|
|
},
|
2021-05-04 01:09:21 +02:00
|
|
|
// Changelists (e.g. cl/42)
|
2021-05-06 15:54:24 +02:00
|
|
|
Handler {
|
2021-05-04 01:09:21 +02:00
|
|
|
pattern: Regex::new("^cl/(?P<cl>\\d+)$").unwrap(),
|
|
|
|
target: |_, captures| Some(format!("https://cl.tvl.fyi/{}", &captures["cl"])),
|
|
|
|
},
|
2021-06-12 16:30:24 +02:00
|
|
|
// Non-parameterised short hostnames should redirect to $host.tvl.fyi
|
|
|
|
Handler {
|
|
|
|
pattern: Regex::new("^(?P<host>b|cl|cs|code|at|todo)$").unwrap(),
|
|
|
|
target: |_, captures| Some(format!("https://{}.tvl.fyi/", &captures["host"])),
|
|
|
|
},
|
2021-11-05 12:29:08 +01:00
|
|
|
// Depot revisions (e.g. r/3002)
|
|
|
|
Handler {
|
|
|
|
pattern: Regex::new("^r/(?P<rev>\\d+)$").unwrap(),
|
|
|
|
target: |_, captures| {
|
|
|
|
Some(format!(
|
|
|
|
"https://code.tvl.fyi/commit/?id=refs/r/{}",
|
|
|
|
&captures["rev"]
|
|
|
|
))
|
|
|
|
},
|
|
|
|
},
|
2021-05-04 17:25:52 +02:00
|
|
|
// Depot paths (e.g. //web/atward or //ops/nixos/whitby/default.nix)
|
|
|
|
// TODO(tazjin): Add support for specifying lines in a query parameter
|
2021-05-06 15:54:24 +02:00
|
|
|
Handler {
|
2021-08-26 20:54:03 +02:00
|
|
|
pattern: Regex::new("^//(?P<path>[a-zA-Z].*)?$").unwrap(),
|
2021-05-06 16:36:50 +02:00
|
|
|
target: |query, captures| {
|
2021-08-26 20:54:03 +02:00
|
|
|
// Pass an empty string if the path is missing, to
|
|
|
|
// redirect to the depot root.
|
2021-11-05 12:29:08 +01:00
|
|
|
let path = captures.name("path").map(|m| m.as_str()).unwrap_or("");
|
2021-08-26 20:54:03 +02:00
|
|
|
|
2021-05-06 16:36:50 +02:00
|
|
|
if query.cs {
|
2021-08-26 20:54:03 +02:00
|
|
|
Some(sourcegraph_path_url(path))
|
2021-05-06 16:36:50 +02:00
|
|
|
} else {
|
2021-08-26 20:54:03 +02:00
|
|
|
Some(cgit_url(path))
|
2021-05-06 16:36:50 +02:00
|
|
|
}
|
|
|
|
},
|
2021-05-04 17:25:52 +02:00
|
|
|
},
|
2021-05-03 23:31:05 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to match against all known query types, and return the
|
|
|
|
/// destination URL if one is found.
|
2021-05-06 16:11:45 +02:00
|
|
|
fn dispatch(handlers: &[Handler], query: &Query) -> Option<String> {
|
|
|
|
for handler in handlers {
|
|
|
|
if let Some(captures) = handler.pattern.captures(&query.query) {
|
|
|
|
if let Some(destination) = (handler.target)(query, captures) {
|
2021-05-03 23:31:05 +02:00
|
|
|
return Some(destination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:58:10 +02:00
|
|
|
/// Return the opensearch.xml file which is required for adding atward
|
|
|
|
/// as a search engine in Firefox.
|
|
|
|
fn opensearch() -> Response {
|
|
|
|
Response::text(include_str!("opensearch.xml"))
|
|
|
|
.with_unique_header("Content-Type", "application/opensearchdescription+xml")
|
|
|
|
}
|
|
|
|
|
2021-05-11 16:46:01 +02:00
|
|
|
/// Render the atward index page which gives users some information
|
|
|
|
/// about how to use the service.
|
|
|
|
fn index() -> Response {
|
2021-05-12 16:00:39 +02:00
|
|
|
Response::html(include_str!(env!("ATWARD_INDEX_HTML")))
|
2021-05-11 16:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Render the fallback page which informs users that their query is
|
|
|
|
/// unsupported.
|
2021-05-04 00:33:53 +02:00
|
|
|
fn fallback() -> Response {
|
2021-05-06 16:02:54 +02:00
|
|
|
Response::text("error for emphasis that i am angery and the query whimchst i angery atward")
|
|
|
|
.with_status_code(404)
|
2021-05-04 00:33:53 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 23:31:05 +02:00
|
|
|
fn main() {
|
2021-05-06 15:54:24 +02:00
|
|
|
let queries = handlers();
|
2021-05-04 01:16:56 +02:00
|
|
|
let address = std::env::var("ATWARD_LISTEN_ADDRESS")
|
|
|
|
.expect("ATWARD_LISTEN_ADDRESS environment variable must be set");
|
2021-05-04 00:33:53 +02:00
|
|
|
|
|
|
|
rouille::start_server(&address, move |request| {
|
|
|
|
rouille::log(&request, std::io::stderr(), || {
|
2021-05-11 23:58:10 +02:00
|
|
|
if request.url() == "/opensearch.xml" {
|
|
|
|
return opensearch();
|
|
|
|
}
|
|
|
|
|
2021-05-06 16:11:45 +02:00
|
|
|
let query = match Query::from_request(&request) {
|
2021-05-05 12:08:14 +02:00
|
|
|
Some(q) => q,
|
2021-05-11 16:46:01 +02:00
|
|
|
None => return index(),
|
2021-05-05 12:08:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match dispatch(&queries, &query) {
|
2021-05-04 00:33:53 +02:00
|
|
|
None => fallback(),
|
|
|
|
Some(destination) => Response::redirect_303(destination),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2021-05-03 23:31:05 +02:00
|
|
|
}
|