/usr/share/doc/libghc-happstack-server-doc/html/src/Happstack-Server-Proxy.html is in libghc-happstack-server-doc 7.4.6.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>src/Happstack/Server/Proxy.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE FlexibleContexts #-}</span>
<a name="line-2"></a><span class='hs-comment'>-- |Support for creating a proxy or reverse-proxy server</span>
<a name="line-3"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Happstack</span><span class='hs-varop'>.</span><span class='hs-conid'>Server</span><span class='hs-varop'>.</span><span class='hs-conid'>Proxy</span> <span class='hs-keyword'>where</span>
<a name="line-4"></a>
<a name="line-5"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadPlus</span><span class='hs-layout'>(</span><span class='hs-varid'>mzero</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>liftM</span><span class='hs-layout'>)</span>
<a name="line-6"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadIO</span><span class='hs-layout'>(</span><span class='hs-varid'>liftIO</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-7"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>ByteString</span><span class='hs-varop'>.</span><span class='hs-conid'>Char8</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>B</span>
<a name="line-8"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span> <span class='hs-layout'>(</span><span class='hs-varid'>isPrefixOf</span><span class='hs-layout'>)</span>
<a name="line-9"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromJust</span><span class='hs-layout'>,</span> <span class='hs-varid'>fromMaybe</span><span class='hs-layout'>)</span>
<a name="line-10"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Happstack</span><span class='hs-varop'>.</span><span class='hs-conid'>Server</span><span class='hs-varop'>.</span><span class='hs-conid'>Monads</span> <span class='hs-layout'>(</span><span class='hs-conid'>ServerMonad</span><span class='hs-layout'>(</span><span class='hs-varid'>askRq</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>FilterMonad</span><span class='hs-layout'>,</span> <span class='hs-conid'>WebMonad</span><span class='hs-layout'>,</span> <span class='hs-varid'>escape'</span><span class='hs-layout'>)</span>
<a name="line-11"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Happstack</span><span class='hs-varop'>.</span><span class='hs-conid'>Server</span><span class='hs-varop'>.</span><span class='hs-conid'>Response</span> <span class='hs-layout'>(</span><span class='hs-varid'>badGateway</span><span class='hs-layout'>,</span> <span class='hs-varid'>toResponse</span><span class='hs-layout'>)</span>
<a name="line-12"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Happstack</span><span class='hs-varop'>.</span><span class='hs-conid'>Server</span><span class='hs-varop'>.</span><span class='hs-conid'>Client</span> <span class='hs-layout'>(</span><span class='hs-varid'>getResponse</span><span class='hs-layout'>)</span>
<a name="line-13"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Happstack</span><span class='hs-varop'>.</span><span class='hs-conid'>Server</span><span class='hs-varop'>.</span><span class='hs-conid'>Types</span> <span class='hs-layout'>(</span><span class='hs-conid'>Request</span><span class='hs-layout'>(</span><span class='hs-varid'>rqPaths</span><span class='hs-layout'>,</span> <span class='hs-varid'>rqHeaders</span><span class='hs-layout'>,</span> <span class='hs-varid'>rqPeer</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>Response</span><span class='hs-layout'>,</span> <span class='hs-varid'>setHeader</span><span class='hs-layout'>,</span> <span class='hs-varid'>getHeader</span><span class='hs-layout'>)</span>
<a name="line-14"></a>
<a name="line-15"></a><a name="proxyServe"></a><span class='hs-comment'>-- | 'proxyServe' is for creating a part that acts as a proxy. The</span>
<a name="line-16"></a><span class='hs-comment'>-- sole argument @['String']@ is a list of allowed domains for</span>
<a name="line-17"></a><span class='hs-comment'>-- proxying. This matches the domain part of the request and the</span>
<a name="line-18"></a><span class='hs-comment'>-- wildcard * can be used. E.g.</span>
<a name="line-19"></a><span class='hs-comment'>--</span>
<a name="line-20"></a><span class='hs-comment'>-- * \"*\" to match anything.</span>
<a name="line-21"></a><span class='hs-comment'>--</span>
<a name="line-22"></a><span class='hs-comment'>-- * \"*.example.com\" to match anything under example.com</span>
<a name="line-23"></a><span class='hs-comment'>--</span>
<a name="line-24"></a><span class='hs-comment'>-- * \"example.com\" to match just example.com</span>
<a name="line-25"></a><span class='hs-comment'>--</span>
<a name="line-26"></a><span class='hs-comment'>--</span>
<a name="line-27"></a><span class='hs-comment'>-- TODO: annoyingly enough, this method eventually calls 'escape', so</span>
<a name="line-28"></a><span class='hs-comment'>-- any headers you set won't be used, and the computation immediately</span>
<a name="line-29"></a><span class='hs-comment'>-- ends.</span>
<a name="line-30"></a><span class='hs-definition'>proxyServe</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>WebMonad</span> <span class='hs-conid'>Response</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>ServerMonad</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadPlus</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>FilterMonad</span> <span class='hs-conid'>Response</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>Response</span>
<a name="line-31"></a><span class='hs-definition'>proxyServe</span> <span class='hs-varid'>allowed</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-32"></a> <span class='hs-varid'>rq</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>askRq</span>
<a name="line-33"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>cond</span> <span class='hs-varid'>rq</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>proxyServe'</span> <span class='hs-varid'>rq</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>mzero</span>
<a name="line-34"></a> <span class='hs-keyword'>where</span>
<a name="line-35"></a> <span class='hs-varid'>cond</span> <span class='hs-varid'>rq</span>
<a name="line-36"></a> <span class='hs-keyglyph'>|</span> <span class='hs-str'>"*"</span> <span class='hs-varop'>`elem`</span> <span class='hs-varid'>allowed</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-37"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>domain</span> <span class='hs-varop'>`elem`</span> <span class='hs-varid'>allowed</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-38"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>superdomain</span> <span class='hs-varop'>`elem`</span> <span class='hs-varid'>wildcards</span> <span class='hs-keyglyph'>=</span><span class='hs-conid'>True</span>
<a name="line-39"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-40"></a> <span class='hs-keyword'>where</span>
<a name="line-41"></a> <span class='hs-varid'>domain</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>head</span> <span class='hs-layout'>(</span><span class='hs-varid'>rqPaths</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span>
<a name="line-42"></a> <span class='hs-varid'>superdomain</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tail</span> <span class='hs-varop'>$</span> <span class='hs-varid'>snd</span> <span class='hs-varop'>$</span> <span class='hs-varid'>break</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span><span class='hs-chr'>'.'</span><span class='hs-layout'>)</span> <span class='hs-varid'>domain</span>
<a name="line-43"></a> <span class='hs-varid'>wildcards</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>drop</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>filter</span> <span class='hs-layout'>(</span><span class='hs-str'>"*."</span> <span class='hs-varop'>`isPrefixOf`</span><span class='hs-layout'>)</span> <span class='hs-varid'>allowed</span><span class='hs-layout'>)</span>
<a name="line-44"></a>
<a name="line-45"></a><a name="proxyServe'"></a><span class='hs-comment'>-- | Take a proxy 'Request' and create a 'Response'. Your basic proxy</span>
<a name="line-46"></a><span class='hs-comment'>-- building block. See 'unproxify'.</span>
<a name="line-47"></a><span class='hs-comment'>--</span>
<a name="line-48"></a><span class='hs-comment'>-- TODO: this would be more useful if it didn\'t call 'escape'</span>
<a name="line-49"></a><span class='hs-comment'>-- (e.g. it let you modify the response afterwards, or set additional</span>
<a name="line-50"></a><span class='hs-comment'>-- headers)</span>
<a name="line-51"></a><span class='hs-definition'>proxyServe'</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>FilterMonad</span> <span class='hs-conid'>Response</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>WebMonad</span> <span class='hs-conid'>Response</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Request</span><span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>Response</span>
<a name="line-52"></a><span class='hs-definition'>proxyServe'</span> <span class='hs-varid'>rq</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftIO</span> <span class='hs-layout'>(</span><span class='hs-varid'>getResponse</span> <span class='hs-layout'>(</span><span class='hs-varid'>unproxify</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>>>=</span>
<a name="line-53"></a> <span class='hs-varid'>either</span> <span class='hs-layout'>(</span><span class='hs-varid'>badGateway</span> <span class='hs-varop'>.</span> <span class='hs-varid'>toResponse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>show</span><span class='hs-layout'>)</span> <span class='hs-varid'>escape'</span>
<a name="line-54"></a>
<a name="line-55"></a><a name="rproxyServe"></a><span class='hs-comment'>-- | This is a reverse proxy implementation. See 'unrproxify'.</span>
<a name="line-56"></a><span class='hs-comment'>--</span>
<a name="line-57"></a><span class='hs-comment'>-- TODO: this would be more useful if it didn\'t call 'escape', just</span>
<a name="line-58"></a><span class='hs-comment'>-- like 'proxyServe''.</span>
<a name="line-59"></a><span class='hs-definition'>rproxyServe</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>ServerMonad</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>WebMonad</span> <span class='hs-conid'>Response</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>FilterMonad</span> <span class='hs-conid'>Response</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span>
<a name="line-60"></a> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ defaultHost</span>
<a name="line-61"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>String</span><span class='hs-layout'>,</span> <span class='hs-conid'>String</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- ^ map to look up hostname mappings. For the reverse proxy</span>
<a name="line-62"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>Response</span> <span class='hs-comment'>-- ^ the result is a 'ServerPartT' that will reverse proxy for you.</span>
<a name="line-63"></a><span class='hs-definition'>rproxyServe</span> <span class='hs-varid'>defaultHost</span> <span class='hs-varid'>list</span> <span class='hs-keyglyph'>=</span>
<a name="line-64"></a> <span class='hs-keyword'>do</span> <span class='hs-varid'>rq</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>askRq</span>
<a name="line-65"></a> <span class='hs-varid'>r</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>liftIO</span> <span class='hs-layout'>(</span><span class='hs-varid'>getResponse</span> <span class='hs-layout'>(</span><span class='hs-varid'>unrproxify</span> <span class='hs-varid'>defaultHost</span> <span class='hs-varid'>list</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-66"></a> <span class='hs-varid'>either</span> <span class='hs-layout'>(</span><span class='hs-varid'>badGateway</span> <span class='hs-varop'>.</span> <span class='hs-varid'>toResponse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>show</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>escape'</span><span class='hs-layout'>)</span> <span class='hs-varid'>r</span>
<a name="line-67"></a>
<a name="line-68"></a>
<a name="line-69"></a><a name="unproxify"></a><span class='hs-definition'>unproxify</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Request</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Request</span>
<a name="line-70"></a><span class='hs-definition'>unproxify</span> <span class='hs-varid'>rq</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rq</span> <span class='hs-layout'>{</span><span class='hs-varid'>rqPaths</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tail</span> <span class='hs-varop'>$</span> <span class='hs-varid'>rqPaths</span> <span class='hs-varid'>rq</span><span class='hs-layout'>,</span>
<a name="line-71"></a> <span class='hs-varid'>rqHeaders</span> <span class='hs-keyglyph'>=</span>
<a name="line-72"></a> <span class='hs-varid'>forwardedFor</span> <span class='hs-varop'>$</span> <span class='hs-varid'>forwardedHost</span> <span class='hs-varop'>$</span>
<a name="line-73"></a> <span class='hs-varid'>setHeader</span> <span class='hs-str'>"host"</span> <span class='hs-layout'>(</span><span class='hs-varid'>head</span> <span class='hs-varop'>$</span> <span class='hs-varid'>rqPaths</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span>
<a name="line-74"></a> <span class='hs-varid'>rqHeaders</span> <span class='hs-varid'>rq</span><span class='hs-layout'>}</span>
<a name="line-75"></a> <span class='hs-keyword'>where</span>
<a name="line-76"></a> <span class='hs-varid'>appendInfo</span> <span class='hs-varid'>hdr</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>setHeader</span> <span class='hs-varid'>hdr</span> <span class='hs-layout'>(</span><span class='hs-varid'>csv</span> <span class='hs-varid'>val</span> <span class='hs-varop'>$</span>
<a name="line-77"></a> <span class='hs-varid'>maybe</span> <span class='hs-str'>""</span> <span class='hs-conid'>B</span><span class='hs-varop'>.</span><span class='hs-varid'>unpack</span> <span class='hs-varop'>$</span>
<a name="line-78"></a> <span class='hs-varid'>getHeader</span> <span class='hs-varid'>hdr</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span>
<a name="line-79"></a> <span class='hs-varid'>forwardedFor</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>appendInfo</span> <span class='hs-str'>"X-Forwarded-For"</span> <span class='hs-layout'>(</span><span class='hs-varid'>fst</span> <span class='hs-varop'>$</span> <span class='hs-varid'>rqPeer</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span>
<a name="line-80"></a> <span class='hs-varid'>forwardedHost</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>appendInfo</span> <span class='hs-str'>"X-Forwarded-Host"</span>
<a name="line-81"></a> <span class='hs-layout'>(</span><span class='hs-conid'>B</span><span class='hs-varop'>.</span><span class='hs-varid'>unpack</span> <span class='hs-varop'>$</span> <span class='hs-varid'>fromJust</span> <span class='hs-varop'>$</span> <span class='hs-varid'>getHeader</span> <span class='hs-str'>"host"</span> <span class='hs-varid'>rq</span><span class='hs-layout'>)</span>
<a name="line-82"></a> <span class='hs-varid'>csv</span> <span class='hs-varid'>v</span> <span class='hs-str'>""</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>v</span>
<a name="line-83"></a> <span class='hs-varid'>csv</span> <span class='hs-varid'>v</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span><span class='hs-varop'>++</span><span class='hs-str'>", "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>v</span>
<a name="line-84"></a>
<a name="line-85"></a><a name="unrproxify"></a><span class='hs-definition'>unrproxify</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>String</span><span class='hs-layout'>,</span> <span class='hs-conid'>String</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Request</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Request</span>
<a name="line-86"></a><span class='hs-definition'>unrproxify</span> <span class='hs-varid'>defaultHost</span> <span class='hs-varid'>list</span> <span class='hs-varid'>rq</span> <span class='hs-keyglyph'>=</span>
<a name="line-87"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>host</span><span class='hs-keyglyph'>::</span><span class='hs-conid'>String</span>
<a name="line-88"></a> <span class='hs-varid'>host</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fromMaybe</span> <span class='hs-varid'>defaultHost</span> <span class='hs-varop'>$</span> <span class='hs-varid'>flip</span> <span class='hs-varid'>lookup</span> <span class='hs-varid'>list</span> <span class='hs-varop'>=<<</span> <span class='hs-conid'>B</span><span class='hs-varop'>.</span><span class='hs-varid'>unpack</span> <span class='hs-varop'>`liftM`</span> <span class='hs-varid'>getHeader</span> <span class='hs-str'>"host"</span> <span class='hs-varid'>rq</span>
<a name="line-89"></a> <span class='hs-varid'>newrq</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rq</span> <span class='hs-layout'>{</span><span class='hs-varid'>rqPaths</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>host</span><span class='hs-conop'>:</span> <span class='hs-varid'>rqPaths</span> <span class='hs-varid'>rq</span><span class='hs-layout'>}</span>
<a name="line-90"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>unproxify</span> <span class='hs-varid'>newrq</span>
<a name="line-91"></a>
<a name="line-92"></a>
</pre></body>
</html>
|