summaryrefslogtreecommitdiff
path: root/protocols/ace/INet/FTP_Request.cpp
blob: 57d2f6f17c649a7af1389f29476abe1278234998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// $Id$

#include "ace/String_Base.h"
#include "ace/OS_NS_ctype.h"
#include "ace/INet/FTP_Request.h"

#if !defined (__ACE_INLINE__)
#include "ace/INet/FTP_Request.inl"
#endif

#include "ace/INet/INet_Log.h"
#include "ace/INet/String_IOStream.h"



ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace FTP
  {

    const ACE_CString Request::FTP_USER = "USER";
    const ACE_CString Request::FTP_PASS = "PASS";
    const ACE_CString Request::FTP_QUIT = "QUIT";
    const ACE_CString Request::FTP_TYPE = "TYPE";
    const ACE_CString Request::FTP_SYST = "SYST";
    const ACE_CString Request::FTP_PWD  = "PWD";
    const ACE_CString Request::FTP_CWD  = "CWD";
    const ACE_CString Request::FTP_CDUP = "CDUP";
    const ACE_CString Request::FTP_RNFR = "RNFR";
    const ACE_CString Request::FTP_RNTO = "RNTO";
    const ACE_CString Request::FTP_DELE = "DELE";
    const ACE_CString Request::FTP_MKD  = "MKD";
    const ACE_CString Request::FTP_RMD  = "RMD";
    const ACE_CString Request::FTP_RETR = "RETR";
    const ACE_CString Request::FTP_STOR = "STOR";
    const ACE_CString Request::FTP_LIST = "LIST";
    const ACE_CString Request::FTP_NLST = "NLST";
    const ACE_CString Request::FTP_ABOR = "ABOR";
    const ACE_CString Request::FTP_EPRT = "EPRT";
    const ACE_CString Request::FTP_PORT = "PORT";
    const ACE_CString Request::FTP_EPSV = "EPSV";
    const ACE_CString Request::FTP_PASV = "PASV";
    const ACE_CString Request::FTP_STAT = "STAT";

    const int Request::eof_ = std::char_traits<char>::eof ();

    Request::Request()
      {
      }

    Request::~Request()
      {
      }

    void Request::arguments (ACE_Array<ACE_CString> & args) const
      {
        ACE::IOS::CString_IStream sis (this->args_);

        int ch = sis.get ();
        while (ch != eof_)
          {
            // skip whitespace
            while (ACE_OS::ace_isspace (ch))  ch = sis.get ();
            if (ch != eof_)
              {
                // get arg
                ACE_Array<ACE_CString>::size_type n =
                    args.size ();
                args.size (n + 1);
                ACE_CString& arg = args[n];
                while (ch != eof_ && !ACE_OS::ace_isspace (ch))
                  {
                    arg += ch;
                    ch = sis.get ();
                  }
              }
          }
      }

    void Request::write(std::ostream& str) const
      {
        str << this->command_.c_str ();
        if (!this->args_.empty ())
          str << ' ' << this->args_.c_str ();
        str << "\r\n";

        INET_DEBUG (6, (LM_DEBUG, DLINFO
                        ACE_TEXT ("ACE_INet_FTP: --> %C %C\n"),
                        this->command_.c_str (),
                        this->command_ == FTP_PASS ?
                            "***" : this->args_.c_str ()));
      }

    bool Request::read(std::istream& str)
      {
        ACE_CString cmd (4, '\0');
        ACE_CString args (128, '\0');

        int ch =  str.peek ();
        if (ch == eof_)
          {
            str.get (); // skip to eof
            return false;
          }
        // skip whitespace
        while (ACE_OS::ace_isspace (str.peek ()))
          {
            str.get ();
          }
        // get method
        ch = str.get ();
        while (!ACE_OS::ace_isspace (ch) && ch != eof_ && cmd.length () < MAX_CMD_LENGTH)
          {
            cmd += ch;
            ch = str.get ();
          }
        if (!ACE_OS::ace_isspace (ch))
          return false; // invalid FTP command string
        if (ch != '\r' && ch != '\n')
          {
            // skip whitespace
            while (ACE_OS::ace_isspace (str.peek ()))
              {
                str.get ();
              }
            // get arguments
            ch = str.get ();
            while (ch != eof_ && ch != '\r' && ch != '\n' && args.length () < MAX_ARG_LENGTH)
              {
                args += ch;
                ch = str.get ();
              }
            if (ch != eof_ && ch != '\r' && ch != '\n')
              {
                return false; // args too long
              }
          }
        if (ch == '\r')
          {
            str.get ();
          }
        this->command (cmd);
        this->args_ = args;
        return true;
      }

  }
}

ACE_END_VERSIONED_NAMESPACE_DECL