void addResult(rowcount_t resultCount, MemoryBuffer &resultData, bool complete)
 {
     Owned<IWorkUnit> wu = &container.queryJob().queryWorkUnit().lock();
     Owned<IWUResult> result = updateWorkUnitResult(wu, resultName, resultSeq);
     if (appendOutput)
         result->addResultRaw(resultData.length(), resultData.toByteArray(), ResultFormatRaw);
     else
         result->setResultRaw(resultData.length(), resultData.toByteArray(), ResultFormatRaw);
     result->setResultRowCount(resultCount);
     result->setResultTotalRowCount(resultCount);
     resultData.clear();
     if (complete)
         result->setResultStatus(ResultStatusCalculated);
     appendOutput = true;
 }
Beispiel #2
0
extern DLLSERVER_API bool getResourceXMLFromFile(const char *filename, const char *type, unsigned id, StringBuffer &xml)
{
    MemoryBuffer data;
    if (!getResourceFromFile(filename, data, type, id))
        return false;
    return decompressResource(data.length(), data.toByteArray(), xml);
}
Beispiel #3
0
    virtual void done()
    {
        Owned<IWorkUnit> wu = &container.queryJob().queryWorkUnit().lock();
        Owned<IWUResult> r = wu->updateResultBySequence(helper->getSequence());
        r->setResultStatus(ResultStatusCalculated);
        r->setResultLogicalName(outputName);
        r.clear();
        wu.clear();

        IPropertyTree &props = newIndexDesc->queryProperties();
        props.setProp("@kind", "key");
        if (0 != (helper->getFlags() & KDPexpires))
            setExpiryTime(props, helper->getExpiryDays());

        // Fill in some logical file properties here
        IPropertyTree &originalProps = originalDesc->queryProperties();;
        if (originalProps.queryProp("ECL"))
            props.setProp("ECL", originalProps.queryProp("ECL"));
        MemoryBuffer rLMB;
        if (originalProps.getPropBin("_record_layout", rLMB))
            props.setPropBin("_record_layout", rLMB.length(), rLMB.toByteArray());
        props.setPropInt("@formatCrc", originalProps.getPropInt("@formatCrc"));
        if (originalProps.getPropBool("@local"))
            props.setPropBool("@local", true);

        container.queryTempHandler()->registerFile(outputName, container.queryOwner().queryGraphId(), 0, false, WUFileStandard, &clusters);
        queryThorFileManager().publish(container.queryJob(), outputName, *newIndexDesc);
        CMasterActivity::done();
    }
void setRtlFormat(IPropertyTree & properties, IOutputMetaData * meta)
{
    if (meta && meta->queryTypeInfo())
    {
        MemoryBuffer out;
        if (dumpTypeInfo(out, meta->querySerializedDiskMeta()->queryTypeInfo()))
            properties.setPropBin("_rtlType", out.length(), out.toByteArray());
    }
}
void CSendLogSerializer::LoadMap(MemoryBuffer& rawdata,StringBuffer& GUID, StringBuffer& line)
{
    line.append(rawdata.length() -1, rawdata.toByteArray());
    const char* strLine = line.str();
    while(*strLine && *strLine != '\t' && *strLine != '\0')
    {
        GUID.append(*strLine);
        strLine++;
    }
}
void CThorTransferGroup::send(SocketEndpoint &ep, CThorRowArray & group)
{
    ISocket * sendSkt = ISocket::connect_wait(ep, 360*120*1000); // give it plenty of time, sequential in nature *could* be delayed in other side listening
    sendSkt->set_block_mode(BF_SYNC_TRANSFER_PULL,0,TRANSFER_TIMEOUT);
    MemoryBuffer mb;
    group.serialize(serializer,mb,false);
    sendSkt->send_block(mb.toByteArray(),mb.length());
    sendSkt->close();
    sendSkt->Release();
}
Beispiel #7
0
extern DEFTYPE_API void serializeRecordMeta(MemoryBuffer & target, IDefRecordMeta * meta, bool compress)
{
    if (compress)
    {
        MemoryBuffer temp;
        doSerializeRecordMeta(temp, meta);
        compressToBuffer(target, temp.length(), temp.toByteArray());
    }
    else
        doSerializeRecordMeta(target, meta);
}
Beispiel #8
0
void LdapUtils::bin2str(MemoryBuffer& from, StringBuffer& to)
{
    const char* frombuf = from.toByteArray();
    char tmp[3];
    for(unsigned i = 0; i < from.length(); i++)
    {
        unsigned char c = frombuf[i];
        sprintf(tmp, "%02X", c);
        tmp[2] = 0;
        to.append("\\").append(tmp);
    }
}
Beispiel #9
0
void ResourceManager::addManifestFromArchive(IPropertyTree *archive)
{
    if (!archive)
        return;
    if (finalized)
        throwError1(HQLERR_ResourceAddAfterFinalManifest, "MANIFEST");
    ensureManifestInfo();
    Owned<IPropertyTreeIterator> manifests = archive->getElements("AdditionalFiles/Manifest");
    ForEach(*manifests)
    {
        const char *xml = manifests->query().queryProp(NULL);
        Owned<IPropertyTree> manifestSrc = createPTreeFromXMLString(xml);
        Owned<IAttributeIterator> aiter = manifestSrc->getAttributes();
        ForEach (*aiter)
            manifest->setProp(aiter->queryName(), aiter->queryValue());
        StringBuffer manifestDir;
        if (manifestSrc->hasProp("@originalFilename"))
            splitDirTail(manifestSrc->queryProp("@originalFilename"), manifestDir);

        Owned<IPropertyTreeIterator> iter = manifestSrc->getElements("*");
        ForEach(*iter)
        {
            IPropertyTree &item = iter->query();
            if (streq(item.queryName(), "Resource") && item.hasProp("@filename"))
            {
                if (!item.hasProp("@type"))
                    item.setProp("@type", "UNKNOWN");
                const char *filename;
                if (item.hasProp("@originalFilename"))
                    filename = item.queryProp("@originalFilename");
                else
                    filename = item.queryProp("@filename");
                int id;
                if (getDuplicateResourceId(item.queryProp("@type"), NULL, filename, id))
                {
                    item.setPropInt("@id", (int)id);
                    manifest->addPropTree("Resource", LINK(&item));
                }
                else
                {
                    VStringBuffer xpath("AdditionalFiles/Resource[@originalFilename=\"%s\"]", filename);
                    MemoryBuffer content;
                    archive->getPropBin(xpath.str(), content);
                    addCompress(item.queryProp("@type"), content.length(), content.toByteArray(), &item);
                }
            }
            else
                manifest->addPropTree(item.queryName(), LINK(&item));
        }
    }
}
Beispiel #10
0
bool ResourceManager::addCompress(const char * type, unsigned len, const void * data, IPropertyTree *manifestEntry, unsigned id, bool addToManifest)
{
    bool isCompressed=false;
    if (len>=32) //lzw assert if too small
    {
        isCompressed = true;
        MemoryBuffer compressed;
        compressResource(compressed, len, data);
        addNamed(type, compressed.length(), compressed.toByteArray(), manifestEntry, id, addToManifest, isCompressed);
    }
    else
        addNamed(type, len, data, manifestEntry, id, addToManifest, isCompressed);
    return isCompressed;
}
void CRecieveLogSerializer::LoadMap(MemoryBuffer& rawdata,GuidMap& GUIDmap, bool printTrace)
{
    //buffer chould be in the form of 000000030\t2515777767.12_11_03_08_25_29\r
    //we want to extract the GUID only....
    StringBuffer line,GUID;
    line.append(rawdata.length() -1, rawdata.toByteArray());
    const char* strLine = line.str();
    while(*strLine && *strLine != '\t' && *strLine != '\0')
    {
        GUID.append(*strLine);
        strLine++;
    }
    if(printTrace)
        DBGLOG("[%s]", GUID.str());
    GUIDmap[GUID.str()] = "1";
}
int CFileSpraySoapBindingEx::onFinishUpload(IEspContext &ctx, CHttpRequest* request, CHttpResponse* response,   const char *service, const char *method, StringArray& fileNames, StringArray& files, IMultiException *me)
{
    if (!me || (me->ordinality()==0))
    {
        if (ctx.getResponseFormat()==ESPSerializationANY)
        {
            StringBuffer newUrl, netAddress, path;
            request->getParameter("NetAddress", netAddress);
            request->getParameter("Path", path);
            newUrl.appendf("/FileSpray/DropZoneFiles?NetAddress=%s&Path=%s", netAddress.str(), path.str());
            response->redirect(*request, newUrl.str());
        }
        else
        {
            IArrayOf<IEspDFUActionResult> results;
            Owned<CUploadFilesResponse> esp_response = new CUploadFilesResponse("FileSpray");
            ForEachItemIn(i, fileNames)
            {
                const char* fileName = fileNames.item(i);
                Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
                res->setID(fileName);
                res->setAction("Upload File");
                res->setResult("Success");
                results.append(*res.getLink());
            }
            if (!results.length())
            {
                Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
                res->setID("<N/A>");
                res->setAction("Upload File");
                res->setResult("No file uploaded");
                results.append(*res.getLink());
            }
            esp_response->setUploadFileResults(results);

            MemoryBuffer content;
            StringBuffer mimetype;
            esp_response->appendContent(&ctx,content, mimetype);
            response->setContent(content.length(), content.toByteArray());
            response->setContentType(mimetype.str());
            response->send();
        }
    }
    else
    {
void RemoteDataSourceServer::doCmdFetchRaw(bool raw, MemoryBuffer & in, MemoryBuffer & out)
{
    Owned<IFvDataSource> ds = readDataSource(in);
    if (!ds)
    {
        out.append(false);
        return;
    }

    __int64 requestedOffset;
    in.read(requestedOffset);

    MemoryBuffer temp;
    bool ok = ds->fetchRawRow(temp, requestedOffset);
    out.append(ok);                     // ok
    out.append(temp.length());
    out.append(temp.length(), temp.toByteArray());
}
Beispiel #14
0
void ResourceManager::addManifest(const char *filename)
{
    if (finalized)
        throwError1(HQLERR_ResourceAddAfterFinalManifest, "MANIFEST");
    Owned<IPropertyTree> manifestSrc = createPTreeFromXMLFile(filename);

    StringBuffer dir; 
    splitDirTail(filename, dir);

    ensureManifestInfo();
    Owned<IAttributeIterator> aiter = manifestSrc->getAttributes();
    ForEach (*aiter)
        manifest->setProp(aiter->queryName(), aiter->queryValue());
    Owned<IPropertyTreeIterator> iter = manifestSrc->getElements("*");
    ForEach(*iter)
    {
        IPropertyTree &item = iter->query();
        if (streq(item.queryName(), "Resource") && item.hasProp("@filename"))
        {
            if (!item.hasProp("@type"))
                item.setProp("@type", "UNKNOWN");
            const char *filename = item.queryProp("@filename");
            int id;
            if (getDuplicateResourceId(item.queryProp("@type"), filename, id))
            {
                item.setPropInt("@id", id);
                manifest->addPropTree("Resource", LINK(&item));
            }
            else
            {
                StringBuffer fullpath;
                if (!isAbsolutePath(filename))
                    fullpath.append(dir);
                fullpath.append(filename);

                MemoryBuffer content;
                loadResource(fullpath.str(), content);
                addCompress(item.queryProp("@type"), content.length(), content.toByteArray(), &item);
            }
        }
        else
            manifest->addPropTree(item.queryName(), LINK(&item));
    }
}
unsigned CLoggingManager::serializeLogRequestContent(IEspUpdateLogRequestWrap* request, const char* GUID, StringBuffer& logData)
{
    appendXMLTag(logData, LOGREQUEST_GUID, GUID);

    const char* option = request->getOption();
    if (!isEmptyString(option))
        appendXMLTag(logData, LOGREQUEST_OPTION, option);

    appendXMLOpenTag(logData, LOGREQUEST);

    const char* logRequest = request->getUpdateLogRequest();
    MemoryBuffer memBuf;
    LZWCompress(logRequest, strlen(logRequest), memBuf, 0x100);
    JBASE64_Encode(memBuf.toByteArray(), memBuf.length(), logData);

    appendXMLCloseTag(logData, LOGREQUEST);

    return logData.length();
}
Beispiel #16
0
void ResourceManager::addNamed(const char * type, unsigned len, const void * data, IPropertyTree *manifestEntry, unsigned id, bool addToManifest, bool compressed)
{
    if (id==(unsigned)-1)
        id = nextmfid++;
    if (addToManifest)
    {
        if (finalized)
            throwError1(HQLERR_ResourceAddAfterFinalManifest, type);
        Owned<IPropertyTree> entry=createPTree("Resource");
        entry->setProp("@type", type);
        entry->setPropInt("@id", id);
        entry->setPropBool("@compressed", compressed);
        entry->setPropBool("@header", true);
        if (manifestEntry)
            mergePTree(entry, manifestEntry);
        ensureManifestInfo()->addPropTree("Resource", entry.getClear());
    }
    MemoryBuffer mb;
    appendResource(mb, len, data, compressed);
    resources.append(*new ResourceItem(type, id, mb.length(), mb.toByteArray()));
}
Beispiel #17
0
CXRefNode::CXRefNode(IPropertyTree* pTreeRoot)
{
    m_bChanged = false;
    try
    {
        m_XRefTree.set(pTreeRoot);
        pTreeRoot->getProp("@name",m_origName);
        //load up our tree with the data.....if there is data
        MemoryBuffer buff;
        pTreeRoot->getPropBin("data",buff);
        if (buff.length())
        {
            m_dataStr.append(buff.length(),buff.toByteArray());
        }
        //lets check to ensure we have the correct children inplace(Orphan,lost,found)
    }
    catch(...)
    {
        ERRLOG("Error in creation of XRefNode...");
    }
}
Beispiel #18
0
void doStuff()
{
    Owned<IRemoteConnection> conn = querySDS().connect("/Orbit", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, DALI_TIMEOUT);
    IPropertyTree *root = conn->queryRoot();
    StringBuffer s;
    if (root->getProp("TestBranch1",s)) {
        printf("TestBranch1: read %s\n",s.str());
    }
    else {

        // save as string
        printf("TestBranch1: set (as string)\n",s.str());
        root->setProp("TestBranch1",MyTestXML);
    }
    MemoryBuffer m;
    if (root->getPropBin("TestBranch2",m)) {
        m.append((byte)0); // add a NULL to returned data
        const char *str = m.toByteArray();  
        printf("TestBranch2: read %s\n",str);
    }
    else {
        // save as raw binary
        printf("TestBranch2: set (as blob)\n",s.str());
        root->setPropBin("TestBranch2",strlen(MyTestXML),MyTestXML); // include NULL
    }
    IPropertyTree *br3 = root->queryPropTree("TestBranch3");
    if (br3) {
        printf("read TestBranch3 as tree\n");
        printf("Hello = %s\n",br3->queryProp("Hello"));
        int n = br3->getPropInt("Bye/@num");        // update
        printf("Bye num = %d\n",n);
        br3->setPropInt("Bye/@num",n+1);
    }
    else {
        // save as tree
        printf("TestBranch3: set (as tree)\n",s.str());
        br3 =  createPTreeFromXMLString(MyTestXML); // parses and creates object tree
        root->setPropTree("TestBranch3", br3);
    }
}
Beispiel #19
0
StringBuffer &CXRefNode::serializeDirectories(StringBuffer &buf)
{
    if(!m_directories.get())
    {
        IPropertyTree* directoriesBranch = m_XRefTree->queryPropTree("Directories");
        if(directoriesBranch == 0)
        {
            directoriesBranch = m_XRefTree->addPropTree("Directories",createPTree());
            commit();
        }
        StringBuffer tmpbuf;
        m_directories.set(directoriesBranch);
    }
    buf.clear();
    MemoryBuffer data;
    m_directories->getPropBin("data",data);
    if (data.length())
    {
        buf.append(data.length(),data.toByteArray());
    }
    return buf;
}
void ResourceManifest::addToArchive(IPropertyTree *archive)
{
    IPropertyTree *additionalFiles = ensurePTree(archive, "AdditionalFiles");

    //xsi namespace required for proper representaion after PTree::setPropBin()
    if (!additionalFiles->hasProp("@xmlns:xsi"))
        additionalFiles->setProp("@xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

    Owned<IPropertyTreeIterator> resources = manifest->getElements("Resource[@resourcePath]");
    ForEach(*resources)
    {
        IPropertyTree &item = resources->query();
        const char *respath = item.queryProp("@resourcePath");

        VStringBuffer xpath("Resource[@resourcePath='%s']", respath);
        if (!additionalFiles->hasProp(xpath.str()))
        {
            IPropertyTree *resTree = additionalFiles->addPropTree("Resource", createPTree("Resource"));

            const char *filepath = item.queryProp("@originalFilename");
            resTree->setProp("@originalFilename", filepath);
            resTree->setProp("@resourcePath", respath);

            MemoryBuffer content;
            loadResource(filepath, content);
            resTree->setPropBin(NULL, content.length(), content.toByteArray());
        }
    }

    StringBuffer xml;
    toXML(manifest, xml);

    IPropertyTree *manifest = additionalFiles->addPropTree("Manifest", createPTree("Manifest", ipt_none));
    manifest->setProp("@originalFilename", absFilename.str());
    manifest->setProp(NULL, xml.str());
}
void CLogSerializer::splitLogRecord(MemoryBuffer& rawdata, StringBuffer& GUID, StringBuffer& line)//
{
    //send log buffer should be in the form of 2635473460.05_01_12_16_13_57\t<cache>...</cache>
    //parse it into GUID and line (as <cache>...</cache>)

    //receive log buffer should be in the form of 2515777767.12_11_03_08_25_29\t
    //we want to extract the GUID only

    const char* begin = rawdata.toByteArray(); //no string termination character \0
    int len = rawdata.length();
    if (begin && len>0)
    {
        const char* p = begin;
        const char* end = begin + len;

        while (*p && *p != '\t' && p < end)
            p++;

        GUID.append(p-begin, begin);

        if (++p < end)
            line.append(end-p, p);
    }
}
size32_t ThorCompress(MemoryBuffer & src, MemoryBuffer & dest, size32_t threshold)
{
    return ThorCompress((const void *)src.toByteArray(), src.length(), dest, threshold);
}
	bool loadPwds()
	{
		try
		{
			if (!pwFile.length())
				throw MakeStringException(-1, "htpasswd Password file not specified");

			Owned<IFile> file = createIFile(pwFile.str());
			if (!file->exists())
			{
				userMap.kill();
				throw MakeStringException(-1, "htpasswd Password file does not exist");
			}

			bool isDir;
			offset_t size;
			CDateTime whenChanged;
			file->getInfo(isDir,size,whenChanged);
			if (isDir)
			{
				userMap.kill();
				throw MakeStringException(-1, "htpasswd Password file specifies a directory");
			}
			if (0 == whenChanged.compare(pwFileLastMod))
				return true;//Don't reload if file unchanged
			userMap.kill();
			OwnedIFileIO io = file->open(IFOread);
			if (!io)
				throw MakeStringException(-1, "htpasswd Unable to open Password file");

			MemoryBuffer mb;
			size32_t count = read(io, 0, (size32_t)-1, mb);
			if (0 == count)
				throw MakeStringException(-1, "htpasswd Password file is empty");

			mb.append((char)NULL);
			char * p = (char*)mb.toByteArray();
			char *saveptr;
			const char * seps = "\f\r\n";
			char * next = strtok_r(p, seps, &saveptr);
			if (next)
			{
				do
				{
					char * colon = strchr(next,':');
					if (NULL == colon)
						throw MakeStringException(-1, "htpasswd Password file appears malformed");
					*colon = (char)NULL;
					userMap.setValue(next, colon+1);//username, enctypted password
				} while (next = strtok_r(NULL, seps, &saveptr));
			}

			io->close();
			pwFileLastMod = whenChanged;//remember when last changed
		}
		catch(IException*)
		{
			throw MakeStringException(-1, "htpasswd Exception accessing Password file");
		}
		return true;
	}
Beispiel #24
0
void ResourceManager::addManifestFile(const char *filename)
{
    Owned<IPropertyTree> manifestSrc = createPTreeFromXMLFile(filename);

    StringBuffer dir; 
    splitDirTail(filename, dir);

    ensureManifestInfo();
    Owned<IAttributeIterator> aiter = manifestSrc->getAttributes();
    ForEach (*aiter)
        manifest->setProp(aiter->queryName(), aiter->queryValue());
    Owned<IPropertyTreeIterator> iter = manifestSrc->getElements("*");
    ForEach(*iter)
    {
        IPropertyTree &item = iter->query();
        if (streq(item.queryName(), "Include") && item.hasProp("@filename"))
            addManifestInclude(item, dir.str());
        else if (streq(item.queryName(), "Resource") && item.hasProp("@filename"))
        {
            StringBuffer filepath;
            StringBuffer respath;
            makeAbsolutePath(item.queryProp("@filename"), dir.str(), filepath);
            makePathUniversal(filepath.str(), respath);

            item.setProp("@originalFilename", filepath.str());
            item.setProp("@resourcePath", respath.str());

            if (containsFileWildcard(filepath))
            {
                StringBuffer wildpath;
                const char *tail = splitDirTail(filepath, wildpath);
                expandManifestDirectory(manifestSrc, item, dir, wildpath, tail, item.getPropBool("@recursive"));
                manifestSrc->removeTree(&item);
            }

        }
        else
            manifest->addPropTree(item.queryName(), LINK(&item));
    }

    Owned<IPropertyTreeIterator> resources = manifestSrc->getElements("Resource[@filename]");
    ForEach(*resources)
    {
        IPropertyTree &item = resources->query();

        if (!item.hasProp("@type"))
            item.setProp("@type", "UNKNOWN");
        int id;
        if (getDuplicateResourceId(item.queryProp("@type"), item.queryProp("@resourcePath"), NULL, id))
        {
            item.setPropInt("@id", id);
            manifest->addPropTree("Resource", LINK(&item));
        }
        else
        {
            MemoryBuffer content;
            loadResource(item.queryProp("@originalFilename"), content);
            addCompress(item.queryProp("@type"), content.length(), content.toByteArray(), &item);
        }
    }
}
Beispiel #25
0
void VarElemArray::serializeCompress(MemoryBuffer &mb)
{
    MemoryBuffer exp;
    serialize(exp);
    fastLZCompressToBuffer(mb,exp.length(),exp.toByteArray());
}
Beispiel #26
0
int readResults(ISocket * socket, bool readBlocked, bool useHTTP, StringBuffer &result, const char *query, size32_t queryLen)
{
    if (readBlocked)
        socket->set_block_mode(BF_SYNC_TRANSFER_PULL,0,60*1000);

    MemoryBuffer remoteReadCursorMb;
    unsigned len;
    bool is_status;
    bool isBlockedResult;
    for (;;)
    {
        if (delay)
            MilliSleep(delay);
        is_status = false;
        isBlockedResult = false;
        try
        {
            if (useHTTP)
                len = 0x10000;
            else if (readBlocked)
                len = socket->receive_block_size();
            else
            {
                socket->read(&len, sizeof(len));
                _WINREV(len);                    
            }
        }
        catch(IException * e)
        {
            if (manyResults)
                showMessage("End of result multiple set\n");
            else
                pexception("failed to read len data", e);
            e->Release();
            return 1;
        }

        if (len == 0)
        {
            if (manyResults)
            {
                showMessage("----End of result set----\n");
                continue;
            }
            break;
        }

        bool isSpecial = false;
        bool pluginRequest = false;
        bool dataBlockRequest = false;
        bool remoteReadRequest = false;
        if (len & 0x80000000)
        {
            unsigned char flag;
            isSpecial = true;
            socket->read(&flag, sizeof(flag));
            switch (flag)
            {
            case '-':
                if (echoResults)
                    fputs("Error:", stdout);
                if (saveResults && trace != NULL)
                    fputs("Error:", trace);
                break;
            case 'D':
                showMessage("request for datablock\n");
                dataBlockRequest = true;
                break;
            case 'P':
                showMessage("request for plugin\n");
                pluginRequest = true;
                break;
            case 'S':
                 if (showStatus)
                 showMessage("Status:");
                 is_status=true;
                 break;
            case 'T':
                 showMessage("Timing:\n");
                 break;
            case 'X':
                showMessage("---Compound query finished---\n");
                return 1;
            case 'R':
                isBlockedResult = true;
                break;
            case 'J':
                remoteReadRequest = true;
                break;
            }
            len &= 0x7FFFFFFF;
            len--;      // flag already read
        }

        MemoryBuffer mb;
        mb.setEndian(__BIG_ENDIAN);
        char *mem = (char *)mb.reserveTruncate(len+1);
        char * t = mem;
        size32_t sendlen = len;
        t[len]=0;
        try
        {
            if (useHTTP)
            {
                try
                {
                    socket->read(t, 0, len, sendlen);
                }
                catch (IException *E)
                {
                    if (E->errorCode()!= JSOCKERR_graceful_close)
                        throw;
                    E->Release();
                    break;
                }
                if (!sendlen)
                    break;
            }
            else if (readBlocked)
                socket->receive_block(t, len); 
            else
                socket->read(t, len);
        }
        catch(IException * e)
        {
            pexception("failed to read data", e);
            e->Release();
            return 1;
        }
        if (pluginRequest)
        {
            //Not very robust!  A poor man's implementation for testing...
            StringBuffer dllname, libname;
            const char * dot = strchr(t, '.');
            dllname.append("\\edata\\bin\\debug\\").append(t);
            libname.append("\\edata\\bin\\debug\\").append(dot-t,t).append(".lib");

            sendFile(dllname.str(), socket);
            sendFile(libname.str(), socket);
        }
        else if (dataBlockRequest)
        {
            //Not very robust!  A poor man's implementation for testing...
            offset_t offset;
            mb.read(offset);
            sendFileChunk((const char *)mb.readDirect(offset), offset, socket);
        }
        else if (remoteReadRequest)
        {
            Owned<IPropertyTree> requestTree = createPTreeFromJSONString(queryLen, query);
            Owned<IPropertyTree> responseTree; // used if response is xml or json
            const char *outputFmtStr = requestTree->queryProp("format");
            const char *response = nullptr;
            if (!outputFmtStr || strieq("xml", outputFmtStr))
            {
                response = (const char *)mb.readDirect(len);
                responseTree.setown(createPTreeFromXMLString(len, response));
            }
            else if (strieq("json", outputFmtStr))
            {
                response = (const char *)mb.readDirect(len);
                responseTree.setown(createPTreeFromJSONString(len, response));
            }
            unsigned cursorHandle;
            if (responseTree)
                cursorHandle = responseTree->getPropInt("cursor");
            else
                mb.read(cursorHandle);
            bool retrySend = false;
            if (cursorHandle)
            {
                PROGLOG("Got handle back: %u; len=%u", cursorHandle, len);
                StringBuffer xml;
                if (responseTree)
                {
                    if (echoResults && response)
                    {
                        fputs(response, stdout);
                        fflush(stdout);
                    }
                    if (!responseTree->getPropBin("cursorBin", remoteReadCursorMb.clear()))
                        break;
                }
                else
                {
                    size32_t dataLen;
                    mb.read(dataLen);
                    if (!dataLen)
                        break;
                    const void *rowData = mb.readDirect(dataLen);
                    // JCSMORE - output binary row data?

                    // cursor
                    size32_t cursorLen;
                    mb.read(cursorLen);
                    if (!cursorLen)
                        break;
                    const void *cursor = mb.readDirect(cursorLen);
                    memcpy(remoteReadCursorMb.clear().reserveTruncate(cursorLen), cursor, cursorLen);
                }

                if (remoteStreamForceResend)
                    cursorHandle = NotFound; // fake that it's a handle dafilesrv doesn't know about

                Owned<IPropertyTree> requestTree = createPTree();
                requestTree->setPropInt("cursor", cursorHandle);

                // Only the handle is needed for continuation, but this tests the behaviour of some clients which may send cursor per request (e.g. to refresh)
                if (remoteStreamSendCursor)
                    requestTree->setPropBin("cursorBin", remoteReadCursorMb.length(), remoteReadCursorMb.toByteArray());

                requestTree->setProp("format", outputFmtStr);
                StringBuffer requestStr;
                toJSON(requestTree, requestStr);
#ifdef _DEBUG
                fputs(requestStr, stdout);
#endif

                sendlen = requestStr.length();
                _WINREV(sendlen);

                try
                {
                    if (!rawSend && !useHTTP)
                        socket->write(&sendlen, sizeof(sendlen));
                    socket->write(requestStr.str(), requestStr.length());
                }
                catch (IJSOCK_Exception *e)
                {
                    retrySend = true;
                    EXCLOG(e, nullptr);
                    e->Release();
                }
            }
            else // dafilesrv didn't know who I was, resent query + serialized cursor
                retrySend = true;
            if (retrySend)
            {
                PROGLOG("Retry send for handle: %u", cursorHandle);
                requestTree->setPropBin("cursorBin", remoteReadCursorMb.length(), remoteReadCursorMb.toByteArray());
                StringBuffer requestStr;
                toJSON(requestTree, requestStr);

                PROGLOG("requestStr = %s", requestStr.str());
                sendlen = requestStr.length();
                _WINREV(sendlen);
                if (!rawSend && !useHTTP)
                    socket->write(&sendlen, sizeof(sendlen));
                socket->write(requestStr.str(), requestStr.length());
            }
        }
        else
        {
            if (isBlockedResult)
            {
                t += 8;
                t += strlen(t)+1;
                sendlen -= (t - mem);
            }
            if (echoResults && (!is_status || showStatus))
            {
                fwrite(t, sendlen, 1, stdout);
                fflush(stdout);
            }
            if (!is_status)
                result.append(sendlen, t);
        }

        if (abortAfterFirst)
            return 0;
    }
    return 0;
}
Beispiel #27
-4
void sendFileChunk(const char * filename, offset_t offset, ISocket * socket) 
{
    FILE *in = fopen(filename, "rb");
    unsigned size = 0;
    void * buff = NULL;

    if (in)
    {
        fseek(in, 0, SEEK_END);
        offset_t endOffset = ftell(in);
        fseek(in, offset, SEEK_SET);
        if (endOffset < offset)
            size = 0;
        else
            size = (unsigned)(endOffset - offset);
        if (size > CHUNK_SIZE)
            size = CHUNK_SIZE;
        buff = malloc(size);
        size_t numRead = fread(buff, 1, size, in);
        fclose(in);
        if (numRead != size)
		{
            printf("read from file %s failed (%u/%u)\n", filename, (unsigned)numRead, size);
            size = 0;
		}
    }
	else
		printf("read from file %s failed\n", filename);


    if (size > 0)
    {
        MemoryBuffer sendBuffer;
        unsigned rev = size + strlen(filename) + 10;
        rev |= 0x80000000;
        _WINREV(rev);
        sendBuffer.append(rev);
        sendBuffer.append('R');
        rev = 0; // should put the sequence number here
        _WINREV(rev);
        sendBuffer.append(rev);
        rev = 0; // should put the # of recs in msg here
        _WINREV(rev);
        sendBuffer.append(rev);
        sendBuffer.append(strlen(filename)+1, filename);
        sendBuffer.append(size, buff);

        socket->write(sendBuffer.toByteArray(), sendBuffer.length());
    }
    else
    {
        unsigned zeroLen = 0;
        socket->write(&zeroLen, sizeof(zeroLen));
    }

    free(buff);
}