計算機科学のブログ

実践Haskell HTTPリクエストの作成 HTTP.Simpleモジュールを使用する getResponseHeaders関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT7(実践Haskell)、LESSON39(HaskellでのHTTPリクエストの作成)、39.2(HTTP.Simpleモジュールを使用する)、クイックチェック 39-2の解答を求めてみる。

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

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

main :: IO ()
main = do
  res <- response
  print $ getResponseHeaders res

入出力結果(Terminal, Zsh)

% stack ghci
Using main module: 1. Package `http-lesson' component http-lesson:exe:http-lesson-exe with main-is file: /Users/…/http-lesson/app/Main.hs
http-lesson> initial-build-steps (lib + exe)
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: http-lesson

* * * * * * * *

Warning: Multiple files use the same module name:
         * Paths_http_lesson found at the following paths
           * /Users/…/http-lesson/.stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/autogen/Paths_http_lesson.hs (http-lesson:lib)
           * /Users/…/http-lesson/.stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/http-lesson-exe/autogen/Paths_http_lesson.hs (http-lesson:exe:http-lesson-exe)
* * * * * * * *

GHCi, version 9.0.2: https://www.haskell.org/ghc/  :? for help
:macro 'doc' overwrites builtin command.  Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /Users/…/.ghc/ghci.conf
[1 of 3] Compiling Lib              ( /Users/…/http-lesson/src/Lib.hs, interpreted )
[2 of 3] Compiling Main             ( /Users/…/http-lesson/app/Main.hs, interpreted )
l[3 of 3] Compiling Paths_http_lesson ( /Users/…/http-lesson/.stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/autogen/Paths_http_lesson.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /private/var/folders/2y/nfx9b0d9267fxn7rhzybw2xh0000gn/T/haskell-stack-ghci/b501d9c1/ghci-script
*Main Lib Paths_http_lesson
λ> :load app/Main.hs
[1 of 1] Compiling Main             ( app/Main.hs, interpreted )
Ok, one module loaded.
(0.02 secs,)
*Main
λ> getRe
getRequestHeader       getResponseHeader      getResponseStatusCode
getRequestQueryString  getResponseHeaders
getResponseBody        getResponseStatus
λ> getResponseHeaders <$> response 
[("Server","nginx"),("Date","Tue, 29 Mar 2022 03:22:57 GMT"),("Content-Type","text/html; charset=utf-8"),("Transfer-Encoding","chunked"),("Connection","keep-alive"),("Vary","Accept-Encoding"),("Cache-Control","private; max-age=0"),("X-Frame-Options","DENY"),("X-Content-Type-Options","nosniff"),("X-XSS-Protection","1; mode=block"),("Referrer-Policy","origin"),("Strict-Transport-Security","max-age=31556900"),("Content-Security-Policy","default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/ https://cdnjs.cloudflare.com/; frame-src 'self' https://www.google.com/recaptcha/; style-src 'self' 'unsafe-inline'; img-src 'self' https://account.ycombinator.com"),("Content-Encoding","gzip")]
it ::
  [(http-types-0.12.3:Network.HTTP.Types.Header.HeaderName,
    BC.ByteString)]
(1.22 secs, 2,820,232 bytes)
*Main
λ> res <- response
res :: Response LC.ByteString
(0.28 secs, 870,424 bytes)
*Main
λ> getResponseHeaders res
[("Server","nginx"),("Date","Tue, 29 Mar 2022 03:23:09 GMT"),("Content-Type","text/html; charset=utf-8"),("Transfer-Encoding","chunked"),("Connection","keep-alive"),("Vary","Accept-Encoding"),("Cache-Control","private; max-age=0"),("X-Frame-Options","DENY"),("X-Content-Type-Options","nosniff"),("X-XSS-Protection","1; mode=block"),("Referrer-Policy","origin"),("Strict-Transport-Security","max-age=31556900"),("Content-Security-Policy","default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/ https://cdnjs.cloudflare.com/; frame-src 'self' https://www.google.com/recaptcha/; style-src 'self' 'unsafe-inline'; img-src 'self' https://account.ycombinator.com"),("Content-Encoding","gzip")]
it ::
  [(http-types-0.12.3:Network.HTTP.Types.Header.HeaderName,
    BC.ByteString)]
(0.05 secs, 1,222,496 bytes)
*Main
λ> main
[("Server","nginx"),("Date","Tue, 29 Mar 2022 03:23:25 GMT"),("Content-Type","text/html; charset=utf-8"),("Transfer-Encoding","chunked"),("Connection","keep-alive"),("Vary","Accept-Encoding"),("Cache-Control","private; max-age=0"),("X-Frame-Options","DENY"),("X-Content-Type-Options","nosniff"),("X-XSS-Protection","1; mode=block"),("Referrer-Policy","origin"),("Strict-Transport-Security","max-age=31556900"),("Content-Security-Policy","default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/ https://cdnjs.cloudflare.com/; frame-src 'self' https://www.google.com/recaptcha/; style-src 'self' 'unsafe-inline'; img-src 'self' https://account.ycombinator.com"),("Content-Encoding","gzip")]
it :: ()
(0.29 secs, 1,547,696 bytes)
*Main
λ> :quit
Leaving GHCi.
%