Class: Baykit::BayServer::Tours::TourReq

Inherits:
Object
  • Object
show all
Includes:
Protocol, Util, Util::Reusable
Defined in:
lib/baykit/bayserver/tours/tour_req.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tur) ⇒ TourReq

Returns a new instance of TourReq.



58
59
60
61
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 58

def initialize(tur)
  @headers = Headers.new()
  @tour = tur
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



55
56
57
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 55

def available
  @available
end

#bytes_consumedObject (readonly)

Returns the value of attribute bytes_consumed.



51
52
53
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 51

def bytes_consumed
  @bytes_consumed
end

#bytes_limitObject (readonly)

Returns the value of attribute bytes_limit.



52
53
54
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 52

def bytes_limit
  @bytes_limit
end

#bytes_postedObject (readonly)

Request content info

Handling request contents



50
51
52
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 50

def bytes_posted
  @bytes_posted
end

#charsetObject

Returns the value of attribute charset.



42
43
44
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 42

def charset
  @charset
end

#consume_listenerObject (readonly)

Returns the value of attribute consume_listener.



54
55
56
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 54

def consume_listener
  @consume_listener
end

#content_handlerObject (readonly)

Returns the value of attribute content_handler.



44
45
46
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 44

def content_handler
  @content_handler
end

#endedObject (readonly)

Returns the value of attribute ended.



56
57
58
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 56

def ended
  @ended
end

#headersObject (readonly)

Returns the value of attribute headers.



24
25
26
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 24

def headers
  @headers
end

#keyObject (readonly)

request id in FCGI or stream id in HTTP/2



18
19
20
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 18

def key
  @key
end

#methodObject

Returns the value of attribute method.



22
23
24
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 22

def method
  @method
end

#path_infoObject

Returns the value of attribute path_info.



28
29
30
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 28

def path_info
  @path_info
end

#protocolObject

Returns the value of attribute protocol.



21
22
23
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 21

def protocol
  @protocol
end

#query_stringObject

Returns the value of attribute query_string.



27
28
29
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 27

def query_string
  @query_string
end

#remote_addressObject

Returns the value of attribute remote_address.



36
37
38
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 36

def remote_address
  @remote_address
end

#remote_host_funcObject

function



38
39
40
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 38

def remote_host_func
  @remote_host_func
end

#remote_passObject

Returns the value of attribute remote_pass.



34
35
36
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 34

def remote_pass
  @remote_pass
end

#remote_portObject

Returns the value of attribute remote_port.



37
38
39
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 37

def remote_port
  @remote_port
end

#remote_userObject

Returns the value of attribute remote_user.



33
34
35
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 33

def remote_user
  @remote_user
end

#req_hostObject

from Host header



30
31
32
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 30

def req_host
  @req_host
end

#req_portObject

from Host header



31
32
33
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 31

def req_port
  @req_port
end

#rewritten_uriObject

set if URI is rewritten



26
27
28
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 26

def rewritten_uri
  @rewritten_uri
end

#script_nameObject

Returns the value of attribute script_name.



29
30
31
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 29

def script_name
  @script_name
end

#server_addressObject

Returns the value of attribute server_address.



39
40
41
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 39

def server_address
  @server_address
end

#server_nameObject

Returns the value of attribute server_name.



41
42
43
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 41

def server_name
  @server_name
end

#server_portObject

Returns the value of attribute server_port.



40
41
42
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 40

def server_port
  @server_port
end

#tourObject (readonly)

Request Header info



17
18
19
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 17

def tour
  @tour
end

#uriObject

Returns the value of attribute uri.



20
21
22
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 20

def uri
  @uri
end

Instance Method Details

#abortObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 211

def abort()
  if !@tour.preparing?
    BayLog.debug("%s cannot abort non-preparing tour", @tour)
    return false
  end

  BayLog.debug("%s abort", @tour)
  if @tour.aborted?
    raise Sink.new("tour has already aborted")
  end

  aborted = true
  if @tour.running? && @content_handler != nil
    aborted = @content_handler.on_abort(@tour)
  end

  if aborted
    @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
  end

  return aborted
end

#buffer_availableObject



245
246
247
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 245

def buffer_available()
  return @bytes_posted - @bytes_consumed < BayServer.harbor.tour_buffer_size
end

#consumed(chk_id, length) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 187

def consumed(chk_id, length)
  @tour.check_tour_id(chk_id)
  BayLog.debug("%s content_consumed: len=%d posted=%d", @tour, length, @bytes_posted)
  if @consume_listener == nil
    raise Sink.new("Request consume listener is null")
  end

  @bytes_consumed += length
  BayLog.debug("%s reqConsumed: len=%d posted=%d limit=%d consumed=%d",
               @tour, length, @bytes_posted, @bytes_limit, @bytes_consumed)

  resume = false
  old_available = @available
  if buffer_available()
    @available = true
  end

  if !old_available && @available
    BayLog.debug("%s request available (^o^): posted=%d consumed=%d", self,  @bytes_posted, @bytes_consumed);
    resume = true
  end
  @consume_listener.call(length, resume)
end

#end_content(check_id) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 171

def end_content(check_id)
  @tour.check_tour_id(check_id)
  if @ended
    raise Sink.new("#{@tour} Request content is already ended")
  end

  if @bytes_limit >= 0 && @bytes_posted != @bytes_limit
    raise ProtocolException.new("Read data exceed content-length: #{@bytes_posted}/#{@bytes_limit}")
  end

  if @content_handler != nil
    @content_handler.on_end_content(@tour)
  end
  @ended = true
end

#init(key) ⇒ Object



63
64
65
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 63

def init(key)
  @key = key
end

#post_content(check_id, data, start, len) ⇒ Object



125
126
127
128
129
130
131
132
133
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
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 125

def post_content(check_id, data, start, len)
  @tour.check_tour_id(check_id)

  if !@tour.running?
    BayLog.debug("%s tour is not running.", @tour)
    return true
  end

  if @content_handler == nil
    BayLog.warn("%s content read, but no content handler", tour)
    return true
  end

  if @consume_listener == nil
    raise Sink.new("Request consume listener is null")
  end

  if @bytes_posted + len > @bytes_limit
    raise ProtocolException.new("Read data exceed content-length: %d/%d", @bytes_posted + len, @bytes_limit)
  end

  # If has error, only read content. (Do not call content handler)
  if @tour.error == nil
    @content_handler.on_read_content(@tour, data, start, len)
  end
  @bytes_posted += len

  BayLog.debug("%s read content: len=%d posted=%d limit=%d consumed=%d",
               @tour, len, @bytes_posted, @bytes_limit, @bytes_consumed)

  if @tour.error == nil
    return true
  end

  old_available = @available
  if !buffer_available()
    @available = false
  end

  if old_available && !@available
    BayLog.debug("%s request unavailable (_ _): posted=%d consumed=%d", self,  @bytes_posted, @bytes_consumed);
  end

  return @available
end

#remote_hostObject

other methods



110
111
112
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 110

def remote_host()
  return @remote_host_func.call()
end

#resetObject

Implements Reusable



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 70

def reset()
  @headers.clear

  @uri = nil
  @method = nil
  @protocol = nil
  @bytes_posted = 0
  @bytes_consumed = 0
  @bytes_limit = 0

  @key = 0

  @rewritten_uri = nil
  @query_string = nil
  @path_info = nil
  @script_name = nil
  @req_host = nil
  @req_port = 0
  @remote_user = nil
  @remote_pass = nil

  @remote_address = nil
  @remote_port = 0
  @remote_host_func = nil
  @server_address = nil
  @server_port = 0
  @server_name = nil

  @charset = nil
  @available = false
  @content_handler = nil
  @consume_listener = nil
  @ended = false

end

#set_consume_listener(limit, &listener) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 114

def set_consume_listener(limit, &listener)
  if limit < 0
    raise Sink.new("invalid limit")
  end
  @bytes_limit = limit
  @consume_listener = listener
  @bytes_posted = 0
  @bytes_consumed = 0
  @available = true
end

#set_content_handler(hnd) ⇒ Object



234
235
236
237
238
239
240
241
242
243
# File 'lib/baykit/bayserver/tours/tour_req.rb', line 234

def set_content_handler(hnd)
  if hnd == nil
    raise Sink.new("nil")
  end
  if @content_handler != nil
    raise Sink.new("content handler already set")
  end

  @content_handler = hnd
end