-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-echo.rb
54 lines (46 loc) · 1.55 KB
/
http-echo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby
require 'sinatra'
require 'yaml'
set :bind, '0.0.0.0'
set :port, ENV['HTTP_PORT']
ADDITION_RESPONSE = ENV['ADDITION_RESPONSE']
HTTP_ECHO_STATUS = 'HTTP_X_ECHO_STATUS'
def self.route(*methods, path, &block)
methods.each do |method|
method.to_sym
self.send method, path, &block
end
end
route :get, :post, :delete, :patch, :put, :head, :options, '/*' do
status = 200
content_type 'text/plain'
if request.env['QUERY_STRING'] == ''
request_path = "#{request.env['REQUEST_PATH']}"
else
request_path = "#{request.env['REQUEST_PATH']}?#{request.env['QUERY_STRING']}"
end
request_method = request.env['REQUEST_METHOD']
http_version = request.env['HTTP_VERSION']
headers = Hash.new
header_keys = request.env.keys.select {|k| k[/^HTTP_.*$/] }.reject {|k| k == 'HTTP_VERSION'}
header_keys.each do |key|
tokens = key.split("_")[1..-1].map {|t| t.capitalize }
headers[key] = tokens.join("-")
status = request.env[key].to_i if key == HTTP_ECHO_STATUS and request.env[key].to_i > status
end
header_keys = ['CONTENT_TYPE', 'CONTENT_LENGTH']
header_keys.each do |key|
if request.env.has_key? key
tokens = key.split("_").map {|t| t.capitalize }
headers[key] = tokens.join("-")
end
end
return_value = Array.new
return_value.push "#{request_method} #{request_path} #{http_version}"
headers.each do |key, header|
return_value.push "#{header}: #{request.env[key]}"
end
return_value.push ADDITION_RESPONSE if ADDITION_RESPONSE
return_value.push ''
[status, return_value.join("\n")]
end