Skip to main content

apimock_server/response/
file_response.rs

1use console::style;
2use hyper::HeaderMap;
3use serde_json::{Map, Value};
4use tokio::task;
5
6use std::{collections::HashMap, fs, path::Path};
7
8use crate::{
9    constant::CSV_RECORDS_DEFAULT_KEY,
10    json_path_util::resolve_with_json_compatible_extensions,
11    response::{
12        confine::confine, error_response::not_found_response, json_response::json_response,
13    },
14    response_handler::ResponseHandler,
15    types::BoxBody,
16};
17
18use super::{
19    error_response::internal_server_error_response,
20    text_response::text_response,
21    util::{
22        binary_content_type, file_extension, json_value_with_jsonpath_key, text_file_content_type,
23    },
24};
25
26pub struct FileResponse {
27    file_path: String,
28    csv_records_key: Option<String>,
29    text_content: Option<String>,
30    binary_content: Option<Vec<u8>>,
31    custom_headers: Option<HashMap<String, Option<String>>>,
32    request_headers: HeaderMap,
33    /// The directory `file_path` must resolve inside, already
34    /// canonicalised by the caller. `None` means it couldn't be (the
35    /// directory doesn't exist) — every candidate is then refused,
36    /// never served unchecked.
37    confine_to: Option<std::path::PathBuf>,
38    /// RFC 067 — see `response_handler::default_response_headers`.
39    cors_allow_credentials_origins: Vec<String>,
40}
41
42impl FileResponse {
43    /// create instance
44    pub fn new(
45        file_path: &str,
46        custom_headers: Option<&HashMap<String, Option<String>>>,
47        request_headers: &HeaderMap,
48        confine_to: Option<&Path>,
49        cors_allow_credentials_origins: &[String],
50    ) -> Self {
51        FileResponse {
52            file_path: file_path.to_owned(),
53            csv_records_key: None,
54            text_content: None,
55            binary_content: None,
56            custom_headers: custom_headers.cloned(),
57            request_headers: request_headers.clone(),
58            confine_to: confine_to.map(Path::to_path_buf),
59            cors_allow_credentials_origins: cors_allow_credentials_origins.to_vec(),
60        }
61    }
62
63    /// create instance
64    pub fn new_with_csv_records_jsonpath(
65        file_path: &str,
66        custom_headers: Option<&HashMap<String, Option<String>>>,
67        csv_records_key: Option<String>,
68        request_headers: &HeaderMap,
69        confine_to: Option<&Path>,
70        cors_allow_credentials_origins: &[String],
71    ) -> Self {
72        let mut ret = FileResponse::new(
73            file_path,
74            custom_headers,
75            request_headers,
76            confine_to,
77            cors_allow_credentials_origins,
78        );
79        ret.csv_records_key = csv_records_key;
80        ret
81    }
82
83    /// response from file path
84    pub async fn file_content_response(
85        &mut self,
86    ) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
87        let file_path = match resolve_with_json_compatible_extensions(self.file_path.as_str()) {
88            Some(x) => x,
89            None => {
90                log::warn!(
91                    "{}:\n{} (missing or a directory)",
92                    style("file not found").red(),
93                    self.file_path
94                );
95                return not_found_response(
96                    &self.request_headers,
97                    &self.cors_allow_credentials_origins,
98                );
99            }
100        };
101
102        // Confine the resolved candidate to the directory it was meant
103        // to come from. This runs after extension/`index.*` resolution
104        // above, so it also catches a path that only escapes at that
105        // stage (e.g. a symlinked `index.html`), not only one that
106        // arrived already outside.
107        let file_path = match confine(file_path.as_str(), self.confine_to.as_deref()) {
108            Some(canonical) => match canonical.to_str() {
109                Some(x) => x.to_owned(),
110                None => {
111                    log::error!(
112                        "{} to get str from canonicalized file path:\n{}",
113                        style("failed").red(),
114                        file_path
115                    );
116                    return not_found_response(
117                        &self.request_headers,
118                        &self.cors_allow_credentials_origins,
119                    );
120                }
121            },
122            None => {
123                return not_found_response(
124                    &self.request_headers,
125                    &self.cors_allow_credentials_origins,
126                );
127            }
128        };
129        self.file_path = file_path.clone();
130
131        // RFC 077 P-05: read the file's bytes once, then decide
132        // text-vs-binary from the bytes already in hand — this used to
133        // be two blocking reads (`read_to_string`, and on its failure a
134        // second `read` of the same file for the binary fallback).
135        // `String::from_utf8` reproduces exactly the same dispatch
136        // `read_to_string`'s success/failure did (it fails on the same
137        // input `read_to_string` would have failed to decode), so the
138        // detection RFC 065's review pinned as load-bearing is
139        // unchanged — see the tests below, written before this diff.
140        let file_path_to_read = file_path.clone();
141        let content = task::spawn_blocking(move || fs::read(file_path_to_read)).await;
142
143        match content {
144            Ok(Ok(bytes)) => match String::from_utf8(bytes) {
145                Ok(text) => {
146                    self.text_content = Some(text);
147                    self.text_file_content_response()
148                }
149                Err(err) => {
150                    self.binary_content = Some(err.into_bytes());
151                    self.binary_content_type_response()
152                }
153            },
154            Ok(Err(err)) => {
155                log::error!("failed to read file ({}): {}", self.file_path, err);
156                internal_server_error_response(
157                    "failed to read response file",
158                    &self.request_headers,
159                    &self.cors_allow_credentials_origins,
160                )
161            }
162            Err(err) => {
163                log::error!("async task failed ({}): {}", self.file_path, err);
164                internal_server_error_response(
165                    "failed to read response file",
166                    &self.request_headers,
167                    &self.cors_allow_credentials_origins,
168                )
169            }
170        }
171    }
172
173    /// text file response
174    ///
175    /// `self.custom_headers` is threaded through here the same way
176    /// `json_file_content_response`/`csv_file_content_response` already
177    /// do below - this branch previously hardcoded `None`, silently
178    /// dropping every custom header on a plain-text `file_path` response
179    /// (RFC 045 Defect 1, extended: this contradicted the RFC's own
180    /// "`file_path` | honoured" claim, which held only for the
181    /// json/json5/csv sub-cases).
182    fn text_file_content_response(&self) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
183        match file_extension(self.file_path.as_str()) {
184            Some(ext) => match ext.as_str() {
185                "json" | "json5" => self.json_file_content_response(),
186                "csv" => self.csv_file_content_response(),
187                _ => text_response(
188                    self.text_content.clone().unwrap_or_default().as_str(),
189                    Some(text_file_content_type(ext).as_str()),
190                    self.custom_headers.as_ref(),
191                    &self.request_headers,
192                    &self.cors_allow_credentials_origins,
193                ),
194            },
195            None => text_response(
196                self.text_content.clone().unwrap_or_default().as_str(),
197                None,
198                self.custom_headers.as_ref(),
199                &self.request_headers,
200                &self.cors_allow_credentials_origins,
201            ),
202        }
203    }
204
205    /// json file response
206    fn json_file_content_response(&self) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
207        let json_str = self.text_content.clone().unwrap_or_default();
208        json_response(
209            json_str.as_str(),
210            None,
211            self.custom_headers.as_ref(),
212            &self.request_headers,
213            Some(self.file_path.as_str()),
214            &self.cors_allow_credentials_origins,
215        )
216    }
217
218    /// csv file response
219    fn csv_file_content_response(&self) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
220        let text_content = self.text_content.clone().unwrap_or_default();
221        let mut rdr = csv::ReaderBuilder::new()
222            .has_headers(true)
223            .from_reader(text_content.as_bytes());
224
225        let csv_headers = if let Ok(csv_headers) = rdr.headers() {
226            csv_headers.clone()
227        } else {
228            log::error!(
229                "failed to analyze csv headers ({})",
230                self.file_path.as_str()
231            );
232            return internal_server_error_response(
233                "failed to analyze csv headers",
234                &self.request_headers,
235                &self.cors_allow_credentials_origins,
236            );
237        };
238
239        let rows = rdr
240            .records()
241            .map(|result| {
242                let record = result?;
243                let obj = csv_headers
244                    .iter()
245                    .zip(record.iter())
246                    .map(|(k, v)| (k.to_string(), Value::String(v.to_string())))
247                    .collect::<Map<_, _>>();
248                Ok(Value::Object(obj))
249            })
250            .collect::<Result<Vec<Value>, csv::Error>>();
251
252        match rows {
253            Ok(rows) => {
254                let jsonpath_key = if let Some(csv_records_key) = self.csv_records_key.as_ref() {
255                    csv_records_key.as_str()
256                } else {
257                    CSV_RECORDS_DEFAULT_KEY
258                };
259                let json_value = json_value_with_jsonpath_key(jsonpath_key, Value::from(rows));
260
261                let body = serde_json::to_string(&json_value);
262                match body {
263                    Ok(body) => json_response(
264                        body.as_str(),
265                        None,
266                        self.custom_headers.as_ref(),
267                        &self.request_headers,
268                        Some(self.file_path.as_str()),
269                        &self.cors_allow_credentials_origins,
270                    ),
271                    Err(err) => {
272                        log::error!(
273                            "failed to convert csv records to json response ({}): {}",
274                            self.file_path.as_str(),
275                            err
276                        );
277                        internal_server_error_response(
278                            "failed to convert csv records to json response",
279                            &self.request_headers,
280                            &self.cors_allow_credentials_origins,
281                        )
282                    }
283                }
284            }
285            Err(err) => {
286                log::error!(
287                    "failed to analyze csv records ({}): {}",
288                    self.file_path.as_str(),
289                    err
290                );
291                internal_server_error_response(
292                    "failed to analyze csv records",
293                    &self.request_headers,
294                    &self.cors_allow_credentials_origins,
295                )
296            }
297        }
298    }
299
300    /// binary file response
301    ///
302    /// `with_custom_headers` runs *after* `with_binary_body` (RFC 065)
303    /// — previously reversed, the same ordering bug as `json_response`
304    /// (D2): `with_binary_body` always sets a derived `content-type`,
305    /// so applying custom headers first let that overwrite an explicit
306    /// one every time, on every binary `file_path` response (`.png`,
307    /// `.pdf`, …).
308    fn binary_content_type_response(&self) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
309        let content = self.binary_content.clone().unwrap_or_default().to_owned();
310        let content_type = binary_content_type(self.file_path.as_str());
311        ResponseHandler::default()
312            .with_binary_body(content, Some(content_type))
313            .with_custom_headers(self.custom_headers.as_ref())
314            .into_response(&self.request_headers, &self.cors_allow_credentials_origins)
315    }
316}
317
318#[cfg(test)]
319mod tests {
320    //! RFC 077 P-05: pinned *before* the read-twice-into-one-read
321    //! refactor, per the tranche handoff. The dispatch between
322    //! `text_file_content_response` and `binary_content_type_response`
323    //! is decided by whether the file's bytes are valid UTF-8 — RFC
324    //! 065's review established this as load-bearing — never by the
325    //! file's extension. Both tests below deliberately mismatch
326    //! extension against content to make that point unambiguous: a
327    //! `.txt` file of invalid-UTF-8 bytes must still be served binary,
328    //! and a `.bin` file of valid-UTF-8 bytes must still be served text.
329    use hyper::HeaderMap;
330
331    use super::*;
332    use crate::response::confine::canonical_dir;
333
334    #[tokio::test]
335    async fn invalid_utf8_bytes_are_served_as_binary_regardless_of_a_text_extension() {
336        let dir = tempfile::tempdir().unwrap();
337        let bytes: &[u8] = &[0xFF, 0xFE, 0x00, 0x01, 0x02];
338        let file_path = dir.path().join("weird.txt");
339        std::fs::write(&file_path, bytes).unwrap();
340        let confine_to = canonical_dir(dir.path().to_str().unwrap());
341
342        let mut file_response = FileResponse::new(
343            file_path.to_str().unwrap(),
344            None,
345            &HeaderMap::new(),
346            confine_to.as_deref(),
347            &[],
348        );
349        let response = file_response.file_content_response().await.unwrap();
350
351        assert_eq!(response.status(), hyper::StatusCode::OK);
352        assert_eq!(
353            response.headers().get("content-type").unwrap(),
354            "application/octet-stream",
355            "invalid-UTF-8 bytes must take the binary path even though the \
356             extension says .txt"
357        );
358        let body = http_body_util::BodyExt::collect(response.into_body())
359            .await
360            .unwrap()
361            .to_bytes();
362        assert_eq!(body.as_ref(), bytes, "binary bytes must round-trip exactly");
363    }
364
365    #[tokio::test]
366    async fn valid_utf8_bytes_are_served_as_text_regardless_of_a_binary_extension() {
367        let dir = tempfile::tempdir().unwrap();
368        let text = "hello, this is plain text";
369        let file_path = dir.path().join("weird.bin");
370        std::fs::write(&file_path, text).unwrap();
371        let confine_to = canonical_dir(dir.path().to_str().unwrap());
372
373        let mut file_response = FileResponse::new(
374            file_path.to_str().unwrap(),
375            None,
376            &HeaderMap::new(),
377            confine_to.as_deref(),
378            &[],
379        );
380        let response = file_response.file_content_response().await.unwrap();
381
382        assert_eq!(response.status(), hyper::StatusCode::OK);
383        assert_eq!(
384            response.headers().get("content-type").unwrap(),
385            "text/plain; charset=utf-8",
386            "valid-UTF-8 bytes must take the text path even though the \
387             extension says .bin"
388        );
389        let body = http_body_util::BodyExt::collect(response.into_body())
390            .await
391            .unwrap()
392            .to_bytes();
393        assert_eq!(body.as_ref(), text.as_bytes());
394    }
395}