Class: Hayabusa::Http_session::Contentgroup

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

Overview

This class handels the adding of content and writing to socket. Since this can be done with multiple threads and multiple IO’s it can get complicated.

Constant Summary collapse

NL =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Contentgroup

Returns a new instance of Contentgroup.



9
10
11
12
13
14
15
16
17
18
# File 'lib/hayabusa_http_session_contentgroup.rb', line 9

def initialize(args = {})
  @socket = args[:socket]
  @chunked = args[:chunked]
  @resp = args[:resp]
  @httpsession = args[:httpsession]
  @mutex = Mutex.new
  @debug = false
  @length_written = args[:length_written] ? args[:length_written] : 0
  @content_length = args[:content_length]
end

Instance Attribute Details

#chunkedObject

Returns the value of attribute chunked.



6
7
8
# File 'lib/hayabusa_http_session_contentgroup.rb', line 6

def chunked
  @chunked
end

#content_lengthObject

Returns the value of attribute content_length.



6
7
8
# File 'lib/hayabusa_http_session_contentgroup.rb', line 6

def content_length
  @content_length
end

#cur_dataObject (readonly)

Returns the value of attribute cur_data.



5
6
7
# File 'lib/hayabusa_http_session_contentgroup.rb', line 5

def cur_data
  @cur_data
end

#doneObject (readonly)

Returns the value of attribute done.



5
6
7
# File 'lib/hayabusa_http_session_contentgroup.rb', line 5

def done
  @done
end

#length_writtenObject

Returns the value of attribute length_written.



6
7
8
# File 'lib/hayabusa_http_session_contentgroup.rb', line 6

def length_written
  @length_written
end

#socketObject

Returns the value of attribute socket.



6
7
8
# File 'lib/hayabusa_http_session_contentgroup.rb', line 6

def socket
  @socket
end

Instance Method Details

#add_to_length_written(size) ⇒ Object

Adds the given size to the length written and raises an exception if it exceeds the sat content-length.



197
198
199
200
# File 'lib/hayabusa_http_session_contentgroup.rb', line 197

def add_to_length_written(size)
  @length_written += size
  raise "Content-Length overwritten: #{@length_written}, #{@content_length}" if @content_length != nil && @length_written > @content_length
end

#force_content(newcont) ⇒ Object

Forces the content to be the input - nothing else can be added after calling this.



51
52
53
# File 'lib/hayabusa_http_session_contentgroup.rb', line 51

def force_content(newcont)
  @ios = [{:str => newcont, :done => true}]
end

#initObject



20
21
22
23
24
25
26
27
28
# File 'lib/hayabusa_http_session_contentgroup.rb', line 20

def init
  @done = false
  @thread = nil
  @cur_data = {
    :str => "",
    :done => false
  }
  @ios = [@cur_data]
end

#joinObject



128
129
130
131
132
# File 'lib/hayabusa_http_session_contentgroup.rb', line 128

def join
  return nil if @forced
  sleep 0.1 while !@thread
  @thread.join
end

#mark_doneObject



123
124
125
126
# File 'lib/hayabusa_http_session_contentgroup.rb', line 123

def mark_done
  @cur_data[:done] = true
  @done = true
end

#new_io(obj = "") ⇒ Object



44
45
46
47
48
# File 'lib/hayabusa_http_session_contentgroup.rb', line 44

def new_io(obj = "")
  @cur_data[:done] = true if @cur_data
  @cur_data = {:str => obj, :done => false}
  @ios << @cur_data
end

#new_threadObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hayabusa_http_session_contentgroup.rb', line 60

def new_thread
  cgroup = Hayabusa::Http_session::Contentgroup.new(:socket => @socket, :chunked => @chunked, :content_length => @content_length)
  cgroup.init
  
  @mutex.synchronize do
    @ios << cgroup
    self.new_io
  end
  
  self.register_thread
  return cgroup
end

#register_threadObject



55
56
57
58
# File 'lib/hayabusa_http_session_contentgroup.rb', line 55

def register_thread
  Thread.current[:hayabusa] = {} if !Thread.current[:hayabusa]
  Thread.current[:hayabusa][:contentgroup] = self
end

#resetObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hayabusa_http_session_contentgroup.rb', line 30

def reset
  @ios = []
  @done = false
  @thread = nil
  @forced = false
  @length_written = 0
  
  @mutex.synchronize do
    self.new_io
  end
  
  self.register_thread
end

#write(cont) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hayabusa_http_session_contentgroup.rb', line 81

def write(cont)
  return if cont.empty?
  
  @mutex.synchronize do
    unless @cur_data[:str].is_a?(String)
      raise "Couldnt add to string with a length of #{cont.length} to str, because str was a #{@cur_data[:str].class.name}. Cont: #{cont}"
    else
      @cur_data[:str] << cont
    end
  end
end

#write_beginObject



73
74
75
76
77
78
79
# File 'lib/hayabusa_http_session_contentgroup.rb', line 73

def write_begin
  begin
    @resp.write if @httpsession.meta["METHOD"] != "HEAD"
  rescue Errno::ECONNRESET, Errno::ENOTCONN, Errno::EPIPE
    #Ignore - the user probaly left.
  end
end

#write_forceObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hayabusa_http_session_contentgroup.rb', line 111

def write_force
  @mutex.synchronize do
    @forced = true if !@thread
  end
  
  if @thread
    @thread.join
  else
    self.write_begin
  end
end

#write_outputObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hayabusa_http_session_contentgroup.rb', line 93

def write_output
  return nil if @thread
  
  @mutex.synchronize do
    @thread = Thread.new do
      begin
        self.write_begin
      rescue => e
        STDERR.puts "Error while writing."
        STDERR.puts e.inspect
        STDERR.puts e.backtrace
        
        raise e
      end
    end
  end
end

#write_to_socketObject



134
135
136
137
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
191
192
193
194
# File 'lib/hayabusa_http_session_contentgroup.rb', line 134

def write_to_socket
  count = 0
  
  @ios.each do |data|
    if data.is_a?(Hayabusa::Http_session::Contentgroup)
      data.length_written = @length_written
      data.content_length = @content_length
      data.chunked = @chunked
      data.write_to_socket
      
      @length_written += data.length_written
    elsif data.key?(:str)
      if data[:str].is_a?(Hash) and data[:str][:type] == :file
        File.open(data[:str][:path], "r") do |file|
          loop do
            begin
              buf = file.sysread(16384)
            rescue EOFError
              break
            end
            
            add_to_length_written(buf.bytesize)
            
            if @chunked
              @socket.write("#{buf.length.to_s(16)}#{NL}#{buf}#{NL}")
            else
              @socket.write(buf)
            end
          end
        end
      else
        loop do
          break if data[:done] and data[:str].size <= 0 
          sleep 0.1 while data[:str].size < 512 and !data[:done]
          
          str = nil
          @mutex.synchronize do
            str = data[:str].bytes
            data[:str] = ""
          end
          
          #512 could take a long time for big pages. 16384 seems to be an optimal number.
          str.each_slice(16384) do |slice|
            buf = slice.pack("C*")
            add_to_length_written(buf.bytesize)
            
            if @chunked
              @socket.write("#{buf.length.to_s(16)}#{NL}#{buf}#{NL}")
            else
              @socket.write(buf)
            end
          end
        end
      end
    else
      raise "Unknown object: '#{data.class.name}'."
    end
  end
  
  count += 1
end