summaryrefslogtreecommitdiffstats
path: root/test/htest.lua
blob: c24a042b05f61155cfc3b24a85fd93139c89f0df (plain) (blame)
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
#!bin/lem
--
-- This file is part of LEM, a Lua Event Machine.
-- Copyright 2011-2012 Emil Renner Berthing
--
-- LEM is free software: you can redistribute it and/or modify it
-- under the terms of the GNU Lesser General Public License as
-- published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- LEM is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with LEM.  If not, see <http://www.gnu.org/licenses/>.
--

package.path = '?.lua'
package.cpath = '?.so'

local utils    = require 'lem.utils'
local io       = require 'lem.io'
local hathaway = require 'lem.hathaway'

hathaway.debug = print -- must be set before import()
hathaway.import()      -- when using single instance API

GET('/', function(req, res)
	print(req.client:getpeer())
	res.status = 302
	res.headers['Location'] = '/dump'
end)

GET('/hello', function(req, res)
	res.headers['Content-Type'] = 'text/plain'
	res:add('Hello, World!\n')
end)

GET('/self', function(req, res)
	res.headers['Content-Type'] = 'text/plain'
	res.file = arg[0]
end)

GET('/dump', function(req, res)
	local accept = req.headers['accept']
	if accept and accept:match('application/xhtml%+xml') then
		res.headers['Content-Type'] = 'application/xhtml+xml'
	else
		res.headers['Content-Type'] = 'text/html'
	end
	res:add([[
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Hathaway HTTP dump</title>
  <style type="text/css">
    th { text-align:left; }
  </style>
</head>
<body>

<h2>Request</h2>
<table>
  <tr><th>Method:</th><td>%s</td></tr>
  <tr><th>Uri:</th><td>%s</td></tr>
  <tr><th>Version:</th><td>%s</td></tr>
</table>

<h2>Headers</h2>
<table>
]], req.method or '', req.uri or '', req.version)

	for k, v in pairs(req.headers) do
		res:add('  <tr><th>%s</th><td>%s</td></tr>\n', k, v)
	end

	res:add([[
</table>

<h2>Body</h2>
<form action="/form" method="POST" accept-charset="UTF-8">
  <p>
    <textarea name="text" cols="80" rows="25"></textarea><br />
    <input type="submit" value="Submit" />
  </p>
</form>

<form action="/quit" method="post">
  <p>
    <input type="hidden" name="quit" value="secret" />
    <input type="submit" value="Quit" />
  </p>
</form>

</body>
</html>
]])
end)

local function urldecode(str)
	return str:gsub('+', ' '):gsub('%%(%x%x)', function (str)
		return string.char(tonumber(str, 16))
	end)
end

local function parseform(str)
	local t = {}
	for k, v in str:gmatch('([^&]+)=([^&]*)') do
		t[urldecode(k)] = urldecode(v)
	end
	return t
end

POST('/form', function(req, res)
	res.headers['Content-Type'] = 'text/plain'
	local body =req:body()
	res:add("You sent:\n%s\n", body)
	res:add('{\n')
	for k, v in pairs(parseform(body)) do
		res:add("  ['%s'] = '%s'\n", k, v)
	end
	res:add('}\n')
end)

GET('/close', function(req, res)
	res.headers['Content-Type'] = 'text/plain'
	res.headers['Connection'] = 'close'
	res:add('This connection should close\n')
end)

POST('/quit', function(req, res)
	local body = req:body()

	res.headers['Content-Type'] = 'text/plain'

	if body == 'quit=secret' then
		res:add("Bye o/\n")
		hathaway.server:close()
	else
		res:add("You didn't supply the right value...\n")
	end
end)

GETM('^/hello/([^/]+)$', function(req, res, name)
	res.headers['Content-Type'] = 'text/plain'
	res:add('Hello, %s!\n', name)
end)

if arg[1] == 'socket' then
	local sock = assert(io.unix.listen('socket', 666))
	Hathaway(sock)
else
	Hathaway('*', arg[1] or '8080')
end
utils.exit(0) -- otherwise open connections will keep us running

-- vim: syntax=lua ts=2 sw=2 noet: