1- -- Copyright (C) 2012 Zhang "agentzh" Yichun (章亦春 )
1+ -- Copyright (C) 2012 Yichun Zhang (agentzh )
22
3- module (" resty.upload" , package.seeall )
3+
4+ local sub = string.sub
5+ local req_socket = ngx .req .socket
6+ local insert = table.insert
7+ local len = string.len
8+ local null = ngx .null
9+ local match = string.match
10+ local setmetatable = setmetatable
11+ local error = error
12+ local get_headers = ngx .req .get_headers
13+
14+
15+ module (... )
416
517_VERSION = ' 0.03'
618
19+
720local MAX_LINE_SIZE = 512
821
922local STATE_BEGIN = 1
1023local STATE_READING_HEADER = 2
1124local STATE_READING_BODY = 3
1225local STATE_EOF = 4
1326
14- local class = resty .upload
1527
16- local mt = { __index = class }
28+ local mt = { __index = _M }
1729
18- local sub = string.sub
19- local req_socket = ngx .req .socket
20- local insert = table.insert
21- local len = string.len
22- local null = ngx .null
2330local state_handlers
2431
32+
2533function new (self , chunk_size )
2634 local boundary = get_boundary ()
2735 if not boundary then
@@ -180,7 +188,7 @@ function read_header(self)
180188 return read_body_part (self )
181189 end
182190
183- local key , value = string. match (line , " ([^: \t ]+)%s*:%s*(.+)" )
191+ local key , value = match (line , " ([^: \t ]+)%s*:%s*(.+)" )
184192 if not key then
185193 return ' header' , line
186194 end
@@ -232,12 +240,12 @@ end
232240
233241
234242function get_boundary ()
235- local header = ngx . var .content_type
243+ local header = get_headers () .content_type
236244 if not header then
237245 return nil
238246 end
239247
240- return string. match (header , " ;%s+boundary=(%S+)" )
248+ return match (header , " ;%s+boundary=(%S+)" )
241249end
242250
243251
@@ -249,9 +257,12 @@ state_handlers = {
249257}
250258
251259
252- -- to prevent use of casual module global variables
253- getmetatable (class ).__newindex = function (table , key , val )
254- error (' attempt to write to undeclared variable "' .. key .. ' ": '
255- .. debug.traceback ())
256- end
260+ local class_mt = {
261+ -- to prevent use of casual module global variables
262+ __newindex = function (table , key , val )
263+ error (' attempt to write to undeclared variable "' .. key .. ' "' )
264+ end
265+ }
266+
267+ setmetatable (_M , class_mt )
257268
0 commit comments