Method: HTTPClient.head

Defined in:
lib/rwd/net.rb

.head(uri, form = {}, recursive = true) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/rwd/net.rb', line 330

def self.head(uri, form={}, recursive=true)
  header  = Header.new(nil)

  begin
    while not uri.nil?
      uri   = EVURI.new(uri) if uri.kind_of? String
      host    = uri.host
      port    = uri.port

      if $proxy.nil? or $proxy.empty? or host == "localhost"
        io    = nil

        @@mutex.synchronize do
          io      = TCPSocket.new(getaddress(host), port.zero? ? 80 : port)
        end

        io.write("HEAD #{uri.path or '/'}#{uri.varstring.empty? ? '' : '?' + uri.varstring} HTTP/1.0\r\nHost: #{host}\r\n\r\n")
      else
        proxy   = EVURI.new($proxy)
        io    = TCPSocket.new(proxy.host, proxy.port.zero? ? 8080 : proxy.port)

        io.write("HEAD #{uri} HTTP/1.0\r\n#{"Proxy-Authorization: Basic "+$proxy_auth+"\r\n" if not $proxy_auth.nil?}\r\n\r\n")
      end

      io.close_write

      res = io.read

      io.close_read

      header, data  = nil, nil
      header, data  = res.split(/\r*\n\r*\n/, 2) if not res.nil?
      header    = Header.new(header)

      if recursive and header.header["location"] != uri.to_s
        uri = EVURI.new(uri) + header.header["location"]
      else
        uri = nil
      end
    end
  rescue Errno::ECONNRESET, Errno::EHOSTUNREACH => e
    $stderr.puts e.message
    sleep 1
    retry
  rescue Errno::ECONNREFUSED => e
    data  = nil
  rescue NoAddressException => e
    $stderr.puts e.message
    header  = Header.new(nil)
  end

  GC.start

  return header
end