-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWebRequestContext.cs
More file actions
165 lines (142 loc) · 4.26 KB
/
Copy pathWebRequestContext.cs
File metadata and controls
165 lines (142 loc) · 4.26 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using ScriptEngine.HostedScript.Library;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace oscript
{
[ContextClass("ВебЗапрос","WebRequest")]
public class WebRequestContext : AutoContext<WebRequestContext>
{
MapImpl _environmentVars = new MapImpl();
MapImpl _get = new MapImpl();
MapImpl _post = new MapImpl();
public WebRequestContext()
{
string get = Environment.GetEnvironmentVariable("QUERY_STRING");
if(get != null)
{
FillGetMap(get);
}
string contentLen = Environment.GetEnvironmentVariable("CONTENT_LENGTH");
if(contentLen != null)
{
FillPostMap(contentLen);
}
FillEnvironmentVars();
}
private void FillEnvironmentVars()
{
foreach (DictionaryEntry item in Environment.GetEnvironmentVariables())
{
_environmentVars.Insert(
ValueFactory.Create((string)item.Key),
ValueFactory.Create((string)item.Value));
}
}
private void FillPostMap(string contentLen)
{
int len = Int32.Parse(contentLen);
if (len > 0)
{
byte[] bytes = new byte[len];
using (var stdin = Console.OpenStandardInput())
{
stdin.Read(bytes, 0, len);
}
string data = Encoding.Default.GetString(bytes);
ParseFormData(data, _post);
}
}
private void FillGetMap(string get)
{
ParseFormData(get, _get);
}
private void ParseFormData(string data, MapImpl map)
{
var pairs = data.Split('&');
foreach (var pair in pairs)
{
var nameVal = pair.Split(new Char[] { '=' }, 2);
if (nameVal.Length == 2)
{
IValue key = ValueFactory.Create(nameVal[0]);
IValue val = ValueFactory.Create(Decode(nameVal[1]));
map.Insert(key, val);
}
else if(pair.Length > 0)
{
IValue val = ValueFactory.Create(Decode(pair));
map.Insert(val, ValueFactory.Create());
}
}
}
private static string Decode(string p)
{
byte[] bytes = new byte[p.Length];
int j = 0;
for (int i = 0; i < p.Length; i++, j++)
{
if (p[i] == '+')
bytes[j] = 0x20;
else if (p[i] == '%')
{
bytes[j] = ByteFromHEX(p, ref i);
}
else
bytes[j] = (byte)p[i];
}
return Encoding.UTF8.GetString(bytes, 0, j);
}
private static byte ByteFromHEX(string pattern, ref int index)
{
System.Diagnostics.Debug.Assert(pattern[index] == '%');
index++;
int major = 0;
int minor = 0;
for (int i = 0; i < 2; i++)
{
int code = (int)pattern[index];
if (code >= 48 && code <= 57)
code = code - 48;
else if (code >= 65 && code <= 70)
code = code - 55;
if (i == 0)
{
major = code;
index++;
}
else
minor = code;
}
return (byte)(major * 16 + minor);
}
[ContextProperty("GET")]
public IValue GET
{
get
{
return _get;
}
}
[ContextProperty("POST")]
public IValue POST
{
get
{
return _post;
}
}
[ContextProperty("ENV")]
public IValue ENV
{
get
{
return _environmentVars;
}
}
}
}