|
6 | 6 |
|
7 | 7 | local uv = prequire "lluv" |
8 | 8 | local Pegasus = require (uv and "lluv.pegasus" or "pegasus") |
| 9 | +local Router = require "pegasus.plugins.router" |
| 10 | +local json = require "dkjson" |
| 11 | +-- local pp = require "pp" |
9 | 12 |
|
10 | | -local server = Pegasus:new{host = '127.0.0.1', port = 7090} |
| 13 | +local function decode_form(form) |
| 14 | + return string.match(form, '\r\nContent%-Disposition:%s*form%-data;%s*name="(.-)".-\r\n\r\n(.-)\r\n') |
| 15 | +end |
| 16 | + |
| 17 | +local function decode_params(str) |
| 18 | + local params = {} |
| 19 | + for k, v in string.gmatch(str, '([^=]+)=([^&]+)&?') do |
| 20 | + params[k] = v |
| 21 | + end |
| 22 | + return params |
| 23 | +end |
| 24 | + |
| 25 | +local function rand_bytes(n) |
| 26 | + local res = {} |
| 27 | + for i = 1, n do |
| 28 | + res[#res + 1] = string.char(math.random(254)) |
| 29 | + end |
| 30 | + return table.concat(res) |
| 31 | +end |
| 32 | + |
| 33 | +local r = Router:new() |
| 34 | + |
| 35 | +local server = Pegasus:new{ |
| 36 | + plugins = { r }; |
| 37 | + host = '127.0.0.1', port = 7090, timout = 10 |
| 38 | +} |
| 39 | + |
| 40 | +r:get('/get', function(request, response) |
| 41 | + local headers = request:headers() |
| 42 | + local params = request:params() |
| 43 | + local path = request:path() |
| 44 | + local ip = request.ip |
| 45 | + |
| 46 | + local result = json.encode({ |
| 47 | + args = params; |
| 48 | + headers = headers; |
| 49 | + origin = ip; |
| 50 | + url = 'http://127.0.0.1' .. path; |
| 51 | + }, {indent = true}) |
| 52 | + |
| 53 | + response:statusCode(200) |
| 54 | + response:contentType('application/json') |
| 55 | + response:write(result) |
| 56 | +end) |
| 57 | + |
| 58 | +r:get('/bytes/:size', function(request, response, params) |
| 59 | + local headers = request:headers() |
| 60 | + local size = tonumber(params.size) or 1024 |
| 61 | + local result = rand_bytes(size) |
| 62 | + |
| 63 | + response:statusCode(200) |
| 64 | + response:contentType('application/octet-stream') |
| 65 | + response:write(result) |
| 66 | +end) |
| 67 | + |
| 68 | +r:post('/post', function(request, response, params) |
| 69 | + local headers = request:headers() |
| 70 | + local params = request:params() |
| 71 | + local path = request:path() |
| 72 | + local ip = request.ip |
| 73 | + |
| 74 | + local body = {} |
| 75 | + while true do |
| 76 | + local result, status = request:receiveBody() |
| 77 | + if result then body[#body + 1] = result |
| 78 | + elseif status ~= 'timeout' then break end |
| 79 | + end |
| 80 | + body = table.concat(body) |
| 81 | + |
| 82 | + local name, data, form = decode_form(body) |
| 83 | + if name then |
| 84 | + form = {[name] = data} |
| 85 | + else |
| 86 | + form = decode_params(body) |
| 87 | + end |
| 88 | + |
| 89 | + local result = json.encode({ |
| 90 | + args = params; |
| 91 | + headers = headers; |
| 92 | + origin = ip; |
| 93 | + form = form; |
| 94 | + url = 'http://127.0.0.1' .. path; |
| 95 | + }, {indent = true}) |
| 96 | + |
| 97 | + response:statusCode(200) |
| 98 | + response:contentType('application/json') |
| 99 | + response:write(result) |
| 100 | +end) |
11 | 101 |
|
12 | 102 | server:start(function(request, response) |
| 103 | + local headers = request:headers() |
| 104 | + |
13 | 105 | response:statusCode(200) |
14 | 106 | response:addHeader('Content-Type', 'text/plain') |
15 | 107 | response:write('Hello from Pegasus') |
|
0 commit comments