

Ruby frameworks like Rails and Sinatra are built on top of the Rack interface. If you want to follow along, here's the code we ended up with. We'll be continuing with the code we wrote last time. Using those, we were able to build an HTTP response to send back to the browser, before closing the connection to wait for a new request to come in.ġ # http_server.rb 2 require 'socket' 3 require 'rack' 4 require 'rack/lobster' 5 6app = Rack::Lobster.newħ server = TCPServer.new 5678 8 9 #1 10 while session = server.acceptġ3 14 #2 15 method, full_path = request.split( ' ' )ġ7 18 #3 19 status, headers, body = app.call( \r\n" 29 end 30 session.print "\r\n" 31 body.each do |part| 32 session.print part.The request method, the path and the query string were passed to the Rack app, which returned a triplet with a status, some response headers and the response body.When that happened, the request-line ( GET /?flip=left HTTP/1.1\r\n) was parsed to get the request method ( GET), the path ( /), and the query parameters ( flip=left).


We publish a new article about once a month, so be sure to subscribe to our newsletter if you're into this sort of thing too. It's all about the process the end result isn't something you'd use in production, we learn about the internal workings of the Ruby language and its popular libraries. In the Ruby Magic series we love to take software apart to learn how it functions under the hood.
