This file is indexed.

/usr/share/lua/5.1/xavante/httpd.lua is in xavante 2.3.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
-----------------------------------------------------------------------------
-- Xavante HTTP handler
--
-- Authors: Javier Guerra and Andre Carregal
-- Copyright (c) 2004-2007 Kepler Project
--
-- $Id: httpd.lua,v 1.45 2009/08/10 20:00:59 mascarenhas Exp $
-----------------------------------------------------------------------------

local string = require "string"
local table = require "table"
local os = require "os"
local io = require "io"

local socket = require "socket"
local url = require "socket.url"
local copas = require "copas"

local _M = {}

local _serversoftware = ""

local _serverports = {}

function _M.strsplit (str)
        local words = {}

        for w in string.gmatch (str, "%S+") do
                table.insert (words, w)
        end

        return words
end


-- Manages one connection, maybe several requests
-- params:
--              skt : client socket

function _M.connection (skt)
        copas.setErrorHandler (_M.errorhandler)

        skt:setoption ("tcp-nodelay", true)
        local srv, port = skt:getsockname ()
        local req = {
                rawskt = skt,
                srv = srv,
                port = port,
                copasskt = copas.wrap (skt),
        }
        req.socket = req.copasskt
        req.serversoftware = _serversoftware

        while _M.read_method (req) do
                local res
                _M.read_headers (req)

                repeat
                        req.params = nil
                        _M.parse_url (req)
                        res = _M.make_response (req)
                until _M.handle_request (req, res) ~= "reparse"
                _M.send_response (req, res)

                req.socket:flush ()
                if not res.keep_alive then
                        break
                end
        end
end


function _M.errorhandler (msg, co, skt)
    msg = tostring(msg)
        io.stderr:write("  Xavante Error: "..msg.."\n", "  "..tostring(co).."\n", "  "..tostring(skt).."\n")
        skt:send ("HTTP/1.0 200 OK\r\n")
        skt:send (string.format ("Date: %s\r\n\r\n", os.date ("!%a, %d %b %Y %H:%M:%S GMT")))
        skt:send (string.format ([[
<html><head><title>Xavante Error!</title></head>
<body>
<h1>Xavante Error!</h1>
<p>%s</p>
</body></html>
]], string.gsub (msg, "\n", "<br/>\n")))
end

-- gets and parses the request line
-- params:
--              req: request object
-- returns:
--              true if ok
--              false if connection closed
-- sets:
--              req.cmd_mth: http method
--              req.cmd_url: url requested (as sent by the client)
--              req.cmd_version: http version (usually 'HTTP/1.1')
function _M.read_method (req)
        local err
        req.cmdline, err = req.socket:receive ()

        if not req.cmdline then return nil end
        req.cmd_mth, req.cmd_url, req.cmd_version = unpack (_M.strsplit (req.cmdline))
        req.cmd_mth = string.upper (req.cmd_mth or 'GET')
        req.cmd_url = req.cmd_url or '/'

        return true
end

-- gets and parses the request header fields
-- params:
--              req: request object
-- sets:
--              req.headers: table of header fields, as name => value
function _M.read_headers (req)
        local headers = {}
        local prevval, prevname

        while 1 do
                local l,err = req.socket:receive ()
                if (not l or l == "") then
                        req.headers = headers
                        return
                end
                local _,_, name, value = string.find (l, "^([^: ]+)%s*:%s*(.+)")
                name = string.lower (name or '')
                if name then
                        prevval = headers [name]
                        if prevval then
                                value = prevval .. "," .. value
                        end
                        headers [name] = value
                        prevname = name
                elseif prevname then
                        headers [prevname] = headers [prevname] .. l
                end
        end
end

function _M.parse_url (req)
        local def_url = string.format ("http://%s%s", req.headers.host or "", req.cmd_url or "")

        req.parsed_url = url.parse (def_url or '')
        req.parsed_url.port = req.parsed_url.port or req.port
        req.built_url = url.build (req.parsed_url)

        req.relpath = url.unescape (req.parsed_url.path)
end


-- sets the default response headers
function _M.default_headers (req)
        return  {
                Date = os.date ("!%a, %d %b %Y %H:%M:%S GMT"),
                Server = _serversoftware,
        }
end

function _M.add_res_header (res, h, v)
    if string.lower(h) == "status" then
        res.statusline = "HTTP/1.1 "..v
    else
        local prevval = res.headers [h]
        if (prevval  == nil) then
            res.headers[h] = v
        elseif type (prevval) == "table" then
            table.insert (prevval, v)
        else
            res.headers[h] = {prevval, v}
        end
    end
end


-- sends the response headers
-- params:
--              res: response object
-- uses:
--              res.sent_headers : if true, headers are already sent, does nothing
--              res.statusline : response status, if nil, sends 200 OK
--              res.headers : table of header fields to send
local function send_res_headers (res)
        if (res.sent_headers) then
                return
        end

        if package.loaded["xavante.cookies"] then
          local cookies = require "xavante.cookies"
          xavante.cookies.set_res_cookies (res)
        end

        res.statusline = res.statusline or "HTTP/1.1 200 OK"

        res.socket:send (res.statusline.."\r\n")
        for name, value in pairs (res.headers) do
                if type(value) == "table" then
                  for _, value in ipairs(value) do
                    res.socket:send (string.format ("%s: %s\r\n", name, value))
                  end
                else
                  res.socket:send (string.format ("%s: %s\r\n", name, value))
                end
        end
        res.socket:send ("\r\n")

        res.sent_headers = true;
end

-- sends content directly to client
--              sends headers first, if necesary
-- params:
--              res ; response object
--              data : content data to send
local function send_res_data (res, data)

        if not res.sent_headers then
                send_res_headers (res)
        end

        if not data or data == "" then
                return
        end

        if data then
                if res.chunked then
                        res.socket:send (string.format ("%X\r\n", string.len (data)))
                        res.socket:send (data)
                        res.socket:send ("\r\n")
                else
                        res.socket:send (data)
                end
        end
end

function _M.make_response (req)
        local res = {
                req = req,
                socket = req.socket,
                headers = _M.default_headers (req),
                add_header = _M.add_res_header,
                send_headers = send_res_headers,
                send_data = send_res_data,
        }

        return res
end

-- sends prebuilt content to the client
--              if possible, sets Content-Length: header field
-- params:
--              req : request object
--              res : response object
-- uses:
--              res.content : content data to send
-- sets:
--              res.keep_alive : if possible to keep using the same connection
function _M.send_response (req, res)

        if res.content then
                if not res.sent_headers then
                        if (type (res.content) == "table" and not res.chunked) then
                                res.content = table.concat (res.content)
                        end
                        if type (res.content) == "string" then
                                res.headers["Content-Length"] = string.len (res.content)
                        end
                end
        else
                if not res.sent_headers then
                        res.statusline = "HTTP/1.1 204 No Content"
                        res.headers["Content-Length"] = 0
                end
        end

    if res.chunked then
        res:add_header ("Transfer-Encoding", "chunked")
    end

        if res.chunked or ((res.headers ["Content-Length"]) and req.headers ["connection"] == "Keep-Alive")
        then
                res.headers ["Connection"] = "Keep-Alive"
                res.keep_alive = true
        else
                res.keep_alive = nil
        end

        if res.content then
                if type (res.content) == "table" then
                        for _,v in ipairs (res.content) do res:send_data (v) end
                else
                        res:send_data (res.content)
                end
        else
                res:send_headers ()
        end

        if res.chunked then
                res.socket:send ("0\r\n\r\n")
        end
end

function _M.getparams (req)
        if not req.parsed_url.query then return nil end
        if req.params then return req.params end

        local params = {}
        req.params = params

        for parm in string.gmatch (req.parsed_url.query, "([^&]+)") do
                local k,v = string.match (parm, "(.*)=(.*)")
                k = url.unescape (k)
                v = url.unescape (v)
                if k ~= nil then
                        if params[k] == nil then
                                params[k] = v
                        elseif type (params[k]) == "table" then
                                table.insert (params[k], v)
                        else
                                params[k] = {params[k], v}
                        end
                end
        end

        return params
end

function _M.err_404 (req, res)
        res.statusline = "HTTP/1.1 404 Not Found"
        res.headers ["Content-Type"] = "text/html"
        res.content = string.format ([[
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>404 Not Found</TITLE>
</HEAD><BODY>
<H1>Not Found</H1>
The requested URL %s was not found on this server.<P>
</BODY></HTML>]], req.built_url);
        return res
end

function _M.err_403 (req, res)
        res.statusline = "HTTP/1.1 403 Forbidden"
        res.headers ["Content-Type"] = "text/html"
        res.content = string.format ([[
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>403 Forbidden</TITLE>
</HEAD><BODY>
<H1>Forbidden</H1>
You are not allowed to access the requested URL %s .<P>
</BODY></HTML>]], req.built_url);
        return res
end

function _M.err_405 (req, res)
        res.statusline = "HTTP/1.1 405 Method Not Allowed"
        res.content = string.format ([[
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>405 Method Not Allowed</TITLE>
</HEAD><BODY>
<H1>Not Found</H1>
The Method %s is not allowed for URL %s on this server.<P>
</BODY></HTML>]], req.cmd_mth, req.built_url);
        return res
end

function _M.redirect (res, d)
        res.headers ["Location"] = d
        res.statusline = "HTTP/1.1 302 Found"
        res.content = "redirect"
end

function _M.register (host, port, serversoftware)
        local _server = assert(socket.bind(host, port))
        _serversoftware = serversoftware
        local _ip, _port = _server:getsockname()
        _serverports[_port] = true
        copas.addserver(_server, _M.connection)
end

function _M.get_ports()
  local ports = {}
  for k, _ in pairs(_serverports) do
    table.insert(ports, tostring(k))
  end
  return ports
end

return _M