01_lib_cgi_rename_header_to_http_header.patch

rename CGI#header to CGI#http_header - Marcus Stollsteimer, 10/05/2012 10:22 PM

Download (10.2 KB)

View differences:

lib/cgi.rb (Arbeitskopie)
122 122
#
123 123
# The simplest way to send output to the HTTP client is using the #out() method.
124 124
# This takes the HTTP headers as a hash parameter, and the body content
125
# via a block.  The headers can be generated as a string using the #header()
125
# via a block.  The headers can be generated as a string using the #http_header()
126 126
# method.  The output stream can be written directly to using the #print()
127 127
# method.
128 128
#
......
262 262
#   CGI.new("html4")    # html4.01 (Strict)
263 263
#   CGI.new("html4Tr")  # html4.01 Transitional
264 264
#   CGI.new("html4Fr")  # html4.01 Frameset
265
#   CGI.new("html5")    # html5
265 266
#
266 267

  
267 268
class CGI
lib/cgi/core.rb (Arbeitskopie)
67 67
  # Create an HTTP header block as a string.
68 68
  #
69 69
  # :call-seq:
70
  #   header(content_type_string="text/html")
71
  #   header(headers_hash)
70
  #   http_header(content_type_string="text/html")
71
  #   http_header(headers_hash)
72 72
  #
73 73
  # Includes the empty line that ends the header block.
74 74
  #
......
127 127
  #
128 128
  # Examples:
129 129
  #
130
  #   header
130
  #   http_header
131 131
  #     # Content-Type: text/html
132 132
  #
133
  #   header("text/plain")
133
  #   http_header("text/plain")
134 134
  #     # Content-Type: text/plain
135 135
  #
136
  #   header("nph"        => true,
137
  #          "status"     => "OK",  # == "200 OK"
138
  #            # "status"     => "200 GOOD",
139
  #          "server"     => ENV['SERVER_SOFTWARE'],
140
  #          "connection" => "close",
141
  #          "type"       => "text/html",
142
  #          "charset"    => "iso-2022-jp",
143
  #            # Content-Type: text/html; charset=iso-2022-jp
144
  #          "length"     => 103,
145
  #          "language"   => "ja",
146
  #          "expires"    => Time.now + 30,
147
  #          "cookie"     => [cookie1, cookie2],
148
  #          "my_header1" => "my_value"
149
  #          "my_header2" => "my_value")
136
  #   http_header("nph"        => true,
137
  #               "status"     => "OK",  # == "200 OK"
138
  #                 # "status"     => "200 GOOD",
139
  #               "server"     => ENV['SERVER_SOFTWARE'],
140
  #               "connection" => "close",
141
  #               "type"       => "text/html",
142
  #               "charset"    => "iso-2022-jp",
143
  #                 # Content-Type: text/html; charset=iso-2022-jp
144
  #               "length"     => 103,
145
  #               "language"   => "ja",
146
  #               "expires"    => Time.now + 30,
147
  #               "cookie"     => [cookie1, cookie2],
148
  #               "my_header1" => "my_value"
149
  #               "my_header2" => "my_value")
150 150
  #
151 151
  # This method does not perform charset conversion.
152
  def header(options='text/html')
152
  def http_header(options='text/html')
153 153
    if options.is_a?(String)
154 154
      content_type = options
155 155
      buf = _header_for_string(content_type)
......
170 170
      buf << EOL    # empty line of separator
171 171
      return buf
172 172
    end
173
  end # header()
173
  end # http_header()
174 174

  
175 175
  def _header_for_string(content_type) #:nodoc:
176 176
    buf = ''
......
283 283
  # +content_type_string+::
284 284
  #   If a string is passed, it is assumed to be the content type.
285 285
  # +headers_hash+::
286
  #   This is a Hash of headers, similar to that used by #header.
286
  #   This is a Hash of headers, similar to that used by #http_header.
287 287
  # +block+::
288 288
  #   A block is required and should evaluate to the body of the response.
289 289
  #
......
344 344
    options["length"] = content.bytesize.to_s
345 345
    output = stdoutput
346 346
    output.binmode if defined? output.binmode
347
    output.print header(options)
347
    output.print http_header(options)
348 348
    output.print content unless "HEAD" == env_table['REQUEST_METHOD']
349 349
  end
350 350

  
test/cgi/test_cgi_core.rb (Arbeitskopie)
357 357
    cgi = CGI.new('html5')
358 358
    expected = '<!DOCTYPE HTML>'
359 359
    assert_equal(expected, cgi.doctype)
360
    # cgi.header not broken
361
    expected = "Content-Type: text/html\r\n\r\n"
362
    actual = cgi.header
363
    assert_equal(expected, actual)
364 360
  end
365 361

  
366 362

  
test/cgi/test_cgi_header.rb (Arbeitskopie)
21 21
  end
22 22

  
23 23

  
24
  def test_cgi_header_simple
24
  def test_cgi_http_header_simple
25 25
    cgi = CGI.new
26 26
    ## default content type
27 27
    expected = "Content-Type: text/html\r\n\r\n"
28
    actual = cgi.header
28
    actual = cgi.http_header
29 29
    assert_equal(expected, actual)
30 30
    ## content type specified as string
31 31
    expected = "Content-Type: text/xhtml; charset=utf8\r\n\r\n"
32
    actual = cgi.header('text/xhtml; charset=utf8')
32
    actual = cgi.http_header('text/xhtml; charset=utf8')
33 33
    assert_equal(expected, actual)
34 34
    ## content type specified as hash
35 35
    expected = "Content-Type: image/png\r\n\r\n"
36
    actual = cgi.header('type'=>'image/png')
36
    actual = cgi.http_header('type'=>'image/png')
37 37
    assert_equal(expected, actual)
38 38
    ## charset specified
39 39
    expected = "Content-Type: text/html; charset=utf8\r\n\r\n"
40
    actual = cgi.header('charset'=>'utf8')
40
    actual = cgi.http_header('charset'=>'utf8')
41 41
    assert_equal(expected, actual)
42 42
  end
43 43

  
44 44

  
45
  def test_cgi_header_complex
45
  def test_cgi_http_header_complex
46 46
    cgi = CGI.new
47 47
    options = {
48 48
      'type'       => 'text/xhtml',
......
64 64
    expected << "Expires: Sun, 23 Jan 2000 12:34:56 GMT\r\n"
65 65
    expected << "location: http://www.ruby-lang.org/\r\n"
66 66
    expected << "\r\n"
67
    actual = cgi.header(options)
67
    actual = cgi.http_header(options)
68 68
    assert_equal(expected, actual)
69 69
  end
70 70

  
71 71

  
72
  def test_cgi_header_argerr
72
  def test_cgi_http_header_argerr
73 73
    cgi = CGI.new
74 74
    #expected = NoMethodError  # must be ArgumentError
75 75
    if RUBY_VERSION>="1.9.0"
......
78 78
      expected = NoMethodError   # for Ruby1.8
79 79
    end
80 80
    assert_raise(expected) do
81
      cgi.header(nil)
81
      cgi.http_header(nil)
82 82
    end
83 83
  end
84 84

  
85 85

  
86
  def test_cgi_header_cookie
86
  def test_cgi_http_header_cookie
87 87
    cgi = CGI.new
88 88
    cookie1 = CGI::Cookie.new('name1', 'abc', '123')
89 89
    cookie2 = CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true)
......
92 92
    c1    = "Set-Cookie: name1=abc&123; path=\r\n"
93 93
    c2    = "Set-Cookie: name2=value2; path=; secure\r\n"
94 94
    ## CGI::Cookie object
95
    actual = cgi.header('cookie'=>cookie1)
95
    actual = cgi.http_header('cookie'=>cookie1)
96 96
    expected = ctype + c1 + sep
97 97
    assert_equal(expected, actual)
98 98
    ## String
99
    actual = cgi.header('cookie'=>cookie2.to_s)
99
    actual = cgi.http_header('cookie'=>cookie2.to_s)
100 100
    expected = ctype + c2 + sep
101 101
    assert_equal(expected, actual)
102 102
    ## Array
103
    actual = cgi.header('cookie'=>[cookie1, cookie2])
103
    actual = cgi.http_header('cookie'=>[cookie1, cookie2])
104 104
    expected = ctype + c1 + c2 + sep
105 105
    assert_equal(expected, actual)
106 106
    ## Hash
107
    actual = cgi.header('cookie'=>{'name1'=>cookie1, 'name2'=>cookie2})
107
    actual = cgi.http_header('cookie'=>{'name1'=>cookie1, 'name2'=>cookie2})
108 108
    expected = ctype + c1 + c2 + sep
109 109
    assert_equal(expected, actual)
110 110
  end
111 111

  
112 112

  
113
  def test_cgi_header_output_cookies
113
  def test_cgi_http_header_output_cookies
114 114
    cgi = CGI.new
115 115
    ## output cookies
116 116
    cookies = [ CGI::Cookie.new('name1', 'abc', '123'),
......
122 122
    expected << "Set-Cookie: name2=value2; path=; secure\r\n"
123 123
    expected << "\r\n"
124 124
    ## header when string
125
    actual = cgi.header('text/html; charset=utf8')
125
    actual = cgi.http_header('text/html; charset=utf8')
126 126
    assert_equal(expected, actual)
127 127
    ## _header_for_string
128
    actual = cgi.header('type'=>'text/html', 'charset'=>'utf8')
128
    actual = cgi.http_header('type'=>'text/html', 'charset'=>'utf8')
129 129
    assert_equal(expected, actual)
130 130
  end
131 131

  
132 132

  
133
  def test_cgi_header_nph
133
  def test_cgi_http_header_nph
134 134
    time_start = Time.now.to_i
135 135
    cgi = CGI.new
136 136
    ## 'nph' is true
137 137
    ENV['SERVER_SOFTWARE'] = 'Apache 2.2.0'
138
    actual1 = cgi.header('nph'=>true)
138
    actual1 = cgi.http_header('nph'=>true)
139 139
    ## when old IIS, NPH-mode is forced
140 140
    ENV['SERVER_SOFTWARE'] = 'IIS/4.0'
141
    actual2 = cgi.header
142
    actual3 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
141
    actual2 = cgi.http_header
142
    actual3 = cgi.http_header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
143 143
    ## newer IIS doesn't require NPH-mode   ## [ruby-dev:30537]
144 144
    ENV['SERVER_SOFTWARE'] = 'IIS/5.0'
145
    actual4 = cgi.header
146
    actual5 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
145
    actual4 = cgi.http_header
146
    actual5 = cgi.http_header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
147 147
    time_end = Time.now.to_i
148 148
    date = /^Date: ([A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d GMT)\r\n/
149 149
    [actual1, actual2, actual3].each do |actual|
test/cgi/test_cgi_modruby.rb (Arbeitskopie)
30 30
    cgi = CGI.new
31 31
    assert(req._setup_cgi_env_invoked?)
32 32
    assert(! req._send_http_header_invoked?)
33
    actual = cgi.header
33
    actual = cgi.http_header
34 34
    assert_equal('', actual)
35 35
    assert_equal('text/html', req.content_type)
36 36
    assert(req._send_http_header_invoked?)
......
51 51
    }
52 52
    assert(req._setup_cgi_env_invoked?)
53 53
    assert(! req._send_http_header_invoked?)
54
    actual = cgi.header(options)
54
    actual = cgi.http_header(options)
55 55
    assert_equal('', actual)
56 56
    assert_equal('image/gif', req.content_type)
57 57
    assert_equal('403 Forbidden', req.status_line)
......
71 71
      'status'   => '200 OK',
72 72
      'location' => 'http://www.example.com/',
73 73
    }
74
    cgi.header(options)
74
    cgi.http_header(options)
75 75
    assert_equal('200 OK', req.status_line)  # should be '302 Found' ?
76 76
    assert_equal(302, req.status)
77 77
    assert_equal('http://www.example.com/', req.headers_out['location'])