annotate net/http/server/stream.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
rev   line source
ferencd@0 1 require 'net/protocol'
ferencd@0 2
ferencd@0 3 module Net
ferencd@0 4 class HTTP < Protocol
ferencd@0 5 module Server
ferencd@0 6 #
ferencd@0 7 # Handles reading and writing to raw HTTP streams.
ferencd@0 8 #
ferencd@0 9 # @since 0.2.0
ferencd@0 10 #
ferencd@0 11 class Stream
ferencd@0 12
ferencd@0 13 include Enumerable
ferencd@0 14
ferencd@0 15 # The raw socket of the stream.
ferencd@0 16 attr_reader :socket
ferencd@0 17
ferencd@0 18 #
ferencd@0 19 # Creates a new stream.
ferencd@0 20 #
ferencd@0 21 # @param [TCPSocket] socket
ferencd@0 22 # The raw socket that will be read/write to.
ferencd@0 23 #
ferencd@0 24 # @since 0.2.0
ferencd@0 25 #
ferencd@0 26 def initialize(socket)
ferencd@0 27 @socket = socket
ferencd@0 28 end
ferencd@0 29
ferencd@0 30 #
ferencd@0 31 # Reads data from the stream.
ferencd@0 32 #
ferencd@0 33 # @param [Integer] length
ferencd@0 34 # The number of bytes to read.
ferencd@0 35 #
ferencd@0 36 # @param [#<<] buffer
ferencd@0 37 # The optional buffer to append the data to.
ferencd@0 38 #
ferencd@0 39 # @return [String, nil]
ferencd@0 40 # A chunk from the stream.
ferencd@0 41 #
ferencd@0 42 # @since 0.2.0
ferencd@0 43 #
ferencd@0 44 def read(length=4096,buffer='')
ferencd@0 45 @socket.read(length,buffer)
ferencd@0 46 end
ferencd@0 47
ferencd@0 48 #
ferencd@0 49 # Reads each chunk from the stream.
ferencd@0 50 #
ferencd@0 51 # @yield [chunk]
ferencd@0 52 # The given block will be passed each chunk.
ferencd@0 53 #
ferencd@0 54 # @yieldparam [String] chunk
ferencd@0 55 # A chunk from the stream.
ferencd@0 56 #
ferencd@0 57 # @return [Enumerator]
ferencd@0 58 # If no block is given, an Enumerator will be returned.
ferencd@0 59 #
ferencd@0 60 # @since 0.2.0
ferencd@0 61 #
ferencd@0 62 def each
ferencd@0 63 return enum_for unless block_given?
ferencd@0 64
ferencd@0 65 while (chunk = read)
ferencd@0 66 yield chunk
ferencd@0 67 end
ferencd@0 68 end
ferencd@0 69
ferencd@0 70 #
ferencd@0 71 # Reads the entire body.
ferencd@0 72 #
ferencd@0 73 # @return [String]
ferencd@0 74 # The complete body.
ferencd@0 75 #
ferencd@0 76 # @since 0.2.0
ferencd@0 77 #
ferencd@0 78 def body
ferencd@0 79 buffer = ''
ferencd@0 80
ferencd@0 81 each { |chunk| buffer << chunk }
ferencd@0 82 return buffer
ferencd@0 83 end
ferencd@0 84
ferencd@0 85 #
ferencd@0 86 # Writes data to the stream.
ferencd@0 87 #
ferencd@0 88 # @param [String] data
ferencd@0 89 # The data to write to the stream.
ferencd@0 90 #
ferencd@0 91 # @return [Integer]
ferencd@0 92 # The length of the data written.
ferencd@0 93 #
ferencd@0 94 # @since 0.2.0
ferencd@0 95 #
ferencd@0 96 def write(data)
ferencd@0 97 result = @socket.write(data)
ferencd@0 98
ferencd@0 99 @socket.flush
ferencd@0 100 return result
ferencd@0 101 end
ferencd@0 102
ferencd@0 103 #
ferencd@0 104 # @see #write
ferencd@0 105 #
ferencd@0 106 # @since 0.2.0
ferencd@0 107 #
ferencd@0 108 def <<(data)
ferencd@0 109 write(data)
ferencd@0 110 end
ferencd@0 111
ferencd@0 112 #
ferencd@0 113 # Closes the stream.
ferencd@0 114 #
ferencd@0 115 # @since 0.2.0
ferencd@0 116 #
ferencd@0 117 def close
ferencd@0 118 end
ferencd@0 119
ferencd@0 120 end
ferencd@0 121 end
ferencd@0 122 end
ferencd@0 123 end