Skip to content

Commit

Permalink
remove hyper-old-types dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jaemk committed Jan 7, 2021
1 parent 82667f3 commit a5d52db
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target/
**/*.rs.bk
Cargo.lock
.idea/
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ tempfile = "3"
flate2 = { version = "1", optional = true }
tar = { version = "0.4", optional = true }
semver = "0.11"
hyper-old-types = "0.11"
zip = { version = "0.5", default-features = false, features = ["time"], optional = true }
either = { version = "1", optional = true }
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json"] }
hyper = "0.14"
indicatif = "0.15"
quick-xml = "0.20"
regex = "1"
Expand All @@ -37,8 +38,3 @@ rustls = ["reqwest/rustls-tls"]
[package.metadata.docs.rs]
# Whether to pass `--all-features` to Cargo (default: false)
all-features = true

[dependencies.reqwest]
version = "0.11"
default-features = false
features = ["blocking", "json"]
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ available (but _disabled_ by default):
* `compression-zip-bzip2`: Support for _zip_'s _bzip2_ compression format;
* `rustls`: Use [pure rust TLS implementation](https://github.com/ctz/rustls) for network requests. This feature does _not_ support 32bit macOS;

Please active the feature(s) needed by your release files.
Please activate the feature(s) needed by your release files.

### Example

Run the following example to see `self_update` in action:

`cargo run --example github --features "archive-tar archive-zip compression-flate2 compression-zip-deflate"`.

There's also an equivalent example for gitlab:

`cargo run --example gitlab --features "archive-tar archive-zip compression-flate2 compression-zip-deflate"`.

which runs something roughly equivalent to:

```rust
use self_update::cargo_crate_version;
Expand All @@ -41,7 +50,7 @@ fn update() -> Result<(), Box<::std::error::Error>> {
let status = self_update::backends::github::Update::configure()
.repo_owner("jaemk")
.repo_name("self_update")
.bin_name("self_update_example")
.bin_name("github")
.show_download_progress(true)
.current_version(cargo_crate_version!())
.build()?
Expand All @@ -51,9 +60,6 @@ fn update() -> Result<(), Box<::std::error::Error>> {
}
```

Run the above example to see `self_update` in action: `cargo run --example github --features "archive-tar compression-flate2"`.
There's also an equivalent example for gitlab: `cargo run --example gitlab --features "archive-tar compression-flate2"`.

Amazon S3, Google GCS, and DigitalOcean Spaces are also supported through the `S3` backend to check for new releases. Provided a `bucket_name`
and `asset_prefix` string, `self_update` will look up all matching files using the following format
as a convention for the filenames: `[directory/]<asset name>-<semver>-<platform/target>.<extension>`.
Expand Down
10 changes: 2 additions & 8 deletions src/backends/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ GitHub releases
use std::env::{self, consts::EXE_SUFFIX};
use std::path::{Path, PathBuf};

use hyper_old_types::header::{LinkValue, RelationType};
use indicatif::ProgressStyle;
use reqwest::{self, header};

use crate::backends::find_rel_next_link;
use crate::{
errors::*,
get_target,
Expand Down Expand Up @@ -204,13 +204,7 @@ impl ReleaseList {
.iter()
.filter_map(|link| {
if let Ok(link) = link.to_str() {
let lv = LinkValue::new(link.to_owned());
if let Some(rels) = lv.rel() {
if rels.contains(&RelationType::Next) {
return Some(link);
}
}
None
find_rel_next_link(link)
} else {
None
}
Expand Down
10 changes: 2 additions & 8 deletions src/backends/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Gitlab releases
use std::env::{self, consts::EXE_SUFFIX};
use std::path::{Path, PathBuf};

use hyper_old_types::header::{LinkValue, RelationType};
use indicatif::ProgressStyle;
use reqwest::{self, header};

use crate::backends::find_rel_next_link;
use crate::{
errors::*,
get_target,
Expand Down Expand Up @@ -188,13 +188,7 @@ impl ReleaseList {
.iter()
.filter_map(|link| {
if let Ok(link) = link.to_str() {
let lv = LinkValue::new(link.to_owned());
if let Some(rels) = lv.rel() {
if rels.contains(&RelationType::Next) {
return Some(link);
}
}
None
find_rel_next_link(link)
} else {
None
}
Expand Down
70 changes: 70 additions & 0 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,73 @@ Collection of modules supporting various release distribution backends
pub mod github;
pub mod gitlab;
pub mod s3;

/// Search for the first "rel" link-header uri in a full link header string.
/// Seems like reqwest/hyper threw away their link-header parser implementation...
///
/// ex:
/// `Link: <https://api.github.com/resource?page=2>; rel="next"`
/// `Link: <https://gitlab.com/api/v4/projects/13083/releases?id=13083&page=2&per_page=20>; rel="next"`
///
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link
/// header values may contain multiple values separated by commas
/// `Link: <https://place.com>; rel="next", <https://wow.com>; rel="next"`
pub(crate) fn find_rel_next_link(link_str: &str) -> Option<&str> {
for link in link_str.split(',') {
let mut uri = None;
let mut is_rel_next = false;
for part in link.split(';') {
let part = part.trim();
if part.starts_with('<') && part.ends_with('>') {
uri = Some(part.trim_start_matches('<').trim_end_matches('>'));
} else if part.starts_with("rel=") {
let part = part
.trim_start_matches("rel=")
.trim_end_matches('"')
.trim_start_matches('"');
if part == "next" {
is_rel_next = true;
}
}

if is_rel_next && uri.is_some() {
return uri;
}
}
}
None
}

#[cfg(test)]
mod test {
use crate::backends::find_rel_next_link;

#[test]
fn test_find_rel_link() {
let val = r##" <https://api.github.com/resource?page=2>; rel="next" "##;
let link = find_rel_next_link(val);
assert_eq!(link, Some("https://api.github.com/resource?page=2"));

let val = r##" <https://gitlab.com/api/v4/projects/13083/releases?id=13083&page=2&per_page=20>; rel="next" "##;
let link = find_rel_next_link(val);
assert_eq!(
link,
Some("https://gitlab.com/api/v4/projects/13083/releases?id=13083&page=2&per_page=20")
);

// returns the first one
let val = r##" <https://place.com>; rel="next", <https://wow.com>; rel="next" "##;
let link = find_rel_next_link(val);
assert_eq!(link, Some("https://place.com"));

// bad format, returns the second one
let val = r##" https://bad-format.com; rel="next", <https://wow.com>; rel="next" "##;
let link = find_rel_next_link(val);
assert_eq!(link, Some("https://wow.com"));

// all bad format, returns none
let val = r##" https://bad-format.com; rel="next", <https://also-bad.com; rel="next" , <https://good.com>; rel="preconnect" "##;
let link = find_rel_next_link(val);
assert!(link.is_none());
}
}
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ available (but _disabled_ by default):
* `compression-zip-bzip2`: Support for _zip_'s _bzip2_ compression format;
* `rustls`: Use [pure rust TLS implementation](https://github.com/ctz/rustls) for network requests. This feature does _not_ support 32bit macOS;
Please active the feature(s) needed by your release files.
Please activate the feature(s) needed by your release files.
### Example
Run the following example to see `self_update` in action:
`cargo run --example github --features "archive-tar archive-zip compression-flate2 compression-zip-deflate"`.
There's also an equivalent example for gitlab:
`cargo run --example gitlab --features "archive-tar archive-zip compression-flate2 compression-zip-deflate"`.
which runs something roughly equivalent to:
```rust
use self_update::cargo_crate_version;
Expand All @@ -41,7 +50,7 @@ fn update() -> Result<(), Box<::std::error::Error>> {
let status = self_update::backends::github::Update::configure()
.repo_owner("jaemk")
.repo_name("self_update")
.bin_name("self_update_example")
.bin_name("github")
.show_download_progress(true)
.current_version(cargo_crate_version!())
.build()?
Expand All @@ -51,9 +60,6 @@ fn update() -> Result<(), Box<::std::error::Error>> {
}
```
Run the above example to see `self_update` in action: `cargo run --example github --features "archive-tar compression-flate2"`.
There's also an equivalent example for gitlab: `cargo run --example gitlab --features "archive-tar compression-flate2"`.
Amazon S3, Google GCS, and DigitalOcean Spaces are also supported through the `S3` backend to check for new releases. Provided a `bucket_name`
and `asset_prefix` string, `self_update` will look up all matching files using the following format
as a convention for the filenames: `[directory/]<asset name>-<semver>-<platform/target>.<extension>`.
Expand Down

0 comments on commit a5d52db

Please sign in to comment.