Ejemplo n.º 1
0
Archivo: mpd.c Proyecto: avih/miniweb
int uhMpd(UrlHandlerParam* param)
{
	char *action;
	char *pbuf = param->pucBuffer;
	int bufsize = param->iDataBytes;
	HTTP_XML_NODE node;

	mwParseQueryString(param);
	action = mwGetVarValue(param->pxVars, "action", 0);

	mwWriteXmlHeader(&pbuf, &bufsize, 10, "gb2312", mwGetVarValue(param->pxVars, "xsl", 0));
	mwWriteXmlString(&pbuf, &bufsize, 0, "<response>");

	node.indent = 1;
	node.name = "state";
	node.fmt = "%s";
	node.flags = 0;

	if (!strcmp(param->pucRequest + 1, "playlist")) {
		PL_ENTRY *ptr;
		int i;
		if (!action) {
		} else if (!strcmp(action, "add")) {
			char *filename = mwGetVarValue(param->pxVars, "stream", 0);
			char *title = mwGetVarValue(param->pxVars, "title", 0);
			if (!title) title = "";
			if (!filename) {
				node.value = "error";
			} else {
				int fnlen;
				int titlelen;
				char *entrydata;
				mwDecodeString(filename);
				fnlen = strlen(filename);
				titlelen = strlen(title);
				entrydata = (char*)malloc(fnlen + titlelen + 3);
				strcpy(entrydata, filename);
				strcpy(entrydata + fnlen + 1, title);
				node.value = plAddEntry(&playlist, entrydata, fnlen + titlelen + 2) ? "OK" : "error";
			}
			mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
		} else if (!strcmp(action, "pin")) {
			int index = mwGetVarValueInt(param->pxVars, "arg", 0);
			node.value = plPinEntryByIndex(&playlist, index) ? "OK" : "error";
			mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
		} else if (!strcmp(action, "del")) {
			int index = mwGetVarValueInt(param->pxVars, "arg", 0);
			node.value = plDelEntryByIndex(&playlist, index) ? "OK" : "error";
			mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
		}
		ptr = playlist;
		mwWriteXmlString(&pbuf, &bufsize, 1, "<playlist>");
		for (i=0; ptr; ptr = ptr->next, i++) {
			char buf[32];
			snprintf(buf, sizeof(buf), "<item index=\"%03d\">", i);
			mwWriteXmlString(&pbuf, &bufsize, 2, buf);

			node.indent = 3;
			node.name = "stream";
			node.fmt = "%s";
			node.value = ptr->data;
			mwWriteXmlLine(&pbuf, &bufsize, &node, 0);

			node.flags = XN_CDATA;
			node.name = "title";
			node.value = (char*)ptr->data + strlen(ptr->data) + 1;
			mwWriteXmlLine(&pbuf, &bufsize, &node, 0);

			mwWriteXmlString(&pbuf, &bufsize, 2, "</item>");
		}
		mwWriteXmlString(&pbuf, &bufsize, 1, "</playlist>");
	} else if (!action) {

	} else if (!strcmp(action, "play")) {
		char *filename = mwGetVarValue(param->pxVars, "stream", 0);
		char *args = mwGetVarValue(param->pxVars, "arg", 0);
		if (filename && *filename) {
			mwDecodeString(filename);
			filename = strdup(filename);
		}
		if ((!filename || !*filename) || plAddEntry(&playlist, filename, strlen(filename) + 1)) {
			if (!mpThreadHandle) {
				if (args) mwDecodeString(args);
				ThreadCreate(&mpThreadHandle, mpThread, args);
			} else {
				mpClose();
			}
			node.value = "OK";
		} else {
			node.value = "error";
		}
		mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
	} else if (!strcmp(action, "query")) {
		int i;
		char* info;
		for (i = 0; info = mwGetVarValue(param->pxVars, "info", i); i++) {
			if (!strcmp(info, "pos")) {
				node.name = "pos";
				node.fmt = "%d";
				node.value = (void*)mpPos;
				mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
			} else if (!strcmp(info, "state")) {
				node.name = "state";
				node.fmt = "%s";
				node.value = states[mpState];
				mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
			}
		}
	} else if (!strcmp(action, "pause")) {
		mpState = (mpState == MP_PLAYING ? MP_PAUSED : MP_PLAYING);
		mpCommand("pause");
	} else if (!strcmp(action, "seek")) {
		char buf[32];
		char *args = mwGetVarValue(param->pxVars, "arg", 0);
		if (args) {
			snprintf(buf, sizeof(buf), "seek %s", args);
			mpCommand(buf);
		}
	} else if (!strcmp(action, "command")) {
		char *cmd = mwGetVarValue(param->pxVars, "arg", 0);
		char *hasResult = mwGetVarValue(param->pxVars, "result", 0);
		if (cmd) {
			if (mpCommand(cmd) > 0) {
				node.value = "OK";
				if (hasResult) {
					int bytes;
					MutexLock(&mpConsoleMutex);

					bytes = snprintf(pbuf, bufsize,  "  <result><![CDATA[");
					pbuf += bytes;
					bufsize -= bytes;

					bytes = mpRead(pbuf, bufsize);
					MutexUnlock(&mpConsoleMutex);
					if (bytes > 0) {
						pbuf += bytes;
						bufsize -= bytes;
					}

					bytes = snprintf(pbuf, bufsize,  "]]></result>");
					pbuf += bytes;
					bufsize -= bytes;
				}
			} else {
				node.value = "error";
			}
			mwWriteXmlLine(&pbuf, &bufsize, &node, 0);

		}
	} else {
		return 0;
	}

	mwWriteXmlString(&pbuf, &bufsize, 0, "</response>");

	param->iDataBytes=(int)(pbuf-param->pucBuffer);
	param->fileType=HTTPFILETYPE_XML;
	return FLAG_DATA_RAW;
}
Ejemplo n.º 2
0
int uhVod(UrlHandlerParam* param)
{
    HTTP_XML_NODE node;
    char *req=param->pucRequest;
    char *pbuf = param->pucBuffer;
    int bufsize = param->dataBytes;
    char *action;
    PL_ENTRY *ptr;
    VOD_CTX* ctx;
    int i;

    if (*req && *req != '?') return 0;
    node.indent = 1;
    node.name = "state";
    node.fmt = "%s";
    node.flags = 0;
    mwWriteXmlHeader(&pbuf, &bufsize, 10, "gb2312", mwGetVarValue(param->pxVars, "xsl", 0));
    mwWriteXmlString(&pbuf, &bufsize, 0, "<response>");

    mwParseQueryString(param);

    ctx = GetVodContext(param->hs->ipAddr.laddr);

    action = mwGetVarValue(param->pxVars, "action", 0);

    if (!action)
    {
        int count = mwGetVarValueInt(param->pxVars, "count", -1);
        ptr = ctx->playlist;
        mwWriteXmlString(&pbuf, &bufsize, 1, "<playlist>");
        for (i=0; ptr && (unsigned int)i < (unsigned int)count; ptr = ptr->next, i++)
        {
            char buf[32];
            snprintf(buf, sizeof(buf), "<item index=\"%03d\">", i);
            mwWriteXmlString(&pbuf, &bufsize, 2, buf);

            node.indent = 3;
            node.name = "stream";
            node.fmt = "%s";
            node.value = ptr->data;
            mwWriteXmlLine(&pbuf, &bufsize, &node, 0);

            node.flags = XN_CDATA;
            node.name = "title";
            node.value = (char*)ptr->data + strlen(ptr->data) + 1;
            mwWriteXmlLine(&pbuf, &bufsize, &node, 0);

            mwWriteXmlString(&pbuf, &bufsize, 2, "</item>");
        }
        mwWriteXmlString(&pbuf, &bufsize, 1, "</playlist>");
    }
    else if (!strcmp(action, "add"))
    {
        char *filename = mwGetVarValue(param->pxVars, "stream", 0);
        char *title = mwGetVarValue(param->pxVars, "title", 0);
        if (!title) title = "";
        if (!filename)
        {
            node.value = "error";
        }
        else
        {
            int fnlen;
            int titlelen;
            char *entrydata;
            mwDecodeString(filename);
            fnlen = strlen(filename);
            titlelen = strlen(title);
            entrydata = (char*)malloc(fnlen + titlelen + 3);
            strcpy(entrydata, filename);
            strcpy(entrydata + fnlen + 1, title);
            node.value = plAddEntry(&ctx->playlist, entrydata, fnlen + titlelen + 2) ? "OK" : "error";
        }
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
    }
    else if (!strcmp(action, "pin"))
    {
        int index = mwGetVarValueInt(param->pxVars, "arg", 0);
        node.value = plPinEntryByIndex(&ctx->playlist, index) ? "OK" : "error";
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
    }
    else if (!strcmp(action, "del"))
    {
        int index = mwGetVarValueInt(param->pxVars, "arg", 0);
        void* data = plDelEntryByIndex(&ctx->playlist, index) ? "OK" : "error";
        if (data)
        {
            free(data);
            node.value = "OK";
        }
        else
        {
            node.value = "error";
        }
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
    }
    else if (!strcmp(action, "play"))
    {
        char* stream = (char*)plGetEntry(&ctx->playlist);
        if (stream)
        {
            node.name = "stream";
        }
        node.name = "stream";
        node.value = stream ? stream : vodloop;
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
        node.name = "control";
        node.fmt = "%s/vodplay?action=control";
        node.value = vodhost;
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
        if (stream) free(stream);
        ctx->nextaction = 0;
    }
    else if (!strcmp(action, "control"))
    {
        int arg = mwGetVarValueInt(param->pxVars, "arg", 0);
        if (arg)
            ctx->nextaction = arg;
        else if (ctx->nextaction)
        {
            node.name = "action";
            node.fmt = "%d";
            node.value = (void*)ctx->nextaction;
            mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
            ctx->nextaction = 0;
        }
    }

#if 0
    arg = mwGetVarValue(param->pxVars, "arg", 0);
    id = mwGetVarValue(param->pxVars, "id", 0);
    switch (GETDWORD(param->pucRequest + 1))
    {
    case DEFDWORD('n','o','p',0):
        strcpy(pbuf,"state=OK");
        break;
    case DEFDWORD('c','m','d',0):
        //action=ACT_SKIP;
        strcpy(pbuf,"Play next");
        param->dataBytes=9;
        return FLAG_DATA_RAW;
    case DEFDWORD('l','i','s','t'):
    {
        PL_ENTRY *ptr = plhdr[session];
        int i;
        mwWriteXmlString(&pbuf, &bufsize, 1, "<playlist>");
        for (i=0; ptr; ptr = ptr->next, i++)
        {
            OutputItemInfo(&pbuf, &bufsize, ptr->data);
        }
        mwWriteXmlString(&pbuf, &bufsize, 1, "</playlist>");
    }
    break;
    case DEFDWORD('a','d','d',0):
    {
        node.name = "state";
        if (plFindEntry(plhdr[session],(void*)id, strlen(id)))
        {
            OutputItemInfo(&pbuf, &bufsize, id);
            node.value = "ordered";
            mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
            break;
        }
        if (plAddEntry(&plhdr[session], strdup(id), strlen(id) + 1))
        {
            node.value =  "OK";
            OutputItemInfo(&pbuf, &bufsize, id);
        }
        else
        {
            node.value =  "error";
        }
        node.name = "state";
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
    }
    break;
    case DEFDWORD('d','e','l',0):
        OutputItemInfo(&pbuf, &bufsize, id);
        node.name = "state";
        node.value = plDelEntry(&plhdr[session],(void*)id) ? "OK" : "error";
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
        break;
    case DEFDWORD('p','i','n',0):
        OutputItemInfo(&pbuf, &bufsize, id);
        node.name = "state";
        node.value = plPinEntry(&plhdr[session],(void*)id) ? "OK" : "error";
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
        break;
    case DEFDWORD('p','l','a','y'):
    {
        void* data = plGetEntry(&plhdr[session]);
        node.name = "state";
        if (data)
        {
            OutputItemInfo(&pbuf, &bufsize, data);
            node.value = "OK";
            free(data);
        }
        else
        {
            node.value = "error";
        }
        mwWriteXmlLine(&pbuf, &bufsize, &node, 0);
    }
    break;
    default:
        strcpy(pbuf,"Invalid request");
    }
#endif
    mwWriteXmlString(&pbuf, &bufsize, 0, "</response>");
    param->dataBytes=(int)(pbuf-param->pucBuffer);
    param->fileType=HTTPFILETYPE_XML;
    return FLAG_DATA_RAW;
}
Ejemplo n.º 3
0
//////////////////////////////////////////////////////////////////////////
// callback from the web server whenever a valid request comes in
//////////////////////////////////////////////////////////////////////////
int uhStats(UrlHandlerParam* param)
{
	char *p;
	char buf[384];
	HttpStats *stats=&((HttpParam*)param->hp)->stats;
	HttpRequest *req=&param->hs->request;
	IPADDR ip = param->hs->ipAddr;
	HTTP_XML_NODE node;
	int bufsize = param->dataBytes;
	int ret=FLAG_DATA_RAW;

	mwGetHttpDateTime(time(NULL), buf, sizeof(buf));

	if (stats->clientCount>4) {
		param->pucBuffer=(char*)malloc(stats->clientCount*256+1024);
		ret=FLAG_DATA_RAW | FLAG_TO_FREE;
	}

	p=param->pucBuffer;

	//generate XML
	mwWriteXmlHeader(&p, &bufsize, 10, 0, 0);

	mwWriteXmlString(&p, &bufsize, 0, "<ServerStats>");

	sprintf(buf, "%d.%d.%d.%d", ip.caddr[3], ip.caddr[2], ip.caddr[1], ip.caddr[0]);

	node.indent = 1;
	node.fmt = "%s";
	node.name = "ClientIP";
	node.value = buf;
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%d";
	node.name = "UpTime";
	node.value = (void*)(time(NULL)-stats->startTime);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%d";
	node.name = "Clients";
	node.value = (void*)(stats->clientCount);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%d";
	node.name = "ExpireTimeout";
	node.value = (void*)(stats->timeOutCount);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%d";
	node.name = "MaxClients";
	node.value = (void*)(stats->clientCountMax);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%u";
	node.name = "Requests";
	node.value = (void*)(stats->reqCount);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%u";
	node.name = "FileSent";
	node.value = (void*)(stats->fileSentCount);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	node.fmt = "%u";
	node.name = "FileSentMB";
	node.value = (void*)(stats->fileSentBytes >> 20);
	mwWriteXmlLine(&p, &bufsize, &node, 0);

	mwWriteXmlString(&p, &bufsize, 1, "<Clients>");

	{
		HttpSocket *phsSocketCur;
		time_t curtime=time(NULL);
		int i;
		for (i = 0; i < ((HttpParam*)param->hp)->maxClients; i++) {
			phsSocketCur = ((HttpParam*)param->hp)->hsSocketQueue + i;
			if (!phsSocketCur->socket) continue;
			ip=phsSocketCur->ipAddr;
			sprintf(buf,"<Client ip=\"%d.%d.%d.%d\" requests=\"%d\" expire=\"%d\" speed=\"%u\" path=\"%s\"/>",
				ip.caddr[3],ip.caddr[2],ip.caddr[1],ip.caddr[0], phsSocketCur->iRequestCount, (int)(phsSocketCur->tmExpirationTime - curtime),
				(unsigned int)(phsSocketCur->response.sentBytes / (((curtime - phsSocketCur->tmAcceptTime) << 10) + 1)), phsSocketCur->request.pucPath);
			mwWriteXmlString(&p, &bufsize, 2, buf);
			/*
			if (phsSocketCur->request.pucPath)
				p+=sprintf(p,"(%d/%d)",phsSocketCur->response.iSentBytes,phsSocketCur->response.iContentLength);
			else
				p+=sprintf(p,"(idle)");
			*/
		}
	}

	mwWriteXmlString(&p, &bufsize, 1, "</Clients>");
	mwWriteXmlString(&p, &bufsize, 0, "</ServerStats>");

	//return data to server
	param->dataBytes=(int)p-(int)(param->pucBuffer);
	param->fileType=HTTPFILETYPE_XML;
	return ret;
}
Ejemplo n.º 4
0
int uhLib(UrlHandlerParam* param)
{
    char *pbuf;
    int bufsize;
    char buf[256];
    char *id;
    char *p;
    int from, count;

    p = strstr(param->pucRequest, "back=");
    if (p) *p = 0;
    mwParseQueryString(param);

    id = mwGetVarValue(param->pxVars, "id", 0);
    from = mwGetVarValueInt(param->pxVars, "from", 0);
    count = mwGetVarValueInt(param->pxVars, "count", -1);

    if (count <= 0)
        bufsize = 1024 * 1024;
    else
        bufsize = count * 256;

    pbuf = (char*)malloc(bufsize);
    param->pucBuffer = pbuf;

    mwWriteXmlHeader(&pbuf, &bufsize, 10, "gb2312", mwGetVarValue(param->pxVars, "xsl", 0));
    mwWriteXmlString(&pbuf, &bufsize, 0, "<response>");

    if (!strcmp(param->pucRequest, "/category"))
    {
        int mincount = mwGetVarValueInt(param->pxVars, "min", 2);
        int catid = id ? atoi(id) : -1;
        int hash = mwGetVarValueInt(param->pxVars, "hash", -1);
        char* name = mwGetVarValue(param->pxVars, "name", 0);
        CATEGORY_INFO* cat;
        int i = 0;
        int idx = 0;
        for (cat = &cats; cat; cat = cat->next, i++)
        {
            if ((hash >= 0 && hash != cat->hash) || (name && strcmp(name, cat->name)) || (catid >= 0 && catid != i) || cat->count < mincount)
                continue;

            if (idx >= from)
            {
                if ((count--) == 0) break;

                snprintf(buf, sizeof(buf), "<category id=\"%d\" hash=\"%02d\" index=\"%03d\">", i, cat->hash, idx);
                mwWriteXmlString(&pbuf, &bufsize, 1, buf);

                snprintf(buf, sizeof(buf), "<name><![CDATA[%s]]></name>", cat->name);
                mwWriteXmlString(&pbuf, &bufsize, 1, buf);

                snprintf(buf, sizeof(buf), "<clips>%d</clips>", cat->count);
                mwWriteXmlString(&pbuf, &bufsize, 1, buf);

                mwWriteXmlString(&pbuf, &bufsize, 1, "</category>");
            }
            idx++;
        }
    }
    else if (!strcmp(param->pucRequest, "/title"))
    {
        int hash = id ? atoi(id) : -1;
        int chars = mwGetVarValueInt(param->pxVars, "chars", 0);
        char* catname = mwGetVarValue(param->pxVars, "category", 0);
        int catid = mwGetVarValueInt(param->pxVars, "catid", -1);
        int i = 0;
        int idx = 0;
        BOOL matched = 0;
        CLIP_INFO* info;
        CATEGORY_INFO* cat;
        int pos = 1;
        for (cat = &cats; cat; cat = cat->next, i++)
        {
            if ((catid >= 0 && catid != i) || (catname && strcmp(catname, cat->name))) continue;
            for (info = cat->clips; info; info = info->next)
            {
                if ((hash >= 0 && hash != info->hash) || (chars && info->chars != chars)) continue;
                if (idx >= from)
                {
                    if (count == 0) break;
                    count--;
                    if (!matched)
                    {
                        snprintf(buf, sizeof(buf), "<category name=\"%s\">", cat->name);
                        mwWriteXmlString(&pbuf, &bufsize, 1, buf);
                        matched = 1;
                    }
                    snprintf(buf, sizeof(buf), "<item id=\"%06d\" pos=\"%d\">", info->hash, pos++);
                    mwWriteXmlString(&pbuf, &bufsize, 2, buf);

                    snprintf(buf, sizeof(buf), "<name><![CDATA[%s]]></name>", info->title);
                    mwWriteXmlString(&pbuf, &bufsize, 2, buf);

                    snprintf(buf, sizeof(buf), "<chars>%d</chars>", info->chars);
                    mwWriteXmlString(&pbuf, &bufsize, 2, buf);

                    mwWriteXmlString(&pbuf, &bufsize, 2, "</item>");
                }
                idx++;
            }
            if (matched) mwWriteXmlString(&pbuf, &bufsize, 1, "</category>");
            if (count == 0) break;
            matched = FALSE;
        }
    }
    else if (!strcmp(param->pucRequest, "/chars"))
    {
        int i;
        for (i = 1; i <= MAX_CHARS; i++)
        {
            if (i > from)
            {
                if ((count--) == 0) break;
                snprintf(buf, sizeof(buf), "<category chars=\"%d\" count=\"%d\"/>", i, charsinfo[i]);
                mwWriteXmlString(&pbuf, &bufsize, 2, buf);
            }
        }
    }
    else if (!strcmp(param->pucRequest, "/query"))
    {
        char buf[256];
        CATEGORY_INFO *cat;
        CLIP_INFO* clip;
        int idx = 0;
        int hash = id ? atoi(id) : -1;
        if (hash > 0)
            clip = GetClipByHash(hash, &cat);
        else
            clip = GetClipByName(0, id, &cat);
        if (!clip) clip = GetClipByFile(id, &cat);
        if (clip)
        {
            mwWriteXmlString(&pbuf, &bufsize, 2, "<item>");
            snprintf(buf, sizeof(buf), "<id><![CDATA[%d]]></id>", clip->hash);
            mwWriteXmlString(&pbuf, &bufsize, 3, buf);
            snprintf(buf, sizeof(buf), "<file><![CDATA[%s]]></file>", clip->filename);
            mwWriteXmlString(&pbuf, &bufsize, 3, buf);
            snprintf(buf, sizeof(buf), "<title><![CDATA[%s]]></title>", clip->title);
            mwWriteXmlString(&pbuf, &bufsize, 3, buf);
            snprintf(buf, sizeof(buf), "<category><![CDATA[%s]]></category>", cat->name);
            mwWriteXmlString(&pbuf, &bufsize, 3, buf);
            mwWriteXmlString(&pbuf, &bufsize, 2, "</item>");
        }
    }

    mwWriteXmlString(&pbuf, &bufsize, 0, "</response>");

    param->dataBytes=(int)(pbuf-param->pucBuffer);
    param->fileType=HTTPFILETYPE_XML;
    return FLAG_DATA_RAW | FLAG_TO_FREE;
}