yahns.git  about / heads / tags
sleepy, multi-threaded, non-blocking application server for Ruby
blob 2e719eb0ebfd2b2d604a190660c49e616f1907b0 8731 bytes (raw)
$ git show HEAD:lib/yahns/http_response.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
 
# -*- encoding: binary -*-
# Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative 'stream_file'
require_relative 'wbuf_str'
require_relative 'chunk_body'

# Writes a Rack response to your client using the HTTP/1.1 specification.
# You use it by simply doing:
#
#   opt = http_response_prep(env)
#   res = rack_app.call(env)
#   http_response_write(res, opt)
#
# Most header correctness (including Content-Length and Content-Type)
# is the job of Rack, with the exception of the "Date" header.
module Yahns::HttpResponse # :nodoc:
  include Unicorn::HttpResponse

  if defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
    MTX = Mutex.new
    def httpdate
      MTX.synchronize { super }
    end
  end

  # avoid GC overhead for frequently used-strings/objects:
  CCC_RESPONSE_START = [ 'HTTP', '/1.1 ' ]

  # no point in using one without the other, these have been in Linux
  # for ages
  if Socket.const_defined?(:MSG_MORE) && Socket.const_defined?(:MSG_DONTWAIT)
    MSG_MORE = Socket::MSG_MORE
    MSG_DONTWAIT = Socket::MSG_DONTWAIT
  else
    MSG_MORE = 0
    MSG_DONTWAIT = 0

    def kgio_syssend(buf, flags)
      kgio_trywrite(buf)
    end
  end

  def response_start
    @hs.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
  end

  def response_wait_write(rv) # rv = [:wait_writable | :wait_readable ]
    k = self.class
    ok = __send__(rv, k.client_timeout) and return ok
    k.logger.info("fd=#{fileno} ip=#@kgio_addr timeout on :#{rv} after "\
                  "#{k.client_timeout}s")
    false
  end

  def err_response(code)
    "#{response_start}#{code} #{Rack::Utils::HTTP_STATUS_CODES[code]}\r\n\r\n"
  end

  def response_header_blocked(header, body, alive, offset, count)
    if body.respond_to?(:to_path) && count
      alive = Yahns::StreamFile.new(body, alive, offset, count)
      body = nil
    end
    wbuf = Yahns::Wbuf.new(body, alive)
    rv = wbuf.wbuf_write(self, header)
    if body && ! alive.respond_to?(:call) # skip body.each if hijacked
      body.each { |chunk| rv = wbuf.wbuf_write(self, chunk) }
    end
    wbuf_maybe(wbuf, rv)
  end

  def wbuf_maybe(wbuf, rv)
    case rv # wbuf_write return value
    when nil # all done
      case rv = wbuf.wbuf_close(self)
      when :ignore # hijacked
        @state = rv
      when Yahns::StreamFile
        @state = rv
        :wait_writable
      when true, false
        http_response_done(rv)
      end
    else
      @state = wbuf
      rv
    end
  end

  def http_response_done(alive)
    @input = @input.close if @input
    if alive
      # @hs.buf will have data if the client pipelined
      if @hs.buf.empty?
        @state = :headers
        :wait_readable
      else
        @state = :pipelined
        # we shouldn't start processing the application again until we know
        # the socket is writable for the response
        :wait_writable
      end
    else
      # shutdown is needed in case the app forked, we rescue here since
      # StreamInput may issue shutdown as well
      shutdown rescue nil
      :close
    end
  end

  def kv_str(buf, key, value)
    if value.include?("\n".freeze)
      # avoiding blank, key-only cookies with /\n+/
      value.split(/\n+/).each { |v| buf << "#{key}: #{v}\r\n" }
    else
      buf << "#{key}: #{value}\r\n"
    end
  end

  # writes the rack_response to socket as an HTTP response
  # returns :wait_readable, :wait_writable, :forget, or nil
  def http_response_write(res, opt)
    status, headers, body = res
    offset = 0
    count = hijack = clen = nil
    alive = @hs.next? && self.class.persistent_connections
    flags = MSG_DONTWAIT
    term = false
    hdr_only, chunk_ok = opt

    code = status.to_i
    hdr_only ||= Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(code)
    msg = Rack::Utils::HTTP_STATUS_CODES[code]
    buf = "#{response_start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
          "Date: #{httpdate}\r\n".dup
    headers.each do |key, value|
      case key
      when %r{\ADate\z}i
        next
      when %r{\AContent-Range\z}i
        if %r{\Abytes (\d+)-(\d+)/\d+\z} =~ value
          offset = $1.to_i
          count = $2.to_i - offset + 1
        end
        kv_str(buf, key, value)
      when %r{\AConnection\z}i
        # allow Rack apps to tell us they want to drop the client
        alive = false if value =~ /\bclose\b/i
      when %r{\AContent-Length\z}i
        term = true
        clen = value.to_i
        flags |= MSG_MORE if clen > 0 && !hdr_only
        kv_str(buf, key, value)
      when %r{\ATransfer-Encoding\z}i
        term = true if value =~ /\bchunked\b/i
        kv_str(buf, key, value)
      when "rack.hijack"
        hijack = value
      else
        kv_str(buf, key, value)
      end
    end
    count ||= clen

    if !term && chunk_ok && !hdr_only
      term = true
      body = Yahns::ChunkBody.new(body, opt)
      buf << "Transfer-Encoding: chunked\r\n".freeze
    end
    alive &&= (term || hdr_only)
    buf << (alive ? "Connection: keep-alive\r\n\r\n".freeze
                  : "Connection: close\r\n\r\n".freeze)
    case rv = kgio_syssend(buf, flags)
    when nil # all done, likely
      buf.clear
      buf = nil # recycle any memory we used ASAP
      break
    when String
      flags = MSG_DONTWAIT
      buf = rv # unlikely, hope the skb grows
    when :wait_writable, :wait_readable # unlikely
      if self.class.output_buffering
        alive = hijack ? hijack : alive
        rv = response_header_blocked(buf, body, alive, offset, count)
        body = nil # ensure we do not close body in ensure
        return rv
      else
        response_wait_write(rv) or return :close
      end
    end while @hs.headers?

    return response_hijacked(hijack) if hijack
    return http_response_done(alive) if hdr_only

    if body.respond_to?(:to_path) && count
      @state = body = Yahns::StreamFile.new(body, alive, offset, count)
      return step_write
    end

    headers = wbuf = rv = nil
    body.each do |x|
      if wbuf
        rv = wbuf.wbuf_write(self, x)
      else
        case rv = String === x ? kgio_trywrite(x) : kgio_trywritev(x)
        when nil # all done, likely and good!
          break
        when String, Array
          x = rv # hope the skb grows when we loop into the trywrite
        when :wait_writable, :wait_readable
          if self.class.output_buffering
            wbuf = Yahns::Wbuf.new(body, alive)
            rv = wbuf.wbuf_write(self, x)
            break
          else
            response_wait_write(rv) or return :close
          end
        end while true
      end
    end

    # if we buffered the write body, we must return :wait_writable
    # (or :wait_readable for SSL) and hit Yahns::HttpClient#step_write
    if wbuf
      body = nil # ensure we do not close the body in ensure
      wbuf_maybe(wbuf, rv)
    else
      http_response_done(alive)
    end
  ensure
    body.respond_to?(:close) and body.close
  end

  # returns nil on success
  # :wait_readable/:wait_writable/:close for epoll
  def do_ccc
    @hs.response_start_sent = true
    wbuf = nil
    rv = nil
    CCC_RESPONSE_START.each do |buf|
      if wbuf
        wbuf << buf
      else
        case rv = kgio_trywrite(buf)
        when nil
          break
        when String
          buf = rv
        when :wait_writable, :wait_readable
          if self.class.output_buffering
            wbuf = buf.dup
            @state = Yahns::WbufStr.new(wbuf, :ccc_done)
            break
          else
            response_wait_write(rv) or return :close
          end
        end while true
      end
    end
    rv
  end

  # only used if input_buffering is true (not :lazy or false)
  # input_buffering==:lazy/false gives control to the app
  # returns nil on success
  # returns :close, :wait_writable, or :wait_readable
  def http_100_response(env)
    env.delete('HTTP_EXPECT'.freeze) =~ /\A100-continue\z/i or return
    buf = @hs.response_start_sent ? "100 Continue\r\n\r\nHTTP/1.1 ".freeze
                                  : "HTTP/1.1 100 Continue\r\n\r\n".freeze

    case rv = kgio_trywrite(buf)
    when String
      buf = rv
    when :wait_writable, :wait_readable
      if self.class.output_buffering
        @state = Yahns::WbufStr.new(buf, :r100_done)
        return rv
      else
        response_wait_write(rv) or return :close
      end
    else
      return rv
    end while true
  end

  # must be called before app dispatch, since the app can
  # do all sorts of nasty things to env
  def http_response_prep(env)
    [ env['REQUEST_METHOD'] == 'HEAD'.freeze, # hdr_only
      env['HTTP_VERSION'] == 'HTTP/1.1'.freeze ] # chunk_ok
  end
end

git clone git://yhbt.net/yahns.git
git clone https://yhbt.net/yahns.git