# WEBrick::HTTPServer (Class)

In: | webrick/httpserver.rb
Parent: | ::WEBrick::GenericServer

## Methods

access_log do_OPTIONS mount mount_proc new run search_servlet service umount unmount

## Classes and Modules

Class WEBrick::HTTPServer::MountTable

## Public Class methods

new(config={}, default=Config::HTTP)

[Source]

    # File webrick/httpserver.rb, line 23
        def initialize(config={}, default=Config::HTTP)
          super
          @http_version = HTTPVersion::convert(@config[:HTTPVersion])

          @mount_tab = MountTable.new
          if @config[:DocumentRoot]
            mount("/", HTTPServlet::FileHandler, @config[:DocumentRoot],
                  @config[:DocumentRootOptions])
          end

          unless @config[:AccessLog]
            @config[:AccessLog] = [
              [ $stderr, AccessLog::COMMON_LOG_FORMAT ],
              [ $stderr, AccessLog::REFERER_LOG_FORMAT ]
            ]
          end
        end

## Public Instance methods

run(sock)

[Source]

    # File webrick/httpserver.rb, line 41
        def run(sock)
          while true
            res = HTTPResponse.new(@config)
            req = HTTPRequest.new(@config)
            begin
              req.parse(sock)
              res.request_method = req.request_method
              res.request_uri = req.request_uri
              res.request_http_version = req.http_version
              res.keep_alive = req.keep_alive?
              if handler = @config[:RequestHandler]
                handler.call(req, res)
              end
              service(req, res)
            rescue HTTPStatus::EOFError, HTTPStatus::RequestTimeout => ex
              res.set_error(ex)
            rescue HTTPStatus::Error => ex
              res.set_error(ex)
            rescue HTTPStatus::Status => ex
              res.status = ex.code
            rescue StandardError => ex
              @logger.error(ex)
              res.set_error(ex, true)
            ensure
              if req.request_line
                req.fixup()
                res.send_response(sock)
                access_log(@config, req, res)
              end
            end
            break if @http_version < "1.1"
            break unless req.keep_alive?
            break unless res.keep_alive?
          end
        end

service(req, res)

[Source]

    # File webrick/httpserver.rb, line 77
        def service(req, res)
          if req.unparsed_uri == "*"
            if req.request_method == "OPTIONS"
              do_OPTIONS(req, res)
              raise HTTPStatus::OK
            end
            raise HTTPStatus::NotFound, "`#{req.unparsed_uri}' not found."
          end

          servlet, options, script_name, path_info = search_servlet(req.path)
          raise HTTPStatus::NotFound, "`#{req.path}' not found." unless servlet
          req.script_name = script_name
          req.path_info = path_info
          si = servlet.get_instance(self, *options)
          @logger.debug(format("%s is invoked.", si.class.name))
          si.service(req, res)
        end

do_OPTIONS(req, res)

[Source]

    # File webrick/httpserver.rb, line 95
        def do_OPTIONS(req, res)
          res["allow"] = "GET,HEAD,POST,OPTIONS"
        end

mount(dir, servlet, *options)

[Source]

    # File webrick/httpserver.rb, line 99
        def mount(dir, servlet, *options)
          @logger.debug(sprintf("%s is mounted on %s.", servlet.inspect, dir))
          @mount_tab[dir] = [ servlet, options ]
        end

mount_proc(dir, proc=nil, &block)

[Source]

    # File webrick/httpserver.rb, line 104
        def mount_proc(dir, proc=nil, &block)
          proc ||= block
          raise HTTPServerError, "must pass a proc or block" unless proc
          mount(dir, HTTPServlet::ProcHandler.new(proc))
        end

unmount(dir)

[Source]

    # File webrick/httpserver.rb, line 110
        def unmount(dir)
          @logger.debug(sprintf("unmount %s.", inspect, dir))
          @mount_tab.delete(dir)
        end

umount(dir)

Alias for unmount

search_servlet(path)

[Source]

    # File webrick/httpserver.rb, line 116
        def search_servlet(path)
          script_name, path_info = @mount_tab.scan(path)
          servlet, options = @mount_tab[script_name]
          if servlet
            [ servlet, options, script_name, path_info ]
          end
        end

access_log(config, req, res)

[Source]

    # File webrick/httpserver.rb, line 124
        def access_log(config, req, res)
          param = AccessLog::setup_params(config, req, res)
          @config[:AccessLog].each{|logger, fmt|
            logger << AccessLog::format(fmt+"\n", param)
          }
        end

[Validate]