[LEDE-DEV] Luci's HTTP API

Karl Palsson karlp at tweak.net.au
Fri Sep 8 03:15:00 PDT 2017


Here's a snippet I've used to accept HTTP POST of a file to in
luci...

http = require("luci.http")


    local method = http.getenv("REQUEST_METHOD")
    local pathinfo = http.getenv("PATH_INFO") -- from luci


--- if method == post and path info == allowed for your requirements.

        http.setfilehandler(
            function(meta, chunk, eof)
                -- If this was multipart form, make sure to only accept the documented form field.
                -- return true to continue with subsequent chunks.
                if meta and (meta.name ~= "raw") then return true end
                if not file then
                    -- Don't trample a file if we don't get any data.
                    if not chunk then return false end
                    file = io.open(file_path, "w")
                end
                if chunk then
                    file:write(chunk)
                end
                if eof then
                    file:close()
                end
                return true
            end
        )
        -- trigger handler!
        http.content()


Cheers,
Karl P


Levente <leventelist at gmail.com> wrote:
> Dear LEDE developers,
> 
> 
> Maybe this is off topic, but I'm trying to use LUCI's HTTP
> protocol parser library for to upload a file to the system. I
> find very little documentation on this.
> 
> I have the following code that obviously does nothing. Could
> you help me add the missing parts?
> 
> 
> http = require "luci.http"
> proto = require "luci.http.protocol"
> 
> 
> local fileOpened = 0
> local fileP
> 
> function filecb(t, data, isLast)
> 
>     if fileOpened == 0 then
>         FileP = io.open("/tmp/blabla", "wb")
>         fileOpened = 1
>     end
> 
>     FileP:write(data)
> 
>     if isLast then
>         io.close(FileP)
>     end
> 
> end
> 
> 
> src = http.source()
> httpMsg = proto.parse_message_header(src)
> 
> proto.parse_message_body(src, httpMsg, filecb)
> 
> http.close()
> 
> 
> So I experience that http.source() is not even returns. It dies
> with this error:
> 
> /usr/bin/lua: /usr/lib/lua/luci/http.lua:193: attempt to index
> field 'request' (a nil value) stack traceback:
>     /usr/lib/lua/luci/http.lua:193: in function 'source'
>     ./config_v2.sh:27: in main chunk
>     [C]: ?
> 
> Browsing the source code this call shall return
> 
> context.request.input
> 
> So the question is how to initialize that?
> 
> 
> Thank you,
> Levente
> 
> _______________________________________________
> Lede-dev mailing list
> Lede-dev at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/lede-dev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.html
Type: application/pgp-signature
Size: 1161 bytes
Desc: OpenPGP Digital Signature
URL: <http://lists.infradead.org/pipermail/lede-dev/attachments/20170908/95683096/attachment.sig>


More information about the Lede-dev mailing list