Skip to content

Commit 7aac486

Browse files
committed
Added AddHttpHeaders trait to make sure there is an abstraction
1 parent a22965d commit 7aac486

File tree

7 files changed

+30
-7
lines changed

7 files changed

+30
-7
lines changed

my-http-server-core/src/http_fail_result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rust_extensions::StrOrString;
22

3-
use crate::{http_headers_to_use::*, HttpOkResult, WebContentType};
3+
use crate::{http_headers::*, HttpOkResult, WebContentType};
44
use my_hyper_utils::*;
55
#[derive(Debug, Clone)]
66
pub struct HttpFailResult {

my-http-server-core/src/http_headers/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ mod headers;
22
pub use headers::*;
33
mod header_value;
44
pub use header_value::*;
5+
mod common_headers;
6+
pub use common_headers::*;
7+
8+
pub trait AddHttpHeaders {
9+
fn add_header(&mut self, header_name: impl Into<String>, header_name: impl Into<String>);
10+
}

my-http-server-core/src/http_ok_result/http_result_builder.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use rust_extensions::StrOrString;
44

5-
use crate::{cookies::*, HttpFailResult, HttpOutput, WebContentType};
5+
use crate::{cookies::*, AddHttpHeaders, HttpFailResult, HttpOutput, WebContentType};
66

77
use super::HttpOkResult;
88

@@ -129,3 +129,15 @@ impl HttpResultBuilder {
129129
.into()
130130
}
131131
}
132+
133+
impl AddHttpHeaders for HttpResultBuilder {
134+
fn add_header(&mut self, key: impl Into<String>, value: impl Into<String>) {
135+
if self.headers.is_none() {
136+
self.headers = Some(HashMap::new());
137+
}
138+
self.headers
139+
.as_mut()
140+
.unwrap()
141+
.insert(key.into(), value.into());
142+
}
143+
}

my-http-server-core/src/http_request/http_request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::{collections::HashMap, net::SocketAddr, sync::Arc};
22

33
use crate::{
4-
http_headers_to_use::*, CookiesReader, HttpFailResult, HttpPath, HttpPathReader,
5-
HttpRequestBody, HttpRequestHeaders, RequestData, RequestIp, UrlEncodedData,
4+
http_headers::*, CookiesReader, HttpFailResult, HttpPath, HttpPathReader, HttpRequestBody,
5+
HttpRequestHeaders, RequestData, RequestIp, UrlEncodedData,
66
};
77

88
use hyper::{Method, Uri};

my-http-server-core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ mod encoded_value;
4444

4545
pub use encoded_value::*;
4646

47-
mod http_headers_to_use;
4847
mod http_request;
4948
pub use http_request::*;
5049
pub mod convert_from_str;

static-files-middleware/src/middleware.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::collections::HashMap;
22

33
use my_http_server_core::{
4-
HttpContext, HttpFailResult, HttpOkResult, HttpOutput, HttpPath, HttpServerMiddleware,
5-
WebContentType,
4+
AddHttpHeaders, HttpContext, HttpFailResult, HttpOkResult, HttpOutput, HttpPath,
5+
HttpServerMiddleware, WebContentType,
66
};
77

88
use crate::FilesAccess;
@@ -202,6 +202,12 @@ impl HttpServerMiddleware for StaticFilesMiddleware {
202202
}
203203
}
204204

205+
impl AddHttpHeaders for StaticFilesMiddleware {
206+
fn add_header(&mut self, header_name: impl Into<String>, header_value: impl Into<String>) {
207+
self.headers.insert(header_name.into(), header_value.into());
208+
}
209+
}
210+
205211
fn get_file_name(file_folder: &str, path: &str) -> String {
206212
format!("{}{}", file_folder, path)
207213
}

0 commit comments

Comments
 (0)