on master help sign in
.azure/azure-linux-template.ymlnew file+93
since splitsnapshots
unifiedsplit
@@ -0,0 +1,93 @@
1+jobs:
2+- job: ${{ parameters.name }}
3+ timeoutInMinutes: 180
4+ pool:
5+ vmImage: ${{ parameters.vmImage }}
6+ strategy:
7+ matrix:
8+ stack-lts-11:
9+ BUILD: stack
10+ STACK_YAML: stack-lts-11.yaml
11+ ARGS: "--resolver lts-11"
12+ stack-lts-12:
13+ BUILD: stack
14+ STACK_YAML: stack-lts-12.yaml
15+ ARGS: "--resolver lts-12"
16+ stack-lts-14:
17+ BUILD: stack
18+ ARGS: "--resolver lts-14"
19+ stack-nightly:
20+ BUILD: stack
21+ STACK_YAML: stack-nightly.yaml
22+ ARGS: "--resolver nightly"
23+ cabal-8.2.2:
24+ BUILD: cabal
25+ GHCVER: 8.4.4
26+ CABALVER: 2.0
27+ cabal-8.4.4:
28+ BUILD: cabal
29+ GHCVER: 8.4.4
30+ CABALVER: 2.2
31+ cabal-8.6.5:
32+ BUILD: cabal
33+ GHCVER: 8.6.5
34+ CABALVER: 2.4
35+ maxParallel: 6
36+ steps:
37+ - script: |
38+ export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
39+ mkdir -p ~/.local/bin
40+ curl -L https://get.haskellstack.org/stable/linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
41+ case "$BUILD" in
42+ cabal)
43+ sudo add-apt-repository -y ppa:hvr/ghc
44+ sudo apt-get update
45+ sudo apt-get install cabal-install-$CABALVER ghc-$GHCVER
46+ # See note here: https://github.com/haskell-CI/haskell-ci#alex--happy-with-ghc--78
47+ if [ "$GHCVER" = "head" ] || [ "${GHCVER%.*}" = "7.8" ] || [ "${GHCVER%.*}" = "7.10" ]; then
48+ sudo apt-get install happy-1.19.4 alex-3.1.3
49+ export PATH=/opt/alex/3.1.3/bin:/opt/happy/1.19.4/bin:$PATH
50+ else
51+ sudo apt-get install happy alex
52+ fi
53+ export PATH=$HOME/.local/bin:/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
54+ cabal --version
55+ cabal update
56+ PACKAGES=$(stack --install-ghc query locals | grep '^ *path' | sed 's@^ *path:@@')
57+ cabal install --only-dependencies --enable-tests --enable-benchmarks --force-reinstalls --ghc-options=-O0 --reorder-goals --max-backjumps=-1 $CABALARGS $PACKAGES
58+ ;;
59+ *)
60+ export PATH=$HOME/.local/bin:$PATH
61+ stack --install-ghc $ARGS test --bench --only-dependencies
62+ ;;
63+ esac
64+ set -ex
65+ case "$BUILD" in
66+ cabal)
67+ cabal install --enable-tests --enable-benchmarks --force-reinstalls --ghc-options=-O0 --reorder-goals --max-backjumps=-1 $CABALARGS $PACKAGES
68+
69+ ORIGDIR=$(pwd)
70+ for dir in $PACKAGES
71+ do
72+ cd $dir
73+ cabal check || [ "$CABALVER" == "1.22" ] # https://github.com/yesodweb/wai/pull/754#issuecomment-504866279
74+ cabal sdist
75+ PKGVER=$(cabal info . | awk '{print $2;exit}')
76+ SRC_TGZ=$PKGVER.tar.gz
77+ cd dist
78+ tar zxfv "$SRC_TGZ"
79+ cd "$PKGVER"
80+ cabal configure --enable-tests --ghc-options -O0
81+ cabal build
82+ cabal test --show-details=streaming
83+ cd $ORIGDIR
84+ done
85+ ;;
86+ *)
87+ stack $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps
88+ ;;
89+ esac
90+ set +ex
91+ env:
92+ OS_NAME: ${{ parameters.os }}
93+ displayName: 'Installation ${{parameters.os}} & Test'
.azure/azure-osx-template.ymlnew file+30
@@ -0,0 +1,30 @@
1+jobs:
2+- job: ${{ parameters.name }}
3+ timeoutInMinutes: 180
4+ pool:
5+ vmImage: ${{ parameters.vmImage }}
6+ strategy:
7+ matrix:
8+ stack-lts-11:
9+ BUILD: stack
10+ STACK_YAML: stack-lts-11.yaml
11+ ARGS: "--resolver lts-11"
12+ stack-lts-12:
13+ BUILD: stack
14+ STACK_YAML: stack-lts-12.yaml
15+ ARGS: "--resolver lts-12"
16+ stack-lts-14:
17+ BUILD: stack
18+ ARGS: "--resolver lts-14"
19+ maxParallel: 3
20+ steps:
21+ - script: |
22+ export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
23+ mkdir -p ~/.local/bin
24+ curl -skL https://get.haskellstack.org/stable/osx-x86_64.tar.gz | tar xz --strip-components=1 --include '*/stack' -C ~/.local/bin;
25+ export PATH=$HOME/.local/bin:$PATH
26+ stack --install-ghc $ARGS test --bench --only-dependencies
27+ stack $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps
28+ env:
29+ OS_NAME: ${{ parameters.os }}
30+ displayName: 'Installation ${{parameters.os}} & Test'
.azure/azure-pipelines.ymlnew file+30
@@ -0,0 +1,30 @@
1+# This is the complex Azure configuration, which is intended for use
2+# on open source libraries which need compatibility across multiple GHC
3+# versions, must work with cabal-install, and should be
4+# cross-platform. For more information and other options, see:
5+#
6+# https://docs.haskellstack.org/en/stable/azure_ci/
7+#
8+# Copy these contents into the root directory of your Github project in a file
9+# named azure-pipelines.yml
10+#
11+# For better organization, you split various jobs into seprate parts
12+# and each of them are controlled via individual file.
13+jobs:
14+- template: azure-linux-template.yml
15+ parameters:
16+ name: Linux
17+ vmImage: ubuntu-16.04
18+ os: linux
19+
20+- template: azure-osx-template.yml
21+ parameters:
22+ name: macOS
23+ vmImage: macOS-10.13
24+ os: osx
25+
26+- template: azure-windows-template.yml
27+ parameters:
28+ name: Windows
29+ vmImage: vs2017-win2016
30+ os: windows
.azure/azure-windows-template.ymlnew file+23
@@ -0,0 +1,23 @@
1+jobs:
2+- job: ${{ parameters.name }}
3+ timeoutInMinutes: 180
4+ pool:
5+ vmImage: ${{ parameters.vmImage }}
6+ strategy:
7+ matrix:
8+ stack-lts-14:
9+ BUILD: stack
10+ ARGS: "--resolver lts-14"
11+ maxParallel: 3
12+ steps:
13+ - bash: |
14+ export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
15+ curl -sSkL http://www.stackage.org/stack/windows-x86_64 -o /usr/bin/stack.zip
16+ unzip -o /usr/bin/stack.zip -d /usr/bin/
17+ stack setup $ARGS > /dev/null
18+ stack --install-ghc $ARGS test --bench --only-dependencies
19+ stack $ARGS test --bench --no-run-benchmarks --no-run-tests
20+ stack $ARGS test --bench --no-run-benchmarks --jobs 1 # non-parallel test execution
21+ env:
22+ OS_NAME: ${{ parameters.os }}
23+ displayName: 'Installation ${{parameters.os}} & Test'
.github/ISSUE_TEMPLATE.mdnew file+28
@@ -0,0 +1,28 @@
1+<!---
2+
3+### Bug Reports
4+
5+Reporting a bug? If relevant, we recommend including:
6+
7+* Your OS name and version
8+* The versions of tools you're using (e.g. `stack`, `ghc`).
9+* The versions of dependencies you're using
10+
11+For your convenience, we recommend pasting this script into bash and uploading the output [as a gist](https://gist.github.com/).
12+
13+```
14+command -v sw_vers && sw_vers # OS X only
15+command -v uname && uname -a # Kernel version
16+command -v stack && stack --version
17+command -v stack && stack ghc -- --version
18+command -v stack && stack list-dependencies
19+```
20+
21+* Also, is there anything custom or unusual about your setup? i.e. new or prerelease versions of GHC, Stack, etc.
22+
23+* Finally, if possible, please reproduce the error in a small script, or if necessary create a new Github repo with the smallest possible reproducing case. [Stack's scripting support](https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter) might be useful for creating your reproduction example.
24+
25+### Support
26+
27+Please direct support questions to [Stack Overflow](https://stackoverflow.com/questions/tagged/yesod+haskell) or the [Yesod Google Group](https://groups.google.com/forum/#!forum/yesodweb). If you don't get a response there, or you suspect there may be a bug in Wai causing your problem, you're welcome to ask here.
28+-->
.github/PULL_REQUEST_TEMPLATE.mdnew file+14
@@ -0,0 +1,14 @@
1+Before submitting your PR, check that you've:
2+
3+- [ ] Bumped the version number
4+- [ ] Documented new APIs with [Haddock markup](https://www.haskell.org/haddock/doc/html/index.html)
5+- [ ] Added [`@since` declarations](http://haskell-haddock.readthedocs.io/en/latest/markup.html#since) to the Haddock
6+
7+After submitting your PR:
8+
9+- [ ] Update the Changelog.md file with a link to your PR
10+- [ ] Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)
11+
12+<!---Thanks so much for contributing! :)
13+
14+_If these checkboxes don't apply to your PR, you can delete them_-->
.gitignore+3
@@ -7,3 +7,6 @@ tarballs/
7/.shelly
7/.shelly
8.cabal-sandbox/
8.cabal-sandbox/
9cabal.sandbox.config
9cabal.sandbox.config
10+.stack-work/
11+*~
12+stack*.yaml.lock
.travis.ymldeleted−10
@@ -1,10 +0,0 @@
1−language: haskell
2−
3−install:
4− - sudo apt-get install libfcgi-dev
5− - cabal unpack wai-logger
6− - mv wai-logger-* wai-logger
7− - cabal install hspec doctest HTTP cabal-meta cabal-src
8− - cabal-meta install --force-reinstalls
9−
10−script: mega-sdist --test
CONTRIBUTING.mdnew file+94
@@ -0,0 +1,94 @@
1+# Contributing
2+
3+Thanks for your interest in contributing to Wai! This file has some tips for developing Wai and getting a pull request accepted.
4+
5+## Development
6+
7+Wai is a mega-repo that contains many Haskell packages, each in a different directory. All the subprojects can be developed with Stack, using `stack <command> <subproject>`, e.g.
8+
9+* `stack build mime-types`
10+* `stack test auto-update`
11+* `stack haddock warp`
12+
13+If you'd like to test your changes in a full-fledged Wai app, you can use Stack to build against it, e.g.:
14+
15+```
16+packages:
17+- '/path/to/this/repo/wai'
18+```
19+
20+## Coding Guidelines
21+
22+### Safety
23+
24+Avoid partial functions. Even if you know the partial function is safe in your instance, partial functions require more reasoning from the programmer and are not resilient to refactoring. For the rare cases where a partial function is appropriate, a custom `error` should be used.
25+
26+### Style
27+
28+Keep coding style consistent with the rest of the file, but don't worry about style too much otherwise. PRs changing code style are viewed skeptically.
29+
30+### Dependencies
31+
32+Avoid adding unnecessary dependencies. If a dependency provides only a minor convenience for your implementation, it's probably better to skip it.
33+
34+If you do add a new dependency, try to support a wide range of versions of it.
35+
36+### Backwards Compatibility
37+
38+Backwards incompatible changes are viewed skeptically—best to ask in an issue to see if a particular backwards incompatible change would be approved. If possible keep backwards compatibility by adding new APIs and deprecating old ones.
39+
40+Keep backwards compatibility with old versions of dependencies when possible.
41+
42+## PR Guidelines
43+
44+### PR Scope
45+
46+As much as possible, keep separate changes in separate PRs.
47+
48+### Testing
49+
50+Tests are recommended, but not required.
51+
52+### Documentation
53+
54+All public APIs must be documented. Documenting private functions is optional, but may be nice depending on their complexity. Example documentation:
55+
56+```
57+-- | Generate an action which will either read from an automatically
58+-- updated value, or run the update action in the current thread.
59+--
60+-- ==== __Examples__
61+--
62+-- > getTime <- mkAutoUpdate defaultUpdateSettings
63+--
64+-- @since 0.1.0
65+mkAutoUpdate :: UpdateSettings a -> IO (IO a)
66+```
67+
68+Examples are recommended, but not required, in documentation. Marking new APIs with `@since <version number>` is required.
69+
70+### Versioning
71+
72+Wai packages roughly follow the Haskell Package Versioning Policy style of A.B.C.[D] (MAJOR.MAJOR.MINOR.[PATCH])
73+
74+* A - Used for massive changes in the library. (Example: 1.2.3.4 becomes 2.0.0)
75+* B - Used for smaller breaking changes, like removing, renaming, or changing behavior of existing public API. (Example: 1.2.3.4 becomes 1.3.0)
76+* C - Used for new public APIs (Example: 1.2.3.4 becomes 1.2.4)
77+* D - Used for bug fixes (Example: 1.2.3.4 becomes 1.2.3.5).
78+ * D is optional in the version number, so 2.0.0 is a valid version.
79+
80+Documentation changes don't require a new version.
81+
82+If you feel there is ambiguity to a change (e.g. fixing a bug in a function, when people may be relying on the old broken behavior), you can ask in an issue or pull request.
83+
84+Unlike in the Package Versioning Policy, deprecations are not counted as MAJOR changes.
85+
86+In some cases, dropping compatibility with a major version of a dependency (e.g. changing from transformers >= 0.3 to transformers >= 0.4), is considered a breaking change.
87+
88+### Changelog
89+
90+After you submit a PR, update the subproject's Changelog.md file with the new version number and a link to your PR. If your PR does not need to bump the version number, include the change in an "Unreleased" section at the top.
91+
92+### Releases
93+
94+Releases should be done as soon as possible after a pull request is merged—don't be shy about reminding us to make a release if we forget.
LICENSE+1−1
@@ -1,4 +1,4 @@
1−Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
1+Copyright (c) 2012-2017 Michael Snoyman, http://www.yesodweb.com/
2
2
3Permission is hereby granted, free of charge, to any person obtaining
3Permission is hereby granted, free of charge, to any person obtaining
4a copy of this software and associated documentation files (the
4a copy of this software and associated documentation files (the
README.md+2
@@ -9,3 +9,5 @@ Code can be written that works with any web framework targeting WAI.
9[Documentation](http://www.yesodweb.com/book/web-application-interface)
9[Documentation](http://www.yesodweb.com/book/web-application-interface)
10
10
11[Haddock Documentation](http://hackage.haskell.org/package/wai)
11[Haddock Documentation](http://hackage.haskell.org/package/wai)
12+
13+[![Build Status](https://dev.azure.com/yesodweb/wai/_apis/build/status/yesodweb.wai?branchName=master)](https://dev.azure.com/yesodweb/wai/_build/latest?definitionId=1&branchName=master)
auto-update/ChangeLog.mdnew file+34
@@ -0,0 +1,34 @@
1+# ChangeLog for auto-update
2+
3+## 0.1.6
4+
5+* Add control of activation on leading vs. trailing edges for Control.Debounce
6+ [#756](https://github.com/yesodweb/wai/pull/756)
7+
8+## 0.1.5
9+
10+* Using the Strict and StrictData language extensions for GHC >8.
11+ [#752](https://github.com/yesodweb/wai/pull/752)
12+
13+## 0.1.4.1
14+
15+* [#693](https://github.com/yesodweb/wai/pull/693):
16+ Improve documentation for `reaperAction` function.
17+* [#732](https://github.com/yesodweb/wai/pull/732):
18+ Fixed memory leak in `reaperAdd` function.
19+
20+## 0.1.4
21+
22+* Provide updateActionModify API in AutoUpdate [#547](https://github.com/yesodweb/wai/pull/547)
23+
24+## 0.1.3.1
25+
26+* Doc improvements
27+
28+## 0.1.3
29+
30+* Adding a new AIP - reaperKill
31+
32+## 0.1.2
33+
34+* Added Control.Debounce
auto-update/Control/AutoUpdate.hsnew file+201
@@ -0,0 +1,201 @@
1+{-# LANGUAGE CPP #-}
2+-- | In a multithreaded environment, running actions on a regularly scheduled
3+-- background thread can dramatically improve performance.
4+-- For example, web servers need to return the current time with each HTTP response.
5+-- For a high-volume server, it's much faster for a dedicated thread to run every
6+-- second, and write the current time to a shared 'IORef', than it is for each
7+-- request to make its own call to 'getCurrentTime'.
8+--
9+-- But for a low-volume server, whose request frequency is less than once per
10+-- second, that approach will result in /more/ calls to 'getCurrentTime' than
11+-- necessary, and worse, kills idle GC.
12+--
13+-- This library solves that problem by allowing you to define actions which will
14+-- either be performed by a dedicated thread, or, in times of low volume, will
15+-- be executed by the calling thread.
16+--
17+-- Example usage:
18+--
19+-- @
20+-- import "Data.Time"
21+-- import "Control.AutoUpdate"
22+--
23+-- getTime <- 'mkAutoUpdate' 'defaultUpdateSettings'
24+-- { 'updateAction' = 'Data.Time.Clock.getCurrentTime'
25+-- , 'updateFreq' = 1000000 -- The default frequency, once per second
26+-- }
27+-- currentTime <- getTime
28+-- @
29+--
30+-- For more examples, <http://www.yesodweb.com/blog/2014/08/announcing-auto-update see the blog post introducing this library>.
31+module Control.AutoUpdate (
32+ -- * Type
33+ UpdateSettings
34+ , defaultUpdateSettings
35+ -- * Accessors
36+ , updateAction
37+ , updateFreq
38+ , updateSpawnThreshold
39+ -- * Creation
40+ , mkAutoUpdate
41+ , mkAutoUpdateWithModify
42+ ) where
43+
44+#if __GLASGOW_HASKELL__ < 709
45+import Control.Applicative ((<*>))
46+#endif
47+import Control.Concurrent (forkIO, threadDelay)
48+import Control.Concurrent.MVar (newEmptyMVar, putMVar, readMVar,
49+ takeMVar, tryPutMVar)
50+import Control.Exception (SomeException, catch, mask_, throw,
51+ try)
52+import Control.Monad (void)
53+import Data.IORef (newIORef, readIORef, writeIORef)
54+
55+-- | Default value for creating an 'UpdateSettings'.
56+--
57+-- @since 0.1.0
58+defaultUpdateSettings :: UpdateSettings ()
59+defaultUpdateSettings = UpdateSettings
60+ { updateFreq = 1000000
61+ , updateSpawnThreshold = 3
62+ , updateAction = return ()
63+ }
64+
65+-- | Settings to control how values are updated.
66+--
67+-- This should be constructed using 'defaultUpdateSettings' and record
68+-- update syntax, e.g.:
69+--
70+-- @
71+-- let settings = 'defaultUpdateSettings' { 'updateAction' = 'Data.Time.Clock.getCurrentTime' }
72+-- @
73+--
74+-- @since 0.1.0
75+data UpdateSettings a = UpdateSettings
76+ { updateFreq :: Int
77+ -- ^ Microseconds between update calls. Same considerations as
78+ -- 'threadDelay' apply.
79+ --
80+ -- Default: 1 second (1000000)
81+ --
82+ -- @since 0.1.0
83+ , updateSpawnThreshold :: Int
84+ -- ^ NOTE: This value no longer has any effect, since worker threads are
85+ -- dedicated instead of spawned on demand.
86+ --
87+ -- Previously, this determined how many times the data must be requested
88+ -- before we decide to spawn a dedicated thread.
89+ --
90+ -- Default: 3
91+ --
92+ -- @since 0.1.0
93+ , updateAction :: IO a
94+ -- ^ Action to be performed to get the current value.
95+ --
96+ -- Default: does nothing.
97+ --
98+ -- @since 0.1.0
99+ }
100+
101+-- | Generate an action which will either read from an automatically
102+-- updated value, or run the update action in the current thread.
103+--
104+-- @since 0.1.0
105+mkAutoUpdate :: UpdateSettings a -> IO (IO a)
106+mkAutoUpdate us = mkAutoUpdateHelper us Nothing
107+
108+-- | Generate an action which will either read from an automatically
109+-- updated value, or run the update action in the current thread if
110+-- the first time or the provided modify action after that.
111+--
112+-- @since 0.1.4
113+mkAutoUpdateWithModify :: UpdateSettings a -> (a -> IO a) -> IO (IO a)
114+mkAutoUpdateWithModify us f = mkAutoUpdateHelper us (Just f)
115+
116+mkAutoUpdateHelper :: UpdateSettings a -> Maybe (a -> IO a) -> IO (IO a)
117+mkAutoUpdateHelper us updateActionModify = do
118+ -- A baton to tell the worker thread to generate a new value.
119+ needsRunning <- newEmptyMVar
120+
121+ -- The initial response variable. Response variables allow the requesting
122+ -- thread to block until a value is generated by the worker thread.
123+ responseVar0 <- newEmptyMVar
124+
125+ -- The current value, if available. We start off with a Left value
126+ -- indicating no value is available, and the above-created responseVar0 to
127+ -- give a variable to block on.
128+ currRef <- newIORef $ Left responseVar0
129+
130+ -- This is used to set a value in the currRef variable when the worker
131+ -- thread exits. In reality, that value should never be used, since the
132+ -- worker thread exiting only occurs if an async exception is thrown, which
133+ -- should only occur if there are no references to needsRunning left.
134+ -- However, this handler will make error messages much clearer if there's a
135+ -- bug in the implementation.
136+ let fillRefOnExit f = do
137+ eres <- try f
138+ case eres of
139+ Left e -> writeIORef currRef $ error $
140+ "Control.AutoUpdate.mkAutoUpdate: worker thread exited with exception: "
141+ ++ show (e :: SomeException)
142+ Right () -> writeIORef currRef $ error $
143+ "Control.AutoUpdate.mkAutoUpdate: worker thread exited normally, "
144+ ++ "which should be impossible due to usage of infinite loop"
145+
146+ -- fork the worker thread immediately. Note that we mask async exceptions,
147+ -- but *not* in an uninterruptible manner. This will allow a
148+ -- BlockedIndefinitelyOnMVar exception to still be thrown, which will take
149+ -- down this thread when all references to the returned function are
150+ -- garbage collected, and therefore there is no thread that can fill the
151+ -- needsRunning MVar.
152+ --
153+ -- Note that since we throw away the ThreadId of this new thread and never
154+ -- calls myThreadId, normal async exceptions can never be thrown to it,
155+ -- only RTS exceptions.
156+ mask_ $ void $ forkIO $ fillRefOnExit $ do
157+ -- This infinite loop makes up out worker thread. It takes an a
158+ -- responseVar value where the next value should be putMVar'ed to for
159+ -- the benefit of any requesters currently blocked on it.
160+ let loop responseVar maybea = do
161+ -- block until a value is actually needed
162+ takeMVar needsRunning
163+
164+ -- new value requested, so run the updateAction
165+ a <- catchSome $ maybe (updateAction us) id (updateActionModify <*> maybea)
166+
167+ -- we got a new value, update currRef and lastValue
168+ writeIORef currRef $ Right a
169+ putMVar responseVar a
170+
171+ -- delay until we're needed again
172+ threadDelay $ updateFreq us
173+
174+ -- delay's over. create a new response variable and set currRef
175+ -- to use it, so that the next requester will block on that
176+ -- variable. Then loop again with the updated response
177+ -- variable.
178+ responseVar' <- newEmptyMVar
179+ writeIORef currRef $ Left responseVar'
180+ loop responseVar' (Just a)
181+
182+ -- Kick off the loop, with the initial responseVar0 variable.
183+ loop responseVar0 Nothing
184+
185+ return $ do
186+ mval <- readIORef currRef
187+ case mval of
188+ Left responseVar -> do
189+ -- no current value, force the worker thread to run...
190+ void $ tryPutMVar needsRunning ()
191+
192+ -- and block for the result from the worker
193+ readMVar responseVar
194+ -- we have a current value, use it
195+ Right val -> return val
196+
197+-- | Turn a runtime exception into an impure exception, so that all 'IO'
198+-- actions will complete successfully. This simply defers the exception until
199+-- the value is forced.
200+catchSome :: IO a -> IO a
201+catchSome act = Control.Exception.catch act $ \e -> return $ throw (e :: SomeException)
warp/Network/Wai/Handler/Warp/IORef.hs → auto-update/Control/AutoUpdate/Util.hsrenamed+9−8
@@ -1,15 +1,16 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+module Control.AutoUpdate.Util
3+ ( atomicModifyIORef'
4+ ) where
2
5
3−module Network.Wai.Handler.Warp.IORef (
6+#ifndef MIN_VERSION_base
4− module Data.IORef
5−#if !MIN_VERSION_base(4,6,0)
7+#define MIN_VERSION_base(x,y,z) 1
6− , atomicModifyIORef'
7#endif
8#endif
8− ) where
9
9
10−import Data.IORef
11−
12−#if !MIN_VERSION_base(4,6,0)
10+#if MIN_VERSION_base(4,6,0)
11+import Data.IORef (atomicModifyIORef')
12+#else
13+import Data.IORef (IORef, atomicModifyIORef)
13-- | Strict version of 'atomicModifyIORef'. This forces both the value stored
14-- | Strict version of 'atomicModifyIORef'. This forces both the value stored
14-- in the 'IORef' as well as the value returned.
15-- in the 'IORef' as well as the value returned.
15atomicModifyIORef' :: IORef a -> (a -> (a,b)) -> IO b
16atomicModifyIORef' :: IORef a -> (a -> (a,b)) -> IO b
auto-update/Control/Debounce.hsnew file+60
@@ -0,0 +1,60 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+-- | Debounce an action, ensuring it doesn't occur more than once for a given
3+-- period of time.
4+--
5+-- This is useful as an optimization, for example to ensure that logs are only
6+-- flushed to disk at most once per second.
7+--
8+-- Example usage:
9+--
10+-- @
11+-- printString <- 'mkDebounce' 'defaultDebounceSettings'
12+-- { 'debounceAction' = putStrLn "Running action"
13+-- , 'debounceFreq' = 5000000 -- 5 seconds
14+-- , 'debounceEdge' = 'DI.trailingEdge' -- Trigger on the trailing edge
15+-- }
16+-- @
17+--
18+-- >>> printString
19+-- Running action
20+-- >>> printString
21+-- <Wait five seconds>
22+-- Running action
23+--
24+-- See the fast-logger package ("System.Log.FastLogger") for real-world usage.
25+--
26+-- @since 0.1.2
27+module Control.Debounce
28+ ( -- * Type
29+ DI.DebounceSettings
30+ , defaultDebounceSettings
31+ -- * Accessors
32+ , DI.debounceFreq
33+ , DI.debounceAction
34+ , DI.debounceEdge
35+ , DI.leadingEdge
36+ , DI.trailingEdge
37+ -- * Creation
38+ , mkDebounce
39+ ) where
40+
41+import Control.Concurrent (newEmptyMVar, threadDelay)
42+import qualified Control.Debounce.Internal as DI
43+
44+-- | Default value for creating a 'DebounceSettings'.
45+--
46+-- @since 0.1.2
47+defaultDebounceSettings :: DI.DebounceSettings
48+defaultDebounceSettings = DI.DebounceSettings
49+ { DI.debounceFreq = 1000000
50+ , DI.debounceAction = return ()
51+ , DI.debounceEdge = DI.leadingEdge
52+ }
53+
54+-- | Generate an action which will trigger the debounced action to be performed.
55+--
56+-- @since 0.1.2
57+mkDebounce :: DI.DebounceSettings -> IO (IO ())
58+mkDebounce settings = do
59+ baton <- newEmptyMVar
60+ DI.mkDebounceInternal baton threadDelay settings
auto-update/Control/Debounce/Internal.hsnew file+98
@@ -0,0 +1,98 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+
3+-- | Unstable API which exposes internals for testing.
4+module Control.Debounce.Internal (
5+ DebounceSettings(..)
6+ , DebounceEdge(..)
7+ , leadingEdge
8+ , trailingEdge
9+ , mkDebounceInternal
10+ ) where
11+
12+import Control.Concurrent (forkIO)
13+import Control.Concurrent.MVar (takeMVar, tryPutMVar, tryTakeMVar, MVar)
14+import Control.Exception (SomeException, handle, mask_)
15+import Control.Monad (forever, void)
16+
17+-- | Settings to control how debouncing should work.
18+--
19+-- This should be constructed using 'defaultDebounceSettings' and record
20+-- update syntax, e.g.:
21+--
22+-- @
23+-- let settings = 'defaultDebounceSettings' { 'debounceAction' = flushLog }
24+-- @
25+--
26+-- @since 0.1.2
27+data DebounceSettings = DebounceSettings
28+ { debounceFreq :: Int
29+ -- ^ Length of the debounce timeout period in microseconds.
30+ --
31+ -- Default: 1 second (1000000)
32+ --
33+ -- @since 0.1.2
34+ , debounceAction :: IO ()
35+ -- ^ Action to be performed.
36+ --
37+ -- Note: all exceptions thrown by this action will be silently discarded.
38+ --
39+ -- Default: does nothing.
40+ --
41+ -- @since 0.1.2
42+ , debounceEdge :: DebounceEdge
43+ -- ^ Whether to perform the action on the leading edge or trailing edge of
44+ -- the timeout.
45+ --
46+ -- Default: 'trailingEdge'.
47+ --
48+ -- @since 0.1.6
49+ }
50+
51+-- | Setting to control whether the action happens at the leading and/or trailing
52+-- edge of the timeout.
53+--
54+-- @since 0.1.6
55+data DebounceEdge =
56+ Leading
57+ -- ^ Perform the action immediately, and then begin a cooldown period.
58+ -- If the trigger happens again during the cooldown, wait until the end of the cooldown
59+ -- and then perform the action again, then enter a new cooldown period.
60+ | Trailing
61+ -- ^ Start a cooldown period and perform the action when the period ends. If another trigger
62+ -- happens during the cooldown, it has no effect.
63+ deriving (Show, Eq)
64+
65+
66+-- | Perform the action immediately, and then begin a cooldown period.
67+-- If the trigger happens again during the cooldown, wait until the end of the cooldown
68+-- and then perform the action again, then enter a new cooldown period.
69+--
70+-- @since 0.1.6
71+leadingEdge :: DebounceEdge
72+leadingEdge = Leading
73+
74+-- | Start a cooldown period and perform the action when the period ends. If another trigger
75+-- happens during the cooldown, it has no effect.
76+--
77+-- @since 0.1.6
78+trailingEdge :: DebounceEdge
79+trailingEdge = Trailing
80+
81+mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
82+mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
83+ mask_ $ void $ forkIO $ forever $ do
84+ takeMVar baton
85+ case edge of
86+ Leading -> do
87+ ignoreExc action
88+ delayFn freq
89+ Trailing -> do
90+ delayFn freq
91+ -- Empty the baton of any other activations during the interval
92+ void $ tryTakeMVar baton
93+ ignoreExc action
94+
95+ return $ void $ tryPutMVar baton ()
96+
97+ignoreExc :: IO () -> IO ()
98+ignoreExc = handle $ \(_ :: SomeException) -> return ()
auto-update/Control/Reaper.hsnew file+280
@@ -0,0 +1,280 @@
1+{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE BangPatterns #-}
3+
4+-- | This module provides the ability to create reapers: dedicated cleanup
5+-- threads. These threads will automatically spawn and die based on the
6+-- presence of a workload to process on. Example uses include:
7+--
8+-- * Killing long-running jobs
9+-- * Closing unused connections in a connection pool
10+-- * Pruning a cache of old items (see example below)
11+--
12+-- For real-world usage, search the <https://github.com/yesodweb/wai WAI family of packages>
13+-- for imports of "Control.Reaper".
14+module Control.Reaper (
15+ -- * Example: Regularly cleaning a cache
16+ -- $example1
17+
18+ -- * Settings
19+ ReaperSettings
20+ , defaultReaperSettings
21+ -- * Accessors
22+ , reaperAction
23+ , reaperDelay
24+ , reaperCons
25+ , reaperNull
26+ , reaperEmpty
27+ -- * Type
28+ , Reaper(..)
29+ -- * Creation
30+ , mkReaper
31+ -- * Helper
32+ , mkListAction
33+ ) where
34+
35+import Control.AutoUpdate.Util (atomicModifyIORef')
36+import Control.Concurrent (forkIO, threadDelay, killThread, ThreadId)
37+import Control.Exception (mask_)
38+import Data.IORef (IORef, newIORef, readIORef, writeIORef)
39+
40+-- | Settings for creating a reaper. This type has two parameters:
41+-- @workload@ gives the entire workload, whereas @item@ gives an
42+-- individual piece of the queue. A common approach is to have @workload@
43+-- be a list of @item@s. This is encouraged by 'defaultReaperSettings' and
44+-- 'mkListAction'.
45+--
46+-- @since 0.1.1
47+data ReaperSettings workload item = ReaperSettings
48+ { reaperAction :: workload -> IO (workload -> workload)
49+ -- ^ The action to perform on a workload. The result of this is a
50+ -- \"workload modifying\" function. In the common case of using lists,
51+ -- the result should be a difference list that prepends the remaining
52+ -- workload to the temporary workload. The temporary workload here
53+ -- refers to items added to the workload while the reaper action is
54+ -- running. For help with setting up such an action, see 'mkListAction'.
55+ --
56+ -- Default: do nothing with the workload, and then prepend it to the
57+ -- temporary workload. This is incredibly useless; you should
58+ -- definitely override this default.
59+ --
60+ -- @since 0.1.1
61+ , reaperDelay :: {-# UNPACK #-} !Int
62+ -- ^ Number of microseconds to delay between calls of 'reaperAction'.
63+ --
64+ -- Default: 30 seconds.
65+ --
66+ -- @since 0.1.1
67+ , reaperCons :: item -> workload -> workload
68+ -- ^ Add an item onto a workload.
69+ --
70+ -- Default: list consing.
71+ --
72+ -- @since 0.1.1
73+ , reaperNull :: workload -> Bool
74+ -- ^ Check if a workload is empty, in which case the worker thread
75+ -- will shut down.
76+ --
77+ -- Default: 'null'.
78+ --
79+ -- @since 0.1.1
80+ , reaperEmpty :: workload
81+ -- ^ An empty workload.
82+ --
83+ -- Default: empty list.
84+ --
85+ -- @since 0.1.1
86+ }
87+
88+-- | Default @ReaperSettings@ value, biased towards having a list of work
89+-- items.
90+--
91+-- @since 0.1.1
92+defaultReaperSettings :: ReaperSettings [item] item
93+defaultReaperSettings = ReaperSettings
94+ { reaperAction = \wl -> return (wl ++)
95+ , reaperDelay = 30000000
96+ , reaperCons = (:)
97+ , reaperNull = null
98+ , reaperEmpty = []
99+ }
100+
101+-- | A data structure to hold reaper APIs.
102+data Reaper workload item = Reaper {
103+ -- | Adding an item to the workload
104+ reaperAdd :: item -> IO ()
105+ -- | Reading workload.
106+ , reaperRead :: IO workload
107+ -- | Stopping the reaper thread if exists.
108+ -- The current workload is returned.
109+ , reaperStop :: IO workload
110+ -- | Killing the reaper thread immediately if exists.
111+ , reaperKill :: IO ()
112+ }
113+
114+-- | State of reaper.
115+data State workload = NoReaper -- ^ No reaper thread
116+ | Workload !workload -- ^ The current jobs
117+
118+-- | Create a reaper addition function. This function can be used to add
119+-- new items to the workload. Spawning of reaper threads will be handled
120+-- for you automatically.
121+--
122+-- @since 0.1.1
123+mkReaper :: ReaperSettings workload item -> IO (Reaper workload item)
124+mkReaper settings@ReaperSettings{..} = do
125+ stateRef <- newIORef NoReaper
126+ tidRef <- newIORef Nothing
127+ return Reaper {
128+ reaperAdd = add settings stateRef tidRef
129+ , reaperRead = readRef stateRef
130+ , reaperStop = stop stateRef
131+ , reaperKill = kill tidRef
132+ }
133+ where
134+ readRef stateRef = do
135+ mx <- readIORef stateRef
136+ case mx of
137+ NoReaper -> return reaperEmpty
138+ Workload wl -> return wl
139+ stop stateRef = atomicModifyIORef' stateRef $ \mx ->
140+ case mx of
141+ NoReaper -> (NoReaper, reaperEmpty)
142+ Workload x -> (Workload reaperEmpty, x)
143+ kill tidRef = do
144+ mtid <- readIORef tidRef
145+ case mtid of
146+ Nothing -> return ()
147+ Just tid -> killThread tid
148+
149+add :: ReaperSettings workload item
150+ -> IORef (State workload) -> IORef (Maybe ThreadId)
151+ -> item -> IO ()
152+add settings@ReaperSettings{..} stateRef tidRef item =
153+ mask_ $ do
154+ next <- atomicModifyIORef' stateRef cons
155+ next
156+ where
157+ cons NoReaper = let wl = reaperCons item reaperEmpty
158+ in (Workload wl, spawn settings stateRef tidRef)
159+ cons (Workload wl) = let wl' = reaperCons item wl
160+ in (Workload wl', return ())
161+
162+spawn :: ReaperSettings workload item
163+ -> IORef (State workload) -> IORef (Maybe ThreadId)
164+ -> IO ()
165+spawn settings stateRef tidRef = do
166+ tid <- forkIO $ reaper settings stateRef tidRef
167+ writeIORef tidRef $ Just tid
168+
169+reaper :: ReaperSettings workload item
170+ -> IORef (State workload) -> IORef (Maybe ThreadId)
171+ -> IO ()
172+reaper settings@ReaperSettings{..} stateRef tidRef = do
173+ threadDelay reaperDelay
174+ -- Getting the current jobs. Push an empty job to the reference.
175+ wl <- atomicModifyIORef' stateRef swapWithEmpty
176+ -- Do the jobs. A function to merge the left jobs and
177+ -- new jobs is returned.
178+ !merge <- reaperAction wl
179+ -- Merging the left jobs and new jobs.
180+ -- If there is no jobs, this thread finishes.
181+ next <- atomicModifyIORef' stateRef (check merge)
182+ next
183+ where
184+ swapWithEmpty NoReaper = error "Control.Reaper.reaper: unexpected NoReaper (1)"
185+ swapWithEmpty (Workload wl) = (Workload reaperEmpty, wl)
186+
187+ check _ NoReaper = error "Control.Reaper.reaper: unexpected NoReaper (2)"
188+ check merge (Workload wl)
189+ -- If there is no job, reaper is terminated.
190+ | reaperNull wl' = (NoReaper, writeIORef tidRef Nothing)
191+ -- If there are jobs, carry them out.
192+ | otherwise = (Workload wl', reaper settings stateRef tidRef)
193+ where
194+ wl' = merge wl
195+
196+-- | A helper function for creating 'reaperAction' functions. You would
197+-- provide this function with a function to process a single work item and
198+-- return either a new work item, or @Nothing@ if the work item is
199+-- expired.
200+--
201+-- @since 0.1.1
202+mkListAction :: (item -> IO (Maybe item'))
203+ -> [item]
204+ -> IO ([item'] -> [item'])
205+mkListAction f =
206+ go id
207+ where
208+ go !front [] = return front
209+ go !front (x:xs) = do
210+ my <- f x
211+ let front' =
212+ case my of
213+ Nothing -> front
214+ Just y -> front . (y:)
215+ go front' xs
216+
217+-- $example1
218+-- In this example code, we use a 'Data.Map.Strict.Map' to cache fibonacci numbers, and a 'Reaper' to prune the cache.
219+--
220+-- The @main@ function first creates a 'Reaper', with fields to initialize the
221+-- cache ('reaperEmpty'), add items to it ('reaperCons'), and prune it ('reaperAction').
222+-- The reaper will run every two seconds ('reaperDelay'), but will stop running while
223+-- 'reaperNull' is true.
224+--
225+-- @main@ then loops infinitely ('Control.Monad.forever'). Each second it calculates the fibonacci number
226+-- for a value between 30 and 34, first trying the cache ('reaperRead' and 'Data.Map.Strict.lookup'),
227+-- then falling back to manually calculating it (@fib@)
228+-- and updating the cache with the result ('reaperAdd')
229+--
230+-- @clean@ simply removes items cached for more than 10 seconds.
231+-- This function is where you would perform IO-related cleanup,
232+-- like killing threads or closing connections, if that was the purpose of your reaper.
233+--
234+-- @
235+-- module Main where
236+--
237+-- import "Data.Time" (UTCTime, getCurrentTime, diffUTCTime)
238+-- import "Control.Reaper"
239+-- import "Control.Concurrent" (threadDelay)
240+-- import "Data.Map.Strict" (Map)
241+-- import qualified "Data.Map.Strict" as Map
242+-- import "Control.Monad" (forever)
243+-- import "System.Random" (getStdRandom, randomR)
244+--
245+-- fib :: 'Int' -> 'Int'
246+-- fib 0 = 0
247+-- fib 1 = 1
248+-- fib n = fib (n-1) + fib (n-2)
249+--
250+-- type Cache = 'Data.Map.Strict.Map' 'Int' ('Int', 'Data.Time.Clock.UTCTime')
251+--
252+-- main :: IO ()
253+-- main = do
254+-- reaper <- 'mkReaper' 'defaultReaperSettings'
255+-- { 'reaperEmpty' = Map.'Data.Map.Strict.empty'
256+-- , 'reaperCons' = \\(k, v, time) workload -> Map.'Data.Map.Strict.insert' k (v, time) workload
257+-- , 'reaperAction' = clean
258+-- , 'reaperDelay' = 1000000 * 2 -- Clean every 2 seconds
259+-- , 'reaperNull' = Map.'Data.Map.Strict.null'
260+-- }
261+-- forever $ do
262+-- fibArg <- 'System.Random.getStdRandom' ('System.Random.randomR' (30,34))
263+-- cache <- 'reaperRead' reaper
264+-- let cachedResult = Map.'Data.Map.Strict.lookup' fibArg cache
265+-- case cachedResult of
266+-- 'Just' (fibResult, _createdAt) -> 'putStrLn' $ "Found in cache: `fib " ++ 'show' fibArg ++ "` " ++ 'show' fibResult
267+-- 'Nothing' -> do
268+-- let fibResult = fib fibArg
269+-- 'putStrLn' $ "Calculating `fib " ++ 'show' fibArg ++ "` " ++ 'show' fibResult
270+-- time <- 'Data.Time.Clock.getCurrentTime'
271+-- ('reaperAdd' reaper) (fibArg, fibResult, time)
272+-- 'threadDelay' 1000000 -- 1 second
273+--
274+-- -- Remove items > 10 seconds old
275+-- clean :: Cache -> IO (Cache -> Cache)
276+-- clean oldMap = do
277+-- currentTime <- 'Data.Time.Clock.getCurrentTime'
278+-- let pruned = Map.'Data.Map.Strict.filter' (\\(_, createdAt) -> currentTime \`diffUTCTime\` createdAt < 10.0) oldMap
279+-- return (\\newData -> Map.'Data.Map.Strict.union' pruned newData)
280+-- @
auto-update/Data/AutoUpdate.hsdeleted−107
@@ -1,107 +0,0 @@
1−{-# LANGUAGE DeriveDataTypeable, RankNTypes #-}
2−-- FIXME: should we replace Data with System?
3−module Data.AutoUpdate where
4−
5−import Control.Applicative ((<$>))
6−import Data.IORef
7−import Control.Concurrent (threadDelay, forkIOWithUnmask)
8−import Control.Monad (forever, void)
9−import Control.Exception (Exception, SomeException(..), handle, catches, throwIO, assert, Handler(..), mask)
10−import Data.Typeable (Typeable)
11−
12−----------------------------------------------------------------
13−
14−data UpdateSettings a = UpdateSettings
15− { updateFreq :: !Int
16− , updateSpawnThreshold :: !Int
17− , updateAction :: !(IO a)
18− }
19−
20−data Status a = AutoUpdated !a !Int -- ^ # of manual update during updateFreq
21− | SemiAuto
22− | ManualUpdates !Int -- ^ # of manual update
23−
24−data AutoUpdate a = AutoUpdate
25− { auSettings :: !(UpdateSettings a)
26− , auStatus :: !(IORef (Status a))
27− }
28−
29−data Action a = Return a | Manual | Spawn
30−
31−data Replaced = Replaced deriving (Show, Typeable)
32−instance Exception Replaced
33−
34−----------------------------------------------------------------
35−
36−mkAutoUpdate :: UpdateSettings a -> IO (AutoUpdate a)
37−mkAutoUpdate settings = AutoUpdate settings <$> newIORef (ManualUpdates 0)
38−
39−getCurrent :: AutoUpdate a -> IO a
40−getCurrent au@(AutoUpdate (UpdateSettings _ spawnThreshold action) istatus) =
41− handle handler update
42− where
43− update = mask $ \restore -> do
44− ea <- atomicModifyIORef' istatus increment
45− case ea of
46− Return a -> return a
47− Manual -> restore action
48− Spawn -> do
49− a <- restore action
50− void $ forkIOWithUnmask $ spawn au a
51− -- Without mask, an asynchronous exeption here may lead
52− -- to spawn multiple threads.
53− return a
54−
55− increment (AutoUpdated a cnt) = (AutoUpdated a (succ cnt), Return a)
56− increment SemiAuto = (SemiAuto, Manual)
57− increment (ManualUpdates i)
58− | i > spawnThreshold = (SemiAuto, Spawn)
59− | otherwise = (ManualUpdates (succ i), Manual)
60−
61− handler (SomeException ex) = do
62− atomicModifyIORef' istatus reset
63− throwIO ex
64−
65− -- Resetting to the safe status
66− reset SemiAuto = (ManualUpdates 0, ())
67− -- If status is AutoUpdated, an thread is running.
68− -- So, let's keep the status.
69− reset status = (status, ())
70−
71−----------------------------------------------------------------
72−
73−spawn :: AutoUpdate a -> a -> (forall b. IO b -> IO b) -> IO ()
74−spawn (AutoUpdate (UpdateSettings freq _ action) istatus) x restore =
75− update `catches` handlers
76− where
77− update = do
78− void $ atomicModifyIORef' istatus (turnToAuto x)
79− restore loop
80−
81− turnToAuto a _ = (AutoUpdated a 0, ())
82−
83− loop = forever $ do
84− threadDelay freq
85− a <- action
86− doit <- atomicModifyIORef' istatus $ trunToManual a
87− doit
88−
89− -- Normal case.
90− trunToManual a (AutoUpdated _ cnt)
91− | cnt >= 1 = (AutoUpdated a 0, return ())
92− | otherwise = (ManualUpdates 0, stop)
93− -- This case must not happen.
94− trunToManual _ _ = assert False (ManualUpdates 0, stop)
95−
96− stop :: IO a
97− stop = throwIO Replaced
98−
99− handlers :: [Handler ()]
100− handlers = [Handler rhandler, Handler ohandler]
101−
102− rhandler Replaced = return ()
103− ohandler (SomeException ex) = do
104− -- This thread is killed.
105− -- Resetting to the safe status.
106− atomicModifyIORef' istatus $ \_ -> (ManualUpdates 0, ())
107− throwIO ex
auto-update/Data/AutoUpdateOld.hsdeleted−70
@@ -1,70 +0,0 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−module Data.AutoUpdate where
3−
4−import Data.IORef
5−import Control.Concurrent (threadDelay, forkIO, ThreadId, killThread, myThreadId)
6−import Control.Monad (join, forever)
7−import Control.Exception (throwTo, Exception, handle, fromException, throwIO, assert)
8−import Data.Typeable (Typeable)
9−
10−data UpdateSettings a = UpdateSettings
11− { updateFreq :: !Int
12− , updateSpawnThreshold :: !Int
13− , updateAction :: !(IO a)
14− }
15−
16−data Status a = AutoUpdated !a !Int !ThreadId | ManualUpdates !Int
17−data AutoUpdate a = AutoUpdate
18− { auSettings :: !(UpdateSettings a)
19− , auStatus :: !(IORef (Status a))
20− }
21−
22−mkAutoUpdate :: UpdateSettings a -> IO (AutoUpdate a)
23−mkAutoUpdate settings = do
24− istatus <- newIORef $ ManualUpdates 0
25− return $ AutoUpdate settings istatus
26−
27−data Action a = Return a | Manual | Spawn
28−
29−getCurrent :: AutoUpdate a -> IO a
30−getCurrent (AutoUpdate (UpdateSettings freq spawnThreshold action) istatus) = do
31− ea <- atomicModifyIORef istatus $ \status ->
32− case status of
33− AutoUpdated a cnt tid -> (AutoUpdated a (succ cnt) tid, Return a)
34− ManualUpdates i -> (ManualUpdates (succ i), if i > spawnThreshold then Spawn else Manual)
35− case ea of
36− Return a -> return a
37− Manual -> action
38− Spawn -> do
39− a <- action
40− tid <- forkIO spawn
41− join $ atomicModifyIORef istatus $ \status ->
42− case status of
43− AutoUpdated _ cnt old -> (AutoUpdated a cnt tid, throwTo old Replaced)
44− ManualUpdates cnt -> (AutoUpdated a cnt tid, return ())
45− return a
46− where
47− spawn = handle onErr $ forever $ do
48− threadDelay freq
49− a <- action
50− let stop = throwIO Replaced
51− join $ atomicModifyIORef istatus $ \status ->
52− case status of
53− AutoUpdated _ cnt tid
54− | cnt >= 1 -> (AutoUpdated a 0 tid, return ())
55− | otherwise -> (ManualUpdates 0, stop)
56− ManualUpdates i -> assert False (ManualUpdates i, stop)
57− onErr ex =
58− case fromException ex of
59− Just Replaced -> return ()
60− Nothing -> do
61− tid <- myThreadId
62− atomicModifyIORef istatus $ \status ->
63− case status of
64− AutoUpdated _ _ tid' | tid == tid' -> (ManualUpdates 0, ())
65− _ -> (status, ())
66− throwIO ex
67−
68−data Replaced = Replaced
69− deriving (Show, Typeable)
70−instance Exception Replaced
wai-test/LICENSE → auto-update/LICENSErenamed+8−8
@@ -1,4 +1,4 @@
1−Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
1+Copyright (c) 2014 Michael Snoyman
2
2
3Permission is hereby granted, free of charge, to any person obtaining
3Permission is hereby granted, free of charge, to any person obtaining
4a copy of this software and associated documentation files (the
4a copy of this software and associated documentation files (the
@@ -8,13 +8,13 @@ distribute, sublicense, and/or sell copies of the Software, and to
8permit persons to whom the Software is furnished to do so, subject to
8permit persons to whom the Software is furnished to do so, subject to
9the following conditions:
9the following conditions:
10
10
11−The above copyright notice and this permission notice shall be
11+The above copyright notice and this permission notice shall be included
12−included in all copies or substantial portions of the Software.
12+in all copies or substantial portions of the Software.
13
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16−MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17−NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18−LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19−OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20−WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
auto-update/README.md+11−3
@@ -1,5 +1,13 @@
1−See https://github.com/yesodweb/yesod-scaffold/pull/15
1+## auto-update
2
2
3−Todo:
3+A common problem is the desire to have an action run at a scheduled interval,
4+but only if it is needed. For example, instead of having every web request
5+result in a new `getCurrentTime` call, we'd like to have a single worker thread
6+run every second, updating an `IORef`. However, if the request frequency is
7+less than once per second, this is a pessimization, and worse, kills idle GC.
4
8
5−- Add ThreadId to prevent race condition.
9+This library allows you to define actions which will either be performed by a
10+dedicated thread or, in times of low volume, will be executed by the calling
11+thread.
12+
13+For original use case, see [yesod-scaffold issue #15](https://github.com/yesodweb/yesod-scaffold/pull/15).
auto-update/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
auto-update/auto-update.cabalnew file+38
@@ -0,0 +1,38 @@
1+name: auto-update
2+version: 0.1.6
3+synopsis: Efficiently run periodic, on-demand actions
4+description: API docs and the README are available at <http://www.stackage.org/package/auto-update>.
5+homepage: https://github.com/yesodweb/wai
6+license: MIT
7+license-file: LICENSE
8+author: Michael Snoyman
9+maintainer: michael@snoyman.com
10+category: Control
11+build-type: Simple
12+extra-source-files: README.md
13+ ChangeLog.md
14+cabal-version: >=1.10
15+
16+library
17+ ghc-options: -Wall
18+ exposed-modules: Control.AutoUpdate
19+ Control.Debounce
20+ Control.Debounce.Internal
21+ Control.Reaper
22+ other-modules: Control.AutoUpdate.Util
23+ build-depends: base >= 4 && < 5
24+ default-language: Haskell2010
25+ if impl(ghc >= 8)
26+ default-extensions: Strict StrictData
27+
28+-- Test suite is currently not robust enough, gives too many false negatives.
29+
30+test-suite spec
31+ main-is: Spec.hs
32+ other-modules: Control.AutoUpdateSpec
33+ Control.DebounceSpec
34+ Control.ReaperSpec
35+ hs-source-dirs: test
36+ type: exitcode-stdio-1.0
37+ build-depends: base, auto-update, exceptions, hspec, retry, HUnit
38+ default-language: Haskell2010
auto-update/test/Control/AutoUpdateSpec.hsnew file+35
@@ -0,0 +1,35 @@
1+module Control.AutoUpdateSpec (spec) where
2+
3+import Control.AutoUpdate
4+import Control.Concurrent (threadDelay)
5+import Control.Monad (replicateM_, forM_)
6+import Data.IORef
7+import Test.Hspec
8+import Test.Hspec.QuickCheck
9+
10+spec :: Spec
11+spec = return ()
12+ -- do
13+ -- prop "incrementer" $ \st' -> do
14+ -- let st = abs st' `mod` 10000
15+ -- ref <- newIORef 0
16+ -- next <- mkAutoUpdate defaultUpdateSettings
17+ -- { updateAction = atomicModifyIORef ref $ \i ->
18+ -- let i' = succ i in i' `seq` (i', i')
19+ -- , updateSpawnThreshold = st
20+ -- , updateFreq = 10000
21+ -- }
22+
23+ -- forM_ [1..st + 1] $ \i -> do
24+ -- j <- next
25+ -- j `shouldBe` i
26+
27+ -- replicateM_ 50 $ do
28+ -- i <- next
29+ -- i `shouldBe` st + 2
30+
31+ -- threadDelay 60000
32+ -- last1 <- readIORef ref
33+ -- threadDelay 20000
34+ -- last2 <- readIORef ref
35+ -- last2 `shouldBe` last1
auto-update/test/Control/DebounceSpec.hsnew file+120
@@ -0,0 +1,120 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+module Control.DebounceSpec (spec) where
3+
4+import Control.Concurrent
5+import Control.Debounce
6+import qualified Control.Debounce.Internal as DI
7+import Control.Monad
8+import Control.Monad.Catch
9+import Control.Retry
10+import Data.IORef
11+import Test.HUnit.Lang
12+import Test.Hspec
13+
14+spec :: Spec
15+spec = describe "mkDebounce" $ do
16+ describe "Leading edge" $ do
17+ it "works for a single event" $ do
18+ (ref, debounced, baton, returnFromWait) <- getDebounce leadingEdge
19+
20+ debounced
21+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
22+
23+ returnFromWait
24+ pause
25+ readIORef ref >>= (`shouldBe` 1)
26+
27+ -- Try another round
28+ debounced
29+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 2)
30+
31+ returnFromWait
32+ pause
33+ readIORef ref >>= (`shouldBe` 2)
34+
35+ it "works for multiple events" $ do
36+ (ref, debounced, baton, returnFromWait) <- getDebounce leadingEdge
37+
38+ debounced
39+ waitForBatonToBeTaken baton
40+ debounced
41+ pause
42+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
43+
44+ returnFromWait
45+ pause
46+ readIORef ref >>= (`shouldBe` 2)
47+
48+ describe "Trailing edge" $ do
49+ it "works for a single event" $ do
50+ (ref, debounced, baton, returnFromWait) <- getDebounce trailingEdge
51+
52+ debounced
53+ pause
54+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 0)
55+
56+ returnFromWait
57+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
58+
59+ -- Try another round
60+ debounced
61+ pause
62+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
63+
64+ returnFromWait
65+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 2)
66+
67+ it "works for multiple events" $ do
68+ (ref, debounced, baton, returnFromWait) <- getDebounce trailingEdge
69+
70+ debounced
71+ waitForBatonToBeTaken baton
72+ debounced
73+ pause
74+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 0)
75+
76+ returnFromWait
77+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
78+
79+
80+-- | Make a controllable delay function
81+getWaitAction :: IO (p -> IO (), IO ())
82+getWaitAction = do
83+ waitVar <- newEmptyMVar
84+ let waitAction _ = takeMVar waitVar
85+ let returnFromWait = putMVar waitVar ()
86+ return (waitAction, returnFromWait)
87+
88+-- | Get a debounce system with access to the internals for testing
89+getDebounce :: DI.DebounceEdge -> IO (IORef Int, IO (), MVar (), IO ())
90+getDebounce edge = do
91+ ref :: IORef Int <- newIORef 0
92+ let action = modifyIORef ref (+ 1)
93+
94+ (waitAction, returnFromWait) <- getWaitAction
95+
96+ baton <- newEmptyMVar
97+
98+ debounced <- DI.mkDebounceInternal baton waitAction defaultDebounceSettings {
99+ debounceFreq = 5000000 -- unused
100+ , debounceAction = action
101+ , debounceEdge = edge
102+ }
103+
104+ return (ref, debounced, baton, returnFromWait)
105+
106+-- | Pause briefly (100ms)
107+pause :: IO ()
108+pause = threadDelay 100000
109+
110+waitForBatonToBeTaken :: MVar () -> IO ()
111+waitForBatonToBeTaken baton = waitUntil 5 $ tryReadMVar baton >>= (`shouldBe` Nothing)
112+
113+-- | Wait up to n seconds for an action to complete without throwing an HUnitFailure
114+waitUntil :: Int -> IO a -> IO ()
115+waitUntil n action = recovering policy [handler] (\_status -> void action)
116+ where policy = constantDelay 1000 `mappend` limitRetries (n * 1000) -- 1ms * n * 1000 tries = n seconds
117+ handler _status = Handler (\(HUnitFailure {}) -> return True)
118+
119+main :: IO ()
120+main = hspec spec
auto-update/test/Control/ReaperSpec.hsnew file+39
@@ -0,0 +1,39 @@
1+module Control.ReaperSpec (spec) where
2+
3+import Control.Concurrent
4+import Control.Reaper
5+import Data.IORef
6+import Test.Hspec
7+import Test.Hspec.QuickCheck
8+
9+
10+spec :: Spec
11+spec = return ()
12+-- prop "works" $ \is -> do
13+-- reaper <- mkReaper defaultReaperSettings
14+-- { reaperAction = action
15+-- , reaperDelay = 1000
16+-- }
17+
18+-- let mkTestCase i = do
19+-- ref <- newIORef 0
20+-- let expected = (abs i `mod` 10) + 1
21+-- reaperAdd reaper (expected, ref)
22+-- return (expected, ref)
23+-- testCases <- mapM mkTestCase is
24+
25+-- let test (expected, ref) = do
26+-- actual <- readIORef ref
27+-- actual `shouldBe` (expected :: Int)
28+-- threadDelay 100000
29+-- mapM_ test testCases
30+-- [] <- reaperRead reaper
31+-- return ()
32+
33+-- type Item = (Int, IORef Int)
34+
35+-- action = mkListAction $ \(i, ref) -> do
36+-- modifyIORef ref succ
37+-- return $ if i > 1
38+-- then Just (pred i, ref)
39+-- else Nothing
wai-test/test/Spec.hs → auto-update/test/Spec.hsrenamed

The content is the same.

mime-types/ChangeLog.mdnew file+15
@@ -0,0 +1,15 @@
1+## 0.1.0.9
2+
3+* Add mjs mime type
4+
5+## 0.1.0.8
6+
7+* Add wasm mime type
8+
9+## 0.1.0.7
10+
11+* Add support for .less files [#534](https://github.com/yesodweb/wai/pull/534)
12+
13+## 0.1.0.6
14+
15+* Add woff2 mime type [#350](https://github.com/yesodweb/wai/pull/350)
mime-types/Network/Mime.hs+8
@@ -468,6 +468,11 @@ defaultMimeMap = Map.fromAscList [
468 , ("lbd", "application/vnd.llamagraphics.life-balance.desktop")
468 , ("lbd", "application/vnd.llamagraphics.life-balance.desktop")
469 , ("lbe", "application/vnd.llamagraphics.life-balance.exchange+xml")
469 , ("lbe", "application/vnd.llamagraphics.life-balance.exchange+xml")
470 , ("les", "application/vnd.hhe.lesson-player")
470 , ("les", "application/vnd.hhe.lesson-player")
471+
472+ -- Added after deliberation in PR (https://github.com/yesodweb/wai/pull/534)
473+ -- Accepted mainly because of StackOverflow (http://stackoverflow.com/questions/7319555/how-to-add-less-to-iis-7-0)
474+ , ("less", "text/css")
475+
471 , ("lha", "application/x-lzh-compressed")
476 , ("lha", "application/x-lzh-compressed")
472 , ("link66", "application/vnd.route66.link66+xml")
477 , ("link66", "application/vnd.route66.link66+xml")
473 , ("list", "text/plain")
478 , ("list", "text/plain")
@@ -526,6 +531,7 @@ defaultMimeMap = Map.fromAscList [
526 , ("mime", "message/rfc822")
531 , ("mime", "message/rfc822")
527 , ("mj2", "video/mj2")
532 , ("mj2", "video/mj2")
528 , ("mjp2", "video/mj2")
533 , ("mjp2", "video/mj2")
534+ , ("mjs", "application/javascript")
529 , ("mk3d", "video/x-matroska")
535 , ("mk3d", "video/x-matroska")
530 , ("mka", "audio/x-matroska")
536 , ("mka", "audio/x-matroska")
531 , ("mks", "video/x-matroska")
537 , ("mks", "video/x-matroska")
@@ -974,6 +980,7 @@ defaultMimeMap = Map.fromAscList [
974 , ("w3d", "application/x-director")
980 , ("w3d", "application/x-director")
975 , ("wad", "application/x-doom")
981 , ("wad", "application/x-doom")
976 , ("war", "application/java-archive")
982 , ("war", "application/java-archive")
983+ , ("wasm", "application/wasm")
977 , ("wav", "audio/x-wav")
984 , ("wav", "audio/x-wav")
978 , ("wax", "audio/x-ms-wax")
985 , ("wax", "audio/x-ms-wax")
979 , ("wbmp", "image/vnd.wap.wbmp")
986 , ("wbmp", "image/vnd.wap.wbmp")
@@ -1000,6 +1007,7 @@ defaultMimeMap = Map.fromAscList [
1000 , ("wmx", "video/x-ms-wmx")
1007 , ("wmx", "video/x-ms-wmx")
1001 , ("wmz", "application/x-msmetafile")
1008 , ("wmz", "application/x-msmetafile")
1002 , ("woff", "application/font-woff")
1009 , ("woff", "application/font-woff")
1010+ , ("woff2", "application/font-woff2")
1003 , ("wpd", "application/vnd.wordperfect")
1011 , ("wpd", "application/vnd.wordperfect")
1004 , ("wpl", "application/vnd.ms-wpl")
1012 , ("wpl", "application/vnd.ms-wpl")
1005 , ("wps", "application/vnd.ms-works")
1013 , ("wps", "application/vnd.ms-works")
mime-types/README.mdnew file+3
@@ -0,0 +1,3 @@
1+## mime-types
2+
3+Basic mime-type handling types and functions
mime-types/mime-types.cabal+3−2
@@ -1,7 +1,7 @@
1name: mime-types
1name: mime-types
2−version: 0.1.0.4
2+version: 0.1.0.9
3synopsis: Basic mime-type handling types and functions
3synopsis: Basic mime-type handling types and functions
4−description: Basic mime-type handling types and functions
4+description: API docs and the README are available at <http://www.stackage.org/package/mime-types>.
5homepage: https://github.com/yesodweb/wai
5homepage: https://github.com/yesodweb/wai
6license: MIT
6license: MIT
7license-file: LICENSE
7license-file: LICENSE
@@ -10,6 +10,7 @@ maintainer: michael@snoyman.com
10category: Web
10category: Web
11build-type: Simple
11build-type: Simple
12cabal-version: >=1.8
12cabal-version: >=1.8
13+extra-source-files: README.md ChangeLog.md
13
14
14library
15library
15 exposed-modules: Network.Mime
16 exposed-modules: Network.Mime
sources.txtdeleted−12
@@ -1,12 +0,0 @@
1−./wai
2−./wai-test
3−./wai-extra
4−./warp
5−./warp-tls
6−./wai-app-static
7−./wai-handler-fastcgi
8−./wai-handler-launch
9−./wai-frontend-monadcgi
10−./wai-websockets
11−./wai-eventsource
12−./mime-types
stack-lts-11.yamlnew file+27
@@ -0,0 +1,27 @@
1+resolver: lts-11.21
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+extra-deps:
17+- streaming-commons-0.2.1.0@sha256:163af02944f374f316e35257c1afbffefd2c2852a85a151d1ea0ad22152cf28d,4990
18+- http2-2.0.0
19+- network-byte-order-0.1.1.0
20+- tls-1.5.3
21+- tls-session-manager-0.0.4
22+- x509-1.7.5
23+nix:
24+ enable: false
25+ packages:
26+ - fcgi
27+ - zlib
stack-lts-12.yamlnew file+26
@@ -0,0 +1,26 @@
1+resolver: lts-12.6
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+nix:
17+ enable: false
18+ packages:
19+ - fcgi
20+ - zlib
21+extra-deps:
22+- http2-2.0.0
23+- network-byte-order-0.1.1.0
24+- tls-1.5.3
25+- tls-session-manager-0.0.4
26+- x509-1.7.5
stack-nightly.yamlnew file+20
@@ -0,0 +1,20 @@
1+resolver: nightly-2020-01-08
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+nix:
17+ enable: false
18+ packages:
19+ - fcgi
20+ - zlib
stack.yamlnew file+25
@@ -0,0 +1,25 @@
1+resolver: lts-14.22
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+nix:
17+ enable: false
18+ packages:
19+ - fcgi
20+ - zlib
21+extra-deps:
22+- http2-2.0.0
23+- network-byte-order-0.1.1.0
24+- tls-1.5.3
25+- tls-session-manager-0.0.4
wai-eventsource/LICENSE → time-manager/LICENSErenamed

The content is the same.

time-manager/System/TimeManager.hsnew file+152
@@ -0,0 +1,152 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+
3+module System.TimeManager (
4+ -- ** Types
5+ Manager
6+ , TimeoutAction
7+ , Handle
8+ -- ** Manager
9+ , initialize
10+ , stopManager
11+ , killManager
12+ , withManager
13+ -- ** Registration
14+ , register
15+ , registerKillThread
16+ -- ** Control
17+ , tickle
18+ , cancel
19+ , pause
20+ , resume
21+ -- ** Exceptions
22+ , TimeoutThread (..)
23+ ) where
24+
25+import Control.Concurrent (myThreadId)
26+import qualified Control.Exception as E
27+import Control.Reaper
28+import Data.Typeable (Typeable)
29+import Data.IORef (IORef)
30+import qualified Data.IORef as I
31+
32+----------------------------------------------------------------
33+
34+-- | A timeout manager
35+type Manager = Reaper [Handle] Handle
36+
37+-- | An action to be performed on timeout.
38+type TimeoutAction = IO ()
39+
40+-- | A handle used by 'Manager'
41+data Handle = Handle !(IORef TimeoutAction) !(IORef State)
42+
43+data State = Active -- Manager turns it to Inactive.
44+ | Inactive -- Manager removes it with timeout action.
45+ | Paused -- Manager does not change it.
46+ | Canceled -- Manager removes it without timeout action.
47+
48+----------------------------------------------------------------
49+
50+-- | Creating timeout manager which works every N micro seconds
51+-- where N is the first argument.
52+initialize :: Int -> IO Manager
53+initialize timeout = mkReaper defaultReaperSettings
54+ { reaperAction = mkListAction prune
55+ , reaperDelay = timeout
56+ }
57+ where
58+ prune m@(Handle actionRef stateRef) = do
59+ state <- I.atomicModifyIORef' stateRef (\x -> (inactivate x, x))
60+ case state of
61+ Inactive -> do
62+ onTimeout <- I.readIORef actionRef
63+ onTimeout `E.catch` ignoreAll
64+ return Nothing
65+ Canceled -> return Nothing
66+ _ -> return $ Just m
67+
68+ inactivate Active = Inactive
69+ inactivate x = x
70+
71+----------------------------------------------------------------
72+
73+-- | Stopping timeout manager with onTimeout fired.
74+stopManager :: Manager -> IO ()
75+stopManager mgr = E.mask_ (reaperStop mgr >>= mapM_ fire)
76+ where
77+ fire (Handle actionRef _) = do
78+ onTimeout <- I.readIORef actionRef
79+ onTimeout `E.catch` ignoreAll
80+
81+ignoreAll :: E.SomeException -> IO ()
82+ignoreAll _ = return ()
83+
84+-- | Killing timeout manager immediately without firing onTimeout.
85+killManager :: Manager -> IO ()
86+killManager = reaperKill
87+
88+----------------------------------------------------------------
89+
90+-- | Registering a timeout action.
91+register :: Manager -> TimeoutAction -> IO Handle
92+register mgr onTimeout = do
93+ actionRef <- I.newIORef onTimeout
94+ stateRef <- I.newIORef Active
95+ let h = Handle actionRef stateRef
96+ reaperAdd mgr h
97+ return h
98+
99+-- | Registering a timeout action of killing this thread.
100+registerKillThread :: Manager -> TimeoutAction -> IO Handle
101+registerKillThread m onTimeout = do
102+ -- If we hold ThreadId, the stack and data of the thread is leaked.
103+ -- If we hold Weak ThreadId, the stack is released. However, its
104+ -- data is still leaked probably because of a bug of GHC.
105+ -- So, let's just use ThreadId and release ThreadId by
106+ -- overriding the timeout action by "cancel".
107+ tid <- myThreadId
108+ -- First run the timeout action in case the child thread is masked.
109+ register m $ onTimeout `E.finally` E.throwTo tid TimeoutThread
110+
111+data TimeoutThread = TimeoutThread
112+ deriving Typeable
113+instance E.Exception TimeoutThread where
114+ toException = E.asyncExceptionToException
115+ fromException = E.asyncExceptionFromException
116+instance Show TimeoutThread where
117+ show TimeoutThread = "Thread killed by timeout manager"
118+
119+----------------------------------------------------------------
120+
121+-- | Setting the state to active.
122+-- 'Manager' turns active to inactive repeatedly.
123+tickle :: Handle -> IO ()
124+tickle (Handle _ stateRef) = I.writeIORef stateRef Active
125+
126+-- | Setting the state to canceled.
127+-- 'Manager' eventually removes this without timeout action.
128+cancel :: Handle -> IO ()
129+cancel (Handle actionRef stateRef) = do
130+ I.writeIORef actionRef (return ()) -- ensuring to release ThreadId
131+ I.writeIORef stateRef Canceled
132+
133+-- | Setting the state to paused.
134+-- 'Manager' does not change the value.
135+pause :: Handle -> IO ()
136+pause (Handle _ stateRef) = I.writeIORef stateRef Paused
137+
138+-- | Setting the paused state to active.
139+-- This is an alias to 'tickle'.
140+resume :: Handle -> IO ()
141+resume = tickle
142+
143+----------------------------------------------------------------
144+
145+-- | Call the inner function with a timeout manager.
146+withManager :: Int -- ^ timeout in microseconds
147+ -> (Manager -> IO a)
148+ -> IO a
149+withManager timeout f = do
150+ -- FIXME when stopManager is available, use it
151+ man <- initialize timeout
152+ f man
time-manager/time-manager.cabalnew file+19
@@ -0,0 +1,19 @@
1+Name: time-manager
2+Version: 0.0.0
3+Synopsis: Scalable timer
4+License: MIT
5+License-file: LICENSE
6+Author: Michael Snoyman and Kazu Yamamoto
7+Maintainer: kazu@iij.ad.jp
8+Homepage: http://github.com/yesodweb/wai
9+Category: System
10+Build-Type: Simple
11+Cabal-Version: >=1.8
12+Stability: Stable
13+Description: Scalable timer functions provided by a timer manager.
14+
15+Library
16+ Build-Depends: base >= 4.8 && < 5
17+ , auto-update
18+ Exposed-modules: System.TimeManager
19+ Ghc-Options: -Wall
wai-app-static/.ghci+1−1
@@ -1 +1 @@
1−:set -itest
1+:set -itest -optP-include -optPdist/build/autogen/cabal_macros.h -XCPP
wai-app-static/ChangeLog.mdnew file+76
@@ -0,0 +1,76 @@
1+# wai-app-static changelog
2+
3+## 3.1.7.1
4+
5+* Update the test suite too
6+
7+## 3.1.7
8+
9+* Use 302 instead of 301 redirect, to avoid caching the presence of an index.html file
10+
11+## 3.1.6.3
12+
13+* The executable warp obeys `-h` option properly for host
14+now. Previously this used to invoke the help option. That can be
15+reached via `--help` as before.
16+
17+## 3.1.6.2
18+
19+* Drop dependency on blaze-builder
20+
21+## 3.1.6.1
22+
23+* Add `<>` import
24+
25+## 3.1.6
26+
27+* Make ssAddTrailingSlash work in combination with ssIndices [#569](https://github.com/yesodweb/wai/pull/569)
28+* Make ssIndices work with ssLookupFile and trailing slashes [#570](https://github.com/yesodweb/wai/pull/570)
29+
30+## 3.1.5
31+
32+* Switch to cryponite
33+
34+## 3.1.4.1
35+
36+* Support wai/warp 3.2
37+
38+## 3.1.4
39+
40+* Reinstate redirectToIndex
41+
42+## 3.1.3
43+
44+* Add 404 handler [#467](https://github.com/yesodweb/wai/pull/467)
45+
46+## 3.1.2
47+
48+* Honor ssIndices when used with defaultWebAppSettings [#460](https://github.com/yesodweb/wai/pull/460)
49+
50+## 3.1.1
51+
52+* Make adding a trailing slash optional [#327](https://github.com/yesodweb/wai/issues/327) [yesod#988](https://github.com/yesodweb/yesod/issues/988)
53+
54+## 3.1.0
55+
56+* Drop system-filepath
57+
58+## 3.0.1.1
59+
60+* Fix root links
61+
62+## 3.0.1
63+
64+* Better HEAD support [#354](https://github.com/yesodweb/wai/issues/354)
65+
66+## 3.0.0.6
67+
68+Fix trailing slashes for `UrlMap` and other non-root setups [#325](https://github.com/yesodweb/wai/issues/325)
69+
70+## 3.0.0.4
71+
72+Add missing trailing slashes [#312](https://github.com/yesodweb/wai/issues/312)
73+
74+## 3.0.0.3
75+
76+Support for time 1.5
wai-app-static/Network/Wai/Application/Static.hs+104−59
@@ -2,6 +2,7 @@
2{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE TemplateHaskell, CPP #-}
3{-# LANGUAGE TemplateHaskell, CPP #-}
4{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE PatternGuards #-}
5-- | Static file serving for WAI.
6-- | Static file serving for WAI.
6module Network.Wai.Application.Static
7module Network.Wai.Application.Static
7 ( -- * WAI application
8 ( -- * WAI application
@@ -20,6 +21,8 @@ module Network.Wai.Application.Static
20 , ssIndices
21 , ssIndices
21 , ssMaxAge
22 , ssMaxAge
22 , ssRedirectToIndex
23 , ssRedirectToIndex
24+ , ssAddTrailingSlash
25+ , ss404Handler
23 ) where
26 ) where
24
27
25import Prelude hiding (FilePath)
28import Prelude hiding (FilePath)
@@ -31,16 +34,15 @@ import qualified Data.ByteString.Lazy as L
31import Data.ByteString.Lazy.Char8 ()
34import Data.ByteString.Lazy.Char8 ()
32import Control.Monad.IO.Class (liftIO)
35import Control.Monad.IO.Class (liftIO)
33
36
34−import Blaze.ByteString.Builder (toByteString)
37+import Data.ByteString.Builder (toLazyByteString)
35
38
36import Data.FileEmbed (embedFile)
39import Data.FileEmbed (embedFile)
37
40
38import Data.Text (Text)
41import Data.Text (Text)
39import qualified Data.Text as T
42import qualified Data.Text as T
43+import qualified Data.Text.Encoding as TE
40
44
41−import Data.Either (rights)
42import Network.HTTP.Date (parseHTTPDate, epochTimeToHTTPDate, formatHTTPDate)
45import Network.HTTP.Date (parseHTTPDate, epochTimeToHTTPDate, formatHTTPDate)
43−import Data.Monoid (First (First, getFirst), mconcat)
44
46
45import WaiAppStatic.Types
47import WaiAppStatic.Types
46import Util
48import Util
@@ -51,6 +53,7 @@ import Network.Mime (MimeType)
51data StaticResponse =
53data StaticResponse =
52 -- | Just the etag hash or Nothing for no etag hash
54 -- | Just the etag hash or Nothing for no etag hash
53 Redirect Pieces (Maybe ByteString)
55 Redirect Pieces (Maybe ByteString)
56+ | RawRedirect ByteString
54 | NotFound
57 | NotFound
55 | FileResponse File H.ResponseHeaders
58 | FileResponse File H.ResponseHeaders
56 | NotModified
59 | NotModified
@@ -71,37 +74,27 @@ filterButLast f (x:xs)
71
74
72-- | Serve an appropriate response for a folder request.
75-- | Serve an appropriate response for a folder request.
73serveFolder :: StaticSettings -> Pieces -> W.Request -> Folder -> IO StaticResponse
76serveFolder :: StaticSettings -> Pieces -> W.Request -> Folder -> IO StaticResponse
74−serveFolder ss@StaticSettings {..} pieces req folder@Folder {..} =
77+serveFolder StaticSettings {..} pieces req folder@Folder {..} =
75− -- first check if there is an index file in this folder
76− case getFirst $ mconcat $ map (findIndex $ rights folderContents) ssIndices of
77− Just index -> do
78− let pieces' = setLast pieces index
79− in if ssRedirectToIndex
80− then return $ Redirect pieces' Nothing
81− -- start the checking process over, with a new set
82− else checkPieces ss pieces' req
83− Nothing ->
84− case ssListing of
78+ case ssListing of
79+ Just _ | Just path <- addTrailingSlash req, ssAddTrailingSlash ->
80+ return $ RawRedirect path
85− Just listing -> do
81+ Just listing -> do
86− -- directory listings turned on, display it
82+ -- directory listings turned on, display it
87− builder <- listing pieces folder
83+ builder <- listing pieces folder
88− return $ WaiResponse $ W.responseBuilder H.status200
84+ return $ WaiResponse $ W.responseBuilder H.status200
89− [ ("Content-Type", "text/html; charset=utf-8")
85+ [ ("Content-Type", "text/html; charset=utf-8")
90− ] builder
86+ ] builder
91− Nothing -> return $ WaiResponse $ W.responseLBS H.status403
87+ Nothing -> return $ WaiResponse $ W.responseLBS H.status403
92− [ ("Content-Type", "text/plain")
88+ [ ("Content-Type", "text/plain")
93− ] "Directory listings disabled"
89+ ] "Directory listings disabled"
94− where
95− setLast :: Pieces -> Piece -> Pieces
96− setLast [] x = [x]
97− setLast [t] x
98− | fromPiece t == "" = [x]
99− setLast (a:b) x = a : setLast b x
100
90
101− findIndex :: [File] -> Piece -> First Piece
91+addTrailingSlash :: W.Request -> Maybe ByteString
102− findIndex files index
92+addTrailingSlash req
103− | index `elem` map fileName files = First $ Just index
93+ | S8.null rp = Just "/"
104− | otherwise = First Nothing
94+ | S8.last rp == '/' = Nothing
95+ | otherwise = Just $ S8.snoc rp '/'
96+ where
97+ rp = W.rawPathInfo req
105
98
106checkPieces :: StaticSettings
99checkPieces :: StaticSettings
107 -> Pieces -- ^ parsed request
100 -> Pieces -- ^ parsed request
@@ -113,11 +106,51 @@ checkPieces _ pieces _ | any (T.null . fromPiece) $ safeInit pieces =
113 return $ Redirect (filterButLast (not . T.null . fromPiece) pieces) Nothing
106 return $ Redirect (filterButLast (not . T.null . fromPiece) pieces) Nothing
114
107
115checkPieces ss@StaticSettings {..} pieces req = do
108checkPieces ss@StaticSettings {..} pieces req = do
116− res <- ssLookupFile pieces
109+ res <- lookupResult
117 case res of
110 case res of
111+ Left location -> return $ RawRedirect location
118− LRNotFound -> return NotFound
112+ Right LRNotFound -> return NotFound
119− LRFile file -> serveFile ss req file
113+ Right (LRFile file) -> serveFile ss req file
120− LRFolder folder -> serveFolder ss pieces req folder
114+ Right (LRFolder folder) -> serveFolder ss pieces req folder
115+ where
116+ lookupResult :: IO (Either ByteString LookupResult)
117+ lookupResult = do
118+ nonIndexResult <- ssLookupFile pieces
119+ case nonIndexResult of
120+ LRFile{} -> return $ Right nonIndexResult
121+ _ -> do
122+ eIndexResult <- lookupIndices (map (\ index -> dropLastIfNull pieces ++ [index]) ssIndices)
123+ return $ case eIndexResult of
124+ Left redirect -> Left redirect
125+ Right indexResult -> case indexResult of
126+ LRNotFound -> Right nonIndexResult
127+ LRFile file | ssRedirectToIndex ->
128+ let relPath =
129+ case reverse pieces of
130+ -- Served at root
131+ [] -> fromPiece $ fileName file
132+ lastSegment:_ ->
133+ case fromPiece lastSegment of
134+ -- Ends with a trailing slash
135+ "" -> fromPiece $ fileName file
136+ -- Lacks a trailing slash
137+ lastSegment' -> T.concat
138+ [ lastSegment'
139+ , "/"
140+ , fromPiece $ fileName file
141+ ]
142+ in Left $ TE.encodeUtf8 relPath
143+ _ -> Right indexResult
144+
145+ lookupIndices :: [Pieces] -> IO (Either ByteString LookupResult)
146+ lookupIndices (x : xs) = do
147+ res <- ssLookupFile x
148+ case res of
149+ LRNotFound -> lookupIndices xs
150+ _ -> return $ case (ssAddTrailingSlash, addTrailingSlash req) of
151+ (True, Just redirect) -> Left redirect
152+ _ -> Right res
153+ lookupIndices [] = return $ Right LRNotFound
121
154
122serveFile :: StaticSettings -> W.Request -> File -> IO StaticResponse
155serveFile :: StaticSettings -> W.Request -> File -> IO StaticResponse
123serveFile StaticSettings {..} req file
156serveFile StaticSettings {..} req file
@@ -131,11 +164,13 @@ serveFile StaticSettings {..} req file
131 -- Didn't match, but we have a hash value. Send the file contents
164 -- Didn't match, but we have a hash value. Send the file contents
132 -- with an ETag header.
165 -- with an ETag header.
133 --
166 --
134− -- Note: It would be arguably better to next check
167+ -- RFC7232 (HTTP 1.1):
135− -- if-modified-since and return a 304 if that indicates a match as
168+ -- > A recipient MUST ignore If-Modified-Since if the request contains an
136− -- well. However, the circumstances under which such a situation
169+ -- > If-None-Match header field; the condition in If-None-Match is
137− -- could arise would be very anomolous, and should likely warrant a
170+ -- > considered to be a more accurate replacement for the condition in
138− -- new file being sent anyway.
171+ -- > If-Modified-Since, and the two are only combined for the sake of
172+ -- > interoperating with older intermediaries that might not implement
173+ -- > If-None-Match.
139 (Just hash, _) -> respond [("ETag", hash)]
174 (Just hash, _) -> respond [("ETag", hash)]
140
175
141 -- No hash value available, fall back to last modified support.
176 -- No hash value available, fall back to last modified support.
@@ -193,52 +228,62 @@ staticApp :: StaticSettings -> W.Application
193staticApp set req = staticAppPieces set (W.pathInfo req) req
228staticApp set req = staticAppPieces set (W.pathInfo req) req
194
229
195staticAppPieces :: StaticSettings -> [Text] -> W.Application
230staticAppPieces :: StaticSettings -> [Text] -> W.Application
196−staticAppPieces _ _ req
231+staticAppPieces _ _ req sendResponse
197− | W.requestMethod req /= "GET" = return $ W.responseLBS
232+ | notElem (W.requestMethod req) ["GET", "HEAD"] = sendResponse $ W.responseLBS
198 H.status405
233 H.status405
199 [("Content-Type", "text/plain")]
234 [("Content-Type", "text/plain")]
200− "Only GET is supported"
235+ "Only GET or HEAD is supported"
201−staticAppPieces _ [".hidden", "folder.png"] _ = return $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/folder.png")]
236+staticAppPieces _ [".hidden", "folder.png"] _ sendResponse = sendResponse $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/folder.png")]
202−staticAppPieces _ [".hidden", "haskell.png"] _ = return $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/haskell.png")]
237+staticAppPieces _ [".hidden", "haskell.png"] _ sendResponse = sendResponse $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/haskell.png")]
203−staticAppPieces ss rawPieces req = liftIO $ do
238+staticAppPieces ss rawPieces req sendResponse = liftIO $ do
204 case toPieces rawPieces of
239 case toPieces rawPieces of
205 Just pieces -> checkPieces ss pieces req >>= response
240 Just pieces -> checkPieces ss pieces req >>= response
206− Nothing -> return $ W.responseLBS H.status403
241+ Nothing -> sendResponse $ W.responseLBS H.status403
207 [ ("Content-Type", "text/plain")
242 [ ("Content-Type", "text/plain")
208 ] "Forbidden"
243 ] "Forbidden"
209 where
244 where
210− response :: StaticResponse -> IO W.Response
245+ response :: StaticResponse -> IO W.ResponseReceived
211 response (FileResponse file ch) = do
246 response (FileResponse file ch) = do
212 mimetype <- ssGetMimeType ss file
247 mimetype <- ssGetMimeType ss file
213− let filesize = fileGetSize file
248+ -- let filesize = fileGetSize file
214 let headers = ("Content-Type", mimetype)
249 let headers = ("Content-Type", mimetype)
250+ -- Let Warp provide the content-length, since it takes
251+ -- range requests into account
215− : ("Content-Length", S8.pack $ show filesize)
252+ -- : ("Content-Length", S8.pack $ show filesize)
216 : ch
253 : ch
217− return $ fileToResponse file H.status200 headers
254+ sendResponse $ fileToResponse file H.status200 headers
218
255
219 response NotModified =
256 response NotModified =
220− return $ W.responseLBS H.status304 [] ""
257+ sendResponse $ W.responseLBS H.status304 [] ""
221
258
222 response (SendContent mt lbs) = do
259 response (SendContent mt lbs) = do
223 -- TODO: set caching headers
260 -- TODO: set caching headers
224− return $ W.responseLBS H.status200
261+ sendResponse $ W.responseLBS H.status200
225 [ ("Content-Type", mt)
262 [ ("Content-Type", mt)
226 -- TODO: set Content-Length
263 -- TODO: set Content-Length
227 ] lbs
264 ] lbs
228
265
229 response (Redirect pieces' mHash) = do
266 response (Redirect pieces' mHash) = do
230− let loc = (ssMkRedirect ss) pieces' $ toByteString (H.encodePathSegments $ map fromPiece pieces')
267+ let loc = ssMkRedirect ss pieces' $ L.toStrict $ toLazyByteString (H.encodePathSegments $ map fromPiece pieces')
231 let qString = case mHash of
268 let qString = case mHash of
232 Just hash -> replace "etag" (Just hash) (W.queryString req)
269 Just hash -> replace "etag" (Just hash) (W.queryString req)
233 Nothing -> remove "etag" (W.queryString req)
270 Nothing -> remove "etag" (W.queryString req)
234
271
235− return $ W.responseLBS H.status301
272+ sendResponse $ W.responseLBS H.status302
236 [ ("Content-Type", "text/plain")
273 [ ("Content-Type", "text/plain")
237 , ("Location", S8.append loc $ H.renderQuery True qString)
274 , ("Location", S8.append loc $ H.renderQuery True qString)
238 ] "Redirect"
275 ] "Redirect"
239
276
277+ response (RawRedirect path) =
278+ sendResponse $ W.responseLBS H.status302
279+ [ ("Content-Type", "text/plain")
280+ , ("Location", path)
281+ ] "Redirect"
282+
283+ response NotFound = case (ss404Handler ss) of
284+ Just app -> app req sendResponse
240− response NotFound = return $ W.responseLBS H.status404
285+ Nothing -> sendResponse $ W.responseLBS H.status404
241 [ ("Content-Type", "text/plain")
286 [ ("Content-Type", "text/plain")
242 ] "File not found"
287 ] "File not found"
243
288
244− response (WaiResponse r) = return r
289+ response (WaiResponse r) = sendResponse r
wai-app-static/README.mdnew file+5
@@ -0,0 +1,5 @@
1+## wai-app-static
2+
3+WAI application for static serving
4+
5+Also provides some helper functions and datatypes for use outside of WAI.
wai-app-static/Util.hs+8−1
@@ -1,9 +1,10 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, ViewPatterns #-}
2module Util
2module Util
3 ( relativeDirFromPieces
3 ( relativeDirFromPieces
4 , defaultMkRedirect
4 , defaultMkRedirect
5 , replace
5 , replace
6 , remove
6 , remove
7+ , dropLastIfNull
7 ) where
8 ) where
8
9
9import WaiAppStatic.Types
10import WaiAppStatic.Types
@@ -36,3 +37,9 @@ defaultMkRedirect pieces newPath
36 | otherwise = relDir `S8.append` S8.tail newPath
37 | otherwise = relDir `S8.append` S8.tail newPath
37 where
38 where
38 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
39 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
40+
41+dropLastIfNull :: [Piece] -> [Piece]
42+dropLastIfNull pieces = case pieces of
43+ [fromPiece -> ""] -> []
44+ (a : r) -> a : dropLastIfNull r
45+ [] -> []
wai-app-static/WaiAppStatic/CmdLine.hs+22−9
@@ -8,13 +8,12 @@ module WaiAppStatic.CmdLine
8import Network.Wai (Middleware)
8import Network.Wai (Middleware)
9import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
9import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
10import Network.Wai.Handler.Warp
10import Network.Wai.Handler.Warp
11− ( runSettings, defaultSettings, settingsHost, settingsPort
11+ ( runSettings, defaultSettings, setHost, setPort
12 )
12 )
13import Options.Applicative
13import Options.Applicative
14import Text.Printf (printf)
14import Text.Printf (printf)
15import System.Directory (canonicalizePath)
15import System.Directory (canonicalizePath)
16import Control.Monad (unless)
16import Control.Monad (unless)
17−import Network.Wai.Middleware.Autohead
18import Network.Wai.Middleware.RequestLogger (logStdout)
17import Network.Wai.Middleware.RequestLogger (logStdout)
19import Network.Wai.Middleware.Gzip
18import Network.Wai.Middleware.Gzip
20import qualified Data.Map as Map
19import qualified Data.Map as Map
@@ -26,6 +25,7 @@ import Network.Mime (defaultMimeMap, mimeByExt, defaultMimeType)
26import WaiAppStatic.Types (ssIndices, toPiece, ssGetMimeType, fileName, fromPiece)
25import WaiAppStatic.Types (ssIndices, toPiece, ssGetMimeType, fileName, fromPiece)
27import Data.Maybe (mapMaybe)
26import Data.Maybe (mapMaybe)
28import Control.Arrow (second)
27import Control.Arrow (second)
28+import Data.Monoid ((<>))
29
29
30data Args = Args
30data Args = Args
31 { docroot :: FilePath
31 { docroot :: FilePath
@@ -38,6 +38,13 @@ data Args = Args
38 , host :: String
38 , host :: String
39 }
39 }
40
40
41+#if MIN_VERSION_optparse_applicative(0, 10, 0)
42+option' :: Mod OptionFields Int -> Parser Int
43+option' = option auto
44+#else
45+option' = option
46+#endif
47+
41args :: Parser Args
48args :: Parser Args
42args = Args
49args = Args
43 <$> strOption
50 <$> strOption
@@ -52,7 +59,7 @@ args = Args
52 <> metavar "INDEX"
59 <> metavar "INDEX"
53 <> help "index files to serve when a directory is required"
60 <> help "index files to serve when a directory is required"
54 )))
61 )))
55− <*> option
62+ <*> option'
56 ( long "port"
63 ( long "port"
57 <> short 'p'
64 <> short 'p'
58 <> metavar "PORT"
65 <> metavar "PORT"
@@ -87,19 +94,25 @@ args = Args
87-- Since 2.0.1
94-- Since 2.0.1
88runCommandLine :: (Args -> Middleware) -> IO ()
95runCommandLine :: (Args -> Middleware) -> IO ()
89runCommandLine middleware = do
96runCommandLine middleware = do
90− args@Args {..} <- execParser $ info (helper <*> args) fullDesc
97+ args@Args {..} <- execParser $ info (helperOption <*> args) fullDesc
91 let mime' = map (pack *** S8.pack) mime
98 let mime' = map (pack *** S8.pack) mime
92 let mimeMap = Map.fromList mime' `Map.union` defaultMimeMap
99 let mimeMap = Map.fromList mime' `Map.union` defaultMimeMap
93 docroot' <- canonicalizePath docroot
100 docroot' <- canonicalizePath docroot
94 unless quiet $ printf "Serving directory %s on port %d with %s index files.\n" docroot' port (if noindex then "no" else show index)
101 unless quiet $ printf "Serving directory %s on port %d with %s index files.\n" docroot' port (if noindex then "no" else show index)
95 let middle = gzip def { gzipFiles = GzipCompress }
102 let middle = gzip def { gzipFiles = GzipCompress }
96 . (if verbose then logStdout else id)
103 . (if verbose then logStdout else id)
97− . autohead
98 . (middleware args)
104 . (middleware args)
99− runSettings defaultSettings
105+ runSettings
100− { settingsPort = port
106+ ( setPort port
101− , settingsHost = fromString host
107+ $ setHost (fromString host)
108+ defaultSettings
109+ )
102− } $ middle $ staticApp (defaultFileServerSettings $ fromString docroot)
110+ $ middle $ staticApp (defaultFileServerSettings $ fromString docroot)
103 { ssIndices = if noindex then [] else mapMaybe (toPiece . pack) index
111 { ssIndices = if noindex then [] else mapMaybe (toPiece . pack) index
104 , ssGetMimeType = return . mimeByExt mimeMap defaultMimeType . fromPiece . fileName
112 , ssGetMimeType = return . mimeByExt mimeMap defaultMimeType . fromPiece . fileName
105 }
113 }
114+ where
115+ helperOption :: Parser (a -> a)
116+ helperOption =
117+ abortOption ShowHelpText $
118+ mconcat [long "help", help "Show this help text", hidden]
wai-app-static/WaiAppStatic/Listing.hs+37−13
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE ViewPatterns #-}
3module WaiAppStatic.Listing
4module WaiAppStatic.Listing
4 ( defaultListing
5 ( defaultListing
5 ) where
6 ) where
@@ -11,7 +12,9 @@ import qualified Data.Text as T
11import Data.Time
12import Data.Time
12import Data.Time.Clock.POSIX
13import Data.Time.Clock.POSIX
13import WaiAppStatic.Types
14import WaiAppStatic.Types
15+#if !MIN_VERSION_time(1,5,0)
14import System.Locale (defaultTimeLocale)
16import System.Locale (defaultTimeLocale)
17+#endif
15import Data.List (sortBy)
18import Data.List (sortBy)
16import Util
19import Util
17
20
@@ -19,7 +22,7 @@ import qualified Text.Blaze.Html.Renderer.Utf8 as HU
19
22
20-- | Provides a default directory listing, suitable for most apps.
23-- | Provides a default directory listing, suitable for most apps.
21--
24--
22−-- Code below taken from Happstack: <http://patch-tag.com/r/mae/happstack/snapshot/current/content/pretty/happstack-server/src/Happstack/Server/FileServe/BuildingBlocks.hs>
25+-- Code below taken from Happstack: <https://github.com/Happstack/happstack-server/blob/87e6c01a65c687d06c61345430a112fc9a444a95/src/Happstack/Server/FileServe/BuildingBlocks.hs>
23defaultListing :: Listing
26defaultListing :: Listing
24defaultListing pieces (Folder contents) = do
27defaultListing pieces (Folder contents) = do
25 let isTop = null pieces || map Just pieces == [toPiece ""]
28 let isTop = null pieces || map Just pieces == [toPiece ""]
@@ -46,8 +49,12 @@ defaultListing pieces (Folder contents) = do
46 , "a { text-decoration: none }"
49 , "a { text-decoration: none }"
47 ]
50 ]
48 H.body $ do
51 H.body $ do
52+ let hasTrailingSlash =
53+ case map fromPiece $ reverse $ pieces of
54+ "":_ -> True
55+ _ -> False
49− H.h1 $ showFolder $ filter (not . T.null . fromPiece) pieces
56+ H.h1 $ showFolder' hasTrailingSlash $ filter (not . T.null . fromPiece) pieces
50− renderDirectoryContentsTable haskellSrc folderSrc fps''
57+ renderDirectoryContentsTable (map fromPiece pieces) haskellSrc folderSrc fps''
51 where
58 where
52 image x = T.unpack $ T.concat [(relativeDirFromPieces pieces), ".hidden/", x, ".png"]
59 image x = T.unpack $ T.concat [(relativeDirFromPieces pieces), ".hidden/", x, ".png"]
53 folderSrc = image "folder"
60 folderSrc = image "folder"
@@ -55,14 +62,21 @@ defaultListing pieces (Folder contents) = do
55 showName "" = "root"
62 showName "" = "root"
56 showName x = x
63 showName x = x
57
64
65+ -- Add a link to the root of the tree
66+ showFolder' :: Bool -> Pieces -> H.Html
67+ showFolder' hasTrailingSlash pieces' = showFolder hasTrailingSlash (unsafeToPiece "root" : pieces')
68+
58− showFolder :: Pieces -> H.Html
69+ showFolder :: Bool -> Pieces -> H.Html
59− showFolder [] = "/"
70+ showFolder _ [] = "/" -- won't happen
60− showFolder [x] = H.toHtml $ showName $ fromPiece x
71+ showFolder _ [x] = H.toHtml $ showName $ fromPiece x
61− showFolder (x:xs) = do
72+ showFolder hasTrailingSlash (x:xs) = do
73+ let len = length xs - (if hasTrailingSlash then 0 else 1)
74+ href
75+ | len == 0 = "."
62− let href = concat $ replicate (length xs) "../" :: String
76+ | otherwise = concat $ replicate len "../" :: String
63 H.a ! A.href (H.toValue href) $ H.toHtml $ showName $ fromPiece x
77 H.a ! A.href (H.toValue href) $ H.toHtml $ showName $ fromPiece x
64 " / " :: H.Html
78 " / " :: H.Html
65− showFolder xs
79+ showFolder hasTrailingSlash xs
66
80
67-- | a function to generate an HTML table showing the contents of a directory on the disk
81-- | a function to generate an HTML table showing the contents of a directory on the disk
68--
82--
@@ -72,11 +86,12 @@ defaultListing pieces (Folder contents) = do
72-- a new page template to wrap around this HTML.
86-- a new page template to wrap around this HTML.
73--
87--
74-- see also: 'getMetaData', 'renderDirectoryContents'
88-- see also: 'getMetaData', 'renderDirectoryContents'
75−renderDirectoryContentsTable :: String
89+renderDirectoryContentsTable :: [T.Text] -- ^ requested path info
90+ -> String
76 -> String
91 -> String
77 -> [Either FolderName File]
92 -> [Either FolderName File]
78 -> H.Html
93 -> H.Html
79−renderDirectoryContentsTable haskellSrc folderSrc fps =
94+renderDirectoryContentsTable pathInfo' haskellSrc folderSrc fps =
80 H.table $ do H.thead $ do H.th ! (A.class_ "first") $ H.img ! (A.src $ H.toValue haskellSrc)
95 H.table $ do H.thead $ do H.th ! (A.class_ "first") $ H.img ! (A.src $ H.toValue haskellSrc)
81 H.th "Name"
96 H.th "Name"
82 H.th "Modified"
97 H.th "Modified"
@@ -98,9 +113,18 @@ renderDirectoryContentsTable haskellSrc folderSrc fps =
98 Left{} -> H.img ! A.src (H.toValue folderSrc)
113 Left{} -> H.img ! A.src (H.toValue folderSrc)
99 ! A.alt "Folder"
114 ! A.alt "Folder"
100 Right{} -> return ()
115 Right{} -> return ()
116+ let name =
101− let name = either id fileName md
117+ case either id fileName md of
118+ (fromPiece -> "") -> unsafeToPiece ".."
119+ x -> x
102 let isFile = either (const False) (const True) md
120 let isFile = either (const False) (const True) md
121+ href = addCurrentDir $ fromPiece name
122+ addCurrentDir x =
123+ case reverse pathInfo' of
124+ "":_ -> x -- has a trailing slash
125+ [] -> x -- at the root
126+ currentDir:_ -> T.concat [currentDir, "/", x]
103− H.td (H.a ! A.href (H.toValue $ fromPiece name `T.append` if isFile then "" else "/") $ H.toHtml $ fromPiece name)
127+ H.td (H.a ! A.href (H.toValue href) $ H.toHtml $ fromPiece name)
104 H.td ! A.class_ "date" $ H.toHtml $
128 H.td ! A.class_ "date" $ H.toHtml $
105 case md of
129 case md of
106 Right File { fileGetModified = Just t } ->
130 Right File { fileGetModified = Just t } ->
wai-app-static/WaiAppStatic/Storage/Embedded/Runtime.hs+7−8
@@ -8,7 +8,7 @@ import WaiAppStatic.Types
8import Data.ByteString (ByteString)
8import Data.ByteString (ByteString)
9import Control.Arrow ((&&&), second)
9import Control.Arrow ((&&&), second)
10import Data.List
10import Data.List
11−import Blaze.ByteString.Builder (fromByteString)
11+import Data.ByteString.Builder (byteString)
12import qualified Network.Wai as W
12import qualified Network.Wai as W
13import qualified Data.Map as Map
13import qualified Data.Map as Map
14import Data.Function (on)
14import Data.Function (on)
@@ -16,8 +16,7 @@ import qualified Data.Text as T
16import Data.Ord
16import Data.Ord
17import qualified Data.ByteString as S
17import qualified Data.ByteString as S
18import Crypto.Hash (hash, MD5, Digest)
18import Crypto.Hash (hash, MD5, Digest)
19−import Data.Byteable (toBytes)
19+import Data.ByteArray.Encoding
20−import qualified Data.ByteString.Base64 as B64
21import WaiAppStatic.Storage.Filesystem (defaultFileServerSettings)
20import WaiAppStatic.Storage.Filesystem (defaultFileServerSettings)
22import System.FilePath (isPathSeparator)
21import System.FilePath (isPathSeparator)
23
22
@@ -50,8 +49,8 @@ embeddedLookup root pieces =
50toEntry :: (Piece, EmbeddedEntry) -> Either FolderName File
49toEntry :: (Piece, EmbeddedEntry) -> Either FolderName File
51toEntry (name, EEFolder{}) = Left name
50toEntry (name, EEFolder{}) = Left name
52toEntry (name, EEFile bs) = Right File
51toEntry (name, EEFile bs) = Right File
53− { fileGetSize = S.length bs
52+ { fileGetSize = fromIntegral $ S.length bs
54− , fileToResponse = \s h -> W.responseBuilder s h $ fromByteString bs
53+ , fileToResponse = \s h -> W.responseBuilder s h $ byteString bs
55 , fileName = name
54 , fileName = name
56 , fileGetHash = return $ Just $ runHash bs
55 , fileGetHash = return $ Just $ runHash bs
57 , fileGetModified = Nothing
56 , fileGetModified = Nothing
@@ -87,12 +86,12 @@ toEmbedded fps =
87
86
88bsToFile :: Piece -> ByteString -> File
87bsToFile :: Piece -> ByteString -> File
89bsToFile name bs = File
88bsToFile name bs = File
90− { fileGetSize = S.length bs
89+ { fileGetSize = fromIntegral $ S.length bs
91− , fileToResponse = \s h -> W.responseBuilder s h $ fromByteString bs
90+ , fileToResponse = \s h -> W.responseBuilder s h $ byteString bs
92 , fileName = name
91 , fileName = name
93 , fileGetHash = return $ Just $ runHash bs
92 , fileGetHash = return $ Just $ runHash bs
94 , fileGetModified = Nothing
93 , fileGetModified = Nothing
95 }
94 }
96
95
97runHash :: ByteString -> ByteString
96runHash :: ByteString -> ByteString
98−runHash = B64.encode . toBytes . (hash :: S.ByteString -> Digest MD5)
97+runHash = convertToBase Base64 . (hash :: S.ByteString -> Digest MD5)
wai-app-static/WaiAppStatic/Storage/Embedded/TH.hs+2−3
@@ -5,9 +5,8 @@ module WaiAppStatic.Storage.Embedded.TH(
5 , mkSettings
5 , mkSettings
6) where
6) where
7
7
8−import Blaze.ByteString.Builder.ByteString (insertByteString)
8+import Data.ByteString.Builder.Extra (byteStringInsert)
9import Codec.Compression.GZip (compress)
9import Codec.Compression.GZip (compress)
10−import Control.Applicative
11import Data.ByteString.Unsafe (unsafePackAddressLen)
10import Data.ByteString.Unsafe (unsafePackAddressLen)
12import Data.Either (lefts, rights)
11import Data.Either (lefts, rights)
13import GHC.Exts (Int(..))
12import GHC.Exts (Int(..))
@@ -130,7 +129,7 @@ embeddedToFile entry = File
130 let h' = if embCompressed entry
129 let h' = if embCompressed entry
131 then h ++ [("Content-Encoding", "gzip")]
130 then h ++ [("Content-Encoding", "gzip")]
132 else h
131 else h
133− in W.responseBuilder s h' $ insertByteString $ embContent entry
132+ in W.responseBuilder s h' $ byteStringInsert $ embContent entry
134
133
135 -- Usually the fileName should just be the filename not the entire path,
134 -- Usually the fileName should just be the filename not the entire path,
136 -- but we need the whole path to make the lookup within lookupMime
135 -- but we need the whole path to make the lookup within lookupMime
wai-app-static/WaiAppStatic/Storage/Filesystem.hs+29−26
@@ -1,5 +1,6 @@
1{-# LANGUAGE ScopedTypeVariables #-}
1{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE BangPatterns #-}
3-- | Access files on the filesystem.
4-- | Access files on the filesystem.
4module WaiAppStatic.Storage.Filesystem
5module WaiAppStatic.Storage.Filesystem
5 ( -- * Types
6 ( -- * Types
@@ -11,10 +12,9 @@ module WaiAppStatic.Storage.Filesystem
11 ) where
12 ) where
12
13
13import WaiAppStatic.Types
14import WaiAppStatic.Types
14−import Prelude hiding (FilePath)
15+import System.FilePath ((</>))
15−import Filesystem.Path.CurrentOS (FilePath, (</>))
16+import System.IO (withBinaryFile, IOMode(..))
16−import qualified Filesystem.Path.CurrentOS as F
17+import System.Directory (doesFileExist, doesDirectoryExist, getDirectoryContents)
17−import qualified Filesystem as F
18import Data.List (foldl')
18import Data.List (foldl')
19import Control.Monad (forM)
19import Control.Monad (forM)
20import Util
20import Util
@@ -25,14 +25,14 @@ import WaiAppStatic.Listing
25import Network.Mime
25import Network.Mime
26import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime, isRegularFile)
26import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime, isRegularFile)
27import Data.Maybe (catMaybes)
27import Data.Maybe (catMaybes)
28−import qualified Crypto.Hash.Conduit (hashFile)
28+import Data.ByteArray.Encoding
29−import Data.Byteable (toBytes)
30−import Crypto.Hash (MD5, Digest)
29+import Crypto.Hash (hashlazy, MD5, Digest)
31−import qualified Data.ByteString.Base64 as B64
30+import qualified Data.ByteString.Lazy as BL (hGetContents)
31+import qualified Data.Text as T
32
32
33-- | Construct a new path from a root and some @Pieces@.
33-- | Construct a new path from a root and some @Pieces@.
34pathFromPieces :: FilePath -> Pieces -> FilePath
34pathFromPieces :: FilePath -> Pieces -> FilePath
35−pathFromPieces = foldl' (\fp p -> fp </> F.fromText (fromPiece p))
35+pathFromPieces = foldl' (\fp p -> fp </> T.unpack (fromPiece p))
36
36
37-- | Settings optimized for a web application. Files will have aggressive
37-- | Settings optimized for a web application. Files will have aggressive
38-- caching applied and hashes calculated, and indices and listings are disabled.
38-- caching applied and hashes calculated, and indices and listings are disabled.
@@ -47,6 +47,8 @@ defaultWebAppSettings root = StaticSettings
47 , ssIndices = []
47 , ssIndices = []
48 , ssRedirectToIndex = False
48 , ssRedirectToIndex = False
49 , ssUseHash = True
49 , ssUseHash = True
50+ , ssAddTrailingSlash = False
51+ , ss404Handler = Nothing
50 }
52 }
51
53
52-- | Settings optimized for a file server. More conservative caching will be
54-- | Settings optimized for a file server. More conservative caching will be
@@ -62,6 +64,8 @@ defaultFileServerSettings root = StaticSettings
62 , ssIndices = map unsafeToPiece ["index.html", "index.htm"]
64 , ssIndices = map unsafeToPiece ["index.html", "index.htm"]
63 , ssRedirectToIndex = False
65 , ssRedirectToIndex = False
64 , ssUseHash = False
66 , ssUseHash = False
67+ , ssAddTrailingSlash = False
68+ , ss404Handler = Nothing
65 }
69 }
66
70
67-- | Same as @defaultWebAppSettings@, but additionally uses a specialized
71-- | Same as @defaultWebAppSettings@, but additionally uses a specialized
@@ -86,12 +90,12 @@ fileHelper :: ETagLookup
86 -> Piece -- ^ file name
90 -> Piece -- ^ file name
87 -> IO (Maybe File)
91 -> IO (Maybe File)
88fileHelper hashFunc fp name = do
92fileHelper hashFunc fp name = do
89− efs <- try $ getFileStatus $ F.encodeString fp
93+ efs <- try $ getFileStatus fp
90 case efs of
94 case efs of
91 Left (_ :: SomeException) -> return Nothing
95 Left (_ :: SomeException) -> return Nothing
92 Right fs | isRegularFile fs -> return $ Just File
96 Right fs | isRegularFile fs -> return $ Just File
93 { fileGetSize = fromIntegral $ fileSize fs
97 { fileGetSize = fromIntegral $ fileSize fs
94− , fileToResponse = \s h -> W.responseFile s h (F.encodeString fp) Nothing
98+ , fileToResponse = \s h -> W.responseFile s h fp Nothing
95 , fileName = name
99 , fileName = name
96 , fileGetHash = hashFunc fp
100 , fileGetHash = hashFunc fp
97 , fileGetModified = Just $ modificationTime fs
101 , fileGetModified = Just $ modificationTime fs
@@ -116,9 +120,10 @@ webAppLookup hashFunc prefix pieces =
116-- | MD5 hash and base64-encode the file contents. Does not check if the file
120-- | MD5 hash and base64-encode the file contents. Does not check if the file
117-- exists.
121-- exists.
118hashFile :: FilePath -> IO ByteString
122hashFile :: FilePath -> IO ByteString
119−hashFile fp = do
123+hashFile fp = withBinaryFile fp ReadMode $ \h -> do
120− h <- Crypto.Hash.Conduit.hashFile (F.encodeString fp)
124+ f <- BL.hGetContents h
121− return $ B64.encode $ toBytes (h :: Digest MD5)
125+ let !hash = hashlazy f :: Digest MD5
126+ return $ convertToBase Base64 hash
122
127
123hashFileIfExists :: ETagLookup
128hashFileIfExists :: ETagLookup
124hashFileIfExists fp = do
129hashFileIfExists fp = do
@@ -128,12 +133,9 @@ hashFileIfExists fp = do
128 Right x -> Just x
133 Right x -> Just x
129
134
130isVisible :: FilePath -> Bool
135isVisible :: FilePath -> Bool
131−isVisible =
136+isVisible ('.':_) = False
132− go . F.encodeString . F.filename
133− where
134− go ('.':_) = False
135− go "" = False
137+isVisible "" = False
136− go _ = True
138+isVisible _ = True
137
139
138-- | Get a proper @LookupResult@, checking if the path is a file or folder.
140-- | Get a proper @LookupResult@, checking if the path is a file or folder.
139-- Compare with @webAppLookup@, which only deals with files.
141-- Compare with @webAppLookup@, which only deals with files.
@@ -141,17 +143,18 @@ fileSystemLookup :: ETagLookup
141 -> FilePath -> Pieces -> IO LookupResult
143 -> FilePath -> Pieces -> IO LookupResult
142fileSystemLookup hashFunc prefix pieces = do
144fileSystemLookup hashFunc prefix pieces = do
143 let fp = pathFromPieces prefix pieces
145 let fp = pathFromPieces prefix pieces
144− fe <- F.isFile fp
146+ fe <- doesFileExist fp
145 if fe
147 if fe
146 then fileHelperLR hashFunc fp lastPiece
148 then fileHelperLR hashFunc fp lastPiece
147 else do
149 else do
148− de <- F.isDirectory fp
150+ de <- doesDirectoryExist fp
149 if de
151 if de
150 then do
152 then do
151− entries' <- fmap (filter isVisible) $ F.listDirectory fp
153+ entries' <- fmap (filter isVisible) $ getDirectoryContents fp
152− entries <- forM entries' $ \fp' -> do
154+ entries <- forM entries' $ \fpRel' -> do
153− let name = unsafeToPiece $ either id id $ F.toText $ F.filename fp'
155+ let name = unsafeToPiece $ T.pack fpRel'
154− de' <- F.isDirectory fp'
156+ fp' = fp </> fpRel'
157+ de' <- doesDirectoryExist fp'
155 if de'
158 if de'
156 then return $ Just $ Left name
159 then return $ Just $ Left name
157 else do
160 else do
wai-app-static/WaiAppStatic/Types.hs+12−3
@@ -1,4 +1,3 @@
1−{-# LANGUAGE OverloadedStrings #-}
2module WaiAppStatic.Types
1module WaiAppStatic.Types
3 ( -- * Pieces
2 ( -- * Pieces
4 Piece
3 Piece
@@ -25,7 +24,7 @@ import qualified Network.Wai as W
25import Data.ByteString (ByteString)
24import Data.ByteString (ByteString)
26import System.Posix.Types (EpochTime)
25import System.Posix.Types (EpochTime)
27import qualified Data.Text as T
26import qualified Data.Text as T
28−import Blaze.ByteString.Builder (Builder)
27+import Data.ByteString.Builder (Builder)
29import Network.Mime (MimeType)
28import Network.Mime (MimeType)
30
29
31-- | An individual component of a path, or of a filepath.
30-- | An individual component of a path, or of a filepath.
@@ -60,6 +59,8 @@ toPieces :: [Text] -> Maybe Pieces
60toPieces = mapM toPiece
59toPieces = mapM toPiece
61
60
62-- | Request coming from a user. Corresponds to @pathInfo@.
61-- | Request coming from a user. Corresponds to @pathInfo@.
62+--
63+-- The root path is the empty list.
63type Pieces = [Piece]
64type Pieces = [Piece]
64
65
65-- | Values for the max-age component of the cache-control response header.
66-- | Values for the max-age component of the cache-control response header.
@@ -79,7 +80,7 @@ data Folder = Folder
79-- | Information on an individual file.
80-- | Information on an individual file.
80data File = File
81data File = File
81 { -- | Size of file in bytes
82 { -- | Size of file in bytes
82− fileGetSize :: Int
83+ fileGetSize :: Integer
83 -- | How to construct a WAI response for this file. Some files are stored
84 -- | How to construct a WAI response for this file. Some files are stored
84 -- on the filesystem and can use @ResponseFile@, while others are stored
85 -- on the filesystem and can use @ResponseFile@, while others are stored
85 -- in memory and should use @ResponseBuilder@.
86 -- in memory and should use @ResponseBuilder@.
@@ -141,4 +142,12 @@ data StaticSettings = StaticSettings
141
142
142 -- | Prefer usage of etag caching to last-modified caching.
143 -- | Prefer usage of etag caching to last-modified caching.
143 , ssUseHash :: Bool
144 , ssUseHash :: Bool
145+
146+ -- | Force a trailing slash at the end of directories
147+ , ssAddTrailingSlash :: Bool
148+
149+ -- | Optional `W.Application` to be used in case of 404 errors
150+ --
151+ -- Since 3.1.3
152+ , ss404Handler :: Maybe W.Application
144 }
153 }
wai-app-static/sample.hs+13−7
@@ -1,11 +1,17 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2−import Network.Wai.Application.Static
3−import Network.Wai.Handler.Warp (run)
4
2
3+import Data.String
4+import Data.Text (pack)
5+import Data.Maybe (mapMaybe)
6+import WaiAppStatic.Types (ssIndices, toPiece)
7+import Network.Wai.Application.Static (staticApp,defaultFileServerSettings)
8+import Network.Wai.Handler.Warp (runSettings, settingsPort, defaultSettings)
9+
5main :: IO ()
10main :: IO ()
6−main = run 3000 $ staticApp defaultWebAppSettings
11+main = runSettings defaultSettings
7− { ssLookupFile = fileSystemLookup "."
12+ {
8− , ssMaxAge = MaxAgeForever
13+ settingsPort = 3000
9− , ssIndices = []
14+ } $ staticApp (defaultFileServerSettings $ fromString ".")
10− , ssListing = Just defaultListing
15+ {
16+ ssIndices = mapMaybe (toPiece . pack) ["index.html"]
11 }
17 }
wai-app-static/test/EmbeddedTestEntries.hs+10
@@ -42,4 +42,14 @@ mkEntries = return
42 , EmbeddableEntry "e6.txt"
42 , EmbeddableEntry "e6.txt"
43 "text/plain"
43 "text/plain"
44 (Right [| return ("" :: T.Text, body 1000 'W') |] )
44 (Right [| return ("" :: T.Text, body 1000 'W') |] )
45+
46+ -- An index file
47+ , EmbeddableEntry "index.html"
48+ "text/html"
49+ (Right [| return ("" :: T.Text, "index file") |] )
50+
51+ -- An index file in a subdir
52+ , EmbeddableEntry "foo/index.html"
53+ "text/html"
54+ (Right [| return ("" :: T.Text, "index file in subdir") |] )
45 ]
55 ]
wai-app-static/test/WaiAppEmbeddedTest.hs+21−5
@@ -8,16 +8,15 @@ import Network.Wai.Application.Static (staticApp)
8import Network.Wai.Test
8import Network.Wai.Test
9import Test.Hspec
9import Test.Hspec
10import WaiAppStatic.Storage.Embedded
10import WaiAppStatic.Storage.Embedded
11−
11+import WaiAppStatic.Types
12−embApp :: Application
13−embApp = staticApp $(mkSettings mkEntries)
14
12
15defRequest :: Request
13defRequest :: Request
16defRequest = defaultRequest
14defRequest = defaultRequest
17
15
18embSpec :: Spec
16embSpec :: Spec
19embSpec = do
17embSpec = do
20− let embed = flip runSession embApp
18+ let embedSettings settings = flip runSession (staticApp settings)
19+ let embed = embedSettings $(mkSettings mkEntries)
21 describe "embedded, compressed entry" $ do
20 describe "embedded, compressed entry" $ do
22 it "served correctly" $ embed $ do
21 it "served correctly" $ embed $ do
23 req <- request (setRawPathInfo defRequest "e1.txt")
22 req <- request (setRawPathInfo defRequest "e1.txt")
@@ -33,6 +32,24 @@ embSpec = do
33 { requestHeaders = [("If-None-Match", "Etag 1")] }
32 { requestHeaders = [("If-None-Match", "Etag 1")] }
34 assertStatus 304 req
33 assertStatus 304 req
35
34
35+ it "ssIndices works" $ do
36+ let testSettings = $(mkSettings mkEntries){
37+ ssIndices = [unsafeToPiece "index.html"]
38+ }
39+ embedSettings testSettings $ do
40+ req <- request defRequest
41+ assertStatus 200 req
42+ assertBody "index file" req
43+
44+ it "ssIndices works with trailing slashes" $ do
45+ let testSettings = $(mkSettings mkEntries){
46+ ssIndices = [unsafeToPiece "index.html"]
47+ }
48+ embedSettings testSettings $ do
49+ req <- request (setRawPathInfo defRequest "/foo/")
50+ assertStatus 200 req
51+ assertBody "index file in subdir" req
52+
36 describe "embedded, uncompressed entry" $ do
53 describe "embedded, uncompressed entry" $ do
37 it "too short" $ embed $ do
54 it "too short" $ embed $ do
38 req <- request (setRawPathInfo defRequest "e2.txt")
55 req <- request (setRawPathInfo defRequest "e2.txt")
@@ -75,4 +92,3 @@ embSpec = do
75 assertNoHeader "Content-Encoding" req
92 assertNoHeader "Content-Encoding" req
76 assertNoHeader "ETag" req
93 assertNoHeader "ETag" req
77 assertBody (body 1000 'W') req
94 assertBody (body 1000 'W') req
78−
wai-app-static/test/WaiAppStaticTest.hs+65−6
@@ -2,13 +2,18 @@
2module WaiAppStaticTest (spec) where
2module WaiAppStaticTest (spec) where
3
3
4import Network.Wai.Application.Static
4import Network.Wai.Application.Static
5+import WaiAppStatic.Types
5
6
6import Test.Hspec
7import Test.Hspec
8+import Test.Mockery.Directory
7import qualified Data.ByteString.Char8 as S8
9import qualified Data.ByteString.Char8 as S8
8-- import qualified Data.ByteString.Lazy.Char8 as L8
10-- import qualified Data.ByteString.Lazy.Char8 as L8
9import System.PosixCompat.Files (getFileStatus, modificationTime)
11import System.PosixCompat.Files (getFileStatus, modificationTime)
12+import System.FilePath
13+import System.IO.Temp
10
14
11import Network.HTTP.Date
15import Network.HTTP.Date
16+import Network.HTTP.Types (status500)
12{-import System.Locale (defaultTimeLocale)-}
17{-import System.Locale (defaultTimeLocale)-}
13{-import Data.Time.Format (formatTime)-}
18{-import Data.Time.Format (formatTime)-}
14
19
@@ -24,7 +29,10 @@ defRequest = defaultRequest
24spec :: Spec
29spec :: Spec
25spec = do
30spec = do
26 let webApp = flip runSession $ staticApp $ defaultWebAppSettings "test"
31 let webApp = flip runSession $ staticApp $ defaultWebAppSettings "test"
32+ let fileServerAppWithSettings settings = flip runSession $ staticApp settings
27− let fileServerApp = flip runSession $ staticApp $ defaultFileServerSettings "test"
33+ let fileServerApp = fileServerAppWithSettings (defaultFileServerSettings "test")
34+ { ssAddTrailingSlash = True
35+ }
28
36
29 let etag = "1B2M2Y8AsgTpgAmY7PhCfg=="
37 let etag = "1B2M2Y8AsgTpgAmY7PhCfg=="
30 let file = "a/b"
38 let file = "a/b"
@@ -49,22 +57,22 @@ spec = do
49 assertStatus 200 =<<
57 assertStatus 200 =<<
50 request (setRawPathInfo defRequest path)
58 request (setRawPathInfo defRequest path)
51
59
52− it "404 for non-existant files" $ webApp $
60+ it "404 for non-existent files" $ webApp $
53 assertStatus 404 =<<
61 assertStatus 404 =<<
54 request (setRawPathInfo defRequest "doesNotExist")
62 request (setRawPathInfo defRequest "doesNotExist")
55
63
56− it "301 redirect when multiple slashes" $ webApp $ do
64+ it "302 redirect when multiple slashes" $ webApp $ do
57 req <- request (setRawPathInfo defRequest "a//b/c")
65 req <- request (setRawPathInfo defRequest "a//b/c")
58− assertStatus 301 req
66+ assertStatus 302 req
59 assertHeader "Location" "../../a/b/c" req
67 assertHeader "Location" "../../a/b/c" req
60
68
61 let absoluteApp = flip runSession $ staticApp $ (defaultWebAppSettings "test") {
69 let absoluteApp = flip runSession $ staticApp $ (defaultWebAppSettings "test") {
62 ssMkRedirect = \_ u -> S8.append "http://www.example.com" u
70 ssMkRedirect = \_ u -> S8.append "http://www.example.com" u
63 }
71 }
64− it "301 redirect when multiple slashes" $ absoluteApp $
72+ it "302 redirect when multiple slashes" $ absoluteApp $
65 flip mapM_ ["/a//b/c", "a//b/c"] $ \path -> do
73 flip mapM_ ["/a//b/c", "a//b/c"] $ \path -> do
66 req <- request (setRawPathInfo defRequest path)
74 req <- request (setRawPathInfo defRequest path)
67− assertStatus 301 req
75+ assertStatus 302 req
68 assertHeader "Location" "http://www.example.com/a/b/c" req
76 assertHeader "Location" "http://www.example.com/a/b/c" req
69
77
70 describe "webApp when requesting a static asset" $ do
78 describe "webApp when requesting a static asset" $ do
@@ -123,3 +131,54 @@ spec = do
123 assertStatus 304 req
131 assertStatus 304 req
124 assertNoHeader "Cache-Control" req
132 assertNoHeader "Cache-Control" req
125
133
134+ context "302 redirect to add a trailing slash on directories if missing" $ do
135+ it "works at the root" $ fileServerApp $ do
136+ req <- request (setRawPathInfo defRequest "/a")
137+ assertStatus 302 req
138+ assertHeader "Location" "/a/" req
139+
140+ it "works when an index.html is delivered" $ do
141+ let settings = (defaultFileServerSettings "."){
142+ ssAddTrailingSlash = True
143+ }
144+ inTempDirectory $ fileServerAppWithSettings settings $ do
145+ liftIO $ touch "foo/index.html"
146+ req <- request (setRawPathInfo defRequest "/foo")
147+ assertStatus 302 req
148+ assertHeader "Location" "/foo/" req
149+
150+ let urlMapApp = flip runSession $ \req send ->
151+ case pathInfo req of
152+ "subPath":rest ->
153+ let req' = req { pathInfo = rest }
154+ in (staticApp (defaultFileServerSettings "test")
155+ { ssAddTrailingSlash = True
156+ }) req' send
157+ _ -> send $ responseLBS status500 []
158+ "urlMapApp: only works at subPath"
159+ it "works with subpath at the root of the file server" $ urlMapApp $ do
160+ req <- request (setRawPathInfo defRequest "/subPath")
161+ assertStatus 302 req
162+ assertHeader "Location" "/subPath/" req
163+
164+ context "with defaultWebAppSettings" $ do
165+ it "ssIndices works" $ do
166+ withSystemTempDirectory "wai-app-static-test" $ \ dir -> do
167+ writeFile (dir </> "index.html") "foo"
168+ let testSettings = (defaultWebAppSettings dir) {
169+ ssIndices = [unsafeToPiece "index.html"]
170+ }
171+ fileServerAppWithSettings testSettings $ do
172+ resp <- request (setRawPathInfo defRequest "/")
173+ assertStatus 200 resp
174+ assertBody "foo" resp
175+
176+ context "with defaultFileServerSettings" $ do
177+ it "prefers ssIndices over ssListing" $ do
178+ withSystemTempDirectory "wai-app-static-test" $ \ dir -> do
179+ writeFile (dir </> "index.html") "foo"
180+ let testSettings = defaultFileServerSettings dir
181+ fileServerAppWithSettings testSettings $ do
182+ resp <- request (setRawPathInfo defRequest "/")
183+ assertStatus 200 resp
184+ assertBody "foo" resp
wai-app-static/wai-app-static.cabal+19−19
@@ -1,11 +1,11 @@
1name: wai-app-static
1name: wai-app-static
2−version: 2.0.1
2+version: 3.1.7.1
3license: MIT
3license: MIT
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
6−maintainer: Michael Snoyman <michael@snoyman.com>
6+maintainer: Michael Snoyman <michael@snoyman.com>, Greg Weber <greg@gregweber.info>
7synopsis: WAI application for static serving
7synopsis: WAI application for static serving
8−description: Also provides some helper functions and datatypes for use outside of WAI.
8+description: API docs and the README are available at <http://www.stackage.org/package/wai-app-static>.
9category: Web, Yesod
9category: Web, Yesod
10stability: Stable
10stability: Stable
11cabal-version: >= 1.8
11cabal-version: >= 1.8
@@ -17,6 +17,8 @@ Extra-source-files:
17 test/*.hs
17 test/*.hs
18 test/a/b
18 test/a/b
19 tests.hs
19 tests.hs
20+ README.md
21+ ChangeLog.md
20
22
21Flag print
23Flag print
22 Description: print debug info
24 Description: print debug info
@@ -24,8 +26,8 @@ Flag print
24
26
25library
27library
26 build-depends: base >= 4 && < 5
28 build-depends: base >= 4 && < 5
27− , wai >= 2.0 && < 2.2
29+ , wai >= 3.0 && < 3.3
28− , bytestring >= 0.9.1.4
30+ , bytestring >= 0.10.4
29 , http-types >= 0.7
31 , http-types >= 0.7
30 , transformers >= 0.2.2
32 , transformers >= 0.2.2
31 , unix-compat >= 0.2
33 , unix-compat >= 0.2
@@ -35,13 +37,8 @@ library
35 , old-locale >= 1.0.0.2
37 , old-locale >= 1.0.0.2
36 , file-embed >= 0.0.3.1
38 , file-embed >= 0.0.3.1
37 , text >= 0.7
39 , text >= 0.7
38− , blaze-builder >= 0.2.1.4
40+ , cryptonite >= 0.6
39− , base64-bytestring >= 0.1
41+ , memory >= 0.7
40− , byteable
41− , cryptohash >= 0.11
42− , cryptohash-conduit >= 0.1.0
43− , system-filepath >= 0.4
44− , system-fileio >= 0.3
45 , http-date
42 , http-date
46 , blaze-html >= 0.5
43 , blaze-html >= 0.5
47 , blaze-markup >= 0.5.1
44 , blaze-markup >= 0.5.1
@@ -50,9 +47,9 @@ library
50 , template-haskell >= 2.7
47 , template-haskell >= 2.7
51 , zlib >= 0.5
48 , zlib >= 0.5
52 , filepath
49 , filepath
53− , wai-extra >= 2.0 && < 2.2
50+ , wai-extra >= 3.0 && < 3.1
54 , optparse-applicative >= 0.7
51 , optparse-applicative >= 0.7
55− , warp >= 2.0 && < 2.2
52+ , warp >= 3.0.11 && < 3.4
56
53
57 exposed-modules: Network.Wai.Application.Static
54 exposed-modules: Network.Wai.Application.Static
58 WaiAppStatic.Storage.Filesystem
55 WaiAppStatic.Storage.Filesystem
@@ -73,10 +70,10 @@ Executable warp
73 Main-is: warp-static.hs
70 Main-is: warp-static.hs
74 hs-source-dirs: app
71 hs-source-dirs: app
75 Build-depends: base >= 4 && < 5
72 Build-depends: base >= 4 && < 5
76− , wai-app-static >= 2.0 && < 2.1
73+ , wai-app-static
77 , directory >= 1.0
74 , directory >= 1.0
78 , containers >= 0.2
75 , containers >= 0.2
79− , bytestring >= 0.9.1.4
76+ , bytestring >= 0.10.4
80 , text >= 0.7
77 , text >= 0.7
81 , mime-types >= 0.1 && < 0.2
78 , mime-types >= 0.1 && < 0.2
82
79
@@ -91,8 +88,8 @@ test-suite runtests
91 , time
88 , time
92 , old-locale
89 , old-locale
93 , http-date
90 , http-date
94− , wai-app-static >= 1.3
91+ , wai-app-static
95− , wai-test
92+ , wai-extra
96 , wai
93 , wai
97 , http-types
94 , http-types
98 , network
95 , network
@@ -101,8 +98,11 @@ test-suite runtests
101 , transformers
98 , transformers
102 , mime-types
99 , mime-types
103 , zlib
100 , zlib
101+ , filepath
102+ , temporary
103+ , mockery
104 -- , containers
104 -- , containers
105− ghc-options: -Wall
105+ ghc-options: -Wall
106
106
107source-repository head
107source-repository head
108 type: git
108 type: git
wai-conduit/ChangeLog.mdnew file+7
@@ -0,0 +1,7 @@
1+## 3.0.0.4
2+
3+* Drop dependency on blaze-builder
4+
5+## 3.0.0.3
6+
7+* Support wai 3.2
wai-handler-fastcgi/LICENSE → wai-conduit/LICENSErenamed+8−8
@@ -1,4 +1,4 @@
1−Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
1+Copyright (c) 2014 Michael Snoyman
2
2
3Permission is hereby granted, free of charge, to any person obtaining
3Permission is hereby granted, free of charge, to any person obtaining
4a copy of this software and associated documentation files (the
4a copy of this software and associated documentation files (the
@@ -8,13 +8,13 @@ distribute, sublicense, and/or sell copies of the Software, and to
8permit persons to whom the Software is furnished to do so, subject to
8permit persons to whom the Software is furnished to do so, subject to
9the following conditions:
9the following conditions:
10
10
11−The above copyright notice and this permission notice shall be
11+The above copyright notice and this permission notice shall be included
12−included in all copies or substantial portions of the Software.
12+in all copies or substantial portions of the Software.
13
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16−MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17−NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18−LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19−OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20−WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wai-conduit/Network/Wai/Conduit.hsnew file+66
@@ -0,0 +1,66 @@
1+-- | A light-weight wrapper around @Network.Wai@ to provide easy conduit support.
2+module Network.Wai.Conduit
3+ ( -- * Request body
4+ sourceRequestBody
5+ -- * Response body
6+ , responseSource
7+ , responseRawSource
8+ -- * Re-export
9+ , module Network.Wai
10+ ) where
11+
12+import Network.Wai
13+import Data.Conduit
14+import Control.Monad.IO.Class (MonadIO, liftIO)
15+import Data.ByteString (ByteString)
16+import qualified Data.ByteString as S
17+import Control.Monad (unless)
18+import Network.HTTP.Types
19+import Data.ByteString.Builder (Builder)
20+import qualified Data.Conduit.List as CL
21+
22+-- | Stream the request body.
23+--
24+-- Since 3.0.0
25+sourceRequestBody :: MonadIO m => Request -> ConduitT () ByteString m ()
26+sourceRequestBody req =
27+ loop
28+ where
29+ go = liftIO (getRequestBodyChunk req)
30+
31+ loop = do
32+ bs <- go
33+ unless (S.null bs) $ do
34+ yield bs
35+ loop
36+
37+-- | Create an HTTP response out of a @Source@.
38+--
39+-- Since 3.0.0
40+responseSource :: Status -> ResponseHeaders -> ConduitT () (Flush Builder) IO () -> Response
41+responseSource s hs src = responseStream s hs $ \send flush ->
42+ runConduit $ src .| CL.mapM_ (\mbuilder ->
43+ case mbuilder of
44+ Chunk b -> send b
45+ Flush -> flush)
46+
47+-- | Create a raw response using a @Source@ and @Sink@ to represent the input
48+-- and output, respectively.
49+--
50+-- Since 3.0.0
51+responseRawSource :: (MonadIO m, MonadIO n)
52+ => (ConduitT () ByteString m () -> ConduitT ByteString Void n () -> IO ())
53+ -> Response
54+ -> Response
55+responseRawSource app =
56+ responseRaw app'
57+ where
58+ app' recv send =
59+ app src sink
60+ where
61+ src = do
62+ bs <- liftIO recv
63+ unless (S.null bs) $ do
64+ yield bs
65+ src
66+ sink = CL.mapM_ $ liftIO . send
wai-conduit/README.mdnew file+4
@@ -0,0 +1,4 @@
1+## wai-conduit
2+
3+Since version 3.0.0, WAI has no built-in streaming data abstraction. This
4+library provides similar functionality to what existed in WAI 2.x.
wai-conduit/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
wai-conduit/example/Main.hsnew file+19
@@ -0,0 +1,19 @@
1+#!/usr/bin/env stack
2+-- stack --resolver lts-11.10 script
3+import Network.Wai
4+import Network.Wai.Handler.Warp
5+import Network.Wai.Conduit
6+import Network.HTTP.Types
7+import Conduit
8+import Data.ByteString.Builder (byteString)
9+import Control.Monad.Trans.Resource
10+
11+main :: IO ()
12+main = run 3000 app
13+
14+app :: Application
15+app _req respond =
16+ runResourceT $ withInternalState $ \is ->
17+ respond $ responseSource status200 [] $
18+ transPipe (`runInternalState` is) (sourceFile "Main.hs") .|
19+ mapC (Chunk . byteString)
wai-conduit/wai-conduit.cabalnew file+23
@@ -0,0 +1,23 @@
1+name: wai-conduit
2+version: 3.0.0.4
3+synopsis: conduit wrappers for WAI
4+description: API docs and the README are available at <http://www.stackage.org/package/wai-conduit>.
5+homepage: https://github.com/yesodweb/wai
6+license: MIT
7+license-file: LICENSE
8+author: Michael Snoyman
9+maintainer: michael@snoyman.com
10+category: Web, Conduit
11+build-type: Simple
12+cabal-version: >=1.10
13+extra-source-files: README.md ChangeLog.md
14+
15+library
16+ exposed-modules: Network.Wai.Conduit
17+ build-depends: base >= 4 && < 5
18+ , wai >= 3.0 && < 3.3
19+ , conduit
20+ , transformers
21+ , bytestring >= 0.10.4
22+ , http-types
23+ default-language: Haskell2010
wai-eventsource/Setup.lhsdeleted−7
@@ -1,7 +0,0 @@
1−#!/usr/bin/env runhaskell
2−
3−> module Main where
4−> import Distribution.Simple
5−
6−> main :: IO ()
7−> main = defaultMain
wai-eventsource/src/Main.hsdeleted−55
@@ -1,55 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−module Main where
3−
4−import Control.Monad
5−import Control.Concurrent (forkIO, threadDelay)
6−import Control.Monad.Trans (liftIO)
7−import Control.Concurrent.Chan
8−import Network.Wai.Handler.Warp (run)
9−import Network.Wai.Middleware.Gzip (gzip, def)
10−import Network.Wai
11−import Network.HTTP.Types (status200)
12−import Data.Time.Clock.POSIX
13−import Blaze.ByteString.Builder.Char.Utf8 (fromString)
14−import qualified Data.Conduit as C
15−
16−import Network.Wai.EventSource
17−
18−app :: Chan ServerEvent -> Application
19−app chan req =
20− case pathInfo req of
21− [] -> return $ ResponseFile status200 [("Content-Type", "text/html")] "static/index.html" Nothing
22− ["esold"] -> eventSourceAppChan chan req
23− ["eschan"] -> eventSourceAppChan chan req
24− ["esio"] -> eventSourceAppIO eventIO req
25− ["essrc"] -> eventSourceAppSource eventSource req
26− _ -> error "unexpected pathInfo"
27−
28−eventChan :: Chan ServerEvent -> IO ()
29−eventChan chan = forever $ do
30− threadDelay 1000000
31− time <- getPOSIXTime
32− writeChan chan (ServerEvent Nothing Nothing [fromString . show $ time])
33−
34−eventIO :: IO ServerEvent
35−eventIO = do
36− threadDelay 1000000
37− time <- getPOSIXTime
38− return $ ServerEvent (Just $ fromString "io")
39− Nothing
40− [fromString . show $ time]
41−
42−eventSource :: C.Source (C.ResourceT IO) ServerEvent
43−eventSource = forever $ do
44− time <- liftIO $ do
45− threadDelay 1000000
46− getPOSIXTime
47− C.yield $ ServerEvent (Just $ fromString "source")
48− Nothing
49− [fromString . show $ time]
50−
51−main = do
52− chan <- newChan
53− _ <- forkIO . eventChan $ chan
54− run 8080 (gzip def $ app chan)
55−
wai-eventsource/src/Network/Wai/EventSource.hsdeleted−67
@@ -1,67 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-|
3− A WAI adapter to the HTML5 Server-Sent Events API.
4−-}
5−module Network.Wai.EventSource (
6− ServerEvent(..),
7− eventSourceAppChan,
8− eventSourceAppSource,
9− eventSourceAppIO,
10− sourceToSource
11− ) where
12−
13−import Blaze.ByteString.Builder (Builder)
14−import Control.Concurrent.Chan (Chan, dupChan, readChan)
15−import Control.Monad.IO.Class (liftIO)
16−import Data.Conduit
17−import qualified Data.Conduit.List as CL
18−import Network.HTTP.Types (status200)
19−import Network.Wai (Application, Response, responseSource)
20−
21−import Network.Wai.EventSource.EventStream
22−
23−-- | Make a new WAI EventSource application reading events from
24−-- the given channel.
25−eventSourceAppChan :: Chan ServerEvent -> Application
26−eventSourceAppChan chan _ = do
27− chan' <- liftIO $ dupChan chan
28− return $ response chanToSource chan'
29−
30−-- | Make a new WAI EventSource application reading events from
31−-- the given source.
32−eventSourceAppSource :: Source IO ServerEvent -> Application
33−eventSourceAppSource src _ = return $ response sourceToSource src
34−
35−-- | Make a new WAI EventSource application reading events from
36−-- the given IO action.
37−eventSourceAppIO :: IO ServerEvent -> Application
38−eventSourceAppIO act _ = return $ response ioToSource act
39−
40−response :: (a -> Source IO (Flush Builder)) -> a -> Response
41−response f a = responseSource status200 [("Content-Type", "text/event-stream")] $ f a
42−
43−chanToSource :: Chan ServerEvent -> Source IO (Flush Builder)
44−chanToSource = ioToSource . readChan
45−
46−ioToSource :: IO ServerEvent -> Source IO (Flush Builder)
47−ioToSource act =
48− loop
49− where
50− loop = do
51− x <- liftIO act
52− case eventToBuilder x of
53− Nothing -> return ()
54− Just y -> do
55− yield $ Chunk y
56− yield Flush
57− loop
58−
59−-- | Convert a ServerEvent source into a Builder source of serialized
60−-- events.
61−sourceToSource :: Monad m => Source m ServerEvent -> Source m (Flush Builder)
62−sourceToSource src = src $= CL.concatMap eventToFlushBuilder
63− where
64− eventToFlushBuilder event =
65− case eventToBuilder event of
66− Nothing -> []
67− Just x -> [Chunk x, Flush]
wai-eventsource/wai-eventsource.cabaldeleted−33
@@ -1,33 +0,0 @@
1−Name: wai-eventsource
2−Version: 2.0.0.2
3−Synopsis: WAI support for server-sent events
4−Description: WAI support for server-sent events
5−License: MIT
6−Author: Chris Smith, Mathias Biilmann Christensen
7−Maintainer: greg@gregweber.info
8−Stability: Experimental
9−Category: Web
10−Build-type: Simple
11−Cabal-version: >=1.8
12−homepage: http://www.yesodweb.com/book/web-application-interface
13−license-file: LICENSE
14−
15−Library
16− Build-depends:
17− base >= 4.3 && < 5
18− , bytestring >= 0.9.1.4
19− , blaze-builder >= 0.3 && < 0.4
20− , conduit >= 0.5 && < 1.2
21− , http-types >= 0.7
22− , wai >= 2.0 && < 2.2
23− , transformers
24−
25− ghc-options: -Wall
26− hs-source-dirs: src
27− exposed-modules:
28− Network.Wai.EventSource
29− Network.Wai.EventSource.EventStream
30−
31−source-repository head
32− type: git
33− location: git://github.com/yesodweb/wai.git
wai-extra/ChangeLog.mdnew file+209
@@ -0,0 +1,209 @@
1+# Changelog for wai-extra
2+
3+## 3.0.29.1
4+
5+* Fix `Network.Wai.Test.request` always sending an empty request body [#794](https://github.com/yesodweb/wai/pull/794)
6+
7+## 3.0.29
8+
9+* Export `Network.Wai.EventSource.eventStreamAppRaw` [#786](https://github.com/yesodweb/wai/pull/786)
10+
11+## 3.0.28
12+
13+* Add `Network.Wai.EventSource.eventStreamAppRaw` [#767](https://github.com/yesodweb/wai/pull/767)
14+
15+## 3.0.27
16+
17+* Add custom request log formatter which includes response headers [#762](https://github.com/yesodweb/wai/pull/762)
18+
19+## 3.0.26.1
20+
21+* When available, supply the response size to custom loggers
22+ [#757](https://github.com/yesodweb/wai/pull/757)
23+
24+## 3.0.26
25+
26+* Throw 413 for too large payload
27+* Throw 431 for too large headers
28+ [#741](https://github.com/yesodweb/wai/pull/741)
29+
30+## 3.0.25
31+
32+* Supporting `network` version 3.0.
33+
34+## 3.0.24.3
35+
36+* Drop unnecessary `lifted-base` dependency
37+* Drop unnecessary `stringsearch` dependency [#714](https://github.com/yesodweb/wai/pull/714)
38+
39+## 3.0.24.2
40+
41+* Consider quoted multipart form boundary markers [#700](https://github.com/yesodweb/wai/pull/700).
42+* Don't raise exceptions in `formatAsJSON` [#709](https://github.com/yesodweb/wai/pull/709)
43+
44+## 3.0.24.1
45+
46+* Fix a "file not found" exception in wai-extra [#705](https://github.com/yesodweb/wai/pull/706)
47+
48+## 3.0.24.0
49+
50+* Add timeout middleware [#702](https://github.com/yesodweb/wai/pull/702).
51+
52+## 3.0.23.0
53+
54+* Add rewriteRoot middleware [#697](https://github.com/yesodweb/wai/pull/697).
55+
56+## 3.0.22.1
57+
58+* Drop dependency on blaze-builder, requiring streaming-commons >= 0.2
59+
60+## 3.0.22.0
61+
62+* Support for streaming-commons 0.2
63+* Support for resourcet 1.2
64+
65+## 3.0.21.0
66+
67+* Export `Network.Wai.Parse.noLimitParseRequestBodyOptions` [#662](https://github.com/yesodweb/wai/pull/662).
68+
69+## 3.0.20.2
70+
71+* Revert previous change to `srequest`, which caused breakage
72+
73+## 3.0.20.1
74+
75+* Set `requestBodyLength` for `srequest` [#654](https://github.com/yesodweb/wai/pull/654)
76+
77+## 3.0.20.0
78+
79+* runSessionWith (runSession variant that gives access to ClientState) [#629](https://github.com/yesodweb/wai/pull/629)
80+
81+## 3.0.19.1
82+
83+* All loggers follow the autoFlush setting [#604](https://github.com/yesodweb/wai/pull/604)
84+
85+## 3.0.19
86+
87+* Add a new function basicAuth', which passes request to the CheckCreds argument.
88+
89+## 3.0.18
90+
91+* ForceSSL: preserve port number when redirecting to https. [#582](https://github.com/yesodweb/wai/pull/582)
92+
93+## 3.0.17
94+
95+* Gzip pre compressed [#580](https://github.com/yesodweb/wai/pull/580)
96+
97+## 3.0.16.1
98+
99+* Fix the way the header length is checked (for limiting the max header length)
100+
101+## 3.0.16.0
102+
103+* Add a new function "parseRequestBodyEx" that allows various size limits to be set.
104+
105+## 3.0.15.3
106+
107+* Allow wai-logger 2.3
108+
109+## 3.0.15.2
110+
111+* Doc improvements
112+
113+## 3.0.15.1
114+
115+* don't use deprecated CRT functions on Windows [#544](https://github.com/yesodweb/wai/pull/544)
116+
117+## 3.0.15
118+
119+* add requestSizeCheck [#525](https://github.com/yesodweb/wai/pull/525)
120+
121+## 3.0.14.3
122+
123+* Add missing `requestHeaderReferer` and `requestHeaderUserAgent` fields to CGI [yesod#1186](https://github.com/yesodweb/yesod/issues/1186)
124+
125+## 3.0.14.2
126+
127+* Case insensitive multipart request header lookup [#518](https://github.com/yesodweb/wai/pull/518)
128+
129+## 3.0.14.1
130+
131+* Doc update for logStdout and logStdoutDev [#515](https://github.com/yesodweb/wai/issues/515)
132+
133+## 3.0.14
134+
135+* Middleware to force domain names. [#506](https://github.com/yesodweb/wai/issues/506) [#507](https://github.com/yesodweb/wai/pull/507)
136+
137+## 3.0.13.1
138+
139+* Support wai 3.2
140+
141+## 3.0.13
142+
143+* Autoflush handle [#466](https://github.com/yesodweb/wai/pull/466)
144+
145+## 3.0.12
146+
147+* Add Network.Wai.Header.contentLength to read the Content-Length header of a response
148+* The gzip middleware no longer zips responses smaller than 860 bytes
149+
150+## 3.0.11
151+
152+* Add constructor for more detailed custom output formats for RequestLogger
153+* Add JSON output formatter for RequestLogger
154+
155+## 3.0.10
156+
157+* Adding Request Body to RequestLogger [#401](https://github.com/yesodweb/wai/pull/401)
158+
159+## 3.0.9
160+
161+* Network.Wai.Middleware.Routed module added
162+
163+## 3.0.7
164+
165+* Add appearsSecure: check if a request appears to be using SSL even in the
166+ presence of reverse proxies [#362](https://github.com/yesodweb/wai/pull/362)
167+* Add ForceSSL middleware [#363](https://github.com/yesodweb/wai/pull/363)
168+* Add Approot middleware
169+
170+## 3.0.6.1
171+
172+* Test code: only include a Cookie header if there are cookies. Without this
173+ patch, yesod-test cookie handling is broken.
174+
175+## 3.0.6
176+
177+* Add Cookie Handling to Network.Wai.Test [#356](https://github.com/yesodweb/wai/pull/356)
178+
179+## 3.0.5
180+
181+* add functions to extract authentication data from Authorization header [#352](add functions to extract authentication data from Authorization header #352)
182+
183+## 3.0.4.6
184+
185+* Access log sequence not valid [#336](https://github.com/yesodweb/wai/issues/336)
186+
187+## 3.0.4.5
188+
189+* Allow fast-logger 2.3
190+
191+## 3.0.4.3
192+
193+Test suite warning cleanup
194+
195+## 3.0.4.2
196+
197+Allow blaze-builder 0.4
198+
199+## 3.0.4.1
200+
201+Fix compilation failure on Windows [#321](https://github.com/yesodweb/wai/issues/321)
202+
203+## 3.0.4
204+
205+Add the `StreamFile` middleware.
206+
207+## 3.0.3
208+
209+Add the `AddHeaders` middleware.
wai-extra/Network/Wai/EventSource.hsnew file+59
@@ -0,0 +1,59 @@
1+{-|
2+ A WAI adapter to the HTML5 Server-Sent Events API.
3+
4+ If running through a proxy like Nginx you might need to add the
5+ headers:
6+
7+ > [ ("X-Accel-Buffering", "no"), ("Cache-Control", "no-cache")]
8+
9+ There is a small example using these functions in the @example@ directory.
10+-}
11+module Network.Wai.EventSource (
12+ ServerEvent(..),
13+ eventSourceAppChan,
14+ eventSourceAppIO,
15+ eventStreamAppRaw
16+ ) where
17+
18+import Data.Function (fix)
19+import Control.Concurrent.Chan (Chan, dupChan, readChan)
20+import Control.Monad.IO.Class (liftIO)
21+import Network.HTTP.Types (status200, hContentType)
22+import Network.Wai (Application, responseStream)
23+
24+import Network.Wai.EventSource.EventStream
25+
26+-- | Make a new WAI EventSource application reading events from
27+-- the given channel.
28+eventSourceAppChan :: Chan ServerEvent -> Application
29+eventSourceAppChan chan req sendResponse = do
30+ chan' <- liftIO $ dupChan chan
31+ eventSourceAppIO (readChan chan') req sendResponse
32+
33+-- | Make a new WAI EventSource application reading events from
34+-- the given IO action.
35+eventSourceAppIO :: IO ServerEvent -> Application
36+eventSourceAppIO src _ sendResponse =
37+ sendResponse $ responseStream
38+ status200
39+ [(hContentType, "text/event-stream")]
40+ $ \sendChunk flush -> fix $ \loop -> do
41+ se <- src
42+ case eventToBuilder se of
43+ Nothing -> return ()
44+ Just b -> sendChunk b >> flush >> loop
45+
46+-- | Make a new WAI EventSource application with a handler that emits events.
47+--
48+-- @since 3.0.28
49+eventStreamAppRaw :: ((ServerEvent -> IO()) -> IO () -> IO ()) -> Application
50+eventStreamAppRaw handler _ sendResponse =
51+ sendResponse $ responseStream
52+ status200
53+ [(hContentType, "text/event-stream")]
54+ $ \sendChunk flush -> handler (sendEvent sendChunk) flush
55+ where
56+ sendEvent sendChunk event =
57+ case eventToBuilder event of
58+ Nothing -> return ()
59+ Just b -> sendChunk b
wai-eventsource/src/Network/Wai/EventSource/EventStream.hs → wai-extra/Network/Wai/EventSource/EventStream.hsrenamed+12−11
@@ -1,5 +1,5 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE CPP #-}
2−{- code adapted by Mathias Billman originaly from Chris Smith https://github.com/cdsmith/gloss-web -}
2+{- code adapted by Mathias Billman originally from Chris Smith https://github.com/cdsmith/gloss-web -}
3
3
4{-|
4{-|
5 Internal module, usually you don't need to use it.
5 Internal module, usually you don't need to use it.
@@ -9,9 +9,10 @@ module Network.Wai.EventSource.EventStream (
9 eventToBuilder
9 eventToBuilder
10 ) where
10 ) where
11
11
12−import Blaze.ByteString.Builder
12+import Data.ByteString.Builder
13−import Blaze.ByteString.Builder.Char8
13+#if __GLASGOW_HASKELL__ < 710
14import Data.Monoid
14import Data.Monoid
15+#endif
15
16
16{-|
17{-|
17 Type representing a communication over an event stream. This can be an
18 Type representing a communication over an event stream. This can be an
@@ -37,18 +38,18 @@ data ServerEvent
37 Newline as a Builder.
38 Newline as a Builder.
38-}
39-}
39nl :: Builder
40nl :: Builder
40−nl = fromChar '\n'
41+nl = char7 '\n'
41
42
42
43
43{-|
44{-|
44 Field names as Builder
45 Field names as Builder
45-}
46-}
46nameField, idField, dataField, retryField, commentField :: Builder
47nameField, idField, dataField, retryField, commentField :: Builder
47−nameField = fromString "event:"
48+nameField = string7 "event:"
48−idField = fromString "id:"
49+idField = string7 "id:"
49−dataField = fromString "data:"
50+dataField = string7 "data:"
50−retryField = fromString "retry:"
51+retryField = string7 "retry:"
51−commentField = fromChar ':'
52+commentField = char7 ':'
52
53
53
54
54{-|
55{-|
@@ -64,7 +65,7 @@ field l b = l `mappend` b `mappend` nl
64-}
65-}
65eventToBuilder :: ServerEvent -> Maybe Builder
66eventToBuilder :: ServerEvent -> Maybe Builder
66eventToBuilder (CommentEvent txt) = Just $ field commentField txt
67eventToBuilder (CommentEvent txt) = Just $ field commentField txt
67−eventToBuilder (RetryEvent n) = Just $ field retryField (fromShow n)
68+eventToBuilder (RetryEvent n) = Just $ field retryField (string8 . show $ n)
68eventToBuilder (CloseEvent) = Nothing
69eventToBuilder (CloseEvent) = Nothing
69eventToBuilder (ServerEvent n i d)= Just $
70eventToBuilder (ServerEvent n i d)= Just $
70 (name n $ evid i $ mconcat (map (field dataField) d)) `mappend` nl
71 (name n $ evid i $ mconcat (map (field dataField) d)) `mappend` nl
wai-extra/Network/Wai/Handler/CGI.hs+80−75
@@ -1,6 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE RankNTypes, CPP #-}
3−{-# LANGUAGE CPP #-}
4-- | Backend for Common Gateway Interface. Almost all users should use the
2-- | Backend for Common Gateway Interface. Almost all users should use the
5-- 'run' function.
3-- 'run' function.
6module Network.Wai.Handler.CGI
4module Network.Wai.Handler.CGI
@@ -13,26 +11,28 @@ module Network.Wai.Handler.CGI
13import Network.Wai
11import Network.Wai
14import Network.Wai.Internal
12import Network.Wai.Internal
15import Network.Socket (getAddrInfo, addrAddress)
13import Network.Socket (getAddrInfo, addrAddress)
14+import Data.IORef
16import Data.Maybe (fromMaybe)
15import Data.Maybe (fromMaybe)
17−import Control.Exception (mask)
18import qualified Data.ByteString.Char8 as B
16import qualified Data.ByteString.Char8 as B
19import qualified Data.ByteString.Lazy as L
17import qualified Data.ByteString.Lazy as L
20import Control.Arrow ((***))
18import Control.Arrow ((***))
21import Data.Char (toLower)
19import Data.Char (toLower)
22import qualified System.IO
20import qualified System.IO
23import qualified Data.String as String
21import qualified Data.String as String
24−import Data.Monoid (mconcat, mempty)
25−import Blaze.ByteString.Builder (fromByteString, toLazyByteString)
22+import Data.ByteString.Builder (byteString, toLazyByteString, char7, string8)
26−import Blaze.ByteString.Builder.Char8 (fromChar, fromString)
23+import Data.ByteString.Builder.Extra (flush)
27−import Data.Conduit.Blaze (builderToByteStringFlush)
28−import Control.Monad.IO.Class (liftIO)
29import Data.ByteString.Lazy.Internal (defaultChunkSize)
24import Data.ByteString.Lazy.Internal (defaultChunkSize)
30import System.IO (Handle)
25import System.IO (Handle)
31−import Network.HTTP.Types (Status (..))
26+import Network.HTTP.Types (Status (..), hRange, hContentType, hContentLength)
32import qualified Network.HTTP.Types as H
27import qualified Network.HTTP.Types as H
33import qualified Data.CaseInsensitive as CI
28import qualified Data.CaseInsensitive as CI
29+#if __GLASGOW_HASKELL__ < 710
34−import Data.Monoid (mappend)
30+import Data.Monoid (mconcat, mempty, mappend)
35−import Data.Conduit
31+#endif
32+
33+import qualified Data.Streaming.ByteString.Builder as Builder
34+import Data.Function (fix)
35+import Control.Monad (unless, void)
36
36
37#if WINDOWS
37#if WINDOWS
38import System.Environment (getEnvironment)
38import System.Environment (getEnvironment)
@@ -76,7 +76,7 @@ runSendfile sf app = do
76-- stick with 'run' or 'runSendfile'.
76-- stick with 'run' or 'runSendfile'.
77runGeneric
77runGeneric
78 :: [(String, String)] -- ^ all variables
78 :: [(String, String)] -- ^ all variables
79− -> (Int -> Source IO B.ByteString) -- ^ responseBody of input
79+ -> (Int -> IO (IO B.ByteString)) -- ^ responseBody of input
80 -> (B.ByteString -> IO ()) -- ^ destination for output
80 -> (B.ByteString -> IO ()) -- ^ destination for output
81 -> Maybe B.ByteString -- ^ does the server support the X-Sendfile header?
81 -> Maybe B.ByteString -- ^ does the server support the X-Sendfile header?
82 -> Application
82 -> Application
@@ -98,76 +98,81 @@ runGeneric vars inputH outputH xsendfile app = do
98 "https" -> True
98 "https" -> True
99 _ -> False
99 _ -> False
100 addrs <- getAddrInfo Nothing (Just remoteHost') Nothing
100 addrs <- getAddrInfo Nothing (Just remoteHost') Nothing
101+ requestBody' <- inputH contentLength
101 let addr =
102 let addr =
102 case addrs of
103 case addrs of
103 a:_ -> addrAddress a
104 a:_ -> addrAddress a
104 [] -> error $ "Invalid REMOTE_ADDR or REMOTE_HOST: " ++ remoteHost'
105 [] -> error $ "Invalid REMOTE_ADDR or REMOTE_HOST: " ++ remoteHost'
105− mask $ \restore -> do
106− let reqHeaders = map (cleanupVarName *** B.pack) vars
106+ reqHeaders = map (cleanupVarName *** B.pack) vars
107− env = Request
107+ env = Request
108− { requestMethod = rmethod
108+ { requestMethod = rmethod
109− , rawPathInfo = B.pack pinfo
109+ , rawPathInfo = B.pack pinfo
110− , pathInfo = H.decodePathSegments $ B.pack pinfo
110+ , pathInfo = H.decodePathSegments $ B.pack pinfo
111− , rawQueryString = B.pack qstring
111+ , rawQueryString = B.pack qstring
112− , queryString = H.parseQuery $ B.pack qstring
112+ , queryString = H.parseQuery $ B.pack qstring
113− , requestHeaders = reqHeaders
113+ , requestHeaders = reqHeaders
114− , isSecure = isSecure'
114+ , isSecure = isSecure'
115− , remoteHost = addr
115+ , remoteHost = addr
116− , httpVersion = H.http11 -- FIXME
116+ , httpVersion = H.http11 -- FIXME
117− , requestBody = inputH contentLength
117+ , requestBody = requestBody'
118− , vault = mempty
118+ , vault = mempty
119− , requestBodyLength = KnownLength $ fromIntegral contentLength
119+ , requestBodyLength = KnownLength $ fromIntegral contentLength
120− , requestHeaderHost = lookup "host" reqHeaders
120+ , requestHeaderHost = lookup "host" reqHeaders
121− , requestHeaderRange = lookup "range" reqHeaders
121+ , requestHeaderRange = lookup hRange reqHeaders
122+#if MIN_VERSION_wai(3,2,0)
123+ , requestHeaderReferer = lookup "referer" reqHeaders
124+ , requestHeaderUserAgent = lookup "user-agent" reqHeaders
125+#endif
122− }
126+ }
123− -- FIXME worry about exception?
127+ void $ app env $ \res ->
124− res <- restore $ app env
125 case (xsendfile, res) of
128 case (xsendfile, res) of
126− (Just sf, ResponseFile s hs fp Nothing) ->
129+ (Just sf, ResponseFile s hs fp Nothing) -> do
127− restore $ mapM_ outputH $ L.toChunks $ toLazyByteString $ sfBuilder s hs sf fp
130+ mapM_ outputH $ L.toChunks $ toLazyByteString $ sfBuilder s hs sf fp
131+ return ResponseReceived
128 _ -> do
132 _ -> do
129− let (s, hs, wb) = responseToSource res
133+ let (s, hs, wb) = responseToStream res
134+ (blazeRecv, blazeFinish) <- Builder.newBuilderRecv Builder.defaultStrategy
130− wb $ \b ->
135+ wb $ \b -> do
131− let src = do
136+ let sendBuilder builder = do
137+ popper <- blazeRecv builder
138+ fix $ \loop -> do
139+ bs <- popper
140+ unless (B.null bs) $ do
141+ outputH bs
142+ loop
132− yield (Chunk $ headers s hs `mappend` fromChar '\n')
143+ sendBuilder $ headers s hs `mappend` char7 '\n'
133− b
144+ b sendBuilder (sendBuilder flush)
134− in src $$ builderSink
145+ blazeFinish >>= maybe (return ()) outputH
146+ return ResponseReceived
135 where
147 where
136 headers s hs = mconcat (map header $ status s : map header' (fixHeaders hs))
148 headers s hs = mconcat (map header $ status s : map header' (fixHeaders hs))
137− status (Status i m) = (fromByteString "Status", mconcat
149+ status (Status i m) = (byteString "Status", mconcat
138− [ fromString $ show i
150+ [ string8 $ show i
139− , fromChar ' '
151+ , char7 ' '
140− , fromByteString m
152+ , byteString m
141 ])
153 ])
142− header' (x, y) = (fromByteString $ CI.original x, fromByteString y)
154+ header' (x, y) = (byteString $ CI.original x, byteString y)
143 header (x, y) = mconcat
155 header (x, y) = mconcat
144 [ x
156 [ x
145− , fromByteString ": "
157+ , byteString ": "
146 , y
158 , y
147− , fromChar '\n'
159+ , char7 '\n'
148 ]
160 ]
149 sfBuilder s hs sf fp = mconcat
161 sfBuilder s hs sf fp = mconcat
150 [ headers s hs
162 [ headers s hs
151− , header $ (fromByteString sf, fromString fp)
163+ , header $ (byteString sf, string8 fp)
152− , fromChar '\n'
164+ , char7 '\n'
153− , fromByteString sf
165+ , byteString sf
154− , fromByteString " not supported"
166+ , byteString " not supported"
155 ]
167 ]
156− bsSink = await >>= maybe (return ()) push
157− push (Chunk bs) = do
158− liftIO $ outputH bs
159− bsSink
160− -- FIXME actually flush?
161− push Flush = bsSink
162− builderSink = builderToByteStringFlush =$ bsSink
163 fixHeaders h =
168 fixHeaders h =
164− case lookup "content-type" h of
169+ case lookup hContentType h of
165− Nothing -> ("Content-Type", "text/html; charset=utf-8") : h
170+ Nothing -> (hContentType, "text/html; charset=utf-8") : h
166 Just _ -> h
171 Just _ -> h
167
172
168cleanupVarName :: String -> CI.CI B.ByteString
173cleanupVarName :: String -> CI.CI B.ByteString
169−cleanupVarName "CONTENT_TYPE" = "Content-Type"
174+cleanupVarName "CONTENT_TYPE" = hContentType
170−cleanupVarName "CONTENT_LENGTH" = "Content-Length"
175+cleanupVarName "CONTENT_LENGTH" = hContentLength
171cleanupVarName "SCRIPT_NAME" = "CGI-Script-Name"
176cleanupVarName "SCRIPT_NAME" = "CGI-Script-Name"
172cleanupVarName s =
177cleanupVarName s =
173 case s of
178 case s of
@@ -178,19 +183,19 @@ cleanupVarName s =
178 helper' (x:rest) = toLower x : helper' rest
183 helper' (x:rest) = toLower x : helper' rest
179 helper' [] = []
184 helper' [] = []
180
185
181−requestBodyHandle :: Handle -> Int -> Source IO B.ByteString
186+requestBodyHandle :: Handle -> Int -> IO (IO B.ByteString)
182requestBodyHandle h = requestBodyFunc $ \i -> do
187requestBodyHandle h = requestBodyFunc $ \i -> do
183 bs <- B.hGet h i
188 bs <- B.hGet h i
184 return $ if B.null bs then Nothing else Just bs
189 return $ if B.null bs then Nothing else Just bs
185
190
186−requestBodyFunc :: (Int -> IO (Maybe B.ByteString)) -> Int -> Source IO B.ByteString
191+requestBodyFunc :: (Int -> IO (Maybe B.ByteString)) -> Int -> IO (IO B.ByteString)
187−requestBodyFunc get =
192+requestBodyFunc get count0 = do
188− loop
193+ ref <- newIORef count0
189− where
194+ return $ do
190− loop 0 = return ()
195+ count <- readIORef ref
191− loop count = do
196+ if count <= 0
197+ then return B.empty
198+ else do
192− mbs <- liftIO $ get $ min count defaultChunkSize
199+ mbs <- get $ min count defaultChunkSize
193− let count' = count - maybe 0 B.length mbs
200+ writeIORef ref $ count - maybe 0 B.length mbs
194− case mbs of
201+ return $ fromMaybe B.empty mbs
195− Nothing -> return ()
196− Just bs -> yield bs >> loop count'
wai-extra/Network/Wai/Handler/SCGI.hs+12−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE ForeignFunctionInterface #-}
1+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2module Network.Wai.Handler.SCGI
2module Network.Wai.Handler.SCGI
3 ( run
3 ( run
4 , runSendfile
4 , runSendfile
@@ -86,6 +86,16 @@ readByteString socket len = do
86foreign import ccall unsafe "accept"
86foreign import ccall unsafe "accept"
87 c'accept :: CInt -> Ptr a -> Ptr a -> IO CInt
87 c'accept :: CInt -> Ptr a -> Ptr a -> IO CInt
88
88
89+#if WINDOWS
90+foreign import ccall unsafe "_close"
91+ c'close :: CInt -> IO CInt
92+
93+foreign import ccall unsafe "_write"
94+ c'write :: CInt -> Ptr CChar -> CInt -> IO CInt
95+
96+foreign import ccall unsafe "_read"
97+ c'read :: CInt -> Ptr CChar -> CInt -> IO CInt
98+#else
89foreign import ccall unsafe "close"
99foreign import ccall unsafe "close"
90 c'close :: CInt -> IO CInt
100 c'close :: CInt -> IO CInt
91
101
@@ -94,3 +104,4 @@ foreign import ccall unsafe "write"
94
104
95foreign import ccall unsafe "read"
105foreign import ccall unsafe "read"
96 c'read :: CInt -> Ptr CChar -> CInt -> IO CInt
106 c'read :: CInt -> Ptr CChar -> CInt -> IO CInt
107+#endif
wai-extra/Network/Wai/Header.hsnew file+18
@@ -0,0 +1,18 @@
1+-- | Some helpers for dealing with WAI 'Header's.
2+
3+module Network.Wai.Header
4+ ( contentLength
5+ ) where
6+
7+import qualified Data.ByteString.Char8 as S8
8+import Network.HTTP.Types as H
9+
10+-- | More useful for a response. A Wai Request already has a requestBodyLength
11+contentLength :: [(HeaderName, S8.ByteString)] -> Maybe Integer
12+contentLength hdrs = lookup H.hContentLength hdrs >>= readInt
13+
14+readInt :: S8.ByteString -> Maybe Integer
15+readInt bs =
16+ case S8.readInteger bs of
17+ Just (i, rest) | S8.null rest -> Just i
18+ _ -> Nothing
wai-extra/Network/Wai/Middleware/AcceptOverride.hs−2
@@ -1,5 +1,3 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−
3module Network.Wai.Middleware.AcceptOverride
1module Network.Wai.Middleware.AcceptOverride
4 ( acceptOverride
2 ( acceptOverride
5 ) where
3 ) where
wai-extra/Network/Wai/Middleware/AddHeaders.hsnew file+24
@@ -0,0 +1,24 @@
1+-- |
2+--
3+-- Since 3.0.3
4+module Network.Wai.Middleware.AddHeaders
5+ ( addHeaders
6+ ) where
7+
8+import Network.HTTP.Types (Header)
9+import Network.Wai (Middleware, modifyResponse, mapResponseHeaders)
10+import Network.Wai.Internal (Response(..))
11+import Data.ByteString (ByteString)
12+
13+import qualified Data.CaseInsensitive as CI
14+import Control.Arrow (first)
15+
16+addHeaders :: [(ByteString, ByteString)] -> Middleware
17+-- ^ Prepend a list of headers without any checks
18+--
19+-- Since 3.0.3
20+
21+addHeaders h = modifyResponse $ addHeaders' (map (first CI.mk) h)
22+
23+addHeaders' :: [Header] -> Response -> Response
24+addHeaders' h = mapResponseHeaders (\hs -> h ++ hs)
wai-extra/Network/Wai/Middleware/Approot.hsnew file+121
@@ -0,0 +1,121 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+-- | Middleware for establishing the root of the application.
3+--
4+-- Many application need the ability to create URLs referring back to the
5+-- application itself. For example: generate RSS feeds or sitemaps, giving
6+-- users copy-paste links, or sending emails. In many cases, the approot can be
7+-- determined correctly from the request headers. However, some things can
8+-- prevent this, especially reverse proxies. This module provides multiple ways
9+-- of configuring approot discovery, and functions for applications to get that
10+-- approot.
11+--
12+-- Approots are structured such that they can be prepended to a string such as
13+-- @/foo/bar?baz=bin@. For example, if your application is hosted on
14+-- example.com using HTTPS, the approot would be @https://example.com@. Note
15+-- the lack of a trailing slash.
16+module Network.Wai.Middleware.Approot
17+ ( -- * Middleware
18+ approotMiddleware
19+ -- * Common providers
20+ , envFallback
21+ , envFallbackNamed
22+ , hardcoded
23+ , fromRequest
24+ -- * Functions for applications
25+ , getApproot
26+ , getApprootMay
27+ ) where
28+
29+import Control.Exception (Exception, throw)
30+import Data.ByteString (ByteString)
31+import qualified Data.ByteString.Char8 as S8
32+import Data.Maybe (fromMaybe)
33+import Data.Typeable (Typeable)
34+import qualified Data.Vault.Lazy as V
35+import Network.Wai (Request, vault, Middleware)
36+import Network.Wai.Request (guessApproot)
37+import System.Environment (getEnvironment)
38+import System.IO.Unsafe (unsafePerformIO)
39+
40+approotKey :: V.Key ByteString
41+approotKey = unsafePerformIO V.newKey
42+{-# NOINLINE approotKey #-}
43+
44+-- | The most generic version of the middleware, allowing you to provide a
45+-- function to get the approot for each request. For many use cases, one of the
46+-- helper functions provided by this module will give the necessary
47+-- functionality more conveniently.
48+--
49+-- Since 3.0.7
50+approotMiddleware :: (Request -> IO ByteString) -- ^ get the approot
51+ -> Middleware
52+approotMiddleware getRoot app req respond = do
53+ ar <- getRoot req
54+ let req' = req { vault = V.insert approotKey ar $ vault req }
55+ app req' respond
56+
57+-- | Same as @'envFallbackNamed' "APPROOT"@.
58+--
59+-- The environment variable @APPROOT@ is used by Keter, School of Haskell, and yesod-devel.
60+--
61+-- Since 3.0.7
62+envFallback :: IO Middleware
63+envFallback = envFallbackNamed "APPROOT"
64+
65+-- | Produce a middleware that takes the approot from the given environment
66+-- variable, falling back to the behavior of 'fromRequest' if the variable is
67+-- not set.
68+--
69+-- Since 3.0.7
70+envFallbackNamed :: String -> IO Middleware
71+envFallbackNamed name = do
72+ env <- getEnvironment
73+ case lookup name env of
74+ Just s -> return $ hardcoded $ S8.pack s
75+ Nothing -> return fromRequest
76+
77+-- | Hard-code the given value as the approot.
78+--
79+-- Since 3.0.7
80+hardcoded :: ByteString -> Middleware
81+hardcoded ar = approotMiddleware (const $ return ar)
82+
83+-- | Get the approot by analyzing the request. This is not a full-proof
84+-- approach, but in many common cases will work. Situations that can break this
85+-- are:
86+--
87+-- * Requests which spoof headers and imply the connection is over HTTPS
88+--
89+-- * Reverse proxies that change ports in surprising ways
90+--
91+-- * Invalid Host headers
92+--
93+-- * Reverse proxies which modify the path info
94+--
95+-- Normally trusting headers in this way is insecure, however in the case of
96+-- approot, the worst that can happen is that the client will get an incorrect
97+-- URL. If you are relying on the approot for some security-sensitive purpose,
98+-- it is highly recommended to use @hardcoded@, which cannot be spoofed.
99+--
100+-- Since 3.0.7
101+fromRequest :: Middleware
102+fromRequest = approotMiddleware (return . guessApproot)
103+
104+data ApprootMiddlewareNotSetup = ApprootMiddlewareNotSetup
105+ deriving (Show, Typeable)
106+instance Exception ApprootMiddlewareNotSetup
107+
108+-- | Get the approot set by the middleware. If the middleware is not in use,
109+-- then this function will return an exception. For a total version of the
110+-- function, see 'getApprootMay'.
111+--
112+-- Since 3.0.7
113+getApproot :: Request -> ByteString
114+getApproot = fromMaybe (throw ApprootMiddlewareNotSetup) . getApprootMay
115+
116+-- | A total version of 'getApproot', which returns 'Nothing' if the middleware
117+-- is not in use.
118+--
119+-- Since 3.0.7
120+getApprootMay :: Request -> Maybe ByteString
121+getApprootMay req = V.lookup approotKey $ vault req
wai-extra/Network/Wai/Middleware/Autohead.hs+8−11
@@ -1,20 +1,17 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE CPP #-}
2-- | Automatically produce responses to HEAD requests based on the underlying
2-- | Automatically produce responses to HEAD requests based on the underlying
3-- applications GET response.
3-- applications GET response.
4module Network.Wai.Middleware.Autohead (autohead) where
4module Network.Wai.Middleware.Autohead (autohead) where
5
5
6import Network.Wai
6import Network.Wai
7−import Network.Wai.Internal
7+#if __GLASGOW_HASKELL__ < 710
8import Data.Monoid (mempty)
8import Data.Monoid (mempty)
9+#endif
9
10
10autohead :: Middleware
11autohead :: Middleware
11−autohead app req
12+autohead app req sendResponse
12− | requestMethod req == "HEAD" = do
13− res <- app req { requestMethod = "GET" }
13+ | requestMethod req == "HEAD" = app req { requestMethod = "GET" } $ \res -> do
14− let go (ResponseFile s hs _ _) = ResponseBuilder s hs mempty
14+ let (s, hs, _) = responseToStream res
15− go (ResponseBuilder s hs _) = ResponseBuilder s hs mempty
15+ sendResponse $ responseBuilder s hs mempty
16− go (ResponseSource s hs _) = ResponseBuilder s hs mempty
17− go (ResponseRaw raw r) = ResponseRaw raw (go r)
18− return (go res)
19− | otherwise = app req
16+ | otherwise = app req sendResponse
20
17
wai-extra/Network/Wai/Middleware/CleanPath.hs+8−6
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE CPP #-}
2module Network.Wai.Middleware.CleanPath
2module Network.Wai.Middleware.CleanPath
3 ( cleanPath
3 ( cleanPath
4 ) where
4 ) where
@@ -6,20 +6,22 @@ module Network.Wai.Middleware.CleanPath
6import Network.Wai
6import Network.Wai
7import qualified Data.ByteString.Char8 as B
7import qualified Data.ByteString.Char8 as B
8import qualified Data.ByteString.Lazy as L
8import qualified Data.ByteString.Lazy as L
9−import Network.HTTP.Types (status301)
9+import Network.HTTP.Types (status301, hLocation)
10import Data.Text (Text)
10import Data.Text (Text)
11+#if __GLASGOW_HASKELL__ < 710
11import Data.Monoid (mconcat)
12import Data.Monoid (mconcat)
13+#endif
12
14
13cleanPath :: ([Text] -> Either B.ByteString [Text])
15cleanPath :: ([Text] -> Either B.ByteString [Text])
14 -> B.ByteString
16 -> B.ByteString
15 -> ([Text] -> Application)
17 -> ([Text] -> Application)
16 -> Application
18 -> Application
17−cleanPath splitter prefix app env =
19+cleanPath splitter prefix app env sendResponse =
18 case splitter $ pathInfo env of
20 case splitter $ pathInfo env of
19− Right pieces -> app pieces env
21+ Right pieces -> app pieces env sendResponse
20− Left p -> return
22+ Left p -> sendResponse
21 $ responseLBS status301
23 $ responseLBS status301
22− [("Location", mconcat [prefix, p, suffix])]
24+ [(hLocation, mconcat [prefix, p, suffix])]
23 $ L.empty
25 $ L.empty
24 where
26 where
25 -- include the query string if present
27 -- include the query string if present
wai-extra/Network/Wai/Middleware/ForceDomain.hsnew file+38
@@ -0,0 +1,38 @@
1+-- |
2+--
3+-- @since 3.0.14
4+module Network.Wai.Middleware.ForceDomain where
5+
6+import Data.ByteString (ByteString)
7+import Data.Monoid ((<>), mempty)
8+import Network.HTTP.Types (hLocation, methodGet, status301, status307)
9+import Prelude
10+
11+import Network.Wai
12+import Network.Wai.Request
13+
14+-- | Force a domain by redirecting.
15+-- The `checkDomain` function takes the current domain and checks whether it is correct.
16+-- It should return `Nothing` if the domain is correct, or `Just "domain.com"` if it is incorrect.
17+--
18+-- @since 3.0.14
19+forceDomain :: (ByteString -> Maybe ByteString) -> Middleware
20+forceDomain checkDomain app req sendResponse =
21+ case requestHeaderHost req >>= checkDomain of
22+ Nothing ->
23+ app req sendResponse
24+ Just domain ->
25+ sendResponse $ redirectResponse domain
26+
27+ where
28+ -- From: Network.Wai.Middleware.ForceSSL
29+ redirectResponse domain =
30+ responseBuilder status [(hLocation, location domain)] mempty
31+
32+ location h =
33+ let p = if appearsSecure req then "https://" else "http://" in
34+ p <> h <> rawPathInfo req <> rawQueryString req
35+
36+ status
37+ | requestMethod req == methodGet = status301
38+ | otherwise = status307
wai-extra/Network/Wai/Middleware/ForceSSL.hsnew file+37
@@ -0,0 +1,37 @@
1+{-# LANGUAGE CPP #-}
2+-- | Redirect non-SSL requests to https
3+--
4+-- Since 3.0.7
5+module Network.Wai.Middleware.ForceSSL
6+ ( forceSSL
7+ ) where
8+
9+import Network.Wai
10+import Network.Wai.Request
11+
12+#if __GLASGOW_HASKELL__ < 710
13+import Control.Applicative ((<$>))
14+import Data.Monoid (mempty)
15+#endif
16+
17+import Data.Monoid ((<>))
18+import Network.HTTP.Types (hLocation, methodGet, status301, status307)
19+
20+-- | For requests that don't appear secure, redirect to https
21+--
22+-- Since 3.0.7
23+forceSSL :: Middleware
24+forceSSL app req sendResponse =
25+ case (appearsSecure req, redirectResponse req) of
26+ (False, Just resp) -> sendResponse resp
27+ _ -> app req sendResponse
28+
29+redirectResponse :: Request -> Maybe Response
30+redirectResponse req = do
31+ host <- requestHeaderHost req
32+ return $ responseBuilder status [(hLocation, location host)] mempty
33+ where
34+ location h = "https://" <> h <> rawPathInfo req <> rawQueryString req
35+ status
36+ | requestMethod req == methodGet = status301
37+ | otherwise = status307
wai-extra/Network/Wai/Middleware/Gzip.hs+130−51
@@ -1,6 +1,4 @@
1−{-# LANGUAGE Rank2Types #-}
1+{-# LANGUAGE Rank2Types, ScopedTypeVariables #-}
2−{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE OverloadedStrings #-}
4---------------------------------------------------------
2---------------------------------------------------------
5-- |
3-- |
6-- Module : Network.Wai.Middleware.Gzip
4-- Module : Network.Wai.Middleware.Gzip
@@ -28,41 +26,61 @@ import Network.Wai
28import Data.Maybe (fromMaybe, isJust)
26import Data.Maybe (fromMaybe, isJust)
29import qualified Data.ByteString.Char8 as S8
27import qualified Data.ByteString.Char8 as S8
30import qualified Data.ByteString as S
28import qualified Data.ByteString as S
31−import Data.Default
29+import Data.Default.Class
32−import Network.HTTP.Types (Status, Header)
30+import Network.HTTP.Types ( Status, Header, hContentEncoding, hUserAgent
33−import Control.Monad.IO.Class (liftIO)
31+ , hContentType, hContentLength)
34−import Control.Monad.Trans.Resource (runResourceT)
35import System.Directory (doesFileExist, createDirectoryIfMissing)
32import System.Directory (doesFileExist, createDirectoryIfMissing)
36−import qualified Data.Conduit as C
33+import Data.ByteString.Builder (byteString)
37−import qualified Data.Conduit.Zlib as CZ
38−import qualified Data.Conduit.Binary as CB
34+import qualified Data.ByteString.Builder.Extra as Blaze (flush)
39−import qualified Data.Conduit.List as CL
40−import Data.Conduit.Blaze (builderToByteStringFlush)
41−import Blaze.ByteString.Builder (fromByteString)
42import Control.Exception (try, SomeException)
35import Control.Exception (try, SomeException)
43import qualified Data.Set as Set
36import qualified Data.Set as Set
37+import Network.Wai.Header
44import Network.Wai.Internal
38import Network.Wai.Internal
39+import qualified Data.Streaming.ByteString.Builder as B
40+import qualified Data.Streaming.Zlib as Z
41+import Control.Monad (unless)
42+import Data.Function (fix)
43+import Control.Exception (throwIO)
44+import qualified System.IO as IO
45+import Data.ByteString.Lazy.Internal (defaultChunkSize)
46+import Data.Word8 (_semicolon)
45
47
46data GzipSettings = GzipSettings
48data GzipSettings = GzipSettings
47 { gzipFiles :: GzipFiles
49 { gzipFiles :: GzipFiles
48 , gzipCheckMime :: S.ByteString -> Bool
50 , gzipCheckMime :: S.ByteString -> Bool
49 }
51 }
50
52
53+-- | Gzip behavior for files.
54+data GzipFiles
55+ = GzipIgnore -- ^ Do not compress file responses.
56+ | GzipCompress -- ^ Compress files. Note that this may counteract
57+ -- zero-copy response optimizations on some
58+ -- platforms.
51−data GzipFiles = GzipIgnore | GzipCompress | GzipCacheFolder FilePath
59+ | GzipCacheFolder FilePath -- ^ Compress files, caching them in
60+ -- some directory.
61+ | GzipPreCompressed GzipFiles -- ^ If we use compression then try to use the filename with ".gz"
62+ -- appended to it, if the file is missing then try next action
63+ --
64+ -- @since 3.0.17
52 deriving (Show, Eq, Read)
65 deriving (Show, Eq, Read)
53
66
67+-- | Use default MIME settings; /do not/ compress files.
54instance Default GzipSettings where
68instance Default GzipSettings where
55 def = GzipSettings GzipIgnore defaultCheckMime
69 def = GzipSettings GzipIgnore defaultCheckMime
56
70
71+-- | MIME types that will be compressed by default:
72+-- @text/@ @*@, @application/json@, @application/javascript@,
73+-- @application/ecmascript@, @image/x-icon@.
57defaultCheckMime :: S.ByteString -> Bool
74defaultCheckMime :: S.ByteString -> Bool
58defaultCheckMime bs =
75defaultCheckMime bs =
59 S8.isPrefixOf "text/" bs || bs' `Set.member` toCompress
76 S8.isPrefixOf "text/" bs || bs' `Set.member` toCompress
60 where
77 where
61− bs' = fst $ S.breakByte 59 bs -- semicolon
78+ bs' = fst $ S.break (== _semicolon) bs
62 toCompress = Set.fromList
79 toCompress = Set.fromList
63 [ "application/json"
80 [ "application/json"
64 , "application/javascript"
81 , "application/javascript"
65 , "application/ecmascript"
82 , "application/ecmascript"
83+ , "image/x-icon"
66 ]
84 ]
67
85
68-- | Use gzip to compress the body of the response.
86-- | Use gzip to compress the body of the response.
@@ -70,47 +88,84 @@ defaultCheckMime bs =
70-- Analyzes the \"Accept-Encoding\" header from the client to determine
88-- Analyzes the \"Accept-Encoding\" header from the client to determine
71-- if gzip is supported.
89-- if gzip is supported.
72--
90--
73−-- Possible future enhancements:
91+-- File responses will be compressed according to the 'GzipFiles' setting.
74--
92--
75−-- * Only compress if the response is above a certain size.
93+-- Will only be applied based on the 'gzipCheckMime' setting. For default
94+-- behavior, see 'defaultCheckMime'.
76gzip :: GzipSettings -> Middleware
95gzip :: GzipSettings -> Middleware
77−gzip set app env = do
96+gzip set app env sendResponse = app env $ \res ->
78− res <- app env
79 case res of
97 case res of
98+ ResponseRaw{} -> sendResponse res
80− ResponseFile{} | gzipFiles set == GzipIgnore -> return res
99+ ResponseFile{} | gzipFiles set == GzipIgnore -> sendResponse res
81− _ -> if "gzip" `elem` enc && not isMSIE6 && not (isEncoded res)
100+ _ -> if "gzip" `elem` enc && not isMSIE6 && not (isEncoded res) && (bigEnough res)
82 then
101 then
83− case (res, gzipFiles set) of
102+ let runAction x = case x of
103+ (ResponseFile s hs file Nothing, GzipPreCompressed nextAction) ->
104+ let
105+ compressedVersion = file ++ ".gz"
106+ in
107+ doesFileExist compressedVersion >>= \y ->
108+ if y
109+ then (sendResponse $ ResponseFile s (fixHeaders hs) compressedVersion Nothing)
110+ else (runAction (ResponseFile s hs file Nothing, nextAction))
84− (ResponseFile s hs file Nothing, GzipCacheFolder cache) ->
111+ (ResponseFile s hs file Nothing, GzipCacheFolder cache) ->
85− case lookup "content-type" hs of
112+ case lookup hContentType hs of
86− Just m
113+ Just m
87− | gzipCheckMime set m -> liftIO $ compressFile s hs file cache
114+ | gzipCheckMime set m -> compressFile s hs file cache sendResponse
88− _ -> return res
115+ _ -> sendResponse res
116+ (ResponseFile {}, GzipIgnore) -> sendResponse res
89− _ -> return $ compressE set res
117+ _ -> compressE set res sendResponse
118+ in runAction (res, gzipFiles set)
90− else return res
119+ else sendResponse res
91 where
120 where
92 enc = fromMaybe [] $ (splitCommas . S8.unpack)
121 enc = fromMaybe [] $ (splitCommas . S8.unpack)
93 `fmap` lookup "Accept-Encoding" (requestHeaders env)
122 `fmap` lookup "Accept-Encoding" (requestHeaders env)
94− ua = fromMaybe "" $ lookup "user-agent" $ requestHeaders env
123+ ua = fromMaybe "" $ lookup hUserAgent $ requestHeaders env
95 isMSIE6 = "MSIE 6" `S.isInfixOf` ua
124 isMSIE6 = "MSIE 6" `S.isInfixOf` ua
96− isEncoded res = isJust $ lookup "Content-Encoding" $ responseHeaders res
125+ isEncoded res = isJust $ lookup hContentEncoding $ responseHeaders res
97
126
127+ bigEnough rsp = case contentLength (responseHeaders rsp) of
128+ Nothing -> True -- This could be a streaming case
129+ Just len -> len >= minimumLength
130+
131+ -- For a small enough response, gzipping will actually increase the size
132+ -- Potentially for anything less than 860 bytes gzipping could be a net loss
133+ -- The actual number is application specific though and may need to be adjusted
134+ -- http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits
135+ minimumLength = 860
136+
98−compressFile :: Status -> [Header] -> FilePath -> FilePath -> IO Response
137+compressFile :: Status -> [Header] -> FilePath -> FilePath -> (Response -> IO a) -> IO a
99−compressFile s hs file cache = do
138+compressFile s hs file cache sendResponse = do
100 e <- doesFileExist tmpfile
139 e <- doesFileExist tmpfile
101 if e
140 if e
102 then onSucc
141 then onSucc
103 else do
142 else do
104 createDirectoryIfMissing True cache
143 createDirectoryIfMissing True cache
105− x <-
144+ x <- try $
106− try $ runResourceT $ CB.sourceFile file
145+ IO.withBinaryFile file IO.ReadMode $ \inH ->
107− C.$$ CZ.gzip C.=$ CB.sinkFile tmpfile
146+ IO.withBinaryFile tmpfile IO.WriteMode $ \outH -> do
147+ deflate <- Z.initDeflate 7 $ Z.WindowBits 31
148+ -- FIXME this code should write to a temporary file, then
149+ -- rename to the final file
150+ let goPopper popper = fix $ \loop -> do
151+ res <- popper
152+ case res of
153+ Z.PRDone -> return ()
154+ Z.PRNext bs -> do
155+ S.hPut outH bs
156+ loop
157+ Z.PRError ex -> throwIO ex
158+ fix $ \loop -> do
159+ bs <- S.hGetSome inH defaultChunkSize
160+ unless (S.null bs) $ do
161+ Z.feedDeflate deflate bs >>= goPopper
162+ loop
163+ goPopper $ Z.finishDeflate deflate
108− either onErr (const onSucc) x
164+ either onErr (const onSucc) (x :: Either SomeException ()) -- FIXME bad! don't catch all exceptions like that!
109 where
165 where
110− onSucc = return $ ResponseFile s (fixHeaders hs) tmpfile Nothing
166+ onSucc = sendResponse $ responseFile s (fixHeaders hs) tmpfile Nothing
111
167
112− onErr :: SomeException -> IO Response
113− onErr = const $ return $ ResponseFile s hs file Nothing -- FIXME log the error message
168+ onErr _ = sendResponse $ responseFile s hs file Nothing -- FIXME log the error message
114
169
115 tmpfile = cache ++ '/' : map safe file
170 tmpfile = cache ++ '/' : map safe file
116 safe c
171 safe c
@@ -123,26 +178,50 @@ compressFile s hs file cache = do
123
178
124compressE :: GzipSettings
179compressE :: GzipSettings
125 -> Response
180 -> Response
126− -> Response
181+ -> (Response -> IO a)
182+ -> IO a
127−compressE set res =
183+compressE set res sendResponse =
128− case lookup "content-type" hs of
184+ case lookup hContentType hs of
129 Just m | gzipCheckMime set m ->
185 Just m | gzipCheckMime set m ->
130 let hs' = fixHeaders hs
186 let hs' = fixHeaders hs
131− in ResponseSource s hs' $ \f -> wb $ \b -> f $
187+ in wb $ \body -> sendResponse $ responseStream s hs' $ \sendChunk flush -> do
132− b C.$= builderToByteStringFlush
188+ (blazeRecv, _) <- B.newBuilderRecv B.defaultStrategy
133− C.$= CZ.compressFlush 1 (CZ.WindowBits 31)
189+ deflate <- Z.initDeflate 1 (Z.WindowBits 31)
134− C.$= CL.map (fmap fromByteString)
190+ let sendBuilder builder = do
191+ popper <- blazeRecv builder
192+ fix $ \loop -> do
193+ bs <- popper
194+ unless (S.null bs) $ do
195+ sendBS bs
196+ loop
197+ sendBS bs = Z.feedDeflate deflate bs >>= deflatePopper
198+ flushBuilder = do
199+ sendBuilder Blaze.flush
200+ deflatePopper $ Z.flushDeflate deflate
201+ flush
202+ deflatePopper popper = fix $ \loop -> do
203+ result <- popper
204+ case result of
205+ Z.PRDone -> return ()
206+ Z.PRNext bs' -> do
207+ sendChunk $ byteString bs'
208+ loop
209+ Z.PRError e -> throwIO e
210+
211+ body sendBuilder flushBuilder
212+ sendBuilder Blaze.flush
213+ deflatePopper $ Z.finishDeflate deflate
135− _ -> res
214+ _ -> sendResponse res
136 where
215 where
137− (s, hs, wb) = responseToSource res
216+ (s, hs, wb) = responseToStream res
138
217
139-- Remove Content-Length header, since we will certainly have a
218-- Remove Content-Length header, since we will certainly have a
140-- different length after gzip compression.
219-- different length after gzip compression.
141fixHeaders :: [Header] -> [Header]
220fixHeaders :: [Header] -> [Header]
142fixHeaders =
221fixHeaders =
143− (("Content-Encoding", "gzip") :) . filter notLength
222+ ((hContentEncoding, "gzip") :) . filter notLength
144 where
223 where
145− notLength (x, _) = x /= "content-length"
224+ notLength (x, _) = x /= hContentLength
146
225
147splitCommas :: String -> [String]
226splitCommas :: String -> [String]
148splitCommas [] = []
227splitCommas [] = []
wai-extra/Network/Wai/Middleware/HttpAuth.hs+68−33
@@ -1,23 +1,32 @@
1−{-# LANGUAGE RecordWildCards, OverloadedStrings #-}
1+{-# LANGUAGE RecordWildCards, TupleSections, CPP #-}
2-- | Implements HTTP Basic Authentication.
2-- | Implements HTTP Basic Authentication.
3--
3--
4-- This module may add digest authentication in the future.
4-- This module may add digest authentication in the future.
5module Network.Wai.Middleware.HttpAuth
5module Network.Wai.Middleware.HttpAuth
6+ ( -- * Middleware
6− ( basicAuth
7+ basicAuth
8+ , basicAuth'
7 , CheckCreds
9 , CheckCreds
8 , AuthSettings
10 , AuthSettings
9 , authRealm
11 , authRealm
10 , authOnNoAuth
12 , authOnNoAuth
11 , authIsProtected
13 , authIsProtected
14+ -- * Helping functions
15+ , extractBasicAuth
16+ , extractBearerAuth
12 ) where
17 ) where
13−
18+#if __GLASGOW_HASKELL__ < 710
14−import Network.Wai
19+import Control.Applicative
15−import Network.HTTP.Types (status401)
20+#endif
16import Data.ByteString (ByteString)
21import Data.ByteString (ByteString)
17−import qualified Data.ByteString as S
22+import Data.ByteString.Base64 (decodeLenient)
18import Data.String (IsString (..))
23import Data.String (IsString (..))
19import Data.Word8 (isSpace, _colon, toLower)
24import Data.Word8 (isSpace, _colon, toLower)
20−import Data.ByteString.Base64 (decodeLenient)
25+import Network.HTTP.Types (status401, hContentType, hAuthorization)
26+import Network.Wai
27+
28+import qualified Data.ByteString as S
29+
21
30
22-- | Check if a given username and password is valid.
31-- | Check if a given username and password is valid.
23type CheckCreds = ByteString
32type CheckCreds = ByteString
@@ -28,64 +37,63 @@ type CheckCreds = ByteString
28--
37--
29-- > basicAuth (\u p -> return $ u == "michael" && p == "mypass") "My Realm"
38-- > basicAuth (\u p -> return $ u == "michael" && p == "mypass") "My Realm"
30--
39--
31−-- Since 1.3.4
40+-- @since 1.3.4
32basicAuth :: CheckCreds
41basicAuth :: CheckCreds
33 -> AuthSettings
42 -> AuthSettings
34 -> Middleware
43 -> Middleware
35−basicAuth checkCreds AuthSettings {..} app req = do
44+basicAuth checkCreds = basicAuth' (\_ -> checkCreds)
45+
46+-- | Like 'basicAuth', but also passes a request to the authentication function.
47+--
48+-- @since 3.0.19
49+basicAuth' :: (Request -> CheckCreds)
50+ -> AuthSettings
51+ -> Middleware
52+basicAuth' checkCreds AuthSettings {..} app req sendResponse = do
36 isProtected <- authIsProtected req
53 isProtected <- authIsProtected req
37 allowed <- if isProtected then check else return True
54 allowed <- if isProtected then check else return True
38 if allowed
55 if allowed
39− then app req
56+ then app req sendResponse
40− else authOnNoAuth authRealm req
57+ else authOnNoAuth authRealm req sendResponse
41 where
58 where
42 check =
59 check =
43− case lookup "Authorization" $ requestHeaders req of
60+ case (lookup hAuthorization $ requestHeaders req)
61+ >>= extractBasicAuth of
44 Nothing -> return False
62 Nothing -> return False
45− Just bs ->
46− let (x, y) = S.break isSpace bs
47− in if S.map toLower x == "basic"
48− then checkB64 $ S.dropWhile isSpace y
49− else return False
50− checkB64 encoded =
51− case S.uncons password' of
52− Just (_, password) -> checkCreds username password
63+ Just (username, password) -> checkCreds req username password
53− Nothing -> return False
54− where
55− raw = decodeLenient encoded
56− (username, password') = S.breakByte _colon raw
57
64
58−-- | Authentication settings. This value is an instance of @IsString@, so the
65+-- | Basic authentication settings. This value is an instance of
59−-- recommended approach to create a value is to provide a string literal (which
66+-- @IsString@, so the recommended approach to create a value is to
67+-- provide a string literal (which will be the realm) and then
60−-- will be the realm) and then overriding individual fields.
68+-- overriding individual fields.
61--
69--
62-- > "My Realm" { authIsProtected = someFunc } :: AuthSettings
70-- > "My Realm" { authIsProtected = someFunc } :: AuthSettings
63--
71--
64−-- Since 1.3.4
72+-- @since 1.3.4
65data AuthSettings = AuthSettings
73data AuthSettings = AuthSettings
66 { authRealm :: !ByteString
74 { authRealm :: !ByteString
67 -- ^
75 -- ^
68 --
76 --
69− -- Since 1.3.4
77+ -- @since 1.3.4
70 , authOnNoAuth :: !(ByteString -> Application)
78 , authOnNoAuth :: !(ByteString -> Application)
71 -- ^ Takes the realm and returns an appropriate 401 response when
79 -- ^ Takes the realm and returns an appropriate 401 response when
72 -- authentication is not provided.
80 -- authentication is not provided.
73 --
81 --
74− -- Since 1.3.4
82+ -- @since 1.3.4
75 , authIsProtected :: !(Request -> IO Bool)
83 , authIsProtected :: !(Request -> IO Bool)
76 -- ^ Determine if access to the requested resource is restricted.
84 -- ^ Determine if access to the requested resource is restricted.
77 --
85 --
78 -- Default: always returns @True@.
86 -- Default: always returns @True@.
79 --
87 --
80− -- Since 1.3.4
88+ -- @since 1.3.4
81 }
89 }
82
90
83instance IsString AuthSettings where
91instance IsString AuthSettings where
84 fromString s = AuthSettings
92 fromString s = AuthSettings
85 { authRealm = fromString s
93 { authRealm = fromString s
86− , authOnNoAuth = \realm _req -> return $ responseLBS
94+ , authOnNoAuth = \realm _req f -> f $ responseLBS
87 status401
95 status401
88− [ ("Content-Type", "text/plain")
96+ [ (hContentType, "text/plain")
89 , ("WWW-Authenticate", S.concat
97 , ("WWW-Authenticate", S.concat
90 [ "Basic realm=\""
98 [ "Basic realm=\""
91 , realm
99 , realm
@@ -95,3 +103,30 @@ instance IsString AuthSettings where
95 "Basic authentication is required"
103 "Basic authentication is required"
96 , authIsProtected = const $ return True
104 , authIsProtected = const $ return True
97 }
105 }
106+
107+-- | Extract basic authentication data from usually __Authorization__
108+-- header value. Returns username and password
109+--
110+-- @since 3.0.5
111+extractBasicAuth :: ByteString -> Maybe (ByteString, ByteString)
112+extractBasicAuth bs =
113+ let (x, y) = S.break isSpace bs
114+ in if S.map toLower x == "basic"
115+ then extract $ S.dropWhile isSpace y
116+ else Nothing
117+ where
118+ extract encoded =
119+ let raw = decodeLenient encoded
120+ (username, password') = S.break (== _colon) raw
121+ in ((username,) . snd) <$> S.uncons password'
122+
123+-- | Extract bearer authentication data from __Authorization__ header
124+-- value. Returns bearer token
125+--
126+-- @since 3.0.5
127+extractBearerAuth :: ByteString -> Maybe ByteString
128+extractBearerAuth bs =
129+ let (x, y) = S.break isSpace bs
130+ in if S.map toLower x == "bearer"
131+ then Just $ S.dropWhile isSpace y
132+ else Nothing
wai-extra/Network/Wai/Middleware/Jsonp.hs+26−32
@@ -1,5 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE RankNTypes, CPP #-}
3---------------------------------------------------------
2---------------------------------------------------------
4-- |
3-- |
5-- Module : Network.Wai.Middleware.Jsonp
4-- Module : Network.Wai.Middleware.Jsonp
@@ -19,15 +18,15 @@ import Network.Wai
19import Network.Wai.Internal
18import Network.Wai.Internal
20import Data.ByteString (ByteString)
19import Data.ByteString (ByteString)
21import qualified Data.ByteString.Char8 as B8
20import qualified Data.ByteString.Char8 as B8
22−import Blaze.ByteString.Builder (Builder, copyByteString)
21+import Data.ByteString.Builder.Extra (byteStringCopy)
23−import Blaze.ByteString.Builder.Char8 (fromChar)
22+import Data.ByteString.Builder (char7)
23+#if __GLASGOW_HASKELL__ < 710
24import Data.Monoid (mappend)
24import Data.Monoid (mappend)
25+#endif
25import Control.Monad (join)
26import Control.Monad (join)
26import Data.Maybe (fromMaybe)
27import Data.Maybe (fromMaybe)
27import qualified Data.ByteString as S
28import qualified Data.ByteString as S
28−import qualified Data.Conduit as C
29−import Data.CaseInsensitive (CI)
30−import Network.HTTP.Types (Status)
29+import Network.HTTP.Types (hAccept, hContentType)
31
30
32-- | Wrap json responses in a jsonp callback.
31-- | Wrap json responses in a jsonp callback.
33--
32--
@@ -37,8 +36,8 @@ import Network.HTTP.Types (Status)
37-- having a content type of \"text\/javascript\" and calling the specified
36-- having a content type of \"text\/javascript\" and calling the specified
38-- callback function.
37-- callback function.
39jsonp :: Middleware
38jsonp :: Middleware
40−jsonp app env = do
39+jsonp app env sendResponse = do
41− let accept = fromMaybe B8.empty $ lookup "Accept" $ requestHeaders env
40+ let accept = fromMaybe B8.empty $ lookup hAccept $ requestHeaders env
42 let callback :: Maybe B8.ByteString
41 let callback :: Maybe B8.ByteString
43 callback =
42 callback =
44 if B8.pack "text/javascript" `B8.isInfixOf` accept
43 if B8.pack "text/javascript" `B8.isInfixOf` accept
@@ -48,48 +47,43 @@ jsonp app env = do
48 case callback of
47 case callback of
49 Nothing -> env
48 Nothing -> env
50 Just _ -> env
49 Just _ -> env
51− { requestHeaders = changeVal "Accept"
50+ { requestHeaders = changeVal hAccept
52 "application/json"
51 "application/json"
53 $ requestHeaders env
52 $ requestHeaders env
54 }
53 }
55− res <- app env'
54+ app env' $ \res ->
56− return $ case callback of
55+ case callback of
57− Nothing -> res
56+ Nothing -> sendResponse res
58− Just c -> go c res
57+ Just c -> go c res
59 where
58 where
60 go c r@(ResponseBuilder s hs b) =
59 go c r@(ResponseBuilder s hs b) =
61− case checkJSON hs of
60+ sendResponse $ case checkJSON hs of
62 Nothing -> r
61 Nothing -> r
63− Just hs' -> ResponseBuilder s hs' $
62+ Just hs' -> responseBuilder s hs' $
64− copyByteString c
63+ byteStringCopy c
65− `mappend` fromChar '('
64+ `mappend` char7 '('
66 `mappend` b
65 `mappend` b
67− `mappend` fromChar ')'
66+ `mappend` char7 ')'
68 go c r =
67 go c r =
69 case checkJSON hs of
68 case checkJSON hs of
70 Just hs' -> addCallback c s hs' wb
69 Just hs' -> addCallback c s hs' wb
71− Nothing -> r
70+ Nothing -> sendResponse r
72 where
71 where
73− (s, hs, wb) = responseToSource r
72+ (s, hs, wb) = responseToStream r
74
73
75 checkJSON hs =
74 checkJSON hs =
76− case lookup "Content-Type" hs of
75+ case lookup hContentType hs of
77 Just x
76 Just x
78 | B8.pack "application/json" `S.isPrefixOf` x ->
77 | B8.pack "application/json" `S.isPrefixOf` x ->
79 Just $ fixHeaders hs
78 Just $ fixHeaders hs
80 _ -> Nothing
79 _ -> Nothing
81− fixHeaders = changeVal "Content-Type" "text/javascript"
80+ fixHeaders = changeVal hContentType "text/javascript"
82
81
83− addCallback :: ByteString
84− -> Status
85− -> [(CI ByteString, ByteString)]
86− -> (forall b. WithSource IO (C.Flush Builder) b)
87− -> Response
88 addCallback cb s hs wb =
82 addCallback cb s hs wb =
89− ResponseSource s hs $ \f -> wb $ \b -> f $
83+ wb $ \body -> sendResponse $ responseStream s hs $ \sendChunk flush -> do
90− C.yield (C.Chunk $ copyByteString cb `mappend` fromChar '(')
84+ sendChunk $ byteStringCopy cb `mappend` char7 '('
91− `mappend` b
85+ _ <- body sendChunk flush
92− `mappend` C.yield (C.Chunk $ fromChar ')')
86+ sendChunk $ char7 ')'
93
87
94changeVal :: Eq a
88changeVal :: Eq a
95 => a
89 => a
wai-extra/Network/Wai/Middleware/Local.hsnew file+26
@@ -0,0 +1,26 @@
1+{-# LANGUAGE CPP #-}
2+-- | Only allow local connections.
3+--
4+module Network.Wai.Middleware.Local
5+ ( local
6+ ) where
7+
8+import Network.Wai (Middleware,remoteHost, Response)
9+import Network.Socket (SockAddr(..))
10+
11+-- | This middleware rejects non-local connections with a specific response.
12+-- It is useful when supporting web-based local applications, which would
13+-- typically want to reject external connections.
14+
15+local :: Response -> Middleware
16+local resp f r k = case remoteHost r of
17+ SockAddrInet _ h | h == fromIntegral home
18+ -> f r k
19+#if !defined(mingw32_HOST_OS) && !defined(_WIN32)
20+ SockAddrUnix _ -> f r k
21+#endif
22+ _ -> k $ resp
23+ where
24+ home :: Integer
25+ home = 127 + (256 * 256 * 256) * 1
26+
wai-extra/Network/Wai/Middleware/MethodOverride.hs+4−6
@@ -1,4 +1,3 @@
1−{-# LANGUAGE OverloadedStrings #-}
2module Network.Wai.Middleware.MethodOverride
1module Network.Wai.Middleware.MethodOverride
3 ( methodOverride
2 ( methodOverride
4 ) where
3 ) where
@@ -6,12 +5,11 @@ module Network.Wai.Middleware.MethodOverride
6import Network.Wai
5import Network.Wai
7import Control.Monad (join)
6import Control.Monad (join)
8
7
9−-- | Allows overriding of the HTTP request method via the _method query string
8+-- | Overriding of HTTP request method via `_method` query string parameter.
10−-- parameter.
11--
9--
12−-- This middleware only applies when the initial request method is POST. This
10+-- This middleware only applies when the initial request method is POST.
13−-- allow submitting of normal HTML forms, without worries of semantics
11+-- Allows submitting of normal HTML forms, without worries of semantic
14−-- mismatches in the HTTP spec.
12+-- mismatches with the HTTP spec.
15methodOverride :: Middleware
13methodOverride :: Middleware
16methodOverride app req =
14methodOverride app req =
17 app req'
15 app req'
wai-extra/Network/Wai/Middleware/MethodOverridePost.hs+18−12
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE CPP #-}
2-----------------------------------------------------------------
2-----------------------------------------------------------------
3-- | Module : Network.Wai.Middleware.MethodOverridePost
3-- | Module : Network.Wai.Middleware.MethodOverridePost
4--
4--
@@ -9,10 +9,13 @@ module Network.Wai.Middleware.MethodOverridePost
9 ) where
9 ) where
10
10
11import Network.Wai
11import Network.Wai
12−import Network.HTTP.Types (parseQuery)
12+import Network.HTTP.Types (parseQuery, hContentType)
13+
14+#if __GLASGOW_HASKELL__ < 710
13−import Data.Monoid (mconcat)
15+import Data.Monoid (mconcat, mempty)
16+#endif
17+import Data.IORef
14−import Data.Conduit.Lazy (lazyConsume)
18+import Data.ByteString.Lazy (toChunks)
15−import Data.Conduit.List (sourceList)
16
19
17-- | Allows overriding of the HTTP request method via the _method post string parameter.
20-- | Allows overriding of the HTTP request method via the _method post string parameter.
18--
21--
@@ -26,13 +29,16 @@ import Data.Conduit.List (sourceList)
26-- * This middleware only applies when the initial request method is POST.
29-- * This middleware only applies when the initial request method is POST.
27--
30--
28methodOverridePost :: Middleware
31methodOverridePost :: Middleware
32+methodOverridePost app req send =
29−methodOverridePost app req = case (requestMethod req, lookup "Content-Type" (requestHeaders req)) of
33+ case (requestMethod req, lookup hContentType (requestHeaders req)) of
30− ("POST", Just "application/x-www-form-urlencoded") -> setPost req >>= app
34+ ("POST", Just "application/x-www-form-urlencoded") -> setPost req >>= flip app send
31− _ -> app req
35+ _ -> app req send
32
36
33setPost :: Request -> IO Request
37setPost :: Request -> IO Request
34setPost req = do
38setPost req = do
35− body <- lazyConsume (requestBody req)
39+ body <- (mconcat . toChunks) `fmap` lazyRequestBody req
40+ ref <- newIORef body
41+ let rb = atomicModifyIORef ref $ \bs -> (mempty, bs)
36− case parseQuery (mconcat body) of
42+ case parseQuery body of
37− (("_method", Just newmethod):_) -> return $ req {requestBody = sourceList body, requestMethod = newmethod}
43+ (("_method", Just newmethod):_) -> return $ req {requestBody = rb, requestMethod = newmethod}
38− _ -> return $ req {requestBody = sourceList body}
44+ _ -> return $ req {requestBody = rb}
wai-extra/Network/Wai/Middleware/RequestLogger.hs+241−158
@@ -1,6 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE RecordWildCards #-}
1{-# LANGUAGE RecordWildCards #-}
3−
4-- NOTE: Due to https://github.com/yesodweb/wai/issues/192, this module should
2-- NOTE: Due to https://github.com/yesodweb/wai/issues/192, this module should
5-- not use CPP.
3-- not use CPP.
6module Network.Wai.Middleware.RequestLogger
4module Network.Wai.Middleware.RequestLogger
@@ -15,48 +13,91 @@ module Network.Wai.Middleware.RequestLogger
15 , destination
13 , destination
16 , OutputFormat (..)
14 , OutputFormat (..)
17 , OutputFormatter
15 , OutputFormatter
16+ , OutputFormatterWithDetails
17+ , OutputFormatterWithDetailsAndHeaders
18 , Destination (..)
18 , Destination (..)
19 , Callback
19 , Callback
20 , IPAddrSource (..)
20 , IPAddrSource (..)
21 ) where
21 ) where
22
22
23−import System.IO (Handle, stdout)
23+import System.IO (Handle, hFlush, stdout)
24+import qualified Data.ByteString.Builder as B (Builder, byteString)
24import qualified Data.ByteString as BS
25import qualified Data.ByteString as BS
25−import Data.ByteString.Char8 (pack, unpack)
26+import Data.ByteString.Char8 (pack)
27+import Control.Monad (when)
26import Control.Monad.IO.Class (liftIO)
28import Control.Monad.IO.Class (liftIO)
29+import Network.Wai
30+ ( Request(..), requestBodyLength, RequestBodyLength(..)
31+ , Middleware
27−import Network.Wai (Request(..), Middleware, responseStatus, Response, responseHeaders)
32+ , Response, responseStatus, responseHeaders
33+ )
28import System.Log.FastLogger
34import System.Log.FastLogger
29import Network.HTTP.Types as H
35import Network.HTTP.Types as H
30import Data.Maybe (fromMaybe)
36import Data.Maybe (fromMaybe)
31−import Data.Monoid (mconcat)
37+import Data.Monoid (mconcat, (<>))
32−
38+import Data.Time (getCurrentTime, diffUTCTime, NominalDiffTime)
33−import Network.Wai.Parse (sinkRequestBody, lbsBackEnd, fileName, Param, File, getRequestBodyType)
39+import Network.Wai.Parse (sinkRequestBody, lbsBackEnd, fileName, Param, File
40+ , getRequestBodyType)
34import qualified Data.ByteString.Lazy as LBS
41import qualified Data.ByteString.Lazy as LBS
35import qualified Data.ByteString.Char8 as S8
42import qualified Data.ByteString.Char8 as S8
36−
37−import qualified Data.Conduit as C
38−import qualified Data.Conduit.List as CL
39−
40import System.Console.ANSI
43import System.Console.ANSI
41−import Data.IORef.Lifted
44+import Data.IORef
42import System.IO.Unsafe
45import System.IO.Unsafe
43−
46+import Network.Wai.Internal (Response (..))
44−import Data.Default (Default (def))
47+import Data.Default.Class (Default (def))
45import Network.Wai.Logger
48import Network.Wai.Logger
46import Network.Wai.Middleware.RequestLogger.Internal
49import Network.Wai.Middleware.RequestLogger.Internal
50+import Network.Wai.Header (contentLength)
51+import Data.Text.Encoding (decodeUtf8')
47
52
53+data OutputFormat
48−data OutputFormat = Apache IPAddrSource
54+ = Apache IPAddrSource
49− | Detailed Bool -- ^ use colors?
55+ | Detailed Bool -- ^ use colors?
50− | CustomOutputFormat OutputFormatter
56+ | CustomOutputFormat OutputFormatter
57+ | CustomOutputFormatWithDetails OutputFormatterWithDetails
58+ | CustomOutputFormatWithDetailsAndHeaders OutputFormatterWithDetailsAndHeaders
51
59
52type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> LogStr
60type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> LogStr
53
61
62+type OutputFormatterWithDetails
63+ = ZonedDate
64+ -> Request
65+ -> Status
66+ -> Maybe Integer
67+ -> NominalDiffTime
68+ -> [S8.ByteString]
69+ -> B.Builder
70+ -> LogStr
71+
72+-- | Same as @OutputFormatterWithDetails@ but with response headers included
73+--
74+-- This is useful if you wish to include arbitrary application data in your
75+-- logs, e.g., an authenticated user ID, which you would set in a response
76+-- header in your application and retrieve in the log formatter.
77+--
78+-- @since 3.0.27
79+type OutputFormatterWithDetailsAndHeaders
80+ = ZonedDate -- ^ When the log message was generated
81+ -> Request -- ^ The WAI request
82+ -> Status -- ^ HTTP status code
83+ -> Maybe Integer -- ^ Response size
84+ -> NominalDiffTime -- ^ Duration of the request
85+ -> [S8.ByteString] -- ^ The request body
86+ -> B.Builder -- ^ Raw response
87+ -> [Header] -- ^ The response headers
88+ -> LogStr
89+
54data Destination = Handle Handle
90data Destination = Handle Handle
55 | Logger LoggerSet
91 | Logger LoggerSet
56 | Callback Callback
92 | Callback Callback
57
93
58type Callback = LogStr -> IO ()
94type Callback = LogStr -> IO ()
59
95
96+-- | @RequestLoggerSettings@ is an instance of Default. See <https://hackage.haskell.org/package/data-default Data.Default> for more information.
97+--
98+-- @outputFormat@, @autoFlush@, and @destination@ are record fields
99+-- for the record type @RequestLoggerSettings@, so they can be used to
100+-- modify settings values using record syntax.
60data RequestLoggerSettings = RequestLoggerSettings
101data RequestLoggerSettings = RequestLoggerSettings
61 {
102 {
62 -- | Default value: @Detailed@ @True@.
103 -- | Default value: @Detailed@ @True@.
@@ -80,68 +121,88 @@ mkRequestLogger :: RequestLoggerSettings -> IO Middleware
80mkRequestLogger RequestLoggerSettings{..} = do
121mkRequestLogger RequestLoggerSettings{..} = do
81 let (callback, flusher) =
122 let (callback, flusher) =
82 case destination of
123 case destination of
83− Handle h -> (BS.hPutStr h . logToByteString, return ())
124+ Handle h -> (BS.hPutStr h . logToByteString, when autoFlush (hFlush h))
84− Logger l -> (pushLogStr l, flushLogStr l)
125+ Logger l -> (pushLogStr l, when autoFlush (flushLogStr l))
85 Callback c -> (c, return ())
126 Callback c -> (c, return ())
127+ callbackAndFlush str = callback str >> flusher
86 case outputFormat of
128 case outputFormat of
87 Apache ipsrc -> do
129 Apache ipsrc -> do
88 getdate <- getDateGetter flusher
130 getdate <- getDateGetter flusher
89− apache <- initLogger ipsrc (LogCallback callback (return ())) getdate
131+ apache <- initLogger ipsrc (LogCallback callback flusher) getdate
90 return $ apacheMiddleware apache
132 return $ apacheMiddleware apache
91− Detailed useColors -> detailedMiddleware
133+ Detailed useColors -> detailedMiddleware callbackAndFlush useColors
92− (\str -> callback str >> flusher)
93− useColors
94 CustomOutputFormat formatter -> do
134 CustomOutputFormat formatter -> do
135+ getDate <- getDateGetter flusher
136+ return $ customMiddleware callbackAndFlush getDate formatter
137+ CustomOutputFormatWithDetails formatter -> do
95 getdate <- getDateGetter flusher
138 getdate <- getDateGetter flusher
96− return $ customMiddleware callback getdate formatter
139+ return $ customMiddlewareWithDetails callbackAndFlush getdate formatter
140+ CustomOutputFormatWithDetailsAndHeaders formatter -> do
141+ getdate <- getDateGetter flusher
142+ return $ customMiddlewareWithDetailsAndHeaders callbackAndFlush getdate formatter
97
143
98apacheMiddleware :: ApacheLoggerActions -> Middleware
144apacheMiddleware :: ApacheLoggerActions -> Middleware
99−apacheMiddleware ala app req = do
145+apacheMiddleware ala app req sendResponse = app req $ \res -> do
100− res <- app req
101− let msize = lookup "content-length" (responseHeaders res) >>= readInt'
146+ let msize = contentLength (responseHeaders res)
102− readInt' bs =
103− case S8.readInteger bs of
104− Just (i, "") -> Just i
105− _ -> Nothing
106 apacheLogger ala req (responseStatus res) msize
147 apacheLogger ala req (responseStatus res) msize
107− return res
148+ sendResponse res
108
149
109customMiddleware :: Callback -> IO ZonedDate -> OutputFormatter -> Middleware
150customMiddleware :: Callback -> IO ZonedDate -> OutputFormatter -> Middleware
110−customMiddleware cb getdate formatter app req = do
151+customMiddleware cb getdate formatter app req sendResponse = app req $ \res -> do
111− res <- app req
112 date <- liftIO getdate
152 date <- liftIO getdate
113− -- We use Nothing for the response size since we generally don't know it
153+ let msize = contentLength (responseHeaders res)
154+ liftIO $ cb $ formatter date req (responseStatus res) msize
155+ sendResponse res
156+
157+customMiddlewareWithDetails :: Callback -> IO ZonedDate -> OutputFormatterWithDetails -> Middleware
158+customMiddlewareWithDetails cb getdate formatter app req sendResponse = do
159+ (req', reqBody) <- getRequestBody req
160+ t0 <- getCurrentTime
161+ app req' $ \res -> do
162+ t1 <- getCurrentTime
163+ date <- liftIO getdate
164+ let msize = contentLength (responseHeaders res)
165+ builderIO <- newIORef $ B.byteString ""
166+ res' <- recordChunks builderIO res
167+ rspRcv <- sendResponse res'
168+ _ <- liftIO . cb .
114− liftIO $ cb $ formatter date req (responseStatus res) Nothing
169+ formatter date req' (responseStatus res') msize (t1 `diffUTCTime` t0) reqBody =<<
170+ readIORef builderIO
115− return res
171+ return rspRcv
116−
172+
173+customMiddlewareWithDetailsAndHeaders :: Callback -> IO ZonedDate -> OutputFormatterWithDetailsAndHeaders -> Middleware
174+customMiddlewareWithDetailsAndHeaders cb getdate formatter app req sendResponse = do
175+ (req', reqBody) <- getRequestBody req
176+ t0 <- getCurrentTime
177+ app req' $ \res -> do
178+ t1 <- getCurrentTime
179+ date <- liftIO getdate
180+ let msize = contentLength (responseHeaders res)
181+ builderIO <- newIORef $ B.byteString ""
182+ res' <- recordChunks builderIO res
183+ rspRcv <- sendResponse res'
184+ _ <- do
185+ rawResponse <- readIORef builderIO
186+ let status = responseStatus res'
187+ duration = t1 `diffUTCTime` t0
188+ resHeaders = responseHeaders res'
189+ liftIO . cb $ formatter date req' status msize duration reqBody rawResponse resHeaders
190+ return rspRcv
117-- | Production request logger middleware.
191-- | Production request logger middleware.
118−-- Implemented on top of "logCallback", but prints to 'stdout'
192+--
193+-- This uses the 'Apache' logging format, and takes IP addresses for clients from
194+-- the socket (see 'IPAddrSource' for more information). It logs to 'stdout'.
195+{-# NOINLINE logStdout #-}
119logStdout :: Middleware
196logStdout :: Middleware
120logStdout = unsafePerformIO $ mkRequestLogger def { outputFormat = Apache FromSocket }
197logStdout = unsafePerformIO $ mkRequestLogger def { outputFormat = Apache FromSocket }
121
198
122-- | Development request logger middleware.
199-- | Development request logger middleware.
123−-- Implemented on top of "logCallbackDev", but prints to 'stdout'
124--
200--
125−-- Flushes 'stdout' on each request, which would be inefficient in production use.
201+-- This uses the 'Detailed' 'True' logging format and logs to 'stdout'.
126−-- Use "logStdout" in production.
202+{-# NOINLINE logStdoutDev #-}
127logStdoutDev :: Middleware
203logStdoutDev :: Middleware
128logStdoutDev = unsafePerformIO $ mkRequestLogger def
204logStdoutDev = unsafePerformIO $ mkRequestLogger def
129
205
130−-- no black or white which are expected to be existing terminal colors.
131−colors0 :: [Color]
132−colors0 = [
133− Red
134− , Green
135− , Yellow
136− , Blue
137− , Magenta
138− , Cyan
139− ]
140−
141−rotateColors :: [Color] -> ([Color], Color)
142−rotateColors [] = error "Impossible! There must be colors!"
143−rotateColors (c:cs) = (cs ++ [c], c)
144−
145-- | Prints a message using the given callback function for each request.
206-- | Prints a message using the given callback function for each request.
146-- This is not for serious production use- it is inefficient.
207-- This is not for serious production use- it is inefficient.
147-- It immediately consumes a POST body and fills it back in and is otherwise inefficient
208-- It immediately consumes a POST body and fills it back in and is otherwise inefficient
@@ -150,119 +211,148 @@ rotateColors (c:cs) = (cs ++ [c], c)
150-- This meanst that you can accurately see the interleaving of requests.
211-- This meanst that you can accurately see the interleaving of requests.
151-- And if the app crashes you have still logged the request.
212-- And if the app crashes you have still logged the request.
152-- However, if you are simulating 10 simultaneous users you may find this confusing.
213-- However, if you are simulating 10 simultaneous users you may find this confusing.
153−-- The request and response are connected by color on Unix and also by the request path.
154--
214--
155-- This is lower-level - use 'logStdoutDev' unless you need greater control.
215-- This is lower-level - use 'logStdoutDev' unless you need greater control.
156--
216--
157-- Example ouput:
217-- Example ouput:
158--
218--
159-- > GET search
219-- > GET search
160−-- > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
220+-- > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
161−-- >
162−-- > Status: 200 OK. search
221+-- > Status: 200 OK 0.010555s
163-- >
222-- >
164-- > GET static/css/normalize.css
223-- > GET static/css/normalize.css
224+-- > Params: [("LXwioiBG","")]
165−-- > Accept: text/css,*/*;q=0.1
225+-- > Accept: text/css,*/*;q=0.1
166−-- > GET [("LXwioiBG","")]
167−-- >
168−-- > Status: 304 Not Modified. static/css/normalize.css
226+-- > Status: 304 Not Modified 0.010555s
169
227
170detailedMiddleware :: Callback -> Bool -> IO Middleware
228detailedMiddleware :: Callback -> Bool -> IO Middleware
171−detailedMiddleware cb useColors = do
229+detailedMiddleware cb useColors =
172− getAddColor <-
230+ let (ansiColor, ansiMethod, ansiStatusCode) =
173− if useColors
231+ if useColors
174− then do
232+ then (ansiColor', ansiMethod', ansiStatusCode')
175− icolors <- newIORef colors0
233+ else (\_ t -> [t], (:[]), \_ t -> [t])
176− return $ do
234+
177− color <- liftIO $ atomicModifyIORef icolors rotateColors
178− return $ ansiColor color
179− else return (return return)
180− return $ detailedMiddleware' cb getAddColor
235+ in return $ detailedMiddleware' cb ansiColor ansiMethod ansiStatusCode
181−
236+
182−ansiColor :: Color -> BS.ByteString -> [BS.ByteString]
237+ansiColor' :: Color -> BS.ByteString -> [BS.ByteString]
183−ansiColor color bs = [
238+ansiColor' color bs =
184− pack $ setSGRCode [SetColor Foreground Vivid color]
239+ [ pack $ setSGRCode [SetColor Foreground Dull color]
185− , bs
240+ , bs
186− , pack $ setSGRCode [Reset]
241+ , pack $ setSGRCode [Reset]
187− ]
242+ ]
243+
244+-- | Tags http method with a unique color.
245+ansiMethod' :: BS.ByteString -> [BS.ByteString]
246+ansiMethod' m = case m of
247+ "GET" -> ansiColor' Cyan m
248+ "HEAD" -> ansiColor' Cyan m
249+ "PUT" -> ansiColor' Green m
250+ "POST" -> ansiColor' Yellow m
251+ "DELETE" -> ansiColor' Red m
252+ _ -> ansiColor' Magenta m
253+
254+ansiStatusCode' :: BS.ByteString -> BS.ByteString -> [BS.ByteString]
255+ansiStatusCode' c t = case S8.take 1 c of
256+ "2" -> ansiColor' Green t
257+ "3" -> ansiColor' Yellow t
258+ "4" -> ansiColor' Red t
259+ "5" -> ansiColor' Magenta t
260+ _ -> ansiColor' Blue t
261+
262+recordChunks :: IORef B.Builder -> Response -> IO Response
263+recordChunks i (ResponseStream s h sb) =
264+ return . ResponseStream s h $ (\send flush -> sb (\b -> modifyIORef i (<> b) >> send b) flush)
265+recordChunks i (ResponseBuilder s h b) =
266+ modifyIORef i (<> b) >> (return $ ResponseBuilder s h b)
267+recordChunks _ r =
268+ return r
269+
270+getRequestBody :: Request -> IO (Request, [S8.ByteString])
271+getRequestBody req = do
272+ let loop front = do
273+ bs <- requestBody req
274+ if S8.null bs
275+ then return $ front []
276+ else loop $ front . (bs:)
277+ body <- loop id
278+ -- logging the body here consumes it, so fill it back up
279+ -- obviously not efficient, but this is the development logger
280+ --
281+ -- Note: previously, we simply used CL.sourceList. However,
282+ -- that meant that you could read the request body in twice.
283+ -- While that in itself is not a problem, the issue is that,
284+ -- in production, you wouldn't be able to do this, and
285+ -- therefore some bugs wouldn't show up during testing. This
286+ -- implementation ensures that each chunk is only returned
287+ -- once.
288+ ichunks <- newIORef body
289+ let rbody = atomicModifyIORef ichunks $ \chunks ->
290+ case chunks of
291+ [] -> ([], S8.empty)
292+ x:y -> (y, x)
293+ let req' = req { requestBody = rbody }
294+ return (req', body)
188
295
189detailedMiddleware' :: Callback
296detailedMiddleware' :: Callback
297+ -> (Color -> BS.ByteString -> [BS.ByteString])
298+ -> (BS.ByteString -> [BS.ByteString])
190− -> IO (BS.ByteString -> [BS.ByteString])
299+ -> (BS.ByteString -> BS.ByteString -> [BS.ByteString])
191 -> Middleware
300 -> Middleware
192−detailedMiddleware' cb getAddColor app req = do
301+detailedMiddleware' cb ansiColor ansiMethod ansiStatusCode app req sendResponse = do
193− let mlen = lookup "content-length" (requestHeaders req) >>= readInt
194 (req', body) <-
302 (req', body) <-
195− case mlen of
303+ -- second tuple item should not be necessary, but a test runner might mess it up
304+ case (requestBodyLength req, contentLength (requestHeaders req)) of
196 -- log the request body if it is small
305 -- log the request body if it is small
306+ (KnownLength len, _) | len <= 2048 -> getRequestBody req
197− Just len | len <= 2048 -> do
307+ (_, Just len) | len <= 2048 -> getRequestBody req
198− body <- requestBody req C.$$ CL.consume
199− -- logging the body here consumes it, so fill it back up
200− -- obviously not efficient, but this is the development logger
201− --
202− -- Note: previously, we simply used CL.sourceList. However,
203− -- that meant that you could read the request body in twice.
204− -- While that in itself is not a problem, the issue is that,
205− -- in production, you wouldn't be able to do this, and
206− -- therefore some bugs wouldn't show up during testing. This
207− -- implementation ensures that each chunk is only returned
208− -- once.
209− ichunks <- newIORef body
210− let rbody = do
211− chunks <- readIORef ichunks
212− case chunks of
213− [] -> return ()
214− x:xs -> do
215− writeIORef ichunks xs
216− C.yield x
217− rbody
218− let req' = req { requestBody = rbody }
219− return (req', body)
220 _ -> return (req, [])
308 _ -> return (req, [])
221
309
310+ let reqbodylog _ = if null body then [""] else ansiColor White " Request Body: " <> body <> ["\n"]
311+ reqbody = concatMap (either (const [""]) reqbodylog . decodeUtf8') body
222 postParams <- if requestMethod req `elem` ["GET", "HEAD"]
312 postParams <- if requestMethod req `elem` ["GET", "HEAD"]
223− then return []
313+ then return []
224− else do postParams <- liftIO $ allPostParams body
314+ else do postParams <- liftIO $ allPostParams body
225− return $ collectPostParams postParams
315+ return $ collectPostParams postParams
226
316
227 let getParams = map emptyGetParam $ queryString req
317 let getParams = map emptyGetParam $ queryString req
228−
229− addColor <- getAddColor
230− let accept = fromMaybe "" $ lookup "Accept" $ requestHeaders req
318+ accept = fromMaybe "" $ lookup H.hAccept $ requestHeaders req
231−
319+ params = let par | not $ null postParams = [pack (show postParams)]
232− -- log the request immediately.
320+ | not $ null getParams = [pack (show getParams)]
233− liftIO $ cb $ mconcat $ map toLogStr $ addColor (requestMethod req) ++
321+ | otherwise = []
234− [ " "
322+ in if null par then [""] else ansiColor White " Params: " <> par <> ["\n"]
235− , rawPathInfo req
323+
236− , " :: "
324+ t0 <- getCurrentTime
237− , accept
238− , paramsToBS "GET " getParams
239− , paramsToBS "POST " postParams
240− , "\n"
241− ]
242−
243− rsp <- app req'
325+ app req' $ \rsp -> do
244−
326+ let isRaw =
327+ case rsp of
328+ ResponseRaw{} -> True
329+ _ -> False
330+ stCode = statusBS rsp
331+ stMsg = msgBS rsp
332+ t1 <- getCurrentTime
333+
245− -- log the status of the response
334+ -- log the status of the response
246− -- this is color coordinated with the request logging
335+ cb $ mconcat $ map toLogStr $
247− -- also includes the request path to connect it to the request
336+ ansiMethod (requestMethod req) ++ [" ", rawPathInfo req, "\n"] ++
248− liftIO $ cb $ mconcat $ map toLogStr $
337+ params ++ reqbody ++
249− addColor "Status: " ++ statusBS rsp ++
338+ ansiColor White " Accept: " ++ [accept, "\n"] ++
250− [ " "
339+ if isRaw then [] else
251− , msgBS rsp
340+ ansiColor White " Status: " ++
252− , ". "
341+ ansiStatusCode stCode (stCode <> " " <> stMsg) ++
253− , rawPathInfo req -- if you need help matching the 2 logging statements
342+ [" ", pack $ show $ diffUTCTime t1 t0, "\n"]
254− , "\n"
343+
255− ]
344+ sendResponse rsp
256− return rsp
257 where
345 where
258− paramsToBS prefix params =
259− if null params then ""
260− else BS.concat ["\n", prefix, pack (show params)]
261−
262 allPostParams body =
346 allPostParams body =
263 case getRequestBodyType req of
347 case getRequestBodyType req of
264 Nothing -> return ([], [])
348 Nothing -> return ([], [])
349+ Just rbt -> do
350+ ichunks <- newIORef body
351+ let rbody = atomicModifyIORef ichunks $ \chunks ->
352+ case chunks of
353+ [] -> ([], S8.empty)
354+ x:y -> (y, x)
265− Just rbt -> CL.sourceList body C.$$ sinkRequestBody lbsBackEnd rbt
355+ sinkRequestBody lbsBackEnd rbt rbody
266
356
267 emptyGetParam :: (BS.ByteString, Maybe BS.ByteString) -> (BS.ByteString, BS.ByteString)
357 emptyGetParam :: (BS.ByteString, Maybe BS.ByteString) -> (BS.ByteString, BS.ByteString)
268 emptyGetParam (k, Just v) = (k,v)
358 emptyGetParam (k, Just v) = (k,v)
@@ -270,19 +360,12 @@ detailedMiddleware' cb getAddColor app req = do
270
360
271 collectPostParams :: ([Param], [File LBS.ByteString]) -> [Param]
361 collectPostParams :: ([Param], [File LBS.ByteString]) -> [Param]
272 collectPostParams (postParams, files) = postParams ++
362 collectPostParams (postParams, files) = postParams ++
273− map (\(k,v) -> (k, BS.append "FILE: " (fileName v))) files
363+ map (\(k,v) -> (k, "FILE: " <> fileName v)) files
274
364
275− readInt bs =
276− case reads $ unpack bs of
277− (i, _):_ -> Just (i :: Int)
278− [] -> Nothing
279
365
366+
280−statusBS :: Response -> [BS.ByteString]
367+statusBS :: Response -> BS.ByteString
281−statusBS rsp =
282− if status > 400 then ansiColor Red bs else [bs]
283− where
284− bs = pack $ show status
285− status = statusCode $ responseStatus rsp
368+statusBS = pack . show . statusCode . responseStatus
286
369
287msgBS :: Response -> BS.ByteString
370msgBS :: Response -> BS.ByteString
288msgBS = statusMessage . responseStatus
371msgBS = statusMessage . responseStatus
wai-extra/Network/Wai/Middleware/RequestLogger/Internal.hs+7−1
@@ -8,9 +8,10 @@ module Network.Wai.Middleware.RequestLogger.Internal
8
8
9import Data.ByteString (ByteString)
9import Data.ByteString (ByteString)
10import Network.Wai.Logger (clockDateCacher)
10import Network.Wai.Logger (clockDateCacher)
11+#if !MIN_VERSION_wai_logger(2, 2, 0)
11import Control.Concurrent (forkIO, threadDelay)
12import Control.Concurrent (forkIO, threadDelay)
12import Control.Monad (forever)
13import Control.Monad (forever)
13−
14+#endif
14import System.Log.FastLogger (LogStr, fromLogStr)
15import System.Log.FastLogger (LogStr, fromLogStr)
15
16
16logToByteString :: LogStr -> ByteString
17logToByteString :: LogStr -> ByteString
@@ -18,10 +19,15 @@ logToByteString = fromLogStr
18
19
19getDateGetter :: IO () -- ^ flusher
20getDateGetter :: IO () -- ^ flusher
20 -> IO (IO ByteString)
21 -> IO (IO ByteString)
22+#if !MIN_VERSION_wai_logger(2, 2, 0)
21getDateGetter flusher = do
23getDateGetter flusher = do
22 (getter, updater) <- clockDateCacher
24 (getter, updater) <- clockDateCacher
23 _ <- forkIO $ forever $ do
25 _ <- forkIO $ forever $ do
24 threadDelay 1000000
26 threadDelay 1000000
25 updater
27 updater
26 flusher
28 flusher
29+#else
30+getDateGetter _ = do
31+ (getter, _) <- clockDateCacher
32+#endif
27 return getter
33 return getter
wai-extra/Network/Wai/Middleware/RequestLogger/JSON.hsnew file+137
@@ -0,0 +1,137 @@
1+{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE CPP #-}
3+
4+module Network.Wai.Middleware.RequestLogger.JSON
5+ ( formatAsJSON
6+ , formatAsJSONWithHeaders
7+ ) where
8+
9+import qualified Data.ByteString.Builder as BB (toLazyByteString)
10+import Data.ByteString.Lazy (toStrict)
11+import Data.Aeson
12+import Data.CaseInsensitive (original)
13+import Data.Monoid ((<>))
14+import qualified Data.ByteString.Char8 as S8
15+import Data.IP
16+import qualified Data.Text as T
17+import Data.Text (Text)
18+import Data.Text.Encoding (decodeUtf8With)
19+import Data.Text.Encoding.Error (lenientDecode)
20+import Data.Time (NominalDiffTime)
21+import Data.Word (Word32)
22+import Network.HTTP.Types as H
23+import Network.Socket (SockAddr (..), PortNumber)
24+import Network.Wai
25+import Network.Wai.Middleware.RequestLogger
26+import System.Log.FastLogger (toLogStr)
27+import Text.Printf (printf)
28+
29+formatAsJSON :: OutputFormatterWithDetails
30+formatAsJSON date req status responseSize duration reqBody response =
31+ toLogStr (encode $
32+ object
33+ [ "request" .= requestToJSON duration req reqBody
34+ , "response" .=
35+ object
36+ [ "status" .= statusCode status
37+ , "size" .= responseSize
38+ , "body" .=
39+ if statusCode status >= 400
40+ then Just . decodeUtf8With lenientDecode . toStrict . BB.toLazyByteString $ response
41+ else Nothing
42+ ]
43+ , "time" .= decodeUtf8With lenientDecode date
44+ ]) <> "\n"
45+
46+-- | Same as @formatAsJSON@ but with response headers included
47+--
48+-- This is useful for passing arbitrary data from your application out to the
49+-- WAI layer for it to be logged, but you may need to be careful to
50+-- subsequently redact any headers which may contain sensitive data.
51+--
52+-- @since 3.0.27
53+formatAsJSONWithHeaders :: OutputFormatterWithDetailsAndHeaders
54+formatAsJSONWithHeaders date req status resSize duration reqBody res resHeaders =
55+ toLogStr (encode $
56+ object
57+ [ "request" .= requestToJSON duration req reqBody
58+ , "response" .= object
59+ [ "status" .= statusCode status
60+ , "size" .= resSize
61+ , "headers" .= responseHeadersToJSON resHeaders
62+ , "body" .=
63+ if statusCode status >= 400
64+ then Just . decodeUtf8With lenientDecode . toStrict . BB.toLazyByteString $ res
65+ else Nothing
66+ ]
67+ , "time" .= decodeUtf8With lenientDecode date
68+ ]) <> "\n"
69+
70+word32ToHostAddress :: Word32 -> Text
71+word32ToHostAddress = T.intercalate "." . map (T.pack . show) . fromIPv4 . fromHostAddress
72+
73+readAsDouble :: String -> Double
74+readAsDouble = read
75+
76+requestToJSON :: NominalDiffTime -> Request -> [S8.ByteString] -> Value
77+requestToJSON duration req reqBody =
78+ object
79+ [ "method" .= decodeUtf8With lenientDecode (requestMethod req)
80+ , "path" .= decodeUtf8With lenientDecode (rawPathInfo req)
81+ , "queryString" .= map queryItemToJSON (queryString req)
82+ , "durationMs" .= (readAsDouble . printf "%.2f" . rationalToDouble $ toRational duration * 1000)
83+ , "size" .= requestBodyLengthToJSON (requestBodyLength req)
84+ , "body" .= decodeUtf8With lenientDecode (S8.concat reqBody)
85+ , "remoteHost" .= sockToJSON (remoteHost req)
86+ , "httpVersion" .= httpVersionToJSON (httpVersion req)
87+ , "headers" .= requestHeadersToJSON (requestHeaders req)
88+ ]
89+ where
90+ rationalToDouble :: Rational -> Double
91+ rationalToDouble = fromRational
92+
93+sockToJSON :: SockAddr -> Value
94+sockToJSON (SockAddrInet pn ha) =
95+ object
96+ [ "port" .= portToJSON pn
97+ , "hostAddress" .= word32ToHostAddress ha
98+ ]
99+sockToJSON (SockAddrInet6 pn _ ha _) =
100+ object
101+ [ "port" .= portToJSON pn
102+ , "hostAddress" .= ha
103+ ]
104+sockToJSON (SockAddrUnix sock) =
105+ object [ "unix" .= sock ]
106+#if !MIN_VERSION_network(3,0,0)
107+sockToJSON (SockAddrCan i) =
108+ object [ "can" .= i ]
109+#endif
110+
111+queryItemToJSON :: QueryItem -> Value
112+queryItemToJSON (name, mValue) = toJSON (decodeUtf8With lenientDecode name, fmap (decodeUtf8With lenientDecode) mValue)
113+
114+requestHeadersToJSON :: RequestHeaders -> Value
115+requestHeadersToJSON = toJSON . map hToJ where
116+ -- Redact cookies
117+ hToJ ("Cookie", _) = toJSON ("Cookie" :: Text, "-RDCT-" :: Text)
118+ hToJ hd = headerToJSON hd
119+
120+responseHeadersToJSON :: [Header] -> Value
121+responseHeadersToJSON = toJSON . map hToJ where
122+ -- Redact cookies
123+ hToJ ("Set-Cookie", _) = toJSON ("Set-Cookie" :: Text, "-RDCT-" :: Text)
124+ hToJ hd = headerToJSON hd
125+
126+headerToJSON :: Header -> Value
127+headerToJSON (headerName, header) = toJSON (decodeUtf8With lenientDecode . original $ headerName, decodeUtf8With lenientDecode header)
128+
129+portToJSON :: PortNumber -> Value
130+portToJSON = toJSON . toInteger
131+
132+httpVersionToJSON :: HttpVersion -> Value
133+httpVersionToJSON (HttpVersion major minor) = String $ T.pack (show major) <> "." <> T.pack (show minor)
134+
135+requestBodyLengthToJSON :: RequestBodyLength -> Value
136+requestBodyLengthToJSON ChunkedBody = String "Unknown"
137+requestBodyLengthToJSON (KnownLength l) = toJSON l
wai-extra/Network/Wai/Middleware/Rewrite.hs+321−15
@@ -1,33 +1,339 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE TupleSections #-}
2+{-# LANGUAGE CPP #-}
3+
2module Network.Wai.Middleware.Rewrite
4module Network.Wai.Middleware.Rewrite
5+ ( -- * How to use this module
6+ -- $howto
7+
8+ -- ** A note on semantics
9+
10+ -- $semantics
11+
12+ -- ** Paths and Queries
13+
14+ -- $pathsandqueries
15+ PathsAndQueries
16+
17+ -- ** An example rewriting paths with queries
18+
19+ -- $takeover
20+
21+ -- ** Upgrading from wai-extra ≤ 3.0.16.1
22+
23+ -- $upgrading
24+
25+ -- * 'Middleware'
26+
27+ -- ** Recommended functions
28+ , rewriteWithQueries
29+ , rewritePureWithQueries
30+ , rewriteRoot
31+
32+ -- ** Deprecated
33+ , rewrite
3− ( rewrite, rewritePure
34+ , rewritePure
35+
36+ -- * Operating on 'Request's
37+
38+ , rewriteRequest
39+ , rewriteRequestPure
4 ) where
40 ) where
5
41
6import Network.Wai
42import Network.Wai
7import Control.Monad.IO.Class (liftIO)
43import Control.Monad.IO.Class (liftIO)
8import Data.Text (Text)
44import Data.Text (Text)
45+import Data.Functor.Identity (Identity(..))
9import qualified Data.Text.Encoding as TE
46import qualified Data.Text.Encoding as TE
10import qualified Data.Text as T
47import qualified Data.Text as T
11import Network.HTTP.Types as H
48import Network.HTTP.Types as H
12
49
50+-- GHC ≤ 7.10 does not export Applicative functions from the prelude.
51+#if __GLASGOW_HASKELL__ <= 710
52+import Control.Applicative
53+#endif
13
54
14−-- | rewrite based on your own conversion rules
55+-- $howto
15−rewrite :: ([Text] -> H.RequestHeaders -> IO [Text]) -> Middleware
56+-- This module provides 'Middleware' to rewrite URL paths. It also provides
16−rewrite convert app req = do
57+-- functions that will convert a 'Request' to a modified 'Request'.
17− newPathInfo <- liftIO $ convert (pathInfo req) (requestHeaders req)
58+-- Both operations require a function that takes URL parameters and
18− let rawPInfo = TE.encodeUtf8 $ T.intercalate "/" newPathInfo
59+-- headers, and returns new URL parameters. Parameters are pieces of URL
19− app req { pathInfo = newPathInfo, rawPathInfo = rawPInfo }
60+-- paths and query parameters.
61+--
62+-- If you are a new user of the library, use 'rewriteWithQueries' or
63+-- 'rewritePureWithQueries' for middleware. For modifying 'Request's
64+-- directly, use 'rewriteRequest' or 'rewriteRequestPure'.
65+
66+-- $semantics
67+--
68+-- Versions of this library in wai-extra ≤ 3.0.16.1 exported only
69+-- 'rewrite' and 'rewritePure' and both modified 'rawPathInfo' of the
70+-- underlying requests. Such modification has been proscribed. The
71+-- semantics of these functions have not changed; instead the recommended
72+-- approach is to use 'rewriteWithQueries' and 'rewritePureWithQueries'.
73+-- The new functions are slightly different, as described in the section
74+-- on upgrading; code for previous library versions can be upgraded with
75+-- a single change, and as the type of the new function is different the
76+-- compiler will indicate where this change must be made.
77+--
78+-- The 'rewriteRequest' and 'rewriteRequestPure' functions use the new
79+-- semantics, too.
20
80
21−-- | rewrite based on your own conversion rules
81+-- $pathsandqueries
22−-- Example convert function:
82+--
83+-- This library defines the type synonym `PathsAndQueries` to make code
84+-- handling paths and queries easier to read.
85+--
86+-- /e.g./ /\/foo\/bar/ would look like
87+--
88+-- > ["foo", "bar"] :: Text
89+--
90+-- /?bar=baz/ would look like
91+--
92+-- > [("bar", Just "baz")] :: QueryText
93+--
94+-- Together,
95+--
96+-- /\/foo?bar=baz/ would look like
97+--
98+-- > (["foo"],[("bar", Just "baz")]) :: PathsAndQueries
23
99
100+-- $takeover
101+-- Let’s say we want to replace a website written in PHP with one written
102+-- using WAI. We’ll use the
103+-- <https://hackage.haskell.org/package/http-reverse-proxy http-reverse-proxy>
104+-- package to serve the old
105+-- site from the new site, but there’s a problem. The old site uses pages like
106+--
107+-- @
108+-- index.php?page=/page/
109+-- @
110+--
111+-- whereas the new site would look like
112+--
113+-- @
114+-- index\//page/
115+-- @
116+--
117+-- In doing this, we want to separate the migration code from our new
118+-- website. So we’d like to handle links internally using the path
119+-- formulation, but externally have the old links still work.
120+--
121+-- Therefore, we will use middleware ('rewritePureWithQueries') from this
122+-- module to rewrite incoming requests from the query formulation to the
123+-- paths formulation.
124+--
125+-- > {-# LANGUAGE ViewPatterns #-}
126+-- >
127+-- > rewritePathFromPhp :: Middleware
128+-- > rewritePathFromPhp = rewritePureWithQueries pathFromPhp
129+-- >
130+-- > pathFromPhp :: PathsAndQueries -> H.RequestHeaders -> PathsAndQueries
131+-- > pathFromPhp (pieces, queries) _ = piecesConvert pieces queries
132+-- > where
133+-- > piecesConvert :: [Text] -> H.Query -> PathsAndQueries
134+-- > piecesConvert ["index.php"] qs@(join . lookup "page" -> Just page) =
135+-- > ( ["index", TE.decodeUtf8With TE.lenientDecode page]
136+-- > , delete ("page", pure page) qs
137+-- > )
138+-- > piecesConvert ps qs = (ps, qs)
139+--
140+-- On the other side, we will use 'rewriteRequestPure' to rewrite outgoing
141+-- requests to the original website from the reverse proxy code (using the
142+-- 'Network.HTTP.ReverseProxy.WPRModifiedRequest' or
143+-- 'Network.HTTP.ReverseProxy.WPRModifiedRequestSecure' constructors. Note,
144+-- these links will only work if the haddock documentation for
145+-- <https://hackage.haskell.org/package/http-reverse-proxy http-reverse-proxy>
146+-- is installed).
147+--
148+-- > rewritePhpFromPath :: Request -> Request
149+-- > rewritePhpFromPath = rewriteRequestPure phpFromPath
150+-- >
151+-- > phpFromPath :: PathsAndQueries -> H.RequestHeaders -> PathsAndQueries
152+-- > phpFromPath (pieces, queries) _ = piecesConvert pieces queries
153+-- > where
154+-- > piecesConvert :: [Text] -> H.Query -> PathsAndQueries
155+-- > piecesConvert ["index", page] qs = ( ["index.php"], ("page", pure . TE.encodeUtf8 $ page) : qs )
156+-- > piecesConvert ps qs = (ps, qs)
157+--
158+-- For the whole example, see
159+-- <https://gist.github.com/dbaynard/c844d0df124f68ec8b6da152c581ce6d>.
160+
161+-- $upgrading
162+-- It is quite simple to upgrade from 'rewrite' and 'rewritePure', to
163+-- 'rewriteWithQueries' and 'rewritePureWithQueries'.
164+-- Insert 'Data.Bifunctor.first', which specialises to
165+--
166+-- @
167+-- 'Data.Bifunctor.first' :: (['Text'] -> ['Text']) -> 'PathsAndQueries' -> 'PathsAndQueries'
168+-- @
169+--
170+-- as the following example demonstrates.
171+--
172+-- Old versions of the library could only handle path pieces, not queries.
173+-- This could have been supplied to 'rewritePure'.
174+--
175+-- @
24−-- staticConvert :: [Text] -> H.RequestHeaders -> [Text]
176+-- staticConvert' :: [Text] -> H.RequestHeaders -> [Text]
25−-- staticConvert pieces _ = piecesConvert pieces
177+-- staticConvert' pieces _ = piecesConvert pieces
178+-- where
179+-- piecesConvert [] = ["static", "html", "pages.html"]
180+-- piecesConvert route@("pages":_) = "static":"html":route
181+-- @
182+--
183+-- Instead, use this function, supplied to 'rewritePureWithQueries'.
184+--
185+-- @
186+-- staticConvert :: 'PathsAndQueries' -> H.RequestHeaders -> 'PathsAndQueries'
187+-- staticConvert pathsAndQueries _ = 'Data.Bifunctor.first' piecesConvert pathsAndQueries
26-- where
188-- where
27-- piecesConvert [] = ["static", "html", "pages.html"]
189-- piecesConvert [] = ["static", "html", "pages.html"]
28-- piecesConvert route@("pages":_) = "static":"html":route
190-- piecesConvert route@("pages":_) = "static":"html":route
191+-- @
192+--
193+-- The former formulation is deprecated for two reasons:
194+--
195+-- 1. The original formulation of 'rewrite' modified 'rawPathInfo', which
196+-- is deprecated behaviour.
197+--
198+-- 2. The original formulation did not allow query parameters to
199+-- influence the path.
200+--
201+-- Concerning the first point, take care with semantics of your program when
202+-- upgrading as the upgraded functions no longer modify 'rawPathInfo'.
203+
204+--------------------------------------------------
205+-- * Types
206+--------------------------------------------------
207+
208+-- | A tuple of the path sections as ['Text'] and query parameters as
209+-- 'H.Query'. This makes writing type signatures for the conversion
210+-- function far more pleasant.
211+--
212+-- Note that this uses 'H.Query' not 'H.QueryText' to more accurately
213+-- reflect the paramaters that can be supplied in URLs. It may be safe to
214+-- treat parameters as text; use the 'H.queryToQueryText' and
215+-- 'H.queryTextToQuery' functions to interconvert.
216+type PathsAndQueries = ([Text], H.Query)
217+
218+--------------------------------------------------
219+-- * Rewriting 'Middleware'
220+--------------------------------------------------
221+
222+-- | Rewrite based on your own conversion function for paths only, to be
223+-- supplied by users of this library (with the conversion operating in 'IO').
224+--
225+-- For new code, use 'rewriteWithQueries' instead.
226+rewrite :: ([Text] -> H.RequestHeaders -> IO [Text]) -> Middleware
227+rewrite convert app req sendResponse = do
228+ let convertIO = pathsOnly . curry $ liftIO . uncurry convert
229+ newReq <- rewriteRequestRawM convertIO req
230+ app newReq sendResponse
231+{-# WARNING rewrite [
232+ "This modifies the 'rawPathInfo' field of a 'Request'."
233+ , " This is not recommended behaviour; it is however how"
234+ , " this function has worked in the past."
235+ , " Use 'rewriteWithQueries' instead"] #-}
236+
237+-- | Rewrite based on pure conversion function for paths only, to be
238+-- supplied by users of this library.
239+--
240+-- For new code, use 'rewritePureWithQueries' instead.
29rewritePure :: ([Text] -> H.RequestHeaders -> [Text]) -> Middleware
241rewritePure :: ([Text] -> H.RequestHeaders -> [Text]) -> Middleware
30rewritePure convert app req =
242rewritePure convert app req =
243+ let convertPure = pathsOnly . curry $ Identity . uncurry convert
244+ newReq = runIdentity $ rewriteRequestRawM convertPure req
245+ in app newReq
246+{-# WARNING rewritePure [
247+ "This modifies the 'rawPathInfo' field of a 'Request'."
248+ , " This is not recommended behaviour; it is however how"
249+ , " this function has worked in the past."
250+ , " Use 'rewritePureWithQueries' instead"] #-}
251+
252+-- | Rewrite based on your own conversion function for paths and queries.
253+-- This function is to be supplied by users of this library, and operates
254+-- in 'IO'.
255+rewriteWithQueries :: (PathsAndQueries -> H.RequestHeaders -> IO PathsAndQueries)
256+ -> Middleware
257+rewriteWithQueries convert app req sendResponse = do
258+ newReq <- rewriteRequestM convert req
259+ app newReq sendResponse
260+
261+-- | Rewrite based on pure conversion function for paths and queries. This
262+-- function is to be supplied by users of this library.
263+rewritePureWithQueries :: (PathsAndQueries -> H.RequestHeaders -> PathsAndQueries)
264+ -> Middleware
265+rewritePureWithQueries convert app req = app $ rewriteRequestPure convert req
266+
267+-- | Rewrite root requests (/) to a specified path
268+--
269+-- Note that /index.html/ in example below should already be a valid route.
270+--
271+-- @
272+-- rewriteRoot "index.html" :: Middleware
273+-- @
274+--
275+-- @since 3.0.23.0
276+rewriteRoot :: Text -> Middleware
277+rewriteRoot root = rewritePureWithQueries onlyRoot
278+ where
279+ onlyRoot ([], q) _ = ([root], q)
280+ onlyRoot paths _ = paths
281+
282+--------------------------------------------------
283+-- * Modifying 'Request's directly
284+--------------------------------------------------
285+
286+-- | Modify a 'Request' using the supplied function in 'IO'. This is suitable for
287+-- the reverse proxy example.
288+rewriteRequest :: (PathsAndQueries -> H.RequestHeaders -> IO PathsAndQueries)
289+ -> Request -> IO Request
290+rewriteRequest convert req =
291+ let convertIO = curry $ liftIO . uncurry convert
292+ in rewriteRequestRawM convertIO req
293+
294+-- | Modify a 'Request' using the pure supplied function. This is suitable for
295+-- the reverse proxy example.
296+rewriteRequestPure :: (PathsAndQueries -> H.RequestHeaders -> PathsAndQueries)
297+ -> Request -> Request
298+rewriteRequestPure convert req =
299+ let convertPure = curry $ Identity . uncurry convert
300+ in runIdentity $ rewriteRequestRawM convertPure req
301+
302+--------------------------------------------------
303+-- * Helper functions
304+--------------------------------------------------
305+
306+-- | This helper function factors out the common behaviour of rewriting requests.
307+rewriteRequestM :: (Applicative m, Monad m)
308+ => (PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries)
309+ -> Request -> m Request
310+rewriteRequestM convert req = do
31− let pInfo = convert (pathInfo req) (requestHeaders req)
311+ (pInfo, qByteStrings) <- curry convert (pathInfo req) (queryString req) (requestHeaders req)
312+ pure req {pathInfo = pInfo, queryString = qByteStrings}
313+
314+-- | This helper function preserves the semantics of wai-extra ≤ 3.0, in
315+-- which the rewrite functions modify the 'rawPathInfo' parameter. Note
316+-- that this has not been extended to modify the 'rawQueryInfo' as
317+-- modifying either of these values has been deprecated.
318+rewriteRequestRawM :: (Applicative m, Monad m)
319+ => (PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries)
320+ -> Request -> m Request
321+rewriteRequestRawM convert req = do
322+ newReq <- rewriteRequestM convert req
32− rawPInfo = TE.encodeUtf8 $ T.intercalate "/" pInfo
323+ let rawPInfo = TE.encodeUtf8 . T.intercalate "/" . pathInfo $ newReq
33− in app req { pathInfo = pInfo, rawPathInfo = rawPInfo }
324+ pure newReq { rawPathInfo = rawPInfo }
325+{-# WARNING rewriteRequestRawM [
326+ "This modifies the 'rawPathInfo' field of a 'Request'."
327+ , " This is not recommended behaviour; it is however how"
328+ , " this function has worked in the past."
329+ , " Use 'rewriteRequestM' instead"] #-}
330+
331+-- | Produce a function that works on 'PathsAndQueries' from one working
332+-- only on paths. This is not exported, as it is only needed to handle
333+-- code written for versions ≤ 3.0 of the library; see the
334+-- example above using 'Data.Bifunctor.first' to do something similar.
335+pathsOnly :: (Applicative m, Monad m)
336+ => ([Text] -> H.RequestHeaders -> m [Text])
337+ -> PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries
338+pathsOnly convert psAndQs headers = (,[]) <$> convert (fst psAndQs) headers
339+{-# INLINE pathsOnly #-}
wai-extra/Network/Wai/Middleware/Routed.hsnew file+39
@@ -0,0 +1,39 @@
1+-- |
2+--
3+-- Since 3.0.9
4+module Network.Wai.Middleware.Routed
5+ ( routedMiddleware
6+ , hostedMiddleware
7+ ) where
8+
9+import Network.Wai
10+import Data.ByteString (ByteString)
11+import Data.Text (Text)
12+
13+-- | Apply a middleware based on a test of pathInfo
14+--
15+-- example:
16+--
17+-- > let corsify = routedMiddleWare ("static" `elem`) addCorsHeaders
18+--
19+-- Since 3.0.9
20+routedMiddleware :: ([Text] -> Bool) -- ^ Only use middleware if this pathInfo test returns True
21+ -> Middleware -- ^ middleware to apply the path prefix guard to
22+ -> Middleware -- ^ modified middleware
23+routedMiddleware pathCheck middle app req
24+ | pathCheck (pathInfo req) = middle app req
25+ | otherwise = app req
26+
27+-- | Only apply the middleware to certain hosts
28+--
29+-- Since 3.0.9
30+hostedMiddleware :: ByteString -- ^ Domain the middleware applies to
31+ -> Middleware -- ^ middleware to apply the path prefix guard to
32+ -> Middleware -- ^ modified middleware
33+hostedMiddleware domain middle app req
34+ | hasDomain domain req = middle app req
35+ | otherwise = app req
36+
37+hasDomain :: ByteString -> Request -> Bool
38+hasDomain domain req = maybe False (== domain) mHost
39+ where mHost = requestHeaderHost req
wai-extra/Network/Wai/Middleware/StreamFile.hsnew file+43
@@ -0,0 +1,43 @@
1+-- |
2+--
3+-- Since 3.0.4
4+module Network.Wai.Middleware.StreamFile
5+ (streamFile) where
6+
7+import Network.Wai (responseStream)
8+import Network.Wai.Internal
9+import Network.Wai (Middleware, responseToStream)
10+import qualified Data.ByteString.Char8 as S8
11+import System.PosixCompat (getFileStatus, fileSize, FileOffset)
12+import Network.HTTP.Types (hContentLength)
13+
14+-- |Convert ResponseFile type responses into ResponseStream type
15+--
16+-- Checks the response type, and if it's a ResponseFile, converts it
17+-- into a ResponseStream. Other response types are passed through
18+-- unchanged.
19+--
20+-- Converted responses get a Content-Length header.
21+--
22+-- Streaming a file will bypass a sendfile system call, and may be
23+-- useful to work around systems without working sendfile
24+-- implementations.
25+--
26+-- Since 3.0.4
27+streamFile :: Middleware
28+streamFile app env sendResponse = app env $ \res ->
29+ case res of
30+ ResponseFile _ _ fp _ -> withBody sendBody
31+ where
32+ (s, hs, withBody) = responseToStream res
33+ sendBody :: StreamingBody -> IO ResponseReceived
34+ sendBody body = do
35+ len <- getFileSize fp
36+ let hs' = (hContentLength, (S8.pack (show len))) : hs
37+ sendResponse $ responseStream s hs' body
38+ _ -> sendResponse res
39+
40+getFileSize :: FilePath -> IO FileOffset
41+getFileSize path = do
42+ stat <- getFileStatus path
43+ return (fileSize stat)
wai-extra/Network/Wai/Middleware/StripHeaders.hsnew file+45
@@ -0,0 +1,45 @@
1+-- This was written for one specific use case and then generalized.
2+
3+-- The specific use case was a JSON API with a consumer that would choke on the
4+-- "Set-Cookie" response header. The solution was to test for the API's
5+-- `pathInfo` in the Request and if it matched, filter the response headers.
6+
7+-- When using this, care should be taken not to strip out headers that are
8+-- required for correct operation of the client (eg Content-Type).
9+
10+module Network.Wai.Middleware.StripHeaders
11+ ( stripHeader
12+ , stripHeaders
13+ , stripHeaderIf
14+ , stripHeadersIf
15+ ) where
16+
17+import Network.Wai (Middleware, Request, modifyResponse, mapResponseHeaders, ifRequest)
18+import Network.Wai.Internal (Response)
19+import Data.ByteString (ByteString)
20+
21+import qualified Data.CaseInsensitive as CI
22+
23+stripHeader :: ByteString -> (Response -> Response)
24+stripHeader h = mapResponseHeaders (filter (\ hdr -> fst hdr /= CI.mk h))
25+
26+stripHeaders :: [ByteString] -> (Response -> Response)
27+stripHeaders hs =
28+ let hnames = map CI.mk hs
29+ in mapResponseHeaders (filter (\ hdr -> fst hdr `notElem` hnames))
30+
31+-- | If the request satisifes the provided predicate, strip headers matching
32+-- the provided header name.
33+--
34+-- Since 3.0.8
35+stripHeaderIf :: ByteString -> (Request -> Bool) -> Middleware
36+stripHeaderIf h rpred =
37+ ifRequest rpred (modifyResponse $ stripHeader h)
38+
39+-- | If the request satisifes the provided predicate, strip all headers whose
40+-- header name is in the list of provided header names.
41+--
42+-- Since 3.0.8
43+stripHeadersIf :: [ByteString] -> (Request -> Bool) -> Middleware
44+stripHeadersIf hs rpred
45+ = ifRequest rpred (modifyResponse $ stripHeaders hs)
wai-extra/Network/Wai/Middleware/Timeout.hsnew file+33
@@ -0,0 +1,33 @@
1+-- | Timeout requests
2+module Network.Wai.Middleware.Timeout
3+ ( timeout
4+ , timeoutStatus
5+ , timeoutAs
6+ ) where
7+
8+import Network.HTTP.Types (Status, status503)
9+import Network.Wai
10+import qualified System.Timeout as Timeout
11+
12+-- | Time out the request after the given number of seconds
13+--
14+-- Timeouts respond with @'status503'@. See @'timeoutStatus'@ or @'timeoutAs'@
15+-- to customize the behavior of the timed-out case.
16+--
17+-- @since 3.0.24.0@
18+timeout :: Int -> Middleware
19+timeout = timeoutStatus status503
20+
21+-- | Time out with the given @'Status'@
22+--
23+-- @since 3.0.24.0@
24+timeoutStatus :: Status -> Int -> Middleware
25+timeoutStatus status = timeoutAs $ responseLBS status [] ""
26+
27+-- | Time out with the given @'Response'@
28+--
29+-- @since 3.0.24.0@
30+timeoutAs :: Response -> Int -> Middleware
31+timeoutAs timeoutReponse seconds app req respond =
32+ maybe (respond timeoutReponse) pure
33+ =<< Timeout.timeout (seconds * 1000000) (app req respond)
wai-extra/Network/Wai/Middleware/Vhost.hs+8−6
@@ -1,5 +1,5 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE CPP #-}
2−module Network.Wai.Middleware.Vhost (vhost, redirectWWW, redirectTo, redirectToLogged) where
2+ module Network.Wai.Middleware.Vhost (vhost, redirectWWW, redirectTo, redirectToLogged) where
3
3
4import Network.Wai
4import Network.Wai
5
5
@@ -7,7 +7,9 @@ import Network.HTTP.Types as H
7import qualified Data.Text.Encoding as TE
7import qualified Data.Text.Encoding as TE
8import Data.Text (Text)
8import Data.Text (Text)
9import qualified Data.ByteString as BS
9import qualified Data.ByteString as BS
10+#if __GLASGOW_HASKELL__ < 710
10import Data.Monoid (mappend)
11import Data.Monoid (mappend)
12+#endif
11
13
12vhost :: [(Request -> Bool, Application)] -> Application -> Application
14vhost :: [(Request -> Bool, Application)] -> Application -> Application
13vhost vhosts def req =
15vhost vhosts def req =
@@ -20,14 +22,14 @@ redirectWWW home =
20 redirectIf home (maybe True (BS.isPrefixOf "www") . lookup "host" . requestHeaders)
22 redirectIf home (maybe True (BS.isPrefixOf "www") . lookup "host" . requestHeaders)
21
23
22redirectIf :: Text -> (Request -> Bool) -> Application -> Application
24redirectIf :: Text -> (Request -> Bool) -> Application -> Application
23−redirectIf home cond app req =
25+redirectIf home cond app req sendResponse =
24 if cond req
26 if cond req
25− then return $ redirectTo $ TE.encodeUtf8 home
27+ then sendResponse $ redirectTo $ TE.encodeUtf8 home
26− else app req
28+ else app req sendResponse
27
29
28redirectTo :: BS.ByteString -> Response
30redirectTo :: BS.ByteString -> Response
29redirectTo location = responseLBS H.status301
31redirectTo location = responseLBS H.status301
30− [ ("Content-Type", "text/plain") , ("Location", location) ] "Redirect"
32+ [ (H.hContentType, "text/plain") , (H.hLocation, location) ] "Redirect"
31
33
32redirectToLogged :: (Text -> IO ()) -> BS.ByteString -> IO Response
34redirectToLogged :: (Text -> IO ()) -> BS.ByteString -> IO Response
33redirectToLogged logger loc = do
35redirectToLogged logger loc = do
wai-extra/Network/Wai/Parse.hs+481−142
@@ -1,5 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2−{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ExistentialQuantification #-}
2{-# LANGUAGE ExistentialQuantification #-}
4{-# LANGUAGE PatternGuards #-}
3{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE TypeFamilies #-}
@@ -12,7 +11,7 @@ module Network.Wai.Parse
12 , RequestBodyType (..)
11 , RequestBodyType (..)
13 , getRequestBodyType
12 , getRequestBodyType
14 , sinkRequestBody
13 , sinkRequestBody
15− , conduitRequestBody
14+ , sinkRequestBodyEx
16 , BackEnd
15 , BackEnd
17 , lbsBackEnd
16 , lbsBackEnd
18 , tempFileBackEnd
17 , tempFileBackEnd
@@ -21,6 +20,24 @@ module Network.Wai.Parse
21 , File
20 , File
22 , FileInfo (..)
21 , FileInfo (..)
23 , parseContentType
22 , parseContentType
23+ , ParseRequestBodyOptions
24+ , defaultParseRequestBodyOptions
25+ , noLimitParseRequestBodyOptions
26+ , parseRequestBodyEx
27+ , setMaxRequestKeyLength
28+ , clearMaxRequestKeyLength
29+ , setMaxRequestNumFiles
30+ , clearMaxRequestNumFiles
31+ , setMaxRequestFileSize
32+ , clearMaxRequestFileSize
33+ , setMaxRequestFilesSize
34+ , clearMaxRequestFilesSize
35+ , setMaxRequestParmsSize
36+ , clearMaxRequestParmsSize
37+ , setMaxHeaderLines
38+ , clearMaxHeaderLines
39+ , setMaxHeaderLineLength
40+ , clearMaxHeaderLineLength
24#if TEST
41#if TEST
25 , Bound (..)
42 , Bound (..)
26 , findBound
43 , findBound
@@ -31,29 +48,30 @@ module Network.Wai.Parse
31#endif
48#endif
32 ) where
49 ) where
33
50
34−import qualified Data.ByteString.Search as Search
51+import qualified Control.Exception as E
35import qualified Data.ByteString as S
52import qualified Data.ByteString as S
36import qualified Data.ByteString.Lazy as L
53import qualified Data.ByteString.Lazy as L
37import qualified Data.ByteString.Char8 as S8
54import qualified Data.ByteString.Char8 as S8
38import Data.Word (Word8)
55import Data.Word (Word8)
56+import Data.Int (Int64)
39−import Data.Maybe (fromMaybe)
57+import Data.Maybe (catMaybes, fromMaybe)
40import Data.List (sortBy)
58import Data.List (sortBy)
41−import Data.Function (on)
59+import Data.Function (on, fix)
42import System.Directory (removeFile, getTemporaryDirectory)
60import System.Directory (removeFile, getTemporaryDirectory)
43import System.IO (hClose, openBinaryTempFile)
61import System.IO (hClose, openBinaryTempFile)
62+import System.IO.Error (isDoesNotExistError)
44import Network.Wai
63import Network.Wai
45−import Data.Conduit
46−import Data.Conduit.Internal ()
47−import qualified Data.Conduit.List as CL
48−import qualified Data.Conduit.Binary as CB
49−import Control.Monad.IO.Class (liftIO)
50import qualified Network.HTTP.Types as H
64import qualified Network.HTTP.Types as H
51−import Data.Either (partitionEithers)
65+import Control.Applicative ((<$>))
66+import Control.Exception (catchJust)
52−import Control.Monad (when, unless)
67+import Control.Monad (when, unless, guard)
53−import Control.Monad.Trans.Class (lift)
54import Control.Monad.Trans.Resource (allocate, release, register, InternalState, runInternalState)
68import Control.Monad.Trans.Resource (allocate, release, register, InternalState, runInternalState)
55−import Data.Conduit.Internal (Pipe (NeedInput, HaveOutput), (>+>), withUpstream, injectLeftovers, ConduitM (..))
69+import Data.IORef
70+import Network.HTTP.Types (hContentType)
71+import Network.HTTP2( HTTP2Error (..), ErrorCodeId (..) )
56−import Data.Void (Void)
72+import Data.CaseInsensitive (mk)
73+
74+import Prelude hiding (lines)
57
75
58breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
76breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
59breakDiscard w s =
77breakDiscard w s =
@@ -85,32 +103,186 @@ parseHttpAccept = map fst
85 _ -> 1.0
103 _ -> 1.0
86
104
87-- | Store uploaded files in memory
105-- | Store uploaded files in memory
88−lbsBackEnd :: Monad m => ignored1 -> ignored2 -> Sink S.ByteString m L.ByteString
106+lbsBackEnd :: Monad m => ignored1 -> ignored2 -> m S.ByteString -> m L.ByteString
89−lbsBackEnd _ _ = fmap L.fromChunks CL.consume
107+lbsBackEnd _ _ popper =
108+ loop id
109+ where
110+ loop front = do
111+ bs <- popper
112+ if S.null bs
113+ then return $ L.fromChunks $ front []
114+ else loop $ front . (bs:)
90
115
91-- | Save uploaded files on disk as temporary files
116-- | Save uploaded files on disk as temporary files
92--
117--
93-- Note: starting with version 2.0, removal of temp files is registered with
118-- Note: starting with version 2.0, removal of temp files is registered with
94-- the provided @InternalState@. It is the responsibility of the caller to
119-- the provided @InternalState@. It is the responsibility of the caller to
95-- ensure that this @InternalState@ gets cleaned up.
120-- ensure that this @InternalState@ gets cleaned up.
96−tempFileBackEnd :: InternalState -> ignored1 -> ignored2 -> Sink S.ByteString IO FilePath
121+tempFileBackEnd :: InternalState -> ignored1 -> ignored2 -> IO S.ByteString -> IO FilePath
97tempFileBackEnd = tempFileBackEndOpts getTemporaryDirectory "webenc.buf"
122tempFileBackEnd = tempFileBackEndOpts getTemporaryDirectory "webenc.buf"
98
123
99−-- | Same as 'tempFileSink', but use configurable temp folders and patterns.
124+-- | Same as 'tempFileBackEnd', but use configurable temp folders and patterns.
100tempFileBackEndOpts :: IO FilePath -- ^ get temporary directory
125tempFileBackEndOpts :: IO FilePath -- ^ get temporary directory
101 -> String -- ^ filename pattern
126 -> String -- ^ filename pattern
102 -> InternalState
127 -> InternalState
103 -> ignored1
128 -> ignored1
104 -> ignored2
129 -> ignored2
105− -> Sink S.ByteString IO FilePath
130+ -> IO S.ByteString
131+ -> IO FilePath
106−tempFileBackEndOpts getTmpDir pattern internalState _ _ = do
132+tempFileBackEndOpts getTmpDir pattrn internalState _ _ popper = do
107− (key, (fp, h)) <- flip runInternalState internalState $ allocate (do
133+ (key, (fp, h)) <- flip runInternalState internalState $ allocate it (hClose . snd)
108− tempDir <- getTmpDir
109− openBinaryTempFile tempDir pattern) (\(_, h) -> hClose h)
110− _ <- runInternalState (register $ removeFile fp) internalState
134+ _ <- runInternalState (register $ removeFileQuiet fp) internalState
111− CB.sinkHandle h
135+ fix $ \loop -> do
136+ bs <- popper
137+ unless (S.null bs) $ do
138+ S.hPut h bs
139+ loop
112− lift $ release key
140+ release key
113 return fp
141 return fp
142+ where
143+ it = do
144+ tempDir <- getTmpDir
145+ openBinaryTempFile tempDir pattrn
146+ removeFileQuiet fp = catchJust (guard . isDoesNotExistError)
147+ (removeFile fp)
148+ (const $ return ())
149+
150+-- | A data structure that describes the behavior of
151+-- the parseRequestBodyEx function.
152+--
153+-- @since 3.0.16.0
154+data ParseRequestBodyOptions = ParseRequestBodyOptions
155+ { -- | The maximum length of a filename
156+ prboKeyLength :: Maybe Int
157+ , -- | The maximum number of files.
158+ prboMaxNumFiles :: Maybe Int
159+ , -- | The maximum filesize per file.
160+ prboMaxFileSize :: Maybe Int64
161+ , -- | The maximum total filesize.
162+ prboMaxFilesSize :: Maybe Int64
163+ , -- | The maximum size of the sum of all parameters
164+ prboMaxParmsSize :: Maybe Int
165+ , -- | The maximum header lines per mime/multipart entry
166+ prboMaxHeaderLines :: Maybe Int
167+ , -- | The maximum header line length per mime/multipart entry
168+ prboMaxHeaderLineLength :: Maybe Int }
169+
170+-- | Set the maximum length of a filename.
171+--
172+-- @since 3.0.16.0
173+setMaxRequestKeyLength :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
174+setMaxRequestKeyLength l p = p { prboKeyLength=Just l }
175+
176+-- | Do not limit the length of filenames.
177+--
178+-- @since 3.0.16.0
179+clearMaxRequestKeyLength :: ParseRequestBodyOptions -> ParseRequestBodyOptions
180+clearMaxRequestKeyLength p = p { prboKeyLength=Nothing }
181+
182+-- | Set the maximum number of files per request.
183+--
184+-- @since 3.0.16.0
185+setMaxRequestNumFiles :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
186+setMaxRequestNumFiles l p = p { prboMaxNumFiles=Just l }
187+
188+-- | Do not limit the maximum number of files per request.
189+--
190+-- @since 3.0.16.0
191+clearMaxRequestNumFiles :: ParseRequestBodyOptions -> ParseRequestBodyOptions
192+clearMaxRequestNumFiles p = p { prboMaxNumFiles=Nothing }
193+
194+-- | Set the maximum filesize per file (in bytes).
195+--
196+-- @since 3.0.16.0
197+setMaxRequestFileSize :: Int64 -> ParseRequestBodyOptions -> ParseRequestBodyOptions
198+setMaxRequestFileSize l p = p { prboMaxFileSize=Just l }
199+
200+-- | Do not limit the maximum filesize per file.
201+--
202+-- @since 3.0.16.0
203+clearMaxRequestFileSize :: ParseRequestBodyOptions -> ParseRequestBodyOptions
204+clearMaxRequestFileSize p = p { prboMaxFileSize=Nothing }
205+
206+-- | Set the maximum size of all files per request.
207+--
208+-- @since 3.0.16.0
209+setMaxRequestFilesSize :: Int64 -> ParseRequestBodyOptions -> ParseRequestBodyOptions
210+setMaxRequestFilesSize l p = p { prboMaxFilesSize=Just l }
211+
212+-- | Do not limit the maximum size of all files per request.
213+--
214+-- @since 3.0.16.0
215+clearMaxRequestFilesSize :: ParseRequestBodyOptions -> ParseRequestBodyOptions
216+clearMaxRequestFilesSize p = p { prboMaxFilesSize=Nothing }
217+
218+-- | Set the maximum size of the sum of all parameters.
219+--
220+-- @since 3.0.16.0
221+setMaxRequestParmsSize :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
222+setMaxRequestParmsSize l p = p { prboMaxParmsSize=Just l }
223+
224+-- | Do not limit the maximum size of the sum of all parameters.
225+--
226+-- @since 3.0.16.0
227+clearMaxRequestParmsSize :: ParseRequestBodyOptions -> ParseRequestBodyOptions
228+clearMaxRequestParmsSize p = p { prboMaxParmsSize=Nothing }
229+
230+-- | Set the maximum header lines per mime/multipart entry.
231+--
232+-- @since 3.0.16.0
233+setMaxHeaderLines :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
234+setMaxHeaderLines l p = p { prboMaxHeaderLines=Just l }
235+
236+-- | Do not limit the maximum header lines per mime/multipart entry.
237+--
238+-- @since 3.0.16.0
239+clearMaxHeaderLines:: ParseRequestBodyOptions -> ParseRequestBodyOptions
240+clearMaxHeaderLines p = p { prboMaxHeaderLines=Nothing }
241+
242+-- | Set the maximum header line length per mime/multipart entry.
243+--
244+-- @since 3.0.16.0
245+setMaxHeaderLineLength :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
246+setMaxHeaderLineLength l p = p { prboMaxHeaderLineLength=Just l }
247+
248+-- | Do not limit the maximum header lines per mime/multipart entry.
249+--
250+-- @since 3.0.16.0
251+clearMaxHeaderLineLength :: ParseRequestBodyOptions -> ParseRequestBodyOptions
252+clearMaxHeaderLineLength p = p { prboMaxHeaderLineLength=Nothing }
253+
254+-- | A reasonable default set of parsing options.
255+-- Maximum key/filename length: 32 bytes;
256+-- maximum files: 10; filesize unlimited; maximum
257+-- size for parameters: 64kbytes; maximum number of header
258+-- lines: 32 bytes (applies only to headers of a mime/multipart message);
259+-- maximum header line length: Apache's default for that is 8190 bytes
260+-- (http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestline)
261+-- so we're using that here as well.
262+--
263+-- @since 3.0.16.0
264+defaultParseRequestBodyOptions :: ParseRequestBodyOptions
265+defaultParseRequestBodyOptions = ParseRequestBodyOptions
266+ { prboKeyLength=Just 32
267+ , prboMaxNumFiles=Just 10
268+ , prboMaxFileSize=Nothing
269+ , prboMaxFilesSize=Nothing
270+ , prboMaxParmsSize=Just 65336
271+ , prboMaxHeaderLines=Just 32
272+ , prboMaxHeaderLineLength=Just 8190 }
273+
274+-- | Do not impose any memory limits.
275+--
276+-- @since 3.0.21.0
277+noLimitParseRequestBodyOptions :: ParseRequestBodyOptions
278+noLimitParseRequestBodyOptions = ParseRequestBodyOptions
279+ { prboKeyLength=Nothing
280+ , prboMaxNumFiles=Nothing
281+ , prboMaxFileSize=Nothing
282+ , prboMaxFilesSize=Nothing
283+ , prboMaxParmsSize=Nothing
284+ , prboMaxHeaderLines=Nothing
285+ , prboMaxHeaderLineLength=Nothing }
114
286
115-- | Information on an uploaded file.
287-- | Information on an uploaded file.
116data FileInfo c = FileInfo
288data FileInfo c = FileInfo
@@ -126,17 +298,26 @@ type Param = (S.ByteString, S.ByteString)
126-- | Post parameter name and associated file information.
298-- | Post parameter name and associated file information.
127type File y = (S.ByteString, FileInfo y)
299type File y = (S.ByteString, FileInfo y)
128
300
129−-- | A file uploading backend. Takes the parameter name, file name, and content
301+-- | A file uploading backend. Takes the parameter name, file name, and a
130−-- type, and returns a `Sink` for storing the contents.
302+-- stream of data.
131type BackEnd a = S.ByteString -- ^ parameter name
303type BackEnd a = S.ByteString -- ^ parameter name
132 -> FileInfo ()
304 -> FileInfo ()
133− -> Sink S.ByteString IO a
305+ -> IO S.ByteString
134−
306+ -> IO a
307+
308+-- | The mimetype of the http body.
309+-- Depending on whether just parameters or parameters and files
310+-- are passed, one or the other mimetype should be used.
311+data RequestBodyType
312+ = -- | application/x-www-form-urlencoded (parameters only)
313+ UrlEncoded
314+ | -- | multipart/form-data (parameters and files)
135−data RequestBodyType = UrlEncoded | Multipart S.ByteString
315+ Multipart S.ByteString
136−
316+
317+-- | Get the mimetype of the body of an http request.
137getRequestBodyType :: Request -> Maybe RequestBodyType
318getRequestBodyType :: Request -> Maybe RequestBodyType
138getRequestBodyType req = do
319getRequestBodyType req = do
139− ctype' <- lookup "Content-Type" $ requestHeaders req
320+ ctype' <- lookup hContentType $ requestHeaders req
140 let (ctype, attrs) = parseContentType ctype'
321 let (ctype, attrs) = parseContentType ctype'
141 case ctype of
322 case ctype of
142 "application/x-www-form-urlencoded" -> return UrlEncoded
323 "application/x-www-form-urlencoded" -> return UrlEncoded
@@ -146,7 +327,7 @@ getRequestBodyType req = do
146-- | Parse a content type value, turning a single @ByteString@ into the actual
327-- | Parse a content type value, turning a single @ByteString@ into the actual
147-- content type and a list of pairs of attributes.
328-- content type and a list of pairs of attributes.
148--
329--
149−-- Since 1.3.2
330+-- @since 1.3.2
150parseContentType :: S.ByteString -> (S.ByteString, [(S.ByteString, S.ByteString)])
331parseContentType :: S.ByteString -> (S.ByteString, [(S.ByteString, S.ByteString)])
151parseContentType a = do
332parseContentType a = do
152 let (ctype, b) = S.break (== semicolon) a
333 let (ctype, b) = S.break (== semicolon) a
@@ -156,6 +337,9 @@ parseContentType a = do
156 semicolon = 59
337 semicolon = 59
157 equals = 61
338 equals = 61
158 space = 32
339 space = 32
340+ dq s = if S.length s > 2 && S.head s == 34 && S.last s == 34 -- quote
341+ then S.tail $ S.init s
342+ else s
159 goAttrs front bs
343 goAttrs front bs
160 | S.null bs = front []
344 | S.null bs = front []
161 | otherwise =
345 | otherwise =
@@ -164,69 +348,172 @@ parseContentType a = do
164 goAttr bs =
348 goAttr bs =
165 let (k, v') = S.break (== equals) bs
349 let (k, v') = S.break (== equals) bs
166 v = S.drop 1 v'
350 v = S.drop 1 v'
167− in (strip k, strip v)
351+ in (strip k, dq $ strip v)
168 strip = S.dropWhile (== space) . fst . S.breakEnd (/= space)
352 strip = S.dropWhile (== space) . fst . S.breakEnd (/= space)
169
353
354+-- | Parse the body of an HTTP request.
355+-- See parseRequestBodyEx for details.
356+-- Note: This function does not limit the memory it allocates.
357+-- When dealing with untrusted data (as is usually the case when
358+-- receiving input from the internet), it is recommended to
359+-- use the 'parseRequestBodyEx' function instead.
170parseRequestBody :: BackEnd y
360parseRequestBody :: BackEnd y
171 -> Request
361 -> Request
172 -> IO ([Param], [File y])
362 -> IO ([Param], [File y])
173−parseRequestBody s r =
363+parseRequestBody = parseRequestBodyEx noLimitParseRequestBodyOptions
364+
365+-- | Parse the body of an HTTP request, limit resource usage.
366+-- The HTTP body can contain both parameters and files.
367+-- This function will return a list of key,value pairs
368+-- for all parameters, and a list of key,a pairs
369+-- for filenames. The a depends on the used backend that
370+-- is responsible for storing the received files.
371+parseRequestBodyEx :: ParseRequestBodyOptions
372+ -> BackEnd y
373+ -> Request
374+ -> IO ([Param], [File y])
375+parseRequestBodyEx o s r =
174 case getRequestBodyType r of
376 case getRequestBodyType r of
175 Nothing -> return ([], [])
377 Nothing -> return ([], [])
176− Just rbt -> fmap partitionEithers $ requestBody r $$ conduitRequestBody s rbt =$ CL.consume
378+ Just rbt -> sinkRequestBodyEx o s rbt (requestBody r)
177
379
178sinkRequestBody :: BackEnd y
380sinkRequestBody :: BackEnd y
179 -> RequestBodyType
381 -> RequestBodyType
382+ -> IO S.ByteString
180− -> Sink S.ByteString IO ([Param], [File y])
383+ -> IO ([Param], [File y])
181−sinkRequestBody s r = fmap partitionEithers $ conduitRequestBody s r =$ CL.consume
384+sinkRequestBody = sinkRequestBodyEx noLimitParseRequestBodyOptions
182
385
386+-- |
387+--
388+-- @since 3.0.16.0
389+sinkRequestBodyEx :: ParseRequestBodyOptions
183−conduitRequestBody :: BackEnd y
390+ -> BackEnd y
184− -> RequestBodyType
391+ -> RequestBodyType
392+ -> IO S.ByteString
393+ -> IO ([Param], [File y])
394+sinkRequestBodyEx o s r body = do
395+ ref <- newIORef ([], [])
396+ let add x = atomicModifyIORef ref $ \(y, z) ->
397+ case x of
398+ Left y' -> ((y':y, z), ())
399+ Right z' -> ((y, z':z), ())
400+ conduitRequestBodyEx o s r body add
401+ (\(a, b) -> (reverse a, reverse b)) <$> readIORef ref
402+
403+conduitRequestBodyEx :: ParseRequestBodyOptions
404+ -> BackEnd y
405+ -> RequestBodyType
406+ -> IO S.ByteString
185− -> Conduit S.ByteString IO (Either Param (File y))
407+ -> (Either Param (File y) -> IO ())
186−conduitRequestBody _ UrlEncoded = do
408+ -> IO ()
409+conduitRequestBodyEx o _ UrlEncoded rbody add = do
187 -- NOTE: in general, url-encoded data will be in a single chunk.
410 -- NOTE: in general, url-encoded data will be in a single chunk.
188 -- Therefore, I'm optimizing for the usual case by sticking with
411 -- Therefore, I'm optimizing for the usual case by sticking with
189 -- strict byte strings here.
412 -- strict byte strings here.
190− bs <- CL.consume
413+ let loop size front = do
414+ bs <- rbody
415+ if S.null bs
416+ then return $ S.concat $ front []
417+ else do
418+ let newsize = size + S.length bs
419+ case prboMaxParmsSize o of
420+ Just maxSize -> when (newsize > maxSize) $
421+ error "Maximum size of parameters exceeded"
422+ Nothing -> return ()
423+ loop newsize $ front . (bs:)
424+ bs <- loop 0 id
191− mapM_ yield $ map Left $ H.parseSimpleQuery $ S.concat bs
425+ mapM_ (add . Left) $ H.parseSimpleQuery bs
192−conduitRequestBody backend (Multipart bound) =
426+conduitRequestBodyEx o backend (Multipart bound) rbody add =
193− parsePieces backend $ S8.pack "--" `S.append` bound
427+ parsePiecesEx o backend (S8.pack "--" `S.append` bound) rbody add
194−
428+
429+
430+-- | Take one header or subheader line.
431+-- Since: 3.0.26
432+-- Throw 431 if headers too large.
195−takeLine :: Monad m => Consumer S.ByteString m (Maybe S.ByteString)
433+takeLine :: Maybe Int -> Source -> IO (Maybe S.ByteString)
196−takeLine =
434+takeLine maxlen src =
197− go id
435+ go ""
198 where
436 where
199− go front = await >>= maybe (close front) (push front)
437+ go front = do
200−
438+ bs <- readSource src
439+ case maxlen of
440+ Just maxlen' -> when (S.length front > maxlen') $
441+ E.throwIO $ ConnectionError (UnknownErrorCode 431)
442+ "Request Header Fields Too Large"
443+ Nothing -> return ()
444+ if S.null bs
445+ then close front
446+ else push front bs
447+
201− close front = leftover (front S.empty) >> return Nothing
448+ close front = leftover src front >> return Nothing
202 push front bs = do
449 push front bs = do
203− let (x, y) = S.break (== 10) $ front bs -- LF
450+ let (x, y) = S.break (== 10) bs -- LF
204 in if S.null y
451 in if S.null y
205− then go $ S.append x
452+ then go $ front `S.append` x
206 else do
453 else do
207− when (S.length y > 1) $ leftover $ S.drop 1 y
454+ when (S.length y > 1) $ leftover src $ S.drop 1 y
455+ let res = front `S.append` x
456+ case maxlen of
457+ Just maxlen' -> when (S.length res > maxlen') $
458+ E.throwIO $ ConnectionError (UnknownErrorCode 431)
459+ "Request Header Fields Too Large"
460+ Nothing -> return ()
208− return $ Just $ killCR x
461+ return $ Just $ killCR $ res
209−
462+
463+takeLines' :: Maybe Int -> Maybe Int -> Source -> IO [S.ByteString]
464+takeLines' lineLength maxLines source =
465+ reverse <$> takeLines'' [] lineLength maxLines source
466+
467+takeLines''
468+ :: [S.ByteString]
469+ -> Maybe Int
470+ -> Maybe Int
471+ -> Source
210−takeLines :: Consumer S.ByteString IO [S.ByteString]
472+ -> IO [S.ByteString]
211−takeLines = do
473+takeLines'' lines lineLength maxLines src = do
474+ case maxLines of
475+ Just maxLines' ->
476+ when (length lines > maxLines') $
477+ error "Too many lines in mime/multipart header"
478+ Nothing -> return ()
212− res <- takeLine
479+ res <- takeLine lineLength src
213 case res of
480 case res of
214− Nothing -> return []
481+ Nothing -> return lines
215 Just l
482 Just l
216− | S.null l -> return []
483+ | S.null l -> return lines
217− | otherwise -> do
484+ | otherwise -> takeLines'' (l:lines) lineLength maxLines src
218− ls <- takeLines
485+
219− return $ l : ls
486+data Source = Source (IO S.ByteString) (IORef S.ByteString)
220−
487+
488+mkSource :: IO S.ByteString -> IO Source
489+mkSource f = do
490+ ref <- newIORef S.empty
491+ return $ Source f ref
492+
493+readSource :: Source -> IO S.ByteString
494+readSource (Source f ref) = do
495+ bs <- atomicModifyIORef ref $ \bs -> (S.empty, bs)
496+ if S.null bs
497+ then f
498+ else return bs
499+
500+leftover :: Source -> S.ByteString -> IO ()
501+leftover (Source _ ref) bs = writeIORef ref bs
502+
503+parsePiecesEx :: ParseRequestBodyOptions
221−parsePieces :: BackEnd y
504+ -> BackEnd y
222− -> S.ByteString
505+ -> S.ByteString
506+ -> IO S.ByteString
223− -> ConduitM S.ByteString (Either Param (File y)) IO ()
507+ -> (Either Param (File y) -> IO ())
224−parsePieces sink bound =
508+ -> IO ()
225− loop
509+parsePiecesEx o sink bound rbody add =
510+ mkSource rbody >>= loop 0 0 0 0
226 where
511 where
227− loop = do
512+ loop :: Int -> Int -> Int -> Int64 -> Source -> IO ()
513+ loop numParms numFiles parmSize filesSize src = do
228− _boundLine <- takeLine
514+ _boundLine <- takeLine (prboMaxHeaderLineLength o) src
229− res' <- takeLines
515+ res' <- takeLines' (prboMaxHeaderLineLength o)
516+ (prboMaxHeaderLines o) src
230 unless (null res') $ do
517 unless (null res') $ do
231 let ls' = map parsePair res'
518 let ls' = map parsePair res'
232 let x = do
519 let x = do
@@ -237,31 +524,57 @@ parsePieces sink bound =
237 return (ct, name, lookup "filename" attrs)
524 return (ct, name, lookup "filename" attrs)
238 case x of
525 case x of
239 Just (mct, name, Just filename) -> do
526 Just (mct, name, Just filename) -> do
527+ case prboKeyLength o of
528+ Just maxKeyLength ->
529+ when (S.length name > maxKeyLength) $
530+ error "Filename is too long"
531+ Nothing -> return ()
532+ case prboMaxNumFiles o of
533+ Just maxFiles -> when (numFiles >= maxFiles) $
534+ error "Maximum number of files exceeded"
535+ Nothing -> return ()
240 let ct = fromMaybe "application/octet-stream" mct
536 let ct = fromMaybe "application/octet-stream" mct
241 fi0 = FileInfo filename ct ()
537 fi0 = FileInfo filename ct ()
538+ fs = catMaybes [ prboMaxFileSize o
539+ , subtract filesSize <$> prboMaxFilesSize o ]
540+ mfs = if fs == [] then Nothing else Just $ minimum fs
242− (wasFound, y) <- sinkTillBound' bound name fi0 sink
541+ ((wasFound, fileSize), y) <- sinkTillBound' bound name fi0 sink src mfs
542+ let newFilesSize = filesSize + fileSize
243− yield $ Right (name, fi0 { fileContent = y })
543+ add $ Right (name, fi0 { fileContent = y })
244− when wasFound loop
544+ when wasFound $ loop numParms (numFiles + 1) parmSize newFilesSize src
245 Just (_ct, name, Nothing) -> do
545 Just (_ct, name, Nothing) -> do
546+ case prboKeyLength o of
547+ Just maxKeyLength ->
548+ when (S.length name > maxKeyLength) $
549+ error "Parameter name is too long"
550+ Nothing -> return ()
246 let seed = id
551 let seed = id
247 let iter front bs = return $ front . (:) bs
552 let iter front bs = return $ front . (:) bs
248− (wasFound, front) <- sinkTillBound bound iter seed
553+ ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src
554+ (fromIntegral <$> prboMaxParmsSize o)
249 let bs = S.concat $ front []
555 let bs = S.concat $ front []
250 let x' = (name, bs)
556 let x' = (name, bs)
557+ let newParmSize = parmSize + S.length name + S.length bs
558+ case prboMaxParmsSize o of
559+ Just maxParmSize -> when (newParmSize > maxParmSize) $
560+ error "Maximum size of parameters exceeded"
561+ Nothing -> return ()
251− yield $ Left x'
562+ add $ Left x'
252− when wasFound loop
563+ when wasFound $ loop (numParms + 1) numFiles
564+ newParmSize filesSize src
253 _ -> do
565 _ -> do
254 -- ignore this part
566 -- ignore this part
255 let seed = ()
567 let seed = ()
256 iter () _ = return ()
568 iter () _ = return ()
257− (wasFound, ()) <- sinkTillBound bound iter seed
569+ ((wasFound, _fileSize), ()) <- sinkTillBound bound iter seed src Nothing
258− when wasFound loop
570+ when wasFound $ loop numParms numFiles parmSize filesSize src
259 where
571 where
260− contDisp = S8.pack "Content-Disposition"
572+ contDisp = mk $ S8.pack "Content-Disposition"
261− contType = S8.pack "Content-Type"
573+ contType = mk $ S8.pack "Content-Type"
262 parsePair s =
574 parsePair s =
263 let (x, y) = breakDiscard 58 s -- colon
575 let (x, y) = breakDiscard 58 s -- colon
264− in (x, S.dropWhile (== 32) y) -- space
576+ in (mk $ x, S.dropWhile (== 32) y) -- space
577+
265
578
266data Bound = FoundBound S.ByteString S.ByteString
579data Bound = FoundBound S.ByteString S.ByteString
267 | NoBound
580 | NoBound
@@ -269,7 +582,7 @@ data Bound = FoundBound S.ByteString S.ByteString
269 deriving (Eq, Show)
582 deriving (Eq, Show)
270
583
271findBound :: S.ByteString -> S.ByteString -> Bound
584findBound :: S.ByteString -> S.ByteString -> Bound
272−findBound b bs = handleBreak $ Search.breakOn b bs
585+findBound b bs = handleBreak $ S.breakSubstring b bs
273 where
586 where
274 handleBreak (h, t)
587 handleBreak (h, t)
275 | S.null t = go [lowBound..S.length bs - 1]
588 | S.null t = go [lowBound..S.length bs - 1]
@@ -295,64 +608,90 @@ sinkTillBound' :: S.ByteString
295 -> S.ByteString
608 -> S.ByteString
296 -> FileInfo ()
609 -> FileInfo ()
297 -> BackEnd y
610 -> BackEnd y
298− -> ConduitM S.ByteString o IO (Bool, y)
611+ -> Source
612+ -> Maybe Int64
613+ -> IO ((Bool, Int64), y)
299−sinkTillBound' bound name fi sink =
614+sinkTillBound' bound name fi sink src max' = do
300− ConduitM $ anyOutput $
615+ (next, final) <- wrapTillBound bound src max'
301− conduitTillBound bound >+> withUpstream (fix $ sink name fi)
616+ y <- sink name fi next
302− where
617+ b <- final
303− fix :: Sink S8.ByteString IO y -> Pipe Void S8.ByteString Void Bool IO y
618+ return (b, y)
304− fix p = ignoreTerm >+> injectLeftovers (unConduitM p)
619+
305− ignoreTerm = await' >>= maybe (return ()) (\x -> yield' x >> ignoreTerm)
620+data WTB = WTBWorking (S.ByteString -> S.ByteString)
306− await' = NeedInput (return . Just) (const $ return Nothing)
621+ | WTBDone Bool
307− yield' = HaveOutput (return ()) (return ())
622+wrapTillBound :: S.ByteString -- ^ bound
308−
623+ -> Source
309− anyOutput p = p >+> dropInput
624+ -> Maybe Int64
310− dropInput = NeedInput (const dropInput) return
625+ -> IO (IO S.ByteString, IO (Bool, Int64)) -- ^ Bool indicates if the bound was found
311−
626+wrapTillBound bound src max' = do
312−conduitTillBound :: Monad m
627+ ref <- newIORef $ WTBWorking id
313− => S.ByteString -- bound
628+ sref <- newIORef (0 :: Int64)
314− -> Pipe S.ByteString S.ByteString S.ByteString () m Bool
629+ return (go ref sref, final ref sref)
315−conduitTillBound bound =
316− unConduitM $
317− go id
318 where
630 where
319− go front = await >>= maybe (close front) (push front)
631+ final ref sref = do
320− close front = do
632+ x <- readIORef ref
321− let bs = front S.empty
633+ case x of
322− unless (S.null bs) $ yield bs
634+ WTBWorking _ -> error "wrapTillBound did not finish"
323− return False
635+ WTBDone y -> do
636+ siz <- readIORef sref
637+ return (y, siz)
638+
639+ go ref sref = do
640+ state <- readIORef ref
641+ case state of
642+ WTBDone _ -> return S.empty
643+ WTBWorking front -> do
644+ bs <- readSource src
645+ cur <- atomicModifyIORef' sref $ \ cur ->
646+ let new = cur + fromIntegral (S.length bs) in (new, new)
647+ case max' of
648+ Just max'' | cur > max'' ->
649+ E.throwIO $ ConnectionError (UnknownErrorCode 413) "Payload Too Large"
650+ _ -> return ()
651+ if S.null bs
652+ then do
653+ writeIORef ref $ WTBDone False
324− push front bs' = do
654+ return $ front bs
325− let bs = front bs'
655+ else push $ front bs
656+ where
657+ push bs = do
326− case findBound bound bs of
658+ case findBound bound bs of
327− FoundBound before after -> do
659+ FoundBound before after -> do
328− let before' = killCRLF before
660+ let before' = killCRLF before
329− yield before'
330− leftover after
661+ leftover src after
331− return True
662+ writeIORef ref $ WTBDone True
663+ return before'
332− NoBound -> do
664+ NoBound -> do
333− -- don't emit newlines, in case it's part of a bound
665+ -- don't emit newlines, in case it's part of a bound
334− let (toEmit, front') =
666+ let (toEmit, front') =
335− if not (S8.null bs) && S8.last bs `elem` "\r\n"
667+ if not (S8.null bs) && S8.last bs `elem` ['\r','\n']
336− then let (x, y) = S.splitAt (S.length bs - 2) bs
668+ then let (x, y) = S.splitAt (S.length bs - 2) bs
337− in (x, S.append y)
669+ in (x, S.append y)
338− else (bs, id)
670+ else (bs, id)
671+ writeIORef ref $ WTBWorking front'
339− yield toEmit
672+ if S.null toEmit
340− go front'
673+ then go ref sref
674+ else return toEmit
341− PartialBound -> go $ S.append bs
675+ PartialBound -> do
676+ writeIORef ref $ WTBWorking $ S.append bs
677+ go ref sref
342
678
343sinkTillBound :: S.ByteString
679sinkTillBound :: S.ByteString
344 -> (x -> S.ByteString -> IO x)
680 -> (x -> S.ByteString -> IO x)
345 -> x
681 -> x
346− -> Consumer S.ByteString IO (Bool, x)
682+ -> Source
683+ -> Maybe Int64
684+ -> IO ((Bool, Int64), x)
347−sinkTillBound bound iter seed0 =
685+sinkTillBound bound iter seed0 src max' = do
348− ConduitM $
686+ (next, final) <- wrapTillBound bound src max'
349− (conduitTillBound bound >+> (withUpstream $ ij $ CL.foldM iter' seed0))
687+ let loop seed = do
350− where
688+ bs <- next
351− iter' a b = liftIO $ iter a b
689+ if S.null bs
352− ij (ConduitM p) = ignoreTerm >+> injectLeftovers p
690+ then return seed
353− ignoreTerm = await' >>= maybe (return ()) (\x -> yield' x >> ignoreTerm)
691+ else iter seed bs >>= loop
354− await' = NeedInput (return . Just) (const $ return Nothing)
692+ seed <- loop seed0
355− yield' = HaveOutput (return ()) (return ())
693+ b <- final
694+ return (b, seed)
356
695
357parseAttrs :: S.ByteString -> [(S.ByteString, S.ByteString)]
696parseAttrs :: S.ByteString -> [(S.ByteString, S.ByteString)]
358parseAttrs = map go . S.split 59 -- semicolon
697parseAttrs = map go . S.split 59 -- semicolon
wai-extra/Network/Wai/Request.hsnew file+101
@@ -0,0 +1,101 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+-- | Some helpers for interrogating a WAI 'Request'.
3+
4+module Network.Wai.Request
5+ ( appearsSecure
6+ , guessApproot
7+ , RequestSizeException(..)
8+ , requestSizeCheck
9+ ) where
10+
11+import Data.ByteString (ByteString)
12+import Data.Maybe (fromMaybe)
13+import Network.HTTP.Types (HeaderName)
14+import Network.Wai
15+
16+import qualified Data.ByteString as S
17+import qualified Data.ByteString.Char8 as C
18+import Control.Exception (Exception, throwIO)
19+import Data.Typeable (Typeable)
20+import Data.Word (Word64)
21+import Data.IORef (atomicModifyIORef', newIORef)
22+
23+
24+-- | Does this request appear to have been made over an SSL connection?
25+--
26+-- This function first checks @'isSecure'@, but also checks for headers that may
27+-- indicate a secure connection even in the presence of reverse proxies.
28+--
29+-- Note: these headers can be easily spoofed, so decisions which require a true
30+-- SSL connection (i.e. sending sensitive information) should only use
31+-- @'isSecure'@. This is not always the case though: for example, deciding to
32+-- force a non-SSL request to SSL by redirect. One can safely choose not to
33+-- redirect when the request /appears/ secure, even if it's actually not.
34+--
35+-- @since 3.0.7
36+appearsSecure :: Request -> Bool
37+appearsSecure request = isSecure request || any (uncurry matchHeader)
38+ [ ("HTTPS" , (== "on"))
39+ , ("HTTP_X_FORWARDED_SSL" , (== "on"))
40+ , ("HTTP_X_FORWARDED_SCHEME", (== "https"))
41+ , ("HTTP_X_FORWARDED_PROTO" , ((== ["https"]) . take 1 . C.split ','))
42+ , ("X-Forwarded-Proto" , (== "https")) -- Used by Nginx and AWS ELB.
43+ ]
44+
45+ where
46+ matchHeader :: HeaderName -> (ByteString -> Bool) -> Bool
47+ matchHeader h f = maybe False f $ lookup h $ requestHeaders request
48+
49+-- | Guess the \"application root\" based on the given request.
50+--
51+-- The application root is the basis for forming URLs pointing at the current
52+-- application. For more information and relevant caveats, please see
53+-- "Network.Wai.Middleware.Approot".
54+--
55+-- @since 3.0.7
56+guessApproot :: Request -> ByteString
57+guessApproot req =
58+ (if appearsSecure req then "https://" else "http://") `S.append`
59+ (fromMaybe "localhost" $ requestHeaderHost req)
60+
61+-- | see 'requestSizeCheck'
62+--
63+-- @since 3.0.15
64+data RequestSizeException
65+ = RequestSizeException Word64
66+ deriving (Eq, Ord, Typeable)
67+
68+instance Exception RequestSizeException
69+
70+instance Show RequestSizeException where
71+ showsPrec p (RequestSizeException limit) =
72+ showString ("Request Body is larger than ") . showsPrec p limit . showString " bytes."
73+
74+-- | Check request body size to avoid server crash when request is too large.
75+--
76+-- This function first checks @'requestBodyLength'@, if content-length is known
77+-- but larger than limit, or it's unknown but we have received too many chunks,
78+-- a 'RequestSizeException' are thrown when user use @'requestBody'@ to extract
79+-- request body inside IO.
80+--
81+-- @since 3.0.15
82+requestSizeCheck :: Word64 -> Request -> IO Request
83+requestSizeCheck maxSize req =
84+ case requestBodyLength req of
85+ KnownLength len ->
86+ if len > maxSize
87+ then return $ req { requestBody = throwIO (RequestSizeException maxSize) }
88+ else return req
89+ ChunkedBody -> do
90+ currentSize <- newIORef 0
91+ return $ req
92+ { requestBody = do
93+ bs <- requestBody req
94+ total <-
95+ atomicModifyIORef' currentSize $ \sz ->
96+ let nextSize = sz + fromIntegral (S.length bs)
97+ in (nextSize, nextSize)
98+ if total > maxSize
99+ then throwIO (RequestSizeException maxSize)
100+ else return bs
101+ }
wai-extra/Network/Wai/Test.hsnew file+333
@@ -0,0 +1,333 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE DeriveDataTypeable #-}
4+module Network.Wai.Test
5+ ( -- * Session
6+ Session
7+ , runSession
8+ -- * Client Cookies
9+ , ClientCookies
10+ , getClientCookies
11+ , modifyClientCookies
12+ , setClientCookie
13+ , deleteClientCookie
14+ -- * Requests
15+ , request
16+ , srequest
17+ , SRequest (..)
18+ , SResponse (..)
19+ , defaultRequest
20+ , setPath
21+ , setRawPathInfo
22+ -- * Assertions
23+ , assertStatus
24+ , assertContentType
25+ , assertBody
26+ , assertBodyContains
27+ , assertHeader
28+ , assertNoHeader
29+ , assertClientCookieExists
30+ , assertNoClientCookieExists
31+ , assertClientCookieValue
32+ , WaiTestFailure (..)
33+ ) where
34+
35+#if __GLASGOW_HASKELL__ < 710
36+import Control.Applicative ((<$>))
37+import Data.Monoid (mempty, mappend)
38+#endif
39+
40+import Network.Wai
41+import Network.Wai.Internal (ResponseReceived (ResponseReceived))
42+import Network.Wai.Test.Internal
43+import Control.Monad.IO.Class (liftIO)
44+import Control.Monad.Trans.Class (lift)
45+import qualified Control.Monad.Trans.State as ST
46+import Control.Monad.Trans.Reader (runReaderT, ask)
47+import Control.Monad (unless)
48+import Control.DeepSeq (deepseq)
49+import Control.Exception (throwIO, Exception)
50+import Data.Typeable (Typeable)
51+import qualified Data.Map as Map
52+import qualified Web.Cookie as Cookie
53+import Data.ByteString (ByteString)
54+import qualified Data.ByteString.Char8 as S8
55+import Data.ByteString.Builder (toLazyByteString)
56+import qualified Data.ByteString.Lazy as L
57+import qualified Data.ByteString.Lazy.Char8 as L8
58+import qualified Network.HTTP.Types as H
59+import Data.CaseInsensitive (CI)
60+import qualified Data.ByteString as S
61+import qualified Data.Text as T
62+import qualified Data.Text.Encoding as TE
63+import Data.IORef
64+import Data.Time.Clock (getCurrentTime)
65+
66+-- |
67+--
68+-- Since 3.0.6
69+getClientCookies :: Session ClientCookies
70+getClientCookies = clientCookies <$> lift ST.get
71+
72+-- |
73+--
74+-- Since 3.0.6
75+modifyClientCookies :: (ClientCookies -> ClientCookies) -> Session ()
76+modifyClientCookies f =
77+ lift (ST.modify (\cs -> cs { clientCookies = f $ clientCookies cs }))
78+
79+-- |
80+--
81+-- Since 3.0.6
82+setClientCookie :: Cookie.SetCookie -> Session ()
83+setClientCookie c =
84+ modifyClientCookies
85+ (Map.insert (Cookie.setCookieName c) c)
86+
87+-- |
88+--
89+-- Since 3.0.6
90+deleteClientCookie :: ByteString -> Session ()
91+deleteClientCookie cookieName =
92+ modifyClientCookies
93+ (Map.delete cookieName)
94+
95+-- | See also: 'runSessionWith'.
96+runSession :: Session a -> Application -> IO a
97+runSession session app = ST.evalStateT (runReaderT session app) initState
98+
99+data SRequest = SRequest
100+ { simpleRequest :: Request
101+ , simpleRequestBody :: L.ByteString
102+ -- ^ Request body that will override the one set in 'simpleRequest'.
103+ --
104+ -- This is usually simpler than setting the body as a stateful IO-action
105+ -- in 'simpleRequest'.
106+ }
107+data SResponse = SResponse
108+ { simpleStatus :: H.Status
109+ , simpleHeaders :: H.ResponseHeaders
110+ , simpleBody :: L.ByteString
111+ }
112+ deriving (Show, Eq)
113+
114+request :: Request -> Session SResponse
115+request req = do
116+ app <- ask
117+ req' <- addCookiesToRequest req
118+ response <- liftIO $ do
119+ ref <- newIORef $ error "runResponse gave no result"
120+ ResponseReceived <- app req' (runResponse ref)
121+ readIORef ref
122+ extractSetCookieFromSResponse response
123+
124+-- | Set whole path (request path + query string).
125+setPath :: Request -> S8.ByteString -> Request
126+setPath req path = req {
127+ pathInfo = segments
128+ , rawPathInfo = (L8.toStrict . toLazyByteString) (H.encodePathSegments segments)
129+ , queryString = query
130+ , rawQueryString = (H.renderQuery True query)
131+ }
132+ where
133+ (segments, query) = H.decodePath path
134+
135+setRawPathInfo :: Request -> S8.ByteString -> Request
136+setRawPathInfo r rawPinfo =
137+ let pInfo = dropFrontSlash $ T.split (== '/') $ TE.decodeUtf8 rawPinfo
138+ in r { rawPathInfo = rawPinfo, pathInfo = pInfo }
139+ where
140+ dropFrontSlash ("":"":[]) = [] -- homepage, a single slash
141+ dropFrontSlash ("":path) = path
142+ dropFrontSlash path = path
143+
144+addCookiesToRequest :: Request -> Session Request
145+addCookiesToRequest req = do
146+ oldClientCookies <- getClientCookies
147+ let requestPath = "/" `T.append` T.intercalate "/" (pathInfo req)
148+ currentUTCTime <- liftIO getCurrentTime
149+ let cookiesForRequest =
150+ Map.filter
151+ (\c -> checkCookieTime currentUTCTime c
152+ && checkCookiePath requestPath c)
153+ oldClientCookies
154+ let cookiePairs = [ (Cookie.setCookieName c, Cookie.setCookieValue c)
155+ | c <- map snd $ Map.toList cookiesForRequest
156+ ]
157+ let cookieValue = L8.toStrict . toLazyByteString $ Cookie.renderCookies cookiePairs
158+ addCookieHeader rest
159+ | null cookiePairs = rest
160+ | otherwise = ("Cookie", cookieValue) : rest
161+ return $ req { requestHeaders = addCookieHeader $ requestHeaders req }
162+ where checkCookieTime t c =
163+ case Cookie.setCookieExpires c of
164+ Nothing -> True
165+ Just t' -> t < t'
166+ checkCookiePath p c =
167+ case Cookie.setCookiePath c of
168+ Nothing -> True
169+ Just p' -> p' `S8.isPrefixOf` TE.encodeUtf8 p
170+
171+extractSetCookieFromSResponse :: SResponse -> Session SResponse
172+extractSetCookieFromSResponse response = do
173+ let setCookieHeaders =
174+ filter (("Set-Cookie"==) . fst) $ simpleHeaders response
175+ let newClientCookies = map (Cookie.parseSetCookie . snd) setCookieHeaders
176+ modifyClientCookies
177+ (Map.union
178+ (Map.fromList [(Cookie.setCookieName c, c) | c <- newClientCookies ]))
179+ return response
180+
181+-- | Similar to 'request', but allows setting the request body as a plain
182+-- 'L.ByteString'.
183+srequest :: SRequest -> Session SResponse
184+srequest (SRequest req bod) = do
185+ refChunks <- liftIO $ newIORef $ L.toChunks bod
186+ request $
187+ req
188+ { requestBody = atomicModifyIORef refChunks $ \bss ->
189+ case bss of
190+ [] -> ([], S.empty)
191+ x:y -> (y, x)
192+ }
193+
194+runResponse :: IORef SResponse -> Response -> IO ResponseReceived
195+runResponse ref res = do
196+ refBuilder <- newIORef mempty
197+ let add y = atomicModifyIORef refBuilder $ \x -> (x `mappend` y, ())
198+ withBody $ \body -> body add (return ())
199+ builder <- readIORef refBuilder
200+ let lbs = toLazyByteString builder
201+ len = L.length lbs
202+ -- Force evaluation of the body to have exceptions thrown at the right
203+ -- time.
204+ seq len $ writeIORef ref $ SResponse s h $ toLazyByteString builder
205+ return ResponseReceived
206+ where
207+ (s, h, withBody) = responseToStream res
208+
209+assertBool :: String -> Bool -> Session ()
210+assertBool s b = unless b $ assertFailure s
211+
212+assertString :: String -> Session ()
213+assertString s = unless (null s) $ assertFailure s
214+
215+assertFailure :: String -> Session ()
216+assertFailure msg = msg `deepseq` liftIO (throwIO (WaiTestFailure msg))
217+
218+data WaiTestFailure = WaiTestFailure String
219+ deriving (Show, Eq, Typeable)
220+instance Exception WaiTestFailure
221+
222+assertContentType :: ByteString -> SResponse -> Session ()
223+assertContentType ct SResponse{simpleHeaders = h} =
224+ case lookup "content-type" h of
225+ Nothing -> assertString $ concat
226+ [ "Expected content type "
227+ , show ct
228+ , ", but no content type provided"
229+ ]
230+ Just ct' -> assertBool (concat
231+ [ "Expected content type "
232+ , show ct
233+ , ", but received "
234+ , show ct'
235+ ]) (go ct == go ct')
236+ where
237+ go = S8.takeWhile (/= ';')
238+
239+assertStatus :: Int -> SResponse -> Session ()
240+assertStatus i SResponse{simpleStatus = s} = assertBool (concat
241+ [ "Expected status code "
242+ , show i
243+ , ", but received "
244+ , show sc
245+ ]) $ i == sc
246+ where
247+ sc = H.statusCode s
248+
249+assertBody :: L.ByteString -> SResponse -> Session ()
250+assertBody lbs SResponse{simpleBody = lbs'} = assertBool (concat
251+ [ "Expected response body "
252+ , show $ L8.unpack lbs
253+ , ", but received "
254+ , show $ L8.unpack lbs'
255+ ]) $ lbs == lbs'
256+
257+assertBodyContains :: L.ByteString -> SResponse -> Session ()
258+assertBodyContains lbs SResponse{simpleBody = lbs'} = assertBool (concat
259+ [ "Expected response body to contain "
260+ , show $ L8.unpack lbs
261+ , ", but received "
262+ , show $ L8.unpack lbs'
263+ ]) $ strict lbs `S.isInfixOf` strict lbs'
264+ where
265+ strict = S.concat . L.toChunks
266+
267+assertHeader :: CI ByteString -> ByteString -> SResponse -> Session ()
268+assertHeader header value SResponse{simpleHeaders = h} =
269+ case lookup header h of
270+ Nothing -> assertString $ concat
271+ [ "Expected header "
272+ , show header
273+ , " to be "
274+ , show value
275+ , ", but it was not present"
276+ ]
277+ Just value' -> assertBool (concat
278+ [ "Expected header "
279+ , show header
280+ , " to be "
281+ , show value
282+ , ", but received "
283+ , show value'
284+ ]) (value == value')
285+
286+assertNoHeader :: CI ByteString -> SResponse -> Session ()
287+assertNoHeader header SResponse{simpleHeaders = h} =
288+ case lookup header h of
289+ Nothing -> return ()
290+ Just s -> assertString $ concat
291+ [ "Unexpected header "
292+ , show header
293+ , " containing "
294+ , show s
295+ ]
296+
297+-- |
298+--
299+-- Since 3.0.6
300+assertClientCookieExists :: String -> ByteString -> Session ()
301+assertClientCookieExists s cookieName = do
302+ cookies <- getClientCookies
303+ assertBool s $ Map.member cookieName cookies
304+
305+-- |
306+--
307+-- Since 3.0.6
308+assertNoClientCookieExists :: String -> ByteString -> Session ()
309+assertNoClientCookieExists s cookieName = do
310+ cookies <- getClientCookies
311+ assertBool s $ not $ Map.member cookieName cookies
312+
313+-- |
314+--
315+-- Since 3.0.6
316+assertClientCookieValue :: String -> ByteString -> ByteString -> Session ()
317+assertClientCookieValue s cookieName cookieValue = do
318+ cookies <- getClientCookies
319+ case Map.lookup cookieName cookies of
320+ Nothing ->
321+ assertFailure (s ++ " (cookie does not exist)")
322+ Just c ->
323+ assertBool
324+ (concat
325+ [ s
326+ , " (actual value "
327+ , show $ Cookie.setCookieValue c
328+ , " expected value "
329+ , show cookieValue
330+ , ")"
331+ ]
332+ )
333+ (Cookie.setCookieValue c == cookieValue)
wai-extra/Network/Wai/Test/Internal.hsnew file+35
@@ -0,0 +1,35 @@
1+module Network.Wai.Test.Internal where
2+
3+import Network.Wai
4+import qualified Control.Monad.Trans.State as ST
5+import Control.Monad.Trans.Reader (ReaderT, runReaderT)
6+import Data.Map (Map)
7+import qualified Data.Map as Map
8+import qualified Web.Cookie as Cookie
9+import Data.ByteString (ByteString)
10+
11+type Session = ReaderT Application (ST.StateT ClientState IO)
12+
13+-- |
14+--
15+-- Since 3.0.6
16+type ClientCookies = Map ByteString Cookie.SetCookie
17+
18+data ClientState = ClientState
19+ { clientCookies :: ClientCookies
20+ }
21+
22+-- |
23+--
24+-- Since 3.0.20.0
25+initState :: ClientState
26+initState = ClientState Map.empty
27+
28+-- | Like 'runSession', but if allows you to hand in cookies and get
29+-- the updated cookies back. One use case for this is writing tests
30+-- that address the application under test alternatingly through rest
31+-- api and through db handle.
32+--
33+-- Since 3.0.20.0
34+runSessionWith :: ClientState -> Session a -> Application -> IO (a, ClientState)
35+runSessionWith st session app = ST.runStateT (runReaderT session app) st
wai-extra/Network/Wai/UrlMap.hs+8−7
@@ -1,4 +1,5 @@
1−{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances #-}
1+{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
2+{-# LANGUAGE ExistentialQuantification #-}
2{- | This module gives you a way to mount applications under sub-URIs.
3{- | This module gives you a way to mount applications under sub-URIs.
3For example:
4For example:
4
5
@@ -83,16 +84,16 @@ instance ToApplication Application where
83 toApplication = id
84 toApplication = id
84
85
85instance ToApplication UrlMap where
86instance ToApplication UrlMap where
86− toApplication urlMap = \req ->
87+ toApplication urlMap req sendResponse =
87 case try (pathInfo req) (unUrlMap urlMap) of
88 case try (pathInfo req) (unUrlMap urlMap) of
88 Just (newPath, app) ->
89 Just (newPath, app) ->
89− app $ req { pathInfo = newPath
90+ app (req { pathInfo = newPath
90− , rawPathInfo = makeRaw newPath
91+ , rawPathInfo = makeRaw newPath
91− }
92+ }) sendResponse
92 Nothing ->
93 Nothing ->
93− return $ responseLBS
94+ sendResponse $ responseLBS
94 status404
95 status404
95− [("content-type", "text/plain")]
96+ [(hContentType, "text/plain")]
96 "Not found\n"
97 "Not found\n"
97
98
98 where
99 where
wai-extra/READMEdeleted−1
@@ -1 +0,0 @@
1−Provides some basic WAI handlers and middleware.
wai-extra/README.mdnew file+16
@@ -0,0 +1,16 @@
1+# wai-extra
2+
3+The goal here is to provide common features without many dependencies.
4+
5+
6+## Example using Server-Sent Events ##
7+
8+There is a small example using Server-Sent Events (SSE) in the
9+`./example` directory.
10+
11+Run the commands below to start the server on http://localhost:8080
12+
13+```
14+$ stack build .
15+$ stack exec example
16+```
wai-extra/example/Main.hsnew file+62
@@ -0,0 +1,62 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Main where
3+
4+import Data.ByteString.Builder (string8)
5+import Control.Concurrent (forkIO, threadDelay)
6+import Control.Concurrent.Chan
7+import Control.Monad
8+import Data.Time.Clock.POSIX (getPOSIXTime)
9+import Network.HTTP.Types (status200)
10+import Network.Wai (Application, Middleware, pathInfo, responseFile)
11+import Network.Wai.EventSource (ServerEvent(..), eventSourceAppChan, eventSourceAppIO)
12+import Network.Wai.Handler.Warp (run)
13+import Network.Wai.Middleware.AddHeaders (addHeaders)
14+import Network.Wai.Middleware.Gzip (gzip, def)
15+
16+
17+app :: Chan ServerEvent -> Application
18+app chan req respond =
19+ case pathInfo req of
20+ [] -> respond $ responseFile status200 [("Content-Type", "text/html")] "example/index.html" Nothing
21+ ["esold"] -> eventSourceAppChan chan req respond
22+ ["eschan"] -> eventSourceAppChan chan req respond
23+ ["esio"] -> eventSourceAppIO eventIO req respond
24+ _ -> error "unexpected pathInfo"
25+
26+eventChan :: Chan ServerEvent -> IO ()
27+eventChan chan = forever $ do
28+ threadDelay 1000000
29+ time <- getPOSIXTime
30+ writeChan chan (ServerEvent Nothing Nothing [string8 . show $ time])
31+
32+eventIO :: IO ServerEvent
33+eventIO = do
34+ threadDelay 1000000
35+ time <- getPOSIXTime
36+ return $ ServerEvent (Just $ string8 "io")
37+ Nothing
38+ [string8 . show $ time]
39+
40+eventRaw :: (ServerEvent -> IO ()) -> IO () -> IO ()
41+eventRaw = handle 0
42+ where
43+ handle counter emit flush = do
44+ threadDelay 1000000
45+ _ <- emit $ ServerEvent (Just $ string8 "raw")
46+ Nothing
47+ [string8 . show $ counter]
48+ _ <- flush
49+ handle (counter + 1) emit flush
50+
51+main :: IO ()
52+main = do
53+ chan <- newChan
54+ _ <- forkIO . eventChan $ chan
55+ run 8080 (gzip def $ headers $ app chan)
56+ where
57+ -- headers required for SSE to work through nginx
58+ -- not required if using warp directly
59+ headers :: Middleware
60+ headers = addHeaders [ ("X-Accel-Buffering", "no")
61+ , ("Cache-Control", "no-cache")
62+ ]
wai-eventsource/static/index.html → wai-extra/example/index.htmlrenamed+3−2
@@ -1,8 +1,9 @@
1<!doctype html>
1<!doctype html>
2<head>
2<head>
3− <title>Hello</title>
3+ <title>Example</title>
4+ <link rel="icon" href="data:;base64,=">
4</head>
5</head>
5−<h1>Testing</h1>
6+<h1>Testing Server-Sent Events</h1>
6
7
7<div id="output"></div>
8<div id="output"></div>
8
9
wai-extra/test/Network/Wai/Middleware/ApprootSpec.hsnew file+37
@@ -0,0 +1,37 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.Middleware.ApprootSpec
3+ ( main
4+ , spec
5+ ) where
6+
7+import Test.Hspec
8+
9+import Network.Wai.Middleware.Approot
10+import Network.Wai.Test
11+import Network.Wai
12+import Network.HTTP.Types
13+import Data.ByteString (ByteString)
14+
15+main :: IO ()
16+main = hspec spec
17+
18+spec :: Spec
19+spec = do
20+ let test name host secure headers expected = it name $ do
21+ resp <- runApp host secure headers
22+ simpleHeaders resp `shouldBe` [("Approot", expected)]
23+ test "respects host header" "foobar" False [] "http://foobar"
24+ test "respects isSecure" "foobar" True [] "https://foobar"
25+ test "respects SSL headers" "foobar" False
26+ [("HTTP_X_FORWARDED_SSL", "on")] "https://foobar"
27+
28+runApp :: ByteString -> Bool -> RequestHeaders -> IO SResponse
29+runApp host secure headers = runSession
30+ (request defaultRequest
31+ { requestHeaderHost = Just host
32+ , isSecure = secure
33+ , requestHeaders = headers
34+ }) $ fromRequest app
35+
36+ where
37+ app req respond = respond $ responseLBS status200 [("Approot", getApproot req)] ""
wai-extra/test/Network/Wai/Middleware/ForceSSLSpec.hsnew file+60
@@ -0,0 +1,60 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.Middleware.ForceSSLSpec
3+ ( main
4+ , spec
5+ ) where
6+
7+import Test.Hspec
8+
9+import Network.Wai.Middleware.ForceSSL
10+
11+import Control.Monad
12+import Data.ByteString (ByteString)
13+import Data.Monoid ((<>))
14+import Network.HTTP.Types (methodPost, status200, status301, status307)
15+import Network.Wai
16+import Network.Wai.Test
17+
18+main :: IO ()
19+main = hspec spec
20+
21+spec :: Spec
22+spec = describe "forceSSL" (forM_ hosts $ \host -> hostSpec host)
23+ where
24+ hosts = ["example.com", "example.com:80", "example.com:8080"]
25+
26+hostSpec :: ByteString -> Spec
27+hostSpec host = describe ("forceSSL on host " <> show host <> "") $ do
28+
29+ it "redirects non-https requests to https" $ do
30+ resp <- runApp host forceSSL defaultRequest
31+
32+ simpleStatus resp `shouldBe` status301
33+ simpleHeaders resp `shouldBe` [("Location", "https://" <> host)]
34+
35+ it "redirects with 307 in the case of a non-GET request" $ do
36+ resp <- runApp host forceSSL defaultRequest
37+ { requestMethod = methodPost }
38+
39+ simpleStatus resp `shouldBe` status307
40+ simpleHeaders resp `shouldBe` [("Location", "https://" <> host)]
41+
42+ it "does not redirect already-secure requests" $ do
43+ resp <- runApp host forceSSL defaultRequest { isSecure = True }
44+
45+ simpleStatus resp `shouldBe` status200
46+
47+ it "preserves the original host, path, and query string" $ do
48+ resp <- runApp host forceSSL defaultRequest
49+ { rawPathInfo = "/foo/bar"
50+ , rawQueryString = "?baz=bat"
51+ }
52+
53+ simpleHeaders resp `shouldBe`
54+ [("Location", "https://" <> host <> "/foo/bar?baz=bat")]
55+
56+runApp :: ByteString -> Middleware -> Request -> IO SResponse
57+runApp host mw req = runSession
58+ (request req { requestHeaderHost = Just host }) $ mw app
59+ where
60+ app _ respond = respond $ responseLBS status200 [] ""
wai-extra/test/Network/Wai/Middleware/RoutedSpec.hsnew file+53
@@ -0,0 +1,53 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.Middleware.RoutedSpec
3+ ( main
4+ , spec
5+ ) where
6+
7+import Test.Hspec
8+
9+import Network.Wai.Middleware.Routed
10+import Network.Wai.Middleware.ForceSSL (forceSSL)
11+
12+import Network.HTTP.Types (hContentType, status200)
13+import Network.Wai
14+import Network.Wai.Test
15+import Data.ByteString (ByteString)
16+import Data.String (IsString)
17+
18+main :: IO ()
19+main = hspec spec
20+
21+spec :: Spec
22+spec = describe "forceSSL" $ do
23+ it "routed middleware" $ do
24+ let destination = "https://example.com/d/"
25+ let routedSslJsonApp prefix = routedMiddleware (checkPrefix prefix) forceSSL jsonApp
26+ checkPrefix p (p1:_) = p == p1
27+ checkPrefix _ _ = False
28+
29+ flip runSession (routedSslJsonApp "r") $ do
30+ res <- testDPath "http"
31+ assertNoHeader location res
32+ assertStatus 200 res
33+ assertBody "{\"foo\":\"bar\"}" res
34+
35+ flip runSession (routedSslJsonApp "d") $ do
36+ res2 <- testDPath "http"
37+ assertHeader location destination res2
38+ assertStatus 301 res2
39+
40+jsonApp :: Application
41+jsonApp _req cps = cps $ responseLBS status200
42+ [(hContentType, "application/json")]
43+ "{\"foo\":\"bar\"}"
44+
45+testDPath :: ByteString -> Session SResponse
46+testDPath proto =
47+ request $ flip setRawPathInfo "/d/" defaultRequest
48+ { requestHeaders = [("X-Forwarded-Proto", proto)]
49+ , requestHeaderHost = Just "example.com"
50+ }
51+
52+location :: IsString ci => ci
53+location = "Location"
wai-extra/test/Network/Wai/Middleware/StripHeadersSpec.hsnew file+58
@@ -0,0 +1,58 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.Middleware.StripHeadersSpec
3+ ( main
4+ , spec
5+ ) where
6+
7+import Test.Hspec
8+
9+import Network.Wai.Middleware.AddHeaders
10+import Network.Wai.Middleware.StripHeaders
11+
12+import Control.Arrow (first)
13+import Data.ByteString (ByteString)
14+import Data.Monoid ((<>))
15+import Network.HTTP.Types (status200)
16+import Network.Wai
17+import Network.Wai.Test
18+
19+import qualified Data.CaseInsensitive as CI
20+
21+
22+main :: IO ()
23+main = hspec spec
24+
25+
26+spec :: Spec
27+spec = describe "stripHeader" $ do
28+ let host = "example.com"
29+ let ciTestHeaders = map (first CI.mk) testHeaders
30+
31+ it "strips a specific header" $ do
32+ resp1 <- runApp host (addHeaders testHeaders) defaultRequest
33+ resp2 <- runApp host (stripHeaderIf "Foo" (const False) . addHeaders testHeaders) defaultRequest
34+ resp3 <- runApp host (stripHeaderIf "Foo" (const True) . addHeaders testHeaders) defaultRequest
35+
36+ simpleHeaders resp1 `shouldBe` ciTestHeaders
37+ simpleHeaders resp2 `shouldBe` ciTestHeaders
38+ simpleHeaders resp3 `shouldBe` tail ciTestHeaders
39+
40+ it "strips specific set of headers" $ do
41+ resp1 <- runApp host (addHeaders testHeaders) defaultRequest
42+ resp2 <- runApp host (stripHeadersIf ["Bar", "Foo"] (const False) . addHeaders testHeaders) defaultRequest
43+ resp3 <- runApp host (stripHeadersIf ["Bar", "Foo"] (const True) . addHeaders testHeaders) defaultRequest
44+
45+ simpleHeaders resp1 `shouldBe` ciTestHeaders
46+ simpleHeaders resp2 `shouldBe` ciTestHeaders
47+ simpleHeaders resp3 `shouldBe` [last ciTestHeaders]
48+
49+
50+testHeaders :: [(ByteString, ByteString)]
51+testHeaders = [("Foo", "fooey"), ("Bar", "barbican"), ("Baz", "bazooka")]
52+
53+
54+runApp :: ByteString -> Middleware -> Request -> IO SResponse
55+runApp host mw req = runSession
56+ (request req { requestHeaderHost = Just $ host <> ":80" }) $ mw app
57+ where
58+ app _ respond = respond $ responseLBS status200 [] ""
wai-extra/test/Network/Wai/Middleware/TimeoutSpec.hsnew file+57
@@ -0,0 +1,57 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module Network.Wai.Middleware.TimeoutSpec
4+ ( spec
5+ ) where
6+
7+import Test.Hspec
8+
9+import Control.Concurrent (threadDelay)
10+import Network.HTTP.Types (status200, status503, status504)
11+import Network.Wai
12+import Network.Wai.Middleware.Timeout
13+import Network.Wai.Test
14+
15+spec :: Spec
16+spec = do
17+ describe "timeout" $ do
18+ it "times out slow requests with 503" $ do
19+ let app _req respond = do
20+ threadDelay $ 2 * 1000000
21+ respond $ responseLBS status200 [] ""
22+
23+ resp <- runApp $ timeout 1 app
24+
25+ simpleStatus resp `shouldBe` status503
26+
27+ it "does not time out fast requests" $ do
28+ let app _req respond = respond $ responseLBS status200 [] ""
29+
30+ resp <- runApp $ timeout 3 app
31+
32+ simpleStatus resp `shouldBe` status200
33+
34+ describe "timeoutStatus" $ do
35+ it "allows customizing the timeout response status" $ do
36+ let app _req respond = do
37+ threadDelay $ 2 * 1000000
38+ respond $ responseLBS status200 [] ""
39+
40+ resp <- runApp $ timeoutStatus status504 1 app
41+
42+ simpleStatus resp `shouldBe` status504
43+
44+ describe "timeoutAs" $ do
45+ it "allows customizing the timeout response" $ do
46+ let app _req respond = do
47+ threadDelay $ 2 * 1000000
48+ respond $ responseLBS status200 [] ""
49+ timeoutResponse = responseLBS status503 [("X-Timeout", "1")] ""
50+
51+ resp <- runApp $ timeoutAs timeoutResponse 1 app
52+
53+ simpleStatus resp `shouldBe` status503
54+ simpleHeaders resp `shouldBe` [("X-Timeout", "1")]
55+
56+runApp :: Application -> IO SResponse
57+runApp = runSession $ request defaultRequest
wai-extra/test/Network/Wai/ParseSpec.hsnew file+294
@@ -0,0 +1,294 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.ParseSpec (main, spec) where
3+
4+import Test.Hspec
5+import Test.HUnit
6+
7+import System.IO
8+import Data.Monoid
9+import qualified Data.IORef as I
10+import qualified Data.ByteString as S
11+import qualified Data.ByteString.Char8 as S8
12+import qualified Data.ByteString.Lazy as L
13+import qualified Data.Text as TS
14+import qualified Data.Text.Encoding as TE
15+import Control.Monad.Trans.Resource (withInternalState, runResourceT)
16+import Network.HTTP2( HTTP2Error (..), ErrorCodeId (..) )
17+
18+import Network.Wai
19+import Network.Wai.Test
20+import Network.Wai.Parse
21+import WaiExtraSpec (toRequest)
22+
23+main :: IO ()
24+main = hspec spec
25+
26+spec :: Spec
27+spec = do
28+ describe "parseContentType" $ do
29+ let go (x, y, z) = it (TS.unpack $ TE.decodeUtf8 x) $ parseContentType x `shouldBe` (y, z)
30+ mapM_ go
31+ [ ("text/plain", "text/plain", [])
32+ , ("text/plain; charset=UTF-8 ", "text/plain", [("charset", "UTF-8")])
33+ , ("text/plain; charset=UTF-8 ; boundary = foo", "text/plain", [("charset", "UTF-8"), ("boundary", "foo")])
34+ , ("text/plain; charset=UTF-8 ; boundary = \"quoted\"", "text/plain", [("charset", "UTF-8"), ("boundary", "quoted")])
35+ ]
36+ it "parseHttpAccept" caseParseHttpAccept
37+ describe "parseRequestBody" $ do
38+ caseParseRequestBody
39+ it "multipart with plus" caseMultipartPlus
40+ it "multipart with multiple attributes" caseMultipartAttrs
41+ it "urlencoded with plus" caseUrlEncPlus
42+ describe "dalvik multipart" $ do
43+ it "non-chunked" $ dalvikHelper True
44+ it "chunked" $ dalvikHelper False
45+
46+caseParseHttpAccept :: Assertion
47+caseParseHttpAccept = do
48+ let input = "text/plain; q=0.5, text/html;charset=utf-8, text/*;q=0.8;ext=blah, text/x-dvi; q=0.8, text/x-c"
49+ expected = ["text/html;charset=utf-8", "text/x-c", "text/x-dvi", "text/*", "text/plain"]
50+ expected @=? parseHttpAccept input
51+
52+parseRequestBody' :: BackEnd file
53+ -> SRequest
54+ -> IO ([(S.ByteString, S.ByteString)], [(S.ByteString, FileInfo file)])
55+parseRequestBody' sink (SRequest req bod) =
56+ case getRequestBodyType req of
57+ Nothing -> return ([], [])
58+ Just rbt -> do
59+ ref <- I.newIORef $ L.toChunks bod
60+ let rb = I.atomicModifyIORef ref $ \chunks ->
61+ case chunks of
62+ [] -> ([], S.empty)
63+ x:y -> (y, x)
64+ sinkRequestBody sink rbt rb
65+
66+caseParseRequestBody :: Spec
67+caseParseRequestBody = do
68+ it "parsing post x-www-form-urlencoded" $ do
69+ let content1 = "foo=bar&baz=bin"
70+ let ctype1 = "application/x-www-form-urlencoded"
71+ result1 <- parseRequestBody' lbsBackEnd $ toRequest ctype1 content1
72+ result1 `shouldBe` ([("foo", "bar"), ("baz", "bin")], [])
73+
74+ let ctype2 = "multipart/form-data; boundary=AaB03x"
75+ let expectedsmap2 =
76+ [ ("title", "A File")
77+ , ("summary", "This is my file\nfile test")
78+ ]
79+ let textPlain = "text/plain; charset=iso-8859-1"
80+ let expectedfile2 =
81+ [("document", FileInfo "b.txt" textPlain "This is a file.\nIt has two lines.")]
82+ let expected2 = (expectedsmap2, expectedfile2)
83+
84+ it "parsing post multipart/form-data" $ do
85+ result2 <- parseRequestBody' lbsBackEnd $ toRequest ctype2 content2
86+ result2 `shouldBe` expected2
87+
88+ it "parsing post multipart/form-data 2" $ do
89+ result2' <- parseRequestBody' lbsBackEnd $ toRequest' ctype2 content2
90+ result2' `shouldBe` expected2
91+
92+
93+ let ctype3 = "multipart/form-data; boundary=----WebKitFormBoundaryB1pWXPZ6lNr8RiLh"
94+ let expectedsmap3 = []
95+ let expectedfile3 = [("yaml", FileInfo "README" "application/octet-stream" "Photo blog using Hack.\n")]
96+ let expected3 = (expectedsmap3, expectedfile3)
97+
98+ let unknownErrorException c (ConnectionError (UnknownErrorCode code) _) = c == code
99+ unknownErrorException _ _ = False
100+
101+
102+ let def = defaultParseRequestBodyOptions
103+ it "parsing actual post multipart/form-data" $ do
104+ result3 <- parseRequestBody' lbsBackEnd $ toRequest ctype3 content3
105+ result3 `shouldBe` expected3
106+
107+ it "parsing actual post multipart/form-data 2" $ do
108+ result3' <- parseRequestBody' lbsBackEnd $ toRequest' ctype3 content3
109+ result3' `shouldBe` expected3
110+
111+ it "parsing with memory limit" $ do
112+ SRequest req4 _bod4 <- toRequest'' ctype3 content3
113+ result4' <- parseRequestBodyEx ( setMaxRequestNumFiles 1 $ setMaxRequestKeyLength 14 def ) lbsBackEnd req4
114+ result4' `shouldBe` expected3
115+
116+ it "exceeding number of files" $ do
117+ SRequest req4 _bod4 <- toRequest'' ctype3 content3
118+ (parseRequestBodyEx ( setMaxRequestNumFiles 0 def ) lbsBackEnd req4) `shouldThrow` anyErrorCall
119+
120+ it "exceeding parameter length" $ do
121+ SRequest req4 _bod4 <- toRequest'' ctype3 content3
122+ (parseRequestBodyEx ( setMaxRequestKeyLength 2 def ) lbsBackEnd req4) `shouldThrow` anyErrorCall
123+
124+ it "exceeding file size" $ do
125+ SRequest req4 _bod4 <- toRequest'' ctype3 content3
126+ (parseRequestBodyEx ( setMaxRequestFileSize 2 def ) lbsBackEnd req4)
127+ `shouldThrow` unknownErrorException 413
128+
129+ it "exceeding total file size" $ do
130+ SRequest req4 _bod4 <- toRequest'' ctype3 content3
131+ (parseRequestBodyEx ( setMaxRequestFilesSize 20 def ) lbsBackEnd req4)
132+ `shouldThrow` unknownErrorException 413
133+ SRequest req5 _bod5 <- toRequest'' ctype3 content5
134+ (parseRequestBodyEx ( setMaxRequestFilesSize 20 def ) lbsBackEnd req5)
135+ `shouldThrow` unknownErrorException 413
136+
137+ it "exceeding max parm value size" $ do
138+ SRequest req4 _bod4 <- toRequest'' ctype2 content2
139+ (parseRequestBodyEx ( setMaxRequestParmsSize 10 def ) lbsBackEnd req4)
140+ `shouldThrow` unknownErrorException 413
141+
142+ it "exceeding max header lines" $ do
143+ SRequest req4 _bod4 <- toRequest'' ctype2 content2
144+ (parseRequestBodyEx ( setMaxHeaderLines 1 def ) lbsBackEnd req4) `shouldThrow` anyErrorCall
145+
146+ it "exceeding header line size" $ do
147+ SRequest req4 _bod4 <- toRequest'' ctype3 content4
148+ (parseRequestBodyEx ( setMaxHeaderLineLength 8190 def ) lbsBackEnd req4)
149+ `shouldThrow` unknownErrorException 431
150+
151+ it "Testing parseRequestBodyEx with application/x-www-form-urlencoded" $ do
152+ let content = "thisisalongparameterkey=andthisbeanevenlongerparametervaluehelloworldhowareyou"
153+ let ctype = "application/x-www-form-urlencoded"
154+ SRequest req _bod <- toRequest'' ctype content
155+ result <- parseRequestBodyEx def lbsBackEnd req
156+ result `shouldBe` ([( "thisisalongparameterkey"
157+ , "andthisbeanevenlongerparametervaluehelloworldhowareyou" )], [])
158+
159+ it "exceeding max parm value size with x-www-form-urlencoded mimetype" $ do
160+ let content = "thisisalongparameterkey=andthisbeanevenlongerparametervaluehelloworldhowareyou"
161+ let ctype = "application/x-www-form-urlencoded"
162+ SRequest req _bod <- toRequest'' ctype content
163+ (parseRequestBodyEx ( setMaxRequestParmsSize 10 def ) lbsBackEnd req) `shouldThrow` anyErrorCall
164+
165+ where
166+ content2 =
167+ "--AaB03x\n"
168+ <> "Content-Disposition: form-data; name=\"document\"; filename=\"b.txt\"\n"
169+ <> "Content-Type: text/plain; charset=iso-8859-1\n\n"
170+ <> "This is a file.\n"
171+ <> "It has two lines.\n"
172+ <> "--AaB03x\n"
173+ <> "Content-Disposition: form-data; name=\"title\"\n"
174+ <> "Content-Type: text/plain; charset=iso-8859-1\n\n"
175+ <> "A File\n"
176+ <> "--AaB03x\n"
177+ <> "Content-Disposition: form-data; name=\"summary\"\n"
178+ <> "Content-Type: text/plain; charset=iso-8859-1\n\n"
179+ <> "This is my file\n"
180+ <> "file test\n"
181+ <> "--AaB03x--"
182+ content3 =
183+ "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\n"
184+ <> "Content-Disposition: form-data; name=\"yaml\"; filename=\"README\"\r\n"
185+ <> "Content-Type: application/octet-stream\r\n\r\n"
186+ <> "Photo blog using Hack.\n\r\n"
187+ <> "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh--\r\n"
188+ content4 =
189+ "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\n"
190+ <> "Content-Disposition: form-data; name=\"alb\"; filename=\"README\"\r\n"
191+ <> "Content-Type: application/octet-stream\r\n\r\n"
192+ <> "Photo blog using Hack.\r\n\r\n"
193+ <> "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\n"
194+ <> "Content-Disposition: form-data; name=\"bla\"; filename=\"riedmi"
195+ <> S8.replicate 8190 'e' <> "\"\r\n"
196+ <> "Content-Type: application/octet-stream\r\n\r\n"
197+ <> "Photo blog using Hack.\r\n\r\n"
198+ <> "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh--\r\n"
199+ content5 =
200+ "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\n"
201+ <> "Content-Disposition: form-data; name=\"yaml\"; filename=\"README\"\r\n"
202+ <> "Content-Type: application/octet-stream\r\n\r\n"
203+ <> "Photo blog using Hack.\n\r\n"
204+ <> "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\n"
205+ <> "Content-Disposition: form-data; name=\"yaml2\"; filename=\"MEADRE\"\r\n"
206+ <> "Content-Type: application/octet-stream\r\n\r\n"
207+ <> "Photo blog using Hack.\n\r\n"
208+ <> "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh--\r\n"
209+
210+caseMultipartPlus :: Assertion
211+caseMultipartPlus = do
212+ result <- parseRequestBody' lbsBackEnd $ toRequest ctype content
213+ result @?= ([("email", "has+plus")], [])
214+ where
215+ content =
216+ "--AaB03x\n" <>
217+ "Content-Disposition: form-data; name=\"email\"\n" <>
218+ "Content-Type: text/plain; charset=iso-8859-1\n\n" <>
219+ "has+plus\n" <>
220+ "--AaB03x--"
221+ ctype = "multipart/form-data; boundary=AaB03x"
222+
223+caseMultipartAttrs :: Assertion
224+caseMultipartAttrs = do
225+ result <- parseRequestBody' lbsBackEnd $ toRequest ctype content
226+ result @?= ([("email", "has+plus")], [])
227+ where
228+ content =
229+ "--AaB03x\n" <>
230+ "Content-Disposition: form-data; name=\"email\"\n" <>
231+ "Content-Type: text/plain; charset=iso-8859-1\n\n" <>
232+ "has+plus\n" <>
233+ "--AaB03x--"
234+ ctype = "multipart/form-data; charset=UTF-8; boundary=AaB03x"
235+
236+caseUrlEncPlus :: Assertion
237+caseUrlEncPlus = do
238+ result <- runResourceT $ withInternalState $ \state ->
239+ parseRequestBody' (tempFileBackEnd state) $ toRequest ctype content
240+ result @?= ([("email", "has+plus")], [])
241+ where
242+ content = "email=has%2Bplus"
243+ ctype = "application/x-www-form-urlencoded"
244+
245+dalvikHelper :: Bool -> Assertion
246+dalvikHelper includeLength = do
247+ let headers' =
248+ [ ("content-type", "multipart/form-data;boundary=*****")
249+ , ("GATEWAY_INTERFACE", "CGI/1.1")
250+ , ("PATH_INFO", "/")
251+ , ("QUERY_STRING", "")
252+ , ("REMOTE_ADDR", "192.168.1.115")
253+ , ("REMOTE_HOST", "ganjizza")
254+ , ("REQUEST_URI", "http://192.168.1.115:3000/")
255+ , ("REQUEST_METHOD", "POST")
256+ , ("HTTP_CONNECTION", "Keep-Alive")
257+ , ("HTTP_COOKIE", "_SESSION=fgUGM5J/k6mGAAW+MMXIJZCJHobw/oEbb6T17KQN0p9yNqiXn/m/ACrsnRjiCEgqtG4fogMUDI+jikoFGcwmPjvuD5d+MDz32iXvDdDJsFdsFMfivuey2H+n6IF6yFGD")
258+ , ("HTTP_USER_AGENT", "Dalvik/1.1.0 (Linux; U; Android 2.1-update1; sdk Build/ECLAIR)")
259+ , ("HTTP_HOST", "192.168.1.115:3000")
260+ , ("HTTP_ACCEPT", "*, */*")
261+ , ("HTTP_VERSION", "HTTP/1.1")
262+ , ("REQUEST_PATH", "/")
263+ ]
264+ headers
265+ | includeLength = ("content-length", "12098") : headers'
266+ | otherwise = headers'
267+ let request' = defaultRequest
268+ { requestHeaders = headers
269+ }
270+ (params, files) <-
271+ case getRequestBodyType request' of
272+ Nothing -> return ([], [])
273+ Just rbt -> withFile "test/requests/dalvik-request" ReadMode $ \h ->
274+ sinkRequestBody lbsBackEnd rbt $ S.hGetSome h 2048
275+ lookup "scannedTime" params @?= Just "1.298590056748E9"
276+ lookup "geoLong" params @?= Just "0"
277+ lookup "geoLat" params @?= Just "0"
278+ length files @?= 1
279+
280+toRequest' :: S8.ByteString -> S8.ByteString -> SRequest
281+toRequest' ctype content = SRequest defaultRequest
282+ { requestHeaders = [("Content-Type", ctype)]
283+ } (L.fromChunks $ map S.singleton $ S.unpack content)
284+
285+toRequest'' :: S8.ByteString -> S8.ByteString -> IO SRequest
286+toRequest'' ctype content = mkRB content >>= \b -> return $ SRequest defaultRequest
287+ { requestHeaders = [("Content-Type", ctype)], requestBody = b
288+ } (L.fromChunks $ map S.singleton $ S.unpack content)
289+
290+mkRB :: S8.ByteString -> IO (IO S8.ByteString)
291+mkRB content = do
292+ r <- I.newIORef content
293+ return $
294+ I.atomicModifyIORef r $ \a -> (S8.empty, a)
wai-extra/test/Network/Wai/RequestSpec.hsnew file+93
@@ -0,0 +1,93 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.RequestSpec
3+ ( main
4+ , spec
5+ ) where
6+
7+import Test.Hspec
8+
9+import Data.ByteString (ByteString)
10+import Network.HTTP.Types (HeaderName)
11+import Network.Wai (Request(..), defaultRequest, RequestBodyLength(..))
12+
13+import Network.Wai.Request
14+import Control.Exception (try)
15+import Control.Monad (forever)
16+
17+main :: IO ()
18+main = hspec spec
19+
20+spec :: Spec
21+spec = do
22+ describe "requestSizeCheck" $ do
23+ it "too large content length should throw RequestSizeException" $ do
24+ let limit = 1024
25+ largeRequest = defaultRequest
26+ { isSecure = False
27+ , requestBodyLength = KnownLength (limit + 1)
28+ , requestBody = return "repeat this chunk"
29+ }
30+ checkedRequest <- requestSizeCheck limit largeRequest
31+ body <- try (requestBody checkedRequest)
32+ case body of
33+ Left (RequestSizeException l) -> l `shouldBe` limit
34+ Right _ -> expectationFailure "request size check failed"
35+
36+ it "too many chunks should throw RequestSizeException" $ do
37+ let limit = 1024
38+ largeRequest = defaultRequest
39+ { isSecure = False
40+ , requestBodyLength = ChunkedBody
41+ , requestBody = return "repeat this chunk"
42+ }
43+ checkedRequest <- requestSizeCheck limit largeRequest
44+ body <- try (forever $ requestBody checkedRequest)
45+ case body of
46+ Left (RequestSizeException l) -> l `shouldBe` limit
47+ Right _ -> expectationFailure "request size check failed"
48+
49+ describe "appearsSecure" $ do
50+ let insecureRequest = defaultRequest
51+ { isSecure = False
52+ , requestHeaders =
53+ [ ("HTTPS", "off")
54+ , ("HTTP_X_FORWARDED_SSL", "off")
55+ , ("HTTP_X_FORWARDED_SCHEME", "http")
56+ , ("HTTP_X_FORWARDED_PROTO", "http,xyz")
57+ ]
58+ }
59+
60+ it "returns False for an insecure request" $
61+ insecureRequest `shouldSatisfy` not . appearsSecure
62+
63+ it "checks if the Request is actually secure" $ do
64+ let req = insecureRequest { isSecure = True }
65+
66+ req `shouldSatisfy` appearsSecure
67+
68+ it "checks for HTTP: on" $ do
69+ let req = addHeader "HTTPS" "on" insecureRequest
70+
71+ req `shouldSatisfy` appearsSecure
72+
73+ it "checks for HTTP_X_FORWARDED_SSL: on" $ do
74+ let req = addHeader "HTTP_X_FORWARDED_SSL" "on" insecureRequest
75+
76+ req `shouldSatisfy` appearsSecure
77+
78+ it "checks for HTTP_X_FORWARDED_SCHEME: https" $ do
79+ let req = addHeader "HTTP_X_FORWARDED_SCHEME" "https" insecureRequest
80+
81+ req `shouldSatisfy` appearsSecure
82+
83+ it "checks for HTTP_X_FORWARDED_PROTO: https,..." $ do
84+ let req = addHeader "HTTP_X_FORWARDED_PROTO" "https,xyz" insecureRequest
85+
86+ req `shouldSatisfy` appearsSecure
87+
88+addHeader :: HeaderName -> ByteString -> Request -> Request
89+addHeader name value req = req
90+ { requestHeaders = (name, value) : otherHeaders }
91+
92+ where
93+ otherHeaders = filter ((/= name) . fst) $ requestHeaders req
wai-extra/test/Network/Wai/TestSpec.hsnew file+227
@@ -0,0 +1,227 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.TestSpec (main, spec) where
3+
4+import Control.Monad (void)
5+
6+import qualified Data.IORef as IORef
7+
8+import qualified Data.Text.Encoding as TE
9+
10+import Data.Time.Calendar (fromGregorian)
11+import Data.Time.Clock (UTCTime(..))
12+
13+import Test.Hspec
14+
15+import Network.Wai
16+import Network.Wai.Test
17+
18+import Network.HTTP.Types (status200)
19+
20+import qualified Data.ByteString.Lazy.Char8 as L8
21+import Data.ByteString.Builder (Builder, toLazyByteString)
22+import Data.ByteString (ByteString)
23+
24+import qualified Web.Cookie as Cookie
25+
26+main :: IO ()
27+main = hspec spec
28+
29+toByteString :: Builder -> ByteString
30+toByteString = L8.toStrict . toLazyByteString
31+
32+spec :: Spec
33+spec = do
34+ describe "setPath" $ do
35+
36+ let req = setPath defaultRequest "/foo/bar/baz?foo=23&bar=42&baz"
37+
38+ it "sets pathInfo" $ do
39+ pathInfo req `shouldBe` ["foo", "bar", "baz"]
40+
41+ it "utf8 path" $
42+ pathInfo (setPath defaultRequest "/foo/%D7%A9%D7%9C%D7%95%D7%9D/bar") `shouldBe`
43+ ["foo", "שלום", "bar"]
44+
45+ it "sets rawPathInfo" $ do
46+ rawPathInfo req `shouldBe` "/foo/bar/baz"
47+
48+ it "sets queryString" $ do
49+ queryString req `shouldBe` [("foo", Just "23"), ("bar", Just "42"), ("baz", Nothing)]
50+
51+ it "sets rawQueryString" $ do
52+ rawQueryString req `shouldBe` "?foo=23&bar=42&baz"
53+
54+ context "when path has no query string" $ do
55+ it "sets rawQueryString to empty string" $ do
56+ rawQueryString (setPath defaultRequest "/foo/bar/baz") `shouldBe` ""
57+
58+ describe "srequest" $ do
59+
60+ let echoApp req respond = do
61+ reqBody <- L8.fromStrict <$> getRequestBodyChunk req
62+ let reqHeaders = requestHeaders req
63+ respond $
64+ responseLBS
65+ status200
66+ reqHeaders
67+ reqBody
68+
69+ it "returns the response body of an echo app" $ do
70+ sresp <- flip runSession echoApp $
71+ srequest $ SRequest defaultRequest "request body"
72+ simpleBody sresp `shouldBe` "request body"
73+
74+ describe "request" $ do
75+
76+ let echoApp req respond = do
77+ reqBody <- L8.fromStrict <$> getRequestBodyChunk req
78+ let reqHeaders = requestHeaders req
79+ respond $
80+ responseLBS
81+ status200
82+ reqHeaders
83+ reqBody
84+
85+ it "returns the status code of an echo app on default request" $ do
86+ sresp <- runSession (request defaultRequest) echoApp
87+ simpleStatus sresp `shouldBe` status200
88+
89+ it "returns the response body of an echo app" $ do
90+ bodyRef <- IORef.newIORef "request body"
91+ let getBodyChunk = IORef.atomicModifyIORef bodyRef $ \leftover -> ("", leftover)
92+ sresp <- flip runSession echoApp $
93+ request $
94+ defaultRequest
95+ { requestBody = getBodyChunk
96+ }
97+ simpleBody sresp `shouldBe` "request body"
98+
99+ it "returns the response headers of an echo app" $ do
100+ sresp <- flip runSession echoApp $
101+ request $
102+ defaultRequest
103+ { requestHeaders = [("foo", "bar")]
104+ }
105+ simpleHeaders sresp `shouldBe` [("foo", "bar")]
106+
107+ let cookieApp req respond =
108+ case pathInfo req of
109+ ["set", name, val] ->
110+ respond $
111+ responseLBS
112+ status200
113+ [( "Set-Cookie"
114+ , toByteString $ Cookie.renderSetCookie $
115+ Cookie.def { Cookie.setCookieName = TE.encodeUtf8 name
116+ , Cookie.setCookieValue = TE.encodeUtf8 val
117+ }
118+ )
119+ ]
120+ "set_cookie_body"
121+ ["delete", name] ->
122+ respond $
123+ responseLBS
124+ status200
125+ [( "Set-Cookie"
126+ , toByteString $ Cookie.renderSetCookie $
127+ Cookie.def { Cookie.setCookieName =
128+ TE.encodeUtf8 name
129+ , Cookie.setCookieExpires =
130+ Just $ UTCTime (fromGregorian 1970 1 1) 0
131+ }
132+ )
133+ ]
134+ "set_cookie_body"
135+ _ ->
136+ respond $
137+ responseLBS
138+ status200
139+ []
140+ ( L8.pack
141+ $ show
142+ $ map snd
143+ $ filter ((=="Cookie") . fst)
144+ $ requestHeaders req
145+ )
146+
147+ it "sends a Cookie header with correct value after receiving a Set-Cookie header" $ do
148+ sresp <- flip runSession cookieApp $ do
149+ void $ request $
150+ setPath defaultRequest "/set/cookie_name/cookie_value"
151+ request $
152+ setPath defaultRequest "/get"
153+ simpleBody sresp `shouldBe` "[\"cookie_name=cookie_value\"]"
154+
155+ it "sends a Cookie header with updated value after receiving a Set-Cookie header update" $ do
156+ sresp <- flip runSession cookieApp $ do
157+ void $ request $
158+ setPath defaultRequest "/set/cookie_name/cookie_value"
159+ void $ request $
160+ setPath defaultRequest "/set/cookie_name/cookie_value2"
161+ request $
162+ setPath defaultRequest "/get"
163+ simpleBody sresp `shouldBe` "[\"cookie_name=cookie_value2\"]"
164+
165+ it "handles multiple cookies" $ do
166+ sresp <- flip runSession cookieApp $ do
167+ void $ request $
168+ setPath defaultRequest "/set/cookie_name/cookie_value"
169+ void $ request $
170+ setPath defaultRequest "/set/cookie_name2/cookie_value2"
171+ request $
172+ setPath defaultRequest "/get"
173+ simpleBody sresp `shouldBe` "[\"cookie_name=cookie_value;cookie_name2=cookie_value2\"]"
174+
175+ it "removes a deleted cookie" $ do
176+ sresp <- flip runSession cookieApp $ do
177+ void $ request $
178+ setPath defaultRequest "/set/cookie_name/cookie_value"
179+ void $ request $
180+ setPath defaultRequest "/set/cookie_name2/cookie_value2"
181+ void $ request $
182+ setPath defaultRequest "/delete/cookie_name2"
183+ request $
184+ setPath defaultRequest "/get"
185+ simpleBody sresp `shouldBe` "[\"cookie_name=cookie_value\"]"
186+
187+ it "sends a cookie set with setClientCookie to server" $ do
188+ sresp <- flip runSession cookieApp $ do
189+ setClientCookie
190+ (Cookie.def { Cookie.setCookieName = "cookie_name"
191+ , Cookie.setCookieValue = "cookie_value"
192+ }
193+ )
194+ request $
195+ setPath defaultRequest "/get"
196+ simpleBody sresp `shouldBe` "[\"cookie_name=cookie_value\"]"
197+
198+ it "sends a cookie updated with setClientCookie to server" $ do
199+ sresp <- flip runSession cookieApp $ do
200+ setClientCookie
201+ (Cookie.def { Cookie.setCookieName = "cookie_name"
202+ , Cookie.setCookieValue = "cookie_value"
203+ }
204+ )
205+ setClientCookie
206+ (Cookie.def { Cookie.setCookieName = "cookie_name"
207+ , Cookie.setCookieValue = "cookie_value2"
208+ }
209+ )
210+ request $
211+ setPath defaultRequest "/get"
212+ simpleBody sresp `shouldBe` "[\"cookie_name=cookie_value2\"]"
213+
214+ it "does not send a cookie deleted with deleteClientCookie to server" $ do
215+ sresp <- flip runSession cookieApp $ do
216+ setClientCookie
217+ (Cookie.def { Cookie.setCookieName = "cookie_name"
218+ , Cookie.setCookieValue = "cookie_value"
219+ }
220+ )
221+ deleteClientCookie "cookie_name"
222+ request $
223+ setPath defaultRequest "/get"
224+ simpleBody sresp `shouldBe` "[]"
225+
226+
227+
wai-extra/test/Spec.hsnew file+1
@@ -0,0 +1 @@
1+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
wai-extra/test/WaiExtraTest.hs → wai-extra/test/WaiExtraSpec.hsrenamed+72−248
@@ -1,24 +1,25 @@
1+{-# LANGUAGE CPP #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2−module WaiExtraTest (specs) where
3+module WaiExtraSpec (spec, toRequest) where
3
4
4import Test.Hspec
5import Test.Hspec
5import Test.HUnit hiding (Test)
6import Test.HUnit hiding (Test)
7+#if MIN_VERSION_base(4,8,0)
8+import Data.Monoid ((<>))
9+#else
6−import Data.Monoid (mappend, mempty)
10+import Data.Monoid (mempty, mappend, (<>))
11+#endif
7
12
8import Network.Wai
13import Network.Wai
9import Network.Wai.Test
14import Network.Wai.Test
10−import Network.Wai.Parse
11import Network.Wai.UrlMap
15import Network.Wai.UrlMap
12import qualified Data.ByteString as S
16import qualified Data.ByteString as S
13import qualified Data.ByteString.Char8 as S8
17import qualified Data.ByteString.Char8 as S8
14−import qualified Data.ByteString.Lazy.Char8 as L8
15import qualified Data.ByteString.Lazy as L
18import qualified Data.ByteString.Lazy as L
16import qualified Data.Text.Lazy as T
19import qualified Data.Text.Lazy as T
17import qualified Data.Text as TS
20import qualified Data.Text as TS
18import qualified Data.Text.Encoding as TE
21import qualified Data.Text.Encoding as TE
19−import Control.Arrow
20import Control.Applicative
22import Control.Applicative
21−import Control.Monad.Trans.Resource (withInternalState, runResourceT)
22
23
23import Network.Wai.Middleware.Jsonp
24import Network.Wai.Middleware.Jsonp
24import Network.Wai.Middleware.Gzip
25import Network.Wai.Middleware.Gzip
@@ -29,38 +30,21 @@ import Network.Wai.Middleware.MethodOverridePost
29import Network.Wai.Middleware.AcceptOverride
30import Network.Wai.Middleware.AcceptOverride
30import Network.Wai.Middleware.RequestLogger
31import Network.Wai.Middleware.RequestLogger
31import Codec.Compression.GZip (decompress)
32import Codec.Compression.GZip (decompress)
33+import Network.Wai.Middleware.StreamFile
32
34
33−import qualified Data.Conduit as C
34−import qualified Data.Conduit.List as CL
35−import Data.Conduit.Binary (sourceHandle)
36import Control.Monad.IO.Class (liftIO)
35import Control.Monad.IO.Class (liftIO)
37import Data.Maybe (fromMaybe)
36import Data.Maybe (fromMaybe)
38−import Network.HTTP.Types (parseSimpleQuery, status200)
37+import Network.HTTP.Types (status200)
39import System.Log.FastLogger
38import System.Log.FastLogger
40−import System.IO (withFile, IOMode (ReadMode))
41
39
42import qualified Data.IORef as I
40import qualified Data.IORef as I
43
41
44−specs :: Spec
42+spec :: Spec
45−specs = do
43+spec = do
46 describe "Network.Wai.UrlMap" $ do
44 describe "Network.Wai.UrlMap" $ do
47 mapM_ (uncurry it) casesUrlMap
45 mapM_ (uncurry it) casesUrlMap
48
46
49− describe "Network.Wai.Parse" $ do
47+ describe "Network.Wai" $ do
50− describe "parseContentType" $ do
51− let go (x, y, z) = it (TS.unpack $ TE.decodeUtf8 x) $ parseContentType x `shouldBe` (y, z)
52− mapM_ go
53− [ ("text/plain", "text/plain", [])
54− , ("text/plain; charset=UTF-8 ", "text/plain", [("charset", "UTF-8")])
55− , ("text/plain; charset=UTF-8 ; boundary = foo", "text/plain", [("charset", "UTF-8"), ("boundary", "foo")])
56− ]
57− it "parseQueryString" caseParseQueryString
58− it "parseQueryString with question mark" caseParseQueryStringQM
59− it "parseHttpAccept" caseParseHttpAccept
60− it "parseRequestBody" caseParseRequestBody
61− it "multipart with plus" caseMultipartPlus
62− it "multipart with multiple attributes" caseMultipartAttrs
63− it "urlencoded with plus" caseUrlEncPlus
64 {-
48 {-
65 , it "findBound" caseFindBound
49 , it "findBound" caseFindBound
66 , it "sinkTillBound" caseSinkTillBound
50 , it "sinkTillBound" caseSinkTillBound
@@ -78,156 +62,9 @@ specs = do
78 it "method override" caseMethodOverride
62 it "method override" caseMethodOverride
79 it "method override post" caseMethodOverridePost
63 it "method override post" caseMethodOverridePost
80 it "accept override" caseAcceptOverride
64 it "accept override" caseAcceptOverride
81− describe "dalvik multipart" $ do
82− it "non-chunked" $ dalvikHelper True
83− it "chunked" $ dalvikHelper False
84 it "debug request body" caseDebugRequestBody
65 it "debug request body" caseDebugRequestBody
85−
66+ it "stream file" caseStreamFile
86−caseParseQueryString :: Assertion
67+ it "stream LBS" caseStreamLBS
87−caseParseQueryString = do
88− let go l r =
89− map (S8.pack *** S8.pack) l @=? parseSimpleQuery (S8.pack r)
90−
91− go [] ""
92− go [("foo", "")] "foo"
93− go [("foo", "bar")] "foo=bar"
94− go [("foo", "bar"), ("baz", "bin")] "foo=bar&baz=bin"
95− go [("%Q", "")] "%Q"
96− go [("%1Q", "")] "%1Q"
97− go [("%1", "")] "%1"
98− go [("/", "")] "%2F"
99− go [("/", "")] "%2f"
100− go [("foo bar", "")] "foo+bar"
101−
102−caseParseQueryStringQM :: Assertion
103−caseParseQueryStringQM = do
104− let go l r =
105− map (S8.pack *** S8.pack) l
106− @=? parseSimpleQuery (S8.pack $ '?' : r)
107−
108− go [] ""
109− go [("foo", "")] "foo"
110− go [("foo", "bar")] "foo=bar"
111− go [("foo", "bar"), ("baz", "bin")] "foo=bar&baz=bin"
112− go [("%Q", "")] "%Q"
113− go [("%1Q", "")] "%1Q"
114− go [("%1", "")] "%1"
115− go [("/", "")] "%2F"
116− go [("/", "")] "%2f"
117− go [("foo bar", "")] "foo+bar"
118−
119−caseParseHttpAccept :: Assertion
120−caseParseHttpAccept = do
121− let input = "text/plain; q=0.5, text/html;charset=utf-8, text/*;q=0.8;ext=blah, text/x-dvi; q=0.8, text/x-c"
122− expected = ["text/html;charset=utf-8", "text/x-c", "text/x-dvi", "text/*", "text/plain"]
123− expected @=? parseHttpAccept input
124−
125−parseRequestBody' :: BackEnd file
126− -> SRequest
127− -> IO ([(S.ByteString, S.ByteString)], [(S.ByteString, FileInfo file)])
128−parseRequestBody' sink (SRequest req bod) =
129− case getRequestBodyType req of
130− Nothing -> return ([], [])
131− Just rbt -> CL.sourceList (L.toChunks bod) C.$$ sinkRequestBody sink rbt
132−
133−caseParseRequestBody :: Assertion
134−caseParseRequestBody =
135− t
136− where
137− content2 = S8.pack $
138− "--AaB03x\n" ++
139− "Content-Disposition: form-data; name=\"document\"; filename=\"b.txt\"\n" ++
140− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
141− "This is a file.\n" ++
142− "It has two lines.\n" ++
143− "--AaB03x\n" ++
144− "Content-Disposition: form-data; name=\"title\"\n" ++
145− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
146− "A File\n" ++
147− "--AaB03x\n" ++
148− "Content-Disposition: form-data; name=\"summary\"\n" ++
149− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
150− "This is my file\n" ++
151− "file test\n" ++
152− "--AaB03x--"
153− content3 = S8.pack "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\nContent-Disposition: form-data; name=\"yaml\"; filename=\"README\"\r\nContent-Type: application/octet-stream\r\n\r\nPhoto blog using Hack.\n\r\n------WebKitFormBoundaryB1pWXPZ6lNr8RiLh--\r\n"
154− t = do
155− let content1 = "foo=bar&baz=bin"
156− let ctype1 = "application/x-www-form-urlencoded"
157− result1 <- parseRequestBody' lbsBackEnd $ toRequest ctype1 content1
158− liftIO $ assertEqual "parsing post x-www-form-urlencoded"
159− (map (S8.pack *** S8.pack) [("foo", "bar"), ("baz", "bin")], [])
160− result1
161−
162− let ctype2 = "multipart/form-data; boundary=AaB03x"
163− result2 <- parseRequestBody' lbsBackEnd $ toRequest ctype2 content2
164− let expectedsmap2 =
165− [ ("title", "A File")
166− , ("summary", "This is my file\nfile test")
167− ]
168− let textPlain = S8.pack $ "text/plain; charset=iso-8859-1"
169− let expectedfile2 =
170− [(S8.pack "document", FileInfo (S8.pack "b.txt") textPlain $ L8.pack
171− "This is a file.\nIt has two lines.")]
172− let expected2 = (map (S8.pack *** S8.pack) expectedsmap2, expectedfile2)
173− liftIO $ assertEqual "parsing post multipart/form-data"
174− expected2
175− result2
176−
177− let ctype3 = "multipart/form-data; boundary=----WebKitFormBoundaryB1pWXPZ6lNr8RiLh"
178− result3 <- parseRequestBody' lbsBackEnd $ toRequest ctype3 content3
179− let expectedsmap3 = []
180− let expectedfile3 = [(S8.pack "yaml", FileInfo (S8.pack "README") (S8.pack "application/octet-stream") $
181− L8.pack "Photo blog using Hack.\n")]
182− let expected3 = (expectedsmap3, expectedfile3)
183− liftIO $ assertEqual "parsing actual post multipart/form-data"
184− expected3
185− result3
186−
187− result2' <- parseRequestBody' lbsBackEnd $ toRequest' ctype2 content2
188− liftIO $ assertEqual "parsing post multipart/form-data 2"
189− expected2
190− result2'
191−
192− result3' <- parseRequestBody' lbsBackEnd $ toRequest' ctype3 content3
193− liftIO $ assertEqual "parsing actual post multipart/form-data 2"
194− expected3
195− result3'
196−
197−caseMultipartPlus :: Assertion
198−caseMultipartPlus = do
199− result <- parseRequestBody' lbsBackEnd $ toRequest ctype content
200− liftIO $ result @?= ([("email", "has+plus")], [])
201− where
202− content = S8.pack $
203− "--AaB03x\n" ++
204− "Content-Disposition: form-data; name=\"email\"\n" ++
205− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
206− "has+plus\n" ++
207− "--AaB03x--"
208− ctype = "multipart/form-data; boundary=AaB03x"
209−
210−caseMultipartAttrs :: Assertion
211−caseMultipartAttrs = do
212− result <- parseRequestBody' lbsBackEnd $ toRequest ctype content
213− liftIO $ result @?= ([("email", "has+plus")], [])
214− where
215− content = S8.pack $
216− "--AaB03x\n" ++
217− "Content-Disposition: form-data; name=\"email\"\n" ++
218− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
219− "has+plus\n" ++
220− "--AaB03x--"
221− ctype = "multipart/form-data; charset=UTF-8; boundary=AaB03x"
222−
223−caseUrlEncPlus :: Assertion
224−caseUrlEncPlus = do
225− result <- runResourceT $ withInternalState $ \state ->
226− parseRequestBody' (tempFileBackEnd state) $ toRequest ctype content
227− liftIO $ result @?= ([("email", "has+plus")], [])
228− where
229− content = S8.pack $ "email=has%2Bplus"
230− ctype = "application/x-www-form-urlencoded"
231
68
232toRequest :: S8.ByteString -> S8.ByteString -> SRequest
69toRequest :: S8.ByteString -> S8.ByteString -> SRequest
233toRequest ctype content = SRequest defaultRequest
70toRequest ctype content = SRequest defaultRequest
@@ -238,28 +75,23 @@ toRequest ctype content = SRequest defaultRequest
238 , queryString = []
75 , queryString = []
239 } (L.fromChunks [content])
76 } (L.fromChunks [content])
240
77
241−toRequest' :: S8.ByteString -> S8.ByteString -> SRequest
242−toRequest' ctype content = SRequest defaultRequest
243− { requestHeaders = [("Content-Type", ctype)]
244− } (L.fromChunks $ map S.singleton $ S.unpack content)
245−
246{-
78{-
247caseFindBound :: Assertion
79caseFindBound :: Assertion
248caseFindBound = do
80caseFindBound = do
249− findBound (S8.pack "def") (S8.pack "abcdefghi") @?=
81+ findBound "def" "abcdefghi" @?=
250− FoundBound (S8.pack "abc") (S8.pack "ghi")
82+ FoundBound "abc" "ghi"
251− findBound (S8.pack "def") (S8.pack "ABC") @?= NoBound
83+ findBound "def" "ABC" @?= NoBound
252− findBound (S8.pack "def") (S8.pack "abcd") @?= PartialBound
84+ findBound "def" "abcd" @?= PartialBound
253− findBound (S8.pack "def") (S8.pack "abcdE") @?= NoBound
85+ findBound "def" "abcdE" @?= NoBound
254− findBound (S8.pack "def") (S8.pack "abcdEdef") @?=
86+ findBound "def" "abcdEdef" @?=
255− FoundBound (S8.pack "abcdE") (S8.pack "")
87+ FoundBound "abcdE" ""
256
88
257caseSinkTillBound :: Assertion
89caseSinkTillBound :: Assertion
258caseSinkTillBound = do
90caseSinkTillBound = do
259 let iter () _ = return ()
91 let iter () _ = return ()
260− let src = S8.pack "this is some text"
92+ let src = "this is some text"
261− bound1 = S8.pack "some"
93+ bound1 = "some"
262− bound2 = S8.pack "some!"
94+ bound2 = "some!"
263 let enum = enumList 1 [src]
95 let enum = enumList 1 [src]
264 let helper _ _ = return ()
96 let helper _ _ = return ()
265 (_, res1) <- run_ $ enum $$ sinkTillBound bound1 helper ()
97 (_, res1) <- run_ $ enum $$ sinkTillBound bound1 helper ()
@@ -295,7 +127,7 @@ caseTakeLine = do
295-}
127-}
296
128
297jsonpApp :: Application
129jsonpApp :: Application
298−jsonpApp = jsonp $ const $ return $ responseLBS
130+jsonpApp = jsonp $ \_ f -> f $ responseLBS
299 status200
131 status200
300 [("Content-Type", "application/json")]
132 [("Content-Type", "application/json")]
301 "{\"foo\":\"bar\"}"
133 "{\"foo\":\"bar\"}"
@@ -324,7 +156,7 @@ caseJsonp = flip runSession jsonpApp $ do
324 assertBody "{\"foo\":\"bar\"}" sres3
156 assertBody "{\"foo\":\"bar\"}" sres3
325
157
326gzipApp :: Application
158gzipApp :: Application
327−gzipApp = gzip def $ const $ return $ responseLBS status200
159+gzipApp = gzip def $ \_ f -> f $ responseLBS status200
328 [("Content-Type", "text/plain")]
160 [("Content-Type", "text/plain")]
329 "test"
161 "test"
330
162
@@ -332,7 +164,7 @@ gzipApp = gzip def $ const $ return $ responseLBS status200
332-- that the compression is skipped based on the presence of
164-- that the compression is skipped based on the presence of
333-- the Content-Encoding header.
165-- the Content-Encoding header.
334gzipPrecompressedApp :: Application
166gzipPrecompressedApp :: Application
335−gzipPrecompressedApp = gzip def $ const $ return $ responseLBS status200
167+gzipPrecompressedApp = gzip def $ \_ f -> f $ responseLBS status200
336 [("Content-Type", "text/plain"), ("Content-Encoding", "gzip")]
168 [("Content-Type", "text/plain"), ("Content-Encoding", "gzip")]
337 "test"
169 "test"
338
170
@@ -380,8 +212,8 @@ caseGzipBypassPre = flip runSession gzipPrecompressedApp $ do
380 assertBody "test" sres1 -- the body is not actually compressed
212 assertBody "test" sres1 -- the body is not actually compressed
381
213
382vhostApp1, vhostApp2, vhostApp :: Application
214vhostApp1, vhostApp2, vhostApp :: Application
383−vhostApp1 = const $ return $ responseLBS status200 [] "app1"
215+vhostApp1 _ f = f $ responseLBS status200 [] "app1"
384−vhostApp2 = const $ return $ responseLBS status200 [] "app2"
216+vhostApp2 _ f = f $ responseLBS status200 [] "app2"
385vhostApp = vhost
217vhostApp = vhost
386 [ ((== Just "foo.com") . lookup "host" . requestHeaders, vhostApp1)
218 [ ((== Just "foo.com") . lookup "host" . requestHeaders, vhostApp1)
387 ]
219 ]
@@ -400,7 +232,7 @@ caseVhost = flip runSession vhostApp $ do
400 assertBody "app2" sres2
232 assertBody "app2" sres2
401
233
402autoheadApp :: Application
234autoheadApp :: Application
403−autoheadApp = autohead $ const $ return $ responseLBS status200
235+autoheadApp = autohead $ \_ f -> f $ responseLBS status200
404 [("Foo", "Bar")] "body"
236 [("Foo", "Bar")] "body"
405
237
406caseAutohead :: Assertion
238caseAutohead :: Assertion
@@ -418,7 +250,7 @@ caseAutohead = flip runSession autoheadApp $ do
418 assertBody "" sres2
250 assertBody "" sres2
419
251
420moApp :: Application
252moApp :: Application
421−moApp = methodOverride $ \req -> return $ responseLBS status200
253+moApp = methodOverride $ \req f -> f $ responseLBS status200
422 [("Method", requestMethod req)] ""
254 [("Method", requestMethod req)] ""
423
255
424caseMethodOverride :: Assertion
256caseMethodOverride :: Assertion
@@ -442,7 +274,7 @@ caseMethodOverride = flip runSession moApp $ do
442 assertHeader "Method" "PUT" sres3
274 assertHeader "Method" "PUT" sres3
443
275
444mopApp :: Application
276mopApp :: Application
445−mopApp = methodOverridePost $ \req -> return $ responseLBS status200 [("Method", requestMethod req)] ""
277+mopApp = methodOverridePost $ \req f -> f $ responseLBS status200 [("Method", requestMethod req)] ""
446
278
447caseMethodOverridePost :: Assertion
279caseMethodOverridePost :: Assertion
448caseMethodOverridePost = flip runSession mopApp $ do
280caseMethodOverridePost = flip runSession mopApp $ do
@@ -468,7 +300,7 @@ caseMethodOverridePost = flip runSession mopApp $ do
468 assertHeader "Method" "POST" sres4
300 assertHeader "Method" "POST" sres4
469
301
470aoApp :: Application
302aoApp :: Application
471−aoApp = acceptOverride $ \req -> return $ responseLBS status200
303+aoApp = acceptOverride $ \req f -> f $ responseLBS status200
472 [("Accept", fromMaybe "" $ lookup "Accept" $ requestHeaders req)] ""
304 [("Accept", fromMaybe "" $ lookup "Accept" $ requestHeaders req)] ""
473
305
474caseAcceptOverride :: Assertion
306caseAcceptOverride :: Assertion
@@ -491,41 +323,6 @@ caseAcceptOverride = flip runSession aoApp $ do
491 }
323 }
492 assertHeader "Accept" "baz" sres3
324 assertHeader "Accept" "baz" sres3
493
325
494−dalvikHelper :: Bool -> Assertion
495−dalvikHelper includeLength = do
496− let headers' =
497− [ ("content-type", "multipart/form-data;boundary=*****")
498− , ("GATEWAY_INTERFACE", "CGI/1.1")
499− , ("PATH_INFO", "/")
500− , ("QUERY_STRING", "")
501− , ("REMOTE_ADDR", "192.168.1.115")
502− , ("REMOTE_HOST", "ganjizza")
503− , ("REQUEST_URI", "http://192.168.1.115:3000/")
504− , ("REQUEST_METHOD", "POST")
505− , ("HTTP_CONNECTION", "Keep-Alive")
506− , ("HTTP_COOKIE", "_SESSION=fgUGM5J/k6mGAAW+MMXIJZCJHobw/oEbb6T17KQN0p9yNqiXn/m/ACrsnRjiCEgqtG4fogMUDI+jikoFGcwmPjvuD5d+MDz32iXvDdDJsFdsFMfivuey2H+n6IF6yFGD")
507− , ("HTTP_USER_AGENT", "Dalvik/1.1.0 (Linux; U; Android 2.1-update1; sdk Build/ECLAIR)")
508− , ("HTTP_HOST", "192.168.1.115:3000")
509− , ("HTTP_ACCEPT", "*, */*")
510− , ("HTTP_VERSION", "HTTP/1.1")
511− , ("REQUEST_PATH", "/")
512− ]
513− headers
514− | includeLength = ("content-length", "12098") : headers'
515− | otherwise = headers'
516− let request' = defaultRequest
517− { requestHeaders = headers
518− }
519− (params, files) <-
520− case getRequestBodyType request' of
521− Nothing -> return ([], [])
522− Just rbt -> withFile "test/requests/dalvik-request" ReadMode $ \h ->
523− sourceHandle h C.$$ sinkRequestBody lbsBackEnd rbt
524− lookup "scannedTime" params @?= Just "1.298590056748E9"
525− lookup "geoLong" params @?= Just "0"
526− lookup "geoLat" params @?= Just "0"
527− length files @?= 1
528−
529caseDebugRequestBody :: Assertion
326caseDebugRequestBody :: Assertion
530caseDebugRequestBody = do
327caseDebugRequestBody = do
531 flip runSession (debugApp postOutput) $ do
328 flip runSession (debugApp postOutput) $ do
@@ -544,22 +341,25 @@ caseDebugRequestBody = do
544 }
341 }
545 where
342 where
546 params = [("foo", "bar"), ("baz", "bin")]
343 params = [("foo", "bar"), ("baz", "bin")]
344+ -- the time cannot be known, so match around it
547− -- FIXME change back once we include post parameter output in logging postOutput = T.pack $ "POST \nAccept: \nPOST " ++ (show params)
345+ postOutput = (T.pack $ "POST /\n Params: " ++ (show params), "s\n")
548− postOutput = T.pack $ "POST / :: \nStatus: 200 OK. /\n"
549− getOutput params' = T.pack $ "GET /location :: \nGET " ++ show params' ++ "\nStatus: 200 OK. /location\n"
346+ getOutput params' = ("GET /location\n Params: " <> T.pack (show params') <> "\n Accept: \n Status: 200 OK 0", "s\n")
550
347
551− debugApp output' req = do
348+ debugApp (beginning, ending) req send = do
552− iactual <- liftIO $ I.newIORef mempty
349+ iactual <- I.newIORef mempty
553− middleware <- liftIO $ mkRequestLogger def
350+ middleware <- mkRequestLogger def
554 { destination = Callback $ \strs -> I.modifyIORef iactual $ (`mappend` strs)
351 { destination = Callback $ \strs -> I.modifyIORef iactual $ (`mappend` strs)
555 , outputFormat = Detailed False
352 , outputFormat = Detailed False
556 }
353 }
557− res <- middleware (\_req -> return $ responseLBS status200 [ ] "") req
354+ res <- middleware (\_req f -> f $ responseLBS status200 [ ] "") req send
558− actual <- liftIO $ I.readIORef iactual
355+ actual <- logToBs <$> I.readIORef iactual
559− liftIO $ assertEqual "debug" output $ logToBs actual
356+ actual `shouldSatisfy` S.isPrefixOf begin
357+ actual `shouldSatisfy` S.isSuffixOf end
358+
560 return res
359 return res
561 where
360 where
361+ begin = TE.encodeUtf8 $ T.toStrict beginning
562− output = TE.encodeUtf8 $ T.toStrict output'
362+ end = TE.encodeUtf8 $ T.toStrict ending
563
363
564 logToBs = fromLogStr
364 logToBs = fromLogStr
565
365
@@ -578,8 +378,8 @@ urlMapTestApp = mapUrls $
578
378
579 where
379 where
580 trivialApp :: S.ByteString -> Application
380 trivialApp :: S.ByteString -> Application
581− trivialApp name req =
381+ trivialApp name req f =
582− return $
382+ f $
583 responseLBS
383 responseLBS
584 status200
384 status200
585 [ ("content-type", "text/plain")
385 [ ("content-type", "text/plain")
@@ -625,3 +425,27 @@ casesUrlMap = [pair1, pair2, pair3, pair4]
625 pair4 = makePair "should 404 if none match" $ do
425 pair4 = makePair "should 404 if none match" $ do
626 res4 <- get "/api/v3"
426 res4 <- get "/api/v3"
627 assertStatus 404 res4
427 assertStatus 404 res4
428+
429+testFile :: FilePath
430+testFile = "test/WaiExtraSpec.hs"
431+
432+streamFileApp :: Application
433+streamFileApp = streamFile $ \_ f -> f $ responseFile status200 [] testFile Nothing
434+
435+caseStreamFile :: Assertion
436+caseStreamFile = flip runSession streamFileApp $ do
437+ sres <- request defaultRequest
438+ assertStatus 200 sres
439+ assertBodyContains "caseStreamFile" sres
440+ assertNoHeader "Transfer-Encoding" sres
441+
442+streamLBSApp :: Application
443+streamLBSApp = streamFile $ \_ f -> f $ responseLBS status200
444+ [("Content-Type", "text/plain")]
445+ "test"
446+
447+caseStreamLBS :: Assertion
448+caseStreamLBS = flip runSession streamLBSApp $ do
449+ sres <- request defaultRequest
450+ assertStatus 200 sres
451+ assertBody "test" sres
wai-extra/tests.hsdeleted−5
@@ -1,5 +0,0 @@
1−import Test.Hspec
2−import qualified WaiExtraTest
3−
4−main :: IO ()
5−main = hspec WaiExtraTest.specs
wai-extra/wai-extra.cabal+148−40
@@ -1,7 +1,69 @@
1Name: wai-extra
1Name: wai-extra
2−Version: 2.1.1.1
2+Version: 3.0.29.1
3Synopsis: Provides some basic WAI handlers and middleware.
3Synopsis: Provides some basic WAI handlers and middleware.
4−Description: The goal here is to provide common features without many dependencies.
4+description:
5+ Provides basic WAI handler and middleware functionality:
6+ .
7+ * WAI Testing Framework
8+ .
9+ Hspec testing facilities and helpers for WAI.
10+ .
11+ * Event Source/Event Stream
12+ .
13+ Send server events to the client. Compatible with the JavaScript
14+ EventSource API.
15+ .
16+ * Accept Override
17+ .
18+ Override the Accept header in a request. Special handling for the
19+ _accept query parameter (which is used throughout WAI override the
20+ Accept header).
21+ .
22+ * Add Headers
23+ .
24+ WAI Middleware for adding arbitrary headers to an HTTP request.
25+ .
26+ * Clean Path
27+ .
28+ Clean a request path to a canonical form.
29+ .
30+ * GZip Compression
31+ .
32+ Negotiate HTTP payload gzip compression.
33+ .
34+ * HTTP Basic Authentication
35+ .
36+ WAI Basic Authentication Middleware which uses Authorization header.
37+ .
38+ * JSONP
39+ .
40+ \"JSON with Padding\" middleware. Automatic wrapping of JSON
41+ responses to convert into JSONP.
42+ .
43+ * Method Override / Post
44+ .
45+ Allows overriding of the HTTP request method via the _method query string
46+ parameter.
47+ .
48+ * Request Logging
49+ .
50+ Request logging middleware for development and production environments
51+ .
52+ * Request Rewrite
53+ .
54+ Rewrite request path info based on a custom conversion rules.
55+ .
56+ * Stream Files
57+ .
58+ Convert ResponseFile type responses into ResponseStream type.
59+ .
60+ * Virtual Host
61+ .
62+ Redirect incoming requests to a new host based on custom rules.
63+ .
64+ .
65+ API docs and the README are available at <http://www.stackage.org/package/wai-extra>.
66+
5License: MIT
67License: MIT
6License-file: LICENSE
68License-file: LICENSE
7Author: Michael Snoyman
69Author: Michael Snoyman
@@ -9,95 +71,141 @@ Maintainer: michael@snoyman.com
9Homepage: http://github.com/yesodweb/wai
71Homepage: http://github.com/yesodweb/wai
10Category: Web
72Category: Web
11Build-Type: Simple
73Build-Type: Simple
12−Cabal-Version: >=1.8
74+Cabal-Version: >=1.10
13Stability: Stable
75Stability: Stable
14extra-source-files:
76extra-source-files:
15− tests.hs
16 test/requests/dalvik-request
77 test/requests/dalvik-request
17 test/json
78 test/json
18 test/test.html
79 test/test.html
19 test/sample.hs
80 test/sample.hs
20− test/WaiExtraTest.hs
81+ ChangeLog.md
82+ README.md
83+
84+flag build-example
85+ description: Build example executable.
86+ manual: True
87+ default: False
21
88
22Library
89Library
23− Build-Depends: base >= 4 && < 5
90+ Build-Depends: base >= 4.8 && < 5
24− , bytestring >= 0.9.1.4
91+ , bytestring >= 0.10.4
25− , wai >= 2.1 && < 2.2
92+ , wai >= 3.0.3.0 && < 3.3
26 , old-locale >= 1.0.0.2 && < 1.1
93 , old-locale >= 1.0.0.2 && < 1.1
27 , time >= 1.1.4
94 , time >= 1.1.4
28− , network >= 2.2.1.5
95+ , network >= 2.6.1.0
29 , directory >= 1.0.1
96 , directory >= 1.0.1
30− , transformers >= 0.2.2 && < 0.4
97+ , transformers >= 0.2.2
31− , blaze-builder >= 0.2.1.4 && < 0.4
32 , http-types >= 0.7
98 , http-types >= 0.7
33 , text >= 0.7
99 , text >= 0.7
34 , case-insensitive >= 0.2
100 , case-insensitive >= 0.2
35− , data-default
101+ , data-default-class
36− , fast-logger >= 2.1 && < 2.2
102+ , fast-logger >= 2.4.5
37− , wai-logger >= 2.0 && < 2.2
103+ , wai-logger >= 2.3.2
38− , conduit >= 1.0 && < 1.2
39− , conduit-extra >= 0.1 && < 1.2
40− , zlib-conduit >= 0.5 && < 1.2
41− , blaze-builder-conduit >= 0.5 && < 1.2
42 , ansi-terminal
104 , ansi-terminal
43− , resourcet >= 0.4.6 && < 1.2
105+ , resourcet >= 0.4.6 && < 1.3
44 , void >= 0.5
106 , void >= 0.5
45− , stringsearch >= 0.3 && < 0.4
46 , containers
107 , containers
47 , base64-bytestring
108 , base64-bytestring
48 , word8
109 , word8
49− , lifted-base >= 0.1.2
110+ , deepseq
111+ , streaming-commons >= 0.2
112+ , unix-compat
113+ , cookie
114+ , vault
115+ , zlib
116+ , aeson
117+ , iproute
118+ , http2
50
119
51 if os(windows)
120 if os(windows)
52 cpp-options: -DWINDOWS
121 cpp-options: -DWINDOWS
53 else
122 else
54 build-depends: unix
123 build-depends: unix
55
124
125+ default-extensions: OverloadedStrings
126+
56 Exposed-modules: Network.Wai.Handler.CGI
127 Exposed-modules: Network.Wai.Handler.CGI
57 Network.Wai.Handler.SCGI
128 Network.Wai.Handler.SCGI
129+ Network.Wai.Header
58 Network.Wai.Middleware.AcceptOverride
130 Network.Wai.Middleware.AcceptOverride
131+ Network.Wai.Middleware.AddHeaders
132+ Network.Wai.Middleware.Approot
59 Network.Wai.Middleware.Autohead
133 Network.Wai.Middleware.Autohead
60 Network.Wai.Middleware.CleanPath
134 Network.Wai.Middleware.CleanPath
135+ Network.Wai.Middleware.Local
61 Network.Wai.Middleware.RequestLogger
136 Network.Wai.Middleware.RequestLogger
137+ Network.Wai.Middleware.RequestLogger.JSON
62 Network.Wai.Middleware.Gzip
138 Network.Wai.Middleware.Gzip
63 Network.Wai.Middleware.Jsonp
139 Network.Wai.Middleware.Jsonp
64 Network.Wai.Middleware.MethodOverride
140 Network.Wai.Middleware.MethodOverride
65 Network.Wai.Middleware.MethodOverridePost
141 Network.Wai.Middleware.MethodOverridePost
66 Network.Wai.Middleware.Rewrite
142 Network.Wai.Middleware.Rewrite
143+ Network.Wai.Middleware.StripHeaders
67 Network.Wai.Middleware.Vhost
144 Network.Wai.Middleware.Vhost
68 Network.Wai.Middleware.HttpAuth
145 Network.Wai.Middleware.HttpAuth
146+ Network.Wai.Middleware.StreamFile
147+ Network.Wai.Middleware.ForceDomain
148+ Network.Wai.Middleware.ForceSSL
149+ Network.Wai.Middleware.Routed
150+ Network.Wai.Middleware.Timeout
69 Network.Wai.Parse
151 Network.Wai.Parse
152+ Network.Wai.Request
70 Network.Wai.UrlMap
153 Network.Wai.UrlMap
154+ Network.Wai.Test
155+ Network.Wai.Test.Internal
156+ Network.Wai.EventSource
157+ Network.Wai.EventSource.EventStream
71 other-modules: Network.Wai.Middleware.RequestLogger.Internal
158 other-modules: Network.Wai.Middleware.RequestLogger.Internal
159+ default-language: Haskell2010
72 ghc-options: -Wall
160 ghc-options: -Wall
73
161
162+executable example
163+ hs-source-dirs: example
164+ main-is: Main.hs
165+ ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
166+ if flag(build-example)
167+ build-depends: base
168+ , wai-extra
169+ , warp
170+ , wai
171+ , time
172+ , http-types
173+ , bytestring
174+ else
175+ buildable: False
176+ default-language: Haskell2010
74
177
75−test-suite tests
178+test-suite spec
76− hs-source-dirs: test
77− main-is: ../tests.hs
78− type: exitcode-stdio-1.0
179+ type: exitcode-stdio-1.0
79−
180+ hs-source-dirs: test
181+ main-is: Spec.hs
182+ other-modules: Network.Wai.TestSpec
183+ Network.Wai.ParseSpec
184+ Network.Wai.RequestSpec
185+ Network.Wai.Middleware.ApprootSpec
186+ Network.Wai.Middleware.ForceSSLSpec
187+ Network.Wai.Middleware.RoutedSpec
188+ Network.Wai.Middleware.StripHeadersSpec
189+ Network.Wai.Middleware.TimeoutSpec
190+ WaiExtraSpec
80 build-depends: base >= 4 && < 5
191 build-depends: base >= 4 && < 5
81 , wai-extra
192 , wai-extra
82− , wai-test >= 1.3
83− , hspec >= 1.3
84− , HUnit
85−
86 , wai
193 , wai
87− , http-types
194+ , hspec >= 1.3
88 , transformers
195 , transformers
196+ , fast-logger
197+ , http-types
89 , zlib
198 , zlib
90 , text
199 , text
91− , bytestring
92− , directory
93− , zlib-bindings
94− , blaze-builder >= 0.2.1.4 && < 0.4
95− , data-default
96− , conduit
97− , fast-logger >= 2.1
98 , resourcet
200 , resourcet
99− , conduit-extra
201+ , bytestring
202+ , HUnit
203+ , cookie
204+ , time
205+ , case-insensitive
206+ , http2
100− ghc-options: -Wall
207+ ghc-options: -Wall
208+ default-language: Haskell2010
101
209
102source-repository head
210source-repository head
103 type: git
211 type: git
wai-frontend-monadcgi/ChangeLog.mdnew file+7
@@ -0,0 +1,7 @@
1+## 3.0.0.3
2+
3+* wai-frontend-monadcgi does not drop '?' in query string [#590](https://github.com/yesodweb/wai/issues/590)
4+
5+## 3.0.0.2
6+
7+* Support for wai 3.2
wai-frontend-monadcgi/Network/Wai/Frontend/MonadCGI.hs+7−6
@@ -10,8 +10,6 @@ import Network.HTTP.Types (Status (..))
10import Control.Monad.IO.Class (liftIO)
10import Control.Monad.IO.Class (liftIO)
11import Data.CaseInsensitive (original)
11import Data.CaseInsensitive (original)
12
12
13−import Data.Conduit.Lazy (lazyConsume)
14−
15import qualified Data.Map as Map
13import qualified Data.Map as Map
16import qualified Data.ByteString.Lazy as BS
14import qualified Data.ByteString.Lazy as BS
17import qualified Data.ByteString.Char8 as S8
15import qualified Data.ByteString.Char8 as S8
@@ -32,8 +30,8 @@ cgiToAppGeneric :: Monad m
32 => (m (Headers, CGIResult) -> IO (Headers, CGIResult))
30 => (m (Headers, CGIResult) -> IO (Headers, CGIResult))
33 -> CGIT m CGIResult
31 -> CGIT m CGIResult
34 -> Application
32 -> Application
35−cgiToAppGeneric toIO cgi env = do
33+cgiToAppGeneric toIO cgi env sendResponse = do
36− input <- fmap BS.fromChunks $ lazyConsume $ requestBody env
34+ input <- lazyRequestBody env
37 let vars = map (first fixVarName . go) (requestHeaders env)
35 let vars = map (first fixVarName . go) (requestHeaders env)
38 ++ getCgiVars env
36 ++ getCgiVars env
39 (inputs, body') = decodeInput vars input
37 (inputs, body') = decodeInput vars input
@@ -51,7 +49,7 @@ cgiToAppGeneric toIO cgi env = do
51 let status' = case lookup (fromString "Status") headers' of
49 let status' = case lookup (fromString "Status") headers' of
52 Nothing -> 200
50 Nothing -> 200
53 Just s -> safeRead 200 $ S8.unpack s
51 Just s -> safeRead 200 $ S8.unpack s
54− return $ responseLBS (Status status' S8.empty) headers' output
52+ sendResponse $ responseLBS (Status status' S8.empty) headers' output
55 where
53 where
56 go (x, y) = (S8.unpack $ original x, S8.unpack y)
54 go (x, y) = (S8.unpack $ original x, S8.unpack y)
57
55
@@ -66,5 +64,8 @@ getCgiVars :: Request -> [(String, String)]
66getCgiVars e =
64getCgiVars e =
67 [ ("PATH_INFO", S8.unpack $ rawPathInfo e)
65 [ ("PATH_INFO", S8.unpack $ rawPathInfo e)
68 , ("REQUEST_METHOD", show $ requestMethod e)
66 , ("REQUEST_METHOD", show $ requestMethod e)
67+ , ("QUERY_STRING",
69− , ("QUERY_STRING", S8.unpack $ rawQueryString e)
68+ case S8.unpack $ rawQueryString e of
69+ '?':rest -> rest
70+ x -> x)
70 ]
71 ]
wai-frontend-monadcgi/READMEdeleted−1
@@ -1 +0,0 @@
1−Run CGI apps on WAI handlers.
wai-frontend-monadcgi/README.mdnew file+5
@@ -0,0 +1,5 @@
1+## wai-frontend-monadcgi
2+
3+Allows programs written against MonadCGI to run with any WAI handler. This is
4+most useful for running your existing CGI apps on Warp. For true "yo
5+dawg"-ness, try running this through the WAI CGI backend.
wai-frontend-monadcgi/wai-frontend-monadcgi.cabal+4−5
@@ -1,27 +1,26 @@
1name: wai-frontend-monadcgi
1name: wai-frontend-monadcgi
2−version: 2.0.0.2
2+version: 3.0.0.3
3license: MIT
3license: MIT
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
6maintainer: Michael Snoyman <michael@snoyman.com>
6maintainer: Michael Snoyman <michael@snoyman.com>
7synopsis: Run CGI apps on WAI.
7synopsis: Run CGI apps on WAI.
8−description: Allows programs written against MonadCGI to run with any WAI handler. This is most useful for running your existing CGI apps on Warp. For true "yo dawg"-ness, try running this through the WAI CGI backend.
8+description: API docs and the README are available at <http://www.stackage.org/package/wai-frontend-monadcgi>.
9category: Web
9category: Web
10stability: stable
10stability: stable
11cabal-version: >= 1.6
11cabal-version: >= 1.6
12build-type: Simple
12build-type: Simple
13+extra-source-files: README.md ChangeLog.md
13
14
14library
15library
15 build-depends: base >= 4 && < 5
16 build-depends: base >= 4 && < 5
16 , bytestring
17 , bytestring
17 , containers >= 0.2
18 , containers >= 0.2
18 , cgi
19 , cgi
19− , conduit >= 0.5 && < 1.2
20− , conduit-extra
21 , http-types
20 , http-types
22 , transformers
21 , transformers
23 , case-insensitive
22 , case-insensitive
24− , wai >= 2.0 && < 2.2
23+ , wai >= 3.0 && < 3.3
25 exposed-modules: Network.Wai.Frontend.MonadCGI
24 exposed-modules: Network.Wai.Frontend.MonadCGI
26 ghc-options: -Wall
25 ghc-options: -Wall
27
26
wai-handler-fastcgi/.gitignoredeleted−4
@@ -1,4 +0,0 @@
1−/dist/
2−*.swp
3−*.hi
4−*.o
wai-handler-fastcgi/Network/Wai/Handler/FastCGI.hscdeleted−192
@@ -1,192 +0,0 @@
1−{-# LANGUAGE ForeignFunctionInterface #-}
2−{-# LANGUAGE EmptyDataDecls #-}
3−{-# LANGUAGE CPP #-}
4−-----------------------------------------------------------------------------
5−-- |
6−-- Module : Network.Wai.Handler.FastCGI
7−-- Copyright : (c) Bjorn Bringert 2004-2005, (c) Lemmih 2006, (c) Michael Snoyman 2010
8−-- License : BSD-style (see the file libraries/network/LICENSE)
9−--
10−-- Maintainer : michael@snoyman.com
11−-- Stability : experimental
12−-- Portability : non-portable (uses FFI)
13−--
14−-- Interface for FastCGI <http://fastcgi.com/>, using the fcgiapp API.
15−-- Totally ripped off by Michael Snoyman to work with Hack, then WAI.
16−--
17−-----------------------------------------------------------------------------
18−module Network.Wai.Handler.FastCGI
19− ( run
20− , runSendfile
21− , runFork
22− ) where
23−
24−import Control.Monad ( liftM, forever )
25−import Data.Word (Word8)
26−import Foreign ( Ptr, castPtr, nullPtr, peekArray0
27− , throwIfNeg_, mallocBytes, free )
28−#ifdef GHC_7_4
29−import Foreign.C (CInt(..), CString, CStringLen)
30−#else
31−import Foreign.C (CInt, CString, CStringLen)
32−#endif
33−import Control.Exception (finally)
34−import Foreign.Storable ( Storable (..) )
35−
36−import qualified Network.Wai as W
37−import qualified Network.Wai.Handler.CGI as CGI
38−
39−import qualified Data.ByteString as S
40−import qualified Data.ByteString.Char8 as S8
41−import qualified Data.ByteString as BS
42−import qualified Data.ByteString.Internal as BSB
43−import qualified Data.ByteString.Unsafe as BSB
44−
45−import Control.Arrow ((***))
46−
47−#include <fcgiapp.h>
48−
49−------------------------------------------------------------------------
50−
51−data FCGX_Stream
52−type StreamPtr = Ptr FCGX_Stream
53−type Environ = Ptr CString
54−
55−------------------------------------------------------------------------
56−
57−foreign import ccall unsafe "fcgiapp.h FCGX_GetStr" fcgx_getStr
58− :: CString -> CInt -> StreamPtr -> IO CInt
59−
60−foreign import ccall unsafe "fcgiapp.h FCGX_PutStr" fcgx_putStr
61− :: CString -> CInt -> StreamPtr -> IO CInt
62−
63−
64−------------------------------------------------------------------------
65−
66−foreign import ccall unsafe "fcgiapp.h FCGX_Init" fcgx_init
67− :: IO CInt
68−
69−runFork :: Maybe S.ByteString -> (IO () -> IO a) -> Int -> W.Application -> IO ()
70−runFork sf fork threads app = do
71− testReturn "FCGX_Init" $ fcgx_init
72− let oneThread = forever $ oneRequest app sf
73− mapM_ fork $ replicate (threads - 1) oneThread
74− oneThread
75−
76−-- | Handle FastCGI requests in an infinite loop.
77−run :: W.Application -> IO ()
78−run = runFork Nothing id 1
79−
80−-- | Handle FastCGI requests in an infinite loop. For a server which supports
81−-- the X-Sendfile header.
82−runSendfile :: S.ByteString -> W.Application -> IO ()
83−runSendfile sf = runFork (Just sf) id 1
84−
85−oneRequest :: W.Application
86− -> Maybe S.ByteString -- X-Sendfile
87− -> IO ()
88−oneRequest app xsendfile = withRequest $ \r -> do
89− putStrLn "Received 1 request"
90− env <- peekEnvp r
91− ins <- peekIn r
92− outs <- peekOut r
93− handleRequest app ins outs env xsendfile
94−
95−peekIn, peekOut :: Ptr FCGX_Request -> IO (Ptr FCGX_Stream)
96−peekIn = (#peek FCGX_Request, in)
97−peekOut = (#peek FCGX_Request, out)
98−
99−peekEnvp :: Ptr FCGX_Request -> IO Environ
100−peekEnvp = (#peek FCGX_Request, envp)
101−
102−foreign import ccall unsafe "fcgiapp.h FCGX_InitRequest" fcgx_initrequest
103− :: Ptr FCGX_Request -> CInt -> CInt -> IO CInt
104−
105−foreign import ccall safe "fcgiapp.h FCGX_Accept_r" fcgx_accept_r
106− :: Ptr FCGX_Request -> IO CInt
107−
108−acceptRequest :: IO (Ptr FCGX_Request)
109−acceptRequest = do
110− reqp <- mallocBytes (#size FCGX_Request)
111− initAndAccept reqp
112− return reqp
113− where initAndAccept reqp = do
114− testReturn "FCGX_InitRequest" $ fcgx_initrequest reqp 0 0
115− testReturn "FCGX_Accept_r" $ fcgx_accept_r reqp
116−
117−withRequest :: (Ptr FCGX_Request -> IO ()) -> IO ()
118−withRequest f = do
119− req <- acceptRequest
120− f req `finally` finishRequest req
121−
122−foreign import ccall unsafe "fcgiapp.h FCGX_Finish_r" fcgx_finish_r
123− :: Ptr FCGX_Request -> IO ()
124−
125−finishRequest :: Ptr FCGX_Request -> IO ()
126−finishRequest reqp = do
127− fcgx_finish_r reqp
128− free reqp
129−
130−handleRequest :: W.Application
131− -> StreamPtr
132− -> StreamPtr
133− -> Environ
134− -> Maybe S.ByteString -- sendfile
135− -> IO ()
136−handleRequest f ins outs env xsendfile = do
137− vars <- environToTable env
138− let vars' = map (S8.unpack *** S8.unpack) vars
139− let input = const $ sRead ins
140− let hPut = sPutStr' outs
141− CGI.runGeneric vars' (CGI.requestBodyFunc input) hPut xsendfile f
142−
143−data FCGX_Request
144−
145−--
146−-- * Stream IO
147−--
148−
149−sPutStr' :: StreamPtr -> BS.ByteString -> IO ()
150−sPutStr' h str =
151− BSB.unsafeUseAsCStringLen str $ fcgxPutCStringLen h
152−
153−fcgxPutCStringLen :: StreamPtr -> CStringLen -> IO ()
154−fcgxPutCStringLen h (cs,len) =
155− testReturn "FCGX_PutStr" $ fcgx_putStr cs (fromIntegral len) h
156−
157−sRead :: StreamPtr -> IO (Maybe BS.ByteString)
158−sRead h = buildByteString (fcgxGetBuf h) 4096
159−
160−fcgxGetBuf :: StreamPtr -> Ptr a -> Int -> IO Int
161−fcgxGetBuf h p c =
162− liftM fromIntegral $ fcgx_getStr (castPtr p) (fromIntegral c) h
163−
164−--
165−-- * ByteString utilities
166−--
167−
168−-- | Data.ByteString.Lazy.hGetContentsN generalized to arbitrary
169−-- reading functions.
170−buildByteString :: (Ptr Word8 -> Int -> IO Int) -> Int -> IO (Maybe BS.ByteString)
171−buildByteString f k = do
172− ps <- BSB.createAndTrim k $ \p -> f p k
173− case BS.length ps of
174− 0 -> return Nothing
175− _ -> return $ Just ps
176−
177−--
178−-- * Utilities
179−--
180−
181−testReturn :: String -> IO CInt -> IO ()
182−testReturn e = throwIfNeg_ (\n -> e ++ " failed with error code: "++ show n)
183−
184−environToTable :: Environ -> IO [(S.ByteString, S.ByteString)]
185−environToTable arr = do
186− css <- peekArray0 nullPtr arr
187− ss <- mapM S.packCString css
188− return $ map splitEq ss
189− where
190− splitEq s =
191− let (a, b) = S.breakByte 61 s
192− in (a, S.drop 1 b)
wai-handler-fastcgi/READMEdeleted

No changes to the content.

wai-handler-fastcgi/Setup.lhsdeleted−7
@@ -1,7 +0,0 @@
1−#!/usr/bin/env runhaskell
2−
3−> module Main where
4−> import Distribution.Simple
5−
6−> main :: IO ()
7−> main = defaultMain
wai-handler-fastcgi/configuredeleted−9
@@ -1,9 +0,0 @@
1−#!/bin/sh
2−#
3−
4−# subst standard header path variables
5−if test -n "$CPPFLAGS" ; then
6− echo "Found CPPFLAGS in environment: '$CPPFLAGS'"
7− sed 's,@CPPFLAGS@,'"$CPPFLAGS"',g;s,@LDFLAGS@,'"$LDFLAGS"',g' \
8− < fastcgi.buildinfo.in > fastcgi.buildinfo
9−fi
wai-handler-fastcgi/fastcgi.buildinfo.indeleted−3
@@ -1,3 +0,0 @@
1−ghc-options: -optc@CPPFLAGS@
2−cc-options: @CPPFLAGS@
3−ld-options: @LDFLAGS@
wai-handler-fastcgi/lighttpd.confdeleted−19
@@ -1,19 +0,0 @@
1−# Run with lighttpd -D -f lighttpd.conf
2−server.port = 3000
3−server.document-root = "."
4−server.modules = ("mod_fastcgi", "mod_rewrite")
5−
6−url.rewrite-once = (
7− "(.*)" => "/app/$1"
8−)
9−
10−fastcgi.server = (
11− "/app" => ((
12− "socket" => "/tmp/test.fastcgi.socket",
13− "check-local" => "disable",
14− "bin-path" => "./test",
15− "min-procs" => 1,
16− "max-procs" => 12,
17− "idle-timeout" => 30
18− ))
19−)
wai-handler-fastcgi/test.hsdeleted−19
@@ -1,19 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−import Network.Wai
3−import Network.Wai.Handler.FastCGI
4−import Control.Monad.IO.Class
5−import qualified Data.ByteString.Lazy as L
6−import qualified Data.ByteString.Lazy.Char8 as L8
7−import qualified Data.Enumerator as E
8−
9−main = run app
10−
11−app req
12− | pathInfo req == "/" = return $ responseLBS status200 [("Content-Type", "text/html")] "<form method='post' action='/post'><input type='text' name='name'><input type='submit'></form>"
13−app req = do
14− bss <- E.consume
15− return $ responseLBS status200 [("Content-Type", "text/plain")] $ L.concat
16− [ L8.pack $ show $ requestHeaders req
17− , "\n"
18− , L.fromChunks bss
19− ]
wai-handler-fastcgi/wai-handler-fastcgi.cabaldeleted−31
@@ -1,31 +0,0 @@
1−name: wai-handler-fastcgi
2−version: 2.0.0.1
3−license: MIT
4−license-file: LICENSE
5−author: Michael Snoyman <michael@snoyman.com>
6−maintainer: Michael Snoyman <michael@snoyman.com>
7−synopsis: Wai handler to fastcgi
8−category: Web
9−stability: stable
10−cabal-version: >= 1.6
11−build-type: Configure
12−extra-source-files: configure fastcgi.buildinfo.in
13−homepage: http://www.yesodweb.com/book/web-application-interface
14−description: Calls out to the libfcgi C library.
15−
16−library
17− build-depends: base >= 4 && < 5
18− , wai >= 2.0 && < 2.2
19− , wai-extra >= 2.0 && < 2.2
20− , bytestring >= 0.9.1.4
21− exposed-modules: Network.Wai.Handler.FastCGI
22− ghc-options: -Wall
23− includes: fcgiapp.h
24− extra-libraries: fcgi
25−
26− if impl(ghc >= 7.4)
27− cpp-options: -DGHC_7_4
28−
29−source-repository head
30− type: git
31− location: git://github.com/yesodweb/wai.git
wai-handler-launch/.gitignoredeleted−2
@@ -1,2 +0,0 @@
1−dist/
2−*.swp
wai-handler-launch/LICENSEdeleted−20
@@ -1,20 +0,0 @@
1−Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
2−
3−Permission is hereby granted, free of charge, to any person obtaining
4−a copy of this software and associated documentation files (the
5−"Software"), to deal in the Software without restriction, including
6−without limitation the rights to use, copy, modify, merge, publish,
7−distribute, sublicense, and/or sell copies of the Software, and to
8−permit persons to whom the Software is furnished to do so, subject to
9−the following conditions:
10−
11−The above copyright notice and this permission notice shall be
12−included in all copies or substantial portions of the Software.
13−
14−THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15−EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16−MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17−NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18−LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19−OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20−WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wai-handler-launch/Network/Wai/Handler/Launch.hsdeleted−156
@@ -1,156 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE ForeignFunctionInterface #-}
3−{-# LANGUAGE CPP #-}
4−module Network.Wai.Handler.Launch
5− ( run
6− , runUrl
7− , runUrlPort
8− ) where
9−
10−import Network.Wai
11−import Network.Wai.Internal
12−import Network.HTTP.Types
13−import qualified Network.Wai.Handler.Warp as Warp
14−import Data.IORef
15−import Control.Concurrent (forkIO, threadDelay)
16−import Control.Monad.IO.Class (liftIO)
17−import qualified Data.ByteString as S
18−import Blaze.ByteString.Builder (fromByteString)
19−#if WINDOWS
20−import Foreign
21−import Foreign.C.String
22−#else
23−import System.Cmd (rawSystem)
24−#endif
25−import Data.Conduit.Zlib (decompressFlush, WindowBits (WindowBits))
26−import Data.Conduit.Blaze (builderToByteStringFlush)
27−import Data.Conduit
28−import qualified Data.Conduit.List as CL
29−
30−ping :: IORef Bool -> Middleware
31−ping var app req
32− | pathInfo req == ["_ping"] = do
33− liftIO $ writeIORef var True
34− return $ responseLBS status200 [] ""
35− | otherwise = do
36− res <- app req
37− let isHtml hs =
38− case lookup "content-type" hs of
39− Just ct -> "text/html" `S.isPrefixOf` ct
40− Nothing -> False
41− case res of
42− ResponseFile _ hs _ _
43− | not $ isHtml hs -> return res
44− ResponseBuilder _ hs _
45− | not $ isHtml hs -> return res
46− ResponseSource _ hs _
47− | not $ isHtml hs -> return res
48− _ -> do
49− let (s, hs, withBody) = responseToSource res
50− let (isEnc, headers') = fixHeaders id hs
51− let headers'' = filter (\(x, _) -> x /= "content-length") headers'
52− let fixEnc src =
53− if isEnc then
54− src $= decompressFlush (WindowBits 31)
55− else src
56− return $ ResponseSource s headers'' $ \f -> withBody $ \body -> f
57− $ fixEnc (body $= builderToByteStringFlush)
58− $= insideHead
59− $= CL.map (fmap fromByteString)
60−
61−toInsert :: S.ByteString
62−toInsert = "<script>setInterval(function(){var x;if(window.XMLHttpRequest){x=new XMLHttpRequest();}else{x=new ActiveXObject(\"Microsoft.XMLHTTP\");}x.open(\"GET\",\"/_ping\",false);x.send();},60000)</script>"
63−
64−insideHead :: Conduit (Flush S.ByteString) IO (Flush S.ByteString)
65−insideHead =
66− loop' (S.empty, whole)
67− where
68− loop' state = await >>= maybe (close state) (push' state)
69− whole = "<head>"
70− push' state (Chunk x) = push state x
71− push' state Flush = yield Flush >> loop' state
72−
73− push (held, atFront) x
74− | atFront `S.isPrefixOf` x = do
75− let y = S.drop (S.length atFront) x
76− mapM_ (yield . Chunk) [held, atFront, toInsert, y]
77− CL.map id
78− | whole `S.isInfixOf` x = do
79− let (before, rest) = S.breakSubstring whole x
80− let after = S.drop (S.length whole) rest
81− mapM_ (yield . Chunk) [held, before, whole, toInsert, after]
82− CL.map id
83− | x `S.isPrefixOf` atFront = do
84− let held' = held `S.append` x
85− atFront' = S.drop (S.length x) atFront
86− loop' (held', atFront')
87− | otherwise = do
88− let (held', atFront', x') = getOverlap whole x
89− mapM_ (yield . Chunk) [held, x']
90− loop' (held', atFront')
91−
92− close (held, _) = mapM_ yield [Chunk held, Chunk toInsert]
93−
94−getOverlap :: S.ByteString -> S.ByteString -> (S.ByteString, S.ByteString, S.ByteString)
95−getOverlap whole x =
96− go whole
97− where
98− go piece
99− | S.null piece = ("", whole, x)
100− | piece `S.isSuffixOf` x =
101− let x' = S.take (S.length x - S.length piece) x
102− atFront = S.drop (S.length piece) whole
103− in (piece, atFront, x')
104− | otherwise = go $ S.init piece
105−
106−fixHeaders :: ([Header] -> [Header])
107− -> [Header]
108− -> (Bool, [Header])
109−fixHeaders front [] = (False, front [])
110−fixHeaders front (("content-encoding", "gzip"):rest) = (True, front rest)
111−fixHeaders front (x:xs) = fixHeaders (front . (:) x) xs
112−
113−#if WINDOWS
114−foreign import ccall "launch"
115− launch' :: Int -> CString -> IO ()
116−#endif
117−
118−launch :: Int -> String -> IO ()
119−
120−#if WINDOWS
121−launch port s = withCString s $ launch' port
122−#else
123−launch port s = forkIO (rawSystem
124−#if MAC
125− "open"
126−#else
127− "xdg-open"
128−#endif
129− ["http://127.0.0.1:" ++ show port ++ "/" ++ s] >> return ()) >> return ()
130−#endif
131−
132−run :: Application -> IO ()
133−run = runUrl ""
134−
135−runUrl :: String -> Application -> IO ()
136−runUrl = runUrlPort 4587
137−
138−runUrlPort :: Int -> String -> Application -> IO ()
139−runUrlPort port url app = do
140− x <- newIORef True
141− _ <- forkIO $ Warp.runSettings Warp.defaultSettings
142− { Warp.settingsPort = port
143− , Warp.settingsOnException = (\_ _ -> return ())
144− , Warp.settingsHost = "*4"
145− } $ ping x app
146− launch port url
147− loop x
148−
149−loop :: IORef Bool -> IO ()
150−loop x = do
151− let seconds = 120
152− threadDelay $ 1000000 * seconds
153− b <- readIORef x
154− if b
155− then writeIORef x False >> loop x
156− else return ()
wai-handler-launch/READMEdeleted

No changes to the content.

wai-handler-launch/Setup.lhsdeleted−7
@@ -1,7 +0,0 @@
1−#!/usr/bin/env runhaskell
2−
3−> module Main where
4−> import Distribution.Simple
5−
6−> main :: IO ()
7−> main = defaultMain
wai-handler-launch/runtests.hsdeleted−22
@@ -1,22 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−import Network.Wai.Handler.Launch (insideHead)
3−import Test.Hspec
4−import Test.Hspec.HUnit
5−import Data.Enumerator hiding (map)
6−import Test.HUnit
7−import qualified Data.ByteString as S
8−
9−main :: IO ()
10−main = hspec $ describe "insideHead"
11− [ it "handles single chunks" $ helper "bar<head>foobaz" ["bar<head>baz"]
12− , it "handles single chunks, no <head>" $ helper "barfoo" ["bar"]
13− , it "handles whole chunks" $ helper "bar<head>foobaz" ["bar", "<head>", "baz"]
14− , it "handles split chunks" $ helper "bar<head>foobaz" ["bar", "<he", "ad>", "baz"]
15− , it "handles many chunks" $ helper "bar<head>foobaz1234567890" ["bar", "<he", "ad>", "baz", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
16− , it "handles many pieces" $ helper "bar<head>foobaz" $ map S.singleton $ S.unpack "bar<head>baz"
17− ]
18−
19−helper :: S.ByteString -> [S.ByteString] -> Assertion
20−helper res pieces = do
21− x <- run_ $ enumList 1 pieces $$ joinI $ insideHead "foo" $$ consume
22− S.concat x @?= res
wai-handler-launch/test.hsdeleted−7
@@ -1,7 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−import Network.Wai
3−import Network.HTTP.Types
4−import Network.Wai.Handler.Launch
5−import Network.Wai.Middleware.Gzip
6−
7−main = runUrl "FIXME" $ gzip True $ \_ -> return $ responseLBS status200 [("Content-Type", "text/html; charset=utf8")] "<html><head></head><body>HELLO THERE"
wai-handler-launch/wai-handler-launch.cabaldeleted−38
@@ -1,38 +0,0 @@
1−Name: wai-handler-launch
2−Version: 2.0.1.2
3−Synopsis: Launch a web app in the default browser.
4−Description: This handles cross-platform launching and inserts Javascript code to ping the server. When the server no longer receives pings, it shuts down.
5−License: MIT
6−License-file: LICENSE
7−Author: Michael Snoyman
8−Maintainer: michael@snoyman.com
9−Category: Web
10−Build-type: Simple
11−Cabal-version: >=1.6
12−
13−Library
14− Exposed-modules: Network.Wai.Handler.Launch
15− build-depends: base >= 4 && < 5
16− , wai >= 2.0 && < 2.2
17− , warp >= 2.0 && < 2.2
18− , http-types >= 0.7
19− , transformers >= 0.2.2 && < 0.4
20− , bytestring >= 0.9.1.4
21− , blaze-builder >= 0.2.1.4 && < 0.4
22− , conduit >= 0.5 && < 1.2
23− , conduit-extra >= 0.0 && < 1.2
24− , blaze-builder-conduit >= 0.5 && < 1.2
25− , zlib-conduit >= 0.5 && < 1.2
26−
27− if os(windows)
28− c-sources: windows.c
29− cpp-options: -DWINDOWS
30− extra-libraries: Shell32
31− else
32− build-depends: process >= 1.0 && < 1.3
33− if os(darwin)
34− cpp-options: -DMAC
35−
36−source-repository head
37− type: git
38− location: git://github.com/yesodweb/wai.git
wai-handler-launch/windows.cdeleted−12
@@ -1,12 +0,0 @@
1−#include <windows.h>
2−#include <shellapi.h>
3−#include <stdio.h>
4−
5−void launch(int port, char *s)
6−{
7− int len = 8 + strlen("http://127.0.0.1:") + strlen(s);
8− char *buff = malloc(len);
9− snprintf(buff, len, "http://127.0.0.1:%d/%s", port, s);
10− ShellExecute(NULL, "open", buff, NULL, NULL, SW_SHOWNORMAL);
11− free(buff);
12−}
wai-handler-webkit/.gitignoredeleted−2
@@ -1,2 +0,0 @@
1−dist
2−*.swp
wai-handler-webkit/Network/Wai/Handler/Webkit.hsdeleted−21
@@ -1,21 +0,0 @@
1−{-# LANGUAGE ForeignFunctionInterface #-}
2−module Network.Wai.Handler.Webkit (run) where
3−
4−import Network.Wai (Application)
5−import qualified Network.Wai.Handler.Warp as S
6−import Control.Concurrent (forkOS)
7−import Control.Concurrent.MVar
8−import Foreign.C.String (CString, withCString)
9−import Control.Exception (finally)
10−
11−run :: String -- ^ Title to show in titlebar
12− -> Application -> IO ()
13−run title app = do
14− mvar <- newEmptyMVar
15− _ <- forkOS $ finally (S.run 3000 app) (putMVar mvar ())
16− _ <- forkOS $ finally (withCString title startBrowser) (putMVar mvar ())
17− _ <- takeMVar mvar
18− return ()
19−
20−foreign import ccall "start_browser"
21− startBrowser :: CString -> IO ()
wai-handler-webkit/README.mddeleted−53
@@ -1,53 +0,0 @@
1−wai-handler-webkit
2−==================
3−
4−Turn WAI applications into standalone GUIs using Qt WebKit.
5−
6−
7−Linux
8−-----
9−
10−You'll need the appropriate system libraries, on Ubuntu:
11−
12− apt-get install libqtwebkit-dev
13−
14−On Lucid, this required adding a PPA:
15−
16− sudo add-apt-repository ppa:kubuntu-ppa/backports
17−
18−
19−Windows
20−-------
21−
22−Install the Qt SDK from <http://qt.nokia.com/downloads/>
23−
24−Your application's Cabal file will require certain options for Windows,
25−shown below. In particular, we need to link with `g++`, so make sure
26−it's in your `PATH`. The option `-optl-mwindows` ensures that the
27−application does not open a terminal.
28−
29− if os(windows)
30− ghc-options: -Wall -threaded -pgml g++ -optl-static -optl-mwindows
31− else
32− ghc-options: -Wall
33−
34−Run `cabal-install` with the Qt `include` and `lib` directories.
35−
36− export QT_PATH=c:/QtSDK/Desktop/Qt/4.7.3/mingw
37−
38− cabal-dev install \
39− --extra-include-dirs=$QT_PATH/include \
40− --extra-include-dirs=$QT_PATH/include/QtCore \
41− --extra-include-dirs=$QT_PATH/include/QtNetwork \
42− --extra-include-dirs=$QT_PATH/include/QtGui \
43− --extra-include-dirs=$QT_PATH/include/QtWebKit \
44− --extra-lib-dirs=$QT_PATH/lib
45−
46−You'll need to include these DLLs with your application:
47−
48− phonon4.dll
49− libgcc_s_dw2-1.dll
50− QtCore4.dll
51− QtGui4.dll
52− QtNetwork4.dll
53− QtWebKit4.dll
wai-handler-webkit/Setup.lhsdeleted−7
@@ -1,7 +0,0 @@
1−#!/usr/bin/env runhaskell
2−
3−> module Main where
4−> import Distribution.Simple
5−
6−> main :: IO ()
7−> main = defaultMain
wai-handler-webkit/sample.hsdeleted−10
@@ -1,10 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−import Network.Wai
3−import Network.Wai.Handler.Webkit
4−import Network.HTTP.Types
5−
6−main :: IO ()
7−main = run "Sample App" app
8−
9−app :: Application
10−app _ = return $ responseLBS status200 [("Content-Type", "text/html")] "<h1>Hello World!</h1>"
wai-handler-webkit/wai-handler-webkit.cabaldeleted−31
@@ -1,31 +0,0 @@
1−name: wai-handler-webkit
2−version: 2.0.0.2
3−license: MIT
4−license-file: LICENSE
5−author: Michael Snoyman <michael@snoyman.com>
6−maintainer: Michael Snoyman <michael@snoyman.com>
7−synopsis: Turn WAI applications into standalone GUIs using QtWebkit.
8−description: Runs any WAI application using
9− the simpleserver handler and then pops up a Webkit browser
10− to view it.
11−category: Web
12−stability: unstable
13−cabal-version: >= 1.6
14−build-type: Simple
15−homepage: https://github.com/yesodweb/wai/tree/master/wai-handler-webkit
16−
17−library
18− build-depends: base >= 4 && < 5
19− , wai >= 2.0 && < 2.2
20− , warp >= 2.0 && < 2.2
21− ghc-options: -Wall
22− exposed-modules: Network.Wai.Handler.Webkit
23− c-sources: webkit.cpp
24− if os(windows)
25− extra-libraries: QtCore4, QtGui4, QtNetwork4, QtWebKit4
26− else
27− pkgconfig-depends: QtWebKit
28−
29−source-repository head
30− type: git
31− location: git://github.com/yesodweb/wai.git
wai-handler-webkit/webkit.cppdeleted−19
@@ -1,19 +0,0 @@
1−#include <QApplication>
2−#include <QPushButton>
3−#include <QtWebKit>
4−
5−extern "C" {
6− int start_browser(char *title)
7− {
8− char *argv[1];
9− argv[0] = title;
10− int argc = 1;
11−
12− QApplication app(argc, argv);
13− QWebView *view = new QWebView();
14− view->load(QUrl("http://localhost:3000/"));
15− view->show();
16−
17− return app.exec();
18− }
19−}
wai-handler-webkit/LICENSE → wai-http2-extra/LICENSErenamed+1−1
@@ -1,4 +1,4 @@
1−Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
1+Copyright (c) 2016 Kazu Yamamoto, http://www.yesodweb.com/
2
2
3Permission is hereby granted, free of charge, to any person obtaining
3Permission is hereby granted, free of charge, to any person obtaining
4a copy of this software and associated documentation files (the
4a copy of this software and associated documentation files (the
wai-http2-extra/Network/Wai/Middleware/Push/Referer.hsnew file+236
@@ -0,0 +1,236 @@
1+{-# LANGUAGE BangPatterns, OverloadedStrings, RecordWildCards #-}
2+
3+-- | Middleware for server push learning dependency based on Referer:.
4+module Network.Wai.Middleware.Push.Referer (
5+ -- * Middleware
6+ pushOnReferer
7+ -- * Making push promise
8+ , URLPath
9+ , MakePushPromise
10+ , defaultMakePushPromise
11+ -- * Settings
12+ , Settings
13+ , defaultSettings
14+ , makePushPromise
15+ , duration
16+ , keyLimit
17+ , valueLimit
18+ ) where
19+
20+import Control.Monad (when, unless)
21+import Control.Reaper
22+import Data.ByteString (ByteString)
23+import qualified Data.ByteString as BS
24+import Data.ByteString.Internal (ByteString(..), memchr)
25+import Data.IORef
26+import Data.Maybe (isNothing)
27+import Data.Word (Word8)
28+import Data.Word8
29+import Foreign.ForeignPtr (withForeignPtr, ForeignPtr)
30+import Foreign.Ptr (Ptr, plusPtr, minusPtr, nullPtr)
31+import Foreign.Storable (peek)
32+import Network.HTTP.Types (Status(..))
33+import Network.Wai
34+import Network.Wai.Handler.Warp hiding (Settings, defaultSettings)
35+import Network.Wai.Internal (Response(..))
36+import System.IO.Unsafe (unsafePerformIO)
37+
38+import qualified Network.Wai.Middleware.Push.Referer.LimitMultiMap as M
39+
40+-- $setup
41+-- >>> :set -XOverloadedStrings
42+
43+-- | Making a push promise based on Referer:,
44+-- path to be pushed and file to be pushed.
45+-- If the middleware should push this file in the next time when
46+-- the page of Referer: is accessed,
47+-- this function should return 'Just'.
48+-- If 'Nothing' is returned,
49+-- the middleware learns nothing.
50+type MakePushPromise = URLPath -- ^ path in referer (key: /index.html)
51+ -> URLPath -- ^ path to be pushed (value: /style.css)
52+ -> FilePath -- ^ file to be pushed (file_path/style.css)
53+ -> IO (Maybe PushPromise)
54+
55+-- | Type for URL path.
56+type URLPath = ByteString
57+
58+type Cache = M.LimitMultiMap URLPath PushPromise
59+
60+initialized :: IORef Bool
61+initialized = unsafePerformIO $ newIORef False
62+{-# NOINLINE initialized #-}
63+
64+cacheReaper :: IORef (Maybe (Reaper Cache (URLPath,PushPromise)))
65+cacheReaper = unsafePerformIO $ newIORef Nothing
66+{-# NOINLINE cacheReaper #-}
67+
68+-- | Settings for server push based on Referer:.
69+data Settings = Settings {
70+ makePushPromise :: MakePushPromise -- ^ Default: 'defaultMakePushPromise'
71+ , duration :: Int -- ^ Duration (in micro seconds) to keep the learning information. The information completely cleared every this duration. Default: 30000000
72+ , keyLimit :: Int -- ^ Max number of keys (e.g. index.html) in the learning information. Default: 20
73+ , valueLimit :: Int -- ^ Max number of values (e.g. style.css) in the learning information. Default: 20
74+ }
75+
76+-- | Default settings.
77+defaultSettings :: Settings
78+defaultSettings = Settings {
79+ makePushPromise = defaultMakePushPromise
80+ , duration = 30000000
81+ , keyLimit = 20
82+ , valueLimit = 20
83+ }
84+
85+tryInitialize :: Settings -> IO ()
86+tryInitialize Settings{..} = do
87+ isInitialized <- atomicModifyIORef' initialized $ \x -> (True, x)
88+ unless isInitialized $ do
89+ reaper <- mkReaper settings
90+ writeIORef cacheReaper (Just reaper)
91+ where
92+ emptyCache = M.empty keyLimit valueLimit
93+ settings :: ReaperSettings Cache (URLPath,PushPromise)
94+ settings = defaultReaperSettings {
95+ reaperAction = \_ -> return (\_ -> emptyCache)
96+ , reaperCons = M.insert
97+ , reaperNull = M.isEmpty
98+ , reaperEmpty = emptyCache
99+ , reaperDelay = duration
100+ }
101+
102+-- | The middleware to push files based on Referer:.
103+-- Learning strategy is implemented in the first argument.
104+pushOnReferer :: Settings -> Middleware
105+pushOnReferer settings@Settings{..} app req sendResponse = do
106+ tryInitialize settings
107+ mreaper <- readIORef cacheReaper
108+ case mreaper of
109+ Nothing -> app req sendResponse
110+ Just reaper -> app req (push reaper)
111+ where
112+ push reaper res@(ResponseFile (Status 200 "OK") _ file Nothing) = do
113+ let !path = rawPathInfo req
114+ m <- reaperRead reaper
115+ case M.lookup path m of
116+ [] -> case requestHeaderReferer req of
117+ Nothing -> return ()
118+ Just referer -> do
119+ (mauth,refPath) <- parseUrl referer
120+ when (isNothing mauth
121+ || requestHeaderHost req == mauth) $ do
122+ when (path /= refPath) $ do -- just in case
123+ let !path' = BS.copy path
124+ !refPath' = BS.copy refPath
125+ mpp <- makePushPromise refPath' path' file
126+ case mpp of
127+ Nothing -> return ()
128+ Just pp -> reaperAdd reaper (refPath',pp)
129+ ps -> do
130+ let !h2d = defaultHTTP2Data { http2dataPushPromise = ps}
131+ setHTTP2Data req (Just h2d)
132+ sendResponse res
133+ push _ res = sendResponse res
134+
135+
136+-- | Learn if the file to be pushed is CSS (.css) or JavaScript (.js) file
137+-- AND the Referer: ends with \"/\" or \".html\" or \".htm\".
138+defaultMakePushPromise :: MakePushPromise
139+defaultMakePushPromise refPath path file
140+ | isHTML refPath = case getCT path of
141+ Nothing -> return Nothing
142+ Just ct -> do
143+ let pp = defaultPushPromise {
144+ promisedPath = path
145+ , promisedFile = file
146+ , promisedResponseHeaders = [("content-type", ct)
147+ ,("x-http2-push", refPath)]
148+ }
149+ return $ Just pp
150+ | otherwise = return Nothing
151+
152+getCT :: URLPath -> Maybe ByteString
153+getCT p
154+ | ".js" `BS.isSuffixOf` p = Just "application/javascript"
155+ | ".css" `BS.isSuffixOf` p = Just "text/css"
156+ | otherwise = Nothing
157+
158+isHTML :: URLPath -> Bool
159+isHTML p = ("/" `BS.isSuffixOf` p)
160+ || (".html" `BS.isSuffixOf` p)
161+ || (".htm" `BS.isSuffixOf` p)
162+
163+-- |
164+--
165+-- >>> parseUrl ""
166+-- (Nothing,"")
167+-- >>> parseUrl "/"
168+-- (Nothing,"/")
169+-- >>> parseUrl "ht"
170+-- (Nothing,"")
171+-- >>> parseUrl "http://example.com/foo/bar/"
172+-- (Just "example.com","/foo/bar/")
173+-- >>> parseUrl "https://www.example.com/path/to/dir/"
174+-- (Just "www.example.com","/path/to/dir/")
175+-- >>> parseUrl "http://www.example.com:8080/path/to/dir/"
176+-- (Just "www.example.com:8080","/path/to/dir/")
177+-- >>> parseUrl "//www.example.com:8080/path/to/dir/"
178+-- (Just "www.example.com:8080","/path/to/dir/")
179+-- >>> parseUrl "/path/to/dir/"
180+-- (Nothing,"/path/to/dir/")
181+
182+parseUrl :: ByteString -> IO (Maybe ByteString, URLPath)
183+parseUrl bs@(PS fptr0 off len)
184+ | len == 0 = return (Nothing, "")
185+ | len == 1 = return (Nothing, bs)
186+ | otherwise = withForeignPtr fptr0 $ \ptr0 -> do
187+ let begptr = ptr0 `plusPtr` off
188+ limptr = begptr `plusPtr` len
189+ parseUrl' fptr0 ptr0 begptr limptr len
190+
191+parseUrl' :: ForeignPtr Word8 -> Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int
192+ -> IO (Maybe ByteString, URLPath)
193+parseUrl' fptr0 ptr0 begptr limptr len0 = do
194+ w0 <- peek begptr
195+ if w0 == _slash then do
196+ w1 <- peek $ begptr `plusPtr` 1
197+ if w1 == _slash then
198+ doubleSlashed begptr len0
199+ else
200+ slashed begptr len0 Nothing
201+ else do
202+ colonptr <- memchr begptr _colon $ fromIntegral len0
203+ if colonptr == nullPtr then
204+ return (Nothing, "")
205+ else do
206+ let !authptr = colonptr `plusPtr` 1
207+ doubleSlashed authptr (limptr `minusPtr` authptr)
208+ where
209+ -- // / ?
210+ doubleSlashed :: Ptr Word8 -> Int -> IO (Maybe ByteString, URLPath)
211+ doubleSlashed ptr len
212+ | len < 2 = return (Nothing, "")
213+ | otherwise = do
214+ let ptr1 = ptr `plusPtr` 2
215+ pathptr <- memchr ptr1 _slash $ fromIntegral len
216+ if pathptr == nullPtr then
217+ return (Nothing, "")
218+ else do
219+ let !auth = bs ptr0 ptr1 pathptr
220+ slashed pathptr (limptr `minusPtr` pathptr) (Just auth)
221+
222+ -- / ?
223+ slashed :: Ptr Word8 -> Int -> Maybe ByteString -> IO (Maybe ByteString, URLPath)
224+ slashed ptr len mauth = do
225+ questionptr <- memchr ptr _question $ fromIntegral len
226+ if questionptr == nullPtr then do
227+ let !path = bs ptr0 ptr limptr
228+ return (mauth, path)
229+ else do
230+ let !path = bs ptr0 ptr questionptr
231+ return (mauth, path)
232+ bs p0 p1 p2 = path
233+ where
234+ !off = p1 `minusPtr` p0
235+ !siz = p2 `minusPtr` p1
236+ !path = PS fptr0 off siz
wai-http2-extra/Network/Wai/Middleware/Push/Referer/LimitMultiMap.hsnew file+40
@@ -0,0 +1,40 @@
1+{-# LANGUAGE BangPatterns #-}
2+
3+module Network.Wai.Middleware.Push.Referer.LimitMultiMap where
4+
5+import Data.Map (Map)
6+import qualified Data.Map.Strict as M
7+import Data.Set (Set)
8+import qualified Data.Set as S
9+
10+data LimitMultiMap k v = LimitMultiMap {
11+ limitKey :: !Int
12+ , limitVal :: !Int
13+ , multiMap :: !(Map k (Set v))
14+ } deriving (Eq, Show)
15+
16+isEmpty :: LimitMultiMap k t -> Bool
17+isEmpty (LimitMultiMap _ _ m) = M.null m
18+
19+empty :: Int -> Int -> LimitMultiMap k v
20+empty lk lv = LimitMultiMap lk lv M.empty
21+
22+insert :: (Ord k, Ord v) => (k,v) -> LimitMultiMap k v -> LimitMultiMap k v
23+insert (k,v) (LimitMultiMap lk lv m)
24+ | siz < lk = let !m' = M.alter alt k m in LimitMultiMap lk lv m'
25+ | siz == lk = let !m' = M.adjust adj k m in LimitMultiMap lk lv m'
26+ | otherwise = error "insert"
27+ where
28+ siz = M.size m
29+ alt Nothing = Just $ S.singleton v
30+ alt s@(Just set)
31+ | S.size set == lv = s
32+ | otherwise = Just $ S.insert v set
33+ adj set
34+ | S.size set == lv = set
35+ | otherwise = S.insert v set
36+
37+lookup :: Ord k => k -> LimitMultiMap k v -> [v]
38+lookup k (LimitMultiMap _ _ m) = case M.lookup k m of
39+ Nothing -> []
40+ Just set -> S.toList set
wai-http2-extra/test/doctests.hsnew file+4
@@ -0,0 +1,4 @@
1+import Test.DocTest
2+
3+main :: IO ()
4+main = doctest ["Network"]
wai-http2-extra/wai-http2-extra.cabalnew file+44
@@ -0,0 +1,44 @@
1+Name: wai-http2-extra
2+Version: 0.1.2
3+Synopsis: WAI utilities for HTTP/2
4+License: MIT
5+License-file: LICENSE
6+Author: Kazu Yamamoto
7+Maintainer: kazu@iij.ad.jp
8+Homepage: http://github.com/yesodweb/wai
9+Category: Web
10+Build-Type: Simple
11+Cabal-Version: >=1.10
12+Stability: Stable
13+Description: WAI utilities for HTTP/2
14+
15+Library
16+ Build-Depends: base >= 3 && < 5
17+ , auto-update >= 0.1.3 && < 0.2
18+ , bytestring
19+ , containers
20+ , http-types
21+ , wai
22+ , warp
23+ , word8
24+ Exposed-modules: Network.Wai.Middleware.Push.Referer
25+ Other-modules: Network.Wai.Middleware.Push.Referer.LimitMultiMap
26+ Ghc-Options: -Wall
27+ if impl(ghc >= 8)
28+ default-extensions: Strict StrictData
29+ default-language: Haskell2010
30+
31+Test-Suite doctest
32+ Type: exitcode-stdio-1.0
33+ HS-Source-Dirs: test
34+ Ghc-Options: -threaded -Wall
35+ Main-Is: doctests.hs
36+ Build-Depends: base
37+ , doctest >= 0.10.1
38+ if impl(ghc >= 8)
39+ default-extensions: Strict StrictData
40+ default-language: Haskell2010
41+
42+Source-Repository head
43+ Type: git
44+ Location: git://github.com/yesodweb/wai.git
wai-test/.ghcideleted−1
@@ -1 +0,0 @@
1−:set -itest -optP-include -optPdist/build/autogen/cabal_macros.h
wai-test/Network/Wai/Test.hsdeleted−201
@@ -1,201 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE DeriveDataTypeable #-}
4−module Network.Wai.Test
5− ( -- * Session
6− Session
7− , runSession
8− -- * Requests
9− , request
10− , srequest
11− , SRequest (..)
12− , SResponse (..)
13− , defaultRequest
14− , setPath
15− , setRawPathInfo
16− -- * Assertions
17− , assertStatus
18− , assertContentType
19− , assertBody
20− , assertBodyContains
21− , assertHeader
22− , assertNoHeader
23− , WaiTestFailure (..)
24− ) where
25−
26−import Network.Wai
27−import Control.Monad.IO.Class (liftIO)
28−import Control.Monad.Trans.State (StateT, evalStateT)
29−import Control.Monad.Trans.Reader (ReaderT, runReaderT, ask)
30−import Control.Monad (unless)
31−import Control.DeepSeq (deepseq)
32−import Control.Exception (throwIO, Exception)
33−import Data.Typeable (Typeable)
34−import Data.Map (Map)
35−import qualified Data.Map as Map
36−import Data.ByteString (ByteString)
37−import qualified Data.ByteString.Char8 as S8
38−import Data.Conduit.Blaze (builderToByteString)
39−import Blaze.ByteString.Builder (flush)
40−import qualified Blaze.ByteString.Builder as B
41−import qualified Data.ByteString.Lazy as L
42−import qualified Data.ByteString.Lazy.Char8 as L8
43−import qualified Network.HTTP.Types as H
44−import Data.CaseInsensitive (CI)
45−import qualified Data.ByteString as S
46−import qualified Data.Text as T
47−import qualified Data.Text.Encoding as TE
48−import qualified Data.Conduit as C
49−import qualified Data.Conduit.List as CL
50−
51−type Session = ReaderT Application (StateT ClientState IO)
52−
53−data ClientState = ClientState
54− { _clientCookies :: Map ByteString ByteString
55− }
56−
57−initState :: ClientState
58−initState = ClientState Map.empty
59−
60−runSession :: Session a -> Application -> IO a
61−runSession session app = evalStateT (runReaderT session app) initState
62−
63−data SRequest = SRequest
64− { simpleRequest :: Request
65− , simpleRequestBody :: L.ByteString
66− }
67−data SResponse = SResponse
68− { simpleStatus :: H.Status
69− , simpleHeaders :: H.ResponseHeaders
70− , simpleBody :: L.ByteString
71− }
72− deriving (Show, Eq)
73−request :: Request -> Session SResponse
74−request = srequest . flip SRequest L.empty
75−
76−-- | Set whole path (request path + query string).
77−setPath :: Request -> S8.ByteString -> Request
78−setPath req path = req {
79− pathInfo = segments
80− , rawPathInfo = B.toByteString (H.encodePathSegments segments)
81− , queryString = query
82− , rawQueryString = (H.renderQuery True query)
83− }
84− where
85− (segments, query) = H.decodePath path
86−
87−setRawPathInfo :: Request -> S8.ByteString -> Request
88−setRawPathInfo r rawPinfo =
89− let pInfo = dropFrontSlash $ T.split (== '/') $ TE.decodeUtf8 rawPinfo
90− in r { rawPathInfo = rawPinfo, pathInfo = pInfo }
91− where
92− dropFrontSlash ("":"":[]) = [] -- homepage, a single slash
93− dropFrontSlash ("":path) = path
94− dropFrontSlash path = path
95−
96−srequest :: SRequest -> Session SResponse
97−srequest (SRequest req bod) = do
98− app <- ask
99− let req' = req
100− { requestBody = CL.sourceList $ L.toChunks bod
101− }
102− liftIO $ app req' >>= runResponse
103− -- FIXME cookie processing
104− --return sres
105−
106−runResponse :: Response -> IO SResponse
107−runResponse res = do
108− bss <- withBody $ \body -> body C.$= CL.map toBuilder C.$= builderToByteString C.$$ CL.consume
109− return $ SResponse s h $ L.fromChunks bss
110− where
111− (s, h, withBody) = responseToSource res
112− toBuilder (C.Chunk builder) = builder
113− toBuilder C.Flush = flush
114−
115−assertBool :: String -> Bool -> Session ()
116−assertBool s b = unless b $ assertFailure s
117−
118−assertString :: String -> Session ()
119−assertString s = unless (null s) $ assertFailure s
120−
121−assertFailure :: String -> Session ()
122−assertFailure msg = msg `deepseq` liftIO (throwIO (WaiTestFailure msg))
123−
124−data WaiTestFailure = WaiTestFailure String
125− deriving (Show, Eq, Typeable)
126−instance Exception WaiTestFailure
127−
128−assertContentType :: ByteString -> SResponse -> Session ()
129−assertContentType ct SResponse{simpleHeaders = h} =
130− case lookup "content-type" h of
131− Nothing -> assertString $ concat
132− [ "Expected content type "
133− , show ct
134− , ", but no content type provided"
135− ]
136− Just ct' -> assertBool (concat
137− [ "Expected content type "
138− , show ct
139− , ", but received "
140− , show ct'
141− ]) (go ct == go ct')
142− where
143− go = S8.takeWhile (/= ';')
144−
145−assertStatus :: Int -> SResponse -> Session ()
146−assertStatus i SResponse{simpleStatus = s} = assertBool (concat
147− [ "Expected status code "
148− , show i
149− , ", but received "
150− , show sc
151− ]) $ i == sc
152− where
153− sc = H.statusCode s
154−
155−assertBody :: L.ByteString -> SResponse -> Session ()
156−assertBody lbs SResponse{simpleBody = lbs'} = assertBool (concat
157− [ "Expected response body "
158− , show $ L8.unpack lbs
159− , ", but received "
160− , show $ L8.unpack lbs'
161− ]) $ lbs == lbs'
162−
163−assertBodyContains :: L.ByteString -> SResponse -> Session ()
164−assertBodyContains lbs SResponse{simpleBody = lbs'} = assertBool (concat
165− [ "Expected response body to contain "
166− , show $ L8.unpack lbs
167− , ", but received "
168− , show $ L8.unpack lbs'
169− ]) $ strict lbs `S.isInfixOf` strict lbs'
170− where
171− strict = S.concat . L.toChunks
172−
173−assertHeader :: CI ByteString -> ByteString -> SResponse -> Session ()
174−assertHeader header value SResponse{simpleHeaders = h} =
175− case lookup header h of
176− Nothing -> assertString $ concat
177− [ "Expected header "
178− , show header
179− , " to be "
180− , show value
181− , ", but it was not present"
182− ]
183− Just value' -> assertBool (concat
184− [ "Expected header "
185− , show header
186− , " to be "
187− , show value
188− , ", but received "
189− , show value'
190− ]) (value == value')
191−
192−assertNoHeader :: CI ByteString -> SResponse -> Session ()
193−assertNoHeader header SResponse{simpleHeaders = h} =
194− case lookup header h of
195− Nothing -> return ()
196− Just s -> assertString $ concat
197− [ "Unexpected header "
198− , show header
199− , " containing "
200− , show s
201− ]
wai-test/READMEdeleted

No changes to the content.

wai-test/Setup.lhsdeleted−8
@@ -1,8 +0,0 @@
1−#!/usr/bin/env runhaskell
2−
3−> module Main where
4−> import Distribution.Simple
5−> import System.Cmd (system)
6−
7−> main :: IO ()
8−> main = defaultMain
wai-test/test/Network/Wai/TestSpec.hsdeleted−32
@@ -1,32 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−module Network.Wai.TestSpec (main, spec) where
3−
4−import Test.Hspec
5−
6−import Network.Wai
7−import Network.Wai.Test
8−
9−main :: IO ()
10−main = hspec spec
11−
12−spec :: Spec
13−spec = do
14− describe "setPath" $ do
15−
16− let req = setPath defaultRequest "/foo/bar/baz?foo=23&bar=42&baz"
17−
18− it "sets pathInfo" $ do
19− pathInfo req `shouldBe` ["foo", "bar", "baz"]
20−
21− it "sets rawPathInfo" $ do
22− rawPathInfo req `shouldBe` "/foo/bar/baz"
23−
24− it "sets queryString" $ do
25− queryString req `shouldBe` [("foo", Just "23"), ("bar", Just "42"), ("baz", Nothing)]
26−
27− it "sets rawQueryString" $ do
28− rawQueryString req `shouldBe` "?foo=23&bar=42&baz"
29−
30− context "when path has no query string" $ do
31− it "sets rawQueryString to empty string" $ do
32− rawQueryString (setPath defaultRequest "/foo/bar/baz") `shouldBe` ""
wai-test/wai-test.cabaldeleted−47
@@ -1,47 +0,0 @@
1−name: wai-test
2−version: 2.0.1.1
3−license: MIT
4−license-file: LICENSE
5−author: Michael Snoyman <michael@snoyman.com>
6−maintainer: Michael Snoyman <michael@snoyman.com>
7−synopsis: Unit test framework (built on HUnit) for WAI applications.
8−category: Testing, Web, Yesod
9−stability: Stable
10−cabal-version: >= 1.8
11−build-type: Simple
12−homepage: http://www.yesodweb.com/book/web-application-interface
13−description: Unit test framework (built on HUnit) for WAI applications.
14−
15−library
16− build-depends: base >= 4 && < 5
17− , wai >= 2.0 && < 2.2
18− , bytestring >= 0.9.1.4
19− , text >= 0.7
20− , blaze-builder >= 0.2.1.4 && < 0.4
21− , transformers >= 0.2.2 && < 0.4
22− , containers >= 0.2
23− , conduit >= 0.5 && < 1.2
24− , conduit-extra
25− , blaze-builder-conduit >= 0.5 && < 1.2
26− , cookie >= 0.2 && < 0.5
27− , http-types >= 0.7
28− , case-insensitive >= 0.2
29− , network
30− , deepseq
31− exposed-modules: Network.Wai.Test
32− ghc-options: -Wall
33−
34−test-suite spec
35− type: exitcode-stdio-1.0
36− hs-source-dirs: test
37− main-is: Spec.hs
38− other-modules: Network.Wai.TestSpec
39− build-depends: base >= 4 && < 5
40− , wai-test
41− , wai
42− , hspec >= 1.3
43− ghc-options: -Wall -Werror
44−
45−source-repository head
46− type: git
47− location: git://github.com/yesodweb/wai.git
wai-websockets/ChangeLog.mdnew file+27
@@ -0,0 +1,27 @@
1+## 3.0.1.2
2+
3+* Drop unused dependency on blaze-builder
4+
5+## 3.0.1.1
6+
7+* Doc improvement
8+
9+## 3.0.1
10+
11+* Improved connection close logic
12+
13+## 3.0.0.9
14+
15+* Clean up stream resources when websockets completes [#549](https://github.com/yesodweb/wai/pull/549)
16+
17+## 3.0.0.8
18+
19+* Support wai 3.2
20+
21+## 3.0.0.7
22+
23+* Improved documentation [#471](https://github.com/yesodweb/wai/pull/471)
24+
25+## 3.0.0.5
26+
27+Allow blaze-builder 0.4
wai-websockets/Network/Wai/Handler/WebSockets.hs+63−42
@@ -1,40 +1,71 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Network.Wai.Handler.WebSockets
2module Network.Wai.Handler.WebSockets
3− ( websocketsApp
3+ ( websocketsOr
4− , websocketsOr
4+ , websocketsApp
5 , isWebSocketsReq
5 , isWebSocketsReq
6 , getRequestHead
6 , getRequestHead
7 , runWebSockets
7 , runWebSockets
8 ) where
8 ) where
9
9
10+import Control.Exception (bracket, tryJust)
10import Data.ByteString (ByteString)
11import Data.ByteString (ByteString)
11import qualified Data.ByteString.Char8 as BC
12import qualified Data.ByteString.Char8 as BC
13+import qualified Data.ByteString.Lazy as BL
12import qualified Data.CaseInsensitive as CI
14import qualified Data.CaseInsensitive as CI
13−import Data.Conduit
14−import Data.IORef (newIORef, readIORef, writeIORef)
15import Network.HTTP.Types (status500)
15import Network.HTTP.Types (status500)
16import qualified Network.Wai as Wai
16import qualified Network.Wai as Wai
17import qualified Network.WebSockets as WS
17import qualified Network.WebSockets as WS
18import qualified Network.WebSockets.Connection as WS
18import qualified Network.WebSockets.Connection as WS
19−import System.IO.Streams (InputStream, OutputStream)
19+import qualified Network.WebSockets.Stream as WS
20−import qualified System.IO.Streams as Streams
21
20
22--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
22+-- | Returns whether or not the given 'Wai.Request' is a WebSocket request.
23isWebSocketsReq :: Wai.Request -> Bool
23isWebSocketsReq :: Wai.Request -> Bool
24isWebSocketsReq req =
24isWebSocketsReq req =
25 fmap CI.mk (lookup "upgrade" $ Wai.requestHeaders req) == Just "websocket"
25 fmap CI.mk (lookup "upgrade" $ Wai.requestHeaders req) == Just "websocket"
26
26
27--------------------------------------------------------------------------------
27--------------------------------------------------------------------------------
28+-- | Upgrade a @websockets@ 'WS.ServerApp' to a @wai@ 'Wai.Application'. Uses
29+-- the given backup 'Wai.Application' to handle 'Wai.Request's that are not
30+-- WebSocket requests.
31+--
32+-- @
33+-- websocketsOr opts ws_app backup_app = \\req respond ->
34+-- __case__ 'websocketsApp' opts ws_app req __of__
35+-- 'Nothing' -> backup_app req send_response
36+-- 'Just' res -> respond res
37+-- @
38+--
39+-- For example, below is an 'Wai.Application' that sends @"Hello, client!"@ to
40+-- each connected client.
41+--
42+-- @
43+-- app :: 'Wai.Application'
44+-- app = 'websocketsOr' 'WS.defaultConnectionOptions' wsApp backupApp
45+-- __where__
46+-- wsApp :: 'WS.ServerApp'
47+-- wsApp pending_conn = do
48+-- conn <- 'WS.acceptRequest' pending_conn
49+-- 'WS.sendTextData' conn ("Hello, client!" :: 'Data.Text.Text')
50+--
51+-- backupApp :: 'Wai.Application'
52+-- backupApp _ respond = respond $ 'Wai.responseLBS' 'Network.HTTP.Types.status400' [] "Not a WebSocket request"
53+-- @
28websocketsOr :: WS.ConnectionOptions
54websocketsOr :: WS.ConnectionOptions
29 -> WS.ServerApp
55 -> WS.ServerApp
30 -> Wai.Application
56 -> Wai.Application
31 -> Wai.Application
57 -> Wai.Application
32−websocketsOr opts app backup req =
58+websocketsOr opts app backup req sendResponse =
33 case websocketsApp opts app req of
59 case websocketsApp opts app req of
34− Nothing -> backup req
60+ Nothing -> backup req sendResponse
35− Just res -> return res
61+ Just res -> sendResponse res
36
62
37--------------------------------------------------------------------------------
63--------------------------------------------------------------------------------
64+-- | Handle a single @wai@ 'Wai.Request' with the given @websockets@
65+-- 'WS.ServerApp'. Returns 'Nothing' if the 'Wai.Request' is not a WebSocket
66+-- request, 'Just' otherwise.
67+--
68+-- Usually, 'websocketsOr' is more convenient.
38websocketsApp :: WS.ConnectionOptions
69websocketsApp :: WS.ConnectionOptions
39 -> WS.ServerApp
70 -> WS.ServerApp
40 -> Wai.Request
71 -> Wai.Request
@@ -57,41 +88,31 @@ getRequestHead req = WS.RequestHead
57 (Wai.isSecure req)
88 (Wai.isSecure req)
58
89
59--------------------------------------------------------------------------------
90--------------------------------------------------------------------------------
60−---- | Internal function to run the WebSocket io-streams using the conduit library
91+-- | Internal function to run the WebSocket io-streams using the conduit library.
61runWebSockets :: WS.ConnectionOptions
92runWebSockets :: WS.ConnectionOptions
62 -> WS.RequestHead
93 -> WS.RequestHead
63 -> (WS.PendingConnection -> IO a)
94 -> (WS.PendingConnection -> IO a)
64− -> Source IO ByteString
95+ -> IO ByteString
65− -> Sink ByteString IO ()
96+ -> (ByteString -> IO ())
66 -> IO a
97 -> IO a
67−runWebSockets opts req app src sink = do
98+runWebSockets opts req app src sink = bracket mkStream ensureClose (app . pc)
68−
69− is <- srcToInput src
70− os <- sinkToOutput sink >>= Streams.builderStream
71−
72− let pc = WS.PendingConnection
73− { WS.pendingOptions = opts
74− , WS.pendingRequest = req
75− , WS.pendingOnAccept = \_ -> return ()
76− , WS.pendingIn = is
77− , WS.pendingOut = os
78− }
79−
80− app pc
81−
82−srcToInput :: Source IO ByteString -> IO (InputStream ByteString)
83−srcToInput src0 = do
84− (rsrc0, ()) <- src0 $$+ return ()
85− ref <- newIORef rsrc0
86− Streams.makeInputStream $ do
87− rsrc <- readIORef ref
88− (rsrc', mbs) <- rsrc $$++ await
89− writeIORef ref rsrc'
90− return mbs
91−
92−sinkToOutput :: Sink ByteString IO () -> IO (OutputStream ByteString)
93−sinkToOutput sink =
94− Streams.makeOutputStream output
95 where
99 where
96− output Nothing = return ()
100+ ensureClose = tryJust onConnectionException . WS.close
97− output (Just bs) = yield bs $$ sink
101+ onConnectionException :: WS.ConnectionException -> Maybe ()
102+ onConnectionException WS.ConnectionClosed = Just ()
103+ onConnectionException _ = Nothing
104+ mkStream =
105+ WS.makeStream
106+ (do
107+ bs <- src
108+ return $ if BC.null bs then Nothing else Just bs)
109+ (\mbBl -> case mbBl of
110+ Nothing -> return ()
111+ Just bl -> mapM_ sink (BL.toChunks bl))
112+
113+ pc stream = WS.PendingConnection
114+ { WS.pendingOptions = opts
115+ , WS.pendingRequest = req
116+ , WS.pendingOnAccept = \_ -> return ()
117+ , WS.pendingStream = stream
118+ }
wai-websockets/README.mdnew file+3
@@ -0,0 +1,3 @@
1+## wai-websockets
2+
3+Use websockets with WAI applications, primarily those hosted via Warp.
wai-websockets/server.lhs+54−38
@@ -8,16 +8,17 @@ this example, keep the [reference](http://jaspervdj.be/websockets/reference)
8nearby to check out the functions we use.
8nearby to check out the functions we use.
9
9
10> {-# LANGUAGE OverloadedStrings, TemplateHaskell #-}
10> {-# LANGUAGE OverloadedStrings, TemplateHaskell #-}
11+> {-# LANGUAGE CPP #-}
12+> {-# OPTIONS_GHC -Wno-unused-top-binds #-} -- for numClients
11> import Data.Char (isPunctuation, isSpace)
13> import Data.Char (isPunctuation, isSpace)
12> import Data.Monoid (mappend)
14> import Data.Monoid (mappend)
13> import Data.Text (Text)
15> import Data.Text (Text)
14−> import Control.Exception (fromException, handle)
16+> import Control.Exception (finally)
15> import Control.Monad (forM_, forever)
17> import Control.Monad (forM_, forever)
16−> import Control.Concurrent (MVar, newMVar, modifyMVar_, readMVar)
18+> import Control.Concurrent (MVar, newMVar, modifyMVar_, readMVar, modifyMVar)
17> import Control.Monad.IO.Class (liftIO)
19> import Control.Monad.IO.Class (liftIO)
18> import qualified Data.Text as T
20> import qualified Data.Text as T
19> import qualified Data.Text.IO as T
21> import qualified Data.Text.IO as T
20−
21> import qualified Network.WebSockets as WS
22> import qualified Network.WebSockets as WS
22> import qualified Network.Wai
23> import qualified Network.Wai
23> import qualified Network.Wai.Handler.Warp as Warp
24> import qualified Network.Wai.Handler.Warp as Warp
@@ -25,7 +26,9 @@ nearby to check out the functions we use.
25> import qualified Network.Wai.Application.Static as Static
26> import qualified Network.Wai.Application.Static as Static
26> import Data.FileEmbed (embedDir)
27> import Data.FileEmbed (embedDir)
27
28
29+
28−We represent a client by his username and a 'WS.Connection'. We will see how we obtain this 'WS.Connection' later on.
30+We represent a client by their username and a `WS.Connection`. We will see how we
31+obtain this `WS.Connection` later on.
29
32
30> type Client = (Text, WS.Connection)
33> type Client = (Text, WS.Connection)
31
34
@@ -35,33 +38,33 @@ later on.
35
38
36> type ServerState = [Client]
39> type ServerState = [Client]
37
40
38−Create a new, initial state
41+Create a new, initial state:
39
42
40> newServerState :: ServerState
43> newServerState :: ServerState
41> newServerState = []
44> newServerState = []
42
45
43−Get the number of active clients
46+Get the number of active clients:
44
47
45> numClients :: ServerState -> Int
48> numClients :: ServerState -> Int
46> numClients = length
49> numClients = length
47
50
48−Check if a user already exists (based on username)
51+Check if a user already exists (based on username):
49
52
50> clientExists :: Client -> ServerState -> Bool
53> clientExists :: Client -> ServerState -> Bool
51> clientExists client = any ((== fst client) . fst)
54> clientExists client = any ((== fst client) . fst)
52
55
53−Add a client (first, you should verify the client is not already connected using
56+Add a client (this does not check if the client already exists, you should do
54−'clientExists')
57+this yourself using `clientExists`):
55
58
56> addClient :: Client -> ServerState -> ServerState
59> addClient :: Client -> ServerState -> ServerState
57> addClient client clients = client : clients
60> addClient client clients = client : clients
58
61
59−Remove a client
62+Remove a client:
60
63
61> removeClient :: Client -> ServerState -> ServerState
64> removeClient :: Client -> ServerState -> ServerState
62> removeClient client = filter ((/= fst client) . fst)
65> removeClient client = filter ((/= fst client) . fst)
63
66
64−Send a message to all clients, and log it on stdout.
67+Send a message to all clients, and log it on stdout:
65
68
66> broadcast :: Text -> ServerState -> IO ()
69> broadcast :: Text -> ServerState -> IO ()
67> broadcast message clients = do
70> broadcast message clients = do
@@ -69,39 +72,54 @@ Send a message to all clients, and log it on stdout.
69> forM_ clients $ \(_, conn) -> WS.sendTextData conn message
72> forM_ clients $ \(_, conn) -> WS.sendTextData conn message
70
73
71The main function first creates a new state for the server, then spawns the
74The main function first creates a new state for the server, then spawns the
72−actual server. For this purpose, we use the simple server provided by
75+actual server.
73−'WS.runServer'.
74
76
75> main :: IO ()
77> main :: IO ()
76> main = do
78> main = do
77> putStrLn "http://localhost:9160/client.html"
79> putStrLn "http://localhost:9160/client.html"
78> state <- newMVar newServerState
80> state <- newMVar newServerState
79−> Warp.runSettings Warp.defaultSettings
81+> Warp.runSettings
80−> { Warp.settingsPort = 9160
82+> (Warp.setPort 9160 Warp.defaultSettings)
81−> } $ WaiWS.websocketsOr WS.defaultConnectionOptions (application state) staticApp
83+> $ WaiWS.websocketsOr WS.defaultConnectionOptions (application state) staticApp
82
84
83> staticApp :: Network.Wai.Application
85> staticApp :: Network.Wai.Application
84> staticApp = Static.staticApp $ Static.embeddedSettings $(embedDir "static")
86> staticApp = Static.staticApp $ Static.embeddedSettings $(embedDir "static")
85
87
86−When a client connects, we accept the connection, regardless of the path.
88+Our main application has the type:
87
89
88> application :: MVar ServerState -> WS.ServerApp
90> application :: MVar ServerState -> WS.ServerApp
91+
92+Note that `WS.ServerApp` is nothing but a type synonym for
93+`WS.PendingConnection -> IO ()`.
94+
95+Our application starts by accepting the connection. In a more realistic
96+application, you probably want to check the path and headers provided by the
97+pending request.
98+
99+We also fork a pinging thread in the background. This will ensure the connection
100+stays alive on some browsers.
101+
89> application state pending = do
102> application state pending = do
90> conn <- WS.acceptRequest pending
103> conn <- WS.acceptRequest pending
104+#if __GLASGOW_HASKELL__ >= 806
105+> WS.withPingThread conn 30 (return ()) $ do
106+#else
107+> WS.forkPingThread conn 30
108+#endif
91
109
92−When a client is succesfully connected, we read the first message. This should
110+When a client is successfully connected, we read the first message. This should
93−be in the format of "Hi, I am Jasper", where Jasper is the requested username.
111+be in the format of "Hi! I am Jasper", where Jasper is the requested username.
94
112
95> msg <- WS.receiveData conn
113> msg <- WS.receiveData conn
96> clients <- liftIO $ readMVar state
114> clients <- liftIO $ readMVar state
97> case msg of
115> case msg of
98
116
99−Check that the first message has the right format
117+Check that the first message has the right format:
100
118
101> _ | not (prefix `T.isPrefixOf` msg) ->
119> _ | not (prefix `T.isPrefixOf` msg) ->
102> WS.sendTextData conn ("Wrong announcement" :: Text)
120> WS.sendTextData conn ("Wrong announcement" :: Text)
103
121
104−Check the validity of the username
122+Check the validity of the username:
105
123
106> | any ($ fst client)
124> | any ($ fst client)
107> [T.null, T.any isPunctuation, T.any isSpace] ->
125> [T.null, T.any isPunctuation, T.any isSpace] ->
@@ -109,14 +127,15 @@ Check the validity of the username
109> "contain punctuation or whitespace, and " `mappend`
127> "contain punctuation or whitespace, and " `mappend`
110> "cannot be empty" :: Text)
128> "cannot be empty" :: Text)
111
129
112−Check that the given username is not already taken
130+Check that the given username is not already taken:
113
131
114> | clientExists client clients ->
132> | clientExists client clients ->
115> WS.sendTextData conn ("User already exists" :: Text)
133> WS.sendTextData conn ("User already exists" :: Text)
116
134
117−All is right!
135+All is right! We're going to allow the client, but for safety reasons we *first*
136+setup a `disconnect` function that will be run when the exception is closed.
118
137
119−> | otherwise -> do
138+> | otherwise -> flip finally disconnect $ do
120
139
121We send a "Welcome!", according to our own little protocol. We add the client to
140We send a "Welcome!", according to our own little protocol. We add the client to
122the list and broadcast the fact that he has joined. Then, we give control to the
141the list and broadcast the fact that he has joined. Then, we give control to the
@@ -131,22 +150,19 @@ the list and broadcast the fact that he has joined. Then, we give control to the
131> return s'
150> return s'
132> talk conn state client
151> talk conn state client
133> where
152> where
134−> prefix = "Hi! I am "
153+> prefix = "Hi! I am "
135−> client = (T.drop (T.length prefix) msg, conn)
154+> client = (T.drop (T.length prefix) msg, conn)
155+> disconnect = do
156+> -- Remove client and return new state
157+> s <- modifyMVar state $ \s ->
158+> let s' = removeClient client s in return (s', s')
159+> broadcast (fst client `mappend` " disconnected") s
136
160
137The talk function continues to read messages from a single client until he
161The talk function continues to read messages from a single client until he
138disconnects. All messages are broadcasted to the other clients.
162disconnects. All messages are broadcasted to the other clients.
139
163
140> talk :: WS.Connection -> MVar ServerState -> Client -> IO ()
164> talk :: WS.Connection -> MVar ServerState -> Client -> IO ()
141−> talk conn state client@(user, _) = handle catchDisconnect $
165+> talk conn state (user, _) = forever $ do
142−> forever $ do
143−> msg <- WS.receiveData conn
166+> msg <- WS.receiveData conn
144−> liftIO $ readMVar state >>= broadcast
167+> liftIO $ readMVar state >>= broadcast
145−> (user `mappend` ": " `mappend` msg)
168+> (user `mappend` ": " `mappend` msg)
146−> where
147−> catchDisconnect e = case fromException e of
148−> Just WS.ConnectionClosed -> liftIO $ modifyMVar_ state $ \s -> do
149−> let s' = removeClient client s
150−> broadcast (user `mappend` " disconnected") s'
151−> return s'
152−> _ -> return ()
wai-websockets/wai-websockets.cabal+11−15
@@ -1,6 +1,6 @@
1Name: wai-websockets
1Name: wai-websockets
2−Version: 2.1.0.1
2+Version: 3.0.1.2
3−Synopsis: Provide a bridge betweeen WAI and the websockets package.
3+Synopsis: Provide a bridge between WAI and the websockets package.
4License: MIT
4License: MIT
5License-file: LICENSE
5License-file: LICENSE
6Author: Michael Snoyman, Jasper Van der Jeugt, Ting-Yen Lai
6Author: Michael Snoyman, Jasper Van der Jeugt, Ting-Yen Lai
@@ -10,23 +10,21 @@ Category: Web, Yesod
10Build-Type: Simple
10Build-Type: Simple
11Cabal-Version: >=1.8
11Cabal-Version: >=1.8
12Stability: Stable
12Stability: Stable
13−Description: This is primarily intended for use with Warp and its settingsIntercept.
13+description: API docs and the README are available at <http://www.stackage.org/package/wai-websockets>.
14
14
15extra-source-files: static/client.js, static/client.html, static/screen.css
15extra-source-files: static/client.js, static/client.html, static/screen.css
16+ README.md ChangeLog.md
16
17
17flag example
18flag example
18
19
19Library
20Library
20 Build-Depends: base >= 3 && < 5
21 Build-Depends: base >= 3 && < 5
21 , bytestring >= 0.9.1.4
22 , bytestring >= 0.9.1.4
22− , conduit >= 0.5 && < 1.2
23− , wai >= 2.1 && < 2.2
23+ , wai >= 3.0 && < 3.3
24− , blaze-builder >= 0.2.1.4 && < 0.4
25 , case-insensitive >= 0.2
24 , case-insensitive >= 0.2
26 , network >= 2.2.1.5
25 , network >= 2.2.1.5
27− , transformers >= 0.2 && < 0.4
26+ , transformers >= 0.2
28− , websockets >= 0.8
27+ , websockets >= 0.9
29− , io-streams >= 1.1 && < 1.2
30 , http-types
28 , http-types
31 Exposed-modules: Network.Wai.Handler.WebSockets
29 Exposed-modules: Network.Wai.Handler.WebSockets
32 ghc-options: -Wall
30 ghc-options: -Wall
@@ -34,10 +32,7 @@ Library
34Executable wai-websockets-example
32Executable wai-websockets-example
35 if flag(example)
33 if flag(example)
36 buildable: True
34 buildable: True
37− else
38− buildable: False
39− Build-Depends: base >= 3 && < 5
35+ Build-Depends: base >= 3 && < 5
40− , conduit
41 , wai-websockets
36 , wai-websockets
42 , websockets
37 , websockets
43 , warp
38 , warp
@@ -45,13 +40,14 @@ Executable wai-websockets-example
45 , wai-app-static
40 , wai-app-static
46 , bytestring
41 , bytestring
47 , case-insensitive
42 , case-insensitive
48− , blaze-builder
49 , transformers
43 , transformers
50 , network
44 , network
51 , text
45 , text
52 , file-embed
46 , file-embed
53− , io-streams
54 , http-types
47 , http-types
48+ other-modules: Network.Wai.Handler.WebSockets
49+ else
50+ buildable: False
55
51
56 ghc-options: -Wall -threaded
52 ghc-options: -Wall -threaded
57 main-is: server.lhs
53 main-is: server.lhs
wai/ChangeLog.mdnew file+56
@@ -0,0 +1,56 @@
1+# ChangeLog for wai
2+
3+## 3.2.2.1
4+
5+* Fix missing reexport of `getRequestBodyChunk` [#753](https://github.com/yesodweb/wai/issues/753)
6+
7+## 3.2.2
8+
9+* Deprecate `requestBody` in favor of the more clearly named `getRequestBodyChunk`. [#726](https://github.com/yesodweb/wai/pull/726)
10+
11+## 3.2.1.2
12+
13+* Remove dependency on blaze-builder [#683](https://github.com/yesodweb/wai/pull/683)
14+
15+## 3.2.1.1
16+
17+* Relax upper bound on bytestring-builder
18+
19+## 3.2.1
20+
21+* add mapResponseStatus [#532](https://github.com/yesodweb/wai/pull/532)
22+
23+## 3.2.0.1
24+
25+* Add missing changelog entry
26+
27+## 3.2.0
28+
29+* Major version up due to breaking changes. We chose 3.2.0, not 3.1.0
30+ for consistency with Warp 3.2.0.
31+* The Network.Wai.HTTP2 module was removed.
32+* tryGetFileSize, hContentRange, hAcceptRanges, contentRangeHeader and
33+ chooseFilePart, adjustForFilePart and parseByteRanges were removed
34+ from the Network.Wai.Internal module.
35+* New fields for Request: requestHeaderReferer and requestHeaderUserAgent.
36+
37+## 3.0.5.0
38+
39+* Avoid using the IsString Builder instance
40+
41+## 3.0.4.0
42+
43+* A new module Network.Wai.HTTP2 is exported.
44+
45+## 3.0.3.0
46+
47+* mapResponseHeaders, ifRequest and modifyResponse are exported.
48+
49+## 3.0.2.3
50+
51+* Allow blaze-builder 0.4
52+
53+## 3.0.2.2
54+
55+* Clarify some documentation on `rawPathInfo`. [Relevant Github
56+ discussion](https://github.com/yesodweb/wai/issues/325#issuecomment-69896780).
wai/Network/Wai.hs+179−83
@@ -1,15 +1,14 @@
1−{-# LANGUAGE Rank2Types #-}
2−{-# LANGUAGE CPP #-}
3{-|
1{-|
4
2
5This module defines a generic web application interface. It is a common
3This module defines a generic web application interface. It is a common
6protocol between web servers and web applications.
4protocol between web servers and web applications.
7
5
8The overriding design principles here are performance and generality. To
6The overriding design principles here are performance and generality. To
9−address performance, this library is built on top of the conduit and
7+address performance, this library uses a streaming interface for request and
10−blaze-builder packages. The advantages of conduits over lazy IO have been
8+response bodies, paired with bytestring's 'Builder' type. The advantages of a
9+streaming API over lazy IO have been debated elsewhere and so will not be
11−debated elsewhere and so will not be addressed here. However, helper functions
10+addressed here. However, helper functions like 'responseLBS' allow you to
12−like 'responseLBS' allow you to continue using lazy IO if you so desire.
11+continue using lazy IO if you so desire.
13
12
14Generality is achieved by removing many variables commonly found in similar
13Generality is achieved by removing many variables commonly found in similar
15projects that are not universal to all servers. The goal is that the 'Request'
14projects that are not universal to all servers. The goal is that the 'Request'
@@ -31,14 +30,15 @@ include:
31
30
32[wai-extra] <http://hackage.haskell.org/package/wai-extra>
31[wai-extra] <http://hackage.haskell.org/package/wai-extra>
33
32
34−[wai-test] <http://hackage.haskell.org/package/wai-test>
35−
36-}
33-}
34+-- Ignore deprecations, because this module needs to use the deprecated requestBody to construct a response.
35+{-# OPTIONS_GHC -fno-warn-deprecations #-}
37module Network.Wai
36module Network.Wai
38 (
37 (
39 -- * Types
38 -- * Types
40 Application
39 Application
41 , Middleware
40 , Middleware
41+ , ResponseReceived
42 -- * Request
42 -- * Request
43 , Request
43 , Request
44 , defaultRequest
44 , defaultRequest
@@ -53,44 +53,52 @@ module Network.Wai
53 , remoteHost
53 , remoteHost
54 , pathInfo
54 , pathInfo
55 , queryString
55 , queryString
56+ , getRequestBodyChunk
56 , requestBody
57 , requestBody
57 , vault
58 , vault
58 , requestBodyLength
59 , requestBodyLength
59 , requestHeaderHost
60 , requestHeaderHost
60 , requestHeaderRange
61 , requestHeaderRange
62+ , requestHeaderReferer
63+ , requestHeaderUserAgent
64+ , strictRequestBody
61 , lazyRequestBody
65 , lazyRequestBody
62 -- * Response
66 -- * Response
63 , Response
67 , Response
68+ , StreamingBody
64 , FilePart (..)
69 , FilePart (..)
65− , WithSource
66 -- ** Response composers
70 -- ** Response composers
67 , responseFile
71 , responseFile
68 , responseBuilder
72 , responseBuilder
69 , responseLBS
73 , responseLBS
70− , responseSource
74+ , responseStream
71− , responseSourceBracket
72 , responseRaw
75 , responseRaw
73− -- * Response accessors
76+ -- ** Response accessors
74 , responseStatus
77 , responseStatus
75 , responseHeaders
78 , responseHeaders
76− , responseToSource
79+ -- ** Response modifiers
80+ , responseToStream
81+ , mapResponseHeaders
82+ , mapResponseStatus
83+ -- * Middleware composition
84+ , ifRequest
85+ , modifyResponse
77 ) where
86 ) where
78
87
79−import Blaze.ByteString.Builder (Builder, fromLazyByteString)
88+import Data.ByteString.Builder (Builder, lazyByteString)
80−import Blaze.ByteString.Builder (fromByteString)
89+import Data.ByteString.Builder (byteString)
81−import Control.Exception (bracket, bracketOnError)
90+import Control.Monad (unless)
82import qualified Data.ByteString as B
91import qualified Data.ByteString as B
83import qualified Data.ByteString.Lazy as L
92import qualified Data.ByteString.Lazy as L
93+import qualified Data.ByteString.Lazy.Internal as LI
94+import Data.ByteString.Lazy.Internal (defaultChunkSize)
84import Data.ByteString.Lazy.Char8 ()
95import Data.ByteString.Lazy.Char8 ()
85−import qualified Data.Conduit as C
96+import Data.Function (fix)
86−import qualified Data.Conduit.Binary as CB
87−import Data.Conduit.Lazy (lazyConsume)
88−import qualified Data.Conduit.List as CL
89−import Data.Monoid (mempty)
90import qualified Network.HTTP.Types as H
97import qualified Network.HTTP.Types as H
91import Network.Socket (SockAddr (SockAddrInet))
98import Network.Socket (SockAddr (SockAddrInet))
92import Network.Wai.Internal
99import Network.Wai.Internal
93import qualified System.IO as IO
100import qualified System.IO as IO
101+import System.IO.Unsafe (unsafeInterleaveIO)
94
102
95----------------------------------------------------------------
103----------------------------------------------------------------
96
104
@@ -113,13 +121,13 @@ responseFile = ResponseFile
113--
121--
114-- A2. No. If the ByteStrings are small, then they will be copied into a larger
122-- A2. No. If the ByteStrings are small, then they will be copied into a larger
115-- buffer, which should be a performance gain overall (less system calls). If
123-- buffer, which should be a performance gain overall (less system calls). If
116−-- they are already large, then blaze-builder uses an InsertByteString
124+-- they are already large, then an insert operation is used
117−-- instruction to avoid copying.
125+-- to avoid copying.
118--
126--
119-- Q3. Doesn't this prevent us from creating comet-style servers, since data
127-- Q3. Doesn't this prevent us from creating comet-style servers, since data
120-- will be cached?
128-- will be cached?
121--
129--
122−-- A3. You can force blaze-builder to output a ByteString before it is an
130+-- A3. You can force a Builder to output a ByteString before it is an
123-- optimal size by sending a flush command.
131-- optimal size by sending a flush command.
124responseBuilder :: H.Status -> H.ResponseHeaders -> Builder -> Response
132responseBuilder :: H.Status -> H.ResponseHeaders -> Builder -> Response
125responseBuilder = ResponseBuilder
133responseBuilder = ResponseBuilder
@@ -127,32 +135,35 @@ responseBuilder = ResponseBuilder
127-- | Creating 'Response' from 'L.ByteString'. This is a wrapper for
135-- | Creating 'Response' from 'L.ByteString'. This is a wrapper for
128-- 'responseBuilder'.
136-- 'responseBuilder'.
129responseLBS :: H.Status -> H.ResponseHeaders -> L.ByteString -> Response
137responseLBS :: H.Status -> H.ResponseHeaders -> L.ByteString -> Response
130−responseLBS s h = ResponseBuilder s h . fromLazyByteString
138+responseLBS s h = ResponseBuilder s h . lazyByteString
131−
132−-- | Creating 'Response' from 'C.Source'.
133−responseSource :: H.Status -> H.ResponseHeaders -> C.Source IO (C.Flush Builder) -> Response
134−responseSource st hs src = ResponseSource st hs ($ src)
135
139
136−-- | Creating 'Response' with allocated resource safely released.
140+-- | Creating 'Response' from a stream of values.
141+--
142+-- In order to allocate resources in an exception-safe manner, you can use the
143+-- @bracket@ pattern outside of the call to @responseStream@. As a trivial
144+-- example:
137--
145--
138−-- * The first argument is an action to allocate resource.
146+-- @
147+-- app :: Application
148+-- app req respond = bracket_
149+-- (putStrLn \"Allocating scarce resource\")
150+-- (putStrLn \"Cleaning up\")
151+-- $ respond $ responseStream status200 [] $ \\write flush -> do
152+-- write $ byteString \"Hello\\n\"
153+-- flush
154+-- write $ byteString \"World\\n\"
155+-- @
139--
156--
140−-- * The second argument is a function to release the resource.
157+-- Note that in some cases you can use @bracket@ from inside @responseStream@
158+-- as well. However, placing the call on the outside allows your status value
159+-- and response headers to depend on the scarce resource.
141--
160--
142−-- * The third argument is a function to create
161+-- Since 3.0.0
143−-- ('H.Status','H.ResponseHeaders','C.Source' 'IO' ('C.Flush' 'Builder'))
144−-- from the resource.
145−responseSourceBracket :: IO a
146− -> (a -> IO ())
147− -> (a -> IO (H.Status
162+responseStream :: H.Status
148− ,H.ResponseHeaders
163+ -> H.ResponseHeaders
149− ,C.Source IO (C.Flush Builder)))
164+ -> StreamingBody
150− -> IO Response
165+ -> Response
151−responseSourceBracket setup teardown action =
166+responseStream = ResponseStream
152− bracketOnError setup teardown $ \resource -> do
153− (st,hdr,src) <- action resource
154− return $ ResponseSource st hdr $ \f ->
155− bracket (return resource) teardown (\_ -> f src)
156
167
157-- | Create a response for a raw application. This is useful for \"upgrade\"
168-- | Create a response for a raw application. This is useful for \"upgrade\"
158-- situations such as WebSockets, where an application requests for the server
169-- situations such as WebSockets, where an application requests for the server
@@ -165,10 +176,10 @@ responseSourceBracket setup teardown action =
165-- @responseRaw@, behavior is undefined.
176-- @responseRaw@, behavior is undefined.
166--
177--
167-- Since 2.1.0
178-- Since 2.1.0
168−responseRaw :: (C.Source IO B.ByteString -> C.Sink B.ByteString IO () -> IO ())
179+responseRaw :: (IO B.ByteString -> (B.ByteString -> IO ()) -> IO ())
169 -> Response
180 -> Response
170 -> Response
181 -> Response
171−responseRaw rawApp fallback = ResponseRaw ($ rawApp) fallback
182+responseRaw = ResponseRaw
172
183
173----------------------------------------------------------------
184----------------------------------------------------------------
174
185
@@ -176,52 +187,82 @@ responseRaw rawApp fallback = ResponseRaw ($ rawApp) fallback
176responseStatus :: Response -> H.Status
187responseStatus :: Response -> H.Status
177responseStatus (ResponseFile s _ _ _) = s
188responseStatus (ResponseFile s _ _ _) = s
178responseStatus (ResponseBuilder s _ _ ) = s
189responseStatus (ResponseBuilder s _ _ ) = s
179−responseStatus (ResponseSource s _ _ ) = s
190+responseStatus (ResponseStream s _ _ ) = s
180responseStatus (ResponseRaw _ res ) = responseStatus res
191responseStatus (ResponseRaw _ res ) = responseStatus res
181
192
182−-- | Accessing 'H.Status' in 'Response'.
193+-- | Accessing 'H.ResponseHeaders' in 'Response'.
183responseHeaders :: Response -> H.ResponseHeaders
194responseHeaders :: Response -> H.ResponseHeaders
184responseHeaders (ResponseFile _ hs _ _) = hs
195responseHeaders (ResponseFile _ hs _ _) = hs
185responseHeaders (ResponseBuilder _ hs _ ) = hs
196responseHeaders (ResponseBuilder _ hs _ ) = hs
186−responseHeaders (ResponseSource _ hs _ ) = hs
197+responseHeaders (ResponseStream _ hs _ ) = hs
187responseHeaders (ResponseRaw _ res) = responseHeaders res
198responseHeaders (ResponseRaw _ res) = responseHeaders res
188
199
189−-- | Converting the body information in 'Response' to 'Source'.
200+-- | Converting the body information in 'Response' to a 'StreamingBody'.
190−responseToSource :: Response
201+responseToStream :: Response
202+ -> ( H.Status
191− -> (H.Status, H.ResponseHeaders, WithSource IO (C.Flush Builder) b)
203+ , H.ResponseHeaders
192−responseToSource (ResponseSource s h b) = (s, h, b)
204+ , (StreamingBody -> IO a) -> IO a
205+ )
206+responseToStream (ResponseStream s h b) = (s, h, ($ b))
193−responseToSource (ResponseFile s h fp (Just part)) =
207+responseToStream (ResponseFile s h fp (Just part)) =
194− (s, h, \f -> IO.withFile fp IO.ReadMode $ \handle -> f $ sourceFilePart handle part C.$= CL.map (C.Chunk . fromByteString))
208+ ( s
209+ , h
210+ , \withBody -> IO.withBinaryFile fp IO.ReadMode $ \handle -> withBody $ \sendChunk _flush -> do
211+ IO.hSeek handle IO.AbsoluteSeek $ filePartOffset part
212+ let loop remaining | remaining <= 0 = return ()
213+ loop remaining = do
214+ bs <- B.hGetSome handle defaultChunkSize
215+ unless (B.null bs) $ do
216+ let x = B.take remaining bs
217+ sendChunk $ byteString x
218+ loop $ remaining - B.length x
219+ loop $ fromIntegral $ filePartByteCount part
220+ )
195−responseToSource (ResponseFile s h fp Nothing) =
221+responseToStream (ResponseFile s h fp Nothing) =
196− (s, h, \f -> IO.withFile fp IO.ReadMode $ \handle -> f $ CB.sourceHandle handle C.$= CL.map (C.Chunk . fromByteString))
222+ ( s
223+ , h
224+ , \withBody -> IO.withBinaryFile fp IO.ReadMode $ \handle ->
225+ withBody $ \sendChunk _flush -> fix $ \loop -> do
226+ bs <- B.hGetSome handle defaultChunkSize
227+ unless (B.null bs) $ do
228+ sendChunk $ byteString bs
229+ loop
230+ )
197−responseToSource (ResponseBuilder s h b) =
231+responseToStream (ResponseBuilder s h b) =
198− (s, h, ($ CL.sourceList [C.Chunk b]))
232+ (s, h, \withBody -> withBody $ \sendChunk _flush -> sendChunk b)
199−responseToSource (ResponseRaw _ res) = responseToSource res
233+responseToStream (ResponseRaw _ res) = responseToStream res
200−
234+
201−sourceFilePart :: IO.Handle -> FilePart -> C.Source IO B.ByteString
235+-- | Apply the provided function to the response header list of the Response.
202−sourceFilePart handle (FilePart offset count _) =
236+mapResponseHeaders :: (H.ResponseHeaders -> H.ResponseHeaders) -> Response -> Response
203− CB.sourceHandleRange handle (Just offset) (Just count)
237+mapResponseHeaders f (ResponseFile s h b1 b2) = ResponseFile s (f h) b1 b2
238+mapResponseHeaders f (ResponseBuilder s h b) = ResponseBuilder s (f h) b
239+mapResponseHeaders f (ResponseStream s h b) = ResponseStream s (f h) b
240+mapResponseHeaders _ r@(ResponseRaw _ _) = r
241+
242+-- | Apply the provided function to the response status of the Response.
243+mapResponseStatus :: (H.Status -> H.Status) -> Response -> Response
244+mapResponseStatus f (ResponseFile s h b1 b2) = ResponseFile (f s) h b1 b2
245+mapResponseStatus f (ResponseBuilder s h b) = ResponseBuilder (f s) h b
246+mapResponseStatus f (ResponseStream s h b) = ResponseStream (f s) h b
247+mapResponseStatus _ r@(ResponseRaw _ _) = r
204
248
205----------------------------------------------------------------
249----------------------------------------------------------------
206
250
207-- | The WAI application.
251-- | The WAI application.
208−type Application = Request -> IO Response
209−
210−-- | Middleware is a component that sits between the server and application. It
211−-- can do such tasks as GZIP encoding or response caching. What follows is the
212−-- general definition of middleware, though a middleware author should feel
213−-- free to modify this.
214--
252--
215−-- As an example of an alternate type for middleware, suppose you write a
253+-- Note that, since WAI 3.0, this type is structured in continuation passing
216−-- function to load up session information. The session information is simply a
254+-- style to allow for proper safe resource handling. This was handled in the
217−-- string map \[(String, String)\]. A logical type signature for this middleware
255+-- past via other means (e.g., @ResourceT@). As a demonstration:
218−-- might be:
219--
256--
220−-- @ loadSession :: ([(String, String)] -> Application) -> Application @
257+-- @
258+-- app :: Application
259+-- app req respond = bracket_
260+-- (putStrLn \"Allocating scarce resource\")
261+-- (putStrLn \"Cleaning up\")
262+-- (respond $ responseLBS status200 [] \"Hello World\")
221−--
263+-- @
222−-- Here, instead of taking a standard 'Application' as its first argument, the
264+type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
223−-- middleware takes a function which consumes the session information as well.
265+
224−type Middleware = Application -> Application
225
266
226-- | A default, blank request.
267-- | A default, blank request.
227--
268--
@@ -237,16 +278,71 @@ defaultRequest = Request
237 , remoteHost = SockAddrInet 0 0
278 , remoteHost = SockAddrInet 0 0
238 , pathInfo = []
279 , pathInfo = []
239 , queryString = []
280 , queryString = []
240− , requestBody = return ()
281+ , requestBody = return B.empty
241 , vault = mempty
282 , vault = mempty
242 , requestBodyLength = KnownLength 0
283 , requestBodyLength = KnownLength 0
243 , requestHeaderHost = Nothing
284 , requestHeaderHost = Nothing
244 , requestHeaderRange = Nothing
285 , requestHeaderRange = Nothing
286+ , requestHeaderReferer = Nothing
287+ , requestHeaderUserAgent = Nothing
245 }
288 }
246
289
290+
291+-- | Middleware is a component that sits between the server and application. It
292+-- can do such tasks as GZIP encoding or response caching. What follows is the
293+-- general definition of middleware, though a middleware author should feel
294+-- free to modify this.
295+--
296+-- As an example of an alternate type for middleware, suppose you write a
297+-- function to load up session information. The session information is simply a
298+-- string map \[(String, String)\]. A logical type signature for this middleware
299+-- might be:
300+--
301+-- @ loadSession :: ([(String, String)] -> Application) -> Application @
302+--
303+-- Here, instead of taking a standard 'Application' as its first argument, the
304+-- middleware takes a function which consumes the session information as well.
305+type Middleware = Application -> Application
306+
307+
308+-- | apply a function that modifies a response as a 'Middleware'
309+modifyResponse :: (Response -> Response) -> Middleware
310+modifyResponse f app req respond = app req $ respond . f
311+
312+
313+-- | conditionally apply a 'Middleware'
314+ifRequest :: (Request -> Bool) -> Middleware -> Middleware
315+ifRequest rpred middle app req | rpred req = middle app req
316+ | otherwise = app req
317+
318+
319+
320+-- | Get the request body as a lazy ByteString. However, do /not/ use any lazy
321+-- I\/O, instead reading the entire body into memory strictly.
322+--
323+-- Since 3.0.1
324+strictRequestBody :: Request -> IO L.ByteString
325+strictRequestBody req =
326+ loop id
327+ where
328+ loop front = do
329+ bs <- getRequestBodyChunk req
330+ if B.null bs
331+ then return $ front LI.Empty
332+ else loop (front . LI.Chunk bs)
333+
247-- | Get the request body as a lazy ByteString. This uses lazy I\/O under the
334-- | Get the request body as a lazy ByteString. This uses lazy I\/O under the
248-- surface, and therefore all typical warnings regarding lazy I/O apply.
335-- surface, and therefore all typical warnings regarding lazy I/O apply.
249--
336--
250-- Since 1.4.1
337-- Since 1.4.1
251lazyRequestBody :: Request -> IO L.ByteString
338lazyRequestBody :: Request -> IO L.ByteString
252−lazyRequestBody = fmap L.fromChunks . lazyConsume . requestBody
339+lazyRequestBody req =
340+ loop
341+ where
342+ loop = unsafeInterleaveIO $ do
343+ bs <- getRequestBodyChunk req
344+ if B.null bs
345+ then return LI.Empty
346+ else do
347+ bss <- loop
348+ return $ LI.Chunk bs bss
wai/Network/Wai/Internal.hs+82−31
@@ -1,23 +1,23 @@
1{-# OPTIONS_HADDOCK not-home #-}
1{-# OPTIONS_HADDOCK not-home #-}
2−{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
4−{-# LANGUAGE RankNTypes #-}
3+{-# LANGUAGE RecordWildCards #-}
5-- | Internal constructors and helper functions. Note that no guarantees are
4-- | Internal constructors and helper functions. Note that no guarantees are
6-- given for stability of these interfaces.
5-- given for stability of these interfaces.
7module Network.Wai.Internal where
6module Network.Wai.Internal where
8
7
9−import Blaze.ByteString.Builder (Builder)
8+import Data.ByteString.Builder (Builder)
10−import qualified Data.ByteString as B
9+import qualified Data.ByteString as B hiding (pack)
11−import qualified Data.Conduit as C
12import Data.Text (Text)
10import Data.Text (Text)
13import Data.Typeable (Typeable)
11import Data.Typeable (Typeable)
14−import Data.Vault.Lazy (Vault)
12+import Data.Vault.Lazy (Vault)
15import Data.Word (Word64)
13import Data.Word (Word64)
16import qualified Network.HTTP.Types as H
14import qualified Network.HTTP.Types as H
17import Network.Socket (SockAddr)
15import Network.Socket (SockAddr)
16+import Data.List (intercalate)
18
17
19-- | Information on the request sent by the client. This abstracts away the
18-- | Information on the request sent by the client. This abstracts away the
20-- details of the underlying implementation.
19-- details of the underlying implementation.
20+{-# DEPRECATED requestBody "requestBody's name is misleading because it only gets a partial chunk of the body. Use getRequestBodyChunk instead." #-}
21data Request = Request {
21data Request = Request {
22 -- | Request method such as GET.
22 -- | Request method such as GET.
23 requestMethod :: H.Method
23 requestMethod :: H.Method
@@ -27,13 +27,24 @@ data Request = Request {
27 -- depending on backend; in a standalone server setting, this is most likely
27 -- depending on backend; in a standalone server setting, this is most likely
28 -- all information after the domain name. In a CGI application, this would be
28 -- all information after the domain name. In a CGI application, this would be
29 -- the information following the path to the CGI executable itself.
29 -- the information following the path to the CGI executable itself.
30+ --
30− -- Do not modify this raw value- modify pathInfo instead.
31+ -- Middlewares and routing tools should not modify this raw value, as it may
32+ -- be used for such things as creating redirect destinations by applications.
33+ -- Instead, if you are writing a middleware or routing framework, modify the
34+ -- @pathInfo@ instead. This is the approach taken by systems like Yesod
35+ -- subsites.
36+ --
37+ -- /Note/: At the time of writing this documentation, there is at least one
38+ -- system (@Network.Wai.UrlMap@ from @wai-extra@) that does not follow the
39+ -- above recommendation. Therefore, it is recommended that you test the
40+ -- behavior of your application when using @rawPathInfo@ and any form of
41+ -- library that might modify the @Request@.
31 , rawPathInfo :: B.ByteString
42 , rawPathInfo :: B.ByteString
32 -- | If no query string was specified, this should be empty. This value
43 -- | If no query string was specified, this should be empty. This value
33 -- /will/ include the leading question mark.
44 -- /will/ include the leading question mark.
34− -- Do not modify this raw value- modify queryString instead.
45+ -- Do not modify this raw value - modify queryString instead.
35 , rawQueryString :: B.ByteString
46 , rawQueryString :: B.ByteString
36− -- | A list of header (a pair of key and value) in an HTTP request.
47+ -- | A list of headers (a pair of key and value) in an HTTP request.
37 , requestHeaders :: H.RequestHeaders
48 , requestHeaders :: H.RequestHeaders
38 -- | Was this request made over an SSL connection?
49 -- | Was this request made over an SSL connection?
39 --
50 --
@@ -41,18 +52,19 @@ data Request = Request {
41 -- this request over SSL, but rather whether the current connection is SSL.
52 -- this request over SSL, but rather whether the current connection is SSL.
42 -- The distinction lies with reverse proxies. In many cases, the client will
53 -- The distinction lies with reverse proxies. In many cases, the client will
43 -- connect to a load balancer over SSL, but connect to the WAI handler
54 -- connect to a load balancer over SSL, but connect to the WAI handler
44− -- without SSL. In such a case, @isSecure@ will be @False@, but from a user
55+ -- without SSL. In such a case, 'isSecure' will be 'False', but from a user
45 -- perspective, there is a secure connection.
56 -- perspective, there is a secure connection.
46 , isSecure :: Bool
57 , isSecure :: Bool
47 -- | The client\'s host information.
58 -- | The client\'s host information.
48 , remoteHost :: SockAddr
59 , remoteHost :: SockAddr
49− -- | Path info in individual pieces- the url without a hostname/port and
60+ -- | Path info in individual pieces - the URL without a hostname/port and
50 -- without a query string, split on forward slashes.
61 -- without a query string, split on forward slashes.
51 , pathInfo :: [Text]
62 , pathInfo :: [Text]
52− -- | Parsed query string information
63+ -- | Parsed query string information.
53 , queryString :: H.Query
64 , queryString :: H.Query
54− -- | A request body provided as 'Source'.
65+ -- | Get the next chunk of the body. Returns 'B.empty' when the
66+ -- body is fully consumed. Since 3.2.2, this is deprecated in favor of 'getRequestBodyChunk'.
55− , requestBody :: C.Source IO B.ByteString
67+ , requestBody :: IO B.ByteString
56 -- | A location for arbitrary data to be shared by applications and middleware.
68 -- | A location for arbitrary data to be shared by applications and middleware.
57 , vault :: Vault
69 , vault :: Vault
58 -- | The size of the request body. In the case of a chunked request body,
70 -- | The size of the request body. In the case of a chunked request body,
@@ -68,37 +80,65 @@ data Request = Request {
68 --
80 --
69 -- Since 2.0.0
81 -- Since 2.0.0
70 , requestHeaderRange :: Maybe B.ByteString
82 , requestHeaderRange :: Maybe B.ByteString
83+ -- | The value of the Referer header in a HTTP request.
84+ --
85+ -- Since 3.2.0
86+ , requestHeaderReferer :: Maybe B.ByteString
87+ -- | The value of the User-Agent header in a HTTP request.
88+ --
89+ -- Since 3.2.0
90+ , requestHeaderUserAgent :: Maybe B.ByteString
71 }
91 }
72 deriving (Typeable)
92 deriving (Typeable)
73
93
74−-- | The strange structure of the third field or ResponseSource is to allow for
94+-- | Get the next chunk of the body. Returns 'B.empty' when the
75−-- exception-safe resource allocation. As an example:
95+-- body is fully consumed.
76--
96--
77−-- > app :: Application
97+-- @since 3.2.2
78−-- > app _ = return $ ResponseSource status200 [] $ \f -> bracket
98+getRequestBodyChunk :: Request -> IO B.ByteString
79−-- > (putStrLn "Allocation" >> return 5)
99+getRequestBodyChunk = requestBody
80−-- > (\i -> putStrLn $ "Cleaning up: " ++ show i)
100+
81−-- > (\_ -> f $ do
101+instance Show Request where
82−-- > yield $ Chunk $ fromByteString "Hello "
102+ show Request{..} = "Request {" ++ intercalate ", " [a ++ " = " ++ b | (a,b) <- fields] ++ "}"
83−-- > yield $ Chunk $ fromByteString "World!")
103+ where
104+ fields =
105+ [("requestMethod",show requestMethod)
106+ ,("httpVersion",show httpVersion)
107+ ,("rawPathInfo",show rawPathInfo)
108+ ,("rawQueryString",show rawQueryString)
109+ ,("requestHeaders",show requestHeaders)
110+ ,("isSecure",show isSecure)
111+ ,("remoteHost",show remoteHost)
112+ ,("pathInfo",show pathInfo)
113+ ,("queryString",show queryString)
114+ ,("requestBody","<IO ByteString>")
115+ ,("vault","<Vault>")
116+ ,("requestBodyLength",show requestBodyLength)
117+ ,("requestHeaderHost",show requestHeaderHost)
118+ ,("requestHeaderRange",show requestHeaderRange)
119+ ]
120+
121+
84data Response
122data Response
85 = ResponseFile H.Status H.ResponseHeaders FilePath (Maybe FilePart)
123 = ResponseFile H.Status H.ResponseHeaders FilePath (Maybe FilePart)
86 | ResponseBuilder H.Status H.ResponseHeaders Builder
124 | ResponseBuilder H.Status H.ResponseHeaders Builder
87− | ResponseSource H.Status H.ResponseHeaders (forall b. WithSource IO (C.Flush Builder) b)
125+ | ResponseStream H.Status H.ResponseHeaders StreamingBody
88− | ResponseRaw (forall b. WithRawApp b) Response
126+ | ResponseRaw (IO B.ByteString -> (B.ByteString -> IO ()) -> IO ()) Response
89 deriving Typeable
127 deriving Typeable
90
128
91−type RawApp = C.Source IO B.ByteString -> C.Sink B.ByteString IO () -> IO ()
129+-- | Represents a streaming HTTP response body. It's a function of two
92−type WithRawApp b = (RawApp -> IO b) -> IO b
130+-- parameters; the first parameter provides a means of sending another chunk of
93−
131+-- data, and the second parameter provides a means of flushing the data to the
94−-- | Auxiliary type for 'ResponseSource'.
132+-- client.
95−type WithSource m a b = (C.Source m a -> m b) -> m b
133+--
134+-- Since 3.0.0
135+type StreamingBody = (Builder -> IO ()) -> IO () -> IO ()
96
136
97-- | The size of the request body. In the case of chunked bodies, the size will
137-- | The size of the request body. In the case of chunked bodies, the size will
98-- not be known.
138-- not be known.
99--
139--
100-- Since 1.4.0
140-- Since 1.4.0
101−data RequestBodyLength = ChunkedBody | KnownLength Word64
141+data RequestBodyLength = ChunkedBody | KnownLength Word64 deriving Show
102
142
103-- | Information on which part to be sent.
143-- | Information on which part to be sent.
104-- Sophisticated application handles Range (and If-Range) then
144-- Sophisticated application handles Range (and If-Range) then
@@ -108,3 +148,14 @@ data FilePart = FilePart
108 , filePartByteCount :: Integer
148 , filePartByteCount :: Integer
109 , filePartFileSize :: Integer
149 , filePartFileSize :: Integer
110 } deriving Show
150 } deriving Show
151+
152+-- | A special datatype to indicate that the WAI handler has received the
153+-- response. This is to avoid the need for Rank2Types in the definition of
154+-- Application.
155+--
156+-- It is /highly/ advised that only WAI handlers import and use the data
157+-- constructor for this data type.
158+--
159+-- Since 3.0.0
160+data ResponseReceived = ResponseReceived
161+ deriving Typeable
wai/README.md+12−12
@@ -13,12 +13,12 @@ import Network.HTTP.Types
13import Network.Wai.Handler.Warp (run)
13import Network.Wai.Handler.Warp (run)
14
14
15app :: Application
15app :: Application
16−app _ = do
16+app _ respond = do
17− putStrLn "I've done some IO here"
17+ putStrLn "I've done some IO here"
18− return $ responseLBS
18+ respond $ responseLBS
19− status200
19+ status200
20− [("Content-Type", "text/plain")]
20+ [("Content-Type", "text/plain")]
21− "Hello, Web!"
21+ "Hello, Web!"
22
22
23main :: IO ()
23main :: IO ()
24main = do
24main = do
@@ -50,7 +50,7 @@ Now we redefine `responseBody` to refer to that file:
50
50
51~~~ {.haskell}
51~~~ {.haskell}
52app2 :: Application
52app2 :: Application
53−app2 _ = return index
53+app2 _ respond = respond index
54
54
55index :: Response
55index :: Response
56index = responseFile
56index = responseFile
@@ -67,16 +67,16 @@ Basic dispatching
67An `Application` maps `Request`s to `Response`s:
67An `Application` maps `Request`s to `Response`s:
68
68
69 ghci> :info Application
69 ghci> :info Application
70− type Application = Request -> IO Response
70+ type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
71
71
72Depending on the path info provided with each `Request` we can serve different `Response`s:
72Depending on the path info provided with each `Request` we can serve different `Response`s:
73
73
74~~~ {.haskell}
74~~~ {.haskell}
75app3 :: Application
75app3 :: Application
76−app3 request = case rawPathInfo request of
76+app3 request respond = respond $ case rawPathInfo request of
77− "/" -> return index
77+ "/" -> index
78− "/raw/" -> return plainIndex
78+ "/raw/" -> plainIndex
79− _ -> return notFound
79+ _ -> notFound
80
80
81plainIndex :: Response
81plainIndex :: Response
82plainIndex = responseFile
82plainIndex = responseFile
wai/test.hsdeleted−9
@@ -1,9 +0,0 @@
1−import Network.Wai
2−import Data.Enumerator.List (consume)
3−import Blaze.ByteString.Builder (toByteString)
4−import Data.Monoid (mconcat)
5−import Data.ByteString.Char8 (pack)
6−
7−main = do
8− b <- responseEnumerator (ResponseFile undefined undefined "test.hs" (Just $ FilePart 7 7)) (const $ const consume)
9− print $ toByteString (mconcat b) == pack "Network"
wai/test/Network/WaiSpec.hsnew file+82
@@ -0,0 +1,82 @@
1+module Network.WaiSpec (spec) where
2+
3+import Test.Hspec
4+import Test.Hspec.QuickCheck (prop)
5+import Network.Wai
6+import Data.IORef
7+import qualified Data.ByteString as S
8+import qualified Data.ByteString.Lazy as L
9+import Data.ByteString.Builder (Builder, toLazyByteString, word8)
10+import Control.Monad (forM_)
11+
12+spec :: Spec
13+spec = do
14+ describe "responseToStream" $ do
15+ let getBody res = do
16+ let (_, _, f) = responseToStream res
17+ f $ \streamingBody -> do
18+ builderRef <- newIORef mempty
19+ let add :: Builder -> IO ()
20+ add b = atomicModifyIORef builderRef $ \builder ->
21+ (builder `mappend` b, ())
22+ flush :: IO ()
23+ flush = return ()
24+ streamingBody add flush
25+ fmap (L.toStrict . toLazyByteString) $ readIORef builderRef
26+ prop "responseLBS" $ \bytes -> do
27+ body <- getBody $ responseLBS undefined undefined $ L.pack bytes
28+ body `shouldBe` S.pack bytes
29+ prop "responseBuilder" $ \bytes -> do
30+ body <- getBody $ responseBuilder undefined undefined
31+ $ mconcat $ map word8 bytes
32+ body `shouldBe` S.pack bytes
33+ prop "responseStream" $ \chunks -> do
34+ body <- getBody $ responseStream undefined undefined $ \sendChunk _ ->
35+ forM_ chunks $ \chunk -> sendChunk $ mconcat $ map word8 chunk
36+ body `shouldBe` S.concat (map S.pack chunks)
37+ it "responseFile total" $ do
38+ let fp = "wai.cabal"
39+ body <- getBody $ responseFile undefined undefined fp Nothing
40+ expected <- S.readFile fp
41+ body `shouldBe` expected
42+ prop "responseFile partial" $ \offset' count' -> do
43+ let fp = "wai.cabal"
44+ totalBS <- S.readFile fp
45+ let total = S.length totalBS
46+ offset = abs offset' `mod` total
47+ count = abs count' `mod` (total - offset)
48+ body <- getBody $ responseFile undefined undefined fp $ Just FilePart
49+ { filePartOffset = fromIntegral offset
50+ , filePartByteCount = fromIntegral count
51+ , filePartFileSize = fromIntegral total
52+ }
53+ let expected = S.take count $ S.drop offset totalBS
54+ body `shouldBe` expected
55+ describe "lazyRequestBody" $ do
56+ prop "works" $ \chunks -> do
57+ ref <- newIORef $ map S.pack $ filter (not . null) chunks
58+ let req = defaultRequest
59+ { requestBody = atomicModifyIORef ref $ \bss ->
60+ case bss of
61+ [] -> ([], S.empty)
62+ x:y -> (y, x)
63+ }
64+ body <- lazyRequestBody req
65+ body `shouldBe` L.fromChunks (map S.pack chunks)
66+ it "is lazy" $ do
67+ let req = defaultRequest
68+ { requestBody = error "requestBody"
69+ }
70+ _ <- lazyRequestBody req
71+ return ()
72+ describe "strictRequestBody" $ do
73+ prop "works" $ \chunks -> do
74+ ref <- newIORef $ map S.pack $ filter (not . null) chunks
75+ let req = defaultRequest
76+ { requestBody = atomicModifyIORef ref $ \bss ->
77+ case bss of
78+ [] -> ([], S.empty)
79+ x:y -> (y, x)
80+ }
81+ body <- strictRequestBody req
82+ body `shouldBe` L.fromChunks (map S.pack chunks)
wai/test/Spec.hsnew file+1
@@ -0,0 +1 @@
1+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
wai/wai.cabal+24−8
@@ -1,7 +1,9 @@
1Name: wai
1Name: wai
2−Version: 2.1.0.2
2+Version: 3.2.2.1
3Synopsis: Web Application Interface.
3Synopsis: Web Application Interface.
4Description: Provides a common protocol for communication between web applications and web servers.
4Description: Provides a common protocol for communication between web applications and web servers.
5+ .
6+ API docs and the README are available at <http://www.stackage.org/package/wai>.
5License: MIT
7License: MIT
6License-file: LICENSE
8License-file: LICENSE
7Author: Michael Snoyman
9Author: Michael Snoyman
@@ -9,24 +11,38 @@ Maintainer: michael@snoyman.com
9Homepage: https://github.com/yesodweb/wai
11Homepage: https://github.com/yesodweb/wai
10Category: Web
12Category: Web
11Build-Type: Simple
13Build-Type: Simple
12−Cabal-Version: >=1.6
14+Cabal-Version: >=1.8
13Stability: Stable
15Stability: Stable
16+extra-source-files: README.md ChangeLog.md
14
17
15Source-repository head
18Source-repository head
16 type: git
19 type: git
17 location: git://github.com/yesodweb/wai.git
20 location: git://github.com/yesodweb/wai.git
18
21
19Library
22Library
20− Build-Depends: base >= 4 && < 5
23+ Build-Depends: base >= 4.8 && < 5
21− , bytestring >= 0.9.1.4
24+ , bytestring >= 0.10.4
22− , blaze-builder >= 0.2.1.4 && < 0.4
23− , conduit >= 1.0.8 && < 1.2
24− , conduit-extra >= 1.0
25 , network >= 2.2.1.5
25 , network >= 2.2.1.5
26 , http-types >= 0.7
26 , http-types >= 0.7
27 , text >= 0.7
27 , text >= 0.7
28− , transformers >= 0.2.2
29 , vault >= 0.3 && < 0.4
28 , vault >= 0.3 && < 0.4
30 Exposed-modules: Network.Wai
29 Exposed-modules: Network.Wai
31 Network.Wai.Internal
30 Network.Wai.Internal
32 ghc-options: -Wall
31 ghc-options: -Wall
32+
33+test-suite test
34+ hs-source-dirs: test
35+ main-is: Spec.hs
36+ type: exitcode-stdio-1.0
37+ ghc-options: -threaded -Wall
38+ cpp-options: -DTEST
39+ build-depends: base >= 4.8 && < 5
40+ , wai
41+ , hspec
42+ , bytestring
43+ other-modules: Network.WaiSpec
44+ build-tool-depends: hspec-discover:hspec-discover
45+
46+source-repository head
47+ type: git
48+ location: git://github.com/yesodweb/wai.git
wai/webkit-sample/webkit-sample.hs+2−2
@@ -5,7 +5,7 @@ import Network.Wai.Handler.Webkit
5import Data.ByteString (ByteString)
5import Data.ByteString (ByteString)
6import qualified Data.ByteString.Lazy as L
6import qualified Data.ByteString.Lazy as L
7import Data.Enumerator (consume, Iteratee)
7import Data.Enumerator (consume, Iteratee)
8−import Blaze.ByteString.Builder (fromLazyByteString)
8+import Data.ByteString.Builder (lazyByteString)
9
9
10main :: IO ()
10main :: IO ()
11main = putStrLn "http://localhost:3000/" >> run "Webkit Sample" app
11main = putStrLn "http://localhost:3000/" >> run "Webkit Sample" app
@@ -27,4 +27,4 @@ postResponse :: L.ByteString -> Iteratee ByteString IO Response
27postResponse lbs = return $ ResponseBuilder
27postResponse lbs = return $ ResponseBuilder
28 status200
28 status200
29 [("Content-Type", "text/plain")]
29 [("Content-Type", "text/plain")]
30− (fromLazyByteString lbs)
30+ (lazyByteString lbs)
warp-tls/ChangeLog.mdnew file+117
@@ -0,0 +1,117 @@
1+## 3.2.11
2+
3+* Ignoring an exception from shutdown (gracefulClose).
4+
5+## 3.2.10
6+
7+* Passing client certificate, if any, to Warp
8+ [#783](https://github.com/yesodweb/wai/pull/783)
9+
10+## 3.2.9
11+
12+* Cooperating setGracefulCloseTimeout1 and setGracefulCloseTimeout2 of Warp.
13+ [#782](https://github.com/yesodweb/wai/pull/782)
14+
15+## 3.2.8
16+
17+* Using gracefullClose of network 3.1.1 or later if available.
18+
19+## 3.2.7
20+
21+* Relaxing version constraint.
22+
23+## 3.2.6
24+
25+* Using the Strict and StrictData language extensions for GHC >8.
26+ [#752](https://github.com/yesodweb/wai/pull/752)
27+
28+## 3.2.5
29+
30+* When tls 1.5.0 is available, TLS 1.3 is automatically supported.
31+
32+## 3.2.4.3
33+
34+* Using warp >= 3.2.17.
35+
36+## 3.2.4.2
37+
38+* Ignore socket errors while sending `close_notify` [#640](https://github.com/yesodweb/wai/issues/640)
39+
40+## 3.2.4
41+
42+* Using tls-session-manager.
43+
44+## 3.2.3
45+
46+* Stop using obsoleted APIs of network.
47+
48+## 3.2.2
49+
50+* New settting parameter: tlsServerDHEParams [#556](https://github.com/yesodweb/wai/pull/556)
51+* Preventing socket leakage [#559](https://github.com/yesodweb/wai/pull/559)
52+
53+## 3.2.1
54+
55+* Removing dependency to cprng-aes.
56+
57+## 3.2.0
58+
59+* Major version up due to breaking changes.
60+* runHTTP2TLS and runHTTP2TLSSocket were removed.
61+
62+## 3.1.4
63+
64+* Add an option to disable HTTP2 [#450](https://github.com/yesodweb/wai/pull/450)
65+
66+## 3.1.3
67+
68+* Removing SHA 512 and SHA 384 from supportedCiphers to rescue Safari and golang. [#429](https://github.com/yesodweb/wai/issues/429)
69+
70+## 3.1.2
71+
72+* [Getting Rating A from the SSL Server Test](http://www.yesodweb.com/blog/2015/08/ssl-server-test)
73+
74+## 3.1.1
75+
76+* Converting "send: resource vanished (Broken pipe)" to ConnectionClosedByPeer. [#421](https://github.com/yesodweb/wai/issues/421)
77+
78+## 3.1.0
79+
80+* Supporting HTTP/2 [#399](https://github.com/yesodweb/wai/pull/399)
81+* Removing RC4 [#400](https://github.com/yesodweb/wai/issues/400)
82+
83+## 3.0.4.2
84+
85+* tls 1.3 support [#390](https://github.com/yesodweb/wai/issues/390)
86+
87+## 3.0.4.1
88+
89+* Fix for leaked FDs [#378](https://github.com/yesodweb/wai/issues/378)
90+
91+## 3.0.4
92+
93+* Replace `acceptSafe` with `accept`, see [#361](https://github.com/yesodweb/wai/issues/361)
94+
95+## 3.0.3
96+
97+* Support chain certs [#349](https://github.com/yesodweb/wai/pull/349)
98+
99+## 3.0.2
100+
101+* Allow warp-tls to request client certificates. [#337](https://github.com/yesodweb/wai/pull/337)
102+
103+## 3.0.1.4
104+
105+Add additional Diffie-Hellman RSA and DSA ciphers to warp-tls.
106+
107+## 3.0.1.3
108+
109+[Unable to allow insecure connections with warp-tls #324](https://github.com/yesodweb/wai/issues/324)
110+
111+## 3.0.1.2
112+
113+[Make sure Timer is tickled in sendfile. #323](https://github.com/yesodweb/wai/pull/323)
114+
115+## 3.0.1
116+
117+[Support for in-memory certificates and keys](https://github.com/yesodweb/wai/issues/301)
warp-tls/Network/Wai/Handler/WarpTLS.hs+484−165
@@ -3,210 +3,529 @@
3{-# LANGUAGE RankNTypes #-}
3{-# LANGUAGE RankNTypes #-}
4{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE DeriveDataTypeable #-}
5{-# LANGUAGE DeriveDataTypeable #-}
6+{-# LANGUAGE PatternGuards #-}
6
7
7−-- | HTTP over SSL/TLS support for Warp via the TLS package.
8+-- | HTTP over TLS support for Warp via the TLS package.
9+--
10+-- If HTTP\/2 is negotiated by ALPN, HTTP\/2 over TLS is used.
11+-- Otherwise HTTP\/1.1 over TLS is used.
12+--
13+-- Support for SSL is now obsoleted.
8
14
9module Network.Wai.Handler.WarpTLS (
15module Network.Wai.Handler.WarpTLS (
10 -- * Settings
16 -- * Settings
11 TLSSettings
17 TLSSettings
18+ , defaultTlsSettings
19+ -- * Smart constructors
20+ , tlsSettings
21+ , tlsSettingsMemory
22+ , tlsSettingsChain
23+ , tlsSettingsChainMemory
24+ -- * Accessors
12 , certFile
25 , certFile
13 , keyFile
26 , keyFile
14− , onInsecure
15 , tlsLogging
27 , tlsLogging
16 , tlsAllowedVersions
28 , tlsAllowedVersions
17 , tlsCiphers
29 , tlsCiphers
18− , defaultTlsSettings
30+ , tlsWantClientCert
19− , tlsSettings
31+ , tlsServerHooks
32+ , tlsServerDHEParams
33+ , tlsSessionManagerConfig
34+ , onInsecure
20 , OnInsecure (..)
35 , OnInsecure (..)
21 -- * Runner
36 -- * Runner
22 , runTLS
37 , runTLS
23 , runTLSSocket
38 , runTLSSocket
24 -- * Exception
39 -- * Exception
25 , WarpTLSException (..)
40 , WarpTLSException (..)
41+ , DH.Params
42+ , DH.generateParams
26 ) where
43 ) where
27
44
28−import qualified Network.TLS as TLS
45+import Control.Applicative ((<|>))
29−import Network.Wai.Handler.Warp
46+import Control.Exception (Exception, throwIO, bracket, finally, handle, fromException, try, IOException, onException, SomeException(..), handleJust)
30−import Network.Wai
47+import qualified Control.Exception as E
31−import Network.Socket
48+import Control.Monad (void, guard)
49+import qualified Data.ByteString as S
32import qualified Data.ByteString.Lazy as L
50import qualified Data.ByteString.Lazy as L
33−import Data.Conduit.Binary (sourceFileRange)
51+import Data.Default.Class (def)
34−import Control.Monad.Trans.Resource (runResourceT)
52+import qualified Data.IORef as I
35−import qualified Data.Conduit as C
53+import Data.Streaming.Network (bindPortTCP, safeRecv)
54+import Data.Typeable (Typeable)
55+import Network.Socket (Socket, close, withSocketsDo, SockAddr, accept)
56+#if MIN_VERSION_network(3,1,1)
57+import Network.Socket (gracefulClose)
58+#endif
59+import Network.Socket.ByteString (sendAll)
36−import qualified Data.Conduit.List as CL
60+import qualified Network.TLS as TLS
37−import Control.Exception (bracket, finally, handle)
61+import qualified Crypto.PubKey.DH as DH
38import qualified Network.TLS.Extra as TLSExtra
62import qualified Network.TLS.Extra as TLSExtra
39−import qualified Data.ByteString as B
63+import qualified Network.TLS.SessionManager as SM
40−#if MIN_VERSION_conduit(1,1,0)
64+import Network.Wai (Application)
41−import Data.Streaming.Network (bindPortTCP, acceptSafe)
65+import Network.Wai.Handler.Warp
66+import Network.Wai.Handler.Warp.Internal
67+import System.IO.Error (isEOFError)
68+
69+----------------------------------------------------------------
70+
71+-- | Settings for WarpTLS.
72+data TLSSettings = TLSSettings {
73+ certFile :: FilePath
74+ -- ^ File containing the certificate.
75+ , chainCertFiles :: [FilePath]
76+ -- ^ Files containing chain certificates.
77+ , keyFile :: FilePath
78+ -- ^ File containing the key
79+ , certMemory :: Maybe S.ByteString
80+ , chainCertsMemory :: [S.ByteString]
81+ , keyMemory :: Maybe S.ByteString
82+ , onInsecure :: OnInsecure
83+ -- ^ Do we allow insecure connections with this server as well?
84+ --
85+ -- >>> onInsecure defaultTlsSettings
86+ -- DenyInsecure "This server only accepts secure HTTPS connections."
87+ --
88+ -- Since 1.4.0
89+ , tlsLogging :: TLS.Logging
90+ -- ^ The level of logging to turn on.
91+ --
92+ -- Default: 'TLS.defaultLogging'.
93+ --
94+ -- Since 1.4.0
95+ , tlsAllowedVersions :: [TLS.Version]
96+#if MIN_VERSION_tls(1,5,0)
97+ -- ^ The TLS versions this server accepts.
98+ --
99+ -- >>> tlsAllowedVersions defaultTlsSettings
100+ -- [TLS13,TLS12,TLS11,TLS10]
101+ --
102+ -- Since 1.4.2
42#else
103#else
43−import Data.Conduit.Network (bindPort, acceptSafe)
104+ -- ^ The TLS versions this server accepts.
44−#define bindPortTCP bindPort
105+ --
106+ -- >>> tlsAllowedVersions defaultTlsSettings
107+ -- [TLS12,TLS11,TLS10]
108+ --
109+ -- Since 1.4.2
45#endif
110#endif
46−import Control.Applicative ((<$>))
111+ , tlsCiphers :: [TLS.Cipher]
47−import Data.Conduit.Network (sourceSocket, sinkSocket)
112+#if MIN_VERSION_tls(1,5,0)
48−import Data.Maybe (fromMaybe)
113+ -- ^ The TLS ciphers this server accepts.
49−import qualified Data.IORef as I
114+ --
50−import Control.Exception (Exception, throwIO)
115+ -- >>> tlsCiphers defaultTlsSettings
51−import Data.Typeable (Typeable)
116+ -- [ECDHE-ECDSA-AES256GCM-SHA384,ECDHE-ECDSA-AES128GCM-SHA256,ECDHE-RSA-AES256GCM-SHA384,ECDHE-RSA-AES128GCM-SHA256,DHE-RSA-AES256GCM-SHA384,DHE-RSA-AES128GCM-SHA256,ECDHE-ECDSA-AES256CBC-SHA384,ECDHE-RSA-AES256CBC-SHA384,DHE-RSA-AES256-SHA256,ECDHE-ECDSA-AES256CBC-SHA,ECDHE-RSA-AES256CBC-SHA,DHE-RSA-AES256-SHA1,RSA-AES256GCM-SHA384,RSA-AES256-SHA256,RSA-AES256-SHA1,AES128GCM-SHA256,AES256GCM-SHA384]
52−import Data.Default.Class
117+ --
53−import qualified Data.Conduit.Binary as CB
118+ -- Since 1.4.2
54−import qualified Crypto.Random.AESCtr
119+#else
55−import Network.Wai.Handler.Warp.Buffer
120+ -- ^ The TLS ciphers this server accepts.
56−
121+ --
122+ -- >>> tlsCiphers defaultTlsSettings
123+ -- [ECDHE-ECDSA-AES256GCM-SHA384,ECDHE-ECDSA-AES128GCM-SHA256,ECDHE-RSA-AES256GCM-SHA384,ECDHE-RSA-AES128GCM-SHA256,DHE-RSA-AES256GCM-SHA384,DHE-RSA-AES128GCM-SHA256,ECDHE-ECDSA-AES256CBC-SHA384,ECDHE-RSA-AES256CBC-SHA384,DHE-RSA-AES256-SHA256,ECDHE-ECDSA-AES256CBC-SHA,ECDHE-RSA-AES256CBC-SHA,DHE-RSA-AES256-SHA1,RSA-AES256GCM-SHA384,RSA-AES256-SHA256,RSA-AES256-SHA1]
124+ --
125+ -- Since 1.4.2
126+#endif
127+ , tlsWantClientCert :: Bool
128+ -- ^ Whether or not to demand a certificate from the client. If this
129+ -- is set to True, you must handle received certificates in a server hook
130+ -- or all connections will fail.
131+ --
132+ -- >>> tlsWantClientCert defaultTlsSettings
133+ -- False
134+ --
135+ -- Since 3.0.2
136+ , tlsServerHooks :: TLS.ServerHooks
137+ -- ^ The server-side hooks called by the tls package, including actions
138+ -- to take when a client certificate is received. See the "Network.TLS"
139+ -- module for details.
140+ --
141+ -- Default: def
142+ --
143+ -- Since 3.0.2
144+ , tlsServerDHEParams :: Maybe DH.Params
145+ -- ^ Configuration for ServerDHEParams
146+ -- more function lives in `cryptonite` package
147+ --
148+ -- Default: Nothing
149+ --
150+ -- Since 3.2.2
151+ , tlsSessionManagerConfig :: Maybe SM.Config
152+ -- ^ Configuration for in-memory TLS session manager.
153+ -- If Nothing, 'TLS.noSessionManager' is used.
154+ -- Otherwise, an in-memory TLS session manager is created
155+ -- according to 'Config'.
156+ --
157+ -- Default: Nothing
158+ --
159+ -- Since 3.2.4
160+ }
161+
162+-- | Default 'TLSSettings'. Use this to create 'TLSSettings' with the field record name (aka accessors).
57−data TLSSettings = TLSSettings
163+defaultTlsSettings :: TLSSettings
164+defaultTlsSettings = TLSSettings {
58− { certFile :: FilePath
165+ certFile = "certificate.pem"
59− -- ^ File containing the certificate.
166+ , chainCertFiles = []
60− , keyFile :: FilePath
167+ , keyFile = "key.pem"
61− -- ^ File containing the key
168+ , certMemory = Nothing
62− , onInsecure :: OnInsecure
169+ , chainCertsMemory = []
63− -- ^ Do we allow insecure connections with this server as well? Default
170+ , keyMemory = Nothing
64− -- is a simple text response stating that a secure connection is required.
171+ , onInsecure = DenyInsecure "This server only accepts secure HTTPS connections."
65− --
66− -- Since 1.4.0
67− , tlsLogging :: TLS.Logging
172+ , tlsLogging = def
68− -- ^ The level of logging to turn on.
173+#if MIN_VERSION_tls(1,5,0)
69− --
70− -- Default: 'TLS.defaultLogging'.
71− --
72− -- Since 1.4.0
73− , tlsAllowedVersions :: [TLS.Version]
174+ , tlsAllowedVersions = [TLS.TLS13,TLS.TLS12,TLS.TLS11,TLS.TLS10]
74− -- ^ The TLS versions this server accepts.
175+#else
75− --
176+ , tlsAllowedVersions = [TLS.TLS12,TLS.TLS11,TLS.TLS10]
76− -- Default: '[TLS.SSL3,TLS.TLS10,TLS.TLS11,TLS.TLS12]'.
177+#endif
77− --
178+ , tlsCiphers = ciphers
78− -- Since 1.4.2
179+ , tlsWantClientCert = False
79− , tlsCiphers :: [TLS.Cipher]
180+ , tlsServerHooks = def
80− -- ^ The TLS ciphers this server accepts.
181+ , tlsServerDHEParams = Nothing
81− --
182+ , tlsSessionManagerConfig = Nothing
82− -- Default: '[TLSExtra.cipher_AES128_SHA1, TLSExtra.cipher_AES256_SHA1, TLSExtra.cipher_RC4_128_MD5, TLSExtra.cipher_RC4_128_SHA1]'
183+ }
83− --
184+
84− -- Since 1.4.2
185+-- taken from stunnel example in tls-extra
85− }
186+ciphers :: [TLS.Cipher]
187+ciphers = TLSExtra.ciphersuite_strong
188+
189+----------------------------------------------------------------
86
190
87-- | An action when a plain HTTP comes to HTTP over TLS/SSL port.
191-- | An action when a plain HTTP comes to HTTP over TLS/SSL port.
88data OnInsecure = DenyInsecure L.ByteString
192data OnInsecure = DenyInsecure L.ByteString
89 | AllowInsecure
193 | AllowInsecure
194+ deriving (Show)
195+
196+----------------------------------------------------------------
90
197
91−-- | A smart constructor for 'TLSSettings'.
198+-- | A smart constructor for 'TLSSettings' based on 'defaultTlsSettings'.
92tlsSettings :: FilePath -- ^ Certificate file
199tlsSettings :: FilePath -- ^ Certificate file
93 -> FilePath -- ^ Key file
200 -> FilePath -- ^ Key file
94 -> TLSSettings
201 -> TLSSettings
95−tlsSettings cert key = defaultTlsSettings
202+tlsSettings cert key = defaultTlsSettings {
203+ certFile = cert
204+ , keyFile = key
205+ }
206+
207+-- | A smart constructor for 'TLSSettings' that allows specifying
208+-- chain certificates based on 'defaultTlsSettings'.
209+--
210+-- Since 3.0.3
211+tlsSettingsChain
212+ :: FilePath -- ^ Certificate file
213+ -> [FilePath] -- ^ Chain certificate files
214+ -> FilePath -- ^ Key file
215+ -> TLSSettings
216+tlsSettingsChain cert chainCerts key = defaultTlsSettings {
96− { certFile = cert
217+ certFile = cert
218+ , chainCertFiles = chainCerts
97− , keyFile = key
219+ , keyFile = key
220+ }
221+
222+-- | A smart constructor for 'TLSSettings', but uses in-memory representations
223+-- of the certificate and key based on 'defaultTlsSettings'.
224+--
225+-- Since 3.0.1
226+tlsSettingsMemory
227+ :: S.ByteString -- ^ Certificate bytes
228+ -> S.ByteString -- ^ Key bytes
229+ -> TLSSettings
230+tlsSettingsMemory cert key = defaultTlsSettings
231+ { certMemory = Just cert
232+ , keyMemory = Just key
98 }
233 }
99
234
100−-- | Default 'TLSSettings'. Use this to create 'TLSSettings' with the field record name.
235+-- | A smart constructor for 'TLSSettings', but uses in-memory representations
101−defaultTlsSettings :: TLSSettings
236+-- of the certificate and key based on 'defaultTlsSettings'.
102−defaultTlsSettings = TLSSettings
237+--
103− { certFile = "certificate.pem"
238+-- Since 3.0.3
104− , keyFile = "key.pem"
239+tlsSettingsChainMemory
105− , onInsecure = DenyInsecure "This server only accepts secure HTTPS connections."
240+ :: S.ByteString -- ^ Certificate bytes
106− , tlsLogging = def
241+ -> [S.ByteString] -- ^ Chain certificate bytes
107− , tlsAllowedVersions = [TLS.SSL3,TLS.TLS10,TLS.TLS11,TLS.TLS12]
242+ -> S.ByteString -- ^ Key bytes
108− , tlsCiphers = ciphers
243+ -> TLSSettings
244+tlsSettingsChainMemory cert chainCerts key = defaultTlsSettings
245+ { certMemory = Just cert
246+ , chainCertsMemory = chainCerts
247+ , keyMemory = Just key
109 }
248 }
110
249
250+----------------------------------------------------------------
251+
252+-- | Running 'Application' with 'TLSSettings' and 'Settings'.
253+runTLS :: TLSSettings -> Settings -> Application -> IO ()
254+runTLS tset set app = withSocketsDo $
255+ bracket
256+ (bindPortTCP (getPort set) (getHost set))
257+ close
258+ (\sock -> runTLSSocket tset set sock app)
259+
260+----------------------------------------------------------------
261+
111-- | Running 'Application' with 'TLSSettings' and 'Settings' using
262-- | Running 'Application' with 'TLSSettings' and 'Settings' using
112-- specified 'Socket'.
263-- specified 'Socket'.
113runTLSSocket :: TLSSettings -> Settings -> Socket -> Application -> IO ()
264runTLSSocket :: TLSSettings -> Settings -> Socket -> Application -> IO ()
114−runTLSSocket TLSSettings {..} set sock app = do
265+runTLSSocket tlsset@TLSSettings{..} set sock app = do
115− credential <- either error id <$> TLS.credentialLoadX509 certFile keyFile
266+ credential <- case (certMemory, keyMemory) of
267+ (Nothing, Nothing) ->
268+ either error id <$>
269+ TLS.credentialLoadX509Chain certFile chainCertFiles keyFile
270+ (mcert, mkey) -> do
271+ cert <- maybe (S.readFile certFile) return mcert
272+ key <- maybe (S.readFile keyFile) return mkey
273+ either error return $
274+ TLS.credentialLoadX509ChainFromMemory cert chainCertsMemory key
275+ mgr <- case tlsSessionManagerConfig of
276+ Nothing -> return TLS.noSessionManager
277+ Just config -> SM.newSessionManager config
278+ runTLSSocket' tlsset set credential mgr sock app
279+
280+runTLSSocket' :: TLSSettings -> Settings -> TLS.Credential -> TLS.SessionManager -> Socket -> Application -> IO ()
281+runTLSSocket' tlsset@TLSSettings{..} set credential mgr sock app =
282+ runSettingsConnectionMakerSecure set get app
283+ where
284+ get = getter tlsset set sock params
116− let params = def
285+ params = def { -- TLS.ServerParams
117− { TLS.serverWantClientCert = False
286+ TLS.serverWantClientCert = tlsWantClientCert
287+ , TLS.serverCACertificates = []
288+ , TLS.serverDHEParams = tlsServerDHEParams
289+ , TLS.serverHooks = hooks
290+ , TLS.serverShared = shared
118− , TLS.serverSupported = def
291+ , TLS.serverSupported = supported
292+#if MIN_VERSION_tls(1,5,0)
293+ , TLS.serverEarlyDataSize = 2018
294+#endif
295+ }
296+ -- Adding alpn to user's tlsServerHooks.
297+ hooks = tlsServerHooks {
298+ TLS.onALPNClientSuggest = TLS.onALPNClientSuggest tlsServerHooks <|>
299+ (if settingsHTTP2Enabled set then Just alpn else Nothing)
300+ }
301+ shared = def {
302+ TLS.sharedCredentials = TLS.Credentials [credential]
303+ , TLS.sharedSessionManager = mgr
304+ }
305+ supported = def { -- TLS.Supported
119− { TLS.supportedVersions = tlsAllowedVersions
306+ TLS.supportedVersions = tlsAllowedVersions
120− , TLS.supportedCiphers = tlsCiphers
307+ , TLS.supportedCiphers = tlsCiphers
121− }
308+ , TLS.supportedCompressions = [TLS.nullCompression]
122− , TLS.serverShared = def
309+ , TLS.supportedSecureRenegotiation = True
123− { TLS.sharedCredentials = TLS.Credentials [credential]
310+ , TLS.supportedClientInitiatedRenegotiation = False
311+ , TLS.supportedSession = True
312+ , TLS.supportedFallbackScsv = True
313+#if MIN_VERSION_tls(1,5,0)
314+ , TLS.supportedGroups = [TLS.X25519,TLS.P256,TLS.P384]
315+#endif
124− }
316+ }
125− }
317+
126− runSettingsConnectionMakerSecure set (getter params) app
318+alpn :: [S.ByteString] -> IO S.ByteString
319+alpn xs
320+ | "h2" `elem` xs = return "h2"
321+ | otherwise = return "http/1.1"
322+
323+----------------------------------------------------------------
324+
325+getter :: TLS.TLSParams params => TLSSettings -> Settings -> Socket -> params -> IO (IO (Connection, Transport), SockAddr)
326+getter tlsset@TLSSettings{..} set sock params = do
327+#if WINDOWS
328+ (s, sa) <- windowsThreadBlockHack $ accept sock
329+#else
330+ (s, sa) <- accept sock
331+#endif
332+ setSocketCloseOnExec s
333+ return (mkConn tlsset set s params, sa)
334+
335+mkConn :: TLS.TLSParams params => TLSSettings -> Settings -> Socket -> params -> IO (Connection, Transport)
336+mkConn tlsset set s params = switch `onException` close s
337+ where
338+ switch = do
339+ firstBS <- safeRecv s 4096
340+ if not (S.null firstBS) && S.head firstBS == 0x16 then
341+ httpOverTls tlsset set s firstBS params
342+ else
343+ plainHTTP tlsset set s firstBS
344+
345+----------------------------------------------------------------
346+
347+httpOverTls :: TLS.TLSParams params => TLSSettings -> Settings -> Socket -> S.ByteString -> params -> IO (Connection, Transport)
348+httpOverTls TLSSettings{..} _set s bs0 params = do
349+ recvN <- makePlainReceiveN s bs0
350+ ctx <- TLS.contextNew (backend recvN) params
351+ TLS.contextHookSetLogging ctx tlsLogging
352+ TLS.handshake ctx
353+ h2 <- (== Just "h2") <$> TLS.getNegotiatedProtocol ctx
354+ isH2 <- I.newIORef h2
355+ writeBuf <- allocateBuffer bufferSize
356+ -- Creating a cache for leftover input data.
357+ ref <- I.newIORef ""
358+ tls <- getTLSinfo ctx
359+ return (conn ctx writeBuf ref isH2, tls)
360+ where
361+ backend recvN = TLS.Backend {
362+ TLS.backendFlush = return ()
363+#if MIN_VERSION_network(3,1,1)
364+ , TLS.backendClose = gracefulClose s 5000 `E.catch` \(SomeException _) -> return ()
365+#else
366+ , TLS.backendClose = close s
367+#endif
368+ , TLS.backendSend = sendAll' s
369+ , TLS.backendRecv = recvN
370+ }
371+ sendAll' sock bs = sendAll sock bs `E.catch` \(SomeException _) ->
372+ throwIO ConnectionClosedByPeer
373+ conn ctx writeBuf ref isH2 = Connection {
374+ connSendMany = TLS.sendData ctx . L.fromChunks
375+ , connSendAll = sendall
376+ , connSendFile = sendfile
377+ , connClose = close'
378+ , connFree = freeBuffer writeBuf
379+ , connRecv = recv ref
380+ , connRecvBuf = recvBuf ref
381+ , connWriteBuffer = writeBuf
382+ , connBufferSize = bufferSize
383+ , connHTTP2 = isH2
384+ }
385+ where
386+ sendall = TLS.sendData ctx . L.fromChunks . return
387+ sendfile fid offset len hook headers =
388+ readSendFile writeBuf bufferSize sendall fid offset len hook headers
389+
390+ close' = void (tryIO sendBye) `finally`
391+ TLS.contextClose ctx
392+
393+ sendBye =
394+ -- It's fine if the connection was closed by the other side before
395+ -- receiving close_notify, see RFC 5246 section 7.2.1.
396+ handleJust
397+ (\e -> guard (e == ConnectionClosedByPeer) >> return e)
398+ (const (return ()))
399+ (TLS.bye ctx)
400+
401+ -- TLS version of recv with a cache for leftover input data.
402+ -- The cache is shared with recvBuf.
403+ recv cref = do
404+ cached <- I.readIORef cref
405+ if cached /= "" then do
406+ I.writeIORef cref ""
407+ return cached
408+ else
409+ recv'
410+
411+ -- TLS version of recv (decrypting) without a cache.
412+ recv' = handle onEOF go
413+ where
414+ onEOF e
415+ | Just TLS.Error_EOF <- fromException e = return S.empty
416+ | Just ioe <- fromException e, isEOFError ioe = return S.empty | otherwise = throwIO e
417+ go = do
418+ x <- TLS.recvData ctx
419+ if S.null x then
420+ go
421+ else
422+ return x
423+
424+ -- TLS version of recvBuf with a cache for leftover input data.
425+ recvBuf cref buf siz = do
426+ cached <- I.readIORef cref
427+ (ret, leftover) <- fill cached buf siz recv'
428+ I.writeIORef cref leftover
429+ return ret
430+
431+fill :: S.ByteString -> Buffer -> BufSize -> Recv -> IO (Bool,S.ByteString)
432+fill bs0 buf0 siz0 recv
433+ | siz0 <= len0 = do
434+ let (bs, leftover) = S.splitAt siz0 bs0
435+ void $ copy buf0 bs
436+ return (True, leftover)
437+ | otherwise = do
438+ buf <- copy buf0 bs0
439+ loop buf (siz0 - len0)
127 where
440 where
128− getter params = do
441+ len0 = S.length bs0
129− (s, sa) <- acceptSafe sock
442+ loop _ 0 = return (True, "")
130− let mkConn = do
443+ loop buf siz = do
131− (fromClient, firstBS) <- sourceSocket s C.$$+ CL.peek
444+ bs <- recv
132− let toClient = sinkSocket s
445+ let len = S.length bs
133− ifromClient <- I.newIORef fromClient
446+ if len == 0 then return (False, "")
134− let getNext sink = do
135− fromClient' <- I.readIORef ifromClient
136− (fromClient'', bs) <- fromClient' C.$$++ sink
137− I.writeIORef ifromClient fromClient''
138− return bs
139− if maybe False ((== 0x16) . fst) (firstBS >>= B.uncons)
140− then do
447+ else if (len <= siz) then do
141− gen <- Crypto.Random.AESCtr.makeSystem
448+ buf' <- copy buf bs
142− ctx <- TLS.contextNew
449+ loop buf' (siz - len)
143− TLS.Backend
450+ else do
144− { TLS.backendFlush = return ()
451+ let (bs1,bs2) = S.splitAt siz bs
145− , TLS.backendClose = sClose s
452+ void $ copy buf bs1
146− , TLS.backendSend = \bs -> C.yield bs C.$$ toClient
453+ return (True, bs2)
147− , TLS.backendRecv = getNext . fmap (B.concat . L.toChunks) . CB.take
454+
148− }
455+getTLSinfo :: TLS.Context -> IO Transport
149− params
456+getTLSinfo ctx = do
150− gen
457+ proto <- TLS.getNegotiatedProtocol ctx
151− TLS.contextHookSetLogging ctx tlsLogging
458+ minfo <- TLS.contextGetInformation ctx
152− TLS.handshake ctx
459+ case minfo of
153− buf <- allocateBuffer bufferSize
460+ Nothing -> return TCP
154− let conn = Connection
461+ Just TLS.Information{..} -> do
155− { connSendMany = TLS.sendData ctx . L.fromChunks
462+ let (major, minor) = case infoVersion of
156− , connSendAll = TLS.sendData ctx . L.fromChunks . return
463+ TLS.SSL2 -> (2,0)
157− , connSendFile = \fp offset len _th headers -> do
464+ TLS.SSL3 -> (3,0)
158− TLS.sendData ctx $ L.fromChunks headers
465+ TLS.TLS10 -> (3,1)
159− runResourceT $ sourceFileRange fp (Just offset) (Just len) C.$$ CL.mapM_ (TLS.sendData ctx . L.fromChunks . return)
466+ TLS.TLS11 -> (3,2)
160− , connClose =
467+ TLS.TLS12 -> (3,3)
161− TLS.bye ctx `finally`
468+#if MIN_VERSION_tls(1,5,0)
162− TLS.contextClose ctx
469+ TLS.TLS13 -> (3,4)
163− , connRecv =
470+#endif
164− let onEOF TLS.Error_EOF = return B.empty
471+ clientCert <- TLS.getClientCertificateChain ctx
165− onEOF e = throwIO e
472+ return TLS {
166− go = handle onEOF $ do
473+ tlsMajorVersion = major
167− x <- TLS.recvData ctx
474+ , tlsMinorVersion = minor
168− if B.null x
475+ , tlsNegotiatedProtocol = proto
169− then go
476+ , tlsChiperID = TLS.cipherID infoCipher
170− else return x
477+ , tlsClientCertificate = clientCert
171− in go
478+ }
172− , connSendFileOverride = NotOverride
479+
173− , connBuffer = buf
480+tryIO :: IO a -> IO (Either IOException a)
174− , connBufferSize = bufferSize
481+tryIO = try
175− }
482+
176− return (conn, True)
483+----------------------------------------------------------------
177− else
484+
485+plainHTTP :: TLSSettings -> Settings -> Socket -> S.ByteString -> IO (Connection, Transport)
178− case onInsecure of
486+plainHTTP TLSSettings{..} set s bs0 = case onInsecure of
179− AllowInsecure -> do
487+ AllowInsecure -> do
180− conn' <- socketConnection s
488+ conn' <- socketConnection set s
181− return (conn'
489+ cachedRef <- I.newIORef bs0
182− { connRecv = getNext $ fmap (fromMaybe B.empty) C.await
490+ let conn'' = conn'
183− }, False)
491+ { connRecv = recvPlain cachedRef (connRecv conn')
492+ }
493+ return (conn'', TCP)
184− DenyInsecure lbs -> do
494+ DenyInsecure lbs -> do
185− let src = do
495+ -- Listening port 443 but TLS records do not arrive.
496+ -- We want to let the browser know that TLS is required.
497+ -- So, we use 426.
498+ -- http://tools.ietf.org/html/rfc2817#section-4.2
499+ -- https://tools.ietf.org/html/rfc7231#section-6.5.15
500+ -- FIXME: should we distinguish HTTP/1.1 and HTTP/2?
501+ -- In the case of HTTP/2, should we send
502+ -- GOAWAY + INADEQUATE_SECURITY?
503+ -- FIXME: Content-Length:
504+ -- FIXME: TLS/<version>
505+ sendAll s "HTTP/1.1 426 Upgrade Required\
506+ \r\nUpgrade: TLS/1.0, HTTP/1.1\
507+ \r\nConnection: Upgrade\
186− C.yield "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"
508+ \r\nContent-Type: text/plain\r\n\r\n"
187− mapM_ C.yield $ L.toChunks lbs
509+ mapM_ (sendAll s) $ L.toChunks lbs
188− src C.$$ sinkSocket s
510+ close s
189− sClose s
190− throwIO InsecureConnectionDenied
511+ throwIO InsecureConnectionDenied
191− return (mkConn, sa)
512+
513+----------------------------------------------------------------
514+
515+-- | Modify the given receive function to first check the given @IORef@ for a
516+-- chunk of data. If present, takes the chunk of data from the @IORef@ and
517+-- empties out the @IORef@. Otherwise, calls the supplied receive function.
518+recvPlain :: I.IORef S.ByteString -> IO S.ByteString -> IO S.ByteString
519+recvPlain ref fallback = do
520+ bs <- I.readIORef ref
521+ if S.null bs
522+ then fallback
523+ else do
524+ I.writeIORef ref S.empty
525+ return bs
526+
527+----------------------------------------------------------------
192
528
193data WarpTLSException = InsecureConnectionDenied
529data WarpTLSException = InsecureConnectionDenied
194 deriving (Show, Typeable)
530 deriving (Show, Typeable)
195instance Exception WarpTLSException
531instance Exception WarpTLSException
196−
197−-- | Running 'Application' with 'TLSSettings' and 'Settings'.
198−runTLS :: TLSSettings -> Settings -> Application -> IO ()
199−runTLS tset set app = withSocketsDo $
200− bracket
201− (bindPortTCP (settingsPort set) (settingsHost set))
202− sClose
203− (\sock -> runTLSSocket tset set sock app)
204−
205−-- taken from stunnel example in tls-extra
206−ciphers :: [TLS.Cipher]
207−ciphers =
208− [ TLSExtra.cipher_AES128_SHA1
209− , TLSExtra.cipher_AES256_SHA1
210− , TLSExtra.cipher_RC4_128_MD5
211− , TLSExtra.cipher_RC4_128_SHA1
212− ]
warp-tls/README.md+4
@@ -1,3 +1,7 @@
1+## warp-tls
2+
3+Serve WAI applications using the Warp webserver and the Haskell TLS library.
4+
1In order to generate a self-signed certificate for testing, try the following:
5In order to generate a self-signed certificate for testing, try the following:
2
6
3 openssl genrsa -out key.pem 2048
7 openssl genrsa -out key.pem 2048
warp-tls/pong.hs+7−7
@@ -1,6 +1,6 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2import Network.Wai
2import Network.Wai
3−import Network.Wai.Handler.Warp (defaultSettings, settingsPort)
3+import Network.Wai.Handler.Warp (defaultSettings, setPort)
4import Network.Wai.Handler.WarpTLS
4import Network.Wai.Handler.WarpTLS
5import Network.HTTP.Types (status200)
5import Network.HTTP.Types (status200)
6import Blaze.ByteString.Builder (copyByteString)
6import Blaze.ByteString.Builder (copyByteString)
@@ -15,9 +15,9 @@ main = do
15 --manager <- newManager def
15 --manager <- newManager def
16 runTLS (tlsSettings "certificate.pem" "key.pem")
16 runTLS (tlsSettings "certificate.pem" "key.pem")
17 { onInsecure = AllowInsecure
17 { onInsecure = AllowInsecure
18− } defaultSettings { settingsPort = 3009 } app
18+ } (setPort 3009 defaultSettings) app
19
19
20−app req = return $
20+app req f = f $
21 case rawPathInfo req of
21 case rawPathInfo req of
22 "/builder/withlen" -> builderWithLen
22 "/builder/withlen" -> builderWithLen
23 "/builder/nolen" -> builderNoLen
23 "/builder/nolen" -> builderNoLen
@@ -41,18 +41,18 @@ builderNoLen = responseBuilder
41 ]
41 ]
42 $ copyByteString "PONG"
42 $ copyByteString "PONG"
43
43
44−sourceWithLen = responseSource
44+sourceWithLen = responseStream
45 status200
45 status200
46 [ ("Content-Type", "text/plain")
46 [ ("Content-Type", "text/plain")
47 , ("Content-Length", "4")
47 , ("Content-Length", "4")
48 ]
48 ]
49− $ CL.sourceList [C.Chunk $ copyByteString "PONG"]
49+ $ \sendChunk _flush -> sendChunk $ copyByteString "PONG"
50
50
51−sourceNoLen = responseSource
51+sourceNoLen = responseStream
52 status200
52 status200
53 [ ("Content-Type", "text/plain")
53 [ ("Content-Type", "text/plain")
54 ]
54 ]
55− $ CL.sourceList [C.Chunk $ copyByteString "PONG"]
55+ $ \sendChunk _flush -> sendChunk $ copyByteString "PONG"
56
56
57fileWithLen = responseFile
57fileWithLen = responseFile
58 status200
58 status200
warp-tls/pong.txt+1−1
@@ -1 +1 @@
1−PONG
1+PONG
warp-tls/warp-tls.cabal+21−14
@@ -1,6 +1,6 @@
1Name: warp-tls
1Name: warp-tls
2−Version: 2.0.3.3
2+Version: 3.2.11
3−Synopsis: HTTP over SSL/TLS support for Warp via the TLS package
3+Synopsis: HTTP over TLS support for Warp via the TLS package
4License: MIT
4License: MIT
5License-file: LICENSE
5License-file: LICENSE
6Author: Michael Snoyman
6Author: Michael Snoyman
@@ -8,27 +8,34 @@ Maintainer: michael@snoyman.com
8Homepage: http://github.com/yesodweb/wai
8Homepage: http://github.com/yesodweb/wai
9Category: Web, Yesod
9Category: Web, Yesod
10Build-Type: Simple
10Build-Type: Simple
11−Cabal-Version: >=1.6
11+Cabal-Version: >= 1.10
12Stability: Stable
12Stability: Stable
13−Description: HTTP over SSL/TLS support for Warp via the TLS package.
13+description: SSLv1 and SSLv2 are obsoleted by IETF.
14+ We should use TLS 1.2 (or TLS 1.1 or TLS 1.0 if necessary).
15+ HTTP/2 can be negotiated by ALPN.
16+ API docs and the README are available at
17+ <http://www.stackage.org/package/warp-tls>.
18+extra-source-files: ChangeLog.md README.md
14
19
15Library
20Library
16− Build-Depends: base >= 4 && < 5
21+ Build-Depends: base >= 4.10 && < 5
17 , bytestring >= 0.9
22 , bytestring >= 0.9
18− , wai >= 2.0 && < 2.2
23+ , wai >= 3.2 && < 3.3
19− , warp >= 2.1.4 && < 2.2
24+ , warp >= 3.3.6 && < 3.4
20− , transformers >= 0.2
21− , conduit >= 0.5 && < 1.2
22− , conduit-extra
23− , network-conduit >= 0.6 && < 1.2
24 , data-default-class >= 0.0.1
25 , data-default-class >= 0.0.1
25− , tls >= 1.2.1
26+ , tls >= 1.5.3
27+ , cryptonite >= 0.12
26 , network >= 2.2.1
28 , network >= 2.2.1
27− , cprng-aes >= 0.5.0
28 , streaming-commons
29 , streaming-commons
29− , resourcet
30+ , tls-session-manager >= 0.0.4
30 Exposed-modules: Network.Wai.Handler.WarpTLS
31 Exposed-modules: Network.Wai.Handler.WarpTLS
31 ghc-options: -Wall
32 ghc-options: -Wall
33+ if os(windows)
34+ Cpp-Options: -DWINDOWS
35+ if impl(ghc >= 8)
36+ Default-Extensions: Strict StrictData
37+ Default-Language: Haskell2010
38+
32
39
33source-repository head
40source-repository head
34 type: git
41 type: git
warp/.ghci+1−1
@@ -1 +1 @@
1−:set -itest -idist/build/autogen -optP-include -optPdist/build/autogen/cabal_macros.h
1+:set -itest -idist/build/autogen -i.stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/autogen
warp/ChangeLog.mdnew file+404
@@ -0,0 +1,404 @@
1+## 3.3.9
2+
3+* Don't insert Last-Modified: if exists.
4+ [#791](https://github.com/yesodweb/wai/pull/791)
5+
6+## 3.3.8
7+
8+* Maximum header size is configurable.
9+ [#781](https://github.com/yesodweb/wai/pull/781)
10+* Ignoring an exception from shutdown (gracefulClose).
11+
12+## 3.3.7
13+
14+* InvalidArgument (Bad file descriptor) is ignored in `receive`.
15+ [#787](https://github.com/yesodweb/wai/pull/787)
16+
17+## 3.3.6
18+
19+* Fixing a bug of thread killed in the case of event source with
20+ HTTP/2 (fixing #692 and #785)
21+* New APIs: clientCertificate to get client's certificate
22+ [#783](https://github.com/yesodweb/wai/pull/783)
23+
24+## 3.3.5
25+
26+* New APIs: setGracefulCloseTimeout1 and setGracefulCloseTimeout2.
27+ For HTTP/1.x, connections are closed immediately by default.
28+ gracefullClose is used for HTTP/2 by default.
29+ [#782](https://github.com/yesodweb/wai/pull/782)
30+
31+## 3.3.4
32+
33+* Setting isSecure of HTTP/2 correctly.
34+
35+## 3.3.3
36+
37+* Calling setOnException in HTTP/2.
38+ [#771](https://github.com/yesodweb/wai/pull/771)
39+
40+## 3.3.2
41+
42+* Fixing a bug of HTTP/2 without fd cache.
43+
44+## 3.3.1
45+
46+* Using gracefullClose of network 3.1.1 or later if available.
47+* If the first line of an HTTP request is really invalid,
48+ don't send an error response
49+
50+## 3.3.0
51+
52+* Switching from the original implementation to HTTP/2 server library.
53+ [#754](https://github.com/yesodweb/wai/pull/754)
54+* Breaking change: The type of `http2dataTrailers` is now
55+ `HTTP2Data -> TrailersMaker`.
56+
57+## 3.2.28
58+
59+* Using the Strict and StrictData language extensions for GHC >8.
60+ [#752](https://github.com/yesodweb/wai/pull/752)
61+* System.TimeManager is now in a separate package: time-manager.
62+ [#750](https://github.com/yesodweb/wai/pull/750)
63+* Fixing a bug of ALPN.
64+* Introducing the half closed state for HTTP/2.
65+ [#717](https://github.com/yesodweb/wai/pull/717)
66+
67+## 3.2.27
68+
69+* Internally, use `lookupEnv` instead of `getEnvironment` to get the
70+ value of the `PORT` environment variable
71+ [#736](https://github.com/yesodweb/wai/pull/736)
72+* Throw 413 for too large payload
73+* Throw 431 for too large headers
74+ [#741](https://github.com/yesodweb/wai/pull/741)
75+* Use exception response handler in HTTP/2 & improve connection preservation
76+ in HTTP/1.x if uncaught exceptions are thrown in an `Application`.
77+ [#738](https://github.com/yesodweb/wai/pull/738)
78+
79+## 3.2.26
80+
81+* Support network package version 3
82+
83+## 3.2.25
84+
85+* Removing Connection: and Transfer-Encoding: from HTTP/2
86+ response header
87+ [#707](https://github.com/yesodweb/wai/pull/707)
88+
89+## 3.2.24
90+
91+* Fix HTTP2 unwanted GoAways on late WindowUpdate frames.
92+ [#711](https://github.com/yesodweb/wai/pull/711)
93+
94+## 3.2.23
95+
96+* Log real requsts when an app throws an error.
97+ [#698](https://github.com/yesodweb/wai/pull/698)
98+
99+## 3.2.22
100+
101+* Fixing large request body in HTTP/2.
102+
103+## 3.2.21
104+
105+* Fixing HTTP/2's timeout handler in request's vault.
106+
107+## 3.2.20
108+
109+* Fixing large request body in HTTP/2
110+ [#593](https://github.com/yesodweb/wai/issues/593)
111+
112+## 3.2.19
113+
114+* Fixing 0-byte request body in HTTP/2
115+ [#597](https://github.com/yesodweb/wai/issues/597)
116+ [#679](https://github.com/yesodweb/wai/issues/679)
117+
118+## 3.2.18.2
119+
120+* Replace dependency on `blaze-builder` with `bsb-http-chunked`
121+
122+## 3.2.18.1
123+
124+* Fix benchmark compilation [#681](https://github.com/yesodweb/wai/issues/681)
125+
126+## 3.2.18
127+
128+* Make `testWithApplicationSettings` actually use the settings passed.
129+ [#677](https://github.com/yesodweb/wai/pull/677).
130+
131+## 3.2.17
132+* Add support for windows thread block hack and closeOnExec to TLS.
133+ [#674](https://github.com/yesodweb/wai/pull/674).
134+
135+## 3.2.16
136+
137+* In `testWithApplication`, don't `throwTo` ignorable exceptions
138+ [#671](https://github.com/yesodweb/wai/issues/671), and
139+ reuse `bindRandomPortTCP`
140+
141+## 3.2.15
142+
143+* Address space leak from exception handlers
144+ [#649](https://github.com/yesodweb/wai/issues/649)
145+
146+## 3.2.14
147+
148+* Support streaming-commons 0.2
149+* Warnings cleanup
150+
151+## 3.2.13
152+
153+* Tickling HTTP/2 timer. [624](https://github.com/yesodweb/wai/pull/624)
154+* Guarantee atomicity of WINDOW_UPDATE increments [622](https://github.com/yesodweb/wai/pull/622)
155+* Relax HTTP2 headers check [621](https://github.com/yesodweb/wai/pull/621)
156+
157+## 3.2.12
158+
159+* If an empty string is set by setServerName, the Server header is not included in response headers [#619](https://github.com/yesodweb/wai/issues/619)
160+
161+## 3.2.11.2
162+
163+* Don't throw exceptions when closing a keep-alive connection
164+ [#618](https://github.com/yesodweb/wai/issues/618)
165+
166+## 3.2.11.1
167+
168+* Move exception handling to top of thread (fixes
169+ [#613](https://github.com/yesodweb/wai/issues/613))
170+
171+## 3.2.11
172+
173+* Fixing 10 HTTP2 bugs pointed out by h2spec v2.
174+
175+## 3.2.10
176+
177+* Add `connFree` to `Connection`. Close socket connections on timeout triggered. Timeout exceptions extend from `SomeAsyncException`. [#602](https://github.com/yesodweb/wai/pull/602) [#605](https://github.com/yesodweb/wai/pull/605)
178+
179+## 3.2.9
180+
181+* Fixing a space leak. [#586](https://github.com/yesodweb/wai/pull/586)
182+
183+## 3.2.8
184+
185+* Fixing HTTP2 requestBodyLength. [#573](https://github.com/yesodweb/wai/pull/573)
186+* Making HTTP/2 :path optional for the CONNECT method. [#572](https://github.com/yesodweb/wai/pull/572)
187+* Adding new APIs for HTTP/2 trailers: http2dataTrailers and modifyHTTP2Data [#566](https://github.com/yesodweb/wai/pull/566)
188+
189+## 3.2.7
190+
191+* Adding new APIs for HTTP/2 server push: getHTTP2Data and setHTTP2Data [#510](https://github.com/yesodweb/wai/pull/510)
192+* Better accept(2) error handling [#553](https://github.com/yesodweb/wai/pull/553)
193+* Adding getGracefulShutdownTimeout.
194+* Add {test,}withApplicationSettings [#531](https://github.com/yesodweb/wai/pull/531)
195+
196+## 3.2.6
197+
198+* Using token based APIs of http2 1.6.
199+
200+## 3.2.5
201+
202+* Ignoring errors from setSocketOption. [#526](https://github.com/yesodweb/wai/issues/526).
203+
204+## 3.2.4
205+
206+* Added `withApplication`, `testWithApplication`, and `openFreePort`
207+* Fixing reaper delay value of file info cache.
208+
209+## 3.2.3
210+
211+* Using http2 v1.5.x which much improves the performance of HTTP/2.
212+* To get rid of the bottleneck of ByteString's (==), a new logic to
213+ compare header names is introduced.
214+
215+## 3.2.2
216+
217+* Throwing errno for pread [#499](https://github.com/yesodweb/wai/issues/499).
218+* Makeing compilable on Windows [#505](https://github.com/yesodweb/wai/issues/505).
219+
220+## 3.2.1
221+
222+* Add back `warpVersion`
223+
224+## 3.2.0
225+
226+* Major version up due to breaking changes. This is because the HTTP/2 code
227+ was started over with Warp 3.1.3 due to performance issue [#470](https://github.com/yesodweb/wai/issues/470).
228+* runHTTP2, runHTTP2Env, runHTTP2Settings and runHTTP2SettingsSocket were removed from the Network.Wai.Handler.Warp module.
229+* The performance of HTTP/2 was drastically improved. Now the performance of HTTP/2 is almost the same as that of HTTP/1.1.
230+* The logic to handle files in HTTP/2 is now identical to that in HTTP/1.1.
231+* Internal stuff was removed from the Network.Wai.Handler.Warp module according to [the plan](http://www.yesodweb.com/blog/2015/06/cleaning-up-warp-apis).
232+
233+## 3.1.12
234+
235+* Setting lower bound for auto-update [#495](https://github.com/yesodweb/wai/issues/495)
236+
237+## 3.1.11
238+
239+* Providing a new API: killManager.
240+* Preventing space leaks due to Weak ThreadId [#488](https://github.com/yesodweb/wai/issues/488)
241+* Setting upper bound for http2.
242+
243+## 3.1.10
244+
245+* `setFileInfoCacheDuration`
246+* `setLogger`
247+* `FileInfo`/`getFileInfo`
248+* Fix: warp-tls strips out the Host request header [#478](https://github.com/yesodweb/wai/issues/478)
249+
250+## 3.1.9
251+
252+* Using the new priority queue based on PSQ provided by http2 lib again.
253+
254+## 3.1.8
255+
256+* Using the new priority queue based on PSQ provided by http2 lib.
257+
258+## 3.1.7
259+
260+* A concatenated Cookie header is prepended to the headers to ensure that it flows pseudo headers. [#454](https://github.com/yesodweb/wai/pull/454)
261+* Providing a new settings: `setHTTP2Disabled` [#450](https://github.com/yesodweb/wai/pull/450)
262+
263+## 3.1.6
264+
265+* Adding back http-types 0.8 support [#449](https://github.com/yesodweb/wai/pull/449)
266+
267+## 3.1.5
268+
269+* Using http-types v0.9.
270+* Fixing build on OpenBSD. [#428](https://github.com/yesodweb/wai/pull/428) [#440](https://github.com/yesodweb/wai/pull/440)
271+* Fixing build on Windows. [#438](https://github.com/yesodweb/wai/pull/438)
272+
273+## 3.1.4
274+
275+* Using newer http2 library to prevent change table size attacks.
276+* API for HTTP/2 server push and trailers. [#426](https://github.com/yesodweb/wai/pull/426)
277+* Preventing response splitting attacks. [#435](https://github.com/yesodweb/wai/pull/435)
278+* Concatenating multiple Cookie: headers in HTTP/2.
279+
280+## 3.1.3
281+
282+* Warp now supports blaze-builder v0.4 or later only.
283+* HTTP/2 code was improved: dynamic priority change, efficient queuing and sender loop continuation. [#423](https://github.com/yesodweb/wai/pull/423) [#424](https://github.com/yesodweb/wai/pull/424)
284+
285+## 3.1.2
286+
287+* Configurable Slowloris size [#418](https://github.com/yesodweb/wai/pull/418)
288+
289+## 3.1.1
290+
291+* Fixing a bug of HTTP/2 when no FD cache is used [#411](https://github.com/yesodweb/wai/pull/411)
292+* Fixing a buffer-pool bug [#406](https://github.com/yesodweb/wai/pull/406) [#407](https://github.com/yesodweb/wai/pull/407)
293+
294+## 3.1.0
295+
296+* Supporting HTTP/2 [#399](https://github.com/yesodweb/wai/pull/399)
297+* Cleaning up APIs [#387](https://github.com/yesodweb/wai/issues/387)
298+
299+## 3.0.13.1
300+
301+* Remove dependency on the void package [#375](https://github.com/yesodweb/wai/pull/375)
302+
303+## 3.0.13
304+
305+* Turn off file descriptor cache by default [#371](https://github.com/yesodweb/wai/issues/371)
306+
307+## 3.0.12.1
308+
309+* Fix for: HEAD requests returning non-empty entity body [#369](https://github.com/yesodweb/wai/issues/369)
310+
311+## 3.0.12
312+
313+* Only conditionally produce HTTP 100 Continue
314+
315+## 3.0.11
316+
317+* Better HEAD support for files [#357](https://github.com/yesodweb/wai/pull/357)
318+
319+## 3.0.10
320+
321+* Fix [missing `IORef` tweak](https://github.com/yesodweb/wai/issues/351)
322+* Disable timeouts as soon as request body is fully consumed. This addresses
323+ the common case of a non-chunked request body. Previously, we would wait
324+ until a zero-length `ByteString` is returned, but that is suboptimal for some
325+ cases. For more information, see [issue
326+ 351](https://github.com/yesodweb/wai/issues/351).
327+* Add `pauseTimeout` function
328+
329+## 3.0.9.3
330+
331+* Don't serve a 416 status code for 0-length files [keter issue #75](https://github.com/snoyberg/keter/issues/75)
332+* Don't serve content-length for 416 responses [#346](https://github.com/yesodweb/wai/issues/346)
333+
334+## 3.0.9.2
335+
336+Fix support for old versions of bytestring
337+
338+## 3.0.9.1
339+
340+Add support for blaze-builder 0.4
341+
342+## 3.0.9
343+
344+* Add runEnv: like run but uses $PORT [#334](https://github.com/yesodweb/wai/pull/334)
345+
346+## 3.0.5.2
347+
348+* [Pass the Request to settingsOnException handlers when available. #326](https://github.com/yesodweb/wai/pull/326)
349+
350+## 3.0.5
351+
352+Support for PROXY protocol, such as used by Amazon ELB TCP. This is useful
353+since, for example, Amazon ELB HTTP does *not* have support for Websockets.
354+More information on the protocol [is available from
355+Amazon](http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#proxy-protocol).
356+
357+## 3.0.4
358+
359+Added `setFork`.
360+
361+## 3.0.3
362+
363+Modify flushing of request bodies. Previously, regardless of the size of the
364+request body, the entire body would be flushed. When uploading large files to a
365+web app that does not accept such files (e.g., returns a 413 too large status),
366+browsers would still send the entire request body and the servers will still
367+receive it.
368+
369+The new behavior is to detect if there is a large amount of data still to be
370+consumed and, if so, immediately terminate the connection. In the case of
371+chunked request bodies, up to a maximum number of bytes is consumed before the
372+connection is terminated.
373+
374+This is controlled by the new setting `setMaximumBodyFlush`. A value of
375+@Nothing@ will return the original behavior of flushing the entire body.
376+
377+## 3.0.0
378+
379+WAI no longer uses conduit for its streaming interface.
380+
381+## 2.1.0
382+
383+The `onOpen` and `onClose` settings now provide the `SockAddr` of the client,
384+and `onOpen` can return a `Bool` which will close the connection. The
385+`responseRaw` response has been added, which provides a more elegant way to
386+handle WebSockets than the previous `settingsIntercept`. The old settings
387+accessors have been deprecated in favor of new setters, which will allow
388+settings changes to be made in the future without breaking backwards
389+compatibility.
390+
391+## 2.0.0
392+
393+ResourceT is not used anymore. Request and Response is now abstract data types.
394+To use their constructors, Internal module should be imported.
395+
396+## 1.3.9
397+
398+Support for byte range requests.
399+
400+## 1.3.7
401+
402+Sockets now have `FD_CLOEXEC` set on them. This behavior is more secure, and
403+the change should not affect the vast majority of use cases. However, it
404+appeared that this is buggy and is fixed in 2.0.0.
warp/Network/Wai/Handler/Warp.hs+376−72
@@ -1,4 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+{-# LANGUAGE RankNTypes #-}
3+{-# LANGUAGE PatternGuards #-}
2{-# OPTIONS_GHC -fno-warn-deprecations #-}
4{-# OPTIONS_GHC -fno-warn-deprecations #-}
3
5
4---------------------------------------------------------
6---------------------------------------------------------
@@ -16,14 +18,32 @@
16---------------------------------------------------------
18---------------------------------------------------------
17
19
18-- | A fast, light-weight HTTP server handler for WAI.
20-- | A fast, light-weight HTTP server handler for WAI.
21+--
22+-- HTTP\/1.0, HTTP\/1.1 and HTTP\/2 are supported. For HTTP\/2,
23+-- Warp supports direct and ALPN (in TLS) but not upgrade.
24+--
25+-- Note on slowloris timeouts: to prevent slowloris attacks, timeouts are used
26+-- at various points in request receiving and response sending. One interesting
27+-- corner case is partial request body consumption; in that case, Warp's
28+-- timeout handling is still in effect, and the timeout will not be triggered
29+-- again. Therefore, it is recommended that once you start consuming the
30+-- request body, you either:
31+--
32+-- * consume the entire body promptly
33+--
34+-- * call the 'pauseTimeout' function
35+--
36+-- For more information, see <https://github.com/yesodweb/wai/issues/351>.
37+--
38+--
19module Network.Wai.Handler.Warp (
39module Network.Wai.Handler.Warp (
20 -- * Run a Warp server
40 -- * Run a Warp server
41+ -- | All of these automatically serve the same 'Application' over HTTP\/1,
42+ -- HTTP\/1.1, and HTTP\/2.
21 run
43 run
44+ , runEnv
22 , runSettings
45 , runSettings
23 , runSettingsSocket
46 , runSettingsSocket
24− , runSettingsConnection
25− , runSettingsConnectionMaker
26− , runSettingsConnectionMakerSecure
27 -- * Settings
47 -- * Settings
28 , Settings
48 , Settings
29 , defaultSettings
49 , defaultSettings
@@ -35,78 +55,99 @@ module Network.Wai.Handler.Warp (
35 , setOnOpen
55 , setOnOpen
36 , setOnClose
56 , setOnClose
37 , setTimeout
57 , setTimeout
38− , setIntercept
39 , setManager
58 , setManager
40 , setFdCacheDuration
59 , setFdCacheDuration
60+ , setFileInfoCacheDuration
41 , setBeforeMainLoop
61 , setBeforeMainLoop
42 , setNoParsePath
62 , setNoParsePath
63+ , setInstallShutdownHandler
64+ , setServerName
65+ , setMaximumBodyFlush
66+ , setFork
67+ , setProxyProtocolNone
68+ , setProxyProtocolRequired
69+ , setProxyProtocolOptional
70+ , setSlowlorisSize
71+ , setHTTP2Disabled
72+ , setLogger
73+ , setServerPushLogger
74+ , setGracefulShutdownTimeout
75+ , setGracefulCloseTimeout1
76+ , setGracefulCloseTimeout2
77+ , setMaxTotalHeaderLength
43 -- ** Getters
78 -- ** Getters
44 , getPort
79 , getPort
45 , getHost
80 , getHost
46− -- ** Accessors
81+ , getOnOpen
47− -- | Note: these accessors are deprecated, please use the @set@ versions instead.
82+ , getOnClose
48− , settingsPort
83+ , getOnException
49− , settingsHost
84+ , getGracefulShutdownTimeout
50− , settingsOnException
85+ , getGracefulCloseTimeout1
51− , settingsOnExceptionResponse
86+ , getGracefulCloseTimeout2
52− , settingsOnOpen
87+ -- ** Exception handler
53− , settingsOnClose
88+ , defaultOnException
54− , settingsTimeout
55− , settingsIntercept
56− , settingsManager
57− , settingsFdCacheDuration
58− , settingsBeforeMainLoop
59− , settingsNoParsePath
60− -- ** Debugging
61− , exceptionResponseForDebug
62 , defaultShouldDisplayException
89 , defaultShouldDisplayException
90+ -- ** Exception response handler
91+ , defaultOnExceptionResponse
92+ , exceptionResponseForDebug
63 -- * Data types
93 -- * Data types
64− , HostPreference (..)
94+ , HostPreference
65 , Port
95 , Port
66 , InvalidRequest (..)
96 , InvalidRequest (..)
67− , ConnSendFileOverride (..)
97+ -- * Utilities
68− -- * Connection
98+ , pauseTimeout
69− , Connection (..)
99+ , FileInfo(..)
70− , socketConnection
100+ , getFileInfo
71− -- * Internal
101+ , clientCertificate
102+ , withApplication
103+ , withApplicationSettings
104+ , testWithApplication
105+ , testWithApplicationSettings
106+ , openFreePort
72− -- ** Version
107+ -- * Version
73 , warpVersion
108 , warpVersion
74− -- ** Data types
109+ -- * HTTP/2
75− , InternalInfo (..)
110+ -- ** HTTP2 data
76− , HeaderValue
111+ , HTTP2Data
77− , IndexedHeader
112+ , http2dataPushPromise
78− , requestMaxIndex
113+ , http2dataTrailers
79− -- ** Time out manager
114+ , defaultHTTP2Data
80− , module Network.Wai.Handler.Warp.Timeout
115+ , getHTTP2Data
81− -- ** File descriptor cache
116+ , setHTTP2Data
82− , module Network.Wai.Handler.Warp.FdCache
117+ , modifyHTTP2Data
83− -- ** Date
118+ -- ** Push promise
84− , module Network.Wai.Handler.Warp.Date
119+ , PushPromise
85− -- ** Request and response
120+ , promisedPath
86− , recvRequest
121+ , promisedFile
87− , sendResponse
122+ , promisedResponseHeaders
123+ , promisedWeight
124+ , defaultPushPromise
88 ) where
125 ) where
89
126
127+import Control.Exception (SomeException, throwIO)
90−import Data.Conduit.Network (HostPreference(..))
128+import Data.Streaming.Network (HostPreference)
91−import Network.Wai.Handler.Warp.Date
129+import qualified Data.Vault.Lazy as Vault
130+import Data.X509
131+import qualified Network.HTTP.Types as H
132+import Network.Socket (SockAddr)
133+import Network.Wai (Request, Response, vault)
134+import System.TimeManager
135+
136+import Network.Wai.Handler.Warp.FileInfoCache
137+import Network.Wai.Handler.Warp.HTTP2.Request (getHTTP2Data, setHTTP2Data, modifyHTTP2Data)
92−import Network.Wai.Handler.Warp.FdCache
138+import Network.Wai.Handler.Warp.HTTP2.Types
93−import Network.Wai.Handler.Warp.Header
139+import Network.Wai.Handler.Warp.Imports
94import Network.Wai.Handler.Warp.Request
140import Network.Wai.Handler.Warp.Request
95−import Network.Wai.Handler.Warp.Response
141+import Network.Wai.Handler.Warp.Response (warpVersion)
96import Network.Wai.Handler.Warp.Run
142import Network.Wai.Handler.Warp.Run
97import Network.Wai.Handler.Warp.Settings
143import Network.Wai.Handler.Warp.Settings
98−import Network.Wai.Handler.Warp.Timeout
144+import Network.Wai.Handler.Warp.Types hiding (getFileInfo)
99−import Network.Wai.Handler.Warp.Types
100−import Control.Exception (SomeException)
101−import Network.Wai (Request, Response)
145+import Network.Wai.Handler.Warp.WithApplication
102−import Network.Socket (SockAddr)
103−import Data.Conduit (Source)
104−import Data.ByteString (ByteString)
105
146
106-- | Port to listen on. Default value: 3000
147-- | Port to listen on. Default value: 3000
107--
148--
108-- Since 2.1.0
149-- Since 2.1.0
109−setPort :: Int -> Settings -> Settings
150+setPort :: Port -> Settings -> Settings
110setPort x y = y { settingsPort = x }
151setPort x y = y { settingsPort = x }
111
152
112-- | Interface to bind to. Default value: HostIPv4
153-- | Interface to bind to. Default value: HostIPv4
@@ -116,22 +157,30 @@ setHost :: HostPreference -> Settings -> Settings
116setHost x y = y { settingsHost = x }
157setHost x y = y { settingsHost = x }
117
158
118-- | What to do with exceptions thrown by either the application or server.
159-- | What to do with exceptions thrown by either the application or server.
119−-- Default: ignore server-generated exceptions (see 'InvalidRequest') and print
160+-- Default: 'defaultOnException'
120−-- application-generated applications to stderr.
121--
161--
122-- Since 2.1.0
162-- Since 2.1.0
123setOnException :: (Maybe Request -> SomeException -> IO ()) -> Settings -> Settings
163setOnException :: (Maybe Request -> SomeException -> IO ()) -> Settings -> Settings
124setOnException x y = y { settingsOnException = x }
164setOnException x y = y { settingsOnException = x }
125
165
126−-- | A function to create `Response` when an exception occurs.
166+-- | A function to create a `Response` when an exception occurs.
167+-- Default: 'defaultOnExceptionResponse'
168+--
169+-- Note that an application can handle its own exceptions without interfering with Warp:
127--
170--
128−-- Default: 500, text/plain, \"Something went wrong\"
171+-- > myApp :: Application
172+-- > myApp request respond = innerApp `catch` onError
173+-- > where
174+-- > onError = respond . response500 request
175+-- >
176+-- > response500 :: Request -> SomeException -> Response
177+-- > response500 req someEx = responseLBS status500 -- ...
129--
178--
130-- Since 2.1.0
179-- Since 2.1.0
131setOnExceptionResponse :: (SomeException -> Response) -> Settings -> Settings
180setOnExceptionResponse :: (SomeException -> Response) -> Settings -> Settings
132setOnExceptionResponse x y = y { settingsOnExceptionResponse = x }
181setOnExceptionResponse x y = y { settingsOnExceptionResponse = x }
133
182
134−-- | What to do when a connection is open. When 'False' is returned, the
183+-- | What to do when a connection is opened. When 'False' is returned, the
135-- connection is closed immediately. Otherwise, the connection is going on.
184-- connection is closed immediately. Otherwise, the connection is going on.
136-- Default: always returns 'True'.
185-- Default: always returns 'True'.
137--
186--
@@ -139,7 +188,7 @@ setOnExceptionResponse x y = y { settingsOnExceptionResponse = x }
139setOnOpen :: (SockAddr -> IO Bool) -> Settings -> Settings
188setOnOpen :: (SockAddr -> IO Bool) -> Settings -> Settings
140setOnOpen x y = y { settingsOnOpen = x }
189setOnOpen x y = y { settingsOnOpen = x }
141
190
142−-- | What to do when a connection is close. Default: do nothing.
191+-- | What to do when a connection is closed. Default: do nothing.
143--
192--
144-- Since 2.1.0
193-- Since 2.1.0
145setOnClose :: (SockAddr -> IO ()) -> Settings -> Settings
194setOnClose :: (SockAddr -> IO ()) -> Settings -> Settings
@@ -151,16 +200,6 @@ setOnClose x y = y { settingsOnClose = x }
151setTimeout :: Int -> Settings -> Settings
200setTimeout :: Int -> Settings -> Settings
152setTimeout x y = y { settingsTimeout = x }
201setTimeout x y = y { settingsTimeout = x }
153
202
154−-- | Register some intercept handler to deal with specific requests. Prime use
155−-- case: websockets.
156−--
157−-- Default: always return @Nothing@.
158−--
159−-- Since 2.1.0
160−setIntercept :: (Request -> IO (Maybe (Source IO ByteString -> Connection -> IO ())))
161− -> Settings -> Settings
162−setIntercept x y = y { settingsIntercept = x }
163−
164-- | Use an existing timeout manager instead of spawning a new one. If used,
203-- | Use an existing timeout manager instead of spawning a new one. If used,
165-- 'settingsTimeout' is ignored.
204-- 'settingsTimeout' is ignored.
166--
205--
@@ -168,10 +207,36 @@ setIntercept x y = y { settingsIntercept = x }
168setManager :: Manager -> Settings -> Settings
207setManager :: Manager -> Settings -> Settings
169setManager x y = y { settingsManager = Just x }
208setManager x y = y { settingsManager = Just x }
170
209
171−-- | Cache duratoin time of file descriptors in seconds. 0 means that the cache mechanism is not used. Default value: 10
210+-- | Cache duration time of file descriptors in seconds. 0 means that the cache mechanism is not used.
211+--
212+-- The FD cache is an optimization that is useful for servers dealing with
213+-- static files. However, if files are being modified, it can cause incorrect
214+-- results in some cases. Therefore, we disable it by default. If you know that
215+-- your files will be static or you prefer performance to file consistency,
216+-- it's recommended to turn this on; a reasonable value for those cases is 10.
217+-- Enabling this cache results in drastic performance improvement for file
218+-- transfers.
219+--
220+-- Default value: 0, was previously 10
221+--
222+-- Since 3.0.13
172setFdCacheDuration :: Int -> Settings -> Settings
223setFdCacheDuration :: Int -> Settings -> Settings
173setFdCacheDuration x y = y { settingsFdCacheDuration = x }
224setFdCacheDuration x y = y { settingsFdCacheDuration = x }
174
225
226+-- | Cache duration time of file information in seconds. 0 means that the cache mechanism is not used.
227+--
228+-- The file information cache is an optimization that is useful for servers dealing with
229+-- static files. However, if files are being modified, it can cause incorrect
230+-- results in some cases. Therefore, we disable it by default. If you know that
231+-- your files will be static or you prefer performance to file consistency,
232+-- it's recommended to turn this on; a reasonable value for those cases is 10.
233+-- Enabling this cache results in drastic performance improvement for file
234+-- transfers.
235+--
236+-- Default value: 0
237+setFileInfoCacheDuration :: Int -> Settings -> Settings
238+setFileInfoCacheDuration x y = y { settingsFileInfoCacheDuration = x }
239+
175-- | Code to run after the listening socket is ready but before entering
240-- | Code to run after the listening socket is ready but before entering
176-- the main event loop. Useful for signaling to tests that they can start
241-- the main event loop. Useful for signaling to tests that they can start
177-- running, or to drop permissions after binding to a restricted port.
242-- running, or to drop permissions after binding to a restricted port.
@@ -195,7 +260,7 @@ setNoParsePath x y = y { settingsNoParsePath = x }
195-- | Get the listening port.
260-- | Get the listening port.
196--
261--
197-- Since 2.1.1
262-- Since 2.1.1
198−getPort :: Settings -> Int
263+getPort :: Settings -> Port
199getPort = settingsPort
264getPort = settingsPort
200
265
201-- | Get the interface to bind to.
266-- | Get the interface to bind to.
@@ -203,3 +268,242 @@ getPort = settingsPort
203-- Since 2.1.1
268-- Since 2.1.1
204getHost :: Settings -> HostPreference
269getHost :: Settings -> HostPreference
205getHost = settingsHost
270getHost = settingsHost
271+
272+-- | Get the action on opening connection.
273+getOnOpen :: Settings -> SockAddr -> IO Bool
274+getOnOpen = settingsOnOpen
275+
276+-- | Get the action on closeing connection.
277+getOnClose :: Settings -> SockAddr -> IO ()
278+getOnClose = settingsOnClose
279+
280+-- | Get the exception handler.
281+getOnException :: Settings -> Maybe Request -> SomeException -> IO ()
282+getOnException = settingsOnException
283+
284+-- | Get the graceful shutdown timeout
285+--
286+-- Since 3.2.8
287+getGracefulShutdownTimeout :: Settings -> Maybe Int
288+getGracefulShutdownTimeout = settingsGracefulShutdownTimeout
289+
290+-- | A code to install shutdown handler.
291+--
292+-- For instance, this code should set up a UNIX signal
293+-- handler. The handler should call the first argument,
294+-- which closes the listen socket, at shutdown.
295+--
296+-- Example usage:
297+--
298+-- @
299+-- settings :: IO () -> 'Settings'
300+-- settings shutdownAction = 'setInstallShutdownHandler' shutdownHandler 'defaultSettings'
301+-- __where__
302+-- shutdownHandler closeSocket =
303+-- void $ 'System.Posix.Signals.installHandler' 'System.Posix.Signals.sigTERM' ('System.Posix.Signals.Catch' $ shutdownAction >> closeSocket) 'Nothing'
304+-- @
305+--
306+-- Note that by default, the graceful shutdown mode lasts indefinitely
307+-- (see 'setGracefulShutdownTimeout'). If you install a signal handler as above,
308+-- upon receiving that signal, the custon shutdown action will run /and/ all
309+-- outstanding requests will be handled.
310+--
311+-- You may instead prefer to do one or both of the following:
312+--
313+-- * Only wait a finite amount of time for outstanding requests to complete,
314+-- using 'setGracefulShutdownTimeout'.
315+-- * Only catch one signal, so the second hard-kills the Warp server, using
316+-- 'System.Posix.Signals.CatchOnce'.
317+--
318+-- Default: does not install any code.
319+--
320+-- Since 3.0.1
321+setInstallShutdownHandler :: (IO () -> IO ()) -> Settings -> Settings
322+setInstallShutdownHandler x y = y { settingsInstallShutdownHandler = x }
323+
324+-- | Default server name to be sent as the \"Server:\" header
325+-- if an application does not set one.
326+-- If an empty string is set, the \"Server:\" header is not sent.
327+-- This is true even if an application set one.
328+--
329+-- Since 3.0.2
330+setServerName :: ByteString -> Settings -> Settings
331+setServerName x y = y { settingsServerName = x }
332+
333+-- | The maximum number of bytes to flush from an unconsumed request body.
334+--
335+-- By default, Warp does not flush the request body so that, if a large body is
336+-- present, the connection is simply terminated instead of wasting time and
337+-- bandwidth on transmitting it. However, some clients do not deal with that
338+-- situation well. You can either change this setting to @Nothing@ to flush the
339+-- entire body in all cases, or in your application ensure that you always
340+-- consume the entire request body.
341+--
342+-- Default: 8192 bytes.
343+--
344+-- Since 3.0.3
345+setMaximumBodyFlush :: Maybe Int -> Settings -> Settings
346+setMaximumBodyFlush x y
347+ | Just x' <- x, x' < 0 = error "setMaximumBodyFlush: must be positive"
348+ | otherwise = y { settingsMaximumBodyFlush = x }
349+
350+-- | Code to fork a new thread to accept a connection.
351+--
352+-- This may be useful if you need OS bound threads, or if
353+-- you wish to develop an alternative threading model.
354+--
355+-- Default: void . forkIOWithUnmask
356+--
357+-- Since 3.0.4
358+setFork :: (((forall a. IO a -> IO a) -> IO ()) -> IO ()) -> Settings -> Settings
359+setFork fork' s = s { settingsFork = fork' }
360+
361+-- | Do not use the PROXY protocol.
362+--
363+-- Since 3.0.5
364+setProxyProtocolNone :: Settings -> Settings
365+setProxyProtocolNone y = y { settingsProxyProtocol = ProxyProtocolNone }
366+
367+-- | Require PROXY header.
368+--
369+-- This is for cases where a "dumb" TCP/SSL proxy is being used, which cannot
370+-- add an @X-Forwarded-For@ HTTP header field but has enabled support for the
371+-- PROXY protocol.
372+--
373+-- See <http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt> and
374+-- <http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#proxy-protocol>.
375+--
376+-- Only the human-readable header format (version 1) is supported. The binary
377+-- header format (version 2) is /not/ supported.
378+--
379+-- Since 3.0.5
380+setProxyProtocolRequired :: Settings -> Settings
381+setProxyProtocolRequired y = y { settingsProxyProtocol = ProxyProtocolRequired }
382+
383+-- | Use the PROXY header if it exists, but also accept
384+-- connections without the header. See 'setProxyProtocolRequired'.
385+--
386+-- WARNING: This is contrary to the PROXY protocol specification and
387+-- using it can indicate a security problem with your
388+-- architecture if the web server is directly accessible
389+-- to the public, since it would allow easy IP address
390+-- spoofing. However, it can be useful in some cases,
391+-- such as if a load balancer health check uses regular
392+-- HTTP without the PROXY header, but proxied
393+-- connections /do/ include the PROXY header.
394+--
395+-- Since 3.0.5
396+setProxyProtocolOptional :: Settings -> Settings
397+setProxyProtocolOptional y = y { settingsProxyProtocol = ProxyProtocolOptional }
398+
399+-- | Size in bytes read to prevent Slowloris attacks. Default value: 2048
400+--
401+-- Since 3.1.2
402+setSlowlorisSize :: Int -> Settings -> Settings
403+setSlowlorisSize x y = y { settingsSlowlorisSize = x }
404+
405+-- | Disable HTTP2.
406+--
407+-- Since 3.1.7
408+setHTTP2Disabled :: Settings -> Settings
409+setHTTP2Disabled y = y { settingsHTTP2Enabled = False }
410+
411+-- | Setting a log function.
412+--
413+-- Since 3.X.X
414+setLogger :: (Request -> H.Status -> Maybe Integer -> IO ()) -- ^ request, status, maybe file-size
415+ -> Settings
416+ -> Settings
417+setLogger lgr y = y { settingsLogger = lgr }
418+
419+-- | Setting a log function for HTTP/2 server push.
420+--
421+-- Since: 3.2.7
422+setServerPushLogger :: (Request -> ByteString -> Integer -> IO ()) -- ^ request, path, file-size
423+ -> Settings
424+ -> Settings
425+setServerPushLogger lgr y = y { settingsServerPushLogger = lgr }
426+
427+-- | Set the graceful shutdown timeout. A timeout of `Nothing' will
428+-- wait indefinitely, and a number, if provided, will be treated as seconds
429+-- to wait for requests to finish, before shutting down the server entirely.
430+--
431+-- Graceful shutdown mode is entered when the server socket is closed; see
432+-- 'setInstallShutdownHandler' for an example of how this could be done in
433+-- response to a UNIX signal.
434+--
435+-- Since 3.2.8
436+setGracefulShutdownTimeout :: Maybe Int
437+ -> Settings -> Settings
438+setGracefulShutdownTimeout time y = y { settingsGracefulShutdownTimeout = time }
439+
440+-- | Set the maximum header size that Warp will tolerate when using HTTP/1.x.
441+--
442+-- Since 3.3.8
443+setMaxTotalHeaderLength :: Int -> Settings -> Settings
444+setMaxTotalHeaderLength maxTotalHeaderLength settings = settings
445+ { settingsMaxTotalHeaderLength = maxTotalHeaderLength }
446+
447+-- | Explicitly pause the slowloris timeout.
448+--
449+-- This is useful for cases where you partially consume a request body. For
450+-- more information, see <https://github.com/yesodweb/wai/issues/351>
451+--
452+-- Since 3.0.10
453+pauseTimeout :: Request -> IO ()
454+pauseTimeout = fromMaybe (return ()) . Vault.lookup pauseTimeoutKey . vault
455+
456+-- | Getting file information of the target file.
457+--
458+-- This function first uses a stat(2) or similar system call
459+-- to obtain information of the target file, then registers
460+-- it into the internal cache.
461+-- From the next time, the information is obtained
462+-- from the cache. This reduces the overhead to call the system call.
463+-- The internal cache is refreshed every duration specified by
464+-- 'setFileInfoCacheDuration'.
465+--
466+-- This function throws an 'IO' exception if the information is not
467+-- available. For instance, the target file does not exist.
468+-- If this function is used an a Request generated by a WAI
469+-- backend besides Warp, it also throws an 'IO' exception.
470+--
471+-- Since 3.1.10
472+getFileInfo :: Request -> FilePath -> IO FileInfo
473+getFileInfo = fromMaybe (\_ -> throwIO (userError "getFileInfo")) . Vault.lookup getFileInfoKey . vault
474+
475+-- | A timeout to limit the time (in milliseconds) waiting for
476+-- FIN for HTTP/1.x. 0 means uses immediate close.
477+-- Default: 0.
478+--
479+-- Since 3.3.5
480+setGracefulCloseTimeout1 :: Int -> Settings -> Settings
481+setGracefulCloseTimeout1 x y = y { settingsGracefulCloseTimeout1 = x }
482+
483+-- | A timeout to limit the time (in milliseconds) waiting for
484+-- FIN for HTTP/1.x. 0 means uses immediate close.
485+--
486+-- Since 3.3.5
487+getGracefulCloseTimeout1 :: Settings -> Int
488+getGracefulCloseTimeout1 = settingsGracefulCloseTimeout1
489+
490+-- | A timeout to limit the time (in milliseconds) waiting for
491+-- FIN for HTTP/2. 0 means uses immediate close.
492+-- Default: 2000.
493+--
494+-- Since 3.3.5
495+setGracefulCloseTimeout2 :: Int -> Settings -> Settings
496+setGracefulCloseTimeout2 x y = y { settingsGracefulCloseTimeout2 = x }
497+
498+-- | A timeout to limit the time (in milliseconds) waiting for
499+-- FIN for HTTP/2. 0 means uses immediate close.
500+--
501+-- Since 3.3.5
502+getGracefulCloseTimeout2 :: Settings -> Int
503+getGracefulCloseTimeout2 = settingsGracefulCloseTimeout2
504+
505+-- | Getting information of client certificate.
506+--
507+-- Since 3.3.5
508+clientCertificate :: Request -> Maybe CertificateChain
509+clientCertificate = join . Vault.lookup getClientCertificateKey . vault
warp/Network/Wai/Handler/Warp/Buffer.hs+96−12
@@ -1,25 +1,109 @@
1−module Network.Wai.Handler.Warp.Buffer where
1+{-# LANGUAGE BangPatterns #-}
2
2
3−import qualified Blaze.ByteString.Builder.Internal.Buffer as B (Buffer (..))
3+module Network.Wai.Handler.Warp.Buffer (
4−import Data.Word (Word8)
4+ bufferSize
5−import Foreign.ForeignPtr (newForeignPtr_)
5+ , allocateBuffer
6−import Foreign.Marshal.Alloc (mallocBytes, free)
6+ , freeBuffer
7−import Foreign.Ptr (Ptr, plusPtr)
7+ , mallocBS
8+ , newBufferPool
9+ , withBufferPool
10+ , toBuilderBuffer
11+ , copy
12+ , bufferIO
13+ ) where
8
14
9−type Buffer = Ptr Word8
15+import qualified Data.ByteString as BS
10−type BufSize = Int
16+import Data.ByteString.Internal (memcpy)
17+import Data.ByteString.Unsafe (unsafeTake, unsafeDrop)
18+import Data.IORef (newIORef, readIORef, writeIORef)
19+import qualified Data.Streaming.ByteString.Builder.Buffer as B (Buffer (..))
20+import Foreign.ForeignPtr
21+import Foreign.Marshal.Alloc (mallocBytes, free, finalizerFree)
22+import Foreign.Ptr (castPtr, plusPtr)
11
23
12−-- FIXME come up with good values here
24+import Network.Wai.Handler.Warp.Imports
25+import Network.Wai.Handler.Warp.Types
26+
27+----------------------------------------------------------------
28+
29+-- | The default size of the write buffer: 16384 (2^14 = 1024 * 16).
30+-- This is the maximum size of TLS record.
31+-- This is also the maximum size of HTTP/2 frame payload
32+-- (excluding frame header).
13bufferSize :: BufSize
33bufferSize :: BufSize
14−bufferSize = 4096
34+bufferSize = 16384
15
35
36+-- | Allocating a buffer with malloc().
16allocateBuffer :: Int -> IO Buffer
37allocateBuffer :: Int -> IO Buffer
17allocateBuffer = mallocBytes
38allocateBuffer = mallocBytes
18
39
40+-- | Releasing a buffer with free().
19freeBuffer :: Buffer -> IO ()
41freeBuffer :: Buffer -> IO ()
20freeBuffer = free
42freeBuffer = free
21
43
44+----------------------------------------------------------------
45+
46+largeBufferSize :: Int
47+largeBufferSize = 16384
48+
49+minBufferSize :: Int
50+minBufferSize = 2048
51+
52+newBufferPool :: IO BufferPool
53+newBufferPool = newIORef BS.empty
54+
55+mallocBS :: Int -> IO ByteString
56+mallocBS size = do
57+ ptr <- allocateBuffer size
58+ fptr <- newForeignPtr finalizerFree ptr
59+ return $! PS fptr 0 size
60+{-# INLINE mallocBS #-}
61+
62+usefulBuffer :: ByteString -> Bool
63+usefulBuffer buffer = BS.length buffer >= minBufferSize
64+{-# INLINE usefulBuffer #-}
65+
66+getBuffer :: BufferPool -> IO ByteString
67+getBuffer pool = do
68+ buffer <- readIORef pool
69+ if usefulBuffer buffer then return buffer else mallocBS largeBufferSize
70+{-# INLINE getBuffer #-}
71+
72+putBuffer :: BufferPool -> ByteString -> IO ()
73+putBuffer pool buffer = writeIORef pool buffer
74+{-# INLINE putBuffer #-}
75+
76+withForeignBuffer :: ByteString -> ((Buffer, BufSize) -> IO Int) -> IO Int
77+withForeignBuffer (PS ps s l) f = withForeignPtr ps $ \p -> f (castPtr p `plusPtr` s, l)
78+{-# INLINE withForeignBuffer #-}
79+
80+withBufferPool :: BufferPool -> ((Buffer, BufSize) -> IO Int) -> IO ByteString
81+withBufferPool pool f = do
82+ buffer <- getBuffer pool
83+ consumed <- withForeignBuffer buffer f
84+ putBuffer pool $! unsafeDrop consumed buffer
85+ return $! unsafeTake consumed buffer
86+{-# INLINE withBufferPool #-}
87+
88+----------------------------------------------------------------
89+--
90+-- Utilities
91+--
92+
22−toBlazeBuffer :: Buffer -> BufSize -> IO B.Buffer
93+toBuilderBuffer :: Buffer -> BufSize -> IO B.Buffer
23−toBlazeBuffer ptr size = do
94+toBuilderBuffer ptr size = do
24 fptr <- newForeignPtr_ ptr
95 fptr <- newForeignPtr_ ptr
25 return $ B.Buffer fptr ptr ptr (ptr `plusPtr` size)
96 return $ B.Buffer fptr ptr ptr (ptr `plusPtr` size)
97+
98+-- | Copying the bytestring to the buffer.
99+-- This function returns the point where the next copy should start.
100+copy :: Buffer -> ByteString -> IO Buffer
101+copy !ptr (PS fp o l) = withForeignPtr fp $ \p -> do
102+ memcpy ptr (p `plusPtr` o) (fromIntegral l)
103+ return $! ptr `plusPtr` l
104+{-# INLINE copy #-}
105+
106+bufferIO :: Buffer -> Int -> (ByteString -> IO ()) -> IO ()
107+bufferIO ptr siz io = do
108+ fptr <- newForeignPtr_ ptr
109+ io $ PS fptr 0 siz
warp/Network/Wai/Handler/Warp/Conduit.hs+106−97
@@ -1,41 +1,40 @@
1+{-# LANGUAGE CPP #-}
2+
1module Network.Wai.Handler.Warp.Conduit where
3module Network.Wai.Handler.Warp.Conduit where
2
4
3−import Control.Applicative
4import Control.Exception
5import Control.Exception
5−import Control.Monad (unless)
6−import Control.Monad.IO.Class (MonadIO, liftIO)
7−import Control.Monad.Trans.Class (lift)
8−import Data.ByteString (ByteString)
9−import Data.ByteString.Lazy.Char8 (pack)
10import qualified Data.ByteString as S
6import qualified Data.ByteString as S
11−import qualified Data.ByteString.Lazy as L
12−import Data.Conduit
13−import qualified Data.Conduit.Binary as CB
14−import Data.Conduit.Internal (ResumableSource (..))
15−import qualified Data.Conduit.List as CL
16import qualified Data.IORef as I
7import qualified Data.IORef as I
17−import Data.Word (Word, Word8)
8+
9+import Network.Wai.Handler.Warp.Imports
18import Network.Wai.Handler.Warp.Types
10import Network.Wai.Handler.Warp.Types
19
11
20----------------------------------------------------------------
12----------------------------------------------------------------
21
13
22-- | Contains a @Source@ and a byte count that is still to be read in.
14-- | Contains a @Source@ and a byte count that is still to be read in.
23−newtype IsolatedBSSource = IsolatedBSSource (I.IORef (Int, ResumableSource IO ByteString))
15+data ISource = ISource !Source !(I.IORef Int)
16+
17+mkISource :: Source -> Int -> IO ISource
18+mkISource src cnt = do
19+ ref <- I.newIORef cnt
20+ return $! ISource src ref
24
21
25-- | Given an @IsolatedBSSource@ provide a @Source@ that only allows up to the
22-- | Given an @IsolatedBSSource@ provide a @Source@ that only allows up to the
26-- specified number of bytes to be passed downstream. All leftovers should be
23-- specified number of bytes to be passed downstream. All leftovers should be
27-- retained within the @Source@. If there are not enough bytes available,
24-- retained within the @Source@. If there are not enough bytes available,
28-- throws a @ConnectionClosedByPeer@ exception.
25-- throws a @ConnectionClosedByPeer@ exception.
29−ibsIsolate :: IsolatedBSSource -> Source IO ByteString
26+readISource :: ISource -> IO ByteString
30−ibsIsolate ibs@(IsolatedBSSource ref) = do
27+readISource (ISource src ref) = do
31− (count, src) <- liftIO $ I.readIORef ref
28+ count <- I.readIORef ref
32− unless (count == 0) $ do
29+ if count == 0
33− -- Get the next chunk (if available) and the updated source
30+ then return S.empty
34− (src', mbs) <- lift $ src $$++ CL.head
31+ else do
32+
33+ bs <- readSource src
35
34
36 -- If no chunk available, then there aren't enough bytes in the
35 -- If no chunk available, then there aren't enough bytes in the
37 -- stream. Throw a ConnectionClosedByPeer
36 -- stream. Throw a ConnectionClosedByPeer
38− bs <- maybe (liftIO $ throwIO ConnectionClosedByPeer) return mbs
37+ when (S.null bs) $ throwIO ConnectionClosedByPeer
39
38
40 let -- How many of the bytes in this chunk to send downstream
39 let -- How many of the bytes in this chunk to send downstream
41 toSend = min count (S.length bs)
40 toSend = min count (S.length bs)
@@ -48,16 +47,8 @@ ibsIsolate ibs@(IsolatedBSSource ref) = do
48 -- downstream, and then loop on this function for the
47 -- downstream, and then loop on this function for the
49 -- next chunk.
48 -- next chunk.
50 | count' > 0 -> do
49 | count' > 0 -> do
51− liftIO $ I.writeIORef ref (count', src')
50+ I.writeIORef ref count'
52− yield bs
51+ return bs
53− ibsIsolate ibs
54−
55− -- The expected count is the total size of the chunk we
56− -- just read. Send this chunk downstream, and then
57− -- terminate the stream.
58− | count == S.length bs -> do
59− liftIO $ I.writeIORef ref (count', src')
60− yield bs
61
52
62 -- Some of the bytes in this chunk should not be sent
53 -- Some of the bytes in this chunk should not be sent
63 -- downstream. Split up the chunk into the sent and
54 -- downstream. Split up the chunk into the sent and
@@ -65,84 +56,107 @@ ibsIsolate ibs@(IsolatedBSSource ref) = do
65 -- source, and send the rest of the chunk downstream.
56 -- source, and send the rest of the chunk downstream.
66 | otherwise -> do
57 | otherwise -> do
67 let (x, y) = S.splitAt toSend bs
58 let (x, y) = S.splitAt toSend bs
68− liftIO $ I.writeIORef ref (count', fmapResume (yield y >>) src')
59+ leftoverSource src y
69− yield x
60+ assert (count' == 0) $ I.writeIORef ref count'
70−
61+ return x
71−-- | Extract the underlying @Source@ from an @IsolatedBSSource@, which will not
72−-- perform any more isolation.
73−ibsDone :: IsolatedBSSource -> IO (ResumableSource IO ByteString)
74−ibsDone (IsolatedBSSource ref) = snd <$> I.readIORef ref
75
62
76----------------------------------------------------------------
63----------------------------------------------------------------
77
64
65+data CSource = CSource !Source !(I.IORef ChunkState)
66+
78data ChunkState = NeedLen
67data ChunkState = NeedLen
79 | NeedLenNewline
68 | NeedLenNewline
80 | HaveLen Word
69 | HaveLen Word
81−
70+ | DoneChunking
82−bsCRLF :: L.ByteString
71+ deriving Show
83−bsCRLF = pack "\r\n"
72+
84−
73+mkCSource :: Source -> IO CSource
85−chunkedSource :: MonadIO m
74+mkCSource src = do
86− => I.IORef (ResumableSource m ByteString, ChunkState)
75+ ref <- I.newIORef NeedLen
87− -> Source m ByteString
76+ return $! CSource src ref
88−chunkedSource ipair = do
77+
78+readCSource :: CSource -> IO ByteString
79+readCSource (CSource src ref) = do
89− (src, mlen) <- liftIO $ I.readIORef ipair
80+ mlen <- I.readIORef ref
90− go src mlen
81+ go mlen
91 where
82 where
92− go' src front = do
83+ withLen 0 bs = do
93− (src', (len, bs)) <- lift $ src $$++ front getLen
84+ leftoverSource src bs
94− let src''
85+ dropCRLF
95− | S.null bs = src'
86+ yield' S.empty DoneChunking
87+ withLen len bs
88+ | S.null bs = do
89+ -- FIXME should this throw an exception if len > 0?
90+ I.writeIORef ref DoneChunking
91+ return S.empty
96− | otherwise = fmapResume (yield bs >>) src'
92+ | otherwise =
97− go src'' $ HaveLen len
93+ case S.length bs `compare` fromIntegral len of
98−
94+ EQ -> yield' bs NeedLenNewline
95+ LT -> yield' bs $ HaveLen $ len - fromIntegral (S.length bs)
96+ GT -> do
97+ let (x, y) = S.splitAt (fromIntegral len) bs
98+ leftoverSource src y
99+ yield' x NeedLenNewline
100+
101+ yield' bs mlen = do
102+ I.writeIORef ref mlen
103+ return bs
104+
105+ dropCRLF = do
106+ bs <- readSource src
107+ case S.uncons bs of
108+ Nothing -> return ()
109+ Just (13, bs') -> dropLF bs'
110+ Just (10, bs') -> leftoverSource src bs'
111+ Just _ -> leftoverSource src bs
112+
113+ dropLF bs =
114+ case S.uncons bs of
115+ Nothing -> do
116+ bs2 <- readSource' src
117+ unless (S.null bs2) $ dropLF bs2
118+ Just (10, bs') -> leftoverSource src bs'
119+ Just _ -> leftoverSource src bs
120+
99− go src NeedLen = go' src id
121+ go NeedLen = getLen
100− go src NeedLenNewline = go' src (CB.take 2 >>)
122+ go NeedLenNewline = dropCRLF >> getLen
101− go src (HaveLen 0) = do
123+ go (HaveLen 0) = do
102 -- Drop the final CRLF
124 -- Drop the final CRLF
103− (src', ()) <- lift $ src $$++ do
125+ dropCRLF
104− crlf <- CB.take 2
126+ I.writeIORef ref DoneChunking
105− unless (crlf == bsCRLF) $ leftover $ S.concat $ L.toChunks crlf
127+ return S.empty
106− liftIO $ I.writeIORef ipair (src', HaveLen 0)
107− go src (HaveLen len) = do
128+ go (HaveLen len) = do
108− (src', mbs) <- lift $ src $$++ CL.head
129+ bs <- readSource src
109− case mbs of
130+ withLen len bs
110− Nothing -> liftIO $ I.writeIORef ipair (src', HaveLen 0)
131+ go DoneChunking = return S.empty
111− Just bs ->
132+
112− case S.length bs `compare` fromIntegral len of
133+ -- Get the length from the source, and then pass off control to withLen
113− EQ -> yield' src' NeedLenNewline bs
114− LT -> do
115− let mlen = HaveLen $ len - fromIntegral (S.length bs)
116− yield' src' mlen bs
117− GT -> do
118− let (x, y) = S.splitAt (fromIntegral len) bs
119− let src'' = fmapResume (yield y >>) src'
120− yield' src'' NeedLenNewline x
121−
122− yield' src mlen bs = do
123− liftIO $ I.writeIORef ipair (src, mlen)
124− yield bs
125− go src mlen
126−
127− getLen :: Monad m => Sink ByteString m (Word, ByteString)
128 getLen = do
134 getLen = do
129− mbs <- CL.head
135+ bs <- readSource src
130− case mbs of
136+ if S.null bs
137+ then do
138+ I.writeIORef ref $ assert False $ HaveLen 0
131− Nothing -> return (0, S.empty)
139+ return S.empty
132− Just bs -> do
140+ else do
133 (x, y) <-
141 (x, y) <-
134− case S.breakByte 10 bs of
142+ case S.break (== 10) bs of
135 (x, y)
143 (x, y)
136 | S.null y -> do
144 | S.null y -> do
137− mbs2 <- CL.head
145+ bs2 <- readSource' src
138− case mbs2 of
146+ return $ if S.null bs2
139− Nothing -> return (x, y)
147+ then (x, y)
140− Just bs2 -> return $ S.breakByte 10 $ bs `S.append` bs2
148+ else S.break (== 10) $ bs `S.append` bs2
141 | otherwise -> return (x, y)
149 | otherwise -> return (x, y)
142 let w =
150 let w =
143 S.foldl' (\i c -> i * 16 + fromIntegral (hexToWord c)) 0
151 S.foldl' (\i c -> i * 16 + fromIntegral (hexToWord c)) 0
144 $ S.takeWhile isHexDigit x
152 $ S.takeWhile isHexDigit x
153+
154+ let y' = S.drop 1 y
155+ y'' <-
156+ if S.null y'
157+ then readSource src
145− return (w, S.drop 1 y)
158+ else return y'
159+ withLen w y''
146
160
147 hexToWord w
161 hexToWord w
148 | w < 58 = w - 48
162 | w < 58 = w - 48
@@ -153,8 +167,3 @@ isHexDigit :: Word8 -> Bool
153isHexDigit w = w >= 48 && w <= 57
167isHexDigit w = w >= 48 && w <= 57
154 || w >= 65 && w <= 70
168 || w >= 65 && w <= 70
155 || w >= 97 && w <= 102
169 || w >= 97 && w <= 102
156−
157−----------------------------------------------------------------
158−
159−fmapResume :: (Source m o1 -> Source m o2) -> ResumableSource m o1 -> ResumableSource m o2
160−fmapResume f (ResumableSource src m) = ResumableSource (f src) m
warp/Network/Wai/Handler/Warp/Counter.hsnew file+30
@@ -0,0 +1,30 @@
1+{-# LANGUAGE CPP #-}
2+
3+module Network.Wai.Handler.Warp.Counter (
4+ Counter
5+ , newCounter
6+ , waitForZero
7+ , increase
8+ , decrease
9+ ) where
10+
11+import Control.Concurrent.STM
12+
13+import Network.Wai.Handler.Warp.Imports
14+
15+
16+newtype Counter = Counter (TVar Int)
17+
18+newCounter :: IO Counter
19+newCounter = Counter <$> newTVarIO 0
20+
21+waitForZero :: Counter -> IO ()
22+waitForZero (Counter ref) = atomically $ do
23+ x <- readTVar ref
24+ unless (x == 0) retry
25+
26+increase :: Counter -> IO ()
27+increase (Counter ref) = atomically $ modifyTVar' ref $ \x -> x + 1
28+
29+decrease :: Counter -> IO ()
30+decrease (Counter ref) = atomically $ modifyTVar' ref $ \x -> x - 1
warp/Network/Wai/Handler/Warp/Date.hs+21−39
@@ -2,58 +2,40 @@
2
2
3module Network.Wai.Handler.Warp.Date (
3module Network.Wai.Handler.Warp.Date (
4 withDateCache
4 withDateCache
5− , getDate
6− , DateCache
7 , GMTDate
5 , GMTDate
8 ) where
6 ) where
9
7
10−import Control.Applicative
8+import Control.AutoUpdate (defaultUpdateSettings, updateAction, mkAutoUpdate)
11−import Control.Concurrent
12−import Control.Exception
13−import Control.Monad
14−import Data.ByteString.Char8
9+import Data.ByteString
15−import Data.IORef
10+import Network.HTTP.Date
16
11
17#if WINDOWS
12#if WINDOWS
18−import Data.Time
13+import Data.Time (UTCTime, getCurrentTime)
19−import System.Locale
14+import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
15+import Foreign.C.Types (CTime(..))
20#else
16#else
21−import Network.HTTP.Date
22import System.Posix (epochTime)
17import System.Posix (epochTime)
23#endif
18#endif
24
19
25-- | The type of the Date header value.
20-- | The type of the Date header value.
26type GMTDate = ByteString
21type GMTDate = ByteString
27
22
28−-- | The type of the cache of the Date header value.
29−data DateCache = DateCache (IORef GMTDate)
30−
31-- | Creating 'DateCache' and executing the action.
23-- | Creating 'DateCache' and executing the action.
32−withDateCache :: (DateCache -> IO a) -> IO a
24+withDateCache :: (IO GMTDate -> IO a) -> IO a
33−withDateCache action = bracket initialize
25+withDateCache action = initialize >>= action
34− (\(t,_) -> killThread t)
26+
35− (\(_,dc) -> action dc)
36−
37−initialize :: IO (ThreadId, DateCache)
27+initialize :: IO (IO GMTDate)
38−initialize = do
28+initialize = mkAutoUpdate defaultUpdateSettings {
39− dc <- DateCache <$> (getCurrentGMTDate >>= newIORef)
29+ updateAction = formatHTTPDate <$> getCurrentHTTPDate
40− t <- forkIO $ forever $ do
30+ }
41− threadDelay 1000000
31+
42− update dc
43− return (t, dc)
44−
45−-- | Getting 'GMTDate' based on 'DateCache'.
46−getDate :: DateCache -> IO GMTDate
47−getDate (DateCache ref) = readIORef ref
48−
49−update :: DateCache -> IO ()
50−update (DateCache ref) = getCurrentGMTDate >>= writeIORef ref
51−
52−getCurrentGMTDate :: IO GMTDate
53#ifdef WINDOWS
32#ifdef WINDOWS
54−getCurrentGMTDate = formatDate <$> getCurrentTime
33+uToH :: UTCTime -> HTTPDate
55− where
34+uToH = epochTimeToHTTPDate . CTime . truncate . utcTimeToPOSIXSeconds
56− formatDate = pack . formatTime defaultTimeLocale "%a, %d %b %Y %H:%M:%S GMT"
35+
36+getCurrentHTTPDate :: IO HTTPDate
37+getCurrentHTTPDate = uToH <$> getCurrentTime
57#else
38#else
39+getCurrentHTTPDate :: IO HTTPDate
58−getCurrentGMTDate = formatHTTPDate . epochTimeToHTTPDate <$> epochTime
40+getCurrentHTTPDate = epochTimeToHTTPDate <$> epochTime
59#endif
41#endif
warp/Network/Wai/Handler/Warp/FdCache.hs+83−102
@@ -1,36 +1,47 @@
1−{-# LANGUAGE BangPatterns, FlexibleInstances, CPP #-}
1+{-# LANGUAGE BangPatterns, CPP #-}
2
2
3-- | File descriptor cache to avoid locks in kernel.
3-- | File descriptor cache to avoid locks in kernel.
4
4
5−#ifndef SENDFILEFD
6module Network.Wai.Handler.Warp.FdCache (
5module Network.Wai.Handler.Warp.FdCache (
7 withFdCache
6 withFdCache
8− , MutableFdCache
7+ , Fd
9 , Refresh
8 , Refresh
9+#ifndef WINDOWS
10+ , openFile
11+ , closeFile
12+ , setFileCloseOnExec
13+#endif
10 ) where
14 ) where
11
15
16+#ifndef WINDOWS
17+import Control.Exception (bracket)
18+import Control.Reaper
19+import Data.IORef
20+import Network.Wai.Handler.Warp.MultiMap as MM
21+import System.Posix.IO (openFd, OpenFileFlags(..), defaultFileFlags, OpenMode(ReadOnly), closeFd, FdOption(CloseOnExec), setFdOption)
22+#endif
23+import System.Posix.Types (Fd)
24+
25+----------------------------------------------------------------
26+
27+-- | An action to activate a Fd cache entry.
12type Refresh = IO ()
28type Refresh = IO ()
13−data MutableFdCache = MutableFdCache
14
29
15−withFdCache :: Int -> (Maybe MutableFdCache -> IO a) -> IO a
30+getFdNothing :: FilePath -> IO (Maybe Fd, Refresh)
16−withFdCache _ f = f Nothing
31+getFdNothing _ = return (Nothing, return ())
17−#else
18−module Network.Wai.Handler.Warp.FdCache (
19− withFdCache
20− , getFd
21− , MutableFdCache
22− , Refresh
23− ) where
24
32
25−import Control.Applicative ((<$>), (<*>))
33+----------------------------------------------------------------
26−import Control.Concurrent (threadDelay)
34+
27−import Control.Exception (bracket, mask_)
35+-- | Creating 'MutableFdCache' and executing the action in the second
28−import Data.Hashable (hash)
36+-- argument. The first argument is a cache duration in second.
29−import Network.Wai.Handler.Warp.IORef
37+withFdCache :: Int -> ((FilePath -> IO (Maybe Fd, Refresh)) -> IO a) -> IO a
30−import Network.Wai.Handler.Warp.MultiMap
38+#ifdef WINDOWS
31−import Network.Wai.Handler.Warp.Thread
39+withFdCache _ action = action getFdNothing
32−import System.Posix.IO (openFd, OpenFileFlags(..), defaultFileFlags, OpenMode(ReadOnly), closeFd)
40+#else
33−import System.Posix.Types (Fd)
41+withFdCache 0 action = action getFdNothing
42+withFdCache duration action = bracket (initialize duration)
43+ terminate
44+ (action . getFd)
34
45
35----------------------------------------------------------------
46----------------------------------------------------------------
36
47
@@ -38,9 +49,6 @@ data Status = Active | Inactive
38
49
39newtype MutableStatus = MutableStatus (IORef Status)
50newtype MutableStatus = MutableStatus (IORef Status)
40
51
41−-- | An action to activate a Fd cache entry.
42−type Refresh = IO ()
43−
44status :: MutableStatus -> IO Status
52status :: MutableStatus -> IO Status
45status (MutableStatus ref) = readIORef ref
53status (MutableStatus ref) = readIORef ref
46
54
@@ -55,107 +63,80 @@ inactive (MutableStatus ref) = writeIORef ref Inactive
55
63
56----------------------------------------------------------------
64----------------------------------------------------------------
57
65
58−data FdEntry = FdEntry !FilePath !Fd !MutableStatus
66+data FdEntry = FdEntry !Fd !MutableStatus
67+
68+openFile :: FilePath -> IO Fd
69+openFile path = do
70+ fd <- openFd path ReadOnly Nothing defaultFileFlags{nonBlock=False}
71+ setFileCloseOnExec fd
72+ return fd
73+
74+closeFile :: Fd -> IO ()
75+closeFile = closeFd
59
76
60newFdEntry :: FilePath -> IO FdEntry
77newFdEntry :: FilePath -> IO FdEntry
61−newFdEntry path = FdEntry path
78+newFdEntry path = FdEntry <$> openFile path <*> newActiveStatus
62− <$> openFd path ReadOnly Nothing defaultFileFlags{nonBlock=True}
79+
63− <*> newActiveStatus
80+setFileCloseOnExec :: Fd -> IO ()
81+setFileCloseOnExec fd = setFdOption fd CloseOnExec True
64
82
65----------------------------------------------------------------
83----------------------------------------------------------------
66
84
67−type Hash = Int
68−type FdCache = MMap Hash FdEntry
85+type FdCache = MultiMap FdEntry
69
86
70-- | Mutable Fd cacher.
87-- | Mutable Fd cacher.
71−newtype MutableFdCache = MutableFdCache (IORef FdCache)
88+newtype MutableFdCache = MutableFdCache (Reaper FdCache (FilePath,FdEntry))
72
89
73fdCache :: MutableFdCache -> IO FdCache
90fdCache :: MutableFdCache -> IO FdCache
74−fdCache (MutableFdCache ref) = readIORef ref
91+fdCache (MutableFdCache reaper) = reaperRead reaper
75−
76−swapWithNew :: IORef FdCache -> IO FdCache
77−swapWithNew ref = atomicModifyIORef' ref $ \t -> (empty, t)
78−
79−update :: MutableFdCache -> (FdCache -> FdCache) -> IO ()
80−update (MutableFdCache ref) = update' ref
81−
82−update' :: IORef FdCache -> (FdCache -> FdCache) -> IO ()
83−update' ref f = atomicModifyIORef' ref $ \t -> (f t, ())
84−
85−look :: MutableFdCache -> FilePath -> Hash -> IO (Maybe FdEntry)
86−look mfc path key = searchWith key check <$> fdCache mfc
87− where
88− check (One ent@(FdEntry path' _ _))
89− | path == path' = Just ent
90− | otherwise = Nothing
91− check (Tom ent@(FdEntry path' _ _) vs)
92− | path == path' = Just ent
93− | otherwise = check vs
94−
95−----------------------------------------------------------------
96
92
97−-- | Creating 'MutableFdCache' and executing the action in the second
93+look :: MutableFdCache -> FilePath -> IO (Maybe FdEntry)
98−-- argument. The first argument is a cache duration in second.
94+look mfc path = MM.lookup path <$> fdCache mfc
99−withFdCache :: Int -> (Maybe MutableFdCache -> IO a) -> IO a
100−withFdCache duration action = bracket (initialize duration)
101− terminate
102− action
103
95
104----------------------------------------------------------------
96----------------------------------------------------------------
105
97
106-- The first argument is a cache duration in second.
98-- The first argument is a cache duration in second.
107−initialize :: Int -> IO (Maybe MutableFdCache)
99+initialize :: Int -> IO MutableFdCache
108−initialize 0 = return Nothing
109−initialize duration = do
100+initialize duration = MutableFdCache <$> mkReaper settings
110− ref' <- forkIOwithBreakableForever empty $ \ref -> do
111− threadDelay duration
112− clean ref
113− return (Just (MutableFdCache ref'))
114−
115−clean :: IORef FdCache -> IO ()
116−clean ref = do
117− old <- swapWithNew ref
118− new <- pruneWith old prune
119− update' ref (merge new)
120−
121−prune :: t -> Some FdEntry -> IO [(t, Some FdEntry)]
122−prune k v@(One (FdEntry _ fd mst)) = status mst >>= prune'
123 where
101 where
124− prune' Active = inactive mst >> return [(k,v)]
102+ settings = defaultReaperSettings {
125− prune' Inactive = closeFd fd >> return []
103+ reaperAction = clean
126−prune k (Tom ent@(FdEntry _ fd mst) vs) = status mst >>= prune'
104+ , reaperDelay = duration
105+ , reaperCons = uncurry insert
106+ , reaperNull = isEmpty
107+ , reaperEmpty = empty
108+ }
109+
110+clean :: FdCache -> IO (FdCache -> FdCache)
111+clean old = do
112+ new <- pruneWith old prune
113+ return $ merge new
127 where
114 where
128− prune' Active = do
115+ prune (_,FdEntry fd mst) = status mst >>= act
116+ where
129− inactive mst
117+ act Active = inactive mst >> return True
130− zs <- prune k vs
131− case zs of
132− [] -> return [(k,One ent)]
133− [(_,zvs)] -> return [(k,Tom ent zvs)]
134− _ -> error "prune"
135− prune' Inactive = closeFd fd >> prune k vs
118+ act Inactive = closeFd fd >> return False
136
119
137----------------------------------------------------------------
120----------------------------------------------------------------
138
121
139−terminate :: Maybe MutableFdCache -> IO ()
122+terminate :: MutableFdCache -> IO ()
140−terminate Nothing = return ()
141−terminate (Just (MutableFdCache ref)) = mask_ $ do
123+terminate (MutableFdCache reaper) = do
142− !t <- breakForever ref
124+ !t <- reaperStop reaper
143− mapM_ closeIt $ toList t
125+ mapM_ (closeIt . snd) $ toList t
144 where
126 where
145− closeIt (_, FdEntry _ fd _) = closeFd fd
127+ closeIt (FdEntry fd _) = closeFd fd
146
128
147----------------------------------------------------------------
129----------------------------------------------------------------
148
130
149-- | Getting 'Fd' and 'Refresh' from the mutable Fd cacher.
131-- | Getting 'Fd' and 'Refresh' from the mutable Fd cacher.
150−getFd :: MutableFdCache -> FilePath -> IO (Fd, Refresh)
132+getFd :: MutableFdCache -> FilePath -> IO (Maybe Fd, Refresh)
151−getFd mfc path = look mfc path key >>= getFd'
133+getFd mfc@(MutableFdCache reaper) path = look mfc path >>= get
152 where
134 where
153− key = hash path
154− getFd' Nothing = do
135+ get Nothing = do
155− ent@(FdEntry _ fd mst) <- newFdEntry path
136+ ent@(FdEntry fd mst) <- newFdEntry path
156− update mfc (insert key ent)
137+ reaperAdd reaper (path,ent)
157− return (fd, refresh mst)
138+ return (Just fd, refresh mst)
158− getFd' (Just (FdEntry _ fd mst)) = do
139+ get (Just (FdEntry fd mst)) = do
159 refresh mst
140 refresh mst
160− return (fd, refresh mst)
141+ return (Just fd, refresh mst)
161#endif
142#endif
warp/Network/Wai/Handler/Warp/File.hsnew file+152
@@ -0,0 +1,152 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE BangPatterns #-}
4+
5+module Network.Wai.Handler.Warp.File (
6+ RspFileInfo(..)
7+ , conditionalRequest
8+ , addContentHeadersForFilePart
9+ , H.parseByteRanges
10+ ) where
11+
12+import Data.Array ((!))
13+import qualified Data.ByteString.Char8 as C8 (pack)
14+import Network.HTTP.Date
15+import qualified Network.HTTP.Types as H
16+import qualified Network.HTTP.Types.Header as H
17+import Network.Wai
18+
19+import qualified Network.Wai.Handler.Warp.FileInfoCache as I
20+import Network.Wai.Handler.Warp.Header
21+import Network.Wai.Handler.Warp.Imports
22+import Network.Wai.Handler.Warp.PackInt
23+
24+
25+-- $setup
26+-- >>> import Test.QuickCheck
27+
28+----------------------------------------------------------------
29+
30+data RspFileInfo = WithoutBody H.Status
31+ | WithBody H.Status H.ResponseHeaders Integer Integer
32+ deriving (Eq,Show)
33+
34+----------------------------------------------------------------
35+
36+conditionalRequest :: I.FileInfo
37+ -> H.ResponseHeaders
38+ -> IndexedHeader -- ^ Response
39+ -> IndexedHeader -- ^ Request
40+ -> RspFileInfo
41+conditionalRequest finfo hs0 rspidx reqidx = case condition of
42+ nobody@(WithoutBody _) -> nobody
43+ WithBody s _ off len -> let !hs1 = addContentHeaders hs0 off len size
44+ !hasLM = isJust $ rspidx ! fromEnum ResLastModified
45+ !hs = [ (H.hLastModified,date) | not hasLM ] ++ hs1
46+ in WithBody s hs off len
47+ where
48+ !mtime = I.fileInfoTime finfo
49+ !size = I.fileInfoSize finfo
50+ !date = I.fileInfoDate finfo
51+ !mcondition = ifmodified reqidx size mtime
52+ <|> ifunmodified reqidx size mtime
53+ <|> ifrange reqidx size mtime
54+ !condition = fromMaybe (unconditional reqidx size) mcondition
55+
56+----------------------------------------------------------------
57+
58+ifModifiedSince :: IndexedHeader -> Maybe HTTPDate
59+ifModifiedSince reqidx = reqidx ! fromEnum ReqIfModifiedSince >>= parseHTTPDate
60+
61+ifUnmodifiedSince :: IndexedHeader -> Maybe HTTPDate
62+ifUnmodifiedSince reqidx = reqidx ! fromEnum ReqIfUnmodifiedSince >>= parseHTTPDate
63+
64+ifRange :: IndexedHeader -> Maybe HTTPDate
65+ifRange reqidx = reqidx ! fromEnum ReqIfRange >>= parseHTTPDate
66+
67+----------------------------------------------------------------
68+
69+ifmodified :: IndexedHeader -> Integer -> HTTPDate -> Maybe RspFileInfo
70+ifmodified reqidx size mtime = do
71+ date <- ifModifiedSince reqidx
72+ return $ if date /= mtime
73+ then unconditional reqidx size
74+ else WithoutBody H.notModified304
75+
76+ifunmodified :: IndexedHeader -> Integer -> HTTPDate -> Maybe RspFileInfo
77+ifunmodified reqidx size mtime = do
78+ date <- ifUnmodifiedSince reqidx
79+ return $ if date == mtime
80+ then unconditional reqidx size
81+ else WithoutBody H.preconditionFailed412
82+
83+ifrange :: IndexedHeader -> Integer -> HTTPDate -> Maybe RspFileInfo
84+ifrange reqidx size mtime = do
85+ date <- ifRange reqidx
86+ rng <- reqidx ! fromEnum ReqRange
87+ return $ if date == mtime
88+ then parseRange rng size
89+ else WithBody H.ok200 [] 0 size
90+
91+unconditional :: IndexedHeader -> Integer -> RspFileInfo
92+unconditional reqidx size = case reqidx ! fromEnum ReqRange of
93+ Nothing -> WithBody H.ok200 [] 0 size
94+ Just rng -> parseRange rng size
95+
96+----------------------------------------------------------------
97+
98+parseRange :: ByteString -> Integer -> RspFileInfo
99+parseRange rng size = case H.parseByteRanges rng of
100+ Nothing -> WithoutBody H.requestedRangeNotSatisfiable416
101+ Just [] -> WithoutBody H.requestedRangeNotSatisfiable416
102+ Just (r:_) -> let (!beg, !end) = checkRange r size
103+ !len = end - beg + 1
104+ s = if beg == 0 && end == size - 1 then
105+ H.ok200
106+ else
107+ H.partialContent206
108+ in WithBody s [] beg len
109+
110+checkRange :: H.ByteRange -> Integer -> (Integer, Integer)
111+checkRange (H.ByteRangeFrom beg) size = (beg, size - 1)
112+checkRange (H.ByteRangeFromTo beg end) size = (beg, min (size - 1) end)
113+checkRange (H.ByteRangeSuffix count) size = (max 0 (size - count), size - 1)
114+
115+----------------------------------------------------------------
116+
117+-- | @contentRangeHeader beg end total@ constructs a Content-Range 'H.Header'
118+-- for the range specified.
119+contentRangeHeader :: Integer -> Integer -> Integer -> H.Header
120+contentRangeHeader beg end total = (H.hContentRange, range)
121+ where
122+ range = C8.pack
123+ -- building with ShowS
124+ $ 'b' : 'y': 't' : 'e' : 's' : ' '
125+ : (if beg > end then ('*':) else
126+ showInt beg
127+ . ('-' :)
128+ . showInt end)
129+ ( '/'
130+ : showInt total "")
131+
132+addContentHeaders :: H.ResponseHeaders -> Integer -> Integer -> Integer -> H.ResponseHeaders
133+addContentHeaders hs off len size
134+ | len == size = hs'
135+ | otherwise = let !ctrng = contentRangeHeader off (off + len - 1) size
136+ in ctrng:hs'
137+ where
138+ !lengthBS = packIntegral len
139+ !hs' = (H.hContentLength, lengthBS) : (H.hAcceptRanges,"bytes") : hs
140+
141+-- |
142+--
143+-- >>> addContentHeadersForFilePart [] (FilePart 2 10 16)
144+-- [("Content-Range","bytes 2-11/16"),("Content-Length","10"),("Accept-Ranges","bytes")]
145+-- >>> addContentHeadersForFilePart [] (FilePart 0 16 16)
146+-- [("Content-Length","16"),("Accept-Ranges","bytes")]
147+addContentHeadersForFilePart :: H.ResponseHeaders -> FilePart -> H.ResponseHeaders
148+addContentHeadersForFilePart hs part = addContentHeaders hs off len size
149+ where
150+ off = filePartOffset part
151+ len = filePartByteCount part
152+ size = filePartFileSize part
warp/Network/Wai/Handler/Warp/FileInfoCache.hsnew file+108
@@ -0,0 +1,108 @@
1+{-# LANGUAGE RecordWildCards, CPP #-}
2+
3+module Network.Wai.Handler.Warp.FileInfoCache (
4+ FileInfo(..)
5+ , withFileInfoCache
6+ , getInfo -- test purpose only
7+ ) where
8+
9+import Control.Exception as E
10+import Control.Reaper
11+import Network.HTTP.Date
12+import System.PosixCompat.Files
13+
14+import Network.Wai.Handler.Warp.HashMap (HashMap)
15+import qualified Network.Wai.Handler.Warp.HashMap as M
16+import Network.Wai.Handler.Warp.Imports
17+
18+----------------------------------------------------------------
19+
20+-- | File information.
21+data FileInfo = FileInfo {
22+ fileInfoName :: !FilePath
23+ , fileInfoSize :: !Integer
24+ , fileInfoTime :: HTTPDate -- ^ Modification time
25+ , fileInfoDate :: ByteString -- ^ Modification time in the GMT format
26+ } deriving (Eq, Show)
27+
28+data Entry = Negative | Positive FileInfo
29+type Cache = HashMap Entry
30+type FileInfoCache = Reaper Cache (FilePath,Entry)
31+
32+----------------------------------------------------------------
33+
34+-- | Getting the file information corresponding to the file.
35+getInfo :: FilePath -> IO FileInfo
36+getInfo path = do
37+ fs <- getFileStatus path -- file access
38+ let regular = not (isDirectory fs)
39+ readable = fileMode fs `intersectFileModes` ownerReadMode /= 0
40+ if regular && readable then do
41+ let time = epochTimeToHTTPDate $ modificationTime fs
42+ date = formatHTTPDate time
43+ size = fromIntegral $ fileSize fs
44+ info = FileInfo {
45+ fileInfoName = path
46+ , fileInfoSize = size
47+ , fileInfoTime = time
48+ , fileInfoDate = date
49+ }
50+ return info
51+ else
52+ throwIO (userError "FileInfoCache:getInfo")
53+
54+getInfoNaive :: FilePath -> IO FileInfo
55+getInfoNaive = getInfo
56+
57+----------------------------------------------------------------
58+
59+getAndRegisterInfo :: FileInfoCache -> FilePath -> IO FileInfo
60+getAndRegisterInfo reaper@Reaper{..} path = do
61+ cache <- reaperRead
62+ case M.lookup path cache of
63+ Just Negative -> throwIO (userError "FileInfoCache:getAndRegisterInfo")
64+ Just (Positive x) -> return x
65+ Nothing -> positive reaper path
66+ `E.onException` negative reaper path
67+
68+positive :: FileInfoCache -> FilePath -> IO FileInfo
69+positive Reaper{..} path = do
70+ info <- getInfo path
71+ reaperAdd (path, Positive info)
72+ return info
73+
74+negative :: FileInfoCache -> FilePath -> IO FileInfo
75+negative Reaper{..} path = do
76+ reaperAdd (path, Negative)
77+ throwIO (userError "FileInfoCache:negative")
78+
79+----------------------------------------------------------------
80+
81+-- | Creating a file information cache
82+-- and executing the action in the second argument.
83+-- The first argument is a cache duration in second.
84+withFileInfoCache :: Int
85+ -> ((FilePath -> IO FileInfo) -> IO a)
86+ -> IO a
87+withFileInfoCache 0 action = action getInfoNaive
88+withFileInfoCache duration action =
89+ E.bracket (initialize duration)
90+ terminate
91+ (action . getAndRegisterInfo)
92+
93+initialize :: Int -> IO FileInfoCache
94+initialize duration = mkReaper settings
95+ where
96+ settings = defaultReaperSettings {
97+ reaperAction = override
98+ , reaperDelay = duration
99+ , reaperCons = \(path,v) -> M.insert path v
100+ , reaperNull = M.isEmpty
101+ , reaperEmpty = M.empty
102+ }
103+
104+override :: Cache -> IO (Cache -> Cache)
105+override _ = return $ const M.empty
106+
107+terminate :: FileInfoCache -> IO ()
108+terminate x = void $ reaperStop x
warp/Network/Wai/Handler/Warp/HTTP2.hsnew file+85
@@ -0,0 +1,85 @@
1+{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE BangPatterns #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE PatternGuards #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+
7+module Network.Wai.Handler.Warp.HTTP2 (http2) where
8+
9+import qualified Data.IORef as I
10+import qualified Control.Exception as E
11+import qualified Network.HTTP2.Server as H2
12+import Network.Socket (SockAddr)
13+import Network.Wai
14+import Network.Wai.Internal (ResponseReceived(..))
15+import qualified System.TimeManager as T
16+
17+import Network.Wai.Handler.Warp.HTTP2.File
18+import Network.Wai.Handler.Warp.HTTP2.PushPromise
19+import Network.Wai.Handler.Warp.HTTP2.Request
20+import Network.Wai.Handler.Warp.HTTP2.Response
21+import Network.Wai.Handler.Warp.Imports
22+import qualified Network.Wai.Handler.Warp.Settings as S
23+import Network.Wai.Handler.Warp.Types
24+
25+----------------------------------------------------------------
26+
27+http2 :: Connection -> Transport -> InternalInfo -> SockAddr -> S.Settings -> (BufSize -> IO ByteString) -> (ByteString -> IO ()) -> Application -> IO ()
28+http2 conn transport ii addr settings readN send app =
29+ H2.run conf http2server
30+ where
31+ conf = H2.Config {
32+ confWriteBuffer = connWriteBuffer conn
33+ , confBufferSize = connBufferSize conn
34+ , confSendAll = send
35+ , confReadN = readN
36+ , confPositionReadMaker = pReadMaker ii
37+ }
38+
39+ http2server h2req aux response = do
40+ req <- toWAIRequest h2req aux
41+ ref <- I.newIORef Nothing
42+ eResponseReceived <- E.try $ app req $ \rsp -> do
43+ h2rsp <- fromResponse settings ii req rsp
44+ pps <- fromPushPromises ii req
45+ I.writeIORef ref $ Just (h2rsp, pps)
46+ _ <- response h2rsp pps
47+ return ResponseReceived
48+ case eResponseReceived of
49+ Right ResponseReceived -> do
50+ Just (h2rsp, pps) <- I.readIORef ref
51+ logResponse h2rsp req
52+ mapM_ (logPushPromise req) pps
53+ Left e@(E.SomeException _)
54+ -- killed by the local worker manager
55+ | Just E.ThreadKilled <- E.fromException e -> return ()
56+ -- killed by the local timeout manager
57+ | Just T.TimeoutThread <- E.fromException e -> return ()
58+ | otherwise -> do
59+ S.settingsOnException settings (Just req) e
60+ let ersp = S.settingsOnExceptionResponse settings e
61+ h2rsp' <- fromResponse settings ii req ersp
62+ _ <- response h2rsp' []
63+ logResponse h2rsp' req
64+ return ()
65+
66+ toWAIRequest h2req aux = toRequest ii settings addr hdr bdylen bdy th transport
67+ where
68+ !hdr = H2.requestHeaders h2req
69+ !bdy = H2.getRequestBodyChunk h2req
70+ !bdylen = H2.requestBodySize h2req
71+ !th = H2.auxTimeHandle aux
72+
73+ logResponse h2rsp req = logger req st msiz
74+ where
75+ !logger = S.settingsLogger settings
76+ !st = H2.responseStatus h2rsp
77+ !msiz = fromIntegral <$> H2.responseBodySize h2rsp
78+
79+ logPushPromise req pp = logger req path siz
80+ where
81+ !logger = S.settingsServerPushLogger settings
82+ !path = H2.promiseRequestPath pp
83+ !siz = case H2.responseBodySize $ H2.promiseResponse pp of
84+ Nothing -> 0
85+ Just s -> fromIntegral s
warp/Network/Wai/Handler/Warp/HTTP2/File.hsnew file+30
@@ -0,0 +1,30 @@
1+{-# LANGUAGE CPP #-}
2+
3+module Network.Wai.Handler.Warp.HTTP2.File where
4+
5+import Network.HTTP2.Server
6+
7+import Network.Wai.Handler.Warp.Types
8+
9+#ifdef WINDOWS
10+pReadMaker :: InternalInfo -> PositionReadMaker
11+pReadMaker _ = defaultPositionReadMaker
12+#else
13+import Network.Wai.Handler.Warp.FdCache
14+import Network.Wai.Handler.Warp.SendFile (positionRead)
15+
16+pReadMaker :: InternalInfo -> PositionReadMaker
17+pReadMaker ii path = do
18+ (mfd, refresh) <- getFd ii path
19+ case mfd of
20+ Just fd -> return (pread fd, Refresher refresh)
21+ Nothing -> do
22+ fd <- openFile path
23+ return (pread fd, Closer $ closeFile fd)
24+ where
25+ pread :: Fd -> PositionRead
26+ pread fd off bytes buf = fromIntegral <$> positionRead fd buf bytes' off'
27+ where
28+ bytes' = fromIntegral bytes
29+ off' = fromIntegral off
30+#endif
warp/Network/Wai/Handler/Warp/HTTP2/PushPromise.hsnew file+36
@@ -0,0 +1,36 @@
1+{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+
5+module Network.Wai.Handler.Warp.HTTP2.PushPromise where
6+
7+import qualified Control.Exception as E
8+import qualified Network.HTTP.Types as H
9+import qualified Network.HTTP2.Server as H2
10+
11+import Network.Wai
12+import Network.Wai.Handler.Warp.FileInfoCache
13+import Network.Wai.Handler.Warp.HTTP2.Request (getHTTP2Data)
14+import Network.Wai.Handler.Warp.HTTP2.Types
15+import Network.Wai.Handler.Warp.Imports
16+import Network.Wai.Handler.Warp.Types
17+
18+fromPushPromises :: InternalInfo -> Request -> IO [H2.PushPromise]
19+fromPushPromises ii req = do
20+ mh2data <- getHTTP2Data req
21+ let pp = case mh2data of
22+ Nothing -> []
23+ Just h2data -> http2dataPushPromise h2data
24+ catMaybes <$> mapM (fromPushPromise ii) pp
25+
26+fromPushPromise :: InternalInfo -> PushPromise -> IO (Maybe H2.PushPromise)
27+fromPushPromise ii (PushPromise path file rsphdr w) = do
28+ efinfo <- E.try $ getFileInfo ii file
29+ case efinfo of
30+ Left (_ex :: E.IOException) -> return Nothing
31+ Right finfo -> do
32+ let !siz = fromIntegral $ fileInfoSize finfo
33+ !fileSpec = H2.FileSpec file 0 siz
34+ !rsp = H2.responseFile H.ok200 rsphdr fileSpec
35+ !pp = H2.pushPromise path rsp w
36+ return $ Just pp
warp/Network/Wai/Handler/Warp/HTTP2/Request.hsnew file+126
@@ -0,0 +1,126 @@
1+{-# LANGUAGE BangPatterns #-}
2+
3+module Network.Wai.Handler.Warp.HTTP2.Request (
4+ toRequest
5+ , getHTTP2Data
6+ , setHTTP2Data
7+ , modifyHTTP2Data
8+ ) where
9+
10+import Control.Arrow (first)
11+import qualified Data.ByteString.Char8 as C8
12+import Data.IORef
13+import qualified Data.Vault.Lazy as Vault
14+import Network.HPACK
15+import Network.HPACK.Token
16+import qualified Network.HTTP.Types as H
17+import Network.Socket (SockAddr)
18+import Network.Wai
19+import Network.Wai.Internal (Request(..))
20+import System.IO.Unsafe (unsafePerformIO)
21+import qualified System.TimeManager as T
22+
23+import Network.Wai.Handler.Warp.HTTP2.Types
24+import Network.Wai.Handler.Warp.Imports
25+import Network.Wai.Handler.Warp.Request (getFileInfoKey, pauseTimeoutKey, getClientCertificateKey)
26+import qualified Network.Wai.Handler.Warp.Settings as S (Settings, settingsNoParsePath)
27+import Network.Wai.Handler.Warp.Types
28+
29+type ToReq = (TokenHeaderList,ValueTable) -> Maybe Int -> IO ByteString -> T.Handle -> Transport -> IO Request
30+
31+----------------------------------------------------------------
32+
33+toRequest :: InternalInfo -> S.Settings -> SockAddr -> ToReq
34+toRequest ii settings addr ht bodylen body th transport = do
35+ ref <- newIORef Nothing
36+ toRequest' ii settings addr ref ht bodylen body th transport
37+
38+toRequest' :: InternalInfo -> S.Settings -> SockAddr
39+ -> IORef (Maybe HTTP2Data)
40+ -> ToReq
41+toRequest' ii settings addr ref (reqths,reqvt) bodylen body th transport = return req
42+ where
43+ !req = Request {
44+ requestMethod = colonMethod
45+ , httpVersion = H.http20
46+ , rawPathInfo = rawPath
47+ , pathInfo = H.decodePathSegments path
48+ , rawQueryString = query
49+ , queryString = H.parseQuery query
50+ , requestHeaders = headers
51+ , isSecure = isTransportSecure transport
52+ , remoteHost = addr
53+ , requestBody = body
54+ , vault = vaultValue
55+ , requestBodyLength = maybe ChunkedBody (KnownLength . fromIntegral) bodylen
56+ , requestHeaderHost = mHost <|> mAuth
57+ , requestHeaderRange = mRange
58+ , requestHeaderReferer = mReferer
59+ , requestHeaderUserAgent = mUserAgent
60+ }
61+ headers = map (first tokenKey) ths
62+ where
63+ ths = case mHost of
64+ Just _ -> reqths
65+ Nothing -> case mAuth of
66+ Just auth -> (tokenHost, auth) : reqths
67+ _ -> reqths
68+ !mPath = getHeaderValue tokenPath reqvt -- SHOULD
69+ !colonMethod = fromJust $ getHeaderValue tokenMethod reqvt -- MUST
70+ !mAuth = getHeaderValue tokenAuthority reqvt -- SHOULD
71+ !mHost = getHeaderValue tokenHost reqvt
72+ !mRange = getHeaderValue tokenRange reqvt
73+ !mReferer = getHeaderValue tokenReferer reqvt
74+ !mUserAgent = getHeaderValue tokenUserAgent reqvt
75+ -- CONNECT request will have ":path" omitted, use ":authority" as unparsed
76+ -- path instead so that it will have consistent behavior compare to HTTP 1.0
77+ (unparsedPath,query) = C8.break (=='?') $ fromJust (mPath <|> mAuth)
78+ !path = H.extractPath unparsedPath
79+ !rawPath = if S.settingsNoParsePath settings then unparsedPath else path
80+ -- fixme: pauseTimeout. th is not available here.
81+ !vaultValue = Vault.insert getFileInfoKey (getFileInfo ii)
82+ $ Vault.insert getHTTP2DataKey (readIORef ref)
83+ $ Vault.insert setHTTP2DataKey (writeIORef ref)
84+ $ Vault.insert modifyHTTP2DataKey (modifyIORef' ref)
85+ $ Vault.insert pauseTimeoutKey (T.pause th)
86+ $ Vault.insert getClientCertificateKey (tlsClientCertificate transport)
87+ Vault.empty
88+
89+getHTTP2DataKey :: Vault.Key (IO (Maybe HTTP2Data))
90+getHTTP2DataKey = unsafePerformIO Vault.newKey
91+{-# NOINLINE getHTTP2DataKey #-}
92+
93+-- | Getting 'HTTP2Data' through vault of the request.
94+-- Warp uses this to receive 'HTTP2Data' from 'Middleware'.
95+--
96+-- Since: 3.2.7
97+getHTTP2Data :: Request -> IO (Maybe HTTP2Data)
98+getHTTP2Data req = case Vault.lookup getHTTP2DataKey (vault req) of
99+ Nothing -> return Nothing
100+ Just getter -> getter
101+
102+setHTTP2DataKey :: Vault.Key (Maybe HTTP2Data -> IO ())
103+setHTTP2DataKey = unsafePerformIO Vault.newKey
104+{-# NOINLINE setHTTP2DataKey #-}
105+
106+-- | Setting 'HTTP2Data' through vault of the request.
107+-- 'Application' or 'Middleware' should use this.
108+--
109+-- Since: 3.2.7
110+setHTTP2Data :: Request -> Maybe HTTP2Data -> IO ()
111+setHTTP2Data req mh2d = case Vault.lookup setHTTP2DataKey (vault req) of
112+ Nothing -> return ()
113+ Just setter -> setter mh2d
114+
115+modifyHTTP2DataKey :: Vault.Key ((Maybe HTTP2Data -> Maybe HTTP2Data) -> IO ())
116+modifyHTTP2DataKey = unsafePerformIO Vault.newKey
117+{-# NOINLINE modifyHTTP2DataKey #-}
118+
119+-- | Modifying 'HTTP2Data' through vault of the request.
120+-- 'Application' or 'Middleware' should use this.
121+--
122+-- Since: 3.2.8
123+modifyHTTP2Data :: Request -> (Maybe HTTP2Data -> Maybe HTTP2Data) -> IO ()
124+modifyHTTP2Data req func = case Vault.lookup modifyHTTP2DataKey (vault req) of
125+ Nothing -> return ()
126+ Just modify -> modify func
warp/Network/Wai/Handler/Warp/HTTP2/Response.hsnew file+127
@@ -0,0 +1,127 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE BangPatterns #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+
6+module Network.Wai.Handler.Warp.HTTP2.Response (
7+ fromResponse
8+ ) where
9+
10+import qualified Control.Exception as E
11+import qualified Data.ByteString.Builder as BB
12+import qualified Network.HTTP.Types as H
13+import qualified Network.HTTP2.Server as H2
14+import Network.Wai hiding (responseFile, responseBuilder, responseStream)
15+import Network.Wai.Internal (Response(..))
16+
17+import Network.Wai.Handler.Warp.File
18+import Network.Wai.Handler.Warp.HTTP2.Request (getHTTP2Data)
19+import Network.Wai.Handler.Warp.HTTP2.Types
20+import Network.Wai.Handler.Warp.Header
21+import qualified Network.Wai.Handler.Warp.Response as R
22+import qualified Network.Wai.Handler.Warp.Settings as S
23+import Network.Wai.Handler.Warp.Types
24+
25+----------------------------------------------------------------
26+
27+fromResponse :: S.Settings -> InternalInfo -> Request -> Response -> IO H2.Response
28+fromResponse settings ii req rsp = do
29+ date <- getDate ii
30+ h2rsp <- case rsp of
31+ ResponseFile st rsphdr path mpart -> do
32+ let rsphdr' = add date svr rsphdr
33+ responseFile st rsphdr' isHead path mpart ii reqhdr
34+ ResponseBuilder st rsphdr builder -> do
35+ let rsphdr' = add date svr rsphdr
36+ return $ responseBuilder st rsphdr' isHead builder
37+ ResponseStream st rsphdr strmbdy -> do
38+ let rsphdr' = add date svr rsphdr
39+ return $ responseStream st rsphdr' isHead strmbdy
40+ _ -> error "ResponseRaw is not supported in HTTP/2"
41+ mh2data <- getHTTP2Data req
42+ case mh2data of
43+ Nothing -> return h2rsp
44+ Just h2data -> do
45+ let !trailers = http2dataTrailers h2data
46+ return $ H2.setResponseTrailersMaker h2rsp trailers
47+ where
48+ !isHead = requestMethod req == H.methodHead
49+ !reqhdr = requestHeaders req
50+ !svr = S.settingsServerName settings
51+ add date server rsphdr = (H.hDate, date) : (H.hServer, server) : rsphdr
52+ -- fixme: not adding svr if already exists
53+
54+----------------------------------------------------------------
55+
56+responseFile :: H.Status -> H.ResponseHeaders -> Bool
57+ -> FilePath -> Maybe FilePart -> InternalInfo -> H.RequestHeaders
58+ -> IO H2.Response
59+responseFile st rsphdr _ _ _ _ _
60+ | noBody st = return $ responseNoBody st rsphdr
61+
62+responseFile st rsphdr isHead path (Just fp) _ _ =
63+ return $ responseFile2XX st rsphdr isHead fileSpec
64+ where
65+ !off' = fromIntegral $ filePartOffset fp
66+ !bytes' = fromIntegral $ filePartByteCount fp
67+ !fileSpec = H2.FileSpec path off' bytes'
68+
69+responseFile _ rsphdr isHead path Nothing ii reqhdr = do
70+ efinfo <- E.try $ getFileInfo ii path
71+ case efinfo of
72+ Left (_ex :: E.IOException) -> return $ response404 rsphdr
73+ Right finfo -> do
74+ let reqidx = indexRequestHeader reqhdr
75+ rspidx = indexResponseHeader rsphdr
76+ case conditionalRequest finfo rsphdr rspidx reqidx of
77+ WithoutBody s -> return $ responseNoBody s rsphdr
78+ WithBody s rsphdr' off bytes -> do
79+ let !off' = fromIntegral off
80+ !bytes' = fromIntegral bytes
81+ !fileSpec = H2.FileSpec path off' bytes'
82+ return $ responseFile2XX s rsphdr' isHead fileSpec
83+
84+----------------------------------------------------------------
85+
86+responseFile2XX :: H.Status -> H.ResponseHeaders -> Bool -> H2.FileSpec -> H2.Response
87+responseFile2XX st rsphdr isHead fileSpec
88+ | isHead = responseNoBody st rsphdr
89+ | otherwise = H2.responseFile st rsphdr fileSpec
90+
91+----------------------------------------------------------------
92+
93+responseBuilder :: H.Status -> H.ResponseHeaders -> Bool
94+ -> BB.Builder
95+ -> H2.Response
96+responseBuilder st rsphdr isHead builder
97+ | noBody st = responseNoBody st rsphdr
98+ | isHead = responseNoBody st rsphdr
99+ | otherwise = H2.responseBuilder st rsphdr builder
100+
101+----------------------------------------------------------------
102+
103+responseStream :: H.Status -> H.ResponseHeaders -> Bool
104+ -> StreamingBody
105+ -> H2.Response
106+responseStream st rsphdr isHead strmbdy
107+ | noBody st = responseNoBody st rsphdr
108+ | isHead = responseNoBody st rsphdr
109+ | otherwise = H2.responseStreaming st rsphdr strmbdy
110+
111+----------------------------------------------------------------
112+
113+responseNoBody :: H.Status -> H.ResponseHeaders -> H2.Response
114+responseNoBody st rsphdr = H2.responseNoBody st rsphdr
115+
116+----------------------------------------------------------------
117+
118+response404 :: H.ResponseHeaders -> H2.Response
119+response404 rsphdr = H2.responseBuilder H.notFound404 rsphdr' body
120+ where
121+ !rsphdr' = R.replaceHeader H.hContentType "text/plain; charset=utf-8" rsphdr
122+ !body = BB.byteString "File not found"
123+
124+----------------------------------------------------------------
125+
126+noBody :: H.Status -> Bool
127+noBody = not . R.hasBody
warp/Network/Wai/Handler/Warp/HTTP2/Types.hsnew file+81
@@ -0,0 +1,81 @@
1+{-# LANGUAGE NamedFieldPuns #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE RecordWildCards #-}
4+
5+module Network.Wai.Handler.Warp.HTTP2.Types where
6+
7+import qualified Data.ByteString as BS
8+import qualified Network.HTTP.Types as H
9+import Network.HTTP2
10+import qualified Network.HTTP2.Server as H2
11+
12+import Network.Wai.Handler.Warp.Imports
13+import Network.Wai.Handler.Warp.Types
14+
15+----------------------------------------------------------------
16+
17+isHTTP2 :: Transport -> Bool
18+isHTTP2 TCP = False
19+isHTTP2 tls = useHTTP2
20+ where
21+ useHTTP2 = case tlsNegotiatedProtocol tls of
22+ Nothing -> False
23+ Just proto -> "h2" `BS.isPrefixOf` proto
24+
25+----------------------------------------------------------------
26+
27+-- | HTTP/2 specific data.
28+--
29+-- Since: 3.2.7
30+data HTTP2Data = HTTP2Data {
31+ -- | Accessor for 'PushPromise' in 'HTTP2Data'.
32+ --
33+ -- Since: 3.2.7
34+ http2dataPushPromise :: [PushPromise]
35+ -- | Accessor for 'H2.TrailersMaker' in 'HTTP2Data'.
36+ --
37+ -- Since: 3.2.8 but the type changed in 3.3.0
38+ , http2dataTrailers :: H2.TrailersMaker
39+ }
40+
41+-- | Default HTTP/2 specific data.
42+--
43+-- Since: 3.2.7
44+defaultHTTP2Data :: HTTP2Data
45+defaultHTTP2Data = HTTP2Data [] H2.defaultTrailersMaker
46+
47+-- | HTTP/2 push promise or sever push.
48+-- This allows files only for backward-compatibility
49+-- while the HTTP/2 library supports other types.
50+--
51+-- Since: 3.2.7
52+data PushPromise = PushPromise {
53+ -- | Accessor for a URL path in 'PushPromise'.
54+ -- E.g. \"\/style\/default.css\".
55+ --
56+ -- Since: 3.2.7
57+ promisedPath :: ByteString
58+ -- | Accessor for 'FilePath' in 'PushPromise'.
59+ -- E.g. \"FILE_PATH/default.css\".
60+ --
61+ -- Since: 3.2.7
62+ , promisedFile :: FilePath
63+ -- | Accessor for 'H.ResponseHeaders' in 'PushPromise'
64+ -- \"content-type\" must be specified.
65+ -- Default value: [].
66+ --
67+ --
68+ -- Since: 3.2.7
69+ , promisedResponseHeaders :: H.ResponseHeaders
70+ -- | Accessor for 'Weight' in 'PushPromise'.
71+ -- Default value: 16.
72+ --
73+ -- Since: 3.2.7
74+ , promisedWeight :: Weight
75+ } deriving (Eq,Ord,Show)
76+
77+-- | Default push promise.
78+--
79+-- Since: 3.2.7
80+defaultPushPromise :: PushPromise
81+defaultPushPromise = PushPromise "" "" [] 16
warp/Network/Wai/Handler/Warp/HashMap.hsnew file+42
@@ -0,0 +1,42 @@
1+{-# LANGUAGE BangPatterns #-}
2+
3+module Network.Wai.Handler.Warp.HashMap where
4+
5+import Data.Hashable (hash)
6+import Data.IntMap.Strict (IntMap)
7+import qualified Data.IntMap.Strict as I
8+import Data.Map.Strict (Map)
9+import qualified Data.Map.Strict as M
10+import Prelude hiding (lookup)
11+
12+----------------------------------------------------------------
13+
14+-- | 'HashMap' is used for cache of file information.
15+-- Hash values of file pathes are used as outer keys.
16+-- Because negative entries are also contained,
17+-- a bad guy can intentionally cause the hash collison.
18+-- So, 'Map' is used internally to prevent
19+-- the hash collision attack.
20+newtype HashMap v = HashMap (IntMap (Map FilePath v))
21+
22+----------------------------------------------------------------
23+
24+empty :: HashMap v
25+empty = HashMap I.empty
26+
27+isEmpty :: HashMap v -> Bool
28+isEmpty (HashMap hm) = I.null hm
29+
30+----------------------------------------------------------------
31+
32+insert :: FilePath -> v -> HashMap v -> HashMap v
33+insert path v (HashMap hm) = HashMap $ I.insertWith f h m hm
34+ where
35+ !h = hash path
36+ !m = M.singleton path v
37+ f = M.union -- fimxe
38+
39+lookup :: FilePath -> HashMap v -> Maybe v
40+lookup path (HashMap hm) = I.lookup h hm >>= M.lookup path
41+ where
42+ !h = hash path
warp/Network/Wai/Handler/Warp/Header.hs+49−27
@@ -1,10 +1,13 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, FlexibleContexts #-}
2
2
3module Network.Wai.Handler.Warp.Header where
3module Network.Wai.Handler.Warp.Header where
4
4
5import Data.Array
5import Data.Array
6import Data.Array.ST
6import Data.Array.ST
7+import qualified Data.ByteString as BS
8+import Data.CaseInsensitive (foldedCase)
7import Network.HTTP.Types
9import Network.HTTP.Types
10+
8import Network.Wai.Handler.Warp.Types
11import Network.Wai.Handler.Warp.Types
9
12
10----------------------------------------------------------------
13----------------------------------------------------------------
@@ -17,29 +20,43 @@ type IndexedHeader = Array Int (Maybe HeaderValue)
17indexRequestHeader :: RequestHeaders -> IndexedHeader
20indexRequestHeader :: RequestHeaders -> IndexedHeader
18indexRequestHeader hdr = traverseHeader hdr requestMaxIndex requestKeyIndex
21indexRequestHeader hdr = traverseHeader hdr requestMaxIndex requestKeyIndex
19
22
20−idxContentLength,idxTransferEncoding,idxExpect :: Int
23+data RequestHeaderIndex = ReqContentLength
21−idxConnection,idxRange,idxHost :: Int
24+ | ReqTransferEncoding
22−idxContentLength = 0
25+ | ReqExpect
23−idxTransferEncoding = 1
26+ | ReqConnection
24−idxExpect = 2
27+ | ReqRange
25−idxConnection = 3
28+ | ReqHost
26−idxRange = 4
29+ | ReqIfModifiedSince
27−idxHost = 5
30+ | ReqIfUnmodifiedSince
31+ | ReqIfRange
32+ | ReqReferer
33+ | ReqUserAgent
34+ deriving (Enum,Bounded)
28
35
29-- | The size for 'IndexedHeader' for HTTP Request.
36-- | The size for 'IndexedHeader' for HTTP Request.
30-- From 0 to this corresponds to \"Content-Length\", \"Transfer-Encoding\",
37-- From 0 to this corresponds to \"Content-Length\", \"Transfer-Encoding\",
31−-- \"Expect\", \"Connection\", \"Range\", and \"Host\".
38+-- \"Expect\", \"Connection\", \"Range\", \"Host\",
39+-- \"If-Modified-Since\", \"If-Unmodified-Since\" and \"If-Range\".
32requestMaxIndex :: Int
40requestMaxIndex :: Int
33−requestMaxIndex = 5
41+requestMaxIndex = fromEnum (maxBound :: RequestHeaderIndex)
34
42
35requestKeyIndex :: HeaderName -> Int
43requestKeyIndex :: HeaderName -> Int
36−requestKeyIndex "content-length" = idxContentLength
44+requestKeyIndex hn = case BS.length bs of
37−requestKeyIndex "transfer-encoding" = idxTransferEncoding
45+ 4 -> if bs == "host" then fromEnum ReqHost else -1
38−requestKeyIndex "expect" = idxExpect
46+ 5 -> if bs == "range" then fromEnum ReqRange else -1
39−requestKeyIndex "connection" = idxConnection
47+ 6 -> if bs == "expect" then fromEnum ReqExpect else -1
40−requestKeyIndex "range" = idxRange
48+ 7 -> if bs == "referer" then fromEnum ReqReferer else -1
41−requestKeyIndex "host" = idxHost
49+ 8 -> if bs == "if-range" then fromEnum ReqIfRange else -1
42−requestKeyIndex _ = -1
50+ 10 -> if bs == "user-agent" then fromEnum ReqUserAgent else
51+ if bs == "connection" then fromEnum ReqConnection else -1
52+ 14 -> if bs == "content-length" then fromEnum ReqContentLength else -1
53+ 17 -> if bs == "transfer-encoding" then fromEnum ReqTransferEncoding else
54+ if bs == "if-modified-since" then fromEnum ReqIfModifiedSince
55+ else -1
56+ 19 -> if bs == "if-unmodified-since" then fromEnum ReqIfUnmodifiedSince else -1
57+ _ -> -1
58+ where
59+ bs = foldedCase hn
43
60
44defaultIndexRequestHeader :: IndexedHeader
61defaultIndexRequestHeader :: IndexedHeader
45defaultIndexRequestHeader = array (0,requestMaxIndex) [(i,Nothing)|i<-[0..requestMaxIndex]]
62defaultIndexRequestHeader = array (0,requestMaxIndex) [(i,Nothing)|i<-[0..requestMaxIndex]]
@@ -49,20 +66,25 @@ defaultIndexRequestHeader = array (0,requestMaxIndex) [(i,Nothing)|i<-[0..reques
49indexResponseHeader :: ResponseHeaders -> IndexedHeader
66indexResponseHeader :: ResponseHeaders -> IndexedHeader
50indexResponseHeader hdr = traverseHeader hdr responseMaxIndex responseKeyIndex
67indexResponseHeader hdr = traverseHeader hdr responseMaxIndex responseKeyIndex
51
68
52−idxServer, idxDate :: Int
69+data ResponseHeaderIndex = ResContentLength
53−--idxContentLength = 0
70+ | ResServer
54−idxServer = 1
71+ | ResDate
55−idxDate = 2
72+ | ResLastModified
73+ deriving (Enum,Bounded)
56
74
57-- | The size for 'IndexedHeader' for HTTP Response.
75-- | The size for 'IndexedHeader' for HTTP Response.
58responseMaxIndex :: Int
76responseMaxIndex :: Int
59−responseMaxIndex = 2
77+responseMaxIndex = fromEnum (maxBound :: ResponseHeaderIndex)
60
78
61responseKeyIndex :: HeaderName -> Int
79responseKeyIndex :: HeaderName -> Int
62−responseKeyIndex "content-length" = idxContentLength
80+responseKeyIndex hn = case BS.length bs of
63−responseKeyIndex "server" = idxServer
81+ 4 -> if bs == "date" then fromEnum ResDate else -1
64−responseKeyIndex "date" = idxDate
82+ 6 -> if bs == "server" then fromEnum ResServer else -1
65−responseKeyIndex _ = -1
83+ 13 -> if bs == "last-modified" then fromEnum ResLastModified else -1
84+ 14 -> if bs == "content-length" then fromEnum ResContentLength else -1
85+ _ -> -1
86+ where
87+ bs = foldedCase hn
66
88
67----------------------------------------------------------------
89----------------------------------------------------------------
68
90
warp/Network/Wai/Handler/Warp/IO.hs+16−21
@@ -1,35 +1,30 @@
1{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE CPP #-}
2
3
3module Network.Wai.Handler.Warp.IO where
4module Network.Wai.Handler.Warp.IO where
4
5
5−import Blaze.ByteString.Builder.Internal.Types (Builder(..), BuildSignal(..), BufRange(..), runBuildStep, buildStep)
6+import Data.ByteString.Builder (Builder)
6−import Data.ByteString.Internal (ByteString(..))
7+import Data.ByteString.Builder.Extra (runBuilder, Next(Done, More, Chunk))
7−import Foreign.ForeignPtr (newForeignPtr_)
8+
8−import Foreign.Ptr (plusPtr, minusPtr)
9import Network.Wai.Handler.Warp.Buffer
9import Network.Wai.Handler.Warp.Buffer
10+import Network.Wai.Handler.Warp.Imports
11+import Network.Wai.Handler.Warp.Types
10
12
11toBufIOWith :: Buffer -> BufSize -> (ByteString -> IO ()) -> Builder -> IO ()
13toBufIOWith :: Buffer -> BufSize -> (ByteString -> IO ()) -> Builder -> IO ()
12−toBufIOWith buf !size io (Builder build) = loop firstStep
14+toBufIOWith buf !size io builder = loop firstWriter
13 where
15 where
14− firstStep = build (buildStep finalStep)
16+ firstWriter = runBuilder builder
15− finalStep (BufRange p _) = return $ Done p ()
17+ runIO len = bufferIO buf len io
16− bufRange = BufRange buf (buf `plusPtr` size)
17− runIO ptr = toBS buf (ptr `minusPtr` buf) >>= io
18− loop step = do
18+ loop writer = do
19− signal <- runBuildStep step bufRange
19+ (len, signal) <- writer buf size
20 case signal of
20 case signal of
21− Done ptr _ -> runIO ptr
21+ Done -> runIO len
22− BufferFull minSize ptr next
22+ More minSize next
23 | size < minSize -> error "toBufIOWith: BufferFull: minSize"
23 | size < minSize -> error "toBufIOWith: BufferFull: minSize"
24 | otherwise -> do
24 | otherwise -> do
25− runIO ptr
25+ runIO len
26 loop next
26 loop next
27− InsertByteString ptr bs next -> do
27+ Chunk bs next -> do
28− runIO ptr
28+ runIO len
29 io bs
29 io bs
30 loop next
30 loop next
31−
32−toBS :: Buffer -> Int -> IO ByteString
33−toBS ptr siz = do
34− fptr <- newForeignPtr_ ptr
35− return $ PS fptr 0 siz
warp/Network/Wai/Handler/Warp/Imports.hsnew file+27
@@ -0,0 +1,27 @@
1+module Network.Wai.Handler.Warp.Imports (
2+ ByteString(..)
3+ , NonEmpty(..)
4+ , module Control.Applicative
5+ , module Control.Monad
6+ , module Data.Bits
7+ , module Data.List
8+ , module Data.Int
9+ , module Data.Monoid
10+ , module Data.Ord
11+ , module Data.Word
12+ , module Data.Maybe
13+ , module Numeric
14+ ) where
15+
16+import Control.Applicative
17+import Control.Monad
18+import Data.Bits
19+import Data.ByteString.Internal (ByteString(..))
20+import Data.Int
21+import Data.List
22+import Data.List.NonEmpty (NonEmpty(..))
23+import Data.Maybe
24+import Data.Monoid
25+import Data.Ord
26+import Data.Word
27+import Numeric
warp/Network/Wai/Handler/Warp/Internal.hs+82−17
@@ -1,21 +1,86 @@
1{-# OPTIONS_GHC -fno-warn-deprecations #-}
1{-# OPTIONS_GHC -fno-warn-deprecations #-}
2−module Network.Wai.Handler.Warp.Internal
3− ( Settings (..)
4− , getOnOpen
5− , getOnClose
6− , getOnException
7− ) where
8
2
9−import Network.Wai.Handler.Warp.Settings (Settings (..))
3+module Network.Wai.Handler.Warp.Internal (
10−import Network.Socket (SockAddr)
4+ -- * Settings
11−import Network.Wai (Request)
5+ Settings (..)
12−import Control.Exception (SomeException)
6+ , ProxyProtocol(..)
7+ -- * Low level run functions
8+ , runSettingsConnection
9+ , runSettingsConnectionMaker
10+ , runSettingsConnectionMakerSecure
11+ , Transport (..)
12+ -- * Connection
13+ , Connection (..)
14+ , socketConnection
15+ -- ** Receive
16+ , Recv
17+ , RecvBuf
18+ , makePlainReceiveN
19+ -- ** Buffer
20+ , Buffer
21+ , BufSize
22+ , bufferSize
23+ , allocateBuffer
24+ , freeBuffer
25+ , copy
26+ -- ** Sendfile
27+ , FileId (..)
28+ , SendFile
29+ , sendFile
30+ , readSendFile
31+ -- * Version
32+ , warpVersion
33+ -- * Data types
34+ , InternalInfo (..)
35+ , HeaderValue
36+ , IndexedHeader
37+ , requestMaxIndex
38+ -- * Time out manager
39+ -- |
40+ --
41+ -- In order to provide slowloris protection, Warp provides timeout handlers. We
42+ -- follow these rules:
43+ --
44+ -- * A timeout is created when a connection is opened.
45+ --
46+ -- * When all request headers are read, the timeout is tickled.
47+ --
48+ -- * Every time at least the slowloris size settings number of bytes of the request
49+ -- body are read, the timeout is tickled.
50+ --
51+ -- * The timeout is paused while executing user code. This will apply to both
52+ -- the application itself, and a ResponseSource response. The timeout is
53+ -- resumed as soon as we return from user code.
54+ --
55+ -- * Every time data is successfully sent to the client, the timeout is tickled.
56+ , module System.TimeManager
57+ -- * File descriptor cache
58+ , module Network.Wai.Handler.Warp.FdCache
59+ -- * File information cache
60+ , module Network.Wai.Handler.Warp.FileInfoCache
61+ -- * Date
62+ , module Network.Wai.Handler.Warp.Date
63+ -- * Request and response
64+ , Source
65+ , recvRequest
66+ , sendResponse
67+ -- * Platform dependent helper functions
68+ , setSocketCloseOnExec
69+ , windowsThreadBlockHack
70+ ) where
13
71
14−getOnOpen :: Settings -> SockAddr -> IO Bool
72+import System.TimeManager
15−getOnOpen = settingsOnOpen
16
73
17−getOnClose :: Settings -> SockAddr -> IO ()
74+import Network.Wai.Handler.Warp.Buffer
18−getOnClose = settingsOnClose
75+import Network.Wai.Handler.Warp.Date
19−
76+import Network.Wai.Handler.Warp.FdCache
20−getOnException :: Settings -> Maybe Request -> SomeException -> IO ()
77+import Network.Wai.Handler.Warp.FileInfoCache
21−getOnException = settingsOnException
78+import Network.Wai.Handler.Warp.Header
79+import Network.Wai.Handler.Warp.Recv
80+import Network.Wai.Handler.Warp.Request
81+import Network.Wai.Handler.Warp.Response
82+import Network.Wai.Handler.Warp.Run
83+import Network.Wai.Handler.Warp.SendFile
84+import Network.Wai.Handler.Warp.Settings
85+import Network.Wai.Handler.Warp.Types
86+import Network.Wai.Handler.Warp.Windows
warp/Network/Wai/Handler/Warp/MultiMap.hs+66−191
@@ -1,236 +1,111 @@
1+{-# LANGUAGE BangPatterns #-}
2+
1module Network.Wai.Handler.Warp.MultiMap (
3module Network.Wai.Handler.Warp.MultiMap (
2− MMap
4+ MultiMap
3− , Some(..)
5+ , isEmpty
4 , empty
6 , empty
5 , singleton
7 , singleton
6 , insert
8 , insert
7− , search
9+ , Network.Wai.Handler.Warp.MultiMap.lookup
8− , searchWith
9− , isEmpty
10− , valid
11 , pruneWith
10 , pruneWith
12− , fromList
13 , toList
11 , toList
14− , fromSortedList
15− , toSortedList
16 , merge
12 , merge
17 ) where
13 ) where
18
14
19−import Control.Applicative ((<$>))
15+import Data.Hashable (hash)
20−import Data.List (foldl')
16+import Data.IntMap.Strict (IntMap)
21−
17+import qualified Data.IntMap.Strict as I
22−----------------------------------------------------------------
18+import Data.Semigroup
23−
19+import Prelude -- Silence redundant import warnings
24−-- | One ore more list to implement multimap.
25−data Some a = One !a
26− | Tom !a !(Some a) -- Two or more
27− deriving (Eq,Show)
28−
29−-- This is slow but assuming rarely used.
30−snoc :: Some a -> a -> Some a
31−snoc (One x) y = Tom x (One y)
32−snoc (Tom x xs) y = Tom x (snoc xs y)
33−
34−top :: Some a -> a
35−top (One x) = x
36−top (Tom x _) = x
37−
38−----------------------------------------------------------------
39−
40−-- | Red black tree as multimap.
41−data MMap k v = Leaf -- color is Black
42− | Node Color !(MMap k v) !k !(Some v) !(MMap k v)
43− deriving (Show)
44−
45−data Color = B -- ^ Black
46− | R -- ^ Red
47− deriving (Eq, Show)
48
20
49----------------------------------------------------------------
21----------------------------------------------------------------
50
22
51−instance (Eq k, Eq v) => Eq (MMap k v) where
23+-- | 'MultiMap' is used for cache of file descriptors.
52− t1 == t2 = toSortedList t1 == toSortedList t2
24+-- Since multiple threads would open file descriptors for
53−
25+-- the same file simultaneously, multiple entries must
54−----------------------------------------------------------------
26+-- be contained for the file.
55−
27+-- Since hash values of file pathes are used as outer keys,
56−-- | O(log N)
28+-- collison would happen for multiple file pathes.
57−search :: Ord k => k -> MMap k v -> Maybe v
29+-- Becase only positive entries are contained,
58−search _ Leaf = Nothing
30+-- a bad guy cannot be cause the hash collision intentinally.
59−search xk (Node _ l k v r) = case compare xk k of
31+-- So, lists are good enough.
60− LT -> search xk l
32+newtype MultiMap v = MultiMap (IntMap [(FilePath,v)])
61− GT -> search xk r
62− EQ -> Just $ top v
63−
64−-- | O(log N)
65−searchWith :: Ord k => k -> (Some v -> Maybe v) -> MMap k v -> Maybe v
66−searchWith _ _ Leaf = Nothing
67−searchWith xk f (Node _ l k v r) = case compare xk k of
68− LT -> searchWith xk f l
69− GT -> searchWith xk f r
70− EQ -> f v
71
33
72----------------------------------------------------------------
34----------------------------------------------------------------
73
35
74-- | O(1)
36-- | O(1)
75−isEmpty :: (Eq k, Eq v) => MMap k v -> Bool
37+empty :: MultiMap v
76−isEmpty Leaf = True
38+empty = MultiMap $ I.empty
77−isEmpty _ = False
78
39
79-- | O(1)
40-- | O(1)
80−empty :: MMap k v
41+isEmpty :: MultiMap v -> Bool
81−empty = Leaf
42+isEmpty (MultiMap mm) = I.null mm
82
43
83----------------------------------------------------------------
44----------------------------------------------------------------
84
45
85-- | O(1)
46-- | O(1)
86−singleton :: Ord k => k -> v -> MMap k v
47+singleton :: FilePath -> v -> MultiMap v
87−singleton k v = Node B Leaf k (One v) Leaf
48+singleton path v = MultiMap mm
88−
49+ where
89−----------------------------------------------------------------
50+ !h = hash path
90−
51+ !mm = I.singleton h [(path,v)]
91−-- | O(log N)
92−insert :: Ord k => k -> v -> MMap k v -> MMap k v
93−insert kx kv t = turnB (insert' kx kv t)
94−
95−insert' :: Ord k => k -> v -> MMap k v -> MMap k v
96−insert' xk xv Leaf = Node R Leaf xk (One xv) Leaf
97−insert' xk xv (Node B l k v r) = case compare xk k of
98− LT -> balanceL' (insert' xk xv l) k v r
99− GT -> balanceR' l k v (insert' xk xv r)
100− EQ -> Node B l k (snoc v xv) r
101−insert' xk xv (Node R l k v r) = case compare xk k of
102− LT -> Node R (insert' xk xv l) k v r
103− GT -> Node R l k v (insert' xk xv r)
104− EQ -> Node R l k (snoc v xv) r
105−
106−balanceL' :: MMap k v -> k -> Some v -> MMap k v -> MMap k v
107−balanceL' (Node R (Node R a xk xv b) yk yv c) zk zv d =
108− Node R (Node B a xk xv b) yk yv (Node B c zk zv d)
109−balanceL' (Node R a xk xv (Node R b yk yv c)) zk zv d =
110− Node R (Node B a xk xv b) yk yv (Node B c zk zv d)
111−balanceL' l k v r = Node B l k v r
112−
113−balanceR' :: MMap k v -> k -> Some v -> MMap k v -> MMap k v
114−balanceR' a xk xv (Node R b yk yv (Node R c zk zv d)) =
115− Node R (Node B a xk xv b) yk yv (Node B c zk zv d)
116−balanceR' a xk xv (Node R (Node R b yk yv c) zk zv d) =
117− Node R (Node B a xk xv b) yk yv (Node B c zk zv d)
118−balanceR' l xk xv r = Node B l xk xv r
119−
120−turnB :: MMap k v -> MMap k v
121−turnB Leaf = error "turnB"
122−turnB (Node _ l k v r) = Node B l k v r
123
52
124----------------------------------------------------------------
53----------------------------------------------------------------
125
54
126−-- | O(N log N)
127−fromList :: Ord k => [(k,v)] -> MMap k v
128−fromList = foldl' (\t (k,v) -> insert k v t) empty
129−
130-- | O(N)
55-- | O(N)
131−toList :: MMap k v -> [(k,v)]
56+lookup :: FilePath -> MultiMap v -> Maybe v
132−toList t = inorder t []
57+lookup path (MultiMap mm) = case I.lookup h mm of
58+ Nothing -> Nothing
59+ Just s -> Prelude.lookup path s
133 where
60 where
134− inorder Leaf xs = xs
61+ !h = hash path
135− inorder (Node _ l k v r) xs = inorder l (pairs k v ++ inorder r xs)
136− pairs k (One v) = [(k,v)]
137− pairs k (Tom v vs) = (k,v) : pairs k vs
138
62
139----------------------------------------------------------------
63----------------------------------------------------------------
140
64
141−-- | O(N)
65+-- | O(log n)
142−-- "Constructing Red-Black Trees" by Ralf Hinze
66+insert :: FilePath -> v -> MultiMap v -> MultiMap v
143−fromSortedList :: Ord k => [(k,Some v)] -> MMap k v
67+insert path v (MultiMap mm) = MultiMap mm'
144−fromSortedList = linkAll . foldr add []
68+ where
145−
69+ !h = hash path
146−data Digit k v = Uno k (Some v) (MMap k v)
70+ !mm' = I.insertWith (<>) h [(path,v)] mm
147− | Due k (Some v) (MMap k v) k (Some v) (MMap k v)
148− deriving (Eq,Show)
149−
150−incr :: Digit k v -> [Digit k v] -> [Digit k v]
151−incr (Uno k v t) [] = [Uno k v t]
152−incr (Uno k1 v1 t1) (Uno k2 v2 t2 : ps) = Due k1 v1 t1 k2 v2 t2 : ps
153−incr (Uno k1 v1 t1) (Due k2 v2 t2 k3 v3 t3 : ps) = Uno k1 v1 t1 : incr (Uno k2 v2 (Node B t2 k3 v3 t3)) ps
154−incr _ _ = error "incr"
155−
156−add :: (k,Some v) -> [Digit k v] -> [Digit k v]
157−add (k,v) ps = incr (Uno k v Leaf) ps
158−
159−linkAll :: [Digit k v] -> MMap k v
160−linkAll = foldl' link Leaf
161−
162−link :: MMap k v -> Digit k v -> MMap k v
163−link l (Uno k v t) = Node B l k v t
164−--link l (Due k1 v1 t1 k2 v2 t2) = Node B (Node R l k1 v1 t1) k2 v2 t2
165−link l (Due k1 v1 t1 k2 v2 t2) = Node B l k1 v1 (Node R t1 k2 v2 t2)
166
71
167----------------------------------------------------------------
72----------------------------------------------------------------
168
73
169−-- | O(N)
74+-- | O(n)
170−toSortedList :: MMap k v -> [(k,Some v)]
75+toList :: MultiMap v -> [(FilePath,v)]
171−toSortedList t = inorder t []
76+toList (MultiMap mm) = concatMap snd $ I.toAscList mm
172− where
173− inorder Leaf xs = xs
174− inorder (Node _ l k v r) xs = inorder l ((k,v) : inorder r xs)
175
77
176----------------------------------------------------------------
78----------------------------------------------------------------
177
79
178−-- | O(N)
80+-- | O(n)
179−pruneWith :: Ord k =>
81+pruneWith :: MultiMap v
180− MMap k v
82+ -> ((FilePath,v) -> IO Bool)
181− -> (k -> Some v -> IO [(k, Some v)])
83+ -> IO (MultiMap v)
182− -> IO (MMap k v)
84+pruneWith (MultiMap mm) action = MultiMap <$> mm'
183−pruneWith t run = fromSortedList <$> inorder t []
184 where
85 where
185− inorder Leaf xs = return xs
86+ !mm' = I.fromAscList <$> go (I.toDescList mm) []
186− inorder (Node _ l k v r) xs = do
87+ go [] !acc = return acc
187− ys <- run k v
88+ go ((h,s):kss) !acc = do
188− zs <- inorder r xs
89+ rs <- prune action s
189− inorder l (ys ++ zs)
90+ case rs of
91+ [] -> go kss acc
92+ _ -> go kss ((h,rs) : acc)
190
93
191----------------------------------------------------------------
94----------------------------------------------------------------
192
95
193−-- O(N log N) where N is the size of the second argument
96+-- O(n + m) where N is the size of the second argument
194−merge :: Ord k => MMap k v -> MMap k v -> MMap k v
97+merge :: MultiMap v -> MultiMap v -> MultiMap v
195−merge base m = foldl' ins base xs
98+merge (MultiMap m1) (MultiMap m2) = MultiMap mm
196 where
99 where
197− ins t (k,v) = insert k v t
100+ !mm = I.unionWith (<>) m1 m2
198− xs = toList m
199
101
200----------------------------------------------------------------
102----------------------------------------------------------------
201−-- for testing
202−
203−valid :: Ord k => MMap k v -> Bool
204−valid t = isBalanced t && isOrdered t
205
103
206−isBalanced :: MMap k v -> Bool
104+prune :: ((FilePath,v) -> IO Bool) -> [(FilePath,v)] -> IO [(FilePath,v)]
207−isBalanced t = isBlackSame t && isRedSeparate t
105+prune action xs0 = go xs0
208−
209−isBlackSame :: MMap k v -> Bool
210−isBlackSame t = all (n==) ns
211− where
212− n:ns = blacks t
213−
214−blacks :: MMap k v -> [Int]
215−blacks = blacks' 0
216− where
217− blacks' n Leaf = [n+1]
218− blacks' n (Node R l _ _ r) = blacks' n l ++ blacks' n r
219− blacks' n (Node B l _ _ r) = blacks' n' l ++ blacks' n' r
220− where
221− n' = n + 1
222−
223−isRedSeparate :: MMap k v -> Bool
224−isRedSeparate = reds B
225−
226−reds :: Color -> MMap k v -> Bool
227−reds _ Leaf = True
228−reds R (Node R _ _ _ _) = False
229−reds _ (Node c l _ _ r) = reds c l && reds c r
230−
231−isOrdered :: Ord k => MMap k v -> Bool
232−isOrdered t = ordered $ toSortedList t
233 where
106 where
234− ordered [] = True
107+ go [] = return []
235− ordered [_] = True
108+ go (x:xs) = do
236− ordered (x:y:xys) = fst x <= fst y && ordered (y:xys)
109+ keep <- action x
110+ rs <- go xs
111+ return $ if keep then x:rs else rs
warp/Network/Wai/Handler/Warp/PackInt.hsnew file+56
@@ -0,0 +1,56 @@
1+{-# LANGUAGE OverloadedStrings, BangPatterns #-}
2+
3+module Network.Wai.Handler.Warp.PackInt where
4+
5+import Data.ByteString.Internal (unsafeCreate)
6+import Foreign.Ptr (Ptr, plusPtr)
7+import Foreign.Storable (poke)
8+import qualified Network.HTTP.Types as H
9+
10+import Network.Wai.Handler.Warp.Imports
11+
12+-- $setup
13+-- >>> import Data.ByteString.Char8 as C8
14+-- >>> import Test.QuickCheck (Large(..))
15+
16+-- |
17+--
18+-- prop> packIntegral (abs n) == C8.pack (show (abs n))
19+-- prop> \(Large n) -> let n' = fromIntegral (abs n :: Int) in packIntegral n' == C8.pack (show n')
20+
21+packIntegral :: Integral a => a -> ByteString
22+packIntegral 0 = "0"
23+packIntegral n | n < 0 = error "packIntegral"
24+packIntegral n = unsafeCreate len go0
25+ where
26+ n' = fromIntegral n + 1 :: Double
27+ len = ceiling $ logBase 10 n'
28+ go0 p = go n $ p `plusPtr` (len - 1)
29+ go :: Integral a => a -> Ptr Word8 -> IO ()
30+ go i p = do
31+ let (d,r) = i `divMod` 10
32+ poke p (48 + fromIntegral r)
33+ when (d /= 0) $ go d (p `plusPtr` (-1))
34+
35+{-# SPECIALIZE packIntegral :: Int -> ByteString #-}
36+{-# SPECIALIZE packIntegral :: Integer -> ByteString #-}
37+
38+-- |
39+--
40+-- >>> packStatus H.status200
41+-- "200"
42+-- >>> packStatus H.preconditionFailed412
43+-- "412"
44+
45+packStatus :: H.Status -> ByteString
46+packStatus status = unsafeCreate 3 $ \p -> do
47+ poke p (toW8 r2)
48+ poke (p `plusPtr` 1) (toW8 r1)
49+ poke (p `plusPtr` 2) (toW8 r0)
50+ where
51+ toW8 :: Int -> Word8
52+ toW8 n = 48 + fromIntegral n
53+ !s = fromIntegral $ H.statusCode status
54+ (!q0,!r0) = s `divMod` 10
55+ (!q1,!r1) = q0 `divMod` 10
56+ !r2 = q1 `mod` 10
warp/Network/Wai/Handler/Warp/ReadInt.hs+4−4
@@ -13,13 +13,13 @@ module Network.Wai.Handler.Warp.ReadInt (
13-- This function lives in its own file because the MagicHash pragma interacts
13-- This function lives in its own file because the MagicHash pragma interacts
14-- poorly with the CPP pragma.
14-- poorly with the CPP pragma.
15
15
16−import Data.ByteString (ByteString)
17import qualified Data.ByteString as S
16import qualified Data.ByteString as S
18−import Data.Int (Int64)
19import GHC.Prim
17import GHC.Prim
20import GHC.Types
18import GHC.Types
21import GHC.Word
19import GHC.Word
22
20
21+import Network.Wai.Handler.Warp.Imports hiding (readInt)
22+
23{-# INLINE readInt #-}
23{-# INLINE readInt #-}
24readInt :: Integral a => ByteString -> a
24readInt :: Integral a => ByteString -> a
25readInt bs = fromIntegral $ readInt64 bs
25readInt bs = fromIntegral $ readInt64 bs
@@ -32,14 +32,14 @@ readInt bs = fromIntegral $ readInt64 bs
32-- use Int64 here and then make a generic 'readInt' that allows conversion to
32-- use Int64 here and then make a generic 'readInt' that allows conversion to
33-- Int and Integer.
33-- Int and Integer.
34
34
35−{- NOINLINE readInt64MH #-}
35+{-# NOINLINE readInt64 #-}
36readInt64 :: ByteString -> Int64
36readInt64 :: ByteString -> Int64
37readInt64 bs = S.foldl' (\ !i !c -> i * 10 + fromIntegral (mhDigitToInt c)) 0
37readInt64 bs = S.foldl' (\ !i !c -> i * 10 + fromIntegral (mhDigitToInt c)) 0
38 $ S.takeWhile isDigit bs
38 $ S.takeWhile isDigit bs
39
39
40data Table = Table !Addr#
40data Table = Table !Addr#
41
41
42−{- NOINLINE mhDigitToInt #-}
42+{-# NOINLINE mhDigitToInt #-}
43mhDigitToInt :: Word8 -> Int
43mhDigitToInt :: Word8 -> Int
44mhDigitToInt (W8# i) = I# (word2Int# (indexWord8OffAddr# addr (word2Int# i)))
44mhDigitToInt (W8# i) = I# (word2Int# (indexWord8OffAddr# addr (word2Int# i)))
45 where
45 where
warp/Network/Wai/Handler/Warp/Recv.hs+122−31
@@ -1,69 +1,160 @@
1−{-# LANGUAGE ForeignFunctionInterface #-}
1+{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
3
3
4module Network.Wai.Handler.Warp.Recv (
4module Network.Wai.Handler.Warp.Recv (
5 receive
5 receive
6+ , receiveBuf
7+ , makeReceiveN
8+ , makePlainReceiveN
9+ , spell
6 ) where
10 ) where
7
11
8−import Control.Applicative ((<$>))
12+import qualified Control.Exception as E
9−import Control.Monad (void)
10−import qualified Data.ByteString as BS (empty)
13+import qualified Data.ByteString as BS
11−import Data.ByteString.Internal (ByteString(..), mallocByteString)
14+import Data.IORef
12−import Data.Word (Word8)
13import Foreign.C.Error (eAGAIN, getErrno, throwErrno)
15import Foreign.C.Error (eAGAIN, getErrno, throwErrno)
14import Foreign.C.Types
16import Foreign.C.Types
15import Foreign.ForeignPtr (withForeignPtr)
17import Foreign.ForeignPtr (withForeignPtr)
16−import Foreign.Ptr (Ptr, castPtr)
18+import Foreign.Ptr (Ptr, castPtr, plusPtr)
17import GHC.Conc (threadWaitRead)
19import GHC.Conc (threadWaitRead)
20+import qualified GHC.IO.Exception as E
18−import Network.Socket (Socket, fdSocket)
21+import Network.Socket (Socket)
22+import qualified System.IO.Error as E
23+#if MIN_VERSION_network(3,1,0)
24+import Network.Socket (withFdSocket)
25+#else
26+import Network.Socket (fdSocket)
27+#endif
19import System.Posix.Types (Fd(..))
28import System.Posix.Types (Fd(..))
29+
20import Network.Wai.Handler.Warp.Buffer
30import Network.Wai.Handler.Warp.Buffer
31+import Network.Wai.Handler.Warp.Imports
32+import Network.Wai.Handler.Warp.Types
21
33
22#ifdef mingw32_HOST_OS
34#ifdef mingw32_HOST_OS
23import GHC.IO.FD (FD(..), readRawBufferPtr)
35import GHC.IO.FD (FD(..), readRawBufferPtr)
36+import Network.Wai.Handler.Warp.Windows
24#endif
37#endif
25
38
26----------------------------------------------------------------
39----------------------------------------------------------------
27
40
41+makeReceiveN :: ByteString -> Recv -> RecvBuf -> IO (BufSize -> IO ByteString)
42+makeReceiveN bs0 recv recvBuf = do
43+ ref <- newIORef bs0
44+ return $ receiveN ref recv recvBuf
45+
46+-- | This function returns a receiving function
47+-- based on two receiving functions.
48+-- The returned function efficiently manages received data
49+-- which is initialized by the first argument.
50+-- The returned function may allocate a byte string with malloc().
28−receive :: Socket -> Buffer -> Int -> IO ByteString
51+makePlainReceiveN :: Socket -> ByteString -> IO (BufSize -> IO ByteString)
29−receive sock buf size = do
52+makePlainReceiveN s bs0 = do
30− bytes <- fromIntegral <$> receiveloop sock' buf' size'
53+ ref <- newIORef bs0
31− if bytes == 0 then
54+ pool <- newBufferPool
55+ return $ receiveN ref (receive s pool) (receiveBuf s)
56+
57+receiveN :: IORef ByteString -> Recv -> RecvBuf -> BufSize -> IO ByteString
58+receiveN ref recv recvBuf size = E.handle handler $ do
59+ cached <- readIORef ref
60+ (bs, leftover) <- spell cached size recv recvBuf
61+ writeIORef ref leftover
62+ return bs
63+ where
64+ handler :: E.SomeException -> IO ByteString
32− return BS.empty
65+ handler _ = return ""
33− else do
66+
34− fptr <- mallocByteString bytes
67+----------------------------------------------------------------
68+
69+spell :: ByteString -> BufSize -> IO ByteString -> RecvBuf -> IO (ByteString, ByteString)
70+spell init0 siz0 recv recvBuf
71+ | siz0 <= len0 = return $ BS.splitAt siz0 init0
72+ -- fixme: hard coding 4096
73+ | siz0 <= 4096 = loop [init0] (siz0 - len0)
74+ | otherwise = do
75+ bs@(PS fptr _ _) <- mallocBS siz0
35− void $ withForeignPtr fptr $ \ptr ->
76+ withForeignPtr fptr $ \ptr -> do
36− c_memcpy ptr buf (fromIntegral bytes)
77+ ptr' <- copy ptr init0
37− return $! PS fptr 0 bytes
78+ full <- recvBuf ptr' (siz0 - len0)
79+ if full then
80+ return (bs, "")
81+ else
82+ return ("", "") -- fixme
38 where
83 where
39− sock' = fdSocket sock
84+ len0 = BS.length init0
40− buf' = castPtr buf
85+ loop bss siz = do
41− size' = fromIntegral size
86+ bs <- recv
87+ let len = BS.length bs
88+ if len == 0 then
89+ return ("", "")
90+ else if len >= siz then do
91+ let (consume, leftover) = BS.splitAt siz bs
92+ ret = BS.concat $ reverse (consume : bss)
93+ return (ret, leftover)
94+ else do
95+ let bss' = bs : bss
96+ siz' = siz - len
97+ loop bss' siz'
42
98
43−#ifdef mingw32_HOST_OS
99+-- The timeout manager may close the socket.
44−receiveloop :: CInt -> Ptr Word8 -> CSize -> IO CInt
100+-- In that case, an error of "Bad file descriptor" occurs.
101+-- We ignores it because we expect TimeoutThread.
102+receive :: Socket -> BufferPool -> Recv
103+receive sock pool = E.handle handler $ withBufferPool pool $ \ (ptr, size) -> do
104+#if MIN_VERSION_network(3,1,0)
105+ withFdSocket sock $ \fd -> do
106+#elif MIN_VERSION_network(3,0,0)
107+ fd <- fdSocket sock
45#else
108#else
46−receiveloop :: CInt -> Ptr CChar -> CSize -> IO CInt
109+ let fd = fdSocket sock
47#endif
110#endif
48−receiveloop sock buf size = do
111+ let size' = fromIntegral size
112+ fromIntegral <$> receiveloop fd ptr size'
113+ where
114+ handler :: E.IOException -> IO ByteString
115+ handler e
116+ | E.ioeGetErrorType e == E.InvalidArgument = return ""
117+ | otherwise = E.throwIO e
118+
119+receiveBuf :: Socket -> RecvBuf
120+receiveBuf sock buf0 siz0 = do
121+#if MIN_VERSION_network(3,1,0)
122+ withFdSocket sock $ \fd -> do
123+#elif MIN_VERSION_network(3,0,0)
124+ fd <- fdSocket sock
125+#else
126+ let fd = fdSocket sock
127+#endif
128+ loop fd buf0 siz0
129+ where
130+ loop _ _ 0 = return True
131+ loop fd buf siz = do
132+ n <- fromIntegral <$> receiveloop fd buf (fromIntegral siz)
133+ -- fixme: what should we do in the case of n == 0
134+ if n == 0 then
135+ return False
136+ else
137+ loop fd (buf `plusPtr` n) (siz - n)
138+
139+receiveloop :: CInt -> Ptr Word8 -> CSize -> IO CInt
140+receiveloop sock ptr size = do
49#ifdef mingw32_HOST_OS
141#ifdef mingw32_HOST_OS
50− bytes <- fmap fromIntegral $ readRawBufferPtr "recv" (FD sock 1) buf 0 size
142+ bytes <- windowsThreadBlockHack $ fromIntegral <$> readRawBufferPtr "recv" (FD sock 1) (castPtr ptr) 0 size
51#else
143#else
52− bytes <- c_recv sock buf size 0
144+ bytes <- c_recv sock (castPtr ptr) size 0
53#endif
145#endif
54 if bytes == -1 then do
146 if bytes == -1 then do
55 errno <- getErrno
147 errno <- getErrno
56 if errno == eAGAIN then do
148 if errno == eAGAIN then do
57 threadWaitRead (Fd sock)
149 threadWaitRead (Fd sock)
58− receiveloop sock buf size
150+ receiveloop sock ptr size
59 else
151 else
60 throwErrno "receiveloop"
152 throwErrno "receiveloop"
61 else
153 else
62 return bytes
154 return bytes
63
155
156+#ifndef mingw32_HOST_OS
64-- fixme: the type of the return value
157-- fixme: the type of the return value
65foreign import ccall unsafe "recv"
158foreign import ccall unsafe "recv"
66 c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt
159 c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt
67−
160+#endif
68−foreign import ccall unsafe "string.h memcpy"
69− c_memcpy :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)
warp/Network/Wai/Handler/Warp/Request.hs+147−76
@@ -1,96 +1,120 @@
1{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE CPP #-}
3{-# LANGUAGE CPP #-}
4+{-# LANGUAGE DeriveDataTypeable #-}
4{-# OPTIONS_GHC -fno-warn-deprecations #-}
5{-# OPTIONS_GHC -fno-warn-deprecations #-}
5
6
6module Network.Wai.Handler.Warp.Request (
7module Network.Wai.Handler.Warp.Request (
7 recvRequest
8 recvRequest
8 , headerLines
9 , headerLines
10+ , pauseTimeoutKey
11+ , getFileInfoKey
12+ , getClientCertificateKey
13+ , NoKeepAliveRequest (..)
9 ) where
14 ) where
10
15
11−import Control.Applicative
12import qualified Control.Concurrent as Conc (yield)
16import qualified Control.Concurrent as Conc (yield)
13−import Control.Exception.Lifted (throwIO)
17+import Control.Exception (throwIO, Exception)
14−import Control.Monad.IO.Class (liftIO)
15import Data.Array ((!))
18import Data.Array ((!))
16−import Data.ByteString (ByteString)
17import qualified Data.ByteString as S
19import qualified Data.ByteString as S
18import qualified Data.ByteString.Unsafe as SU
20import qualified Data.ByteString.Unsafe as SU
19import qualified Data.CaseInsensitive as CI
21import qualified Data.CaseInsensitive as CI
20−import Data.Conduit
21import qualified Data.IORef as I
22import qualified Data.IORef as I
22−import Data.Monoid (mempty)
23+import Data.Typeable (Typeable)
24+import qualified Data.Vault.Lazy as Vault
25+import Data.X509
23import qualified Network.HTTP.Types as H
26import qualified Network.HTTP.Types as H
24import Network.Socket (SockAddr)
27import Network.Socket (SockAddr)
25import Network.Wai
28import Network.Wai
26−import Network.Wai.Handler.Warp.Conduit
27−import Network.Wai.Handler.Warp.Header
28−import Network.Wai.Handler.Warp.ReadInt
29−import Network.Wai.Handler.Warp.RequestHeader
30−import Network.Wai.Handler.Warp.Settings (Settings, settingsNoParsePath)
31−import qualified Network.Wai.Handler.Warp.Timeout as Timeout
32import Network.Wai.Handler.Warp.Types
29import Network.Wai.Handler.Warp.Types
33import Network.Wai.Internal
30import Network.Wai.Internal
34import Prelude hiding (lines)
31import Prelude hiding (lines)
32+import System.IO.Unsafe (unsafePerformIO)
33+import qualified System.TimeManager as Timeout
35
34
36−----------------------------------------------------------------
35+import Network.Wai.Handler.Warp.Conduit
37−
36+import Network.Wai.Handler.Warp.FileInfoCache
38−-- FIXME come up with good values here
37+import Network.Wai.Handler.Warp.Header
39−maxTotalHeaderLength :: Int
38+import Network.Wai.Handler.Warp.Imports hiding (readInt, lines)
40−maxTotalHeaderLength = 50 * 1024
39+import Network.Wai.Handler.Warp.ReadInt
40+import Network.Wai.Handler.Warp.RequestHeader
41+import Network.Wai.Handler.Warp.Settings (Settings, settingsNoParsePath, settingsMaxTotalHeaderLength)
41
42
42----------------------------------------------------------------
43----------------------------------------------------------------
43
44
44-- | Receiving a HTTP request from 'Connection' and parsing its header
45-- | Receiving a HTTP request from 'Connection' and parsing its header
45-- to create 'Request'.
46-- to create 'Request'.
47+recvRequest :: Bool -- ^ first request on this connection?
46−recvRequest :: Settings
48+ -> Settings
47 -> Connection
49 -> Connection
48 -> InternalInfo
50 -> InternalInfo
51+ -> Timeout.Handle
49 -> SockAddr -- ^ Peer's address.
52 -> SockAddr -- ^ Peer's address.
50− -> Source IO ByteString -- ^ Where HTTP request comes from.
53+ -> Source -- ^ Where HTTP request comes from.
54+ -> Transport
51 -> IO (Request
55 -> IO (Request
56+ ,Maybe (I.IORef Int)
52 ,IndexedHeader
57 ,IndexedHeader
53− ,IO (ResumableSource IO ByteString)
54− ,Maybe ByteString) -- ^
58+ ,IO ByteString) -- ^
55 -- 'Request' passed to 'Application',
59 -- 'Request' passed to 'Application',
60+ -- how many bytes remain to be consumed, if known
56 -- 'IndexedHeader' of HTTP request for internal use,
61 -- 'IndexedHeader' of HTTP request for internal use,
57− -- leftover source (i.e. body and other HTTP reqeusts in HTTP pipelining),
62+ -- Body producing action used for flushing the request body
58− -- leftovers from request header parsing (used for raw responses)
59
63
60−recvRequest settings conn ii addr src0 = do
64+recvRequest firstRequest settings conn ii th addr src transport = do
61− (src, (leftover', hdrlines)) <- src0 $$+ headerLines
65+ hdrlines <- headerLines (settingsMaxTotalHeaderLength settings) firstRequest src
62 (method, unparsedPath, path, query, httpversion, hdr) <- parseHeaderLines hdrlines
66 (method, unparsedPath, path, query, httpversion, hdr) <- parseHeaderLines hdrlines
63 let idxhdr = indexRequestHeader hdr
67 let idxhdr = indexRequestHeader hdr
64− expect = idxhdr ! idxExpect
68+ expect = idxhdr ! fromEnum ReqExpect
65− cl = idxhdr ! idxContentLength
69+ cl = idxhdr ! fromEnum ReqContentLength
66− te = idxhdr ! idxTransferEncoding
70+ te = idxhdr ! fromEnum ReqTransferEncoding
67− liftIO $ handleExpect conn httpversion expect
71+ handle100Continue = handleExpect conn httpversion expect
72+ rawPath = if settingsNoParsePath settings then unparsedPath else path
73+ vaultValue = Vault.insert pauseTimeoutKey (Timeout.pause th)
74+ $ Vault.insert getFileInfoKey (getFileInfo ii)
75+ $ Vault.insert getClientCertificateKey (tlsClientCertificate transport)
76+ Vault.empty
68− (rbody, bodyLength, getSource) <- bodyAndSource src cl te
77+ (rbody, remainingRef, bodyLength) <- bodyAndSource src cl te
78+ -- body producing function which will produce '100-continue', if needed
79+ rbody' <- timeoutBody remainingRef th rbody handle100Continue
80+ -- body producing function which will never produce 100-continue
81+ rbodyFlush <- timeoutBody remainingRef th rbody (return ())
69 let req = Request {
82 let req = Request {
70 requestMethod = method
83 requestMethod = method
71 , httpVersion = httpversion
84 , httpVersion = httpversion
72 , pathInfo = H.decodePathSegments path
85 , pathInfo = H.decodePathSegments path
73− , rawPathInfo = if settingsNoParsePath settings then unparsedPath else path
86+ , rawPathInfo = rawPath
74 , rawQueryString = query
87 , rawQueryString = query
75 , queryString = H.parseQuery query
88 , queryString = H.parseQuery query
76 , requestHeaders = hdr
89 , requestHeaders = hdr
77− , isSecure = False
90+ , isSecure = isTransportSecure transport
78 , remoteHost = addr
91 , remoteHost = addr
79− , requestBody = timeoutBody th rbody
92+ , requestBody = rbody'
80− , vault = mempty
93+ , vault = vaultValue
81 , requestBodyLength = bodyLength
94 , requestBodyLength = bodyLength
82− , requestHeaderHost = idxhdr ! idxHost
95+ , requestHeaderHost = idxhdr ! fromEnum ReqHost
83− , requestHeaderRange = idxhdr ! idxRange
96+ , requestHeaderRange = idxhdr ! fromEnum ReqRange
97+ , requestHeaderReferer = idxhdr ! fromEnum ReqReferer
98+ , requestHeaderUserAgent = idxhdr ! fromEnum ReqUserAgent
84 }
99 }
85− return (req, idxhdr, getSource, leftover')
100+ return (req, remainingRef, idxhdr, rbodyFlush)
86− where
87− th = threadHandle ii
88
101
89----------------------------------------------------------------
102----------------------------------------------------------------
90
103
91−headerLines :: Sink ByteString IO (Maybe ByteString, [ByteString])
104+headerLines :: Int -> Bool -> Source -> IO [ByteString]
92−headerLines =
105+headerLines maxTotalHeaderLength firstRequest src = do
93− await >>= maybe (throwIO (NotEnoughLines [])) (push (THStatus 0 id id))
106+ bs <- readSource src
107+ if S.null bs
108+ -- When we're working on a keep-alive connection and trying to
109+ -- get the second or later request, we don't want to treat the
110+ -- lack of data as a real exception. See the http1 function in
111+ -- the Run module for more details.
112+ then if firstRequest then throwIO ConnectionClosedByPeer else throwIO NoKeepAliveRequest
113+ else push maxTotalHeaderLength src (THStatus 0 id id) bs
114+
115+data NoKeepAliveRequest = NoKeepAliveRequest
116+ deriving (Show, Typeable)
117+instance Exception NoKeepAliveRequest
94
118
95----------------------------------------------------------------
119----------------------------------------------------------------
96
120
@@ -109,19 +133,20 @@ handleExpect _ _ _ = return ()
109
133
110----------------------------------------------------------------
134----------------------------------------------------------------
111
135
112−bodyAndSource :: ResumableSource IO ByteString
136+bodyAndSource :: Source
113− -> Maybe HeaderValue
137+ -> Maybe HeaderValue -- ^ content length
114− -> Maybe HeaderValue
138+ -> Maybe HeaderValue -- ^ transfer-encoding
115− -> IO (Source IO ByteString
139+ -> IO (IO ByteString
140+ ,Maybe (I.IORef Int)
116 ,RequestBodyLength
141 ,RequestBodyLength
117− ,IO (ResumableSource IO ByteString))
142+ )
118bodyAndSource src cl te
143bodyAndSource src cl te
119 | chunked = do
144 | chunked = do
120− ref <- I.newIORef (src, NeedLen)
145+ csrc <- mkCSource src
121− return (chunkedSource ref, ChunkedBody, fst <$> I.readIORef ref)
146+ return (readCSource csrc, Nothing, ChunkedBody)
122 | otherwise = do
147 | otherwise = do
123− ibs <- IsolatedBSSource <$> I.newIORef (len, src)
148+ isrc@(ISource _ remaining) <- mkISource src len
124− return (ibsIsolate ibs, bodyLen, ibsDone ibs)
149+ return (readISource isrc, Just remaining, bodyLen)
125 where
150 where
126 len = toLength cl
151 len = toLength cl
127 bodyLen = KnownLength $ fromIntegral len
152 bodyLen = KnownLength $ fromIntegral len
@@ -137,17 +162,46 @@ isChunked _ = False
137
162
138----------------------------------------------------------------
163----------------------------------------------------------------
139
164
140−timeoutBody :: Timeout.Handle -> Source IO ByteString -> Source IO ByteString
165+timeoutBody :: Maybe (I.IORef Int) -- ^ remaining
166+ -> Timeout.Handle
167+ -> IO ByteString
168+ -> IO ()
169+ -> IO (IO ByteString)
141−timeoutBody timeoutHandle rbody = do
170+timeoutBody remainingRef timeoutHandle rbody handle100Continue = do
171+ isFirstRef <- I.newIORef True
172+
173+ let checkEmpty =
174+ case remainingRef of
175+ Nothing -> return . S.null
176+ Just ref -> \bs -> if S.null bs
177+ then return True
178+ else do
179+ x <- I.readIORef ref
180+ return $! x <= 0
181+
182+ return $ do
183+ isFirst <- I.readIORef isFirstRef
184+
185+ when isFirst $ do
186+ -- Only check if we need to produce the 100 Continue status
187+ -- when asking for the first chunk of the body
188+ handle100Continue
142− -- Timeout handling was paused after receiving the full request
189+ -- Timeout handling was paused after receiving the full request
143− -- headers. Now we need to resume it to avoid a slowloris
190+ -- headers. Now we need to resume it to avoid a slowloris
144− -- attack during request body sending.
191+ -- attack during request body sending.
145− liftIO $ Timeout.resume timeoutHandle
192+ Timeout.resume timeoutHandle
193+ I.writeIORef isFirstRef False
194+
195+ bs <- rbody
196+
146− -- As soon as we finish receiving the request body, whether
197+ -- As soon as we finish receiving the request body, whether
147− -- because the application is not interested in more bytes, or
198+ -- because the application is not interested in more bytes, or
148− -- because there is no more data available, pause the timeout
199+ -- because there is no more data available, pause the timeout
149− -- handler again.
200+ -- handler again.
201+ isEmpty <- checkEmpty bs
150− addCleanup (const $ liftIO $ Timeout.pause timeoutHandle) rbody
202+ when isEmpty (Timeout.pause timeoutHandle)
203+
204+ return bs
151
205
152----------------------------------------------------------------
206----------------------------------------------------------------
153
207
@@ -161,11 +215,13 @@ data THStatus = THStatus
161
215
162----------------------------------------------------------------
216----------------------------------------------------------------
163
217
218+{- FIXME
164close :: Sink ByteString IO a
219close :: Sink ByteString IO a
165close = throwIO IncompleteHeaders
220close = throwIO IncompleteHeaders
221+-}
166
222
167−push :: THStatus -> ByteString -> Sink ByteString IO (Maybe ByteString, [ByteString])
223+push :: Int -> Source -> THStatus -> ByteString -> IO [ByteString]
168−push (THStatus len lines prepend) bs'
224+push maxTotalHeaderLength src (THStatus len lines prepend) bs'
169 -- Too many bytes
225 -- Too many bytes
170 | len > maxTotalHeaderLength = throwIO OverLargeHeader
226 | len > maxTotalHeaderLength = throwIO OverLargeHeader
171 | otherwise = push' mnl
227 | otherwise = push' mnl
@@ -187,15 +243,19 @@ push (THStatus len lines prepend) bs'
187 Just (nl, False)
243 Just (nl, False)
188
244
189 {-# INLINE push' #-}
245 {-# INLINE push' #-}
246+ push' :: Maybe (Int, Bool) -> IO [ByteString]
190 -- No newline find in this chunk. Add it to the prepend,
247 -- No newline find in this chunk. Add it to the prepend,
191 -- update the length, and continue processing.
248 -- update the length, and continue processing.
192− push' Nothing = await >>= maybe close (push status)
249+ push' Nothing = do
250+ bst <- readSource' src
251+ when (S.null bst) $ throwIO IncompleteHeaders
252+ push maxTotalHeaderLength src status bst
193 where
253 where
194 len' = len + bsLen
254 len' = len + bsLen
195 prepend' = S.append bs
255 prepend' = S.append bs
196 status = THStatus len' lines prepend'
256 status = THStatus len' lines prepend'
197 -- Found a newline, but next line continues as a multiline header
257 -- Found a newline, but next line continues as a multiline header
198− push' (Just (end, True)) = push status rest
258+ push' (Just (end, True)) = push maxTotalHeaderLength src status rest
199 where
259 where
200 rest = S.drop (end + 1) bs
260 rest = S.drop (end + 1) bs
201 prepend' = S.append (SU.unsafeTake (checkCR bs end) bs)
261 prepend' = S.append (SU.unsafeTake (checkCR bs end) bs)
@@ -204,12 +264,9 @@ push (THStatus len lines prepend) bs'
204 -- Found a newline at position end.
264 -- Found a newline at position end.
205 push' (Just (end, False))
265 push' (Just (end, False))
206 -- leftover
266 -- leftover
207− | S.null line = let lines' = lines []
267+ | S.null line = do
208− rest = if start < bsLen then
209− Just (SU.unsafeDrop start bs)
268+ when (start < bsLen) $ leftoverSource src (SU.unsafeDrop start bs)
210− else
269+ return (lines [])
211− Nothing
212− in maybe (return ()) leftover rest >> return (rest, lines')
213 -- more headers
270 -- more headers
214 | otherwise = let len' = len + start
271 | otherwise = let len' = len + start
215 lines' = lines . (line:)
272 lines' = lines . (line:)
@@ -217,16 +274,30 @@ push (THStatus len lines prepend) bs'
217 in if start < bsLen then
274 in if start < bsLen then
218 -- more bytes in this chunk, push again
275 -- more bytes in this chunk, push again
219 let bs'' = SU.unsafeDrop start bs
276 let bs'' = SU.unsafeDrop start bs
220− in push status bs''
277+ in push maxTotalHeaderLength src status bs''
221− else
278+ else do
222 -- no more bytes in this chunk, ask for more
279 -- no more bytes in this chunk, ask for more
223− await >>= maybe close (push status)
280+ bst <- readSource' src
281+ when (S.null bs) $ throwIO IncompleteHeaders
282+ push maxTotalHeaderLength src status bst
224 where
283 where
225 start = end + 1 -- start of next chunk
284 start = end + 1 -- start of next chunk
226 line = SU.unsafeTake (checkCR bs end) bs
285 line = SU.unsafeTake (checkCR bs end) bs
227
286
228{-# INLINE checkCR #-}
287{-# INLINE checkCR #-}
229checkCR :: ByteString -> Int -> Int
288checkCR :: ByteString -> Int -> Int
230−checkCR bs pos = if 13 == S.index bs p then p else pos -- 13 is CR
289+checkCR bs pos = if pos > 0 && 13 == S.index bs p then p else pos -- 13 is CR
231 where
290 where
232 !p = pos - 1
291 !p = pos - 1
292+
293+pauseTimeoutKey :: Vault.Key (IO ())
294+pauseTimeoutKey = unsafePerformIO Vault.newKey
295+{-# NOINLINE pauseTimeoutKey #-}
296+
297+getFileInfoKey :: Vault.Key (FilePath -> IO FileInfo)
298+getFileInfoKey = unsafePerformIO Vault.newKey
299+{-# NOINLINE getFileInfoKey #-}
300+
301+getClientCertificateKey :: Vault.Key (Maybe CertificateChain)
302+getClientCertificateKey = unsafePerformIO Vault.newKey
303+{-# NOINLINE getClientCertificateKey #-}
warp/Network/Wai/Handler/Warp/RequestHeader.hs+17−67
@@ -1,24 +1,22 @@
1−{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
3
2
4module Network.Wai.Handler.Warp.RequestHeader (
3module Network.Wai.Handler.Warp.RequestHeader (
5 parseHeaderLines
4 parseHeaderLines
6− , parseByteRanges
7 ) where
5 ) where
8
6
9import Control.Exception (throwIO)
7import Control.Exception (throwIO)
10−import Control.Monad (when)
11import qualified Data.ByteString as S
8import qualified Data.ByteString as S
12−import qualified Data.ByteString.Char8 as B (unpack, readInteger)
9+import qualified Data.ByteString.Char8 as C8 (unpack)
13−import Data.ByteString.Internal (ByteString(..), memchr)
10+import Data.ByteString.Internal (memchr)
14import qualified Data.CaseInsensitive as CI
11import qualified Data.CaseInsensitive as CI
15−import Data.Word (Word8)
16import Foreign.ForeignPtr (withForeignPtr)
12import Foreign.ForeignPtr (withForeignPtr)
17import Foreign.Ptr (Ptr, plusPtr, minusPtr, nullPtr)
13import Foreign.Ptr (Ptr, plusPtr, minusPtr, nullPtr)
18import Foreign.Storable (peek)
14import Foreign.Storable (peek)
19import qualified Network.HTTP.Types as H
15import qualified Network.HTTP.Types as H
16+
17+import Network.Wai.Handler.Warp.Imports
20import Network.Wai.Handler.Warp.Types
18import Network.Wai.Handler.Warp.Types
21−import qualified Network.HTTP.Types.Header as HH
19+
22-- $setup
20-- $setup
23-- >>> :set -XOverloadedStrings
21-- >>> :set -XOverloadedStrings
24
22
@@ -35,7 +33,7 @@ parseHeaderLines :: [ByteString]
35parseHeaderLines [] = throwIO $ NotEnoughLines []
33parseHeaderLines [] = throwIO $ NotEnoughLines []
36parseHeaderLines (firstLine:otherLines) = do
34parseHeaderLines (firstLine:otherLines) = do
37 (method, path', query, httpversion) <- parseRequestLine firstLine
35 (method, path', query, httpversion) <- parseRequestLine firstLine
38− let path = parsePath path'
36+ let path = H.extractPath path'
39 hdr = map parseHeader otherLines
37 hdr = map parseHeader otherLines
40 return (method, path', path, query, httpversion, hdr)
38 return (method, path', path, query, httpversion, hdr)
41
39
@@ -51,6 +49,8 @@ parseHeaderLines (firstLine:otherLines) = do
51-- *** Exception: Warp: Invalid first line of request: "GET "
49-- *** Exception: Warp: Invalid first line of request: "GET "
52-- >>> parseRequestLine "GET /NotHTTP UNKNOWN/1.1"
50-- >>> parseRequestLine "GET /NotHTTP UNKNOWN/1.1"
53-- *** Exception: Warp: Request line specified a non-HTTP request
51-- *** Exception: Warp: Request line specified a non-HTTP request
52+-- >>> parseRequestLine "PRI * HTTP/2.0"
53+-- ("PRI","*","",HTTP/2.0)
54parseRequestLine :: ByteString
54parseRequestLine :: ByteString
55 -> IO (H.Method
55 -> IO (H.Method
56 ,ByteString -- Path
56 ,ByteString -- Path
@@ -88,7 +88,7 @@ parseRequestLine requestLine@(PS fptr off len) = withForeignPtr fptr $ \ptr -> d
88
88
89 return (method,path,query,hv)
89 return (method,path,query,hv)
90 where
90 where
91− baderr = BadFirstLine $ B.unpack requestLine
91+ baderr = BadFirstLine $ C8.unpack requestLine
92 check :: Ptr Word8 -> Int -> Word8 -> IO ()
92 check :: Ptr Word8 -> Int -> Word8 -> IO ()
93 check p n w = do
93 check p n w = do
94 w0 <- peek $ p `plusPtr` n
94 w0 <- peek $ p `plusPtr` n
@@ -101,12 +101,13 @@ parseRequestLine requestLine@(PS fptr off len) = withForeignPtr fptr $ \ptr -> d
101 check httpptr 4 47 -- '/'
101 check httpptr 4 47 -- '/'
102 check httpptr 6 46 -- '.'
102 check httpptr 6 46 -- '.'
103 httpVersion httpptr = do
103 httpVersion httpptr = do
104− major <- peek $ httpptr `plusPtr` 5
104+ major <- peek (httpptr `plusPtr` 5) :: IO Word8
105− minor <- peek $ httpptr `plusPtr` 7
105+ minor <- peek (httpptr `plusPtr` 7) :: IO Word8
106− if major == (49 :: Word8) && minor == (49 :: Word8) then
106+ let version
107− return H.http11
107+ | major == 49 = if minor == 49 then H.http11 else H.http10
108− else
108+ | major == 50 && minor == 48 = H.HttpVersion 2 0
109− return H.http10
109+ | otherwise = H.http10
110+ return version
110 bs ptr p0 p1 = PS fptr o l
111 bs ptr p0 p1 = PS fptr o l
111 where
112 where
112 o = p0 `minusPtr` ptr
113 o = p0 `minusPtr` ptr
@@ -114,29 +115,6 @@ parseRequestLine requestLine@(PS fptr off len) = withForeignPtr fptr $ \ptr -> d
114
115
115----------------------------------------------------------------
116----------------------------------------------------------------
116
117
117−-- |
118−--
119−-- >>> parsePath ""
120−-- "/"
121−-- >>> parsePath "http://example.com:8080/path"
122−-- "/path"
123−-- >>> parsePath "http://example.com"
124−-- "/"
125−-- >>> parsePath "/path"
126−-- "/path"
127−
128−-- FIXME: parsePath "http://example.com" should be "/"?
129−parsePath :: ByteString -> ByteString
130−parsePath path
131− | "http://" `S.isPrefixOf` path = ensureNonEmpty $ extractPath path
132− | otherwise = ensureNonEmpty path
133− where
134− extractPath = snd . S.breakByte 47 . S.drop 7 -- 47 is '/'.
135− ensureNonEmpty "" = "/"
136− ensureNonEmpty p = p
137−
138−----------------------------------------------------------------
139−
140-- |
118-- |
141--
119--
142-- >>> parseHeader "Content-Length:47"
120-- >>> parseHeader "Content-Length:47"
@@ -150,34 +128,6 @@ parsePath path
150
128
151parseHeader :: ByteString -> H.Header
129parseHeader :: ByteString -> H.Header
152parseHeader s =
130parseHeader s =
153− let (k, rest) = S.breakByte 58 s -- ':'
131+ let (k, rest) = S.break (== 58) s -- ':'
154 rest' = S.dropWhile (\c -> c == 32 || c == 9) $ S.drop 1 rest
132 rest' = S.dropWhile (\c -> c == 32 || c == 9) $ S.drop 1 rest
155 in (CI.mk k, rest')
133 in (CI.mk k, rest')
156−
157−parseByteRanges :: S.ByteString -> Maybe HH.ByteRanges
158−parseByteRanges bs1 = do
159− bs2 <- stripPrefix "bytes=" bs1
160− (r, bs3) <- range bs2
161− ranges (r:) bs3
162− where
163− range bs2 =
164− case stripPrefix "-" bs2 of
165− Just bs3 -> do
166− (i, bs4) <- B.readInteger bs3
167− Just (HH.ByteRangeSuffix i, bs4)
168− Nothing -> do
169− (i, bs3) <- B.readInteger bs2
170− bs4 <- stripPrefix "-" bs3
171− case B.readInteger bs4 of
172− Nothing -> Just (HH.ByteRangeFrom i, bs4)
173− Just (j, bs5) -> Just (HH.ByteRangeFromTo i j, bs5)
174− ranges front bs3 =
175− case stripPrefix "," bs3 of
176− Nothing -> Just (front [])
177− Just bs4 -> do
178− (r, bs5) <- range bs4
179− ranges (front . (r:)) bs5
180−
181− stripPrefix x y
182− | x `S.isPrefixOf` y = Just (S.drop (S.length x) y)
183− | otherwise = Nothing
warp/Network/Wai/Handler/Warp/Response.hs+242−244
@@ -1,125 +1,60 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE TupleSections #-}
4{-# LANGUAGE RankNTypes #-}
3{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE CPP #-}
4{-# LANGUAGE CPP #-}
5+{-# LANGUAGE BangPatterns #-}
6
6
7module Network.Wai.Handler.Warp.Response (
7module Network.Wai.Handler.Warp.Response (
8 sendResponse
8 sendResponse
9− , fileRange -- for testing
9+ , sanitizeHeaderValue -- for testing
10 , warpVersion
10 , warpVersion
11− , defaultServerValue
11+ , hasBody
12+ , replaceHeader
13+ , addServer -- testing
12 ) where
14 ) where
13
15
14−import Blaze.ByteString.Builder (fromByteString, Builder, flush)
15−import Blaze.ByteString.Builder.HTTP (chunkedTransferEncoding, chunkedTransferTerminator)
16+import Data.ByteString.Builder.HTTP.Chunked (chunkedTransferEncoding, chunkedTransferTerminator)
16−import Control.Applicative
17−import Control.Exception
17+import qualified Control.Exception as E
18−import Control.Monad.IO.Class (liftIO)
19import Data.Array ((!))
18import Data.Array ((!))
20−import Data.ByteString (ByteString)
21import qualified Data.ByteString as S
19import qualified Data.ByteString as S
22−import Control.Monad (unless)
20+import Data.ByteString.Builder (byteString, Builder)
21+import Data.ByteString.Builder.Extra (flush)
23−import qualified Data.ByteString.Char8 as B (pack)
22+import qualified Data.ByteString.Char8 as C8
24import qualified Data.CaseInsensitive as CI
23import qualified Data.CaseInsensitive as CI
25−import Data.Conduit
26−import qualified Data.Conduit.List as CL
27−import Data.Conduit.Blaze (unsafeBuilderToByteString)
28import Data.Function (on)
24import Data.Function (on)
29−import Data.List (deleteBy)
25+import Data.Streaming.ByteString.Builder (newByteStringBuilderRecv, reuseBufferStrategy)
30−import Data.Maybe (isJust, listToMaybe)
31−#if MIN_VERSION_base(4,5,0)
32−import Data.Monoid ((<>))
33−#else
34−import Data.Monoid (mappend)
35−#endif
36import Data.Version (showVersion)
26import Data.Version (showVersion)
27+import Data.Word8 (_cr, _lf)
37import qualified Network.HTTP.Types as H
28import qualified Network.HTTP.Types as H
29+import qualified Network.HTTP.Types.Header as H
38import Network.Wai
30import Network.Wai
31+import Network.Wai.Internal
32+import qualified Paths_warp
33+import qualified System.TimeManager as T
34+
35+import Network.Wai.Handler.Warp.Buffer (toBuilderBuffer)
39import qualified Network.Wai.Handler.Warp.Date as D
36import qualified Network.Wai.Handler.Warp.Date as D
37+import Network.Wai.Handler.Warp.File
40import Network.Wai.Handler.Warp.Header
38import Network.Wai.Handler.Warp.Header
41−import Network.Wai.Handler.Warp.Buffer (toBlazeBuffer)
42import Network.Wai.Handler.Warp.IO (toBufIOWith)
39import Network.Wai.Handler.Warp.IO (toBufIOWith)
40+import Network.Wai.Handler.Warp.Imports
43import Network.Wai.Handler.Warp.ResponseHeader
41import Network.Wai.Handler.Warp.ResponseHeader
44−import Network.Wai.Handler.Warp.RequestHeader (parseByteRanges)
42+import Network.Wai.Handler.Warp.Settings
45−import qualified Network.Wai.Handler.Warp.Timeout as T
46import Network.Wai.Handler.Warp.Types
43import Network.Wai.Handler.Warp.Types
47−import Network.Wai.Internal
48−import Numeric (showInt)
49−import qualified Paths_warp
50−import qualified System.PosixCompat.Files as P
51−
52−#if !MIN_VERSION_base(4,5,0)
53−(<>) :: Monoid m => m -> m -> m
54−(<>) = mappend
55−#endif
56
44
57-- $setup
45-- $setup
58-- >>> :set -XOverloadedStrings
46-- >>> :set -XOverloadedStrings
59
47
60−mapRight :: (b -> c) -> Either a b -> Either a c
61−mapRight f eith = case eith of
62− Right x -> Right (f x)
63− Left l -> Left l
64−
65−----------------------------------------------------------------
66−
67−fileRange :: H.Status -> H.ResponseHeaders -> FilePath
68− -> Maybe FilePart -> Maybe HeaderValue
69− -> IO (Either IOException
70− (H.Status, H.ResponseHeaders, Integer, Integer))
71−fileRange s0 hs0 path mPart mRange =
72− mapRight (fileRangeSized . fromIntegral . P.fileSize) <$>
73− try (P.getFileStatus path)
74− where
75− fileRangeSized :: Integer -> (H.Status, H.ResponseHeaders, Integer, Integer)
76− fileRangeSized fileSize =
77− let (beg, end, len, isEntire) = checkPartRange fileSize mPart mRange
78− hs1 = addContentLength len hs0
79− hs | isEntire = hs1
80− | otherwise = addContentRange beg end fileSize hs1
81− s | isEntire = s0
82− | otherwise = H.status206
83− in (s, hs, beg, len)
84−
85−
86−checkPartRange :: Integer -> Maybe FilePart -> Maybe HeaderValue
87− -> (Integer, Integer, Integer, Bool)
88−checkPartRange fileSize = checkPart
89− where
90− checkPart Nothing Nothing = (0, fileSize - 1, fileSize, True)
91− checkPart Nothing (Just range) = case parseByteRanges range >>= listToMaybe of
92− -- Range is broken
93− Nothing -> (0, fileSize - 1, fileSize, True)
94− Just hrange -> checkRange hrange
95− -- Ignore Range if FilePart is specified.
96− -- We assume that an application handled Range and specified
97− -- FilePart.
98− checkPart (Just part) _ = (beg, end, len, isEntire)
99− where
100− beg = filePartOffset part
101− len = filePartByteCount part
102− end = beg + len - 1
103− isEntire = beg == 0 && len == fileSize
104−
105− checkRange (H.ByteRangeFrom beg) = fromRange beg (fileSize - 1)
106− checkRange (H.ByteRangeFromTo beg end) = fromRange beg end
107− checkRange (H.ByteRangeSuffix count) = fromRange (fileSize - count) (fileSize - 1)
108−
109− fromRange beg end = (beg, end, len, isEntire)
110− where
111− len = end - beg + 1
112− isEntire = beg == 0 && len == fileSize
113−
114----------------------------------------------------------------
48----------------------------------------------------------------
115
49
116-- | Sending a HTTP response to 'Connection' according to 'Response'.
50-- | Sending a HTTP response to 'Connection' according to 'Response'.
117--
51--
118−-- Applications/middlewares MUST specify a proper 'H.ResponseHeaders'.
52+-- Applications/middlewares MUST provide a proper 'H.ResponseHeaders'.
119-- so that inconsistency does not happen.
53-- so that inconsistency does not happen.
120-- No header is deleted by this function.
54-- No header is deleted by this function.
121--
55--
122−-- Especially, Applications/middlewares MUST take care of
56+-- Especially, Applications/middlewares MUST provide a proper
57+-- Content-Type. They MUST NOT provide
123-- Content-Length, Content-Range, and Transfer-Encoding
58-- Content-Length, Content-Range, and Transfer-Encoding
124-- because they are inserted, when necessary,
59-- because they are inserted, when necessary,
125-- regardless they already exist.
60-- regardless they already exist.
@@ -131,184 +66,269 @@ checkPartRange fileSize = checkPart
131--
66--
132-- There are three basic APIs to create 'Response':
67-- There are three basic APIs to create 'Response':
133--
68--
69+-- ['responseBuilder' :: 'H.Status' -> 'H.ResponseHeaders' -> 'Builder' -> 'Response']
70+-- HTTP response body is created from 'Builder'.
71+-- Transfer-Encoding: chunked is used in HTTP/1.1.
72+--
73+-- ['responseStream' :: 'H.Status' -> 'H.ResponseHeaders' -> 'StreamingBody' -> 'Response']
74+-- HTTP response body is created from 'Builder'.
75+-- Transfer-Encoding: chunked is used in HTTP/1.1.
76+--
77+-- ['responseRaw' :: ('IO' 'ByteString' -> ('ByteString' -> 'IO' ()) -> 'IO' ()) -> 'Response' -> 'Response']
78+-- No header is added and no Transfer-Encoding: is applied.
79+--
134-- ['responseFile' :: 'H.Status' -> 'H.ResponseHeaders' -> 'FilePath' -> 'Maybe' 'FilePart' -> 'Response']
80-- ['responseFile' :: 'H.Status' -> 'H.ResponseHeaders' -> 'FilePath' -> 'Maybe' 'FilePart' -> 'Response']
135−-- HTTP response body is sent by sendfile().
81+-- HTTP response body is sent (by sendfile(), if possible) for GET method.
136−-- Applications are categorized into simple and sophisticated.
82+-- HTTP response body is not sent by HEAD method.
137−-- Simple applications should specify 'Nothing' to
138−-- 'Maybe' 'FilePart'. The size of the specified file is obtained
139−-- by disk access. Then Range is handled.
140−-- Sophisticated applications should specify 'Just' to
141−-- 'Maybe' 'FilePart'. They should treat Range (and If-Range) by
142−-- thierselves. In both cases,
143−-- Content-Length and Content-Range (if necessary) are automatically
83+-- Content-Length and Content-Range are automatically
144−-- added into the HTTP response header.
84+-- added into the HTTP response header if necessary.
145-- If Content-Length and Content-Range exist in the HTTP response header,
85-- If Content-Length and Content-Range exist in the HTTP response header,
146-- they would cause inconsistency.
86-- they would cause inconsistency.
147−-- Status is also changed to 206 if necessary.
87+-- \"Accept-Ranges: bytes\" is also inserted.
148--
88--
149−-- ['responseBuilder' :: 'H.Status' -> 'H.ResponseHeaders' -> 'Builder' -> 'Response']
89+-- Applications are categorized into simple and sophisticated.
150−-- HTTP response body is created from 'Source'.
90+-- Sophisticated applications should specify 'Just' to
151−-- Typically, Transfer-Encoding: chunked is used.
91+-- 'Maybe' 'FilePart'. They should treat the conditional request
152−-- If Content-Length is specified, Transfer-Encoding: chunked is not used.
92+-- by themselves. A proper 'Status' (200 or 206) must be provided.
153--
93--
154−-- ['responseSource' :: 'H.Status' -> 'H.ResponseHeaders' -> 'Source' 'IO' ('Flush' 'Builder') -> 'Response']
94+-- Simple applications should specify 'Nothing' to
155−-- HTTP response body is created from 'Builder'.
95+-- 'Maybe' 'FilePart'. The size of the specified file is obtained
156−-- Typically, Transfer-Encoding: chunked is used.
96+-- by disk access or from the file infor cache.
157−-- If Content-Length is specified, Transfer-Encoding: chunked is not used.
97+-- If-Modified-Since, If-Unmodified-Since, If-Range and Range
98+-- are processed. Since a proper status is chosen, 'Status' is
99+-- ignored. Last-Modified is inserted.
158
100
101+sendResponse :: Settings
159−sendResponse :: Connection
102+ -> Connection
160 -> InternalInfo
103 -> InternalInfo
161− -> (forall a. IO a -> IO a) -- ^ Restore masking state.
104+ -> T.Handle
162 -> Request -- ^ HTTP request.
105 -> Request -- ^ HTTP request.
163 -> IndexedHeader -- ^ Indexed header of HTTP request.
106 -> IndexedHeader -- ^ Indexed header of HTTP request.
164− -> Maybe ByteString -- ^ leftovers from header parsing
107+ -> IO ByteString -- ^ source from client, for raw response
165 -> Response -- ^ HTTP response including status code and response header.
108 -> Response -- ^ HTTP response including status code and response header.
166 -> IO Bool -- ^ Returing True if the connection is persistent.
109 -> IO Bool -- ^ Returing True if the connection is persistent.
167−sendResponse conn ii restore req reqidxhdr leftover' response = do
110+sendResponse settings conn ii th req reqidxhdr src response = do
168 hs <- addServerAndDate hs0
111 hs <- addServerAndDate hs0
169− if hasBody s req then do
112+ if hasBody s then do
113+ -- The response to HEAD does not have body.
114+ -- But to handle the conditional requests defined RFC 7232 and
115+ -- to generate appropriate content-length, content-range,
116+ -- and status, the response to HEAD is processed here.
117+ --
118+ -- See definition of rsp below for proper body stripping.
170− sendRsp conn ver s hs restore rsp
119+ (ms, mlen) <- sendRsp conn ii ver s hs rspidxhdr rsp
120+ case ms of
121+ Nothing -> return ()
122+ Just realStatus -> logger req realStatus mlen
171 T.tickle th
123 T.tickle th
172 return ret
124 return ret
173 else do
125 else do
174− sendResponseNoBody conn ver s hs restore response
126+ _ <- sendRsp conn ii ver s hs rspidxhdr RspNoBody
127+ logger req s Nothing
175 T.tickle th
128 T.tickle th
176 return isPersist
129 return isPersist
177 where
130 where
131+ defServer = settingsServerName settings
132+ logger = settingsLogger settings
178 ver = httpVersion req
133 ver = httpVersion req
179 s = responseStatus response
134 s = responseStatus response
180− hs0 = responseHeaders response
135+ hs0 = sanitizeHeaders $ responseHeaders response
181 rspidxhdr = indexResponseHeader hs0
136 rspidxhdr = indexResponseHeader hs0
182− th = threadHandle ii
137+ getdate = getDate ii
183− dc = dateCacher ii
184− addServerAndDate = addDate dc rspidxhdr . addServer rspidxhdr
138+ addServerAndDate = addDate getdate rspidxhdr . addServer defServer rspidxhdr
185− mRange = reqidxhdr ! idxRange
186− reqinfo@(isPersist,_) = infoFromRequest req reqidxhdr
139+ (isPersist,isChunked0) = infoFromRequest req reqidxhdr
140+ isChunked = not isHead && isChunked0
187− (isKeepAlive, needsChunked) = infoFromResponse rspidxhdr reqinfo
141+ (isKeepAlive, needsChunked) = infoFromResponse rspidxhdr (isPersist,isChunked)
142+ isHead = requestMethod req == H.methodHead
188 rsp = case response of
143 rsp = case response of
189− ResponseFile _ _ path mPart -> RspFile path mPart mRange (T.tickle th)
144+ ResponseFile _ _ path mPart -> RspFile path mPart reqidxhdr isHead (T.tickle th)
145+ ResponseBuilder _ _ b
146+ | isHead -> RspNoBody
190− ResponseBuilder _ _ b -> RspBuilder b needsChunked
147+ | otherwise -> RspBuilder b needsChunked
148+ ResponseStream _ _ fb
149+ | isHead -> RspNoBody
191− ResponseSource _ _ fb -> RspSource fb needsChunked th
150+ | otherwise -> RspStream fb needsChunked th
192− ResponseRaw raw _ -> RspRaw raw leftover' (T.tickle th)
151+ ResponseRaw raw _ -> RspRaw raw src (T.tickle th)
152+ -- Make sure we don't hang on to 'response' (avoid space leak)
193− ret = case response of
153+ !ret = case response of
194 ResponseFile {} -> isPersist
154 ResponseFile {} -> isPersist
195 ResponseBuilder {} -> isKeepAlive
155 ResponseBuilder {} -> isKeepAlive
196− ResponseSource {} -> isKeepAlive
156+ ResponseStream {} -> isKeepAlive
197 ResponseRaw {} -> False
157 ResponseRaw {} -> False
198
158
199----------------------------------------------------------------
159----------------------------------------------------------------
200
160
161+sanitizeHeaders :: H.ResponseHeaders -> H.ResponseHeaders
162+sanitizeHeaders = map (sanitize <$>)
163+ where
164+ sanitize v
165+ | containsNewlines v = sanitizeHeaderValue v -- slow path
166+ | otherwise = v -- fast path
167+
168+{-# INLINE containsNewlines #-}
169+containsNewlines :: ByteString -> Bool
170+containsNewlines = S.any (\w -> w == _cr || w == _lf)
171+
172+{-# INLINE sanitizeHeaderValue #-}
173+sanitizeHeaderValue :: ByteString -> ByteString
174+sanitizeHeaderValue v = case C8.lines $ S.filter (/= _cr) v of
175+ [] -> ""
176+ x : xs -> C8.intercalate "\r\n" (x : mapMaybe addSpaceIfMissing xs)
177+ where
178+ addSpaceIfMissing line = case C8.uncons line of
179+ Nothing -> Nothing
180+ Just (first, _)
181+ | first == ' ' || first == '\t' -> Just line
182+ | otherwise -> Just $ " " <> line
183+
184+----------------------------------------------------------------
185+
186+data Rsp = RspNoBody
201−data Rsp = RspFile FilePath (Maybe FilePart) (Maybe HeaderValue) (IO ())
187+ | RspFile FilePath (Maybe FilePart) IndexedHeader Bool (IO ())
202 | RspBuilder Builder Bool
188 | RspBuilder Builder Bool
203− | RspSource (forall b. WithSource IO (Flush Builder) b) Bool T.Handle
189+ | RspStream StreamingBody Bool T.Handle
204− | RspRaw (forall b. WithRawApp b) (Maybe ByteString) (IO ())
190+ | RspRaw (IO ByteString -> (ByteString -> IO ()) -> IO ()) (IO ByteString) (IO ())
205
191
206----------------------------------------------------------------
192----------------------------------------------------------------
207
193
208sendRsp :: Connection
194sendRsp :: Connection
195+ -> InternalInfo
209 -> H.HttpVersion
196 -> H.HttpVersion
210 -> H.Status
197 -> H.Status
211 -> H.ResponseHeaders
198 -> H.ResponseHeaders
212− -> (forall a. IO a -> IO a) -- ^ restore
199+ -> IndexedHeader -- Response
213 -> Rsp
200 -> Rsp
214− -> IO ()
201+ -> IO (Maybe H.Status, Maybe Integer)
215−sendRsp conn ver s0 hs0 restore (RspFile path mPart mRange hook) = restore $ do
216− ex <- fileRange s0 hs path mPart mRange
217− case ex of
218− Left _ex ->
219−#ifdef WARP_DEBUG
220− print _ex >>
221−#endif
222− sendRsp conn ver s2 hs2 id (RspBuilder body True)
223− Right (s, hs1, beg, len) -> do
224− lheader <- composeHeader ver s hs1
225− connSendFile conn path beg len hook [lheader]
226− where
227− hs = addAcceptRanges hs0
228− s2 = H.status404
229− hs2 = replaceHeader H.hContentType "text/plain; charset=utf-8" hs0
230− body = fromByteString "File not found"
231
202
232----------------------------------------------------------------
203----------------------------------------------------------------
233
204
205+sendRsp conn _ ver s hs _ RspNoBody = do
206+ -- Not adding Content-Length.
207+ -- User agents treats it as Content-Length: 0.
208+ composeHeader ver s hs >>= connSendAll conn
209+ return (Just s, Nothing)
210+
211+----------------------------------------------------------------
212+
234−sendRsp conn ver s hs restore (RspBuilder body needsChunked) = restore $ do
213+sendRsp conn _ ver s hs _ (RspBuilder body needsChunked) = do
235 header <- composeHeaderBuilder ver s hs needsChunked
214 header <- composeHeaderBuilder ver s hs needsChunked
236 let hdrBdy
215 let hdrBdy
237 | needsChunked = header <> chunkedTransferEncoding body
216 | needsChunked = header <> chunkedTransferEncoding body
238 <> chunkedTransferTerminator
217 <> chunkedTransferTerminator
239 | otherwise = header <> body
218 | otherwise = header <> body
240− buffer = connBuffer conn
219+ buffer = connWriteBuffer conn
241 size = connBufferSize conn
220 size = connBufferSize conn
242 toBufIOWith buffer size (connSendAll conn) hdrBdy
221 toBufIOWith buffer size (connSendAll conn) hdrBdy
222+ return (Just s, Nothing) -- fixme: can we tell the actual sent bytes?
243
223
244----------------------------------------------------------------
224----------------------------------------------------------------
245
225
246−sendRsp conn ver s hs restore (RspSource withBodyFlush needsChunked th) =
226+sendRsp conn _ ver s hs _ (RspStream streamingBody needsChunked th) = do
247− withBodyFlush $ \bodyFlush -> restore $ do
248 header <- composeHeaderBuilder ver s hs needsChunked
227 header <- composeHeaderBuilder ver s hs needsChunked
249− let src = yield header >> cbody bodyFlush
228+ (recv, finish) <- newByteStringBuilderRecv $ reuseBufferStrategy
250− buffer <- toBlazeBuffer (connBuffer conn) (connBufferSize conn)
229+ $ toBuilderBuffer (connWriteBuffer conn) (connBufferSize conn)
251− src $$ unsafeBuilderToByteString (return buffer) =$ connSink conn th
230+ let send builder = do
231+ popper <- recv builder
232+ let loop = do
233+ bs <- popper
234+ unless (S.null bs) $ do
235+ sendFragment conn th bs
236+ loop
237+ loop
238+ sendChunk
239+ | needsChunked = send . chunkedTransferEncoding
240+ | otherwise = send
241+ send header
242+ streamingBody sendChunk (sendChunk flush)
243+ when needsChunked $ send chunkedTransferTerminator
244+ mbs <- finish
245+ maybe (return ()) (sendFragment conn th) mbs
246+ return (Just s, Nothing) -- fixme: can we tell the actual sent bytes?
247+
248+----------------------------------------------------------------
249+
250+sendRsp conn _ _ _ _ _ (RspRaw withApp src tickle) = do
251+ withApp recv send
252+ return (Nothing, Nothing)
252 where
253 where
253− cbody bodyFlush = if needsChunked then body $= chunk else body
254+ recv = do
254− where
255+ bs <- src
255− body = mapOutput (\x -> case x of
256+ unless (S.null bs) tickle
256− Flush -> flush
257+ return bs
257− Chunk builder -> builder)
258+ send bs = connSendAll conn bs >> tickle
258− bodyFlush
259− chunk :: Conduit Builder IO Builder
260− chunk = await >>= maybe (yield chunkedTransferTerminator) (\x -> yield (chunkedTransferEncoding x) >> chunk)
261
259
262----------------------------------------------------------------
260----------------------------------------------------------------
263
261
264−sendRsp conn _ _ _ restore (RspRaw withApp mbs tickle) =
262+-- Sophisticated WAI applications.
265− withApp $ \app -> restore $ app src sink
263+-- We respect s0. s0 MUST be a proper value.
264+sendRsp conn ii ver s0 hs0 rspidxhdr (RspFile path (Just part) _ isHead hook) =
265+ sendRspFile2XX conn ii ver s0 hs rspidxhdr path beg len isHead hook
266 where
266 where
267− sink = CL.mapM_ (\bs -> connSendAll conn bs >> tickle)
267+ beg = filePartOffset part
268− src = do
268+ len = filePartByteCount part
269− maybe (return ()) yield mbs
269+ hs = addContentHeadersForFilePart hs0 part
270− loop
270+
271− where
271+----------------------------------------------------------------
272− loop = do
272+
273− bs <- liftIO $ connRecv conn
273+-- Simple WAI applications.
274− unless (S.null bs) $ do
274+-- Status is ignored
275− liftIO tickle
275+sendRsp conn ii ver _ hs0 rspidxhdr (RspFile path Nothing reqidxhdr isHead hook) = do
276− yield bs >> loop
276+ efinfo <- E.try $ getFileInfo ii path
277+ case efinfo of
278+ Left (_ex :: E.IOException) ->
279+#ifdef WARP_DEBUG
280+ print _ex >>
281+#endif
282+ sendRspFile404 conn ii ver hs0 rspidxhdr
283+ Right finfo -> case conditionalRequest finfo hs0 rspidxhdr reqidxhdr of
284+ WithoutBody s -> sendRsp conn ii ver s hs0 rspidxhdr RspNoBody
285+ WithBody s hs beg len -> sendRspFile2XX conn ii ver s hs rspidxhdr path beg len isHead hook
277
286
278----------------------------------------------------------------
287----------------------------------------------------------------
279
288
280−sendResponseNoBody :: Connection
289+sendRspFile2XX :: Connection
290+ -> InternalInfo
281− -> H.HttpVersion
291+ -> H.HttpVersion
282− -> H.Status
292+ -> H.Status
283− -> H.ResponseHeaders
293+ -> H.ResponseHeaders
284− -> (forall a. IO a -> IO a) -- ^ restore
294+ -> IndexedHeader
285− -> Response
295+ -> FilePath
286− -> IO ()
296+ -> Integer
287−sendResponseNoBody conn ver s hs restore (ResponseSource _ _ withBodyFlush) =
297+ -> Integer
288− withBodyFlush $ \_bodyFlush ->
298+ -> Bool
289− restore $ composeHeader ver s hs >>= connSendAll conn
299+ -> IO ()
290−sendResponseNoBody conn ver s hs restore (ResponseRaw withRaw _) =
300+ -> IO (Maybe H.Status, Maybe Integer)
291− withRaw $ \_raw ->
301+sendRspFile2XX conn ii ver s hs rspidxhdr path beg len isHead hook
292− restore $ composeHeader ver s hs >>= connSendAll conn
302+ | isHead = sendRsp conn ii ver s hs rspidxhdr RspNoBody
293−sendResponseNoBody conn ver s hs restore ResponseBuilder{} =
303+ | otherwise = do
294− restore $ composeHeader ver s hs >>= connSendAll conn
304+ lheader <- composeHeader ver s hs
295−sendResponseNoBody conn ver s hs restore ResponseFile{} =
305+ (mfd, fresher) <- getFd ii path
296− restore $ composeHeader ver s hs >>= connSendAll conn
306+ let fid = FileId path mfd
307+ hook' = hook >> fresher
308+ connSendFile conn fid beg len hook' [lheader]
309+ return (Just s, Just len)
310+
311+sendRspFile404 :: Connection
312+ -> InternalInfo
313+ -> H.HttpVersion
314+ -> H.ResponseHeaders
315+ -> IndexedHeader
316+ -> IO (Maybe H.Status, Maybe Integer)
317+sendRspFile404 conn ii ver hs0 rspidxhdr = sendRsp conn ii ver s hs rspidxhdr (RspBuilder body True)
318+ where
319+ s = H.notFound404
320+ hs = replaceHeader H.hContentType "text/plain; charset=utf-8" hs0
321+ body = byteString "File not found"
297
322
298----------------------------------------------------------------
323----------------------------------------------------------------
299----------------------------------------------------------------
324----------------------------------------------------------------
300
325
301-- | Use 'connSendAll' to send this data while respecting timeout rules.
326-- | Use 'connSendAll' to send this data while respecting timeout rules.
302−connSink :: Connection -> T.Handle -> Sink ByteString IO ()
327+sendFragment :: Connection -> T.Handle -> ByteString -> IO ()
303−connSink Connection { connSendAll = send } th = sink
328+sendFragment Connection { connSendAll = send } th bs = do
304− where
305− sink = await >>= maybe close push
306− close = liftIO (T.resume th)
307− push x = do
308− liftIO $ T.resume th
329+ T.resume th
309− liftIO $ send x
330+ send bs
310− liftIO $ T.pause th
331+ T.pause th
311− sink
312 -- We pause timeouts before passing control back to user code. This ensures
332 -- We pause timeouts before passing control back to user code. This ensures
313 -- that a timeout will only ever be executed when Warp is in control. We
333 -- that a timeout will only ever be executed when Warp is in control. We
314 -- also make sure to resume the timeout after the completion of user code
334 -- also make sure to resume the timeout after the completion of user code
@@ -326,7 +346,7 @@ checkPersist req reqidxhdr
326 | otherwise = checkPersist10 conn
346 | otherwise = checkPersist10 conn
327 where
347 where
328 ver = httpVersion req
348 ver = httpVersion req
329− conn = reqidxhdr ! idxConnection
349+ conn = reqidxhdr ! fromEnum ReqConnection
330 checkPersist11 (Just x)
350 checkPersist11 (Just x)
331 | CI.foldCase x == "close" = False
351 | CI.foldCase x == "close" = False
332 checkPersist11 _ = True
352 checkPersist11 _ = True
@@ -351,49 +371,26 @@ infoFromResponse rspidxhdr (isPersist,isChunked) = (isKeepAlive, needsChunked)
351 where
371 where
352 needsChunked = isChunked && not hasLength
372 needsChunked = isChunked && not hasLength
353 isKeepAlive = isPersist && (isChunked || hasLength)
373 isKeepAlive = isPersist && (isChunked || hasLength)
354− hasLength = isJust $ rspidxhdr ! idxContentLength
374+ hasLength = isJust $ rspidxhdr ! fromEnum ResContentLength
355
375
356----------------------------------------------------------------
376----------------------------------------------------------------
357
377
358−hasBody :: H.Status -> Request -> Bool
378+hasBody :: H.Status -> Bool
359−hasBody s req = sc /= 204
379+hasBody s = sc /= 204
360− && sc /= 304
380+ && sc /= 304
361− && sc >= 200
381+ && sc >= 200
362− && method /= H.methodHead
363 where
382 where
364 sc = H.statusCode s
383 sc = H.statusCode s
365− method = requestMethod req
366
384
367----------------------------------------------------------------
385----------------------------------------------------------------
368
386
369−addAcceptRanges :: H.ResponseHeaders -> H.ResponseHeaders
370−addAcceptRanges hdrs = (hAcceptRanges, "bytes") : hdrs
371−
372addTransferEncoding :: H.ResponseHeaders -> H.ResponseHeaders
387addTransferEncoding :: H.ResponseHeaders -> H.ResponseHeaders
373−addTransferEncoding hdrs = (hTransferEncoding, "chunked") : hdrs
388+addTransferEncoding hdrs = (H.hTransferEncoding, "chunked") : hdrs
374
389
375−addContentLength :: Integer -> H.ResponseHeaders -> H.ResponseHeaders
376−addContentLength cl hdrs = (H.hContentLength, len) : hdrs
377− where
378− len = B.pack $ show cl
379−
380−addContentRange :: Integer -> Integer -> Integer
381− -> H.ResponseHeaders -> H.ResponseHeaders
382−addContentRange beg end total hdrs = (hContentRange, range) : hdrs
383− where
384− range = B.pack
385− -- building with ShowS
386− $ 'b' : 'y': 't' : 'e' : 's' : ' '
387− : showInt beg
388− ( '-'
389− : showInt end
390− ( '/'
391− : showInt total ""))
392−
393−addDate :: D.DateCache -> IndexedHeader -> H.ResponseHeaders -> IO H.ResponseHeaders
390+addDate :: IO D.GMTDate -> IndexedHeader -> H.ResponseHeaders -> IO H.ResponseHeaders
394−addDate dc rspidxhdr hdrs = case rspidxhdr ! idxDate of
391+addDate getdate rspidxhdr hdrs = case rspidxhdr ! fromEnum ResDate of
395 Nothing -> do
392 Nothing -> do
396− gmtdate <- D.getDate dc
393+ gmtdate <- getdate
397 return $ (H.hDate, gmtdate) : hdrs
394 return $ (H.hDate, gmtdate) : hdrs
398 Just _ -> return hdrs
395 Just _ -> return hdrs
399
396
@@ -403,12 +400,13 @@ addDate dc rspidxhdr hdrs = case rspidxhdr ! idxDate of
403warpVersion :: String
400warpVersion :: String
404warpVersion = showVersion Paths_warp.version
401warpVersion = showVersion Paths_warp.version
405
402
406−defaultServerValue :: HeaderValue
403+{-# INLINE addServer #-}
407−defaultServerValue = B.pack $ "Warp/" ++ warpVersion
408−
409−addServer :: IndexedHeader -> H.ResponseHeaders -> H.ResponseHeaders
404+addServer :: HeaderValue -> IndexedHeader -> H.ResponseHeaders -> H.ResponseHeaders
410−addServer rspidxhdr hdrs = case rspidxhdr ! idxServer of
405+addServer "" rspidxhdr hdrs = case rspidxhdr ! fromEnum ResServer of
411− Nothing -> (hServer, defaultServerValue) : hdrs
406+ Nothing -> hdrs
407+ _ -> filter ((/= H.hServer) . fst) hdrs
408+addServer serverName rspidxhdr hdrs = case rspidxhdr ! fromEnum ResServer of
409+ Nothing -> (H.hServer, serverName) : hdrs
412 _ -> hdrs
410 _ -> hdrs
413
411
414----------------------------------------------------------------
412----------------------------------------------------------------
@@ -424,6 +422,6 @@ replaceHeader k v hdrs = (k,v) : deleteBy ((==) `on` fst) (k,v) hdrs
424
422
425composeHeaderBuilder :: H.HttpVersion -> H.Status -> H.ResponseHeaders -> Bool -> IO Builder
423composeHeaderBuilder :: H.HttpVersion -> H.Status -> H.ResponseHeaders -> Bool -> IO Builder
426composeHeaderBuilder ver s hs True =
424composeHeaderBuilder ver s hs True =
427− fromByteString <$> composeHeader ver s (addTransferEncoding hs)
425+ byteString <$> composeHeader ver s (addTransferEncoding hs)
428composeHeaderBuilder ver s hs False =
426composeHeaderBuilder ver s hs False =
429− fromByteString <$> composeHeader ver s hs
427+ byteString <$> composeHeader ver s hs
warp/Network/Wai/Handler/Warp/ResponseHeader.hs+5−15
@@ -3,18 +3,16 @@
3
3
4module Network.Wai.Handler.Warp.ResponseHeader (composeHeader) where
4module Network.Wai.Handler.Warp.ResponseHeader (composeHeader) where
5
5
6−import Control.Monad
7−import Data.ByteString (ByteString)
8import qualified Data.ByteString as S
6import qualified Data.ByteString as S
9−import Data.ByteString.Internal (ByteString(..), create, memcpy)
7+import Data.ByteString.Internal (create)
10import qualified Data.CaseInsensitive as CI
8import qualified Data.CaseInsensitive as CI
11−import Data.List (foldl')
12−import Data.Word (Word8)
13−import Foreign.ForeignPtr
14import Foreign.Ptr
9import Foreign.Ptr
15import GHC.Storable
10import GHC.Storable
16import qualified Network.HTTP.Types as H
11import qualified Network.HTTP.Types as H
17
12
13+import Network.Wai.Handler.Warp.Buffer (copy)
14+import Network.Wai.Handler.Warp.Imports
15+
18----------------------------------------------------------------
16----------------------------------------------------------------
19
17
20composeHeader :: H.HttpVersion -> H.Status -> H.ResponseHeaders -> IO ByteString
18composeHeader :: H.HttpVersion -> H.Status -> H.ResponseHeaders -> IO ByteString
@@ -24,15 +22,9 @@ composeHeader !httpversion !status !responseHeaders = create len $ \ptr -> do
24 void $ copyCRLF ptr2
22 void $ copyCRLF ptr2
25 where
23 where
26 !len = 17 + slen + foldl' fieldLength 0 responseHeaders
24 !len = 17 + slen + foldl' fieldLength 0 responseHeaders
27− fieldLength !l !(k,v) = l + S.length (CI.original k) + S.length v + 4
25+ fieldLength !l (!k,!v) = l + S.length (CI.original k) + S.length v + 4
28 !slen = S.length $ H.statusMessage status
26 !slen = S.length $ H.statusMessage status
29
27
30−{-# INLINE copy #-}
31−copy :: Ptr Word8 -> ByteString -> IO (Ptr Word8)
32−copy !ptr (PS fp o l) = withForeignPtr fp $ \p -> do
33− memcpy ptr (p `plusPtr` o) (fromIntegral l)
34− return $! ptr `plusPtr` l
35−
36httpVer11 :: ByteString
28httpVer11 :: ByteString
37httpVer11 = "HTTP/1.1 "
29httpVer11 = "HTTP/1.1 "
38
30
@@ -90,5 +82,3 @@ cr :: Word8
90cr = 13
82cr = 13
91lf :: Word8
83lf :: Word8
92lf = 10
84lf = 10
93−
94−
warp/Network/Wai/Handler/Warp/Run.hs+491−209
@@ -1,121 +1,168 @@
1+{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE PackageImports #-}
5+{-# LANGUAGE PatternGuards #-}
6+{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE TupleSections #-}
8{-# LANGUAGE TupleSections #-}
9+{-# LANGUAGE ViewPatterns #-}
5{-# OPTIONS_GHC -fno-warn-deprecations #-}
10{-# OPTIONS_GHC -fno-warn-deprecations #-}
6
11
7module Network.Wai.Handler.Warp.Run where
12module Network.Wai.Handler.Warp.Run where
8
13
9−import Control.Concurrent (threadDelay, forkIOWithUnmask)
14+import "iproute" Data.IP (toHostAddress, toHostAddress6)
15+import Control.Arrow (first)
10import qualified Control.Concurrent as Conc (yield)
16import qualified Control.Concurrent as Conc (yield)
11import Control.Exception as E
17import Control.Exception as E
12−import Control.Monad (forever, when, unless, void)
13−import Control.Monad.IO.Class (liftIO)
14−import Data.ByteString (ByteString)
15import qualified Data.ByteString as S
18import qualified Data.ByteString as S
16−import Data.Conduit
19+import Data.Char (chr)
17−import Data.Conduit.Internal (ResumableSource (..))
20+import Data.IORef (IORef, newIORef, readIORef, writeIORef)
18−import qualified Data.Conduit.List as CL
19−#if MIN_VERSION_conduit(1,1,0)
20import Data.Streaming.Network (bindPortTCP)
21import Data.Streaming.Network (bindPortTCP)
21−#else
22+import Foreign.C.Error (Errno(..), eCONNABORTED)
22−import Data.Conduit.Network (bindPort)
23+import GHC.IO.Exception (IOException(..))
23−#define bindPortTCP bindPort
24+import qualified Network.HTTP2 as H2
25+import Network.Socket (Socket, close, accept, withSocketsDo, SockAddr(SockAddrInet, SockAddrInet6), setSocketOption, SocketOption(..))
26+#if MIN_VERSION_network(3,1,1)
27+import Network.Socket (gracefulClose)
24#endif
28#endif
25−import Network (sClose, Socket)
26−import Network.Socket (accept, SockAddr)
27import qualified Network.Socket.ByteString as Sock
29import qualified Network.Socket.ByteString as Sock
28import Network.Wai
30import Network.Wai
31+import Network.Wai.Internal (ResponseReceived (ResponseReceived))
32+import System.Environment (lookupEnv)
33+import qualified System.TimeManager as T
34+import System.Timeout (timeout)
35+
36+import Network.Wai.Handler.Warp.Buffer
37+import Network.Wai.Handler.Warp.Counter
29import qualified Network.Wai.Handler.Warp.Date as D
38import qualified Network.Wai.Handler.Warp.Date as D
30import qualified Network.Wai.Handler.Warp.FdCache as F
39import qualified Network.Wai.Handler.Warp.FdCache as F
40+import qualified Network.Wai.Handler.Warp.FileInfoCache as I
41+import Network.Wai.Handler.Warp.HTTP2 (http2)
31−import Network.Wai.Handler.Warp.Buffer
42+import Network.Wai.Handler.Warp.HTTP2.Types (isHTTP2)
32import Network.Wai.Handler.Warp.Header
43import Network.Wai.Handler.Warp.Header
44+import Network.Wai.Handler.Warp.Imports hiding (readInt)
45+import Network.Wai.Handler.Warp.ReadInt
33import Network.Wai.Handler.Warp.Recv
46import Network.Wai.Handler.Warp.Recv
34import Network.Wai.Handler.Warp.Request
47import Network.Wai.Handler.Warp.Request
35import Network.Wai.Handler.Warp.Response
48import Network.Wai.Handler.Warp.Response
36import Network.Wai.Handler.Warp.SendFile
49import Network.Wai.Handler.Warp.SendFile
37import Network.Wai.Handler.Warp.Settings
50import Network.Wai.Handler.Warp.Settings
38−import qualified Network.Wai.Handler.Warp.Timeout as T
39import Network.Wai.Handler.Warp.Types
51import Network.Wai.Handler.Warp.Types
40−import Data.IORef (IORef, newIORef, readIORef, writeIORef)
52+
41
53
42#if WINDOWS
54#if WINDOWS
43−import qualified Control.Concurrent.MVar as MV
55+import Network.Wai.Handler.Warp.Windows
44−import Network.Socket (withSocketsDo)
45−import Control.Concurrent (forkIO)
46#else
56#else
47−import System.Posix.IO (FdOption(CloseOnExec), setFdOption)
48import Network.Socket (fdSocket)
57import Network.Socket (fdSocket)
49#endif
58#endif
50
59
51−-- | Default action value for 'Connection'.
60+-- | Creating 'Connection' for plain HTTP based on a given socket.
52−socketConnection :: Socket -> IO Connection
61+socketConnection :: Settings -> Socket -> IO Connection
62+#if MIN_VERSION_network(3,1,1)
53−socketConnection s = do
63+socketConnection set s = do
64+#else
65+socketConnection _ s = do
66+#endif
67+ bufferPool <- newBufferPool
54− buf <- allocateBuffer bufferSize
68+ writeBuf <- allocateBuffer bufferSize
69+ let sendall = Sock.sendAll s
70+ isH2 <- newIORef False -- HTTP/1.x
55 return Connection {
71 return Connection {
56 connSendMany = Sock.sendMany s
72 connSendMany = Sock.sendMany s
57− , connSendAll = Sock.sendAll s
73+ , connSendAll = sendall
58− , connSendFile = defaultSendFile s
74+ , connSendFile = sendFile s writeBuf bufferSize sendall
75+#if MIN_VERSION_network(3,1,1)
59− , connClose = sClose s >> freeBuffer buf
76+ , connClose = do
77+ h2 <- readIORef isH2
78+ let tm = if h2 then settingsGracefulCloseTimeout2 set
79+ else settingsGracefulCloseTimeout1 set
80+ if tm == 0 then
81+ close s
82+ else
83+ gracefulClose s tm `E.catch` \(E.SomeException _) -> return ()
84+#else
85+ , connClose = close s
86+#endif
87+ , connFree = freeBuffer writeBuf
60− , connRecv = receive s buf bufferSize
88+ , connRecv = receive s bufferPool
61− , connBuffer = buf
89+ , connRecvBuf = receiveBuf s
90+ , connWriteBuffer = writeBuf
62 , connBufferSize = bufferSize
91 , connBufferSize = bufferSize
63− , connSendFileOverride = Override s
92+ , connHTTP2 = isH2
64 }
93 }
65
94
66−#if __GLASGOW_HASKELL__ < 702
67−allowInterrupt :: IO ()
68−allowInterrupt = unblock $ return ()
69−#endif
70−
71−-- | Run an 'Application' on the given port. This calls 'runSettings' with
95+-- | Run an 'Application' on the given port.
72−-- 'defaultSettings'.
96+-- This calls 'runSettings' with 'defaultSettings'.
73run :: Port -> Application -> IO ()
97run :: Port -> Application -> IO ()
74run p = runSettings defaultSettings { settingsPort = p }
98run p = runSettings defaultSettings { settingsPort = p }
75
99
100+-- | Run an 'Application' on the port present in the @PORT@
101+-- environment variable. Uses the 'Port' given when the variable is unset.
102+-- This calls 'runSettings' with 'defaultSettings'.
103+--
104+-- Since 3.0.9
105+runEnv :: Port -> Application -> IO ()
106+runEnv p app = do
107+ mp <- lookupEnv "PORT"
108+
109+ maybe (run p app) runReadPort mp
110+
111+ where
112+ runReadPort :: String -> IO ()
113+ runReadPort sp = case reads sp of
114+ ((p', _):_) -> run p' app
115+ _ -> fail $ "Invalid value in $PORT: " ++ sp
116+
76-- | Run an 'Application' with the given 'Settings'.
117-- | Run an 'Application' with the given 'Settings'.
118+-- This opens a listen socket on the port defined in 'Settings' and
119+-- calls 'runSettingsSocket'.
77runSettings :: Settings -> Application -> IO ()
120runSettings :: Settings -> Application -> IO ()
78−#if WINDOWS
79−runSettings set app = withSocketsDo $ do
121+runSettings set app = withSocketsDo $
80− var <- MV.newMVar Nothing
81− let clean = MV.modifyMVar_ var $ \s -> maybe (return ()) sClose s >> return Nothing
82− void . forkIO $ bracket
83− (bindPortTCP (settingsPort set) (settingsHost set))
84− (const clean)
85− (\s -> do
86− MV.modifyMVar_ var (\_ -> return $ Just s)
87− runSettingsSocket set s app)
88− forever (threadDelay maxBound) `finally` clean
89−#else
90−runSettings set app =
91 bracket
122 bracket
92 (bindPortTCP (settingsPort set) (settingsHost set))
123 (bindPortTCP (settingsPort set) (settingsHost set))
93− sClose
124+ close
94 (\socket -> do
125 (\socket -> do
95 setSocketCloseOnExec socket
126 setSocketCloseOnExec socket
96 runSettingsSocket set socket app)
127 runSettingsSocket set socket app)
97−#endif
98
128
99−-- | Same as 'runSettings', but uses a user-supplied socket instead of opening
129+-- | This installs a shutdown handler for the given socket and
100−-- one. This allows the user to provide, for example, Unix named socket, which
130+-- calls 'runSettingsConnection' with the default connection setup action
131+-- which handles plain (non-cipher) HTTP.
132+-- When the listen socket in the second argument is closed, all live
133+-- connections are gracefully shut down.
134+--
135+-- The supplied socket can be a Unix named socket, which
101-- can be used when reverse HTTP proxying into your application.
136-- can be used when reverse HTTP proxying into your application.
102--
137--
103-- Note that the 'settingsPort' will still be passed to 'Application's via the
138-- Note that the 'settingsPort' will still be passed to 'Application's via the
104-- 'serverPort' record.
139-- 'serverPort' record.
105runSettingsSocket :: Settings -> Socket -> Application -> IO ()
140runSettingsSocket :: Settings -> Socket -> Application -> IO ()
106−runSettingsSocket set socket app =
141+runSettingsSocket set socket app = do
142+ settingsInstallShutdownHandler set closeListenSocket
107 runSettingsConnection set getConn app
143 runSettingsConnection set getConn app
108 where
144 where
109 getConn = do
145 getConn = do
146+#if WINDOWS
147+ (s, sa) <- windowsThreadBlockHack $ accept socket
148+#else
110 (s, sa) <- accept socket
149 (s, sa) <- accept socket
150+#endif
111 setSocketCloseOnExec s
151 setSocketCloseOnExec s
152+ -- NoDelay causes an error for AF_UNIX.
153+ setSocketOption s NoDelay 1 `E.catch` \(E.SomeException _) -> return ()
112− conn <- socketConnection s
154+ conn <- socketConnection set s
113 return (conn, sa)
155 return (conn, sa)
114
156
115−-- | Allows you to provide a function which will return a 'Connection'. In
157+ closeListenSocket = close socket
116−-- cases where creating the @Connection@ can be expensive, this allows the
158+
159+-- | The connection setup action would be expensive. A good example
160+-- is initialization of TLS.
161+-- So, this converts the connection setup action to the connection maker
162+-- which will be executed after forking a new worker thread.
163+-- Then this calls 'runSettingsConnectionMaker' with the connection maker.
117−-- expensive computations to be performed in a separate thread instead of the
164+-- This allows the expensive computations to be performed
118−-- main server loop.
165+-- in a separate worker thread instead of the main server loop.
119--
166--
120-- Since 1.3.5
167-- Since 1.3.5
121runSettingsConnection :: Settings -> IO (Connection, SockAddr) -> Application -> IO ()
168runSettingsConnection :: Settings -> IO (Connection, SockAddr) -> Application -> IO ()
@@ -125,193 +172,428 @@ runSettingsConnection set getConn app = runSettingsConnectionMaker set getConnMa
125 (conn, sa) <- getConn
172 (conn, sa) <- getConn
126 return (return conn, sa)
173 return (return conn, sa)
127
174
175+-- | This modifies the connection maker so that it returns 'TCP' for 'Transport'
176+-- (i.e. plain HTTP) then calls 'runSettingsConnectionMakerSecure'.
128runSettingsConnectionMaker :: Settings -> IO (IO Connection, SockAddr) -> Application -> IO ()
177runSettingsConnectionMaker :: Settings -> IO (IO Connection, SockAddr) -> Application -> IO ()
129runSettingsConnectionMaker x y =
178runSettingsConnectionMaker x y =
130− runSettingsConnectionMakerSecure x (go y)
179+ runSettingsConnectionMakerSecure x (toTCP <$> y)
131 where
180 where
132− go = fmap (\(a, b) -> (fmap (, False) a, b))
181+ toTCP = first ((, TCP) <$>)
182+
183+----------------------------------------------------------------
133
184
134−-- | Allows you to provide a function which will return a function
185+-- | The core run function which takes 'Settings',
135−-- which will return 'Connection'.
186+-- a connection maker and 'Application'.
187+-- The connection maker can return a connection of either plain HTTP
188+-- or HTTP over TLS.
136--
189--
137-- Since 2.1.4
190-- Since 2.1.4
138−runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Bool), SockAddr) -> Application -> IO ()
191+runSettingsConnectionMakerSecure :: Settings -> IO (IO (Connection, Transport), SockAddr) -> Application -> IO ()
139runSettingsConnectionMakerSecure set getConnMaker app = do
192runSettingsConnectionMakerSecure set getConnMaker app = do
140 settingsBeforeMainLoop set
193 settingsBeforeMainLoop set
194+ counter <- newCounter
195+ withII $ acceptConnection set getConnMaker app counter
196+ where
197+ withII action =
198+ withTimeoutManager $ \tm ->
199+ D.withDateCache $ \dc ->
200+ F.withFdCache fdCacheDurationInSeconds $ \fdc ->
201+ I.withFileInfoCache fdFileInfoDurationInSeconds $ \fic -> do
202+ let ii = InternalInfo tm dc fdc fic
203+ action ii
141
204
205+ !fdCacheDurationInSeconds = settingsFdCacheDuration set * 1000000
206+ !fdFileInfoDurationInSeconds = settingsFileInfoCacheDuration set * 1000000
207+ !timeoutInSeconds = settingsTimeout set * 1000000
208+ withTimeoutManager f = case settingsManager set of
209+ Just tm -> f tm
210+ Nothing -> bracket
211+ (T.initialize timeoutInSeconds)
212+ T.stopManager
213+ f
214+
142− -- Note that there is a thorough discussion of the exception safety of the
215+-- Note that there is a thorough discussion of the exception safety of the
143− -- following code at: https://github.com/yesodweb/wai/issues/146
216+-- following code at: https://github.com/yesodweb/wai/issues/146
144− --
217+--
145− -- We need to make sure of two things:
218+-- We need to make sure of two things:
146− --
219+--
147− -- 1. Asynchronous exceptions are not blocked entirely in the main loop.
220+-- 1. Asynchronous exceptions are not blocked entirely in the main loop.
148− -- Doing so would make it impossible to kill the Warp thread.
221+-- Doing so would make it impossible to kill the Warp thread.
149− --
222+--
150− -- 2. Once a connection maker is received via getConnLoop, the connection
223+-- 2. Once a connection maker is received via acceptNewConnection, the
151− -- is guaranteed to be closed, even in the presence of async exceptions.
224+-- connection is guaranteed to be closed, even in the presence of
225+-- async exceptions.
226+--
227+-- Our approach is explained in the comments below.
228+acceptConnection :: Settings
229+ -> IO (IO (Connection, Transport), SockAddr)
230+ -> Application
231+ -> Counter
232+ -> InternalInfo
233+ -> IO ()
234+acceptConnection set getConnMaker app counter ii = do
235+ -- First mask all exceptions in acceptLoop. This is necessary to
236+ -- ensure that no async exception is throw between the call to
237+ -- acceptNewConnection and the registering of connClose.
152 --
238 --
153− -- Our approach is explained in the comments below.
239+ -- acceptLoop can be broken by closing the listing socket.
154−
240+ void $ mask_ acceptLoop
155− -- First mask all exceptions in the main loop. This is necessary to ensure
241+ -- In some cases, we want to stop Warp here without graceful shutdown.
156− -- that no async exception is throw between the call to getConnLoop and the
242+ -- So, async exceptions are allowed here.
157− -- registering of connClose.
243+ -- That's why `finally` is not used.
158− D.withDateCache $ \dc -> do
244+ gracefulShutdown set counter
159− F.withFdCache (settingsFdCacheDuration set * 1000000) $ \fc -> do
245+ where
160− withTimeoutManager $ \tm -> mask_ . forever $ do
246+ acceptLoop = do
161 -- Allow async exceptions before receiving the next connection maker.
247 -- Allow async exceptions before receiving the next connection maker.
162 allowInterrupt
248 allowInterrupt
163
249
164− -- getConnLoop will try to receive the next incoming request. It
250+ -- acceptNewConnection will try to receive the next incoming
165− -- returns a /connection maker/, not a connection, since in some
251+ -- request. It returns a /connection maker/, not a connection,
166− -- circumstances creating a working connection from a raw socket may be
252+ -- since in some circumstances creating a working connection
167− -- an expensive operation, and this expensive work should not be
253+ -- from a raw socket may be an expensive operation, and this
254+ -- expensive work should not be performed in the main event
168− -- performed in the main event loop. An example of something expensive
255+ -- loop. An example of something expensive would be TLS
169− -- would be TLS negotiation.
256+ -- negotiation.
170− (mkConn, addr) <- getConnLoop
257+ mx <- acceptNewConnection
258+ case mx of
259+ Nothing -> return ()
260+ Just (mkConn, addr) -> do
261+ fork set mkConn addr app counter ii
262+ acceptLoop
171
263
264+ acceptNewConnection = do
265+ ex <- try getConnMaker
266+ case ex of
267+ Right x -> return $ Just x
268+ Left e -> do
269+ let eConnAborted = getErrno eCONNABORTED
270+ getErrno (Errno cInt) = cInt
271+ if ioe_errno e == Just eConnAborted
272+ then acceptNewConnection
273+ else do
274+ settingsOnException set Nothing $ toException e
275+ return Nothing
276+
172− -- Fork a new worker thread for this connection maker, and ask for a
277+-- Fork a new worker thread for this connection maker, and ask for a
173− -- function to unmask (i.e., allow async exceptions to be thrown).
278+-- function to unmask (i.e., allow async exceptions to be thrown).
279+fork :: Settings
280+ -> IO (Connection, Transport)
281+ -> SockAddr
282+ -> Application
283+ -> Counter
284+ -> InternalInfo
285+ -> IO ()
286+fork set mkConn addr app counter ii = settingsFork set $ \unmask ->
287+ -- Call the user-supplied on exception code if any
288+ -- exceptions are thrown.
289+ handle (settingsOnException set Nothing) $
290+ -- Run the connection maker to get a new connection, and ensure
291+ -- that the connection is closed. If the mkConn call throws an
292+ -- exception, we will leak the connection. If the mkConn call is
293+ -- vulnerable to attacks (e.g., Slowloris), we do nothing to
294+ -- protect the server. It is therefore vital that mkConn is well
295+ -- vetted.
174 --
296 --
175− -- GHC 7.8 cannot infer the type of "void . forkIOWithUnmask"
176− void $ forkIOWithUnmask $ \unmask ->
177− -- Run the connection maker to get a new connection, and ensure
178− -- that the connection is closed. If the mkConn call throws an
179− -- exception, we will leak the connection. If the mkConn call is
180− -- vulnerable to attacks (e.g., Slowloris), we do nothing to
181− -- protect the server. It is therefore vital that mkConn is well
182− -- vetted.
183− --
184− -- We grab the connection before registering timeouts since the
297+ -- We grab the connection before registering timeouts since the
185− -- timeouts will be useless during connection creation, due to the
298+ -- timeouts will be useless during connection creation, due to the
186− -- fact that async exceptions are still masked.
299+ -- fact that async exceptions are still masked.
187− bracket mkConn (connClose . fst) $ \(conn', isSecure') ->
300+ bracket mkConn cleanUp (serve unmask)
188−
189− -- We need to register a timeout handler for this thread, and
190− -- cancel that handler as soon as we exit.
191− bracket (T.registerKillThread tm) T.cancel $ \th ->
192− let ii = InternalInfo th fc dc
193− conn = setSendFile conn' fc
194− -- We now have fully registered a connection close handler
195− -- in the case of all exceptions, so it is safe to one
196− -- again allow async exceptions.
197− in unmask .
198− -- Call the user-supplied on exception code if any
199− -- exceptions are thrown.
200− handle (onE Nothing) .
201−
202− -- Call the user-supplied code for connection open and close events
203− bracket (onOpen addr) (const $ onClose addr) $ \goingon ->
204−
205− -- Actually serve this connection.
206− -- onnClose above ensures the termination of the connection.
207− when goingon $ serveConnection conn ii addr isSecure' set app
208 where
301 where
209− -- FIXME: only IOEception is caught. What about other exceptions?
302+ cleanUp (conn, _) = connClose conn `finally` connFree conn
210− getConnLoop = getConnMaker `E.catch` \(e :: IOException) -> do
303+
211− onE Nothing (toException e)
304+ -- We need to register a timeout handler for this thread, and
212− -- "resource exhausted (Too many open files)" may happen by accept().
305+ -- cancel that handler as soon as we exit.
213− -- Wait a second hoping that resource will be available.
306+ serve unmask (conn, transport) = bracket register cancel $ \th -> do
214− threadDelay 1000000
307+ -- We now have fully registered a connection close handler in
215− getConnLoop
308+ -- the case of all exceptions, so it is safe to once again
216− onE mreq e =
309+ -- allow async exceptions.
217− case fromException e of
310+ unmask .
218− Just (NotEnoughLines []) -> return ()
311+ -- Call the user-supplied code for connection open and
219− _ -> settingsOnException set mreq e
312+ -- close events
313+ bracket (onOpen addr) (onClose addr) $ \goingon ->
314+ -- Actually serve this connection. bracket with closeConn
315+ -- above ensures the connection is closed.
316+ when goingon $ serveConnection conn ii th addr transport set app
317+ where
318+ register = T.registerKillThread (timeoutManager ii) (connClose conn)
319+ cancel = T.cancel
320+
220− onOpen = settingsOnOpen set
321+ onOpen adr = increase counter >> settingsOnOpen set adr
221− onClose = settingsOnClose set
322+ onClose adr _ = decrease counter >> settingsOnClose set adr
222−
223− withTimeoutManager f =
224− case settingsManager set of
225− Nothing -> bracket
226− (T.initialize $ settingsTimeout set * 1000000)
227− T.stopManager
228− f
229− Just tm -> f tm
230
323
231serveConnection :: Connection
324serveConnection :: Connection
232 -> InternalInfo
325 -> InternalInfo
326+ -> T.Handle
233 -> SockAddr
327 -> SockAddr
234− -> Bool -- ^ is secure?
328+ -> Transport
235 -> Settings
329 -> Settings
236 -> Application
330 -> Application
237 -> IO ()
331 -> IO ()
238−serveConnection conn ii addr isSecure' settings app = do
332+serveConnection conn ii th origAddr transport settings app = do
333+ -- fixme: Upgrading to HTTP/2 should be supported.
334+ (h2,bs) <- if isHTTP2 transport then
335+ return (True, "")
336+ else do
337+ bs0 <- connRecv conn
338+ if S.length bs0 >= 4 && "PRI " `S.isPrefixOf` bs0 then
339+ return (True, bs0)
340+ else
341+ return (False, bs0)
239 istatus <- newIORef False
342 istatus <- newIORef False
240− recvSendLoop istatus (connSource conn th istatus) `E.catch` \e -> do
343+ if settingsHTTP2Enabled settings && h2 then do
344+ rawRecvN <- makeReceiveN bs (connRecv conn) (connRecvBuf conn)
345+ -- This thread becomes the sender in http2 library.
346+ -- In the case of event source, one request comes and one
347+ -- worker gets busy. But it is likely that the receiver does
348+ -- not receive any data at all while the sender is sending
349+ -- output data from the worker. It's not good enough to tickle
350+ -- the time handler in the receiver only. So, we should tickle
351+ -- the time handler in both the receiver and the sender.
352+ let recvN = wrappedRecvN th istatus (settingsSlowlorisSize settings) rawRecvN
353+ sendBS x = connSendAll conn x >> T.tickle th
354+ -- fixme: origAddr
355+ checkTLS
356+ setConnHTTP2 conn True
357+ http2 conn transport ii origAddr settings recvN sendBS app
358+ else do
359+ src <- mkSource (wrappedRecv conn th istatus (settingsSlowlorisSize settings))
360+ writeIORef istatus True
361+ leftoverSource src bs
362+ addr <- getProxyProtocolAddr src
363+ http1 True addr istatus src `E.catch` \e ->
364+ case () of
365+ ()
366+ -- See comment below referencing
367+ -- https://github.com/yesodweb/wai/issues/618
368+ | Just NoKeepAliveRequest <- fromException e -> return ()
369+ -- No valid request
370+ | Just (BadFirstLine _) <- fromException e -> return ()
371+ | otherwise -> do
241− sendErrorResponse istatus e
372+ _ <- sendErrorResponse (dummyreq addr) istatus e
242− throwIO (e :: SomeException)
373+ throwIO e
243
374
244 where
375 where
245− th = threadHandle ii
376+ getProxyProtocolAddr src =
377+ case settingsProxyProtocol settings of
378+ ProxyProtocolNone ->
379+ return origAddr
380+ ProxyProtocolRequired -> do
381+ seg <- readSource src
382+ parseProxyProtocolHeader src seg
383+ ProxyProtocolOptional -> do
384+ seg <- readSource src
385+ if S.isPrefixOf "PROXY " seg
386+ then parseProxyProtocolHeader src seg
387+ else do leftoverSource src seg
388+ return origAddr
389+
390+ parseProxyProtocolHeader src seg = do
391+ let (header,seg') = S.break (== 0x0d) seg -- 0x0d == CR
392+ maybeAddr = case S.split 0x20 header of -- 0x20 == space
393+ ["PROXY","TCP4",clientAddr,_,clientPort,_] ->
394+ case [x | (x, t) <- reads (decodeAscii clientAddr), null t] of
395+ [a] -> Just (SockAddrInet (readInt clientPort)
396+ (toHostAddress a))
397+ _ -> Nothing
398+ ["PROXY","TCP6",clientAddr,_,clientPort,_] ->
399+ case [x | (x, t) <- reads (decodeAscii clientAddr), null t] of
400+ [a] -> Just (SockAddrInet6 (readInt clientPort)
401+ 0
402+ (toHostAddress6 a)
403+ 0)
404+ _ -> Nothing
405+ ("PROXY":"UNKNOWN":_) ->
406+ Just origAddr
407+ _ ->
408+ Nothing
409+ case maybeAddr of
410+ Nothing -> throwIO (BadProxyHeader (decodeAscii header))
411+ Just a -> do leftoverSource src (S.drop 2 seg') -- drop CRLF
412+ return a
413+
414+ decodeAscii = map (chr . fromEnum) . S.unpack
415+
416+ shouldSendErrorResponse se
417+ | Just ConnectionClosedByPeer <- fromException se = False
418+ | otherwise = True
246
419
247− sendErrorResponse istatus e = do
420+ sendErrorResponse req istatus e = do
248 status <- readIORef istatus
421 status <- readIORef istatus
249− when status $ void $ mask $ \restore ->
422+ if shouldSendErrorResponse e && status
423+ then do
250− sendResponse conn ii restore dummyreq defaultIndexRequestHeader Nothing (errorResponse e)
424+ sendResponse settings conn ii th req defaultIndexRequestHeader (return S.empty) (errorResponse e)
425+ else return False
251
426
252− dummyreq = defaultRequest { remoteHost = addr }
427+ dummyreq addr = defaultRequest { remoteHost = addr }
253
428
254 errorResponse e = settingsOnExceptionResponse settings e
429 errorResponse e = settingsOnExceptionResponse settings e
255
430
256− recvSendLoop istatus fromClient = do
431+ http1 firstRequest addr istatus src = do
257− (req', idxhdr, getSource, leftover') <- recvRequest settings conn ii addr fromClient
432+ (req, mremainingRef, idxhdr, nextBodyFlush) <- recvRequest firstRequest settings conn ii th addr src transport
258− let req = req' { isSecure = isSecure' }
433+ keepAlive <- processRequest istatus src req mremainingRef idxhdr nextBodyFlush
259− intercept' <- settingsIntercept settings req
434+ `E.catch` \e -> do
260− case intercept' of
435+ settingsOnException settings (Just req) e
261− Nothing -> do
436+ -- Don't throw the error again to prevent calling settingsOnException twice.
437+ return False
438+
439+ -- When doing a keep-alive connection, the other side may just
440+ -- close the connection. We don't want to treat that as an
441+ -- exceptional situation, so we pass in False to http1 (which
442+ -- in turn passes in False to recvRequest), indicating that
443+ -- this is not the first request. If, when trying to read the
444+ -- request headers, no data is available, recvRequest will
445+ -- throw a NoKeepAliveRequest exception, which we catch here
446+ -- and ignore. See: https://github.com/yesodweb/wai/issues/618
447+ when keepAlive $ http1 False addr istatus src
448+
449+ processRequest istatus src req mremainingRef idxhdr nextBodyFlush = do
262− -- Let the application run for as long as it wants
450+ -- Let the application run for as long as it wants
263− T.pause th
451+ T.pause th
264−
452+
265− -- In the event that some scarce resource was acquired during
453+ -- In the event that some scarce resource was acquired during
266− -- creating the request, we need to make sure that we don't get
454+ -- creating the request, we need to make sure that we don't get
267− -- an async exception before calling the ResponseSource.
455+ -- an async exception before calling the ResponseSource.
268− keepAlive <- mask $ \restore -> do
456+ keepAliveRef <- newIORef $ error "keepAliveRef not filled"
269− res <- restore $ app req
457+ r <- E.try $ app req $ \res -> do
270− T.resume th
458+ T.resume th
271− -- FIXME consider forcing evaluation of the res here to
459+ -- FIXME consider forcing evaluation of the res here to
272− -- send more meaningful error messages to the user.
460+ -- send more meaningful error messages to the user.
273− -- However, it may affect performance.
461+ -- However, it may affect performance.
274− writeIORef istatus False
462+ writeIORef istatus False
275− sendResponse conn ii restore req idxhdr leftover' res
463+ keepAlive <- sendResponse settings conn ii th req idxhdr (readSource src) res
276−
464+ writeIORef keepAliveRef keepAlive
465+ return ResponseReceived
466+ case r of
467+ Right ResponseReceived -> return ()
468+ Left e@(SomeException _)
469+ | Just (ExceptionInsideResponseBody e') <- fromException e -> throwIO e'
470+ | otherwise -> do
471+ keepAlive <- sendErrorResponse req istatus e
472+ settingsOnException settings (Just req) e
473+ writeIORef keepAliveRef keepAlive
474+
475+ keepAlive <- readIORef keepAliveRef
476+
277− -- We just send a Response and it takes a time to
477+ -- We just send a Response and it takes a time to
278− -- receive a Request again. If we immediately call recv,
478+ -- receive a Request again. If we immediately call recv,
279− -- it is likely to fail and the IO manager works.
479+ -- it is likely to fail and cause the IO manager to do some work.
280− -- It is very costy. So, we yield to another Haskell
480+ -- It is very costly, so we yield to another Haskell
281− -- thread hoping that the next Request will arraive
481+ -- thread hoping that the next Request will arrive
282− -- when this Haskell thread will be re-scheduled.
482+ -- when this Haskell thread will be re-scheduled.
283− -- This improves performance at least when
483+ -- This improves performance at least when
284− -- the number of cores is small.
484+ -- the number of cores is small.
285− Conc.yield
485+ Conc.yield
286−
486+
287− when keepAlive $ do
487+ if keepAlive
288− -- flush the rest of the request body
488+ then
289− requestBody req $$ CL.sinkNull
489+ -- If there is an unknown or large amount of data to still be read
290− ResumableSource fromClient' _ <- getSource
490+ -- from the request body, simple drop this connection instead of
491+ -- reading it all in to satisfy a keep-alive request.
492+ case settingsMaximumBodyFlush settings of
493+ Nothing -> do
494+ flushEntireBody nextBodyFlush
291 T.resume th
495 T.resume th
292− recvSendLoop istatus fromClient'
496+ return True
293− Just intercept -> do
497+ Just maxToRead -> do
294− T.pause th
498+ let tryKeepAlive = do
295− ResumableSource fromClient' _ <- getSource
499+ -- flush the rest of the request body
296− intercept fromClient' conn
500+ isComplete <- flushBody nextBodyFlush maxToRead
297−
501+ if isComplete then do
298−connSource :: Connection -> T.Handle -> IORef Bool -> Source IO ByteString
502+ T.resume th
299−connSource Connection { connRecv = recv } th istatus = src
503+ return True
504+ else
505+ return False
506+ case mremainingRef of
507+ Just ref -> do
508+ remaining <- readIORef ref
509+ if remaining <= maxToRead then
510+ tryKeepAlive
511+ else
512+ return False
513+ Nothing -> tryKeepAlive
514+ else
515+ return False
516+
517+ checkTLS = case transport of
518+ TCP -> return () -- direct
519+ tls -> unless (tls12orLater tls) $ goaway conn H2.InadequateSecurity "Weak TLS"
520+ tls12orLater tls = tlsMajorVersion tls == 3 && tlsMinorVersion tls >= 3
521+
522+-- connClose must not be called here since Run:fork calls it
523+goaway :: Connection -> H2.ErrorCodeId -> ByteString -> IO ()
524+goaway Connection{..} etype debugmsg = connSendAll bytestream
525+ where
526+ einfo = H2.encodeInfo id 0
527+ frame = H2.GoAwayFrame 0 etype debugmsg
528+ bytestream = H2.encodeFrame einfo frame
529+
530+flushEntireBody :: IO ByteString -> IO ()
531+flushEntireBody src =
532+ loop
533+ where
534+ loop = do
535+ bs <- src
536+ unless (S.null bs) loop
537+
538+flushBody :: IO ByteString -- ^ get next chunk
539+ -> Int -- ^ maximum to flush
540+ -> IO Bool -- ^ True == flushed the entire body, False == we didn't
541+flushBody src =
542+ loop
300 where
543 where
301− src = do
544+ loop toRead = do
302− bs <- liftIO recv
545+ bs <- src
546+ let toRead' = toRead - S.length bs
547+ case () of
548+ ()
549+ | S.null bs -> return True
550+ | toRead' >= 0 -> loop toRead'
551+ | otherwise -> return False
552+
553+wrappedRecv :: Connection -> T.Handle -> IORef Bool -> Int -> IO ByteString
554+wrappedRecv Connection { connRecv = recv } th istatus slowlorisSize = do
555+ bs <- recv
303− unless (S.null bs) $ do
556+ unless (S.null bs) $ do
304− liftIO $ do
305− writeIORef istatus True
557+ writeIORef istatus True
306− when (S.length bs >= 2048) $ T.tickle th
558+ when (S.length bs >= slowlorisSize) $ T.tickle th
307− yield bs
559+ return bs
308− src
309
560
561+wrappedRecvN :: T.Handle -> IORef Bool -> Int -> (BufSize -> IO ByteString) -> (BufSize -> IO ByteString)
562+wrappedRecvN th istatus slowlorisSize readN bufsize = do
563+ bs <- readN bufsize
564+ unless (S.null bs) $ do
565+ writeIORef istatus True
566+ -- TODO: think about the slowloris protection in HTTP2: current code
567+ -- might open a slow-loris attack vector. Rather than timing we should
568+ -- consider limiting the per-client connections assuming that in HTTP2
569+ -- we should allow only few connections per host (real-world
570+ -- deployments with large NATs may be trickier).
571+ when (S.length bs >= slowlorisSize || bufsize <= slowlorisSize) $ T.tickle th
572+ return bs
573+
574+-- | Set flag FileCloseOnExec flag on a socket (on Unix)
575+--
310-- Copied from: https://github.com/mzero/plush/blob/master/src/Plush/Server/Warp.hs
576-- Copied from: https://github.com/mzero/plush/blob/master/src/Plush/Server/Warp.hs
577+--
578+-- @since 3.2.17
311setSocketCloseOnExec :: Socket -> IO ()
579setSocketCloseOnExec :: Socket -> IO ()
312#if WINDOWS
580#if WINDOWS
313setSocketCloseOnExec _ = return ()
581setSocketCloseOnExec _ = return ()
314#else
582#else
315−setSocketCloseOnExec socket =
583+setSocketCloseOnExec socket = do
316− setFdOption (fromIntegral $ fdSocket socket) CloseOnExec True
584+#if MIN_VERSION_network(3,0,0)
585+ fd <- fdSocket socket
586+#else
587+ let fd = fdSocket socket
317#endif
588#endif
589+ F.setFileCloseOnExec $ fromIntegral fd
590+#endif
591+
592+gracefulShutdown :: Settings -> Counter -> IO ()
593+gracefulShutdown set counter =
594+ case settingsGracefulShutdownTimeout set of
595+ Nothing ->
596+ waitForZero counter
597+ (Just seconds) ->
598+ void (timeout (seconds * microsPerSecond) (waitForZero counter))
599+ where microsPerSecond = 1000000
warp/Network/Wai/Handler/Warp/SendFile.hs+139−19
@@ -1,29 +1,149 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2
2
3−module Network.Wai.Handler.Warp.SendFile where
3+module Network.Wai.Handler.Warp.SendFile (
4+ sendFile
5+ , readSendFile
6+ , packHeader -- for testing
7+#ifndef WINDOWS
8+ , positionRead
9+#endif
10+ ) where
4
11
5−import Data.ByteString (ByteString)
12+import qualified Data.ByteString as BS
6−import Network.Sendfile
7import Network.Socket (Socket)
13import Network.Socket (Socket)
14+
15+#ifdef WINDOWS
16+import Foreign.ForeignPtr (newForeignPtr_)
17+import Foreign.Ptr (plusPtr)
8−import qualified Network.Wai.Handler.Warp.FdCache as F
18+import qualified System.IO as IO
19+#else
20+import Control.Exception
21+import Foreign.C.Error (throwErrno)
22+import Foreign.C.Types
23+import Foreign.Ptr (Ptr, castPtr, plusPtr)
24+import Network.Sendfile
25+import Network.Wai.Handler.Warp.FdCache (openFile, closeFile)
26+import System.Posix.Types
27+#endif
28+
29+import Network.Wai.Handler.Warp.Buffer
30+import Network.Wai.Handler.Warp.Imports
9import Network.Wai.Handler.Warp.Types
31import Network.Wai.Handler.Warp.Types
10
32
11−defaultSendFile :: Socket -> FilePath -> Integer -> Integer -> IO () -> [ByteString] -> IO ()
33+----------------------------------------------------------------
12−defaultSendFile s path off len act hdr = sendfileWithHeader s path (PartOfFile off len) act hdr
13
34
35+-- | Function to send a file based on sendfile() for Linux\/Mac\/FreeBSD.
36+-- This makes use of the file descriptor cache.
37+-- For other OSes, this is identical to 'readSendFile'.
38+--
39+-- Since: 3.1.0
40+sendFile :: Socket -> Buffer -> BufSize -> (ByteString -> IO ()) -> SendFile
41+#ifdef SENDFILEFD
42+sendFile s _ _ _ fid off len act hdr = case mfid of
43+ -- settingsFdCacheDuration is 0
44+ Nothing -> sendfileWithHeader s path (PartOfFile off len) act hdr
45+ Just fd -> sendfileFdWithHeader s fd (PartOfFile off len) act hdr
46+ where
47+ mfid = fileIdFd fid
48+ path = fileIdPath fid
49+#else
50+sendFile _ = readSendFile
51+#endif
52+
53+----------------------------------------------------------------
14
54
15−#if SENDFILEFD
55+packHeader :: Buffer -> BufSize -> (ByteString -> IO ())
16−setSendFile :: Connection -> Maybe F.MutableFdCache -> Connection
56+ -> IO () -> [ByteString]
17−setSendFile conn Nothing = conn
57+ -> Int
18−setSendFile conn (Just fdcs) = case connSendFileOverride conn of
58+ -> IO Int
19− NotOverride -> conn
59+packHeader _ _ _ _ [] n = return n
20− Override s -> conn { connSendFile = sendFile fdcs s }
60+packHeader buf siz send hook (bs:bss) n
61+ | len < room = do
62+ let dst = buf `plusPtr` n
63+ void $ copy dst bs
64+ packHeader buf siz send hook bss (n + len)
65+ | otherwise = do
66+ let dst = buf `plusPtr` n
67+ (bs1, bs2) = BS.splitAt room bs
68+ void $ copy dst bs1
69+ bufferIO buf siz send
70+ hook
71+ packHeader buf siz send hook (bs2:bss) 0
72+ where
73+ len = BS.length bs
74+ room = siz - n
21
75
22−sendFile :: F.MutableFdCache -> Socket -> FilePath -> Integer -> Integer -> IO () -> [ByteString] -> IO ()
76+mini :: Int -> Integer -> Int
23−sendFile fdcs s path off len act hdr = do
77+mini i n
24− (fd, fresher) <- F.getFd fdcs path
78+ | fromIntegral i < n = i
25− sendfileFdWithHeader s fd (PartOfFile off len) (act>>fresher) hdr
79+ | otherwise = fromIntegral n
80+
81+-- | Function to send a file based on pread()\/send() for Unix.
82+-- This makes use of the file descriptor cache.
83+-- For Windows, this is emulated by 'Handle'.
84+--
85+-- Since: 3.1.0
86+#ifdef WINDOWS
87+readSendFile :: Buffer -> BufSize -> (ByteString -> IO ()) -> SendFile
88+readSendFile buf siz send fid off0 len0 hook headers = do
89+ hn <- packHeader buf siz send hook headers 0
90+ let room = siz - hn
91+ buf' = buf `plusPtr` hn
92+ IO.withBinaryFile path IO.ReadMode $ \h -> do
93+ IO.hSeek h IO.AbsoluteSeek off0
94+ n <- IO.hGetBufSome h buf' (mini room len0)
95+ bufferIO buf (hn + n) send
96+ hook
97+ let n' = fromIntegral n
98+ fptr <- newForeignPtr_ buf
99+ loop h fptr (len0 - n')
100+ where
101+ path = fileIdPath fid
102+ loop h fptr len
103+ | len <= 0 = return ()
104+ | otherwise = do
105+ n <- IO.hGetBufSome h buf (mini siz len)
106+ when (n /= 0) $ do
107+ let bs = PS fptr 0 n
108+ n' = fromIntegral n
109+ send bs
110+ hook
111+ loop h fptr (len - n')
26#else
112#else
27−setSendFile :: Connection -> Maybe F.MutableFdCache -> Connection
113+readSendFile :: Buffer -> BufSize -> (ByteString -> IO ()) -> SendFile
28−setSendFile conn _ = conn
114+readSendFile buf siz send fid off0 len0 hook headers =
115+ bracket setup teardown $ \fd -> do
116+ hn <- packHeader buf siz send hook headers 0
117+ let room = siz - hn
118+ buf' = buf `plusPtr` hn
119+ n <- positionRead fd buf' (mini room len0) off0
120+ bufferIO buf (hn + n) send
121+ hook
122+ let n' = fromIntegral n
123+ loop fd (len0 - n') (off0 + n')
124+ where
125+ path = fileIdPath fid
126+ setup = case fileIdFd fid of
127+ Just fd -> return fd
128+ Nothing -> openFile path
129+ teardown fd = case fileIdFd fid of
130+ Just _ -> return ()
131+ Nothing -> closeFile fd
132+ loop fd len off
133+ | len <= 0 = return ()
134+ | otherwise = do
135+ n <- positionRead fd buf (mini siz len) off
136+ bufferIO buf n send
137+ let n' = fromIntegral n
138+ hook
139+ loop fd (len - n') (off + n')
140+
141+positionRead :: Fd -> Buffer -> BufSize -> Integer -> IO Int
142+positionRead fd buf siz off = do
143+ bytes <- fromIntegral <$> c_pread fd (castPtr buf) (fromIntegral siz) (fromIntegral off)
144+ when (bytes < 0) $ throwErrno "positionRead"
145+ return bytes
146+
147+foreign import ccall unsafe "pread"
148+ c_pread :: Fd -> Ptr CChar -> ByteCount -> FileOffset -> IO CSsize
29#endif
149#endif
warp/Network/Wai/Handler/Warp/Settings.hs+150−38
@@ -1,34 +1,39 @@
1{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, ViewPatterns #-}
1{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, ViewPatterns #-}
2−{-# LANGUAGE PatternGuards #-}
2+{-# LANGUAGE PatternGuards, RankNTypes #-}
3+{-# LANGUAGE ImpredicativeTypes, CPP #-}
3
4
4module Network.Wai.Handler.Warp.Settings where
5module Network.Wai.Handler.Warp.Settings where
5
6
7+import Control.Concurrent (forkIOWithUnmask)
6import Control.Exception
8import Control.Exception
7−import Control.Monad (when)
9+import Data.ByteString.Builder (byteString)
8−import qualified Data.ByteString as S
10+import qualified Data.ByteString.Char8 as C8
11+import Data.ByteString.Lazy (fromStrict)
12+import Data.Streaming.Network (HostPreference)
9import qualified Data.Text as T
13import qualified Data.Text as T
10import qualified Data.Text.IO as TIO
14import qualified Data.Text.IO as TIO
11−import Data.Conduit
15+import Data.Version (showVersion)
12−import Data.Conduit.Network (HostPreference)
13import GHC.IO.Exception (IOErrorType(..))
16import GHC.IO.Exception (IOErrorType(..))
14import qualified Network.HTTP.Types as H
17import qualified Network.HTTP.Types as H
18+import Network.HTTP2( HTTP2Error (..), ErrorCodeId (..) )
15import Network.Socket (SockAddr)
19import Network.Socket (SockAddr)
16import Network.Wai
20import Network.Wai
17−import Network.Wai.Handler.Warp.Timeout
21+import qualified Paths_warp
18−import Network.Wai.Handler.Warp.Types
19import System.IO (stderr)
22import System.IO (stderr)
20import System.IO.Error (ioeGetErrorType)
23import System.IO.Error (ioeGetErrorType)
21−import qualified Data.Text.Lazy as TL
24+import System.TimeManager
22−import qualified Data.Text.Lazy.Encoding as TLE
25+
26+import Network.Wai.Handler.Warp.Imports
27+import Network.Wai.Handler.Warp.Types
23
28
24-- | Various Warp server settings. This is purposely kept as an abstract data
29-- | Various Warp server settings. This is purposely kept as an abstract data
25-- type so that new settings can be added without breaking backwards
30-- type so that new settings can be added without breaking backwards
26-- compatibility. In order to create a 'Settings' value, use 'defaultSettings'
31-- compatibility. In order to create a 'Settings' value, use 'defaultSettings'
27−-- and record syntax to modify individual records. For example:
32+-- and the various \'set\' functions to modify individual fields. For example:
28--
33--
29−-- > defaultSettings { settingsTimeout = 20 }
34+-- > setTimeout 20 defaultSettings
30data Settings = Settings
35data Settings = Settings
31− { settingsPort :: Int -- ^ Port to listen on. Default value: 3000
36+ { settingsPort :: Port -- ^ Port to listen on. Default value: 3000
32 , settingsHost :: HostPreference -- ^ Default value: HostIPv4
37 , settingsHost :: HostPreference -- ^ Default value: HostIPv4
33 , settingsOnException :: Maybe Request -> SomeException -> IO () -- ^ What to do with exceptions thrown by either the application or server. Default: ignore server-generated exceptions (see 'InvalidRequest') and print application-generated applications to stderr.
38 , settingsOnException :: Maybe Request -> SomeException -> IO () -- ^ What to do with exceptions thrown by either the application or server. Default: ignore server-generated exceptions (see 'InvalidRequest') and print application-generated applications to stderr.
34 , settingsOnExceptionResponse :: SomeException -> Response
39 , settingsOnExceptionResponse :: SomeException -> Response
@@ -40,9 +45,9 @@ data Settings = Settings
40 , settingsOnOpen :: SockAddr -> IO Bool -- ^ What to do when a connection is open. When 'False' is returned, the connection is closed immediately. Otherwise, the connection is going on. Default: always returns 'True'.
45 , settingsOnOpen :: SockAddr -> IO Bool -- ^ What to do when a connection is open. When 'False' is returned, the connection is closed immediately. Otherwise, the connection is going on. Default: always returns 'True'.
41 , settingsOnClose :: SockAddr -> IO () -- ^ What to do when a connection is close. Default: do nothing.
46 , settingsOnClose :: SockAddr -> IO () -- ^ What to do when a connection is close. Default: do nothing.
42 , settingsTimeout :: Int -- ^ Timeout value in seconds. Default value: 30
47 , settingsTimeout :: Int -- ^ Timeout value in seconds. Default value: 30
43− , settingsIntercept :: Request -> IO (Maybe (Source IO S.ByteString -> Connection -> IO ()))
44 , settingsManager :: Maybe Manager -- ^ Use an existing timeout manager instead of spawning a new one. If used, 'settingsTimeout' is ignored. Default is 'Nothing'
48 , settingsManager :: Maybe Manager -- ^ Use an existing timeout manager instead of spawning a new one. If used, 'settingsTimeout' is ignored. Default is 'Nothing'
45− , settingsFdCacheDuration :: Int -- ^ Cache duratoin time of file descriptors in seconds. 0 means that the cache mechanism is not used. Default value: 10
49+ , settingsFdCacheDuration :: Int -- ^ Cache duration time of file descriptors in seconds. 0 means that the cache mechanism is not used. Default value: 0
50+ , settingsFileInfoCacheDuration :: Int -- ^ Cache duration time of file information in seconds. 0 means that the cache mechanism is not used. Default value: 0
46 , settingsBeforeMainLoop :: IO ()
51 , settingsBeforeMainLoop :: IO ()
47 -- ^ Code to run after the listening socket is ready but before entering
52 -- ^ Code to run after the listening socket is ready but before entering
48 -- the main event loop. Useful for signaling to tests that they can start
53 -- the main event loop. Useful for signaling to tests that they can start
@@ -51,6 +56,17 @@ data Settings = Settings
51 -- Default: do nothing.
56 -- Default: do nothing.
52 --
57 --
53 -- Since 1.3.6
58 -- Since 1.3.6
59+
60+ , settingsFork :: ((forall a. IO a -> IO a) -> IO ()) -> IO ()
61+ -- ^ Code to fork a new thread to accept a connection.
62+ --
63+ -- This may be useful if you need OS bound threads, or if
64+ -- you wish to develop an alternative threading model.
65+ --
66+ -- Default: void . forkIOWithUnmask
67+ --
68+ -- Since 3.0.4
69+
54 , settingsNoParsePath :: Bool
70 , settingsNoParsePath :: Bool
55 -- ^ Perform no parsing on the rawPathInfo.
71 -- ^ Perform no parsing on the rawPathInfo.
56 --
72 --
@@ -59,27 +75,105 @@ data Settings = Settings
59 -- Default: False
75 -- Default: False
60 --
76 --
61 -- Since 2.0.3
77 -- Since 2.0.3
78+ , settingsInstallShutdownHandler :: IO () -> IO ()
79+ -- ^ An action to install a handler (e.g. Unix signal handler)
80+ -- to close a listen socket.
81+ -- The first argument is an action to close the listen socket.
82+ --
83+ -- Default: no action
84+ --
85+ -- Since 3.0.1
86+ , settingsServerName :: ByteString
87+ -- ^ Default server name if application does not set one.
88+ --
89+ -- Since 3.0.2
90+ , settingsMaximumBodyFlush :: Maybe Int
91+ -- ^ See @setMaximumBodyFlush@.
92+ --
93+ -- Since 3.0.3
94+ , settingsProxyProtocol :: ProxyProtocol
95+ -- ^ Specify usage of the PROXY protocol.
96+ --
97+ -- Since 3.0.5
98+ , settingsSlowlorisSize :: Int
99+ -- ^ Size of bytes read to prevent Slowloris protection. Default value: 2048
100+ --
101+ -- Since 3.1.2
102+ , settingsHTTP2Enabled :: Bool
103+ -- ^ Whether to enable HTTP2 ALPN/upgrades. Default: True
104+ --
105+ -- Since 3.1.7
106+ , settingsLogger :: Request -> H.Status -> Maybe Integer -> IO ()
107+ -- ^ A log function. Default: no action.
108+ --
109+ -- Since 3.1.10
110+ , settingsServerPushLogger :: Request -> ByteString -> Integer -> IO ()
111+ -- ^ A HTTP/2 server push log function. Default: no action.
112+ --
113+ -- Since 3.2.7
114+ , settingsGracefulShutdownTimeout :: Maybe Int
115+ -- ^ An optional timeout to limit the time (in seconds) waiting for
116+ -- a graceful shutdown of the web server.
117+ --
118+ -- Since 3.2.8
119+ , settingsGracefulCloseTimeout1 :: Int
120+ -- ^ A timeout to limit the time (in milliseconds) waiting for
121+ -- FIN for HTTP/1.x. 0 means uses immediate close.
122+ -- Default: 0.
123+ --
124+ -- Since 3.3.5
125+ , settingsGracefulCloseTimeout2 :: Int
126+ -- ^ A timeout to limit the time (in milliseconds) waiting for
127+ -- FIN for HTTP/2. 0 means uses immediate close.
128+ -- Default: 2000.
129+ --
130+ -- Since 3.3.5
131+ , settingsMaxTotalHeaderLength :: Int
132+ -- ^ Determines the maximum header size that Warp will tolerate when using HTTP/1.x.
133+ --
134+ -- Since 3.3.8
62 }
135 }
63
136
137+-- | Specify usage of the PROXY protocol.
138+data ProxyProtocol = ProxyProtocolNone
139+ -- ^ See @setProxyProtocolNone@.
140+ | ProxyProtocolRequired
141+ -- ^ See @setProxyProtocolRequired@.
142+ | ProxyProtocolOptional
143+ -- ^ See @setProxyProtocolOptional@.
144+
64-- | The default settings for the Warp server. See the individual settings for
145-- | The default settings for the Warp server. See the individual settings for
65-- the default value.
146-- the default value.
66defaultSettings :: Settings
147defaultSettings :: Settings
67defaultSettings = Settings
148defaultSettings = Settings
68 { settingsPort = 3000
149 { settingsPort = 3000
69 , settingsHost = "*4"
150 , settingsHost = "*4"
70− , settingsOnException = defaultExceptionHandler
151+ , settingsOnException = defaultOnException
71− , settingsOnExceptionResponse = defaultExceptionResponse
152+ , settingsOnExceptionResponse = defaultOnExceptionResponse
72 , settingsOnOpen = const $ return True
153 , settingsOnOpen = const $ return True
73 , settingsOnClose = const $ return ()
154 , settingsOnClose = const $ return ()
74 , settingsTimeout = 30
155 , settingsTimeout = 30
75− , settingsIntercept = const (return Nothing)
76 , settingsManager = Nothing
156 , settingsManager = Nothing
77− , settingsFdCacheDuration = 10
157+ , settingsFdCacheDuration = 0
158+ , settingsFileInfoCacheDuration = 0
78 , settingsBeforeMainLoop = return ()
159 , settingsBeforeMainLoop = return ()
160+ , settingsFork = void . forkIOWithUnmask
79 , settingsNoParsePath = False
161 , settingsNoParsePath = False
162+ , settingsInstallShutdownHandler = const $ return ()
163+ , settingsServerName = C8.pack $ "Warp/" ++ showVersion Paths_warp.version
164+ , settingsMaximumBodyFlush = Just 8192
165+ , settingsProxyProtocol = ProxyProtocolNone
166+ , settingsSlowlorisSize = 2048
167+ , settingsHTTP2Enabled = True
168+ , settingsLogger = \_ _ _ -> return ()
169+ , settingsServerPushLogger = \_ _ _ -> return ()
170+ , settingsGracefulShutdownTimeout = Nothing
171+ , settingsGracefulCloseTimeout1 = 0
172+ , settingsGracefulCloseTimeout2 = 2000
173+ , settingsMaxTotalHeaderLength = 50 * 1024
80 }
174 }
81
175
82−-- | Apply the logic provided by 'defaultExceptionHandler' to determine if an
176+-- | Apply the logic provided by 'defaultOnException' to determine if an
83-- exception should be shown or not. The goal is to hide exceptions which occur
177-- exception should be shown or not. The goal is to hide exceptions which occur
84-- under the normal course of the web server running.
178-- under the normal course of the web server running.
85--
179--
@@ -93,27 +187,45 @@ defaultShouldDisplayException se
93 | Just TimeoutThread <- fromException se = False
187 | Just TimeoutThread <- fromException se = False
94 | otherwise = True
188 | otherwise = True
95
189
190+-- | Printing an exception to standard error
191+-- if `defaultShouldDisplayException` returns `True`.
192+--
193+-- Since: 3.1.0
96−defaultExceptionHandler :: Maybe Request -> SomeException -> IO ()
194+defaultOnException :: Maybe Request -> SomeException -> IO ()
97−defaultExceptionHandler _ e =
195+defaultOnException _ e =
98 when (defaultShouldDisplayException e)
196 when (defaultShouldDisplayException e)
99 $ TIO.hPutStrLn stderr $ T.pack $ show e
197 $ TIO.hPutStrLn stderr $ T.pack $ show e
100
198
199+-- | Sending 400 for bad requests.
200+-- Sending 500 for internal server errors.
201+-- Since: 3.1.0
202+-- Sending 413 for too large payload.
203+-- Sending 431 for too large headers.
204+-- Since 3.2.27
101−defaultExceptionResponse :: SomeException -> Response
205+defaultOnExceptionResponse :: SomeException -> Response
206+defaultOnExceptionResponse e
207+ | Just (_ :: InvalidRequest) <-
208+ fromException e = responseLBS H.badRequest400
209+ [(H.hContentType, "text/plain; charset=utf-8")]
210+ "Bad Request"
211+ | Just (ConnectionError (UnknownErrorCode 413) t) <-
212+ fromException e = responseLBS H.status413
213+ [(H.hContentType, "text/plain; charset=utf-8")]
214+ (fromStrict t)
215+ | Just (ConnectionError (UnknownErrorCode 431) t) <-
216+ fromException e = responseLBS H.status431
217+ [(H.hContentType, "text/plain; charset=utf-8")]
218+ (fromStrict t)
219+ | otherwise = responseLBS H.internalServerError500
102−defaultExceptionResponse _ = responseLBS H.internalServerError500 [(H.hContentType, "text/plain; charset=utf-8")] "Something went wrong"
220+ [(H.hContentType, "text/plain; charset=utf-8")]
221+ "Something went wrong"
103
222
223+-- | Exception handler for the debugging purpose.
104−-- | Default implementation of 'settingsOnExceptionResponse' for the debugging purpose. 500, text/plain, a showed exception.
224+-- 500, text/plain, a showed exception.
225+--
226+-- Since: 2.0.3.2
105exceptionResponseForDebug :: SomeException -> Response
227exceptionResponseForDebug :: SomeException -> Response
228+exceptionResponseForDebug e =
229+ responseBuilder H.internalServerError500
106−exceptionResponseForDebug e = responseLBS H.internalServerError500 [(H.hContentType, "text/plain; charset=utf-8")] (TLE.encodeUtf8 $ TL.pack $ "Exception: " ++ show e)
230+ [(H.hContentType, "text/plain; charset=utf-8")]
107−
231+ $ byteString . C8.pack $ "Exception: " ++ show e
108−{-# DEPRECATED settingsPort "Use setPort instead" #-}
109−{-# DEPRECATED settingsHost "Use setHost instead" #-}
110−{-# DEPRECATED settingsOnException "Use setOnException instead" #-}
111−{-# DEPRECATED settingsOnExceptionResponse "Use setOnExceptionResponse instead" #-}
112−{-# DEPRECATED settingsOnOpen "Use setOnOpen instead" #-}
113−{-# DEPRECATED settingsOnClose "Use setOnClose instead" #-}
114−{-# DEPRECATED settingsTimeout "Use setTimeout instead" #-}
115−{-# DEPRECATED settingsIntercept "Use setIntercept instead" #-}
116−{-# DEPRECATED settingsManager "Use setManager instead" #-}
117−{-# DEPRECATED settingsFdCacheDuration "Use setFdCacheDuration instead" #-}
118−{-# DEPRECATED settingsBeforeMainLoop "Use setBeforeMainLoop instead" #-}
119−{-# DEPRECATED settingsNoParsePath "Use setNoParsePath instead" #-}
warp/Network/Wai/Handler/Warp/Thread.hsdeleted−28
@@ -1,28 +0,0 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−
3−module Network.Wai.Handler.Warp.Thread (
4− forkIOwithBreakableForever
5− , breakForever
6− ) where
7−
8−import Control.Concurrent (forkIO)
9−import Control.Exception (handle, throw, mask_, Exception)
10−import Control.Monad (void, forever)
11−import Data.IORef
12−import Data.Typeable
13−
14−data BreakForever = BreakForever deriving (Show, Typeable)
15−
16−instance Exception BreakForever
17−
18−forkIOwithBreakableForever :: a -> (IORef a -> IO ()) -> IO (IORef a)
19−forkIOwithBreakableForever ini action = do
20− ref <- newIORef ini
21− void . forkIO . handle stopPropagation . forever . mask_ $ action ref
22− return ref
23−
24−stopPropagation :: BreakForever -> IO ()
25−stopPropagation _ = return ()
26−
27−breakForever :: IORef a -> IO a
28−breakForever ref = atomicModifyIORef ref $ \x -> (throw BreakForever, x)
warp/Network/Wai/Handler/Warp/Timeout.hsdeleted−183
@@ -1,183 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE BangPatterns #-}
3−{-# LANGUAGE DeriveDataTypeable #-}
4−{-# LANGUAGE UnboxedTuples, MagicHash #-}
5−
6−-- |
7−--
8−-- In order to provide slowloris protection, Warp provides timeout handlers. We
9−-- follow these rules:
10−--
11−-- * A timeout is created when a connection is opened.
12−--
13−-- * When all request headers are read, the timeout is tickled.
14−--
15−-- * Every time at least 2048 bytes of the request body are read, the timeout
16−-- is tickled.
17−--
18−-- * The timeout is paused while executing user code. This will apply to both
19−-- the application itself, and a ResponseSource response. The timeout is
20−-- resumed as soon as we return from user code.
21−--
22−-- * Every time data is successfully sent to the client, the timeout is tickled.
23−
24−module Network.Wai.Handler.Warp.Timeout (
25− -- * Types
26− Manager
27− , TimeoutAction
28− , Handle
29− -- * Manager
30− , initialize
31− , stopManager
32− , withManager
33− -- * Registration
34− , register
35− , registerKillThread
36− -- * Control
37− , tickle
38− , cancel
39− , pause
40− , resume
41− -- * Exceptions
42− , TimeoutThread (..)
43− ) where
44−
45−#if MIN_VERSION_base(4,6,0)
46−import Control.Concurrent (mkWeakThreadId, ThreadId)
47−#else
48−import GHC.Conc (ThreadId(..))
49−import GHC.Exts (mkWeak#)
50−import GHC.IO (IO (IO))
51−#endif
52−import Control.Concurrent (threadDelay, myThreadId)
53−import qualified Control.Exception as E
54−import GHC.Weak (Weak (..))
55−import Network.Wai.Handler.Warp.IORef (IORef)
56−import qualified Network.Wai.Handler.Warp.IORef as I
57−import Network.Wai.Handler.Warp.Thread
58−import System.Mem.Weak (deRefWeak)
59−import Data.Typeable (Typeable)
60−
61−----------------------------------------------------------------
62−
63−-- | A timeout manager
64−newtype Manager = Manager (IORef [Handle])
65−
66−-- | An action to be performed on timeout.
67−type TimeoutAction = IO ()
68−
69−-- | A handle used by 'Manager'
70−data Handle = Handle TimeoutAction (IORef State)
71−
72−data State = Active -- Manager turns it to Inactive.
73− | Inactive -- Manager removes it with timeout action.
74− | Paused -- Manager does not change it.
75− | Canceled -- Manager removes it without timeout action.
76−
77−----------------------------------------------------------------
78−
79−-- | Creating timeout manager which works every N micro seconds
80−-- where N is the first argument.
81−initialize :: Int -> IO Manager
82−initialize timeout = do
83− ref' <- forkIOwithBreakableForever [] $ \ref -> do
84− threadDelay timeout
85− old <- I.atomicModifyIORef' ref (\x -> ([], x))
86− merge <- prune old id
87− I.atomicModifyIORef' ref (\new -> (merge new, ()))
88− return $ Manager ref'
89− where
90− prune [] front = return front
91− prune (m@(Handle onTimeout iactive):rest) front = do
92− state <- I.atomicModifyIORef' iactive (\x -> (inactivate x, x))
93− case state of
94− Inactive -> do
95− onTimeout `E.catch` ignoreAll
96− prune rest front
97− Canceled -> prune rest front
98− _ -> prune rest (front . (:) m)
99− inactivate Active = Inactive
100− inactivate x = x
101−
102−----------------------------------------------------------------
103−
104−-- | Stopping timeout manager.
105−stopManager :: Manager -> IO ()
106−stopManager (Manager ref) = E.mask_ $ do
107− !handles <- breakForever ref
108− mapM_ fire handles
109− where
110− fire (Handle onTimeout _) = onTimeout `E.catch` ignoreAll
111−
112−ignoreAll :: E.SomeException -> IO ()
113−ignoreAll _ = return ()
114−
115−----------------------------------------------------------------
116−
117−-- | Registering a timeout action.
118−register :: Manager -> TimeoutAction -> IO Handle
119−register (Manager ref) onTimeout = do
120− iactive <- I.newIORef Active
121− let h = Handle onTimeout iactive
122− I.atomicModifyIORef' ref (\x -> (h : x, ()))
123− return h
124−
125−-- | Registering a timeout action of killing this thread.
126−registerKillThread :: Manager -> IO Handle
127−registerKillThread m = do
128− wtid <- myThreadId >>= mkWeakThreadId
129− register m $ killIfExist wtid
130−
131−-- If ThreadId is hold referred by a strong reference,
132−-- it leaks even after the thread is killed.
133−-- So, let's use a weak reference so that CG can throw ThreadId away.
134−-- deRefWeak checks if ThreadId referenced by the weak reference
135−-- exists. If exists, it means that the thread is alive.
136−killIfExist :: Weak ThreadId -> TimeoutAction
137−killIfExist wtid = deRefWeak wtid >>= maybe (return ()) (flip E.throwTo TimeoutThread)
138−
139−data TimeoutThread = TimeoutThread
140− deriving Typeable
141−instance E.Exception TimeoutThread
142−instance Show TimeoutThread where
143− show TimeoutThread = "Thread killed by Warp's timeout reaper"
144−
145−#if !MIN_VERSION_base(4,6,0)
146−mkWeakThreadId :: ThreadId -> IO (Weak ThreadId)
147−mkWeakThreadId t@(ThreadId t#) = IO $ \s ->
148− case mkWeak# t# t Nothing s of
149− (# s1, w #) -> (# s1, Weak w #)
150−#endif
151−
152−----------------------------------------------------------------
153−
154−-- | Setting the state to active.
155−-- 'Manager' turns active to inactive repeatedly.
156−tickle :: Handle -> IO ()
157−tickle (Handle _ iactive) = I.writeIORef iactive Active
158−
159−-- | Setting the state to canceled.
160−-- 'Manager' eventually removes this without timeout action.
161−cancel :: Handle -> IO ()
162−cancel (Handle _ iactive) = I.writeIORef iactive Canceled
163−
164−-- | Setting the state to paused.
165−-- 'Manager' does not change the value.
166−pause :: Handle -> IO ()
167−pause (Handle _ iactive) = I.writeIORef iactive Paused
168−
169−-- | Setting the paused state to active.
170−-- This is an alias to 'tickle'.
171−resume :: Handle -> IO ()
172−resume = tickle
173−
174−----------------------------------------------------------------
175−
176−-- | Call the inner function with a timeout manager.
177−withManager :: Int -- ^ timeout in microseconds
178− -> (Manager -> IO a)
179− -> IO a
180−withManager timeout f = do
181− -- FIXME when stopManager is available, use it
182− man <- initialize timeout
183− f man
warp/Network/Wai/Handler/Warp/Types.hs+134−35
@@ -5,14 +5,18 @@
5module Network.Wai.Handler.Warp.Types where
5module Network.Wai.Handler.Warp.Types where
6
6
7import Control.Exception
7import Control.Exception
8−import Data.ByteString (ByteString)
8+import qualified Data.ByteString as S
9+import Data.IORef (IORef, readIORef, writeIORef, newIORef)
9import Data.Typeable (Typeable)
10import Data.Typeable (Typeable)
10−import Network.HTTP.Types.Header
11+import Data.X509
11−import Network.Socket (Socket)
12+import Foreign.Ptr (Ptr)
13+import System.Posix.Types (Fd)
14+import qualified System.TimeManager as T
15+
12import qualified Network.Wai.Handler.Warp.Date as D
16import qualified Network.Wai.Handler.Warp.Date as D
13import qualified Network.Wai.Handler.Warp.FdCache as F
17import qualified Network.Wai.Handler.Warp.FdCache as F
14−import qualified Network.Wai.Handler.Warp.Timeout as T
18+import qualified Network.Wai.Handler.Warp.FileInfoCache as I
15−import Network.Wai.Handler.Warp.Buffer (Buffer,BufSize)
19+import Network.Wai.Handler.Warp.Imports
16
20
17----------------------------------------------------------------
21----------------------------------------------------------------
18
22
@@ -24,18 +28,6 @@ type Port = Int
24-- | The type for header value used with 'HeaderName'.
28-- | The type for header value used with 'HeaderName'.
25type HeaderValue = ByteString
29type HeaderValue = ByteString
26
30
27−hTransferEncoding :: HeaderName
28−hTransferEncoding = "Transfer-Encoding"
29−
30−hContentRange :: HeaderName
31−hContentRange = "Content-Range"
32−
33−hAcceptRanges :: HeaderName
34−hAcceptRanges = "Accept-Ranges"
35−
36−hServer :: HeaderName
37−hServer = "Server"
38−
39----------------------------------------------------------------
31----------------------------------------------------------------
40
32
41-- | Error types for bad 'Request'.
33-- | Error types for bad 'Request'.
@@ -45,6 +37,7 @@ data InvalidRequest = NotEnoughLines [String]
45 | IncompleteHeaders
37 | IncompleteHeaders
46 | ConnectionClosedByPeer
38 | ConnectionClosedByPeer
47 | OverLargeHeader
39 | OverLargeHeader
40+ | BadProxyHeader String
48 deriving (Eq, Typeable)
41 deriving (Eq, Typeable)
49
42
50instance Show InvalidRequest where
43instance Show InvalidRequest where
@@ -54,36 +47,142 @@ instance Show InvalidRequest where
54 show IncompleteHeaders = "Warp: Request headers did not finish transmission"
47 show IncompleteHeaders = "Warp: Request headers did not finish transmission"
55 show ConnectionClosedByPeer = "Warp: Client closed connection prematurely"
48 show ConnectionClosedByPeer = "Warp: Client closed connection prematurely"
56 show OverLargeHeader = "Warp: Request headers too large, possible memory attack detected. Closing connection."
49 show OverLargeHeader = "Warp: Request headers too large, possible memory attack detected. Closing connection."
50+ show (BadProxyHeader s) = "Warp: Invalid PROXY protocol header: " ++ show s
57
51
58instance Exception InvalidRequest
52instance Exception InvalidRequest
59
53
60----------------------------------------------------------------
54----------------------------------------------------------------
61
55
62−-- | Whether or not 'ConnSendFileOverride' in 'Connection' can be
56+-- | Exception thrown if something goes wrong while in the midst of
63−-- overridden. This is a kind of hack to keep the signature of
57+-- sending a response, since the status code can't be altered at that
64−-- 'Connection' clean.
58+-- point.
65−data ConnSendFileOverride = NotOverride -- ^ Don't override
59+--
66− | Override Socket -- ^ Override with this 'Socket'
60+-- Used to determine whether keeping the HTTP1.1 connection / HTTP2 stream alive is safe
61+-- or irrecoverable.
62+
63+newtype ExceptionInsideResponseBody = ExceptionInsideResponseBody SomeException
64+ deriving (Show, Typeable)
65+
66+instance Exception ExceptionInsideResponseBody
67
67
68----------------------------------------------------------------
68----------------------------------------------------------------
69
69
70+-- | Data type to abstract file identifiers.
71+-- On Unix, a file descriptor would be specified to make use of
72+-- the file descriptor cache.
73+--
74+-- Since: 3.1.0
75+data FileId = FileId {
76+ fileIdPath :: FilePath
77+ , fileIdFd :: Maybe Fd
78+ }
79+
80+-- | fileid, offset, length, hook action, HTTP headers
81+--
82+-- Since: 3.1.0
83+type SendFile = FileId -> Integer -> Integer -> IO () -> [ByteString] -> IO ()
84+
85+-- | Type for read buffer pool
86+type BufferPool = IORef ByteString
87+
88+-- | Type for buffer
89+type Buffer = Ptr Word8
90+
91+-- | Type for buffer size
92+type BufSize = Int
93+
94+-- | Type for the action to receive input data
95+type Recv = IO ByteString
96+
97+-- | Type for the action to receive input data with a buffer.
98+-- The result boolean indicates whether or not the buffer is fully filled.
99+type RecvBuf = Buffer -> BufSize -> IO Bool
100+
70-- | Data type to manipulate IO actions for connections.
101-- | Data type to manipulate IO actions for connections.
102+-- This is used to abstract IO actions for plain HTTP and HTTP over TLS.
71−data Connection = Connection
103+data Connection = Connection {
104+ -- | This is not used at this moment.
72− { connSendMany :: [ByteString] -> IO ()
105+ connSendMany :: [ByteString] -> IO ()
106+ -- | The sending function.
73− , connSendAll :: ByteString -> IO ()
107+ , connSendAll :: ByteString -> IO ()
74− , connSendFile :: FilePath -> Integer -> Integer -> IO () -> [ByteString] -> IO () -- ^ filepath, offset, length, hook action, HTTP headers
108+ -- | The sending function for files in HTTP/1.1.
75− , connClose :: IO ()
109+ , connSendFile :: SendFile
110+ -- | The connection closing function. Warp guarantees it will only be
111+ -- called once. Other functions (like 'connRecv') may be called after
112+ -- 'connClose' is called.
113+ , connClose :: IO ()
114+ -- | Free any buffers allocated. Warp guarantees it will only be
115+ -- called once, and no other functions will be called after it.
116+ , connFree :: IO ()
117+ -- | The connection receiving function. This returns "" for EOF.
76− , connRecv :: IO ByteString
118+ , connRecv :: Recv
77− , connBuffer :: Buffer
119+ -- | The connection receiving function. This tries to fill the buffer.
120+ -- This returns when the buffer is filled or reaches EOF.
121+ , connRecvBuf :: RecvBuf
122+ -- | The write buffer.
123+ , connWriteBuffer :: Buffer
124+ -- | The size of the write buffer.
78− , connBufferSize :: BufSize
125+ , connBufferSize :: BufSize
79− , connSendFileOverride :: ConnSendFileOverride
126+ -- | Is this connection HTTP/2?
127+ , connHTTP2 :: IORef Bool
80 }
128 }
81
129
130+getConnHTTP2 :: Connection -> IO Bool
131+getConnHTTP2 conn = readIORef (connHTTP2 conn)
132+
133+setConnHTTP2 :: Connection -> Bool -> IO ()
134+setConnHTTP2 conn b = writeIORef (connHTTP2 conn) b
135+
82----------------------------------------------------------------
136----------------------------------------------------------------
83
137
84−-- | Internal information.
85data InternalInfo = InternalInfo {
138data InternalInfo = InternalInfo {
86− threadHandle :: T.Handle
139+ timeoutManager :: T.Manager
87− , fdCacher :: Maybe F.MutableFdCache
140+ , getDate :: IO D.GMTDate
88− , dateCacher :: D.DateCache
141+ , getFd :: FilePath -> IO (Maybe F.Fd, F.Refresh)
142+ , getFileInfo :: FilePath -> IO I.FileInfo
89 }
143 }
144+
145+----------------------------------------------------------------
146+
147+-- | Type for input streaming.
148+data Source = Source !(IORef ByteString) !(IO ByteString)
149+
150+mkSource :: IO ByteString -> IO Source
151+mkSource func = do
152+ ref <- newIORef S.empty
153+ return $! Source ref func
154+
155+readSource :: Source -> IO ByteString
156+readSource (Source ref func) = do
157+ bs <- readIORef ref
158+ if S.null bs
159+ then func
160+ else do
161+ writeIORef ref S.empty
162+ return bs
163+
164+-- | Read from a Source, ignoring any leftovers.
165+readSource' :: Source -> IO ByteString
166+readSource' (Source _ func) = func
167+
168+leftoverSource :: Source -> ByteString -> IO ()
169+leftoverSource (Source ref _) bs = writeIORef ref bs
170+
171+readLeftoverSource :: Source -> IO ByteString
172+readLeftoverSource (Source ref _) = readIORef ref
173+
174+----------------------------------------------------------------
175+
176+-- | What kind of transport is used for this connection?
177+data Transport = TCP -- ^ Plain channel: TCP
178+ | TLS {
179+ tlsMajorVersion :: Int
180+ , tlsMinorVersion :: Int
181+ , tlsNegotiatedProtocol :: Maybe ByteString -- ^ The result of Application Layer Protocol Negociation in RFC 7301
182+ , tlsChiperID :: Word16
183+ , tlsClientCertificate :: Maybe CertificateChain
184+ } -- ^ Encrypted channel: TLS or SSL
185+
186+isTransportSecure :: Transport -> Bool
187+isTransportSecure TCP = False
188+isTransportSecure _ = True
warp/Network/Wai/Handler/Warp/Windows.hsnew file+27
@@ -0,0 +1,27 @@
1+{-# LANGUAGE CPP #-}
2+module Network.Wai.Handler.Warp.Windows
3+ ( windowsThreadBlockHack
4+ ) where
5+
6+#if WINDOWS
7+import Control.Exception
8+import Control.Concurrent.MVar
9+import Control.Concurrent
10+
11+import Network.Wai.Handler.Warp.Imports
12+
13+-- | Allow main socket listening thread to be interrupted on Windows platform
14+--
15+-- @since 3.2.17
16+windowsThreadBlockHack :: IO a -> IO a
17+windowsThreadBlockHack act = do
18+ var <- newEmptyMVar :: IO (MVar (Either SomeException a))
19+ void . forkIO $ try act >>= putMVar var
20+ res <- takeMVar var
21+ case res of
22+ Left e -> throwIO e
23+ Right r -> return r
24+#else
25+windowsThreadBlockHack :: IO a -> IO a
26+windowsThreadBlockHack = id
27+#endif
warp/Network/Wai/Handler/Warp/WithApplication.hsnew file+104
@@ -0,0 +1,104 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.Handler.Warp.WithApplication (
3+ withApplication,
4+ withApplicationSettings,
5+ testWithApplication,
6+ testWithApplicationSettings,
7+ openFreePort,
8+ withFreePort,
9+) where
10+
11+import Control.Concurrent
12+import Control.Concurrent.Async
13+import Control.Exception
14+import Control.Monad (when)
15+import Data.Streaming.Network (bindRandomPortTCP)
16+import Network.Socket
17+import Network.Wai
18+import Network.Wai.Handler.Warp.Run
19+import Network.Wai.Handler.Warp.Settings
20+import Network.Wai.Handler.Warp.Types
21+
22+-- | Runs the given 'Application' on a free port. Passes the port to the given
23+-- operation and executes it, while the 'Application' is running. Shuts down the
24+-- server before returning.
25+--
26+-- @since 3.2.4
27+withApplication :: IO Application -> (Port -> IO a) -> IO a
28+withApplication = withApplicationSettings defaultSettings
29+
30+-- | 'withApplication' with given 'Settings'. This will ignore the port value
31+-- set by 'setPort' in 'Settings'.
32+--
33+-- @since 3.2.7
34+withApplicationSettings :: Settings -> IO Application -> (Port -> IO a) -> IO a
35+withApplicationSettings settings' mkApp action = do
36+ app <- mkApp
37+ withFreePort $ \ (port, sock) -> do
38+ started <- mkWaiter
39+ let settings =
40+ settings' {
41+ settingsBeforeMainLoop
42+ = notify started () >> settingsBeforeMainLoop settings'
43+ }
44+ result <- race
45+ (runSettingsSocket settings sock app)
46+ (waitFor started >> action port)
47+ case result of
48+ Left () -> throwIO $ ErrorCall "Unexpected: runSettingsSocket exited"
49+ Right x -> return x
50+
51+-- | Same as 'withApplication' but with different exception handling: If the
52+-- given 'Application' throws an exception, 'testWithApplication' will re-throw
53+-- the exception to the calling thread, possibly interrupting the execution of
54+-- the given operation.
55+--
56+-- This is handy for running tests against an 'Application' over a real network
57+-- port. When running tests, it's useful to let exceptions thrown by your
58+-- 'Application' propagate to the main thread of the test-suite.
59+--
60+-- __The exception handling makes this function unsuitable for use in production.__
61+-- Use 'withApplication' instead.
62+--
63+-- @since 3.2.4
64+testWithApplication :: IO Application -> (Port -> IO a) -> IO a
65+testWithApplication = testWithApplicationSettings defaultSettings
66+
67+-- | 'testWithApplication' with given 'Settings'.
68+--
69+-- @since 3.2.7
70+testWithApplicationSettings :: Settings -> IO Application -> (Port -> IO a) -> IO a
71+testWithApplicationSettings settings mkApp action = do
72+ callingThread <- myThreadId
73+ app <- mkApp
74+ let wrappedApp request respond =
75+ app request respond `catch` \ e -> do
76+ when
77+ (defaultShouldDisplayException e)
78+ (throwTo callingThread e)
79+ throwIO e
80+ withApplicationSettings settings (return wrappedApp) action
81+
82+data Waiter a
83+ = Waiter {
84+ notify :: a -> IO (),
85+ waitFor :: IO a
86+ }
87+
88+mkWaiter :: IO (Waiter a)
89+mkWaiter = do
90+ mvar <- newEmptyMVar
91+ return Waiter {
92+ notify = putMVar mvar,
93+ waitFor = readMVar mvar
94+ }
95+
96+-- | Opens a socket on a free port and returns both port and socket.
97+--
98+-- @since 3.2.4
99+openFreePort :: IO (Port, Socket)
100+openFreePort = bindRandomPortTCP "127.0.0.1"
101+
102+-- | Like 'openFreePort' but closes the socket before exiting.
103+withFreePort :: ((Port, Socket) -> IO a) -> IO a
104+withFreePort = bracket openFreePort (close . snd)
warp/README.mdnew file+3
@@ -0,0 +1,3 @@
1+# Warp
2+
3+Warp is a server library for HTTP/1.x and HTTP/2 based WAI(Web Application Interface in Haskell). For more information, see [Warp](http://www.aosabook.org/en/posa/warp.html).
warp/attic/pong.hs+12−12
@@ -9,7 +9,7 @@ import qualified Data.Conduit.List as CL
9
9
10main = run 3000 app
10main = run 3000 app
11
11
12−app req = return $
12+app req = ($
13 case rawPathInfo req of
13 case rawPathInfo req of
14 "/builder/withlen" -> builderWithLen
14 "/builder/withlen" -> builderWithLen
15 "/builder/nolen" -> builderNoLen
15 "/builder/nolen" -> builderNoLen
@@ -17,36 +17,36 @@ app req = return $
17 "/file/nolen" -> fileNoLen
17 "/file/nolen" -> fileNoLen
18 "/source/withlen" -> sourceWithLen
18 "/source/withlen" -> sourceWithLen
19 "/source/nolen" -> sourceNoLen
19 "/source/nolen" -> sourceNoLen
20− "/notfound" -> ResponseFile status200 [] "notfound" Nothing
20+ "/notfound" -> responseFile status200 [] "notfound" Nothing
21− x -> index x
21+ x -> index x)
22
22
23−builderWithLen = ResponseBuilder
23+builderWithLen = responseBuilder
24 status200
24 status200
25 [ ("Content-Type", "text/plain")
25 [ ("Content-Type", "text/plain")
26 , ("Content-Length", "4")
26 , ("Content-Length", "4")
27 ]
27 ]
28 $ copyByteString "PONG"
28 $ copyByteString "PONG"
29
29
30−builderNoLen = ResponseBuilder
30+builderNoLen = responseBuilder
31 status200
31 status200
32 [ ("Content-Type", "text/plain")
32 [ ("Content-Type", "text/plain")
33 ]
33 ]
34 $ copyByteString "PONG"
34 $ copyByteString "PONG"
35
35
36−sourceWithLen = ResponseSource
36+sourceWithLen = responseStream
37 status200
37 status200
38 [ ("Content-Type", "text/plain")
38 [ ("Content-Type", "text/plain")
39 , ("Content-Length", "4")
39 , ("Content-Length", "4")
40 ]
40 ]
41− $ CL.sourceList [C.Chunk $ copyByteString "PONG"]
41+ $ \send _ -> send $ copyByteString "PONG"
42
42
43−sourceNoLen = ResponseSource
43+sourceNoLen = responseStream
44 status200
44 status200
45 [ ("Content-Type", "text/plain")
45 [ ("Content-Type", "text/plain")
46 ]
46 ]
47− $ CL.sourceList [C.Chunk $ copyByteString "PONG"]
47+ $ \send _ -> send $ copyByteString "PONG"
48
48
49−fileWithLen = ResponseFile
49+fileWithLen = responseFile
50 status200
50 status200
51 [ ("Content-Type", "text/plain")
51 [ ("Content-Type", "text/plain")
52 , ("Content-Length", "4")
52 , ("Content-Length", "4")
@@ -54,14 +54,14 @@ fileWithLen = ResponseFile
54 "pong.txt"
54 "pong.txt"
55 Nothing
55 Nothing
56
56
57−fileNoLen = ResponseFile
57+fileNoLen = responseFile
58 status200
58 status200
59 [ ("Content-Type", "text/plain")
59 [ ("Content-Type", "text/plain")
60 ]
60 ]
61 "pong.txt"
61 "pong.txt"
62 Nothing
62 Nothing
63
63
64−index p = ResponseBuilder status200 [("Content-Type", "text/html")] $ mconcat $ map copyByteString
64+index p = responseBuilder status200 [("Content-Type", "text/html")] $ mconcat $ map copyByteString
65 [ "<p><a href='/builder/withlen'>builder withlen</a></p>\n"
65 [ "<p><a href='/builder/withlen'>builder withlen</a></p>\n"
66 , "<p><a href='/builder/nolen'>builder nolen</a></p>\n"
66 , "<p><a href='/builder/nolen'>builder nolen</a></p>\n"
67 , "<p><a href='/file/withlen'>file withlen</a></p>\n"
67 , "<p><a href='/file/withlen'>file withlen</a></p>\n"
warp/attic/server-no-keepalive.hs+1−1
@@ -12,7 +12,7 @@ import Network.Wai (responseLBS)
12import Network.HTTP.Types (status200)
12import Network.HTTP.Types (status200)
13import Data.Enumerator (($$), run_)
13import Data.Enumerator (($$), run_)
14
14
15−app = const $ return $ responseLBS status200 [("Content-type", "text/plain")] "This is not kept alive under any circumtances"
15+app = const $ return $ responseLBS status200 [("Content-type", "text/plain")] "This is not kept alive under any circumstances"
16
16
17main = withManager 30000000 $ \man -> bracket
17main = withManager 30000000 $ \man -> bracket
18 (bindPort (settingsPort set) (settingsHost set))
18 (bindPort (settingsPort set) (settingsHost set))
warp/bench/Parser.hs+21−16
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE BangPatterns #-}
3
4
@@ -18,7 +19,11 @@ import Foreign.ForeignPtr
18import Foreign.Ptr
19import Foreign.Ptr
19import Foreign.Storable
20import Foreign.Storable
20
21
22+#if MIN_VERSION_gauge(0, 2, 0)
21−import Criterion.Main
23+import Gauge
24+#else
25+import Gauge.Main
26+#endif
22
27
23-- $setup
28-- $setup
24-- >>> :set -XOverloadedStrings
29-- >>> :set -XOverloadedStrings
@@ -32,15 +37,15 @@ main = do
32 defaultMain [
37 defaultMain [
33 bgroup "requestLine1" [
38 bgroup "requestLine1" [
34 bench "parseRequestLine3" $ whnf parseRequestLine3 requestLine1
39 bench "parseRequestLine3" $ whnf parseRequestLine3 requestLine1
35− , bench "parseRequestLine2" $ parseRequestLine2 requestLine1
40+ , bench "parseRequestLine2" $ whnfIO $ parseRequestLine2 requestLine1
36− , bench "parseRequestLine1" $ parseRequestLine1 requestLine1
41+ , bench "parseRequestLine1" $ whnfIO $ parseRequestLine1 requestLine1
37− , bench "parseRequestLine0" $ parseRequestLine0 requestLine1
42+ , bench "parseRequestLine0" $ whnfIO $ parseRequestLine0 requestLine1
38 ]
43 ]
39 , bgroup "requestLine2" [
44 , bgroup "requestLine2" [
40 bench "parseRequestLine3" $ whnf parseRequestLine3 requestLine2
45 bench "parseRequestLine3" $ whnf parseRequestLine3 requestLine2
41− , bench "parseRequestLine2" $ parseRequestLine2 requestLine2
46+ , bench "parseRequestLine2" $ whnfIO $ parseRequestLine2 requestLine2
42− , bench "parseRequestLine1" $ parseRequestLine1 requestLine2
47+ , bench "parseRequestLine1" $ whnfIO $ parseRequestLine1 requestLine2
43− , bench "parseRequestLine0" $ parseRequestLine0 requestLine2
48+ , bench "parseRequestLine0" $ whnfIO $ parseRequestLine0 requestLine2
44 ]
49 ]
45 ]
50 ]
46
51
@@ -63,15 +68,15 @@ parseRequestLine3 :: ByteString
63 ,H.HttpVersion)
68 ,H.HttpVersion)
64parseRequestLine3 requestLine = ret
69parseRequestLine3 requestLine = ret
65 where
70 where
66− (!method,!rest) = S.breakByte 32 requestLine -- ' '
71+ (!method,!rest) = S.break (== 32) requestLine -- ' '
67 (!pathQuery,!httpVer')
72 (!pathQuery,!httpVer')
68 | rest == "" = throw badmsg
73 | rest == "" = throw badmsg
69− | otherwise = S.breakByte 32 (S.drop 1 rest) -- ' '
74+ | otherwise = S.break (== 32) (S.drop 1 rest) -- ' '
70− (!path,!query) = S.breakByte 63 pathQuery -- '?'
75+ (!path,!query) = S.break (== 63) pathQuery -- '?'
71 !httpVer = S.drop 1 httpVer'
76 !httpVer = S.drop 1 httpVer'
72 (!http,!ver)
77 (!http,!ver)
73 | httpVer == "" = throw badmsg
78 | httpVer == "" = throw badmsg
74− | otherwise = S.breakByte 47 httpVer -- '/'
79+ | otherwise = S.break (== 47) httpVer -- '/'
75 !hv | http /= "HTTP" = throw NonHttp
80 !hv | http /= "HTTP" = throw NonHttp
76 | ver == "/1.1" = H.http11
81 | ver == "/1.1" = H.http11
77 | otherwise = H.http10
82 | otherwise = H.http10
@@ -169,13 +174,13 @@ parseRequestLine1 :: ByteString
169 ,ByteString -- Query
174 ,ByteString -- Query
170 ,H.HttpVersion)
175 ,H.HttpVersion)
171parseRequestLine1 requestLine = do
176parseRequestLine1 requestLine = do
172− let (!method,!rest) = S.breakByte 32 requestLine -- ' '
177+ let (!method,!rest) = S.break (== 32) requestLine -- ' '
173− (!pathQuery,!httpVer') = S.breakByte 32 (S.drop 1 rest) -- ' '
178+ (!pathQuery,!httpVer') = S.break (== 32) (S.drop 1 rest) -- ' '
174 !httpVer = S.drop 1 httpVer'
179 !httpVer = S.drop 1 httpVer'
175 when (rest == "" || httpVer == "") $
180 when (rest == "" || httpVer == "") $
176 throwIO $ BadFirstLine $ B.unpack requestLine
181 throwIO $ BadFirstLine $ B.unpack requestLine
177− let (!path,!query) = S.breakByte 63 pathQuery -- '?'
182+ let (!path,!query) = S.break (== 63) pathQuery -- '?'
178− (!http,!ver) = S.breakByte 47 httpVer -- '/'
183+ (!http,!ver) = S.break (== 47) httpVer -- '/'
179 when (http /= "HTTP") $ throwIO NonHttp
184 when (http /= "HTTP") $ throwIO NonHttp
180 let !hv | ver == "/1.1" = H.http11
185 let !hv | ver == "/1.1" = H.http11
181 | otherwise = H.http10
186 | otherwise = H.http10
@@ -205,7 +210,7 @@ parseRequestLine0 s =
205 !http' = S.concat http''
210 !http' = S.concat http''
206 (!hfirst, !hsecond) = S.splitAt 5 http'
211 (!hfirst, !hsecond) = S.splitAt 5 http'
207 if hfirst == "HTTP/"
212 if hfirst == "HTTP/"
208− then let (!rpath, !qstring) = S.breakByte 63 query -- '?'
213+ then let (!rpath, !qstring) = S.break (== 63) query -- '?'
209 !hv =
214 !hv =
210 case hsecond of
215 case hsecond of
211 "1.1" -> H.http11
216 "1.1" -> H.http11
warp/test/BufferPoolSpec.hsnew file+47
@@ -0,0 +1,47 @@
1+module BufferPoolSpec where
2+
3+import qualified Data.ByteString as B
4+import qualified Data.ByteString.Internal as B (ByteString(PS))
5+import Foreign.ForeignPtr (withForeignPtr)
6+import Foreign.Marshal.Utils (copyBytes)
7+import Foreign.Ptr (plusPtr)
8+
9+import Test.Hspec (Spec, hspec, shouldBe, describe, it)
10+
11+import Network.Wai.Handler.Warp.Buffer
12+ ( bufferSize
13+ , newBufferPool
14+ , withBufferPool
15+ )
16+import Network.Wai.Handler.Warp.Types (Buffer, BufSize)
17+
18+main :: IO ()
19+main = hspec spec
20+
21+-- Two ByteStrings each big enough to fill a 'bufferSize' buffer (16K).
22+wantData, otherData :: B.ByteString
23+wantData = B.replicate bufferSize 0xac
24+otherData = B.replicate bufferSize 0x77
25+
26+spec :: Spec
27+spec = describe "withBufferPool" $ do
28+ it "does not clobber buffers" $ do
29+ pool <- newBufferPool
30+ -- 'pool' contains B.empty; prime it to contain a real buffer.
31+ _ <- withBufferPool pool $ const $ return 0
32+ -- 'pool' contains a 16K buffer; fill it with \xac and keep the result.
33+ got <- withBufferPool pool $ blitBuffer wantData
34+ got `shouldBe` wantData
35+ -- 'pool' should now be empty and reallocate, rather than clobber the
36+ -- previous buffer.
37+ _ <- withBufferPool pool $ blitBuffer otherData
38+ got `shouldBe` wantData
39+
40+-- Fill the Buffer with the contents of the ByteString and return the number of
41+-- bytes written. To be used with 'withBufferPool'.
42+blitBuffer :: B.ByteString -> (Buffer, BufSize) -> IO Int
43+blitBuffer (B.PS fp off len) (dst, len') = withForeignPtr fp $ \ptr -> do
44+ let src = ptr `plusPtr` off
45+ n = min len len'
46+ copyBytes dst src n
47+ return n
warp/test/ConduitSpec.hs+47−12
@@ -2,10 +2,10 @@
2module ConduitSpec (main, spec) where
2module ConduitSpec (main, spec) where
3
3
4import Network.Wai.Handler.Warp.Conduit
4import Network.Wai.Handler.Warp.Conduit
5−import Data.Conduit
5+import Network.Wai.Handler.Warp.Types
6−import qualified Data.Conduit.List as CL
6+import Control.Monad (replicateM)
7import Test.Hspec
7import Test.Hspec
8−import qualified Data.IORef as I
8+import Data.IORef as I
9import qualified Data.ByteString as S
9import qualified Data.ByteString as S
10
10
11main :: IO ()
11main :: IO ()
@@ -14,19 +14,54 @@ main = hspec spec
14spec :: Spec
14spec :: Spec
15spec = describe "conduit" $ do
15spec = describe "conduit" $ do
16 it "IsolatedBSSource" $ do
16 it "IsolatedBSSource" $ do
17− (rsrc, ()) <- mapM_ yield (map S.singleton [1..50]) $$+ return ()
17+ ref <- newIORef $ map S.singleton [1..50]
18− ibs <- fmap IsolatedBSSource $ I.newIORef (40, rsrc)
18+ src <- mkSource $ do
19− x <- ibsIsolate ibs $$ CL.take 20
19+ x <- readIORef ref
20+ case x of
21+ [] -> return S.empty
22+ y:z -> do
23+ writeIORef ref z
24+ return y
25+ isrc <- mkISource src 40
26+ x <- replicateM 20 $ readISource isrc
20 S.concat x `shouldBe` S.pack [1..20]
27 S.concat x `shouldBe` S.pack [1..20]
21
28
22− y <- ibsIsolate ibs $$ CL.consume
29+ y <- replicateM 40 $ readISource isrc
23 S.concat y `shouldBe` S.pack [21..40]
30 S.concat y `shouldBe` S.pack [21..40]
24
31
25− rsrc' <- ibsDone ibs
32+ z <- replicateM 40 $ readSource src
26− z <- rsrc' $$+- CL.consume
27 S.concat z `shouldBe` S.pack [41..50]
33 S.concat z `shouldBe` S.pack [41..50]
28 it "chunkedSource" $ do
34 it "chunkedSource" $ do
29− (rsrc, ()) <- yield "5\r\n12345\r\n3\r\n678\r\n0\r\nBLAH" $$+ return ()
35+ ref <- newIORef $ "5\r\n12345\r\n3\r\n678\r\n0\r\n\r\nBLAH"
30− ref <- I.newIORef (rsrc, NeedLen)
36+ src <- mkSource $ do
31− x <- chunkedSource ref $$ CL.consume
37+ x <- readIORef ref
38+ writeIORef ref S.empty
39+ return x
40+ csrc <- mkCSource src
41+
42+ x <- replicateM 15 $ readCSource csrc
32 S.concat x `shouldBe` "12345678"
43 S.concat x `shouldBe` "12345678"
44+
45+ y <- replicateM 15 $ readSource src
46+ S.concat y `shouldBe` "BLAH"
47+ it "chunk boundaries" $ do
48+ ref <- newIORef
49+ [ "5\r\n"
50+ , "12345\r\n3\r"
51+ , "\n678\r\n0\r\n"
52+ , "\r\nBLAH"
53+ ]
54+ src <- mkSource $ do
55+ x <- readIORef ref
56+ case x of
57+ [] -> return S.empty
58+ y:z -> do
59+ writeIORef ref z
60+ return y
61+ csrc <- mkCSource src
62+
63+ x <- replicateM 15 $ readCSource csrc
64+ S.concat x `shouldBe` "12345678"
65+
66+ y <- replicateM 15 $ readSource src
67+ S.concat y `shouldBe` "BLAH"
warp/test/ExceptionSpec.hs+30−45
@@ -1,81 +1,66 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, CPP #-}
2
2
3module ExceptionSpec (main, spec) where
3module ExceptionSpec (main, spec) where
4
4
5+#if __GLASGOW_HASKELL__ < 709
5import Control.Applicative
6import Control.Applicative
7+#endif
6import Control.Monad
8import Control.Monad
7−import Control.Concurrent (forkIO, threadDelay)
8−import Network.HTTP
9−import Network.Stream
10import Network.HTTP.Types hiding (Header)
9import Network.HTTP.Types hiding (Header)
11−import Network.Wai hiding (Response)
10+import Network.Wai hiding (Response, responseStatus)
12import Network.Wai.Internal (Request(..))
11import Network.Wai.Internal (Request(..))
13import Network.Wai.Handler.Warp
12import Network.Wai.Handler.Warp
14−import System.IO.Unsafe (unsafePerformIO)
15import Test.Hspec
13import Test.Hspec
16import Control.Exception
14import Control.Exception
17import qualified Data.Streaming.Network as N
15import qualified Data.Streaming.Network as N
18import Control.Concurrent.Async (withAsync)
16import Control.Concurrent.Async (withAsync)
19−import Network.Socket (sClose)
17+import Network.Socket (close)
18+
19+import HTTP
20
20
21main :: IO ()
21main :: IO ()
22main = hspec spec
22main = hspec spec
23
23
24withTestServer :: (Int -> IO a) -> IO a
24withTestServer :: (Int -> IO a) -> IO a
25withTestServer inner = bracket
25withTestServer inner = bracket
26− (N.bindRandomPortTCP "*4")
26+ (N.bindRandomPortTCP "127.0.0.1")
27− (sClose . snd)
27+ (close . snd)
28− $ \(port, lsocket) -> do
28+ $ \(prt, lsocket) -> do
29 withAsync (runSettingsSocket defaultSettings lsocket testApp)
29 withAsync (runSettingsSocket defaultSettings lsocket testApp)
30− $ \_ -> inner port
30+ $ \_ -> inner prt
31
31
32testApp :: Application
32testApp :: Application
33−testApp (Network.Wai.Internal.Request {pathInfo = [x]})
33+testApp (Network.Wai.Internal.Request {pathInfo = [x]}) f
34 | x == "statusError" =
34 | x == "statusError" =
35− return $ responseLBS undefined [] "foo"
35+ f $ responseLBS undefined [] "foo"
36 | x == "headersError" =
36 | x == "headersError" =
37− return $ responseLBS ok200 undefined "foo"
37+ f $ responseLBS ok200 undefined "foo"
38 | x == "headerError" =
38 | x == "headerError" =
39− return $ responseLBS ok200 [undefined] "foo"
39+ f $ responseLBS ok200 [undefined] "foo"
40 | x == "bodyError" =
40 | x == "bodyError" =
41− return $ responseLBS ok200 [] undefined
41+ f $ responseLBS ok200 [] undefined
42 | x == "ioException" = do
42 | x == "ioException" = do
43 void $ fail "ioException"
43 void $ fail "ioException"
44− return $ responseLBS ok200 [] "foo"
44+ f $ responseLBS ok200 [] "foo"
45−testApp _ =
45+testApp _ f =
46− return $ responseLBS ok200 [] "foo"
46+ f $ responseLBS ok200 [] "foo"
47
47
48spec :: Spec
48spec :: Spec
49spec = describe "responds even if there is an exception" $ do
49spec = describe "responds even if there is an exception" $ do
50 {- Disabling these tests. We can consider forcing evaluation in Warp.
50 {- Disabling these tests. We can consider forcing evaluation in Warp.
51 it "statusError" $ do
51 it "statusError" $ do
52− sc <- rspCode <$> sendGET "http://localhost:2345/statusError"
52+ sc <- responseStatus <$> sendGET "http://127.0.0.1:2345/statusError"
53− sc `shouldBe` (5,0,0)
53+ sc `shouldBe` internalServerError500
54 it "headersError" $ do
54 it "headersError" $ do
55− sc <- rspCode <$> sendGET "http://localhost:2345/headersError"
55+ sc <- responseStatus <$> sendGET "http://127.0.0.1:2345/headersError"
56− sc `shouldBe` (5,0,0)
56+ sc `shouldBe` internalServerError500
57 it "headerError" $ do
57 it "headerError" $ do
58− sc <- rspCode <$> sendGET "http://localhost:2345/headerError"
58+ sc <- responseStatus <$> sendGET "http://127.0.0.1:2345/headerError"
59− sc `shouldBe` (5,0,0)
59+ sc `shouldBe` internalServerError500
60 it "bodyError" $ do
60 it "bodyError" $ do
61− sc <- rspCode <$> sendGET "http://localhost:2345/bodyError"
61+ sc <- responseStatus <$> sendGET "http://127.0.0.1:2345/bodyError"
62− sc `shouldBe` (5,0,0)
62+ sc `shouldBe` internalServerError500
63 -}
63 -}
64− it "ioException" $ withTestServer $ \port -> do
64+ it "ioException" $ withTestServer $ \prt -> do
65− sc <- rspCode <$> sendGET (concat $ ["http://localhost:", show port, "/ioException"])
65+ sc <- responseStatus <$> sendGET ("http://127.0.0.1:" ++ show prt ++ "/ioException")
66− sc `shouldBe` (5,0,0)
66+ sc `shouldBe` internalServerError500
67−
68−----------------------------------------------------------------
69−
70−sendGET :: String -> IO (Response String)
71−sendGET url = sendGETwH url []
72−
73−sendGETwH :: String -> [Header] -> IO (Response String)
74−sendGETwH url hdr = unResult $ simpleHTTP $ (getRequest url) { rqHeaders = hdr }
75−
76−unResult :: IO (Result (Response String)) -> IO (Response String)
77−unResult action = do
78− res <- action
79− case res of
80− Right rsp -> return rsp
81− Left _ -> error "Connection error"
warp/test/FdCacheSpec.hs+3−3
@@ -3,7 +3,7 @@
3module FdCacheSpec where
3module FdCacheSpec where
4
4
5import Test.Hspec
5import Test.Hspec
6−#ifdef SENDFILEFD
6+#ifndef WINDOWS
7import Data.IORef
7import Data.IORef
8import Network.Wai.Handler.Warp.FdCache
8import Network.Wai.Handler.Warp.FdCache
9import System.Posix.IO (fdRead)
9import System.Posix.IO (fdRead)
@@ -16,8 +16,8 @@ spec :: Spec
16spec = describe "withFdCache" $ do
16spec = describe "withFdCache" $ do
17 it "clean up Fd" $ do
17 it "clean up Fd" $ do
18 ref <- newIORef (Fd (-1))
18 ref <- newIORef (Fd (-1))
19− withFdCache 30000000 $ \(Just mfc) -> do
19+ withFdCache 30000000 $ \getFd -> do
20− (fd,_) <- getFd mfc "warp.cabal"
20+ (Just fd,_) <- getFd "warp.cabal"
21 writeIORef ref fd
21 writeIORef ref fd
22 nfd <- readIORef ref
22 nfd <- readIORef ref
23 fdRead nfd 1 `shouldThrow` anyIOException
23 fdRead nfd 1 `shouldThrow` anyIOException
warp/test/FileSpec.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module FileSpec (main, spec) where
4+
5+import Network.HTTP.Types
6+import Network.Wai.Handler.Warp.File
7+import Network.Wai.Handler.Warp.FileInfoCache
8+import Network.Wai.Handler.Warp.Header
9+
10+import Test.Hspec
11+
12+main :: IO ()
13+main = hspec spec
14+
15+testFileRange :: String
16+ -> RequestHeaders -> FilePath
17+ -> RspFileInfo
18+ -> Spec
19+testFileRange desc reqhs file ans = it desc $ do
20+ finfo <- getInfo file
21+ let WithBody s hs off len = ans
22+ hs' = ("Last-Modified",fileInfoDate finfo) : hs
23+ ans' = WithBody s hs' off len
24+ conditionalRequest finfo [] (indexResponseHeader hs) (indexRequestHeader reqhs) `shouldBe` ans'
25+
26+spec :: Spec
27+spec = do
28+ describe "conditionalRequest" $ do
29+ testFileRange
30+ "gets a file size from file system"
31+ [] "attic/hex"
32+ $ WithBody ok200 [("Content-Length","16"),("Accept-Ranges","bytes")] 0 16
33+ testFileRange
34+ "gets a file size from file system and handles Range and returns Partical Content"
35+ [("Range","bytes=2-14")] "attic/hex"
36+ $ WithBody status206 [("Content-Range","bytes 2-14/16"),("Content-Length","13"),("Accept-Ranges","bytes")] 2 13
37+ testFileRange
38+ "truncates end point of range to file size"
39+ [("Range","bytes=10-20")] "attic/hex"
40+ $ WithBody status206 [("Content-Range","bytes 10-15/16"),("Content-Length","6"),("Accept-Ranges","bytes")] 10 6
41+ testFileRange
42+ "gets a file size from file system and handles Range and returns OK if Range means the entire"
43+ [("Range:","bytes=0-15")] "attic/hex"
44+ $ WithBody status200 [("Content-Length","16"),("Accept-Ranges","bytes")] 0 16
45+
warp/test/HTTP.hsnew file+41
@@ -0,0 +1,41 @@
1+module HTTP (
2+ sendGET
3+ , sendGETwH
4+ , sendHEAD
5+ , sendHEADwH
6+ , responseBody
7+ , responseStatus
8+ , responseHeaders
9+ , getHeaderValue
10+ , HeaderName
11+ ) where
12+
13+import Network.HTTP.Client
14+import Network.HTTP.Types
15+import Data.ByteString
16+import qualified Data.ByteString.Lazy as BL
17+
18+sendGET :: String -> IO (Response BL.ByteString)
19+sendGET url = sendGETwH url []
20+
21+sendGETwH :: String -> [Header] -> IO (Response BL.ByteString)
22+sendGETwH url hdr = do
23+ manager <- newManager defaultManagerSettings
24+ request <- parseRequest url
25+ let request' = request { requestHeaders = hdr }
26+ response <- httpLbs request' manager
27+ return response
28+
29+sendHEAD :: String -> IO (Response BL.ByteString)
30+sendHEAD url = sendHEADwH url []
31+
32+sendHEADwH :: String -> [Header] -> IO (Response BL.ByteString)
33+sendHEADwH url hdr = do
34+ manager <- newManager defaultManagerSettings
35+ request <- parseRequest url
36+ let request' = request { requestHeaders = hdr, method = methodHead }
37+ response <- httpLbs request' manager
38+ return response
39+
40+getHeaderValue :: HeaderName -> [Header] -> Maybe ByteString
41+getHeaderValue = lookup
warp/test/MultiMapSpec.hsdeleted−30
@@ -1,30 +0,0 @@
1−module MultiMapSpec where
2−
3−import Network.Wai.Handler.Warp.MultiMap
4−import Test.Hspec
5−import Test.QuickCheck (property)
6−
7−type Alist = [(Int,Char)]
8−
9−spec :: Spec
10−spec = do
11− describe "fromList" $ do
12− it "generates a valid tree" $ property $ \xs ->
13− valid $ fromList (xs :: Alist)
14− describe "toSortedList" $ do
15− it "generated a sorted list" $ property $ \xs ->
16− ordered $ toSortedList $ fromList (xs :: Alist)
17− describe "search" $ do
18− it "acts as the list model" $ property $ \x xs ->
19− search x (fromList xs) == lookup x (xs :: Alist)
20− describe "fromSortedList" $ do
21− it "generates a valid tree" $ property $ \xs ->
22− valid . fromSortedList . toSortedList . fromList $ (xs :: Alist)
23− it "maintains the tree with toSortedList" $ property $ \xs ->
24− let t1 = fromList (xs :: Alist)
25− t2 = fromSortedList $ toSortedList t1
26− in t1 == t2
27−
28−ordered :: Ord a => [(a, b)] -> Bool
29−ordered (x:y:xys) = fst x <= fst y && ordered (y:xys)
30−ordered _ = True
warp/test/RequestSpec.hs+95−18
@@ -4,28 +4,30 @@
4
4
5module RequestSpec (main, spec) where
5module RequestSpec (main, spec) where
6
6
7−import Data.Conduit
7+import Network.Wai.Handler.Warp.File (parseByteRanges)
8−import qualified Data.Conduit.Binary as CB
9−import Data.Conduit.List (sourceList)
10import Network.Wai.Handler.Warp.Request
8import Network.Wai.Handler.Warp.Request
11−import Network.Wai.Handler.Warp.RequestHeader (parseByteRanges)
12import Network.Wai.Handler.Warp.Types
9import Network.Wai.Handler.Warp.Types
10+import Network.Wai.Handler.Warp.Settings (settingsMaxTotalHeaderLength, defaultSettings)
13import Test.Hspec
11import Test.Hspec
14import Test.Hspec.QuickCheck
12import Test.Hspec.QuickCheck
13+import qualified Data.ByteString as S
15import qualified Data.ByteString.Char8 as S8
14import qualified Data.ByteString.Char8 as S8
16−import qualified Data.ByteString.Lazy as L
17import qualified Network.HTTP.Types.Header as HH
15import qualified Network.HTTP.Types.Header as HH
16+import Data.IORef
18
17
19main :: IO ()
18main :: IO ()
20main = hspec spec
19main = hspec spec
21
20
21+defaultMaxTotalHeaderLength :: Int
22+defaultMaxTotalHeaderLength = settingsMaxTotalHeaderLength defaultSettings
23+
22spec :: Spec
24spec :: Spec
23spec = do
25spec = do
24 describe "headerLines" $ do
26 describe "headerLines" $ do
25 it "takes until blank" $
27 it "takes until blank" $
26− blankSafe >>= (`shouldBe` (Nothing, ["foo", "bar", "baz"]))
28+ blankSafe >>= (`shouldBe` ("", ["foo", "bar", "baz"]))
27 it "ignored leading whitespace in bodies" $
29 it "ignored leading whitespace in bodies" $
28− whiteSafe >>= (`shouldBe` (Just " hi there", ["foo", "bar", "baz"]))
30+ whiteSafe >>= (`shouldBe` (" hi there", ["foo", "bar", "baz"]))
29 it "throws OverLargeHeader when too many" $
31 it "throws OverLargeHeader when too many" $
30 tooMany `shouldThrow` overLargeHeader
32 tooMany `shouldThrow` overLargeHeader
31 it "throws OverLargeHeader when too large" $
33 it "throws OverLargeHeader when too large" $
@@ -35,8 +37,9 @@ spec = do
35 [ "GET / HTTP/1.1\r\nConnection: Close\r"
37 [ "GET / HTTP/1.1\r\nConnection: Close\r"
36 , "\n\r\n"
38 , "\n\r\n"
37 ]
39 ]
38− (mleftover, actual) <- mapM_ yield chunks $$ headerLines
40+ (actual, src) <- headerLinesList' chunks
39− mleftover `shouldBe` Nothing
41+ leftover <- readLeftoverSource src
42+ leftover `shouldBe` S.empty
40 actual `shouldBe` ["GET / HTTP/1.1", "Connection: Close"]
43 actual `shouldBe` ["GET / HTTP/1.1", "Connection: Close"]
41 prop "random chunking" $ \breaks extraS -> do
44 prop "random chunking" $ \breaks extraS -> do
42 let bsFull = "GET / HTTP/1.1\r\nConnection: Close\r\n\r\n" `S8.append` extra
45 let bsFull = "GET / HTTP/1.1\r\nConnection: Close\r\n\r\n" `S8.append` extra
@@ -47,11 +50,9 @@ spec = do
47 bs1 : loop xs bs2
50 bs1 : loop xs bs2
48 where
51 where
49 (bs1, bs2) = S8.splitAt ((x `mod` 10) + 1) bs
52 (bs1, bs2) = S8.splitAt ((x `mod` 10) + 1) bs
50− (leftover, actual) <- mapM_ yield chunks $$ do
53+ (actual, src) <- headerLinesList' chunks
51− (_, actual) <- headerLines
52− x' <- CB.take (length extraS)
54+ leftover <- consumeLen (length extraS) src
53− let x = S8.concat $ L.toChunks x'
55+
54− return (x, actual)
55 actual `shouldBe` ["GET / HTTP/1.1", "Connection: Close"]
56 actual `shouldBe` ["GET / HTTP/1.1", "Connection: Close"]
56 leftover `shouldBe` extra
57 leftover `shouldBe` extra
57 describe "parseByteRanges" $ do
58 describe "parseByteRanges" $ do
@@ -62,11 +63,87 @@ spec = do
62 test "bytes=9500-" $ Just [HH.ByteRangeFrom 9500]
63 test "bytes=9500-" $ Just [HH.ByteRangeFrom 9500]
63 test "foobytes=9500-" Nothing
64 test "foobytes=9500-" Nothing
64 test "bytes=0-0,-1" $ Just [HH.ByteRangeFromTo 0 0, HH.ByteRangeSuffix 1]
65 test "bytes=0-0,-1" $ Just [HH.ByteRangeFromTo 0 0, HH.ByteRangeSuffix 1]
66+
67+ describe "headerLines" $ do
68+ it "can handle a nomarl case" $ do
69+ src <- mkSourceFunc ["Status: 200\r\nContent-Type: text/plain\r\n\r\n"] >>= mkSource
70+ x <- headerLines defaultMaxTotalHeaderLength True src
71+ x `shouldBe` ["Status: 200", "Content-Type: text/plain"]
72+
73+ it "can handle a nasty case (1)" $ do
74+ src <- mkSourceFunc ["Status: 200", "\r\nContent-Type: text/plain", "\r\n\r\n"] >>= mkSource
75+ x <- headerLines defaultMaxTotalHeaderLength True src
76+ x `shouldBe` ["Status: 200", "Content-Type: text/plain"]
77+
78+ it "can handle a nasty case (1)" $ do
79+ src <- mkSourceFunc ["Status: 200", "\r", "\nContent-Type: text/plain", "\r", "\n\r\n"] >>= mkSource
80+ x <- headerLines defaultMaxTotalHeaderLength True src
81+ x `shouldBe` ["Status: 200", "Content-Type: text/plain"]
82+
83+ it "can handle a nasty case (1)" $ do
84+ src <- mkSourceFunc ["Status: 200", "\r", "\n", "Content-Type: text/plain", "\r", "\n", "\r", "\n"] >>= mkSource
85+ x <- headerLines defaultMaxTotalHeaderLength True src
86+ x `shouldBe` ["Status: 200", "Content-Type: text/plain"]
87+
88+ it "can handle an illegal case (1)" $ do
89+ src <- mkSourceFunc ["\nStatus:", "\n 200", "\nContent-Type: text/plain", "\r\n\r\n"] >>= mkSource
90+ x <- headerLines defaultMaxTotalHeaderLength True src
91+ x `shouldBe` []
92+ y <- headerLines defaultMaxTotalHeaderLength True src
93+ y `shouldBe` ["Status: 200", "Content-Type: text/plain"]
94+
65 where
95 where
66− blankSafe = (sourceList ["f", "oo\n", "bar\nbaz\n\r\n"]) $$ headerLines
96+ blankSafe = headerLinesList ["f", "oo\n", "bar\nbaz\n\r\n"]
67− whiteSafe = (sourceList ["foo\r\nbar\r\nbaz\r\n\r\n hi there"]) $$ headerLines
97+ whiteSafe = headerLinesList ["foo\r\nbar\r\nbaz\r\n\r\n hi there"]
68− tooMany = (sourceList $ repeat "f\n") $$ headerLines
98+ tooMany = headerLinesList $ repeat "f\n"
69− tooLarge = (sourceList $ repeat "f") $$ headerLines
99+ tooLarge = headerLinesList $ repeat "f"
100+
101+headerLinesList :: [S8.ByteString] -> IO (S8.ByteString, [S8.ByteString])
102+headerLinesList orig = do
103+ (res, src) <- headerLinesList' orig
104+ leftover <- readLeftoverSource src
105+ return (leftover, res)
106+
107+headerLinesList' :: [S8.ByteString] -> IO ([S8.ByteString], Source)
108+headerLinesList' orig = do
109+ ref <- newIORef orig
110+ let src = do
111+ x <- readIORef ref
112+ case x of
113+ [] -> return S.empty
114+ y:z -> do
115+ writeIORef ref z
116+ return y
117+ src' <- mkSource src
118+ res <- headerLines defaultMaxTotalHeaderLength True src'
119+ return (res, src')
120+
121+consumeLen :: Int -> Source -> IO S8.ByteString
122+consumeLen len0 src =
123+ loop id len0
124+ where
125+ loop front len
126+ | len <= 0 = return $ S.concat $ front []
127+ | otherwise = do
128+ bs <- readSource src
129+ if S.null bs
130+ then loop front 0
131+ else do
132+ let (x, _) = S.splitAt len bs
133+ loop (front . (x:)) (len - S.length x)
70
134
71overLargeHeader :: Selector InvalidRequest
135overLargeHeader :: Selector InvalidRequest
72overLargeHeader e = e == OverLargeHeader
136overLargeHeader e = e == OverLargeHeader
137+
138+mkSourceFunc :: [S8.ByteString] -> IO (IO S8.ByteString)
139+mkSourceFunc bss = do
140+ ref <- newIORef bss
141+ return $ reader ref
142+ where
143+ reader ref = do
144+ xss <- readIORef ref
145+ case xss of
146+ [] -> return S.empty
147+ (x:xs) -> do
148+ writeIORef ref xs
149+ return x
warp/test/ResponseHeaderSpec.hs+25−5
@@ -5,24 +5,44 @@ module ResponseHeaderSpec (main, spec) where
5import Data.ByteString
5import Data.ByteString
6import qualified Network.HTTP.Types as H
6import qualified Network.HTTP.Types as H
7import Network.Wai.Handler.Warp.ResponseHeader
7import Network.Wai.Handler.Warp.ResponseHeader
8+import Network.Wai.Handler.Warp.Response
9+import Network.Wai.Handler.Warp.Header
8import Test.Hspec
10import Test.Hspec
9
11
10main :: IO ()
12main :: IO ()
11main = hspec spec
13main = hspec spec
12
14
13spec :: Spec
15spec :: Spec
16+spec = do
14−spec = describe "composeHeader" $ do
17+ describe "composeHeader" $ do
15− it "composes a HTTP header" $
18+ it "composes a HTTP header" $
16− composeHeader H.http11 H.ok200 headers `shouldReturn` composedHeader
19+ composeHeader H.http11 H.ok200 headers `shouldReturn` composedHeader
20+ describe "addServer" $ do
21+ it "adds Server if not exist" $ do
22+ let hdrs = []
23+ rspidxhdr = indexResponseHeader hdrs
24+ addServer "MyServer" rspidxhdr hdrs `shouldBe` [("Server","MyServer")]
25+ it "does not add Server if exists" $ do
26+ let hdrs = [("Server","MyServer")]
27+ rspidxhdr = indexResponseHeader hdrs
28+ addServer "MyServer2" rspidxhdr hdrs `shouldBe` hdrs
29+ it "does not add Server if empty" $ do
30+ let hdrs = []
31+ rspidxhdr = indexResponseHeader hdrs
32+ addServer "" rspidxhdr hdrs `shouldBe` hdrs
33+ it "deletes Server " $ do
34+ let hdrs = [("Server","MyServer")]
35+ rspidxhdr = indexResponseHeader hdrs
36+ addServer "" rspidxhdr hdrs `shouldBe` []
17
37
18headers :: H.ResponseHeaders
38headers :: H.ResponseHeaders
19headers = [
39headers = [
20 ("Date", "Mon, 13 Aug 2012 04:22:55 GMT")
40 ("Date", "Mon, 13 Aug 2012 04:22:55 GMT")
21− , ("Content-Lenght", "151")
41+ , ("Content-Length", "151")
22 , ("Server", "Mighttpd/2.5.8")
42 , ("Server", "Mighttpd/2.5.8")
23 , ("Last-Modified", "Fri, 22 Jun 2012 01:18:08 GMT")
43 , ("Last-Modified", "Fri, 22 Jun 2012 01:18:08 GMT")
24 , ("Content-Type", "text/html")
44 , ("Content-Type", "text/html")
25 ]
45 ]
26
46
27composedHeader :: ByteString
47composedHeader :: ByteString
28−composedHeader = "HTTP/1.1 200 OK\r\nDate: Mon, 13 Aug 2012 04:22:55 GMT\r\nContent-Lenght: 151\r\nServer: Mighttpd/2.5.8\r\nLast-Modified: Fri, 22 Jun 2012 01:18:08 GMT\r\nContent-Type: text/html\r\n\r\n"
48+composedHeader = "HTTP/1.1 200 OK\r\nDate: Mon, 13 Aug 2012 04:22:55 GMT\r\nContent-Length: 151\r\nServer: Mighttpd/2.5.8\r\nLast-Modified: Fri, 22 Jun 2012 01:18:08 GMT\r\nContent-Type: text/html\r\n\r\n"
warp/test/ResponseSpec.hs+53−70
@@ -1,17 +1,16 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2+
2module ResponseSpec (main, spec) where
3module ResponseSpec (main, spec) where
3
4
4import Control.Concurrent (threadDelay)
5import Control.Concurrent (threadDelay)
5import qualified Data.ByteString as S
6import qualified Data.ByteString as S
6import qualified Data.ByteString.Char8 as S8
7import qualified Data.ByteString.Char8 as S8
7import Data.Maybe (mapMaybe)
8import Data.Maybe (mapMaybe)
8−import Network (connectTo, PortID (PortNumber))
9import Network.HTTP.Types
9import Network.HTTP.Types
10−import Network.Wai
10+import Network.Wai hiding (responseHeaders)
11import Network.Wai.Handler.Warp
11import Network.Wai.Handler.Warp
12import Network.Wai.Handler.Warp.Response
12import Network.Wai.Handler.Warp.Response
13−import RunSpec (withApp)
13+import RunSpec (withApp, msWrite, msRead, withMySocket)
14−import System.IO (hClose, hFlush)
15import Test.Hspec
14import Test.Hspec
16
15
17main :: IO ()
16main :: IO ()
@@ -19,24 +18,21 @@ main = hspec spec
19
18
20testRange :: S.ByteString -- ^ range value
19testRange :: S.ByteString -- ^ range value
21 -> String -- ^ expected output
20 -> String -- ^ expected output
22− -> String -- ^ expected content-range value
21+ -> Maybe String -- ^ expected content-range value
23 -> Spec
22 -> Spec
24−testRange range out crange = it title $ withApp defaultSettings app $ \port -> do
23+testRange range out crange = it title $ withApp defaultSettings app $ withMySocket $ \ms -> do
25− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
26− S.hPutStr handle "GET / HTTP/1.0\r\n"
24+ msWrite ms "GET / HTTP/1.0\r\n"
27− S.hPutStr handle "Range: bytes="
25+ msWrite ms "Range: bytes="
28− S.hPutStr handle range
26+ msWrite ms range
29− S.hPutStr handle "\r\n\r\n"
27+ msWrite ms "\r\n\r\n"
30− hFlush handle
31 threadDelay 10000
28 threadDelay 10000
32− bss <- fmap (lines . filter (/= '\r') . S8.unpack) $ S.hGetSome handle 1024
29+ bss <- fmap (lines . filter (/= '\r') . S8.unpack) $ msRead ms 1024
33− hClose handle
30+ last bss `shouldBe` out
34− out `shouldBe` last bss
35 let hs = mapMaybe toHeader bss
31 let hs = mapMaybe toHeader bss
36− lookup "Content-Range" hs `shouldBe` Just ("bytes " ++ crange)
32+ lookup "Content-Range" hs `shouldBe` fmap ("bytes " ++) crange
37 lookup "Content-Length" hs `shouldBe` Just (show $ length $ last bss)
33 lookup "Content-Length" hs `shouldBe` Just (show $ length $ last bss)
38 where
34 where
39− app _ = return $ responseFile status200 [] "attic/hex" Nothing
35+ app _ = ($ responseFile status200 [] "attic/hex" Nothing)
40 title = show (range, out, crange)
36 title = show (range, out, crange)
41 toHeader s =
37 toHeader s =
42 case break (== ':') s of
38 case break (== ':') s of
@@ -48,19 +44,16 @@ testPartial :: Integer -- ^ file size
48 -> Integer -- ^ byte count
44 -> Integer -- ^ byte count
49 -> String -- ^ expected output
45 -> String -- ^ expected output
50 -> Spec
46 -> Spec
51−testPartial size offset count out = it title $ withApp defaultSettings app $ \port -> do
47+testPartial size offset count out = it title $ withApp defaultSettings app $ withMySocket $ \ms -> do
52− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
53− S.hPutStr handle "GET / HTTP/1.0\r\n\r\n"
48+ msWrite ms "GET / HTTP/1.0\r\n\r\n"
54− hFlush handle
55 threadDelay 10000
49 threadDelay 10000
56− bss <- fmap (lines . filter (/= '\r') . S8.unpack) $ S.hGetSome handle 1024
50+ bss <- fmap (lines . filter (/= '\r') . S8.unpack) $ msRead ms 1024
57− hClose handle
58 out `shouldBe` last bss
51 out `shouldBe` last bss
59 let hs = mapMaybe toHeader bss
52 let hs = mapMaybe toHeader bss
60 lookup "Content-Length" hs `shouldBe` Just (show $ length $ last bss)
53 lookup "Content-Length" hs `shouldBe` Just (show $ length $ last bss)
61 lookup "Content-Range" hs `shouldBe` Just range
54 lookup "Content-Range" hs `shouldBe` Just range
62 where
55 where
63− app _ = return $ responseFile status200 [] "attic/hex" $ Just $ FilePart offset count size
56+ app _ = ($ responseFile status200 [] "attic/hex" $ Just $ FilePart offset count size)
64 title = show (offset, count, out)
57 title = show (offset, count, out)
65 toHeader s =
58 toHeader s =
66 case break (== ':') s of
59 case break (== ':') s of
@@ -68,57 +61,47 @@ testPartial size offset count out = it title $ withApp defaultSettings app $ \po
68 _ -> Nothing
61 _ -> Nothing
69 range = "bytes " ++ show offset ++ "-" ++ show (offset + count - 1) ++ "/" ++ show size
62 range = "bytes " ++ show offset ++ "-" ++ show (offset + count - 1) ++ "/" ++ show size
70
63
71−testFileRange :: String
72− -> Status -> ResponseHeaders -> FilePath
73− -> Maybe FilePart -> Maybe HeaderValue
74− -> Either String (Status, ResponseHeaders, Integer, Integer)
75− -> Spec
76−testFileRange desc s rsphdr file mPart mRange ans = it desc $ do
77− eres <- fileRange s rsphdr file mPart mRange
78− let res = case eres of
79− Left e -> Left $ show e
80− Right r -> Right r
81− res `shouldBe` ans
82−
83spec :: Spec
64spec :: Spec
84spec = do
65spec = do
66+{- http-client does not support this.
67+ describe "preventing response splitting attack" $ do
68+ it "sanitizes header values" $ do
69+ let app _ respond = respond $ responseLBS status200 [("foo", "foo\r\nbar")] "Hello"
70+ withApp defaultSettings app $ \port -> do
71+ res <- sendGET $ "http://127.0.0.1:" ++ show port
72+ getHeaderValue "foo" (responseHeaders res) `shouldBe`
73+ Just "foo bar" -- HTTP inserts two spaces for \r\n.
74+-}
75+
76+ describe "sanitizeHeaderValue" $ do
77+ it "doesn't alter valid multiline header values" $ do
78+ sanitizeHeaderValue "foo\r\n bar" `shouldBe` "foo\r\n bar"
79+
80+ it "adds missing spaces after \r\n" $ do
81+ sanitizeHeaderValue "foo\r\nbar" `shouldBe` "foo\r\n bar"
82+
83+ it "discards empty lines" $ do
84+ sanitizeHeaderValue "foo\r\n\r\nbar" `shouldBe` "foo\r\n bar"
85+
86+ context "when sanitizing single occurrences of \n" $ do
87+ it "replaces \n with \r\n" $ do
88+ sanitizeHeaderValue "foo\n bar" `shouldBe` "foo\r\n bar"
89+
90+ it "adds missing spaces after \n" $ do
91+ sanitizeHeaderValue "foo\nbar" `shouldBe` "foo\r\n bar"
92+
93+ it "discards single occurrences of \r" $ do
94+ sanitizeHeaderValue "foo\rbar" `shouldBe` "foobar"
95+
85 describe "range requests" $ do
96 describe "range requests" $ do
86− testRange "2-3" "23" "2-3/16"
97+ testRange "2-3" "23" $ Just "2-3/16"
87− testRange "5-" "56789abcdef" "5-15/16"
98+ testRange "5-" "56789abcdef" $ Just "5-15/16"
88− testRange "5-8" "5678" "5-8/16"
99+ testRange "5-8" "5678" $ Just "5-8/16"
89− testRange "-3" "def" "13-15/16"
100+ testRange "-3" "def" $ Just "13-15/16"
101+ testRange "16-" "" $ Just "*/16"
102+ testRange "-17" "0123456789abcdef" Nothing
90
103
91 describe "partial files" $ do
104 describe "partial files" $ do
92 testPartial 16 2 2 "23"
105 testPartial 16 2 2 "23"
93 testPartial 16 0 2 "01"
106 testPartial 16 0 2 "01"
94 testPartial 16 3 8 "3456789a"
107 testPartial 16 3 8 "3456789a"
95−
96− describe "fileRange" $ do
97− testFileRange
98− "gets a file size from file system"
99− status200 [] "attic/hex" Nothing Nothing
100− $ Right (status200,[("Content-Length","16")],0,16)
101− testFileRange
102− "gets an error if a file does not exist"
103− status200 [] "attic/nonexist" Nothing Nothing
104− $ Left "attic/nonexist: getFileStatus: does not exist (No such file or directory)"
105− testFileRange
106− "changes status if FileParts is specified"
107− status200 [] "attic/hex" (Just (FilePart 2 10 16)) Nothing
108− $ Right (status206,[("Content-Range", "bytes 2-11/16"),("Content-Length","10")],2,10)
109− testFileRange
110− "does not change status and does not add Content-Range if FileParts means the entire"
111− status200 [] "attic/hex" (Just (FilePart 0 16 16)) Nothing
112− $ Right (status200,[("Content-Length","16")],0,16)
113− testFileRange
114− "gets a file size from file system and handles Range and returns Partical Content"
115− status200 [] "attic/hex" Nothing (Just "bytes=2-14")
116− $ Right (status206,[("Content-Range","bytes 2-14/16"),("Content-Length","13")],2,13)
117− testFileRange
118− "gets a file size from file system and handles Range and returns OK if Range means the entire"
119− status200 [] "attic/hex" Nothing (Just "bytes=0-15")
120− $ Right (status200,[("Content-Length","16")],0,16)
121− testFileRange
122− "igores Range if FilePart is specified"
123− status200 [] "attic/hex" (Just (FilePart 2 10 16)) (Just "bytes=8-9")
124− $ Right (status206,[("Content-Range", "bytes 2-11/16"),("Content-Length","10")],2,10)
warp/test/RunSpec.hs+250−112
@@ -1,32 +1,33 @@
1−{-# LANGUAGE CPP #-}
2{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
4
3
5−module RunSpec (main, spec, withApp) where
4+module RunSpec (main, spec, withApp, MySocket, msWrite, msRead, withMySocket) where
6
5
7import Control.Concurrent (forkIO, killThread, threadDelay)
6import Control.Concurrent (forkIO, killThread, threadDelay)
7+import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
8+import Control.Concurrent.STM
9+import qualified Control.Exception as E
10+import Control.Exception.Lifted (bracket, try, IOException, onException)
8−import Control.Monad (forM_, replicateM_)
11+import Control.Monad (forM_, replicateM_, unless)
9−import System.Timeout (timeout)
10import Control.Monad.IO.Class (MonadIO, liftIO)
12import Control.Monad.IO.Class (MonadIO, liftIO)
11−import Data.ByteString (ByteString, hPutStr, hGetSome)
13+import Data.ByteString (ByteString)
12import qualified Data.ByteString as S
14import qualified Data.ByteString as S
15+import Data.ByteString.Builder (byteString)
13import qualified Data.ByteString.Char8 as S8
16import qualified Data.ByteString.Char8 as S8
14import qualified Data.ByteString.Lazy as L
17import qualified Data.ByteString.Lazy as L
15−import Data.Conduit (($$), (=$))
16−import qualified Data.Conduit.List
17import qualified Data.IORef as I
18import qualified Data.IORef as I
18−import Network (connectTo, PortID (PortNumber))
19+import Data.Streaming.Network (bindPortTCP, getSocketTCP, safeRecv)
19import Network.HTTP.Types
20import Network.HTTP.Types
20−import Network.Wai
21+import Network.Socket
22+import Network.Socket.ByteString (sendAll)
23+import Network.Wai hiding (responseHeaders)
24+import Network.Wai.Internal (getRequestBodyChunk)
21import Network.Wai.Handler.Warp
25import Network.Wai.Handler.Warp
22−import System.IO (hFlush, hClose)
23import System.IO.Unsafe (unsafePerformIO)
26import System.IO.Unsafe (unsafePerformIO)
27+import System.Timeout (timeout)
24import Test.Hspec
28import Test.Hspec
25−import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
29+
26−import Control.Exception.Lifted (bracket, try, IOException, onException)
27−import Data.Streaming.Network (bindPortTCP)
28−import Network.Socket (sClose)
29−import qualified Network.HTTP as HTTP
30+import HTTP
30
31
31main :: IO ()
32main :: IO ()
32main = hspec spec
33main = hspec spec
@@ -34,6 +35,52 @@ main = hspec spec
34type Counter = I.IORef (Either String Int)
35type Counter = I.IORef (Either String Int)
35type CounterApplication = Counter -> Application
36type CounterApplication = Counter -> Application
36
37
38+data MySocket = MySocket
39+ { msSocket :: !Socket
40+ , msBuffer :: !(I.IORef ByteString)
41+ }
42+
43+msWrite :: MySocket -> ByteString -> IO ()
44+msWrite ms bs = sendAll (msSocket ms) bs
45+
46+msRead :: MySocket -> Int -> IO ByteString
47+msRead (MySocket s ref) expected = do
48+ bs <- I.readIORef ref
49+ inner (bs:) (S.length bs)
50+ where
51+ inner front total =
52+ case compare total expected of
53+ EQ -> do
54+ I.writeIORef ref mempty
55+ pure $ S.concat $ front []
56+ GT -> do
57+ let bs = S.concat $ front []
58+ (x, y) = S.splitAt expected bs
59+ I.writeIORef ref y
60+ pure x
61+ LT -> do
62+ bs <- safeRecv s 4096
63+ if S.null bs
64+ then do
65+ I.writeIORef ref mempty
66+ pure $ S.concat $ front []
67+ else inner (front . (bs:)) (total + S.length bs)
68+
69+msClose :: MySocket -> IO ()
70+msClose = Network.Socket.close . msSocket
71+
72+connectTo :: Int -> IO MySocket
73+connectTo port = do
74+ s <- fst <$> getSocketTCP "127.0.0.1" port
75+ ref <- I.newIORef mempty
76+ return MySocket {
77+ msSocket = s
78+ , msBuffer = ref
79+ }
80+
81+withMySocket :: (MySocket -> IO a) -> Int -> IO a
82+withMySocket body port = bracket (connectTo port) msClose body
83+
37incr :: MonadIO m => Counter -> m ()
84incr :: MonadIO m => Counter -> m ()
38incr icount = liftIO $ I.atomicModifyIORef icount $ \ecount ->
85incr icount = liftIO $ I.atomicModifyIORef icount $ \ecount ->
39 ((case ecount of
86 ((case ecount of
@@ -44,8 +91,8 @@ err :: (MonadIO m, Show a) => Counter -> a -> m ()
44err icount msg = liftIO $ I.writeIORef icount $ Left $ show msg
91err icount msg = liftIO $ I.writeIORef icount $ Left $ show msg
45
92
46readBody :: CounterApplication
93readBody :: CounterApplication
47−readBody icount req = do
94+readBody icount req f = do
48− body <- requestBody req $$ Data.Conduit.List.consume
95+ body <- consumeBody $ getRequestBodyChunk req
49 case () of
96 case () of
50 ()
97 ()
51 | pathInfo req == ["hello"] && L.fromChunks body /= "Hello"
98 | pathInfo req == ["hello"] && L.fromChunks body /= "Hello"
@@ -55,33 +102,34 @@ readBody icount req = do
55 | not $ requestMethod req `elem` ["GET", "POST"]
102 | not $ requestMethod req `elem` ["GET", "POST"]
56 -> err icount ("Invalid request method (readBody)" :: String, requestMethod req)
103 -> err icount ("Invalid request method (readBody)" :: String, requestMethod req)
57 | otherwise -> incr icount
104 | otherwise -> incr icount
58− return $ responseLBS status200 [] "Read the body"
105+ f $ responseLBS status200 [] "Read the body"
59
106
60ignoreBody :: CounterApplication
107ignoreBody :: CounterApplication
61−ignoreBody icount req = do
108+ignoreBody icount req f = do
62 if (requestMethod req `elem` ["GET", "POST"])
109 if (requestMethod req `elem` ["GET", "POST"])
63 then incr icount
110 then incr icount
64 else err icount ("Invalid request method" :: String, requestMethod req)
111 else err icount ("Invalid request method" :: String, requestMethod req)
65− return $ responseLBS status200 [] "Ignored the body"
112+ f $ responseLBS status200 [] "Ignored the body"
66
113
67doubleConnect :: CounterApplication
114doubleConnect :: CounterApplication
68−doubleConnect icount req = do
115+doubleConnect icount req f = do
69− _ <- requestBody req $$ Data.Conduit.List.consume
116+ _ <- consumeBody $ getRequestBodyChunk req
70− _ <- requestBody req $$ Data.Conduit.List.consume
117+ _ <- consumeBody $ getRequestBodyChunk req
71 incr icount
118 incr icount
72− return $ responseLBS status200 [] "double connect"
119+ f $ responseLBS status200 [] "double connect"
73
120
74nextPort :: I.IORef Int
121nextPort :: I.IORef Int
75nextPort = unsafePerformIO $ I.newIORef 5000
122nextPort = unsafePerformIO $ I.newIORef 5000
123+{-# NOINLINE nextPort #-}
76
124
77getPort :: IO Int
125getPort :: IO Int
78getPort = do
126getPort = do
79 port <- I.atomicModifyIORef nextPort $ \p -> (p + 1, p)
127 port <- I.atomicModifyIORef nextPort $ \p -> (p + 1, p)
80− esocket <- try $ bindPortTCP port "*4"
128+ esocket <- try $ bindPortTCP port "127.0.0.1"
81 case esocket of
129 case esocket of
82 Left (_ :: IOException) -> RunSpec.getPort
130 Left (_ :: IOException) -> RunSpec.getPort
83− Right socket -> do
131+ Right sock -> do
84− sClose socket
132+ close sock
85 return port
133 return port
86
134
87withApp :: Settings -> Application -> (Int -> IO a) -> IO a
135withApp :: Settings -> Application -> (Int -> IO a) -> IO a
@@ -89,12 +137,19 @@ withApp settings app f = do
89 port <- RunSpec.getPort
137 port <- RunSpec.getPort
90 baton <- newEmptyMVar
138 baton <- newEmptyMVar
91 let settings' = setPort port
139 let settings' = setPort port
140+ $ setHost "127.0.0.1"
92 $ setBeforeMainLoop (putMVar baton ())
141 $ setBeforeMainLoop (putMVar baton ())
93 settings
142 settings
94 bracket
143 bracket
95 (forkIO $ runSettings settings' app `onException` putMVar baton ())
144 (forkIO $ runSettings settings' app `onException` putMVar baton ())
96 killThread
145 killThread
146+ (const $ do
97− (const $ takeMVar baton >> f port)
147+ takeMVar baton
148+ -- use timeout to make sure we don't take too long
149+ mres <- timeout (60 * 1000 * 1000) (f port)
150+ case mres of
151+ Nothing -> error "Timeout triggered, too slow!"
152+ Just a -> pure a)
98
153
99runTest :: Int -- ^ expected number of requests
154runTest :: Int -- ^ expected number of requests
100 -> CounterApplication
155 -> CounterApplication
@@ -102,17 +157,16 @@ runTest :: Int -- ^ expected number of requests
102 -> IO ()
157 -> IO ()
103runTest expected app chunks = do
158runTest expected app chunks = do
104 ref <- I.newIORef (Right 0)
159 ref <- I.newIORef (Right 0)
105− withApp defaultSettings (app ref) $ \port -> do
160+ withApp defaultSettings (app ref) $ withMySocket $ \ms -> do
106− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
107− forM_ chunks $ \chunk -> hPutStr handle chunk >> hFlush handle
161+ forM_ chunks $ \chunk -> msWrite ms chunk
108− _ <- timeout 100000 $ replicateM_ expected $ hGetSome handle 4096
162+ _ <- timeout 100000 $ replicateM_ expected $ msRead ms 4096
109 res <- I.readIORef ref
163 res <- I.readIORef ref
110 case res of
164 case res of
111 Left s -> error s
165 Left s -> error s
112 Right i -> i `shouldBe` expected
166 Right i -> i `shouldBe` expected
113
167
114dummyApp :: Application
168dummyApp :: Application
115−dummyApp _ = return $ responseLBS status200 [] "foo"
169+dummyApp _ f = f $ responseLBS status200 [] "foo"
116
170
117runTerminateTest :: InvalidRequest
171runTerminateTest :: InvalidRequest
118 -> ByteString
172 -> ByteString
@@ -120,11 +174,9 @@ runTerminateTest :: InvalidRequest
120runTerminateTest expected input = do
174runTerminateTest expected input = do
121 ref <- I.newIORef Nothing
175 ref <- I.newIORef Nothing
122 let onExc _ = I.writeIORef ref . Just
176 let onExc _ = I.writeIORef ref . Just
123− withApp (setOnException onExc defaultSettings) dummyApp $ \port -> do
177+ withApp (setOnException onExc defaultSettings) dummyApp $ withMySocket $ \ms -> do
124− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
178+ msWrite ms input
125− hPutStr handle input
179+ msClose ms -- explicitly
126− hFlush handle
127− hClose handle
128 threadDelay 1000
180 threadDelay 1000
129 res <- I.readIORef ref
181 res <- I.readIORef ref
130 show res `shouldBe` show (Just expected)
182 show res `shouldBe` show (Just expected)
@@ -135,6 +187,12 @@ singleGet = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
135singlePostHello :: ByteString
187singlePostHello :: ByteString
136singlePostHello = "POST /hello HTTP/1.1\r\nHost: localhost\r\nContent-length: 5\r\n\r\nHello"
188singlePostHello = "POST /hello HTTP/1.1\r\nHost: localhost\r\nContent-length: 5\r\n\r\nHello"
137
189
190+singleChunkedPostHello :: [ByteString]
191+singleChunkedPostHello =
192+ [ "POST /hello HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n"
193+ , "5\r\nHello\r\n0\r\n"
194+ ]
195+
138spec :: Spec
196spec :: Spec
139spec = do
197spec = do
140 describe "non-pipelining" $ do
198 describe "non-pipelining" $ do
@@ -148,6 +206,14 @@ spec = do
148 [ singlePostHello
206 [ singlePostHello
149 , singleGet
207 , singleGet
150 ]
208 ]
209+ it "chunked body, read" $ runTest 2 readBody $ concat
210+ [ singleChunkedPostHello
211+ , [singleGet]
212+ ]
213+ it "chunked body, ignore" $ runTest 2 ignoreBody $ concat
214+ [ singleChunkedPostHello
215+ , [singleGet]
216+ ]
151 describe "pipelining" $ do
217 describe "pipelining" $ do
152 it "no body, read" $ runTest 5 readBody [S.concat $ replicate 5 singleGet]
218 it "no body, read" $ runTest 5 readBody [S.concat $ replicate 5 singleGet]
153 it "no body, ignore" $ runTest 5 ignoreBody [S.concat $ replicate 5 singleGet]
219 it "no body, ignore" $ runTest 5 ignoreBody [S.concat $ replicate 5 singleGet]
@@ -159,6 +225,14 @@ spec = do
159 [ singlePostHello
225 [ singlePostHello
160 , singleGet
226 , singleGet
161 ]
227 ]
228+ it "chunked body, read" $ runTest 2 readBody $ return $ S.concat
229+ [ S.concat singleChunkedPostHello
230+ , singleGet
231+ ]
232+ it "chunked body, ignore" $ runTest 2 ignoreBody $ return $ S.concat
233+ [ S.concat singleChunkedPostHello
234+ , singleGet
235+ ]
162 describe "no hanging" $ do
236 describe "no hanging" $ do
163 it "has body, read" $ runTest 1 readBody $ map S.singleton $ S.unpack singlePostHello
237 it "has body, read" $ runTest 1 readBody $ map S.singleton $ S.unpack singlePostHello
164 it "double connect" $ runTest 1 doubleConnect [singlePostHello]
238 it "double connect" $ runTest 1 doubleConnect [singlePostHello]
@@ -170,17 +244,14 @@ spec = do
170 describe "special input" $ do
244 describe "special input" $ do
171 it "multiline headers" $ do
245 it "multiline headers" $ do
172 iheaders <- I.newIORef []
246 iheaders <- I.newIORef []
173− let app req = do
247+ let app req f = do
174 liftIO $ I.writeIORef iheaders $ requestHeaders req
248 liftIO $ I.writeIORef iheaders $ requestHeaders req
175− return $ responseLBS status200 [] ""
249+ f $ responseLBS status200 [] ""
176− withApp defaultSettings app $ \port -> do
250+ withApp defaultSettings app $ withMySocket $ \ms -> do
177− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
178 let input = S.concat
251 let input = S.concat
179 [ "GET / HTTP/1.1\r\nfoo: bar\r\n baz\r\n\tbin\r\n\r\n"
252 [ "GET / HTTP/1.1\r\nfoo: bar\r\n baz\r\n\tbin\r\n\r\n"
180 ]
253 ]
181− hPutStr handle input
254+ msWrite ms input
182− hFlush handle
183− hClose handle
184 threadDelay 1000
255 threadDelay 1000
185 headers <- I.readIORef iheaders
256 headers <- I.readIORef iheaders
186 headers `shouldBe`
257 headers `shouldBe`
@@ -188,17 +259,14 @@ spec = do
188 ]
259 ]
189 it "no space between colon and value" $ do
260 it "no space between colon and value" $ do
190 iheaders <- I.newIORef []
261 iheaders <- I.newIORef []
191− let app req = do
262+ let app req f = do
192 liftIO $ I.writeIORef iheaders $ requestHeaders req
263 liftIO $ I.writeIORef iheaders $ requestHeaders req
193− return $ responseLBS status200 [] ""
264+ f $ responseLBS status200 [] ""
194− withApp defaultSettings app $ \port -> do
265+ withApp defaultSettings app $ withMySocket $ \ms -> do
195− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
196 let input = S.concat
266 let input = S.concat
197 [ "GET / HTTP/1.1\r\nfoo:bar\r\n\r\n"
267 [ "GET / HTTP/1.1\r\nfoo:bar\r\n\r\n"
198 ]
268 ]
199− hPutStr handle input
269+ msWrite ms input
200− hFlush handle
201− hClose handle
202 threadDelay 1000
270 threadDelay 1000
203 headers <- I.readIORef iheaders
271 headers <- I.readIORef iheaders
204 headers `shouldBe`
272 headers `shouldBe`
@@ -207,23 +275,24 @@ spec = do
207
275
208 describe "chunked bodies" $ do
276 describe "chunked bodies" $ do
209 it "works" $ do
277 it "works" $ do
278+ countVar <- newTVarIO (0 :: Int)
210 ifront <- I.newIORef id
279 ifront <- I.newIORef id
211− let app req = do
280+ let app req f = do
212− bss <- requestBody req $$ Data.Conduit.List.consume
281+ bss <- consumeBody $ getRequestBodyChunk req
213 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
282 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
283+ atomically $ modifyTVar countVar (+ 1)
214− return $ responseLBS status200 [] ""
284+ f $ responseLBS status200 [] ""
215− withApp defaultSettings app $ \port -> do
285+ withApp defaultSettings app $ withMySocket $ \ms -> do
216− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
217 let input = S.concat
286 let input = S.concat
218 [ "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
287 [ "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
219 , "c\r\nHello World\n\r\n3\r\nBye\r\n0\r\n\r\n"
288 , "c\r\nHello World\n\r\n3\r\nBye\r\n0\r\n\r\n"
220 , "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
289 , "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
221 , "b\r\nHello World\r\n0\r\n\r\n"
290 , "b\r\nHello World\r\n0\r\n\r\n"
222 ]
291 ]
223− hPutStr handle input
292+ msWrite ms input
224− hFlush handle
293+ atomically $ do
225− hClose handle
294+ count <- readTVar countVar
226− threadDelay 1000
295+ check $ count == 2
227 front <- I.readIORef ifront
296 front <- I.readIORef ifront
228 front [] `shouldBe`
297 front [] `shouldBe`
229 [ "Hello World\nBye"
298 [ "Hello World\nBye"
@@ -231,38 +300,44 @@ spec = do
231 ]
300 ]
232 it "lots of chunks" $ do
301 it "lots of chunks" $ do
233 ifront <- I.newIORef id
302 ifront <- I.newIORef id
303+ countVar <- newTVarIO (0 :: Int)
234− let app req = do
304+ let app req f = do
235− bss <- requestBody req $$ Data.Conduit.List.consume
305+ bss <- consumeBody $ getRequestBodyChunk req
236− liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
306+ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
307+ atomically $ modifyTVar countVar (+ 1)
237− return $ responseLBS status200 [] ""
308+ f $ responseLBS status200 [] ""
238− withApp defaultSettings app $ \port -> do
309+ withApp defaultSettings app $ withMySocket $ \ms -> do
239− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
240 let input = concat $ replicate 2 $
310 let input = concat $ replicate 2 $
241 ["POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"] ++
311 ["POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"] ++
242 (replicate 50 "5\r\n12345\r\n") ++
312 (replicate 50 "5\r\n12345\r\n") ++
243 ["0\r\n\r\n"]
313 ["0\r\n\r\n"]
244− mapM_ (\bs -> hPutStr handle bs >> hFlush handle) input
314+ mapM_ (msWrite ms) input
245− hClose handle
315+ atomically $ do
246− threadDelay 1000
316+ count <- readTVar countVar
317+ check $ count == 2
247 front <- I.readIORef ifront
318 front <- I.readIORef ifront
248 front [] `shouldBe` replicate 2 (S.concat $ replicate 50 "12345")
319 front [] `shouldBe` replicate 2 (S.concat $ replicate 50 "12345")
320+-- For some reason, the following test on Windows causes the socket
321+-- to be killed prematurely. Worth investigating in the future if possible.
249 it "in chunks" $ do
322 it "in chunks" $ do
250 ifront <- I.newIORef id
323 ifront <- I.newIORef id
324+ countVar <- newTVarIO (0 :: Int)
251− let app req = do
325+ let app req f = do
252− bss <- requestBody req $$ Data.Conduit.List.consume
326+ bss <- consumeBody $ getRequestBodyChunk req
253 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
327 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
328+ atomically $ modifyTVar countVar (+ 1)
254− return $ responseLBS status200 [] ""
329+ f $ responseLBS status200 [] ""
255− withApp defaultSettings app $ \port -> do
330+ withApp defaultSettings app $ withMySocket $ \ms -> do
256− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
257 let input = S.concat
331 let input = S.concat
258 [ "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
332 [ "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
259 , "c\r\nHello World\n\r\n3\r\nBye\r\n0\r\n"
333 , "c\r\nHello World\n\r\n3\r\nBye\r\n0\r\n"
260 , "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
334 , "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
261 , "b\r\nHello World\r\n0\r\n\r\n"
335 , "b\r\nHello World\r\n0\r\n\r\n"
262 ]
336 ]
263− mapM_ (\bs -> hPutStr handle bs >> hFlush handle) $ map S.singleton $ S.unpack input
337+ mapM_ (msWrite ms) $ map S.singleton $ S.unpack input
264− hClose handle
338+ atomically $ do
265− threadDelay 1000
339+ count <- readTVar countVar
340+ check $ count == 2
266 front <- I.readIORef ifront
341 front <- I.readIORef ifront
267 front [] `shouldBe`
342 front [] `shouldBe`
268 [ "Hello World\nBye"
343 [ "Hello World\nBye"
@@ -270,56 +345,119 @@ spec = do
270 ]
345 ]
271 it "timeout in request body" $ do
346 it "timeout in request body" $ do
272 ifront <- I.newIORef id
347 ifront <- I.newIORef id
273− let app req = do
348+ let app req f = do
274− bss <- (requestBody req $$ Data.Conduit.List.consume) `onException`
349+ bss <- (consumeBody $ getRequestBodyChunk req) `onException`
275 liftIO (I.atomicModifyIORef ifront (\front -> (front . ("consume interrupted":), ())))
350 liftIO (I.atomicModifyIORef ifront (\front -> (front . ("consume interrupted":), ())))
276− liftIO $ threadDelay 4000000 `onException`
351+ liftIO $ threadDelay 4000000 `E.catch` \e -> do
277− I.atomicModifyIORef ifront (\front -> (front . ("threadDelay interrupted":), ()))
352+ I.atomicModifyIORef ifront (\front ->
353+ ( front . ((S8.pack $ "threadDelay interrupted: " ++ show e):)
354+ , ()))
355+ E.throwIO (e :: E.SomeException)
278 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
356 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
279− return $ responseLBS status200 [] ""
357+ f $ responseLBS status200 [] ""
280− withApp (setTimeout 1 defaultSettings) app $ \port -> do
358+ withApp (setTimeout 1 defaultSettings) app $ withMySocket $ \ms -> do
281 let bs1 = S.replicate 2048 88
359 let bs1 = S.replicate 2048 88
282 bs2 = "This is short"
360 bs2 = "This is short"
283 bs = S.append bs1 bs2
361 bs = S.append bs1 bs2
284− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
285− hPutStr handle "POST / HTTP/1.1\r\n"
362+ msWrite ms "POST / HTTP/1.1\r\n"
286− hPutStr handle "content-length: "
363+ msWrite ms "content-length: "
287− hPutStr handle $ S8.pack $ show $ S.length bs
364+ msWrite ms $ S8.pack $ show $ S.length bs
288− hPutStr handle "\r\n\r\n"
365+ msWrite ms "\r\n\r\n"
289 threadDelay 100000
366 threadDelay 100000
290− hPutStr handle bs1
367+ msWrite ms bs1
291 threadDelay 100000
368 threadDelay 100000
292− hPutStr handle bs2
369+ msWrite ms bs2
293− hClose handle
294 threadDelay 5000000
370 threadDelay 5000000
295 front <- I.readIORef ifront
371 front <- I.readIORef ifront
296 S.concat (front []) `shouldBe` bs
372 S.concat (front []) `shouldBe` bs
297 describe "raw body" $ do
373 describe "raw body" $ do
298 it "works" $ do
374 it "works" $ do
299− let app _req = do
375+ let app _req f = do
300 let backup = responseLBS status200 [] "Not raw"
376 let backup = responseLBS status200 [] "Not raw"
301− return $ flip responseRaw backup $ \src sink ->
377+ f $ flip responseRaw backup $ \src sink -> do
302− src
378+ let loop = do
303− $$ Data.Conduit.List.map doubleBS
379+ bs <- src
380+ unless (S.null bs) $ do
304− =$ sink
381+ sink $ doubleBS bs
382+ loop
383+ loop
305 doubleBS = S.concatMap $ \w -> S.pack [w, w]
384 doubleBS = S.concatMap $ \w -> S.pack [w, w]
306− withApp defaultSettings app $ \port -> do
385+ withApp defaultSettings app $ withMySocket $ \ms -> do
307− handle <- connectTo "127.0.0.1" $ PortNumber $ fromIntegral port
308− hPutStr handle "POST / HTTP/1.1\r\n\r\n12345"
386+ msWrite ms "POST / HTTP/1.1\r\n\r\n12345"
309− hFlush handle
310− timeout 100000 (S.hGet handle 10) >>= (`shouldBe` Just "1122334455")
387+ timeout 100000 (msRead ms 10) >>= (`shouldBe` Just "1122334455")
311− hPutStr handle "67890"
388+ msWrite ms "67890"
312− hFlush handle
313− timeout 100000 (S.hGet handle 10) >>= (`shouldBe` Just "6677889900")
389+ timeout 100000 (msRead ms 10) >>= (`shouldBe` Just "6677889900")
314−
315 it "only one date and server header" $ do
390 it "only one date and server header" $ do
316− let app _ = return $ responseLBS status200
391+ let app _ f = f $ responseLBS status200
317 [ ("server", "server")
392 [ ("server", "server")
318 , ("date", "date")
393 , ("date", "date")
319 ] ""
394 ] ""
395+ getValues key = map snd
396+ . filter (\(key', _) -> key == key')
397+ . responseHeaders
320 withApp defaultSettings app $ \port -> do
398 withApp defaultSettings app $ \port -> do
321− Right res <- HTTP.simpleHTTP (HTTP.getRequest $ "http://127.0.0.1:" ++ show port)
399+ res <- sendGET $ "http://127.0.0.1:" ++ show port
322− map HTTP.hdrValue (HTTP.retrieveHeaders HTTP.HdrServer res)
323− `shouldBe` ["server"]
400+ getValues hServer res `shouldBe` ["server"]
324− map HTTP.hdrValue (HTTP.retrieveHeaders HTTP.HdrDate res)
325− `shouldBe` ["date"]
401+ getValues hDate res `shouldBe` ["date"]
402+
403+ it "streaming echo #249" $ do
404+ countVar <- newTVarIO (0 :: Int)
405+ let app req f = f $ responseStream status200 [] $ \write _ -> do
406+ let loop = do
407+ bs <- getRequestBodyChunk req
408+ unless (S.null bs) $ do
409+ write $ byteString bs
410+ atomically $ modifyTVar countVar (+ 1)
411+ loop
412+ loop
413+ withApp defaultSettings app $ withMySocket $ \ms -> do
414+ msWrite ms "POST / HTTP/1.1\r\ntransfer-encoding: chunked\r\n\r\n"
415+ threadDelay 10000
416+ msWrite ms "5\r\nhello\r\n0\r\n\r\n"
417+ atomically $ do
418+ count <- readTVar countVar
419+ check $ count >= 1
420+ bs <- safeRecv (msSocket ms) 4096 -- must not use msRead
421+ S.takeWhile (/= 13) bs `shouldBe` "HTTP/1.1 200 OK"
422+
423+ it "streaming response with length" $ do
424+ let app _ f = f $ responseStream status200 [("content-length", "20")] $ \write _ -> do
425+ replicateM_ 4 $ write $ byteString "Hello"
426+ withApp defaultSettings app $ \port -> do
427+ res <- sendGET $ "http://127.0.0.1:" ++ show port
428+ responseBody res `shouldBe` "HelloHelloHelloHello"
429+
430+ describe "head requests" $ do
431+ let fp = "test/head-response"
432+ let app req f =
433+ f $ case pathInfo req of
434+ ["builder"] -> responseBuilder status200 [] $ error "should never be evaluated"
435+ ["streaming"] -> responseStream status200 [] $ \write _ ->
436+ write $ error "should never be evaluated"
437+ ["file"] -> responseFile status200 [] fp Nothing
438+ _ -> error "invalid path"
439+ it "builder" $ withApp defaultSettings app $ \port -> do
440+ res <- sendHEAD $ concat ["http://127.0.0.1:", show port, "/builder"]
441+ responseBody res `shouldBe` ""
442+ it "streaming" $ withApp defaultSettings app $ \port -> do
443+ res <- sendHEAD $ concat ["http://127.0.0.1:", show port, "/streaming"]
444+ responseBody res `shouldBe` ""
445+ it "file, no range" $ withApp defaultSettings app $ \port -> do
446+ bs <- S.readFile fp
447+ res <- sendHEAD $ concat ["http://127.0.0.1:", show port, "/file"]
448+ getHeaderValue hContentLength (responseHeaders res) `shouldBe` Just (S8.pack $ show $ S.length bs)
449+ it "file, with range" $ withApp defaultSettings app $ \port -> do
450+ res <- sendHEADwH
451+ (concat ["http://127.0.0.1:", show port, "/file"])
452+ [(hRange, "bytes=0-1")]
453+ getHeaderValue hContentLength (responseHeaders res) `shouldBe` Just "2"
454+
455+consumeBody :: IO ByteString -> IO [ByteString]
456+consumeBody body =
457+ loop id
458+ where
459+ loop front = do
460+ bs <- body
461+ if S.null bs
462+ then return $ front []
463+ else loop $ front . (bs:)
warp/test/SendFileSpec.hsnew file+116

This diff is too big to show here.

warp/test/ThreadSpec.hsdeleted−26

This diff is too big to show here.

warp/test/WithApplicationSpec.hsnew file+45

This diff is too big to show here.

warp/test/doctests.hs+1−8

This diff is too big to show here.

warp/test/head-responsenew file+1

This diff is too big to show here.

warp/test/inputFilenew file+100

This diff is too big to show here.

warp/warp.cabal+163−67

This diff is too big to show here.