計算機科学のブログ

実践Haskell HTTPリクエストの作成 SSLをサポートしない場合、setRequestSecure関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT7(実践Haskell)、LESSON39(HaskellでのHTTPリクエストの作成)、39.6(練習問題)Q39-1の解答を求めてみる。

package.yaml

name:                http-lesson
version:             0.1.0.0
github:              "githubuser/http-lesson"
license:             BSD3
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2022 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/http-lesson#readme>

dependencies:
- base >= 4.7 && < 5

library:
  source-dirs: src

executables:
  http-lesson-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - http-lesson
    - bytestring
    - http-conduit
    default-extensions: OverloadedStrings

tests:
  http-lesson-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - http-lesson

コード

Main.hs

module Main where

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as LC
import Network.HTTP.Simple
  ( Request,
    Response,
    defaultRequest,
    httpLBS,
    setRequestHeader,
    setRequestHost,
    setRequestMethod,
    setRequestPath,
    setRequestPort,
    setRequestSecure,
  )

myToken :: BC.ByteString
myToken = "<API トークン>"

response :: IO (Response LC.ByteString)
response = httpLBS "http://news.ycombinator.com"

buildRequestNOSSL :: BC.ByteString -> BC.ByteString -> BC.ByteString -> BC.ByteString -> Request
buildRequestNOSSL token host method path =
  setRequestMethod method $
    setRequestHost host $
      setRequestHeader "token" [myToken] $
        setRequestPath path $
          setRequestSecure False $
            setRequestPort 443 defaultRequest

main :: IO ()
main = do
  print $ buildRequestNOSSL "token" "host" "Get" "path"

入出力結果(Terminal, Zsh)

% stack build
http-lesson-0.1.0.0: unregistering (local file changes: app/Main.hs)
http-lesson> configure (lib + exe)
Configuring http-lesson-0.1.0.0...
http-lesson> build (lib + exe)
Preprocessing library for http-lesson-0.1.0.0..
Building library for http-lesson-0.1.0.0..
Preprocessing executable 'http-lesson-exe' for http-lesson-0.1.0.0..
Building executable 'http-lesson-exe' for http-lesson-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/http-lesson-exe/http-lesson-exe ...
http-lesson> copy/register
Installing library in /Users/…/http-lesson/.stack-work/install/x86_64-osx/e8aa255f05230c729a6925540310c5d25fa2ee7edb295ecd65b1d55a14d8280b/9.0.2/lib/x86_64-osx-ghc-9.0.2/http-lesson-0.1.0.0-7VoYrQcVxswJmRwg5jgroZ
Installing executable http-lesson-exe in /Users/…/http-lesson/.stack-work/install/x86_64-osx/e8aa255f05230c729a6925540310c5d25fa2ee7edb295ecd65b1d55a14d8280b/9.0.2/bin
Registering library for http-lesson-0.1.0.0..
% stack exec http-lesson-exe
Request {
  host                 = "host"
  port                 = 443
  secure               = False
  requestHeaders       = [("token","<API \200\252\175\243>")]
  path                 = "path"
  queryString          = ""
  method               = "Get"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = ResponseTimeoutDefault
  requestVersion       = HTTP/1.1
  proxySecureMode      = ProxySecureWithConnect
}

%