pub struct Request<State> { /* private fields */ }Expand description
HTTP request.
请求、路由参数以及访问请求的各种方式。 中间件和endpoints之间的通信
Implementations
sourceimpl<State> Request<State>
impl<State> Request<State>
sourcepub fn method(&self) -> Method
pub fn method(&self) -> Method
访问请求的HTTP方法。
Examples
use summer_boot::Request;
let mut app = summer_boot::new();
app.at("/").get(|req: Request<()>| async move {
assert_eq!(req.method(), http_types::Method::Get);
Ok("")
});
app.listen("127.0.0.1:8080").await?;sourcepub fn version(&self) -> Option<Version>
pub fn version(&self) -> Option<Version>
访问请求的HTTP版本。
Examples
use summer_boot::Request;
let mut app = summer_boot::new();
app.at("/").get(|req: Request<()>| async move {
assert_eq!(req.version(), Some(http_types::Version::Http1_1));
Ok("")
});
app.listen("127.0.0.1:8080").await?;sourcepub fn local_addr(&self) -> Option<&str>
pub fn local_addr(&self) -> Option<&str>
获取基础传输的本地地址
sourcepub fn remote(&self) -> Option<&str>
pub fn remote(&self) -> Option<&str>
获取此请求的远程地址。
按以下优先级确定:
Forwardedheadforkey- 第一个
X-Forwarded-Forheader - 传输的对等地址
sourcepub fn host(&self) -> Option<&str>
pub fn host(&self) -> Option<&str>
获取此请求的目标主机。
按以下优先级确定:
Forwardedheaderhostkey- 第一个
X-Forwarded-Hostheader Hostheader- URL域
sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
以“Mime”形式获取请求内容类型。
这将获取请求 Content-Type header。
sourcepub fn header(&self, key: impl Into<HeaderName>) -> Option<&HeaderValues>
pub fn header(&self, key: impl Into<HeaderName>) -> Option<&HeaderValues>
获取HTTP header.
Examples
use summer_boot::Request;
let mut app = summer_boot::new();
app.at("/").get(|req: Request<()>| async move {
assert_eq!(req.header("X-Forwarded-For").unwrap(), "127.0.0.1");
Ok("")
});
app.listen("127.0.0.1:8080").await?;sourcepub fn header_mut(
&mut self,
name: impl Into<HeaderName>
) -> Option<&mut HeaderValues>
pub fn header_mut(
&mut self,
name: impl Into<HeaderName>
) -> Option<&mut HeaderValues>
获取标题的可变引用。
sourcepub fn insert_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues
) -> Option<HeaderValues>
pub fn insert_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues
) -> Option<HeaderValues>
设置一个 HTTP header.
sourcepub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues
)
pub fn append_header(
&mut self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues
)
将header添加到headers。
与 insert 不同,此函数不会重写标头的内容,而是插入
如果没有header。添加到现有的headers列表中。
sourcepub fn remove_header(
&mut self,
name: impl Into<HeaderName>
) -> Option<HeaderValues>
pub fn remove_header(
&mut self,
name: impl Into<HeaderName>
) -> Option<HeaderValues>
移除一个 header.
sourcepub fn header_names(&self) -> Names<'_>
pub fn header_names(&self) -> Names<'_>
以任意顺序访问所有header名称的迭代。
sourcepub fn header_values(&self) -> Values<'_>
pub fn header_values(&self) -> Values<'_>
以任意顺序访问所有header值的迭代。
sourcepub fn param(&self, key: &str) -> Result<&str>
pub fn param(&self, key: &str) -> Result<&str>
按名称提取和解析路由参数。
以 &str 形式返回参数,该参数是从此 Request 借用的。
名称应不包括引用 :。
Errors
如果 key 不是路由的有效参数,则返回错误。
Examples
use summer_boot::{Request, Result};
async fn greet(req: Request<()>) -> Result<String> {
let name = req.param("name").unwrap_or("world");
Ok(format!("Hello, {}!", name))
}
let mut app = summer_boot::new();
app.at("/hello").get(greet);
app.at("/hello/:name").get(greet);
app.listen("127.0.0.1:8080").await?;sourcepub fn wildcard(&self) -> Option<&str>
pub fn wildcard(&self) -> Option<&str>
从路由中提取通配符(如果存在)
以 &str 形式返回参数,该参数是从此 Request 借用的。
Examples
use summer_boot::{Request, Result};
async fn greet(req: Request<()>) -> Result<String> {
let name = req.wildcard().unwrap_or("world");
Ok(format!("Hello, {}!", name))
}
let mut app = summer_boot::new();
app.at("/hello/*").get(greet);
app.listen("127.0.0.1:8080").await?;sourcepub fn query<'de, T: Deserialize<'de>>(&'de self) -> Result<T>
pub fn query<'de, T: Deserialize<'de>>(&'de self) -> Result<T>
使用serde_qs将URL查询组件解析为结构
将整个查询作为未解析的字符串获取,使用 request.url().query()。
Examples
use std::collections::HashMap;
use summer_boot::http_types::{self, convert::Deserialize};
use summer_boot::Request;
// 所有权结构:
#[derive(Deserialize)]
struct Index {
page: u32,
selections: HashMap<String, String>,
}
let req: Request<()> = http_types::Request::get("https://baidu.com/get?page=2&selections[width]=narrow&selections[height]=tall").into();
let Index { page, selections } = req.query().unwrap();
assert_eq!(page, 2);
assert_eq!(selections["width"], "narrow");
assert_eq!(selections["height"], "tall");
// 使用借用s:
#[derive(Deserialize)]
struct Query<'q> {
format: &'q str,
}
let req: Request<()> = http_types::Request::get("https://httpbin.org/get?format=bananna").into();
let Query { format } = req.query().unwrap();
assert_eq!(format, "bananna");sourcepub fn take_body(&mut self) -> Body
pub fn take_body(&mut self) -> Body
处理请求 Body
可以在获取或读取body后调用此方法,
但是将返回一个空的Body.
这对于通过AsyncReader或AsyncBufReader有用。
sourcepub async fn body_bytes(&mut self) -> Result<Vec<u8>>
pub async fn body_bytes(&mut self) -> Result<Vec<u8>>
将整个请求body读取字节缓冲区。
可以在读取body后调用此方法,但生成空缓冲区。
Errors
读取body时遇到的任何I/O错误都会立即返回错误 Err
Examples
use summer_boot::Request;
let mut app = summer_boot::new();
app.at("/").get(|mut req: Request<()>| async move {
let _body: Vec<u8> = req.body_bytes().await.unwrap();
Ok("")
});
app.listen("127.0.0.1:8080").await?;sourcepub async fn body_string(&mut self) -> Result<String>
pub async fn body_string(&mut self) -> Result<String>
将整个请求body读取字符串。
可以在读取body后调用此方法,但生成空缓冲区。
Errors
读取body时遇到的任何I/O错误都会立即返回错误 Err
如果body不能解释有效的UTF-8,则返回 Err
Examples
use summer_boot::Request;
let mut app = summer_boot::new();
app.at("/").get(|mut req: Request<()>| async move {
let _body: String = req.body_string().await.unwrap();
Ok("")
});
app.listen("127.0.0.1:8080").await?;sourcepub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
pub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
sourcepub async fn body_form<T: DeserializeOwned>(&mut self) -> Result<T>
pub async fn body_form<T: DeserializeOwned>(&mut self) -> Result<T>
将请求主体解析为表单
use serde::Deserialize;
let mut app = summer_boot::new();
#[derive(Deserialize)]
struct Animal {
name: String,
legs: u8
}
app.at("/").post(|mut req: summer_boot::Request<()>| async move {
let animal: Animal = req.body_form().await?;
Ok(format!(
"hello, {}! i've put in an order for {} shoes",
animal.name, animal.legs
))
});
app.listen("localhost:8000").await?;
// $ curl localhost:8000/test/api -d "name=chashu&legs=4"
// hello, chashu! i've put in an order for 4 shoes
// $ curl localhost:8000/test/api -d "name=mary%20millipede&legs=750"
// number too large to fit in target typeTrait Implementations
sourceimpl<State> AsyncRead for Request<State>
impl<State> AsyncRead for Request<State>
sourcefn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize>>
Attempt to read from the AsyncRead into buf. Read more
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
Attempt to read from the AsyncRead into bufs using vectored
IO operations. Read more
sourceimpl<State> Index<&str> for Request<State>
impl<State> Index<&str> for Request<State>
sourcefn index(&self, name: &str) -> &HeaderValues
fn index(&self, name: &str) -> &HeaderValues
type Output = HeaderValues
type Output = HeaderValues
The returned type after indexing.
sourceimpl<State> Index<HeaderName> for Request<State>
impl<State> Index<HeaderName> for Request<State>
sourcefn index(&self, name: HeaderName) -> &HeaderValues
fn index(&self, name: HeaderName) -> &HeaderValues
type Output = HeaderValues
type Output = HeaderValues
The returned type after indexing.
sourceimpl<State> IntoIterator for Request<State>
impl<State> IntoIterator for Request<State>
type Item = (HeaderName, HeaderValues)
type Item = (HeaderName, HeaderValues)
The type of the elements being iterated over.
sourceimpl<'a, State> IntoIterator for &'a Request<State>
impl<'a, State> IntoIterator for &'a Request<State>
type Item = (&'a HeaderName, &'a HeaderValues)
type Item = (&'a HeaderName, &'a HeaderValues)
The type of the elements being iterated over.
sourceimpl<'a, State> IntoIterator for &'a mut Request<State>
impl<'a, State> IntoIterator for &'a mut Request<State>
type Item = (&'a HeaderName, &'a mut HeaderValues)
type Item = (&'a HeaderName, &'a mut HeaderValues)
The type of the elements being iterated over.
impl<'__pin, State> Unpin for Request<State> where
__Origin<'__pin, State>: Unpin,
Auto Trait Implementations
impl<State> !RefUnwindSafe for Request<State>
impl<State> Send for Request<State> where
State: Send,
impl<State> Sync for Request<State> where
State: Sync,
impl<State> !UnwindSafe for Request<State>
Blanket Implementations
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Reads some bytes from the byte stream. Read more
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
Reads the exact number of bytes required to fill buf. Read more
fn take(self, limit: u64) -> Take<Self>
fn take(self, limit: u64) -> Take<Self>
Creates an adapter which will read at most limit bytes from it. Read more
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ReadExt for T where
T: AsyncRead + ?Sized,
impl<T> ReadExt for T where
T: AsyncRead + ?Sized,
sourcefn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Reads some bytes from the byte stream. Read more
sourcefn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
sourcefn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
Reads all bytes from the byte stream. Read more
sourcefn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
Reads all bytes from the byte stream and appends them into a string. Read more
sourcefn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
Reads the exact number of bytes required to fill buf. Read more
sourcefn take(self, limit: u64) -> Take<Self>
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit bytes from it. Read more
sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read. Read more