Module: Stomper::FrameSerializer::V1_1

Defined in:
lib/stomper/frame_serializer.rb

Overview

Stomp Protocol 1.1 specific frame writing / reading methods.

Constant Summary collapse

CHARACTER_ESCAPES =

Mapping of characters to their appropriate escape sequences. This is used when escaping headers for frames being written to the stream.

{
  ':' => "\\c",
  "\n" => "\\n",
  "\\" => "\\\\"
}
ESCAPE_SEQUENCES =

Mapping of escape sequences to their appropriate characters. This is used when unescaping headers being read from the stream.

{
  'c' => ':',
  '\\' => "\\",
  'n' => "\n"
}

Instance Method Summary collapse

Instance Method Details

#escape_header_name(hdr) ⇒ String Also known as: escape_header_value

Escape a header name to comply with Stomp Protocol 1.1 specifications. All special characters (the keys of CHARACTER_ESCAPES) are replaced by their corresponding escape sequences.

Parameters:

  • str (String)

Returns:

  • (String)

    escaped header name



167
168
169
170
171
# File 'lib/stomper/frame_serializer.rb', line 167

def escape_header_name(hdr)
  hdr.each_char.inject('') do |esc, ch|
    esc << (CHARACTER_ESCAPES[ch] || ch)
  end
end

#unescape_header_name(str) ⇒ String Also known as: unescape_header_value

Return the header name after known escape sequences have been translated to their respective values. The keys of ESCAPE_SEQUENCES, prefixed with a ‘' character, denote the allowed escape sequences. If an unknown escape sequence is encountered, an error is raised.

Parameters:

  • str (String)

    header string to unescape

Returns:

  • (String)

    unescaped header string

Raises:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/stomper/frame_serializer.rb', line 184

def unescape_header_name(str)
  state = :read
  str.each_char.inject('') do |unesc, ch|
    case state
    when :read
      if ch == '\\'
        state = :unescape
      else
        unesc << ch
      end
    when :unescape
      state = :read
      if ESCAPE_SEQUENCES[ch]
        unesc << ESCAPE_SEQUENCES[ch]
      else
        raise ::Stomper::Errors::InvalidHeaderEscapeSequenceError,
          "invalid header escape sequence encountered '\\#{ch}'"
      end
    end
    unesc
  end.tap do
    raise ::Stomper::Errors::InvalidHeaderEscapeSequenceError,
      "incomplete escape sequence encountered in '#{str}'" if state != :read
  end
end