From 556747d169cdeea1f1e031d8e85cbbd563e98306 Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Wed, 14 Sep 2022 11:35:19 -0400 Subject: [PATCH] Support running with --enable=frozen-string-literal In apps that run with --enable=frozen-string-literal we can get a FrozenError. Using String.new instead of '' fixes that without breaking it for anyone else. When running the specs with frozen strings (RUBYOPT="--enable=frozen-string-literal" rake), there were three files that generate errors. I added the magic comment: # frozen-string-literal: false to those to work both ways. I have a related PR on http-2 to get all the specs passing: https://github.com/igrigorik/http-2/pull/161 --- lib/net-http2/socket.rb | 5 +++-- lib/net-http2/stream.rb | 2 +- spec/api/errors_spec.rb | 1 + spec/api/sending_async_requests_spec.rb | 1 + spec/support/dummy_server.rb | 1 + 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/net-http2/socket.rb b/lib/net-http2/socket.rb index 3628647..10ae003 100644 --- a/lib/net-http2/socket.rb +++ b/lib/net-http2/socket.rb @@ -70,7 +70,8 @@ def self.proxy_tcp_socket(uri, options) # So we’ll keep HTTP/1.1 http_version = '1.1' - buf = "CONNECT #{uri.host}:#{uri.port} HTTP/#{http_version}\r\n" + buf = String.new + buf << "CONNECT #{uri.host}:#{uri.port} HTTP/#{http_version}\r\n" buf << "Host: #{uri.host}:#{uri.port}\r\n" if proxy_user credential = ["#{proxy_user}:#{proxy_pass}"].pack('m') @@ -87,7 +88,7 @@ def self.proxy_tcp_socket(uri, options) private def self.validate_proxy_response!(socket) - result = '' + result = String.new loop do line = socket.gets break if !line || line.strip.empty? diff --git a/lib/net-http2/stream.rb b/lib/net-http2/stream.rb index c9264a9..a8f5c16 100644 --- a/lib/net-http2/stream.rb +++ b/lib/net-http2/stream.rb @@ -5,7 +5,7 @@ class Stream def initialize(options={}) @h2_stream = options[:h2_stream] @headers = {} - @data = '' + @data = String.new @request = nil @async = false @completed = false diff --git a/spec/api/errors_spec.rb b/spec/api/errors_spec.rb index f7b671d..4fd26d8 100644 --- a/spec/api/errors_spec.rb +++ b/spec/api/errors_spec.rb @@ -1,3 +1,4 @@ +# frozen-string-literal: false require 'spec_helper' describe "Errors" do diff --git a/spec/api/sending_async_requests_spec.rb b/spec/api/sending_async_requests_spec.rb index 5ed7d6e..7e8de89 100644 --- a/spec/api/sending_async_requests_spec.rb +++ b/spec/api/sending_async_requests_spec.rb @@ -1,3 +1,4 @@ +# frozen-string-literal: false require 'spec_helper' describe "Sending async requests" do diff --git a/spec/support/dummy_server.rb b/spec/support/dummy_server.rb index 811cf38..338059f 100644 --- a/spec/support/dummy_server.rb +++ b/spec/support/dummy_server.rb @@ -1,3 +1,4 @@ +# frozen-string-literal: false module NetHttp2 module Dummy