class Strelka::HTTPResponse
An HTTP response class.
Constants
- CONTENT_TYPE_CHARSET_RE
Pattern for matching a 'charset' parameter in a media-type string, such as the Content-type header
Attributes
Overridden charset of the response's entity body, as either an Encoding or a String. This will be appended to the Content-type header when the response is sent to the client, replacing any charset setting in the Content-type header already. Defaults to nil, which will cause the encoding of the entity body object to be used instead unless there's already one present in the Content-type. In any case, if the encoding is Encoding::ASCII_8BIT, no charset will be appended to the content-type header.
An Array of any encodings that have been applied to the response's entity body, in the order they were applied. These will be set as the response's Content-Encoding header when it is sent to the client. Defaults to the empty Array.
The natural language(s) of the response's entity body. These will be set as the response's Content-Language header when it is sent to the client. Defaults to the empty Array.
A auto-vivifying nested Hash that plugins can use to pass data amongst themselves. The notes hash is shared between the request and response objects if the request is set on the response with request=
.
Public Instance Methods
Overridden to add charset, encodings, and languages to outgoing headers if any of them are set.
# File lib/strelka/httpresponse.rb, line 70 def normalized_headers headers = super self.add_content_type_charset( headers ) headers.content_encoding ||= self.encodings.join(', ') unless self.encodings.empty? headers.content_language ||= self.languages.join(', ') unless self.languages.empty? self.add_cookie_headers( headers ) return headers end
Set the request object associated with this response to request
.
# File lib/strelka/httpresponse.rb, line 101 def request=( request ) super @notes = request.notes end
Overridden to reset charset, language, and encoding data, too.
# File lib/strelka/httpresponse.rb, line 83 def reset super @charset = nil @languages.clear @encodings.clear end
Protected Instance Methods
Add a charset to the content-type header in headers
if possible.
# File lib/strelka/httpresponse.rb, line 112 def add_content_type_charset( headers ) return unless headers.content_type && headers.content_type.start_with?( 'text' ) charset = self.find_header_charset self.log.debug "Setting the charset in the content-type header to: %p" % [ charset.name ] headers.content_type = headers.content_type.sub( CONTENT_TYPE_CHARSET_RE, '' ) headers.content_type += "; charset=#{charset.name}" unless charset == Encoding::ASCII_8BIT end
Return an Encoding object for the 'charset' parameter of the content-type header, if there is one.
# File lib/strelka/httpresponse.rb, line 142 def content_type_charset return nil unless self.content_type name = self.content_type[ CONTENT_TYPE_CHARSET_RE, :charset ] or return nil enc = Encoding.find( name ) self.log.debug "Extracted content-type charset: %p" % [ enc ] return enc end
Get the body's charset, if possible. Returns nil
if the charset couldn't be determined.
# File lib/strelka/httpresponse.rb, line 155 def entity_body_charset self.log.debug "Deriving charset from the entity body..." enc = nil enc ||= @body.internal_encoding if @body.respond_to?( :internal_encoding ) enc ||= @body.external_encoding if @body.respond_to?( :external_encoding ) self.log.debug " encoding of the entity body is: %p" % [ enc ] return enc end
Try to find a character set for the request, using the charset
attribute first, then the 'charset' parameter from the content-type header, then the Encoding object associated with the entity body, then the default internal and external encodings (if they're set, in that order). If none of those are found, this method returns ISO-8859-1.
# File lib/strelka/httpresponse.rb, line 130 def find_header_charset return ( self.charset || self.content_type_charset || self.entity_body_charset || Encoding.default_internal || Encoding.default_external || Encoding::ISO_8859_1 ) end
Add some instance variables to new HTTPResponses.
# File lib/strelka/httpresponse.rb, line 26 def initialize( * ) # :notnew: @charset = nil @languages = [] @encodings = [] @notes = Hash.new {|h,k| h[k] = {} } @cookies = nil super end