Software /
code /
prosody
Annotate
util/http.lua @ 9759:1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Function calls are more expensive than table lookups
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 06 Jan 2019 10:39:33 +0100 |
parent | 9504:cfbea3064aa9 |
child | 9785:ff88b03c343f |
rev | line source |
---|---|
5299
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
1 -- Prosody IM |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
2 -- Copyright (C) 2013 Florian Zeitz |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
3 -- |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
6 -- |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
7 |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
8 local format, char = string.format, string.char; |
9759
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
9 local pairs, ipairs = pairs, ipairs; |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
10 local t_insert, t_concat = table.insert, table.concat; |
5299
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
11 |
9759
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
12 local url_codes = {}; |
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
13 for i = 0, 255 do |
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
14 local c = char(i); |
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
15 local u = format("%%%02x", i); |
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
16 url_codes[c] = u; |
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
17 url_codes[u] = c; |
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
18 end |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
19 local function urlencode(s) |
9759
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
20 return s and (s:gsub("[^a-zA-Z0-9.~_-]", url_codes)); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
21 end |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
22 local function urldecode(s) |
9759
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
23 return s and (s:gsub("%%%x%x", url_codes)); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
24 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
25 |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
26 local function _formencodepart(s) |
9759
1af5106a2c34
util.http: Pre-generate urlencoding mappings (optimization)
Kim Alvefur <zash@zash.se>
parents:
9504
diff
changeset
|
27 return s and (urlencode(s):gsub("%%20", "+")); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
28 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
29 |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
30 local function formencode(form) |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
31 local result = {}; |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
32 if form[1] then -- Array of ordered { name, value } |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
33 for _, field in ipairs(form) do |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
34 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
35 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
36 else -- Unordered map of name -> value |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
37 for name, value in pairs(form) do |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
38 t_insert(result, _formencodepart(name).."=".._formencodepart(value)); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
39 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
40 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
41 return t_concat(result, "&"); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
42 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
43 |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
44 local function formdecode(s) |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
45 if not s:match("=") then return urldecode(s); end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
46 local r = {}; |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
47 for k, v in s:gmatch("([^=&]*)=([^&]*)") do |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
48 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20"); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
49 k, v = urldecode(k), urldecode(v); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
50 t_insert(r, { name = k, value = v }); |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
51 r[k] = v; |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
52 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
53 return r; |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
54 end |
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)
Matthew Wild <mwild1@gmail.com>
parents:
5299
diff
changeset
|
55 |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
56 local function contains_token(field, token) |
5299
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
57 field = ","..field:gsub("[ \t]", ""):lower()..","; |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
58 return field:find(","..token:lower()..",", 1, true) ~= nil; |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
59 end |
cc9d460aa779
util.http: New module for HTTP helper functions
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
60 |
9504
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
61 local function normalize_path(path, is_dir) |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
62 if is_dir then |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
63 if path:sub(-1,-1) ~= "/" then path = path.."/"; end |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
64 else |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
65 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
66 end |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
67 if path:sub(1,1) ~= "/" then path = "/"..path; end |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
68 return path; |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
69 end |
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
70 |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
71 return { |
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
72 urlencode = urlencode, urldecode = urldecode; |
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
73 formencode = formencode, formdecode = formdecode; |
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
74 contains_token = contains_token; |
9504
cfbea3064aa9
mod_http: Move normalize_path to util.http
Kim Alvefur <zash@zash.se>
parents:
5471
diff
changeset
|
75 normalize_path = normalize_path; |
5471
34bfd26525f5
util.http: Refactor and import all necessary functions
Matthew Wild <mwild1@gmail.com>
parents:
5458
diff
changeset
|
76 }; |