Skip to main content

crawlkit_core/
response.rs

1//! HTTP 响应封装
2
3use std::collections::HashMap;
4
5/// 统一的 HTTP 响应结构
6///
7/// 无论底层使用 reqwest、wreq 还是 Chrome,都统一转换为该类型。
8#[derive(Debug, Clone)]
9pub struct Response {
10    /// 最终请求的 URL(可能经过重定向)
11    pub url: String,
12    /// HTTP 状态码
13    pub status: u16,
14    /// 响应头
15    pub headers: HashMap<String, String>,
16    /// 响应体(文本)
17    pub body: String,
18}
19
20impl Response {
21    /// 状态码是否为 2xx
22    pub fn is_success(&self) -> bool {
23        (200..300).contains(&self.status)
24    }
25
26    /// 获取 Content-Type
27    pub fn content_type(&self) -> Option<&str> {
28        self.headers.get("content-type").map(String::as_str)
29    }
30
31    /// 是否为 HTML 内容
32    pub fn is_html(&self) -> bool {
33        self.content_type()
34            .is_some_and(|ct| ct.to_ascii_lowercase().contains("text/html"))
35    }
36}