summaryrefslogtreecommitdiff
path: root/ACE/protocols/ace/INet/FTP_Response.cpp
blob: 84b30c5888997b9dde4afe75942b2bd607277180 (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
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_ctype.h"
#include "ace/String_Base.h"
#include "ace/INet/FTP_Response.h"

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

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



ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace FTP
  {
    const int Response::eof_ = std::char_traits<char>::eof ();

    Response::Response()
      : status_ (NORESPONSE)
      {
      }

    Response::~Response()
      {
      }

    void Response::write(ostream& str) const
      {
        ACE_Array<ACE_CString>::size_type n = 0;
        str << this->status_;
        if (this->response_.size () > 0)
          {
            n = this->response_.size () - 1;
            str << (n > 0 ? '-' : ' ') << this->response_[0].c_str ();
          }
        str << "\r\n";
        for (ACE_Array<ACE_CString>::size_type i = 1;
             i < n;
             ++i)
          {
            str << this->response_[i].c_str () << "\r\n";
          }
        if (n > 0)
          {
            str << this->status_ << ' '
                << this->response_[n].c_str () << "\r\n";
          }
      }

    bool Response::read(istream& str)
      {
        int ch;
        str >> this->status_;
        ch = str.get ();
        if (str.bad () || this->status_type () == NOSTATE || (ch != ' ' && ch != '-'))
          {
            return false; // invalid status
          }

        bool multi_line = (ch == '-');

        ACE_Array<ACE_CString>::size_type n =
            this->response_.size ();
        this->response_.size (n+1);
        this->response_[n].clear ();
        ACE::IOS::CString_OStream sos (this->response_[n]);
        sos << this->status_;
        sos.put (ch);
        ch = this->read_line (str, sos);
        if (ch == '\r') ch = str.get ();
        sos.close (); // close the stream before resizing the array invalidates the string reference

        INET_DEBUG (6, (LM_DEBUG, DLINFO
                        ACE_TEXT ("ACE_INet_FTP: <-- %C\n"),
                        this->response_[n].c_str ()));

        if (multi_line)
          {
            while (ch != eof_)
              {
                int nxt_stat = 0;

                n = this->response_.size ();
                this->response_.size (n+1);
                this->response_[n].clear ();
                ACE::IOS::CString_OStream nxt_sos (this->response_[n]);

                if (ACE_OS::ace_isdigit (str.peek ()))
                  {
                    str >> nxt_stat;
                    ch = str.get ();
                    if (str.bad () || (nxt_stat == this->status_ && ch != ' '))
                      {
                        this->status_ = NORESPONSE;
                        return false;
                      }
                    nxt_sos << nxt_stat;
                    nxt_sos.put(ch);
                  }
                ch = this->read_line (str, nxt_sos);

                nxt_sos.close ();

                INET_DEBUG (9, (LM_DEBUG, DLINFO
                                ACE_TEXT ("ACE_INet_FTP: <-+ %C\n"),
                                this->response_[n].c_str ()));

                if (nxt_stat == this->status_)
                  {
                    return true;
                  }
              }

            this->status_ = NORESPONSE;
            return false;
          }
        else
          {
            return true;
          }
      }
  }
}

ACE_END_VERSIONED_NAMESPACE_DECL