Fossil SCM
Changed /json/stat to use brief mode by default due to relatively high runtime cost, replaced 'brief' param with 'full'. Added json_getenv_bool().
Commit
c1914eaa7944d9c06432dcbdac6f5415a0ffc023
Parent
17f11a088447528…
1 file changed
+42
-7
+42
-7
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -288,12 +288,12 @@ | ||
| 288 | 288 | |
| 289 | 289 | /* |
| 290 | 290 | ** Wrapper around json_getenv() which... |
| 291 | 291 | ** |
| 292 | 292 | ** If it finds a value and that value is-a JSON number or is a string |
| 293 | -** which looks like an integer or is-a JSON bool then it is converted | |
| 294 | -** to an int. If none of those apply then dflt is returned. | |
| 293 | +** which looks like an integer or is-a JSON bool/null then it is | |
| 294 | +** converted to an int. If none of those apply then dflt is returned. | |
| 295 | 295 | */ |
| 296 | 296 | int json_getenv_int(char const * pKey, int dflt ){ |
| 297 | 297 | cson_value const * v = json_getenv(pKey); |
| 298 | 298 | if(!v){ |
| 299 | 299 | return dflt; |
| @@ -303,16 +303,51 @@ | ||
| 303 | 303 | char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 304 | 304 | assert( (NULL!=sv) && "This is quite unexpected." ); |
| 305 | 305 | return sv ? atoi(sv) : dflt; |
| 306 | 306 | }else if( cson_value_is_bool(v) ){ |
| 307 | 307 | return cson_value_get_bool(v) ? 1 : 0; |
| 308 | + }else if( cson_value_is_null(v) ){ | |
| 309 | + return 0; | |
| 308 | 310 | }else{ |
| 309 | 311 | /* we should arguably treat JSON null as 0. */ |
| 310 | 312 | return dflt; |
| 311 | 313 | } |
| 312 | 314 | } |
| 313 | 315 | |
| 316 | + | |
| 317 | +/* | |
| 318 | +** Wrapper around json_getenv() which tries to evaluate a payload/env | |
| 319 | +** value as a boolean. Uses mostly the same logic as | |
| 320 | +** json_getenv_int(), with the addition that string values which | |
| 321 | +** either start with a digit 1..9 or the letters [tT] are considered | |
| 322 | +** to be true. If this function cannot find a matching key/value then | |
| 323 | +** dflt is returned. e.g. if it finds the key but the value is-a | |
| 324 | +** Object then dftl is returned. | |
| 325 | +*/ | |
| 326 | +char json_getenv_bool(char const * pKey, char dflt ){ | |
| 327 | + cson_value const * v = json_getenv(pKey); | |
| 328 | + if(!v){ | |
| 329 | + return dflt; | |
| 330 | + }else if( cson_value_is_number(v) ){ | |
| 331 | + return cson_value_get_integer(v) ? 1 : 0; | |
| 332 | + }else if( cson_value_is_string(v) ){ | |
| 333 | + char const * sv = cson_string_cstr(cson_value_get_string(v)); | |
| 334 | + if(!*sv || ('0'==*sv)){ | |
| 335 | + return 0; | |
| 336 | + }else{ | |
| 337 | + return (('t'==*sv) || ('T'==*sv) | |
| 338 | + || (('1'<=*sv) && ('9'>=*sv))) | |
| 339 | + ? 1 : 0; | |
| 340 | + } | |
| 341 | + }else if( cson_value_is_bool(v) ){ | |
| 342 | + return cson_value_get_bool(v) ? 1 : 0; | |
| 343 | + }else if( cson_value_is_null(v) ){ | |
| 344 | + return 0; | |
| 345 | + }else{ | |
| 346 | + return dflt; | |
| 347 | + } | |
| 348 | +} | |
| 314 | 349 | |
| 315 | 350 | /* |
| 316 | 351 | ** Returns the string form of a json_getenv() value, but ONLY If that |
| 317 | 352 | ** value is-a String. Non-strings are not converted to strings for |
| 318 | 353 | ** this purpose. Returned memory is owned by g.json or fossil and is |
| @@ -1222,11 +1257,11 @@ | ||
| 1222 | 1257 | ** |
| 1223 | 1258 | */ |
| 1224 | 1259 | cson_value * json_page_stat(){ |
| 1225 | 1260 | i64 t, fsize; |
| 1226 | 1261 | int n, m; |
| 1227 | - int brief; | |
| 1262 | + int full; | |
| 1228 | 1263 | const char *zDb; |
| 1229 | 1264 | enum { BufLen = 1000 }; |
| 1230 | 1265 | char zBuf[BufLen]; |
| 1231 | 1266 | cson_value * jv = NULL; |
| 1232 | 1267 | cson_object * jo = NULL; |
| @@ -1235,13 +1270,13 @@ | ||
| 1235 | 1270 | if( !g.perm.Read ){ |
| 1236 | 1271 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 1237 | 1272 | return NULL; |
| 1238 | 1273 | } |
| 1239 | 1274 | if( g.isHTTP ){ |
| 1240 | - brief = json_getenv_int("brief",0); | |
| 1275 | + full = json_getenv_bool("full",0); | |
| 1241 | 1276 | }else{ |
| 1242 | - brief = (0!=find_option("brief","b",0)); | |
| 1277 | + full = (0!=find_option("full","f",0)); | |
| 1243 | 1278 | } |
| 1244 | 1279 | #define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf))); |
| 1245 | 1280 | |
| 1246 | 1281 | jv = cson_value_new_object(); |
| 1247 | 1282 | jo = cson_value_get_object(jv); |
| @@ -1253,11 +1288,11 @@ | ||
| 1253 | 1288 | with a blob for this purpose. |
| 1254 | 1289 | */ |
| 1255 | 1290 | fsize = file_size(g.zRepositoryName); |
| 1256 | 1291 | cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize)); |
| 1257 | 1292 | |
| 1258 | - if(!brief){ | |
| 1293 | + if(full){ | |
| 1259 | 1294 | n = db_int(0, "SELECT count(*) FROM blob"); |
| 1260 | 1295 | m = db_int(0, "SELECT count(*) FROM delta"); |
| 1261 | 1296 | cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n)); |
| 1262 | 1297 | cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m)); |
| 1263 | 1298 | if( n>0 ){ |
| @@ -1292,11 +1327,11 @@ | ||
| 1292 | 1327 | " WHERE +tagname GLOB 'wiki-*'"); |
| 1293 | 1328 | cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n)); |
| 1294 | 1329 | n = db_int(0, "SELECT count(*) FROM tag /*scan*/" |
| 1295 | 1330 | " WHERE +tagname GLOB 'tkt-*'"); |
| 1296 | 1331 | cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n)); |
| 1297 | - }/*!brief*/ | |
| 1332 | + }/*full*/ | |
| 1298 | 1333 | n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)" |
| 1299 | 1334 | " + 0.99"); |
| 1300 | 1335 | cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n)); |
| 1301 | 1336 | cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24)); |
| 1302 | 1337 | sqlite3_snprintf(BufLen, zBuf, db_get("project-code","")); |
| 1303 | 1338 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -288,12 +288,12 @@ | |
| 288 | |
| 289 | /* |
| 290 | ** Wrapper around json_getenv() which... |
| 291 | ** |
| 292 | ** If it finds a value and that value is-a JSON number or is a string |
| 293 | ** which looks like an integer or is-a JSON bool then it is converted |
| 294 | ** to an int. If none of those apply then dflt is returned. |
| 295 | */ |
| 296 | int json_getenv_int(char const * pKey, int dflt ){ |
| 297 | cson_value const * v = json_getenv(pKey); |
| 298 | if(!v){ |
| 299 | return dflt; |
| @@ -303,16 +303,51 @@ | |
| 303 | char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 304 | assert( (NULL!=sv) && "This is quite unexpected." ); |
| 305 | return sv ? atoi(sv) : dflt; |
| 306 | }else if( cson_value_is_bool(v) ){ |
| 307 | return cson_value_get_bool(v) ? 1 : 0; |
| 308 | }else{ |
| 309 | /* we should arguably treat JSON null as 0. */ |
| 310 | return dflt; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /* |
| 316 | ** Returns the string form of a json_getenv() value, but ONLY If that |
| 317 | ** value is-a String. Non-strings are not converted to strings for |
| 318 | ** this purpose. Returned memory is owned by g.json or fossil and is |
| @@ -1222,11 +1257,11 @@ | |
| 1222 | ** |
| 1223 | */ |
| 1224 | cson_value * json_page_stat(){ |
| 1225 | i64 t, fsize; |
| 1226 | int n, m; |
| 1227 | int brief; |
| 1228 | const char *zDb; |
| 1229 | enum { BufLen = 1000 }; |
| 1230 | char zBuf[BufLen]; |
| 1231 | cson_value * jv = NULL; |
| 1232 | cson_object * jo = NULL; |
| @@ -1235,13 +1270,13 @@ | |
| 1235 | if( !g.perm.Read ){ |
| 1236 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | if( g.isHTTP ){ |
| 1240 | brief = json_getenv_int("brief",0); |
| 1241 | }else{ |
| 1242 | brief = (0!=find_option("brief","b",0)); |
| 1243 | } |
| 1244 | #define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf))); |
| 1245 | |
| 1246 | jv = cson_value_new_object(); |
| 1247 | jo = cson_value_get_object(jv); |
| @@ -1253,11 +1288,11 @@ | |
| 1253 | with a blob for this purpose. |
| 1254 | */ |
| 1255 | fsize = file_size(g.zRepositoryName); |
| 1256 | cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize)); |
| 1257 | |
| 1258 | if(!brief){ |
| 1259 | n = db_int(0, "SELECT count(*) FROM blob"); |
| 1260 | m = db_int(0, "SELECT count(*) FROM delta"); |
| 1261 | cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n)); |
| 1262 | cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m)); |
| 1263 | if( n>0 ){ |
| @@ -1292,11 +1327,11 @@ | |
| 1292 | " WHERE +tagname GLOB 'wiki-*'"); |
| 1293 | cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n)); |
| 1294 | n = db_int(0, "SELECT count(*) FROM tag /*scan*/" |
| 1295 | " WHERE +tagname GLOB 'tkt-*'"); |
| 1296 | cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n)); |
| 1297 | }/*!brief*/ |
| 1298 | n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)" |
| 1299 | " + 0.99"); |
| 1300 | cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n)); |
| 1301 | cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24)); |
| 1302 | sqlite3_snprintf(BufLen, zBuf, db_get("project-code","")); |
| 1303 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -288,12 +288,12 @@ | |
| 288 | |
| 289 | /* |
| 290 | ** Wrapper around json_getenv() which... |
| 291 | ** |
| 292 | ** If it finds a value and that value is-a JSON number or is a string |
| 293 | ** which looks like an integer or is-a JSON bool/null then it is |
| 294 | ** converted to an int. If none of those apply then dflt is returned. |
| 295 | */ |
| 296 | int json_getenv_int(char const * pKey, int dflt ){ |
| 297 | cson_value const * v = json_getenv(pKey); |
| 298 | if(!v){ |
| 299 | return dflt; |
| @@ -303,16 +303,51 @@ | |
| 303 | char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 304 | assert( (NULL!=sv) && "This is quite unexpected." ); |
| 305 | return sv ? atoi(sv) : dflt; |
| 306 | }else if( cson_value_is_bool(v) ){ |
| 307 | return cson_value_get_bool(v) ? 1 : 0; |
| 308 | }else if( cson_value_is_null(v) ){ |
| 309 | return 0; |
| 310 | }else{ |
| 311 | /* we should arguably treat JSON null as 0. */ |
| 312 | return dflt; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* |
| 318 | ** Wrapper around json_getenv() which tries to evaluate a payload/env |
| 319 | ** value as a boolean. Uses mostly the same logic as |
| 320 | ** json_getenv_int(), with the addition that string values which |
| 321 | ** either start with a digit 1..9 or the letters [tT] are considered |
| 322 | ** to be true. If this function cannot find a matching key/value then |
| 323 | ** dflt is returned. e.g. if it finds the key but the value is-a |
| 324 | ** Object then dftl is returned. |
| 325 | */ |
| 326 | char json_getenv_bool(char const * pKey, char dflt ){ |
| 327 | cson_value const * v = json_getenv(pKey); |
| 328 | if(!v){ |
| 329 | return dflt; |
| 330 | }else if( cson_value_is_number(v) ){ |
| 331 | return cson_value_get_integer(v) ? 1 : 0; |
| 332 | }else if( cson_value_is_string(v) ){ |
| 333 | char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 334 | if(!*sv || ('0'==*sv)){ |
| 335 | return 0; |
| 336 | }else{ |
| 337 | return (('t'==*sv) || ('T'==*sv) |
| 338 | || (('1'<=*sv) && ('9'>=*sv))) |
| 339 | ? 1 : 0; |
| 340 | } |
| 341 | }else if( cson_value_is_bool(v) ){ |
| 342 | return cson_value_get_bool(v) ? 1 : 0; |
| 343 | }else if( cson_value_is_null(v) ){ |
| 344 | return 0; |
| 345 | }else{ |
| 346 | return dflt; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | ** Returns the string form of a json_getenv() value, but ONLY If that |
| 352 | ** value is-a String. Non-strings are not converted to strings for |
| 353 | ** this purpose. Returned memory is owned by g.json or fossil and is |
| @@ -1222,11 +1257,11 @@ | |
| 1257 | ** |
| 1258 | */ |
| 1259 | cson_value * json_page_stat(){ |
| 1260 | i64 t, fsize; |
| 1261 | int n, m; |
| 1262 | int full; |
| 1263 | const char *zDb; |
| 1264 | enum { BufLen = 1000 }; |
| 1265 | char zBuf[BufLen]; |
| 1266 | cson_value * jv = NULL; |
| 1267 | cson_object * jo = NULL; |
| @@ -1235,13 +1270,13 @@ | |
| 1270 | if( !g.perm.Read ){ |
| 1271 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | if( g.isHTTP ){ |
| 1275 | full = json_getenv_bool("full",0); |
| 1276 | }else{ |
| 1277 | full = (0!=find_option("full","f",0)); |
| 1278 | } |
| 1279 | #define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf))); |
| 1280 | |
| 1281 | jv = cson_value_new_object(); |
| 1282 | jo = cson_value_get_object(jv); |
| @@ -1253,11 +1288,11 @@ | |
| 1288 | with a blob for this purpose. |
| 1289 | */ |
| 1290 | fsize = file_size(g.zRepositoryName); |
| 1291 | cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize)); |
| 1292 | |
| 1293 | if(full){ |
| 1294 | n = db_int(0, "SELECT count(*) FROM blob"); |
| 1295 | m = db_int(0, "SELECT count(*) FROM delta"); |
| 1296 | cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n)); |
| 1297 | cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m)); |
| 1298 | if( n>0 ){ |
| @@ -1292,11 +1327,11 @@ | |
| 1327 | " WHERE +tagname GLOB 'wiki-*'"); |
| 1328 | cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n)); |
| 1329 | n = db_int(0, "SELECT count(*) FROM tag /*scan*/" |
| 1330 | " WHERE +tagname GLOB 'tkt-*'"); |
| 1331 | cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n)); |
| 1332 | }/*full*/ |
| 1333 | n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)" |
| 1334 | " + 0.99"); |
| 1335 | cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n)); |
| 1336 | cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24)); |
| 1337 | sqlite3_snprintf(BufLen, zBuf, db_get("project-code","")); |
| 1338 |