1

In my Servant/Wai app I want to redict all the requests from "domain.com" to "www.domain.com"

{-# LANGUAGE OverloadedStrings #-}
--.......

app :: Application
app req respond = do
  case requestHeaderHost req of
    Just host -> do
      case BS.unpack host of
        "www":rest -> respond =<< redirect' HttpTp.status302 [] "domain.com"

      _ -> undefined

    Nothing -> undefined

The error is

No instance for (Data.String.IsString GHC.Word.Word8)
  arising from the literal ‘"www"’
In the pattern: "www"

I know what it means and I think that the class Show should've have been implemented for Word8 and if not there must be a reason. Maybe I'm doing it the wrong way?

How can I fix this or do it another better way?

Update:

I can't get it to compile:

-- 1
Just host -> do
  case BS.isPrefixOf (BS.pack $ show "www") host of 

-- 2
Just host -> do
  case Text.isPrefixOf (Text.pack $ show "www") host of 

-- 3
Just host -> do
  case DL.isPrefixOf  "www" host of 

There's always type mismatch.

Sojo
  • 5,455
  • 3
  • 10
  • 11
  • Maybe this will help (if it does, keep in mind that pasting error messages into Google search can produce wonders): http://stackoverflow.com/questions/3753445/haskell-a-problem-with-the-simplest-wai-example – Kapol Apr 25 '16 at 09:52

1 Answers1

4

The pattern "www":rest implies the type [[Char]], while you need [Char]. Here's what your pattern should be:

'w':'w':'w':rest

Oh. And you should use Data.ByteString.Char8.unpack (or Data.ByteString.Lazy.Char8.unpack, if it's lazy) to be able to match against characters. Otherwise you need to use the ASCII code of 'w' instead of the character.

Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
  • @user6250601 Seems like your ByteString is strict. Use `Data.ByteString.Char8.unpack` instead of `Data.ByteString.Lazy.Char8.unpack`. – Nikita Volkov Apr 25 '16 at 11:13
  • @user6250601 Pay a close attention to the module names and types. The types `Data.ByteString.ByteString` and `Data.ByteString.Lazy.ByteString` are different! Your errors are caused by you using the `Data.ByteString.Lazy` API against a strict bytestring, while only `Data.ByteString` and `Data.ByteString.Char8` are applicable to it. – Nikita Volkov Apr 25 '16 at 12:43