Repository: puma/puma
File: lib/puma/request.rb (Ruby)

Code before:
      if @early_hints
        env[EARLY_HINTS] = lambda { |headers|
          begin
            fast_write io, str_early_hints(headers)
          rescue ConnectionError => e
            @log_writer.debug_error e
            # noop, if we lost the socket we just won't send the early hints
          end
        }
      end

      req_env_post_parse env

      # A rack extension. If the app writes #call'ables to this
      # array, we will invoke them when the request is done.
      #
      after_reply = env[RACK_AFTER_REPLY] = []

      begin
        begin
          if env[REQUEST_METHOD] != CONNECT
            status, headers, res_body = @thread_pool.with_force_shutdown do
              @app.call(env)
            end
          else
            @log_writer.log "CONNECT method used, but it's not supported by puma."
            status, headers, res_body = [501, {}, ["CONNECT method is not supported"]]
          end

          return :async if client.hijacked

          status = status.to_i

          if status == -1
            unless headers.empty? and res_body == []
              raise "async response must have empty headers and body"
            end

            return :async
          end
        rescue ThreadPool::ForceShutdown => e
          @log_writer.unknown_error e, client, "Rack app"
          @log_writer.log "Detected force shutdown of a thread"

          status, headers, res_body = lowlevel_error(e, env, 503)
        rescue Exception => e
          @log_writer.unknown_error e, client, "Rack app"

          status, headers, res_body = lowlevel_error(e, env, 500)
        end


Review comment (suggestion):
Actually, the [text on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501) suggests a little different logic is possible:

```ruby 
if ALLOWED_HTTP_METHODS.include?(env[REQUEST_METHOD])
  # call app 
else 
  # return 501
end
```

Code after:
      if @early_hints
        env[EARLY_HINTS] = lambda { |headers|
          begin
            fast_write io, str_early_hints(headers)
          rescue ConnectionError => e
            @log_writer.debug_error e
            # noop, if we lost the socket we just won't send the early hints
          end
        }
      end

      req_env_post_parse env

      # A rack extension. If the app writes #call'ables to this
      # array, we will invoke them when the request is done.
      #
      after_reply = env[RACK_AFTER_REPLY] = []

      begin
        begin
          if SUPPORTED_HTTP_METHODS.include?(env[REQUEST_METHOD])
            status, headers, res_body = @thread_pool.with_force_shutdown do
              @app.call(env)
            end
          else
            @log_writer.log "Unsupported HTTP method used: #{env[REQUEST_METHOD]}"
            status, headers, res_body = [501, {}, ["#{env[REQUEST_METHOD]} method is not supported"]]
          end

          return :async if client.hijacked

          status = status.to_i

          if status == -1
            unless headers.empty? and res_body == []
              raise "async response must have empty headers and body"
            end

            return :async
          end
        rescue ThreadPool::ForceShutdown => e
          @log_writer.unknown_error e, client, "Rack app"
          @log_writer.log "Detected force shutdown of a thread"

          status, headers, res_body = lowlevel_error(e, env, 503)
        rescue Exception => e
          @log_writer.unknown_error e, client, "Rack app"

          status, headers, res_body = lowlevel_error(e, env, 500)
        end