Rack is a modular interface between
web server
A web server is computer software and underlying hardware that accepts requests via HTTP (the network protocol created to distribute web content) or its secure variant HTTPS. A user agent, commonly a web browser or web crawler, initi ...
s and
web application
A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection.
History
In earlier computing models like client-serve ...
s developed in the
Ruby programming language. With Rack,
application programming interfaces (APIs) for
web framework
A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build an ...
s and
middleware
Middleware is a type of computer software that provides services to software applications beyond those available from the operating system. It can be described as "software glue".
Middleware makes it easier for software developers to implement c ...
are
wrapped into a single
method call
A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
handling
HTTP requests and
responses.
Rack is used by many Ruby web frameworks and
libraries
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
, such as
Ruby on Rails
Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
and
Sinatra. It is available as a Ruby
Gem
A gemstone (also called a fine gem, jewel, precious stone, or semiprecious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewelry or other adornments. However, certain rocks (such as lapis lazuli, opal, a ...
. Many Ruby applications are called "rack-compliant".
Rack has inspired similar frameworks in
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
(jack.js),
Clojure,
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
(
Plack),
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
(Clack), and
.NET (
OWIN).
Overview
The characteristics of a Rack application is that the application object responds to the call method. The call method takes in the environment object as argument and returns the Rack response object.
Environment
The environment that is taken as argument by the call method refers to an object that has:
a) Information on the HTTP Request
This includes the information like:
*
HTTP request method
*The
URL
A Uniform Resource Locator (URL), colloquially termed as a web address, is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it. A URL is a specific type of Uniform Resource Identifie ...
information(information that would direct to the application, information that directs to the actual location in the application,
Query string
A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters. A query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML, cho ...
)
*Server information like the server name and server port
*The HTTP
metavariables that are received from the client
b) Rack specific information
This includes the information like
* The version of the Rack application that is running
* The URL scheme that is used, that is, if the request that is received is http or https.
* The raw HTTP data.
* A Ruby object for reporting errors.
* Information like if the application object is simultaneously invoked from another thread or process.
* Information on the server expectations and capabilities (capability of the server for connection hijacking).
In case the application is being used as a middleware, the environment can have objects that would provide session information, logging capabilities, information on the size of the data that can be used for read and writes etc. In addition to these, the server can store their own data in the environment.
Rack response
The rack server object returns a response which contains three parts: the status, headers and the body.
*The status contains the
HTTP status codes such as 200,404.
*The header contains the response for each and gives the key-value pairs. The keys have to be strings.
* Body contains the final data which is sent by the server to the requester.
Rack::Response provides a convenient interface to create a Rack response. The class Rack::Response is defined in lib/rack/response.rb. To use the Response class, instantiate it from the middleware layer down the stack. It can be used to modify the cookies.
Middleware in racks
Rack makes it easy to add a chain of
middleware
Middleware is a type of computer software that provides services to software applications beyond those available from the operating system. It can be described as "software glue".
Middleware makes it easier for software developers to implement c ...
components between the application and the web server. Multiple middleware components can be used in the rack which modifies the request/response before handing it over to the next component. This is called middleware stack.
The Rack server adds multiple middle middleware by default for the functionalities like showing exception with all the details, validating the request and responses according to the Rack spec
etc.
Example application
A Rack-compatible "
Hello World
''Hello'' is a salutation or greeting in the English language. It is first attested in writing from 1826. Early uses
''Hello'', with that spelling, was used in publications in the U.S. as early as the 18 October 1826 edition of the '' Norwich ...
" application in
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
syntax:
# helloWorld.ru
# The application that has the call method defined.
class HelloWorld
# Call method that would return the HTTP status code, the content type and the content.
def call (env)
00, , ["Hello World"
end
end
run HelloWorld.new
The server for the above code can be initiated using "rackup helloWorld.ru" and can be accessed at http://localhost:9292/ The default port used by the Rack application is 9292.
See also
*
Python WSGI
*
Perl PSGI
* JSGI">Javascript JSGI
* Python Paste">PSGI">Perl_PSGI&l.html" ;"title="PSGI.html" ;"title="Wsgi">Python WSGI
*
Perl PSGI
* JSGI">Javascript JSGI
* Python Paste
* Seaside (software)">Smalltalk Seaside
* FastCGI">PSGI">Perl PSGI
* JSGI">Javascript JSGI
* Python Paste
* Seaside (software)">Smalltalk Seaside
* FastCGI
* Java Servlet
* Server-side JavaScript
* Apache JServ Protocol
* Internet Communications Engine, ZeroC Ice
* Etch (protocol), Cisco Etch
* ISAPI Internet Server Application Programming Interface (Microsoft)
References
External links
*
{{DEFAULTSORT:Rack (Web Server Interface)
Free software programmed in Ruby
Software using the MIT license