mirror of https://github.com/fafhrd91/actix-web
Compare commits
No commits in common. "672c2c7cd9c56ac6819852784fb360f2cc411d3a" and "f19bab17b19a48478d4c5bd9a1255d0e0a8dbf2c" have entirely different histories.
672c2c7cd9
...
f19bab17b1
|
|
@ -3,7 +3,6 @@
|
|||
## Unreleased
|
||||
|
||||
- Encode the HTTP/1 `Connection: Upgrade` header in Camel-Case when camel-case header formatting is enabled.[#3953]
|
||||
- Fix `HeaderMap` iterators' `len()` and `size_hint()` implementations for multi-value headers.
|
||||
|
||||
[#3953]: https://github.com/actix/actix-web/pull/3953
|
||||
|
||||
|
|
|
|||
|
|
@ -537,7 +537,7 @@ impl HeaderMap {
|
|||
/// assert!(pairs.contains(&(&header::SET_COOKIE, &HeaderValue::from_static("two=2"))));
|
||||
/// ```
|
||||
pub fn iter(&self) -> Iter<'_> {
|
||||
Iter::new(self.inner.iter(), self.len())
|
||||
Iter::new(self.inner.iter())
|
||||
}
|
||||
|
||||
/// An iterator over all contained header names.
|
||||
|
|
@ -626,8 +626,7 @@ impl HeaderMap {
|
|||
/// assert!(map.is_empty());
|
||||
/// ```
|
||||
pub fn drain(&mut self) -> Drain<'_> {
|
||||
let len = self.len();
|
||||
Drain::new(self.inner.drain(), len)
|
||||
Drain::new(self.inner.drain())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -639,8 +638,7 @@ impl IntoIterator for HeaderMap {
|
|||
|
||||
#[inline]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
let len = self.len();
|
||||
IntoIter::new(self.inner.into_iter(), len)
|
||||
IntoIter::new(self.inner.into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -650,7 +648,7 @@ impl<'a> IntoIterator for &'a HeaderMap {
|
|||
|
||||
#[inline]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
Iter::new(self.inner.iter(), self.len())
|
||||
Iter::new(self.inner.iter())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -762,16 +760,14 @@ pub struct Iter<'a> {
|
|||
inner: hash_map::Iter<'a, HeaderName, Value>,
|
||||
multi_inner: Option<(&'a HeaderName, &'a SmallVec<[HeaderValue; 4]>)>,
|
||||
multi_idx: usize,
|
||||
remaining: usize,
|
||||
}
|
||||
|
||||
impl<'a> Iter<'a> {
|
||||
fn new(iter: hash_map::Iter<'a, HeaderName, Value>, remaining: usize) -> Self {
|
||||
fn new(iter: hash_map::Iter<'a, HeaderName, Value>) -> Self {
|
||||
Self {
|
||||
inner: iter,
|
||||
multi_idx: 0,
|
||||
multi_inner: None,
|
||||
remaining,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -785,7 +781,6 @@ impl<'a> Iterator for Iter<'a> {
|
|||
match vals.get(self.multi_idx) {
|
||||
Some(val) => {
|
||||
self.multi_idx += 1;
|
||||
self.remaining -= 1;
|
||||
return Some((name, val));
|
||||
}
|
||||
None => {
|
||||
|
|
@ -805,7 +800,9 @@ impl<'a> Iterator for Iter<'a> {
|
|||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.remaining, Some(self.remaining))
|
||||
// take inner lower bound
|
||||
// make no attempt at an upper bound
|
||||
(self.inner.size_hint().0, None)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -821,16 +818,14 @@ pub struct Drain<'a> {
|
|||
inner: hash_map::Drain<'a, HeaderName, Value>,
|
||||
multi_inner: Option<(Option<HeaderName>, SmallVec<[HeaderValue; 4]>)>,
|
||||
multi_idx: usize,
|
||||
remaining: usize,
|
||||
}
|
||||
|
||||
impl<'a> Drain<'a> {
|
||||
fn new(iter: hash_map::Drain<'a, HeaderName, Value>, remaining: usize) -> Self {
|
||||
fn new(iter: hash_map::Drain<'a, HeaderName, Value>) -> Self {
|
||||
Self {
|
||||
inner: iter,
|
||||
multi_inner: None,
|
||||
multi_idx: 0,
|
||||
remaining,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -843,7 +838,6 @@ impl Iterator for Drain<'_> {
|
|||
if let Some((ref mut name, ref mut vals)) = self.multi_inner {
|
||||
if !vals.is_empty() {
|
||||
// OPTIMIZE: array removals
|
||||
self.remaining -= 1;
|
||||
return Some((name.take(), vals.remove(0)));
|
||||
} else {
|
||||
// no more items in value iterator; reset state
|
||||
|
|
@ -861,7 +855,9 @@ impl Iterator for Drain<'_> {
|
|||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.remaining, Some(self.remaining))
|
||||
// take inner lower bound
|
||||
// make no attempt at an upper bound
|
||||
(self.inner.size_hint().0, None)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -876,15 +872,13 @@ impl iter::FusedIterator for Drain<'_> {}
|
|||
pub struct IntoIter {
|
||||
inner: hash_map::IntoIter<HeaderName, Value>,
|
||||
multi_inner: Option<(HeaderName, smallvec::IntoIter<[HeaderValue; 4]>)>,
|
||||
remaining: usize,
|
||||
}
|
||||
|
||||
impl IntoIter {
|
||||
fn new(inner: hash_map::IntoIter<HeaderName, Value>, remaining: usize) -> Self {
|
||||
fn new(inner: hash_map::IntoIter<HeaderName, Value>) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
multi_inner: None,
|
||||
remaining,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -897,7 +891,6 @@ impl Iterator for IntoIter {
|
|||
if let Some((ref name, ref mut vals)) = self.multi_inner {
|
||||
match vals.next() {
|
||||
Some(val) => {
|
||||
self.remaining -= 1;
|
||||
return Some((name.clone(), val));
|
||||
}
|
||||
None => {
|
||||
|
|
@ -916,7 +909,9 @@ impl Iterator for IntoIter {
|
|||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.remaining, Some(self.remaining))
|
||||
// take inner lower bound
|
||||
// make no attempt at an upper bound
|
||||
(self.inner.size_hint().0, None)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1165,40 +1160,6 @@ mod tests {
|
|||
assert!(vals.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iter_len_counts_values() {
|
||||
let mut map = HeaderMap::new();
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("a=1"));
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("b=2"));
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("c=3"));
|
||||
|
||||
assert_eq!(map.iter().count(), 3);
|
||||
assert_eq!(map.iter().len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn into_iter_len_counts_values() {
|
||||
let mut map = HeaderMap::new();
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("a=1"));
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("b=2"));
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("c=3"));
|
||||
|
||||
assert_eq!(map.clone().into_iter().count(), 3);
|
||||
assert_eq!(map.into_iter().len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drain_len_counts_values() {
|
||||
let mut map = HeaderMap::new();
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("a=1"));
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("b=2"));
|
||||
map.append(header::SET_COOKIE, HeaderValue::from_static("c=3"));
|
||||
|
||||
let mut drained = map.clone();
|
||||
assert_eq!(map.drain().count(), 3);
|
||||
assert_eq!(drained.drain().len(), 3);
|
||||
}
|
||||
|
||||
fn owned_pair<'a>((name, val): (&'a HeaderName, &'a HeaderValue)) -> (HeaderName, HeaderValue) {
|
||||
(name.clone(), val.clone())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
- Panic when calling `Route::to()` or `Route::service()` after `Route::wrap()` to prevent silently dropping route middleware. [#3944]
|
||||
- Fix `HttpRequest::{match_pattern,match_name}` reporting path-only matches when route guards disambiguate overlapping resources. [#3346]
|
||||
- Fix `Readlines` handling of lines split across payload chunks so combined line limits are enforced and complete lines are yielded.
|
||||
|
||||
[#3944]: https://github.com/actix/actix-web/pull/3944
|
||||
[#3346]: https://github.com/actix/actix-web/issues/3346
|
||||
|
|
|
|||
|
|
@ -65,23 +65,6 @@ where
|
|||
err: Some(err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes one complete logical line using the request's configured encoding.
|
||||
///
|
||||
/// Callers are expected to pass only the bytes that belong to the line being yielded,
|
||||
/// whether they came from the internal buffer, the current payload chunk, or both.
|
||||
fn decode(encoding: &'static Encoding, bytes: &[u8]) -> Result<String, ReadlinesError> {
|
||||
if encoding == UTF_8 {
|
||||
str::from_utf8(bytes)
|
||||
.map_err(|_| ReadlinesError::EncodingError)
|
||||
.map(str::to_owned)
|
||||
} else {
|
||||
encoding
|
||||
.decode_without_bom_handling_and_without_replacement(bytes)
|
||||
.map(Cow::into_owned)
|
||||
.ok_or(ReadlinesError::EncodingError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Stream for Readlines<T>
|
||||
|
|
@ -112,7 +95,18 @@ where
|
|||
if ind + 1 > this.limit {
|
||||
return Poll::Ready(Some(Err(ReadlinesError::LimitOverflow)));
|
||||
}
|
||||
let line = Self::decode(this.encoding, &this.buf.split_to(ind + 1))?;
|
||||
let line = if this.encoding == UTF_8 {
|
||||
str::from_utf8(&this.buf.split_to(ind + 1))
|
||||
.map_err(|_| ReadlinesError::EncodingError)?
|
||||
.to_owned()
|
||||
} else {
|
||||
this.encoding
|
||||
.decode_without_bom_handling_and_without_replacement(
|
||||
&this.buf.split_to(ind + 1),
|
||||
)
|
||||
.map(Cow::into_owned)
|
||||
.ok_or(ReadlinesError::EncodingError)?
|
||||
};
|
||||
return Poll::Ready(Some(Ok(line)));
|
||||
}
|
||||
this.checked_buff = true;
|
||||
|
|
@ -131,17 +125,24 @@ where
|
|||
}
|
||||
if let Some(ind) = found {
|
||||
// check if line is longer than limit
|
||||
if this.buf.len() + ind + 1 > this.limit {
|
||||
if ind + 1 > this.limit {
|
||||
return Poll::Ready(Some(Err(ReadlinesError::LimitOverflow)));
|
||||
}
|
||||
|
||||
this.buf.extend_from_slice(&bytes.split_to(ind + 1));
|
||||
let line = Self::decode(this.encoding, &this.buf)?;
|
||||
this.buf.clear();
|
||||
|
||||
// buffer bytes following the returned line
|
||||
let line = if this.encoding == UTF_8 {
|
||||
str::from_utf8(&bytes.split_to(ind + 1))
|
||||
.map_err(|_| ReadlinesError::EncodingError)?
|
||||
.to_owned()
|
||||
} else {
|
||||
this.encoding
|
||||
.decode_without_bom_handling_and_without_replacement(
|
||||
&bytes.split_to(ind + 1),
|
||||
)
|
||||
.map(Cow::into_owned)
|
||||
.ok_or(ReadlinesError::EncodingError)?
|
||||
};
|
||||
// extend buffer with rest of the bytes;
|
||||
this.buf.extend_from_slice(&bytes);
|
||||
this.checked_buff = this.buf.is_empty();
|
||||
this.checked_buff = false;
|
||||
return Poll::Ready(Some(Ok(line)));
|
||||
}
|
||||
this.buf.extend_from_slice(&bytes);
|
||||
|
|
@ -155,7 +156,16 @@ where
|
|||
if this.buf.len() > this.limit {
|
||||
return Poll::Ready(Some(Err(ReadlinesError::LimitOverflow)));
|
||||
}
|
||||
let line = Self::decode(this.encoding, &this.buf)?;
|
||||
let line = if this.encoding == UTF_8 {
|
||||
str::from_utf8(&this.buf)
|
||||
.map_err(|_| ReadlinesError::EncodingError)?
|
||||
.to_owned()
|
||||
} else {
|
||||
this.encoding
|
||||
.decode_without_bom_handling_and_without_replacement(&this.buf)
|
||||
.map(Cow::into_owned)
|
||||
.ok_or(ReadlinesError::EncodingError)?
|
||||
};
|
||||
this.buf.clear();
|
||||
Poll::Ready(Some(Ok(line)))
|
||||
}
|
||||
|
|
@ -167,16 +177,10 @@ where
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use actix_http::{h1, Request};
|
||||
use futures_util::{task::noop_waker_ref, StreamExt as _};
|
||||
use futures_util::StreamExt as _;
|
||||
|
||||
use super::*;
|
||||
use crate::{error::ReadlinesError, test::TestRequest};
|
||||
use crate::test::TestRequest;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_readlines() {
|
||||
|
|
@ -204,46 +208,4 @@ mod tests {
|
|||
"Contrary to popular belief, Lorem Ipsum is not simply random text."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_readlines_limit_across_chunks() {
|
||||
let (mut sender, payload) = h1::Payload::create(false);
|
||||
let payload: actix_http::Payload = payload.into();
|
||||
let mut req = Request::with_payload(payload);
|
||||
let mut stream = Readlines::new(&mut req).limit(10);
|
||||
let mut cx = Context::from_waker(noop_waker_ref());
|
||||
|
||||
sender.feed_data(Bytes::from_static(b"AAAAAAAAAA"));
|
||||
assert!(matches!(
|
||||
Pin::new(&mut stream).poll_next(&mut cx),
|
||||
Poll::Pending
|
||||
));
|
||||
|
||||
sender.feed_data(Bytes::from_static(b"A\n"));
|
||||
assert!(matches!(
|
||||
Pin::new(&mut stream).poll_next(&mut cx),
|
||||
Poll::Ready(Some(Err(ReadlinesError::LimitOverflow)))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_readlines_returns_full_line_across_chunks() {
|
||||
let (mut sender, payload) = h1::Payload::create(false);
|
||||
let payload: actix_http::Payload = payload.into();
|
||||
let mut req = Request::with_payload(payload);
|
||||
let mut stream = Readlines::new(&mut req);
|
||||
let mut cx = Context::from_waker(noop_waker_ref());
|
||||
|
||||
sender.feed_data(Bytes::from_static(b"hello "));
|
||||
assert!(matches!(
|
||||
Pin::new(&mut stream).poll_next(&mut cx),
|
||||
Poll::Pending
|
||||
));
|
||||
|
||||
sender.feed_data(Bytes::from_static(b"world\nnext"));
|
||||
assert!(matches!(
|
||||
Pin::new(&mut stream).poll_next(&mut cx),
|
||||
Poll::Ready(Some(Ok(ref line))) if line == "hello world\n"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue