/
content.cr
224 lines (178 loc) · 4.85 KB
/
content.cr
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
212
213
214
215
216
217
218
219
220
221
222
223
224
require "http/common"
module HTTP
# :nodoc:
module Content
CONTINUE = "HTTP/1.1 100 Continue\r\n\r\n"
@continue_sent = false
setter expects_continue : Bool = false
def close
@expects_continue = false
super
end
protected def ensure_send_continue
return unless @expects_continue
return if @continue_sent
@io << CONTINUE
@io.flush
@continue_sent = true
end
end
# :nodoc:
class FixedLengthContent < IO::Sized
include Content
def read(slice : Bytes)
ensure_send_continue
super
end
def read_byte
ensure_send_continue
super
end
def peek
ensure_send_continue
super
end
def skip(bytes_count)
ensure_send_continue
super
end
def write(slice : Bytes)
raise IO::Error.new "Can't write to FixedLengthContent"
end
end
# :nodoc:
class UnknownLengthContent < IO
include Content
def initialize(@io : IO)
end
def read(slice : Bytes)
ensure_send_continue
@io.read(slice)
end
def read_byte
ensure_send_continue
@io.read_byte
end
def peek
ensure_send_continue
@io.peek
end
def skip(bytes_count)
ensure_send_continue
@io.skip(bytes_count)
end
def write(slice : Bytes)
raise IO::Error.new "Can't write to UnknownLengthContent"
end
end
# :nodoc:
class ChunkedContent < IO
include Content
# Returns trailing headers read by this chunked content.
#
# The value will only be populated once the entire content has been read,
# i.e. this IO is at EOF.
#
# All headers in the trailing headers section will be returned. Applications
# need to make sure to ignore them or fail if headers are not allowed
# in the chunked trailer part (see [RFC 7230 section 4.1.2](https://tools.ietf.org/html/rfc7230#section-4.1.2)).
getter headers : HTTP::Headers { HTTP::Headers.new }
def initialize(@io : IO)
@chunk_remaining = -1
@received_final_chunk = false
end
def read(slice : Bytes)
ensure_send_continue
count = slice.size
return 0 if count == 0
next_chunk
return 0 if @received_final_chunk
to_read = Math.min(count, @chunk_remaining)
bytes_read = @io.read slice[0, to_read]
if bytes_read == 0
raise IO::EOFError.new("Invalid HTTP chunked content")
end
@chunk_remaining -= bytes_read
bytes_read
end
def read_byte
ensure_send_continue
next_chunk
return super if @received_final_chunk
byte = @io.read_byte
if byte
@chunk_remaining -= 1
byte
else
raise IO::EOFError.new("Invalid HTTP chunked content")
end
end
def peek
ensure_send_continue
next_chunk
return Bytes.empty if @received_final_chunk
peek = @io.peek || return
if @chunk_remaining < peek.size
peek = peek[0, @chunk_remaining]
elsif peek.size == 0
raise IO::EOFError.new("Invalid HTTP chunked content")
end
peek
end
def skip(bytes_count)
ensure_send_continue
if bytes_count <= @chunk_remaining
@io.skip(bytes_count)
@chunk_remaining -= bytes_count
else
super
end
end
# Checks if the last read consumed a chunk and we
# need to start consuming the next one.
private def next_chunk
return if @chunk_remaining > 0 || @received_final_chunk
# As soon as we finish reading a chunk we return,
# in case the following content is delayed (see #3270) and read the chunk
# delimiter and next chunk start on the next call to `read`.
read_crlf unless @chunk_remaining == -1 # -1 is the initial value
@chunk_remaining = read_chunk_size
if @chunk_remaining == 0
read_trailer
@received_final_chunk = true
end
end
private def read_crlf
char = @io.read_char
if char == '\r'
char = @io.read_char
end
if char != '\n'
raise IO::Error.new("Invalid HTTP chunked content: expected CRLF")
end
end
private def read_chunk_size
line = @io.read_line(HTTP::MAX_HEADER_SIZE, chomp: true)
if index = line.byte_index(';'.ord)
chunk_size = line.byte_slice(0, index)
else
chunk_size = line
end
chunk_size.to_i?(16) || raise IO::Error.new("Invalid HTTP chunked content: invalid chunk size")
end
private def read_trailer
while true
line = @io.read_line(HTTP::MAX_HEADER_SIZE, chomp: true)
break if line.empty?
key, value = HTTP.parse_header(line)
break unless headers.add?(key, value)
end
end
def write(slice : Bytes)
raise IO::Error.new "Can't write to ChunkedContent"
end
def closed?
@received_final_chunk || super
end
end
end