net: use an empty body for the null body case

This commit is contained in:
Gregory Terzian 2020-05-27 16:01:29 +08:00
parent 7d51997214
commit 3535dd7412

View file

@ -421,57 +421,65 @@ fn obtain_response(
let devtools_bytes = StdArc::new(Mutex::new(vec![])); let devtools_bytes = StdArc::new(Mutex::new(vec![]));
let request_body = match body { // https://url.spec.whatwg.org/#percent-encoded-bytes
Some(chunk_requester) => { let encoded_url = url
// TODO: If body is a stream, append `Transfer-Encoding`/`chunked`, .clone()
// see step 4.2 of https://fetch.spec.whatwg.org/#concept-http-network-fetch .into_url()
.as_ref()
.replace("|", "%7C")
.replace("{", "%7B")
.replace("}", "%7D");
let (body_chan, body_port) = ipc::channel().unwrap(); let request = if let Some(chunk_requester) = body {
// TODO: If body is a stream, append `Transfer-Encoding`/`chunked`,
// see step 4.2 of https://fetch.spec.whatwg.org/#concept-http-network-fetch
let (sender, receiver) = channel(1); let (body_chan, body_port) = ipc::channel().unwrap();
let _ = chunk_requester.send(BodyChunkRequest::Connect(body_chan)); let (sender, receiver) = channel(1);
// https://fetch.spec.whatwg.org/#concept-request-transmit-body let _ = chunk_requester.send(BodyChunkRequest::Connect(body_chan));
// Request the first chunk, corresponding to Step 3 and 4.
let _ = chunk_requester.send(BodyChunkRequest::Chunk);
let devtools_bytes = devtools_bytes.clone(); // https://fetch.spec.whatwg.org/#concept-request-transmit-body
// Request the first chunk, corresponding to Step 3 and 4.
let _ = chunk_requester.send(BodyChunkRequest::Chunk);
ROUTER.add_route( let devtools_bytes = devtools_bytes.clone();
body_port.to_opaque(),
Box::new(move |message| {
let bytes: Vec<u8> = message.to().unwrap();
let chunk_requester = chunk_requester.clone();
let sender = sender.clone();
devtools_bytes.lock().unwrap().append(&mut bytes.clone()); ROUTER.add_route(
body_port.to_opaque(),
Box::new(move |message| {
let bytes: Vec<u8> = message.to().unwrap();
let chunk_requester = chunk_requester.clone();
let sender = sender.clone();
HANDLE.lock().unwrap().as_mut().unwrap().spawn( devtools_bytes.lock().unwrap().append(&mut bytes.clone());
// Step 5.1.2.2
// Transmit a chunk over the network(and blocking until this is done).
sender
.send(bytes)
.map(move |_| {
// Step 5.1.2.3
// Request the next chunk.
let _ = chunk_requester.send(BodyChunkRequest::Chunk);
()
})
.map_err(|_| ()),
);
}),
);
receiver HANDLE.lock().unwrap().as_mut().unwrap().spawn(
}, // Step 5.1.2.2
_ => { // Transmit a chunk over the network(and blocking until this is done).
let (_sender, mut receiver) = channel(1); sender
.send(bytes)
.map(move |_| {
// Step 5.1.2.3
// Request the next chunk.
let _ = chunk_requester.send(BodyChunkRequest::Chunk);
()
})
.map_err(|_| ()),
);
}),
);
receiver.close(); HyperRequest::builder()
.method(method)
receiver .uri(encoded_url)
}, .body(Body::wrap_stream(receiver))
} else {
HyperRequest::builder()
.method(method)
.uri(encoded_url)
.body(Body::empty())
}; };
context context
@ -489,19 +497,6 @@ fn obtain_response(
.unwrap() .unwrap()
.set_attribute(ResourceAttribute::ConnectStart(connect_start)); .set_attribute(ResourceAttribute::ConnectStart(connect_start));
// https://url.spec.whatwg.org/#percent-encoded-bytes
let request = HyperRequest::builder()
.method(method)
.uri(
url.clone()
.into_url()
.as_ref()
.replace("|", "%7C")
.replace("{", "%7B")
.replace("}", "%7D"),
)
.body(Body::wrap_stream(request_body));
// TODO: We currently don't know when the handhhake before the connection is done // TODO: We currently don't know when the handhhake before the connection is done
// so our best bet would be to set `secure_connection_start` here when we are currently // so our best bet would be to set `secure_connection_start` here when we are currently
// fetching on a HTTPS url. // fetching on a HTTPS url.