-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove WrappedHttpResponse abstraction #19369
Conversation
Heads up! This PR modifies the following files:
|
components/net/lib.rs
Outdated
@@ -1,7 +1,7 @@ | |||
/* This Source Code Form is subject to the terms of the Mozilla Public | |||
* License, v. 2.0. If a copy of the MPL was not distributed with this | |||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
|
|||
#![feature(type_ascription)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be necessary.
components/net/http_loader.rs
Outdated
@@ -499,8 +480,7 @@ fn obtain_response(connector: &Pool<Connector>, | |||
debug!("Not notifying devtools (no request_id)"); | |||
None | |||
}; | |||
|
|||
return Ok((WrappedHttpResponse { response: response }, msg)); | |||
return Ok(({ response: HyperResponse }, msg)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was surprised that this builds, but I guess the type_ascription feature makes it valid. In any case, we should just use Ok((response, msg))
instead.
components/net/http_loader.rs
Outdated
fn from_http_response(response: HyperResponse) -> io::Result<StreamedResponse> { | ||
let mut encode_type = ""; | ||
let decoder; | ||
if let Some(encoding) = response.headers.get::<ContentEncoding>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this instead:
let decoder = if let Some(encoding) = response.headers.get::<ContentEncoding>() {
if encoding.contains(&Encoding::Gzip) {
Decoder::Gzip(GzDecoder::new(response)?)
} else if encoding.contains(...) {
...
}
} else {
Decoder::Plain(response)
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdm
this will cause borrow issue
I don't know how to solve it
error[E0505]: cannot move out of `response` because it is borrowed
--> components/net/http_loader.rs:297:61
|
291 | if let Some(encoding) = response.headers.get::<ContentEncoding>(){
| ---------------- borrow of `response.headers` occurs here
...
297 | decoder = Decoder::Brotli(Decompressor::new(response, 1024));
| ^^^^^^^^ move out of `response` occurs here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ref encoding
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for not familiar with Rust...
@jdm Do you mean this?
impl StreamedResponse {
fn from_http_response(response: HyperResponse) -> io::Result<StreamedResponse> {
let decoder;
if let Some(ref encoding) = response.headers.get::<ContentEncoding>() {
if encoding.contains(&Encoding::Gzip) {
decoder = Decoder::Gzip(GzDecoder::new(response)?);
} else if encoding.contains(&Encoding::Deflate) {
decoder = Decoder::Deflate(DeflateDecoder::new(response));
} else if encoding.contains(&Encoding::EncodingExt("br".to_owned())) {
decoder = Decoder::Brotli(Decompressor::new(response, 1024));
}
} else {
decoder = Decoder::Plain(response);
}
Ok(StreamedResponse { decoder: decoder })
}
}
it will get
error[E0505]: cannot move out of `response` because it is borrowed
--> components/net/http_loader.rs:293:56
|
291 | if let Some(ref encoding) = response.headers.get::<ContentEncoding>() {
| ---------------- borrow of `response.headers` occurs here
292 | if encoding.contains(&Encoding::Gzip) {
293 | decoder = Decoder::Gzip(GzDecoder::new(response)?);
| ^^^^^^^^ move out of `response` occurs here
error[E0505]: cannot move out of `response` because it is borrowed
--> components/net/http_loader.rs:295:64
|
291 | if let Some(ref encoding) = response.headers.get::<ContentEncoding>() {
| ---------------- borrow of `response.headers` occurs here
...
295 | decoder = Decoder::Deflate(DeflateDecoder::new(response));
| ^^^^^^^^ move out of `response` occurs here
error[E0505]: cannot move out of `response` because it is borrowed
--> components/net/http_loader.rs:297:61
|
291 | if let Some(ref encoding) = response.headers.get::<ContentEncoding>() {
| ---------------- borrow of `response.headers` occurs here
...
297 | decoder = Decoder::Brotli(Decompressor::new(response, 1024));
| ^^^^^^^^ move out of `response` occurs here
error[E0505]: cannot move out of `response` because it is borrowed
--> components/net/http_loader.rs:300:38
|
291 | if let Some(ref encoding) = response.headers.get::<ContentEncoding>() {
| ---------------- borrow of `response.headers` occurs here
...
300 | decoder = Decoder::Plain(response);
| ^^^^^^^^ move out of `response` occurs here
error[E0381]: use of possibly uninitialized variable: `decoder`
--> components/net/http_loader.rs:302:40
|
302 | Ok(StreamedResponse { decoder: decoder })
| ^^^^^^^ use of possibly uninitialized `decoder`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original implement avoid to call response
in a "response
calling".
But this method will encounter this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. In that case, instead of using strings let's use a simple enum DecoderType { Gzip, Deflate, Brotli, Plain }
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think calling .clone()
on response.headers.get::<ContentEncoding>()
should solve the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but no need to clone the whole header.
@jdm This time should be OK |
components/net/http_loader.rs
Outdated
_ => { | ||
fn from_http_response(response: HyperResponse) -> io::Result<StreamedResponse> { | ||
let decoder = { | ||
let headers = response.headers.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloning all of the headers associated with this response is inefficient. Let's do headers.get::<ContentEncoding>().cloned()
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdm If I change to
impl StreamedResponse {
fn from_http_response(response: HyperResponse) -> io::Result<StreamedResponse> {
let decoder = {
let contentEncoding = response.headers.get::<ContentEncoding>().clone();
if let Some(ref encoding) = contentEncoding {
if encoding.contains(&Encoding::Gzip) {
Decoder::Gzip(GzDecoder::new(response)?)
Error again:
error[E0505]: cannot move out of `response` because it is borrowed
--> components/net/http_loader.rs:294:50
|
291 | let contentEncoding = response.headers.get::<ContentEncoding>().clone();
| ---------------- borrow of `response.headers` occurs here
...
294 | Decoder::Gzip(GzDecoder::new(response)?)
| ^^^^^^^^ move out of `response` occurs here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if change to
let contentEncoding = response.headers.clone().get::<ContentEncoding>();
the lift time will be not long enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did say cloned
instead of clone
. Does that make a difference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry :(
I didn't read carefully.
@bors-servo: r+ |
📌 Commit 2e3d1d8 has been approved by |
Remove WrappedHttpResponse abstraction <!-- Please describe your changes on the following line: --> Remove `WrappedHttpResponse` abstraction, and use `HyperResponse` --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #19361 (github issue number if applicable). <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19369) <!-- Reviewable:end -->
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
Remove
WrappedHttpResponse
abstraction, and useHyperResponse
./mach build -d
does not report any errors./mach test-tidy
does not report any errorsThis change is