123456789_123456789_123456789_123456789_123456789_

Class: WEBrick::CGI::Socket

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: lib/webrick/cgi.rb

Overview

Provides HTTP socket emulation from the ::WEBrick::CGI environment

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(config, env, stdin, stdout) ⇒ Socket (private)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 175

def initialize(config, env, stdin, stdout)
  @config = config
  @env = env
  @header_part = StringIO.new
  @body_part = stdin
  @out_port = stdout
  @out_port.binmode

  @server_addr = @env["SERVER_ADDR"] || "0.0.0.0"
  @server_name = @env["SERVER_NAME"]
  @server_port = @env["SERVER_PORT"]
  @remote_addr = @env["REMOTE_ADDR"]
  @remote_host = @env["REMOTE_HOST"] || @remote_addr
  @remote_port = @env["REMOTE_PORT"] || 0

  begin
    @header_part << request_line << CRLF
    setup_header
    @header_part << CRLF
    @header_part.rewind
  rescue Exception
    raise CGIError, "invalid CGI environment"
  end
end

Instance Attribute Details

#eof?Boolean (readonly)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 260

def eof?
  input.eof?
end

Instance Method Details

#<<(data)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 264

def <<(data)
  @out_port << data
end

#add_header(hdrname, value) (private)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 228

def add_header(hdrname, value)
  unless value.empty?
    @header_part << hdrname << ": " << value << CRLF
  end
end

#addr

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 244

def addr
  [nil, @server_port, @server_name, @server_addr]
end

#cert

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 272

def cert
  return nil unless defined?(OpenSSL)
  if pem = @env["SSL_SERVER_CERT"]
    OpenSSL::X509::Certificate.new(pem) unless pem.empty?
  end
end

#cipher

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 301

def cipher
  return nil unless defined?(OpenSSL)
  if cipher = @env["SSL_CIPHER"]
    ret = [ cipher ]
    ret << @env["SSL_PROTOCOL"]
    ret << @env["SSL_CIPHER_USEKEYSIZE"]
    ret << @env["SSL_CIPHER_ALGKEYSIZE"]
    ret
  end
end

#each

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 256

def each
  input.each{|line| yield(line) }
end

#gets(eol = LF, size = nil)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 248

def gets(eol=LF, size=nil)
  input.gets(eol, size)
end

#input (private)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 234

def input
  @header_part.eof? ? @body_part : @header_part
end

#peer_cert

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 279

def peer_cert
  return nil unless defined?(OpenSSL)
  if pem = @env["SSL_CLIENT_CERT"]
    OpenSSL::X509::Certificate.new(pem) unless pem.empty?
  end
end

#peer_cert_chain

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 286

def peer_cert_chain
  return nil unless defined?(OpenSSL)
  if @env["SSL_CLIENT_CERT_CHAIN_0"]
    keys = @env.keys
    certs = keys.sort.collect{|k|
      if /^SSL_CLIENT_CERT_CHAIN_\d+$/ =~ k
        if pem = @env[k]
          OpenSSL::X509::Certificate.new(pem) unless pem.empty?
        end
      end
    }
    certs.compact
  end
end

#peeraddr

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 240

def peeraddr
  [nil, @remote_port, @remote_host, @remote_addr]
end

#read(size = nil)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 252

def read(size=nil)
  input.read(size)
end

#request_line (private)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 200

def request_line
  meth = @env["REQUEST_METHOD"] || "GET"
  unless url = @env["REQUEST_URI"]
    url = (@env["SCRIPT_NAME"] || File.expand_path($0)).dup
    url << @env["PATH_INFO"].to_s
    url = WEBrick::HTTPUtils.escape_path(url)
    if query_string = @env["QUERY_STRING"]
      unless query_string.empty?
        url << "?" << query_string
      end
    end
  end
  # we cannot get real HTTP version of client ;)
  httpv = @config[:HTTPVersion]
  return "#{meth} #{url} HTTP/#{httpv}"
end

#setup_header (private)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 217

def setup_header
  @env.each{|key, value|
    case key
    when "CONTENT_TYPE", "CONTENT_LENGTH"
      add_header(key.gsub(/_/, "-"), value)
    when /^HTTP_(.*)/
      add_header($1.gsub(/_/, "-"), value)
    end
  }
end

#write(data)

[ GitHub ]

  
# File 'lib/webrick/cgi.rb', line 268

def write(data)
  @out_port.write(data)
end