Class: FakeFtp::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_ftp/server.rb

Constant Summary collapse

CMDS =
%w(
  acct
  cwd
  cdup
  dele
  list
  mdtm
  mkd
  nlst
  pass
  pasv
  port
  pwd
  quit
  stor
  retr
  rnfr
  rnto
  type
  user
)
LNBK =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control_port = 21, data_port = nil, options = {}) ⇒ Server

Returns a new instance of Server.

Raises:

  • (Errno::EADDRINUSE)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fake_ftp/server.rb', line 34

def initialize(control_port = 21, data_port = nil, options = {})
  self.port = control_port
  self.passive_port = data_port
  raise(Errno::EADDRINUSE, "#{port}") if !control_port.zero? && is_running?
  raise(Errno::EADDRINUSE, "#{passive_port}") if passive_port && !passive_port.zero? && is_running?(passive_port)
  @connection = nil
  @options = options
  @files = []
  @mode = :active
  @path = "/pub"
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/fake_ftp/server.rb', line 9

def mode
  @mode
end

#passive_portObject

Returns the value of attribute passive_port.



8
9
10
# File 'lib/fake_ftp/server.rb', line 8

def passive_port
  @passive_port
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/fake_ftp/server.rb', line 9

def path
  @path
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/fake_ftp/server.rb', line 8

def port
  @port
end

Instance Method Details

#add_file(filename, data, last_modified_time = Time.now) ⇒ Object



58
59
60
# File 'lib/fake_ftp/server.rb', line 58

def add_file(filename, data, last_modified_time = Time.now)
  @files << FakeFtp::File.new(::File.basename(filename.to_s), data, @mode, last_modified_time)
end

#file(name) ⇒ Object



50
51
52
# File 'lib/fake_ftp/server.rb', line 50

def file(name)
  @files.detect { |file| file.name == name }
end

#filesObject



46
47
48
# File 'lib/fake_ftp/server.rb', line 46

def files
  @files.map(&:name)
end

#is_running?(tcp_port = nil) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/fake_ftp/server.rb', line 104

def is_running?(tcp_port = nil)
  tcp_port.nil? ? port_is_open?(port) : port_is_open?(tcp_port)
end

#resetObject



54
55
56
# File 'lib/fake_ftp/server.rb', line 54

def reset
  @files.clear
end

#startObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fake_ftp/server.rb', line 62

def start
  @started = true
  @server = ::TCPServer.new('127.0.0.1', port)
  @port = @server.addr[1]
  @thread = Thread.new do
    while @started
      @client = @server.accept rescue nil
      if @client
        respond_with('220 Can has FTP?')
        @connection = Thread.new(@client) do |socket|
          while @started && !socket.nil? && !socket.closed?
            input = socket.gets rescue nil
            respond_with parse(input) if input
          end
          unless @client.nil?
            @client.close unless @client.closed?
            @client = nil
          end
        end
      end
    end
    unless @server.nil?
      @server.close unless @server.closed?
      @server = nil
    end
  end

  if passive_port
    @data_server = ::TCPServer.new('127.0.0.1', passive_port)
    @passive_port = @data_server.addr[1]
  end
end

#stopObject



95
96
97
98
99
100
101
102
# File 'lib/fake_ftp/server.rb', line 95

def stop
  @started = false
  @client.close if @client
  @server.close if @server
  @server = nil
  @data_server.close if @data_server
  @data_server = nil
end