Skip to content

Commit

Permalink
Implement Chunked Encoding Parsing (Fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
uwap committed Nov 22, 2016
1 parent 09d7bd0 commit a890f1f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
![Travis Status](https://travis-ci.org/uwap/idris-http.svg "Travis Build Status")

# idris-http
An HTTP library for idris 0.11.2.
An HTTP library for idris 0.12.3.

## How to install

Clone the repository and install it using the idris package manager.
Idris-http depends on [lightyear](http://github.com/ziman/lightyear/).
Idris-http depends on [lightyear](http://github.com/ziman/lightyear/) and [idris-bytes](https://github.com/ziman/idris-bytes).
You can install it with our install-dependencies script.

The easiest way to install idris-http is to run the command line code below:
Expand Down
2 changes: 1 addition & 1 deletion http.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package http

modules = Http, Http.Uri, Http.RawResponse, Http.Request, Http.Response, Http.Error
sourcedir = src
opts = "-p contrib -p lightyear"
opts = "-p contrib -p lightyear -p bytes"
2 changes: 1 addition & 1 deletion install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

dependencies=("https://github.com/ziman/lightyear")
dependencies=("https://github.com/ziman/lightyear" "https://github.com/ziman/idris-bytes")
wd=$(pwd)

function checkSuccess {
Expand Down
39 changes: 38 additions & 1 deletion src/Http/Response.idr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Http.Request
import Http.RawResponse
import Http.Error

import Data.Bytes as B

%access export

public export
Expand Down Expand Up @@ -54,9 +56,44 @@ headerFieldParser = do
opt (char ' ') >! crlf
pure (pack (toLower <$> key), pack value)

private
parseBodyChunkEncoded : Parser String
parseBodyChunkEncoded = do
x <- hexParser
opt $ do
spaces
char ';'
some (noneOf "\n\r")
crlf
if x == 0 then many (noneOf "\n\r") *> crlf *> pure ""
else liftA2 (++) (parseBody x) parseBodyChunkEncoded
where
hexParser' : Int -> Parser Int
hexParser' x = do
c <- hexDigit
hex2 <- opt $ hexParser' (x*16)
let hex = ord $ toUpper c
let num = if hex >= ord '0' && hex <= ord '9'
then hex - ord '0'
else 10 + hex - ord 'A'
case hex2 of
Just x => pure (x * num)
Nothing => pure num
hexParser : Parser Int
hexParser = hexParser' 1
parseBody : Int -> Parser String
parseBody x = do
n <- pack <$> many (oneOf "\n\r")
s <- pack <$> some (noneOf "\n\r")
let len = B.length (B.fromString (s ++ n))
if x - len < 0 then fail ("Somehow the String " ++ s ++ n ++ " is longer than " ++ show x)
else if x - len == 0 then crlf *> pure s
else map (s ++) $ parseBody (x - len)

private
bodyParser : SortedMap String String -> Parser String
bodyParser map with (lookup "content-length" map)
| Nothing = pure "error: Chunked encoding not supported yet."
| Nothing = parseBodyChunkEncoded
| Just x = pack <$> ntimes (cast $ the Int (cast x)) anyChar

private
Expand Down

0 comments on commit a890f1f

Please sign in to comment.