parent
19a3943012
commit
ff80dc4730
@ -1,6 +1,16 @@
|
||||
Generate strings from markov chain of stdin
|
||||
HTTP server connecting to a Markov chain
|
||||
|
||||
Usage:
|
||||
Feeding:
|
||||
# PUT /put
|
||||
Request body is fed to the chain
|
||||
|
||||
$ cat corpus | markov
|
||||
$ cat corpus | markov <n of outputs to generate>
|
||||
NOTE: Strings fed to the chain must be valid UTF-8 and below 16 KB in size
|
||||
|
||||
Generating:
|
||||
# GET /get
|
||||
Generate a string from the chain
|
||||
|
||||
# GET /get/<number>
|
||||
Generate <number> strings from the chain
|
||||
|
||||
NOTE: Number must be lower than 256
|
@ -0,0 +1,74 @@
|
||||
use std::{
|
||||
net::{
|
||||
IpAddr,
|
||||
AddrParseError,
|
||||
},
|
||||
str,
|
||||
error,
|
||||
fmt,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct XFormatError;
|
||||
|
||||
impl error::Error for XFormatError{}
|
||||
|
||||
impl fmt::Display for XFormatError
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "X-Forwarded-For was not in the correct format")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Default)]
|
||||
pub struct XForwardedFor(Vec<IpAddr>);
|
||||
|
||||
impl XForwardedFor
|
||||
{
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self(Vec::new())
|
||||
}
|
||||
pub fn single(ip: impl Into<IpAddr>) -> Self
|
||||
{
|
||||
Self(vec![ip.into()])
|
||||
}
|
||||
pub fn addrs(&self) -> &[IpAddr]
|
||||
{
|
||||
&self.0[..]
|
||||
}
|
||||
|
||||
pub fn into_first(self) -> Option<IpAddr>
|
||||
{
|
||||
self.0.into_iter().next()
|
||||
}
|
||||
|
||||
pub fn into_addrs(self) -> Vec<IpAddr>
|
||||
{
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl str::FromStr for XForwardedFor
|
||||
{
|
||||
type Err = XFormatError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut output = Vec::new();
|
||||
for next in s.split(',')
|
||||
{
|
||||
output.push(next.trim().parse()?)
|
||||
}
|
||||
Ok(Self(output))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AddrParseError> for XFormatError
|
||||
{
|
||||
#[inline(always)] fn from(_: AddrParseError) -> Self
|
||||
{
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue