3 HTTP server implementation for LuCI - helper class
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.httpd.server", package.seeall)
23 function error400( client, msg )
24 client:send( "HTTP/1.0 400 Bad request\r\n" )
25 client:send( "Content-Type: text/plain\r\n\r\n" )
28 client:send( msg .. "\r\n" )
34 function error503( client )
35 client:send( "HTTP/1.0 503 Server unavailable\r\n" )
36 client:send( "Content-Type: text/plain\r\n\r\n" )
37 client:send( "There are too many clients connected, try again later\r\n" )
42 function client_handler(client)
44 client:settimeout( 0 )
46 -- Create LTN12 block source
47 local block_source = function()
51 local chunk, err, part = client:receive( READ_BUFSIZE )
53 if chunk == nil and err == "timeout" then
55 elseif chunk ~= nil then
63 -- Create LTN12 line source
64 local line_source = ltn12.source.simplify( function()
68 local chunk, err, part = client:receive("*l")
71 if chunk == nil and err ~= "timeout" then
74 and "Line exceeds maximum allowed length["..part.."]"
81 chunk = chunk:gsub("\r$","")
83 -- We got end of headers, switch to dummy source
94 coroutine.yield(client)
98 local message, err = luci.http.protocol.parse_message_header( line_source )
101 local s, e = luci.http.protocol.parse_message_body( block_source, message )
104 luci.util.dumptable( message )
107 error400( client, e )
110 error400( client, err )
114 error400( client, "Dummy response" )