Skip to content
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

Merged
merged 1 commit into from
Nov 25, 2017
Merged

Remove WrappedHttpResponse abstraction #19369

merged 1 commit into from
Nov 25, 2017

Conversation

tigercosmos
Copy link
Contributor

@tigercosmos tigercosmos commented Nov 24, 2017

Remove WrappedHttpResponse abstraction, and use HyperResponse



This change is Reviewable

@highfive
Copy link

Heads up! This PR modifies the following files:

  • @KiChjang: components/net/lib.rs, components/net/http_loader.rs

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 24, 2017
@highfive
Copy link

warning Warning warning

  • These commits modify net code, but no tests are modified. Please consider adding a test!

@@ -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)]
Copy link
Member

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.

@@ -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));
Copy link
Member

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.

fn from_http_response(response: HyperResponse) -> io::Result<StreamedResponse> {
let mut encode_type = "";
let decoder;
if let Some(encoding) = response.headers.get::<ContentEncoding>() {
Copy link
Member

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)
};

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ref encoding.

Copy link
Contributor Author

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`

Copy link
Contributor Author

@tigercosmos tigercosmos Nov 24, 2017

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.

Copy link
Member

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 }.

Copy link
Contributor

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.

Copy link
Member

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 jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 24, 2017
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 24, 2017
@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 24, 2017
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 25, 2017
@tigercosmos
Copy link
Contributor Author

@jdm This time should be OK

_ => {
fn from_http_response(response: HyperResponse) -> io::Result<StreamedResponse> {
let decoder = {
let headers = response.headers.clone();
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

@tigercosmos tigercosmos Nov 25, 2017

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 25, 2017
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 25, 2017
@jdm
Copy link
Member

jdm commented Nov 25, 2017

@bors-servo: r+

@bors-servo
Copy link
Contributor

📌 Commit 2e3d1d8 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 25, 2017
@bors-servo
Copy link
Contributor

⌛ Testing commit 2e3d1d8 with merge adfd51c...

bors-servo pushed a commit that referenced this pull request Nov 25, 2017
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 -->
@bors-servo
Copy link
Contributor

☀️ 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
Approved by: jdm
Pushing adfd51c to master...

@bors-servo bors-servo merged commit 2e3d1d8 into servo:master Nov 25, 2017
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 25, 2017
@tigercosmos
Copy link
Contributor Author

thanks @jdm @KiChjang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove WrappedHttpResponse abstraction
5 participants