-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.vb
370 lines (318 loc) · 12.2 KB
/
Request.vb
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
Public Class Request
Inherits SimpleWorkerRequest
Private Enum CompressType
None
Xml
JavaScript
End Enum
Private m_State As ReadHeadersResult
Private m_VirtualPath As String
Private m_PhysicalPath As String
Private m_Stream As Stream
Private m_BodyBuffer As New MemoryStream
Private m_XmlCompress As CompressType
Public Sub New(ByRef state As ReadHeadersResult, ByVal objStream As Stream, ByVal VirtualPath As String, ByVal PhysicalPath As String)
MyBase.New(String.Empty, String.Empty, Nothing)
m_State = state
m_VirtualPath = VirtualPath
m_PhysicalPath = PhysicalPath
m_Stream = objStream
End Sub
#Region "Реализация HttpWorkerRequest"
Public Overrides Function GetUriPath() As String
Return m_State.Path
End Function
Public Overrides Function GetQueryString() As String
Return m_State.QueryString
End Function
'Public Overrides Function GetQueryStringRawBytes() As Byte()
' Return m_QueryStringBytes
'End Function
Public Overrides Function GetRawUrl() As String
Return m_State.Url
End Function
Public Overrides Function GetHttpVerbName() As String
Return m_State.GetHttpMethodString
End Function
Public Overrides Function GetHttpVersion() As String
Select Case m_State.HttpVersion
Case ReadHeadersResult.HttpVersions.Http11
Return "HTTP/1.1"
Case ReadHeadersResult.HttpVersions.Http10
Return "HTTP/1.0"
Case ReadHeadersResult.HttpVersions.Http09
Return "HTTP/0.9"
Case Else
Return "HTTP/0.9"
End Select
End Function
Public Overrides Function GetRemoteAddress() As String
Return m_State.RemoteIP
End Function
Public Overrides Function GetRemotePort() As Integer
Return 0
End Function
Public Overrides Function GetLocalAddress() As String
Return m_State.LocalIp
End Function
Public Overrides Function GetLocalPort() As Integer
Return m_State.LocalPort
End Function
Public Overrides Function GetFilePath() As String
Return m_State.FilePath
End Function
Public Overrides Function GetFilePathTranslated() As String
Return m_State.PathTranslated
End Function
Public Overrides Function GetPathInfo() As String
' Это свойство не поддерживается, так как в каталогах может быть точка
Return String.Empty
End Function
Public Overrides Function GetAppPath() As String
Return m_VirtualPath
End Function
Public Overrides Function GetAppPathTranslated() As String
Return m_PhysicalPath
End Function
Public Overrides Function GetPreloadedEntityBody() As Byte()
Dim PreloadedContentLength As Integer = m_State.HeaderBytesLength - m_State.EndHeadersOffset
If PreloadedContentLength > 0 Then
Dim PreloadedContent(PreloadedContentLength - 1) As Byte
Buffer.BlockCopy(m_State.HeaderBytes, m_State.EndHeadersOffset, PreloadedContent, 0, PreloadedContentLength)
Return PreloadedContent
Else
Return Nothing
End If
End Function
Public Overrides Function IsEntireEntityBodyIsPreloaded() As Boolean
Return m_State.HeaderBytesLength - m_State.EndHeadersOffset > 0
End Function
Public Overrides Function ReadEntityBody(ByVal buffer() As Byte, ByVal size As Integer) As Integer
Dim bytesRead As Integer
Try
bytesRead = m_Stream.Read(buffer, 0, size)
Catch ex As Exception
Return 0
End Try
Return bytesRead
End Function
Public Overrides Function GetKnownRequestHeader(ByVal index As Integer) As String
Return m_State.RequestHeaders(index)
End Function
Public Overrides Function GetUnknownRequestHeader(ByVal name As String) As String
For i As Integer = 0 To m_State.UnknownRequestHeadersCount - 1
If m_State.UnknownRequestHeadersName(i) = name Then
Return m_State.UnknownRequestHeadersValue(i)
End If
Next
Return String.Empty
End Function
Public Overloads Overrides Function GetServerName() As String
Return m_State.ServerName
End Function
Public Overrides Function GetServerVariable(ByVal name As String) As String
Select Case name
Case "ALL_RAW"
Return m_State.AllRawRequestHeaders
Case "SERVER_PROTOCOL"
Return GetHttpVersion()
Case Else
Return String.Empty
End Select
End Function
Public Overrides Function MapPath(ByVal path As String) As String
Return m_State.MapPath(m_VirtualPath, path, m_PhysicalPath)
End Function
Public Overrides Sub SendStatus(ByVal statusCode As Integer, ByVal statusDescription As String)
m_State.StatusCode = statusCode
If statusCode >= 400 Then
m_State.KeepAlive = False
End If
End Sub
Public Overrides Sub SendKnownResponseHeader(ByVal index As Integer, ByVal value As String)
If Not m_State.HeadersSent Then
Select Case index
Case HeaderConnection, HeaderDate
' Игнорировать эти заголовки
Return
Case HeaderContentType
Dim strContentType = value.Split(";"c)(0)
Select Case strContentType
Case "application/x-javascript"
' Заменить заголовок устаревшего типа
value = "application/javascript; charset=utf-8"
strContentType = "application/javascript"
m_XmlCompress = CompressType.JavaScript
Case "application/javascript"
value = "application/javascript; charset=utf-8"
m_XmlCompress = CompressType.JavaScript
Case "application/xhtml+xml", "text/html"
' Необходимо применить сжатие
' Удаление лишних пробелов, переносов строк и табуляций
m_XmlCompress = CompressType.Xml
End Select
' Проверить на текстовое содержимое, чтобы можно было включать сжатие
Dim kvp = m_State.ContentTypes.Find(Function(x) x.ContentType = strContentType)
m_State.AddCompressionMethodHeader(If(kvp Is Nothing, False, kvp.IsTextFormat))
End Select
m_State.ResponseHeaders(index) = value
End If
End Sub
Public Overrides Sub SendUnknownResponseHeader(ByVal name As String, ByVal value As String)
If Not m_State.HeadersSent Then
m_State.UnknownResponseHeaders.AppendLine(name & ": " & value)
End If
End Sub
Public Overrides Sub SendCalculatedContentLength(ByVal contentLength As Integer)
' Ничего не делаем
End Sub
Public Overrides Function HeadersSent() As Boolean
Return m_State.HeadersSent
End Function
Public Overrides Function IsClientConnected() As Boolean
Return True
End Function
Public Overrides Sub CloseConnection()
End Sub
''' <summary>
''' Добавление данных в поток из *.aspx файлов
''' </summary>
Public Overloads Overrides Sub SendResponseFromMemory(ByVal data() As Byte, ByVal length As Integer)
If length > 0 Then
m_BodyBuffer.Write(data, 0, length)
End If
End Sub
Public Overloads Overrides Sub SendResponseFromFile(ByVal filename As String, ByVal offset As Long, ByVal length As Long)
If length > 0 Then
Dim f As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
SendResponseFromFileStream(f, offset, length)
f.Close()
End If
End Sub
Public Overloads Overrides Sub SendResponseFromFile(ByVal handle As IntPtr, ByVal offset As Long, ByVal length As Long)
If length > 0 Then
Dim sh As Microsoft.Win32.SafeHandles.SafeFileHandle = New Microsoft.Win32.SafeHandles.SafeFileHandle(handle, True)
Dim f As New FileStream(sh, FileAccess.Read)
SendResponseFromFileStream(f, offset, length)
f.Close()
End If
End Sub
Public Overrides Sub FlushResponse(ByVal finalFlush As Boolean)
Select Case m_XmlCompress
Case CompressType.Xml
' Сжать содержимое
m_BodyBuffer.Seek(0, SeekOrigin.Begin)
Using ms As New MemoryStream ' сжатое содержимое
Using sr As New StreamReader(m_BodyBuffer) ' читатель
Dim strLine = sr.ReadLine
Do Until strLine Is Nothing
' Очистить строку от пробелов и табуляций
strLine = strLine.Trim(" "c, " "c)
' Получить байты строки
Dim bytes = Encoding.UTF8.GetBytes(If(strLine.EndsWith("//<![CDATA["), strLine & vbCrLf, strLine))
' Записать
ms.Write(bytes, 0, bytes.Length)
strLine = sr.ReadLine
Loop
End Using
' Пересоздать поток ответа клиенту
m_BodyBuffer.Close()
m_BodyBuffer = New MemoryStream
' Записать сжатое содержимое в поток ответа клиенту
ms.Seek(0, SeekOrigin.Begin)
ms.CopyTo(m_BodyBuffer)
End Using
Case CompressType.JavaScript
' Сжать содержимое
m_BodyBuffer.Seek(0, SeekOrigin.Begin)
Using ms As New MemoryStream ' сжатое содержимое
Using sr As New StreamReader(m_BodyBuffer) ' читатель
Dim strLine = sr.ReadLine
Do Until strLine Is Nothing
' Очистить строку от пробелов и табуляций
strLine = strLine.Trim(" "c, " "c)
' Получить байты строки
Dim bytes = Encoding.UTF8.GetBytes(If(strLine.StartsWith("//"), String.Empty, If(strLine = "xmlRequestFrame.style.top = ""-100px""", "xmlRequestFrame.style.top = ""-100px"";", strLine & " ")))
' Записать
ms.Write(bytes, 0, bytes.Length)
strLine = sr.ReadLine
Loop
End Using
' Пересоздать поток ответа клиенту
m_BodyBuffer.Close()
m_BodyBuffer = New MemoryStream
' Записать сжатое содержимое в поток ответа клиенту
ms.Seek(0, SeekOrigin.Begin)
ms.CopyTo(m_BodyBuffer)
End Using
End Select
' Здесь можно сделать грязный хак
' чтобы кешировать ресурсы типа axd
If Not m_State.HeadersSent Then
m_State.WriteHeaders(m_Stream, m_BodyBuffer.Length)
m_State.HeadersSent = True
End If
If finalFlush Then
m_BodyBuffer.Seek(0, SeekOrigin.Begin)
If m_State.StatusCode >= 500 Then
' Нужно протоколировать ошибки сервера
ClientConnection.WriteLog(m_State.LockObject, m_State.Logs, Encoding.UTF8.GetString(m_BodyBuffer.ToArray))
m_BodyBuffer.Seek(0, SeekOrigin.Begin)
End If
m_State.CopyBodyStreamToClientStream(m_Stream, m_BodyBuffer)
m_State.Flush(m_Stream)
m_BodyBuffer.Close()
m_BodyBuffer.Dispose()
End If
End Sub
Public Overrides Sub EndOfRequest()
End Sub
' проходит ли соединение через SSL
Public Overrides Function IsSecure() As Boolean
Return m_State.IsSecure
End Function
' Также нужно реализовать
' GetClientCertificateValidUntil
' GetClientCertificateValidFrom
' GetClientCertificatePublicKey
' GetClientCertificateEncoding
' GetClientCertificateBinaryIssuer
' GetClientCertificate
#End Region
Private Sub SendResponseFromFileStream(ByVal f As FileStream, ByVal offset As Long, ByVal length As Long)
Const maxChunkLength As Integer = 64 * 1024
Dim fileSize As Long = f.Length
If length = -1 Then
length = fileSize - offset
End If
If length = 0 OrElse offset < 0 OrElse length > fileSize - offset Then
Return
End If
If offset > 0 Then
f.Seek(offset, SeekOrigin.Begin)
End If
If length <= maxChunkLength Then
Dim fileBytes(CInt(length) - 1) As Byte
Dim bytesRead As Integer = f.Read(fileBytes, 0, CInt(length))
SendResponseFromMemory(fileBytes, bytesRead)
Else
Dim chunk(maxChunkLength - 1) As Byte
Dim bytesRemaining As Integer = CInt(length)
Do While bytesRemaining > 0
Dim bytesToRead As Integer
If bytesRemaining < maxChunkLength Then
bytesToRead = bytesRemaining
Else
bytesToRead = maxChunkLength
End If
Dim bytesRead As Integer = f.Read(chunk, 0, bytesToRead)
SendResponseFromMemory(chunk, bytesRead)
bytesRemaining -= bytesRead
' Сбросить все данные в поток
If bytesRemaining > 0 AndAlso bytesRead > 0 Then
FlushResponse(False)
End If
Loop
End If
End Sub
End Class