Class: HTTPClient

Inherits:
Object show all
Defined in:
lib/rwd/net.rb

Defined Under Namespace

Classes: Chunk, Header, NoAddressException

Constant Summary collapse

@@versie =
1
@@mutex =
Mutex.new
@@hosts =
{}

Class Method Summary collapse

Class Method Details

.from_cache(action, uri, form) ⇒ Object


491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/rwd/net.rb', line 491

def self.from_cache(action, uri, form)
  loc		= uri.to_s + form.sort.inspect
  hash	= MD5.new("#{@@versie} #{loc}")

  dir		= "#{temp}/evcache.#{user}/httpclient.#{action}"
  file	= "#{dir}/#{hash}"
  data	= nil

  File.mkpath(dir)

  expire	= 356*24*60*60

  if File.file?(file) and (Time.new.to_f - File.stat(file).mtime.to_f < expire)
    @@mutex.synchronize do
      File.open(file, "rb")	{|f| data = f.read}
    end
  else
    data	= method(action).call(uri, form)

    if not data.nil?
      @@mutex.synchronize do
        File.open(file, "wb")	{|f| f.write data}
      end
    end
  end

  return data
end

.get(uri, httpheader = {}, form = {}) ⇒ Object


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/rwd/net.rb', line 386

def self.get(uri, httpheader={}, form={})
  post	= Array.new
  form.each_pair do |var, value|
    post << "#{var.to_html}=#{value.to_html}"
  end
  post	= post.join("?")

  data	= 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

        if post.empty?
          io.write "GET %s%s HTTP/1.0\r\n" % [(uri.path or '/'), (uri.varstring.empty? ? '' : '?' + uri.varstring)]
        else
          io.write "POST %s%s HTTP/1.0\r\n" % [(uri.path or '/'), (uri.varstring.empty? ? '' : '?' + uri.varstring)]
        end
      else
        proxy	= EVURI.new($proxy)
        io	= TCPSocket.new(proxy.host, proxy.port.zero? ? 8080 : proxy.port)

        if post.empty?
          io.write "GET %s HTTP/1.0\r\n" % uri
        else
          io.write "POST %s HTTP/1.0\r\n" % uri
        end
      end

      io.write "Host: %s\r\n" % host
      io.write "User-Agent: xyz\r\n"
      io.write "Proxy-Authorization: Basic %s\r\n" % $proxy_auth	unless $proxy_auth.nil?
      #io.write "Accept-Encoding: deflate\r\n"
      #io.write "Accept-Charset: ISO-8859-1\r\n"
      io.write "Connection: close\r\n"
      io.write "Content-Type: application/x-www-form-urlencoded\r\n"	unless post.empty?
      io.write "Content-Length: %s\r\n" % post.length			unless post.empty?
      httpheader.each do |k, v|
        $stderr.puts "%s: %s\r\n" % [k, v]
        io.write "%s: %s\r\n" % [k, v]
      end
      io.write "\r\n"
      io.write post							unless post.empty?

      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)
      length	= header.header["content-length"]
      data	= ""	if length == "0"

      if header.header["location"] != uri.to_s
        uri	= EVURI.new(uri) + header.header["location"]
      else
        uri	= nil
      end

      if header.header["transfer-encoding"] == "chunked"
        data	= Chunk.new(data).to_s	if not data.nil?
      end

      #if header.header["content-encoding"] == "gzip"
        #data	= "gzip -d".exec(data)	if not data.nil?
      #end

      data	= nil	unless header.code == 200
    end
  rescue Errno::ECONNRESET, Errno::EHOSTUNREACH => e
    $stderr.puts e.message
    sleep 1
    retry
  rescue Errno::ECONNREFUSED => e
    data	= nil
  rescue NoAddressException, Errno::ECONNREFUSED => e
    $stderr.puts e.message
    data	= nil
  end

  GC.start

  return data
end

.get_from_cache(uri, form = {}) ⇒ Object


487
488
489
# File 'lib/rwd/net.rb', line 487

def self.get_from_cache(uri, form={})
  from_cache("get", uri, form)
end

.getaddress(host) ⇒ Object

Raises:


317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rwd/net.rb', line 317

def self.getaddress(host)
  if not @@hosts.include?(host)
    @@hosts[host]	= ""
    evtimeout(5) do	# ??? Doet 'ut niet?...
      @@hosts[host]	= IPSocket.getaddress(host)
    end
  end

  raise NoAddressException, host	if @@hosts[host].empty?

  @@hosts[host]
end

.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

.head_from_cache(uri, form = {}) ⇒ Object


483
484
485
# File 'lib/rwd/net.rb', line 483

def self.head_from_cache(uri, form={})
  from_cache("head", uri, form)
end