mirror of
https://github.com/fafhrd91/actix-web
synced 2025-02-20 03:14:21 +01:00
migrate actix-files
This commit is contained in:
parent
6ac4ac66b9
commit
69cadcdedb
@ -32,7 +32,7 @@ members = [
|
|||||||
"awc",
|
"awc",
|
||||||
"actix-http",
|
"actix-http",
|
||||||
"actix-cors",
|
"actix-cors",
|
||||||
#"actix-files",
|
"actix-files",
|
||||||
#"actix-framed",
|
#"actix-framed",
|
||||||
#"actix-session",
|
#"actix-session",
|
||||||
"actix-identity",
|
"actix-identity",
|
||||||
@ -126,7 +126,7 @@ actix-http-test = { path = "test-server" }
|
|||||||
actix-web-codegen = { path = "actix-web-codegen" }
|
actix-web-codegen = { path = "actix-web-codegen" }
|
||||||
# actix-web-actors = { path = "actix-web-actors" }
|
# actix-web-actors = { path = "actix-web-actors" }
|
||||||
# actix-session = { path = "actix-session" }
|
# actix-session = { path = "actix-session" }
|
||||||
# actix-files = { path = "actix-files" }
|
actix-files = { path = "actix-files" }
|
||||||
# actix-multipart = { path = "actix-multipart" }
|
# actix-multipart = { path = "actix-multipart" }
|
||||||
awc = { path = "awc" }
|
awc = { path = "awc" }
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.1.7"
|
version = "0.2.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Static files support for actix web."
|
description = "Static files support for actix web."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -11,19 +11,19 @@ documentation = "https://docs.rs/actix-files/"
|
|||||||
categories = ["asynchronous", "web-programming::http-server"]
|
categories = ["asynchronous", "web-programming::http-server"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
# workspace = ".."
|
workspace = ".."
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_files"
|
name = "actix_files"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "1.0.9", default-features = false }
|
actix-web = { version = "2.0.0-alpha.1", default-features = false }
|
||||||
actix-http = "0.2.11"
|
actix-http = "0.3.0-alpha.1"
|
||||||
actix-service = "0.4.2"
|
actix-service = "1.0.0-alpha.1"
|
||||||
bitflags = "1"
|
bitflags = "1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1.24"
|
futures = "0.3.1"
|
||||||
derive_more = "0.15.0"
|
derive_more = "0.15.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime = "0.3"
|
mime = "0.3"
|
||||||
@ -32,4 +32,4 @@ percent-encoding = "2.1"
|
|||||||
v_htmlescape = "0.4"
|
v_htmlescape = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-web = { version = "1.0.9", features=["ssl"] }
|
actix-web = { version = "2.0.0-alpha.1", features=["openssl"] }
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -18,6 +18,7 @@ use actix_web::http::header::{
|
|||||||
use actix_web::http::{ContentEncoding, StatusCode};
|
use actix_web::http::{ContentEncoding, StatusCode};
|
||||||
use actix_web::middleware::BodyEncoding;
|
use actix_web::middleware::BodyEncoding;
|
||||||
use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder};
|
use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder};
|
||||||
|
use futures::future::{ready, Ready};
|
||||||
|
|
||||||
use crate::range::HttpRange;
|
use crate::range::HttpRange;
|
||||||
use crate::ChunkedReadFile;
|
use crate::ChunkedReadFile;
|
||||||
@ -255,62 +256,8 @@ impl NamedFile {
|
|||||||
pub(crate) fn last_modified(&self) -> Option<header::HttpDate> {
|
pub(crate) fn last_modified(&self) -> Option<header::HttpDate> {
|
||||||
self.modified.map(|mtime| mtime.into())
|
self.modified.map(|mtime| mtime.into())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for NamedFile {
|
pub fn into_response(self, req: &HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
type Target = File;
|
|
||||||
|
|
||||||
fn deref(&self) -> &File {
|
|
||||||
&self.file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for NamedFile {
|
|
||||||
fn deref_mut(&mut self) -> &mut File {
|
|
||||||
&mut self.file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns true if `req` has no `If-Match` header or one which matches `etag`.
|
|
||||||
fn any_match(etag: Option<&header::EntityTag>, req: &HttpRequest) -> bool {
|
|
||||||
match req.get_header::<header::IfMatch>() {
|
|
||||||
None | Some(header::IfMatch::Any) => true,
|
|
||||||
Some(header::IfMatch::Items(ref items)) => {
|
|
||||||
if let Some(some_etag) = etag {
|
|
||||||
for item in items {
|
|
||||||
if item.strong_eq(some_etag) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns true if `req` doesn't have an `If-None-Match` header matching `req`.
|
|
||||||
fn none_match(etag: Option<&header::EntityTag>, req: &HttpRequest) -> bool {
|
|
||||||
match req.get_header::<header::IfNoneMatch>() {
|
|
||||||
Some(header::IfNoneMatch::Any) => false,
|
|
||||||
Some(header::IfNoneMatch::Items(ref items)) => {
|
|
||||||
if let Some(some_etag) = etag {
|
|
||||||
for item in items {
|
|
||||||
if item.weak_eq(some_etag) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
None => true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Responder for NamedFile {
|
|
||||||
type Error = Error;
|
|
||||||
type Future = Result<HttpResponse, Error>;
|
|
||||||
|
|
||||||
fn respond_to(self, req: &HttpRequest) -> Self::Future {
|
|
||||||
if self.status_code != StatusCode::OK {
|
if self.status_code != StatusCode::OK {
|
||||||
let mut resp = HttpResponse::build(self.status_code);
|
let mut resp = HttpResponse::build(self.status_code);
|
||||||
resp.set(header::ContentType(self.content_type.clone()))
|
resp.set(header::ContentType(self.content_type.clone()))
|
||||||
@ -442,8 +389,67 @@ impl Responder for NamedFile {
|
|||||||
counter: 0,
|
counter: 0,
|
||||||
};
|
};
|
||||||
if offset != 0 || length != self.md.len() {
|
if offset != 0 || length != self.md.len() {
|
||||||
return Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader));
|
Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader))
|
||||||
};
|
} else {
|
||||||
Ok(resp.body(SizedStream::new(length, reader)))
|
Ok(resp.body(SizedStream::new(length, reader)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for NamedFile {
|
||||||
|
type Target = File;
|
||||||
|
|
||||||
|
fn deref(&self) -> &File {
|
||||||
|
&self.file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for NamedFile {
|
||||||
|
fn deref_mut(&mut self) -> &mut File {
|
||||||
|
&mut self.file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if `req` has no `If-Match` header or one which matches `etag`.
|
||||||
|
fn any_match(etag: Option<&header::EntityTag>, req: &HttpRequest) -> bool {
|
||||||
|
match req.get_header::<header::IfMatch>() {
|
||||||
|
None | Some(header::IfMatch::Any) => true,
|
||||||
|
Some(header::IfMatch::Items(ref items)) => {
|
||||||
|
if let Some(some_etag) = etag {
|
||||||
|
for item in items {
|
||||||
|
if item.strong_eq(some_etag) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if `req` doesn't have an `If-None-Match` header matching `req`.
|
||||||
|
fn none_match(etag: Option<&header::EntityTag>, req: &HttpRequest) -> bool {
|
||||||
|
match req.get_header::<header::IfNoneMatch>() {
|
||||||
|
Some(header::IfNoneMatch::Any) => false,
|
||||||
|
Some(header::IfNoneMatch::Items(ref items)) => {
|
||||||
|
if let Some(some_etag) = etag {
|
||||||
|
for item in items {
|
||||||
|
if item.weak_eq(some_etag) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
None => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Responder for NamedFile {
|
||||||
|
type Error = Error;
|
||||||
|
type Future = Ready<Result<HttpResponse, Error>>;
|
||||||
|
|
||||||
|
fn respond_to(self, req: &HttpRequest) -> Self::Future {
|
||||||
|
ready(self.into_response(req))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use std::{fmt, io, result};
|
|||||||
use actix_utils::timeout::TimeoutError;
|
use actix_utils::timeout::TimeoutError;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use derive_more::{Display, From};
|
use derive_more::{Display, From};
|
||||||
use futures::channel::oneshot::Canceled;
|
pub use futures::channel::oneshot::Canceled;
|
||||||
use http::uri::InvalidUri;
|
use http::uri::InvalidUri;
|
||||||
use http::{header, Error as HttpError, StatusCode};
|
use http::{header, Error as HttpError, StatusCode};
|
||||||
use httparse;
|
use httparse;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user