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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
use std::fmt::Debug;
use std::io::{BufRead, BufReader, Read};
use std::time::{Duration, Instant};
use bytes::Bytes;
#[cfg(feature = "charset")]
use encoding_rs::{Encoding, UTF_8};
#[cfg(feature = "gzip")]
use flate2::read::MultiGzDecoder;
use http::{Method, Response as HttpResponse};
#[cfg(feature = "charset")]
use mime::Mime;
use crate::body::Body;
#[cfg(feature = "cookie")]
use crate::cookies;
use crate::errors::{new_io_error, Error, Result};
use crate::record::{HTTPRecord, LocalPeerRecord, RedirectRecord};
use crate::{Request, COLON_SPACE, CR_LF, SPACE};
/// A Response to a submitted `Request`.
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Response {
#[cfg_attr(feature = "serde", serde(with = "http_serde::version"))]
version: http::Version,
#[cfg_attr(feature = "serde", serde(with = "http_serde::uri"))]
uri: http::Uri,
#[cfg_attr(feature = "serde", serde(with = "http_serde::status_code"))]
status_code: http::StatusCode,
#[cfg_attr(feature = "serde", serde(with = "http_serde::header_map"))]
headers: http::HeaderMap<http::HeaderValue>,
#[cfg_attr(feature = "serde", serde(skip))]
extensions: http::Extensions,
body: Option<Body>,
}
impl PartialEq for Response {
fn eq(&self, other: &Self) -> bool {
self.version == other.version
&& self.status_code == other.status_code
&& self.headers == other.headers
&& self.body.eq(&self.body)
}
}
impl<T> From<HttpResponse<T>> for Response
where
T: Into<Body>,
{
fn from(value: HttpResponse<T>) -> Self {
let (parts, body) = value.into_parts();
let body = body.into();
Self {
version: parts.version,
uri: Default::default(),
status_code: parts.status,
headers: parts.headers,
extensions: parts.extensions,
body: if body.is_empty() { None } else { Some(body) },
}
}
}
impl Response {
pub(crate) fn to_raw(&self) -> Bytes {
let mut http_response = Vec::new();
http_response.extend(format!("{:?}", self.version).as_bytes());
http_response.extend(SPACE);
http_response.extend(format!("{}", self.status_code).as_bytes());
http_response.extend(CR_LF);
for (k, v) in self.headers.iter() {
http_response.extend(k.as_str().as_bytes());
http_response.extend(COLON_SPACE);
http_response.extend(v.as_bytes());
http_response.extend(CR_LF);
}
http_response.extend(CR_LF);
// 添加body
if let Some(b) = self.body() {
if !b.is_empty() {
http_response.extend(b.as_ref());
}
}
Bytes::from(http_response)
}
/// An HTTP response builder
///
/// This type can be used to construct an instance of `Response` through a
/// builder-like pattern.
pub fn builder() -> http::response::Builder {
http::response::Builder::new()
}
}
impl Response {
/// Retrieve the cookies contained in the response.
///
/// Note that invalid 'Set-Cookie' headers will be ignored.
///
/// # Optional
///
/// This requires the optional `cookie` feature to be enabled.
#[cfg(feature = "cookie")]
#[cfg_attr(docsrs, doc(cfg(feature = "cookie")))]
pub fn cookies(&self) -> impl Iterator<Item = cookies::Cookie> {
cookies::extract_response_cookies(&self.headers).filter_map(|x| x.ok())
}
/// 获取编码并且尝试解码
#[cfg(feature = "charset")]
pub fn text_with_charset(&self, default_encoding: &str) -> Result<String> {
let body = if let Some(b) = self.body() {
b
} else {
return Ok(String::new());
};
let content_type = self
.headers
.get(http::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.parse::<Mime>().ok());
let header_encoding = content_type
.as_ref()
.and_then(|mime| mime.get_param("charset").map(|charset| charset.as_str()))
.unwrap_or(default_encoding);
let mut decode_text = String::new();
for encoding_name in &[header_encoding, default_encoding] {
let encoding = Encoding::for_label(encoding_name.as_bytes()).unwrap_or(UTF_8);
let (text, _, is_errors) = encoding.decode(body);
if !is_errors {
decode_text = text.to_string();
break;
}
}
Ok(decode_text)
}
/// Get the response text.
///
/// This method decodes the response body with BOM sniffing
/// and with malformed sequences replaced with the REPLACEMENT CHARACTER.
/// Encoding is determined from the `charset` parameter of `Content-Type` header,
/// and defaults to `utf-8` if not presented.
///
/// # Note
///
/// If the `charset` feature is disabled the method will only attempt to decode the
/// response as UTF-8, regardless of the given `Content-Type`
///
/// # Example
///
/// ```rust
/// # extern crate slinger;
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let content = slinger::get("http://httpbin.org/range/26")?.text()?;
/// # Ok(())
/// # }
/// ```
pub fn text(&self) -> Result<String> {
#[cfg(feature = "charset")]
{
let default_encoding = "utf-8";
self.text_with_charset(default_encoding)
}
#[cfg(not(feature = "charset"))]
Ok(String::from_utf8_lossy(&self.body().clone().unwrap_or_default()).to_string())
}
/// Get the `StatusCode` of this `Response`.
///
/// # Examples
///
/// Checking for general status class:
///
/// ```rust
/// # #[cfg(feature = "json")]
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/get")?;
/// if resp.status().is_success() {
/// println!("success!");
/// } else if resp.status().is_server_error() {
/// println!("server error!");
/// } else {
/// println!("Something else happened. Status: {:?}", resp.status());
/// }
/// # Ok(())
/// # }
/// ```
///
/// Checking for specific status codes:
///
/// ```rust
/// use slinger::Client;
/// use slinger::http::StatusCode;
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new();
///
/// let resp = client.post("http://httpbin.org/post")
/// .body("possibly too large")
/// .send()?;
///
/// match resp.status_code() {
/// StatusCode::OK => println!("success!"),
/// StatusCode::PAYLOAD_TOO_LARGE => {
/// println!("Request payload is too large!");
/// }
/// s => println!("Received response status: {s:?}"),
/// };
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn status_code(&self) -> http::StatusCode {
self.status_code
}
/// Get the HTTP `Version` of this `Response`.
#[inline]
pub fn version(&self) -> http::Version {
self.version
}
/// Get the `Headers` of this `Response`.
///
/// # Example
///
/// Saving an etag when caching a file:
///
/// ```
/// use slinger::Client;
/// use slinger::http::header::ETAG;
///
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new();
///
/// let mut resp = client.get("http://httpbin.org/cache").send()?;
/// if resp.status_code().is_success() {
/// if let Some(etag) = resp.headers().get(ETAG) {
/// std::fs::write("etag", etag.as_bytes());
/// }
/// }
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn headers(&self) -> &http::HeaderMap {
&self.headers
}
/// Get a mutable reference to the `Headers` of this `Response`.
#[inline]
pub fn headers_mut(&mut self) -> &mut http::HeaderMap {
&mut self.headers
}
/// Get the content-length of the response, if it is known.
///
/// Reasons it may not be known:
///
/// - The server didn't send a `content-length` header.
/// - The response is gzipped and automatically decoded (thus changing
/// the actual decoded length).
pub fn content_length(&self) -> Option<u64> {
self
.headers
.get(http::header::CONTENT_LENGTH)
.and_then(|x| x.to_str().ok()?.parse().ok())
}
/// Get the final `http::Uri` of this `Response`.
///
/// # Example
///
/// ```rust
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/redirect/1")?;
/// assert_eq!(resp.uri().to_string().as_str(), "http://httpbin.org/get");
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn uri(&self) -> &http::Uri {
&self.uri
}
#[inline]
pub(crate) fn url_mut(&mut self) -> &mut http::Uri {
&mut self.uri
}
/// Get the full response body as `Bytes`.
///
/// # Example
///
/// ```
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/ip")?;
/// let body = resp.body();
/// println!("bytes: {body:?}");
/// # Ok(())
/// # }
/// ```
pub fn body(&self) -> &Option<Body> {
&self.body
}
/// private
pub fn body_mut(&mut self) -> &mut Option<Body> {
&mut self.body
}
/// Returns a reference to the associated extensions.
pub fn extensions(&self) -> &http::Extensions {
&self.extensions
}
/// Returns a mutable reference to the associated extensions.
pub fn extensions_mut(&mut self) -> &mut http::Extensions {
&mut self.extensions
}
}
/// 放一些响应中间过程记录,存起来方便获取
impl Response {
/// Get the remote address used to get this `Response`.
///
/// # Example
///
/// ```rust
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/redirect/1")?;
/// println!("httpbin.org address: {:?}", resp.local_peer_record());
/// # Ok(())
/// # }
/// ```
pub fn local_peer_record(&self) -> Option<&LocalPeerRecord> {
self.extensions().get::<LocalPeerRecord>()
}
/// Get the certificate to get this `Response`.
///
/// # Example
///
/// ```rust
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("https://httpbin.org/")?;
/// println!("httpbin.org certificate: {:?}", resp.certificate());
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "tls")]
pub fn certificate(&self) -> Option<&openssl::x509::X509> {
self.extensions().get::<openssl::x509::X509>()
}
/// Get the http record used to get this `Response`.
///
/// # Example
///
/// ```rust
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/redirect/1")?;
/// println!("httpbin.org http: {:?}", resp.http_record());
/// # Ok(())
/// # }
/// ```
pub fn http_record(&self) -> Option<&Vec<HTTPRecord>> {
self.extensions().get::<Vec<HTTPRecord>>()
}
/// Get the request used to get this `Response`.
///
/// # Example
///
/// ```rust
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/redirect/1")?;
/// println!("httpbin.org request: {:?}", resp.request());
/// # Ok(())
/// # }
/// ```
pub fn request(&self) -> Option<&Request> {
self.extensions().get::<Request>()
}
/// Get the redirect record used to get this `Response`.
///
/// # Example
///
/// ```rust
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = slinger::get("http://httpbin.org/redirect-to?url=http://www.example.com/")?;
/// println!("httpbin.org redirect: {:?}", resp.redirect_record());
/// # Ok(())
/// # }
/// ```
pub fn redirect_record(&self) -> Option<&RedirectRecord> {
self.extensions().get::<RedirectRecord>()
}
}
/// A builder to construct the properties of a `Response`.
///
/// To construct a `ResponseBuilder`, refer to the `Client` documentation.
#[derive(Debug)]
pub struct ResponseBuilder<T: Read> {
builder: http::response::Builder,
reader: BufReader<T>,
config: ResponseConfig,
}
/// response config
#[derive(Debug, Default)]
pub struct ResponseConfig {
method: Method,
timeout: Option<Duration>,
unsafe_response: bool,
max_read: Option<u64>,
}
impl ResponseConfig {
/// new a response config
pub fn new(request: &Request, timeout: Option<Duration>) -> Self {
let method = request.method().clone();
let unsafe_response = request.is_unsafe();
ResponseConfig {
method,
timeout,
unsafe_response,
max_read: None,
}
}
}
impl<T: Read> ResponseBuilder<T> {
/// Constructs a new response.
pub fn new(reader: BufReader<T>, config: ResponseConfig) -> ResponseBuilder<T> {
ResponseBuilder {
builder: Default::default(),
reader,
config,
}
}
fn parser_version(&mut self) -> Result<(http::Version, http::StatusCode)> {
let mut buffer = String::new();
self.reader.read_line(&mut buffer)?;
let mut version = http::Version::default();
let mut sc = http::StatusCode::default();
let (mut vf, mut sf) = (false, false);
for (index, vc) in buffer.splitn(3, |c| c == ' ').enumerate() {
if vc.is_empty() {
return Err(new_io_error(
std::io::ErrorKind::InvalidData,
"invalid http version and status_code data",
));
}
match index {
0 => {
version = match vc {
"HTTP/0.9" => http::Version::HTTP_09,
"HTTP/1.0" => http::Version::HTTP_10,
"HTTP/1.1" => http::Version::HTTP_11,
"HTTP/2.0" => http::Version::HTTP_2,
"HTTP/3.0" => http::Version::HTTP_3,
_ => {
return Err(new_io_error(
std::io::ErrorKind::InvalidData,
"invalid http version",
));
}
};
vf = true;
}
1 => {
sc = http::StatusCode::try_from(vc).map_err(|x| Error::Http(http::Error::from(x)))?;
sf = true;
}
_ => {}
}
}
if !(vf && sf) {
return Err(new_io_error(
std::io::ErrorKind::InvalidData,
"invalid http version and status_code data",
));
}
Ok((version, sc))
}
fn read_headers(&mut self) -> http::HeaderMap {
// 读取请求头
let mut headers = http::HeaderMap::new();
let mut header_line = Vec::new();
while let Ok(length) = self.reader.read_until(b'\n', &mut header_line) {
if length == 0 || header_line == b"\r\n" {
break;
}
if let Ok((Some(k), Some(v))) = parser_headers(&header_line) {
headers.insert(k, v);
};
header_line.clear();
}
headers
}
fn read_body(&mut self, header: &http::HeaderMap) -> Result<Vec<u8>> {
let mut body = Vec::new();
if matches!(self.config.method, Method::HEAD) {
return Ok(body);
}
let mut content_length: Option<u64> = header.get(http::header::CONTENT_LENGTH).and_then(|x| {
x.to_str()
.ok()?
.parse()
.ok()
.and_then(|l| if l == 0 { None } else { Some(l) })
});
if self.config.unsafe_response {
content_length = None;
}
if let Some(te) = header.get(http::header::TRANSFER_ENCODING) {
if te == "chunked" {
body = self.read_chunked_body()?;
}
} else if let Some(mut cl) = content_length {
// 如果有最大读取限制,取一个最小的长度
if let Some(max_read) = self.config.max_read {
cl = std::cmp::min(cl, max_read);
}
let mut buffer = vec![0; 12]; // 定义一个缓冲区
let mut total_bytes_read = 0;
let mut start = Instant::now();
let timeout = self.config.timeout;
loop {
match self.reader.read(&mut buffer) {
Ok(0) => break,
Ok(n) => {
body.extend_from_slice(&buffer[..n]);
total_bytes_read += n;
// 当有读取到数据的时候重置计时器
start = Instant::now();
}
Err(ref err) if err.kind() == std::io::ErrorKind::WouldBlock => {
// 如果没有数据可读,但超时尚未到达,可以在这里等待或重试
// 当已经有数据了或者触发超时就跳出循环,防止防火墙一直把会话挂着不释放
if total_bytes_read > 0 {
break;
} else if let Some(to) = timeout {
if start.elapsed() > to {
break;
}
}
std::thread::sleep(Duration::from_micros(100));
}
Err(_err) => break,
}
// 检查是否读取到了全部数据,如果是,则退出循环
if total_bytes_read >= cl as usize {
break;
}
}
}
#[cfg(feature = "gzip")]
if let Some(ce) = header.get(http::header::CONTENT_ENCODING) {
if ce == "gzip" {
let mut gzip_body = Vec::new();
let mut d = MultiGzDecoder::new(&body[..]);
d.read_to_end(&mut gzip_body)?;
body = gzip_body;
}
}
Ok(body)
}
fn read_chunked_body(&mut self) -> Result<Vec<u8>> {
let mut body: Vec<u8> = Vec::new();
loop {
let mut chunk: String = String::new();
loop {
let mut one_byte = vec![0; 1];
self.reader.read_exact(&mut one_byte)?;
if one_byte[0] != 10 && one_byte[0] != 13 {
chunk.push(one_byte[0] as char);
break;
}
}
loop {
let mut one_byte = vec![0; 1];
self.reader.read_exact(&mut one_byte)?;
if one_byte[0] == 10 || one_byte[0] == 13 {
self.reader.read_exact(&mut one_byte)?;
break;
} else {
chunk.push(one_byte[0] as char)
}
}
if chunk == "0" || chunk.is_empty() {
break;
}
let chunk = usize::from_str_radix(&chunk, 16)?;
let mut chunk_of_bytes = vec![0; chunk];
self.reader.read_exact(&mut chunk_of_bytes)?;
body.append(&mut chunk_of_bytes);
}
Ok(body)
}
/// Build a `Response`, which can be inspected, modified and executed with
/// `Client::execute()`.
pub fn build(mut self) -> Result<Response> {
let (v, c) = self.parser_version()?;
self.builder = self.builder.version(v).status(c);
let header = self.read_headers();
// 读取body
let body = self.read_body(&header)?;
if let Some(h) = self.builder.headers_mut() {
*h = header;
}
let resp = self.builder.body(body)?;
Ok(resp.into())
}
}
pub(crate) fn parser_headers(
buffer: &[u8],
) -> Result<(Option<http::HeaderName>, Option<http::HeaderValue>)> {
let mut k = None;
let mut v = None;
let buffer = buffer.strip_suffix(CR_LF).unwrap_or(buffer);
for (index, h) in buffer.splitn(2, |s| s == &58).enumerate() {
let h = h.strip_prefix(SPACE).unwrap_or(h);
match index {
0 => match http::HeaderName::from_bytes(h) {
Ok(hk) => k = Some(hk),
Err(err) => {
return Err(Error::Http(http::Error::from(err)));
}
},
1 => match http::HeaderValue::from_bytes(h) {
Ok(hv) => v = Some(hv),
Err(err) => {
return Err(Error::Http(http::Error::from(err)));
}
},
_ => {}
}
}
Ok((k, v))
}