redirect :: Word16 -> W.Application
redirect p req resp =
  case requestDomain req of
    Nothing -> badRequest "missing host" req resp
    Just domain -> do
      Log.info $ "redirecting to " ++ show location ++ ": " ++ showReq req
      resp $ W.responseBuilder status [(hLocation, location)] mempty
      where status =
              if W.requestMethod req == methodGet
                then movedPermanently301
                else temporaryRedirect307
            newhost =
              if p == 443
                then domain
                else domain <> ":" <> pack (show p)
            location =
              "https://" <> newhost <> W.rawPathInfo req <> W.rawQueryString req
redirect :: Word16 -> W.Application
redirect p req resp =
  case requestDomain req of
    Nothing -> badRequest "missing host" req resp
    Just domain -> do
      Log.info $ "redirecting to " ++ show location ++ ": " ++ showReq req
      resp $ W.responseBuilder status [(hLocation, location)] mempty
      where status =
              if W.requestMethod req == methodGet
                then movedPermanently301
                else temporaryRedirect307
            newhost =
              if p == 443
                then domain
                else domain <> ":" <> pack (show p)
            location =
              "https://" <> newhost <> W.rawPathInfo req <> W.rawQueryString req
redirect p req resp =
  case requestDomain req of
    Nothing -> badRequest "missing host" req resp
    Just domain -> do
      Log.info $ "redirecting to " ++ show location ++ ": " ++ showReq req
      resp $ W.responseBuilder status [(hLocation, location)] mempty
      where status =
              if W.requestMethod req == methodGet
                then movedPermanently301
                else temporaryRedirect307
            newhost =
              if p == 443
                then domain
                else domain <> ":" <> pack (show p)
            location =
              "https://" <> newhost <> W.rawPathInfo req <> W.rawQueryString req