Class: OnStomp::Connections::Serializers::Stomp_1_1

Inherits:
Object
  • Object
show all
Includes:
Stomp_1
Defined in:
lib/onstomp/connections/serializers/stomp_1_1.rb

Overview

Frame serializer / parser for STOMP 1.1 connections.

Constant Summary

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.

Hash[CHARACTER_ESCAPES.map { |k,v| [v,k] }]

Instance Method Summary (collapse)

Methods included from Stomp_1

#bytes_to_frame, #finish_body, #finish_command, #finish_header_line, #frame_to_bytes, #frame_to_string_base, #parse_body, #parse_command, #parse_header_line, #parser_flush, #reset_parser

Constructor Details

- (Stomp_1_1) initialize

Creates a new serializer and calls OnStomp::Connections::Serializers::Stomp_1#reset_parser



16
17
18
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 16

def initialize
  reset_parser
end

Instance Method Details

- (String) encode_header(s)

Note:

No-op for Ruby 1.8.x

Encodes the given string to 'UTF-8'

Parameters:

  • s (String)

Returns:

  • (String)


79
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 79

def encode_header(s); s; end

- (String) escape_header(s)

Escapes a header name or value by replacing special characters with their appropriate escape sequences. The header will also be encoded to 'UTF-8' when using Ruby 1.9+

Parameters:

  • s (String)

    header name or value

Returns:

  • (String)


34
35
36
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 34

def escape_header s
  encode_header(s).gsub(/[:\n\\\\]/) { |c| CHARACTER_ESCAPES[c] }
end

- (OnStomp::Components::Frame) force_body_encoding(f)

Note:

No-op for Ruby 1.8.x

Forces the encoding of the given frame's body to match its charset.



91
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 91

def force_body_encoding(f); f; end

- (String) force_header_encoding(s)

Note:

No-op for Ruby 1.8.x

Forces the encoding of the given string to 'UTF-8'

Parameters:

  • s (String)

Returns:

  • (String)


86
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 86

def force_header_encoding(s); s; end

- (String) frame_to_string(frame)

Converts a frame to a string

Parameters:

Returns:

  • (String)


23
24
25
26
27
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 23

def frame_to_string frame
  frame_to_string_base(make_ct(frame)) do |k,v|
    "#{escape_header k}:#{escape_header v}\n"
  end
end

- (OnStomp::Components::Frame) make_ct(f)

Note:

No-op for Ruby 1.8.x

Set an appropriate +content-type+ header with charset parameter for frames with a text body



132
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 132

def make_ct(f); f; end

- (Object) prepare_parsed_frame(frame)

Forces the frame's body to match the charset specified in its content-type header, if applicable.

Parameters:



70
71
72
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 70

def prepare_parsed_frame frame
  force_body_encoding frame
end

- ([String, String]) split_header(str)

Splits a header line into a header name / header value pair at the first ':' character unescapes them, and returns the pair.

Parameters:

  • str (String)

    header line to split

Returns:

  • ([String, String])

Raises:



58
59
60
61
62
63
64
65
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 58

def split_header(str)
  col = str.index(':')
  unless col
    raise OnStomp::MalformedHeaderError, "unterminated header: '#{str}'"
  end
  [ unescape_header(str[0...col]),
    unescape_header(str[(col+1)..-1]) ]
end

- (String) unescape_header(s)

Unescapes a header name or pair parsed from the read buffer by converting known escape sequences into their special characters. The header string will have a 'UTF-8' encoding forced upon it when using Ruby 1.9+, as per the STOMP 1.1 spec.

Parameters:

  • s (String)

    header name or value

Returns:

  • (String)

Raises:



46
47
48
49
50
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 46

def unescape_header s
  force_header_encoding(s).gsub(/\\.?/) do |c|
    ESCAPE_SEQUENCES[c] || raise(OnStomp::InvalidHeaderEscapeSequenceError, "#{c}")
  end
end