Skip to content

Commit 55935a6

Browse files
committed
feat: prepare internals
1 parent c4a465b commit 55935a6

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/http-server/src/handler/file_server/service/http_utils.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ use anyhow::{Context, Result};
66
use chrono::{DateTime, Local, Utc};
77
use futures::Stream;
88
use http::response::Builder as HttpResponseBuilder;
9+
use http_body_util::Full;
910
use hyper::body::Body;
1011
use hyper::body::Bytes;
1112
use tokio::io::{AsyncRead, ReadBuf};
1213

14+
use crate::server::HttpResponse;
15+
1316
use super::file::File;
1417

1518
const FILE_BUFFER_SIZE: usize = 8 * 1024;
@@ -112,7 +115,7 @@ impl ResponseHeaders {
112115
pub async fn make_http_file_response(
113116
file: File,
114117
cache_control_directive: CacheControlDirective,
115-
) -> Result<hyper::http::Response<Body>> {
118+
) -> Result<HttpResponse> {
116119
let headers = ResponseHeaders::new(&file, cache_control_directive)?;
117120
let builder = HttpResponseBuilder::new()
118121
.header(http::header::CONTENT_LENGTH, headers.content_length)
@@ -121,23 +124,14 @@ pub async fn make_http_file_response(
121124
.header(http::header::ETAG, headers.etag)
122125
.header(http::header::LAST_MODIFIED, headers.last_modified);
123126

124-
let body = file_bytes_into_http_body(file).await;
127+
let body = Full::new(Bytes::from(file.bytes()));
125128
let response = builder
126129
.body(body)
127130
.context("Failed to build HTTP File Response")?;
128131

129132
Ok(response)
130133
}
131134

132-
pub async fn file_bytes_into_http_body(file: File) -> Body {
133-
let byte_stream = ByteStream {
134-
file: file.file,
135-
buffer: Box::new([MaybeUninit::uninit(); FILE_BUFFER_SIZE]),
136-
};
137-
138-
Body::wrap_stream(byte_stream)
139-
}
140-
141135
pub struct ByteStream {
142136
file: tokio::fs::File,
143137
buffer: FileBuffer,

src/http-server/src/handler/file_server/service/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ mod http_utils;
44
mod query_params;
55
mod scoped_file_system;
66

7+
use bytes::Bytes;
78
use chrono::{DateTime, Local};
89

910
pub use file::File;
10-
use humansize::{format_size, DECIMAL};
11+
use http_body_util::Full;
12+
use humansize::{DECIMAL, format_size};
1113

1214
pub use scoped_file_system::{Entry, ScopedFileSystem};
1315

1416
use anyhow::{Context, Result};
15-
use handlebars::{handlebars_helper, Handlebars};
17+
use handlebars::{Handlebars, handlebars_helper};
1618
use http::response::Builder as HttpResponseBuilder;
1719
use http::{StatusCode, Uri};
1820
use hyper::Response;
@@ -23,10 +25,11 @@ use std::path::{Component, Path, PathBuf};
2325
use std::str::FromStr;
2426
use std::sync::Arc;
2527

26-
use crate::handler::file_server::utils::url_encode::{decode_uri, encode_uri, PERCENT_ENCODE_SET};
28+
use crate::handler::file_server::utils::url_encode::{PERCENT_ENCODE_SET, decode_uri, encode_uri};
29+
use crate::server::HttpResponse;
2730

2831
use self::directory_entry::{BreadcrumbItem, DirectoryEntry, DirectoryIndex, Sort};
29-
use self::http_utils::{make_http_file_response, CacheControlDirective};
32+
use self::http_utils::{CacheControlDirective, make_http_file_response};
3033
use self::query_params::{QueryParams, SortBy};
3134

3235
/// Explorer's Handlebars template filename
@@ -136,7 +139,7 @@ impl<'a> FileServer {
136139
///
137140
/// If the matched path resolves to a file, attempts to render it if the
138141
/// MIME type is supported, otherwise returns the binary (downloadable file)
139-
pub async fn resolve(&self, req_path: String) -> Result<Response<Body>> {
142+
pub async fn resolve(&self, req_path: String) -> Result<HttpResponse> {
140143
let (path, query_params) = FileServer::parse_path(req_path.as_str())?;
141144

142145
match self.scoped_file_system.resolve(path).await {
@@ -202,12 +205,12 @@ impl<'a> FileServer {
202205
let response = hyper::Response::builder()
203206
.status(status)
204207
.header(http::header::CONTENT_TYPE, "text/html")
205-
.body(hyper::Body::from(
208+
.body(Full::new(Bytes::from(
206209
handlebars::Handlebars::new().render_template(
207210
include_str!("./template/error.hbs"),
208211
&serde_json::json!({"error": err.to_string(), "code": code}),
209212
)?,
210-
))?;
213+
)))?;
211214

212215
Ok(response)
213216
}
@@ -221,15 +224,15 @@ impl<'a> FileServer {
221224
&self,
222225
path: PathBuf,
223226
query_params: Option<QueryParams>,
224-
) -> Result<Response<Body>> {
227+
) -> Result<HttpResponse> {
225228
let directory_index =
226229
FileServer::index_directory(self.config.root_dir.clone(), path, query_params)?;
227230
let html = self
228231
.handlebars
229232
.render(EXPLORER_TEMPLATE, &directory_index)
230233
.unwrap();
231234

232-
let body = Body::from(html);
235+
let body = Full::new(Bytes::from(html.as_bytes().to_vec()));
233236

234237
Ok(HttpResponseBuilder::new()
235238
.header(http::header::CONTENT_TYPE, "text/html")

0 commit comments

Comments
 (0)