namespace LightSpeed { defineTest test_textToBase64("baseX.textToBase64","UHJpc2VybmUgemx1dG91Y2t5IGt1biB1cGVsIGRhYmVsc2tlIGtvZHk=MTIzNA==",[](PrintTextA &out) { CharsToBytesConvert charToByte; ByteToBase64Convert base64conv; ConverterChain<CharsToBytesConvert &, ByteToBase64Convert &> convChain(charToByte, base64conv); ConstStrA text("Priserne zlutoucky kun upel dabelske kody"); StringA conv = StringA(convertString(convChain, text)); out("%1") << conv; base64conv.eolb = false; ConstStrA text2("1234"); StringA conv2 = StringA(convertString(convChain, text2)); out("%1") << conv2; }); defineTest test_base64ToText("baseX.base64ToText","Priserne zlutoucky kun upel dabelske kody",[](PrintTextA &out){ BytesToCharsConvert charToByte; Base64ToByteConvert base64conv; ConverterChain<Base64ToByteConvert &, BytesToCharsConvert &> convChain(base64conv,charToByte); ConstStrA text("UHJpc2VybmUgemx1dG91Y2t5IGt1biB1cGVsIGRhYmVsc2tlIGtvZHk="); StringA conv = StringA(convertString(convChain, text)); out("%1") << conv; }); }
void WebSocketsClient::sendTextMessage(ConstStrA msg) { Synchronized<FastLockR> _(lock); if (stream != null) try { Super::sendTextMessage(msg,true); stream->flush(); return; } catch (NetworkException &) { onReconnect().thenCall(Action::create(this,&WebSocketsClient::sendTextMessage, StringA(msg))); lostConnection(naturalNull); } else { onReconnect().thenCall(Action::create(this,&WebSocketsClient::sendTextMessage, StringA(msg))); } }
StringA LinuxNetAddress::asString(bool resolve) { if (addrinfo == 0) return StringA(); TextFormatBuff<char, SmallAlloc<256> > buff; char hostbuff[512]; char svcbuff[256]; int flags = resolve?0:NI_NUMERICHOST|NI_NUMERICSERV; getnameinfo(addrinfo->ai_addr, addrinfo->ai_addrlen, hostbuff,sizeof(hostbuff), svcbuff,sizeof(svcbuff),flags); buff("%1:%2") << hostbuff << svcbuff; return StringA(buff.write()); }
void HttpClient::proxyConnect(PNetworkStream stream, ConstStrA host, ConstStrA authorization) { if (host.find(':') == naturalNull) { return proxyConnect(stream,StringA(host+ConstStrA(":443")),authorization); } PNetworkStream nstream = new BufferedNetworkStream(stream); HttpRequest req(stream.get(), host, mCONNECT, !useHTTP10); if (!authorization.empty()) { req.setHeader(fldProxyAuthorization,authorization); } req.closeOutput(); class ResponseResult: public IHttpResponseCB { public: virtual void storeStatus(natural statusCode, ConstStrA statusMessage) { status = statusCode; statusMsg.append(statusMessage); } virtual void storeHeaderLine(ConstStrA, ConstStrA) { } natural status; AutoArray<char, SmallAlloc<32> > statusMsg; }; ResponseResult res; HttpResponse resp(stream.get(),res,HttpResponse::readHeadersNow); if (status != 200) throw HttpStatusException(THISLOCATION,host,res.status, res.statusMsg); }
atomicValue& QueryCache::trackSeqNumbers(ConstStrA databaseName) { Synchronized<FastLock> _(lock); atomicValue *p = seqMap.find(StrKey(databaseName)); if (p) { return *p; } else { seqMap.insert(StrKey((StringA(databaseName))),0); return *seqMap.find(StrKey(databaseName)); } }
/* * vislib::sys::Path::GetTempDirectoryA */ vislib::StringA vislib::sys::Path::GetTempDirectoryA(void) { #ifdef _WIN32 char buffer[MAX_PATH + 1]; buffer[MAX_PATH] = buffer[0] = 0; if (::GetTempPathA(MAX_PATH, buffer) > MAX_PATH) { throw SystemException(__FILE__, __LINE__); } return buffer; // return Environment::GetVariable("TEMP", false); #else /* _WIN32 */ return StringA("/tmp"); #endif /* _WIN32 */ }
//--------------------------------------------------------------------------- void ConfigFileRo::load (const char* pszPath) { StringAVec vecLines; // reset m_Settings.clear(); // check file if (!FileSystem::doesExist(pszPath)) GLB_THROWEXCODE(EXCODE_FILE_DOESNTEXIST); if (FileSystem::getSize(pszPath) > std::numeric_limits<uint32>::max()) GLB_THROWEXCODE(EXCODE_MEMORY); // file is too big // extract file content { uint32 uiSize = (uint32)FileSystem::getSize(pszPath); // it is safe to cast-down here StringA strLines; byte* pBuffer; pBuffer = (byte*)strLines.acquireBuffer(uiSize, false); FileSystem::extractContent(pszPath, pBuffer, uiSize); strLines.releaseBuffer(); strLines.trimWhitespaces(); strLines.explodeWithOneOf(vecLines, "\r\n"); } // parse lines for (uint i = 0; i < vecLines.size(); ++i) { StringAVec vecL; // skip empty and comment lines vecLines[i].trimWhitespaces(); if (vecLines[i].isEmpty() || vecLines[i][0] == '#') continue; // break key-value pair vecLines[i].explodeWithOneOf(vecL, "\t ", 2); if (vecL.size() >= 1) { if (vecL.size() < 2) vecL.push_back(StringA()); if (m_Settings.find(vecL[0]) != m_Settings.end()) GLB_LOGWARN("Duplicated setting name \"%s\" in config file \"%s\" !", vecL[0].c_str(), pszPath); m_Settings.insert(Settings::value_type(vecL[0], vecL[1])); } } }
ITCPServerConnHandler::Command HttpReqImpl::readHeader() { try { AutoArray<char, SmallAlloc<8192> > linebuff; NStream::Buffer &buffer = inout->getBuffer(); natural fetched = buffer.fetch(); if (fetched == 0) { errorPage(413,ConstStrA(),"Header is too large"); return ITCPServerConnHandler::cmdRemove; } natural pos = buffer.lookup(ConstBin("\r\n"),fetched); while (pos != naturalNull) { linebuff.resize(pos+2); buffer.read(linebuff.data(),pos+2); ConstStrA line = linebuff.head(pos); if (method.empty()) { if (pos == 0) return ITCPServerConnHandler::cmdWaitRead; reqBeginTime = TimeStamp::now(); reportDuration = true; cropWhite(line); ConstStrA::SplitIterator splt = line.split(' '); method = hdrPool.add(ConstStrA(splt.getNext())); path = hdrPool.add(ConstStrA(splt.getNext())); while (path.empty()) path = hdrPool.add(ConstStrA(splt.getNext())); protocol = hdrPool.add(ConstStrA(splt.getNext())); while (protocol.empty()) protocol = hdrPool.add(ConstStrA(splt.getNext())); //parse some fields TextParser<char,StaticAlloc<256> > parser; //check http version if (parser("HTTP/%u1.%u2",protocol)) { httpMajVer = (unsigned short)((natural)parser[1]); httpMinVer = (unsigned short)((natural)parser[2]); if (httpMajVer != 1 || (httpMinVer != 0 && httpMinVer != 1)) return errorPageKA(505); useHTTP11(httpMinVer == 1); } else { return errorPageKA(400,StringA(ConstStrA("Unknown protocol: ")+ protocol)); } } else if (line.empty()) { return finishReadHeader(); } else { natural dblcolon = line.find(':'); if (dblcolon == naturalNull) { errorPage(400,ConstStrA(),"line"); return ITCPServerConnHandler::cmdRemove; } ConstStrA field = line.head(dblcolon); ConstStrA value = line.offset(dblcolon+1); cropWhite(field); cropWhite(value); requestHdrs.insert(hdrPool.add(field),hdrPool.add(value)); } pos = buffer.lookup(ConstBin("\r\n")); } return ITCPServerConnHandler::cmdWaitRead; } catch (AllocatorLimitException &) { errorPage(413,ConstStrA(),"Header is too large (4096 bytes)"); return ITCPServerConnHandler::cmdRemove; } }
void QueryCache::set(ConstStrA url, const CachedItem& item) { Synchronized<FastLock> _(lock); StrKey k((StringA(url))); itemMap.erase(k); itemMap.insert(k, item); }
/* * vislib::sys::TextFileReader::ReadLine */ bool vislib::sys::TextFileReader::ReadLine(vislib::StringA& outLine, unsigned int maxSize) { ASSERT(this->file != NULL); vislib::StringA prepend; unsigned int start = this->bufPos; unsigned int maxToRead = maxSize; unsigned int len = 0; while (maxToRead > 0) { if (this->bufPos >= this->validBufSize) { if (len > 0) { // copy partial line to output variable prepend.Append(StringA(this->buf + start, len)); } // buffer depleted. Need new data. this->bufStart += this->validBufSize; ASSERT(this->file->Tell() == this->bufStart); // inconsitency // ... detected, maybe we should always seek here (or at least // throw and exception instead of an assertion. Think about! this->bufPos = 0; start = 0; len = 0; this->validBufSize = static_cast<unsigned int>( this->file->Read(this->buf, this->bufSize)); if (this->validBufSize == 0) { // unable to read, maybe eof outLine = prepend; return !outLine.IsEmpty(); } } if ((this->buf[this->bufPos] == 0x0A) || (this->buf[this->bufPos] == 0x0D)) { // I detected a new line character :-) outLine = prepend + StringA(this->buf + start, len + 1); len = outLine.Length(); // make the newline pretty outLine[static_cast<int>(len) - 1] = '\n'; // check for combined newline char c1 = this->buf[this->bufPos++], c2 = 0; if (this->bufPos < this->validBufSize) { c2 = this->buf[this->bufPos]; } else { // arglegarglgarg EndOfBuffer Buhuhu this->bufStart += this->validBufSize; ASSERT(this->file->Tell() == this->bufStart); // inconsitency // ... detected, maybe we should always seek here (or at least // throw and exception instead of an assertion. Think about! this->bufPos = 0; start = 0; len = 0; this->validBufSize = static_cast<unsigned int>( this->file->Read(this->buf, this->bufSize)); if (this->validBufSize > 0) { c2 = this->buf[0]; } } if (((c2 == 0x0A) || (c2 == 0x0D)) && (c2 != c1)) { // this is a combined newline, at least I am pretty sure this->bufPos++; } return true; } else { // normal character. this->bufPos++; len++; maxToRead--; } } // no new line buf read max characters. outLine = prepend + StringA(this->buf + start, len); return true; }
IniConfigT::Section IniConfigT::Section::openPrefix(const ConstStrA &pfx) const{ return Section(owner,section,StringA(prefix+pfx)); }
IniConfigT::Section IniConfigT::Section::openSection(const ConstStrA &name) const{ return Section(owner,StringA(section+ConstStrA(".")+name),ConstStrA()); }