Skip to content

Commit e24bfb3

Browse files
committed
feat: add local and peer IPs to HttpRawRequest and HttpRequest
1 parent b1fcd51 commit e24bfb3

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/http/request.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use anyhow::{bail, Context, Result};
22
use log::trace;
33
use serde::{Deserialize, Serialize};
4-
use std::{collections::HashMap, net::TcpStream, str::FromStr};
4+
use std::{
5+
collections::HashMap,
6+
net::{IpAddr, TcpStream},
7+
str::FromStr,
8+
};
59

610
use super::{HttpCookie, HttpHeader, HttpMethod, HttpRequestRaw, HttpVersion, MultipartBody};
711

@@ -17,6 +21,9 @@ pub struct HttpRequest {
1721
pub headers: HashMap<String, HttpHeader>,
1822
pub cookies: HashMap<String, HttpCookie>,
1923
pub body: Vec<u8>,
24+
25+
pub peer_ip: IpAddr,
26+
pub local_ip: IpAddr,
2027
}
2128

2229
impl HttpRequest {
@@ -63,6 +70,8 @@ impl HttpRequest {
6370
resource_path,
6471
query: query_params,
6572
url,
73+
peer_ip: raw_request.peer_ip,
74+
local_ip: raw_request.local_ip,
6675
})
6776
}
6877

@@ -169,12 +178,16 @@ mod tests {
169178
headers: HashMap::new(),
170179
cookies: HashMap::new(),
171180
body: vec![],
181+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
182+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
172183
};
173184

174185
let raw_request = HttpRequestRaw {
175186
request_line: "GET /api/weather HTTP/1.1".to_owned(),
176187
headers: vec![],
177188
body: vec![],
189+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
190+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
178191
};
179192

180193
let actual = HttpRequest::from_raw_request(raw_request).unwrap();
@@ -196,12 +209,16 @@ mod tests {
196209
headers: HashMap::new(),
197210
cookies: HashMap::new(),
198211
body: vec![],
212+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
213+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
199214
};
200215

201216
let raw_request = HttpRequestRaw {
202217
request_line: "GET /api/weather?country=France&city=Paris HTTP/1.1".to_owned(),
203218
headers: vec![],
204219
body: vec![],
220+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
221+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
205222
};
206223

207224
let actual = HttpRequest::from_raw_request(raw_request).unwrap();
@@ -229,13 +246,17 @@ mod tests {
229246
headers: headers.clone(),
230247
cookies: HashMap::new(),
231248
body: vec![],
249+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
250+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
232251
};
233252

234253
let headers_vec: Vec<HttpHeader> = headers.values().cloned().collect();
235254
let raw_request = HttpRequestRaw {
236255
request_line: "GET /api/weather HTTP/1.1".to_owned(),
237256
headers: headers_vec,
238257
body: vec![],
258+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
259+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
239260
};
240261

241262
let actual = HttpRequest::from_raw_request(raw_request).unwrap();
@@ -255,12 +276,16 @@ mod tests {
255276
headers: HashMap::new(),
256277
cookies: HashMap::new(),
257278
body: body_bytes.to_vec(),
279+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
280+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
258281
};
259282

260283
let raw_request = HttpRequestRaw {
261284
request_line: "POST /users HTTP/1.1".to_owned(),
262285
headers: vec![],
263286
body: body_bytes.to_vec(),
287+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
288+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
264289
};
265290

266291
let actual = HttpRequest::from_raw_request(raw_request).unwrap();

src/http/request_raw.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Result;
22
use log::trace;
33
use std::{
44
io::{BufRead, BufReader, Read},
5-
net::TcpStream,
5+
net::{IpAddr, TcpStream},
66
};
77

88
use super::HttpHeader;
@@ -11,13 +11,18 @@ pub struct HttpRequestRaw {
1111
pub request_line: String,
1212
pub headers: Vec<HttpHeader>,
1313
pub body: Vec<u8>,
14+
pub peer_ip: IpAddr,
15+
pub local_ip: IpAddr,
1416
}
1517

1618
impl HttpRequestRaw {
1719
pub fn from_tcp(stream: &TcpStream) -> Result<HttpRequestRaw> {
1820
trace!("trying to convert TCP message into HTTP request");
1921
let mut buf_reader = BufReader::new(stream);
2022

23+
let peer_ip = stream.peer_addr()?.ip();
24+
let local_ip = stream.local_addr()?.ip();
25+
2126
let mut request_line = String::new();
2227
let mut headers = Vec::new();
2328
let mut body = Vec::new();
@@ -61,6 +66,8 @@ impl HttpRequestRaw {
6166
request_line,
6267
headers,
6368
body,
69+
peer_ip,
70+
local_ip,
6471
})
6572
}
6673
}

src/router.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ impl RoutingData {
342342

343343
#[cfg(test)]
344344
mod tests {
345+
use std::net::IpAddr;
346+
345347
use serde_json::{json, Value};
346348

347349
use crate::http::{HttpRequestRaw, HttpResponseBuilder};
@@ -421,6 +423,8 @@ mod tests {
421423
request_line: "GET /hello HTTP/1.1".to_owned(),
422424
headers: Vec::new(),
423425
body: vec![],
426+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
427+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
424428
})
425429
.unwrap();
426430

@@ -438,6 +442,8 @@ mod tests {
438442
request_line: "GET /not-a-real-page HTTP/1.1".to_owned(),
439443
headers: Vec::new(),
440444
body: vec![],
445+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
446+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
441447
})
442448
.unwrap();
443449

@@ -457,6 +463,8 @@ mod tests {
457463
request_line: "GET /hello HTTP/1.1".to_owned(),
458464
headers: Vec::new(),
459465
body: vec![],
466+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
467+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
460468
})
461469
.unwrap();
462470

@@ -478,6 +486,8 @@ mod tests {
478486
request_line: "GET /hello HTTP/1.1".to_owned(),
479487
headers: Vec::new(),
480488
body: vec![],
489+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
490+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
481491
})
482492
.unwrap();
483493

@@ -493,6 +503,8 @@ mod tests {
493503
request_line: "POST /user HTTP/1.1".to_owned(),
494504
headers: Vec::new(),
495505
body: vec![],
506+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
507+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
496508
})
497509
.unwrap();
498510

@@ -510,6 +522,8 @@ mod tests {
510522
request_line: "GET /users/5/details HTTP/1.1".to_owned(),
511523
headers: Vec::new(),
512524
body: vec![],
525+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
526+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
513527
})
514528
.unwrap();
515529

@@ -528,6 +542,8 @@ mod tests {
528542
request_line: "GET /users/7/details HTTP/1.1".to_owned(),
529543
headers: Vec::new(),
530544
body: vec![],
545+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
546+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
531547
})
532548
.unwrap();
533549

@@ -544,6 +560,8 @@ mod tests {
544560
request_line: "GET /users HTTP/1.1".to_owned(),
545561
headers: Vec::new(),
546562
body: vec![],
563+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
564+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
547565
})
548566
.unwrap();
549567

@@ -561,6 +579,8 @@ mod tests {
561579
request_line: "GET /users/17/info/gender HTTP/1.1".to_owned(),
562580
headers: Vec::new(),
563581
body: vec![],
582+
peer_ip: IpAddr::from_str("0.0.0.0").unwrap(),
583+
local_ip: IpAddr::from_str("0.0.0.0").unwrap(),
564584
})
565585
.unwrap();
566586

0 commit comments

Comments
 (0)