-
Notifications
You must be signed in to change notification settings - Fork 0
/
WolframServerTool.wl
99 lines (68 loc) · 2.48 KB
/
WolframServerTool.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(* ::Package:: *)
BeginPackage["WolframServerTool`"]
ToMIME::usage = "Convert file extensions to MIMETypeStrings, which accept all possible MIMETypes instead of those in $ImportFormats and $ExportFormats.";
ResourceLocalize::usage = "Redirect WolframCloud resources in a HTML response to the local path.";
MakeFormRules::usage = "Convert the body bytes in an HTTPRequest to an association to make the \"FormRules\" property available.";
$CloudResourcePath::usage = "A list of cloud resource paths to be localized.";
$MIMETypeMap::usage = "";
$CloudResourcePath = {"https://www.wolframcloud.com/res/themes/"};
$MIMETypeMap = Rule @@@ Get@"MIMETypeMap.wl" // Association;
(* ::Section:: *)
(* WolframServer` *)
(* ::Subsection:: *)
(* HTTPRespond *)
MakeFormRules[request_HTTPRequest] := With[
{
multipart = StartOfString~~"multipart/form-data; boundary="~~boundaryPattern__~~EndOfString,
formURLEncoded = "application/x-www-form-urlencoded"
},
MapAt[
StringSwitch[request["ContentType"],
formURLEncoded,
URLQueryDecode@StringTrim@FromCharacterCode[#, "UTF-8"] &,
multipart,
Association@parseMultipart[
StringTrim@FromCharacterCode[#, "UTF-8"],
StringReplace[multipart -> "--"~~boundaryPattern]@req["ContentType"]
] &,
_,
Identity
],
request,
{2, "Body"}
]
]
(* ::Subsubsection:: *)
(*MakeFormRules*)
Begin["`Private`"]
StringSwitch[expr_String, seq:PatternSequence[_, _]..] := Switch[expr, ##] &[
Sequence @@ Join @@ MapAt[_?(StringMatchQ[#])&, 1] /@ Partition[{seq}, 2]
]
parseMultipart[body_String, boundary_String] := handlePartDisposition /@ StringSplit[body, boundary~~"\n"|"--"]
handlePartDisposition[part_String] := With[
{
formDataWithName = StartOfString~~"Content-Disposition: form-data"~~"; name=\""~~namePattern:Shortest@__~~"\""~~"\n\n"~~bodyPattern:__~~"\n"~~EndOfString
},
StringSwitch[part,
formDataWithName, Sequence@@URLQueryDecode@StringReplace[formDataWithName -> URLEncode@namePattern~~"="~~URLEncode@bodyPattern]@part
]
]
End[]
(* ::Section:: *)
(* WolframServerService` *)
(* ::Subsection:: *)
(* ExportForm[#, "HTML"]& *)
ResourceLocalize = MapAt[
StringReplace[{
Alternatives@@$CloudResourcePath ~~ FileName_ -> "/Resources/" ~~ FileName,
"http://www.wolframcdn.com/consent/cookie-consent.php" -> ""
}] @ ExportString[#, "Byte"] &
, 1];
(* ::Subsection:: *)
(* GetResource *)
ToMIME[format_String] := Which[
!MissingQ@$MIMETypeMap,
$MIMETypeMap@ToLowerCase@format,
True,
StringForm["text/``", ToLowerCase@format]
]