Skip to content

Commit

Permalink
Merge pull request #542 from missive/bugfix/content-length-multibyte
Browse files Browse the repository at this point in the history
Support migration output messages containing multibyte characters
  • Loading branch information
st0012 authored Dec 10, 2024
2 parents e28e268 + 94f0024 commit 955dc27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def send_message(request, **params)
json = message.to_json

@mutex.synchronize do
@stdin.write("Content-Length: #{json.length}\r\n\r\n", json)
@stdin.write("Content-Length: #{json.bytesize}\r\n\r\n", json)
end
rescue Errno::EPIPE
# The server connection died
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Common
# Write a message to the client. Can be used for sending notifications to the editor
def send_message(message)
json_message = message.to_json
@stdout.write("Content-Length: #{json_message.length}\r\n\r\n#{json_message}")
@stdout.write("Content-Length: #{json_message.bytesize}\r\n\r\n#{json_message}")
end

# Log a message to the editor's output panel
Expand Down
12 changes: 12 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ def resolve_route_info(requirements)
)
end

test "send_message uses bytesize for content length with ASCII characters" do
@server.send(:send_message, { test: "hello" })
assert_equal "Content-Length: 16\r\n\r\n{\"test\":\"hello\"}", @stdout.string
end

test "send_message uses bytesize for content length with multibyte characters" do
@server.send(:send_message, { test: "こんにちは" }) # Japanese "hello"
expected = "Content-Length: 26\r\n\r\n"
expected += { test: "こんにちは" }.to_json.force_encoding(Encoding::ASCII_8BIT)
assert_equal expected, @stdout.string
end

private

def response
Expand Down

0 comments on commit 955dc27

Please sign in to comment.