gem install 'rack-protection'
ruby server.rb
There's a page with 2 forms, the one without CSRF token field will get 403 Forbidden response.
module AutoLoader | |
def self.included(mod) | |
caller_path, = caller(1..1).first.partition(':') | |
pattern = "#{File.dirname(caller_path)}/#{File.basename(caller_path, '.rb')}/*.rb" | |
Dir.glob(pattern).each do |path| | |
class_name = ::Utils.classify(File.basename(path, '.rb')).to_sym | |
mod.autoload class_name, path | |
end | |
end | |
end |
require 'benchmark' | |
n = 100000000 | |
Benchmark.bmbm do |x| | |
x.report('[4, 5].max') { n.times { [4, 5].max } } | |
a, b = 4, 5 | |
x.report('[a, b].max') { n.times { [a, b].max } } | |
end |
#!/bin/sh | |
# $ dasherize Hello, world! I am tonytonyjan. | |
printf "$*" \ | |
| tr '[:upper:]' '[:lower:]' \ | |
| tr -C '[:alnum:]' ' ' \ | |
| tr -s ' ' '-' \ | |
| sed -e 's/^-*//' -e 's/-*$//' \ | |
| tr -d '\n' |
# conversation: https://github.com/rack/rack/commit/734a00c5f4bb46e9a5e6e2677d89a2f285dcc185 | |
# | |
# user system total real | |
# reverse: 0.010000 0.010000 0.020000 ( 0.024657) | |
# join: 0.050000 0.010000 0.060000 ( 0.056571) | |
# regexp: 0.050000 0.000000 0.050000 ( 0.048339) | |
require 'benchmark' | |
def join(session_data) |
class Interactor | |
attr_reader :error | |
def self.perform(*args) | |
new(*args).tap { |interactor| catch(:fail) { interactor.perform } } | |
end | |
def success? | |
@error.nil? | |
end |
See demo
require 'socket' | |
class Client | |
def initialize server, socket | |
@server, @socket = server, socket | |
start | |
end | |
def send message | |
@socket.puts message |
require 'cgi' | |
require 'json' | |
require 'active_support' | |
def verify_and_decrypt_session_cookie(cookie, secret_key_base) | |
cookie = CGI::unescape(cookie) | |
salt = 'encrypted cookie' | |
signed_salt = 'signed encrypted cookie' | |
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000) | |
secret = key_generator.generate_key(salt) |
n = ARGV.first.to_i | |
fact = Hash.new(0) | |
while match_data = /^(.{2,}?)\1+$/.match('.'*n) | |
fact[match_data[1].length] += 1 | |
n /= match_data[1].length | |
end | |
fact[n] += 1 | |
puts fact.sort.map!{|k,v| "#{k}#{"**#{v}" if v > 1}"}.join(' * ') |