Rust actix web客户端将负载作为流处理
简单的http客户端,带有rust中的actix。从服务器获取响应,可以处理标头。但当我试图处理有效载荷时,我对生锈的理解失败了。 我的愿望是逐字节处理有效负载,以获得巨大的流和分块数据。简单的body()解决方案效果很好。但是如何从有效负载中获取字节。它似乎是impl Stream,所以fn size_hint()是可访问的,但解压器impl Decoder impl Stream不会从流中轮询下一个 梅因Rust actix web客户端将负载作为流处理,rust,client,response,payload,actix-web,Rust,Client,Response,Payload,Actix Web,简单的http客户端,带有rust中的actix。从服务器获取响应,可以处理标头。但当我试图处理有效载荷时,我对生锈的理解失败了。 我的愿望是逐字节处理有效负载,以获得巨大的流和分块数据。简单的body()解决方案效果很好。但是如何从有效负载中获取字节。它似乎是impl Stream,所以fn size_hint()是可访问的,但解压器impl Decoder impl Stream不会从流中轮询下一个 梅因 use actix::prelude::*; use actix_http::enco
use actix::prelude::*;
use actix_http::encoding::{Decoder};
use actix_web::{HttpMessage,client::ClientBuilder,dev::Payload,http::{Version, StatusCode},web::Bytes};
use futures::future::{TryFutureExt};
use futures_core::stream::Stream;
use std::pin::Pin;
use std::boxed::Box;
use std::task::{Context, Poll};
#[actix_web::main]
async fn main() {
const URL : [&str; 2] = ["..",".."];
let client = ClientBuilder::new()
.disable_timeout()
.disable_redirects()
.header("User-Agent", "actix-web/3.0")
.header("Accept-Encoding", "gzip, deflate, br, chunked")
.header("Connection", "keepalive")
.header("DNT", "1")
.max_http_version( Version::HTTP_2 )
.finish();
// Create request builder and send request
let response =
client.get(URL[0]).send().map_err(|err|{
println!("Error on send {:?}",err);
});
match response.await{
Ok( mut v) => {
println!("encoding: {:?}",v.encoding());
println!("content type: {:?}",v.content_type());
println!("mime type: {:?}",v.mime_type());
println!("chunked: {:?}",v.chunked());
match v.status(){
StatusCode::OK => {
/* works fine
match v.body().await {
Ok(m) => { println!("body {:?}", m); },
Err(r) => { println!("Error.body {:?}",r); }
}
*/
// but what about handling byte by byte?
let payload = v.take_payload();
match payload {
Payload::None => { println!(" no data "); },
Payload::H1(h1) => {println!(" data h1 {:?}", h1); },
Payload::H2(d2) => {println!(" data h2 "); },
Payload::Stream(s1) => {
println!(" data stream ");
println!(" size: {:?}", s1.size_hint());
// Decompress impl Decoder
// Decoder impl Stream
// but how to activate poll_next
// compiler can size_hint from Stream
// but poll_next not found??
}
};
},
StatusCode::NOT_FOUND => {
println!(" resource was not found");
},
_ => {
}
}
},
Err(err) => {
println!("Error {:?}",err);
}
};
}
开始时,尝试使用actix::prelude::*调出所有必要的定义。这不是解决方案。您会得到什么错误?编译器错误:错误[E0599]:在当前作用域中找不到struct
actix\u http::encoding::Decoder
的名为poll\u next
的方法而不是crate actix web http客户端我使用crate hyper http客户端,它支持分块读取,并使我能够更轻松地控制读取过程。由于没有什么缺点,我必须自己解压数据,使用板条箱异步压缩很容易,默认情况下支持brotli、gzip、bzip2。