oaf 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGY1M2EzN2Y0NzBjMjA1NjBhNzQzZGMxYTdhYmY0MTE5YzYxZDczNg==
4
+ YWQyNWVmNjg3MTg0NjYwMzgyYzI1OTVhZGQ3ZDFiMWM3NzhhNzBjZQ==
5
5
  data.tar.gz: !binary |-
6
- ZmQxNzAyZjM1MmNkNjM0MzJiMjZhNWQ4NzMxNDYyNGVlYzRjYTcxZQ==
6
+ NDdiZWMxMmYzMDI3NmZhYmNkY2VhYzVhZmEzZGQ5N2ZmNjVkNzRmNA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZmUyNWJhM2Q5YzZkN2JlZTdmNjI3MGVlOThkMzQ1MmYwYjFkY2JlMzNhNzQ4
10
- OThiZDVhNDQ1MGMxZmU5ZWJjNDg3ZDhlOGMxNzdlZTVlNmI2ZDhmNzM3MjBh
11
- YzdjZmUzNDI2Y2FmODdhYjM0Yzc1NDIwMzYyZmIzZmNhZTdmMTM=
9
+ NjY1ODU1NmM0N2NjYWZhMDZmMTg3MmIxNmY5MTFiNTUxZjgzOWU4ZDk3OWQ2
10
+ YzliNDgxMDM2ZGRlNWY5YjI2ZWM5ZmQ3NmI3OWNhZDAyNmJmOTZiYWYwMTE0
11
+ NzIwNmQ2ZDgxNjVkOTdjNWI5NDNiOTI0OWUxMDEzNjkwMWI2MWE=
12
12
  data.tar.gz: !binary |-
13
- YjZlYWQ3MzlhODg5MDQ3NTI1NjgwOTQ3MmVjZmViNDQwMTUyOWE2YzIwNDky
14
- ZTMwZTFkMzVjZjJjN2Q4OGM4ZDc0OTJiMmNjNmEwZTZlMDZmZjU3YTU4NGEw
15
- NjY3YzQ2ZjVhNGVmYmI4ZDRlZmIxM2Y4ZjY0NzE1MzRmZDFmYTA=
13
+ OThmYWJkMTUwNDliZDYzMGFjMWM1NmI3ODUxMzIxNTIxMmQ1MTYxNWJjY2Rj
14
+ MjI0OTBlNDE2ZWQ4M2I3ZmVmMzE5MzU4MWZlNWUyNjllMjM3YzM4MTM4ZDA0
15
+ NmRhZjM0ZGFkMTIwZDZmMTI5MjA2YzliZDk1MDJhOTA3M2NiOWQ=
data/.travis.yml CHANGED
@@ -1,3 +1,5 @@
1
1
  language: ruby
2
- ruby:
3
- - "1.9.3"
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - 1.8.7
data/lib/oaf/http.rb CHANGED
@@ -48,6 +48,47 @@ module Oaf
48
48
  [headers, status, body]
49
49
  end
50
50
 
51
+ # Safely retrieves the request body, and assumes an empty string if it
52
+ # cannot be retrieved. This helps get around a nasty exception in WEBrick.
53
+ #
54
+ # == Parameters:
55
+ # req::
56
+ # A WEBrick::HTTPRequest object
57
+ #
58
+ # == Returns:
59
+ # A string containing the request body
60
+ #
61
+ def get_request_body req
62
+ if ['POST', 'PUT'].member? req.request_method
63
+ begin
64
+ result = req.body
65
+ rescue WEBrick::HTTPStatus::LengthRequired
66
+ result = '' # needs to be in rescue for coverage
67
+ end
68
+ end
69
+ result
70
+ end
71
+
72
+ # Consume HTTP response details and set them into a response object.
73
+ #
74
+ # == Parameters:
75
+ # res::
76
+ # A WEBrick::HTTPResponse object
77
+ # headers::
78
+ # A hash containing HTTP response headers
79
+ # body::
80
+ # A string containing the HTTP response body
81
+ # status::
82
+ # An integer indicating the response status
83
+ #
84
+ def set_response! res, headers, body, status
85
+ headers.each do |name, value|
86
+ res.header[name] = value
87
+ end
88
+ res.body = body
89
+ res.status = status
90
+ end
91
+
51
92
  # Invokes the Webrick web server library to handle incoming requests, and
52
93
  # routes them to the appropriate scripts if they exist on the filesystem.
53
94
  #
@@ -60,24 +101,13 @@ module Oaf
60
101
  def serve path, port
61
102
  server = WEBrick::HTTPServer.new :Port => port
62
103
  server.mount_proc '/' do |req, res|
63
- req_body = ''
64
104
  req_headers = req.header
65
105
  req_query = req.query
66
- if ['POST', 'PUT'].member? req.request_method
67
- begin
68
- req_body = req.body
69
- rescue WEBrick::HTTPStatus::LengthRequired
70
- true # without this, coverage is not collected.
71
- end
72
- end
106
+ req_body = Oaf::HTTP::get_request_body req
73
107
  file = Oaf::Util.get_request_file path, req.path, req.request_method
74
108
  out = Oaf::Util.get_output file, req_headers, req_body, req_query
75
109
  res_headers, res_status, res_body = Oaf::HTTP.parse_response out
76
- res_headers.each do |name, value|
77
- res.header[name] = value
78
- end
79
- res.status = res_status
80
- res.body = res_body
110
+ Oaf::HTTP.set_response! res, res_headers, res_body, res_status
81
111
  end
82
112
  trap 'INT' do server.shutdown end
83
113
  server.start
data/lib/oaf/util.rb CHANGED
@@ -20,8 +20,6 @@
20
20
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
- require 'open3'
24
-
25
23
  module Oaf
26
24
 
27
25
  module Util
@@ -55,7 +53,7 @@ module Oaf
55
53
  def get_http_header line
56
54
  return nil if not is_http_header? line
57
55
  parts = line.split(':')
58
- [parts[0].strip, parts[1].strip]
56
+ {parts[0].strip => parts[1].strip}
59
57
  end
60
58
 
61
59
  # Retrieves the numeric value from a line of text as an HTTP status code.
@@ -101,31 +99,47 @@ module Oaf
101
99
  #
102
100
  def prepare_environment headers, query, body
103
101
  result = Hash.new
104
- headers.each do |name, value|
105
- name = Oaf::Util.prepare_key name
106
- result["oaf_header_#{name}"] = Oaf::Util.flatten value
107
- end
108
- query.each do |name, value|
109
- name = Oaf::Util.prepare_key name
110
- result["oaf_query_#{name}"] = Oaf::Util.flatten value
102
+ {'header' => headers, 'query' => query}.each do |prefix, data|
103
+ data.each do |name, value|
104
+ result.merge! Oaf::Util.environment_item prefix, name, value
105
+ end
111
106
  end
112
- result["oaf_request_body"] = Oaf::Util.flatten body
113
- result
107
+ result.merge Oaf::Util.environment_item 'request', 'body', body
114
108
  end
115
109
 
116
- # Replace characters that would not be suitable for an environment variable
117
- # name. Currently this only replaces dashes with underscores. If the need
118
- # arises, more can be added here later.
110
+ # Prepares a key for placement in the execution environment. This includes
111
+ # namespacing variables and converting characters to predictable and
112
+ # easy-to-use names.
119
113
  #
120
114
  # == Parameters:
115
+ # prefix::
116
+ # A prefix for the key. This helps with separation.
121
117
  # key::
122
118
  # The key to sanitize
123
119
  #
124
120
  # == Returns:
125
121
  # A string with the prepared value
126
122
  #
127
- def prepare_key key
128
- key.gsub('-', '_').downcase
123
+ def prepare_key prefix, key
124
+ "oaf_#{prefix}_#{key.gsub('-', '_').downcase}"
125
+ end
126
+
127
+ # Formats a single environment item into a hash, which can be merged into a
128
+ # collective environment mapping later on.
129
+ #
130
+ # == Parameters:
131
+ # prefix::
132
+ # The prefix for the type of item being added.
133
+ # key::
134
+ # The key name of the environment property
135
+ # value::
136
+ # The value for the environment property
137
+ #
138
+ # == Returns:
139
+ # A hash with prepared values ready to merge into an environment hash
140
+ #
141
+ def environment_item prefix, key, value
142
+ {Oaf::Util.prepare_key(prefix, key) => Oaf::Util.flatten(value)}
129
143
  end
130
144
 
131
145
  # Flatten a hash or array into a string. This is useful for preparing some
@@ -176,21 +190,16 @@ module Oaf
176
190
  headers = {}
177
191
  status = 200
178
192
  size = 0
179
- if text.to_s != ''
180
- parts = text.split /^---$/
181
- if parts.length > 1
182
- meta = parts.last.split "\n"
183
- for part in meta
184
- if Oaf::Util.is_http_header? part
185
- header, value = Oaf::Util.get_http_header part
186
- headers.merge! header => value
187
- elsif Oaf::Util.is_http_status? part
188
- status = Oaf::Util.get_http_status part
189
- else
190
- next
191
- end
192
- size += size == 0 ? 2 : 1 # compensate for delimiter
193
+ parts = text.split /^---$/
194
+ if parts.length > 1
195
+ for part in parts.last.split "\n"
196
+ if Oaf::Util.is_http_header? part
197
+ headers.merge! Oaf::Util.get_http_header part
198
+ elsif Oaf::Util.is_http_status? part
199
+ status = Oaf::Util.get_http_status part
200
+ else next
193
201
  end
202
+ size += size == 0 ? 2 : 1 # compensate for delimiter
194
203
  end
195
204
  end
196
205
  [headers, status, size]
@@ -232,11 +241,14 @@ module Oaf
232
241
  File.exist?(file) ? file : nil
233
242
  end
234
243
 
235
- # Run a command with stdout and stderr buffered. This suppresses error
236
- # messages from the server process and enables us to return them in the
237
- # HTTP response instead.
244
+ # Fork a new process, in which we can safely modify the running environment
245
+ # and run a command, sending data back to the parent process using an IO
246
+ # pipe. This can be done in a single line in ruby >= 1.9, but we will do it
247
+ # the hard way to maintain compatibility with older rubies.
238
248
  #
239
249
  # == Parameters:
250
+ # env::
251
+ # The environment data to use in the subprocess.
240
252
  # command::
241
253
  # The command to execute against the server
242
254
  #
@@ -244,8 +256,16 @@ module Oaf
244
256
  # A string of stderr concatenated to stdout.
245
257
  #
246
258
  def run_buffered env, command
247
- stdin, stdout, stderr = Open3.popen3 env, "#{command} 2>&1"
248
- stdout.read
259
+ out, wout = IO.pipe
260
+ pid = fork do
261
+ out.close
262
+ ENV.replace env
263
+ wout.write %x(#{command} 2>&1)
264
+ at_exit { exit! }
265
+ end
266
+ wout.close
267
+ Process.wait pid
268
+ out.read
249
269
  end
250
270
 
251
271
  # Executes a file, or reads its contents if it is not executable, passing
data/lib/oaf/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
23
  module Oaf
24
- VERSION = '0.2.2'
24
+ VERSION = '0.2.3'
25
25
  end
@@ -36,7 +36,7 @@ module Oaf
36
36
 
37
37
  it "should parse a name and value from a header line" do
38
38
  result = Oaf::Util.get_http_header 'x-powered-by: oaf'
39
- result.should eq(['x-powered-by', 'oaf'])
39
+ result.should eq('x-powered-by' => 'oaf')
40
40
  end
41
41
 
42
42
  it "should detect an invalid header line during header parsing" do
@@ -82,17 +82,17 @@ module Oaf
82
82
 
83
83
  describe "Argument Sanitizers" do
84
84
  it "should replace dashes with underscores in key names" do
85
- result = Oaf::Util.prepare_key 'x-powered-by'
86
- result.should eq('x_powered_by')
85
+ result = Oaf::Util.prepare_key 'header', 'x-powered-by'
86
+ result.should eq('oaf_header_x_powered_by')
87
87
  end
88
88
 
89
89
  it "should convert all letters to lowercase in key names" do
90
- result = Oaf::Util.prepare_key 'X-Powered-By'
91
- result.should eq('x_powered_by')
90
+ result = Oaf::Util.prepare_key 'header', 'X-Powered-By'
91
+ result.should eq('oaf_header_x_powered_by')
92
92
  end
93
93
 
94
94
  it "should flatten a hash into a string" do
95
- result = Oaf::Util.flatten({'item1' => 'value1'})
95
+ result = Oaf::Util.flatten('item1' => 'value1')
96
96
  result.should eq('item1value1')
97
97
  end
98
98
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Uber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-30 00:00:00.000000000 Z
11
+ date: 2013-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler