on develop help sign in
Added support for getting all response headers for XHR
commit1ebc8bd3dcfc978d2d6ce1ff4a9e16fca8afd86b authorMagesh <magesh85@gmail.com> dateTue 19 Jul 2016, 18:17 (10 years ago) parente8b7d31
1 file changed+12
src/Reflex/Dom/Xhr.hs+12
src/Reflex/Dom/Xhr.hs+12
@@ -50,6 +50,9 @@ import Data.Map (Map)
5050import qualified Data.Map as Map
5151import Data.Maybe
5252import Data.Text (Text)
53+import qualified Data.Text as T
54+import GHCJS.DOM.XMLHttpRequest (getAllResponseHeaders)
55+import Data.List
5356import Data.Text.Encoding
5457import Data.Traversable
5558import Reflex
@@ -81,6 +84,7 @@ data XhrResponse
8184 , _xhrResponse_statusText :: Text
8285 , _xhrResponse_response :: Maybe XhrResponseBody
8386 , _xhrResponse_responseText :: Maybe Text
87+ , _xhrResponse_headers :: Map Text Text
8488 }
8589 deriving (Typeable)
8690
@@ -146,11 +150,19 @@ newXMLHttpRequestWithError req cb = do
146150 then liftIO $ xmlHttpRequestGetResponseText xhr
147151 else return Nothing
148152 r <- liftIO $ xmlHttpRequestGetResponse xhr
153+ hdrsTxt <- liftIO $ xmlHttpRequestGetAllResponseHeaders xhr
154+ let stripBoth (txt1, txt2) = (T.strip txt1, T.strip $ T.drop 1 txt2)
155+ -- XMLHttpRequest return all the response headers separated by CRLF
156+ let hdrs = Map.fromList
157+ $ fmap (stripBoth . (T.span (/=':')))
158+ $ dropWhileEnd T.null
159+ $ T.splitOn (T.pack "\r\n") hdrsTxt
149160 _ <- liftIO $ postGui $ cb $ Right $
150161 XhrResponse { _xhrResponse_status = status
151162 , _xhrResponse_statusText = statusText
152163 , _xhrResponse_response = r
153164 , _xhrResponse_responseText = t
165+ , _xhrResponse_headers = hdrs
154166 }
155167 return ()
156168 else return ()