
If the index.html exists within /somedir, then it will be served up without any problems. That request will automatically be converted by our server into /somedir/index.html. Suppose a request is received for /somedir.
#RUBY HTTP CLIENT CODE#
This change is more simple than it seems, and can be accomplished by addingĪ single line of code to our server script: Servers will serve an index file when the client requests a directory.

If you visit in your web browser, you’ll see a 404 Notįound response, even though you’ve created an index.html file. Upon running the script, you should be able toįiles outside of the public/ folder. In a public/ folder that is contained within the same directory as your With the implementation shown above, and then create an index.html file Replace the requested_file stub in the example from the previous section To test this implementation (and finally see your file server in action), pop : clean << part end # return the web root joined to the clean path File. # Otherwise, add the component to the Array of clean components part = '.' ? clean. empty? || part = '.' # If the path component goes up one directory level ("."), # remove the last clean component. each do | part | # skip any empty or current directory (".") path components next if part. path ) clean = # Split the path into components parts = path. Into a path to a file in the server’s public folder:ĭef requested_file ( request_line ) request_uri = request_line. Out any parameters and URI-encoding, and then finally turn that Server’s filesystem is easy: you extract the Request-URI, scrub Practically speaking, mapping the Request-Line to a file on the Is only one more feature left to implement before we can serveįiles over HTTP: the requested_file method. Similar to the kind we’d encounter in everyday code. “Hello World” example, most of it is routine file manipulation close endĪlthough there is a lot more code here than what we saw in the print "HTTP/1.1 200 OK \r\n " + "Content-Type: text/plain \r\n " + "Content-Length: # \r\n " + "Connection: close \r\n " socket. Note that HTTP is whitespace # sensitive, and expects each header line to end with CRLF (i.e. puts request response = "Hello World! \n " # We need to include the Content-Type and Content-Length headers # to let the client know the size and type of data # contained in the response. gets # Log the request to the console for debugging STDERR. accept # Read the first line of the request (the Request-Line) request = socket. (In fact, TCPSocket is a subclass of IO.) socket = server. loop do # Wait until a client connects, then return a TCPSocket # that can be used in a similar fashion to other Ruby # I/O objects. new ( 'localhost', 2345 ) # loop infinitely, processing one incoming # connection at a time. Require 'socket' # Provides TCPServer and TCPSocket classes # Initialize a TCPServer object that will listen # on localhost:2345 for incoming connections. The server accepts the connection, opening aĢ) When the connection has been made, the HTTP client sends a HTTP request: The following steps roughly cover the typical HTTP request/response lifecycle:ġ) The browser issues an HTTP request by opening a TCP socket connection toĮ on port 80.

What happens at the protocol level when someone clicks a We’ll need come down from the clouds a bit in order to explore Them for a living, but much of our work is done far above the HTTP level. We all use web applications daily and many of us build Pieces of technology that we all use on a regular basis. It will allow you to look under the hood of one of the most fundamental We build will not be robust or anywhere near feature complete,
#RUBY HTTP CLIENT HOW TO#
Technique by building a simple HTTP server in Ruby.īy the time you’re done reading, you will know how to serve files from yourĬomputer to a web browser with no dependencies other than a few standard Implementing a simpler version of a technology that you use every day can Works on everything from web crawling to answering support requests. This article was written by Luke Francl, a Ruby developer living in
