Skip to content
Snippets Groups Projects
Select Git revision
  • 5402e58e6bd57faaffd38fa15a11084e77d0426b
  • master default protected
  • release/202005
  • release/202001
  • release/201912
  • release/201911
  • release/releaseWindowsTestOne
  • release/windowsReleaseTest
  • release/releaseTest
  • release/releaseWindowsTest
  • release/201910
  • release/qt/201910
  • release/windows-test/201910
  • release/201908
  • release/201906
  • release/201905
  • release/201904
  • release/201903
  • release/201902
  • release/201901
  • release/201812
  • 4.0.0
  • 2.2.0
  • 2.1.0
  • 2.0.1
  • 2.0.0
  • 1.4.1
  • 1.4.0
  • 1.3.0
  • 1.2.0
  • 1.1.0
31 results

responsemessage.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    responsemessage.cpp 2.85 KiB
    /**
     *  Copyright (C) 2005 Savoir-Faire Linux inc.
     *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
     *                                                                              
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *                                                                              
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     */
    #include "responsemessage.h"
    #include <cc++/url.h>
    
    /**
     * Used by isFinal() to check for the first digit
     * 2456 Means that 2XX, 4XX, 5XX and 6XX are final messages
     */
    const std::string ResponseMessage::FINALCODE = "2456";
    
    ResponseMessage::ResponseMessage(const std::string& code,
          const std::string& seq,
          const std::string& message) : _code(code), _seq(seq)
    {
        _message = "";
        appendMessage(message);
    }
    
    /*
     * Construct a message with a list of argument
     * and a space separator between each argument
     */
    ResponseMessage::ResponseMessage(const std::string& code,
          const std::string& seq,
          TokenList& arg) : _code(code), _seq(seq)
    {
      TokenList::iterator iter=arg.begin();
      if (iter!=arg.end()) {
        appendMessage(*iter);
        iter++;
      }
      // add space between each
      while(iter!=arg.end()) {
        _message.append(" ");
        appendMessage(*iter);
        iter++;
      }
    }
    
    
    ResponseMessage::~ResponseMessage()
    {
    }
    
    /**
     * Prepare and return a message, ready to be send to the client
     * @return message that contains: code sequenceId message
     */
    std::string 
    ResponseMessage::toString() const
    {
      return _code + " " + _seq + " " + _message;
    }
    
    /**
     * Return true when the response message is final (no more coming message for a request)
     * Note: the code should be 3 numbers-long.
     * @return true if it's the last message of the request
     */
    bool 
    ResponseMessage::isFinal() const
    {
      bool final = false;
      // only 3 chars code are valid
      // code that begin by: 2,4,5,6 are final
      
      if (_code.size() == 3) {
        if ( FINALCODE.find(_code[0]) != std::string::npos ) {
          final = true;
        }
      }
      return final;
    }
    
    void
    ResponseMessage::appendMessage(const std::string& strToken)
    {
      int len = strToken.length();
      if (len) {
        char *tmp = new char[len*3+2];
        ost::urlEncode(strToken.c_str(), tmp, len*3+2);
        // we don't have to put a '\0' right?
        _message.append(tmp);
        delete [] tmp; tmp = NULL;
      }
    }