Class: Protocol::Multipart::Parser
- Inherits:
-
Object
- Object
- Protocol::Multipart::Parser
- Defined in:
- lib/protocol/multipart/parser.rb
Overview
A parser for multipart data based on RFC 2046 and RFC 2387. Parses multipart bodies and provides an enumerable interface to access the parts.
Defined Under Namespace
Classes: Part
Instance Method Summary collapse
-
#each ⇒ Object
Enumerate through each part in the multipart data.
-
#initialize(readable, boundary) ⇒ Parser
constructor
Initialize a new multipart parser.
Constructor Details
#initialize(readable, boundary) ⇒ Parser
Initialize a new multipart parser.
150 151 152 153 154 155 |
# File 'lib/protocol/multipart/parser.rb', line 150 def initialize(readable, boundary) @readable = IO::Stream(readable) @boundary = boundary @boundary_marker = "--#{@boundary}\r\n".freeze end |
Instance Method Details
#each ⇒ Object
Enumerate through each part in the multipart data. Yields each part for processing. If no block is given, returns an enumerator.
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 |
# File 'lib/protocol/multipart/parser.rb', line 161 def each return to_enum unless block_given? # Read lines until we find the first boundary: while true if line = @readable.gets("\r\n", chomp: false) if line == @boundary_marker break end else # End of stream reached without finding boundary: raise EOFError, "No multipart boundary found in stream!" end end while true part = read_part break unless part if part.read_empty_boundary? else begin yield part ensure # After yielding, ensure the part is finished to advance to the next boundary. This is either a no-op if user already read the part, or reads remaining data. part.discard end end # Check if this was the last part: break if part.closing_boundary? end return true end |