Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gorilla/sessions
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.3
Choose a base ref
...
head repository: gorilla/sessions
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.2.0
Choose a head ref
  • 8 commits
  • 9 files changed
  • 5 contributors

Commits on Oct 12, 2018

  1. README.md: Update site URL

    kisielk authored Oct 12, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8619d3c View commit details

Commits on Oct 15, 2018

  1. Run go mod tidy

    Needing to quote the package strings is from an earlier version of vgo.
    keegancsmith authored and kisielk committed Oct 15, 2018
    Copy the full SHA
    68d1ede View commit details

Commits on Nov 27, 2018

  1. [docs] Fix type in README example

    nikhita authored and elithrar committed Nov 27, 2018
    Copy the full SHA
    4109461 View commit details

Commits on Dec 7, 2018

  1. Add stalebot config

    elithrar authored and kisielk committed Dec 7, 2018
    Copy the full SHA
    b72c0ab View commit details

Commits on Dec 8, 2018

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    726776d View commit details
  2. Use golang context pkg instead of gorilla/context to fix memory leaks (

    …#175)
    
    * - use golang context pkg instead of gorilla/context to fix memory leaks
    * - add test case for checking request context content upon shallow copy
    * - update docs, readme.md and travis.yml
    secracon authored and elithrar committed Dec 8, 2018
    1
    Copy the full SHA
    12bd476 View commit details

Commits on Jun 27, 2019

  1. Create config.yml (#195)

    * Create config.yml
    
    * Update config.yml
    
    * Delete .travis.yml
    elithrar authored Jun 27, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    56c33e9 View commit details

Commits on Jul 9, 2019

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4355a99 View commit details
Showing with 118 additions and 59 deletions.
  1. +65 −0 .circleci/config.yml
  2. +12 −0 .github/stale.yml
  3. +0 −28 .travis.yml
  4. +2 −14 README.md
  5. +0 −8 doc.go
  6. +2 −5 go.mod
  7. +2 −0 go.sum
  8. +4 −4 sessions.go
  9. +31 −0 sessions_test.go
65 changes: 65 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
version: 2.0

jobs:
# Base test configuration for Go library tests Each distinct version should
# inherit this base, and override (at least) the container image used.
"test": &test
docker:
- image: circleci/golang:latest
working_directory: /go/src/github.com/gorilla/sessions
steps: &steps
- checkout
- run: go version
- run: go get -t -v ./...
- run: diff -u <(echo -n) <(gofmt -d .)
- run: if [[ "$LATEST" = true ]]; then go vet -v .; fi
- run: go test -v -race ./...

"latest":
<<: *test
environment:
LATEST: true


"1.12":
<<: *test
docker:
- image: circleci/golang:1.12

"1.11":
<<: *test
docker:
- image: circleci/golang:1.11

"1.10":
<<: *test
docker:
- image: circleci/golang:1.10

"1.9":
<<: *test
docker:
- image: circleci/golang:1.9

"1.8":
<<: *test
docker:
- image: circleci/golang:1.8

"1.7":
<<: *test
docker:
- image: circleci/golang:1.7


workflows:
version: 2
build:
jobs:
- "latest"
- "1.12"
- "1.11"
- "1.10"
- "1.9"
- "1.8"
- "1.7"
12 changes: 12 additions & 0 deletions .github/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
daysUntilStale: 60
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- v2
- needs-review
- work-required
staleLabel: stale
markComment: >
This issue has been automatically marked as stale because it hasn't seen
a recent update. It'll be automatically closed in a few days.
closeComment: false
28 changes: 0 additions & 28 deletions .travis.yml

This file was deleted.

16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ Let's start with an example that shows the sessions API in a nutshell:
// environmental variable, or flag (or both), and don't accidentally commit it
// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY"))
var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))

func MyHandler(w http.ResponseWriter, r *http.Request) {
// Get a session. We're ignoring the error resulted from decoding an
@@ -51,20 +51,8 @@ secret key used to authenticate the session. Inside the handler, we call
some session values in session.Values, which is a `map[interface{}]interface{}`.
And finally we call `session.Save()` to save the session in the response.

Important Note: If you aren't using gorilla/mux, you need to wrap your handlers
with
[`context.ClearHandler`](http://www.gorillatoolkit.org/pkg/context#ClearHandler)
or else you will leak memory! An easy way to do this is to wrap the top-level
mux when calling http.ListenAndServe:

```go
http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))
```

The ClearHandler function is provided by the gorilla/context package.

More examples are available [on the Gorilla
website](http://www.gorillatoolkit.org/pkg/sessions).
website](https://www.gorillatoolkit.org/pkg/sessions).

## Store Implementations

8 changes: 0 additions & 8 deletions doc.go
Original file line number Diff line number Diff line change
@@ -59,14 +59,6 @@ session.Save(r, w), and either display an error message or otherwise handle it.
Save must be called before writing to the response, otherwise the session
cookie will not be sent to the client.
Important Note: If you aren't using gorilla/mux, you need to wrap your handlers
with context.ClearHandler as or else you will leak memory! An easy way to do this
is to wrap the top-level mux when calling http.ListenAndServe:
http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))
The ClearHandler function is provided by the gorilla/context package.
That's all you need to know for the basic usage. Let's take a look at other
options, starting with flash messages.
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module "github.com/gorilla/sessions"
module github.com/gorilla/sessions

require (
"github.com/gorilla/context" v1.1.1
"github.com/gorilla/securecookie" v1.1.1
)
require github.com/gorilla/securecookie v1.1.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
8 changes: 4 additions & 4 deletions sessions.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,11 @@
package sessions

import (
"context"
"encoding/gob"
"fmt"
"net/http"
"time"

"github.com/gorilla/context"
)

// Default flashes key.
@@ -108,15 +107,16 @@ const registryKey contextKey = 0

// GetRegistry returns a registry instance for the current request.
func GetRegistry(r *http.Request) *Registry {
registry := context.Get(r, registryKey)
var ctx = r.Context()
registry := ctx.Value(registryKey)
if registry != nil {
return registry.(*Registry)
}
newRegistry := &Registry{
request: r,
sessions: make(map[string]sessionInfo),
}
context.Set(r, registryKey, newRegistry)
*r = *r.WithContext(context.WithValue(ctx, registryKey, newRegistry))
return newRegistry
}

31 changes: 31 additions & 0 deletions sessions_test.go
Original file line number Diff line number Diff line change
@@ -153,6 +153,37 @@ func TestFlashes(t *testing.T) {
if custom.Type != 42 || custom.Message != "foo" {
t.Errorf("Expected %#v, got %#v", FlashMessage{42, "foo"}, custom)
}

// Round 5 ----------------------------------------------------------------
// Check if a request shallow copy resets the request context data store.

req, _ = http.NewRequest("GET", "http://localhost:8080/", nil)

// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}

// Put a test value into the session data store.
session.Values["test"] = "test-value"

// Create a shallow copy of the request.
req = req.WithContext(req.Context())

// Get the session again.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}

// Check if the previous inserted value still exists.
if session.Values["test"] == nil {
t.Fatalf("Session test value is lost in the request context!")
}

// Check if the previous inserted value has the same value.
if session.Values["test"] != "test-value" {
t.Fatalf("Session test value is changed in the request context!")
}
}

func TestCookieStoreMapPanic(t *testing.T) {