rust - 如何访问 Actix-web 中 Future 中的 HttpRequest 数据?

标签 rust future lifetime rust-actix

我想要一个 Actix Web 处理程序,它通过将 POST 正文打印到控制台并构建包含来自请求对象的当前 URL 的 HTTP 响应来响应 POST 请求。

在读取请求的 POST 正文时,似乎需要涉及 future 。到目前为止我得到的最接近的是:

fn handler(req: HttpRequest) -> FutureResponse<HttpResponse> {
    req.body()
        .from_err()
        .and_then(|bytes: Bytes| {
            println!("Body: {:?}", bytes);
            let url = format!("{scheme}://{host}",
                scheme = req.connection_info().scheme(),
                host = req.connection_info().host());
            Ok(HttpResponse::Ok().body(url).into())
        }).responder()
}

这不会编译,因为 future 比处理程序还长,所以我尝试读取 req.connection_info() 是非法的。编译器错误建议我在闭包定义中使用 move 关键字,即 .and_then(move |bytes: Bytes| {。这也不会编译,因为 reqreq.body() 调用中移动,然后在移动后在构造 url 的引用中被捕获。

在访问 POST 正文的同时,构建一个范围的合理方法是什么?我可以在其中访问附加到请求对象的数据(例如 connection_info)?

最佳答案

最简单的解决方案是在未来完全不访问它:

extern crate actix_web; // 0.6.14
extern crate bytes;     // 0.4.8
extern crate futures;   // 0.1.21

use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse};
use bytes::Bytes;
use futures::future::Future;

fn handler(req: HttpRequest) -> FutureResponse<HttpResponse> {
    let url = format!(
        "{scheme}://{host}",
        scheme = req.connection_info().scheme(),
        host = req.connection_info().host(),
    );

    req.body()
        .from_err()
        .and_then(move |bytes: Bytes| {
            println!("Body: {:?}", bytes);
            Ok(HttpResponse::Ok().body(url).into())
        })
        .responder()
}

如果这不仅仅是出于演示目的的快速破解,通过连接字符串构建 URL 是一个糟糕的想法,因为它不能正确地转义值。您应该使用可以为您执行此操作的类型。

关于rust - 如何访问 Actix-web 中 Future 中的 HttpRequest 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225281/

相关文章:

vector - 如何从矢量创建 VecDeque?

java - 在调用 Future.get() 之前中断线程

scala - 在 Scala 中使用 Future 和 Promise 取消

file-io - 在 Rust 中以字符串形式获取文件内容的更简洁方法?

vector - 如何将集合中的多个元素传递给一个或多个元素可变的函数?

java - `Future<?>` 任务完成后检查字段是否安全?

c++ - 临时对象的生命周期 : iterator to temporary vector in nested function call

c# - 不同 ASP.NET MVC 组件中的对象作用域和生命周期是什么?

rust - 为什么不能在同一结构中存储值和对该值的引用?

rust - 值(value)的生命周期与借用的生命周期