Lookup files in /var/gemini

This commit is contained in:
Julien Blanchard 2019-08-27 11:04:23 +02:00
parent 64f6265bb6
commit 2e3e2e691e
1 changed files with 14 additions and 34 deletions

View File

@ -17,8 +17,6 @@ use percent_encoding::percent_decode;
extern crate chrono;
use chrono::{Datelike, Timelike, Utc};
const GUESTBOOK: &str = "/var/gemini/guestbook";
fn main() {
let args: Vec<String> = env::args().collect();
let pkcs12 = make_pkcs12(&args[1], &args[2]);
@ -106,7 +104,7 @@ fn append_guestbook(message: &str) {
.create(true)
.append(true)
.write(true)
.open(GUESTBOOK)
.open("/var/gemini/guestbook")
.expect("file not found");
let now = Utc::now();
@ -119,7 +117,7 @@ fn append_guestbook(message: &str) {
}
fn read_guestbook() -> Option<String> {
let mut file = File::open(GUESTBOOK).expect("file not found");
let mut file = File::open("/var/gemini/guestbook").expect("file not found");
let mut content = String::new();
file.read_to_string(&mut content)
.expect("Unable to read file");
@ -138,6 +136,7 @@ fn random_url() -> &'static str {
"gemini://heavysquare.com",
"gemini://mozz.us",
"gemini://dgold.eu",
"gemini://tilde.black",
];
let url = urls.choose(&mut rand::thread_rng()).unwrap();
url.to_owned()
@ -156,25 +155,31 @@ fn route(stream: TlsStream<TcpStream>, request: String) {
let query = url.query();
let route = match url.path() {
"/" => (20, String::from("index.gemini"), None),
"/" => (20, String::from("/index.gemini"), None),
"/roulette" => (31, String::new(), None),
"/guestbook" => (20, String::from(GUESTBOOK), None),
"/guestbook" => (20, String::from("/guestbook"), None),
"/guestbook/new" => {
if query.is_some() {
(20, String::from(GUESTBOOK), query)
(20, String::from("/guestbook"), query)
} else {
(10, String::from("Leave a message."), None)
}
},
path => (20, str::replace(path, "/", ""), None),
path => (20, String::from(path), None),
};
match route {
(10, message, _) => respond_input(stream, String::from(message)),
(20, path, query) => {
let full_path = if path.starts_with("/var/gemini") {
path
} else {
format!("/var/gemini{}", path)
};
let open_file = OpenOptions::new()
.read(true)
.open(&path);
.open(&full_path);
match open_file {
Ok(mut file) => {
@ -192,28 +197,3 @@ fn route(stream: TlsStream<TcpStream>, request: String) {
_ => (),
}
}
// 10 => Status::Input(meta),
// 2 => Status::Success(meta),
// 20 => Status::Success(meta),
// 200 => Status::Success(meta),
// 21 => Status::SuccessEndOfClientCertificateSession(meta),
// 30 => Status::RedirectTemporary(meta),
// 31 => Status::RedirectPermanent(meta),
// 40 => Status::TemporaryFailure(meta),
// 41 => Status::ServerUnavailable(meta),
// 42 => Status::CGIError(meta),
// 43 => Status::ProxyError(meta),
// 44 => Status::SlowDown(meta),
// 50 => Status::PermanentFailure(meta),
// 51 => Status::NotFound(meta),
// 52 => Status::Gone(meta),
// 53 => Status::ProxyRequestRefused(meta),
// 59 => Status::BadRequest(meta),
// 60 => Status::ClientCertificateRequired(meta),
// 61 => Status::TransientCertificateRequired(meta),
// 62 => Status::AuthorisedCertificatedRequired(meta),
// 63 => Status::CertificateNotAccepted(meta),
// 64 => Status::FutureCertificateRejected(meta),
// 65 => Status::ExpiredCertificateRejected(meta),
// _ => Status::Unknown(meta)