-- |-- Module: Network.IHttp.Tools-- Copyright: (c) 2010 Ertugrul Soeylemez-- License: BSD3-- Maintainer: Ertugrul Soeylemez <es@ertes.de>-- Stability: beta---- Enumeratees and other tools.{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}moduleNetwork.IHttp.Tools(-- * Parser toolsparseIter,parseFull,-- * Iteratee toolsiterFinally,iterTry,-- * Printing toolsshowMethod,showVersion,-- * Character toolsasciiToUpper)whereimportControl.ContStuffimportControl.ExceptionasEximportData.Attoparsec.Char8asPimportData.ByteStringasBimportData.ByteString.Char8()importData.CharimportData.EnumeratorasEimportData.ListasLimportNetwork.IHttp.TypesimportText.Printf-- | Fast ASCII version of 'toUpper'.asciiToUpper::Char->CharasciiToUpperc|c>='a'&&c<='z'=chr$ordc-0x20|otherwise=c-- | Runs the first computation and then the second, even if the first-- one throws an exception. 'Iteratee' version of 'Ex.finally'.iterFinally::Monadm=>Iterateeamb->Iterateeamc->IterateeambiterFinallycd=domRes<-catchError(Right<$>c)(return.Left)casemResofLefterr->d>>throwErrorerrRightres->res<$d-- | Try the given 'IO' computation and turn IO exceptions into iteratee-- exceptions ('Error').iterTry::MonadIOm=>IOb->IterateeambiterTryc=domRes<-liftIO(Ex.tryc)casemResofLefterr->throwError(err::Ex.SomeException)Rightres->returnres-- | Fully parse a string with the given parser. Throw an iteratee-- error with the given error constructor, if it fails.parseIter::(Ex.Exceptionex,Monadm)=>Parserb->(String->ex)->ByteString->IterateeambparseIterparserexstr=caseparseFullparserstrofLefterr->throwError(exerr)Rightres->returnres-- | Fully parse a string with the given parser.parseFull::foralla.Parsera->ByteString->EitherStringaparseFullparserstr=loop(parseparserstr)whereloop::P.Resulta->EitherStringaloop(Fail_ctxsmsg)=Left(printf"%s (%s)"(L.intercalate": "ctxs)msg)loop(Partialk)=loop(kB.empty)loop(Done_res)=Rightres-- | Turn a method into its corresponding HTTP string.showMethod::HttpMethod->ByteStringshowMethodConnectMethod="CONNECT"showMethodDeleteMethod="DELETE"showMethodGetMethod="GET"showMethodHeadMethod="HEAD"showMethodOptionsMethod="OPTIONS"showMethodPatchMethod="PATCH"showMethodPostMethod="POST"showMethodPutMethod="PUT"showMethodTraceMethod="TRACE"showMethod(XMethodstr)=str-- | Turn an HTTP version into its corresponding HTTP string.showVersion::HttpVersion->ByteStringshowVersionHttp1_0="HTTP/1.0"showVersionHttp1_1="HTTP/1.1"