Ver Fonte

Decode "~" in url path

Thibaut Courouble há 9 anos atrás
pai
commit
d0802988f1
2 ficheiros alterados com 10 adições e 1 exclusões
  1. 2 1
      lib/app.rb
  2. 8 0
      test/app_test.rb

+ 2 - 1
lib/app.rb

@@ -287,7 +287,8 @@ class App < Sinatra::Application
     'angular~1.2' => 'angularjs~1.2'
   }
 
-  get %r{\A/([\w~\.]+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest|
+  get %r{\A/([\w~\.%]+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest|
+    doc.sub! '%7E', '~'
     return redirect "/#{DOC_REDIRECTS[doc]}#{type}#{rest}" if DOC_REDIRECTS.key?(doc)
     return redirect "/angularjs/api#{rest}", 301 if doc == 'angular' && rest.start_with?('/ng')
     return 404 unless @doc = find_doc(doc)

+ 8 - 0
test/app_test.rb

@@ -164,6 +164,14 @@ class AppTest < MiniTest::Spec
       assert last_response.not_found?
     end
 
+    it "decodes '~' properly" do
+      get '/html%7E5/'
+      assert last_response.ok?
+
+      get '/html%7E42/'
+      assert last_response.not_found?
+    end
+
     it "redirects with trailing slash" do
       get '/html'
       assert last_response.redirect?