-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbuild.rs
More file actions
135 lines (115 loc) · 5.03 KB
/
Copy pathbuild.rs
File metadata and controls
135 lines (115 loc) · 5.03 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
fn main() {
println!("cargo:rerun-if-changed=build.rs");
#[cfg(feature = "adblock_easylist")]
easylist::fetch_lists();
}
#[cfg(feature = "adblock_easylist")]
mod easylist {
use std::io::{Read, Write};
use std::net::TcpStream;
use std::path::Path;
const LISTS: &[(&str, &str, &str)] = &[
("easylist.to", "/easylist/easylist.txt", "easylist.txt"),
(
"easylist.to",
"/easylist/easyprivacy.txt",
"easyprivacy.txt",
),
];
pub fn fetch_lists() {
let out_dir = std::env::var("OUT_DIR").unwrap();
for &(host, path, filename) in LISTS {
let dest = Path::new(&out_dir).join(filename);
// Cache: skip if already downloaded, non-trivial, and valid ABP content.
if dest.exists() {
if let Ok(content) = std::fs::read_to_string(&dest) {
if content.len() > 1024 && content.contains("[Adblock Plus") {
continue;
}
}
}
match fetch_https(host, path) {
Ok(body) => {
// Validate: must start with the ABP header and have enough rules.
if body.contains("[Adblock Plus") && body.lines().count() > 100 {
let _ = std::fs::write(&dest, &body);
println!("cargo:warning=Downloaded {filename} ({} bytes)", body.len());
} else {
if !dest.exists() {
let _ = std::fs::write(&dest, "");
}
println!("cargo:warning={filename}: response failed validation (corrupt or truncated), using fallback");
}
}
Err(e) => {
// Write empty fallback so include_str! compiles.
if !dest.exists() {
let _ = std::fs::write(&dest, "");
}
println!("cargo:warning=Failed to download {filename}: {e}");
}
}
}
}
fn fetch_https(host: &str, path: &str) -> Result<String, Box<dyn std::error::Error>> {
let connector = native_tls::TlsConnector::new()?;
let stream = TcpStream::connect((host, 443))?;
stream.set_read_timeout(Some(std::time::Duration::from_secs(30)))?;
let mut tls = connector.connect(host, stream)?;
let request =
format!("GET {path} HTTP/1.0\r\nHost: {host}\r\nAccept-Encoding: identity\r\n\r\n");
tls.write_all(request.as_bytes())?;
let mut buf = Vec::with_capacity(4 * 1024 * 1024);
tls.read_to_end(&mut buf)?;
let response = String::from_utf8_lossy(&buf);
// Follow a single 301/302 redirect if needed.
if response.starts_with("HTTP/1.1 301") || response.starts_with("HTTP/1.1 302") {
if let Some(loc) = extract_header(&response, "Location") {
if let Some((rhost, rpath)) = parse_https_url(loc) {
return fetch_https_direct(&rhost, &rpath);
}
}
}
// Strip HTTP headers to get body.
match response.find("\r\n\r\n") {
Some(idx) => Ok(response[idx + 4..].to_string()),
None => Err("malformed HTTP response".into()),
}
}
fn fetch_https_direct(host: &str, path: &str) -> Result<String, Box<dyn std::error::Error>> {
let connector = native_tls::TlsConnector::new()?;
let stream = TcpStream::connect((host, 443))?;
stream.set_read_timeout(Some(std::time::Duration::from_secs(30)))?;
let mut tls = connector.connect(host, stream)?;
let request =
format!("GET {path} HTTP/1.0\r\nHost: {host}\r\nAccept-Encoding: identity\r\n\r\n");
tls.write_all(request.as_bytes())?;
let mut buf = Vec::with_capacity(4 * 1024 * 1024);
tls.read_to_end(&mut buf)?;
let response = String::from_utf8_lossy(&buf);
match response.find("\r\n\r\n") {
Some(idx) => Ok(response[idx + 4..].to_string()),
None => Err("malformed HTTP response".into()),
}
}
fn extract_header<'a>(response: &'a str, name: &str) -> Option<&'a str> {
// Search headers case-insensitively, line by line.
let header_end = response.find("\r\n\r\n").unwrap_or(response.len());
let headers_section = &response[..header_end];
let prefix = format!("{}: ", name.to_ascii_lowercase());
for line in headers_section.split("\r\n") {
if line.to_ascii_lowercase().starts_with(&prefix) {
return Some(line[prefix.len()..].trim());
}
}
None
}
fn parse_https_url(url: &str) -> Option<(String, String)> {
let rest = url.strip_prefix("https://")?;
let (host, path) = match rest.find('/') {
Some(i) => (rest[..i].to_string(), rest[i..].to_string()),
None => (rest.to_string(), "/".to_string()),
};
Some((host, path))
}
}