Example #1
0
static int String_replaceMatch(String::StringPair* pTable, String& s, bool bReverse)
{
  int nIndex = 0;
  const char* szKey = 0;
  if (bReverse) {
    szKey = pTable[nIndex].szRight;
  } else {
    szKey = pTable[nIndex].szLeft;
  }

  while (szKey != 0) {
    if (String::strncmp(s, szKey, s.bytes()) == 0) {
      if (s.bytes() == String::strlen(szKey)) {
        return nIndex;
      } else {
        return String_Escape_Match_partial;
      }
    }
    nIndex++;
    if (bReverse) {
      szKey = pTable[nIndex].szRight;
    } else {
      szKey = pTable[nIndex].szLeft;
    }
  }

  return String_Escape_Match_none;
}
Example #2
0
String String::filenameBasePath(const char* szText)
{
  String sPath = szText;
  String sPart;

  if (sPath.endsWith("/")
    #if defined(WIN32)
    || sPath.endsWith("\\")
    #endif
    )
  {
      // Already is a base path
  }
  else
  {
    if (sPath.reverseToken("/" 
      #if defined(WIN32)
      "\\"
      #endif
      , sPart)) {
      sPath.append(szText + sPath.bytes(), ::strlen(szText) - (sPath.bytes() + sPart.bytes()));
    }
  }

  return sPath;
}
Example #3
0
void String::trim(const char* szChars)
{
  if (empty()) { return; } // do nothing

  // Find prefix
  const char* p = szStr_;
  String sChars = szChars;
  while (sChars.findChar(UTF8_Char(p)) != 0 && *p != '\0') {
    p += UTF8_CharSize(p);
  }

  // Find suffix
  const char* q = szStr_ + nBytes_;
  int bDone = 0;
  do {
    q--;
    if (UTF8_CharSize(q)) {
      if (sChars.findChar(UTF8_Char(q)) == 0) {
        q += UTF8_CharSize(q);
        bDone = 1;
      }
    }
  } while (q >= szStr_ && !bDone);

  // Strip em
  if (p != szStr_ || q != szStr_ + nBytes_) {
    String sTmp;
    sTmp.append(p, q-p);
    append(sTmp.c_str(), sTmp.bytes(), 1);
  }
}
Example #4
0
int String::nextToken(const char* szSep, String& sPart)
{
  if (empty()) { return 0; } // do nothing

  int bFound = 0;

  const char* pSep = findChar(szSep);
  if (pSep != 0) {
    sPart.append(szStr_, pSep - szStr_, 1);
    String sRemain;
    sRemain.append(pSep, nBytes_ - (pSep - szStr_));
    const char* pNext = sRemain.findNotChar(szSep);
    if (pNext == 0) {
      clear();
    } else {
      append(pNext, sRemain.bytes() - (pNext - sRemain.c_str()), 1);
    }
    bFound = 1;
  } else {
    if (nBytes_ > 0) {
      sPart = szStr_;
      clear();
      bFound = 1;
    } else {
      sPart.clear();
    }
  }

  return bFound;
}
Example #5
0
AP_MSG_HANDLER_METHOD(ServerModule, HttpServer_SendResponse)
{
  HttpConnection* pConnection = findHttpConnection(pMsg->hConnection);
  if (pConnection == 0) { throw ApException(LOG_CONTEXT, "findHttpConnection(" ApHandleFormat ") failed", ApHandlePrintf(pMsg->hConnection)); }

  String sHeader;

  if (!pMsg->sProtocol) {
    pMsg->sProtocol = "HTTP/1.1";
  }

  if (pMsg->nStatus == 0) {
    pMsg->nStatus = 200;
  }

  if (!pMsg->sMessage) {
    if (pMsg->nStatus == 200) {
      pMsg->sMessage = "OK";
    } else {
      pMsg->sMessage = "No Message";
    }
  }

  sHeader.appendf("%s %d %s\r\n", _sz(pMsg->sProtocol), pMsg->nStatus, _sz(pMsg->sMessage));
  
  {
    for (Apollo::KeyValueElem* e = 0; (e = pMsg->kvHeader.nextElem(e)) != 0; ) {
      sHeader.appendf("%s: %s\r\n", _sz(e->getKey()), _sz(e->getString()));
    }
  }

  if (pMsg->sbBody.Length() > 0) {
    if (pMsg->kvHeader.find("content-length", Apollo::KeyValueList::IgnoreCase)) {
    } else {
      sHeader.appendf("Content-length: %d\r\n", pMsg->sbBody.Length());
    }
  } else {
    sHeader.appendf("Content-length: 0\r\n");
  }

  //sHeader += "Connection: close\r\n";
  sHeader += "\r\n";
  
  pConnection->DataOut((unsigned char*) sHeader.c_str(), sHeader.bytes());

  if (pMsg->sbBody.Length() > 0) {
    pConnection->DataOut(pMsg->sbBody.Data(), pMsg->sbBody.Length());
  }

  pMsg->apStatus = ApMessage::Ok;
}
Example #6
0
String GmModule::encrypt(const String& sIn)
{
  Apollo::Crypto data;
  data.SetData((unsigned char*) (const char*) sIn, sIn.bytes());

  Buffer b;
  if (!data.encryptWithLoginCredentials(b)) {
    apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "encryptWithLoginCredentials() failed"));
  }

  String sCryptedBase64;
  b.encodeBase64(sCryptedBase64);

  return sCryptedBase64;
}
Example #7
0
AP_MSG_HANDLER_METHOD(ServerModule, TcpServer_SendSrpc)
{
  String sMsg = pMsg->srpc.toString();

  if (apLog_IsVerbose) {
    apLog_Verbose((LOG_CHANNEL, LOG_CONTEXT, "conn=" ApHandleFormat " send: %s", ApHandlePrintf(pMsg->hConnection), _sz(sMsg)));
  }

  sMsg += "\n";

  TcpConnection* pConnection = findTcpConnection(pMsg->hConnection);
  if (pConnection == 0) { throw ApException(LOG_CONTEXT, "findTcpConnection(" ApHandleFormat ") failed", ApHandlePrintf(pMsg->hConnection)); }
  if (! pConnection->DataOut((unsigned char*) sMsg.c_str(), sMsg.bytes()) ) { throw ApException(LOG_CONTEXT, "Connection " ApHandleFormat " DataOut() failed", ApHandlePrintf(pMsg->hConnection)); }

  pMsg->apStatus = ApMessage::Ok;
}
Example #8
0
int String::reverseToken(const char* szSep, String& sPart)
{
  if (empty()) { return 0; } // do nothing

  int bFound = 0;
  String sSep = szSep;

  const char* p = szStr_ + nBytes_;
  int bDone = 0;
  if (p <= szStr_) {
    sPart.clear();
    // !found
  } else {
    do {
      p--;
      if (UTF8_CharSize(p)) {
        if (sSep.findChar(UTF8_Char(p)) != 0) {
          p += UTF8_CharSize(p);
          bDone = 1;
        }
      }
    } while (p > szStr_ && !bDone);
    sPart = p;
    bFound = !sPart.empty();
  }

  const char* q = p;
  bDone = 0;
  if (q <= szStr_) {
    clear();
  } else {
    do {
      q--;
      if (UTF8_CharSize(q)) {
        if (sSep.findChar(UTF8_Char(q)) == 0) {
          q += UTF8_CharSize(q);
          bDone = 1;
        }
      }
    } while (q > szStr_ && !bDone);
    String sTmp;
    sTmp.append(szStr_, q - szStr_);
    append(sTmp.c_str(), sTmp.bytes(), 1);
  }

  return bFound;
}
Example #9
0
int LoginTask::sendDigestAuth()
{
  // #login: stream-id(43956666) . password(a) = '43956666a' -> sha1('43956666a') = 'f944d75c9cd15c106b69f5795b0171fe86b7c182'
  // <iq id='2' type='set'>
  //   <query xmlns='jabber:iq:auth'>
  //     <username>wolfspelz</username>
  //     <digest>f944d75c9cd15c106b69f5795b0171fe86b7c182</digest>
  //     <resource>atHome</resource>
  //   </query>
  // </iq>
  int ok = 1;

  String sToken = sStreamId_; 
  sToken += pClient_->getPassword();
  Apollo::MessageDigest sha1((unsigned char*) sToken.c_str(), sToken.bytes());
  sToken = sha1.getSHA1Hex();

  sId_ = pClient_->getNextStanzaId();
  SetStanza request(sId_, pClient_->getJabberId().host());
  Apollo::XMLNode& query = request.addQuery(XMPP_NS_AUTH);

  Apollo::XMLNode& username = query.addChildRef("username");
  username.setCData(pClient_->getJabberId().user());

  Apollo::XMLNode& digest = query.addChildRef("digest");
  digest.setCData(sToken);

  Apollo::XMLNode& resource = query.addChildRef("resource");
  resource.setCData(pClient_->getResource());

  ok = pClient_->sendStanza(request);
  if (!ok) {
    apLog_Error((LOG_CHANNEL, LOG_CONTEXT, "pClient_->sendStanza() failed <%s ...", _sz(request.getName())));
  }

  return ok;
}
Example #10
0
int String::replaceCore(StringPair* pTable, bool bReverse)
{
  int nReplaced = 0;
  String sNew;
  String sToken;

  const char* szSrc = c_str();
  while (*szSrc != '\0') {
    unsigned int nCharLen = UTF8_CharSize(szSrc);
    if (nCharLen == 0) {
      // Not a start character: skip
      nCharLen = 1;
    }
    sToken.append(szSrc, nCharLen);
    szSrc += nCharLen;
    int nMatch = String_replaceMatch(pTable, sToken, bReverse);
    switch (nMatch) {
      case String_Escape_Match_none:
        sNew += sToken;
        sToken.clear();
        break;
      case String_Escape_Match_partial:
        break;
      default:
        nReplaced++;
        sToken.clear();
        if (bReverse) {
          sNew += pTable[nMatch].szLeft;
        } else {
          sNew += pTable[nMatch].szRight;
        }
    }
  }

  append(sNew.c_str(), sNew.bytes(), 1);
  return nReplaced;
}
Example #11
0
void String::operator+=(const String& s)
{
  if (!s.empty()) {
    append(s.c_str(), s.bytes(), 0);
  }
}
Example #12
0
void String::set(const String& s)
{
  if (!s.empty()) {
    append(s.c_str(), s.bytes(), 1);
  }
}
Example #13
0
	Variant(String value)      : type_(StringType) { initRef(value.bytes()); }