FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OneScript/src/oscript/CgiBehavior.cs at develop · ivtroitskiy/OneScript · GitHub
ivtroitskiy
/
OneScript
Public
forked from
EvilBeaver/OneScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OneScript
/
src
/
oscript
/
CgiBehavior.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
280 lines (223 loc) · 6.32 KB
Breadcrumbs
OneScript
/
src
/
oscript
/
CgiBehavior.cs
Copy path
File metadata and controls
280 lines (223 loc) · 6.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
Linq
;
using
System
.
Text
;
using
OneScript
.
Contexts
;
using
OneScript
.
Exceptions
;
using
OneScript
.
StandardLibrary
;
using
oscript
.
Web
;
using
ScriptEngine
.
HostedScript
;
using
ScriptEngine
.
Hosting
;
using
ScriptEngine
.
Machine
;
using
ScriptEngine
.
Machine
.
Contexts
;
namespace
oscript
{
internal
class
CgiBehavior
:
AppBehavior
,
IHostApplication
,
IRuntimeContextInstance
,
IAttachableContext
{
private
static
readonly
ContextMethodsMapper
<
CgiBehavior
>
_methods
=
new
ContextMethodsMapper
<
CgiBehavior
>
(
)
;
private
readonly
HashSet
<
string
>
_headersWritten
=
new
HashSet
<
string
>
(
StringComparer
.
OrdinalIgnoreCase
)
;
private
bool
_isContentEchoed
;
public
CgiBehavior
(
)
{
Encoding
=
new
UTF8Encoding
(
)
;
}
public
override
int
Execute
(
)
{
var
scriptFile
=
Environment
.
GetEnvironmentVariable
(
"SCRIPT_FILENAME"
)
??
Environment
.
GetEnvironmentVariable
(
"PATH_TRANSLATED"
)
;
if
(
scriptFile
==
null
)
{
Header
(
"Content-type"
,
"text/plain"
)
;
Echo
(
"No CGI Variables found"
)
;
return
1
;
}
if
(
!
File
.
Exists
(
scriptFile
)
)
{
Header
(
"Content-type"
,
"text/plain"
)
;
Echo
(
$
"Script file not found:
{
scriptFile
}
"
)
;
return
1
;
}
return
RunCGIMode
(
scriptFile
)
;
}
private
int
RunCGIMode
(
string
scriptFile
)
{
var
builder
=
ConsoleHostBuilder
.
Create
(
scriptFile
)
;
builder
.
SetupEnvironment
(
e
=>
{
e
.
AddAssembly
(
GetType
(
)
.
Assembly
)
;
}
)
;
var
engine
=
ConsoleHostBuilder
.
Build
(
builder
)
;
var
request
=
new
WebRequestContext
(
)
;
engine
.
InjectGlobalProperty
(
"ВебЗапрос"
,
"WebRequest"
,
request
,
true
)
;
engine
.
InjectObject
(
this
)
;
var
source
=
engine
.
Loader
.
FromFile
(
scriptFile
)
;
Process
process
;
try
{
process
=
engine
.
CreateProcess
(
this
,
source
)
;
}
catch
(
Exception
e
)
{
ShowExceptionInfo
(
e
)
;
return
1
;
}
var
exitCode
=
process
.
Start
(
)
;
if
(
!
_isContentEchoed
)
Echo
(
""
)
;
request
.
Dispose
(
)
;
return
exitCode
;
}
public
void
OnAttach
(
out
IVariable
[
]
variables
,
out
BslMethodInfo
[
]
methods
)
{
variables
=
Array
.
Empty
<
IVariable
>
(
)
;
methods
=
this
.
GetMethods
(
)
.
ToArray
(
)
;
}
#region CGIHost
[
ContextMethod
(
"ВывестиЗаголовок"
,
"Header"
)
]
public
void
Header
(
string
header
,
string
value
)
{
if
(
_isContentEchoed
)
throw
new
InvalidOperationException
(
"Headers can not be written after the main content"
)
;
Output
(
header
+
": "
+
value
+
"
\r
\n
"
)
;
_headersWritten
.
Add
(
header
)
;
}
[
ContextMethod
(
"ОтправитьФайл"
,
"SendFile"
)
]
public
void
SendFile
(
string
filePath
,
string
downloadFileName
=
null
)
{
if
(
_isContentEchoed
)
throw
new
InvalidOperationException
(
"Content already sent!"
)
;
if
(
!
IsHeaderWritten
(
"Content-type"
)
)
Header
(
"Content-type"
,
"application/octet-stream"
)
;
if
(
string
.
IsNullOrEmpty
(
downloadFileName
)
)
{
var
finfo
=
new
FileInfo
(
filePath
)
;
downloadFileName
=
finfo
.
Name
;
}
using
(
var
fs
=
new
FileStream
(
filePath
,
FileMode
.
Open
)
)
{
Header
(
"Content-disposition"
,
$
"inline; filename=
\"
{
downloadFileName
}
\"
"
)
;
Header
(
"Content-length"
,
fs
.
Length
.
ToString
(
)
)
;
oscript
.
Output
.
WriteLine
(
)
;
using
(
var
stdout
=
Console
.
OpenStandardOutput
(
)
)
{
fs
.
CopyTo
(
stdout
)
;
}
}
}
public
Encoding
Encoding
{
get
;
set
;
}
private
bool
IsHeaderWritten
(
string
header
)
{
return
_headersWritten
.
Contains
(
header
)
;
}
public
void
Echo
(
string
str
,
MessageStatusEnum
status
=
MessageStatusEnum
.
Ordinary
)
{
if
(
!
_isContentEchoed
)
{
if
(
!
IsHeaderWritten
(
"Content-type"
)
)
Header
(
"Content-type"
,
"text/html"
)
;
if
(
!
IsHeaderWritten
(
"Content-encoding"
)
)
Header
(
"Content-encoding"
,
Encoding
.
BodyName
)
;
oscript
.
Output
.
WriteLine
(
)
;
_isContentEchoed
=
true
;
}
if
(
str
!=
""
)
{
Output
(
str
)
;
oscript
.
Output
.
WriteLine
(
)
;
}
}
private
void
Output
(
string
str
)
{
using
(
var
stream
=
Console
.
OpenStandardOutput
(
)
)
{
var
bytes
=
Encoding
.
GetBytes
(
str
)
;
stream
.
Write
(
bytes
,
0
,
bytes
.
Length
)
;
}
}
public
void
ShowExceptionInfo
(
Exception
exc
)
{
Echo
(
exc
.
ToString
(
)
)
;
}
public
bool
InputString
(
out
string
result
,
string
prompt
,
int
maxLen
,
bool
multiline
)
{
result
=
null
;
return
false
;
}
public
string
[
]
GetCommandLineArguments
(
)
{
return
new
string
[
0
]
;
}
#endregion
#region IRuntimeContextInstance Members
public
bool
IsIndexed
=>
false
;
public
bool
DynamicMethodSignatures
=>
false
;
public
IValue
GetIndexedValue
(
IValue
index
)
{
throw
new
NotImplementedException
(
)
;
}
public
void
SetIndexedValue
(
IValue
index
,
IValue
val
)
{
throw
new
NotImplementedException
(
)
;
}
public
int
GetPropertyNumber
(
string
name
)
{
throw
PropertyAccessException
.
PropNotFoundException
(
name
)
;
}
public
bool
IsPropReadable
(
int
propNum
)
{
return
false
;
}
public
bool
IsPropWritable
(
int
propNum
)
{
return
false
;
}
public
IValue
GetPropValue
(
int
propNum
)
{
throw
new
ArgumentException
(
)
;
}
public
void
SetPropValue
(
int
propNum
,
IValue
newVal
)
{
throw
new
InvalidOperationException
(
"global props are not writable"
)
;
}
public
int
GetMethodNumber
(
string
name
)
{
return
_methods
.
FindMethod
(
name
)
;
}
public
BslMethodInfo
GetMethodInfo
(
int
methodNumber
)
{
return
_methods
.
GetRuntimeMethod
(
methodNumber
)
;
}
public
BslPropertyInfo
GetPropertyInfo
(
int
propertyNumber
)
{
throw
new
NotImplementedException
(
)
;
}
public
void
CallAsProcedure
(
int
methodNumber
,
IValue
[
]
arguments
)
{
_methods
.
GetCallableDelegate
(
methodNumber
)
(
this
,
arguments
)
;
}
public
void
CallAsFunction
(
int
methodNumber
,
IValue
[
]
arguments
,
out
IValue
retValue
)
{
retValue
=
_methods
.
GetCallableDelegate
(
methodNumber
)
(
this
,
arguments
)
;
}
public
int
GetPropCount
(
)
{
return
0
;
}
public
string
GetPropName
(
int
propNum
)
{
throw
new
NotImplementedException
(
)
;
}
public
int
GetMethodsCount
(
)
{
return
_methods
.
Count
;
}
#endregion
}
}
Back
|
FazBrowse Home
|
New Git URL