282c37a5f6db8b7704e1d0f3617f55f38b8aea70 hiram Fri Apr 19 15:27:08 2019 -0700 more error testing for illegal inputs refs #18869 diff --git src/hg/hubApi/list.c src/hg/hubApi/list.c index e87d9a8..260e2fe 100644 --- src/hg/hubApi/list.c +++ src/hg/hubApi/list.c @@ -186,98 +186,30 @@ } else { struct chromInfo *ci = trackHubAllChromInfo(foundGenome->name); slSort(ci, chromInfoCmp); jsonWriteNumber(jw, "chromCount", (long long)slCount(ci)); jsonWriteObjectStart(jw, "chromosomes"); struct chromInfo *el = ci; for ( ; el != NULL; el = el->next ) { jsonWriteNumber(jw, el->chrom, (long long)el->size); } jsonWriteObjectEnd(jw); /* chromosomes */ } apiFinishOutput(0, NULL, jw); - -#ifdef NOT -char *table = cgiOptionalString("track"); -struct sqlConnection *conn = hAllocConn(db); -/* in trackDb language: track == table */ -if (table) - { - if (! sqlTableExists(conn, table)) - apiErrAbort(err400, err400Msg, "can not find specified 'track=%s' for endpoint: /list/chromosomes?db=%s;track=%s", table, db, table); - if (sqlColumnExists(conn, table, "chrom")) - { - char *dataTime = sqlTableUpdate(conn, table); - time_t dataTimeStamp = sqlDateToUnixTime(dataTime); - replaceChar(dataTime, ' ', 'T'); /* ISO 8601 */ - struct jsonWrite *jw = apiStartOutput(); - jsonWriteString(jw, "genome", db); - jsonWriteString(jw, "track", table); - jsonWriteString(jw, "dataTime", dataTime); - jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp); - freeMem(dataTime); - struct slPair *list = NULL; - char query[2048]; - sqlSafef(query, sizeof(query), "select distinct chrom from %s", table); - struct sqlResult *sr = sqlGetResult(conn, query); - char **row; - while ((row = sqlNextRow(sr)) != NULL) - { - int size = hChromSize(db, row[0]); - slAddHead(&list, slPairNew(row[0], intToPt(size))); - } - sqlFreeResult(&sr); - slPairIntSort(&list); - slReverse(&list); - jsonWriteNumber(jw, "chromCount", (long long)slCount(list)); - jsonWriteObjectStart(jw, "chromosomes"); - struct slPair *el = list; - for ( ; el != NULL; el = el->next ) - jsonWriteNumber(jw, el->name, (long long)ptToInt(el->val)); - jsonWriteObjectEnd(jw); /* chromosomes */ - apiFinishOutput(0, NULL, jw); - } - else - apiErrAbort(err400, err400Msg, "track '%s' is not a position track, request table without chrom specification, genome: '%s'", table, db); - } -else - { - char *dataTime = sqlTableUpdate(conn, "chromInfo"); - time_t dataTimeStamp = sqlDateToUnixTime(dataTime); - replaceChar(dataTime, ' ', 'T'); /* ISO 8601 */ - struct chromInfo *ciList = createChromInfoList(NULL, db); - slSort(ciList, chromInfoCmp); - struct chromInfo *el = ciList; - struct jsonWrite *jw = apiStartOutput(); - jsonWriteString(jw, "genome", db); - jsonWriteString(jw, "dataTime", dataTime); - jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp); - freeMem(dataTime); - jsonWriteNumber(jw, "chromCount", (long long)slCount(ciList)); - jsonWriteObjectStart(jw, "chromosomes"); - for ( ; el != NULL; el = el->next ) - { - jsonWriteNumber(jw, el->chrom, (long long)el->size); - } - jsonWriteObjectEnd(jw); /* chromosomes */ - apiFinishOutput(0, NULL, jw); - } -hFreeConn(&conn); -#endif } static void chromInfoJsonOutput(FILE *f, char *db) /* for given db, if there is a track, list the chromosomes in that track, * for no track, simply list the chromosomes in the sequence */ { char *table = cgiOptionalString("track"); struct sqlConnection *conn = hAllocConnMaybe(db); if (NULL == conn) apiErrAbort(err400, err400Msg, "can not find database 'db=%s' for endpoint '/list/chromosomes", db); /* in trackDb language: track == table */ if (table) { @@ -459,30 +391,38 @@ jsonWriteString(jw, "twoBitPath", el->twoBitPath); jsonWriteString(jw, "groups", el->groups); jsonWriteString(jw, "defaultPos", el->defaultPos); jsonWriteNumber(jw, "orderKey", el->orderKey); jsonWriteObjectEnd(jw); } jsonWriteObjectEnd(jw); apiFinishOutput(0, NULL, jw); } } else if (sameWord("tracks", words[1])) { char *hubUrl = cgiOptionalString("hubUrl"); char *genome = cgiOptionalString("genome"); char *db = cgiOptionalString("db"); + if (isNotEmpty(db)) + { + struct sqlConnection *conn = hAllocConnMaybe(db); + if (NULL == conn) + apiErrAbort(err400, err400Msg, "can not find database 'db=%s' for endpoint '/list/chromosomes", db); + else + hFreeConn(&conn); + } if (isEmpty(hubUrl) && isEmpty(db)) apiErrAbort(err400, err400Msg, "ERROR: missing hubUrl or db name for endpoint /list/tracks"); if (isEmpty(hubUrl)) // missing hubUrl implies UCSC database { trackDbJsonOutput(db, stdout); // only need db for this function return; } if (isEmpty(genome) || isEmpty(hubUrl)) { if (isEmpty(genome)) apiErrAbort(err400, err400Msg, "ERROR: must supply genome='someName' the name of a genome in a hub for /list/tracks\n"); if (isEmpty(hubUrl)) apiErrAbort(err400, err400Msg, "ERROR: must supply hubUrl='http:...' some URL to a hub for /list/tracks"); } struct trackHub *hub = errCatchTrackHubOpen(hubUrl); @@ -495,30 +435,38 @@ jsonWriteObjectStart(jw, genome); struct trackDb *el = NULL; for (el = tdbList; el != NULL; el = el->next ) { recursiveTrackList(jw, el); } jsonWriteObjectEnd(jw); apiFinishOutput(0, NULL, jw); } } else if (sameWord("chromosomes", words[1])) { char *hubUrl = cgiOptionalString("hubUrl"); char *genome = cgiOptionalString("genome"); char *db = cgiOptionalString("db"); + if (isNotEmpty(db)) + { + struct sqlConnection *conn = hAllocConnMaybe(db); + if (NULL == conn) + apiErrAbort(err400, err400Msg, "can not find database 'db=%s' for endpoint '/list/chromosomes", db); + else + hFreeConn(&conn); + } if (isEmpty(hubUrl) && isEmpty(db)) apiErrAbort(err400, err400Msg, "ERROR: must '%s' '%s' supply hubUrl or db name to return chromosome list", hubUrl, db); if (isEmpty(hubUrl)) // missing hubUrl implies UCSC database { chromInfoJsonOutput(stdout, db); return; } else { hubChromInfoJsonOutput(stdout, hubUrl, genome); return; } } else