didao 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/didao.rb +46 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad59e1bc42717a0910e88956419d0f146284d9b5b2664f0ea484cfee0ef8a7fe
4
+ data.tar.gz: 2841815b8715ff482d13dfc41a341efb9eece074da7673a3997c05b95692de2d
5
+ SHA512:
6
+ metadata.gz: e5cad82644641ec7ef26544d162ce4665ce10028db6cd6f3684c2a41389d49d13192ffff922a32233f97bbb8de290bd3d3743c5416b9c30a18196e2a577befbe
7
+ data.tar.gz: 2b74d50e90c87159d44446bcc959dc14f02724591e81aceb741e13c20050a119fe69c222ffb104d9031cee0217750cb74d65aed504d9efda862a5978928ea8b6
data/lib/didao.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'socket'
4
+
5
+ # A class representing a single gopher server instance
6
+ class Didao
7
+ def initialize(port = 70, path = Dir.pwd)
8
+ @port = port
9
+ @path = path
10
+ @path += '/' unless @path =~ %r{\/$}
11
+ @server = TCPServer.open(port)
12
+ end
13
+
14
+ def process_file(file_name)
15
+ file_name = file_name.sub(%r{^\/.\/}, '')
16
+ File.open(@path + file_name, 'rb').read
17
+ # This is technically nonstandard, as gophermaps should always end with '.'
18
+ # This is best remedied outside this file, which should just be basic
19
+ # server operations.
20
+ rescue Errno::ENOENT => e
21
+ puts e.message
22
+ 'Not Found'
23
+ end
24
+
25
+ def handle_request(request, source)
26
+ gopher_path = request.gsub("\r\n", '<CR><LF>')
27
+ puts '---'
28
+ puts "#{Time.now}: Received request: \"#{gopher_path}\" from #{source}"
29
+ file = request == "\r\n" ? 'gophermap' : request.chomp
30
+ puts "#{Time.now}: Returning requested file: \"#{file}\""
31
+ process_file(file)
32
+ end
33
+
34
+ def run
35
+ puts "Didao listening on #{@port}. Press Ctrl+C to exit."
36
+ loop do
37
+ client = @server.accept
38
+ Thread.start(client) do |conn|
39
+ conn.puts handle_request(conn.readline, conn.peeraddr[3])
40
+ conn.close
41
+ end
42
+ end
43
+ rescue Interrupt
44
+ puts "\nClosing Didao Server..."
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: didao
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Church
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ A very simple gopher server, which can be use to get a\
15
+ simple gopher site up quickly, or to build a better server.
16
+ email: kyle.church@surdegg.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/didao.rb
22
+ homepage: https://bitbucket.org/SurdEgg/didao/src/master/
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A very simple gopher server.
45
+ test_files: []