mirror of https://github.com/breard-r/acmed.git
Rodolphe Bréard
4 weeks ago
Failed to extract signature
5 changed files with 1696 additions and 148 deletions
-
1661Cargo.lock
-
6Cargo.toml
-
76build.rs
-
75src/http.rs
-
26src/main.rs
1661
Cargo.lock
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,76 @@ |
|||
extern crate serde;
|
|||
extern crate toml;
|
|||
|
|||
use serde::Deserialize;
|
|||
use std::env;
|
|||
use std::fs::File;
|
|||
use std::io::prelude::*;
|
|||
use std::path::PathBuf;
|
|||
|
|||
macro_rules! set_rustc_env_var {
|
|||
($name: expr, $value: expr) => {{
|
|||
println!("cargo:rustc-env={}={}", $name, $value);
|
|||
}};
|
|||
}
|
|||
|
|||
#[derive(Deserialize)]
|
|||
pub struct LockFile {
|
|||
package: Vec<Package>,
|
|||
}
|
|||
|
|||
#[derive(Deserialize)]
|
|||
struct Package {
|
|||
name: String,
|
|||
version: String,
|
|||
}
|
|||
|
|||
struct Error;
|
|||
|
|||
impl From<std::io::Error> for Error {
|
|||
fn from(_error: std::io::Error) -> Self {
|
|||
Error {}
|
|||
}
|
|||
}
|
|||
|
|||
impl From<toml::de::Error> for Error {
|
|||
fn from(_error: toml::de::Error) -> Self {
|
|||
Error {}
|
|||
}
|
|||
}
|
|||
|
|||
fn get_lock_file() -> Result<LockFile, Error> {
|
|||
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|||
path.push("Cargo.lock");
|
|||
let mut file = File::open(path)?;
|
|||
let mut contents = String::new();
|
|||
file.read_to_string(&mut contents)?;
|
|||
let ret: LockFile = toml::from_str(&contents)?;
|
|||
Ok(ret)
|
|||
}
|
|||
|
|||
fn set_http_agent() {
|
|||
let lock = match get_lock_file() {
|
|||
Ok(l) => l,
|
|||
Err(_) => {
|
|||
return;
|
|||
}
|
|||
};
|
|||
for p in lock.package.iter() {
|
|||
if p.name == "reqwest" {
|
|||
let agent = format!("{}/{}", p.name, p.version);
|
|||
set_rustc_env_var!("ACMED_HTTP_LIB_AGENT", agent);
|
|||
return;
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
fn set_target() {
|
|||
if let Ok(target) = env::var("TARGET") {
|
|||
set_rustc_env_var!("ACMED_TARGET", target);
|
|||
};
|
|||
}
|
|||
|
|||
fn main() {
|
|||
set_target();
|
|||
set_http_agent();
|
|||
}
|
@ -0,0 +1,75 @@ |
|||
use crate::config::AcmedConfig;
|
|||
use anyhow::{Context, Result};
|
|||
use reqwest::header::{HeaderMap, HeaderValue};
|
|||
use reqwest::{header, Client, ClientBuilder, Request, Response};
|
|||
use tokio::sync::{mpsc, oneshot};
|
|||
|
|||
#[derive(Debug)]
|
|||
struct RawRequest {
|
|||
request: Request,
|
|||
tx: oneshot::Sender<Result<Response, reqwest::Error>>,
|
|||
}
|
|||
|
|||
#[derive(Clone, Debug)]
|
|||
pub struct HttpClient {
|
|||
tx: mpsc::UnboundedSender<RawRequest>,
|
|||
}
|
|||
|
|||
impl HttpClient {
|
|||
pub async fn send(&self, request: Request) -> Result<Response> {
|
|||
let (tx, rx) = oneshot::channel();
|
|||
let raw_req = RawRequest { request, tx };
|
|||
self.tx.send(raw_req)?;
|
|||
let ret = rx.await?;
|
|||
if let Err(ref e) = ret {
|
|||
tracing::error!("http error: {e:#?}");
|
|||
}
|
|||
Ok(ret?)
|
|||
}
|
|||
}
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(super) struct HttpRoutine {
|
|||
tx: mpsc::UnboundedSender<RawRequest>,
|
|||
rx: mpsc::UnboundedReceiver<RawRequest>,
|
|||
}
|
|||
|
|||
impl HttpRoutine {
|
|||
pub(super) fn new(config: &AcmedConfig) -> Self {
|
|||
let (tx, mut rx) = mpsc::unbounded_channel();
|
|||
Self { tx, rx }
|
|||
}
|
|||
|
|||
pub(super) fn get_client(&self) -> HttpClient {
|
|||
HttpClient {
|
|||
tx: self.tx.clone(),
|
|||
}
|
|||
}
|
|||
|
|||
pub(super) async fn run(mut self) {
|
|||
tracing::trace!("starting the http routine");
|
|||
let client = self.get_http_client();
|
|||
while let Some(raw_req) = self.rx.recv().await {
|
|||
tracing::debug!("new http request: {:?}", raw_req.request);
|
|||
let ret = client.execute(raw_req.request).await;
|
|||
let _ = raw_req.tx.send(ret);
|
|||
}
|
|||
tracing::warn!("the http routine has stopped");
|
|||
}
|
|||
|
|||
fn get_http_client(&self) -> Client {
|
|||
let useragent = format!(
|
|||
"{}/{} ({}) {}",
|
|||
env!("CARGO_BIN_NAME"),
|
|||
env!("CARGO_PKG_VERSION"),
|
|||
env!("ACMED_TARGET"),
|
|||
env!("ACMED_HTTP_LIB_AGENT")
|
|||
);
|
|||
let mut client_builder = ClientBuilder::new();
|
|||
let mut default_headers = HeaderMap::new();
|
|||
default_headers.append(header::ACCEPT_LANGUAGE, "en-US,en;q=0.5".parse().unwrap());
|
|||
default_headers.append(header::USER_AGENT, useragent.parse().unwrap());
|
|||
client_builder = client_builder.default_headers(default_headers);
|
|||
client_builder.build().unwrap()
|
|||
}
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue