Compare commits

..

26 Commits
main ... main

Author SHA1 Message Date
Cadey Ratio e9bcdf9f52 Merge pull request 'Add support for alt-text in preformatted blocks' (#15) from alch_emii/maj-prs:alt-text into main
Reviewed-on: cadey/maj#15
2021-09-08 13:30:32 +00:00
Emi Tatsuo c49c76b5ad
Merge remote-tracking branch 'upstream/main' into alt-text
continuous-integration/drone/pr Build is passing Details
2020-12-15 09:09:22 -05:00
Cadey Ratio f6424dca1a Merge pull request 'Update tokio & rustls, remove async-std & async-tls' (#19) from alch_emii/maj-prs:remove-async-std into main
continuous-integration/drone/push Build encountered an error Details
Reviewed-on: cadey/maj#19
2020-12-15 01:24:37 +00:00
Cadey Ratio 6524ee688a Merge pull request 'Enable CI tests for pull requests' (#17) from alch_emii/maj-prs:pr-test into main
continuous-integration/drone/push Build encountered an error Details
Reviewed-on: cadey/maj#17
2020-12-15 01:21:10 +00:00
Emi Tatsuo aaac9d0c93
Updated other workspace members
continuous-integration/drone/pr Build is passing Details
2020-12-11 10:07:01 -05:00
Emi Tatsuo 2566d930bf
Remove dependency on async-std & async-tls in maj, upgrade Tokio
Still gotta patch out the other crates tho
2020-12-10 14:51:40 -05:00
Emi Tatsuo 3b4e9c77ec
Use triggers instead of step conditions in .drone.yml where relevant
continuous-integration/drone/pr Build is failing Details
2020-12-10 13:12:30 -05:00
Emi Tatsuo accd372320
Enable CI tests for pull requests
continuous-integration/drone/pr Build is failing Details
2020-12-10 12:10:34 -05:00
Emi Tatsuo 5a6208dedf
Fix dependant workspace members
continuous-integration/drone/pr Build encountered an error Details
2020-12-10 11:53:57 -05:00
Emi Tatsuo 3dadcc05ba
Merge remote-tracking branch 'upstream/main' into alt-text
continuous-integration/drone/pr Build is passing Details
2020-12-10 11:04:22 -05:00
Cadey Ratio d08994c0cb Merge pull request 'Add a doctest for `blank_line()`' (#16) from alch_emii/maj-prs:blank-line-doctest into main
continuous-integration/drone/push Build is failing Details
Reviewed-on: cadey/maj#16
2020-12-10 12:47:31 +00:00
Emi Tatsuo e44decab07
Merge remote-tracking branch 'upstream/main' into blank-line-doctest
continuous-integration/drone/pr Build is passing Details
2020-12-07 18:27:26 -05:00
Cadey Ratio cf73a3bb1e Merge pull request 'Add a `blank_line()` method to `Builder`' (#13) from alch_emii/maj-prs:blank-line into main
continuous-integration/drone/push Build is failing Details
Reviewed-on: cadey/maj#13
2020-12-06 01:14:19 +00:00
Cadey Ratio 87a96151b5 Merge pull request 'Add conversion traits to Builder' (#12) from alch_emii/maj-prs:to-string into main
continuous-integration/drone/push Build is failing Details
Reviewed-on: cadey/maj#12
2020-12-06 01:13:21 +00:00
Emii Tatsuo 9bc2ea1159
Fix bug with type inferring for the `preformatted()` method
continuous-integration/drone/pr Build encountered an error Details
2020-11-30 14:17:13 -05:00
Emii Tatsuo 4c86fbba1e
Add support for alt-text in preformatted blocks ⚠️
continuous-integration/drone/pr Build is failing Details
⚠️  Breaking Change ⚠️

This adds support for parsing and rendering alt-text in preformatted blocks.  This changes the public API both around the `Node` enum and the `preformatted()` method of the builder.  I cannot think of a way to avoid this that is not needlessly overcomplicated, except maybe merging the preformatted & body into a single newline delimited string but that's hella gross and i don't think anyone wants that.  Lemme know if you can think of a better way of doing this

Preformatted blocks without alt text can still be created by passing in an empty string.  Because alt text isn't separated by a space, this does not add any unnecessary padding.  I chose not to accept Option<String> here because an empty string serves the same function but encourages users to use alt-text in their documents, which is very important for increasing accessibility.
2020-11-30 14:05:12 -05:00
Emii Tatsuo a7fabdc909
Allow Builder as Into<Vec<Node>>
continuous-integration/drone/pr Build encountered an error Details
2020-11-30 01:40:23 -05:00
Emii Tatsuo adf82e9d9b
Add a doctest for the `blank_line()` method
continuous-integration/drone/pr Build is passing Details
This adds a simple doctest for `blank_line()` but will not be included in the original PR because it is contingent on the merge of a pending PR for adding the `to_string()` method.
2020-11-30 01:17:41 -05:00
Emii Tatsuo c69ec3b7df
Merge branch 'to-string' into blank-line 2020-11-30 01:16:37 -05:00
Emii Tatsuo ac88fb60ee
Add a `blank_line()` method to `Builder`
continuous-integration/drone/pr Build is failing Details
Many times users may want to seperate lines of text using a blank line.  Currently, this can be accomplished by either adding '\n' to the previous `text()` call, which requires that the user has the ability to access this, and can also look a little messy, or by calling `text()` with an empty string, which definately works, but having an explicit method might be a nice sugar for a lot of users, and barely adds any weight to the codebase.

This is definately a small thing, and almost closer to a personal preferance than anything, but I definately think it would make the library a tiny bit nicer to use, and there's barely any tradeoff.  It's still up to you though if you'd rather keep your codebase small.
2020-11-30 01:11:12 -05:00
Emii Tatsuo 2f3dd72d90
Add AsRef and AsMut<[Node]> to builder
continuous-integration/drone/pr Build encountered an error Details
2020-11-30 00:52:08 -05:00
Emii Tatsuo 34dca8d92d
Impl ToString for Builder, accept AsRef<[Node]> in `render()`
continuous-integration/drone/pr Build is failing Details
This adds a to_string method to the `Builder` allowing for the easy conversion of a Vec<Node> into a String, for any usecases where a library might not be directly writing to an io::Write, or may want to do String-y things with your document first.  Without this, users would have to write to a Vec<u8> and convert to a String, which is kinda unintuitive, takes a lot of steps, and doesn't produce very readable code.  This simplifies it to one method call.

* Implementation of the std::str::ToString method for Builder
* Accepting any AsRef<[Node]> in render (including accepting the old Vec<Node>, so not breaking)
* Addition of estimate_len() to Node, used to pre-allocate the correct size of the String buffer

* `estimate_len` has some quick doctests and examples.  I know most of the rest of the project uses test methods, but I hope this is alright given that the tests may add some more clarity to the purpose and function of the method.
* `to_string` has a single line of unsafe code.  As the associated comment explains, this is provably safe, and exists just to avoid having to choose between having a bunch of duplicate code or inefficiently performing a UTF-8 check on a whole bunch bytes that we already know are safe.  That said, I totally get it if you're just generally against unsafe code and will change it to be an alternative if you so wish
* ToString is implemented instead of Display.  This is to discourage users from directly using this in a println!() or write!() macro, which would not be a thing you would normally expect to do with this.  It also gives us the advantage of being able to pre-allocate a buffer size, meaning less expensive String resizing.
* I couldn't think of a clever way to get `render()` to work with both `io::Write`s or `fmt::Write`s without duplicating the code, but I'm dumb and might be missing something, so if there's a way to do that instead of doing my funky unsafe hack that's cool and I can do that instead.
2020-11-29 23:17:15 -05:00
Cadey Ratio c743056263 Remove kindlegen from shell.nix
continuous-integration/drone/push Build is passing Details
Closes #11
2020-11-01 22:47:28 +00:00
Cadey Ratio bebfa4d7b1 release gemtext 0.2.1 with clone fix from @boringcactus
continuous-integration/drone/push Build is passing Details
2020-10-06 17:43:42 -04:00
Cadey Ratio 725957bf8c Merge pull request 'make gemtext::Node `Clone`' (#10) from boringcactus/maj:make-gemtext-node-clone into main
continuous-integration/drone/push Build is passing Details
Reviewed-on: cadey/maj#10
2020-10-06 21:42:28 +00:00
Melody Horn c07d81077a make gemtext::Node `Clone`
continuous-integration/drone/pr Build is passing Details
2020-10-05 04:38:19 -06:00
15 changed files with 185 additions and 67 deletions

View File

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

View File

@ -10,8 +10,6 @@ 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" }
async-trait = { version = "0", optional = true }
log = "0.4"
mime_guess = "2.0"
@ -19,11 +17,11 @@ num = "0.2"
num-derive = "0.3"
num-traits = "0.2"
once_cell = "1.4"
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
rustls = { version = "0.19", optional = true, features = ["dangerous_configuration"] }
structopt = "0.3"
thiserror = "1"
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
tokio = { version = "0.2", features = ["full"], optional = true }
tokio-rustls = { version = "0.21", features = ["dangerous_configuration"] }
tokio = { version = "0.3", features = ["full"] }
url = "2"
webpki-roots = { version = "0.20", optional = true }
webpki = { version = "0.21.0", optional = true }
@ -37,12 +35,8 @@ pretty_env_logger = "0.4"
default = ["client", "server"]
client = [
"tokio-rustls",
"webpki",
"webpki-roots",
"tokio",
"async-std",
"async-tls/client"
]
server = [
@ -50,8 +44,6 @@ server = [
"webpki",
"webpki-roots",
"async-trait",
"async-std",
"async-tls/server"
]
[workspace]

View File

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

View File

@ -18,6 +18,28 @@ 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(),
@ -26,8 +48,12 @@ impl Builder {
self
}
pub fn preformatted<T: Into<String>>(mut self, data: T) -> Builder {
self.nodes.push(Node::Preformatted(data.into()));
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() });
self
}
@ -54,11 +80,53 @@ 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: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()> {
use Node::*;
for node in nodes {
for node in nodes.as_ref() {
match node {
Text(body) => {
let special_prefixes = ["=>", "```", "#", "*", ">"];
@ -71,8 +139,8 @@ pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
Some(name) => write!(out, "=> {} {}\n", to, name)?,
None => write!(out, "=> {}\n", to)?,
},
Preformatted(body) => write!(out, "```\n{}\n```\n", body)?,
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(level as usize), body)?,
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n", alt, body)?,
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(*level as usize), body)?,
ListItem(body) => write!(out, "* {}\n", body)?,
Quote(body) => write!(out, "> {}\n", body)?,
};
@ -82,7 +150,7 @@ pub fn render(nodes: Vec<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)]
#[derive(Debug, PartialEq, Eq, Clone)]
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
@ -123,7 +191,19 @@ 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.
Preformatted(String),
///
/// 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 },
/// Lines beginning with "#" are heading lines. Heading lines consist of one,
/// two or three consecutive "#" characters, followed by optional whitespace,
@ -166,24 +246,73 @@ 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 line.starts_with("```") {
if let Some(trailing) = line.strip_prefix("```") {
collect_preformatted = !collect_preformatted;
if !collect_preformatted {
result.push(Node::Preformatted(
String::from_utf8(preformatted_buffer)
result.push(Node::Preformatted {
alt: alt.to_string(),
body: String::from_utf8(preformatted_buffer)
.unwrap()
.trim_end()
.to_string(),
));
});
preformatted_buffer = vec![];
} else {
alt = trailing.trim();
}
continue;
}
@ -282,13 +411,13 @@ mod tests {
#[test]
fn preformatted() {
let _ = pretty_env_logger::try_init();
let msg = "```\n\
hi there\n\
let msg = "```hi there\n\
obi-wan kenobi\n\
```\n\
\n\
Test\n";
let expected: Vec<Node> = vec![
Node::Preformatted("hi there".to_string()),
Node::Preformatted{ alt: "hi there".to_string(), body: "obi-wan kenobi".to_string() },
Node::Text(String::new()),
Node::Text("Test".to_string()),
];
@ -338,7 +467,7 @@ mod tests {
let _ = pretty_env_logger::try_init();
let msg = include_str!("../../testdata/ambig_preformatted.gmi");
let expected: Vec<Node> = vec![
Node::Preformatted("FOO".to_string()),
Node::Preformatted { alt: "foo".to_string(), body: "FOO".to_string() },
Node::Text("Foo bar".to_string()),
];
assert_eq!(expected, parse(msg));

View File

@ -11,7 +11,7 @@ cursive = "0.15"
log = "0.4"
url = "2"
webpki = "0.21.0"
rustls = { version = "0.18", features = ["dangerous_configuration"] }
rustls = { version = "0.19", 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(data) => styled.append(StyledString::plain(data)),
Preformatted { body, .. } => styled.append(StyledString::plain(body)),
Heading { level, body } => styled.append(StyledString::styled(
format!("{} {}", "#".repeat(level as usize), body),
Style::from(Effect::Bold),

View File

@ -8,15 +8,14 @@ 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.18", features = ["dangerous_configuration"] }
rustls = { version = "0.19", features = ["dangerous_configuration"] }
structopt = "0.3"
tokio = { version = "0.2", features = ["full"] }
tokio = { version = "0.3", 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(body) => write!(out, "```\n{}\n```\n\n", body)?,
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n\n", alt, body)?,
Heading { level, body } => {
write!(out, "##{} {}\n\n", "#".repeat(level as usize), body)?
}

View File

@ -6,8 +6,6 @@ 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
@ -15,10 +13,6 @@ in pkgs.mkShell {
pkg-config
ncurses
kindlegen
nur.repos.mic92.pandoc-bin
tex
];
RUST_LOG="info,majsite=debug,majsite::server=debug";

View File

@ -9,7 +9,6 @@ build = "build.rs"
[dependencies]
anyhow = "1"
async-std = "1.5"
async-trait = "0"
dnd_dice_roller = "0.3"
env_logger = "0"
@ -17,13 +16,14 @@ log = "0"
mime = "0.3.0"
percent-encoding = "2"
rand = "0"
rustls = { version = "0.18", features = ["dangerous_configuration"] }
rustls = { version = "0.19", 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,7 +80,12 @@ fn gemtext_to_html(inp: Vec<u8>) -> (String, impl ToHtml) {
name.as_ref().or(Some(&to.to_string())).unwrap()
)
.unwrap(),
Preformatted(body) => write!(buf, "<code><pre>{}</pre></code>", body).unwrap(),
Preformatted { alt, body } => write!(
buf,
"<code><pre title = \"{}\">{}</pre></code>",
alt.replace("\"", "\\\"").replace("\\", "\\\\"),
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,7 +64,8 @@ fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
}
fn main() -> Result<(), maj::server::Error> {
#[tokio::main]
async fn main() -> Result<(), maj::server::Error> {
env_logger::init();
let opts = Options::from_args();
let certs = load_certs(&opts.cert).unwrap();
@ -96,7 +97,7 @@ fn main() -> Result<(), maj::server::Error> {
thread::spawn(move || http::run(h.clone(), port));
}
task::block_on(maj::server::serve(h.clone(), config, opts.host, opts.port))?;
maj::server::serve(h.clone(), config, opts.host, opts.port).await?;
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 async_std::fs::metadata(&path).await {
match tokio::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 = async_std::fs::File::open(&path).await?;
let mut file = tokio::fs::File::open(&path).await?;
let mut buf: Vec<u8> = Vec::new();
async_std::io::copy(&mut file, &mut buf).await?;
tokio::io::copy(&mut file, &mut buf).await?;
// Send header.
if path.extension() == Some(OsStr::new("gmi"))

View File

@ -1,11 +1,10 @@
use crate::{Response, StatusCode};
use async_std::{
io::prelude::*,
use tokio::{
prelude::*,
net::{TcpListener, TcpStream},
stream::StreamExt,
task,
};
use async_tls::TlsAcceptor;
use tokio_rustls::TlsAcceptor;
use async_trait::async_trait;
use rustls::Certificate;
use std::{error::Error as StdError, net::SocketAddr, sync::Arc};
@ -53,12 +52,10 @@ 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 Some(Ok(stream)) = incoming.next().await {
while let Ok((stream, addr)) = listener.accept().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));
@ -83,7 +80,7 @@ async fn handle_request(
if let Some(u_port) = url.port() {
if port != u_port {
let _ = write_header(
&mut stream,
stream,
StatusCode::ProxyRequestRefused,
"Cannot proxy to that URL",
)
@ -117,7 +114,7 @@ async fn handle_request(
Ok(())
}
pub async fn write_header<W: Write + Unpin>(
pub async fn write_header<W: AsyncWrite + Unpin>(
mut stream: W,
status: StatusCode,
meta: &str,
@ -129,7 +126,7 @@ pub async fn write_header<W: Write + Unpin>(
}
/// Return the URL requested by the client.
async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
async fn parse_request<R: AsyncRead + 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.
@ -167,7 +164,7 @@ async fn handle<T>(
stream: &mut T,
addr: SocketAddr,
) where
T: Write + Unpin,
T: AsyncWrite + Unpin,
{
let u = req.url.clone();
match h.handle(req).await {