-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:rails/rails
- Loading branch information
Showing
94 changed files
with
1,575 additions
and
392 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
pkg | ||
.bundle | ||
Gemfile.lock | ||
# Don't put *.swp, *.bak, etc here; those belong in a global ~/.gitignore. | ||
# Check out http://help.github.com/ignore-files/ for how to set that up. | ||
|
||
debug.log | ||
doc/rdoc | ||
activemodel/doc | ||
activeresource/doc | ||
activerecord/doc | ||
activerecord/sqlnet.log | ||
actionpack/doc | ||
actionmailer/doc | ||
activesupport/doc | ||
activesupport/test/tmp | ||
activemodel/test/fixtures/fixture_database.sqlite3 | ||
actionpack/test/tmp | ||
activesupport/test/fixtures/isolation_test | ||
dist | ||
railties/test/500.html | ||
railties/test/fixtures/tmp | ||
railties/test/initializer/root/log | ||
railties/doc | ||
railties/guides/output | ||
railties/tmp | ||
.rvmrc | ||
.rbenv-version | ||
RDOC_MAIN.rdoc | ||
/.bundle | ||
/.rbenv-version | ||
/.rvmrc | ||
/Gemfile.lock | ||
/pkg | ||
/dist | ||
/doc/rdoc | ||
/*/doc | ||
/*/test/tmp | ||
/activerecord/sqlnet.log | ||
/activemodel/test/fixtures/fixture_database.sqlite3 | ||
/activesupport/test/fixtures/isolation_test | ||
/railties/test/500.html | ||
/railties/test/fixtures/tmp | ||
/railties/test/initializer/root/log | ||
/railties/doc | ||
/railties/guides/output | ||
/railties/tmp | ||
/RDOC_MAIN.rdoc |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'securerandom' | ||
require 'active_support/core_ext/string/access' | ||
require 'active_support/core_ext/object/blank' | ||
|
||
module ActionDispatch | ||
# Makes a unique request id available to the action_dispatch.request_id env variable (which is then accessible through | ||
# ActionDispatch::Request#uuid) and sends the same id to the client via the X-Request-Id header. | ||
# | ||
# The unique request id is either based off the X-Request-Id header in the request, which would typically be generated | ||
# by a firewall, load balancer, or the web server, or, if this header is not available, a random uuid. If the | ||
# header is accepted from the outside world, we sanitize it to a max of 255 chars and alphanumeric and dashes only. | ||
# | ||
# The unique request id can be used to trace a request end-to-end and would typically end up being part of log files | ||
# from multiple pieces of the stack. | ||
class RequestId | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id | ||
status, headers, body = @app.call(env) | ||
|
||
headers["X-Request-Id"] = env["action_dispatch.request_id"] | ||
[ status, headers, body ] | ||
end | ||
|
||
private | ||
def external_request_id(env) | ||
if request_id = env["HTTP_X_REQUEST_ID"].presence | ||
request_id.gsub(/[^\w\-]/, "").first(255) | ||
end | ||
end | ||
|
||
def internal_request_id | ||
SecureRandom.hex(16) | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
actionpack/lib/action_dispatch/middleware/session/cache_store.rb
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'action_dispatch/middleware/session/abstract_store' | ||
require 'rack/session/memcache' | ||
|
||
module ActionDispatch | ||
module Session | ||
# Session store that uses an ActiveSupport::Cache::Store to store the sessions. This store is most useful | ||
# if you don't store critical data in your sessions and you don't need them to live for extended periods | ||
# of time. | ||
class CacheStore < AbstractStore | ||
# Create a new store. The cache to use can be passed in the <tt>:cache</tt> option. If it is | ||
# not specified, <tt>Rails.cache</tt> will be used. | ||
def initialize(app, options = {}) | ||
@cache = options[:cache] || Rails.cache | ||
options[:expire_after] ||= @cache.options[:expires_in] | ||
super | ||
end | ||
|
||
# Get a session from the cache. | ||
def get_session(env, sid) | ||
sid ||= generate_sid | ||
session = @cache.read(cache_key(sid)) | ||
session ||= {} | ||
[sid, session] | ||
end | ||
|
||
# Set a session in the cache. | ||
def set_session(env, sid, session, options) | ||
key = cache_key(sid) | ||
if session | ||
@cache.write(key, session, :expires_in => options[:expire_after]) | ||
else | ||
@cache.delete(key) | ||
end | ||
sid | ||
end | ||
|
||
# Remove a session from the cache. | ||
def destroy_session(env, sid, options) | ||
@cache.delete(cache_key(sid)) | ||
generate_sid | ||
end | ||
|
||
private | ||
# Turn the session id into a cache key. | ||
def cache_key(sid) | ||
"_session_id:#{sid}" | ||
end | ||
end | ||
end | ||
end |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'abstract_unit' | ||
|
||
class RequestIdTest < ActiveSupport::TestCase | ||
test "passing on the request id from the outside" do | ||
assert_equal "external-uu-rid", stub_request('HTTP_X_REQUEST_ID' => 'external-uu-rid').uuid | ||
end | ||
|
||
test "ensure that only alphanumeric uurids are accepted" do | ||
assert_equal "X-Hacked-HeaderStuff", stub_request('HTTP_X_REQUEST_ID' => '; X-Hacked-Header: Stuff').uuid | ||
end | ||
|
||
test "ensure that 255 char limit on the request id is being enforced" do | ||
assert_equal "X" * 255, stub_request('HTTP_X_REQUEST_ID' => 'X' * 500).uuid | ||
end | ||
|
||
test "generating a request id when none is supplied" do | ||
assert_match(/\w+/, stub_request.uuid) | ||
end | ||
|
||
private | ||
|
||
def stub_request(env = {}) | ||
ActionDispatch::RequestId.new(lambda { |environment| [ 200, environment, [] ] }).call(env) | ||
ActionDispatch::Request.new(env) | ||
end | ||
end | ||
|
||
class RequestIdResponseTest < ActionDispatch::IntegrationTest | ||
class TestController < ActionController::Base | ||
def index | ||
head :ok | ||
end | ||
end | ||
|
||
test "request id is passed all the way to the response" do | ||
with_test_route_set do | ||
get '/' | ||
assert_match(/\w+/, @response.headers["X-Request-Id"]) | ||
end | ||
end | ||
|
||
test "request id given on request is passed all the way to the response" do | ||
with_test_route_set do | ||
get '/', {}, 'HTTP_X_REQUEST_ID' => 'X' * 500 | ||
assert_equal "X" * 255, @response.headers["X-Request-Id"] | ||
end | ||
end | ||
|
||
|
||
private | ||
|
||
def with_test_route_set | ||
with_routing do |set| | ||
set.draw do | ||
match '/', :to => ::RequestIdResponseTest::TestController.action(:index) | ||
end | ||
|
||
@app = self.class.build_app(set) do |middleware| | ||
middleware.use ActionDispatch::RequestId | ||
end | ||
|
||
yield | ||
end | ||
end | ||
end |
Oops, something went wrong.