2

现在是hs-tls.

在我可以使用必应翻译 api 之前,我必须获得一个访问令牌。这基本上相当于以下代码段,可以正常工作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 Bad Request HTTP 响应代码。

当我尝试在 Haskell 中复制它时,它立即失败(重新格式化):

*** 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-simpleclienttls-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)
4

1 回答 1

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)

解释是:

  1. 客户端发送握手包
  2. 远程端关闭连接
  3. 客户端引发断管异常
  4. 客户端发送警报数据包以通知远程端内部错误(即损坏的管道异常)
  5. 客户端引发 FailedConnectionException2

更新:您可以通过运行来完成同样的事情:

tls-simpleclient -d -v datamarket.accesscontrol.windows.net

可以在https://github.com/vincenthz/hs-tls中找到 tls-simpleclient

于 2014-12-06T17:41:45.447 回答