Skip to content

Commit

Permalink
refactor: use http crate primitives instead of a custom impl (#706)
Browse files Browse the repository at this point in the history
* refactor: use `http` crate primitives instead of custom impl

* android

* macos

* fix macos & ios build

* linux

* fix linux build

* change files
  • Loading branch information
amrbashir authored Sep 29, 2022
1 parent 8ec2b2a commit 1510e45
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 617 deletions.
5 changes: 5 additions & 0 deletions .changes/custom-protocol-http-crate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

**Breaking change** Custom protocol now takes `Request` and returns `Response` types from `http` crate.
5 changes: 5 additions & 0 deletions .changes/removed-error-variants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

**Breaking change** Removed http error variants from `wry::Error` and replaced with generic `HttpError` variant that can be used to convert `http` crate errors.
5 changes: 5 additions & 0 deletions .changes/use-http-crate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

**Breaking change** Removed `http` module and replaced with re-export of `http` crate.
11 changes: 7 additions & 4 deletions examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::ResponseBuilder,
http::{header::CONTENT_TYPE, Response},
webview::WebViewBuilder,
};

Expand All @@ -24,8 +24,8 @@ fn main() -> wry::Result<()> {
let webview = WebViewBuilder::new(window)
.unwrap()
.with_custom_protocol("wry".into(), move |request| {
// Remove url scheme
let path = request.uri().replace("wry://", "");
let path = request.uri().to_string();
let path = path.strip_prefix("wry://").unwrap();
// Read the file content from file path
let content = read(canonicalize(&path)?)?;

Expand All @@ -42,7 +42,10 @@ fn main() -> wry::Result<()> {
unimplemented!();
};

ResponseBuilder::new().mimetype(meta).body(data)
Response::builder()
.header(CONTENT_TYPE, meta)
.body(data)
.map_err(Into::into)
})
// tell the webview to load the custom protocol
.with_url("wry://examples/index.html")?
Expand Down
13 changes: 8 additions & 5 deletions examples/form_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::{method::Method, ResponseBuilder},
http::{header::CONTENT_TYPE, method::Method, Response},
webview::WebViewBuilder,
};

Expand All @@ -30,11 +30,14 @@ fn main() -> wry::Result<()> {
println!("Value sent; {:?}", body);
}
}
// Remove url scheme
let path = request.uri().replace("wry://", "");
ResponseBuilder::new()
.mimetype("text/html")

let path = request.uri().to_string();
let path = path.strip_prefix("wry://").unwrap();

Response::builder()
.header(CONTENT_TYPE, "text/html")
.body(read(canonicalize(&path)?)?)
.map_err(Into::into)
})
// tell the webview to load the custom protocol
.with_url("wry://examples/form.html")?
Expand Down
1 change: 0 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<h1>Welcome to WRY!</h1>
<a href="/hello.html">Link</a>
<script type="text/javascript" src="/hello.js"></script>
<img src="/icon.png" />
</body>

</html>
22 changes: 14 additions & 8 deletions examples/stream_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use wry::http::Response;

fn main() -> wry::Result<()> {
use http_range::HttpRange;
use std::{
Expand All @@ -16,7 +18,7 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::ResponseBuilder,
http::{header::CONTENT_TYPE, status::StatusCode},
webview::WebViewBuilder,
};

Expand Down Expand Up @@ -51,15 +53,16 @@ fn main() -> wry::Result<()> {
let _webview = WebViewBuilder::new(window)
.unwrap()
.with_custom_protocol("wry".into(), move |request| {
// Remove url scheme
let path = request.uri().replace("wry://", "");
let path = request.uri().to_string();
let path = path.strip_prefix("wry://").unwrap();

// Read the file content from file path
let mut content = File::open(canonicalize(&path)?)?;

// Return asset contents and mime types based on file extentions
// If you don't want to do this manually, there are some crates for you.
// Such as `infer` and `mime_guess`.
let mut status_code = 200;
let mut status_code = StatusCode::OK;
let mut buf = Vec::new();

// guess our mimetype from the path
Expand All @@ -72,7 +75,7 @@ fn main() -> wry::Result<()> {
};

// prepare our http response
let mut response = ResponseBuilder::new();
let mut response = Response::builder();

// read our range header if it exist, so we can return partial content
if let Some(range) = request.headers().get("range") {
Expand All @@ -98,8 +101,7 @@ fn main() -> wry::Result<()> {
// last byte we are reading, the length of the range include the last byte
// who should be skipped on the header
let last_byte = range.start + real_length - 1;
// partial content
status_code = 206;
status_code = StatusCode::PARTIAL_CONTENT;

response = response.header("Connection", "Keep-Alive");
response = response.header("Accept-Ranges", "bytes");
Expand All @@ -120,7 +122,11 @@ fn main() -> wry::Result<()> {
content.read_to_end(&mut buf)?;
}

response.mimetype(mimetype).status(status_code).body(buf)
response
.header(CONTENT_TYPE, mimetype)
.status(status_code)
.body(buf)
.map_err(Into::into)
})
// tell the webview to load the custom protocol
.with_url("wry://examples/stream.html")?
Expand Down
19 changes: 0 additions & 19 deletions src/http/mod.rs

This file was deleted.

Loading

0 comments on commit 1510e45

Please sign in to comment.