-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start preparing for http server code utils
- Loading branch information
glendc
committed
Nov 7, 2023
1 parent
5ebf5af
commit 2245492
Showing
16 changed files
with
214 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use http::{ | ||
header::{AsHeaderName, GetAll}, | ||
HeaderValue, Request, Response, | ||
}; | ||
|
||
pub trait HeaderValueGetter { | ||
fn header_value<K>(&self, key: K) -> Option<&HeaderValue> | ||
where | ||
K: AsHeaderName; | ||
fn header_values<K>(&self, key: K) -> GetAll<'_, HeaderValue> | ||
where | ||
K: AsHeaderName; | ||
} | ||
|
||
impl<Body> HeaderValueGetter for Request<Body> { | ||
fn header_value<K>(&self, key: K) -> Option<&HeaderValue> | ||
where | ||
K: AsHeaderName, | ||
{ | ||
self.headers().get(key) | ||
} | ||
|
||
fn header_values<K>(&self, key: K) -> GetAll<'_, HeaderValue> | ||
where | ||
K: AsHeaderName, | ||
{ | ||
self.headers().get_all(key) | ||
} | ||
} | ||
|
||
impl<Body> HeaderValueGetter for Response<Body> { | ||
fn header_value<K>(&self, key: K) -> Option<&HeaderValue> | ||
where | ||
K: AsHeaderName, | ||
{ | ||
self.headers().get(key) | ||
} | ||
|
||
fn header_values<K>(&self, key: K) -> GetAll<'_, HeaderValue> | ||
where | ||
K: AsHeaderName, | ||
{ | ||
self.headers().get_all(key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod headers; | ||
pub use headers::HeaderValueGetter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod tcp; | ||
pub use tcp::TcpStream; | ||
|
||
pub mod http; | ||
|
||
pub use tokio::net::ToSocketAddrs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use std::{ | ||
io, | ||
net::SocketAddr, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use tokio::net::TcpStream as TokioTcpStream; | ||
|
||
use crate::{ | ||
state::{Extendable, Extensions}, | ||
stream::{AsyncRead, AsyncWrite, ReadBuf}, | ||
}; | ||
|
||
pin_project_lite::pin_project! { | ||
#[derive(Debug)] | ||
pub struct TcpStream<S> { | ||
#[pin] | ||
inner: S, | ||
extensions: Extensions, | ||
} | ||
} | ||
|
||
impl<S> TcpStream<S> { | ||
pub fn new(inner: S) -> Self { | ||
Self { | ||
inner, | ||
extensions: Extensions::new(), | ||
} | ||
} | ||
|
||
pub fn into_parts(self) -> (S, Extensions) { | ||
(self.inner, self.extensions) | ||
} | ||
|
||
pub fn from_parts(inner: S, extensions: Extensions) -> Self { | ||
Self { inner, extensions } | ||
} | ||
} | ||
|
||
impl TcpStream<TokioTcpStream> { | ||
pub fn peer_addr(&self) -> io::Result<SocketAddr> { | ||
self.inner.peer_addr() | ||
} | ||
|
||
pub fn local_addr(&self) -> io::Result<SocketAddr> { | ||
self.inner.local_addr() | ||
} | ||
|
||
pub fn ttl(&self) -> io::Result<u32> { | ||
self.inner.ttl() | ||
} | ||
|
||
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> { | ||
self.inner.set_ttl(ttl) | ||
} | ||
} | ||
|
||
impl<S> Extendable for TcpStream<S> { | ||
fn extensions(&self) -> &Extensions { | ||
&self.extensions | ||
} | ||
|
||
fn extensions_mut(&mut self) -> &mut Extensions { | ||
&mut self.extensions | ||
} | ||
} | ||
|
||
impl<S> AsyncRead for TcpStream<S> | ||
where | ||
S: AsyncRead, | ||
{ | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<io::Result<()>> { | ||
self.project().inner.poll_read(cx, buf) | ||
} | ||
} | ||
|
||
impl<S> AsyncWrite for TcpStream<S> | ||
where | ||
S: AsyncWrite, | ||
{ | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<io::Result<usize>> { | ||
self.project().inner.poll_write(cx, buf) | ||
} | ||
|
||
fn poll_write_vectored( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &[io::IoSlice<'_>], | ||
) -> Poll<io::Result<usize>> { | ||
self.project().inner.poll_write_vectored(cx, bufs) | ||
} | ||
|
||
fn is_write_vectored(&self) -> bool { | ||
self.inner.is_write_vectored() | ||
} | ||
|
||
#[inline] | ||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
self.project().inner.poll_flush(cx) | ||
} | ||
|
||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
self.project().inner.poll_shutdown(cx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod header; | ||
pub mod layer; | ||
pub mod service; | ||
|
||
mod conn; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod tracker; | ||
pub use tracker::{BytesRWTrackerHandle, BytesTrackerLayer, BytesTrackerService}; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters