Fossil SCM
minor internal cleanups and doc additions.
Commit
2f3e438507702c0fbbe63e8035154b1bfae26cd1
Parent
76d0fa2bb0c561c…
1 file changed
+36
-18
+36
-18
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -41,15 +41,19 @@ | ||
| 41 | 41 | #include "cson_amalgamation.h" |
| 42 | 42 | #include "json_detail.h" /* workaround for apparent enum limitation in makeheaders */ |
| 43 | 43 | #endif |
| 44 | 44 | |
| 45 | 45 | /* |
| 46 | -** Signature for JSON page/command callbacks. By the time the callback | |
| 47 | -** is called, json_page_top() or json_cmd_top() will have set up the | |
| 48 | -** JSON-related environment. Implementations may generate a "result | |
| 49 | -** payload" of any JSON type by returning its value from this function | |
| 50 | -** (ownership is tranferred to the caller). On error they should set | |
| 46 | +** Signature for JSON page/command callbacks. Each callback is | |
| 47 | +** responsible for handling one JSON request/command and/or | |
| 48 | +** dispatching to sub-commands. | |
| 49 | +** | |
| 50 | +** By the time the callback is called, json_page_top() (HTTP mode) or | |
| 51 | +** json_cmd_top() (CLI mode) will have set up the JSON-related | |
| 52 | +** environment. Implementations may generate a "result payload" of any | |
| 53 | +** JSON type by returning its value from this function (ownership is | |
| 54 | +** tranferred to the caller). On error they should set | |
| 51 | 55 | ** g.json.resultCode to one of the FossilJsonCodes values and return |
| 52 | 56 | ** either their payload object or NULL. Note that NULL is a legal |
| 53 | 57 | ** success value - it simply means the response will contain no |
| 54 | 58 | ** payload. If g.json.resultCode is non-zero when this function |
| 55 | 59 | ** returns then the top-level dispatcher will destroy any payload |
| @@ -70,14 +74,17 @@ | ||
| 70 | 74 | ** All of the setup/response code is handled by the top dispatcher |
| 71 | 75 | ** functions and the callbacks concern themselves only with generating |
| 72 | 76 | ** the payload. |
| 73 | 77 | ** |
| 74 | 78 | ** It is imperitive that NO callback functions EVER output ANYTHING to |
| 75 | -** stdout, as that will effectively corrupt any HTTP output. | |
| 79 | +** stdout, as that will effectively corrupt any JSON output, and | |
| 80 | +** almost certainly will corrupt any HTTP response headers. Output | |
| 81 | +** sent to stderr ends up in my apache log, so that might be useful | |
| 82 | +** for debuggering in some cases, but so such code should be left | |
| 83 | +** enabled for non-debuggering builds. | |
| 76 | 84 | */ |
| 77 | 85 | typedef cson_value * (*fossil_json_f)(unsigned int depth); |
| 78 | - | |
| 79 | 86 | |
| 80 | 87 | /* |
| 81 | 88 | ** Placeholder /json/XXX page impl for NYI (Not Yet Implemented) |
| 82 | 89 | ** (but planned) pages/commands. |
| 83 | 90 | */ |
| @@ -88,17 +95,28 @@ | ||
| 88 | 95 | |
| 89 | 96 | /* |
| 90 | 97 | ** Holds keys used for various JSON API properties. |
| 91 | 98 | */ |
| 92 | 99 | static const struct FossilJsonKeys_{ |
| 93 | - char const * authToken; | |
| 100 | + /** maintainers: please keep alpha sorted (case-insensitive) */ | |
| 94 | 101 | char const * commandPath; |
| 95 | 102 | char const * anonymousSeed; |
| 103 | + char const * authToken; | |
| 104 | + char const * payload; | |
| 105 | + char const * requestId; | |
| 106 | + char const * resultCode; | |
| 107 | + char const * resultText; | |
| 108 | + char const * timestamp; | |
| 96 | 109 | } FossilJsonKeys = { |
| 97 | - "authToken" /*authToken*/, | |
| 98 | 110 | "COMMAND_PATH" /*commandPath*/, |
| 99 | - "anonymousSeed" /*anonymousSeed*/ | |
| 111 | + "anonymousSeed" /*anonymousSeed*/, | |
| 112 | + "authToken" /*authToken*/, | |
| 113 | + "payload" /* payload */, | |
| 114 | + "requestId" /*requestId*/, | |
| 115 | + "resultCode" /*resultCode*/, | |
| 116 | + "resultText" /*resultText*/, | |
| 117 | + "timestamp" /*timestamp*/ | |
| 100 | 118 | }; |
| 101 | 119 | |
| 102 | 120 | /* |
| 103 | 121 | ** Given a FossilJsonCodes value, it returns a string suitable for use |
| 104 | 122 | ** as a resultText string. Returns some unspecified string if errCode |
| @@ -548,11 +566,11 @@ | ||
| 548 | 566 | |
| 549 | 567 | /* g.json.reqPayload exists only to simplify some of our access to |
| 550 | 568 | the request payload. We currently only use this in the context of |
| 551 | 569 | Object payloads, not Arrays, strings, etc. |
| 552 | 570 | */ |
| 553 | - g.json.reqPayload.v = cson_object_get( g.json.post.o, "payload" ); | |
| 571 | + g.json.reqPayload.v = cson_object_get( g.json.post.o, FossilJsonKeys.payload ); | |
| 554 | 572 | if( g.json.reqPayload.v ){ |
| 555 | 573 | g.json.reqPayload.o = cson_value_get_object( g.json.reqPayload.v ) |
| 556 | 574 | /* g.json.reqPayload.o may legally be NULL, which means only that |
| 557 | 575 | g.json.reqPayload.v is-not-a Object. |
| 558 | 576 | */; |
| @@ -765,11 +783,11 @@ | ||
| 765 | 783 | }while(0) |
| 766 | 784 | |
| 767 | 785 | tmp = cson_value_new_string(MANIFEST_UUID,strlen(MANIFEST_UUID)); |
| 768 | 786 | SET("fossil"); |
| 769 | 787 | |
| 770 | - {/* "timestamp" */ | |
| 788 | + {/* timestamp */ | |
| 771 | 789 | cson_int_t jsTime; |
| 772 | 790 | #if 1 |
| 773 | 791 | jsTime = (cson_int_t)time(0); |
| 774 | 792 | #elif 1 |
| 775 | 793 | /* Ge Weijers has pointed out that time(0) commonly returns |
| @@ -791,23 +809,23 @@ | ||
| 791 | 809 | (or before that). |
| 792 | 810 | */ |
| 793 | 811 | jsTime = (cson_int_t)db_int64(0, "SELECT strftime('%%s','now')"); |
| 794 | 812 | #endif |
| 795 | 813 | tmp = cson_value_new_integer(jsTime); |
| 796 | - SET("timestamp"); | |
| 814 | + SET(FossilJsonKeys.timestamp); | |
| 797 | 815 | } |
| 798 | 816 | if( 0 != resultCode ){ |
| 799 | 817 | if( ! pMsg ) pMsg = json_err_str(resultCode); |
| 800 | 818 | tmp = json_rc_string(resultCode); |
| 801 | - SET("resultCode"); | |
| 819 | + SET(FossilJsonKeys.resultCode); | |
| 802 | 820 | } |
| 803 | 821 | if( pMsg && *pMsg ){ |
| 804 | 822 | tmp = cson_value_new_string(pMsg,strlen(pMsg)); |
| 805 | - SET("resultText"); | |
| 823 | + SET(FossilJsonKeys.resultText); | |
| 806 | 824 | } |
| 807 | - tmp = json_getenv("requestId"); | |
| 808 | - if( tmp ) cson_object_set( o, "requestId", tmp ); | |
| 825 | + tmp = json_getenv(FossilJsonKeys.requestId); | |
| 826 | + if( tmp ) cson_object_set( o, FossilJsonKeys.requestId, tmp ); | |
| 809 | 827 | |
| 810 | 828 | if(0){/* these are only intended for my own testing...*/ |
| 811 | 829 | if(g.json.cmd.v){ |
| 812 | 830 | tmp = g.json.cmd.v; |
| 813 | 831 | SET("$commandPath"); |
| @@ -828,11 +846,11 @@ | ||
| 828 | 846 | if( resultCode ){ |
| 829 | 847 | cson_value_free(payload); |
| 830 | 848 | payload = NULL; |
| 831 | 849 | }else{ |
| 832 | 850 | tmp = payload; |
| 833 | - SET("payload"); | |
| 851 | + SET(FossilJsonKeys.payload); | |
| 834 | 852 | } |
| 835 | 853 | } |
| 836 | 854 | |
| 837 | 855 | #undef SET |
| 838 | 856 | goto ok; |
| 839 | 857 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -41,15 +41,19 @@ | |
| 41 | #include "cson_amalgamation.h" |
| 42 | #include "json_detail.h" /* workaround for apparent enum limitation in makeheaders */ |
| 43 | #endif |
| 44 | |
| 45 | /* |
| 46 | ** Signature for JSON page/command callbacks. By the time the callback |
| 47 | ** is called, json_page_top() or json_cmd_top() will have set up the |
| 48 | ** JSON-related environment. Implementations may generate a "result |
| 49 | ** payload" of any JSON type by returning its value from this function |
| 50 | ** (ownership is tranferred to the caller). On error they should set |
| 51 | ** g.json.resultCode to one of the FossilJsonCodes values and return |
| 52 | ** either their payload object or NULL. Note that NULL is a legal |
| 53 | ** success value - it simply means the response will contain no |
| 54 | ** payload. If g.json.resultCode is non-zero when this function |
| 55 | ** returns then the top-level dispatcher will destroy any payload |
| @@ -70,14 +74,17 @@ | |
| 70 | ** All of the setup/response code is handled by the top dispatcher |
| 71 | ** functions and the callbacks concern themselves only with generating |
| 72 | ** the payload. |
| 73 | ** |
| 74 | ** It is imperitive that NO callback functions EVER output ANYTHING to |
| 75 | ** stdout, as that will effectively corrupt any HTTP output. |
| 76 | */ |
| 77 | typedef cson_value * (*fossil_json_f)(unsigned int depth); |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | ** Placeholder /json/XXX page impl for NYI (Not Yet Implemented) |
| 82 | ** (but planned) pages/commands. |
| 83 | */ |
| @@ -88,17 +95,28 @@ | |
| 88 | |
| 89 | /* |
| 90 | ** Holds keys used for various JSON API properties. |
| 91 | */ |
| 92 | static const struct FossilJsonKeys_{ |
| 93 | char const * authToken; |
| 94 | char const * commandPath; |
| 95 | char const * anonymousSeed; |
| 96 | } FossilJsonKeys = { |
| 97 | "authToken" /*authToken*/, |
| 98 | "COMMAND_PATH" /*commandPath*/, |
| 99 | "anonymousSeed" /*anonymousSeed*/ |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | ** Given a FossilJsonCodes value, it returns a string suitable for use |
| 104 | ** as a resultText string. Returns some unspecified string if errCode |
| @@ -548,11 +566,11 @@ | |
| 548 | |
| 549 | /* g.json.reqPayload exists only to simplify some of our access to |
| 550 | the request payload. We currently only use this in the context of |
| 551 | Object payloads, not Arrays, strings, etc. |
| 552 | */ |
| 553 | g.json.reqPayload.v = cson_object_get( g.json.post.o, "payload" ); |
| 554 | if( g.json.reqPayload.v ){ |
| 555 | g.json.reqPayload.o = cson_value_get_object( g.json.reqPayload.v ) |
| 556 | /* g.json.reqPayload.o may legally be NULL, which means only that |
| 557 | g.json.reqPayload.v is-not-a Object. |
| 558 | */; |
| @@ -765,11 +783,11 @@ | |
| 765 | }while(0) |
| 766 | |
| 767 | tmp = cson_value_new_string(MANIFEST_UUID,strlen(MANIFEST_UUID)); |
| 768 | SET("fossil"); |
| 769 | |
| 770 | {/* "timestamp" */ |
| 771 | cson_int_t jsTime; |
| 772 | #if 1 |
| 773 | jsTime = (cson_int_t)time(0); |
| 774 | #elif 1 |
| 775 | /* Ge Weijers has pointed out that time(0) commonly returns |
| @@ -791,23 +809,23 @@ | |
| 791 | (or before that). |
| 792 | */ |
| 793 | jsTime = (cson_int_t)db_int64(0, "SELECT strftime('%%s','now')"); |
| 794 | #endif |
| 795 | tmp = cson_value_new_integer(jsTime); |
| 796 | SET("timestamp"); |
| 797 | } |
| 798 | if( 0 != resultCode ){ |
| 799 | if( ! pMsg ) pMsg = json_err_str(resultCode); |
| 800 | tmp = json_rc_string(resultCode); |
| 801 | SET("resultCode"); |
| 802 | } |
| 803 | if( pMsg && *pMsg ){ |
| 804 | tmp = cson_value_new_string(pMsg,strlen(pMsg)); |
| 805 | SET("resultText"); |
| 806 | } |
| 807 | tmp = json_getenv("requestId"); |
| 808 | if( tmp ) cson_object_set( o, "requestId", tmp ); |
| 809 | |
| 810 | if(0){/* these are only intended for my own testing...*/ |
| 811 | if(g.json.cmd.v){ |
| 812 | tmp = g.json.cmd.v; |
| 813 | SET("$commandPath"); |
| @@ -828,11 +846,11 @@ | |
| 828 | if( resultCode ){ |
| 829 | cson_value_free(payload); |
| 830 | payload = NULL; |
| 831 | }else{ |
| 832 | tmp = payload; |
| 833 | SET("payload"); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | #undef SET |
| 838 | goto ok; |
| 839 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -41,15 +41,19 @@ | |
| 41 | #include "cson_amalgamation.h" |
| 42 | #include "json_detail.h" /* workaround for apparent enum limitation in makeheaders */ |
| 43 | #endif |
| 44 | |
| 45 | /* |
| 46 | ** Signature for JSON page/command callbacks. Each callback is |
| 47 | ** responsible for handling one JSON request/command and/or |
| 48 | ** dispatching to sub-commands. |
| 49 | ** |
| 50 | ** By the time the callback is called, json_page_top() (HTTP mode) or |
| 51 | ** json_cmd_top() (CLI mode) will have set up the JSON-related |
| 52 | ** environment. Implementations may generate a "result payload" of any |
| 53 | ** JSON type by returning its value from this function (ownership is |
| 54 | ** tranferred to the caller). On error they should set |
| 55 | ** g.json.resultCode to one of the FossilJsonCodes values and return |
| 56 | ** either their payload object or NULL. Note that NULL is a legal |
| 57 | ** success value - it simply means the response will contain no |
| 58 | ** payload. If g.json.resultCode is non-zero when this function |
| 59 | ** returns then the top-level dispatcher will destroy any payload |
| @@ -70,14 +74,17 @@ | |
| 74 | ** All of the setup/response code is handled by the top dispatcher |
| 75 | ** functions and the callbacks concern themselves only with generating |
| 76 | ** the payload. |
| 77 | ** |
| 78 | ** It is imperitive that NO callback functions EVER output ANYTHING to |
| 79 | ** stdout, as that will effectively corrupt any JSON output, and |
| 80 | ** almost certainly will corrupt any HTTP response headers. Output |
| 81 | ** sent to stderr ends up in my apache log, so that might be useful |
| 82 | ** for debuggering in some cases, but so such code should be left |
| 83 | ** enabled for non-debuggering builds. |
| 84 | */ |
| 85 | typedef cson_value * (*fossil_json_f)(unsigned int depth); |
| 86 | |
| 87 | /* |
| 88 | ** Placeholder /json/XXX page impl for NYI (Not Yet Implemented) |
| 89 | ** (but planned) pages/commands. |
| 90 | */ |
| @@ -88,17 +95,28 @@ | |
| 95 | |
| 96 | /* |
| 97 | ** Holds keys used for various JSON API properties. |
| 98 | */ |
| 99 | static const struct FossilJsonKeys_{ |
| 100 | /** maintainers: please keep alpha sorted (case-insensitive) */ |
| 101 | char const * commandPath; |
| 102 | char const * anonymousSeed; |
| 103 | char const * authToken; |
| 104 | char const * payload; |
| 105 | char const * requestId; |
| 106 | char const * resultCode; |
| 107 | char const * resultText; |
| 108 | char const * timestamp; |
| 109 | } FossilJsonKeys = { |
| 110 | "COMMAND_PATH" /*commandPath*/, |
| 111 | "anonymousSeed" /*anonymousSeed*/, |
| 112 | "authToken" /*authToken*/, |
| 113 | "payload" /* payload */, |
| 114 | "requestId" /*requestId*/, |
| 115 | "resultCode" /*resultCode*/, |
| 116 | "resultText" /*resultText*/, |
| 117 | "timestamp" /*timestamp*/ |
| 118 | }; |
| 119 | |
| 120 | /* |
| 121 | ** Given a FossilJsonCodes value, it returns a string suitable for use |
| 122 | ** as a resultText string. Returns some unspecified string if errCode |
| @@ -548,11 +566,11 @@ | |
| 566 | |
| 567 | /* g.json.reqPayload exists only to simplify some of our access to |
| 568 | the request payload. We currently only use this in the context of |
| 569 | Object payloads, not Arrays, strings, etc. |
| 570 | */ |
| 571 | g.json.reqPayload.v = cson_object_get( g.json.post.o, FossilJsonKeys.payload ); |
| 572 | if( g.json.reqPayload.v ){ |
| 573 | g.json.reqPayload.o = cson_value_get_object( g.json.reqPayload.v ) |
| 574 | /* g.json.reqPayload.o may legally be NULL, which means only that |
| 575 | g.json.reqPayload.v is-not-a Object. |
| 576 | */; |
| @@ -765,11 +783,11 @@ | |
| 783 | }while(0) |
| 784 | |
| 785 | tmp = cson_value_new_string(MANIFEST_UUID,strlen(MANIFEST_UUID)); |
| 786 | SET("fossil"); |
| 787 | |
| 788 | {/* timestamp */ |
| 789 | cson_int_t jsTime; |
| 790 | #if 1 |
| 791 | jsTime = (cson_int_t)time(0); |
| 792 | #elif 1 |
| 793 | /* Ge Weijers has pointed out that time(0) commonly returns |
| @@ -791,23 +809,23 @@ | |
| 809 | (or before that). |
| 810 | */ |
| 811 | jsTime = (cson_int_t)db_int64(0, "SELECT strftime('%%s','now')"); |
| 812 | #endif |
| 813 | tmp = cson_value_new_integer(jsTime); |
| 814 | SET(FossilJsonKeys.timestamp); |
| 815 | } |
| 816 | if( 0 != resultCode ){ |
| 817 | if( ! pMsg ) pMsg = json_err_str(resultCode); |
| 818 | tmp = json_rc_string(resultCode); |
| 819 | SET(FossilJsonKeys.resultCode); |
| 820 | } |
| 821 | if( pMsg && *pMsg ){ |
| 822 | tmp = cson_value_new_string(pMsg,strlen(pMsg)); |
| 823 | SET(FossilJsonKeys.resultText); |
| 824 | } |
| 825 | tmp = json_getenv(FossilJsonKeys.requestId); |
| 826 | if( tmp ) cson_object_set( o, FossilJsonKeys.requestId, tmp ); |
| 827 | |
| 828 | if(0){/* these are only intended for my own testing...*/ |
| 829 | if(g.json.cmd.v){ |
| 830 | tmp = g.json.cmd.v; |
| 831 | SET("$commandPath"); |
| @@ -828,11 +846,11 @@ | |
| 846 | if( resultCode ){ |
| 847 | cson_value_free(payload); |
| 848 | payload = NULL; |
| 849 | }else{ |
| 850 | tmp = payload; |
| 851 | SET(FossilJsonKeys.payload); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | #undef SET |
| 856 | goto ok; |
| 857 |