net-socket 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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.markdown +55 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/examples/http-server.rb +32 -0
- data/lib/net/socket/socket-hack.rb +10 -0
- data/lib/net/socket/tcp/client.rb +8 -0
- data/lib/net/socket/tcp/server.rb +45 -0
- data/lib/net/socket/tcp.rb +8 -0
- data/lib/net/socket/version.rb +7 -0
- data/lib/net/socket.rb +8 -0
- data/net-socket.gemspec +24 -0
- metadata +90 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c34f14fd3ccc2e318c29eaac5a6f88a65a4dd40d
|
|
4
|
+
data.tar.gz: 938de9bf66a478a26d17a1e86708163f1ca3fded
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6f3db470fb460a3a31950b3155d3ae1428ef162d7f984c8cff95df04aaf23dc0b9e6db8ac85126db2f1bddac8168d9e26cc65e3221762887f07f704fc0e8164e
|
|
7
|
+
data.tar.gz: 889052ebb43a98d16c9d145b2d07fcd2a05688c6f8104185e56cd41155934a56194416de110673235abcc2d10b7dcc32aaf8ca140ff084002ef0b8d46eb65657
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Marie Markwell <me@marie.so>
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Net::Socket
|
|
2
|
+
|
|
3
|
+
A fancy-pants socket API for Ruby.
|
|
4
|
+
|
|
5
|
+
## Bugs
|
|
6
|
+
|
|
7
|
+
This is actually a bug with Ruby, not Net::Socket, but if you `require 'net/socket'` between creating a `Net::HTTP` or `Net::FTP` instance, Net::HTTP or Net::FTP may raise an "uninitialized constant" exception. The reason for this is that Net::HTTP and Net::FTP reference `Socket`, which winds up referencing Net::Socket. There is a small hack-ish fix for this in [lib/net/socket/socket-hack.rb](https://gitlab.com/spinny/net-socket/blob/main/lib/net/socket/socket-hack.rb), and I will be submitting a pull request to fix it in Ruby.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'net-socket'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install net-socket
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
# Yay echo server!
|
|
29
|
+
|
|
30
|
+
require 'net/socket'
|
|
31
|
+
include Net::Socket
|
|
32
|
+
|
|
33
|
+
async = !!ARGV.delete('--async')
|
|
34
|
+
socket = TCP::Server.new('0.0.0.0', 9001)
|
|
35
|
+
|
|
36
|
+
socket.each_request(async) do |conn|
|
|
37
|
+
conn.puts conn.read
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
socket.wait # Wait for the socket to close.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
46
|
+
|
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
48
|
+
|
|
49
|
+
## Contributing
|
|
50
|
+
|
|
51
|
+
1. Fork it ( https://gitlab.com/spinny/net-socket/fork )
|
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
55
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "net/socket"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
|
4
|
+
|
|
5
|
+
require 'net/socket'
|
|
6
|
+
|
|
7
|
+
include Net::Socket
|
|
8
|
+
|
|
9
|
+
async = !!ARGV.delete('--async')
|
|
10
|
+
socket = TCP::Server.new("0.0.0.0", 8000)
|
|
11
|
+
|
|
12
|
+
puts "Server listening #{async ? 'a' : ''}synchronously at 0.0.0.0:8000."
|
|
13
|
+
|
|
14
|
+
socket.each_request(async) do |conn|
|
|
15
|
+
# Store response in a variable so that we can get the exact length later.
|
|
16
|
+
response = "<html><body>Hello from Ruby socket world!</body></html>"
|
|
17
|
+
|
|
18
|
+
# Protocol, version, and status code.
|
|
19
|
+
conn.write "HTTP/1.0 200 OK\r\n"
|
|
20
|
+
|
|
21
|
+
# Start headers.
|
|
22
|
+
conn.write "Server: Ruby Met::Socket example\r\n"
|
|
23
|
+
conn.write "Content-Type: text/html\r\n"
|
|
24
|
+
conn.write "Content-Length: #{response.length}\r\n"
|
|
25
|
+
conn.write "\r\n"
|
|
26
|
+
# End headers.
|
|
27
|
+
|
|
28
|
+
# Content.
|
|
29
|
+
conn.write response
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
socket.wait
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'net/socket/version'
|
|
2
|
+
require 'socket'
|
|
3
|
+
|
|
4
|
+
module Net::Socket::TCP
|
|
5
|
+
class Server < TCPServer
|
|
6
|
+
def each_request(async = false, &block)
|
|
7
|
+
if async
|
|
8
|
+
each_request_async(&block)
|
|
9
|
+
else
|
|
10
|
+
each_request_sync(&block)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def wait
|
|
15
|
+
IO.select([socket]) until socket.closed?
|
|
16
|
+
rescue
|
|
17
|
+
# We don't care about IO.select-related exceptions,
|
|
18
|
+
# so we just ignore them and return.
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
def each_request_sync(&block)
|
|
23
|
+
loop { handle_request(accept(), &block) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def each_request_async(&block)
|
|
27
|
+
Thread.new do
|
|
28
|
+
loop do
|
|
29
|
+
Thread.new(accept(), block) do |request, block|
|
|
30
|
+
handle_request(request, &block)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Return nil to avoid collecting references to Threads.
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def handle_request(request, &block)
|
|
40
|
+
yield request
|
|
41
|
+
|
|
42
|
+
request.close
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/net/socket.rb
ADDED
data/net-socket.gemspec
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'net/socket/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "net-socket"
|
|
8
|
+
spec.version = Net::Socket::VERSION
|
|
9
|
+
spec.authors = ["Marie Markwell"]
|
|
10
|
+
spec.email = ["me@marie.so"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{A nice wrapper for Ruby's built in socket APIs.}
|
|
13
|
+
spec.description = spec.summary
|
|
14
|
+
spec.homepage = "https://gitlab.com/spinny/net-socket"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
spec.bindir = "exe"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: net-socket
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Marie Markwell
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-10-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.8'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.8'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
description: A nice wrapper for Ruby's built in socket APIs.
|
|
42
|
+
email:
|
|
43
|
+
- me@marie.so
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".gitignore"
|
|
49
|
+
- ".rspec"
|
|
50
|
+
- ".travis.yml"
|
|
51
|
+
- Gemfile
|
|
52
|
+
- LICENSE
|
|
53
|
+
- README.markdown
|
|
54
|
+
- Rakefile
|
|
55
|
+
- bin/console
|
|
56
|
+
- bin/setup
|
|
57
|
+
- examples/http-server.rb
|
|
58
|
+
- lib/net/socket.rb
|
|
59
|
+
- lib/net/socket/socket-hack.rb
|
|
60
|
+
- lib/net/socket/tcp.rb
|
|
61
|
+
- lib/net/socket/tcp/client.rb
|
|
62
|
+
- lib/net/socket/tcp/server.rb
|
|
63
|
+
- lib/net/socket/version.rb
|
|
64
|
+
- net-socket.gemspec
|
|
65
|
+
homepage: https://gitlab.com/spinny/net-socket
|
|
66
|
+
licenses:
|
|
67
|
+
- MIT
|
|
68
|
+
metadata: {}
|
|
69
|
+
post_install_message:
|
|
70
|
+
rdoc_options: []
|
|
71
|
+
require_paths:
|
|
72
|
+
- lib
|
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
requirements: []
|
|
84
|
+
rubyforge_project:
|
|
85
|
+
rubygems_version: 2.2.2
|
|
86
|
+
signing_key:
|
|
87
|
+
specification_version: 4
|
|
88
|
+
summary: A nice wrapper for Ruby's built in socket APIs.
|
|
89
|
+
test_files: []
|
|
90
|
+
has_rdoc:
|