Skip to content

Commit 2fbe773

Browse files
authored
Avoid copying inner request (#39)
1 parent 3899d3e commit 2fbe773

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

src/napi.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ pub struct RequestOptions {
587587
/// It provides methods to access the HTTP method, URI, headers, and body of
588588
/// the request along with a toJSON method to convert it to a JSON object.
589589
#[napi]
590-
#[derive(Debug, Clone)]
590+
#[derive(Debug)]
591591
pub struct Request(InnerRequest);
592592

593593
#[napi]
@@ -914,6 +914,44 @@ impl Request {
914914
}
915915
}
916916

917+
// Rust-only methods (not exposed to JavaScript)
918+
impl Request {
919+
/// Consume this Request and return the inner HTTP request.
920+
pub fn into_inner(self) -> InnerRequest {
921+
self.0
922+
}
923+
}
924+
925+
impl Clone for Request {
926+
fn clone(&self) -> Self {
927+
use crate::RequestExt;
928+
929+
// Build a new request with all fields cloned
930+
let mut builder = http::request::Builder::new()
931+
.method(self.0.method().clone())
932+
.uri(self.0.uri().clone())
933+
.version(self.0.version());
934+
935+
for (key, value) in self.0.headers() {
936+
builder = builder.header(key.clone(), value.clone());
937+
}
938+
939+
let mut req = builder
940+
.body(self.0.body().clone())
941+
.expect("Failed to build request");
942+
943+
// Copy extensions manually
944+
if let Some(docroot) = self.0.document_root() {
945+
req.set_document_root(docroot.clone());
946+
}
947+
if let Some(socket) = self.0.socket_info() {
948+
req.set_socket_info(socket.clone());
949+
}
950+
951+
Request(req)
952+
}
953+
}
954+
917955
impl Deref for Request {
918956
type Target = InnerRequest;
919957

@@ -922,12 +960,6 @@ impl Deref for Request {
922960
}
923961
}
924962

925-
impl DerefMut for Request {
926-
fn deref_mut(&mut self) -> &mut Self::Target {
927-
&mut self.0
928-
}
929-
}
930-
931963
impl From<InnerRequest> for Request {
932964
fn from(request: InnerRequest) -> Self {
933965
Request(request)

0 commit comments

Comments
 (0)