1- use std:: fmt:: Debug ;
1+ use std:: { fmt:: Debug , net :: { AddrParseError , SocketAddr } } ;
22
33use bytes:: { Bytes , BytesMut } ;
44use url:: { ParseError , Url } ;
@@ -37,6 +37,8 @@ pub struct Request {
3737 headers : Headers ,
3838 // TODO: Support Stream bodies when napi.rs supports it
3939 body : Bytes ,
40+ local_socket : Option < SocketAddr > ,
41+ remote_socket : Option < SocketAddr > ,
4042}
4143
4244unsafe impl Sync for Request { }
@@ -57,14 +59,18 @@ impl Request {
5759 /// "POST".to_string(),
5860 /// "http://example.com/test.php".parse().unwrap(),
5961 /// headers,
60- /// "Hello, World!"
62+ /// "Hello, World!",
63+ /// None,
64+ /// None,
6165 /// );
62- pub fn new < T : Into < Bytes > > ( method : String , url : Url , headers : Headers , body : T ) -> Self {
66+ pub fn new < T : Into < Bytes > > ( method : String , url : Url , headers : Headers , body : T , local_socket : Option < SocketAddr > , remote_socket : Option < SocketAddr > ) -> Self {
6367 Self {
6468 method,
6569 url,
6670 headers,
6771 body : body. into ( ) ,
72+ local_socket,
73+ remote_socket,
6874 }
6975 }
7076
@@ -133,7 +139,9 @@ impl Request {
133139 /// "POST".to_string(),
134140 /// "http://example.com/test.php".parse().unwrap(),
135141 /// Headers::new(),
136- /// "Hello, World!"
142+ /// "Hello, World!",
143+ /// None,
144+ /// None,
137145 /// );
138146 ///
139147 /// assert_eq!(request.method(), "POST");
@@ -153,7 +161,9 @@ impl Request {
153161 /// "POST".to_string(),
154162 /// "http://example.com/test.php".parse().unwrap(),
155163 /// Headers::new(),
156- /// "Hello, World!"
164+ /// "Hello, World!",
165+ /// None,
166+ /// None,
157167 /// );
158168 ///
159169 /// assert_eq!(request.url().as_str(), "http://example.com/test.php");
@@ -176,7 +186,9 @@ impl Request {
176186 /// "POST".to_string(),
177187 /// "http://example.com/test.php".parse().unwrap(),
178188 /// headers,
179- /// "Hello, World!"
189+ /// "Hello, World!",
190+ /// None,
191+ /// None,
180192 /// );
181193 ///
182194 /// assert_eq!(request.headers().get("Accept"), Some(&vec!["text/html".to_string()]));
@@ -196,14 +208,60 @@ impl Request {
196208 /// "POST".to_string(),
197209 /// "http://example.com/test.php".parse().unwrap(),
198210 /// Headers::new(),
199- /// "Hello, World!"
211+ /// "Hello, World!",
212+ /// None,
213+ /// None,
200214 /// );
201215 ///
202216 /// assert_eq!(request.body(), "Hello, World!");
203217 /// ```
204218 pub fn body ( & self ) -> Bytes {
205219 self . body . clone ( )
206220 }
221+
222+ /// Returns the local socket address of the request.
223+ ///
224+ /// # Examples
225+ ///
226+ /// ```
227+ /// use lang_handler::{Request, Headers};
228+ ///
229+ /// let request = Request::new(
230+ /// "POST".to_string(),
231+ /// "http://example.com/test.php".parse().unwrap(),
232+ /// Headers::new(),
233+ /// "Hello, World!",
234+ /// None,
235+ /// None,
236+ /// );
237+ ///
238+ /// assert_eq!(request.local_socket(), None);
239+ /// ```
240+ pub fn local_socket ( & self ) -> Option < SocketAddr > {
241+ self . local_socket
242+ }
243+
244+ /// Returns the remote socket address of the request.
245+ ///
246+ /// # Examples
247+ ///
248+ /// ```
249+ /// use lang_handler::{Request, Headers};
250+ ///
251+ /// let request = Request::new(
252+ /// "POST".to_string(),
253+ /// "http://example.com/test.php".parse().unwrap(),
254+ /// Headers::new(),
255+ /// "Hello, World!",
256+ /// None,
257+ /// None,
258+ /// );
259+ ///
260+ /// assert_eq!(request.remote_socket(), None);
261+ /// ```
262+ pub fn remote_socket ( & self ) -> Option < SocketAddr > {
263+ self . remote_socket
264+ }
207265}
208266
209267/// Builds an HTTP request.
@@ -233,6 +291,8 @@ pub struct RequestBuilder {
233291 url : Option < Url > ,
234292 headers : Headers ,
235293 body : BytesMut ,
294+ local_socket : Option < SocketAddr > ,
295+ remote_socket : Option < SocketAddr > ,
236296}
237297
238298impl RequestBuilder {
@@ -251,6 +311,8 @@ impl RequestBuilder {
251311 url : None ,
252312 headers : Headers :: new ( ) ,
253313 body : BytesMut :: with_capacity ( 1024 ) ,
314+ local_socket : None ,
315+ remote_socket : None ,
254316 }
255317 }
256318
@@ -285,6 +347,8 @@ impl RequestBuilder {
285347 url : Some ( request. url ( ) . clone ( ) ) ,
286348 headers : request. headers ( ) . clone ( ) ,
287349 body : BytesMut :: from ( request. body ( ) ) ,
350+ local_socket : request. local_socket . clone ( ) ,
351+ remote_socket : request. remote_socket . clone ( ) ,
288352 }
289353 }
290354
@@ -372,6 +436,58 @@ impl RequestBuilder {
372436 self
373437 }
374438
439+ /// Sets the local socket of the request.
440+ ///
441+ /// # Examples
442+ ///
443+ /// ```
444+ /// use lang_handler::RequestBuilder;
445+ ///
446+ /// let request = RequestBuilder::new()
447+ /// .local_socket("127.0.0.1:8080").expect("invalid local socket")
448+ /// .build();
449+ ///
450+ /// assert_eq!(request.local_socket(), "127.0.0.1:8080");
451+ /// ```
452+ pub fn local_socket < T > ( mut self , local_socket : T ) -> Result < Self , AddrParseError >
453+ where
454+ T : Into < String > ,
455+ {
456+ match local_socket. into ( ) . parse ( ) {
457+ Err ( e) => Err ( e) ,
458+ Ok ( local_socket) => {
459+ self . local_socket = Some ( local_socket) ;
460+ Ok ( self )
461+ }
462+ }
463+ }
464+
465+ /// Sets the remote socket of the request.
466+ ///
467+ /// # Examples
468+ ///
469+ /// ```
470+ /// use lang_handler::RequestBuilder;
471+ ///
472+ /// let request = RequestBuilder::new()
473+ /// .remote_socket("127.0.0.1:8080").expect("invalid remote socket")
474+ /// .build();
475+ ///
476+ /// assert_eq!(request.remote_socket(), "127.0.0.1:8080");
477+ /// ```
478+ pub fn remote_socket < T > ( mut self , remote_socket : T ) -> Result < Self , AddrParseError >
479+ where
480+ T : Into < String > ,
481+ {
482+ match remote_socket. into ( ) . parse ( ) {
483+ Err ( e) => Err ( e) ,
484+ Ok ( remote_socket) => {
485+ self . remote_socket = Some ( remote_socket) ;
486+ Ok ( self )
487+ }
488+ }
489+ }
490+
375491 /// Builds the request.
376492 ///
377493 /// # Examples
@@ -395,6 +511,8 @@ impl RequestBuilder {
395511 . unwrap_or_else ( || Url :: parse ( "http://example.com" ) . unwrap ( ) ) ,
396512 headers : self . headers ,
397513 body : self . body . freeze ( ) ,
514+ local_socket : self . local_socket ,
515+ remote_socket : self . remote_socket ,
398516 }
399517 }
400518}
0 commit comments