1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-12 07:23:11 +01: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 ### Fixed
* Double ampersand in Logger format is escaped correctly. [#2067] * 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 ### Removed
* The `client` mod was removed. Clients should now use `awc` directly. * The `client` mod was removed. Clients should now use `awc` directly.
[871ca5e4](https://github.com/actix/actix-web/commit/871ca5e4ae2bdc22d1ea02701c2992fa8d04aed7) [871ca5e4](https://github.com/actix/actix-web/commit/871ca5e4ae2bdc22d1ea02701c2992fa8d04aed7)
[#2067]: https://github.com/actix/actix-web/pull/2067 [#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 ## 4.0.0-beta.4 - 2021-03-09

View File

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