Sha256: 1864eb14502c3e3d478f3726829606f8e550c171619750deb332aa3a49a20b4b

Contents?: true

Size: 1.7 KB

Versions: 12

Compression:

Stored size: 1.7 KB

Contents

# Some commands are supposed to occur in sequence.  For example, USER
# must be immediately followed by PASS.  This class keeps track of
# when a specific command either must arrive or must not arrive, and
# raises a "bad sequence" error when commands arrive in the wrong
# sequence.

module Ftpd
  class CommandSequenceChecker

    include Error

    def initialize
      @must_expect = []
      @expected_command = nil
    end

    # Set the command to expect next.  If not set, then any command
    # will be accepted, so long as it hasn't been registered using
    # {#must_expect}.  Otherwise, the set command must be next or a
    # sequence error will result.
    #
    # @param command [String] The command.  Must be lowercase.

    def expect(command)
      @expected_command = command
    end

    # Register a command that must be expected.  When that command is
    # received without {#expect} having been called for it, a sequence
    # error will result.

    def must_expect(command)
      @must_expect << command
    end

    # Check a command.  If expecting a specific command and this
    # command isn't it, then raise an error that will cause a "503 Bad
    # sequence" error to be sent.  After checking, the expected
    # command is cleared and any command will be accepted until
    # {#expect} is called again.
    #
    # @param command [String] The command.  Must be lowercase.
    # @raise [CommandError] A "503 Bad sequence" error

    def check(command)
      if @expected_command
        begin
          sequence_error unless command == @expected_command
        ensure
          @expected_command = nil
        end
      else
        sequence_error if @must_expect.include?(command)
      end
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ftpd-0.16.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.15.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.14.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.13.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.12.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.11.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.10.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.9.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.7.1 lib/ftpd/command_sequence_checker.rb
ftpd-0.8.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.7.0 lib/ftpd/command_sequence_checker.rb
ftpd-0.6.0 lib/ftpd/command_sequence_checker.rb