标签: hyper

如何下载带有超级和恢复错误的大文件?

我想用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下载整个文件后才会调用?

rust hyper

5
推荐指数
1
解决办法
1624
查看次数

如何从另一个线程使用 hyper::client?

我有多个线程执行一些繁重的操作,我需要在工作中使用客户端。我使用 Hyper v0.11 作为 HTTP 客户端,我想重用连接,所以我需要共享hyper::Client连接以保持打开连接(在keep-alive模式下)。

客户端不可在线程之间共享(它没有实现SyncSend)。这是我尝试执行的代码的小片段:

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)

multithreading rust hyper rust-tokio

5
推荐指数
1
解决办法
1935
查看次数

如何在转发流的同时从流中复制数据

我正在使用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)

rust hyper

5
推荐指数
1
解决办法
673
查看次数

actix-web 处理程序中的 HTTP 请求 -&gt; 一次多个执行程序:EnterError

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)

rust hyper rust-tokio actix-web

5
推荐指数
1
解决办法
2240
查看次数

如何更改我的超级终端(从 hyper.is 下载)的主题?

如果您对基本上只是一个终端的可下载软件“Hyper”有任何经验,那么您也许可以帮助我。我不能改变主题,它的外观。我使用的是最新版本的 MacOS。我尝试打开文件 hyper.js 但我的电脑找不到它。“~/Library/Application Support/Hyper/.hyper.js”标记错误。我该怎么做才能更改主题?

terminal hyper

5
推荐指数
2
解决办法
5844
查看次数

Rust 超级流媒体功能

我目前正在尝试通过 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 …

streaming rust hyper

5
推荐指数
0
解决办法
1540
查看次数

如何在 Rust Hyper 中将响应正文读取为字符串?

这个问题有几个答案(这里这里这里),但没有一个对我有用:(

到目前为止我尝试过的:


    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)

我不确定,在这里做什么?

asynchronous rust hyper

5
推荐指数
2
解决办法
8431
查看次数

使用serde_json解析对象内的对象

我被卡住了,下面是我收到的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 field assest"),line:0,column:0}',src/libcore/result.rs:860:4

json rust hyper serde serde-json

4
推荐指数
1
解决办法
428
查看次数

如何通过读取和转换文件来创建流?

我正在尝试读取文件、解密它并返回数据。因为文件可能非常大,所以我想在流中执行此操作。

我找不到一个好的模式来实现流。我正在尝试做这样的事情:

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)

rust hyper rust-tokio

4
推荐指数
1
解决办法
9958
查看次数

如何发送分块传输编码的超响应?

我正在尝试发送具有特定数量的字节和块的超级响应。我不知道如何生成通用分块响应或设置传输编码标头。似乎有一个用于 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)

http rust hyper

4
推荐指数
1
解决办法
1446
查看次数