-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_server.rb
More file actions
109 lines (90 loc) · 3.21 KB
/
app_server.rb
File metadata and controls
109 lines (90 loc) · 3.21 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'rubygems'
require 'sinatra/base'
require 'open-uri'
require 'net/http'
require 'json'
require 'webrick'
require 'webrick/https'
require 'openssl'
#set :bind, '0.0.0.0'
#set :port, 5679
webrick_options = {
:Port => 5678,
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLCertificate => OpenSSL::X509::Certificate.new( File.open(File.join(File.dirname(File.expand_path(__FILE__)), "../keys2/new-servercert.cer")).read),
:SSLPrivateKey => OpenSSL::PKey::RSA.new( File.open(File.join(File.dirname(File.expand_path(__FILE__)), "../keys2/PRIVKEY2.PEM")).read),
:SSLCertName => [[ "CN", WEBrick::Utils::getservername ]]
}
# April Fool's joke from IETF, 1998
WEBrick::HTTPStatus::StatusMessage[418] = "I'm a teapot"
# Missing from WEBrick for some reason
WEBrick::HTTPStatus::StatusMessage[506] = "Variant Also Negotiates"
WEBrick::HTTPStatus::StatusMessage[508] = "Loop Detected"
WEBrick::HTTPStatus::StatusMessage[509] = "Bandwith Limit Exceeded"
WEBrick::HTTPStatus::StatusMessage[510] = "Not Extended"
class AppServer < Sinatra::Base
get '/hello_world' do
open(File.join(File.dirname(File.expand_path(__FILE__)), 'hello world.html')).read
end
get '/hello_world_redirect' do
redirect '/hello_world'
end
get '/gallery' do
open(File.join(File.dirname(File.expand_path(__FILE__)), 'gallery.html')).read
end
get '/login_form' do
open(File.join(File.dirname(File.expand_path(__FILE__)), 'login.html')).read
end
post '/process_login' do
"username was '#{params['username']}', password was '#{params['password']}'"
end
get '/images/*' do
image = params[:splat] ? "#{params[:splat].first}" : nil
send_file (File.join(File.dirname(File.expand_path(__FILE__)), "images/#{image}"))
end
get '/verb' do
response.headers["Received-Method"] = "GET"
"{verb: \"GET\"}"
end
post '/verb' do
response.headers["Received-Method"] = "POST"
body = request.body.read.to_s
"{verb: \"POST\", body: \"#{body}\"}"
end
put '/verb' do
response.headers["Received-Method"] = "PUT"
body = request.body.read.to_s
"{verb: \"PUT\", body: \"#{body}\"}"
end
delete '/verb' do
response.headers["Received-Method"] = "DELETE"
"{verb: \"DELETE\"}"
end
options '/verb' do
response.headers["Received-Method"] = "OPTIONS"
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "HEAD, GET, PUT, POST, DELETE, OPTIONS"
"{verb: \"OPTIONS\", options: [\"HEAD\", \"GET\", \"PUT\", \"POST\", \"DELETE\", \"OPTIONS\"]}"
end
get '/etags' do
headers = env.to_hash
headers['ETag'] = 'a7c876b7e686897696'
response = JSON.generate({:headers=> headers})
puts response
response
end
get '/' do
"document at root"
end
get '/status/*' do
code = params[:splat] ? "#{params[:splat].first}".to_i : 200
puts code
status code
end
end
server = ::Rack::Handler::WEBrick
trap(:INT) do
server.shutdown
end
server.run(AppServer, webrick_options)