-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathhttp.rs
More file actions
115 lines (97 loc) · 3.28 KB
/
http.rs
File metadata and controls
115 lines (97 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{CommonSourceConfig, RemoteFile, RemoteFileUri, SourceId, SourceLocation};
/// Configuration for symbol server HTTP endpoints.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct HttpSourceConfig {
/// Unique source identifier.
pub id: SourceId,
/// Absolute URL of the symbol server.
pub url: Url,
/// Additional headers to be sent to the symbol server with every request.
#[serde(default)]
pub headers: HttpHeaders,
/// Configuration common to all sources.
#[serde(flatten)]
pub files: CommonSourceConfig,
/// If true, it should be possible to download from this source
/// even if SSL certificates can't be verified.
///
/// Don't use this lightly!
#[serde(default)]
pub accept_invalid_certs: bool,
}
/// A list of http headers passed to a [`HttpSourceConfig`].
#[derive(Clone, Default, Deserialize, Serialize)]
#[serde(transparent)]
pub struct HttpHeaders(pub BTreeMap<String, String>);
impl fmt::Debug for HttpHeaders {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut map = f.debug_map();
for key in self.0.keys() {
map.entry(key, &"<header value>");
}
map.finish()
}
}
/// The HTTP-specific [`RemoteFile`].
#[derive(Debug, Clone)]
pub struct HttpRemoteFile {
/// The underlying [`HttpSourceConfig`].
pub source: Arc<HttpSourceConfig>,
pub(crate) location: SourceLocation,
/// Additional HTTP headers to send with a symbol request.
pub headers: HttpHeaders,
}
impl From<HttpRemoteFile> for RemoteFile {
fn from(source: HttpRemoteFile) -> Self {
Self::Http(source)
}
}
impl HttpRemoteFile {
/// Creates a new [`HttpRemoteFile`].
pub fn new(source: Arc<HttpSourceConfig>, location: SourceLocation) -> Self {
Self {
source,
location,
headers: Default::default(),
}
}
/// Creates a new [`HttpRemoteFile`] from the given [`Url`].
/// This internally creates a bogus [`HttpSourceConfig`].
pub fn from_url(url: Url, verify_ssl: bool) -> Self {
let source = Arc::new(HttpSourceConfig {
id: SourceId::new("web-scraping"),
url,
headers: Default::default(),
files: Default::default(),
accept_invalid_certs: !verify_ssl,
});
let location = SourceLocation::new("");
HttpRemoteFile::new(source, location)
}
/// Adds bearer authorization to the request and returns the updated file.
pub fn bearer_auth(mut self, token: &str) -> Self {
self.headers
.0
.insert("Authorization".to_owned(), format!("Bearer {token}"));
self
}
/// Returns a [`RemoteFileUri`] for the file.
pub fn uri(&self) -> RemoteFileUri {
match self.url() {
Ok(url) => url.as_ref().into(),
Err(_) => "".into(),
}
}
/// Returns the URL from which to download this object file.
pub fn url(&self) -> anyhow::Result<Url> {
self.location.to_url(&self.source.url)
}
pub(crate) fn host(&self) -> String {
self.source.url.host_str().unwrap_or_default().to_string()
}
}