Created
July 31, 2015 20:57
-
-
Save dwayne/309ed557819ef8a129d0 to your computer and use it in GitHub Desktop.
Revisions
-
dwayne created this gist
Jul 31, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ I will use the simple Sinatra application below to explain. First, we run the app. ``` $ ruby app.rb ``` This launches a server at `localhost:4567`. 1. Open your browser at `http://localhost:4567`. 2. Enter your name, say Dwayne, and click **Set**. 3. Doing so causes your browser to send a **POST** request to **/set** with **name=Dwayne**. 4. The corresponding route handler then "sets a cookie" under the name **name** with value **Dwayne**. 5. What really happens is that the **Set-Cookie** header is set with **name=Dwayne**. 6. The redirect is executed causing a `303 status` and at least the **Set-Cookie** and **Location** headers to be sent to the browser. 7. The browser stores the cookie and will send it via the **COOKIE** header with any subsequent request, which happens to be now. 8. You see, the 303 redirect causes the browser to immediately make another request to the URL given in the **Location** header, **/**. Hence, the cookie information will be sent along. 9. The corresponding route handler then fetches the relevant cookie and displays the appropriate message. **References** - [My HTTP Cookies Bookmarks](https://pinboard.in/search/u:dwaynecrooks?query=http-cookies) - [Rack `response.set_cookie`](http://www.rubydoc.info/github/rack/rack/master/Rack/Response#set_cookie-instance_method) - [Rack `request.cookies`](http://www.rubydoc.info/github/rack/rack/master/Rack/Request#cookies-instance_method) - [Where does Google Chrome store cookies in Linux?](http://stackoverflow.com/q/1841901/391924) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ source 'https://rubygems.org' gem 'sinatra' gem 'thin' This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ require 'sinatra' get '/' do @name = request.cookies['name'] || 'world' erb :index end post '/set' do response.set_cookie('name', params[:name]) redirect to('/') end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ <p> Hello, <%= @name %>! </p> <form action="/set" method="POST"> <label> What's your name?<br> <input type="text" name="name"> </label> <button type="submit">Set</button> </form>