1: Imports Microsoft.VisualBasic
2: Imports System.IO
3: Imports System.Text.RegularExpressions
4: Imports System.Configuration
5:
6:
7: Public Class BuildTokenFilter
8: Inherits Stream
9: Private _responseStream As Stream
10: Public Sub New(ByVal responseStream As Stream)
11: _responseStream = responseStream
12: End Sub
13: Public Overrides ReadOnly Property CanRead() As Boolean
14: Get
15: Return _responseStream.CanRead
16: End Get
17: End Property
18: Public Overrides ReadOnly Property CanSeek() As Boolean
19: Get
20: Return _responseStream.CanSeek
21: End Get
22: End Property
23:
24: Public Overrides ReadOnly Property CanWrite() As Boolean
25: Get
26: Return _responseStream.CanWrite
27: End Get
28: End Property
29:
30: Public Overrides Sub Flush()
31: _responseStream.Flush()
32: End Sub
33:
34: Public Overrides ReadOnly Property Length() As Long
35: Get
36: Return _responseStream.Length
37: End Get
38: End Property
39:
40: Public Overrides Property Position() As Long
41: Get
42: Return _responseStream.Position
43: End Get
44: Set(ByVal value As Long)
45: _responseStream.Position = value
46: End Set
47: End Property
48:
49: Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
50: Return _responseStream.Read(buffer, offset, count)
51: End Function
52: Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
53: _responseStream.Seek(offset, origin)
54: End Function
55:
56: Public Overrides Sub SetLength(ByVal value As Long)
57: _responseStream.SetLength(value)
58: End Sub
59: Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
60: Dim strRegex As String = "src=(?<Link>.*js)"
61: Dim BuildTokenString As String = "?token=" & IIf(ConfigurationManager.AppSettings("BuildToken") = Nothing, "1.0", ConfigurationManager.AppSettings("BuildToken"))
62: Dim objRegex As New Regex(strRegex)
63: Dim html As String = System.Text.Encoding.UTF8.GetString(buffer)
64: Dim extCharCount As Integer = 0
65:
66: Dim objCol As MatchCollection = objRegex.Matches(html)
67: For Each m As Match In objCol
68: extCharCount += BuildTokenString.Length
69: Dim newJSValue As String = m.Value & BuildTokenString
70: html = html.Replace(m.Value, newJSValue)
71: Next
72: buffer = System.Text.Encoding.UTF8.GetBytes(html)
73: _responseStream.Write(buffer, offset, count + extCharCount)
74: End Sub
75: End Class