annotate Resources/Toolbox.lua @ 2248:69b0f4e8a49b

Escape multipart type parameter value in Content-Type header ## Summary Multipart responses do not quote/escape the value of their type parameter (the subtype) even though it always contains at least one special character (the slash "/"), which confuses standard-compliant HTTP clients. ## Details The Content-Type header in HTTP is in RFC 7231, Section 3.1.1.5: https://tools.ietf.org/html/rfc7231#section-3.1.1.5 The section defers to the media type section (3.1.1.1) for the syntax of the media type: https://tools.ietf.org/html/rfc7231#section-3.1.1.1 This states that a parameter value can be quoted: parameter = token "=" ( token / quoted-string ) A parameter value that matches the token production can be transmitted either as a token or within a quoted-string. The quoted and unquoted values are equivalent. Tokens are defined in RFC 7230, Section 3.2.6 (via RFC 7231, appendix C): https://tools.ietf.org/html/rfc7231#appendix-C https://tools.ietf.org/html/rfc7230#section-3.2.6 Here we observe that tokens cannot contain a slash "/" character: token = 1*tchar tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA ; any VCHAR, except delimiters Delimiters are chosen from the set of US-ASCII visual characters not allowed in a token (DQUOTE and "(),/:;<=>?@[\]{}"). However, the current implementation does not quote/escape the value of the type parameter: multipart/related; type=application/dicom Instead, it should be: multipart/related; type="application/dicom" All of this also seems to apply to the MIME Content-Type header definition, even though it is a little different: https://www.iana.org/assignments/message-headers https://tools.ietf.org/html/rfc2045#section-5.1 https://tools.ietf.org/html/rfc2387
author Thibault Nélis <tn@osimis.io>
date Mon, 16 Jan 2017 13:07:11 +0100
parents 6406f5493d92
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 394
diff changeset
1 --[[ PrintRecursive(struct, [limit], [indent]) Recursively print arbitrary data.
384
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 Set limit (default 100) to stanch infinite loops.
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 Indents tables as [KEY] VALUE, nested tables as [KEY] [KEY]...[KEY] VALUE
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 Set indent ("") to prefix each line: Mytable [KEY] [KEY]...[KEY] VALUE
394
9784f19f7e1b path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 384
diff changeset
5 Source: https://gist.github.com/stuby/5445834#file-rprint-lua
384
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 --]]
394
9784f19f7e1b path relative to configuration path, list of lua scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 384
diff changeset
7
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 394
diff changeset
8 function PrintRecursive(s, l, i) -- recursive Print (structure, limit, indent)
1006
649d47854314 proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
9 l = (l) or 100; -- default item limit
649d47854314 proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 418
diff changeset
10 i = i or ""; -- indent string
384
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 local ts = type(s);
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 if (ts ~= "table") then print (i,ts,s); return l-1 end
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 print (i,ts); -- print "table"
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 for k,v in pairs(s) do -- print "[KEY] VALUE"
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 394
diff changeset
16 l = PrintRecursive(v, l, i.."\t["..tostring(k).."]");
384
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 if (l < 0) then break end
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 end
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 return l
18fe778eeb95 wrapper around lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 end
418
b79bf2f4ab2e execution of lua through REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 397
diff changeset
21
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
22
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
23
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
24
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
25 function _InitializeJob()
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
26 _job = {}
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
27 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
28
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
29
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
30 function _AccessJob()
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
31 return _job
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
32 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
33
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
34
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
35 function SendToModality(resourceId, modality, localAet)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
36 if resourceId == nil then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
37 error('Cannot send a nonexistent resource')
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
38 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
39
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
40 table.insert(_job, {
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
41 Operation = 'store-scu',
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
42 Resource = resourceId,
1427
d710ea64f0fd Custom setting of the local AET during C-Store SCU (both in Lua and in the REST API)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1065
diff changeset
43 Modality = modality,
d710ea64f0fd Custom setting of the local AET during C-Store SCU (both in Lua and in the REST API)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1065
diff changeset
44 LocalAet = localAet
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
45 })
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
46 return resourceId
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
47 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
48
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
49
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
50 function SendToPeer(resourceId, peer)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
51 if resourceId == nil then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
52 error('Cannot send a nonexistent resource')
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
53 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
54
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
55 table.insert(_job, {
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
56 Operation = 'store-peer',
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
57 Resource = resourceId,
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
58 Peer = peer
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
59 })
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
60 return resourceId
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
61 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
62
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
63
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
64 function Delete(resourceId)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
65 if resourceId == nil then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
66 error('Cannot delete a nonexistent resource')
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
67 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
68
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
69 table.insert(_job, {
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
70 Operation = 'delete',
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
71 Resource = resourceId
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
72 })
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
73 return nil -- Forbid chaining
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
74 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
75
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
76
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
77 function ModifyResource(resourceId, replacements, removals, removePrivateTags)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
78 if resourceId == nil then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
79 error('Cannot modify a nonexistent resource')
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
80 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
81
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
82 if resourceId == '' then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
83 error('Cannot modify twice an resource');
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
84 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
85
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
86 table.insert(_job, {
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
87 Operation = 'modify',
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
88 Resource = resourceId,
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
89 Replace = replacements,
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
90 Remove = removals,
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
91 RemovePrivateTags = removePrivateTags
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
92 })
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
93
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
94 return '' -- Chain with another operation
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
95 end
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
96
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
97
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
98 function ModifyInstance(resourceId, replacements, removals, removePrivateTags)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
99 return ModifyResource(resourceId, replacements, removals, removePrivateTags)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
100 end
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
101
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
102
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
103 -- This function is only applicable to individual instances
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
104 function CallSystem(resourceId, command, args)
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
105 if resourceId == nil then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
106 error('Cannot execute a system call on a nonexistent resource')
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
107 end
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
108
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
109 if command == nil then
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
110 error('No command was specified for system call')
1065
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
111 end
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
112
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
113 table.insert(_job, {
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
114 Operation = 'call-system',
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
115 Resource = resourceId,
1065
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
116 Command = command,
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
117 Arguments = args
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
118 })
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
119
1435
6406f5493d92 refactoring: Lua toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1427
diff changeset
120 return resourceId
1065
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
121 end
921532f67770 Lua scripts can invoke system commands, with CallSystem()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
122
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1006
diff changeset
123
418
b79bf2f4ab2e execution of lua through REST
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 397
diff changeset
124 print('Lua toolbox installed')