Sha256: 850ffed67f387506f3d19d78c2bc8d5eb8173f3335d0a24563af3baec35622e3

Contents?: true

Size: 1.48 KB

Versions: 47

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'
require 'http/parser'
require 'rack'

module RackAdapter
  class << self
    def run(app)
      ->(socket, req) { respond(socket, req, app.(env(req))) }
    end

    def env(req)
      {}
    end

    def respond(socket, request, (status_code, headers, body))
      body = body.join
      headers = "Content-Type: text/plain\r\nContent-Length: #{body.bytesize}\r\n"
      socket.write "HTTP/1.1 #{status_code}\r\n#{headers}\r\n#{body}"
    end
  end
end

$connection_count = 0

def handle_client(socket, &handler)
  $connection_count += 1
  parser = Http::Parser.new
  reqs = []
  parser.on_message_complete = proc do |env|
    reqs << Object.new # parser
  end
  socket.read_loop do |data|
    parser << data
    while (req = reqs.shift)
      handler.call(socket, req)
      req = nil
    end
  end
rescue IOError, SystemCallError => e
  # do nothing
ensure
  $connection_count -= 1
  socket&.close
end

def handle_request(client, parser)
  status_code = "200 OK"
  data = "Hello world!\n"
  headers = "Content-Type: text/plain\r\nContent-Length: #{data.bytesize}\r\n"
  client.write "HTTP/1.1 #{status_code}\r\n#{headers}\r\n#{data}"
end

server = TCPServer.open('0.0.0.0', 1234)
puts "pid #{Process.pid}"
puts "listening on port 1234"

app = RackAdapter.run(lambda { |env|
  [
    200,
    {"Content-Type" => "text/plain"},
    ["Hello, world!\n"]
  ]
})

loop do
  client = server.accept
  spin { handle_client(client, &app) }
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
polyphony-1.6 examples/io/rack_server.rb
polyphony-1.5 examples/io/rack_server.rb
polyphony-1.4 examples/io/rack_server.rb
polyphony-1.3 examples/io/rack_server.rb
polyphony-1.2.1 examples/io/rack_server.rb
polyphony-1.2 examples/io/rack_server.rb
polyphony-1.1.1 examples/io/rack_server.rb
polyphony-1.1 examples/io/rack_server.rb
polyphony-1.0.2 examples/io/rack_server.rb
polyphony-1.0.1 examples/io/rack_server.rb
polyphony-1.0 examples/io/rack_server.rb
polyphony-0.99.6 examples/io/rack_server.rb
polyphony-0.99.5 examples/io/rack_server.rb
polyphony-0.99.4 examples/io/rack_server.rb
polyphony-0.99.3 examples/io/rack_server.rb
polyphony-0.99.2 examples/io/rack_server.rb
polyphony-0.99.1 examples/io/rack_server.rb
polyphony-0.99 examples/io/rack_server.rb
polyphony-0.98 examples/io/rack_server.rb
polyphony-0.97 examples/io/rack_server.rb