Class: NchanTools::Subscriber::HTTPChunkedClient

Inherits:
LongPollClient show all
Includes:
Celluloid::IO
Defined in:
lib/nchan_tools/pubsub.rb

Instance Attribute Summary

Attributes inherited from LongPollClient

#timeout

Attributes inherited from Client

#concurrency

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LongPollClient

#close, #error, #initialize, #listen, #request_code_ok, #stop

Methods inherited from Client

#error, #handle_bundle_error, inherited, #initialize, lookup, #poke, #stop, unique_aliases

Constructor Details

This class inherits a constructor from NchanTools::Subscriber::LongPollClient

Class Method Details

.aliasesObject



1440
1441
1442
# File 'lib/nchan_tools/pubsub.rb', line 1440

def self.aliases
  [:chunked]
end

Instance Method Details

#new_bundle(uri, opt) ⇒ Object



1444
1445
1446
1447
1448
# File 'lib/nchan_tools/pubsub.rb', line 1444

def new_bundle(uri, opt)
   opt[:accept]="*/*"
   opt[:headers]=(opt[:headers] or {}).merge({"TE" => "Chunked"})
  super
end

#provides_msgid?Boolean

Returns:

  • (Boolean)


1427
1428
1429
# File 'lib/nchan_tools/pubsub.rb', line 1427

def provides_msgid?
  false
end

#run(*args) ⇒ Object



1431
1432
1433
1434
1435
1436
1437
1438
# File 'lib/nchan_tools/pubsub.rb', line 1431

def run(*args)
  if @http2
    @subscriber.on_failure error(0, "Chunked transfer is not allowed in HTTP/2")
    @connected = 0
    return
  end
  super
end

#setup_bundle(b) ⇒ Object



1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/nchan_tools/pubsub.rb', line 1450

def setup_bundle(b)
  super
  b.body_buf = nil
  b.on_headers do |code, headers|
    if code == 200
      if headers["Transfer-Encoding"] != "chunked"
        @subscriber.on_failure error(0, "Transfer-Encoding should be 'chunked', was '#{headers["Transfer-Encoding"]}'.", b)
        close b
      else
        @notready -= 1
        @cooked_ready.signal true if @notready == 0
        b.connected= true
      end
    else
      b.buffer_body!
      b.stop_after_headers = false
    end
  end
  
  b.stop_after_headers = true
  @inchunk = false
  @chunksize = 0
  @repeat = true
  @chunkbuf = ""
  b.on_chunk do |chunk|
    #puts "yeah"
    @chunkbuf << chunk
    @repeat = true
    while @repeat
      @repeat = false
      if !@inchunk && @chunkbuf.slice!(/^([a-fA-F0-9]+)\r\n/m)
        @chunksize = $~[1].to_i(16)
        @inchunk = true
      end
      
      if @inchunk
        if @chunkbuf.length >= @chunksize + 2
          msgbody = @chunkbuf.slice!(0...@chunksize)
          @chunkbuf.slice!(/^\r\n/m)
          @timer.reset if @timer
          unless @nomsg
            msg=Message.new msgbody, nil, nil
          else
            msg=msgbody
          end
          if @subscriber.on_message(msg, b) == false
            @subscriber.finished+=1
            close b
          end
          @repeat = true if @chunkbuf.length > 0
          @inchunk = false
          @chunksize = 0
        end
      end
    end
  end
  
  b.on_response do |code, headers, body|
    if code != 200
      @subscriber.on_failure(error(code, "", b))
    else
      @subscriber.on_failure error(410, "Server Closed Connection", b)
    end
    close b
  end
  
  b
end