http.protocol: Support filehandlers for unhandled encodings
[project/luci.git] / modules / luci-base / luasrc / http / protocol.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 -- This class contains several functions useful for http message- and content
5 -- decoding and to retrive form data from raw http messages.
6 module("luci.http.protocol", package.seeall)
7
8 local ltn12 = require("luci.ltn12")
9
10 HTTP_MAX_CONTENT = 1024*8 -- 8 kB maximum content size
11
12 -- the "+" sign to " " - and return the decoded string.
13 function urldecode( str, no_plus )
14
15 local function __chrdec( hex )
16 return string.char( tonumber( hex, 16 ) )
17 end
18
19 if type(str) == "string" then
20 if not no_plus then
21 str = str:gsub( "+", " " )
22 end
23
24 str = str:gsub( "%%([a-fA-F0-9][a-fA-F0-9])", __chrdec )
25 end
26
27 return str
28 end
29
30 -- from given url or string. Returns a table with urldecoded values.
31 -- Simple parameters are stored as string values associated with the parameter
32 -- name within the table. Parameters with multiple values are stored as array
33 -- containing the corresponding values.
34 function urldecode_params( url, tbl )
35
36 local params = tbl or { }
37
38 if url:find("?") then
39 url = url:gsub( "^.+%?([^?]+)", "%1" )
40 end
41
42 for pair in url:gmatch( "[^&;]+" ) do
43
44 -- find key and value
45 local key = urldecode( pair:match("^([^=]+)") )
46 local val = urldecode( pair:match("^[^=]+=(.+)$") )
47
48 -- store
49 if type(key) == "string" and key:len() > 0 then
50 if type(val) ~= "string" then val = "" end
51
52 if not params[key] then
53 params[key] = val
54 elseif type(params[key]) ~= "table" then
55 params[key] = { params[key], val }
56 else
57 table.insert( params[key], val )
58 end
59 end
60 end
61
62 return params
63 end
64
65 function urlencode( str )
66
67 local function __chrenc( chr )
68 return string.format(
69 "%%%02x", string.byte( chr )
70 )
71 end
72
73 if type(str) == "string" then
74 str = str:gsub(
75 "([^a-zA-Z0-9$_%-%.!*'(),])",
76 __chrenc
77 )
78 end
79
80 return str
81 end
82
83 -- separated by "&". Tables are encoded as parameters with multiple values by
84 -- repeating the parameter name with each value.
85 function urlencode_params( tbl )
86 local enc = ""
87
88 for k, v in pairs(tbl) do
89 if type(v) == "table" then
90 for i, v2 in ipairs(v) do
91 enc = enc .. ( #enc > 0 and "&" or "" ) ..
92 urlencode(k) .. "=" .. urlencode(v2)
93 end
94 else
95 enc = enc .. ( #enc > 0 and "&" or "" ) ..
96 urlencode(k) .. "=" .. urlencode(v)
97 end
98 end
99
100 return enc
101 end
102
103 -- (Internal function)
104 -- Initialize given parameter and coerce string into table when the parameter
105 -- already exists.
106 local function __initval( tbl, key )
107 if tbl[key] == nil then
108 tbl[key] = ""
109 elseif type(tbl[key]) == "string" then
110 tbl[key] = { tbl[key], "" }
111 else
112 table.insert( tbl[key], "" )
113 end
114 end
115
116 -- (Internal function)
117 -- Append given data to given parameter, either by extending the string value
118 -- or by appending it to the last string in the parameter's value table.
119 local function __appendval( tbl, key, chunk )
120 if type(tbl[key]) == "table" then
121 tbl[key][#tbl[key]] = tbl[key][#tbl[key]] .. chunk
122 else
123 tbl[key] = tbl[key] .. chunk
124 end
125 end
126
127 -- (Internal function)
128 -- Finish the value of given parameter, either by transforming the string value
129 -- or - in the case of multi value parameters - the last element in the
130 -- associated values table.
131 local function __finishval( tbl, key, handler )
132 if handler then
133 if type(tbl[key]) == "table" then
134 tbl[key][#tbl[key]] = handler( tbl[key][#tbl[key]] )
135 else
136 tbl[key] = handler( tbl[key] )
137 end
138 end
139 end
140
141
142 -- Table of our process states
143 local process_states = { }
144
145 -- Extract "magic", the first line of a http message.
146 -- Extracts the message type ("get", "post" or "response"), the requested uri
147 -- or the status code if the line descripes a http response.
148 process_states['magic'] = function( msg, chunk, err )
149
150 if chunk ~= nil then
151 -- ignore empty lines before request
152 if #chunk == 0 then
153 return true, nil
154 end
155
156 -- Is it a request?
157 local method, uri, http_ver = chunk:match("^([A-Z]+) ([^ ]+) HTTP/([01]%.[019])$")
158
159 -- Yup, it is
160 if method then
161
162 msg.type = "request"
163 msg.request_method = method:lower()
164 msg.request_uri = uri
165 msg.http_version = tonumber( http_ver )
166 msg.headers = { }
167
168 -- We're done, next state is header parsing
169 return true, function( chunk )
170 return process_states['headers']( msg, chunk )
171 end
172
173 -- Is it a response?
174 else
175
176 local http_ver, code, message = chunk:match("^HTTP/([01]%.[019]) ([0-9]+) ([^\r\n]+)$")
177
178 -- Is a response
179 if code then
180
181 msg.type = "response"
182 msg.status_code = code
183 msg.status_message = message
184 msg.http_version = tonumber( http_ver )
185 msg.headers = { }
186
187 -- We're done, next state is header parsing
188 return true, function( chunk )
189 return process_states['headers']( msg, chunk )
190 end
191 end
192 end
193 end
194
195 -- Can't handle it
196 return nil, "Invalid HTTP message magic"
197 end
198
199
200 -- Extract headers from given string.
201 process_states['headers'] = function( msg, chunk )
202
203 if chunk ~= nil then
204
205 -- Look for a valid header format
206 local hdr, val = chunk:match( "^([A-Za-z][A-Za-z0-9%-_]+): +(.+)$" )
207
208 if type(hdr) == "string" and hdr:len() > 0 and
209 type(val) == "string" and val:len() > 0
210 then
211 msg.headers[hdr] = val
212
213 -- Valid header line, proceed
214 return true, nil
215
216 elseif #chunk == 0 then
217 -- Empty line, we won't accept data anymore
218 return false, nil
219 else
220 -- Junk data
221 return nil, "Invalid HTTP header received"
222 end
223 else
224 return nil, "Unexpected EOF"
225 end
226 end
227
228
229 -- data line by line with the trailing \r\n stripped of.
230 function header_source( sock )
231 return ltn12.source.simplify( function()
232
233 local chunk, err, part = sock:receive("*l")
234
235 -- Line too long
236 if chunk == nil then
237 if err ~= "timeout" then
238 return nil, part
239 and "Line exceeds maximum allowed length"
240 or "Unexpected EOF"
241 else
242 return nil, err
243 end
244
245 -- Line ok
246 elseif chunk ~= nil then
247
248 -- Strip trailing CR
249 chunk = chunk:gsub("\r$","")
250
251 return chunk, nil
252 end
253 end )
254 end
255
256 -- Content-Type. Stores all extracted data associated with its parameter name
257 -- in the params table withing the given message object. Multiple parameter
258 -- values are stored as tables, ordinary ones as strings.
259 -- If an optional file callback function is given then it is feeded with the
260 -- file contents chunk by chunk and only the extracted file name is stored
261 -- within the params table. The callback function will be called subsequently
262 -- with three arguments:
263 -- o Table containing decoded (name, file) and raw (headers) mime header data
264 -- o String value containing a chunk of the file data
265 -- o Boolean which indicates wheather the current chunk is the last one (eof)
266 function mimedecode_message_body( src, msg, filecb )
267
268 if msg and msg.env.CONTENT_TYPE then
269 msg.mime_boundary = msg.env.CONTENT_TYPE:match("^multipart/form%-data; boundary=(.+)$")
270 end
271
272 if not msg.mime_boundary then
273 return nil, "Invalid Content-Type found"
274 end
275
276
277 local tlen = 0
278 local inhdr = false
279 local field = nil
280 local store = nil
281 local lchunk = nil
282
283 local function parse_headers( chunk, field )
284
285 local stat
286 repeat
287 chunk, stat = chunk:gsub(
288 "^([A-Z][A-Za-z0-9%-_]+): +([^\r\n]+)\r\n",
289 function(k,v)
290 field.headers[k] = v
291 return ""
292 end
293 )
294 until stat == 0
295
296 chunk, stat = chunk:gsub("^\r\n","")
297
298 -- End of headers
299 if stat > 0 then
300 if field.headers["Content-Disposition"] then
301 if field.headers["Content-Disposition"]:match("^form%-data; ") then
302 field.name = field.headers["Content-Disposition"]:match('name="(.-)"')
303 field.file = field.headers["Content-Disposition"]:match('filename="(.+)"$')
304 end
305 end
306
307 if not field.headers["Content-Type"] then
308 field.headers["Content-Type"] = "text/plain"
309 end
310
311 if field.name and field.file and filecb then
312 __initval( msg.params, field.name )
313 __appendval( msg.params, field.name, field.file )
314
315 store = filecb
316 elseif field.name then
317 __initval( msg.params, field.name )
318
319 store = function( hdr, buf, eof )
320 __appendval( msg.params, field.name, buf )
321 end
322 else
323 store = nil
324 end
325
326 return chunk, true
327 end
328
329 return chunk, false
330 end
331
332 local function snk( chunk )
333
334 tlen = tlen + ( chunk and #chunk or 0 )
335
336 if msg.env.CONTENT_LENGTH and tlen > tonumber(msg.env.CONTENT_LENGTH) + 2 then
337 return nil, "Message body size exceeds Content-Length"
338 end
339
340 if chunk and not lchunk then
341 lchunk = "\r\n" .. chunk
342
343 elseif lchunk then
344 local data = lchunk .. ( chunk or "" )
345 local spos, epos, found
346
347 repeat
348 spos, epos = data:find( "\r\n--" .. msg.mime_boundary .. "\r\n", 1, true )
349
350 if not spos then
351 spos, epos = data:find( "\r\n--" .. msg.mime_boundary .. "--\r\n", 1, true )
352 end
353
354
355 if spos then
356 local predata = data:sub( 1, spos - 1 )
357
358 if inhdr then
359 predata, eof = parse_headers( predata, field )
360
361 if not eof then
362 return nil, "Invalid MIME section header"
363 elseif not field.name then
364 return nil, "Invalid Content-Disposition header"
365 end
366 end
367
368 if store then
369 store( field, predata, true )
370 end
371
372
373 field = { headers = { } }
374 found = found or true
375
376 data, eof = parse_headers( data:sub( epos + 1, #data ), field )
377 inhdr = not eof
378 end
379 until not spos
380
381 if found then
382 -- We found at least some boundary. Save
383 -- the unparsed remaining data for the
384 -- next chunk.
385 lchunk, data = data, nil
386 else
387 -- There was a complete chunk without a boundary. Parse it as headers or
388 -- append it as data, depending on our current state.
389 if inhdr then
390 lchunk, eof = parse_headers( data, field )
391 inhdr = not eof
392 else
393 -- We're inside data, so append the data. Note that we only append
394 -- lchunk, not all of data, since there is a chance that chunk
395 -- contains half a boundary. Assuming that each chunk is at least the
396 -- boundary in size, this should prevent problems
397 store( field, lchunk, false )
398 lchunk, chunk = chunk, nil
399 end
400 end
401 end
402
403 return true
404 end
405
406 return ltn12.pump.all( src, snk )
407 end
408
409 -- Content-Type. Stores all extracted data associated with its parameter name
410 -- in the params table withing the given message object. Multiple parameter
411 -- values are stored as tables, ordinary ones as strings.
412 function urldecode_message_body( src, msg )
413
414 local tlen = 0
415 local lchunk = nil
416
417 local function snk( chunk )
418
419 tlen = tlen + ( chunk and #chunk or 0 )
420
421 if msg.env.CONTENT_LENGTH and tlen > tonumber(msg.env.CONTENT_LENGTH) + 2 then
422 return nil, "Message body size exceeds Content-Length"
423 elseif tlen > HTTP_MAX_CONTENT then
424 return nil, "Message body size exceeds maximum allowed length"
425 end
426
427 if not lchunk and chunk then
428 lchunk = chunk
429
430 elseif lchunk then
431 local data = lchunk .. ( chunk or "&" )
432 local spos, epos
433
434 repeat
435 spos, epos = data:find("^.-[;&]")
436
437 if spos then
438 local pair = data:sub( spos, epos - 1 )
439 local key = pair:match("^(.-)=")
440 local val = pair:match("=([^%s]*)%s*$")
441
442 if key and #key > 0 then
443 __initval( msg.params, key )
444 __appendval( msg.params, key, val )
445 __finishval( msg.params, key, urldecode )
446 end
447
448 data = data:sub( epos + 1, #data )
449 end
450 until not spos
451
452 lchunk = data
453 end
454
455 return true
456 end
457
458 return ltn12.pump.all( src, snk )
459 end
460
461 -- version, message headers and resulting CGI environment variables from the
462 -- given ltn12 source.
463 function parse_message_header( src )
464
465 local ok = true
466 local msg = { }
467
468 local sink = ltn12.sink.simplify(
469 function( chunk )
470 return process_states['magic']( msg, chunk )
471 end
472 )
473
474 -- Pump input data...
475 while ok do
476
477 -- get data
478 ok, err = ltn12.pump.step( src, sink )
479
480 -- error
481 if not ok and err then
482 return nil, err
483
484 -- eof
485 elseif not ok then
486
487 -- Process get parameters
488 if ( msg.request_method == "get" or msg.request_method == "post" ) and
489 msg.request_uri:match("?")
490 then
491 msg.params = urldecode_params( msg.request_uri )
492 else
493 msg.params = { }
494 end
495
496 -- Populate common environment variables
497 msg.env = {
498 CONTENT_LENGTH = msg.headers['Content-Length'];
499 CONTENT_TYPE = msg.headers['Content-Type'] or msg.headers['Content-type'];
500 REQUEST_METHOD = msg.request_method:upper();
501 REQUEST_URI = msg.request_uri;
502 SCRIPT_NAME = msg.request_uri:gsub("?.+$","");
503 SCRIPT_FILENAME = ""; -- XXX implement me
504 SERVER_PROTOCOL = "HTTP/" .. string.format("%.1f", msg.http_version);
505 QUERY_STRING = msg.request_uri:match("?")
506 and msg.request_uri:gsub("^.+?","") or ""
507 }
508
509 -- Populate HTTP_* environment variables
510 for i, hdr in ipairs( {
511 'Accept',
512 'Accept-Charset',
513 'Accept-Encoding',
514 'Accept-Language',
515 'Connection',
516 'Cookie',
517 'Host',
518 'Referer',
519 'User-Agent',
520 } ) do
521 local var = 'HTTP_' .. hdr:upper():gsub("%-","_")
522 local val = msg.headers[hdr]
523
524 msg.env[var] = val
525 end
526 end
527 end
528
529 return msg
530 end
531
532 -- This function will examine the Content-Type within the given message object
533 -- to select the appropriate content decoder.
534 -- Currently the application/x-www-urlencoded and application/form-data
535 -- mime types are supported. If the encountered content encoding can't be
536 -- handled then the whole message body will be stored unaltered as "content"
537 -- property within the given message object.
538 function parse_message_body( src, msg, filecb )
539 -- Is it multipart/mime ?
540 if msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE and
541 msg.env.CONTENT_TYPE:match("^multipart/form%-data")
542 then
543
544 return mimedecode_message_body( src, msg, filecb )
545
546 -- Is it application/x-www-form-urlencoded ?
547 elseif msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE and
548 msg.env.CONTENT_TYPE:match("^application/x%-www%-form%-urlencoded")
549 then
550 return urldecode_message_body( src, msg, filecb )
551
552
553 -- Unhandled encoding
554 -- If a file callback is given then feed it chunk by chunk, else
555 -- store whole buffer in message.content
556 else
557
558 local sink
559
560 -- If we have a file callback then feed it
561 if type(filecb) == "function" then
562 local meta = {
563 name = "raw",
564 encoding = msg.env.CONTENT_TYPE
565 }
566 sink = function( chunk )
567 if chunk then
568 return filecb(meta, chunk, false)
569 else
570 return filecb(meta, nil, true)
571 end
572 end
573 -- ... else append to .content
574 else
575 msg.content = ""
576 msg.content_length = 0
577
578 sink = function( chunk )
579 if chunk then
580 if ( msg.content_length + #chunk ) <= HTTP_MAX_CONTENT then
581 msg.content = msg.content .. chunk
582 msg.content_length = msg.content_length + #chunk
583 return true
584 else
585 return nil, "POST data exceeds maximum allowed length"
586 end
587 end
588 return true
589 end
590 end
591
592 -- Pump data...
593 while true do
594 local ok, err = ltn12.pump.step( src, sink )
595
596 if not ok and err then
597 return nil, err
598 elseif not ok then -- eof
599 return true
600 end
601 end
602
603 return true
604 end
605 end
606
607 statusmsg = {
608 [200] = "OK",
609 [206] = "Partial Content",
610 [301] = "Moved Permanently",
611 [302] = "Found",
612 [304] = "Not Modified",
613 [400] = "Bad Request",
614 [403] = "Forbidden",
615 [404] = "Not Found",
616 [405] = "Method Not Allowed",
617 [408] = "Request Time-out",
618 [411] = "Length Required",
619 [412] = "Precondition Failed",
620 [416] = "Requested range not satisfiable",
621 [500] = "Internal Server Error",
622 [503] = "Server Unavailable",
623 }