@@ -633,19 +633,6 @@ local function normalize_headers(hdrs)
633
633
return res
634
634
end
635
635
636
- local function parse_request (req )
637
- local p = lib ._parse_request (req )
638
- if p .error then
639
- return p
640
- end
641
- p .path = uri_unescape (p .path )
642
- if p .path :sub (1 , 1 ) ~= " /" or p .path :find (" ./" , nil , true ) ~= nil then
643
- p .error = " invalid uri"
644
- return p
645
- end
646
- return p
647
- end
648
-
649
636
local function httpd_stop (self )
650
637
if type (self ) ~= ' table' then
651
638
error (" httpd: usage: httpd:stop()" )
@@ -937,7 +924,25 @@ local function url_for_httpd(httpd, name, args, query)
937
924
end
938
925
end
939
926
940
- local function http_server_http11_handler (session )
927
+ local function httpd_http11_parse_request (session , request_raw )
928
+ local request_parsed = lib ._parse_request (request_raw )
929
+ if request_parsed .error then
930
+ return nil , request_parsed .error
931
+ end
932
+ request_parsed .path = uri_unescape (request_parsed .path )
933
+ if request_parsed .path :sub (1 , 1 ) ~= " /" or
934
+ request_parsed .path :find (" ./" , nil , true ) ~= nil then
935
+ return nil , " invalid uri"
936
+ end
937
+ request_parsed .httpd = session .server
938
+ request_parsed .s = session .socket
939
+ request_parsed .peer = session .peer
940
+ setmetatable (request_parsed , request_mt )
941
+
942
+ return request_parsed
943
+ end
944
+
945
+ local function httpd_http11_handler (session )
941
946
local hdrs = ' '
942
947
943
948
while true do
@@ -958,16 +963,12 @@ local function http_server_http11_handler(session)
958
963
end
959
964
960
965
log .debug (" request:\n %s" , hdrs )
961
- local p = parse_request ( hdrs )
962
- if p . error ~= nil then
963
- log .error (' failed to parse request: %s' , p . error )
964
- session :write (sprintf (" HTTP/1.1 400 Bad request\r\n\r\n %s" , p . error ))
965
- return false
966
+ local p , err = httpd_http11_parse_request ( session , hdrs )
967
+ if not p then
968
+ log .error (' failed to parse request: %s' , err )
969
+ session :write (sprintf (" HTTP/1.1 400 Bad request\r\n\r\n %s" , err ))
970
+ return
966
971
end
967
- p .httpd = session .server
968
- p .s = session .socket
969
- p .peer = session .peer
970
- setmetatable (p , request_mt )
971
972
972
973
if p .headers [' expect' ] == ' 100-continue' then
973
974
session :write (' HTTP/1.1 100 Continue\r\n\r\n ' )
@@ -1143,11 +1144,11 @@ local function session_new(self, socket, peer)
1143
1144
server = self ,
1144
1145
socket = socket ,
1145
1146
peer = peer ,
1146
- ctx = { proto = ' HTTP/1.1' , handler = http_server_http11_handler },
1147
+ ctx = { proto = ' HTTP/1.1' , handler = httpd_http11_handler },
1147
1148
}, session_mt )
1148
1149
end
1149
1150
1150
- local function http_server_tcp_handler (self , sckt , peer )
1151
+ local function httpd_tcp_handler (self , sckt , peer )
1151
1152
local session = session_new (self , sckt , peer )
1152
1153
1153
1154
local rv = true
@@ -1163,7 +1164,7 @@ local function httpd_start(self)
1163
1164
1164
1165
local server = assertf (socket .tcp_server (self .host , self .port , {
1165
1166
name = ' http' ,
1166
- handler = function (...) http_server_tcp_handler (self , ... ) end
1167
+ handler = function (...) httpd_tcp_handler (self , ... ) end
1167
1168
}), " Can't create tcp_server: %s" , errno .strerror ())
1168
1169
1169
1170
rawset (self , ' is_run' , true )
@@ -1173,7 +1174,7 @@ local function httpd_start(self)
1173
1174
return self
1174
1175
end
1175
1176
1176
- local http_server_methods = {
1177
+ local httpd_methods = {
1177
1178
stop = httpd_stop ,
1178
1179
start = httpd_start ,
1179
1180
route = add_route ,
@@ -1183,11 +1184,11 @@ local http_server_methods = {
1183
1184
url_for = url_for_httpd ,
1184
1185
}
1185
1186
1186
- local http_server_mt = {
1187
- __index = http_server_methods
1187
+ local httpd_mt = {
1188
+ __index = httpd_methods
1188
1189
}
1189
1190
1190
- local http_server_options_default = {
1191
+ local httpd_options_default = {
1191
1192
max_header_size = 4096 ,
1192
1193
header_timeout = 100 ,
1193
1194
handler = handler ,
@@ -1201,13 +1202,13 @@ local http_server_options_default = {
1201
1202
display_errors = true ,
1202
1203
}
1203
1204
1204
- local function http_server_new (host , port , options )
1205
+ local function httpd_new (host , port , options )
1205
1206
options = options or {}
1206
1207
local opts_tp = type (options )
1207
1208
assertf (opts_tp == ' table' , " options must be table, not '%s'" , opts_tp )
1208
1209
1209
1210
-- populate options table with default values
1210
- options = extend (table .copy (http_server_options_default ), options , true )
1211
+ options = extend (table .copy (httpd_options_default ), options , true )
1211
1212
1212
1213
local self = setmetatable ({
1213
1214
host = host ,
@@ -1222,12 +1223,12 @@ local function http_server_new(host, port, options)
1222
1223
1223
1224
-- caches
1224
1225
cache = { tpl = {}, ctx = {}, static = {}, },
1225
- }, http_server_mt )
1226
+ }, httpd_mt )
1226
1227
1227
1228
return self
1228
1229
end
1229
1230
1230
1231
return {
1231
1232
DETACHED = DETACHED ,
1232
- new = http_server_new
1233
+ new = httpd_new
1233
1234
}
0 commit comments