summaryrefslogtreecommitdiffstats
path: root/lem/hathaway.lua
blob: ca43ab37a92fe6b2b496e40ae28fb5646856ebad (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
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
--
-- This file is part of LEM, a Lua Event Machine.
-- Copyright 2011-2013 Emil Renner Berthing
-- Copyright 2012 Asbjørn Sloth Tønnesen
--
-- 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/>.
--

local format = string.format

local httpserv = require 'lem.http.server'
local httpresp = require 'lem.http.response'

local M = {}

function M.debug() end

do
	local lookup = {}
	M.lookup = lookup

	function M.GET(path, handler)
		local entry = lookup[path]
		if entry then
			entry['HEAD'] = handler
			entry['GET'] = handler
		else
			entry = {
				['HEAD'] = handler,
				['GET'] = handler,
			}
			lookup[path] = entry
		end
	end

	do
		local function static_setter(method)
			return function(path, handler)
				local entry = lookup[path]
				if entry then
					entry[method] = handler
				else
					lookup[path] = { [method] = handler }
				end
			end
		end

		M.POST    = static_setter('POST')
		M.PUT     = static_setter('PUT')
		M.DELETE  = static_setter('DELETE')
		M.OPTIONS = static_setter('OPTIONS')
	end

	function M.GETM(pattern, handler)
		local i = 1
		while true do
			local entry = lookup[i]
			if entry == nil then
				lookup[i] = { pattern,
					['GET'] = handler,
					['HEAD'] = handler
				}
				break
			end
			if entry[1] == pattern then
				entry['GET'] = handler
				entry['HEAD'] = handler
				break
			end
			i = i + 1
		end
	end

	do
		local function match_setter(method)
			return function(pattern, handler)
				local i = 1
				while true do
					local entry = lookup[i]
					if entry == nil then
						lookup[i] = { pattern, [method] = handler }
						break
					end
					if entry[1] == pattern then
						entry[method] = handler
						break
					end
					i = i + 1
				end
			end
		end

		M.POSTM    = match_setter('POST')
		M.PUTM     = match_setter('PUT')
		M.DELETEM  = match_setter('DELETE')
		M.OPTIONSM = match_setter('OPTIONS')
	end

	local function check_match(entry, req, res, ok, ...)
		if not ok then return false end
		local handler = entry[req.method]
		if handler then
			handler(req, res, ok, ...)
		else
			httpresp.method_not_allowed(req, res)
		end
		return true
	end

	local function handler(req, res)
		local method, path = req.method, req.path
		M.debug('info', format("%s %s HTTP/%s", method, req.uri, req.version))
		local entry = lookup[path]
		if entry then
			local handler = entry[method]
			if handler then
				handler(req, res)
			else
				httpresp.method_not_allowed(req, res)
			end
		else
			local i = 0
			repeat
				i = i + 1
				local entry = lookup[i]
				if not entry then
					httpresp.not_found(req, res)
					break
				end
			until check_match(entry, req, res, path:match(entry[1]))
		end
	end

	function M.Hathaway(host, port)
		local server, err
		if port then
			server, err = httpserv.new(host, port, handler)
		else
			server, err = httpserv.new(host, handler)
		end
		if not server then M.debug('new', err) return nil, err end

		M.server = server
		server.debug = M.debug

		local ok, err = server:run()
		if not ok and err ~= 'interrupted' then
			M.debug('run', err)
			return nil, err
		end
		return true
	end
end

function M.import(env)
	if not env then
		env = _G
	end

	env.GET      = M.GET
	env.POST     = M.POST
	env.PUT      = M.PUT
	env.DELETE   = M.DELETE
	env.OPTIONS  = M.OPTIONS
	env.GETM     = M.GETM
	env.POSTM    = M.POSTM
	env.PUTM     = M.PUTM
	env.DELETEM  = M.DELETEM
	env.OPTIONSM = M.OPTIONSM
	env.Hathaway = M.Hathaway
end

return M

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