Crate parse_link_header[−][src]
Expand description
A library for parse http link header.
How to use
Note for version 0.1.x
The version 0.1 can’t correctly handle the relative ref
which described in https://tools.ietf.org/html/rfc3986#section-4.1
The parsed value of version 0.1 refers to the return value of https://github.com/thlorenz/parse-link-header, which is a HashMap
with the same structure.
So if you want to parse relative ref
, please use version 0.2
.
Or if you don’t care about relative ref
and wanna simple HashMap<String, HashMap<String, String>>
result, you can use version 0.1
.
Example
In your Cargo.toml
, add:
[dependencies]
parse_link_header = "0.3"
Then:
let link_header = r#"<https://api.github.com/repositories/41986369/contributors?page=2>; rel="next", <https://api.github.com/repositories/41986369/contributors?page=14>; rel="last""#;
let res = parse_link_header::parse(link_header);
assert!(res.is_ok());
let val = res.unwrap();
assert_eq!(val.len(), 2);
assert_eq!(val.get(&Some("next".to_string())).unwrap().raw_uri, "https://api.github.com/repositories/41986369/contributors?page=2");
assert_eq!(val.get(&Some("last".to_string())).unwrap().raw_uri, "https://api.github.com/repositories/41986369/contributors?page=14");
The parsed value is a Result<HashMap<Option<Rel>, Link>, Error>
(aka a
LinkMap
), which Rel
and Link
is:
use std::collections::HashMap;
use http::Uri;
#[derive(Debug, PartialEq)]
pub struct Link {
pub uri: Uri, // https://docs.rs/http/0.2.1/http/uri/struct.Uri.html
pub raw_uri: String,
pub queries: HashMap<String, String>,
pub params: HashMap<String, String>,
}
type Rel = String;
You can see why the key of HashMap
is Option<Rel>
because if you won’t provide a rel
type, the key will be an empty string.
Refer to https://tools.ietf.org/html/rfc8288#section-3.3 (October 2017), the rel parameter MUST be present.
Therefore, if you find that key is None
, please check if you provide the rel
type.
parse_with_rel
Version >= 0.3.0
Alternatively, use the parse_with_rel()
function to get a HashMap<String, Link>
(aka a
RelLinkMap
), as in:
let link_header = r#"<https://api.github.com/repositories/41986369/contributors?page=2>; rel="next", <https://api.github.com/repositories/41986369/contributors?page=14>; rel="last""#;
let res = parse_link_header::parse_with_rel(link_header);
assert!(res.is_ok());
let val = res.unwrap();
assert_eq!(val.len(), 2);
assert_eq!(val.get("next").unwrap().raw_uri, "https://api.github.com/repositories/41986369/contributors?page=2");
assert_eq!(val.get("last").unwrap().raw_uri, "https://api.github.com/repositories/41986369/contributors?page=14");
Feature: url
Version >= 0.3.0
If you enable the url
feature, the uri
field of struct Link
will be
of type url::Url from the url crate, rather than the
http::Uri
it normally is. This allows direct use of the uri
field with other popular
crates that use url
, such as reqwest
.
NOTE: This implictly disabled support for relative refs, as URLs do not support relative refs (whereas URIs do).
Structs
An error encountered when attempting to parse a Link:
HTTP header
Struct to describe a single Link:
header entry
Enums
Enum to indicate the type of error encountered
Functions
Parse link header into a RelLinkMap
.
Type Definitions
Type alias for the parsed data returned as a HashMap
with a key of Option<Rel>
.
Type alias for the parsed data returned as a HashMap
where the rel
parameter is required to
be present.
A Result
alias where the Err
case is parse_link_header::Error
.