apphttp 0.1.0
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
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/apphttp.rb +106 -0
- metadata +70 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a4cb846a2215c4c862dd838caeb74ecb4af881c6f93b6a14228d9fb16df4bb38
|
4
|
+
data.tar.gz: 28ccc08e4bc6df11df826d6d294a6b6d6d19a7b8564da5bb98e0260c8403377e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0716725be6eac3ac5ce2ed7f5380dbf3ed82b35de5b54546c5cbd0ec121b92eca878e8f686c3e91843c8d3a9d20594a6e0e7423a8a43fa25c0afb985429b6c7a
|
7
|
+
data.tar.gz: f15ff123d1e6ba25acfc8273b18cb9f9d055b7443dc6e6de3dff2ce50fb842c98434bb62aae28b6ae0ab2cd54318a4aac59e620d7850fb9b8173435da30572f7
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data.tar.gz.sig
ADDED
Binary file
|
data/lib/apphttp.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# file: apphttp.rb
|
4
|
+
|
5
|
+
# desc: Makes it trivial to web enable a Ruby project
|
6
|
+
|
7
|
+
require 'socket'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
|
11
|
+
class AppHttp
|
12
|
+
|
13
|
+
def initialize(app, host: '0.0.0.0', port: '9232', debug: false)
|
14
|
+
@app, @host, @port, @debug = app, host, port, debug
|
15
|
+
end
|
16
|
+
|
17
|
+
def start
|
18
|
+
|
19
|
+
server = TCPServer.new(@host, @port)
|
20
|
+
|
21
|
+
while (session = server.accept)
|
22
|
+
|
23
|
+
raw_request = session.gets
|
24
|
+
request = raw_request[/.[^\s]+(?= HTTP\/1\.\d)/].strip
|
25
|
+
|
26
|
+
result,content_type = get(request)
|
27
|
+
puts 'content_type: ' + content_type.inspect if @debug
|
28
|
+
|
29
|
+
if result then
|
30
|
+
response = result
|
31
|
+
else
|
32
|
+
response = "404: page not found"
|
33
|
+
content_type = 'text/plain'
|
34
|
+
end
|
35
|
+
|
36
|
+
session.print "HTTP/1.1 200 OK\r\nContent-type: #{content_type}\r\n" +
|
37
|
+
"Content-Length: #{response.bytesize}\r\n" +
|
38
|
+
"Connection: close\r\n"
|
39
|
+
session.print "\r\n"
|
40
|
+
session.print response
|
41
|
+
session.close
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def get(s)
|
48
|
+
|
49
|
+
args = []
|
50
|
+
|
51
|
+
if s.count('/') > 1 then
|
52
|
+
|
53
|
+
name, args = *s.split('/')[1..-1]
|
54
|
+
|
55
|
+
else
|
56
|
+
|
57
|
+
name, raw_params = s[/(?<=^\/).*/].split('?',2)
|
58
|
+
|
59
|
+
if raw_params then
|
60
|
+
|
61
|
+
raw_pairs = raw_params.split('&')
|
62
|
+
|
63
|
+
h = raw_pairs.inject({}) do |r,x|
|
64
|
+
key,value = x.split('=',2)
|
65
|
+
r.merge(key.to_sym => value) if value
|
66
|
+
end
|
67
|
+
|
68
|
+
return unless h
|
69
|
+
|
70
|
+
h2 = h.inject({}) do |r,x|
|
71
|
+
|
72
|
+
if x.first.to_s =~ /^arg/ then
|
73
|
+
args << x.last
|
74
|
+
else
|
75
|
+
r.merge!(x.first => x.last)
|
76
|
+
end
|
77
|
+
r
|
78
|
+
end
|
79
|
+
|
80
|
+
args << h2 if h2.any?
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
if @app.respond_to? name.to_sym then
|
87
|
+
|
88
|
+
begin
|
89
|
+
r = @app.method(name.to_sym).call(*args)
|
90
|
+
rescue
|
91
|
+
r = ($!).inspect
|
92
|
+
end
|
93
|
+
|
94
|
+
case r.class.to_s
|
95
|
+
when "String"
|
96
|
+
[r, 'text/plain']
|
97
|
+
when "Hash"
|
98
|
+
[r.to_json,'application/json']
|
99
|
+
else
|
100
|
+
[r.to_s, 'text/plain']
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apphttp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Robertson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
|
14
|
+
YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMTkwOTAzMjIzMTMxWhcN
|
15
|
+
MjAwOTAyMjIzMTMxWjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
|
16
|
+
cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQC+oSh7
|
17
|
+
hdE7JV9vwvzzMaRIegnH2rAtqShRteD9HT+kLnCf1BA9xtiMbw7RzQ/Jjf4Yraxe
|
18
|
+
rv+6p6H/F7tJVU7e8Fk1iOdsWrjnmki4/CgTXRkzYCvzPfUljh8YPFTRnhEbCWQF
|
19
|
+
x1vlHa4JrW5YAn+oanQk0ERGIKhUpUwrhJUe9g4lv70ZsMMCda2/dF2ahZP3et3K
|
20
|
+
IvZhXsterFmnjmd/dj9dw0O0qID4BdHQq800Ef7tCxfUkHbKGtTOOJJAQYpDvLV5
|
21
|
+
Xj+QPmIPKZ04X3ahLppdh5dQnzC7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6CraruZ22EGLX8W
|
37
|
+
-----END CERTIFICATE-----
|
38
|
+
date: 2019-09-03 00:00:00.000000000 Z
|
39
|
+
dependencies: []
|
40
|
+
description:
|
41
|
+
email: james@jamesrobertson.eu
|
42
|
+
executables: []
|
43
|
+
extensions: []
|
44
|
+
extra_rdoc_files: []
|
45
|
+
files:
|
46
|
+
- lib/apphttp.rb
|
47
|
+
homepage: https://github.com/jrobertson/apphttp
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.0.1
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Makes it trivial to web enable a Ruby project.
|
70
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|