Skip to content

Commit

Permalink
move fastcgi header into records
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelneu committed May 8, 2019
1 parent e657f51 commit d09c21e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 52 deletions.
16 changes: 13 additions & 3 deletions src/Classes/FastCGIBeginRequest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Implements IFastCGIRecord

Private m_header As FastCGIHeader
Public Role As Integer
Public Flags As Byte
Public Reserved As String


Private Sub Class_Initialize()
Dim bytes As String * 5
Reserved = bytes
Set m_header = New FastCGIHeader
m_header.ProtocolVersion = 1
m_header.RequestId = m_requestId
m_header.MessageType = FastCGI.FASTCGI_TYPE_BEGIN_REQUEST
m_header.ContentLength = 8
m_header.PaddingLength = 0

Role = 1
Reserved = StringExtensions.Repeat(Chr(0), 5)
End Sub


Expand All @@ -27,8 +34,11 @@ End Sub


Private Function IFastCGIRecord_ToBytes() As String
Dim header As IFastCGIRecord
Set header = m_header

Dim bytes As String
bytes = ""
bytes = header.ToBytes()

bytes = bytes & Marshal.Int16ToBytes(Role)
bytes = bytes & Marshal.Int8ToBytes(Flags)
Expand Down
6 changes: 6 additions & 0 deletions src/Classes/FastCGIEndRequest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Implements IFastCGIRecord

Private m_header As FastCGIHeader
Public AppStatus As Long
Public ProtocolStatus As Byte
Public Reserved As String


Private Sub Class_Initialize()

End Sub


Private Sub IFastCGIRecord_FromBytes(ByVal bytes As String)
End Sub

Expand Down
19 changes: 17 additions & 2 deletions src/Classes/FastCGIHeader.cls
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ Public Reserved As String


Private Sub Class_Initialize()
Dim bytes As String * 1
Reserved = bytes
Reserved = StringExtensions.Repeat(Chr(0), 1)
End Sub


Private Sub IFastCGIRecord_FromBytes(ByVal bytes As String)
ProtocolVersion = Marshal.BytesToInt8(bytes)
bytes = StringExtensions.Substring(bytes, 1)

MessageType = Marshal.BytesToInt8(bytes)
bytes = StringExtensions.Substring(bytes, 1)

RequestId = Marshal.BytesToInt16(bytes)
bytes = StringExtensions.Substring(bytes, 2)

ContentLength = Marshal.BytesToInt16(bytes)
bytes = StringExtensions.Substring(bytes, 2)

PaddingLength = Marshal.BytesToInt8(bytes)
bytes = StringExtensions.Substring(bytes, 1)

Reserved = bytes
End Sub


Expand Down
13 changes: 11 additions & 2 deletions src/Classes/FastCGIParams.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Implements IFastCGIRecord

Private m_header As FastCGIHeader
Private m_params As Collection


Private Sub Class_Initialize()
Set m_header = New FastCGIHeader
m_header.ProtocolVersion = 1
m_header.RequestId = m_requestId
m_header.MessageType = FastCGI.FASTCGI_TYPE_PARAMS
m_header.PaddingLength = 0

Set m_params = New Collection
End Sub

Expand All @@ -33,14 +40,16 @@ End Sub


Private Function IFastCGIRecord_ToBytes() As String
Dim record As IFastCGIRecord
Dim bytes As String
bytes = ""

For Each param In m_params
Dim record As IFastCGIRecord
Set record = param
bytes = bytes & record.ToBytes()
Next

IFastCGIRecord_ToBytes = bytes
m_header.ContentLength = Len(bytes)
Set record = m_header
IFastCGIRecord_ToBytes = record.ToBytes() & bytes
End Function
28 changes: 27 additions & 1 deletion src/Classes/FastCGIStream.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,39 @@ Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Implements IFastCGIRecord

Private m_header As FastCGIHeader
Public Content As String


Private Sub Class_Initialize()
Set m_header = New FastCGIHeader
m_header.ProtocolVersion = 1
m_header.RequestId = 1
m_header.PaddingLength = 0
End Sub


Public Property Get StreamType() As Integer
StreamType = m_header.MessageType
End Property


Public Property Let StreamType(ByVal value As Integer)
If value = FastCGI.FASTCGI_TYPE_STDIN Or value <> FastCGI.FASTCGI_TYPE_STDOUT Then
m_header.MessageType = value
End If
End Property


Private Sub IFastCGIRecord_FromBytes(ByVal bytes As String)
End Sub


Private Function IFastCGIRecord_ToBytes() As String
IFastCGIRecord_ToBytes = Content
m_header.ContentLength = Len(Content)

Dim record As IFastCGIRecord
Set record = m_header

IFastCGIRecord_ToBytes = record.ToBytes() & Content
End Function
52 changes: 8 additions & 44 deletions src/Classes/FastCGIWebController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,27 @@ End Function


Private Sub SendBegin()
Dim header As FastCGIHeader
Set header = New FastCGIHeader
header.ProtocolVersion = 1
header.RequestId = m_requestId
header.MessageType = FastCGI.FASTCGI_TYPE_BEGIN_REQUEST
header.ContentLength = 8
header.PaddingLength = 0

Dim record As IFastCGIRecord
Dim bytes As String
bytes = ""

Set record = header
bytes = bytes & record.ToBytes()

Set record = New FastCGIBeginRequest
bytes = bytes & record.ToBytes()

m_clientSocket.SendString bytes
m_clientSocket.SendString record.ToBytes()
End Sub


Private Sub SendParams()
Dim params As FastCGIParams
Set params = New FastCGIParams

params.Add "SERVER_SOFTWARE", "Microsoft Excel/" & Application.version
params.Add "GATEWAY_INTERFACE", "CGI/1.1"

Dim bytes As String
Dim record As IFastCGIRecord
Set record = params
bytes = record.ToBytes()

Dim header As FastCGIHeader
Set header = New FastCGIHeader
header.ProtocolVersion = 1
header.RequestId = m_requestId
header.MessageType = FastCGI.FASTCGI_TYPE_PARAMS
header.ContentLength = Len(bytes)
header.PaddingLength = 0

Set record = header
bytes = record.ToBytes() & bytes
Dim bytes As String
bytes = record.ToBytes()

header.ContentLength = 0
Set record = header
Set record = New FastCGIParams
bytes = bytes & record.ToBytes()

m_clientSocket.SendString bytes
Expand All @@ -87,27 +61,17 @@ End Sub
Private Sub SendInput(text As String)
Dim stdin As FastCGIStream
Set stdin = New FastCGIStream
stdin.StreamType = FastCGI.FASTCGI_TYPE_STDIN
stdin.Content = text

Dim bytes As String
Dim record As IFastCGIRecord
Set record = stdin
bytes = record.ToBytes()

Dim header As FastCGIHeader
Set header = New FastCGIHeader
header.ProtocolVersion = 1
header.RequestId = m_requestId
header.MessageType = FastCGI.FASTCGI_TYPE_STDIN
header.ContentLength = Len(bytes)
header.PaddingLength = 0

Set record = header
bytes = record.ToBytes() & bytes

If Len(text) > 0 Then
header.ContentLength = 0
Set record = header
stdin.Content = ""
Set record = stdin
bytes = bytes & record.ToBytes()
End If

Expand Down

0 comments on commit d09c21e

Please sign in to comment.