Skip to content

Commit

Permalink
read and write fastcgi records on sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelneu committed May 8, 2019
1 parent c4da2b6 commit d287022
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 22 deletions.
21 changes: 19 additions & 2 deletions src/Classes/FastCGIBeginRequest.cls
Expand Up @@ -18,8 +18,8 @@ Public Reserved As String
Private Sub Class_Initialize()
Set m_header = New FastCGIHeader
m_header.ProtocolVersion = 1
m_header.RequestId = m_requestId
m_header.MessageType = FastCGI.FASTCGI_TYPE_BEGIN_REQUEST
m_header.RequestId = 1
m_header.ContentLength = 8
m_header.PaddingLength = 0

Expand All @@ -29,15 +29,32 @@ End Sub


Private Sub IFastCGIRecord_ReadFromTcpClient(client As TcpClient)
Dim record As IFastCGIRecord
Set record = m_header
record.ReadFromTcpClient client

Dim bytes As String
bytes = client.ReceiveBytes(m_header.ContentLength)

Role = Marshal.BytesToInt16(StringExtensions.Substring(bytes, 0, 2))
bytes = StringExtensions.Substring(bytes, 2)

Flags = Marshal.BytesToInt8(StringExtensions.Substring(bytes, 0, 1))
bytes = StringExtensions.Substring(bytes, 1)

Reserved = bytes

client.ReceiveBytes m_header.PaddingLength
End Sub


Private Sub IFastCGIRecord_WriteToTcpClient(client As TcpClient)
Dim header As IFastCGIRecord
Set header = m_header
header.WriteToTcpClient client

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

bytes = bytes & Marshal.Int16ToBytes(Role)
bytes = bytes & Marshal.Int8ToBytes(Flags)
Expand Down
35 changes: 35 additions & 0 deletions src/Classes/FastCGIEndRequest.cls
Expand Up @@ -17,12 +17,47 @@ Public Reserved As String

Private Sub Class_Initialize()
Set m_header = New FastCGIHeader
m_header.ProtocolVersion = 1
m_header.MessageType = FastCGI.FASTCGI_TYPE_END_REQUEST
m_header.RequestId = 1
m_header.ContentLength = 8
m_header.PaddingLength = 0

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


Private Sub IFastCGIRecord_ReadFromTcpClient(client As TcpClient)
Dim record As IFastCGIRecord
Set record = m_header
record.ReadFromTcpClient client

Dim bytes As String
bytes = client.ReceiveBytes(m_header.ContentLength)

AppStatus = Marshal.BytesToInt32(StringExtensions.Substring(bytes, 0, 4))
bytes = StringExtensions.Substring(bytes, 4)

ProtocolStatus = Marshal.BytesToInt8(StringExtensions.Substring(bytes, 0, 1))
bytes = StringExtensions.Substring(bytes, 1)

Reserved = bytes

client.ReceiveBytes m_header.PaddingLength
End Sub


Private Sub IFastCGIRecord_WriteToTcpClient(client As TcpClient)
Dim record As IFastCGIRecord
Set record = m_header
record.WriteToTcpClient client

Dim bytes As String
bytes = ""

bytes = bytes & Marshal.Int32ToBytes(AppStatus)
bytes = bytes & Marshal.Int8ToBytes(ProtocolStatus)
bytes = bytes & Reserved

client.SendString bytes
End Sub
36 changes: 33 additions & 3 deletions src/Classes/FastCGIParams.cls
Expand Up @@ -16,8 +16,8 @@ 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.RequestId = 1
m_header.PaddingLength = 0

Set m_params = New Collection
Expand All @@ -36,6 +36,37 @@ End Sub


Private Sub IFastCGIRecord_ReadFromTcpClient(client As TcpClient)
Dim record As IFastCGIRecord
Set record = m_header
record.ReadFromTcpClient client

Set m_params = New Collection

Dim bytes As String
bytes = client.ReceiveBytes(m_header.ContentLength)

Dim keyLength As Integer
Dim valueLength As Integer
Dim param As FastCGIParam

Do While Len(bytes) > 0
keyLength = Marshal.BytesToInt8(StringExtensions.Substring(bytes, 0, 1))
bytes = StringExtensions.Substring(bytes, 1)
valueLength = Marshal.BytesToInt8(StringExtensions.Substring(bytes, 0, 1))
bytes = StringExtensions.Substring(bytes, 1)

Set param = New FastCGIParam

param.key = StringExtensions.Substring(bytes, 0, keyLength)
bytes = StringExtensions.Substring(bytes, keyLength)

param.value = StringExtensions.Substring(bytes, 0, valueLength)
bytes = StringExtensions.Substring(bytes, valueLength)

m_params.Add param
Loop

client.ReceiveBytes m_header.PaddingLength
End Sub


Expand All @@ -53,7 +84,6 @@ Private Sub IFastCGIRecord_WriteToTcpClient(client As TcpClient)

m_header.ContentLength = Len(bytes)
Set record = m_header
bytes = record.ToBytes() & bytes

record.WriteToTcpClient client
client.SendString bytes
End Sub
9 changes: 8 additions & 1 deletion src/Classes/FastCGIStream.cls
Expand Up @@ -34,6 +34,12 @@ End Property


Private Sub IFastCGIRecord_ReadFromTcpClient(client As TcpClient)
Dim record As IFastCGIRecord
Set record = m_header
record.ReadFromTcpClient client

Content = client.ReceiveBytes(m_header.ContentLength)
client.ReceiveBytes m_header.PaddingLength
End Sub


Expand All @@ -42,6 +48,7 @@ Private Sub IFastCGIRecord_WriteToTcpClient(client As TcpClient)

Dim record As IFastCGIRecord
Set record = m_header
record.WriteToTcpClient client

client.SendString record.ToBytes() & Content
client.SendString Content
End Sub
68 changes: 59 additions & 9 deletions src/Classes/FastCGIWebController.cls
Expand Up @@ -30,15 +30,22 @@ Private Function IWebController_ProcessRequest(request As HttpRequest) As HttpRe
End Function


Private Sub SendBegin()
Private Sub WriteBegin()
Dim record As IFastCGIRecord
Set record = New FastCGIBeginRequest

record.WriteToTcpClient client
record.WriteToTcpClient m_clientSocket
End Sub


Private Sub SendParams()
Private Function ReadBegin() As FastCGIBeginRequest
Dim record As IFastCGIRecord
Set record = New FastCGIBeginRequest
record.ReadFromTcpClient m_clientSocket
Set ReadBegin = record
End Function


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

Expand All @@ -47,14 +54,22 @@ Private Sub SendParams()

Dim record As IFastCGIRecord
Set record = params
record.WriteToTcpClient client
record.WriteToTcpClient m_clientSocket

Set record = New FastCGIParams
record.WriteToTcpClient client
record.WriteToTcpClient m_clientSocket
End Sub


Private Sub SendInput(text As String)
Private Function ReadParams() As FastCGIParams
Dim record As IFastCGIRecord
Set record = New FastCGIParams
record.ReadFromTcpClient m_clientSocket
Set ReadParams = record
End Function


Private Sub WriteInput(text As String)
Dim stdin As FastCGIStream
Set stdin = New FastCGIStream
stdin.StreamType = FastCGI.FASTCGI_TYPE_STDIN
Expand All @@ -63,11 +78,46 @@ Private Sub SendInput(text As String)
Dim bytes As String
Dim record As IFastCGIRecord
Set record = stdin
record.WriteToTcpClient client
record.WriteToTcpClient m_clientSocket

If Len(text) > 0 Then
stdin.Content = ""
Set record = stdin
record.WriteToTcpClient client
record.WriteToTcpClient m_clientSocket
End If
End Sub


Private Sub WriteOutput(text As String)
Dim stdin As FastCGIStream
Set stdin = New FastCGIStream
stdin.StreamType = FastCGI.FASTCGI_TYPE_STDOUT
stdin.Content = text

Dim bytes As String
Dim record As IFastCGIRecord
Set record = stdin
record.WriteToTcpClient m_clientSocket
End Sub


Private Sub ReadStream()
Dim record As IFastCGIRecord
Set record = New FastCGIStream
record.ReadFromTcpClient m_clientSocket
End Sub


Private Sub WriteEnd()
Dim record As IFastCGIRecord
Set record = New FastCGIEndRequest
record.WriteToTcpClient m_clientSocket
End Sub


Private Sub ReadEnd()
Dim record As IFastCGIRecord
Set record = New FastCGIEndRequest
record.ReadFromTcpClient m_clientSocket
End Sub

22 changes: 15 additions & 7 deletions src/Classes/TcpClient.cls
Expand Up @@ -41,21 +41,29 @@ Public Function SendString(ByVal message As String) As Long
End Function


Public Function ReceiveString() As String
Dim message As String
Dim buffer As String * 1024
Public Function ReceiveBytes(ByVal bytes As Long) As String
Dim buffer As String
buffer = StringExtensions.Repeat(Chr(0), bytes)

Dim readBytes As Long
readBytes = wsock32.recv(m_clientSocket, buffer, bytes, 0)

ReceiveBytes = StringExtensions.Substring(buffer, 0, readBytes)
End Function


Public Function ReceiveString() As String
Dim buffer As String
Dim message As String
message = ""

Do
buffer = ""
readBytes = wsock32.recv(m_clientSocket, buffer, Len(buffer), 0)
buffer = ReceiveBytes(1024)

If readBytes > 0 Then
If Len(buffer) > 0 Then
message = message & Trim(buffer)
End If
Loop While readBytes > 0
Loop While Len(buffer) > 0

ReceiveString = Trim(message)
End Function
Expand Down

0 comments on commit d287022

Please sign in to comment.