Skip to content

Commit 6817b3a

Browse files
committed
CORS support
1 parent 33c9e6a commit 6817b3a

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

regexplanet.rb

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
configure do
77
mime_type :ico, 'image/x-icon'
88
set :static_cache_control, [:public, {:max_age => 604800 } ]
9+
#set :protection, :except => [:frame_options ]
10+
#disable :protection
911
end
1012

1113
error do
@@ -16,7 +18,13 @@
1618
} )
1719
end
1820

19-
get '/status.json' do
21+
22+
def get_or_post(path, opts={}, &block)
23+
get(path, opts, &block)
24+
post(path, opts, &block)
25+
end
26+
27+
get_or_post '/status.json' do
2028
jsonp( {
2129
:success => true,
2230
:message => "OK",
@@ -30,11 +38,39 @@
3038
} )
3139
end
3240

33-
get '/test.json' do
41+
options '/test.json' do
42+
content_type :text
43+
headers \
44+
'Access-Control-Allow-Origin' => '*',
45+
'Access-Control-Allow-Methods' => 'POST, GET',
46+
'Access-Control-Max-Age' => '604800'
47+
body 'Yes, CORS is allowed!'
48+
end
49+
50+
get_or_post '/test.json' do
51+
content_type :text
52+
headers \
53+
'Access-Control-Allow-Origin' => '*',
54+
'Access-Control-Allow-Methods' => 'POST, GET',
55+
'Access-Control-Max-Age' => '604800'
56+
3457
output = ""
3558

3659
str_regex = params[:regex]
3760

61+
if str_regex.nil? || str_regex.length() == 0
62+
return jsonp(
63+
:success => false,
64+
:message => "No regular expression to test"
65+
)
66+
end
67+
68+
if request.request_method == "get"
69+
form_data = request.query_string
70+
else
71+
form_data = request.env["rack.input"].read
72+
end
73+
3874
output << "<table class=\"table table-bordered table-striped\" style=\"width:auto;\">\n"
3975

4076
output << "\t<tr>\n"
@@ -51,7 +87,7 @@
5187
output << "</td>\n"
5288

5389
options = 0
54-
str_options = request_params_multi(request.query_string)["option"]
90+
str_options = request_params_multi(form_data)[:option]
5591
if str_options
5692
if str_options.include?("comment")
5793
options += Regexp::EXTENDED
@@ -110,6 +146,7 @@
110146
output << "\t</tr>\n"
111147
end
112148

149+
# when ruby1.9
113150
#names = regex.names
114151
#if names
115152
# output << "\t<tr>\n"
@@ -132,9 +169,7 @@
132169
output << "\t\t<th>match()</th>"
133170
output << "\t</tr>\n"
134171

135-
inputs = request_params_multi(request.query_string)["input"]
136-
137-
print inputs
172+
inputs = request_params_multi(form_data)["input"]
138173

139174
if not inputs
140175
output << "\t<tr>\n"
@@ -201,9 +236,7 @@
201236

202237
output << "</table>\n"
203238

204-
content_type :text
205239
jsonp( {
206-
:debug => inputs,
207240
:success => true,
208241
:message => "OK",
209242
:html => output,

0 commit comments

Comments
 (0)