Skip to content

Commit 4450623

Browse files
authored
Merge pull request #54 from yoshuawuyts/client-host
feat(client): insert host header
2 parents e84e447 + 8005e42 commit 4450623

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/client.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Encoder {
4949
}
5050

5151
/// Encode an HTTP request on the client.
52-
pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
52+
pub async fn encode(req: Request) -> Result<Encoder, Error> {
5353
let mut buf: Vec<u8> = vec![];
5454

5555
let mut url = req.url().path().to_owned();
@@ -66,6 +66,20 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
6666
log::trace!("> {}", &val);
6767
buf.write_all(val.as_bytes()).await?;
6868

69+
// Insert Host header
70+
// Insert host
71+
let host = req.url().host_str().ok_or_else(|| {
72+
Error::from_str(
73+
ErrorKind::InvalidInput,
74+
"missing hostname",
75+
StatusCode::BadRequest,
76+
)
77+
})?;
78+
79+
let val = format!("host: {}\r\n", host);
80+
log::trace!("> {}", &val);
81+
buf.write_all(val.as_bytes()).await?;
82+
6983
// If the body isn't streaming, we can set the content-length ahead of time. Else we need to
7084
// send all items in chunks.
7185
if let Some(len) = req.len() {

tests/fixtures/client-request1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
GET / HTTP/1.1
2+
host: example.com
23
content-length: 0
34
date: {DATE}
45

0 commit comments

Comments
 (0)