1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-10 17:29:36 +00:00

handle header error with CustomResponder (#2093)

This commit is contained in:
fakeshadow 2021-03-19 22:18:06 -07:00 committed by GitHub
parent 8d9de76826
commit 746d983849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View file

@ -4,11 +4,16 @@
### Fixed
* Double ampersand in Logger format is escaped correctly. [#2067]
### Changed
* `CustomResponder` would return error as `HttpResponse` when `CustomResponder::with_header` failed instead of skipping.
(Only the first error is kept when multiple error occur) [#2093]
### Removed
* The `client` mod was removed. Clients should now use `awc` directly.
[871ca5e4](https://github.com/actix/actix-web/commit/871ca5e4ae2bdc22d1ea02701c2992fa8d04aed7)
[#2067]: https://github.com/actix/actix-web/pull/2067
[#2093]: https://github.com/actix/actix-web/pull/2093
## 4.0.0-beta.4 - 2021-03-09

View file

@ -155,8 +155,7 @@ impl Responder for BytesMut {
pub struct CustomResponder<T> {
responder: T,
status: Option<StatusCode>,
headers: Option<HeaderMap>,
error: Option<HttpError>,
headers: Result<HeaderMap, HttpError>,
}
impl<T: Responder> CustomResponder<T> {
@ -164,8 +163,7 @@ impl<T: Responder> CustomResponder<T> {
CustomResponder {
responder,
status: None,
headers: None,
error: None,
headers: Ok(HeaderMap::new()),
}
}
@ -206,32 +204,33 @@ impl<T: Responder> CustomResponder<T> {
where
H: IntoHeaderPair,
{
if self.headers.is_none() {
self.headers = Some(HeaderMap::new());
if let Ok(ref mut headers) = self.headers {
match header.try_into_header_pair() {
Ok((key, value)) => headers.append(key, value),
Err(e) => self.headers = Err(e.into()),
};
}
match header.try_into_header_pair() {
Ok((key, value)) => self.headers.as_mut().unwrap().append(key, value),
Err(e) => self.error = Some(e.into()),
};
self
}
}
impl<T: Responder> Responder for CustomResponder<T> {
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
let headers = match self.headers {
Ok(headers) => headers,
Err(err) => return HttpResponse::from_error(Error::from(err)),
};
let mut res = self.responder.respond_to(req);
if let Some(status) = self.status {
*res.status_mut() = status;
}
if let Some(ref headers) = self.headers {
for (k, v) in headers {
// TODO: before v4, decide if this should be append instead
res.headers_mut().insert(k.clone(), v.clone());
}
for (k, v) in headers {
// TODO: before v4, decide if this should be append instead
res.headers_mut().insert(k, v);
}
res