我想用hyper下载大文件(500mb),如果下载失败,我可以恢复.
对于收到的每个数据块,有没有办法使用hyper来运行某些功能?该send()方法返回一个Result<Response>,但我在Response上找不到任何返回块上迭代器的方法.理想情况下,我可以做以下事情:
client.get(&url.to_string())
.send()
.map(|mut res| {
let mut chunk = String::new();
// write this chunk to disk
});
Run Code Online (Sandbox Code Playgroud)
这是可能的,还是map只有在Hyper下载整个文件后才会调用?
我有多个线程执行一些繁重的操作,我需要在工作中使用客户端。我使用 Hyper v0.11 作为 HTTP 客户端,我想重用连接,所以我需要共享hyper::Client连接以保持打开连接(在keep-alive模式下)。
客户端不可在线程之间共享(它没有实现Sync或Send)。这是我尝试执行的代码的小片段:
let mut core = Core::new().expect("Create Client Event Loop");
let handle = core.handle();
let remote = core.remote();
let client = Client::new(&handle.clone());
thread::spawn(move || {
// intensive operations...
let response = &client.get("http://google.com".parse().unwrap()).and_then(|res| {
println!("Response: {}", res.status());
Ok(())
});
remote.clone().spawn(|_| {
response.map(|_| { () }).map_err(|_| { () })
});
// more intensive operations...
});
core.run(futures::future::empty::<(), ()>()).unwrap();
Run Code Online (Sandbox Code Playgroud)
此代码无法编译:
let mut core = Core::new().expect("Create Client Event Loop");
let handle = …Run Code Online (Sandbox Code Playgroud) 我正在使用hyper 0.12 来构建代理服务。当从上游服务器接收响应身体我要转发它返回给客户端的ASAP,并保存该内容在用于后续处理的缓冲器。
所以我需要一个函数:
Stream(a hyper::Body,准确地说)Stream与输入流功能相同的Future<Item = Vec<u8>, Error = ...>当输出流被完全消耗时,还返回某种通过输入流的缓冲内容解决的问题我一生都无法弄清楚如何做到这一点。
我想我正在寻找的功能看起来像这样:
type BufferFuture = Box<Future<Item = Vec<u8>, Error = ()>>;
pub fn copy_body(body: hyper::Body) -> (hyper::Body, BufferFuture) {
let body2 = ... // ???
let buffer = body.fold(Vec::<u8>::new(), |mut buf, chunk| {
buf.extend_from_slice(&chunk);
// ...somehow send this chunk to body2 also?
});
(body2, buffer);
}
Run Code Online (Sandbox Code Playgroud)
以下是我尝试过的,它一直工作到send_data()失败(显然)。
type BufferFuture = Box<Future<Item = Vec<u8>, Error …Run Code Online (Sandbox Code Playgroud) 在actix-web解析器中创建超级发布请求时,会引发以下错误 - 如何通过将请求生成到现有执行程序来发送 http 请求?
thread 'actix-rt:worker:1' panicked at 'Multiple executors at once: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Panic in Arbiter thread, shutting down system.
Run Code Online (Sandbox Code Playgroud)
主文件
extern crate actix_web;
extern crate serde_json;
extern crate actix_rt;
extern crate hyper;
use serde_json::{Value, json};
use hyper::{Client, Uri, Body, Request};
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use actix_rt::System;
use actix_web::client;
use …Run Code Online (Sandbox Code Playgroud) 如果您对基本上只是一个终端的可下载软件“Hyper”有任何经验,那么您也许可以帮助我。我不能改变主题,它的外观。我使用的是最新版本的 MacOS。我尝试打开文件 hyper.js 但我的电脑找不到它。“~/Library/Application Support/Hyper/.hyper.js”标记错误。我该怎么做才能更改主题?
我目前正在尝试通过 rust with hyper (0.13) 中的简单 HTTP get 请求来实现数据流。这个想法很简单。如果客户端发送请求,服务器每 5 秒以“块”形式响应,使连接始终保持打开状态。
我正在尝试从 futures 箱中实现特征 Stream
impl Stream for MonitorString {
type Item = Result<String, serde_json::Error>;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Result<String, serde_json::Error>>> {
thread::sleep(Duration::from_secs(5));
Poll::Ready(Some(self.data.to_string()))
}
}
Run Code Online (Sandbox Code Playgroud)
然后建立一个响应,例如
type StreamingServiceResult = Result<Response<Body>, Box<dyn std::error::Error + Sync + Send>>;
pub async fn handle_request(_: Request<Body>, params: Params, _: Query) -> StreamingServiceResult {
Ok(Response::builder()
.header(hyper::header::CONTENT_TYPE, "application/json")
.header(hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(hyper::header::TRANSFER_ENCODING, "chunked")
.body(Body::wrap_stream(MonitorString::new(...)))
}
Run Code Online (Sandbox Code Playgroud)
当我向服务器发送请求时,它会打开连接并挂起。通过一些调试,我看到调用了 poll_next 函数,但仅当流没有更多内容可产生时才发送响应(poll::Ready(None) 由 poll_next 返回)并整体发送。
我可以看到我对 Stream …
这个问题有几个答案(这里,这里,这里),但没有一个对我有用:(
到目前为止我尝试过的:
use hyper as http;
use futures::TryStreamExt;
fn test_heartbeat() {
let mut runtime = tokio::runtime::Runtime::new().expect("Could not get default runtime");
runtime.spawn(httpserve());
let addr = "http://localhost:3030".parse().unwrap();
let expected = json::to_string(&HeartBeat::default()).unwrap();
let client = http::Client::new();
let actual = runtime.block_on(client.get(addr));
assert!(actual.is_ok());
if let Ok(response) = actual {
let (_, body) = response.into_parts();
// what shall be done here?
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定,在这里做什么?
我被卡住了,下面是我收到的JSON:
{
"BCH": {
"aclass": "currency",
"altname": "BCH",
"decimals": 10,
"display_decimals": 5
}
}
Run Code Online (Sandbox Code Playgroud)
我对我的结构应该如何使用serde_jsoncrate 解析属性感到困惑.以下是我目前的情况:
#[derive(Deserialize, Debug)]
struct Assets {
aclass: String,
altname: String,
decimals: u8,
display_decimals: u8,
}
#[derive(Deserialize, Debug)]
struct Currency {
assest: Assets,
}
fn to_assets_type(value: serde_json::Value) -> Currency {
serde_json::from_value(value).unwrap()
}
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
线程'main'惊慌失措'调用
Result::unwrap()一个Err值:ErrorImpl {code:Message("missing fieldassest"),line:0,column:0}',src/libcore/result.rs:860:4
我正在尝试读取文件、解密它并返回数据。因为文件可能非常大,所以我想在流中执行此操作。
我找不到一个好的模式来实现流。我正在尝试做这样的事情:
let stream = stream::unfold(decrypted_init_length, |decrypted_length| async move {
if decrypted_length < start + length {
let mut encrypted_chunk = vec![0u8; encrypted_block_size];
match f.read(&mut encrypted_chunk[..]) {
Ok(size) => {
if size > 0 {
let decrypted = my_decrypt_fn(&encrypted_chunk[..]);
let updated_decrypted_length = decrypted_length + decrypted.len();
Some((decrypted, updated_decrypted_length))
} else {
None
}
}
Err(e) => {
println!("Error {}", e);
None
}
}
} else {
None
}
});
Run Code Online (Sandbox Code Playgroud)
问题是f.read上面的异步闭包中不允许这样做,并出现以下错误:
let stream = stream::unfold(decrypted_init_length, |decrypted_length| async move {
if …Run Code Online (Sandbox Code Playgroud) 我正在尝试发送具有特定数量的字节和块的超级响应。我不知道如何生成通用分块响应或设置传输编码标头。似乎有一个用于 hyper 的 httpWriter/chunkedWriter 现在已被折旧。
这是我的尝试,但是未设置传输编码标头,并且我不认为这是获得分块响应的正确方法。
let chunked_body = "5\r\nhello\r\n5\r\n worl\r\n1\r\nd\r\n0\r\n\r\n";
let mut resp: hyper::Response<Body> = Response::new(Body::from(chunked_body));
resp.headers_mut().insert(TRANSFER_ENCODING, HeaderValue::from_static("Chunked"));
Ok(resp)
Run Code Online (Sandbox Code Playgroud) hyper ×10
rust ×9
rust-tokio ×3
actix-web ×1
asynchronous ×1
http ×1
json ×1
serde ×1
serde-json ×1
streaming ×1
terminal ×1