]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/webservice.rb
web service: fix basic auth handler
[user/henk/code/ruby/rbot.git] / lib / rbot / core / webservice.rb
index 297771a8751035c8e961253969f05c4725df4c70..c823594ad437fecc8473e29fd8b35118b625f984 100644 (file)
@@ -73,8 +73,9 @@ class Bot
             if botuser and botuser.password == password
               @source = botuser
               true
+            else
+              false
             end
-            false
           else
             true # no need to request auth at this point
           end
@@ -102,18 +103,26 @@ class Bot
         true
       end
 
-      # Sends a plaintext response
-      def send_plaintext(body, status=200)
+      # Sends a response with the specified body, status and content type.
+      def send_response(body, status, type)
         @res.status = status
-        @res['Content-Type'] = 'text/plain'
+        @res['Content-Type'] = type
         @res.body = body
       end
 
+      # Sends a plaintext response
+      def send_plaintext(body, status=200)
+        send_response(body, status, 'text/plain')
+      end
+
       # Sends a json response
       def send_json(body, status=200)
-        @res.status = status
-        @res['Content-Type'] = 'application/json'
-        @res.body = body
+        send_response(body, status, 'application/json')
+      end
+
+      # Sends a html response
+      def send_html(body, status=200)
+        send_response(body, status, 'text/html')
       end
     end
 
@@ -343,6 +352,11 @@ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
     begin
       m = WebMessage.new(@bot, req, res)
       @bot.web_dispatcher.handle m
+    rescue WEBrick::HTTPStatus::Unauthorized
+      res.status = 401
+      res['Content-Type'] = 'text/plain'
+      res.body = 'Authentication Required!'
+      error 'authentication error (wrong password)'
     rescue
       res.status = 500
       res['Content-Type'] = 'text/plain'