class SCV::Server

This is only intended to be a very basic web server that serves files from the ‘.scv` directory and also allows uploading files there.

Wihout! Any! Authentication!

You do understand what this means, right?

Attributes

port[R]

Public Class Methods

new(repository_path, port: 4242) click to toggle source
# File lib/scv/server.rb, line 17
def initialize(repository_path, port: 4242)
  @repository_path = repository_path
  @port            = port
end

Public Instance Methods

start() click to toggle source
# File lib/scv/server.rb, line 22
def start
  Rack::Handler::WEBrick.run method(:request_handler), {Port: @port}
end

Private Instance Methods

fetch(path, request) click to toggle source
# File lib/scv/server.rb, line 42
def fetch(path, request)
  return [404, {}, []] unless File.file?(path)

  file = File.new path, 'rb'

  [200, {}, file]
end
head(path, request) click to toggle source
# File lib/scv/server.rb, line 59
def head(path, request)
  status = File.file?(path) ? 200 : 404

  [status, {}, []]
end
request_handler(env) click to toggle source
# File lib/scv/server.rb, line 28
def request_handler(env)
  request = Rack::Request.new(env)

  if request.get?
    fetch File.join(@repository_path, env['REQUEST_PATH']), request
  elsif request.put?
    store File.join(@repository_path, env['REQUEST_PATH']), request
  elsif request.head?
    head  File.join(@repository_path, env['REQUEST_PATH']), request
  else
    [405, {Allow: 'GET, POST, HEAD'}, ['Unsupported HTTP method!']]
  end
end
store(path, request) click to toggle source
# File lib/scv/server.rb, line 50
def store(path, request)
  content = request.body.read

  FileUtils.mkdir_p File.dirname(path)
  File.write path, content

  [200, {}, []]
end