int uhStream(UrlHandlerParam* param) { char* file; mwParseQueryString(param); file = mwGetVarValue(param->pxVars, "file", 0); param->hs->fd = _open(file, _O_BINARY|_O_RDONLY); return FLAG_DATA_FD; }
int uhFileStream(UrlHandlerParam* param) { if (!param->hs->ptr) { // first request char* file; if (mwParseQueryString(param) <= 0 || !(file = mwGetVarValue(param->pxVars, "file", 0))) return 0; // no file specified param->hs->ptr = (void*)open(file, O_BINARY | O_RDONLY); if (!param->hs->ptr) return 0; } param->dataBytes = read((int)param->hs->ptr, param->pucBuffer, param->dataBytes); param->fileType = HTTPFILETYPE_XML; return param->dataBytes > 0 ? (FLAG_DATA_STREAM | FLAG_CHUNK) : 0; }
int uhVodStream(UrlHandlerParam* param) { static int code = 0; static int prevtime = 0; int id; char* file; mwParseQueryString(param); file = mwGetVarValue(param->pxVars, "file", 0); id = mwGetVarValueInt(param->pxVars, "id" , -1); if (!file) { CLIP_INFO* clip = GetClipByHash(id, 0); if (clip) { snprintf(param->pucBuffer, param->dataBytes, "~%s/%s", vodroot, clip->filename); return FLAG_DATA_FILE; } } else { snprintf(param->pucBuffer, param->dataBytes, "~%s/%s", vodroot, file); } return 0; }
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; }
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; }
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; }