Last active
December 27, 2024 21:57
-
-
Save jeffkreeftmeijer/7f08d1f7e381b9c552666750914925eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'rack' | |
require 'rack/lobster' | |
app = Rack::Lobster.new | |
server = TCPServer.new 5678 | |
while session = server.accept | |
request = session.gets | |
puts request | |
method, full_path = request.split(' ') | |
path, query = full_path.split('?') | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
server = TCPSocket.new 'localhost', 5678 | |
while line = server.gets | |
puts line | |
end | |
server.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
server = TCPServer.new 5678 | |
while session = server.accept | |
session.print "Hello world! The time is #{Time.now}" | |
session.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment