Class: Spider::Response

Inherits:
Object show all
Defined in:
lib/spiderfw/controller/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



9
10
11
12
# File 'lib/spiderfw/controller/response.rb', line 9

def initialize()
    @headers = {}
    @cookies = Cookies.new
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



7
8
9
# File 'lib/spiderfw/controller/response.rb', line 7

def cookies
  @cookies
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/spiderfw/controller/response.rb', line 7

def headers
  @headers
end

#server_outputObject

Returns the value of attribute server_output.



7
8
9
# File 'lib/spiderfw/controller/response.rb', line 7

def server_output
  @server_output
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/spiderfw/controller/response.rb', line 6

def status
  @status
end

Instance Method Details

#body=(io) ⇒ Object



14
15
16
# File 'lib/spiderfw/controller/response.rb', line 14

def body=(io)
    @server_output.set_body_io(io)
end

#bufferObject



78
79
80
# File 'lib/spiderfw/controller/response.rb', line 78

def buffer
    @buffer
end

#buffer_until_lengthObject



106
107
108
109
110
111
# File 'lib/spiderfw/controller/response.rb', line 106

def buffer_until_length
    unless content_length
        start_buffering
        @buffer_until_length = true
    end
end

#buffering?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/spiderfw/controller/response.rb', line 52

def buffering?
    @prev_io != nil
end

#clear_bufferObject



82
83
84
# File 'lib/spiderfw/controller/response.rb', line 82

def clear_buffer
    @buffer = nil
end

#connection_keep_aliveObject



122
123
124
125
# File 'lib/spiderfw/controller/response.rb', line 122

def connection_keep_alive
    @headers['Connection'] = 'Keep-Alive'
    buffer_until_length unless Spider.conf.get('webserver.has_buffering_proxy')
end

#content_lengthObject



102
103
104
# File 'lib/spiderfw/controller/response.rb', line 102

def content_length
    @headers['Content-Length']
end

#content_length=(val) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/spiderfw/controller/response.rb', line 94

def content_length=(val)
    @headers['Content-Length'] = val
    if @buffer_until_length && val
        output_buffer
        @buffer_until_length = false
    end
end

#content_typeObject



90
91
92
# File 'lib/spiderfw/controller/response.rb', line 90

def content_type
    @headers['Content-Type']
end

#content_type=(val) ⇒ Object



86
87
88
# File 'lib/spiderfw/controller/response.rb', line 86

def content_type=(val)
    @headers['Content-Type'] = val
end

#do_not_buffer!Object



127
128
129
130
131
132
133
# File 'lib/spiderfw/controller/response.rb', line 127

def do_not_buffer!
    unless Spider.conf.get('webserver.has_buffering_proxy')
        @headers['Connection'] = 'Close'
    end
    output_buffer
    @buffer_until_length = false
end

#finish!Object



113
114
115
116
117
118
119
120
# File 'lib/spiderfw/controller/response.rb', line 113

def finish!
    if buffering?
        stop_buffering
        needs_length = @needs_length
        self.content_length = @buffer.length
        output_buffer unless needs_length
    end
end

#no_cookiesObject



23
24
25
# File 'lib/spiderfw/controller/response.rb', line 23

def no_cookies
    @no_cookies = true
end

#output_bufferObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/spiderfw/controller/response.rb', line 67

def output_buffer
    stop_buffering if buffering?
    return unless @buffer
    while d = @buffer.read(1024)
        $out << d
    end
    buffer = @buffer
    @buffer = nil
    buffer
end

#prepare_headersObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spiderfw/controller/response.rb', line 27

def prepare_headers
    unless @no_cookies
        @headers['Set-Cookie'] ||= []
        @cookies.each do |k, v|
            h = "#{k}=#{v}"
            h += '; expires='+v.expires.strftime("%a, %b %d %Y %H:%M:%S %Z") if (v.expires.respond_to?(:strftime))
            h += "; path=#{v.path}" if (v.path)
            h += "; domain=#{v.domain}" if (v.domain)
            h += "; secure" if (v.secure)
            @headers['Set-Cookie'] << h
        end
    end
    Spider::Logger.debug("HEADERS:")
    Spider::Logger.debug(@headers)
end

#register(key, val) ⇒ Object



18
19
20
21
# File 'lib/spiderfw/controller/response.rb', line 18

def register(key, val)
    instance_variable_set("@#{key}", val)
    self.class.send(:attr_accessor, key) # FIXME: threadsafety
end

#start_bufferingObject



56
57
58
59
# File 'lib/spiderfw/controller/response.rb', line 56

def start_buffering
    @buffer = StringIO.new
    @prev_io = ThreadOut.output_to(@buffer)
end

#stop_bufferingObject



61
62
63
64
65
# File 'lib/spiderfw/controller/response.rb', line 61

def stop_buffering
    ThreadOut.output_to(@prev_io)
    @prev_io = nil
    @buffer.rewind
end