Class: OnStomp::Connections::Serializers::Stomp_1_1
- Inherits:
-
Object
- Object
- OnStomp::Connections::Serializers::Stomp_1_1
- Includes:
- Stomp_1
- Defined in:
- lib/onstomp/connections/serializers/stomp_1_1.rb
Overview
Frame serializer / parser for STOMP 1.1 connections.
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.
Hash[CHARACTER_ESCAPES.map { |k,v| [v,k]
Instance Method Summary collapse
-
#encode_header(s) ⇒ String
Encodes the given string to 'UTF-8'.
-
#escape_header(s) ⇒ String
Escapes a header name or value by replacing special characters with their appropriate escape sequences.
-
#force_body_encoding(f) ⇒ OnStomp::Components::Frame
Forces the encoding of the given frame's body to match its charset.
-
#force_header_encoding(s) ⇒ String
Forces the encoding of the given string to 'UTF-8'.
-
#frame_to_string(frame) ⇒ String
Converts a frame to a string.
-
#initialize ⇒ Stomp_1_1
constructor
Creates a new serializer and calls OnStomp::Connections::Serializers::Stomp_1#reset_parser.
-
#make_ct(f) ⇒ OnStomp::Components::Frame
Set an appropriate +content-type+ header with
charset
parameter for frames with a text body. -
#prepare_parsed_frame(frame) ⇒ Object
Forces the frame's body to match the charset specified in its
content-type
header, if applicable. -
#split_header(str) ⇒ [String, String]
Splits a header line into a header name / header value pair at the first ':' character unescapes them, and returns the pair.
-
#unescape_header(s) ⇒ String
Unescapes a header name or pair parsed from the read buffer by converting known escape sequences into their special characters.
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
#initialize ⇒ Stomp_1_1
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
#encode_header(s) ⇒ String
No-op for Ruby 1.8.x
Encodes the given string to 'UTF-8'
79 80 81 |
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 79 def encode_header(s) s.encoding.name == 'UTF-8' ? s : s.encode('UTF-8') end |
#escape_header(s) ⇒ String
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+
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 |
#force_body_encoding(f) ⇒ OnStomp::Components::Frame
No-op for Ruby 1.8.x
Forces the encoding of the given frame's body to match its charset.
91 92 93 94 95 96 |
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 91 def force_body_encoding f type, subtype, charset = f.content_type charset ||= (type == 'text') ? 'UTF-8' : 'ASCII-8BIT' f.body.force_encoding(charset) f end |
#force_header_encoding(s) ⇒ String
No-op for Ruby 1.8.x
Forces the encoding of the given string to 'UTF-8'
86 |
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 86 def force_header_encoding(s); s.tap { s.force_encoding('UTF-8') }; end |
#frame_to_string(frame) ⇒ String
Converts a frame to a 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 |
#make_ct(f) ⇒ OnStomp::Components::Frame
No-op for Ruby 1.8.x
Set an appropriate +content-type+ header with charset
parameter for
frames with a text body
102 103 104 105 106 107 108 109 110 |
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 102 def make_ct f return f if f.body.nil? t, st = f.content_type enc = f.body.encoding.name if enc != 'ASCII-8BIT' f[:'content-type'] = "#{t||'text'}/#{st||'plain'};charset=#{enc}" end f end |
#prepare_parsed_frame(frame) ⇒ Object
Forces the frame's body to match the charset specified in its content-type
header, if applicable.
70 71 72 |
# File 'lib/onstomp/connections/serializers/stomp_1_1.rb', line 70 def prepare_parsed_frame frame force_body_encoding frame end |
#split_header(str) ⇒ [String, String]
Splits a header line into a header name / header value pair at the first ':' character unescapes them, and returns the pair.
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 |
#unescape_header(s) ⇒ String
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.
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 |