1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-19 16:58:14 +00:00
actix-web/actix-web/src/http/header/if_range.rs

118 lines
3.5 KiB
Rust
Raw Normal View History

2019-02-07 21:24:24 +00:00
use std::fmt::{self, Display, Write};
use super::{
from_one_raw_str, EntityTag, Header, HeaderName, HeaderValue, HttpDate, InvalidHeaderValue,
TryIntoHeaderValue, Writer,
2019-02-07 21:24:24 +00:00
};
2023-07-17 01:38:12 +00:00
use crate::{error::ParseError, http::header, HttpMessage};
2019-02-07 21:24:24 +00:00
/// `If-Range` header, defined
/// in [RFC 7233 §3.2](https://datatracker.ietf.org/doc/html/rfc7233#section-3.2)
2019-02-07 21:24:24 +00:00
///
/// If a client has a partial copy of a representation and wishes to have
/// an up-to-date copy of the entire representation, it could use the
/// Range header field with a conditional GET (using either or both of
/// If-Unmodified-Since and If-Match.) However, if the precondition
/// fails because the representation has been modified, the client would
/// then have to make a second request to obtain the entire current
/// representation.
///
/// The `If-Range` header field allows a client to \"short-circuit\" the
/// second request. Informally, its meaning is as follows: if the
/// representation is unchanged, send me the part(s) that I am requesting
/// in Range; otherwise, send me the entire representation.
///
/// # ABNF
2021-12-02 15:25:39 +00:00
/// ```plain
2019-02-07 21:24:24 +00:00
/// If-Range = entity-tag / HTTP-date
/// ```
///
/// # Example Values
2019-02-07 21:24:24 +00:00
///
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
/// * `\"xyzzy\"`
///
/// # Examples
2021-01-15 02:11:10 +00:00
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::{EntityTag, IfRange};
2019-02-07 21:24:24 +00:00
///
/// let mut builder = HttpResponse::Ok();
2021-01-15 02:11:10 +00:00
/// builder.insert_header(
/// IfRange::EntityTag(
/// EntityTag::new(false, "abc".to_owned())
/// )
/// );
2019-02-07 21:24:24 +00:00
/// ```
///
2021-01-15 02:11:10 +00:00
/// ```
2019-02-07 21:24:24 +00:00
/// use std::time::{Duration, SystemTime};
/// use actix_web::{http::header::IfRange, HttpResponse};
2019-02-07 21:24:24 +00:00
///
/// let mut builder = HttpResponse::Ok();
2019-02-07 21:24:24 +00:00
/// let fetched = SystemTime::now() - Duration::from_secs(60 * 60 * 24);
2021-01-15 02:11:10 +00:00
/// builder.insert_header(
/// IfRange::Date(fetched.into())
/// );
2019-02-07 21:24:24 +00:00
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
2019-02-07 21:24:24 +00:00
pub enum IfRange {
2021-01-15 02:11:10 +00:00
/// The entity-tag the client has of the resource.
2019-02-07 21:24:24 +00:00
EntityTag(EntityTag),
2021-01-15 02:11:10 +00:00
/// The date when the client retrieved the resource.
2019-02-07 21:24:24 +00:00
Date(HttpDate),
}
impl Header for IfRange {
fn name() -> HeaderName {
header::IF_RANGE
}
#[inline]
fn parse<T>(msg: &T) -> Result<Self, ParseError>
where
T: HttpMessage,
{
2021-04-16 19:28:21 +00:00
let etag: Result<EntityTag, _> = from_one_raw_str(msg.headers().get(&header::IF_RANGE));
2019-02-07 21:24:24 +00:00
if let Ok(etag) = etag {
return Ok(IfRange::EntityTag(etag));
}
2021-04-16 19:28:21 +00:00
let date: Result<HttpDate, _> = from_one_raw_str(msg.headers().get(&header::IF_RANGE));
2019-02-07 21:24:24 +00:00
if let Ok(date) = date {
return Ok(IfRange::Date(date));
}
Err(ParseError::Header)
}
}
impl Display for IfRange {
2019-12-07 18:46:51 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-02-07 21:24:24 +00:00
match *self {
IfRange::EntityTag(ref x) => Display::fmt(x, f),
IfRange::Date(ref x) => Display::fmt(x, f),
}
}
}
impl TryIntoHeaderValue for IfRange {
2019-12-05 17:35:43 +00:00
type Error = InvalidHeaderValue;
2019-02-07 21:24:24 +00:00
2021-01-15 02:11:10 +00:00
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
2019-02-07 21:24:24 +00:00
let mut writer = Writer::new();
let _ = write!(&mut writer, "{}", self);
2019-12-05 17:35:43 +00:00
HeaderValue::from_maybe_shared(writer.take())
2019-02-07 21:24:24 +00:00
}
}
#[cfg(test)]
2021-12-02 15:25:39 +00:00
mod test_parse_and_format {
use std::str;
2019-02-07 21:24:24 +00:00
use super::IfRange as HeaderField;
use crate::http::header::*;
2021-01-15 02:11:10 +00:00
2021-06-25 11:25:50 +00:00
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
crate::http::header::common_header_test!(test2, vec![b"\"abc\""]);
crate::http::header::common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>);
2019-02-07 21:24:24 +00:00
}