hs-tls
中的fixed bug。在我能够点击翻译api之前,我必须obtain an access token。这基本上相当于以下代码段,它可以在curl
curl -Lp --data-urlencode client_id=$APPID \
--data-urlencode client_secret=$SECRET \
--data-urlencode scope='http://api.microsofttranslator.com' \
--data-urlencode grant_type="client_credentials" \
https://datamarket.accesscontrol.windows.net/v2/OAuth2-13
并立即响应一些包含令牌的JSON。请记住,即使是错误/格式错误的请求也会引发包含错误消息的响应,尽管通常也会有400个错误请求HTTP响应代码。
*** Exception: FailedConnectionException2 "datamarket.accesscontrol.windows.net"
443 True <socket: 11>: hPutBuf: resource vanished (Broken pipe)
此外,当我尝试向其他主机发送更简单的(https)请求时,一切都按预期工作。
如果你让它与另一个有用的图书馆合作,但不是这个问题的真正含义。
自包含示例如下所示。
{-# LANGUAGE OverloadedStrings #-}
module Test where
import Control.Monad
import Data.Maybe
import Network.HTTP.Client as N
import Network.HTTP.Client.TLS as N
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.ByteString.Char8 as BS
import Debug.Trace
badURL :: String
badURL = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
goodURL :: String
goodURL = "https://httpbin.org/post"
requestData :: [(BS.ByteString, BS.ByteString)]
requestData = [
("client_id", "APPID"),
("client_secret", "SECRETSECRET"),
("scope", "http://api.microsofttranslator.com"),
("grant_type", "client_credentials")]
mkReq :: String -> Request
mkReq url = urlEncodedBody requestData . fromJust $ parseUrl url
go :: String -> IO ()
go url = do
let addHeaders h r = r { requestHeaders = h ++ requestHeaders r }
headers = [("User-Agent", "curl/7.39.0")
,("Accept", "*/*")
]
req = addHeaders headers (mkReq url)
N.withManager N.tlsManagerSettings $ \man -> do
resp <- httpLbs req man
guard (trace "wefwef" True)
B.putStrLn $ responseBody resp
return ()
{-
- *Main> go goodURL
- wefwef
- {
- "args": {},
- "data": "",
- "files": {},
- "form": {
- "client_id": "APPID",
- "client_secret": "SECRETSECRET",
- "grant_type": "client_credentials",
- "scope": "http://api.microsofttranslator.com"
- },
- "headers": {
- "Accept": "*/*",
- "Accept-Encoding": "gzip",
- "Connect-Time": "1",
- "Connection": "close",
- "Content-Length": "119",
- "Content-Type": "application/x-www-form-urlencoded",
- "Host": "httpbin.org",
- "Total-Route-Time": "0",
- "User-Agent": "curl/7.39.0",
- "Via": "1.1 vegur",
- "X-Request-Id": "3ed2de84-a1a3-4560-b7e3-f2dabfe45727"
- },
- "json": null,
- "origin": "108.85.134.2",
- "url": "https://httpbin.org/post"
- }
- *Main> go badURL
- *** Exception: FailedConnectionException2 "datamarket.accesscontrol.windows.net" 443 True <socket: 11>: hPutBuf: resource vanished (Broken pipe)
-}
我也尝试了tls-simpleclient
和tls-retrievecertificate
,但也发现管道错误。
$ tls-simpleclient -d -v datamarket.accesscontrol.windows.net
sending query:
GET / HTTP/1.0
debug: >> Handshake [ClientHello TLS12 (ClientRandom "\235=wnV\156z\143M\168+n\165`\193\217\132G\144\204\187\178\SOHG\156\EM\195\168\251l\232+") (Session Nothing) [107,103,57,51,56,50,47,53,4,5,10] [0] [(0,"\NUL'\NUL\NUL$datamarket.accesscontrol.windows.net"),(65281,"\NUL")] Nothing]
debug: >> Alert [(AlertLevel_Fatal,InternalError)]
tls-simpleclient: send: resource vanished (Broken pipe)
$ .cabal-sandbox/bin/tls-retrievecertificate datamarket.accesscontrol.windows.net 443
connecting to datamarket.accesscontrol.windows.net on port 443 ...
tls-retrievecertificate: send: resource vanished (Broken pipe)
答案 0 :(得分:2)
似乎datamarket.accesscontrol.windows.net不喜欢Haskell的TLS实现。
您可以通过执行以下操作添加TLS数据包的日志记录:
在connection
包中,对Network/Connection.hs
添加import Network.TLS ((Logging(..))
修改tlsEstablish
,如下所示:
tlsEstablish :: Handle -> TLS.ClientParams -> IO TLS.Context
tlsEstablish handle tlsParams = do
rng <- RNG.makeSystem
ctx <- TLS.contextNew handle tlsParams rng
+ let logging = def { loggingPacketSent = (\s -> putStrLn $ "<-- sent packet " ++ s)
+ , loggingPacketRecv = (\s -> putStrLn $ "--> recv packet " ++ s)
+ }
+ TLS.contextHookSetLogging ctx logging
TLS.handshake ctx
return ctx
执行go goodURL
时,您会看到以下流量:
<-- sent packet Handshake [ClientHello TLS12 ...
--> recv packet Handshake [ServerHello TLS12 ...
...
但是当你执行go badURL
时,你会看到:
<-- sent packet Handshake [ClientHello TLS12
<-- sent packet Alert [(AlertLevel_Fatal,InternalError)]
*** Exception: FailedConnectionException2 "datamarket.accesscontrol.windows.net" 443 True <socket: 13>: hPutBuf: resource vanished (Broken pipe)
解释是:
更新:你可以通过运行来完成同样的事情:
tls-simpleclient -d -v datamarket.accesscontrol.windows.net
其中tls-simpleclient可以在https://github.com/vincenthz/hs-tls
中找到