Skip to content

Instantly share code, notes, and snippets.

@znz
Created January 12, 2014 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save znz/8385630 to your computer and use it in GitHub Desktop.
Save znz/8385630 to your computer and use it in GitHub Desktop.
cross origin example (see http://qa.atmarkit.co.jp/q/3432 )
#!/usr/bin/env rackup
# -*- coding: utf-8 -*-
app = lambda do |env|
req = Rack::Request.new(env)
res = Rack::Response.new
case req.host
when /iframe/
res.set_cookie 'key', 'value'
if req.cookies["key"] == "value"
res.write 'OK'
elsif /set/ !~ req.path_info
res.redirect("#{req.url}/set")
else
res.write 'NG'
end
when /main/
res.write <<-"HTML"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>cross origin example</title>
</head>
<body>
<h1>cross origin example</h1>
<iframe src="http://iframe.127.0.0.1.xip.io:#{req.port}/iframe"></iframe>
</body>
</html>
HTML
else
res.redirect("#{req.scheme}://main.127.0.0.1.xip.io:#{req.port}/main")
end
res.finish
end
run app
source "https://rubygems.org"
gem "rack"
GEM
remote: https://rubygems.org/
specs:
rack (1.5.2)
PLATFORMS
ruby
DEPENDENCIES
rack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment