Skip to content

Commit

Permalink
Merge pull request #41 from kana-rus/improve_headers
Browse files Browse the repository at this point in the history
Improve headers
  • Loading branch information
kanarus authored Dec 30, 2023
2 parents 38babfd + 1934e9f commit d83dbb2
Show file tree
Hide file tree
Showing 46 changed files with 1,992 additions and 1,337 deletions.
7 changes: 4 additions & 3 deletions examples/form/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ async fn post_submit(c: Context, form_data: FormData) -> Response {

struct Logger;
impl IntoFang for Logger {
fn bite(self) -> ohkami::Fang {
fn into_fang(self) -> ohkami::Fang {
Fang(|_: &mut Context, req: &mut Request| {
println!("[request] {} {}", req.method(), req.path());
println!("[request] {} {}", req.method, req.path());

if let Some ((content_type, body)) = req.payload() {
if let Some(body) = req.payload() {
let content_type = req.headers.ContentType().unwrap();
println!("[payload] {content_type:?}\n{}", body.escape_ascii());
}
})
Expand Down
8 changes: 4 additions & 4 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ mod fangs {

pub struct AppendServer;
impl IntoFang for AppendServer {
fn bite(self) -> Fang {
fn into_fang(self) -> Fang {
Fang(|c: &mut Context| {
c.headers
c.set_headers()
.Server("ohkami");

tracing::info!("\
Expand All @@ -76,9 +76,9 @@ mod fangs {

pub struct LogRequest;
impl IntoFang for LogRequest {
fn bite(self) -> Fang {
fn into_fang(self) -> Fang {
Fang(|req: &mut Request| {
let __method__ = req.method();
let __method__ = req.method;
let __path__ = req.path();

tracing::info!("\
Expand Down
20 changes: 9 additions & 11 deletions ohkami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ license = "MIT"
features = ["rt_tokio"]

[dependencies]
ohkami_macros = { version = "0.4", path = "../ohkami_macros" }
ohkami_macros = { version = "0.5", path = "../ohkami_macros" }
tokio = { version = "1", optional = true, features = ["net", "rt", "io-util", "sync"] }
async-std = { version = "1", optional = true }
byte_reader = { version = "1.2.2", features = ["text"] }
serde = "1.0"
serde_json = "1.0"
chrono = "0.4"
percent-encoding = "2.2.0"
byte_reader = { version = "1.2.2", features = ["text"] }

[features]
rt_tokio = ["dep:tokio"]
Expand All @@ -36,11 +34,11 @@ DEBUG = [
"websocket",
"serde/derive",
"tokio?/macros",
"async-std?/attributes"
"async-std?/attributes",
]
# default = [
# "rt_tokio",
# #"rt_async-std",
# "DEBUG",
# # "nightly"
# ]
#default = [
# "rt_tokio",
# #"rt_async-std",
# "DEBUG",
# # "nightly"
#]
61 changes: 0 additions & 61 deletions ohkami/src/layer0_lib/content_type.rs

This file was deleted.

37 changes: 7 additions & 30 deletions ohkami/src/layer0_lib/cors.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#![allow(non_snake_case)]

use crate::http::Method;
use crate::{http::Method};


pub(crate) enum AccessControlAllowOrigin {
Any,
Only(&'static str),
} impl AccessControlAllowOrigin {
#[inline(always)] pub(crate) fn is_any(&self) -> bool {
#[inline(always)] pub(crate) const fn is_any(&self) -> bool {
match self {
Self::Any => true,
_ => false,
}
}

#[inline(always)] pub(crate) fn from_literal(lit: &'static str) -> Self {
match lit {
"*" => Self::Any,
origin => Self::Only(origin),
#[inline(always)] pub(crate) const fn from_literal(lit: &'static str) -> Self {
match lit.as_bytes() {
b"*" => Self::Any,
origin => Self::Only(unsafe{std::str::from_utf8_unchecked(origin)}),
}
}

Expand All @@ -44,7 +44,7 @@ pub struct CORS {
pub(crate) ExposeHeaders: Option<Vec<&'static str>>,
pub(crate) MaxAge: Option<u32>,
} impl CORS {
pub(crate) fn new(AllowOrigin: &'static str) -> Self {
pub(crate) const fn new(AllowOrigin: &'static str) -> Self {
Self {
AllowOrigin: AccessControlAllowOrigin::from_literal(AllowOrigin),
AllowCredentials: false,
Expand All @@ -54,29 +54,6 @@ pub struct CORS {
MaxAge: None,
}
}

pub(crate) fn to_string(&self) -> String {
let mut h = format!("Access-Control-Allow-Origin: {}\r\n", self.AllowOrigin.as_str());
if self.AllowCredentials {
h.push_str("Access-Control-Allow-Credentials: true\r\n");
}
if let Some(seconds) = &self.MaxAge {
h.push_str(&format!("Access-Control-Max-Age: {seconds}\r\n"));
}
if let Some(methods) = &self.AllowMethods {
let methods = methods.into_iter().map(|m| m.to_string()).collect::<Vec<_>>().join(",");
h.push_str(&format!("Access-Control-Allow-Methods: {methods}\r\n"));
}
if let Some(headers) = &self.AllowHeaders {
let headers = headers.join(",");
h.push_str(&format!("Access-Control-Allow-Headers: {headers}\r\n"));
}
if let Some(headers) = &self.ExposeHeaders {
let headers = headers.join(",");
h.push_str(&format!("Access-Control-Expose-Headers: {headers}\r\n"));
}
h
}
}

impl CORS {
Expand Down
9 changes: 0 additions & 9 deletions ohkami/src/layer0_lib/datetime.rs

This file was deleted.

8 changes: 8 additions & 0 deletions ohkami/src/layer0_lib/headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub(crate) mod client;
pub(crate) mod server;


pub fn append(value: impl Into<std::borrow::Cow<'static, str>>) -> Append {
Append(value.into())
}
pub struct Append(std::borrow::Cow<'static, str>);
Loading

0 comments on commit d83dbb2

Please sign in to comment.