-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMessage.rbbas
211 lines (186 loc) · 5.06 KB
/
Message.rbbas
1
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#tag Class
Protected Class Message
#tag Method, Flags = &h1
Protected Sub Constructor(Headers As HTTP.Headers)
mHeaders = Headers
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub Constructor(CopyFrom As HTTP.Message)
Me.ProtocolVersion = CopyFrom.ProtocolVersion
Me.Path = CopyFrom.Path
If Me.ProtocolVersion > 1.11 Or Me.ProtocolVersion < 1.0 Then RaiseEvent HTTPDebug("Warn: Unknown HTTP version.", -1)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub DeleteAllHeaders()
mHeaders.DeleteAllHeaders
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function HasHeader(HeaderName As String) As Boolean
For i As Integer = 0 To mHeaders.Count - 1
If mHeaders.Name(i) = HeaderName Then Return True
Next
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function Header(Headername As String) As String
For i As Integer = 0 To mHeaders.Count - 1
If mHeaders.Name(i) = headername Then
Return mHeaders.Value(i)
End If
Next
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub Header(Name As String, AllowDuplicate As Boolean = False, Assigns Value As String)
If Not AllowDuplicate And Me.HasHeader(Name) Then mHeaders.Delete(Name)
mHeaders.AppendHeader(Name, Value)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function IsChunked() As Boolean
If Me.Header("Transfer-Encoding") = "chunked" Then Return True
Dim i As Integer = InStrB(MessageBody, CRLF)
If i <= 0 Then Return False
If i > 64 Then Return False 'too big to be a chunk header
Dim hed, sz As String
hed = NthFieldB(MessageBody, CRLF, 1)
sz = NthField(hed, ";", 1)
If IsNumeric(sz) Then
Return Val("&h" + sz) > 0
End If
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function IsCompressed() As Boolean
If Me.Header("Content-Encoding") = "deflate" Then Return True
If Me.Header("Content-Encoding") = "gzip" Then Return True
If LeftB(MessageBody, 2) = Chr(&h1F) + Chr(&h8B) Then Return True
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function ToString(HeadersOnly As Boolean) As String
Dim data As String
If Me IsA HTTP.Request Then
data = mHeaders.Source + CRLF + CRLF
Else
data = mHeaders.Source(True) + CRLF + CRLF
End If
If Not HeadersOnly Then data = data + Me.MessageBody
Return data
End Function
#tag EndMethod
#tag Hook, Flags = &h0
Event HTTPDebug(Message As String, Level As Integer)
#tag EndHook
#tag ComputedProperty, Flags = &h0
#tag Getter
Get
Dim c As ContentType
If Me.HasHeader("Content-Type") Then
Dim s As String = Me.Header("Content-Type")
If s.Trim <> "" Then
c = Me.Header("Content-Type")
Else
c = "unspecified/unspecified"
RaiseEvent HTTPDebug("Warn: Content-Type is empty.", -1)
End If
ElseIf Me.Path <> Nil Then
Dim s As String = NthField(Me.Path.Path.ToString, "/", CountFields(Me.Path.Path.ToString, "/"))
c = SpecialFolder.Temporary.Child(s)
Else
c = "application/octet-stream"
End If
If c.SuperType = "text" And c.CharSet = Nil Then
If Me.MessageBody.Encoding <> Nil Then c.CharSet = Me.MessageBody.Encoding
End If
Return c
End Get
#tag EndGetter
#tag Setter
Set
Me.Header("Content-Type") = value.ToString
If value.CharSet <> Nil Then MessageBody = DefineEncoding(MessageBody, value.CharSet)
End Set
#tag EndSetter
ContentType As ContentType
#tag EndComputedProperty
#tag Property, Flags = &h0
MessageBody As String
#tag EndProperty
#tag Property, Flags = &h1
Protected mHeaders As Headers
#tag EndProperty
#tag Property, Flags = &h21
Private mPath As URIHelpers.URI
#tag EndProperty
#tag ComputedProperty, Flags = &h0
#tag Getter
Get
If mPath = Nil Then
mPath = ""
End If
return mPath
End Get
#tag EndGetter
#tag Setter
Set
mPath = value
End Set
#tag EndSetter
Path As URIHelpers.URI
#tag EndComputedProperty
#tag Property, Flags = &h0
ProtocolVersion As Single = 1.1
#tag EndProperty
#tag ViewBehavior
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="MessageBody"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="ProtocolVersion"
Group="Behavior"
InitialValue="1.1"
Type="Single"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InheritedFrom="Object"
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
InheritedFrom="Object"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass