1#
2# https.rb -- SSL/TLS enhancement for HTTPServer
3#
4# Author: IPR -- Internet Programming with Ruby -- writers
5# Copyright (c) 2001 GOTOU Yuuzou
6# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
7# reserved.
8#
9# $IPR: https.rb,v 1.15 2003/07/22 19:20:42 gotoyuzo Exp $
10
11require 'webrick/ssl'
12
13module WEBrick
14  module Config
15    HTTP.update(SSL)
16  end
17
18  ##
19  #--
20  # Adds SSL functionality to WEBrick::HTTPRequest
21
22  class HTTPRequest
23
24    ##
25    # HTTP request SSL cipher
26
27    attr_reader :cipher
28
29    ##
30    # HTTP request server certificate
31
32    attr_reader :server_cert
33
34    ##
35    # HTTP request client certificate
36
37    attr_reader :client_cert
38
39    # :stopdoc:
40
41    alias orig_parse parse
42
43    def parse(socket=nil)
44      if socket.respond_to?(:cert)
45        @server_cert = socket.cert || @config[:SSLCertificate]
46        @client_cert = socket.peer_cert
47        @client_cert_chain = socket.peer_cert_chain
48        @cipher      = socket.cipher
49      end
50      orig_parse(socket)
51    end
52
53    alias orig_parse_uri parse_uri
54
55    def parse_uri(str, scheme="https")
56      if server_cert
57        return orig_parse_uri(str, scheme)
58      end
59      return orig_parse_uri(str)
60    end
61    private :parse_uri
62
63    alias orig_meta_vars meta_vars
64
65    def meta_vars
66      meta = orig_meta_vars
67      if server_cert
68        meta["HTTPS"] = "on"
69        meta["SSL_SERVER_CERT"] = @server_cert.to_pem
70        meta["SSL_CLIENT_CERT"] = @client_cert ? @client_cert.to_pem : ""
71        if @client_cert_chain
72          @client_cert_chain.each_with_index{|cert, i|
73            meta["SSL_CLIENT_CERT_CHAIN_#{i}"] = cert.to_pem
74          }
75        end
76        meta["SSL_CIPHER"] = @cipher[0]
77        meta["SSL_PROTOCOL"] = @cipher[1]
78        meta["SSL_CIPHER_USEKEYSIZE"] = @cipher[2].to_s
79        meta["SSL_CIPHER_ALGKEYSIZE"] = @cipher[3].to_s
80      end
81      meta
82    end
83
84    # :startdoc:
85  end
86end
87