JustPaste.it

require 'rack/lobster'
require "openssl"

app = Rack::Lobster.new
tcp_server = TCPServer.new('0.0.0.0', 443)
# http_server.rb
require 'socket'
require 'rack'
require 'rack/lobster'
require "openssl"

app = Rack::Lobster.new
tcp_server = TCPServer.new('0.0.0.0', 443)

 

sslContext = OpenSSL::SSL::SSLContext.new
sslContext.verify_mode = OpenSSL::SSL::VERIFY_NONE
sslContext.cert = OpenSSL::X509::Certificate.new(File.open("certificates/certificate.pem"))
sslContext.key = OpenSSL::PKey::RSA.new(File.open("certificates/key.pem"))
server = OpenSSL::SSL::SSLServer.new(tcp_server, sslContext)


loop {
  Thread.start(server.accept) do |session|

      request = session.gets
      puts request

      # 1
      method, full_path = request.split(' ')
      # 2
      path, query = full_path.split('?')

      # 3
      status, headers, body = app.call({
        'REQUEST_METHOD' => method,
        'PATH_INFO' => path,
        'QUERY_STRING' => query
      })

      session.print "HTTP/1.1 #{status}\r\n"
      headers.each do |key, value|
        session.print "#{key}: #{value}\r\n"
      end
      session.print "\r\n"
      body.each do |part|
        session.print part
      end
      session.close
  end
}