-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
multipart.cr
152 lines (140 loc) · 4.66 KB
/
multipart.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
require "random/secure"
require "./multipart/*"
require "mime/media_type"
# The `MIME::Multipart` module contains utilities for parsing MIME multipart
# messages, which contain multiple body parts, each containing a header section
# and binary body. The `multipart/form-data` content-type has a separate set of
# utilities in the `HTTP::FormData` module.
module MIME::Multipart
# Parses a MIME multipart message, yielding `HTTP::Headers` and an `IO` for
# each body part.
#
# Please note that the IO object yielded to the block is only valid while the
# block is executing. The IO is closed as soon as the supplied block returns.
#
# ```
# require "mime/multipart"
#
# multipart = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
# MIME::Multipart.parse(IO::Memory.new(multipart), "aA40") do |headers, io|
# headers["Content-Type"] # => "text/plain"
# io.gets_to_end # => "body"
# end
# ```
#
# See: `Multipart::Parser`
def self.parse(io, boundary, &)
parser = Parser.new(io, boundary)
while parser.has_next?
parser.next { |headers, io| yield headers, io }
end
end
# Extracts the multipart boundary from the Content-Type header. May return
# `nil` is the boundary was not found.
#
# ```
# require "mime/multipart"
#
# MIME::Multipart.parse_boundary("multipart/mixed; boundary=\"abcde\"") # => "abcde"
# ```
def self.parse_boundary(content_type) : String?
type = MIME::MediaType.parse?(content_type)
if type && type.type == "multipart"
type["boundary"]?.presence
end
end
# Parses a MIME multipart message, yielding `HTTP::Headers` and an `IO` for
# each body part.
#
# Please note that the IO object yielded to the block is only valid while the
# block is executing. The IO is closed as soon as the supplied block returns.
#
# ```
# require "http"
# require "mime/multipart"
#
# headers = HTTP::Headers{"Content-Type" => "multipart/mixed; boundary=aA40"}
# body = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
# request = HTTP::Request.new("POST", "/", headers, body)
#
# MIME::Multipart.parse(request) do |headers, io|
# headers["Content-Type"] # => "text/plain"
# io.gets_to_end # => "body"
# end
# ```
#
# See: `Multipart::Parser`
def self.parse(request : HTTP::Request, &)
if content_type = request.headers["Content-Type"]?
boundary = parse_boundary(content_type)
end
return nil unless boundary
body = request.body
return nil unless body
parse(body, boundary) { |headers, io| yield headers, io }
end
# Parses a MIME multipart message, yielding `HTTP::Headers` and an `IO` for
# each body part.
#
# Please note that the IO object yielded to the block is only valid while the
# block is executing. The IO is closed as soon as the supplied block returns.
#
# ```
# require "http"
# require "mime/multipart"
#
# headers = HTTP::Headers{"Content-Type" => "multipart/byteranges; boundary=aA40"}
# body = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
# response = HTTP::Client::Response.new(
# status: :ok,
# headers: headers,
# body: body,
# )
#
# MIME::Multipart.parse(response) do |headers, io|
# headers["Content-Type"] # => "text/plain"
# io.gets_to_end # => "body"
# end
# ```
#
# See: `Multipart::Parser`
def self.parse(response : HTTP::Client::Response, &)
if content_type = response.headers["Content-Type"]?
boundary = parse_boundary(content_type)
end
return nil unless boundary
if body = response.body.presence
body = IO::Memory.new(body)
else
body = response.body_io?
end
return nil unless body
parse(body, boundary) { |headers, io| yield headers, io }
end
# Yields a `Multipart::Builder` to the given block, writing to *io* and
# using *boundary*. `#finish` is automatically called on the builder.
def self.build(io : IO, boundary : String = Multipart.generate_boundary, &)
builder = Builder.new(io, boundary)
yield builder
builder.finish
end
# Yields a `Multipart::Builder` to the given block, returning the generated
# message as a `String`.
def self.build(boundary : String = Multipart.generate_boundary, &)
String.build do |io|
build(io, boundary) { |g| yield g }
end
end
# Returns a unique string suitable for use as a multipart boundary.
#
# ```
# require "mime/multipart"
#
# MIME::Multipart.generate_boundary # => "---------------------------dQu6bXHYb4m5zrRC3xPTGwV"
# ```
def self.generate_boundary : String
"--------------------------#{Random::Secure.urlsafe_base64(18)}"
end
class Error < Exception
end
end