从Network.HTTP.Proxy.Proxy创建Network.Wreq.Proxy(Make Network.Wreq.Proxy from Network.HTTP.Proxy.Proxy)

编程入门 行业动态 更新时间:2024-10-16 22:23:16
从Network.HTTP.Proxy.Proxy创建Network.Wreq.Proxy(Make Network.Wreq.Proxy from Network.HTTP.Proxy.Proxy)

Network.HTTP.Proxy有一个很好的函数叫做fetchProxy :

fetchProxy :: Bool -> IO Proxy

fetchProxy flg获取本地代理设置并将字符串解析为代理值。 如果要通知格式错误的代理配置字符串,请为flg提供True。 代理设置来自HTTP_PROXY环境变量[...]

我想通过Wreq库使用以这种方式获得的Proxy ,通过从HTTP导入Wreq库,它有自己的Proxy 定义,如下所示 :

import Network.HTTP.Client.Internal (Proxy(..), Response)

Network.HTTP.Proxy.Proxy和Network.Wreq.Proxy之间似乎存在类型不匹配,我认为它们必须完全相同。

我这样导入:

import Network.Wreq import Network.HTTP.Proxy (fetchProxy)

如何在Wreq中使用HTTP.Proxy.Proxy?为什么GHC将它们视为不同的类型?

Network.HTTP.Proxy has a nice function called fetchProxy:

fetchProxy :: Bool -> IO Proxy

fetchProxy flg gets the local proxy settings and parse the string into a Proxy value. If you want to be informed of ill-formed proxy configuration strings, supply True for flg. Proxy settings are sourced from the HTTP_PROXY environment variable [...]

I want to use the Proxy obtained this way with Wreq library, which has it's own Proxy defined like this, by importing it from HTTP:

import Network.HTTP.Client.Internal (Proxy(..), Response)

There appears to be a type mismatch between Network.HTTP.Proxy.Proxy and Network.Wreq.Proxy, where I presume they must be identical.

I import both like this:

import Network.Wreq import Network.HTTP.Proxy (fetchProxy)

How can I use HTTP.Proxy.Proxy with Wreq and why does GHC see them as different types?

最满意答案

Wreq的作者可能只是不知道其他Proxy因为他们似乎存储了相同的信息。 然而,让他们互相交谈会很棘手,因为fetchProxy将host:port存储为字符串,而Wreq的Proxy需要单独的主机和端口。 你必须做一些URI解析:

import Control.Lens import Data.Text.Strict.Lens import Network.HTTP.Proxy import Network.Wreq import URI.ByteString main :: IO () main = do Network.HTTP.Proxy.Proxy host _ <- fetchProxy True case parseURI strictURIParserOptions (host ^. packed . re utf8) of Left e -> do putStrLn "uh oh" print e Right uri -> case ( uri ^? uriAuthorityL . _Just . authorityHostL . hostBSL , uri ^? uriAuthorityL . _Just . authorityPortL . _Just . portNumberL) of (Just host_, Just port_) -> do let opts = defaults & proxy ?~ httpProxy host_ port_ response <- getWith opts "http://example.com" print response _ -> putStrLn "uh oh"

我在这里使用lens做无聊的打包/解包字符串,编码UTF8,并与uri-bytestring包交谈以获得URI解析。 但一般的想法是,Haskell中的数据类型可以通过构造函数上的模式匹配进行切片和切割; 一旦解压缩, host:string就会汇入httpProxy调用,返回httpProxy的Proxy类型。 通过限定构造函数的名称( Newtork.HTTP.Proxy.Proxy ),我让编译器知道我想要该名称的模块。

从本地手动解析环境变量中的代理信息也不会太困难,并且启动代码可能更少。 您甚至可以为主机和端口提供单独的环境变量,这样就不需要进行URI解析。 URI具有如此低的熵,因此它们是用于存储配置信息的可怕格式。

It's likely that the Wreq authors were just unaware of the other Proxy as they seem to be storing equivalent information. It'll be tricky to get them to talk to each other, however, since fetchProxy stores the host:port as a string and Wreq's Proxy wants the individual host and port. You'll have to do some URI parsing:

import Control.Lens import Data.Text.Strict.Lens import Network.HTTP.Proxy import Network.Wreq import URI.ByteString main :: IO () main = do Network.HTTP.Proxy.Proxy host _ <- fetchProxy True case parseURI strictURIParserOptions (host ^. packed . re utf8) of Left e -> do putStrLn "uh oh" print e Right uri -> case ( uri ^? uriAuthorityL . _Just . authorityHostL . hostBSL , uri ^? uriAuthorityL . _Just . authorityPortL . _Just . portNumberL) of (Just host_, Just port_) -> do let opts = defaults & proxy ?~ httpProxy host_ port_ response <- getWith opts "http://example.com" print response _ -> putStrLn "uh oh"

I'm using lens here to do the boring bits and pieces of packing/unpacking strings, encoding UTF8, and talking to the uri-bytestring package to get URI parsing. But the general idea is that datatypes in Haskell can be sliced and diced simply by pattern matching on the constructor; once extracted, the host:string here is funneled down into the httpProxy call, which returns Wreq's Proxy type. By qualifying the name of the constructor (Newtork.HTTP.Proxy.Proxy) I've let the compiler know which module I want that name from.

It would also not be too difficult, and probably less code to boot, to manually parse proxy information from the environment variables yourself. You could even have a separate environment variable for host and port, which would obviate the need for URI parsing. URIs are have such massively low entropy that they're an awful format for storing configuration information.

更多推荐

本文发布于:2023-07-14 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HTTP   Network   Wreq   Proxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!