| | @@ -175,11 +175,13 @@ |
| 175 | 175 | #define BEGIN_TIMER |
| 176 | 176 | #define END_TIMER 0.0 |
| 177 | 177 | #define HAS_TIMER 0 |
| 178 | 178 | #endif |
| 179 | 179 | |
| 180 | | - |
| 180 | +/* |
| 181 | +** Returns true (non-0) if fossil appears to be running in JSON mode. |
| 182 | +*/ |
| 181 | 183 | char fossil_has_json(){ |
| 182 | 184 | return g.json.isJsonMode && (g.isHTTP || g.json.post.o); |
| 183 | 185 | } |
| 184 | 186 | |
| 185 | 187 | /* |
| | @@ -467,25 +469,26 @@ |
| 467 | 469 | ** which looks like an integer or is-a JSON bool/null then it is |
| 468 | 470 | ** converted to an int. If none of those apply then dflt is returned. |
| 469 | 471 | */ |
| 470 | 472 | int json_getenv_int(char const * pKey, int dflt ){ |
| 471 | 473 | cson_value const * v = json_getenv(pKey); |
| 472 | | - if(!v){ |
| 473 | | - return dflt; |
| 474 | | - }else if( cson_value_is_number(v) ){ |
| 475 | | - return (int)cson_value_get_integer(v); |
| 476 | | - }else if( cson_value_is_string(v) ){ |
| 477 | | - char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 478 | | - assert( (NULL!=sv) && "This is quite unexpected." ); |
| 479 | | - return sv ? atoi(sv) : dflt; |
| 480 | | - }else if( cson_value_is_bool(v) ){ |
| 481 | | - return cson_value_get_bool(v) ? 1 : 0; |
| 482 | | - }else if( cson_value_is_null(v) ){ |
| 483 | | - return 0; |
| 484 | | - }else{ |
| 485 | | - /* we should arguably treat JSON null as 0. */ |
| 486 | | - return dflt; |
| 474 | + const cson_type_id type = v ? cson_value_type_id(v) : CSON_TYPE_UNDEF; |
| 475 | + switch(type){ |
| 476 | + case CSON_TYPE_INTEGER: |
| 477 | + case CSON_TYPE_DOUBLE: |
| 478 | + return (int)cson_value_get_integer(v); |
| 479 | + case CSON_TYPE_STRING: { |
| 480 | + char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 481 | + assert( (NULL!=sv) && "This is quite unexpected." ); |
| 482 | + return sv ? atoi(sv) : dflt; |
| 483 | + } |
| 484 | + case CSON_TYPE_BOOL: |
| 485 | + return cson_value_get_bool(v) ? 1 : 0; |
| 486 | + case CSON_TYPE_NULL: |
| 487 | + return 0; |
| 488 | + default: |
| 489 | + return dflt; |
| 487 | 490 | } |
| 488 | 491 | } |
| 489 | 492 | |
| 490 | 493 | |
| 491 | 494 | /* |
| | @@ -503,31 +506,34 @@ |
| 503 | 506 | ** whether or not this function found a match (it will return -1 in |
| 504 | 507 | ** that case). |
| 505 | 508 | */ |
| 506 | 509 | char json_getenv_bool(char const * pKey, char dflt ){ |
| 507 | 510 | cson_value const * v = json_getenv(pKey); |
| 508 | | - if(!v){ |
| 509 | | - return dflt; |
| 510 | | - }else if( cson_value_is_number(v) ){ |
| 511 | | - return cson_value_get_integer(v) ? 1 : 0; |
| 512 | | - }else if( cson_value_is_string(v) ){ |
| 513 | | - char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 514 | | - if(!*sv || ('0'==*sv)){ |
| 515 | | - return 0; |
| 516 | | - }else{ |
| 517 | | - return ((('1'<=*sv) && ('9'>=*sv)) |
| 518 | | - || ('t'==*sv) || ('T'==*sv) |
| 519 | | - || ('y'==*sv) || ('Y'==*sv) |
| 520 | | - ) |
| 521 | | - ? 1 : 0; |
| 522 | | - } |
| 523 | | - }else if( cson_value_is_bool(v) ){ |
| 524 | | - return cson_value_get_bool(v) ? 1 : 0; |
| 525 | | - }else if( cson_value_is_null(v) ){ |
| 526 | | - return 0; |
| 527 | | - }else{ |
| 528 | | - return dflt; |
| 511 | + const cson_type_id type = v ? cson_value_type_id(v) : CSON_TYPE_UNDEF; |
| 512 | + switch(type){ |
| 513 | + case CSON_TYPE_INTEGER: |
| 514 | + case CSON_TYPE_DOUBLE: |
| 515 | + return cson_value_get_integer(v) ? 1 : 0; |
| 516 | + case CSON_TYPE_STRING: { |
| 517 | + char const * sv = cson_string_cstr(cson_value_get_string(v)); |
| 518 | + assert( (NULL!=sv) && "This is quite unexpected." ); |
| 519 | + if(!*sv || ('0'==*sv)){ |
| 520 | + return 0; |
| 521 | + }else{ |
| 522 | + return ((('1'<=*sv) && ('9'>=*sv)) |
| 523 | + || ('t'==*sv) || ('T'==*sv) |
| 524 | + || ('y'==*sv) || ('Y'==*sv) |
| 525 | + ) |
| 526 | + ? 1 : 0; |
| 527 | + } |
| 528 | + } |
| 529 | + case CSON_TYPE_BOOL: |
| 530 | + return cson_value_get_bool(v) ? 1 : 0; |
| 531 | + case CSON_TYPE_NULL: |
| 532 | + return 0; |
| 533 | + default: |
| 534 | + return dflt; |
| 529 | 535 | } |
| 530 | 536 | } |
| 531 | 537 | |
| 532 | 538 | /* |
| 533 | 539 | ** Returns the string form of a json_getenv() value, but ONLY If that |
| | @@ -755,13 +761,13 @@ |
| 755 | 761 | && cson_value_is_string(g.json.authToken) |
| 756 | 762 | && !PD(login_cookie_name(),NULL)){ |
| 757 | 763 | /* tell fossil to use this login info. |
| 758 | 764 | |
| 759 | 765 | FIXME?: because the JSON bits don't carry around |
| 760 | | - login_cookie_name(), there is a potential login hijacking |
| 761 | | - window here. We may need to change the JSON auth token to be |
| 762 | | - in the form: login_cookie_name()=... |
| 766 | + login_cookie_name(), there is(?) a potential(?) login hijacking |
| 767 | + window here. We may need to change the JSON auth token to be in |
| 768 | + the form: login_cookie_name()=... |
| 763 | 769 | |
| 764 | 770 | Then again, the hardened cookie value helps ensure that |
| 765 | 771 | only a proper key/value match is valid. |
| 766 | 772 | */ |
| 767 | 773 | cgi_replace_parameter( login_cookie_name(), cson_value_get_cstr(g.json.authToken) ); |
| 768 | 774 | |