File: headers_case_sensitivity.rb

package info (click to toggle)
ruby-excon 0.112.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 7,855; makefile: 5
file content (83 lines) | stat: -rw-r--r-- 2,554 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'rubygems'
require 'stringio'
require 'tach'

def all_match_socket
  io = StringIO.new
  io << "Connection: close\n"
  io << "Content-Length: 000\n"
  io << "Content-Type: text/html\n"
  io << "Date: Xxx, 00 Xxx 0000 00:00:00 GMT\n"
  io << "Server: xxx\n"
  io << "Transfer-Encoding: chunked\n"
  io << "\n\n"
  io.rewind
  io
end

Formatador.display_line('all_match')
Formatador.indent do
  Tach.meter(10_000) do
    tach('compare on read') do
      socket, headers = all_match_socket, {}
      until ((data = socket.readline).chop!).empty?
        key, value = data.split(': ')
        headers[key] = value
        (key.casecmp('Transfer-Encoding') == 0) && (value.casecmp('chunked') == 0)
        (key.casecmp('Connection') == 0) && (value.casecmp('close') == 0)
        (key.casecmp('Content-Length') == 0)
      end
    end

    tach('original') do
      socket, headers = all_match_socket, {}
      until ((data = socket.readline).chop!).empty?
        key, value = data.split(': ')
        headers[key] = value
      end
      headers.has_key?('Transfer-Encoding') && headers['Transfer-Encoding'].casecmp('chunked') == 0
      headers.has_key?('Connection') && headers['Connection'].casecmp('close') == 0
      headers.has_key?('Content-Length')
    end
  end
end

def none_match_socket
  io = StringIO.new
  io << "Cache-Control: max-age=0\n"
  io << "Content-Type: text/html\n"
  io << "Date: Xxx, 00 Xxx 0000 00:00:00 GMT\n"
  io << "Expires: Xxx, 00 Xxx 0000 00:00:00 GMT\n"
  io << "Last-Modified: Xxx, 00 Xxx 0000 00:00:00 GMT\n"
  io << "Server: xxx\n"
  io << "\n\n"
  io.rewind
  io
end

Formatador.display_line('none_match')
Formatador.indent do
  Tach.meter(10_000) do
    tach('compare on read') do
      socket, headers = none_match_socket, {}
      until ((data = socket.readline).chop!).empty?
        key, value = data.split(': ')
        headers[key] = value
        (key.casecmp('Transfer-Encoding') == 0) && (value.casecmp('chunked') == 0)
        (key.casecmp('Connection') == 0) && (value.casecmp('close') == 0)
        (key.casecmp('Content-Length') == 0)
      end
    end

    tach('original') do
      socket, headers = none_match_socket, {}
      until ((data = socket.readline).chop!).empty?
        key, value = data.split(': ')
        headers[key] = value
      end
      headers.has_key?('Transfer-Encoding') && headers['Transfer-Encoding'].casecmp('chunked') == 0
      headers.has_key?('Connection') && headers['Connection'].casecmp('close') == 0
      headers.has_key?('Content-Length')
    end
  end
end