All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ly.grizzly-jruby.1.7.19.source-code.dispatch.rb Maven / Gradle / Ivy

#
# The contents of this file are subject to the terms
# of the Common Development and Distribution License
# (the License).  You may not use this file except in
# compliance with the License.
#
# You can obtain a copy of the license at
# https://glassfish.dev.java.net/public/CDDLv1.0.html or
# glassfish/bootstrap/legal/CDDLv1.0.txt.
# See the License for the specific language governing
# permissions and limitations under the License.
#
# When distributing Covered Code, include this CDDL
# Header Notice in each file and include the License file
# at glassfish/bootstrap/legal/CDDLv1.0.txt.
# If applicable, add the following below the CDDL Header,
# with the fields enclosed by brackets [] replaced by
# you own identifying information:
# "Portions Copyrighted [year] [name of copyright owner]"
#
# Copyright 2006 Sun Microsystems, Inc. All rights reserved.
#
class Grizzlet
  def service(req)
    setup_logger
    info        = req.getRequestProcessor()
    request_uri = req.requestURI.to_s
    headers     = req.getMimeHeaders()

    # RFC3875 The Common Gateway Interface (CGI) Version 1.1
    #ENV['AUTH_TYPE']      = req.getAuthType().to_s
    ENV['CONTENT_LENGTH']    = info.getContentLength().to_s
    ENV['CONTENT_TYPE']      = req.getContentType()

    ENV['GATEWAY_INTERFACE'] = 'CGI/1.1'
    ENV['PATH_INFO']         = request_uri
    ENV['PATH_TRANSLATED']   = request_uri.split('?', 2).first
    ENV['QUERY_STRING']      = req.queryString().to_s
    ENV['REMOTE_ADDR']       = info.getRemoteAddr()
    ENV['REMOTE_HOST']       = req.remoteHost().to_s
    ENV['REMOTE_USER']       = req.getRemoteUser().to_s

    ENV['REQUEST_METHOD']    = info.getMethod()
    ENV['SCRIPT_NAME']       = ''

    ENV['SERVER_NAME']       = req.serverName().to_s
    ENV['SERVER_PORT']       = req.getServerPort().to_s
    ENV['SERVER_PROTOCOL']   = req.protocol().to_s
    ENV['SERVER_SOFTWARE']   = 'Grizzly/1.7'
    ENV['REQUEST_URI']       = request_uri

    for i in 0 ... headers.size
      name = headers.getName(i).to_s
      value = headers.getValue(i).to_s
      ENV['HTTP_' + name.upcase.tr('-','_')] = value
    end

    if !$root.nil?
      ActionController::AbstractRequest.relative_url_root = $root
      ActionController::CgiRequest.relative_url_root = $root
    end 

    Dispatcher.dispatch
  end

  def setup_logger
    if defined?(::RAILS_DEFAULT_LOGGER)
      class << ::RAILS_DEFAULT_LOGGER # Make these accessible to wire in the log device
        public :instance_variable_get, :instance_variable_set
      end

      # use config.logger?
      if defined?(ActiveSupport::BufferedLogger) # Rails 2.x
        old_device = ::RAILS_DEFAULT_LOGGER.instance_variable_get "@log"
        old_device.close rescue nil
        ::RAILS_DEFAULT_LOGGER.instance_variable_set "@log", logdev
      else # Rails 1.x
        old_device = ::RAILS_DEFAULT_LOGGER.instance_variable_get "@logdev"
        old_device.close rescue nil
        ::RAILS_DEFAULT_LOGGER.instance_variable_set "@logdev", Logger::LogDevice.new(logdev)
      end
    end
  end

  class GrizzlyLog
      def initialize(context = $logger)
        @context = context
      end
      def puts(msg)
        write msg.to_s
      end
      def write(msg)
        @context.log(msg)
      end
      def flush; end
      def close; end
  end

  def logdev
    @logdev ||= GrizzlyLog.new $logger
  end

  def logger
    @logger ||= begin; require 'logger'; Logger.new(logdev); end
  end

end


Grizzlet.new




© 2015 - 2025 Weber Informatics LLC | Privacy Policy