Sha256: 0c382e04fe95f35db257038e9ee6891b6bde07160583f83b4fe056aea4c99071

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'webrick'
require 'webrick/httpstatus'

module Intranet
  class Core
    # @!visibility protected
    # WEBrick servlet for the Intranet.
    class Servlet < WEBrick::HTTPServlet::AbstractServlet
      # Initializes a new Servlet instance.
      # @param server [WEBrick::HTTPServer] The web server.
      # @param builder [Intranet::Core::Builder] The web pages builder.
      def initialize(server, builder)
        super(server)
        @builder = builder
      end

      # Processes a GET request issued to the web server.
      # @param request [WEBrick::HTTPRequest] The GET request.
      # @param response [WEBrick::HTTPResponse] Fields +status+, +content_type+ and +body+ are
      #                                         filled with the server response.
      def do_GET(request, response) # rubocop:disable Naming/MethodName
        # See https://github.com/nahi/webrick/blob/master/lib/webrick/httprequest.rb
        # We could use request.request_uri (which is a URI object) but its path is not normalized
        # (it may contain '../' or event start with '..'). Hence we use request.past which has been
        # normalized with HTTPUtils::normalize_path, and request.query for the URL parameters.
        path = request.path
        path += 'index.html' if path[-1] == '/' # path is normalized (ie. does not contain '../')

        status, content_type, body = @builder.do_get(path, request.query)

        raise WEBrick::HTTPStatus[status] if WEBrick::HTTPStatus.error?(status)

        response.status = status
        response.content_type = content_type
        response.body = body
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
intranet-core-1.2.0 lib/intranet/core/servlet.rb
intranet-core-1.1.1 lib/intranet/core/servlet.rb
intranet-core-1.0.2 lib/intranet/core/servlet.rb
intranet-core-1.0.1 lib/intranet/core/servlet.rb
intranet-core-1.0.0 lib/intranet/core/servlet.rb