Mercurial Hosting > luan
comparison http/src/luan/modules/http/Http.luan @ 497:55f9f74f1e55
Http.request is now pure luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 17 May 2015 19:25:47 -0600 |
parents | 598123096772 |
children | ee55be414a34 |
comparison
equal
deleted
inserted
replaced
496:c65df5b25932 | 497:55f9f74f1e55 |
---|---|
1 local Luan = require "luan:Luan" | |
2 local ipairs = Luan.ipairs | |
3 local pairs = Luan.pairs | |
4 local set_metatable = Luan.set_metatable | |
1 local Io = require "luan:Io" | 5 local Io = require "luan:Io" |
6 local Html = require "luan:Html" | |
7 local url_encode = Html.url_encode | |
2 | 8 |
9 | |
10 local singular_metatable = {} | |
11 | |
12 function singular_metatable.__index(table,key) | |
13 local list = table.__plural[key] | |
14 return list and list[1] | |
15 end | |
16 | |
17 function singular_metatable.__new_index(table,key,value) | |
18 table.__plural[key] = {value} | |
19 end | |
20 | |
21 function singular_metatable.__pairs(table) | |
22 local iter = pairs(table.__plural) | |
23 return function() | |
24 local key, value = iter() | |
25 return key, value and value[1] | |
26 end | |
27 end | |
28 | |
29 | |
30 local function new_common(this) | |
31 this = this or {} | |
32 this.headers = {} | |
33 this.header = {__plural=this.headers} | |
34 set_metatable(this.header,singular_metatable) | |
35 return this | |
36 end | |
37 | |
38 | |
39 function new_request(this) | |
40 this = new_common(this) | |
41 this.method = "GET" -- default | |
42 -- this.path | |
43 -- this.protocol | |
44 this.scheme = "http" -- default | |
45 this.parameters = {} | |
46 this.parameter = {__plural=this.parameters} | |
47 set_metatable(this.parameter,singular_metatable) | |
48 this.cookie = {} | |
49 | |
50 function this.url() | |
51 local string_uri = Io.uri "string:" | |
52 local out = string_uri.text_writer() | |
53 | |
54 out.write( this.scheme, "://", this.header.host, this.path ) | |
55 if this.method ~= "POST" then | |
56 local and_char = "?" | |
57 for name, values in pairs(this.parameters) do | |
58 for _, value in ipairs(values) do | |
59 out.write( and_char, url_encode(name), "=", url_encode(value) ) | |
60 and_char = "&" | |
61 end | |
62 end | |
63 end | |
64 | |
65 out.close() | |
66 return string_uri.read_text() | |
67 end | |
68 | |
69 return this | |
70 end | |
71 | |
72 -- request = new_request{} -- filled in by HttpServicer | |
3 | 73 |
4 | 74 |
5 | 75 |
6 function init_for_test() | 76 function init_for_test() |
7 | 77 |
8 welcome_file = "index.html" | 78 test = test or {} |
79 | |
80 test.welcome_file = "index.html" | |
9 | 81 |
10 function get_page(path) | 82 function get_page(path) |
11 if welcome_file ~= nil and path.matches ".*/" then | 83 if test.welcome_file ~= nil and path.matches ".*/" then |
12 path = path .. welcome_file | 84 path = path .. test.welcome_file |
13 end | 85 end |
14 local old_out = Io.stdout | 86 local old_out = Io.stdout |
15 local mod = require("site:"..path) | 87 local mod = require("site:"..path) |
16 mod.respond() | 88 mod.respond() |
17 text_writer.close() | 89 text_writer.close() |
18 Io.stdout = old_out | 90 Io.stdout = old_out |
19 return result.read_text() | 91 return result.read_text() |
20 end | 92 end |
21 | 93 |
22 cookies = cookies or {} | 94 test.cookies = test.cookies or {} |
23 | 95 |
24 request = { | 96 request = new_request{} |
25 parameters = {}; | 97 request.cookies = test.cookies |
26 } | |
27 request.cookies = cookies | |
28 | 98 |
29 response = { | 99 response = { |
30 | 100 |
31 text_writer = function() | 101 text_writer = function() |
32 result = Io.uri "string:" | 102 result = Io.uri "string:" |
33 text_writer = result.text_writer() | 103 text_writer = result.text_writer() |
34 return text_writer | 104 return text_writer |
35 end; | 105 end; |
36 | 106 |
37 set_cookie = function(name,value) | 107 set_cookie = function(name,value) |
38 cookies[name] = value | 108 test.cookies[name] = value |
39 end; | 109 end; |
40 | 110 |
41 remove_cookie = function(name) | 111 remove_cookie = function(name) |
42 cookies[name] = nil | 112 test.cookies[name] = nil |
43 end; | 113 end; |
44 | 114 |
45 send_redirect = function(url) | 115 send_redirect = function(url) |
46 response.redirect = url | 116 response.redirect = url |
47 end; | 117 end; |