ht2p 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/ht2p/client.rb CHANGED
@@ -30,35 +30,11 @@ class HT2P::Client
30
30
  end
31
31
 
32
32
  def request_header(method, header)
33
- @socket.write "%s %s%s HTTP/1.1\r\n" % [
34
- method.to_s.upcase,
35
- @uri.path,
36
- @uri.query && "?#{@uri.query}"
37
- ]
38
- @socket.write "Host: #{@uri.host}\r\n"
39
- @socket.write header.to_s
40
- @socket.write "\r\n"
33
+ @socket.write header.format(method, @uri)
41
34
  end
42
35
 
43
36
  def response_header
44
- code = nil
45
- header = HT2P::Header.new
46
-
47
- key = nil
48
- while line = @socket.gets.chop!
49
- line.empty? && break
50
-
51
- if md = %r!HTTP[\w\./]+\s+(\d+)!.match(line)
52
- code = md[1]
53
- elsif md = /(.+?):\s*(.*)/.match(line)
54
- key, val = md[1].downcase, md[2]
55
- header[key] = val
56
- elsif md = /\s+(.*)/.match(line)
57
- header[key] = md[1]
58
- end
59
- end
60
-
61
- [code.to_i, header]
37
+ HT2P::Header.load @socket
62
38
  end
63
39
 
64
40
  def write(val)
data/lib/ht2p/header.rb CHANGED
@@ -1,4 +1,26 @@
1
1
  class HT2P::Header < Hash
2
+ def self.load(io)
3
+ code = nil
4
+ header = self.new
5
+
6
+ key = nil
7
+ while line = io.gets
8
+ line.chop!
9
+ line.empty? && break
10
+
11
+ if md = %r!HTTP[\w\./]+\s+(\d+)!.match(line)
12
+ code = md[1].to_i
13
+ elsif md = /(.+?):\s*(.*)/.match(line)
14
+ key, val = md[1].downcase, md[2]
15
+ header[key] = val
16
+ elsif md = /\s+(.*)/.match(line)
17
+ header.append(key, md[1])
18
+ end
19
+ end
20
+
21
+ [code, header]
22
+ end
23
+
2
24
  def []=(key, val)
3
25
  if self.key? key
4
26
  if self[key].is_a? Array
@@ -11,6 +33,14 @@ class HT2P::Header < Hash
11
33
  end
12
34
  end
13
35
 
36
+ def append(key, val)
37
+ if self[key].is_a? Array
38
+ self[key].last << val
39
+ else
40
+ self[key] << val
41
+ end
42
+ end
43
+
14
44
  def to_s
15
45
  inject('') do |ret, (key, val)|
16
46
  if val.is_a? Array
@@ -24,4 +54,12 @@ class HT2P::Header < Hash
24
54
  ret
25
55
  end
26
56
  end
57
+
58
+ def format(method, uri)
59
+ "%s %s%s HTTP/1.1\r\n" % [
60
+ method.to_s.upcase,
61
+ uri.path,
62
+ uri.query && "?#{uri.query}"
63
+ ] << "Host: #{uri.host}\r\n" << to_s << "\r\n"
64
+ end
27
65
  end
data/spec/ht2p_spec.rb CHANGED
@@ -34,3 +34,98 @@ describe HT2P::Client do
34
34
  end
35
35
  end
36
36
  end
37
+
38
+ HEADER=<<END
39
+ HTTP/1.1 100 continue
40
+ HTTP/1.1 200 ok
41
+ a:A
42
+ b:B
43
+ b: B
44
+ c: C
45
+ C
46
+ d: D
47
+ d: D
48
+ D
49
+ END
50
+
51
+ require 'stringio'
52
+
53
+ describe HT2P::Header do
54
+ before do
55
+ @header = HT2P::Header.new
56
+ end
57
+
58
+ it 'should store some keys and values like a Hash' do
59
+ @header['a'] = 'A'
60
+ @header['a'].should == 'A'
61
+ end
62
+
63
+ it 'should change the value to an Array if the key has been stored' do
64
+ @header['a'] = 'A'
65
+ @header['a'] = 'A'
66
+ @header['a'].should == ['A', 'A']
67
+ end
68
+
69
+ it 'should perform `append` to append the value' do
70
+ @header['a'] = 'A'
71
+ @header.append('a', 'A')
72
+ @header['a'].should == 'AA'
73
+
74
+ @header['b'] = 'B'
75
+ @header['b'] = 'B'
76
+ @header.append('b', 'B')
77
+ @header['b'].should == ['B', 'BB']
78
+ end
79
+
80
+ it 'should perform `load` to load HTTP header' do
81
+ code, header = HT2P::Header.load(StringIO.new(HEADER))
82
+ code.should == 200
83
+ header['a'].should == 'A'
84
+ header['b'].should == ['B', 'B']
85
+ header['c'].should == 'CC'
86
+ header['d'].should == ['D', 'DD']
87
+ end
88
+
89
+ it 'should perform `format` to return header string' do
90
+ uri = URI.parse('http://example.com/')
91
+ s =<<END
92
+ GET / HTTP/1.1
93
+ Host: example.com
94
+
95
+ END
96
+ @header.format(:get, uri).should == s.gsub("\n", "\r\n")
97
+
98
+ @header['a'] = 'A'
99
+ uri = URI.parse('http://example.com/')
100
+ s =<<END
101
+ GET / HTTP/1.1
102
+ Host: example.com
103
+ a: A
104
+
105
+ END
106
+ @header.format(:get, uri).should == s.gsub("\n", "\r\n")
107
+
108
+ @header['b'] = 'B'
109
+ uri = URI.parse('http://example.com/')
110
+ s =<<END
111
+ GET / HTTP/1.1
112
+ Host: example.com
113
+ a: A
114
+ b: B
115
+
116
+ END
117
+ @header.format(:get, uri).should == s.gsub("\n", "\r\n")
118
+
119
+ @header['b'] = 'BB'
120
+ uri = URI.parse('http://example.com/')
121
+ s =<<END
122
+ GET / HTTP/1.1
123
+ Host: example.com
124
+ a: A
125
+ b: B
126
+ b: BB
127
+
128
+ END
129
+ @header.format(:get, uri).should == s.gsub("\n", "\r\n")
130
+ end
131
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ht2p
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jun Kikuchi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-11 00:00:00 +09:00
12
+ date: 2009-11-12 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency