Class: Http2

Inherits:
Object
  • Object
show all
Defined in:
lib/http2.rb

Overview

This class tries to emulate a browser in Ruby without any visual stuff. Remember cookies, keep sessions alive, reset connections according to keep-alive rules and more.

Examples

Http2.new(:host => “www.somedomain.com”, :port => 80, :ssl => false, :debug => false) do |http|

res = http.get("index.rhtml?show=some_page")
html = res.body
print html

res = res.post("index.rhtml?choice=login", {"username" => "John Doe", "password" => 123})
print res.body
print "#{res.headers}"

end

Constant Summary collapse

VALID_ARGUMENTS_INITIALIZE =
[:host, :port, :ssl, :nl, :user_agent, :raise_errors, :follow_redirects, :debug, :encoding_gzip, :autostate, :basic_auth, :extra_headers, :proxy]
VALID_ARGUMENTS_POST =
[:post, :url, :default_headers, :headers, :json, :method, :cookies, :on_content, :content_type]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Http2

Returns a new instance of Http2.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/http2.rb', line 30

def initialize(args = {})
  args = {:host => args} if args.is_a?(String)
  raise "Arguments wasnt a hash." if !args.is_a?(Hash)
  
  args.each do |key, val|
    raise "Invalid key: '#{key}'." if !VALID_ARGUMENTS_INITIALIZE.include?(key)
  end
  
  @args = args
  @cookies = {}
  @debug = @args[:debug]
  @autostate_values = {} if @args[:autostate]
  
  require "monitor" unless ::Kernel.const_defined?(:Monitor)
  @mutex = Monitor.new
  
  if !@args[:port]
    if @args[:ssl]
      @args[:port] = 443
    else
      @args[:port] = 80
    end
  end
  
  if @args[:nl]
    @nl = @args[:nl]
  else
    @nl = "\r\n"
  end
  
  if @args[:user_agent]
    @uagent = @args[:user_agent]
  else
    @uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
  end
  
  if !@args.key?(:raise_errors) || @args[:raise_errors]
    @raise_errors = true
  else
    @raise_errors = false
  end
  
  raise "No host was given." if !@args[:host]
  self.reconnect
  
  if block_given?
    begin
      yield(self)
    ensure
      self.destroy
    end
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



27
28
29
# File 'lib/http2.rb', line 27

def args
  @args
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



27
28
29
# File 'lib/http2.rb', line 27

def cookies
  @cookies
end

#respObject (readonly)

Returns the value of attribute resp.



27
28
29
# File 'lib/http2.rb', line 27

def resp
  @resp
end

Class Method Details

.const_missing(name) ⇒ Object

Autoloader for subclasses.



14
15
16
17
# File 'lib/http2.rb', line 14

def self.const_missing(name)
  require "#{File.dirname(__FILE__)}/../include/#{name.to_s.downcase}.rb"
  return Http2.const_get(name)
end

Converts a URL to “is.gd”-short-URL.



20
21
22
23
24
25
# File 'lib/http2.rb', line 20

def self.isgdlink(url)
  Http2.new(:host => "is.gd") do |http|
    resp = http.get("/api.php?longurl=#{url}")
    return resp.body
  end
end

.post_convert_data(pdata, args = nil) ⇒ Object

This is used to convert a hash to valid post-data recursivly.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/http2.rb', line 308

def self.post_convert_data(pdata, args = nil)
  praw = ""
  
  if pdata.is_a?(Hash)
    pdata.each do |key, val|
      praw << "&" if praw != ""
      
      if args and args[:orig_key]
        key = "#{args[:orig_key]}[#{key}]"
      end
      
      if val.is_a?(Hash) or val.is_a?(Array)
        praw << self.post_convert_data(val, {:orig_key => key})
      else
        praw << "#{Http2::Utils.urlenc(key)}=#{Http2::Utils.urlenc(Http2.post_convert_data(val))}"
      end
    end
  elsif pdata.is_a?(Array)
    count = 0
    pdata.each do |val|
      praw << "&" if praw != ""
      
      if args and args[:orig_key]
        key = "#{args[:orig_key]}[#{count}]"
      else
        key = count
      end
      
      if val.is_a?(Hash) or val.is_a?(Array)
        praw << self.post_convert_data(val, {:orig_key => key})
      else
        praw << "#{Http2::Utils.urlenc(key)}=#{Http2::Utils.urlenc(Http2.post_convert_data(val))}"
      end
      
      count += 1
    end
  else
    return pdata.to_s
  end
  
  return praw
end

Instance Method Details

#change(args) ⇒ Object

Closes current connection if any, changes the arguments on the object and reconnects keeping all cookies and other stuff intact.



85
86
87
88
89
# File 'lib/http2.rb', line 85

def change(args)
  self.close
  @args.merge!(args)
  self.reconnect
end

#closeObject

Closes the current connection if any.



92
93
94
95
96
# File 'lib/http2.rb', line 92

def close
  @sock.close if @sock and !@sock.closed?
  @sock_ssl.close if @sock_ssl and !@sock_ssl.closed?
  @sock_plain.close if @sock_plain and !@sock_plain.closed?
end

#default_headers(args = {}) ⇒ Object

Returns the default headers for a request.

Examples

headers_hash = http.default_headers print “#headers_hash”



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/http2.rb', line 274

def default_headers(args = {})
  return args[:default_headers] if args[:default_headers]
  
  headers = {
    "Connection" => "Keep-Alive",
    "User-Agent" => @uagent
  }
  
  #Possible to give custom host-argument.
  _args = args[:host] ? args : @args
  headers["Host"] = _args[:host]
  headers["Host"] += ":#{_args[:port]}" unless _args[:port] && [80,443].include?(_args[:port].to_i)
  
  if !@args.key?(:encoding_gzip) or @args[:encoding_gzip]
    headers["Accept-Encoding"] = "gzip"
  end
  
  if @args[:basic_auth]
    require "base64" unless ::Kernel.const_defined?(:Base64)
    headers["Authorization"] = "Basic #{Base64.encode64("#{@args[:basic_auth][:user]}:#{@args[:basic_auth][:passwd]}").strip}"
  end
  
  if @args[:extra_headers]
    headers.merge!(@args[:extra_headers])
  end
  
  if args[:headers]
    headers.merge!(args[:headers])
  end
  
  return headers
end

#delete(args) ⇒ Object

Proxies the request to another method but forces the method to be “DELETE”.



244
245
246
247
248
249
250
# File 'lib/http2.rb', line 244

def delete(args)
  if args[:json]
    return self.post(args.merge(:method => :delete))
  else
    return self.get(args.merge(:method => :delete))
  end
end

#destroyObject

Destroys the object unsetting all variables and closing all sockets.

Examples

http.destroy



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/http2.rb', line 118

def destroy
  @args = nil
  @cookies = nil
  @debug = nil
  @mutex = nil
  @uagent = nil
  @keepalive_timeout = nil
  @request_last = nil
  
  @sock.close if @sock and !@sock.closed?
  @sock = nil
  
  @sock_plain.close if @sock_plain and !@sock_plain.closed?
  @sock_plain = nil
  
  @sock_ssl.close if @sock_ssl and !@sock_ssl.closed?
  @sock_ssl = nil
end

#get(args) ⇒ Object

Returns a result-object based on the arguments.

Examples

res = http.get(“somepage.html”) print res.body #=> <String>-object containing the HTML gotten.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/http2.rb', line 217

def get(args)
  args = self.parse_args(args)
  
  if args.key?(:method) && args[:method]
    method = args[:method].to_s.upcase
  else
    method = "GET"
  end
  
  header_str = "#{method} /#{args[:url]} HTTP/1.1#{@nl}"
  header_str << self.header_str(self.default_headers(args), args)
  header_str << @nl
  
  @mutex.synchronize do
    print "Http2: Writing headers.\n" if @debug
    print "Header str: #{header_str}\n" if @debug
    self.write(header_str)
    
    print "Http2: Reading response.\n" if @debug
    resp = self.read_response(args)
    
    print "Http2: Done with get request.\n" if @debug
    return resp
  end
end

#header_str(headers_hash, args = {}) ⇒ Object

Returns a header-string which normally would be used for a request in the given state.



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/http2.rb', line 500

def header_str(headers_hash, args = {})
  if @cookies.length > 0 and (!args.key?(:cookies) or args[:cookies])
    cstr = ""
    
    first = true
    @cookies.each do |cookie_name, cookie_data|
      cstr << "; " if !first
      first = false if first
      
      if cookie_data.is_a?(Hash)
        name = cookie_data["name"]
        value = cookie_data["value"]
      else
        name = cookie_name
        value = cookie_data
      end
      
      raise "Unexpected lines: #{value.lines.to_a.length}." if value.lines.to_a.length != 1
      cstr << "#{Http2::Utils.urlenc(name)}=#{Http2::Utils.urlenc(value)}"
    end
    
    headers_hash["Cookie"] = cstr
  end
  
  headers_str = ""
  headers_hash.each do |key, val|
    headers_str << "#{key}: #{val}#{@nl}"
  end
  
  return headers_str
end

#on_content_call(args, str) ⇒ Object



532
533
534
# File 'lib/http2.rb', line 532

def on_content_call(args, str)
  args[:on_content].call(str) if args.key?(:on_content)
end

#parse_args(*args) ⇒ Object

Forces various stuff into arguments-hash like URL from original arguments and enables single-string-shortcuts and more.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/http2.rb', line 193

def parse_args(*args)
  if args.length == 1 and args.first.is_a?(String)
    args = {:url => args.first}
  elsif args.length >= 2
    raise "Couldnt parse arguments."
  elsif args.is_a?(Array) and args.length == 1
    args = args.first
  else
    raise "Invalid arguments: '#{args.class.name}'."
  end
  
  if !args.key?(:url) or !args[:url]
    raise "No URL given: '#{args[:url]}'."
  elsif args[:url].to_s.split("\n").length > 1
    raise "Multiple lines given in URL: '#{args[:url]}'."
  end
  
  return args
end

#parse_body(line, args) ⇒ Object

Parses the body based on given headers and saves it to the result-object. http.parse_body(str)



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/http2.rb', line 735

def parse_body(line, args)
  if @resp.args[:http_version] = "1.1"
    return "break" if @length == 0
    
    if @transfer_encoding == "chunked"
      len = line.strip.hex
      
      if len > 0
        read = @sock.read(len)
        return "break" if read == "" or (read == "\n" || read == "\r\n")
        @resp.args[:body] << read
        self.on_content_call(args, read)
      end
      
      nl = @sock.gets
      if len == 0
        if nl == "\n" || nl == "\r\n"
          return "break"
        else
          raise "Dont know what to do :'-("
        end
      end
      
      raise "Should have read newline but didnt: '#{nl}'." if nl != @nl
    else
      puts "Http2: Adding #{line.to_s.bytesize} to the body." if @debug
      @resp.args[:body] << line.to_s
      self.on_content_call(args, line)
      return "break" if @resp.header?("content-length") && @resp.args[:body].length >= @resp.header("content-length").to_i
    end
  else
    raise "Dont know how to read HTTP version: '#{@resp.args[:http_version]}'."
  end
end

#parse_header(line, args = {}) ⇒ Object

Parse a header-line and saves it on the object.

Examples

http.parse_header(“Content-Type: text/htmlrn”)



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/http2.rb', line 678

def parse_header(line, args = {})
  if match = line.match(/^(.+?):\s*(.+)#{@nl}$/)
    key = match[1].to_s.downcase
    
    if key == "set-cookie"
      Http2::Utils.parse_set_cookies(match[2]).each do |cookie_data|
        @cookies[cookie_data["name"]] = cookie_data
      end
    elsif key == "keep-alive"
      if ka_max = match[2].to_s.match(/max=(\d+)/)
        @keepalive_max = ka_max[1].to_i
        print "Http2: Keepalive-max set to: '#{@keepalive_max}'.\n" if @debug
      end
      
      if ka_timeout = match[2].to_s.match(/timeout=(\d+)/)
        @keepalive_timeout = ka_timeout[1].to_i
        print "Http2: Keepalive-timeout set to: '#{@keepalive_timeout}'.\n" if @debug
      end
    elsif key == "connection"
      @connection = match[2].to_s.downcase
    elsif key == "content-encoding"
      @encoding = match[2].to_s.downcase
    elsif key == "content-length"
      @length = match[2].to_i
    elsif key == "content-type"
      ctype = match[2].to_s
      if match_charset = ctype.match(/\s*;\s*charset=(.+)/i)
        @charset = match_charset[1].downcase
        @resp.args[:charset] = @charset
        ctype.gsub!(match_charset[0], "")
      end
      
      @ctype = ctype
      @resp.args[:contenttype] = @ctype
    elsif key == "transfer-encoding"
      @transfer_encoding = match[2].to_s.downcase.strip
    end
    
    puts "Http2: Parsed header: #{match[1]}: #{match[2]}" if @debug
    @resp.headers[key] = [] unless @resp.headers.key?(key)
    @resp.headers[key] << match[2]
    
    if key != "transfer-encoding" and key != "content-length" and key != "connection" and key != "keep-alive"
      self.on_content_call(args, line)
    end
  elsif match = line.match(/^HTTP\/([\d\.]+)\s+(\d+)\s+(.+)$/)
    @resp.args[:code] = match[2]
    @resp.args[:http_version] = match[1]
    
    self.on_content_call(args, line)
  else
    raise "Could not understand header string: '#{line}'.\n\n#{@sock.read(409600)}"
  end
end

#post(args) ⇒ Object

Posts to a certain page.

Examples

res = http.post(“login.php”, {“username” => “John Doe”, “password” => 123)



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
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/http2.rb', line 355

def post(args)
  args.each do |key, val|
    raise "Invalid key: '#{key}'." unless VALID_ARGUMENTS_POST.include?(key)
  end
  
  args = self.parse_args(args)
  
  if args.key?(:method) && args[:method]
    method = args[:method].to_s.upcase
  else
    method = "POST"
  end
  
  if args[:json]
    require "json" unless ::Kernel.const_defined?(:JSON)
    praw = args[:json].to_json
    content_type = "application/json"
  elsif args[:post].is_a?(String)
    praw = args[:post]
  else
    phash = args[:post] ? args[:post].clone : {}
    autostate_set_on_post_hash(phash) if @args[:autostate]
    praw = Http2.post_convert_data(phash)
  end

  content_type = args[:content_type] || content_type || "application/x-www-form-urlencoded"
  
  @mutex.synchronize do
    puts "Http2: Doing post." if @debug
    
    header_str = "#{method} /#{args[:url]} HTTP/1.1#{@nl}"
    header_str << self.header_str({"Content-Length" => praw.bytesize, "Content-Type" => content_type}.merge(self.default_headers(args)), args)
    header_str << @nl
    header_str << praw
    
    puts "Http2: Header str: #{header_str}" if @debug
    
    self.write(header_str)
    return self.read_response(args)
  end
end

#post_multipart(*args) ⇒ Object

Posts to a certain page using the multipart-method.

Examples

res = http.post_multipart(“upload.php”, => 123, “file” => Tempfile.new(?))



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
482
483
484
485
486
# File 'lib/http2.rb', line 400

def post_multipart(*args)
  args = self.parse_args(*args)
  
  phash = args[:post].clone
  autostate_set_on_post_hash(phash) if @args[:autostate]
  
  #Generate random string.
  boundary = rand(36**50).to_s(36)
  
  #Use tempfile to store contents to avoid eating memory if posting something really big.
  require "tempfile"
  
  Tempfile.open("http2_post_multipart_tmp_#{boundary}") do |praw|
    phash.each do |key, val|
      praw << "--#{boundary}#{@nl}"
      
      if val.class.name.to_s == "Tempfile" and val.respond_to?(:original_filename)
        praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val.original_filename}\";#{@nl}"
        praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
      elsif val.is_a?(Hash) and val[:filename]
        praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val[:filename]}\";#{@nl}"
        
        if val[:content]
          praw << "Content-Length: #{val[:content].to_s.bytesize}#{@nl}"
        elsif val[:fpath]
          praw << "Content-Length: #{File.size(val[:fpath])}#{@nl}"
        else
          raise "Could not figure out where to get content from."
        end
      else
        praw << "Content-Disposition: form-data; name=\"#{key}\";#{@nl}"
        praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
      end
      
      praw << "Content-Type: text/plain#{@nl}"
      praw << @nl
      
      if val.class.name.to_s == "StringIO"
        praw << val.read
      elsif val.is_a?(Hash) and val[:content]
        praw << val[:content].to_s
      elsif val.is_a?(Hash) and val[:fpath]
        File.open(val[:fpath], "r") do |fp|
          begin
            while data = fp.sysread(4096)
              praw << data
            end
          rescue EOFError
            #ignore.
          end
        end
      else
        praw << val.to_s
      end
      
      praw << @nl
    end
    
    praw << "--#{boundary}--"
    
    
    #Generate header-string containing 'praw'-variable.
    header_str = "POST /#{args[:url]} HTTP/1.1#{@nl}"
    header_str << self.header_str(self.default_headers(args).merge(
      "Content-Type" => "multipart/form-data; boundary=#{boundary}",
      "Content-Length" => praw.size
    ), args)
    header_str << @nl
    
    
    #Debug.
    print "Http2: Headerstr: #{header_str}\n" if @debug
    
    
    #Write and return.
    @mutex.synchronize do
      self.write(header_str)
      
      praw.rewind
      praw.lines do |data|
        self.sock_write(data)
      end
      
      return self.read_response(args)
    end
  end
end

#read_response(args = {}) ⇒ Object

Reads the response after posting headers and data.

Examples

res = http.read_response



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/http2.rb', line 539

def read_response(args = {})
  @mode = "headers"
  @transfer_encoding = nil
  @resp = Http2::Response.new(:request_args => args, :debug => @debug)
  rec_count = 0
  
  loop do
    begin
      if @length and @length > 0 and @mode == "body"
        line = @sock.read(@length)
        raise "Expected to get #{@length} of bytes but got #{line.bytesize}" if @length != line.bytesize
      else
        line = @sock.gets
      end
      
      if line
        rec_count += line.length
      elsif !line and rec_count <= 0
        @sock = nil
        raise Errno::ECONNABORTED, "Server closed the connection before being able to read anything (KeepAliveMax: '#{@keepalive_max}', Connection: '#{@connection}', PID: '#{Process.pid}')."
      end
      
      puts "<#{@mode}>: '#{line}'" if @debug
    rescue Errno::ECONNRESET => e
      if rec_count > 0
        print "Http2: The connection was reset while reading - breaking gently...\n" if @debug
        @sock = nil
        break
      else
        raise Errno::ECONNABORTED, "Server closed the connection before being able to read anything (KeepAliveMax: '#{@keepalive_max}', Connection: '#{@connection}', PID: '#{Process.pid}')."
      end
    end
    
    break if line.to_s == ""
    
    if @mode == "headers" and (line == "\n" || line == "\r\n")
      puts "Http2: Changing mode to body!" if @debug
      raise "No headers was given at all? Possibly corrupt state after last request?" if @resp.headers.empty?
      break if @length == 0
      @mode = "body"
      self.on_content_call(args, @nl)
      next
    end
    
    if @mode == "headers"
      self.parse_header(line, args)
    elsif @mode == "body"
      stat = self.parse_body(line, args)
      break if stat == "break"
      next if stat == "next"
    end
  end
  
  
  #Release variables.
  resp = @resp
  @resp = nil
  @mode = nil
  
  
  #Check if we should reconnect based on keep-alive-max.
  if @keepalive_max == 1 or @connection == "close"
    @sock.close if !@sock.closed?
    @sock = nil
  end
  
  
  
  # Validate that the response is as it should be.
  puts "Http2: Validating response." if @debug
  resp.validate!
  
  
  #Check if the content is gzip-encoded - if so: decode it!
  if @encoding == "gzip"
    require "zlib"
    require "stringio"
    io = StringIO.new(resp.args[:body])
    gz = Zlib::GzipReader.new(io)
    untrusted_str = gz.read
    
    begin
      valid_string = ic.encode("UTF-8")
    rescue
      valid_string = untrusted_str.force_encoding("UTF-8").encode("UTF-8", :invalid => :replace, :replace => "").encode("UTF-8")
    end
    
    resp.args[:body] = valid_string
  end
  
  
  
  
  raise "No status-code was received from the server. Headers: '#{resp.headers}' Body: '#{resp.args[:body]}'." if !resp.args[:code]
  
  if (resp.args[:code].to_s == "302" || resp.args[:code].to_s == "307") and resp.header?("location") and (!@args.key?(:follow_redirects) or @args[:follow_redirects])
    require "uri"
    uri = URI.parse(resp.header("location"))
    url = uri.path
    url << "?#{uri.query}" if uri.query.to_s.length > 0
    
    args = {:host => uri.host}
    args[:ssl] = true if uri.scheme == "https"
    args[:port] = uri.port if uri.port
    
    puts "Http2: Redirecting from location-header to '#{url}'." if @debug
    
    if !args[:host] or args[:host] == @args[:host]
      return self.get(url)
    else
      http = Http2.new(args)
      return http.get(url)
    end
  elsif @raise_errors && resp.args[:code].to_i == 500
    err = Http2::Errors::Internalserver.new(resp.body)
    err.response = resp
    raise err
  elsif @raise_errors && resp.args[:code].to_i == 403
    err = Http2::Errors::Noaccess.new(resp.body)
    err.response = resp
    raise err
  elsif @raise_errors && resp.args[:code].to_i == 400
    err = Http2::Errors::Badrequest.new(resp.body)
    err.response = resp
    raise err
  elsif @raise_errors && resp.args[:code].to_i == 404
    err = Http2::Errors::Notfound.new(resp.body)
    err.response = resp
    raise err
  else
    autostate_register(resp) if @args[:autostate]
    
    return resp
  end
end

#reconnectObject

Reconnects to the host.



138
139
140
141
142
143
144
145
146
147
148
149
150
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
# File 'lib/http2.rb', line 138

def reconnect
  require "socket"
  puts "Http2: Reconnect." if @debug
  
  #Reset variables.
  @keepalive_max = nil
  @keepalive_timeout = nil
  @connection = nil
  @contenttype = nil
  @charset = nil
  
  #Open connection.
  if @args[:proxy] && @args[:ssl]
    print "Http2: Initializing proxy stuff.\n" if @debug
    @sock_plain = TCPSocket.new(@args[:proxy][:host], @args[:proxy][:port])
    
    @sock_plain.write("CONNECT #{@args[:host]}:#{@args[:port]} HTTP/1.0#{@nl}")
    @sock_plain.write("User-Agent: #{@uagent}#{@nl}")
    
    if @args[:proxy][:user] and @args[:proxy][:passwd]
      credential = ["#{@args[:proxy][:user]}:#{@args[:proxy][:passwd]}"].pack("m")
      credential.delete!("\r\n")
      @sock_plain.write("Proxy-Authorization: Basic #{credential}#{@nl}")
    end
    
    @sock_plain.write(@nl)
    
    res = @sock_plain.gets
    raise res if res.to_s.downcase != "http/1.0 200 connection established#{@nl}"
  elsif @args[:proxy]
    print "Http2: Opening socket connection to '#{@args[:host]}:#{@args[:port]}' through proxy '#{@args[:proxy][:host]}:#{@args[:proxy][:port]}'.\n" if @debug
    @sock_plain = TCPSocket.new(@args[:proxy][:host], @args[:proxy][:port].to_i)
  else
    print "Http2: Opening socket connection to '#{@args[:host]}:#{@args[:port]}'.\n" if @debug
    @sock_plain = TCPSocket.new(@args[:host], @args[:port].to_i)
  end
  
  if @args[:ssl]
    print "Http2: Initializing SSL.\n" if @debug
    require "openssl" unless ::Kernel.const_defined?(:OpenSSL)
    
    ssl_context = OpenSSL::SSL::SSLContext.new
    #ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
    
    @sock_ssl = OpenSSL::SSL::SSLSocket.new(@sock_plain, ssl_context)
    @sock_ssl.sync_close = true
    @sock_ssl.connect
    
    @sock = @sock_ssl
  else
    @sock = @sock_plain
  end
end

#sock_puts(str) ⇒ Object



495
496
497
# File 'lib/http2.rb', line 495

def sock_puts(str)
  self.sock_write("#{str}#{@nl}")
end

#sock_write(str) ⇒ Object



488
489
490
491
492
493
# File 'lib/http2.rb', line 488

def sock_write(str)
  str = str.to_s
  return nil if str.empty?
  count = @sock.write(str)
  raise "Couldnt write to socket: '#{count}', '#{str}'." if count <= 0
end

#socket_working?Boolean

Returns boolean based on the if the object is connected and the socket is working.

Examples

puts “Socket is working.” if http.socket_working?

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/http2.rb', line 101

def socket_working?
  return false if !@sock or @sock.closed?
  
  if @keepalive_timeout and @request_last
    between = Time.now.to_i - @request_last.to_i
    if between >= @keepalive_timeout
      puts "Http2: We are over the keepalive-wait - returning false for socket_working?." if @debug
      return false
    end
  end
  
  return true
end

#write(str) ⇒ Object

Tries to write a string to the socket. If it fails it reconnects and tries again.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/http2.rb', line 253

def write(str)
  #Reset variables.
  @length = nil
  @encoding = nil
  self.reconnect if !self.socket_working?
  
  begin
    raise Errno::EPIPE, "The socket is closed." if !@sock or @sock.closed?
    self.sock_write(str)
  rescue Errno::EPIPE #this can also be thrown by puts.
    self.reconnect
    self.sock_write(str)
  end
  
  @request_last = Time.now
end