<commit_before># encoding: UTF-8

module Vines
  class Stream
    class Http < Client
      attr_accessor :session

      def initialize(config)
        super
        @session = Http::Session.new(self)
      end

      # Override Stream#create_parser to provide an HTTP parser rather than
      # a Nokogiri XML parser.
      #
      # Returns nothing.
      def create_parser
        @parser = ::Http::Parser.new.tap do |parser|
          body = ''
          parser.on_body = proc {|data| body << data }
          parser.on_message_complete = proc do
            process_request(Request.new(self, @parser, body))
            body = ''
          end
        end
      end

      # If the session ID is valid, switch this stream's session to the new
      # ID and return true. Some clients, like Google Chrome, reuse one stream
      # for multiple sessions.
      #
      # sid - The String session ID.
      #
      # Returns true if the server previously distributed this SID to a client.
      def valid_session?(sid)
        if session = Sessions[sid]
          @session = session
        end
        !!session
      end

      %w[max_stanza_size max_resources_per_account bind root].each do |name|
        define_method name do |*args|
          config[:http].send(name, *args)
        end
      end

      def process_request(request)
        if request.method == 'POST'
          if request.path == self.bind && request.options?
            request.reply_to_options
          elsif request.path == self.bind
            body = Nokogiri::XML(request.body).root
            if session = Sessions[body['sid']]
              @session = session
            else
              @session = Http::Session.new(self)
            end
            @session.request(request)
            @nodes.push(body)
          end
        end
        request.reply('It works!', 'text/plain')
      end

      # Alias the Stream#write method before overriding it so we can call
      # it later from a Session instance.
      alias :stream_write :write

      # Override Stream#write to queue stanzas rather than immediately writing
      # to the stream. Stanza responses must be paired with a queued request.
      #
      # If a request is not waiting, the written stanzas will buffer until they
      # can be sent in the next response.
      #
      # data - The XML String or XML::Node to write to the HTTP socket.
      #
      # Returns nothing.
      def write(data)
        @session.write(data)
      end

      # Parse the one or more stanzas from a single body element. BOSH clients
      # buffer stanzas sent in quick succession, and send them as a bundle, to
      # save on the request/response cycle.
      #
      # TODO This parses the XML again just to strip namespaces. Figure out
      # Nokogiri namespace handling instead.
      #
      # body - The XML::Node containing the BOSH `body` element.
      #
      # Returns an Array of XML::Node stanzas.
      def parse_body(body)
        body.namespace = nil<commit_msg>Fix; Only serve plain-text if it is no POST<commit_after># encoding: UTF-8

module Vines
  class Stream
    class Http < Client
      attr_accessor :session

      def initialize(config)
        super
        @session = Http::Session.new(self)
      end

      # Override Stream#create_parser to provide an HTTP parser rather than
      # a Nokogiri XML parser.
      #
      # Returns nothing.
      def create_parser
        @parser = ::Http::Parser.new.tap do |parser|
          body = ''
          parser.on_body = proc {|data| body << data }
          parser.on_message_complete = proc do
            process_request(Request.new(self, @parser, body))
            body = ''
          end
        end
      end

      # If the session ID is valid, switch this stream's session to the new
      # ID and return true. Some clients, like Google Chrome, reuse one stream
      # for multiple sessions.
      #
      # sid - The String session ID.
      #
      # Returns true if the server previously distributed this SID to a client.
      def valid_session?(sid)
        if session = Sessions[sid]
          @session = session
        end
        !!session
      end

      %w[max_stanza_size max_resources_per_account bind root].each do |name|
        define_method name do |*args|
          config[:http].send(name, *args)
        end
      end

      def process_request(request)
        if request.method == 'POST'
          if request.path == self.bind && request.options?
            request.reply_to_options
          elsif request.path == self.bind
            body = Nokogiri::XML(request.body).root
            if session = Sessions[body['sid']]
              @session = session
            else
              @session = Http::Session.new(self)
            end
            @session.request(request)
            @nodes.push(body)
          end
        else
          request.reply('It works!', 'text/plain')
        end
      end

      # Alias the Stream#write method before overriding it so we can call
      # it later from a Session instance.
      alias :stream_write :write

      # Override Stream#write to queue stanzas rather than immediately writing
      # to the stream. Stanza responses must be paired with a queued request.
      #
      # If a request is not waiting, the written stanzas will buffer until they
      # can be sent in the next response.
      #
      # data - The XML String or XML::Node to write to the HTTP socket.
      #
      # Returns nothing.
      def write(data)
        @session.write(data)
      end

      # Parse the one or more stanzas from a single body element. BOSH clients
      # buffer stanzas sent in quick succession, and send them as a bundle, to
      # save on the request/response cycle.
      #
      # TODO This parses the XML again just to strip namespaces. Figure out
      # Nokogiri namespace handling instead.
      #
      # body - The XML::Node containing the BOSH `body` element.
      #
      # Returns an Array of XML::Node stanzas.
      def parse_body(body)
        body.namespace = nil
{'fraction_non_alphanumeric': 0.07357529972080802, 'fraction_numerical': 0.00032846115946789294, 'mean_word_length': 1.90414878397711, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7286215', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1610, 'n_words': 608}