Skip to content

Commit

Permalink
Merge pull request #102 from Shopify/fstr
Browse files Browse the repository at this point in the history
Ensure compatibility with `--enable-frozen-string-literal`
  • Loading branch information
casperisfine authored Mar 21, 2024
2 parents 5540cf8 + 69bde31 commit a313378
Show file tree
Hide file tree
Showing 78 changed files with 153 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ jobs:
- name: Install packages
run: sudo apt-get install -y ragel socat netcat

- name: Tests
- name: Tests ${{ matrix.rubyopt }}
run: bundle exec rake
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ DEPENDENCIES
rake-compiler

BUNDLED WITH
2.3.22
2.3.27
1 change: 1 addition & 0 deletions benchmark/cow_benchmark.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require "net/http"

app_path = File.expand_path('../examples/constant_caches.ru', __dir__)
Expand Down
1 change: 1 addition & 0 deletions examples/constant_caches.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require_relative "../lib/pitchfork/mem_info"

module App
Expand Down
1 change: 1 addition & 0 deletions examples/echo.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# Example application that echoes read data back to the HTTP client.
# This emulates the old echo protocol people used to run.
#
Expand Down
1 change: 1 addition & 0 deletions examples/hello.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
run lambda { |env|
/\A100-continue\z/i =~ env['HTTP_EXPECT'] and return [100, {}, []]
body = "Hello World!\n"
Expand Down
1 change: 1 addition & 0 deletions examples/pitchfork.conf.minimal.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# Minimal sample configuration file for Pitchfork

# listen 2007 # by default Pitchfork listens on port 8080
Expand Down
1 change: 1 addition & 0 deletions examples/pitchfork.conf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# Sample verbose configuration file for Pitchfork

# Use at least one worker per core if you're on a dedicated server,
Expand Down
1 change: 1 addition & 0 deletions exe/pitchfork
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# -*- encoding: binary -*-
require 'pitchfork/launcher'
require 'optparse'
Expand Down
1 change: 1 addition & 0 deletions ext/pitchfork_http/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
require 'mkmf'

have_const("PR_SET_CHILD_SUBREAPER", "sys/prctl.h")
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
require 'etc'
require 'stringio'
require 'raindrops'
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/app/old_rails/static.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
# :enddoc:
# This code is based on the original Rails handler in Mongrel
# Copyright (c) 2005 Zed A. Shaw
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/children.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

module Pitchfork
# This class keep tracks of the state of all the master children.
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/configurator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
require 'logger'

module Pitchfork
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/const.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

module Pitchfork
module Const # :nodoc:
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/flock.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'tempfile'

module Pitchfork
Expand Down
13 changes: 7 additions & 6 deletions lib/pitchfork/http_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
# :enddoc:
# no stable API here

Expand All @@ -19,10 +20,10 @@ class HttpParser
"SERVER_SOFTWARE" => "Pitchfork #{Pitchfork::Const::UNICORN_VERSION}"
}

NULL_IO = StringIO.new("")
NULL_IO = StringIO.new.binmode

# :stopdoc:
HTTP_RESPONSE_START = [ 'HTTP'.freeze, '/1.1 '.freeze ]
HTTP_RESPONSE_START = [ 'HTTP', '/1.1 ' ]
EMPTY_ARRAY = [].freeze
@@input_class = Pitchfork::TeeInput
@@check_client_connection = false
Expand Down Expand Up @@ -104,7 +105,7 @@ def call
end

def hijacked?
env.include?('rack.hijack_io'.freeze)
env.include?('rack.hijack_io')
end

if Raindrops.const_defined?(:TCP_Info)
Expand Down Expand Up @@ -199,11 +200,11 @@ def self.is_chunked?(v) # :nodoc:
val.downcase!
end

if vals.pop == 'chunked'.freeze
return true unless vals.include?('chunked'.freeze)
if vals.pop == 'chunked'
return true unless vals.include?('chunked')
raise Pitchfork::HttpParserError, 'double chunked', []
end
return false unless vals.include?('chunked'.freeze)
return false unless vals.include?('chunked')
raise Pitchfork::HttpParserError, 'chunked not last', []
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/pitchfork/http_response.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
# :enddoc:

module Pitchfork
Expand Down Expand Up @@ -48,10 +49,10 @@ def http_response_write(socket, status, headers, body,
if headers
code = status.to_i
msg = STATUS_CODES[code]
start = req.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
start = req.response_start_sent ? '' : 'HTTP/1.1 '
buf = "#{start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
"Date: #{httpdate}\r\n" \
"Connection: close\r\n"
"Connection: close\r\n".b
headers.each do |key, value|
case key
when %r{\A(?:Date|Connection)\z}i
Expand Down
8 changes: 5 additions & 3 deletions lib/pitchfork/http_server.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

require 'pitchfork/pitchfork_http'
require 'pitchfork/flock'
require 'pitchfork/soft_timeout'
Expand Down Expand Up @@ -91,7 +93,7 @@ def extend_deadline(extra_time)
# in new projects
LISTENERS = []

NOOP = '.'.freeze
NOOP = '.'

# :startdoc:
# This Hash is considered a stable interface and changing its contents
Expand Down Expand Up @@ -696,7 +698,7 @@ def handle_error(client, e)

def e103_response_write(client, headers)
rss = @request.response_start_sent
buf = rss ? "103 Early Hints\r\n" : "HTTP/1.1 103 Early Hints\r\n"
buf = (rss ? "103 Early Hints\r\n" : "HTTP/1.1 103 Early Hints\r\n").b
headers.each { |key, value| append_header(buf, key, value) }
buf << (rss ? "\r\nHTTP/1.1 ".freeze : "\r\n".freeze)
client.write(buf)
Expand All @@ -710,7 +712,7 @@ def e100_response_write(client, env)
client.write(@request.response_start_sent ?
"100 Continue\r\n\r\nHTTP/1.1 ".freeze :
"HTTP/1.1 100 Continue\r\n\r\n".freeze)
env.delete('HTTP_EXPECT'.freeze)
env.delete('HTTP_EXPECT')
end

# once a client is accepted, it is processed in its entirety here
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/launcher.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

# :enddoc:
$stdout.sync = $stderr.sync = true
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/message.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

# :stopdoc:
module Pitchfork
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/select_waiter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module Pitchfork
# fallback for non-Linux and Linux <4.5 systems w/o EPOLLEXCLUSIVE
class SelectWaiter # :nodoc:
Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/socket_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
# :enddoc:
require 'socket'

Expand Down
11 changes: 6 additions & 5 deletions lib/pitchfork/stream_input.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

module Pitchfork
# When processing uploads, pitchfork may expose a StreamInput object under
Expand All @@ -17,7 +18,7 @@ def initialize(socket, request) # :nodoc:
@socket = socket
@parser = request
@buf = request.buf
@rbuf = ''
@rbuf = +''
@bytes_read = 0
filter_body(@rbuf, @buf) unless @buf.empty?
end
Expand All @@ -41,7 +42,7 @@ def initialize(socket, request) # :nodoc:
# ios.read(length [, buffer]) will return immediately if there is
# any data and only block when nothing is available (providing
# IO#readpartial semantics).
def read(length = nil, rv = '')
def read(length = nil, rv = ''.b)
if length
if length <= @rbuf.size
length < 0 and raise ArgumentError, "negative length #{length} given"
Expand Down Expand Up @@ -79,16 +80,16 @@ def read(length = nil, rv = '')
# unlike IO#gets.
def gets(sep = $/)
if sep.nil?
read_all(rv = '')
read_all(rv = ''.b)
return rv.empty? ? nil : rv
end
re = /\A(.*?#{Regexp.escape(sep)})/

begin
@rbuf.sub!(re, '') and return $1
@rbuf.sub!(re, ''.b) and return $1
return @rbuf.empty? ? nil : @rbuf.slice!(0, @rbuf.size) if eof?
@socket.readpartial(@@io_chunk_size, @buf) or eof!
filter_body(once = '', @buf)
filter_body(once = ''.b, @buf)
@rbuf << once
end while true
end
Expand Down
5 changes: 3 additions & 2 deletions lib/pitchfork/tee_input.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true

module Pitchfork
# Acts like tee(1) on an input input to provide a input-like stream
Expand Down Expand Up @@ -42,7 +43,7 @@ def initialize(socket, request) # :nodoc:
@len = request.content_length
super
@tmp = @len && @len <= @@client_body_buffer_size ?
StringIO.new("") : new_tmpio
StringIO.new.binmode : new_tmpio
end

# :call-seq:
Expand Down Expand Up @@ -121,7 +122,7 @@ def rewind

# consumes the stream of the socket
def consume!
junk = ""
junk = "".b
nil while read(@@io_chunk_size, junk)
end

Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/tmpio.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
# :stopdoc:
require 'tmpdir'

Expand Down
1 change: 1 addition & 0 deletions lib/pitchfork/worker.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
# frozen_string_literal: true
require 'pitchfork/shared_memory'

module Pitchfork
Expand Down
1 change: 1 addition & 0 deletions test/integration/apps/chunked.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class ChunkedBody
def each(&block)
('A'..'Z').each do |char|
Expand Down
1 change: 1 addition & 0 deletions test/integration/apps/stream.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class ChunkedBody
def each(&block)
('A'..'Z').each do |char|
Expand Down
1 change: 1 addition & 0 deletions test/integration/bin/content-md5-put
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# -*- encoding: binary -*-
# simple chunked HTTP PUT request generator (and just that),
# it reads stdin and writes to stdout so socat can write to a
Expand Down
1 change: 1 addition & 0 deletions test/integration/bin/sha1sum.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# frozen_string_literal: true
# Reads from stdin and outputs the SHA1 hex digest of the input

require 'digest/sha1'
Expand Down
1 change: 1 addition & 0 deletions test/integration/bin/unused_listen
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# -*- encoding: binary -*-
# this is to remain compatible with the unused_port function in the
# Unicorn test/test_helper.rb file
Expand Down
1 change: 1 addition & 0 deletions test/integration/broken-app.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# we do not want Rack::Lint or anything to protect us
use Rack::ContentLength
use Rack::ContentType, "text/plain"
Expand Down
1 change: 1 addition & 0 deletions test/integration/env.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run lambda { |env| [ 200, {}, [ env.inspect << "\n" ] ] }
1 change: 1 addition & 0 deletions test/integration/fails-rack-lint.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# This rack app returns a header with key named "status", which will cause
# Rack::Lint to throw an exception if it is present. This
# is used to check whether Rack::Lint is in the stack or not.
Expand Down
1 change: 1 addition & 0 deletions test/integration/fork_unsafe.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run lambda { |env|
Expand Down
1 change: 1 addition & 0 deletions test/integration/heartbeat-timeout.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
headers = { 'content-type' => 'text/plain' }
run lambda { |env|
Expand Down
1 change: 1 addition & 0 deletions test/integration/info.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run lambda { |env|
Expand Down
1 change: 1 addition & 0 deletions test/integration/listener_names.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run(lambda { |_| [ 200, {}, [ Pitchfork.listener_names.inspect ] ] })
1 change: 1 addition & 0 deletions test/integration/pid.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run lambda { |env| [ 200, {}, [ "#$$\n" ] ] }
1 change: 1 addition & 0 deletions test/integration/rack-input-tests.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# SHA1 checksum generator
require 'digest/sha1'
use Rack::ContentLength
Expand Down
1 change: 1 addition & 0 deletions test/integration/sleep.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run lambda { |env|
Expand Down
1 change: 1 addition & 0 deletions test/integration/t0013.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#\ -E none
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
Expand Down
1 change: 1 addition & 0 deletions test/integration/t0014.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#\ -E none
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
Expand Down
1 change: 1 addition & 0 deletions test/integration/t0116.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#\ -E none
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
Expand Down
1 change: 1 addition & 0 deletions test/integration/t0301.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#\-N --debug
run(lambda do |env|
case env['PATH_INFO']
Expand Down
1 change: 1 addition & 0 deletions test/integration/t2000.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
use Rack::ContentLength
use Rack::ContentType, "text/plain"
run lambda { |env|
Expand Down
1 change: 1 addition & 0 deletions test/integration/test_boot.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'integration_test_helper'

class TestBoot < Pitchfork::IntegrationTest
Expand Down
Loading

0 comments on commit a313378

Please sign in to comment.