Added an example program; a HTTP proxy server redirecting to HTTPS when possible.

master
vi 2015-11-07 18:14:19 +08:00
parent a6f28e07a1
commit 1b46c03817
5 changed files with 89 additions and 0 deletions

20
examples/LICENSE Normal file
View File

@ -0,0 +1,20 @@
Copyright (c) 2015 vi
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

29
examples/RedirectProxy.hs Normal file
View File

@ -0,0 +1,29 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RecordWildCards #-}
module Main (main) where
import "base" Data.Monoid ((<>))
import "bytestring" Data.ByteString.Char8 (pack, unpack)
import "http-proxy" Network.HTTP.Proxy (runProxySettings, defaultProxySettings, Settings(..), Request(..))
import "http-types" Network.HTTP.Types (status302)
import "https-everywhere-rules" Data.HTTPSEverywhere.Rules (rewriteURL)
import "network-uri" Network.URI (URI, parseURI)
import "wai" Network.Wai (Response, responseLBS)
(<&>) :: Functor f => f a -> (a -> b) -> f b
(<&>) = flip fmap; infixl 1 <&>
httpRedirect :: URI -> Response
httpRedirect to = responseLBS status302 [("Location", pack . show $ to)] ""
tryHTTPS :: Request -> IO (Either Response Request)
tryHTTPS Request{..} = case parseURI . unpack $ requestPath <> queryString of
Just uri -> rewriteURL uri <&> \rewrite -> if rewrite == uri
then return Request{..}
else Left $ httpRedirect rewrite
Nothing -> return $ Right Request{..}
main :: IO ()
main = runProxySettings $ defaultProxySettings { proxyRequestModifier = tryHTTPS }

2
examples/Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,34 @@
name:
https-everywhere-rules-examples
version:
0.0.1
homepage:
https://github.com/fmap/https-everywhere-rules
license:
MIT
license-file:
LICENSE
author:
vi
maintainer:
me@vikramverma.com
category:
Network
build-type:
Simple
cabal-version:
>=1.10
executable RedirectProxy
main-is:
RedirectProxy.hs
build-depends:
base >=4.7 && <4.9,
bytestring,
http-proxy,
http-types,
https-everywhere-rules,
network-uri,
wai
default-language:
Haskell2010

View File

@ -2,6 +2,10 @@ flags: {}
packages:
- https-everywhere-rules/
- https-everywhere-rules-raw/
- examples/
- location:
git: https://github.com/erikd/http-proxy.git
commit: 3f7a4013c126215e61eeb34a2372b4bd1ef77c76
extra-deps:
- functor-infix-0.0.3
- taggy-0.2.0