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;
}