Skip to content

Instantly share code, notes, and snippets.

@dwayne
Created July 31, 2015 20:57
Show Gist options
  • Select an option

  • Save dwayne/309ed557819ef8a129d0 to your computer and use it in GitHub Desktop.

Select an option

Save dwayne/309ed557819ef8a129d0 to your computer and use it in GitHub Desktop.

Revisions

  1. dwayne created this gist Jul 31, 2015.
    26 changes: 26 additions & 0 deletions 0-how-cookies-work.md
    Original 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)
    4 changes: 4 additions & 0 deletions 1-app-Gemfile.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    source 'https://rubygems.org'

    gem 'sinatra'
    gem 'thin'
    11 changes: 11 additions & 0 deletions 1-app-app.rb
    Original 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
    11 changes: 11 additions & 0 deletions 1-app-views-index.erb
    Original 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>