Class: Ftpd::CommandSequenceChecker

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/ftpd/command_sequence_checker.rb

Instance Method Summary collapse

Methods included from Error

#error, #sequence_error, #syntax_error, #unimplemented_error

Constructor Details

#initializeCommandSequenceChecker

Returns a new instance of CommandSequenceChecker.



14
15
16
17
# File 'lib/ftpd/command_sequence_checker.rb', line 14

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

Instance Method Details

#check(command) ⇒ Object

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.

Parameters:

  • command (String)

    The command. Must be lowercase.

Raises:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ftpd/command_sequence_checker.rb', line 47

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

#expect(command) ⇒ Object

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.

Parameters:

  • command (String)

    The command. Must be lowercase.



26
27
28
# File 'lib/ftpd/command_sequence_checker.rb', line 26

def expect(command)
  @expected_command = command
end

#must_expect(command) ⇒ Object

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



34
35
36
# File 'lib/ftpd/command_sequence_checker.rb', line 34

def must_expect(command)
  @must_expect << command
end