use http_cache_semantics::{RequestLike, ResponseLike};
use reqwest::StatusCode;
use spider_fingerprint::http;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HttpVersion {
Http09,
Http10,
Http11,
H2,
H3,
}
#[derive(Debug, Clone)]
pub struct HttpResponse {
pub body: Vec<u8>,
pub headers: std::collections::HashMap<String, String>,
pub status: u16,
pub url: url::Url,
pub version: HttpVersion,
}
#[derive(Debug, Default)]
pub struct HttpRequestLike {
pub uri: http::uri::Uri,
pub method: reqwest::Method,
pub headers: http::HeaderMap,
}
#[derive(Debug, Default)]
pub struct HttpResponseLike {
pub status: StatusCode,
pub headers: http::HeaderMap,
}
impl RequestLike for HttpRequestLike {
fn uri(&self) -> http::uri::Uri {
self.uri.clone()
}
fn is_same_uri(&self, other: &http::Uri) -> bool {
&self.uri == other
}
fn method(&self) -> &reqwest::Method {
&self.method
}
fn headers(&self) -> &http::HeaderMap {
&self.headers
}
}
impl ResponseLike for HttpResponseLike {
fn status(&self) -> StatusCode {
self.status
}
fn headers(&self) -> &http::HeaderMap {
&self.headers
}
}
pub fn convert_headers(
headers: &std::collections::HashMap<String, String>,
) -> reqwest::header::HeaderMap {
let mut header_map = reqwest::header::HeaderMap::new();
for (index, items) in headers.iter().enumerate() {
if let Ok(head) = reqwest::header::HeaderValue::from_str(items.1) {
use std::str::FromStr;
if let Ok(key) = reqwest::header::HeaderName::from_str(items.0) {
header_map.insert(key, head);
}
}
if index > 1000 {
break;
}
}
header_map
}