146 lines
4.9 KiB
VB.net
146 lines
4.9 KiB
VB.net
Imports System.Collections.Specialized
|
|
Imports System.IO
|
|
Imports System.Runtime.InteropServices
|
|
Imports System.Windows.Forms
|
|
Imports CefSharp
|
|
Imports CefSharp.Callback
|
|
|
|
Public Class ResourceHandler
|
|
Implements IResourceHandler
|
|
|
|
Public Const DefaultMimeType As String = "text/html"
|
|
Private tempBuffer As Byte()
|
|
Public Property Charset As String
|
|
Public Property MimeType As String
|
|
Public Property Stream As Stream
|
|
Public Property StatusCode As Integer
|
|
Public Property StatusText As String
|
|
Public Property m_ResponseLength As Long?
|
|
Public Property Headers As NameValueCollection
|
|
Public Property AutoDisposeStream As Boolean
|
|
Public Property ErrorCode As CefErrorCode?
|
|
Private disposedValue As Boolean
|
|
|
|
Public Sub New(ByVal Optional mimeType As String = DefaultMimeType, ByVal Optional stream As Stream = Nothing, ByVal Optional autoDisposeStream As Boolean = False, ByVal Optional charset As String = Nothing)
|
|
If String.IsNullOrEmpty(mimeType) Then
|
|
Throw New ArgumentNullException("mimeType", "Please provide a valid mimeType")
|
|
End If
|
|
|
|
StatusCode = 200
|
|
StatusText = "OK"
|
|
mimeType = "application/pdf"
|
|
Headers = New NameValueCollection()
|
|
stream = stream
|
|
autoDisposeStream = autoDisposeStream
|
|
charset = charset
|
|
Headers.Add("Access-Control-Allow-Origin", "*")
|
|
End Sub
|
|
Public Sub GetResponseHeaders(response As IResponse, ByRef responseLength As Long, ByRef redirectUrl As String) Implements IResourceHandler.GetResponseHeaders
|
|
redirectUrl = Nothing
|
|
responseLength = -1
|
|
'response.MimeType = MimeType
|
|
response.MimeType = "application/pdf"
|
|
response.StatusCode = StatusCode
|
|
response.StatusText = StatusText
|
|
response.Headers = Headers
|
|
|
|
If Not String.IsNullOrEmpty(Charset) Then
|
|
response.Charset = Charset
|
|
End If
|
|
|
|
If Not IsNothing(m_ResponseLength) Then
|
|
responseLength = m_ResponseLength
|
|
End If
|
|
|
|
If Stream IsNot Nothing AndAlso Stream.CanSeek Then
|
|
|
|
If m_ResponseLength Is Nothing OrElse responseLength = 0 Then
|
|
responseLength = Stream.Length
|
|
End If
|
|
|
|
Stream.Position = 0
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub Cancel() Implements IResourceHandler.Cancel
|
|
Return
|
|
End Sub
|
|
|
|
Public Function Open(request As IRequest, ByRef handleRequest As Boolean, callback As ICallback) As Boolean Implements IResourceHandler.Open
|
|
Dim assembly = Reflection.Assembly.GetExecutingAssembly()
|
|
Dim resourceName = "Icarus.Icarus.pdf"
|
|
|
|
Stream = assembly.GetManifestResourceStream(resourceName)
|
|
callback.Continue()
|
|
Return True
|
|
End Function
|
|
|
|
Public Function ProcessRequest(request As IRequest, callback As ICallback) As Boolean Implements IResourceHandler.ProcessRequest
|
|
Return True
|
|
End Function
|
|
|
|
Public Function Skip(bytesToSkip As Long, ByRef bytesSkipped As Long, callback As IResourceSkipCallback) As Boolean Implements IResourceHandler.Skip
|
|
callback.Dispose()
|
|
|
|
If Stream Is Nothing OrElse Not Stream.CanSeek Then
|
|
bytesSkipped = -2
|
|
Return False
|
|
End If
|
|
|
|
bytesSkipped = bytesToSkip
|
|
Stream.Seek(bytesToSkip, SeekOrigin.Current)
|
|
Return True
|
|
End Function
|
|
|
|
Public Function Read(dataOut As Stream, ByRef bytesRead As Integer, callback As IResourceReadCallback) As Boolean Implements IResourceHandler.Read
|
|
bytesRead = 0
|
|
callback.Dispose()
|
|
|
|
If Stream Is Nothing Then
|
|
Return False
|
|
End If
|
|
|
|
If tempBuffer Is Nothing OrElse tempBuffer.Length < dataOut.Length Then
|
|
tempBuffer = New Byte(dataOut.Length - 1) {}
|
|
End If
|
|
|
|
bytesRead = Stream.Read(tempBuffer, 0, CInt(dataOut.Length))
|
|
|
|
If bytesRead = 0 Then
|
|
Return False
|
|
End If
|
|
|
|
dataOut.Write(tempBuffer, 0, bytesRead)
|
|
Return bytesRead > 0
|
|
End Function
|
|
|
|
Public Function ReadResponse(dataOut As Stream, ByRef bytesRead As Integer, callback As ICallback) As Boolean Implements IResourceHandler.ReadResponse
|
|
Throw New NotImplementedException()
|
|
End Function
|
|
|
|
Protected Overridable Sub Dispose(disposing As Boolean)
|
|
If Not disposedValue Then
|
|
If disposing Then
|
|
' TODO: dispose managed state (managed objects)
|
|
End If
|
|
|
|
' TODO: free unmanaged resources (unmanaged objects) and override finalizer
|
|
' TODO: set large fields to null
|
|
disposedValue = True
|
|
End If
|
|
End Sub
|
|
|
|
' ' TODO: override finalizer only if 'Dispose(disposing As Boolean)' has code to free unmanaged resources
|
|
' Protected Overrides Sub Finalize()
|
|
' ' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
|
|
' Dispose(disposing:=False)
|
|
' MyBase.Finalize()
|
|
' End Sub
|
|
|
|
Public Sub Dispose() Implements IDisposable.Dispose
|
|
' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
|
|
Dispose(disposing:=True)
|
|
GC.SuppressFinalize(Me)
|
|
End Sub
|
|
End Class
|