eventmachine-win32 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,23 @@
1
- # $Id: header_and_content.rb 281 2006-11-20 03:17:22Z blackhedd $
1
+ # $Id: header_and_content.rb 322 2007-05-22 22:18:35Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
4
- # Date:: 15 November 2006
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 15 Nov 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
@@ -1,34 +1,23 @@
1
- # $Id: httpclient.rb 226 2006-08-10 08:55:49Z blackhedd $
1
+ # $Id: httpclient.rb 322 2007-05-22 22:18:35Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
4
5
  # Date:: 16 July 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
@@ -43,8 +32,9 @@ class HttpClient < Connection
43
32
  include EventMachine::Deferrable
44
33
 
45
34
 
35
+ MaxPostContentLength = 20 * 1024 * 1024
36
+
46
37
  # USAGE SAMPLE:
47
- # WARNING, POST is not yet supported!!!!!
48
38
  #
49
39
  # EventMachine.run {
50
40
  # http = EventMachine::Protocols::HttpClient.request(
@@ -62,7 +52,7 @@ class HttpClient < Connection
62
52
  #
63
53
 
64
54
  # TODO:
65
- # POST REQUESTS!!!!!!!!!!!
55
+ # Add streaming so we can support enormous POSTs. Current max is 20meg.
66
56
  # Timeout for connections that run too long or hang somewhere in the middle.
67
57
  # Persistent connections (HTTP/1.1), may need a associated delegate object.
68
58
  # DNS: Some way to cache DNS lookups for hostnames we connect to. Ruby's
@@ -100,10 +90,11 @@ class HttpClient < Connection
100
90
  end
101
91
 
102
92
  def send_request args
93
+ args[:verb] ||= args[:method] # Support :method as an alternative to :verb.
103
94
  args[:verb] ||= :get # IS THIS A GOOD IDEA, to default to GET if nothing was specified?
104
95
 
105
96
  verb = args[:verb].to_s.upcase
106
- unless ["GET", "POST", "HEAD"].include?(verb)
97
+ unless ["GET", "POST", "PUT", "DELETE", "HEAD"].include?(verb)
107
98
  set_deferred_status :failed, {:status => 0} # TODO, not signalling the error type
108
99
  return # NOTE THE EARLY RETURN, we're not sending any data.
109
100
  end
@@ -120,16 +111,33 @@ class HttpClient < Connection
120
111
 
121
112
  # Allow an override for the host header if it's not the connect-string.
122
113
  host = args[:host_header] || args[:host] || "_"
114
+ # For now, ALWAYS tuck in the port string, although we may want to omit it if it's the default.
115
+ port = args[:port]
116
+
117
+ # POST items.
118
+ postcontenttype = args[:contenttype] || "application/octet-stream"
119
+ postcontent = args[:content] || ""
120
+ raise "oversized content in HTTP POST" if postcontent.length > MaxPostContentLength
123
121
 
124
122
  # ESSENTIAL for the request's line-endings to be CRLF, not LF. Some servers misbehave otherwise.
125
123
  req = [
126
- "#{verb} #{request}#{qs} HTTP/1.1",
127
- "Host: #{host}",
124
+ "#{verb} #{request}#{qs} HTTP/1.0",
125
+ "Host: #{host}:#{port}",
128
126
  "User-agent: Ruby EventMachine",
129
- ""
130
- ].map {|l| "#{l}\r\n"}.join
127
+ ]
131
128
 
132
- send_data req
129
+ if verb == "POST" || verb == "PUT"
130
+ req << "Content-type: #{postcontenttype}"
131
+ req << "Content-length: #{postcontent.length}"
132
+ end
133
+
134
+ req << ""
135
+ reqstring = req.map {|l| "#{l}\r\n"}.join
136
+ send_data reqstring
137
+
138
+ if verb == "POST" || verb == "PUT"
139
+ send_data postcontent
140
+ end
133
141
  end
134
142
 
135
143
 
@@ -149,7 +157,12 @@ class HttpClient < Connection
149
157
  if ary.length == 2
150
158
  data = ary.last
151
159
  if ary.first == ""
152
- @read_state = :content
160
+ if @content_length and @content_length > 0
161
+ @read_state = :content
162
+ else
163
+ dispatch_response
164
+ @read_state = :base
165
+ end
153
166
  else
154
167
  @headers << ary.first
155
168
  if @headers.length == 1
@@ -1,141 +1,120 @@
1
- # $Id: line_and_text.rb 279 2006-11-18 15:40:25Z blackhedd $
1
+ # $Id: line_and_text.rb 322 2007-05-22 22:18:35Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
4
5
  # Date:: 15 November 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
35
24
  #
36
-
37
-
25
+ #
26
+ require File.dirname(__FILE__) + '/buftok'
38
27
 
39
28
  module EventMachine
40
- module Protocols
41
-
42
- class LineAndTextProtocol < Connection
43
- MaxLineLength = 16*1024
44
- MaxBinaryLength = 32*1024*1024
29
+ module Protocols
45
30
 
46
- def initialize *args
47
- super
48
- lbp_init_line_state
49
- end
50
- def receive_data data
51
- if @lbp_mode == :lines
52
- @lbp_data << data
53
- while i = @lbp_data.index("\n")
54
- # line-length test is provisional. Need to be tunable and do something
55
- # more intelligent than throwing something.
56
- if i > MaxLineLength
57
- receive_error("overlength line") if respond_to?(:receive_error)
58
- close_connection
59
- break # exit the while loop
60
- end
61
- line = @lbp_data.slice!(0..i).chomp
62
- receive_line line if respond_to?(:receive_line)
63
- end
64
- else
65
- if @lbp_binary_limit > 0
66
- wanted = @lbp_binary_limit - @lbp_binary_bytes_received
67
- chunk = nil
68
- if data.length > wanted
69
- chunk = data.slice!(0...wanted)
70
- else
71
- chunk = data
72
- data = ""
73
- end
74
- @lbp_binary_buffer[@lbp_binary_bytes_received...(@lbp_binary_bytes_received+chunk.length)] = chunk
75
- @lbp_binary_bytes_received += chunk.length
76
- if @lbp_binary_bytes_received == @lbp_binary_limit
77
- receive_binary_data(@lbp_binary_buffer) if respond_to?(:receive_binary_data)
78
- lbp_init_line_state
79
- end
80
- receive_data(data) if data.length > 0
81
- else
82
- receive_binary_data(data) if respond_to?(:receive_binary_data)
83
- data = ""
84
- end
85
- end
86
- end
31
+ class LineAndTextProtocol < Connection
32
+ MaxLineLength = 16*1024
33
+ MaxBinaryLength = 32*1024*1024
87
34
 
88
- def unbind
89
- if @lbp_mode == :binary and @lbp_binary_limit > 0
90
- if respond_to?(:receive_binary_data)
91
- receive_binary_data( @lbp_binary_buffer[0...@lbp_binary_bytes_received] )
92
- end
93
- end
94
- end
35
+ def initialize *args
36
+ super
37
+ lbp_init_line_state
38
+ end
39
+ def receive_data data
40
+ if @lbp_mode == :lines
41
+ begin
42
+ @lpb_buffer.extract(data).each { |line| receive_line(line) if respond_to?(:receive_line) }
43
+ rescue Exception
44
+ receive_error('overlength line') if respond_to?(:receive_error)
45
+ close_connection
46
+ return
47
+ end
48
+ else
49
+ if @lbp_binary_limit > 0
50
+ wanted = @lbp_binary_limit - @lbp_binary_bytes_received
51
+ chunk = nil
52
+ if data.length > wanted
53
+ chunk = data.slice!(0...wanted)
54
+ else
55
+ chunk = data
56
+ data = ""
57
+ end
58
+ @lbp_binary_buffer[@lbp_binary_bytes_received...(@lbp_binary_bytes_received+chunk.length)] = chunk
59
+ @lbp_binary_bytes_received += chunk.length
60
+ if @lbp_binary_bytes_received == @lbp_binary_limit
61
+ receive_binary_data(@lbp_binary_buffer) if respond_to?(:receive_binary_data)
62
+ lbp_init_line_state
63
+ end
64
+ receive_data(data) if data.length > 0
65
+ else
66
+ receive_binary_data(data) if respond_to?(:receive_binary_data)
67
+ data = ""
68
+ end
69
+ end
70
+ end
95
71
 
96
- # Set up to read the supplied number of binary bytes.
97
- # This recycles all the data currently waiting in the line buffer, if any.
98
- # If the limit is nil, then ALL subsequent data will be treated as binary
99
- # data and passed to the upstream protocol handler as we receive it.
100
- # If a limit is given, we'll hold the incoming binary data and not
101
- # pass it upstream until we've seen it all, or until there is an unbind
102
- # (in which case we'll pass up a partial).
103
- # Specifying nil for the limit (the default) means there is no limit.
104
- # Specifiyng zero for the limit will cause an immediate transition back to line mode.
105
- #
106
- def set_binary_mode size = nil
107
- if @lbp_mode == :lines
108
- if size == 0
109
- receive_binary_data("") if respond_to?(:receive_binary_data)
110
- # Do no more work here. Stay in line mode and keep consuming data.
111
- else
112
- @lbp_binary_limit = size.to_i # (nil will be stored as zero)
113
- if @lbp_binary_limit > 0
114
- raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check
115
- @lbp_binary_buffer = "\0" * @lbp_binary_limit
116
- @lbp_binary_bytes_received = 0
117
- end
72
+ def unbind
73
+ if @lbp_mode == :binary and @lbp_binary_limit > 0
74
+ if respond_to?(:receive_binary_data)
75
+ receive_binary_data( @lbp_binary_buffer[0...@lbp_binary_bytes_received] )
76
+ end
77
+ end
78
+ end
118
79
 
119
- @lbp_mode = :binary
120
- if @lbp_data.length > 0
121
- d,@lbp_data = @lbp_data,""
122
- receive_data d
123
- end
124
- end
125
- else
126
- raise "invalid operation"
127
- end
128
- end
80
+ # Set up to read the supplied number of binary bytes.
81
+ # This recycles all the data currently waiting in the line buffer, if any.
82
+ # If the limit is nil, then ALL subsequent data will be treated as binary
83
+ # data and passed to the upstream protocol handler as we receive it.
84
+ # If a limit is given, we'll hold the incoming binary data and not
85
+ # pass it upstream until we've seen it all, or until there is an unbind
86
+ # (in which case we'll pass up a partial).
87
+ # Specifying nil for the limit (the default) means there is no limit.
88
+ # Specifiyng zero for the limit will cause an immediate transition back to line mode.
89
+ #
90
+ def set_binary_mode size = nil
91
+ if @lbp_mode == :lines
92
+ if size == 0
93
+ receive_binary_data("") if respond_to?(:receive_binary_data)
94
+ # Do no more work here. Stay in line mode and keep consuming data.
95
+ else
96
+ @lbp_binary_limit = size.to_i # (nil will be stored as zero)
97
+ if @lbp_binary_limit > 0
98
+ raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check
99
+ @lbp_binary_buffer = "\0" * @lbp_binary_limit
100
+ @lbp_binary_bytes_received = 0
101
+ end
129
102
 
130
- #--
131
- # For internal use, establish protocol baseline for handling lines.
132
- def lbp_init_line_state
133
- @lbp_data = ""
134
- @lbp_mode = :lines
135
- end
136
- private :lbp_init_line_state
103
+ @lbp_mode = :binary
104
+ receive_data @lpb_buffer.flush
105
+ end
106
+ else
107
+ raise "invalid operation"
108
+ end
109
+ end
137
110
 
138
- end
111
+ #--
112
+ # For internal use, establish protocol baseline for handling lines.
113
+ def lbp_init_line_state
114
+ @lpb_buffer = BufferedTokenizer.new("\n", MaxLineLength)
115
+ @lbp_mode = :lines
116
+ end
117
+ private :lbp_init_line_state
139
118
  end
119
+ end
140
120
  end
141
-
@@ -1,37 +1,27 @@
1
- # $Id: tcptest.rb 218 2006-07-17 10:29:20Z blackhedd $
1
+ # $Id: tcptest.rb 322 2007-05-22 22:18:35Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
4
5
  # Date:: 16 July 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
24
+ #
35
25
  #
36
26
 
37
27