Authentication untested.
This commit is contained in:
@@ -26,7 +26,7 @@ pub mod listener;
|
||||
pub mod parser;
|
||||
pub mod utils;
|
||||
|
||||
use std::{collections::BTreeMap, fmt::Display, net::SocketAddr};
|
||||
use std::{collections::BTreeMap, fmt::Display, net::SocketAddr, time::Duration};
|
||||
|
||||
use rustls::ServerConfig;
|
||||
use tokio::net::TcpSocket;
|
||||
@@ -70,6 +70,12 @@ pub enum ServerProtocol {
|
||||
Http,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
||||
pub struct Rate {
|
||||
pub requests: u64,
|
||||
pub period: Duration,
|
||||
}
|
||||
|
||||
impl Display for ServerProtocol {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
use std::{net::IpAddr, time::Duration};
|
||||
|
||||
use super::Config;
|
||||
use super::{Config, Rate};
|
||||
|
||||
impl Config {
|
||||
pub fn property<T: ParseValue>(&self, key: impl AsKey) -> super::Result<Option<T>> {
|
||||
@@ -35,6 +35,16 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn property_or_static<T: ParseValue>(
|
||||
&self,
|
||||
key: impl AsKey,
|
||||
default: &str,
|
||||
) -> super::Result<T> {
|
||||
let key = key.as_key();
|
||||
let value = self.keys.get(&key).map_or(default, |v| v.as_str());
|
||||
T::parse_value(key, value)
|
||||
}
|
||||
|
||||
pub fn property_or_default<T: ParseValue>(
|
||||
&self,
|
||||
key: impl AsKey,
|
||||
@@ -368,6 +378,36 @@ impl ParseValue for Duration {
|
||||
}
|
||||
}
|
||||
|
||||
impl ParseValue for Rate {
|
||||
fn parse_value(key: impl AsKey, value: &str) -> super::Result<Self> {
|
||||
if let Some((requests, period)) = value.split_once('/') {
|
||||
Ok(Rate {
|
||||
requests: requests
|
||||
.trim()
|
||||
.parse::<u64>()
|
||||
.ok()
|
||||
.and_then(|r| if r > 0 { Some(r) } else { None })
|
||||
.ok_or_else(|| {
|
||||
format!(
|
||||
"Invalid rate value {:?} for property {:?}.",
|
||||
value,
|
||||
key.as_key()
|
||||
)
|
||||
})?,
|
||||
period: period.parse_key(key)?,
|
||||
})
|
||||
} else if ["false", "none", "unlimited"].contains(&value) {
|
||||
Ok(Rate::default())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Invalid rate value {:?} for property {:?}.",
|
||||
value,
|
||||
key.as_key()
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsKey: Clone {
|
||||
fn as_key(&self) -> String;
|
||||
fn as_prefix(&self) -> String;
|
||||
|
||||
@@ -19,6 +19,7 @@ pub struct ConcurrencyLimiter {
|
||||
pub concurrent: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct InFlight {
|
||||
concurrent: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(
|
||||
Debug, serde::Serialize, serde::Deserialize, Clone, PartialOrd, Ord, PartialEq, Eq, Hash,
|
||||
Debug, serde::Serialize, serde::Deserialize, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash,
|
||||
)]
|
||||
pub struct Bitmap<T: BitmapItem> {
|
||||
pub bitmap: u64,
|
||||
@@ -66,6 +66,12 @@ impl<T: BitmapItem> Bitmap<T> {
|
||||
self.bitmap |= 1 << item.into();
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn with_item(mut self, item: T) -> Self {
|
||||
self.insert(item);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn remove(&mut self, item: T) {
|
||||
debug_assert!(item.is_valid());
|
||||
@@ -88,6 +94,16 @@ impl<T: BitmapItem> Bitmap<T> {
|
||||
self.bitmap & (1 << item.into()) != 0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn contains_any(&self, items: impl Iterator<Item = T>) -> bool {
|
||||
for item in items {
|
||||
if self.bitmap & (1 << item.into()) != 0 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.bitmap == 0
|
||||
@@ -127,6 +143,12 @@ impl<T: BitmapItem> Deref for Bitmap<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: BitmapItem> From<Bitmap<T>> for u64 {
|
||||
fn from(value: Bitmap<T>) -> Self {
|
||||
value.bitmap
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: BitmapItem> Iterator for Bitmap<T> {
|
||||
type Item = T;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user