http-session

Version, currently master branch1 version
  • master branchlatestOct 10, 2024

github.com/straight-shoota/http-session

Type-safe sessions for `HTTP::Server`

3 stars
1 dependent
License: Apache-2.0

Installation

# Add this to your shard.yml
dependencies:
  http-session:
    github: straight-shoota/http-session
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
>= 0.36.1
License
Apache-2.0
Author
Johannes Müller

Dependencies

This version declares no dependencies.

README

Build Status

http-session

This shard provides type-safe sessions for HTTP::Server.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      http-session:
        github: straight-shoota/http-session
  2. Run shards install

Usage

The basic tool for session management is HTTPSession::Manager.

The session manager uses a HTTPSession::Storage backend for storage.

Currently available implementations:

  • HTTPSession::Storage::Memory: In-memory storage. Won't persist beyond server restarts.

Example

require "http-session"

storage = HTTPSession::Storage::Memory(Int32).new
sessions = HTTPSession::Manager.new(storage)

server = HTTP::Server.new([HTTP::LogHandler.new, HTTP::ErrorHandler.new]) do |context|
  if context.request.path == "/"
    counter = sessions.get(context) || 0
    counter += 1
    sessions.set(context, counter)
    context.response.puts counter
  else
    context.response.respond_with_status :not_found
  end
end

address = server.bind_tcp 0
puts "Listening on http://#{address}"

server.listen

More examples can be found in examples/.

Security

This shard follows the guidelines from the OWASP Session Management Cheat Sheet in order to apply best practices for effective threat control.

Contributing

  1. Fork it (https://github.com/straight-shoota/http-session/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors