3 HTTP protocol implementation for LuCI - filter implementation
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.http.protocol.filter", package.seeall)
18 local ltn12 = require("luci.ltn12")
21 -- Factory that produces a filter which normalizes chunked transfer encoding
22 function decode_chunked()
27 return ltn12.filter.cycle(
28 function( chunk, ctx )
34 if ( length - read ) > 0 then
35 return nil, "Unexpected EOF"
46 if read == length then
48 -- Find chunk length indicator
49 local spos, epos = chunk:find("^\r?\n?[a-fA-F0-9]+ *\r\n")
50 if spos and spos == 1 then
53 chunk:sub( 1, epos ):gsub( "[^a-fA-F0-9]", "" ), 16
56 -- Check for end of chunk
58 chunk = chunk:sub( epos + 1, #chunk )
66 if ( read + #chunk ) <= length then
68 return buf .. chunk, ""
70 local rest = length - read
72 buf = buf .. chunk:sub( 1, rest )
73 chunk = chunk:sub( rest + 1, #chunk )