Fossil SCM

minor internal cleanups and doc additions.

stephan 2011-09-20 16:27 UTC json
Commit 2f3e438507702c0fbbe63e8035154b1bfae26cd1
1 file changed +36 -18
+36 -18
--- src/json.c
+++ src/json.c
@@ -41,15 +41,19 @@
4141
#include "cson_amalgamation.h"
4242
#include "json_detail.h" /* workaround for apparent enum limitation in makeheaders */
4343
#endif
4444
4545
/*
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
5155
** g.json.resultCode to one of the FossilJsonCodes values and return
5256
** either their payload object or NULL. Note that NULL is a legal
5357
** success value - it simply means the response will contain no
5458
** payload. If g.json.resultCode is non-zero when this function
5559
** returns then the top-level dispatcher will destroy any payload
@@ -70,14 +74,17 @@
7074
** All of the setup/response code is handled by the top dispatcher
7175
** functions and the callbacks concern themselves only with generating
7276
** the payload.
7377
**
7478
** 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.
7684
*/
7785
typedef cson_value * (*fossil_json_f)(unsigned int depth);
78
-
7986
8087
/*
8188
** Placeholder /json/XXX page impl for NYI (Not Yet Implemented)
8289
** (but planned) pages/commands.
8390
*/
@@ -88,17 +95,28 @@
8895
8996
/*
9097
** Holds keys used for various JSON API properties.
9198
*/
9299
static const struct FossilJsonKeys_{
93
- char const * authToken;
100
+ /** maintainers: please keep alpha sorted (case-insensitive) */
94101
char const * commandPath;
95102
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;
96109
} FossilJsonKeys = {
97
- "authToken" /*authToken*/,
98110
"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*/
100118
};
101119
102120
/*
103121
** Given a FossilJsonCodes value, it returns a string suitable for use
104122
** as a resultText string. Returns some unspecified string if errCode
@@ -548,11 +566,11 @@
548566
549567
/* g.json.reqPayload exists only to simplify some of our access to
550568
the request payload. We currently only use this in the context of
551569
Object payloads, not Arrays, strings, etc.
552570
*/
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 );
554572
if( g.json.reqPayload.v ){
555573
g.json.reqPayload.o = cson_value_get_object( g.json.reqPayload.v )
556574
/* g.json.reqPayload.o may legally be NULL, which means only that
557575
g.json.reqPayload.v is-not-a Object.
558576
*/;
@@ -765,11 +783,11 @@
765783
}while(0)
766784
767785
tmp = cson_value_new_string(MANIFEST_UUID,strlen(MANIFEST_UUID));
768786
SET("fossil");
769787
770
- {/* "timestamp" */
788
+ {/* timestamp */
771789
cson_int_t jsTime;
772790
#if 1
773791
jsTime = (cson_int_t)time(0);
774792
#elif 1
775793
/* Ge Weijers has pointed out that time(0) commonly returns
@@ -791,23 +809,23 @@
791809
(or before that).
792810
*/
793811
jsTime = (cson_int_t)db_int64(0, "SELECT strftime('%%s','now')");
794812
#endif
795813
tmp = cson_value_new_integer(jsTime);
796
- SET("timestamp");
814
+ SET(FossilJsonKeys.timestamp);
797815
}
798816
if( 0 != resultCode ){
799817
if( ! pMsg ) pMsg = json_err_str(resultCode);
800818
tmp = json_rc_string(resultCode);
801
- SET("resultCode");
819
+ SET(FossilJsonKeys.resultCode);
802820
}
803821
if( pMsg && *pMsg ){
804822
tmp = cson_value_new_string(pMsg,strlen(pMsg));
805
- SET("resultText");
823
+ SET(FossilJsonKeys.resultText);
806824
}
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 );
809827
810828
if(0){/* these are only intended for my own testing...*/
811829
if(g.json.cmd.v){
812830
tmp = g.json.cmd.v;
813831
SET("$commandPath");
@@ -828,11 +846,11 @@
828846
if( resultCode ){
829847
cson_value_free(payload);
830848
payload = NULL;
831849
}else{
832850
tmp = payload;
833
- SET("payload");
851
+ SET(FossilJsonKeys.payload);
834852
}
835853
}
836854
837855
#undef SET
838856
goto ok;
839857
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button