Software /
code /
prosody
Comparison
net/http.lua @ 5458:84162b81c863
net.http, util.http: Move definitions of urlencode/decode and formencode/decode to util.http (possible to use them without unnecessary network-related dependencies)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 11 Apr 2013 17:32:59 +0100 |
parent | 5448:cbe9fa2d3787 |
child | 5464:712dbe1a0146 |
comparison
equal
deleted
inserted
replaced
5454:5f69fddf6fb9 | 5458:84162b81c863 |
---|---|
8 | 8 |
9 local socket = require "socket" | 9 local socket = require "socket" |
10 local b64 = require "util.encodings".base64.encode; | 10 local b64 = require "util.encodings".base64.encode; |
11 local url = require "socket.url" | 11 local url = require "socket.url" |
12 local httpstream_new = require "util.httpstream".new; | 12 local httpstream_new = require "util.httpstream".new; |
13 local util_http = require "util.http"; | |
13 | 14 |
14 local ssl_available = pcall(require, "ssl"); | 15 local ssl_available = pcall(require, "ssl"); |
15 | 16 |
16 local server = require "net.server" | 17 local server = require "net.server" |
17 | 18 |
68 request:reader(nil); | 69 request:reader(nil); |
69 end | 70 end |
70 requests[conn] = nil; | 71 requests[conn] = nil; |
71 end | 72 end |
72 | 73 |
73 function urlencode(s) return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); end | 74 local function request_reader(request, data) |
74 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end | |
75 | |
76 local function _formencodepart(s) | |
77 return s and (s:gsub("%W", function (c) | |
78 if c ~= " " then | |
79 return format("%%%02x", c:byte()); | |
80 else | |
81 return "+"; | |
82 end | |
83 end)); | |
84 end | |
85 | |
86 function formencode(form) | |
87 local result = {}; | |
88 if form[1] then -- Array of ordered { name, value } | |
89 for _, field in ipairs(form) do | |
90 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); | |
91 end | |
92 else -- Unordered map of name -> value | |
93 for name, value in pairs(form) do | |
94 t_insert(result, _formencodepart(name).."=".._formencodepart(value)); | |
95 end | |
96 end | |
97 return t_concat(result, "&"); | |
98 end | |
99 | |
100 function formdecode(s) | |
101 if not s:match("=") then return urldecode(s); end | |
102 local r = {}; | |
103 for k, v in s:gmatch("([^=&]*)=([^&]*)") do | |
104 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20"); | |
105 k, v = urldecode(k), urldecode(v); | |
106 t_insert(r, { name = k, value = v }); | |
107 r[k] = v; | |
108 end | |
109 return r; | |
110 end | |
111 | |
112 local function request_reader(request, data, startpos) | |
113 if not request.parser then | 75 if not request.parser then |
114 if not data then return; end | 76 if not data then return; end |
115 local function success_cb(r) | 77 local function success_cb(r) |
116 if request.callback then | 78 if request.callback then |
117 for k,v in pairs(r) do request[k] = v; end | 79 for k,v in pairs(r) do request[k] = v; end |
214 request.conn = nil; | 176 request.conn = nil; |
215 request.handler:close() | 177 request.handler:close() |
216 end | 178 end |
217 end | 179 end |
218 | 180 |
219 _M.urlencode = urlencode; | 181 local urlencode, urldecode = util_http.urlencode, util_http.urldecode; |
182 local formencode, formdecode = util_http.formencode, util_http.formdecode; | |
183 | |
184 _M.urlencode, _M.urldecode = urlencode, urldecode; | |
185 _M.formencode, _M.formdecode = formencode, formdecode; | |
220 | 186 |
221 return _M; | 187 return _M; |