oaf 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +3 -0
- data/README.md +32 -15
- data/lib/oaf/http/handler.rb +2 -1
- data/lib/oaf/util.rb +17 -12
- data/lib/oaf/version.rb +1 -1
- data/spec/oaf/util_spec.rb +7 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MTc3ZjQwMGU4N2M5MDkyZGNhN2JkY2YxNWYwN2Q3MWUyNzRkZWI4OA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTg3YWZjMTYwYjcyMmI1NDU3M2QxOGI5YmM0MjliOTcwNDk3Nzg0NQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MGM5M2IzOTMwZDhkNTUyZDBiMTE1YjFiYjM2Y2ExNmVhZTQwNDBjZjQxNGU5
|
10
|
+
NDUzNWNmZmQwMzFmZTI0NDc3NjdlY2FhNDNlNWFhNzFhZmI5Zjc5NzkxZmE5
|
11
|
+
M2U3YmE5ZWZkMjNmYTBiZThkMjRkMDE2OWVjMWU0OWVkNDk3MzY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MGY5ODIwOGIzM2I4MGM0NDkwZTBiZWVmNjU3NTFjNjg2ZDRkYTU1MGM1ZWMy
|
14
|
+
ODE3ZDA3YmY5ZGQwMmY2ODAwMTQzNWFiYWQwMTVmM2ZjNDkyYjQ0NGI5MmU4
|
15
|
+
ZDZkZWFmMTRkOGEwNDY5NmYyNjVjODk3NWIwYzFlNmVhODE3ZTk=
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -6,8 +6,9 @@ Care-free web app prototyping using files and scripts
|
|
6
6
|
[![Gem Version](https://badge.fury.io/rb/oaf.png)](http://badge.fury.io/rb/oaf)
|
7
7
|
[![Build Status](https://travis-ci.org/ryanuber/oaf.png)](https://travis-ci.org/ryanuber/oaf)
|
8
8
|
[![Coverage Status](https://coveralls.io/repos/ryanuber/oaf/badge.png)](https://coveralls.io/r/ryanuber/oaf)
|
9
|
-
[![Code
|
10
|
-
|
9
|
+
[![Code Climate](https://codeclimate.com/github/ryanuber/oaf.png)](https://codeclimate.com/github/ryanuber/oaf)
|
10
|
+
|
11
|
+
[Documentation](http://rubydoc.info/gems/oaf/frames)
|
11
12
|
|
12
13
|
`Oaf` provides stupid-easy way of creating dynamic web applications by setting
|
13
14
|
all best practices and security considerations aside until you are sure that you
|
@@ -21,31 +22,47 @@ Example
|
|
21
22
|
-------
|
22
23
|
|
23
24
|
Create an executable file named `hello.GET`:
|
25
|
+
|
24
26
|
```
|
25
27
|
#!/bin/bash
|
26
28
|
echo "Hello, ${USER}!"
|
27
29
|
```
|
28
30
|
|
29
31
|
Start the server by running the `oaf` command, then make a request:
|
32
|
+
|
30
33
|
```
|
31
34
|
$ curl localhost:9000/hello
|
32
35
|
Hello, ryanuber!
|
33
36
|
```
|
34
37
|
|
38
|
+
Installation
|
39
|
+
------------
|
40
|
+
|
41
|
+
Oaf is available on [rubygems](http://rubygems.org/gems/oaf), which means you
|
42
|
+
can do:
|
43
|
+
|
44
|
+
```
|
45
|
+
gem install oaf
|
46
|
+
```
|
47
|
+
|
35
48
|
### Accepted files
|
49
|
+
|
36
50
|
`Oaf` will run *ANY* file with the executable bit set, be it shell, Python, Ruby,
|
37
51
|
compiled binary, or whatever else you might have.
|
38
52
|
|
39
53
|
`Oaf` can also use plain text files.
|
40
54
|
|
41
55
|
### How file permissions affect output
|
56
|
+
|
42
57
|
* If the file in your request is executable, the output of its execution will
|
43
58
|
be used as the return data.
|
44
59
|
* If the file is *NOT* executable, then the contents of the file will be used
|
45
60
|
as the return data.
|
46
61
|
|
47
62
|
### Nested methods
|
63
|
+
|
48
64
|
You can create nested methods using simple directories. Example:
|
65
|
+
|
49
66
|
```
|
50
67
|
$ ls ./examples/
|
51
68
|
hello.GET
|
@@ -75,26 +92,24 @@ Separated by 3 dashes on a line of their own (`---`), the very last block
|
|
75
92
|
of output can contain headers and response status.
|
76
93
|
|
77
94
|
### Getting request headers, query parameters, and body
|
95
|
+
|
78
96
|
Headers, query parameters, and request body are all passed to executables using
|
79
97
|
the environment. To defeat overlap in variables, they are namespaced using a
|
80
|
-
prefix. The
|
98
|
+
prefix. The environment variables are as follows:
|
81
99
|
|
82
|
-
* Headers: `oaf_header_
|
83
|
-
*
|
100
|
+
* Headers: `oaf_header_*`
|
101
|
+
* Request parameters: `oaf_param_*`
|
84
102
|
* Request body: `oaf_request_body`
|
103
|
+
* Request path: `oaf_request_path`
|
85
104
|
|
86
105
|
Below is a quick example of a shell script which makes use of the request data:
|
87
|
-
|
106
|
+
|
107
|
+
```
|
88
108
|
#!/bin/bash
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
echo "You passed the 'myparam' value: $oaf_query_myparam"
|
94
|
-
fi
|
95
|
-
if [ -n "$oaf_request_body" ]; then
|
96
|
-
echo "You passed the request body: $oaf_request_body"
|
97
|
-
fi
|
109
|
+
echo "You passed the Accept header: $oaf_header_accept"
|
110
|
+
echo "You passed the 'message' value: $oaf_param_message"
|
111
|
+
echo "You passed the request body: $oaf_request_body"
|
112
|
+
echo "This method is located at: $oaf_request_path"
|
98
113
|
```
|
99
114
|
|
100
115
|
Headers query parameter names are converted to all-lowercase, and dashes are
|
@@ -103,6 +118,7 @@ example, if you wanted to get at the `Content-Type` header, you could with the
|
|
103
118
|
environment variable `$oaf_header_content_type`.
|
104
119
|
|
105
120
|
### Catch-all methods
|
121
|
+
|
106
122
|
Catch-all's can be defined by naming a file inside of a directory, beginning and
|
107
123
|
ending with underscores (`_`). So for example, `test/_default_.GET` will match:
|
108
124
|
`GET /test/anything`, `GET /test/blah`, etc.
|
@@ -112,6 +128,7 @@ file at `/test.GET`.
|
|
112
128
|
|
113
129
|
Q&A
|
114
130
|
---
|
131
|
+
|
115
132
|
**Why are the headers and status at the bottom of the response?**
|
116
133
|
Because it is much easier to echo these last. Since Oaf reads response
|
117
134
|
data directly from stdout/stderr, it is very easy for debug or error messages
|
data/lib/oaf/http/handler.rb
CHANGED
@@ -66,7 +66,8 @@ module Oaf::HTTP
|
|
66
66
|
req_query = req.query
|
67
67
|
req_body = Oaf::HTTP::Server.get_request_body req
|
68
68
|
file = Oaf::Util.get_request_file @path, req.path, req.request_method
|
69
|
-
out = Oaf::Util.get_output
|
69
|
+
out = Oaf::Util.get_output(file, req.path, req_headers, req_body,
|
70
|
+
req_query)
|
70
71
|
res_headers, res_status, res_body = Oaf::HTTP::Server.parse_response out
|
71
72
|
Oaf::HTTP::Server.set_response! res, res_headers, res_body, res_status
|
72
73
|
end
|
data/lib/oaf/util.rb
CHANGED
@@ -87,23 +87,26 @@ module Oaf
|
|
87
87
|
# can then be passed on as environment data to a script.
|
88
88
|
#
|
89
89
|
# == Parameters:
|
90
|
+
# req_path::
|
91
|
+
# A string with the HTTP request path
|
90
92
|
# headers::
|
91
93
|
# A hash of request headers
|
92
|
-
#
|
93
|
-
# A hash of
|
94
|
+
# params::
|
95
|
+
# A hash of request parameters
|
94
96
|
# body::
|
95
97
|
# A string containing the request body
|
96
98
|
#
|
97
99
|
# == Returns:
|
98
100
|
# A flat hash containing namespaced environment parameters
|
99
101
|
#
|
100
|
-
def prepare_environment headers,
|
102
|
+
def prepare_environment req_path, headers, params, body
|
101
103
|
result = Hash.new
|
102
|
-
{'header' => headers, '
|
104
|
+
{'header' => headers, 'param' => params}.each do |prefix, data|
|
103
105
|
data.each do |name, value|
|
104
106
|
result.merge! Oaf::Util.environment_item prefix, name, value
|
105
107
|
end
|
106
108
|
end
|
109
|
+
result.merge! Oaf::Util.environment_item 'request', 'path', req_path
|
107
110
|
result.merge Oaf::Util.environment_item 'request', 'body', body
|
108
111
|
end
|
109
112
|
|
@@ -222,16 +225,16 @@ module Oaf
|
|
222
225
|
# == Parameters:
|
223
226
|
# root::
|
224
227
|
# The root path to search within.
|
225
|
-
#
|
226
|
-
# The
|
228
|
+
# req_path::
|
229
|
+
# The HTTP request path
|
227
230
|
# method::
|
228
231
|
# The HTTP method of the request
|
229
232
|
#
|
230
233
|
# == Returns:
|
231
234
|
# The path to a file to use, or `nil` if none is found.
|
232
235
|
#
|
233
|
-
def get_request_file root,
|
234
|
-
file = File.join root, "#{
|
236
|
+
def get_request_file root, req_path, method
|
237
|
+
file = File.join root, "#{req_path}.#{method}"
|
235
238
|
if not File.exist? file
|
236
239
|
Dir.glob(File.join(File.dirname(file), "_*_.#{method}")).each do |f|
|
237
240
|
file = f
|
@@ -272,23 +275,25 @@ module Oaf
|
|
272
275
|
# it the request headers and body as arguments, and returns the result.
|
273
276
|
#
|
274
277
|
# == Parameters:
|
278
|
+
# req_path::
|
279
|
+
# The HTTP request path
|
275
280
|
# file::
|
276
281
|
# The path to the file to use for output
|
277
282
|
# headers::
|
278
283
|
# The HTTP request headers to pass
|
279
284
|
# body::
|
280
285
|
# The HTTP request body to pass
|
281
|
-
#
|
282
|
-
# The HTTP
|
286
|
+
# params::
|
287
|
+
# The HTTP request parameters to pass
|
283
288
|
#
|
284
289
|
# == Returns:
|
285
290
|
# The result from the file, or a default result if the file is not found.
|
286
291
|
#
|
287
|
-
def get_output file, headers=[], body=[],
|
292
|
+
def get_output file, req_path=nil, headers=[], body=[], params=[]
|
288
293
|
if file.nil?
|
289
294
|
out = Oaf::Util.get_default_response
|
290
295
|
elsif File.executable? file
|
291
|
-
env = Oaf::Util.prepare_environment headers,
|
296
|
+
env = Oaf::Util.prepare_environment req_path, headers, params, body
|
292
297
|
out = Oaf::Util.run_buffered env, file
|
293
298
|
else
|
294
299
|
out = File.open(file).read
|
data/lib/oaf/version.rb
CHANGED
data/spec/oaf/util_spec.rb
CHANGED
@@ -220,7 +220,8 @@ module Oaf
|
|
220
220
|
@f4 = Tempfile.new 'oaf'
|
221
221
|
@f4.chmod 0755
|
222
222
|
@f4.write "#!/bin/bash\necho \"$oaf_header_x_powered_by\"\n" +
|
223
|
-
"echo \"$
|
223
|
+
"echo \"$oaf_param_myparam\"\necho \"$oaf_request_body\"\n" +
|
224
|
+
"echo \"$oaf_request_path\""
|
224
225
|
@f4.close
|
225
226
|
end
|
226
227
|
|
@@ -253,10 +254,12 @@ module Oaf
|
|
253
254
|
|
254
255
|
it "should register environment variables for headers, query, and body" do
|
255
256
|
headers = {'x-powered-by' => 'oaf'}
|
256
|
-
|
257
|
+
params = {'myparam' => 'myval'}
|
257
258
|
body = 'Test Body'
|
258
|
-
|
259
|
-
result.
|
259
|
+
path = '/test/path'
|
260
|
+
result = Oaf::Util.get_output @f4.path, path, headers, body, params
|
261
|
+
result.should eq("#{headers['x-powered-by']}\n#{params['myparam']}\n" +
|
262
|
+
"#{body}\n#{path}\n")
|
260
263
|
end
|
261
264
|
end
|
262
265
|
end
|
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.
|
4
|
+
version: 0.3.0
|
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-
|
11
|
+
date: 2013-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|