multipart_parser 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +27 -0
- data/README.md +182 -0
- data/Rakefile +12 -0
- data/ext/multipart_parser/ext_help.h +18 -0
- data/ext/multipart_parser/extconf.rb +3 -0
- data/ext/multipart_parser/multipart_parser.c +581 -0
- data/ext/multipart_parser/multipart_parser_c.c +302 -0
- data/ext/multipart_parser/multipart_parser_c.h +67 -0
- data/lib/multipart_parser/version.rb +3 -0
- data/lib/multipart_parser.rb +21 -0
- data/multipart_parser.gemspec +25 -0
- data/spec/multipart_parser_spec.rb +157 -0
- data/spec/spec_helper.rb +2 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b5401a95c9f84dd50f880f1b3f7600f75ef67c7
|
4
|
+
data.tar.gz: 7dd6495855fe2ffb3352683f382871ea2e7e3d7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6ade21353b43a28ddbeb6c75ea07970070dfbe79692b2977ea438236f26ff43fd58d7ca6b1fca91c2e0394a4d0510931e8b5253e2b53922624db5bbeae076ed7
|
7
|
+
data.tar.gz: b19416e99743307d1696b7bf792c08c31416e9548b9cd14e5f67eba29fbd5282aae371a57de340f6f88eeeb00e5f73e74e3817b73510d53a2a469433b8cb31eb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
Copyright (c) 2015 Benjamin Bryant
|
4
|
+
|
5
|
+
|
6
|
+
ext/multipart_parser/multipart_parser_c.c and ext/multipart_parser/multipart_parser_c.h modified from code written by Igor Afonov https://github.com/iafonov/multipart-parser-c also issued under an MIT license.
|
7
|
+
|
8
|
+
MIT License
|
9
|
+
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
11
|
+
a copy of this software and associated documentation files (the
|
12
|
+
"Software"), to deal in the Software without restriction, including
|
13
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
14
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to
|
16
|
+
the following conditions:
|
17
|
+
|
18
|
+
The above copyright notice and this permission notice shall be
|
19
|
+
included in all copies or substantial portions of the Software.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
22
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
23
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
24
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
25
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
26
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
27
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
# MultipartParser
|
2
|
+
|
3
|
+
A c backed multipart/form-data parser.
|
4
|
+
|
5
|
+
Ruby extenstion for https://github.com/iafonov/multipart-parser-c, modeled after http_parser.rb
|
6
|
+
|
7
|
+
|
8
|
+
Multipart-parser C has been modfied to have a more consistent callback structure with http_parser.rb
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'multipart_parser'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install multipart_parser
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
|
33
|
+
require 'multipart_parser'
|
34
|
+
|
35
|
+
parser = MultipartParser.new("Boundary+17376C87E2579930")
|
36
|
+
|
37
|
+
|
38
|
+
# Setups callbacks
|
39
|
+
parser.on_message_begin = proc { puts "# => on_message_begin"}
|
40
|
+
parser.on_part_begin = proc { puts "# => on_part_begin"}
|
41
|
+
parser.on_headers_complete = proc {|headers| puts "# => on_headers_complete: #{headers.inspect}"}
|
42
|
+
parser.on_data = proc {|data| puts "# => on_data: #{data}"}
|
43
|
+
parser.on_part_complete = proc {puts "# => on_part_complete"}
|
44
|
+
parser.on_message_complete = proc {puts "# => on_message_complete"}
|
45
|
+
|
46
|
+
|
47
|
+
parser << "--Boundary+17376C87E2579930\r\n"
|
48
|
+
|
49
|
+
# => on_message_begin
|
50
|
+
# => on_part_begin
|
51
|
+
|
52
|
+
parser << "Content-Disposition: form-data; name=ID1\r\n"
|
53
|
+
parser << "Content-Type: text/plain\r\n\r\n"
|
54
|
+
parser << "This is the first part.\r\n"
|
55
|
+
|
56
|
+
# => on_headers_complete: {"Content-Disposition"=>"form-data; name=ID1", "Content-Type"=>"text/plain"}
|
57
|
+
# => on_data: This is the first part.
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
parser << "--Boundary+17376C87E2579930\r\n"
|
62
|
+
|
63
|
+
# => on_part_complete
|
64
|
+
# => on_part_begin
|
65
|
+
# =>
|
66
|
+
parser << "Content-Disposition: form-data; name=ID2\r\n"
|
67
|
+
parser << "Content-Type: text/plain\r\n\r\n"
|
68
|
+
parser << "This is the second part.\r\n";
|
69
|
+
|
70
|
+
parser << "--Boundary+17376C87E2579930--\r\n"
|
71
|
+
|
72
|
+
# => on_headers_complete: {"Content-Disposition"=>"form-data; name=ID2", "Content-Type"=>"text/plain"}
|
73
|
+
# => on_data: This is the second part.
|
74
|
+
# => on_part_complete
|
75
|
+
# => on_message_complete
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# Also allows you to pass a callback object
|
81
|
+
|
82
|
+
|
83
|
+
class Handler
|
84
|
+
def on_message_begin; puts "# => on_message_begin"; end
|
85
|
+
def on_part_begin; puts "# => on_part_begin"; end
|
86
|
+
def on_headers_complete(headers); puts "# => on_headers_complete: #{headers.inspect}"; end
|
87
|
+
def on_data(data); puts "# => on_data: #{data}"; end
|
88
|
+
def on_part_complete; puts "# => on_part_complete"; end
|
89
|
+
def on_message_complete; puts "# => on_message_complete"; end
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
parser = MultipartParser.new("Boundary+17376C87E2579930", Handler.new)
|
94
|
+
|
95
|
+
|
96
|
+
parser << "--Boundary+17376C87E2579930\r\n"
|
97
|
+
|
98
|
+
# => on_message_begin
|
99
|
+
# => on_part_begin
|
100
|
+
|
101
|
+
parser << "Content-Disposition: form-data; name=ID1\r\n"
|
102
|
+
parser << "Content-Type: text/plain\r\n\r\n"
|
103
|
+
parser << "This is the first part.\r\n"
|
104
|
+
|
105
|
+
# => on_headers_complete: {"Content-Disposition"=>"form-data; name=ID1", "Content-Type"=>"text/plain"}
|
106
|
+
# => on_data: This is the first part.
|
107
|
+
|
108
|
+
parser << "--Boundary+17376C87E2579930\r\n"
|
109
|
+
|
110
|
+
# => on_part_complete
|
111
|
+
# => on_part_begin
|
112
|
+
|
113
|
+
|
114
|
+
parser << "Content-Disposition: form-data; name=ID2\r\n"
|
115
|
+
parser << "Content-Type: text/plain\r\n\r\n"
|
116
|
+
parser << "This is the second part.\r\n";
|
117
|
+
|
118
|
+
parser << "--Boundary+17376C87E2579930--\r\n"
|
119
|
+
|
120
|
+
# => on_headers_complete: {"Content-Disposition"=>"form-data; name=ID2", "Content-Type"=>"text/plain"}
|
121
|
+
# => on_data: This is the second part.
|
122
|
+
# => on_part_complete
|
123
|
+
# => on_message_complete
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
|
129
|
+
# Accepts a config paramter to modify how the headers are returned
|
130
|
+
|
131
|
+
parser = MultipartParser.new("Boundary+17376C87E2579930", Handler.new, :arrays)
|
132
|
+
|
133
|
+
# valid values are :arrays, :strings, :mixed
|
134
|
+
|
135
|
+
parser << "--Boundary+17376C87E2579930\r\n"
|
136
|
+
parser << "Content-Disposition: form-data; name=ID_A\r\n"
|
137
|
+
parser << "Content-Disposition: form-data; name=ID_B\r\n"
|
138
|
+
parser << "Content-Type: text/plain\r\n\r\n"
|
139
|
+
parser << "This is the first part.\r\n"
|
140
|
+
|
141
|
+
|
142
|
+
# => on_headers_complete: {"Content-Disposition"=>["form-data; name=ID_A", "form-data; name=ID_B"], "Content-Type"=>["text/plain"]}
|
143
|
+
|
144
|
+
|
145
|
+
parser = MultipartParser.new("Boundary+17376C87E2579930", Handler.new, :strings)
|
146
|
+
|
147
|
+
# => on_headers_complete: {"Content-Disposition"=>"form-data; name=ID_A, form-data; name=ID_B", "Content-Type"=>"text/plain"}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
parser = MultipartParser.new("Boundary+17376C87E2579930", Handler.new, :mixed)
|
152
|
+
|
153
|
+
# => on_headers_complete: {"Content-Disposition"=>["form-data; name=ID_A", "form-data; name=ID_B"], "Content-Type"=>"text/plain"}
|
154
|
+
|
155
|
+
|
156
|
+
```
|
157
|
+
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
|
161
|
+
# Set the default(:mixed)
|
162
|
+
|
163
|
+
|
164
|
+
MultipartParser.default_header_value_type = :arrays
|
165
|
+
|
166
|
+
|
167
|
+
parser = MultipartParser.new("Boundary+17376C87E2579930", Handler.new)
|
168
|
+
|
169
|
+
# => on_headers_complete: {"Content-Disposition"=>["form-data; name=ID_A", "form-data; name=ID_B"], "Content-Type"=>["text/plain"]}
|
170
|
+
|
171
|
+
|
172
|
+
```
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
## Contributing
|
177
|
+
|
178
|
+
1. Fork it ( https://github.com/[my-github-username]/multipart_parser/fork )
|
179
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
180
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
181
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
182
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
require "rake/extensiontask"
|
9
|
+
|
10
|
+
Rake::ExtensionTask.new("multipart_parser") do |ext|
|
11
|
+
ext.lib_dir = "lib/multipart_parser"
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#ifndef ext_help_h
|
2
|
+
#define ext_help_h
|
3
|
+
|
4
|
+
#define RAISE_NOT_NULL(T) if(T == NULL) rb_raise(rb_eArgError, "NULL found for " # T " when shouldn't be.");
|
5
|
+
#define DATA_GET(from,type,name) Data_Get_Struct(from,type,name); RAISE_NOT_NULL(name);
|
6
|
+
#define REQUIRE_TYPE(V, T) if(TYPE(V) != T) rb_raise(rb_eTypeError, "Wrong argument type for " # V " required " # T);
|
7
|
+
|
8
|
+
/* for compatibility with Ruby 1.8.5, which doesn't declare RSTRING_PTR */
|
9
|
+
#ifndef RSTRING_PTR
|
10
|
+
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
|
11
|
+
#endif
|
12
|
+
|
13
|
+
/* for compatibility with Ruby 1.8.5, which doesn't declare RSTRING_LEN */
|
14
|
+
#ifndef RSTRING_LEN
|
15
|
+
#define RSTRING_LEN(s) (RSTRING(s)->len)
|
16
|
+
#endif
|
17
|
+
|
18
|
+
#endif
|