Class: EventMachine::Protocols::HttpClient

Inherits:
Connection
  • Object
show all
Includes:
Deferrable
Defined in:
lib/protocols/httpclient.rb

Constant Summary collapse

MaxPostContentLength =
20 * 1024 * 1024

Instance Attribute Summary

Attributes inherited from Connection

#signature

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deferrable

#callback, #cancel_timeout, #errback, #fail, future, #set_deferred_failure, #set_deferred_status, #set_deferred_success, #succeed, #timeout

Methods inherited from Connection

#close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #get_peername, #get_pid, #initialize, #reconnect, #send_data, #send_datagram, #send_file_data, #set_comm_inactivity_timeout, #start_tls, #stream_file_data

Constructor Details

This class inherits a constructor from EventMachine::Connection

Class Method Details

.request(args = {}) ⇒ Object

TODO: Add streaming so we can support enormous POSTs. Current max is 20meg. Timeout for connections that run too long or hang somewhere in the middle. Persistent connections (HTTP/1.1), may need a associated delegate object. DNS: Some way to cache DNS lookups for hostnames we connect to. Ruby’s DNS lookups are unbelievably slow. HEAD requests. Chunked transfer encoding. Convenience methods for requests. get, post, url, etc. SSL. Handle status codes like 304, 100, etc. Refactor this code so that protocol errors all get handled one way (an exception?), instead of sprinkling set_deferred_status :failed calls everywhere.



68
69
70
71
72
73
74
# File 'lib/protocols/httpclient.rb', line 68

def self.request( args = {} )
  args[:port] ||= 80
  EventMachine.connect( args[:host], args[:port], self ) {|c|
    # According to the docs, we will get here AFTER post_init is called.
    c.instance_eval {@args = args}
  }
end

Instance Method Details

#connection_completedObject

We send the request when we get a connection. AND, we set an instance variable to indicate we passed through here. That allows #unbind to know whether there was a successful connection. NB: This naive technique won’t work when we have to support multiple requests on a single connection.



87
88
89
90
# File 'lib/protocols/httpclient.rb', line 87

def connection_completed
  @connected = true
  send_request @args
end

#dispatch_responseObject



229
230
231
232
233
234
235
236
237
238
# File 'lib/protocols/httpclient.rb', line 229

def dispatch_response
  @read_state = :base
  set_deferred_status :succeeded, {
    :content => @content,
    :headers => @headers,
    :status => @status
  }
  # TODO, we close the connection for now, but this is wrong for persistent clients.
  close_connection
end

#post_initObject



76
77
78
79
80
# File 'lib/protocols/httpclient.rb', line 76

def post_init
  @start_time = Time.now
  @data = ""
  @read_state = :base
end

#receive_data(data) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/protocols/httpclient.rb', line 151

def receive_data data
  while data and data.length > 0
    case @read_state
    when :base
      # Perform any per-request initialization here and don't consume any data.
      @data = ""
      @headers = []
      @content_length = nil # not zero
      @content = ""
      @status = nil
      @read_state = :header
    when :header
      ary = data.split( /\r?\n/m, 2 )
      if ary.length == 2
        data = ary.last
        if ary.first == ""
     if @content_length and @content_length > 0
  @read_state = :content
     else
  dispatch_response
  @read_state = :base
     end
        else
          @headers << ary.first
          if @headers.length == 1
            parse_response_line
          elsif ary.first =~ /\Acontent-length:\s*/i
            # Only take the FIRST content-length header that appears,
            # which we can distinguish because @content_length is nil.
            # TODO, it's actually a fatal error if there is more than one
            # content-length header, because the caller is presumptively
            # a bad guy. (There is an exploit that depends on multiple
            # content-length headers.)
            @content_length ||= $'.to_i
          end
        end
      else
        @data << data
        data = ""
      end
    when :content
      # If there was no content-length header, we have to wait until the connection
      # closes. Everything we get until that point is content.
      # TODO: Must impose a content-size limit, and also must implement chunking.
      # Also, must support either temporary files for large content, or calling
      # a content-consumer block supplied by the user.
      if @content_length
        bytes_needed = @content_length - @content.length
        @content += data[0, bytes_needed]
        data = data[bytes_needed..-1] || ""
        if @content_length == @content.length
          dispatch_response
          @read_state = :base
        end
      else
        @content << data
        data = ""
      end
    end
  end
end

#send_request(args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/protocols/httpclient.rb', line 92

def send_request args
  args[:verb] ||= args[:method] # Support :method as an alternative to :verb.
  args[:verb] ||= :get # IS THIS A GOOD IDEA, to default to GET if nothing was specified?

  verb = args[:verb].to_s.upcase
  unless ["GET", "POST", "PUT", "DELETE", "HEAD"].include?(verb)
    set_deferred_status :failed, {:status => 0} # TODO, not signalling the error type
    return # NOTE THE EARLY RETURN, we're not sending any data.
  end

  request = args[:request] || "/"
  unless request[0,1] == "/"
    request = "/" + request
  end

  qs = args[:query_string] || ""
  if qs.length > 0 and qs[0,1] != '?'
    qs = "?" + qs
  end

  # Allow an override for the host header if it's not the connect-string.
  host = args[:host_header] || args[:host] || "_"
  # For now, ALWAYS tuck in the port string, although we may want to omit it if it's the default.
  port = args[:port]

  # POST items.
  postcontenttype = args[:contenttype] || "application/octet-stream"
  postcontent = args[:content] || ""
  raise "oversized content in HTTP POST" if postcontent.length > MaxPostContentLength

  # ESSENTIAL for the request's line-endings to be CRLF, not LF. Some servers misbehave otherwise.
  # TODO: We ASSUME the caller wants to send a 1.1 request. May not be a good assumption.
  req = [
    "#{verb} #{request}#{qs} HTTP/1.1",
    "Host: #{host}:#{port}",
    "User-agent: Ruby EventMachine",
  ]

  if verb == "POST" || verb == "PUT"
    req << "Content-type: #{postcontenttype}"
    req << "Content-length: #{postcontent.length}"
  end

  # TODO, this cookie handler assumes it's getting a single, semicolon-delimited string.
  # Eventually we will want to deal intelligently with arrays and hashes.
  if args[:cookie]
    req << "Cookie: #{args[:cookie]}"
  end

  req << ""
  reqstring = req.map {|l| "#{l}\r\n"}.join
  send_data reqstring

  if verb == "POST" || verb == "PUT"
    send_data postcontent
  end
end

#unbindObject



240
241
242
243
244
245
246
# File 'lib/protocols/httpclient.rb', line 240

def unbind
  if !@connected
    set_deferred_status :failed, {:status => 0} # YECCCCH. Find a better way to signal no-connect/network error.
  elsif (@read_state == :content and @content_length == nil)
    dispatch_response
  end
end