-
Notifications
You must be signed in to change notification settings - Fork 2
/
class_request.asp
318 lines (262 loc) · 9.08 KB
/
class_request.asp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<%
' ASPProxyRequest
' AUTHOR: Chris Read
'
' Take the multipart/form-data and process the sucka
' CONTENT_TYPE holds the post type and boundary in IE, Opera, Netscape
'
' Usage
'
'Set objRequest = New ProxyRequest
'
'Response.Write objRequest("file1_mimetype")
'Response.Write objRequest("file1_filename") & "<br>"
'Response.Write objRequest("file1_sourcepath") & "<br>"
'
'objRequest.SaveItem "file1",Server.MapPath(objRequest("file1_filename"))
Class ProxyRequest
Private m_objStorage
Private m_objForm
Private m_MultiPart
Public Cookies
Public ClientCertificate
Public ServerVariables
Public QueryString
Public TotalBytes
Public Function BinaryRead()
m_objStorage.Position = 0
BinaryRead = m_objStorage.Read
End Function
Public Sub SaveItem(sIndex,sFilename) ' Save and item to file
Dim aTemp, objCopy
Set objCopy = Server.CreateObject("ADODB.Stream")
objCopy.Open
if m_objForm.Exists(sIndex) then
aTemp = Split(m_objForm.Item(sIndex),"|")
if UBound(aTemp) > 0 then
m_objStorage.Position = CLng(aTemp(0)) - 1
if not m_objForm.Exists(sIndex & "_filename") then
if m_MultiPart then
sTemp = ASCIIToUNICODE(m_objStorage.Read(CLng(aTemp(1))))
else
sTemp = URLDecode(ASCIIToUNICODE(m_objStorage.Read(CLng(aTemp(1)))))
end if
objCopy.Type = 2
objCopy.Charset = "x-ansi"
objCopy.WriteText sTemp
else
objCopy.Type = 1
m_objStorage.CopyTo objCopy,CLng(aTemp(1))
end if
objCopy.Position = 0
objCopy.SaveToFile sFilename
end if
end if
End Sub
Public Property Get Form(sIndex)
Dim aTemp
if m_objForm.Exists(sIndex) then
aTemp = Split(m_objForm.Item(sIndex),"|")
if UBound(aTemp) > 0 then
' We have a winner
m_objStorage.Position = CLng(aTemp(0))
Form = m_objStorage.Read(aTemp(1))
end if
end if
End Property
Public Default Property Get Item(sIndex) ' Default property
Dim aTemp, sTemp
Item = ""
if Right(sIndex,9) = "_filename" or Right(sIndex,9) = "_mimetype" or Right(sIndex,11) = "_sourcepath" then
' Just return the information
Item = m_objForm.Item(sIndex)
elseif m_objForm.Exists(sIndex) then
aTemp = Split(m_objForm.Item(sIndex),"|")
if UBound(aTemp) > 0 then
m_objStorage.Position = CLng(aTemp(0)) - 1
if not m_objForm.Exists(sIndex & "_filename") then
if m_MultiPart then
sTemp = ASCIIToUNICODE(m_objStorage.Read(CLng(aTemp(1))))
else
sTemp = URLDecode(ASCIIToUNICODE(m_objStorage.Read(CLng(aTemp(1)))))
end if
Item = sTemp
else
Item = m_objStorage.Read(aTemp(1))
end if
end if
else
Item = QueryString(sIndex)
end if
End Property
Private Function ASCIIToUNICODE(sText)
Dim lTemp, sTemp
sTemp = ""
if IsNull(sText) then
ASCIIToUNICODE = ""
else
For lTemp = 1 To LenB(sText)
sTemp = sTemp & Chr(AscB(MidB(sText,lTemp,1)))
Next
ASCIIToUNICODE = sTemp
end if
End Function
Private Function UNICODEToASCII(sText)
Dim lTemp, sTemp
sTemp = ""
if IsNull(sText) then
UNICODEToASCII = ""
else
For lTemp = 1 To Len(sText)
sTemp = sTemp & ChrB(Asc(Mid(sText,lTemp,1)))
Next
UNICODEToASCII = sTemp
end if
End Function
Private Function URLDecode(sText)
Dim reSearch, objMatches, objMatch
Set reSearch = New RegExp
reSearch.Pattern = "%[0-9,A-F]{2}"
reSearch.Global = True
reSearch.Multiline = True
reSearch.IgnoreCase = True
' Regain our spaces
sText = Replace(sText,"+"," ")
Set objMatches = reSearch.Execute(sText)
For Each objMatch In objMatches
sText = Replace(sText,objMatch.Value,Chr(CInt("&H" & Right(objMatch.Value,2))))
Next
URLDecode = sText
End Function
Public Function ExportRequest()
ExportRequest = BinaryRead()
End Function
' Load a request session back into this object
Public Sub ImportRequest(sRequest)
Dim sText, lTemp
Dim lContent, lContentStart, lContentLength
Dim aPairs, aTokens
TotalBytes = LenB(sRequest)
Set m_objStorage = Server.CreateObject("ADODB.Stream")
Set m_objForm = Server.CreateObject("Scripting.Dictionary")
m_objStorage.Type = 1 ' adTypeBinary
m_objStorage.Open
m_objStorage.Write sRequest
m_objStorage.Position = 0
sText = m_objStorage.Read() ' Referencial string
' We don't load multi-part forms
m_MultiPart = False
m_objStorage.Position = 0
m_objStorage.Type = 2
m_objStorage.Charset = "x-ansi"
aPairs = Split(m_objStorage.ReadText,"&")
lContentStart = 0
lContentLength = 0
For lTemp = 0 To UBound(aPairs)
aTokens = Split(aPairs(lTemp),"=")
lContentStart = lContentStart + Len(aTokens(0)) + 2
m_objForm.Add aTokens(0),CStr(lContentStart) & "|" & Len(aTokens(1))
lContentStart = lContentStart + Len(aTokens(1))
Next
m_objStorage.Position = 0
m_objStorage.Type = 1
End Sub
Private Sub Class_Initialize()
Dim aContentType, sBoundary, sText, lTemp
Dim sHeader, lHeaderStart, lHeaderLength
Dim sName, lNameStart, lNameLength
Dim sFilename, lFilenameStart, lFilenameLength
Dim sContentType, lContentTypeStart, lContentTypeLength
Dim lContent, lContentStart, lContentLength
Dim lPosition, aPairs, aTokens
Set Cookies = Request.Cookies
Set ClientCertificate = Request.ClientCertificate
Set ServerVariables = Request.ServerVariables
Set QueryString = Request.QueryString
TotalBytes = Request.TotalBytes
Set m_objStorage = Server.CreateObject("ADODB.Stream")
Set m_objForm = Server.CreateObject("Scripting.Dictionary")
m_objStorage.Type = 1 ' adTypeBinary
m_objStorage.Open
lPosition = 0
' Speed up the BinaryRead method by reading in 64Kb blocks
' This can cut a 5Mb transfer from 96 seconds to 4
while lPosition < Request.TotalBytes
m_objStorage.Write Request.BinaryRead(65535) ' 64Kb at a time
lPosition = lPosition + 65535
wend
m_objStorage.Position = 0
sText = m_objStorage.Read() ' Referencial string
aContentType = Split(Request.ServerVariables("CONTENT_TYPE"),";")
if UBound(aContentType) >= 0 then
Select Case UCase(aContentType(0))
Case "MULTIPART/FORM-DATA"
m_MultiPart = True
sBoundary = LeftB(sText,InStrB(sText,ChrB(13) & ChrB(10)) - 1)
' Go on a discovery to find all the form elements
lTemp = InStrB(sText,sBoundary)
while lTemp > 0
lHeaderStart = lTemp + LenB(sBoundary) + 2 ' Include CRLF pair
lHeaderLength = InStrB(lTemp,sText,ChrB(13) & ChrB(10) & ChrB(13) & ChrB(10)) - lHeaderStart + 4
if lHeaderLength > 0 then ' Element found, add it to our dictionary
sHeader = ASCIIToUNICODE(MidB(sText,lHeaderStart,lHeaderLength))
lNameStart = InStr(sHeader,"name=") + 6
lNameLength = InStr(lNameStart,sHeader,Chr(34)) - lNameStart
sName = Mid(sHeader,lNameStart,lNameLength)
sFilename = ""
sContentType = ""
lFilenameStart = InStr(sHeader,"filename=") + 10
if lFilenameStart > 10 then
lFilenameLength = InStr(lFilenameStart,sHeader,Chr(34)) - lFilenameStart
sFilename = Mid(sHeader,lFilenameStart,lFilenameLength)
end if
lContentTypeStart = InStr(sHeader,"Content-Type: ") + 14
if lContentTypeStart > 15 then
lContentTypeLength = InStr(lContentTypeStart,sHeader,Chr(13) & Chr(10)) - lContentTypeStart
sContentType = Mid(sHeader,lContentTypeStart,lContentTypeLength)
end if
lContentStart = lHeaderStart + lHeaderLength
lContentLength = InStrB(lContentStart,sText,sBoundary) - lContentStart - 2
' Ok, we add this information to the dictionary
m_objForm.Add sName,CStr(lContentStart) & "|" & CStr(lContentLength)
if sFilename <> "" then
sPath = Left(sFilename,InStrRev(sFilename,"\"))
sFilename = Right(sFilename,Len(sFilename) - InStrRev(sFilename,"\"))
m_objForm.Add sName & "_sourcepath",sPath
m_objForm.Add sName & "_filename",sFilename
end if
if sContentType <> "" then
m_objForm.Add sName & "_mimetype",sContentType
end if
end if
lTemp = InStrB(lTemp + LenB(sBoundary),sText,sBoundary)
wend
Case Else
m_MultiPart = False
m_objStorage.Position = 0
m_objStorage.Type = 2
m_objStorage.Charset = "x-ansi"
aPairs = Split(m_objStorage.ReadText,"&")
lContentStart = 0
lContentLength = 0
For lTemp = 0 To UBound(aPairs)
aTokens = Split(aPairs(lTemp),"=")
lContentStart = lContentStart + Len(aTokens(0)) + 2
m_objForm.Add aTokens(0),CStr(lContentStart) & "|" & Len(aTokens(1))
lContentStart = lContentStart + Len(aTokens(1))
Next
m_objStorage.Position = 0
m_objStorage.Type = 1
End Select
end if
End Sub
Private Sub Class_Terminate()
Set Cookies = Nothing
Set ClientCertificate = Nothing
Set ServerVariables = Nothing
Set QueryString = Nothing
Set m_objStorage = Nothing
Set m_objForm = Nothing
End Sub
End Class
%>