| 1 |
#Region "Microsoft.VisualBasic::a38716614aab2999e8cfa50f8ecbc47e, Microsoft.VisualBasic.Core\Net\Protocol\RequestStream.vb"
|
| 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 |
#End Region
|
| 59 |
|
| 60 |
Imports System.Runtime.CompilerServices
|
| 61 |
Imports System.Text
|
| 62 |
Imports System.Xml.Serialization
|
| 63 |
Imports Microsoft.VisualBasic.Language
|
| 64 |
Imports Microsoft.VisualBasic.Net.Http
|
| 65 |
Imports Microsoft.VisualBasic.Scripting.Abstract
|
| 66 |
Imports Microsoft.VisualBasic.Serialization.JSON
|
| 67 |
Imports Microsoft.VisualBasic.Text
|
| 68 |
|
| 69 |
Namespace Net.Protocols
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
<Serializable> Public Class RequestStream : Inherits RawStream
|
| 78 |
Implements ISerializable
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
<XmlAttribute("Entry")>
|
| 85 |
Public Property ProtocolCategory As Int64
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
<XmlAttribute("Protocol")>
|
| 92 |
Public Property Protocol As Int64
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
<XmlAttribute("bufLen")>
|
| 98 |
Public Property BufferLength As Int64
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
<XmlAttribute("rawBuf")>
|
| 104 |
Public Property ChunkBuffer As Byte()
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
Public ReadOnly Property FullRead As Boolean
|
| 111 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 112 |
Get
|
| 113 |
Return BufferLength = ChunkBuffer.Length
|
| 114 |
End Get
|
| 115 |
End Property
|
| 116 |
|
| 117 |
Sub New(protocolCategory&, protocol&, buffer As Byte())
|
| 118 |
Me.ProtocolCategory = protocolCategory
|
| 119 |
Me.Protocol = protocol
|
| 120 |
Me.BufferLength = buffer.Length
|
| 121 |
Me.ChunkBuffer = buffer
|
| 122 |
End Sub
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
Sub New(protocolCategory&, protocol&)
|
| 130 |
Call Me.New(protocolCategory, protocol, New Byte() {})
|
| 131 |
End Sub
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
Sub New(ProtocolCategory As Long, Protocol As Long, str As String)
|
| 140 |
Call Me.New(ProtocolCategory, Protocol, UTF8WithoutBOM.GetBytes(str))
|
| 141 |
End Sub
|
| 142 |
|
| 143 |
Sub New(ProtocolCategory As Long, Protocol As Long, str As String, encoding As Encoding)
|
| 144 |
Call Me.New(ProtocolCategory, Protocol, encoding.GetBytes(str))
|
| 145 |
End Sub
|
| 146 |
|
| 147 |
Sub New(ProtocolCategory As Long, Protocol As Long, buffer As ISerializable)
|
| 148 |
Call Me.New(ProtocolCategory, Protocol, buffer.Serialize)
|
| 149 |
End Sub
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
Sub New(data As String)
|
| 156 |
Call Me.New(HTTP_RFC.RFC_OK, HTTP_RFC.RFC_OK, data)
|
| 157 |
End Sub
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
Sub New(rawStream As Byte())
|
| 164 |
Call MyBase.New(rawStream)
|
| 165 |
|
| 166 |
Dim bitChunk As Byte() = New Byte(INT64 - 1) {}
|
| 167 |
Dim p As int = Scan0
|
| 168 |
|
| 169 |
Call Array.ConstrainedCopy(rawStream, ++p, bitChunk, Scan0, INT64)
|
| 170 |
Me.ProtocolCategory = BitConverter.ToInt64(bitChunk, Scan0)
|
| 171 |
|
| 172 |
Call Array.ConstrainedCopy(rawStream, ++(p + INT64), bitChunk, Scan0, INT64)
|
| 173 |
Me.Protocol = BitConverter.ToInt64(bitChunk, Scan0)
|
| 174 |
|
| 175 |
bitChunk = New Byte(INT64 - 1) {}
|
| 176 |
Call Array.ConstrainedCopy(rawStream, p + INT64, bitChunk, Scan0, INT64)
|
| 177 |
|
| 178 |
Me.BufferLength = BitConverter.ToInt64(bitChunk, Scan0)
|
| 179 |
Me.ChunkBuffer = New Byte(Me.BufferLength - 1) {}
|
| 180 |
|
| 181 |
If CLng(p + INT64) + BufferLength > rawStream.Length Then
|
| 182 |
|
| 183 |
ChunkBuffer = New Byte() {}
|
| 184 |
Else
|
| 185 |
Call Array.ConstrainedCopy(
|
| 186 |
rawStream, p,
|
| 187 |
ChunkBuffer, Scan0, BufferLength
|
| 188 |
)
|
| 189 |
End If
|
| 190 |
End Sub
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 198 |
Public Function GetUTF8String() As String
|
| 199 |
Return UTF8WithoutBOM.GetString(ChunkBuffer)
|
| 200 |
End Function
|
| 201 |
|
| 202 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 203 |
Public Function GetString(encoding As Encoding) As String
|
| 204 |
Return encoding.GetString(ChunkBuffer)
|
| 205 |
End Function
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 215 |
Public Function LoadObject(Of T)(handler As LoadObject(Of T)) As T
|
| 216 |
Return handler(GetUTF8String)
|
| 217 |
End Function
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 226 |
Public Function LoadObject(Of T)() As T
|
| 227 |
Return GetUTF8String.LoadJSON(Of T)
|
| 228 |
End Function
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
Public Overloads Function GetRawStream(Of TStream As RawStream)() As TStream
|
| 236 |
Return GetRawStream(Of TStream)(rawStream:=ChunkBuffer)
|
| 237 |
End Function
|
| 238 |
|
| 239 |
Public Overrides Function ToString() As String
|
| 240 |
Dim str As String = $"(string) {NameOf(ChunkBuffer)}:={GetUTF8String()}; {TotalBytes()} bytes"
|
| 241 |
Return $"{NameOf(ProtocolCategory)}:={ProtocolCategory}; {NameOf(Protocol)}:={Protocol}; {NameOf(BufferLength)}:={BufferLength}; // {str}"
|
| 242 |
End Function
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
Public Shared Function CreateProtocol(Of T)(category As Long, protocol As Long, params As T) As RequestStream
|
| 253 |
Dim json As String = params.GetJson
|
| 254 |
Return New RequestStream(category, protocol, json)
|
| 255 |
End Function
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
Public Shared Function CreatePackage(Of T)(obj As T) As RequestStream
|
| 264 |
Return New RequestStream(HTTP_RFC.RFC_OK, HTTP_RFC.RFC_OK, obj.GetJson)
|
| 265 |
End Function
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
Public Shared Function CreatePackage(pack As Byte()) As RequestStream
|
| 273 |
Return New RequestStream(HTTP_RFC.RFC_OK, HTTP_RFC.RFC_OK, pack)
|
| 274 |
End Function
|
| 275 |
|
| 276 |
Private Shared ReadOnly ___offset As Byte() = New Byte() {RequestStream.BITWISE_FLAG}
|
| 277 |
|
| 278 |
Const BITWISE_FLAG As Byte = 255
|
| 279 |
Const MIN_LEN As Integer = INT64 + 1 + INT64 + 1 + INT64
|
| 280 |
|
| 281 |
Public Shared Function IsAvaliableStream(stream As Byte()) As Boolean
|
| 282 |
If stream.Length < MIN_LEN Then
|
| 283 |
Return False
|
| 284 |
End If
|
| 285 |
|
| 286 |
If stream(INT64) <> BITWISE_FLAG OrElse stream(INT64 + 1 + INT64) <> BITWISE_FLAG Then
|
| 287 |
Return False
|
| 288 |
Else
|
| 289 |
Return True
|
| 290 |
End If
|
| 291 |
End Function
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
Public ReadOnly Property TotalBytes() As Long
|
| 298 |
Get
|
| 299 |
Return INT64 + 1 +
|
| 300 |
INT64 + 1 +
|
| 301 |
INT64 +
|
| 302 |
BufferLength
|
| 303 |
End Get
|
| 304 |
End Property
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
Public Overrides Function Serialize() As Byte() Implements ISerializable.Serialize
|
| 311 |
Dim ProtocolCategory As Byte() = BitConverter.GetBytes(Me.ProtocolCategory)
|
| 312 |
Dim Protocol As Byte() = BitConverter.GetBytes(Me.Protocol)
|
| 313 |
Dim BufferLength As Byte() = BitConverter.GetBytes(Me.BufferLength)
|
| 314 |
Dim bufs As Byte() = New Byte(TotalBytes - 1) {}
|
| 315 |
Dim p As int = Scan0
|
| 316 |
Dim l As New int
|
| 317 |
|
| 318 |
Call Array.ConstrainedCopy(ProtocolCategory, Scan0, bufs, p << (l = ProtocolCategory.Length), l)
|
| 319 |
Call Array.ConstrainedCopy(___offset, Scan0, bufs, ++p, 1)
|
| 320 |
Call Array.ConstrainedCopy(Protocol, Scan0, bufs, p << (l = Protocol.Length), l)
|
| 321 |
Call Array.ConstrainedCopy(___offset, Scan0, bufs, ++p, 1)
|
| 322 |
Call Array.ConstrainedCopy(BufferLength, Scan0, bufs, p << (l = BufferLength.Length), l)
|
| 323 |
Call Array.ConstrainedCopy(Me.ChunkBuffer, Scan0, bufs, p << (l = Me.BufferLength), l)
|
| 324 |
|
| 325 |
Return bufs
|
| 326 |
End Function
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
Public Const SYS_PROTOCOL As Long = -8888888L
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
Public Enum Protocols As Long
|
| 337 |
OK = HTTP_RFC.RFC_OK
|
| 338 |
|
| 339 |
Ping = 10L
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
SSL = -107L
|
| 344 |
SSLHandshake = -200L
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
SSL_PublicToken = -405L
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
InvalidCertificates = -404L
|
| 353 |
|
| 354 |
NOT_SYS = 0L
|
| 355 |
End Enum
|
| 356 |
|
| 357 |
Public Shared Function SystemProtocol(Protocol As Protocols, Message As String) As RequestStream
|
| 358 |
Return New RequestStream(SYS_PROTOCOL, Protocol, Message)
|
| 359 |
End Function
|
| 360 |
|
| 361 |
Public ReadOnly Property TryGetSystemProtocol As Protocols
|
| 362 |
Get
|
| 363 |
Try
|
| 364 |
Return CType(Protocol, Protocols)
|
| 365 |
Catch ex As Exception
|
| 366 |
Return Protocols.NOT_SYS
|
| 367 |
End Try
|
| 368 |
End Get
|
| 369 |
End Property
|
| 370 |
|
| 371 |
Public ReadOnly Property IsPing As Boolean
|
| 372 |
Get
|
| 373 |
Return ProtocolCategory = SYS_PROTOCOL AndAlso Protocol = Protocols.Ping
|
| 374 |
End Get
|
| 375 |
End Property
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
Public ReadOnly Property IsSSLProtocol As Boolean
|
| 382 |
Get
|
| 383 |
Return ProtocolCategory = SYS_PROTOCOL AndAlso Protocol = Protocols.SSL
|
| 384 |
End Get
|
| 385 |
End Property
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
Public ReadOnly Property IsSSL_PublicToken As Boolean
|
| 392 |
Get
|
| 393 |
Return ProtocolCategory = SYS_PROTOCOL AndAlso Protocol = Protocols.SSL_PublicToken
|
| 394 |
End Get
|
| 395 |
End Property
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
Public ReadOnly Property IsPlantText As Boolean
|
| 402 |
Get
|
| 403 |
Return ProtocolCategory = Scan0 AndAlso Protocol = Scan0
|
| 404 |
End Get
|
| 405 |
End Property
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
Public ReadOnly Property IsSSLHandshaking As Boolean
|
| 412 |
Get
|
| 413 |
Return ProtocolCategory = SYS_PROTOCOL AndAlso Protocol = Protocols.SSLHandshake
|
| 414 |
End Get
|
| 415 |
End Property
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
Public Overloads Shared Operator =(a As RequestStream, b As RequestStream) As Boolean
|
| 424 |
Return String.Equals(a.GetUTF8String, b.GetUTF8String)
|
| 425 |
End Operator
|
| 426 |
|
| 427 |
Public Overloads Shared Operator <>(a As RequestStream, b As RequestStream) As Boolean
|
| 428 |
Return Not String.Equals(a.GetUTF8String, b.GetUTF8String)
|
| 429 |
End Operator
|
| 430 |
|
| 431 |
Public Overloads Shared Operator =(request As String, requestStream As RequestStream) As Boolean
|
| 432 |
Return String.Equals(request, requestStream.GetUTF8String)
|
| 433 |
End Operator
|
| 434 |
|
| 435 |
Public Overloads Shared Operator <>(request As String, requestStream As RequestStream) As Boolean
|
| 436 |
Return Not String.Equals(request, requestStream.GetUTF8String)
|
| 437 |
End Operator
|
| 438 |
|
| 439 |
Public Overloads Shared Operator =(requestStream As RequestStream, request As String) As Boolean
|
| 440 |
Return String.Equals(request, requestStream.GetUTF8String)
|
| 441 |
End Operator
|
| 442 |
|
| 443 |
Public Overloads Shared Operator <>(requestStream As RequestStream, request As String) As Boolean
|
| 444 |
Return Not String.Equals(request, requestStream.GetUTF8String)
|
| 445 |
End Operator
|
| 446 |
End Class
|
| 447 |
End Namespace
|