Compare commits

..

1 Commits

Author SHA1 Message Date
Melody Horn 290c2626d1 preserve client certs 2020-09-25 03:43:26 -06:00
17 changed files with 75 additions and 216 deletions

View File

@ -7,6 +7,9 @@ steps:
pull: always
commands:
- cargo test --all --all-features
when:
event:
- push
- name: auto-release
image: xena/gitea-release
@ -22,10 +25,6 @@ steps:
- push
branch:
- main
trigger:
event:
- push
- pull_request
---
@ -40,6 +39,6 @@ steps:
environment:
CARGO_TOKEN:
from_secret: CARGO_TOKEN
trigger:
event:
- tag
when:
event:
- tag

View File

@ -1,15 +1,5 @@
# Changelog
## 0.6.2
Bump gemtext to 0.2.0
## 0.6.1
### FIXED
- [#9] Fixes from @boringcactus for gemtext rendering
## 0.6.0
### ADDED

View File

@ -1,6 +1,6 @@
[package]
name = "maj"
version = "0.6.2"
version = "0.6.0"
authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018"
license = "0BSD"
@ -10,6 +10,8 @@ repository = "https://tulpa.dev/cadey/maj"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-std = { version = "1.6", optional = true }
async-tls = { default-features = false, optional = true, version = "0", git = "https://github.com/FlorianUekermann/async-tls.git" }
async-trait = { version = "0", optional = true }
log = "0.4"
mime_guess = "2.0"
@ -17,11 +19,11 @@ num = "0.2"
num-derive = "0.3"
num-traits = "0.2"
once_cell = "1.4"
rustls = { version = "0.19", optional = true, features = ["dangerous_configuration"] }
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
structopt = "0.3"
thiserror = "1"
tokio-rustls = { version = "0.21", features = ["dangerous_configuration"] }
tokio = { version = "0.3", features = ["full"] }
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
tokio = { version = "0.2", features = ["full"], optional = true }
url = "2"
webpki-roots = { version = "0.20", optional = true }
webpki = { version = "0.21.0", optional = true }
@ -35,8 +37,12 @@ pretty_env_logger = "0.4"
default = ["client", "server"]
client = [
"tokio-rustls",
"webpki",
"webpki-roots",
"tokio",
"async-std",
"async-tls/client"
]
server = [
@ -44,6 +50,8 @@ server = [
"webpki",
"webpki-roots",
"async-trait",
"async-std",
"async-tls/server"
]
[workspace]

View File

@ -1 +1 @@
0.6.2
0.6.0

View File

@ -1,6 +1,6 @@
[package]
name = "gemtext"
version = "0.2.1"
version = "0.1.0"
authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018"
license = "0BSD"

View File

@ -18,28 +18,6 @@ impl Builder {
self
}
/// Append a single blank line to the document
///
/// This is equivilent to calling [`text()`] with an empty string, or pushing a blank
/// [`Node`]
///
/// ```
/// # use gemtext::Builder;
/// let greeting = Builder::new()
/// .text("Hello")
/// .blank_line()
/// .text("universe")
/// .to_string();
///
/// assert_eq!(greeting.trim(), "Hello\n\nuniverse");
/// ```
///
/// [`text()`]: Self::text()
pub fn blank_line(mut self) -> Self {
self.nodes.push(Node::blank());
self
}
pub fn link<T: Into<String>>(mut self, to: T, name: Option<String>) -> Builder {
self.nodes.push(Node::Link {
to: to.into(),
@ -48,12 +26,8 @@ impl Builder {
self
}
pub fn preformatted<A, T>(mut self, alt_text: A, data: T) -> Builder
where
A: Into<String>,
T: Into<String>,
{
self.nodes.push(Node::Preformatted { alt: alt_text.into(), body: data.into() });
pub fn preformatted<T: Into<String>>(mut self, data: T) -> Builder {
self.nodes.push(Node::Preformatted(data.into()));
self
}
@ -80,67 +54,19 @@ impl Builder {
}
}
impl ToString for Builder {
/// Render a document to a string
///
/// This produces a text/gemini compliant text document, represented as a string
fn to_string(&self) -> String {
let len: usize = self.nodes.iter().map(Node::estimate_len).sum(); // sum up node lengths
let mut bytes = Vec::with_capacity(len + self.nodes.len()); // add in inter-node newlines
render(self, &mut bytes).unwrap(); // Writing to a string shouldn't produce errors
unsafe {
// This is safe because bytes is composed of Strings. We could have this as
// pure safe code by replicating the `render()` method and switching it to use
// a fmt::Write (or even `String::push()`)instead of a io::Write, but this has
// the same effect, with much DRYer code.
String::from_utf8_unchecked(bytes)
}
}
}
impl AsRef<[Node]> for Builder {
/// Get a reference to the internal node list of this builder
fn as_ref(&self) -> &[Node] {
self.nodes.as_ref()
}
}
impl AsMut<[Node]> for Builder {
/// Get a mutable reference to the internal node list of this builder
fn as_mut(&mut self) -> &mut [Node] {
self.nodes.as_mut()
}
}
impl From<Builder> for Vec<Node> {
/// Convert into a collection of [`Node`]s.
///
/// Equivilent to calling [`Builder::build()`]
fn from(builder: Builder) -> Self {
builder.build()
}
}
/// Render a set of nodes as a document to a writer.
pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()> {
pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
use Node::*;
for node in nodes.as_ref() {
for node in nodes {
match node {
Text(body) => {
let special_prefixes = ["=>", "```", "#", "*", ">"];
if special_prefixes.iter().any(|prefix| body.starts_with(prefix)) {
write!(out, " ")?;
}
write!(out, "{}\n", body)?
},
Text(body) => write!(out, "{}\n", body)?,
Link { to, name } => match name {
Some(name) => write!(out, "=> {} {}\n", to, name)?,
None => write!(out, "=> {}\n", to)?,
},
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n", alt, body)?,
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(*level as usize), body)?,
Preformatted(body) => write!(out, "```\n{}\n```\n", body)?,
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(level as usize), body)?,
ListItem(body) => write!(out, "* {}\n", body)?,
Quote(body) => write!(out, "> {}\n", body)?,
};
@ -150,7 +76,7 @@ pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()>
}
/// Individual nodes of the document. Each node correlates to a line in the file.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq)]
pub enum Node {
/// Text lines are the most fundamental line type - any line which does not
/// match the definition of another line type defined below defaults to
@ -191,19 +117,7 @@ pub enum Node {
/// (e.g. Python) should be able to be copied and pasted from the client into
/// a file and interpreted/compiled without any problems arising from the
/// client's manner of displaying them.
///
/// The first preformatted toggle of a document is often followed by a short
/// string, which acts as alt-text for the preformatted block. This is also
/// often used to denote the language of code in a block of text. For example,
/// a block starting with the text `\`\`\`rust` may be interpreted as rust
/// code, and a block starting with `\`\`\` An ascii art owl` would be
/// described aptly to visually impaired users using a screen reader. The alt
/// text may be separated from the toggle by whitespace. `gemtext` currently
/// renders alt text without this separation.
///
/// To create a preformatted block with no alt text, simply pass a zero-length
/// string as alt text.
Preformatted { alt: String, body: String },
Preformatted(String),
/// Lines beginning with "#" are heading lines. Heading lines consist of one,
/// two or three consecutive "#" characters, followed by optional whitespace,
@ -246,73 +160,24 @@ impl Node {
pub fn blank() -> Node {
Node::Text("".to_string())
}
/// Cheaply estimate the length of this node
///
/// This measures length in bytes, *not characters*. So if the user includes
/// non-ascii characters, a single one of these characters may add several bytes to
/// the length, despite only displaying as one character.
///
/// This does include any newlines, but not any trailing newlines. For example, a
/// preformatted text block containing a single line reading "trans rights! 🏳️‍⚧️"
/// would have a length of 30: 3 backticks, a newline, the text (including 16 bytes
/// for the trans flag), another newline, and another 3 backticks.
///
/// ```
/// # use gemtext::Node;
/// let simple_text = Node::Text(String::from("Henlo worl"));
/// let linky_link = Node::Link { to: "gemini://cetacean.club/maj/".to_string(), name: Some("Maj".to_string()) };
/// let human_rights = Node::Preformatted {
/// alt: "".to_string(),
/// body: "trans rights! 🏳️‍⚧️".to_string(),
/// };
///
/// assert_eq!(
/// simple_text.estimate_len(),
/// "Henlo worl".as_bytes().len()
/// );
/// assert_eq!(
/// linky_link.estimate_len(),
/// "=> gemini://cetacean.club/maj/ Maj".as_bytes().len()
/// );
/// assert_eq!(
/// human_rights.estimate_len(),
/// "```\ntrans rights! 🏳️‍⚧️\n```".as_bytes().len()
/// );
/// ```
pub fn estimate_len(&self) -> usize {
match self {
Self::Text(text) => text.len(),
Self::Link { to, name } => 3 + to.as_bytes().len() +
name.as_ref().map(|n| n.as_bytes().len() + 1).unwrap_or(0),
Self::Preformatted { alt, body } => alt.as_bytes().len()
+ body.as_bytes().len() + 8,
Self::Heading { level, body } => *level as usize + 1 + body.as_bytes().len(),
Self::ListItem(item) | Self::Quote(item)=> 2 + item.as_bytes().len(),
}
}
}
pub fn parse(doc: &str) -> Vec<Node> {
let mut result: Vec<Node> = vec![];
let mut collect_preformatted: bool = false;
let mut preformatted_buffer: Vec<u8> = vec![];
let mut alt = "";
for line in doc.lines() {
if let Some(trailing) = line.strip_prefix("```") {
if line.starts_with("```") {
collect_preformatted = !collect_preformatted;
if !collect_preformatted {
result.push(Node::Preformatted {
alt: alt.to_string(),
body: String::from_utf8(preformatted_buffer)
result.push(Node::Preformatted(
String::from_utf8(preformatted_buffer)
.unwrap()
.trim_end()
.to_string(),
});
));
preformatted_buffer = vec![];
} else {
alt = trailing.trim();
}
continue;
}
@ -411,13 +276,13 @@ mod tests {
#[test]
fn preformatted() {
let _ = pretty_env_logger::try_init();
let msg = "```hi there\n\
obi-wan kenobi\n\
let msg = "```\n\
hi there\n\
```\n\
\n\
Test\n";
let expected: Vec<Node> = vec![
Node::Preformatted{ alt: "hi there".to_string(), body: "obi-wan kenobi".to_string() },
Node::Preformatted("hi there".to_string()),
Node::Text(String::new()),
Node::Text("Test".to_string()),
];
@ -467,20 +332,9 @@ mod tests {
let _ = pretty_env_logger::try_init();
let msg = include_str!("../../testdata/ambig_preformatted.gmi");
let expected: Vec<Node> = vec![
Node::Preformatted { alt: "foo".to_string(), body: "FOO".to_string() },
Node::Preformatted("FOO".to_string()),
Node::Text("Foo bar".to_string()),
];
assert_eq!(expected, parse(msg));
}
#[test]
fn ambiguous_text() {
let _ = pretty_env_logger::try_init();
let original = Node::Text("#1 World's Best Coder".to_string());
let expected = " #1 World's Best Coder\n";
let mut rendered: Vec<u8> = vec![];
render(vec![original], &mut rendered).unwrap();
let rendered = String::from_utf8(rendered).unwrap();
assert_eq!(expected, rendered)
}
}

View File

@ -11,7 +11,7 @@ cursive = "0.15"
log = "0.4"
url = "2"
webpki = "0.21.0"
rustls = { version = "0.19", features = ["dangerous_configuration"] }
rustls = { version = "0.18", features = ["dangerous_configuration"] }
smol = { version = "0.3", features = ["tokio02"] }
maj = { path = ".." }

View File

@ -224,7 +224,7 @@ pub fn render(body: &str) -> StyledString {
styled.append(StyledString::styled(name, Style::from(Effect::Underline)))
}
},
Preformatted { body, .. } => styled.append(StyledString::plain(body)),
Preformatted(data) => styled.append(StyledString::plain(data)),
Heading { level, body } => styled.append(StyledString::styled(
format!("{} {}", "#".repeat(level as usize), body),
Style::from(Effect::Bold),

View File

@ -8,14 +8,15 @@ edition = "2018"
[dependencies]
anyhow = "1"
async-std = "1.5"
async-trait = "0"
atom_syndication = "0.9"
chrono = "*"
log = "0"
pretty_env_logger = "0.4"
webpki = "0.21.0"
rustls = { version = "0.19", features = ["dangerous_configuration"] }
rustls = { version = "0.18", features = ["dangerous_configuration"] }
structopt = "0.3"
tokio = { version = "0.3", features = ["full"] }
tokio = { version = "0.2", features = ["full"] }
maj = { path = "../..", features = ["server", "client"], default-features = false }

View File

@ -25,7 +25,7 @@ fn gem_to_md(tcana: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
Some(name) => write!(out, "[{}]({})\n\n", name, to)?,
None => write!(out, "[{0}]({0})", to)?,
},
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n\n", alt, body)?,
Preformatted(body) => write!(out, "```\n{}\n```\n\n", body)?,
Heading { level, body } => {
write!(out, "##{} {}\n\n", "#".repeat(level as usize), body)?
}

View File

@ -6,6 +6,8 @@ let
"https://github.com/nix-community/NUR/archive/master.tar.gz") {
inherit pkgs;
};
tex = with pkgs;
texlive.combine { inherit (texlive) scheme-medium bitter titlesec; };
in pkgs.mkShell {
buildInputs = with pkgs; [
pkgs.latest.rustChannels.stable.rust
@ -13,6 +15,10 @@ in pkgs.mkShell {
pkg-config
ncurses
kindlegen
nur.repos.mic92.pandoc-bin
tex
];
RUST_LOG="info,majsite=debug,majsite::server=debug";

View File

@ -9,6 +9,7 @@ build = "build.rs"
[dependencies]
anyhow = "1"
async-std = "1.5"
async-trait = "0"
dnd_dice_roller = "0.3"
env_logger = "0"
@ -16,14 +17,13 @@ log = "0"
mime = "0.3.0"
percent-encoding = "2"
rand = "0"
rustls = { version = "0.19", features = ["dangerous_configuration"] }
rustls = { version = "0.18", features = ["dangerous_configuration"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
smol = { version = "0.3", features = ["tokio02"] }
structopt = "0.3"
url = "2"
warp = "0.2"
tokio = { version = "0.3", features = ["rt"] }
maj = { path = "..", features = ["server"], default-features = false }

View File

@ -80,12 +80,7 @@ fn gemtext_to_html(inp: Vec<u8>) -> (String, impl ToHtml) {
name.as_ref().or(Some(&to.to_string())).unwrap()
)
.unwrap(),
Preformatted { alt, body } => write!(
buf,
"<code><pre title = \"{}\">{}</pre></code>",
alt.replace("\"", "\\\"").replace("\\", "\\\\"),
body
).unwrap(),
Preformatted(body) => write!(buf, "<code><pre>{}</pre></code>", body).unwrap(),
ListItem(body) => write!(buf, "<li>{}</li>", body).unwrap(),
Quote(body) => write!(buf, "<blockquote>{}</blockquote>", body).unwrap(),
}

View File

@ -1,8 +1,8 @@
use async_std::task;
use rustls::{
internal::pemfile::{certs, pkcs8_private_keys},
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
};
use std::{
fs::File,
io::{self, BufReader},
@ -64,8 +64,7 @@ fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
}
#[tokio::main]
async fn main() -> Result<(), maj::server::Error> {
fn main() -> Result<(), maj::server::Error> {
env_logger::init();
let opts = Options::from_args();
let certs = load_certs(&opts.cert).unwrap();
@ -97,7 +96,7 @@ async fn main() -> Result<(), maj::server::Error> {
thread::spawn(move || http::run(h.clone(), port));
}
maj::server::serve(h.clone(), config, opts.host, opts.port).await?;
task::block_on(maj::server::serve(h.clone(), config, opts.host, opts.port))?;
Ok(())
}

View File

@ -56,7 +56,7 @@ async fn dice(req: Request) -> Result<Response, Error> {
.text("")
.text(format!("You rolled {} and you got:", dice))
.text("")
.preformatted("", format!("{}", dice_roll(dice)?))
.preformatted(format!("{}", dice_roll(dice)?))
.text("")
.link("/dice", Some("Do another roll".to_string()));

View File

@ -26,7 +26,7 @@ impl MajHandler for Handler {
log::debug!("opening file {:?}", path);
match tokio::fs::metadata(&path).await {
match async_std::fs::metadata(&path).await {
Ok(stat) => {
if stat.is_dir() {
if r.url.as_str().ends_with('/') {
@ -43,9 +43,9 @@ impl MajHandler for Handler {
}
}
let mut file = tokio::fs::File::open(&path).await?;
let mut file = async_std::fs::File::open(&path).await?;
let mut buf: Vec<u8> = Vec::new();
tokio::io::copy(&mut file, &mut buf).await?;
async_std::io::copy(&mut file, &mut buf).await?;
// Send header.
if path.extension() == Some(OsStr::new("gmi"))

View File

@ -1,10 +1,11 @@
use crate::{Response, StatusCode};
use tokio::{
prelude::*,
use async_std::{
io::prelude::*,
net::{TcpListener, TcpStream},
stream::StreamExt,
task,
};
use tokio_rustls::TlsAcceptor;
use async_tls::TlsAcceptor;
use async_trait::async_trait;
use rustls::Certificate;
use std::{error::Error as StdError, net::SocketAddr, sync::Arc};
@ -52,10 +53,12 @@ where
{
let cfg = Arc::new(cfg);
let listener = TcpListener::bind(&format!("{}:{}", host, port)).await?;
let mut incoming = listener.incoming();
let acceptor = Arc::new(TlsAcceptor::from(cfg.clone()));
while let Ok((stream, addr)) = listener.accept().await {
while let Some(Ok(stream)) = incoming.next().await {
let h = h.clone();
let acceptor = acceptor.clone();
let addr = stream.peer_addr().unwrap();
let port = port.clone();
task::spawn(handle_request(h, stream, acceptor, addr, port));
@ -80,7 +83,7 @@ async fn handle_request(
if let Some(u_port) = url.port() {
if port != u_port {
let _ = write_header(
stream,
&mut stream,
StatusCode::ProxyRequestRefused,
"Cannot proxy to that URL",
)
@ -99,10 +102,14 @@ async fn handle_request(
Err(RequestParsingError::InvalidScheme(url.scheme().to_string()))?
}
let certs = stream
.peer_certificates()
.map(|certs| certs.into_iter().map(Certificate).collect());
let req = Request {
url: url,
addr: addr,
certs: None,
certs: certs,
};
handle(h, req, &mut stream, addr).await;
}
@ -114,7 +121,7 @@ async fn handle_request(
Ok(())
}
pub async fn write_header<W: AsyncWrite + Unpin>(
pub async fn write_header<W: Write + Unpin>(
mut stream: W,
status: StatusCode,
meta: &str,
@ -126,7 +133,7 @@ pub async fn write_header<W: AsyncWrite + Unpin>(
}
/// Return the URL requested by the client.
async fn parse_request<R: AsyncRead + Unpin>(mut stream: R) -> Result<Url> {
async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
// can use a fixed-sized buffer on the stack, avoiding allocations and
// copying, and stopping bad clients from making us use too much memory.
@ -164,7 +171,7 @@ async fn handle<T>(
stream: &mut T,
addr: SocketAddr,
) where
T: AsyncWrite + Unpin,
T: Write + Unpin,
{
let u = req.url.clone();
match h.handle(req).await {