net-http-server 0.2.2 → 0.2.4
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 +1 -0
- data/ChangeLog.md +17 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +1 -1
- data/README.md +26 -18
- data/Rakefile +11 -28
- data/examples/config.ru +3 -0
- data/examples/test_app.rb +21 -0
- data/gemspec.yml +10 -4
- data/lib/net/http/server/chunked_stream.rb +4 -5
- data/lib/net/http/server/daemon.rb +17 -17
- data/lib/net/http/server/parser.rb +7 -7
- data/lib/net/http/server/requests.rb +1 -1
- data/lib/net/http/server/server.rb +16 -13
- data/lib/net/http/server/stream.rb +1 -1
- data/lib/net/http/server/version.rb +1 -1
- data/lib/rack/handler/http.rb +9 -9
- data/net-http-server.gemspec +40 -109
- data/spec/net/http/server/chunked_stream_spec.rb +11 -11
- data/spec/net/http/server/daemon_spec.rb +4 -4
- data/spec/net/http/server/parser_spec.rb +23 -23
- data/spec/net/http/server/requests_spec.rb +12 -12
- data/spec/net/http/server/responses_spec.rb +17 -17
- data/spec/net/http/server/stream_spec.rb +7 -7
- data/spec/rack/handler/helpers/test_request.rb +12 -3
- data/spec/rack/handler/http_spec.rb +27 -26
- data/spec/spec_helper.rb +0 -1
- metadata +37 -55
- data/.gemtest +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 19c40d467cfd114121cf57956ff8960bb015d5f303cc2c53a6304a0ce8b20736
|
4
|
+
data.tar.gz: e19a4019f980b8669605adb452bb9d5d52399466e85d80ca7e366c1a5b80a1ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39930b05f9b9e7b9a1fe8635564fab0608e65a31939a038d67f9592fb687c5f03bde1d072d1e5a1f6ad12924cb76802966c078f713b9d05c8a7ad948603ab686
|
7
|
+
data.tar.gz: cf1bac198885038b6149286ffe757908528fa4f659306831c84877b7d39e2dbcdb8136ad86629782cf186387af6e047170242bb5cc1544c174fde8a46194f189
|
data/.gitignore
CHANGED
data/ChangeLog.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
### 0.2.4 / 2024-03-04
|
2
|
+
|
3
|
+
* Support Ruby 3.4.0:
|
4
|
+
* No longer use mutable String literals, and instead use
|
5
|
+
`String.new(encoding: Encoding::UTF_8)` to fix warnings under Ruby 3.4.0.
|
6
|
+
|
7
|
+
### 0.2.3 / 2022-06-18
|
8
|
+
|
9
|
+
* Add [gserver] ~> 0.0 as a dependency.
|
10
|
+
* Switched to using Ruby 2.x keyword arguments.
|
11
|
+
* Fix a bug in {Rack::Handler::HTTP} where the URI `query` string was not being
|
12
|
+
properly loaded.
|
13
|
+
* Fixed a bug in {Net::HTTP::Server::Parser} where the older `absnt?` method
|
14
|
+
was being used instead of the newer `absent?` method.
|
15
|
+
|
1
16
|
### 0.2.2 / 2012-09-08
|
2
17
|
|
3
18
|
* Added an example `rackup` command.
|
@@ -32,3 +47,5 @@
|
|
32
47
|
* Added {Net::HTTP::Server::Responses}.
|
33
48
|
* Added {Net::HTTP::Server::Daemon}.
|
34
49
|
* Added {Rack::Handler::HTTP}.
|
50
|
+
|
51
|
+
[gserver]: https://rubygems.org/gems/gserver
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem 'rake'
|
7
|
+
gem 'rubygems-tasks', '~> 0.1'
|
8
|
+
|
9
|
+
gem 'rack', '~> 2.0'
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
|
+
|
12
|
+
gem 'kramdown'
|
13
|
+
gem 'redcarpet', platform: :mri
|
14
|
+
gem 'yard', '~> 0.9'
|
15
|
+
gem 'yard-spellcheck'
|
16
|
+
end
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# net-http-server
|
2
2
|
|
3
|
-
* [Homepage](
|
4
|
-
* [Issues](
|
5
|
-
* [Documentation](
|
6
|
-
* [Email](mailto:postmodern.mod3 at gmail.com)
|
3
|
+
* [Homepage](https://github.com/postmodern/net-http-server#readme)
|
4
|
+
* [Issues](https://github.com/postmodern/net-http-server/issues)
|
5
|
+
* [Documentation](https://rubydoc.info/gems/net-http-server)
|
7
6
|
|
8
7
|
## Description
|
9
8
|
|
@@ -14,41 +13,50 @@
|
|
14
13
|
* Pure Ruby.
|
15
14
|
* Supports Streamed Request/Response Bodies.
|
16
15
|
* Supports Chunked Transfer-Encoding.
|
17
|
-
* Provides a [Rack](
|
16
|
+
* Provides a [Rack](https://github.com/rack/rack#readme) Handler.
|
18
17
|
|
19
18
|
## Examples
|
20
19
|
|
21
20
|
Simple HTTP Server:
|
22
21
|
|
23
|
-
|
24
|
-
|
22
|
+
```ruby
|
23
|
+
require 'net/http/server'
|
24
|
+
require 'pp'
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
Net::HTTP::Server.run(:port => 8080) do |request,stream|
|
27
|
+
pp request
|
28
28
|
|
29
|
-
|
30
|
-
|
29
|
+
[200, {'Content-Type' => 'text/html'}, ['Hello World']]
|
30
|
+
end
|
31
|
+
```
|
31
32
|
|
32
33
|
Use it with Rack:
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
```ruby
|
36
|
+
require 'rack/handler/http'
|
37
|
+
|
38
|
+
Rack::Handler::HTTP.run app
|
39
|
+
```
|
37
40
|
|
38
41
|
Using it with `rackup`:
|
39
42
|
|
40
|
-
|
43
|
+
```shell
|
44
|
+
$ rackup -s HTTP
|
45
|
+
```
|
41
46
|
|
42
47
|
## Requirements
|
43
48
|
|
44
|
-
* [parslet](http://
|
49
|
+
* [parslet](http://kschiess.github.io/parslet/) ~> 1.0
|
50
|
+
* [gserver](https://rubygems.org/gems/gserver) ~> 0.0
|
45
51
|
|
46
52
|
## Install
|
47
53
|
|
48
|
-
|
54
|
+
```shell
|
55
|
+
$ gem install net-http-server
|
56
|
+
```
|
49
57
|
|
50
58
|
## Copyright
|
51
59
|
|
52
|
-
Copyright (c) 2011 Hal Brodigan
|
60
|
+
Copyright (c) 2011-2022 Hal Brodigan
|
53
61
|
|
54
62
|
See {file:LICENSE.txt} for details.
|
data/Rakefile
CHANGED
@@ -1,36 +1,19 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
|
4
1
|
begin
|
5
|
-
|
6
|
-
require 'rubygems/tasks'
|
7
|
-
|
8
|
-
Gem::Tasks.new
|
2
|
+
require 'bundler/setup'
|
9
3
|
rescue LoadError => e
|
10
4
|
warn e.message
|
11
|
-
warn "Run `gem install
|
5
|
+
warn "Run `gem install bundler` to install Bundler"
|
6
|
+
exit -1
|
12
7
|
end
|
13
8
|
|
14
|
-
|
15
|
-
|
16
|
-
require 'rspec/core/rake_task'
|
9
|
+
require 'rubygems/tasks'
|
10
|
+
Gem::Tasks.new
|
17
11
|
|
18
|
-
|
19
|
-
|
20
|
-
task :spec do
|
21
|
-
abort "Please run `gem install rspec` to install RSpec."
|
22
|
-
end
|
23
|
-
end
|
24
|
-
task :test => :spec
|
25
|
-
task :default => :spec
|
12
|
+
require 'rspec/core/rake_task'
|
13
|
+
RSpec::Core::RakeTask.new
|
26
14
|
|
27
|
-
|
28
|
-
|
29
|
-
require 'yard'
|
15
|
+
task :test => :spec
|
16
|
+
task :default => :spec
|
30
17
|
|
31
|
-
|
32
|
-
|
33
|
-
task :yard do
|
34
|
-
abort "Please run `gem install yard` to install YARD."
|
35
|
-
end
|
36
|
-
end
|
18
|
+
require 'yard'
|
19
|
+
YARD::Rake::YardocTask.new
|
data/examples/config.ru
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'sinatra/base'
|
4
|
+
|
5
|
+
class TestApp < Sinatra::Base
|
6
|
+
|
7
|
+
get '/' do
|
8
|
+
'hello'
|
9
|
+
end
|
10
|
+
|
11
|
+
get '/other' do
|
12
|
+
halt 404
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'rack/handler/http'
|
18
|
+
|
19
|
+
puts "Listening on http://localhost:8080/ ..."
|
20
|
+
Rack::Handler::HTTP.run Rack::Lint.new(TestApp), Port: 8080
|
21
|
+
puts "Shutting down ..."
|
data/gemspec.yml
CHANGED
@@ -4,13 +4,19 @@ description: A Rack compatible pure Ruby HTTP Server.
|
|
4
4
|
license: MIT
|
5
5
|
authors: Postmodern
|
6
6
|
email: postmodern.mod3@gmail.com
|
7
|
-
homepage:
|
7
|
+
homepage: https://github.com/postmodern/net-http-server#readme
|
8
8
|
has_yard: true
|
9
9
|
|
10
|
+
metadata:
|
11
|
+
documentation_uri: https://rubydoc.info/gems/net-http-server
|
12
|
+
source_code_uri: https://github.com/postmodern/net-http-server
|
13
|
+
bug_tracker_uri: https://github.com/postmodern/net-http-server/issues
|
14
|
+
changelog_uri: https://github.com/postmodern/net-http-server/blob/master/ChangeLog.md
|
15
|
+
rubygems_mfa_required: 'true'
|
16
|
+
|
10
17
|
dependencies:
|
11
18
|
parslet: ~> 1.0
|
19
|
+
gserver: ~> 0.0
|
12
20
|
|
13
21
|
development_dependencies:
|
14
|
-
|
15
|
-
rspec: ~> 2.4
|
16
|
-
yard: ~> 0.8
|
22
|
+
bundler: ~> 2.0
|
@@ -14,7 +14,7 @@ module Net
|
|
14
14
|
class ChunkedStream < Stream
|
15
15
|
|
16
16
|
#
|
17
|
-
# Initializes the
|
17
|
+
# Initializes the chunked stream.
|
18
18
|
#
|
19
19
|
# @param [#read, #write, #flush] socket
|
20
20
|
# The socket to read from and write to.
|
@@ -22,7 +22,7 @@ module Net
|
|
22
22
|
def initialize(socket)
|
23
23
|
super(socket)
|
24
24
|
|
25
|
-
@buffer =
|
25
|
+
@buffer = String.new(encoding: Encoding::UTF_8)
|
26
26
|
end
|
27
27
|
|
28
28
|
#
|
@@ -47,9 +47,8 @@ module Net
|
|
47
47
|
end
|
48
48
|
|
49
49
|
until @buffer.length >= length
|
50
|
-
length_line
|
51
|
-
chunk_length
|
52
|
-
chunk_length = chunk_length.to_i(16)
|
50
|
+
length_line = @socket.readline("\r\n").chomp
|
51
|
+
chunk_length = length_line.split(';',2).first.to_i(16)
|
53
52
|
|
54
53
|
# read the chunk
|
55
54
|
@buffer << @socket.read(chunk_length)
|
@@ -26,22 +26,19 @@ module Net
|
|
26
26
|
|
27
27
|
# Creates a new HTTP Daemon.
|
28
28
|
#
|
29
|
-
# @param [
|
30
|
-
# Options for the daemon.
|
31
|
-
#
|
32
|
-
# @option options [String] :host (DEFAULT_HOST)
|
29
|
+
# @param [String] host
|
33
30
|
# The host to run on.
|
34
31
|
#
|
35
|
-
# @
|
32
|
+
# @param [String] port
|
36
33
|
# The port to listen on.
|
37
34
|
#
|
38
|
-
# @
|
35
|
+
# @param [Integer] max_connections
|
39
36
|
# The maximum number of simultaneous connections.
|
40
37
|
#
|
41
|
-
# @
|
38
|
+
# @param [IO] log
|
42
39
|
# The log to write errors to.
|
43
40
|
#
|
44
|
-
# @
|
41
|
+
# @param [#call] handler
|
45
42
|
# The HTTP Request Handler object.
|
46
43
|
#
|
47
44
|
# @yield [request, socket]
|
@@ -53,15 +50,18 @@ module Net
|
|
53
50
|
# @yieldparam [TCPSocket] socket
|
54
51
|
# The TCP socket of the client.
|
55
52
|
#
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
53
|
+
# @raise [ArgumentError]
|
54
|
+
# No `handler:` value was given.
|
55
|
+
#
|
56
|
+
def initialize(host: DEFAULT_HOST,
|
57
|
+
port: DEFAULT_PORT,
|
58
|
+
max_connections: MAX_CONNECTIONS,
|
59
|
+
log: $stderr,
|
60
|
+
handler: nil,
|
61
|
+
&block)
|
62
|
+
super(port.to_i,host,max_connections,log,false,true)
|
63
63
|
|
64
|
-
handler(
|
64
|
+
handler(handler,&block)
|
65
65
|
end
|
66
66
|
|
67
67
|
#
|
@@ -106,7 +106,7 @@ module Net
|
|
106
106
|
|
107
107
|
begin
|
108
108
|
request = parser.parse(raw_request)
|
109
|
-
rescue Parslet::ParseFailed
|
109
|
+
rescue Parslet::ParseFailed
|
110
110
|
return Responses::BAD_REQUEST
|
111
111
|
end
|
112
112
|
|
@@ -9,7 +9,7 @@ module Net
|
|
9
9
|
#
|
10
10
|
# * [Thin](https://github.com/macournoyer/thin/blob/master/ext/thin_parser/common.rl)
|
11
11
|
# * [Unicorn](https://github.com/defunkt/unicorn/blob/master/ext/unicorn_http/unicorn_http_common.rl)
|
12
|
-
# * [RFC
|
12
|
+
# * [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html)
|
13
13
|
#
|
14
14
|
class Parser < Parslet::Parser
|
15
15
|
|
@@ -30,7 +30,7 @@ module Net
|
|
30
30
|
rule(:crlf) { str("\r\n") }
|
31
31
|
|
32
32
|
rule(:ctl) { cntrl | str("\x7f") }
|
33
|
-
rule(:text) { lws | (ctl.
|
33
|
+
rule(:text) { lws | (ctl.absent? >> ascii) }
|
34
34
|
|
35
35
|
rule(:safe) { charset('$', '-', '_', '.') }
|
36
36
|
rule(:extra) { charset('!', '*', "'", '(', ')', ',') }
|
@@ -39,7 +39,7 @@ module Net
|
|
39
39
|
|
40
40
|
rule(:unsafe) { ctl | charset(' ', '#', '%') | sorta_safe }
|
41
41
|
rule(:national) {
|
42
|
-
(alpha | digit | reserved | extra | safe | unsafe).
|
42
|
+
(alpha | digit | reserved | extra | safe | unsafe).absent? >> any
|
43
43
|
}
|
44
44
|
|
45
45
|
rule(:unreserved) { alpha | digit | safe | extra | national }
|
@@ -57,13 +57,13 @@ module Net
|
|
57
57
|
#
|
58
58
|
# Elements
|
59
59
|
#
|
60
|
-
rule(:token) { (ctl | separators).
|
60
|
+
rule(:token) { (ctl | separators).absent? >> ascii }
|
61
61
|
|
62
|
-
rule(:comment_text) { (str('(') | str(')')).
|
62
|
+
rule(:comment_text) { (str('(') | str(')')).absent? >> text }
|
63
63
|
rule(:comment) { str('(') >> comment_text.repeat >> str(')') }
|
64
64
|
|
65
65
|
rule(:quoted_pair) { str("\\") >> ascii }
|
66
|
-
rule(:quoted_text) { quoted_pair | str('"').
|
66
|
+
rule(:quoted_text) { quoted_pair | str('"').absent? >> text }
|
67
67
|
rule(:quoted_string) { str('"') >> quoted_text >> str('"') }
|
68
68
|
|
69
69
|
#
|
@@ -117,7 +117,7 @@ module Net
|
|
117
117
|
http_version
|
118
118
|
}
|
119
119
|
|
120
|
-
rule(:header_name) { (str(':').
|
120
|
+
rule(:header_name) { (str(':').absent? >> token).repeat(1) }
|
121
121
|
rule(:header_value) {
|
122
122
|
(text | token | separators | quoted_string).repeat(1)
|
123
123
|
}
|
@@ -8,23 +8,23 @@ module Net
|
|
8
8
|
#
|
9
9
|
# Starts the HTTP Server.
|
10
10
|
#
|
11
|
-
# @param [
|
12
|
-
#
|
11
|
+
# @param [Boolean] background
|
12
|
+
# Specifies whether to run the server in the background or
|
13
|
+
# foreground.
|
13
14
|
#
|
14
|
-
# @
|
15
|
+
# @param [Hash{Symbol => Object}] kwargs
|
16
|
+
# Additional keyword arguments for {Daemon#initialize}.
|
17
|
+
#
|
18
|
+
# @option kwargs [String] :host (DEFAULT_HOST)
|
15
19
|
# The host to run on.
|
16
20
|
#
|
17
|
-
# @option
|
21
|
+
# @option kwargs [String] :port (DEFAULT_PORT)
|
18
22
|
# The port to listen on.
|
19
23
|
#
|
20
|
-
# @option
|
24
|
+
# @option kwargs [Integer] :max_connections (MAX_CONNECTIONS)
|
21
25
|
# The maximum number of simultaneous connections.
|
22
26
|
#
|
23
|
-
# @option
|
24
|
-
# Specifies whether to run the server in the background or
|
25
|
-
# foreground.
|
26
|
-
#
|
27
|
-
# @option options [#call] :handler
|
27
|
+
# @option kwargs [#call] :handler
|
28
28
|
# The HTTP Request Handler object.
|
29
29
|
#
|
30
30
|
# @yield [request, socket]
|
@@ -36,11 +36,14 @@ module Net
|
|
36
36
|
# @yieldparam [TCPSocket] socket
|
37
37
|
# The TCP socket of the client.
|
38
38
|
#
|
39
|
-
|
40
|
-
|
39
|
+
# @raise [ArgumentError]
|
40
|
+
# No `handler:` value was given.
|
41
|
+
#
|
42
|
+
def Server.run(background: false, **kwargs,&block)
|
43
|
+
daemon = Daemon.new(**kwargs,&block)
|
41
44
|
|
42
45
|
daemon.start
|
43
|
-
daemon.join unless
|
46
|
+
daemon.join unless background
|
44
47
|
return daemon
|
45
48
|
end
|
46
49
|
|
data/lib/rack/handler/http.rb
CHANGED
@@ -37,7 +37,7 @@ module Rack
|
|
37
37
|
# @param [#call] app
|
38
38
|
# The application the handler will be passing requests to.
|
39
39
|
#
|
40
|
-
# @param [Hash] options
|
40
|
+
# @param [Hash{Symbol => Object}] options
|
41
41
|
# Additional options.
|
42
42
|
#
|
43
43
|
# @option options [String] :Host
|
@@ -46,7 +46,7 @@ module Rack
|
|
46
46
|
# @option options [Integer] :Port
|
47
47
|
# The port to listen on.
|
48
48
|
#
|
49
|
-
def initialize(app
|
49
|
+
def initialize(app,**options)
|
50
50
|
@app = app
|
51
51
|
@options = options
|
52
52
|
@server = nil
|
@@ -57,8 +57,8 @@ module Rack
|
|
57
57
|
#
|
58
58
|
# @see #initialize
|
59
59
|
#
|
60
|
-
def self.run(app
|
61
|
-
new(app
|
60
|
+
def self.run(app,**options)
|
61
|
+
new(app,**options).run
|
62
62
|
end
|
63
63
|
|
64
64
|
#
|
@@ -66,9 +66,9 @@ module Rack
|
|
66
66
|
#
|
67
67
|
def run
|
68
68
|
@server = Net::HTTP::Server::Daemon.new(
|
69
|
-
:
|
70
|
-
:
|
71
|
-
:
|
69
|
+
host: @options[:Host],
|
70
|
+
port: @options[:Port],
|
71
|
+
handler: self
|
72
72
|
)
|
73
73
|
|
74
74
|
@server.start
|
@@ -113,12 +113,12 @@ module Rack
|
|
113
113
|
|
114
114
|
env['REQUEST_METHOD'] = request[:method].to_s
|
115
115
|
env['PATH_INFO'] = request_uri.fetch(:path,'*').to_s
|
116
|
-
env['QUERY_STRING'] = request_uri[:
|
116
|
+
env['QUERY_STRING'] = request_uri[:query].to_s
|
117
117
|
|
118
118
|
# add the headers
|
119
119
|
request[:headers].each do |name,value|
|
120
120
|
key = name.dup
|
121
|
-
|
121
|
+
|
122
122
|
key.upcase!
|
123
123
|
key.tr!('-','_')
|
124
124
|
|