简体   繁体   English

actix-web 中“扩展”的作用是什么?

[英]What is the role of a "Extension" in actix-web?

I'm trying to figure out how Extension s are created in the actix-web Rust library.我试图弄清楚如何在actix-web Rust 库中创建Extension I think I'm missing the concept of request extensions.我想我错过了请求扩展的概念。 What do request extensions do?请求扩展有什么作用? How are they different from HTTP headers?它们与 HTTP 接头有何不同?

Extensions are used for storing request-local data.扩展用于存储请求本地数据。 They are different than application data since it is specific to individual requests.它们与应用程序数据不同,因为它特定于单个请求。 And is different from headers since they are set within the server, not by the client.并且与标头不同,因为它们是在服务器中设置的,而不是由客户端设置的。

This is primarily used for passing data between middleware and the handlers;这主要用于在中间件和处理程序之间传递数据; like authentication or route-prefix processing.如身份验证或路由前缀处理。 They can be set in the middleware by req.extensions_mut().insert(...) and can be retrieved in the handler via req.extensions().get<...>() or via the ReqData extractor.它们可以通过req.extensions_mut().insert(...)在中间件中设置,并且可以通过req.extensions().get<...>()或通过ReqData提取器在处理程序中检索。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM