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
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 #4
0
String ApLib::getRandomString()
{
  String sData = sRandomSeed_ + String::from(nRandomCnt_++);
  Apollo::MessageDigest md((unsigned char*) sData.c_str(), sData.bytes());
  String sRandom = md.getSHA1Hex();
  return sRandom;
}
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
0
int Test_Begin_Net_HTTPClient()
{
  int ok = 0;

  pTest_Net_HTTPClient_GET = new Test_HTTPClient_GET();
  if (pTest_Net_HTTPClient_GET != 0) {
    ok = pTest_Net_HTTPClient_GET->Get("http://www.virtual-presence.org/");
  }

  pTest_Net_HTTPClient_POST = new Test_HTTPClient_POST();
  if (pTest_Net_HTTPClient_POST != 0) {
    String sPostData = "a=b&b=c+d%20e";
    Buffer sbPostData;
    ok = pTest_Net_HTTPClient_POST->Post("http://www.virtual-presence.org/", (const unsigned char*) sPostData.c_str(), sPostData.bytes());
  }

  pTest_Net_HTTPClient_Cancel1GET = new Test_HTTPClient_Cancel1GET();
  if (pTest_Net_HTTPClient_Cancel1GET != 0) {
    ok = pTest_Net_HTTPClient_Cancel1GET->Get("http://www.virtual-presence.org/");
    if (ok) {
      pTest_Net_HTTPClient_Cancel1GET->Cancel();
    }
  }

  pTest_Net_HTTPClient_Cancel2GET = new Test_HTTPClient_Cancel2GET();
  if (pTest_Net_HTTPClient_Cancel2GET != 0) {
    ok = pTest_Net_HTTPClient_Cancel2GET->Get("http://www.virtual-presence.org/");
  }

  {
    Test_HTTPClient_AutoDelete* p = new Test_HTTPClient_AutoDelete();
    if (p != 0) {
      ok = p->Get("http://www.virtual-presence.org/");
    }
  }

  {
    Test_HTTPClient_CancelAutoDelete* p = new Test_HTTPClient_CancelAutoDelete();
    if (p != 0) {
      ok = p->Get("http://www.virtual-presence.org/");
    }
  }

  return ok;
}
Example #13
0
String ApLib::getMachineId()
{
  String sData;

#if defined(WIN32)
  String sVolumeSerialNumber;
  if (Apollo::getConfig("System/MachineIdUseVolumeSerialNumber", 1)) {
    DWORD dwVolumeSerialNumber = 0;
    String sRootPathName = "C:\\";
    if (::GetVolumeInformation(
      sRootPathName, // LPCTSTR lpRootPathName,
      0,// LPTSTR lpVolumeNameBuffer,
      0,// DWORD nVolumeNameSize,
      &dwVolumeSerialNumber, // LPDWORD lpVolumeSerialNumber,
      0,// LPDWORD lpMaximumComponentLength,
      0,// LPDWORD lpFileSystemFlags,
      0,// LPTSTR lpFileSystemNameBuffer,
      0 // DWORD nFileSystemNameSize
      )) {
        sVolumeSerialNumber = String::from(dwVolumeSerialNumber);
    }
  }

  String sComputerName;
  if (Apollo::getConfig("System/MachineIdUseComputerName", 1)) {
    WCHAR pNameBuffer[10240+1];
    DWORD nNameBufferSize = 10240;
    if (::GetComputerName(pNameBuffer, &nNameBufferSize)) {
      pNameBuffer[nNameBufferSize] = 0;
      sComputerName = pNameBuffer;
    }
  }

  String sMacAddress;
  if (Apollo::getConfig("System/MachineIdUseMacAddress", 1)) {
    List lAddress;

    DWORD dwBufLen = 0;
    if (::GetAdaptersInfo(0, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
      PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO) malloc(dwBufLen);
      if (pAdapterInfo != 0) {
        if (GetAdaptersInfo(pAdapterInfo, &dwBufLen) == ERROR_SUCCESS) {
          PIP_ADAPTER_INFO ppAdapterInfo = pAdapterInfo;
          do {
            String sIpAddress = (char*) & (ppAdapterInfo->IpAddressList.IpAddress);
            if (  sIpAddress != "0.0.0.0" 
              &&  sIpAddress != "127.0.0.1"
              //&& !sIpAddress.startsWith("10.")
              //&& !sIpAddress.startsWith("169.254.")
              //&& !sIpAddress.startsWith("192.168.")
              //&& !sIpAddress.startsWith("192.0.2.")
              ) {
              String sMac;
              sMac.appendf("%02X%02X%02X%02X%02X%02X"
                , ppAdapterInfo->Address[0]
                , ppAdapterInfo->Address[1]
                , ppAdapterInfo->Address[2]
                , ppAdapterInfo->Address[3]
                , ppAdapterInfo->Address[4]
                , ppAdapterInfo->Address[5]
                );
                lAddress.AddLast(sMac, (long) ppAdapterInfo->DhcpEnabled);
            }

            ppAdapterInfo = ppAdapterInfo->Next;
          } while (ppAdapterInfo != 0);
        }
        ::free(pAdapterInfo);
        pAdapterInfo = 0;
      }
    }

    for (Elem* e = 0; (e = lAddress.Next(e)) != 0; ) {
      if (e->getInt()) {
        sMacAddress += e->getName();
      }
    }

    if (sMacAddress.empty() && lAddress.length() > 0) {
      sMacAddress = lAddress.First()->getName();
    }
  }

  if (sVolumeSerialNumber.isset()) {
    sData += sVolumeSerialNumber;
  }

  if (sComputerName.isset()) {
    sData += sComputerName;
  }

  if (sMacAddress.isset()) {
    sData += sMacAddress;
  }

#endif

  String sMachineId;

  if (sData != "") {
    //sMachineId = sData;
    Apollo::MessageDigest digest((unsigned char *) sData.c_str(), sData.bytes());
    sMachineId = digest.getSHA1Hex();
  }

  return sMachineId;
}
Example #14
0
String ApLib::shortHash(const String& sData, unsigned int nLength)
{
  Apollo::MessageDigest md((unsigned char*) sData.c_str(), sData.bytes());
  return md.getSHA1Hex().subString(0, nLength);
}
Example #15
0
void String::operator+=(const String& s)
{
  if (!s.empty()) {
    append(s.c_str(), s.bytes(), 0);
  }
}
Example #16
0
void String::set(const String& s)
{
  if (!s.empty()) {
    append(s.c_str(), s.bytes(), 1);
  }
}
Example #17
0
	Variant(String value)      : type_(StringType) { initRef(value.bytes()); }