Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Stomp content-length header should be in bytes #315

Merged
merged 1 commit into from

2 participants

@tjsingleton

In 1.9.x String#size doesn't return the number of bytes, but the number of characters. The Stomp protocol wants the number of bytes in the content-length header. Using String#size falls down when there are multibyte characters. I updated the header to use bytesize when it's available matching the form in EM::Connection#send_data.

It is recommended that SEND frames include a content-length header which is a byte count for the length of the message body."

http://docs.codehaus.org/display/STOMP/Protocol

I wasn't sure the best way to write a test for it. I'd be happy to update the test if given advice.

@tmm1 tmm1 merged commit 7a44d7c into from
@sodabrew sodabrew referenced this pull request from a commit in sodabrew/eventmachine
@ibc ibc Stomp content-length header should be in bytes (eventmachine#315) 457baa8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2012
  1. The content-length header should reflect the byte size, not number of…

    TJ Singleton authored
    … characters.
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 2 deletions.
  1. +5 −2 lib/em/protocols/stomp.rb
  2. +37 −0 tests/test_stomp.rb
View
7 lib/em/protocols/stomp.rb
@@ -104,12 +104,15 @@ def consume_line line
# @private
def send_frame verb, headers={}, body=""
+ body = body.to_s
ary = [verb, "\n"]
+ body_bytesize = body.bytesize if body.respond_to? :bytesize
+ body_bytesize ||= body.size
headers.each {|k,v| ary << "#{k}:#{v}\n" }
- ary << "content-length: #{body.to_s.length}\n"
+ ary << "content-length: #{body_bytesize}\n"
ary << "content-type: text/plain; charset=UTF-8\n" unless headers.has_key? 'content-type'
ary << "\n"
- ary << body.to_s
+ ary << body
ary << "\0"
send_data ary.join
end
View
37 tests/test_stomp.rb
@@ -0,0 +1,37 @@
+require 'em_test_helper'
+
+class TestStomp < Test::Unit::TestCase
+ CONTENT_LENGTH_REGEX = /^content-length: (\d+)$/
+
+ def bytesize(str)
+ str = str.to_s
+ size = str.bytesize if str.respond_to?(:bytesize) # bytesize added in 1.9
+ size || str.size
+ end
+
+ def test_content_length_in_bytes
+ connection = Object.new
+ connection.instance_eval do
+ extend EM::P::Stomp
+
+ def last_sent_content_length
+ @sent && Integer(@sent[CONTENT_LENGTH_REGEX, 1])
+ end
+
+ def send_data(string)
+ @sent = string
+ end
+ end
+
+ queue = "queue"
+ failure_message = "header content-length is not the byte size of last sent body"
+
+ body = "test"
+ connection.send queue, body
+ assert_equal bytesize(body), connection.last_sent_content_length, failure_message
+
+ body = "test\u221A"
+ connection.send queue, body
+ assert_equal bytesize(body), connection.last_sent_content_length, failure_message
+ end
+end
Something went wrong with that request. Please try again.