#ifdef FOSSIL_ENABLE_JSON /* ** Copyright (c) 2011 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the Simplified BSD License (also ** known as the "2-Clause License" or "FreeBSD License".) ** This program is distributed in the hope that it will be useful, ** but without any warranty; without even the implied warranty of ** merchantability or fitness for a particular purpose. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ******************************************************************************* ** ** Code for the JSON API. ** ** For notes regarding the public JSON interface, please see: ** ** https://docs.google.com/document/d/1fXViveNhDbiXgCuE7QDXQOKeFzf2qNUkBEgiUvoqFN4/edit ** ** ** Notes for hackers... ** ** Here's how command/page dispatching works: json_page_top() (in HTTP mode) or ** json_cmd_top() (in CLI mode) catch the "json" path/command. Those functions then ** dispatch to a JSON-mode-specific command/page handler with the type fossil_json_f(). ** See the API docs for that typedef (below) for the semantics of the callbacks. ** ** */ #include "config.h" #include "VERSION.h" #include "json.h" #include #include #if INTERFACE #include "json_detail.h" /* workaround for apparent enum limitation in makeheaders */ #endif const FossilJsonKeys_ FossilJsonKeys = { "anonymousSeed" /*anonymousSeed*/, "authToken" /*authToken*/, "COMMAND_PATH" /*commandPath*/, "mtime" /*mtime*/, "payload" /* payload */, "requestId" /*requestId*/, "resultCode" /*resultCode*/, "resultText" /*resultText*/, "timestamp" /*timestamp*/ }; /* Timer code taken from sqlite3's shell.c, modified slightly. FIXME: move the timer into the fossil core API so that we can start the timer early on in the app init phase. Right now we're just timing the json ops themselves. */ #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL) #include #include /* Saved resource information for the beginning of an operation */ static struct rusage sBegin; /* ** Begin timing an operation */ static void beginTimer(void){ getrusage(RUSAGE_SELF, &sBegin); } /* Return the difference of two time_structs in milliseconds */ static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ return ((pEnd->tv_usec - pStart->tv_usec)*0.001 + (double)((pEnd->tv_sec - pStart->tv_sec)*1000.0)); } /* ** Print the timing results. */ static double endTimer(void){ struct rusage sEnd; getrusage(RUSAGE_SELF, &sEnd); return timeDiff(&sBegin.ru_utime, &sEnd.ru_utime) + timeDiff(&sBegin.ru_stime, &sEnd.ru_stime); #if 0 printf("CPU Time: user %f sys %f\n", timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); #endif } #define BEGIN_TIMER beginTimer() #define END_TIMER endTimer() #define HAS_TIMER 1 #elif (defined(_WIN32) || defined(WIN32)) #include /* Saved resource information for the beginning of an operation */ static HANDLE hProcess; static FILETIME ftKernelBegin; static FILETIME ftUserBegin; typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME); static GETPROCTIMES getProcessTimesAddr = NULL; /* ** Check to see if we have timer support. Return 1 if necessary ** support found (or found previously). */ static int hasTimer(void){ if( getProcessTimesAddr ){ return 1; } else { /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions. ** See if the version we are running on has it, and if it does, save off ** a pointer to it and the current process handle. */ hProcess = GetCurrentProcess(); if( hProcess ){ HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); if( NULL != hinstLib ){ getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); if( NULL != getProcessTimesAddr ){ return 1; } FreeLibrary(hinstLib); } } } return 0; } /* ** Begin timing an operation */ static void beginTimer(void){ if( getProcessTimesAddr ){ FILETIME ftCreation, ftExit; getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin); } } /* Return the difference of two FILETIME structs in milliseconds */ static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ sqlite_int64 i64Start = *((sqlite_int64 *) pStart); sqlite_int64 i64End = *((sqlite_int64 *) pEnd); return (double) ((i64End - i64Start) / 10000.0); } /* ** Print the timing results. */ static double endTimer(void){ if(getProcessTimesAddr){ FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd); return timeDiff(&ftUserBegin, &ftUserEnd) + timeDiff(&ftKernelBegin, &ftKernelEnd); } } #define BEGIN_TIMER beginTimer() #define END_TIMER endTimer() #define HAS_TIMER hasTimer() #else #define BEGIN_TIMER #define END_TIMER 0.0 #define HAS_TIMER 0 #endif char fossil_has_json(){ return g.json.isJsonMode && (g.isHTTP || g.json.post.o); } /* ** Placeholder /json/XXX page impl for NYI (Not Yet Implemented) ** (but planned) pages/commands. */ cson_value * json_page_nyi(){ g.json.resultCode = FSL_JSON_E_NYI; return NULL; } /* ** Given a FossilJsonCodes value, it returns a string suitable for use ** as a resultCode string. Returns some unspecified non-empty string ** if errCode is not one of the FossilJsonCodes values. */ static char const * json_err_cstr( int errCode ){ switch( errCode ){ case 0: return "Success"; #define C(X,V) case FSL_JSON_E_ ## X: return V C(GENERIC,"Generic error"); C(INVALID_REQUEST,"Invalid request"); C(UNKNOWN_COMMAND,"Unknown command or subcommand"); C(UNKNOWN,"Unknown error"); C(TIMEOUT,"Timeout reached"); C(ASSERT,"Assertion failed"); C(ALLOC,"Resource allocation failed"); C(NYI,"Not yet implemented"); C(PANIC,"x"); C(MANIFEST_READ_FAILED,"Reading artifact manifest failed"); C(FILE_OPEN_FAILED,"Opening file failed"); C(AUTH,"Authentication error"); C(MISSING_AUTH,"Authentication info missing from request"); C(DENIED,"Access denied"); C(WRONG_MODE,"Request not allowed (wrong operation mode)"); C(LOGIN_FAILED,"Login failed"); C(LOGIN_FAILED_NOSEED,"Anonymous login attempt was missing password seed"); C(LOGIN_FAILED_NONAME,"Login failed - name not supplied"); C(LOGIN_FAILED_NOPW,"Login failed - password not supplied"); C(LOGIN_FAILED_NOTFOUND,"Login failed - no match found"); C(USAGE,"Usage error"); C(INVALID_ARGS,"Invalid argument(s)"); C(MISSING_ARGS,"Missing argument(s)"); C(AMBIGUOUS_UUID,"Resource identifier is ambiguous"); C(UNRESOLVED_UUID,"Provided uuid/tag/branch could not be resolved"); C(RESOURCE_ALREADY_EXISTS,"Resource already exists"); C(RESOURCE_NOT_FOUND,"Resource not found"); C(DB,"Database error"); C(STMT_PREP,"Statement preparation failed"); C(STMT_BIND,"Statement parameter binding failed"); C(STMT_EXEC,"Statement execution/stepping failed"); C(DB_LOCKED,"Database is locked"); C(DB_NEEDS_REBUILD,"Fossil repository needs to be rebuilt"); C(DB_NOT_FOUND,"Fossil repository db file could not be found."); C(DB_NOT_VALID, "Fossil repository db file is not valid."); #undef C default: return "Unknown Error"; } } /* ** Implements the cson_data_dest_f() interface and outputs the data to ** a fossil Blob object. pState must be-a initialized (Blob*), to ** which n bytes of src will be appended. **/ int cson_data_dest_Blob(void * pState, void const * src, unsigned int n){ Blob * b = (Blob*)pState; blob_append( b, (char const *)src, (int)n ) /* will die on OOM */; return 0; } /* ** Implements the cson_data_source_f() interface and reads input from ** a fossil Blob object. pState must be-a (Blob*) populated with JSON ** data. */ int cson_data_src_Blob(void * pState, void * dest, unsigned int * n){ Blob * b = (Blob*)pState; *n = blob_read( b, dest, *n ); return 0; } /* ** Convenience wrapper around cson_output() which appends the output ** to pDest. pOpt may be NULL, in which case g.json.outOpt will be used. */ int cson_output_Blob( cson_value const * pVal, Blob * pDest, cson_output_opt const * pOpt ){ return cson_output( pVal, cson_data_dest_Blob, pDest, pOpt ? pOpt : &g.json.outOpt ); } /* ** Convenience wrapper around cson_parse() which reads its input ** from pSrc. pSrc is rewound before parsing. ** ** pInfo may be NULL. If it is not NULL then it will contain details ** about the parse state when this function returns. ** ** On success a new JSON Object or Array is returned (owned by the ** caller). On error NULL is returned. */ cson_value * cson_parse_Blob( Blob * pSrc, cson_parse_info * pInfo ){ cson_value * root = NULL; blob_rewind( pSrc ); cson_parse( &root, cson_data_src_Blob, pSrc, NULL, pInfo ); return root; } /* ** Implements the cson_data_dest_f() interface and outputs the data to ** cgi_append_content(). pState is ignored. **/ int cson_data_dest_cgi(void * pState, void const * src, unsigned int n){ cgi_append_content( (char const *)src, (int)n ); return 0; } /* ** Returns a string in the form FOSSIL-XXXX, where XXXX is a ** left-zero-padded value of code. The returned buffer is static, and ** must be copied if needed for later. The returned value will always ** be 11 bytes long (not including the trailing NUL byte). ** ** In practice we will only ever call this one time per app execution ** when constructing the JSON response envelope, so the static buffer ** "shouldn't" be a problem. ** */ char const * json_rc_cstr( int code ){ enum { BufSize = 12 }; static char buf[BufSize] = {'F','O','S','S','I','L','-',0}; assert((code >= 1000) && (code <= 9999) && "Invalid Fossil/JSON code."); sprintf(buf+7,"%04d", code); return buf; } /* ** Adds v to the API-internal cleanup mechanism. key is ingored ** (legacy) but might be re-introduced and "should" be a unique ** (app-wide) value. Failure to insert an item may be caused by any ** of the following: ** ** - Allocation error. ** - g.json.gc.a is NULL ** - key is NULL or empty. ** ** Returns 0 on success. ** ** Ownership of v is transfered to (or shared with) g.json.gc, and v ** will be valid until that object is cleaned up or some internal code ** incorrectly removes it from the gc (which we never do). If this ** function fails, it is fatal to the app (as it indicates an ** allocation error (more likely than not) or a serious internal error ** such as numeric overflow). */ void json_gc_add( char const * key, cson_value * v ){ int const rc = cson_array_append( g.json.gc.a, v ); assert( NULL != g.json.gc.a ); if( 0 != rc ){ cson_value_free( v ); } assert( (0==rc) && "Adding item to GC failed." ); if(0!=rc){ fprintf(stderr,"%s: FATAL: alloc error.\n", fossil_nameofexe()) /* reminder: allocation error is the only reasonable cause of error here, provided g.json.gc.a and v are not NULL. */ ; fossil_exit(1)/*not fossil_panic() b/c it might land us somewhere where this function is called again. */; } } /* ** Returns the value of json_rc_cstr(code) as a new JSON ** string, which is owned by the caller and must eventually ** be cson_value_free()d or transfered to a JSON container. */ cson_value * json_rc_string( int code ){ return cson_value_new_string( json_rc_cstr(code), 11 ); } cson_value * json_new_string( char const * str ){ return str ? cson_value_new_string(str,strlen(str)) : NULL; } cson_value * json_new_string_f( char const * fmt, ... ){ cson_value * v; char * zStr; va_list vargs; va_start(vargs,fmt); zStr = vmprintf(fmt,vargs); va_end(vargs); v = cson_value_new_string(zStr, strlen(zStr)); free(zStr); return v; } cson_value * json_new_int( int v ){ return cson_value_new_integer((cson_int_t)v); } /* ** Gets a POST/POST.payload/GET/COOKIE/ENV value. The returned memory ** is owned by the g.json object (one of its sub-objects). Returns ** NULL if no match is found. ** ** ENV means the system environment (getenv()). ** ** Precedence: POST.payload, GET/COOKIE/non-JSON POST, JSON POST, ENV. ** ** FIXME: the precedence SHOULD be: GET, POST.payload, POST, COOKIE, ** ENV, but the amalgamation of the GET/POST vars makes it difficult ** for me to do that. Since fossil only uses one cookie, cookie ** precedence isn't a real/high-priority problem. */ cson_value * json_getenv( char const * zKey ){ cson_value * rc; rc = g.json.reqPayload.o ? cson_object_get( g.json.reqPayload.o, zKey ) : NULL; if(rc){ return rc; } rc = cson_object_get( g.json.param.o, zKey ); if( rc ){ return rc; } rc = cson_object_get( g.json.post.o, zKey ); if(rc){ return rc; }else{ char const * cv = PD(zKey,NULL); if(!cv && !g.isHTTP){ /* reminder to self: in CLI mode i'd like to try find_option(zKey,NULL,XYZ) here, but we don't have a sane default for the XYZ param here. */ cv = fossil_getenv(zKey); } if(cv){/*transform it to JSON for later use.*/ /* use sscanf() to figure out if it's an int, and transform it to JSON int if it is. FIXME: use strtol(), since it has more accurate error handling. */ int intVal = -1; char endOfIntCheck; int const scanRc = sscanf(cv,"%d%c",&intVal, &endOfIntCheck) /* The %c bit there is to make sure that we don't accept 123x as a number. sscanf() returns the number of tokens successfully parsed, so an RC of 1 will be correct for "123" but "123x" will have RC==2. But it appears to not be working that way :/ */ ; if(1==scanRc){ json_setenv( zKey, cson_value_new_integer(intVal) ); }else{ rc = cson_value_new_string(cv,strlen(cv)); json_setenv( zKey, rc ); } return rc; }else{ return NULL; } } } /* ** Wrapper around json_getenv() which... ** ** If it finds a value and that value is-a JSON number or is a string ** which looks like an integer or is-a JSON bool/null then it is ** converted to an int. If none of those apply then dflt is returned. */ int json_getenv_int(char const * pKey, int dflt ){ cson_value const * v = json_getenv(pKey); if(!v){ return dflt; }else if( cson_value_is_number(v) ){ return (int)cson_value_get_integer(v); }else if( cson_value_is_string(v) ){ char const * sv = cson_string_cstr(cson_value_get_string(v)); assert( (NULL!=sv) && "This is quite unexpected." ); return sv ? atoi(sv) : dflt; }else if( cson_value_is_bool(v) ){ return cson_value_get_bool(v) ? 1 : 0; }else if( cson_value_is_null(v) ){ return 0; }else{ /* we should arguably treat JSON null as 0. */ return dflt; } } /* ** Wrapper around json_getenv() which tries to evaluate a payload/env ** value as a boolean. Uses mostly the same logic as ** json_getenv_int(), with the addition that string values which ** either start with a digit 1..9 or the letters [tT] are considered ** to be true. If this function cannot find a matching key/value then ** dflt is returned. e.g. if it finds the key but the value is-a ** Object then dftl is returned. ** ** If an entry is found, this function guarantees that it will return ** either 0 or 1, as opposed to "0 or non-zero", so that clients can ** pass a different value as dflt. Thus they can use, e.g. -1 to know ** whether or not this function found a match (it will return -1 in ** that case). */ char json_getenv_bool(char const * pKey, char dflt ){ cson_value const * v = json_getenv(pKey); if(!v){ return dflt; }else if( cson_value_is_number(v) ){ return cson_value_get_integer(v) ? 1 : 0; }else if( cson_value_is_string(v) ){ char const * sv = cson_string_cstr(cson_value_get_string(v)); if(!*sv || ('0'==*sv)){ return 0; }else{ return (('t'==*sv) || ('T'==*sv) || (('1'<=*sv) && ('9'>=*sv))) ? 1 : 0; } }else if( cson_value_is_bool(v) ){ return cson_value_get_bool(v) ? 1 : 0; }else if( cson_value_is_null(v) ){ return 0; }else{ return dflt; } } /* ** Returns the string form of a json_getenv() value, but ONLY If that ** value is-a String. Non-strings are not converted to strings for ** this purpose. Returned memory is owned by g.json or fossil and is ** valid until end-of-app or the given key is replaced in fossil's ** internals via cgi_replace_parameter() and friends or json_setenv(). */ char const * json_getenv_cstr( char const * zKey ){ return cson_value_get_cstr( json_getenv(zKey) ); } /* ** An extended form of find_option() which tries to look up a combo ** GET/POST/CLI argument. ** ** zKey must be the GET/POST parameter key. zCLILong must be the "long ** form" CLI flag (NULL means to use zKey). zCLIShort may be NULL or ** the "short form" CLI flag (if NULL, no short form is used). ** ** If argPos is >=0 and no other match is found, ** json_command_arg(argPos) is also checked. ** ** On error (no match found) NULL is returned. ** ** This ONLY works for String JSON/GET/CLI values, not JSON ** booleans and whatnot. */ char const * json_find_option_cstr2(char const * zKey, char const * zCLILong, char const * zCLIShort, int argPos){ char const * rc = NULL; assert(NULL != zKey); if(!g.isHTTP){ rc = find_option(zCLILong ? zCLILong : zKey, zCLIShort, 1); } if(!rc && fossil_has_json()){ rc = json_getenv_cstr(zKey); } if(!rc && (argPos>=0)){ rc = json_command_arg((unsigned char)argPos); } return rc; } /* ** Short-hand form of json_find_option_cstr(zKey,zCLILong,zCLIShort,-1). */ char const * json_find_option_cstr(char const * zKey, char const * zCLILong, char const * zCLIShort){ return json_find_option_cstr2(zKey, zCLILong, zCLIShort, -1); } /* ** The boolean equivalent of json_find_option_cstr(). ** If the option is not found, dftl is returned. */ char json_find_option_bool(char const * zKey, char const * zCLILong, char const * zCLIShort, char dflt ){ char rc = -1; if(!g.isHTTP){ if(NULL != find_option(zCLILong ? zCLILong : zKey, zCLIShort, 0)){ rc = 1; } } if((-1==rc) && fossil_has_json()){ rc = json_getenv_bool(zKey,-1); } return (-1==rc) ? dflt : rc; } /* ** The integer equivalent of json_find_option_cstr(). ** If the option is not found, dftl is returned. */ int json_find_option_int(char const * zKey, char const * zCLILong, char const * zCLIShort, int dflt ){ enum { Magic = -947 }; int rc = Magic; if(!g.isHTTP){ /* FIXME: use strtol() for better error/dflt handling. */ char const * opt = find_option(zCLILong ? zCLILong : zKey, zCLIShort, 1); if(NULL!=opt){ rc = atoi(opt); } } if(Magic==rc){ rc = json_getenv_int(zKey,Magic); } return (Magic==rc) ? dflt : rc; } /* ** Adds v to g.json.param.o using the given key. May cause any prior ** item with that key to be destroyed (depends on current reference ** count for that value). On success, transfers (or shares) ownership ** of v to (or with) g.json.param.o. On error ownership of v is not ** modified. */ int json_setenv( char const * zKey, cson_value * v ){ return cson_object_set( g.json.param.o, zKey, v ); } /* ** Guesses a RESPONSE Content-Type value based (primarily) on the ** HTTP_ACCEPT header. ** ** It will try to figure out if the client can support ** application/json or application/javascript, and will fall back to ** text/plain if it cannot figure out anything more specific. ** ** Returned memory is static and immutable, but if the environment ** changes after calling this then subsequent calls to this function ** might return different (also static/immutable) values. */ char const * json_guess_content_type(){ char const * cset; char doUtf8; cset = PD("HTTP_ACCEPT_CHARSET",NULL); doUtf8 = ((NULL == cset) || (NULL!=strstr("utf-8",cset))) ? 1 : 0; if( g.json.jsonp ){ return doUtf8 ? "application/javascript; charset=utf-8" : "application/javascript"; }else{ /* Content-type If the browser does not sent an ACCEPT for application/json then we fall back to text/plain. */ char const * cstr; cstr = PD("HTTP_ACCEPT",NULL); if( NULL == cstr ){ return doUtf8 ? "application/json; charset=utf-8" : "application/json"; }else{ if( strstr( cstr, "application/json" ) || strstr( cstr, "*/*" ) ){ return doUtf8 ? "application/json; charset=utf-8" : "application/json"; }else{ return "text/plain"; } } } } /* ** Sends pResponse to the output stream as the response object. This ** function does no validation of pResponse except to assert() that it ** is not NULL. The caller is responsible for ensuring that it meets ** API response envelope conventions. ** ** In CLI mode pResponse is sent to stdout immediately. In HTTP ** mode pResponse replaces any current CGI content but cgi_reply() ** is not called to flush the output. ** ** If g.json.jsonp is not NULL then the content type is set to ** application/javascript and the output is wrapped in a jsonp ** wrapper. */ void json_send_response( cson_value const * pResponse ){ assert( NULL != pResponse ); if( g.isHTTP ){ cgi_reset_content(); if( g.json.jsonp ){ cgi_printf("%s(",g.json.jsonp); } cson_output( pResponse, cson_data_dest_cgi, NULL, &g.json.outOpt ); if( g.json.jsonp ){ cgi_append_content(")",1); } }else{/*CLI mode*/ if( g.json.jsonp ){ fprintf(stdout,"%s(",g.json.jsonp); } cson_output_FILE( pResponse, stdout, &g.json.outOpt ); if( g.json.jsonp ){ fwrite(")\n", 2, 1, stdout); } } } /* ** Returns the current request's JSON authentication token, or NULL if ** none is found. The token's memory is owned by (or shared with) ** g.json. ** ** If an auth token is found in the GET/POST request data then fossil ** is given that data for use in authentication for this ** session. i.e. the GET/POST data overrides fossil's authentication ** cookie value (if any) and also works with clients which do not ** support cookies. ** ** Must be called once before login_check_credentials() is called or ** we will not be able to replace fossil's internal idea of the auth ** info in time (and future changes to that state may cause unexpected ** results). ** ** The result of this call are cached for future calls. */ cson_value * json_auth_token(){ if( !g.json.authToken ){ /* Try to get an authorization token from GET parameter, POSTed JSON, or fossil cookie (in that order). */ g.json.authToken = json_getenv(FossilJsonKeys.authToken); if(g.json.authToken && cson_value_is_string(g.json.authToken) && !PD(login_cookie_name(),NULL)){ /* tell fossil to use this login info. FIXME?: because the JSON bits don't carry around login_cookie_name(), there is a potential login hijacking window here. We may need to change the JSON auth token to be in the form: login_cookie_name()=... Then again, the hardened cookie value helps ensure that only a proper key/value match is valid. */ cgi_replace_parameter( login_cookie_name(), cson_value_get_cstr(g.json.authToken) ); }else if( g.isHTTP ){ /* try fossil's conventional cookie. */ /* Reminder: chicken/egg scenario regarding db access in CLI mode because login_cookie_name() needs the db. CLI mode does not use any authentication, so we don't need to support it here. */ char const * zCookie = P(login_cookie_name()); if( zCookie && *zCookie ){ /* Transfer fossil's cookie to JSON for downstream convenience... */ cson_value * v = cson_value_new_string(zCookie, strlen(zCookie)); json_gc_add( FossilJsonKeys.authToken, v ); g.json.authToken = v; } } } return g.json.authToken; } /* ** IFF json.reqPayload.o is not NULL then this returns ** cson_object_get(json.reqPayload.o,pKey), else it returns NULL. ** ** The returned value is owned by (or shared with) json.reqPayload.v. */ cson_value * json_req_payload_get(char const *pKey){ return g.json.reqPayload.o ? cson_object_get(g.json.reqPayload.o,pKey) : NULL; } /* ** Initializes some JSON bits which need to be initialized relatively ** early on. It should only be called from cgi_init() or ** json_cmd_top() (early on in those functions). ** ** Initializes g.json.gc and g.json.param. This code does not (and ** must not) rely on any of the fossil environment having been set ** up. e.g. it must not use cgi_parameter() and friends because this ** must be called before those data are initialized. */ void json_main_bootstrap(){ cson_value * v; assert( (NULL == g.json.gc.v) && "cgi_json_bootstrap() was called twice!" ); /* g.json.gc is our "garbage collector" - where we put JSON values which need a long lifetime but don't have a logical parent to put them in. */ v = cson_value_new_array(); g.json.gc.v = v; g.json.gc.a = cson_value_get_array(v); cson_value_add_reference(v) /* Needed to allow us to include this value in other JSON containers without transfering ownership to those containers. All other persistent g.json.XXX.v values get appended to g.json.gc.a, and therefore already have a live reference for this purpose. */ ; /* g.json.param holds the JSONized counterpart of fossil's cgi_parameter_xxx() family of data. We store them as JSON, as opposed to using fossil's data directly, because we can retain full type information for data this way (as opposed to it always being of type string). */ v = cson_value_new_object(); g.json.param.v = v; g.json.param.o = cson_value_get_object(v); json_gc_add("$PARAMS", v); } /* ** Appends a warning object to the (pending) JSON response. ** ** Code must be a FSL_JSON_W_xxx value from the FossilJsonCodes enum. ** ** A Warning object has this JSON structure: ** ** { "code":integer, "text":"string" } ** ** But the text part is optional. ** ** If msg is non-NULL and not empty then it is used as the "text" ** property's value. It is copied, and need not refer to static ** memory. ** ** CURRENTLY this code only allows a given warning code to be ** added one time, and elides subsequent warnings. The intention ** is to remove that burden from loops which produce warnings. ** ** FIXME: if msg is NULL then use a standard string for ** the given code. If !*msg then elide the "text" property, ** for consistency with how json_err() works. */ void json_warn( int code, char const * fmt, ... ){ cson_object * obj = NULL; assert( (code>FSL_JSON_W_START) && (codeindent) ? (g.isHTTP ? 0 : 1) : (unsigned char)indent; g.json.outOpt.addNewline = g.isHTTP ? 0 : (g.json.jsonp ? 0 : 1); } if( g.isHTTP ){ json_auth_token()/* will copy our auth token, if any, to fossil's core, which we need before we call login_check_credentials(). */; login_check_credentials()/* populates g.perm */; } else{ db_find_and_open_repository(OPEN_ANY_SCHEMA,0); } } /* ** Returns the ndx'th item in the "command path", where index 0 is the ** position of the "json" part of the path. Returns NULL if ndx is out ** of bounds or there is no "json" path element. ** ** In CLI mode the "path" is the list of arguments (skipping argv[0]). ** In server/CGI modes the path is taken from PATH_INFO. ** ** The returned bytes are owned by g.json.cmd.v and _may_ be ** invalidated if that object is modified (depending on how it is ** modified). ** ** Note that CLI options are not included in the command path. Use ** find_option() to get those. ** */ char const * json_command_arg(unsigned char ndx){ cson_array * ar = g.json.cmd.a; assert((NULL!=ar) && "Internal error. Was json_mode_bootstrap() called?"); assert((g.argc>1) && "Internal error - we never should have gotten this far."); if( g.json.cmd.offset < 0 ){ /* first-time setup. */ short i = 0; #define NEXT cson_string_cstr( \ cson_value_get_string( \ cson_array_get(ar,i) \ )) char const * tok = NEXT; while( tok ){ if( !g.isHTTP/*workaround for "abbreviated name" in CLI mode*/ ? (0==strcmp(g.argv[1],tok)) : (0==strncmp("json",tok,4)) ){ g.json.cmd.offset = i; break; } ++i; tok = NEXT; } } #undef NEXT if(g.json.cmd.offset < 0){ return NULL; }else{ ndx = g.json.cmd.offset + ndx; return cson_string_cstr(cson_value_get_string(cson_array_get( ar, g.json.cmd.offset + ndx ))); } } /* ** If g.json.reqPayload.o is NULL then NULL is returned, else the ** given property is searched for in the request payload. If found it ** is returned. The returned value is owned by (or shares ownership ** with) g.json, and must NOT be cson_value_free()'d by the ** caller. */ cson_value * json_payload_property( char const * key ){ return g.json.reqPayload.o ? cson_object_get( g.json.reqPayload.o, key ) : NULL; } /* Returns the C-string form of json_auth_token(), or NULL ** if json_auth_token() returns NULL. */ char const * json_auth_token_cstr(){ return cson_value_get_cstr( json_auth_token() ); } /* ** Returns the JsonPageDef with the given name, or NULL if no match is ** found. ** ** head must be a pointer to an array of JsonPageDefs in which the ** last entry has a NULL name. */ JsonPageDef const * json_handler_for_name( char const * name, JsonPageDef const * head ){ JsonPageDef const * pageDef = head; assert( head != NULL ); if(name && *name) for( ; pageDef->name; ++pageDef ){ if( 0 == strcmp(name, pageDef->name) ){ return pageDef; } } return NULL; } /* ** Given a Fossil/JSON result code, this function "dumbs it down" ** according to the current value of g.json.errorDetailParanoia. The ** dumbed-down value is returned. ** ** This function assert()s that code is in the inclusive range 0 to ** 9999. ** ** Note that WARNING codes (1..999) are never dumbed down. ** */ static int json_dumbdown_rc( int code ){ if(!g.json.errorDetailParanoia || !code || ((code>=FSL_JSON_W_START) && (code= 1000) && (code <= 9999) && "Invalid Fossil/JSON code."); switch( g.json.errorDetailParanoia ){ case 1: modulo = 10; break; case 2: modulo = 100; break; case 3: modulo = 1000; break; default: break; } if( modulo ) code = code - (code % modulo); return code; } } /* ** Convenience routine which converts a Julian time value into a Unix ** Epoch timestamp. Requires the db, so this cannot be used before the ** repo is opened (will trigger a fatal error in db_xxx()). The returned ** value is owned by the caller. */ cson_value * json_julian_to_timestamp(double j){ return cson_value_new_integer((cson_int_t) db_int64(0,"SELECT cast(strftime('%%s',%lf) as int)",j) ); } /* ** Returns a timestamp value. */ cson_int_t json_timestamp(){ return (cson_int_t)time(0); } /* ** Returns a new JSON value (owned by the caller) representing ** a timestamp. If timeVal is < 0 then time(0) is used to fetch ** the time, else timeVal is used as-is. The returned value is ** owned by the caller. */ cson_value * json_new_timestamp(cson_int_t timeVal){ return cson_value_new_integer((timeVal<0) ? (cson_int_t)time(0) : timeVal); } /* ** Internal helper for json_create_response(). Appends the first ** g.json.dispatchDepth elements of g.json.cmd.a, skipping the first ** one (the "json" part), to a string and returns that string value ** (which is owned by the caller). */ static cson_value * json_response_command_path(){ if(!g.json.cmd.a){ return NULL; }else{ cson_value * rc = NULL; Blob path = empty_blob; char const * part; unsigned int aLen = g.json.dispatchDepth+1; /*cson_array_length_get(g.json.cmd.a);*/ unsigned int i = 1; for( ; i < aLen; ++i ){ char const * part = cson_string_cstr(cson_value_get_string(cson_array_get(g.json.cmd.a, i))); if(!part){ fossil_warning("Iterating further than expected in %s.", __FILE__); break; } blob_appendf(&path,"%s%s", (i>1 ? "/": ""), part); } rc = json_new_string((blob_size(&path)>0) ? blob_buffer(&path) : "") /* reminder; we need an empty string instead of NULL in this case, to avoid what outwardly looks like (but is not) an allocation error in json_create_response(). */ ; blob_reset(&path); return rc; } } /* ** Returns a JSON Object representation of the global g object. ** Returned value is owned by the caller. */ cson_value * json_g_to_json(){ cson_object * o = NULL; cson_object * pay = NULL; pay = o = cson_new_object(); #define INT(OBJ,K) cson_object_set(o, #K, json_new_int(OBJ.K)) #define CSTR(OBJ,K) cson_object_set(o, #K, OBJ.K ? json_new_string(OBJ.K) : cson_value_null()) #define VAL(K,V) cson_object_set(o, #K, (V) ? (V) : cson_value_null()) VAL(capabilities, json_cap_value()); INT(g, argc); INT(g, isConst); INT(g, useAttach); INT(g, configOpen); INT(g, repositoryOpen); INT(g, localOpen); INT(g, minPrefix); INT(g, fSqlTrace); INT(g, fSqlStats); INT(g, fSqlPrint); INT(g, fQuiet); INT(g, fHttpTrace); INT(g, fSystemTrace); INT(g, fNoSync); INT(g, iErrPriority); INT(g, sslNotAvailable); INT(g, cgiOutput); INT(g, xferPanic); INT(g, fullHttpReply); INT(g, xlinkClusterOnly); INT(g, fTimeFormat); INT(g, markPrivate); INT(g, clockSkewSeen); INT(g, isHTTP); INT(g, urlIsFile); INT(g, urlIsHttps); INT(g, urlIsSsh); INT(g, urlPort); INT(g, urlDfltPort); INT(g, dontKeepUrl); INT(g, useLocalauth); INT(g, noPswd); INT(g, userUid); INT(g, rcvid); INT(g, okCsrf); INT(g, thTrace); INT(g, isHome); INT(g, nAux); INT(g, allowSymlinks); CSTR(g, zMainDbType); CSTR(g, zHome); CSTR(g, zLocalRoot); CSTR(g, zPath); CSTR(g, zExtra); CSTR(g, zBaseURL); CSTR(g, zTop); CSTR(g, zContentType); CSTR(g, zErrMsg); CSTR(g, urlName); CSTR(g, urlHostname); CSTR(g, urlProtocol); CSTR(g, urlPath); CSTR(g, urlUser); CSTR(g, urlPasswd); CSTR(g, urlCanonical); CSTR(g, urlProxyAuth); CSTR(g, urlFossil); CSTR(g, zLogin); CSTR(g, zSSLIdentity); CSTR(g, zIpAddr); CSTR(g, zNonce); CSTR(g, zCsrfToken); o = cson_new_object(); cson_object_set(pay, "json", cson_object_value(o) ); INT(g.json, isJsonMode); INT(g.json, resultCode); INT(g.json, errorDetailParanoia); INT(g.json, dispatchDepth); VAL(authToken, g.json.authToken); CSTR(g.json, jsonp); VAL(gc, g.json.gc.v); VAL(cmd, g.json.cmd.v); VAL(param, g.json.param.v); VAL(POST, g.json.post.v); VAL(warnings, g.json.warnings.v); /*cson_output_opt outOpt;*/ #undef INT #undef CSTR #undef VAL return cson_object_value(pay); } /* ** Creates a new Fossil/JSON response envelope skeleton. It is owned ** by the caller, who must eventually free it using cson_value_free(), ** or add it to a cson container to transfer ownership. Returns NULL ** on error. ** ** If payload is not NULL and resultCode is 0 then it is set as the ** "payload" property of the returned object. If resultCode is 0 then ** it defaults to g.json.resultCode. If resultCode is (or defaults to) ** non-zero and payload is not NULL then this function calls ** cson_value_free(payload) and does not insert the payload into the ** response. In either case, onwership of payload is transfered to (or ** shared with, if the caller holds a reference) this function. ** ** pMsg is an optional message string property (resultText) of the ** response. If resultCode is non-0 and pMsg is NULL then ** json_err_cstr() is used to get the error string. The caller may ** provide his own or may use an empty string to suppress the ** resultText property. ** */ cson_value * json_create_response( int resultCode, char const * pMsg, cson_value * payload){ cson_value * v = NULL; cson_value * tmp = NULL; cson_object * o = NULL; int rc; resultCode = json_dumbdown_rc(resultCode ? resultCode : g.json.resultCode); o = cson_new_object(); v = cson_object_value(o); if( ! o ) return NULL; #define SET(K) if(!tmp) goto cleanup; \ rc = cson_object_set( o, K, tmp ); \ if(rc) do{\ cson_value_free(tmp); \ tmp = NULL; \ goto cleanup; \ }while(0) tmp = json_new_string(MANIFEST_UUID); SET("fossil"); tmp = json_new_timestamp(-1); SET(FossilJsonKeys.timestamp); if( 0 != resultCode ){ if( ! pMsg ){ pMsg = g.zErrMsg; if(!pMsg){ pMsg = json_err_cstr(resultCode); } } tmp = json_new_string(json_rc_cstr(resultCode)); SET(FossilJsonKeys.resultCode); } if( pMsg && *pMsg ){ tmp = json_new_string(pMsg); SET(FossilJsonKeys.resultText); } if(g.json.cmd.commandStr){ tmp = json_new_string(g.json.cmd.commandStr); }else{ tmp = json_response_command_path(); } SET("command"); tmp = json_getenv(FossilJsonKeys.requestId); if( tmp ) cson_object_set( o, FossilJsonKeys.requestId, tmp ); if(0){/* these are only intended for my own testing...*/ if(g.json.cmd.v){ tmp = g.json.cmd.v; SET("$commandPath"); } if(g.json.param.v){ tmp = g.json.param.v; SET("$params"); } if(0){/*Only for debuggering, add some info to the response.*/ tmp = cson_value_new_integer( g.json.cmd.offset ); cson_object_set( o, "cmd.offset", tmp ); cson_object_set( o, "isCGI", cson_value_new_bool( g.isHTTP ) ); } } if(HAS_TIMER){ /* This is, philosophically speaking, not quite the right place for ending the timer, but this is the one function which all of the JSON exit paths use (and they call it after processing, just before they end). */ double span; span = END_TIMER; /* i'm actually seeing sub-ms runtimes in some tests, but a time of 0 is "just wrong", so we'll bump that up to 1ms. */ cson_object_set(o,"procTimeMs", cson_value_new_integer((cson_int_t)((span>1.0)?span:1))); } if(g.json.warnings.v){ tmp = g.json.warnings.v; SET("warnings"); } /* Only add the payload to SUCCESS responses. Else delete it. */ if( NULL != payload ){ if( resultCode ){ cson_value_free(payload); payload = NULL; }else{ tmp = payload; SET(FossilJsonKeys.payload); } } if(json_find_option_bool("debugFossilG","json-debug-g",NULL,0) &&(g.perm.Admin||g.perm.Setup)){ tmp = json_g_to_json(); SET("g"); } #undef SET goto ok; cleanup: cson_value_free(v); v = NULL; ok: return v; } /* ** Outputs a JSON error response to either the cgi_xxx() family of ** buffers (in CGI/server mode) or stdout (in CLI mode). If rc is 0 ** then g.json.resultCode is used. If that is also 0 then the "Unknown ** Error" code is used. ** ** If g.isHTTP then the generated JSON error response object replaces ** any currently buffered page output. Because the output goes via ** the cgi_xxx() family of functions, this function inherits any ** compression which fossil does for its output. ** ** If alsoOutput is true AND g.isHTTP then cgi_reply() is called to ** flush the output (and headers). Generally only do this if you are ** about to call exit(). ** ** If !g.isHTTP then alsoOutput is ignored and all output is sent to ** stdout immediately. ** ** For generating the resultText property: if msg is not NULL then it ** is used as-is. If it is NULL then g.zErrMsg is checked, and if that ** is NULL then json_err_cstr(code) is used. */ void json_err( int code, char const * msg, char alsoOutput ){ int rc = code ? code : (g.json.resultCode ? g.json.resultCode : FSL_JSON_E_UNKNOWN); cson_value * resp = NULL; rc = json_dumbdown_rc(rc); if( rc && !msg ){ msg = g.zErrMsg; if(!msg){ msg = json_err_cstr(rc); } } resp = json_create_response(rc, msg, NULL); if(!resp){ /* about the only error case here is out-of-memory. DO NOT call fossil_panic() here because that calls this function. */ fprintf(stderr, "%s: Fatal error: could not allocate " "response object.\n", fossil_nameofexe()); fossil_exit(1); } if( g.isHTTP ){ if(alsoOutput){ json_send_response(resp); }else{ /* almost a duplicate of json_send_response() :( */ cgi_reset_content(); if( g.json.jsonp ){ cgi_printf("%s(",g.json.jsonp); } cson_output( resp, cson_data_dest_cgi, NULL, &g.json.outOpt ); if( g.json.jsonp ){ cgi_append_content(")",1); } } }else{ json_send_response(resp); } cson_value_free(resp); } /* ** Sets g.json.resultCode and g.zErrMsg, but does not report the error ** via json_err(). Returns the code passed to it. ** ** code must be in the inclusive range 1000..9999. */ int json_set_err( int code, char const * fmt, ... ){ assert( (code>=1000) && (code<=9999) ); free(g.zErrMsg); g.json.resultCode = code; if(!fmt || !*fmt){ g.zErrMsg = mprintf("%s", json_err_cstr(code)); }else{ va_list vargs; va_start(vargs,fmt); char * msg = vmprintf(fmt, vargs); va_end(vargs); g.zErrMsg = msg; } return code; } /* ** Iterates through a prepared SELECT statement and converts each row ** to a JSON object. If pTgt is not NULL then this function will ** append the results to pTgt and return cson_array_value(pTgt). If ** pTgt is NULL then a new Array object is created and returned (owned ** by the caller). Each row of pStmt is converted to an Object and ** appended to the array. If the result set has no rows AND pTgt is ** NULL then NULL (not an empty array) is returned. */ cson_value * json_stmt_to_array_of_obj(Stmt *pStmt, cson_array * pTgt){ cson_array * a = pTgt; char const * warnMsg = NULL; cson_value * colNamesV = NULL; cson_array * colNames = NULL; while( (SQLITE_ROW==db_step(pStmt)) ){ cson_value * row = NULL; if(!a){ a = cson_new_array(); assert(NULL!=a); } if(!colNames){ colNamesV = cson_sqlite3_column_names(pStmt->pStmt); assert(NULL != colNamesV); cson_value_add_reference(colNamesV)/*avoids an ownership problem*/; colNames = cson_value_get_array(colNamesV); assert(NULL != colNames); } row = cson_sqlite3_row_to_object2(pStmt->pStmt, colNames); if(!row && !warnMsg){ warnMsg = "Could not convert at least one result row to JSON."; continue; } if( 0 != cson_array_append(a, row) ){ cson_value_free(row); assert( 0 && "Alloc error."); if(pTgt != a) { cson_free_array(a); } return NULL; } } cson_value_free(colNamesV); if(warnMsg){ json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, warnMsg ); } return cson_array_value(a); } /* ** Works just like json_stmt_to_array_of_obj(), but each row in the ** result set is represented as an Array of values instead of an ** Object (key/value pairs). If pTgt is NULL and the statement ** has no results then NULL is returned, not an empty array. */ cson_value * json_stmt_to_array_of_array(Stmt *pStmt, cson_array * pTgt){ cson_array * a = pTgt; while( (SQLITE_ROW==db_step(pStmt)) ){ cson_value * row = NULL; if(!a){ a = cson_new_array(); assert(NULL!=a); } row = cson_sqlite3_row_to_array(pStmt->pStmt); cson_array_append(a, row); } return cson_array_value(a); } /* ** Executes the given SQL and runs it through ** json_stmt_to_array_of_obj(), returning the result of that ** function. If resetBlob is true then blob_reset(pSql) is called ** after preparing the query. ** ** pTgt has the same semantics as described for ** json_stmt_to_array_of_obj(). */ cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt, char resetBlob){ Stmt q = empty_Stmt; cson_value * pay = NULL; assert( blob_size(pSql) > 0 ); db_prepare(&q, "%s", blob_str(pSql)); if(resetBlob){ blob_reset(pSql); } pay = json_stmt_to_array_of_obj(&q, pTgt); db_finalize(&q); return pay; } /* ** If the given COMMIT rid has any tags associated with it, this ** function returns a JSON Array containing the tag names (owned by ** the caller), else it returns NULL. ** ** See info_tags_of_checkin() for more details (this is simply a JSON ** wrapper for that function). */ cson_value * json_tags_for_checkin_rid(int rid, char propagatingOnly){ cson_value * v = NULL; char * tags = info_tags_of_checkin(rid, propagatingOnly); if(tags){ if(*tags){ v = json_string_split2(tags,',',0); } free(tags); } return v; } /* ** Returns a "new" value representing the boolean value of zVal ** (false if zVal is NULL). Note that cson does not really allocate ** any memory for boolean values, but they "should" (for reasons of ** style and philosophy) be cleaned up like any other values (but ** it's a no-op for bools). */ cson_value * json_value_to_bool(cson_value const * zVal){ return cson_value_get_bool(zVal) ? cson_value_true() : cson_value_false(); } /* ** Impl of /json/resultCodes ** */ cson_value * json_page_resultCodes(){ cson_value * listV = cson_value_new_array(); cson_array * list = cson_value_get_array(listV); cson_value * objV = NULL; cson_object * obj = NULL; cson_string * kRC; cson_string * kSymbol; cson_string * kNumber; cson_string * kDesc; int rc = cson_array_reserve( list, 35 ); if(rc){ assert( 0 && "Allocation error."); exit(1); } kRC = cson_new_string("resultCode",10); kSymbol = cson_new_string("cSymbol",7); kNumber = cson_new_string("number",6); kDesc = cson_new_string("description",11); #define C(K) objV = cson_value_new_object(); obj = cson_value_get_object(objV); \ cson_object_set_s(obj, kRC, json_new_string(json_rc_cstr(FSL_JSON_E_##K)) ); \ cson_object_set_s(obj, kSymbol, json_new_string("FSL_JSON_E_"#K) ); \ cson_object_set_s(obj, kNumber, cson_value_new_integer(FSL_JSON_E_##K) ); \ cson_object_set_s(obj, kDesc, json_new_string(json_err_cstr(FSL_JSON_E_##K))); \ cson_array_append( list, objV ); obj = NULL; objV = NULL C(GENERIC); C(INVALID_REQUEST); C(UNKNOWN_COMMAND); C(UNKNOWN); C(TIMEOUT); C(ASSERT); C(ALLOC); C(NYI); C(PANIC); C(MANIFEST_READ_FAILED); C(FILE_OPEN_FAILED); C(AUTH); C(MISSING_AUTH); C(DENIED); C(WRONG_MODE); C(LOGIN_FAILED); C(LOGIN_FAILED_NOSEED); C(LOGIN_FAILED_NONAME); C(LOGIN_FAILED_NOPW); C(LOGIN_FAILED_NOTFOUND); C(USAGE); C(INVALID_ARGS); C(MISSING_ARGS); C(AMBIGUOUS_UUID); C(UNRESOLVED_UUID); C(RESOURCE_ALREADY_EXISTS); C(RESOURCE_NOT_FOUND); C(DB); C(STMT_PREP); C(STMT_BIND); C(STMT_EXEC); C(DB_LOCKED); C(DB_NEEDS_REBUILD); C(DB_NOT_FOUND); C(DB_NOT_VALID); #undef C return listV; } /* ** /json/version implementation. ** ** Returns the payload object (owned by the caller). */ cson_value * json_page_version(){ cson_value * jval = NULL; cson_object * jobj = NULL; jval = cson_value_new_object(); jobj = cson_value_get_object(jval); #define FSET(X,K) cson_object_set( jobj, K, cson_value_new_string(X,strlen(X))) FSET(MANIFEST_UUID,"manifestUuid"); FSET(MANIFEST_VERSION,"manifestVersion"); FSET(MANIFEST_DATE,"manifestDate"); FSET(MANIFEST_YEAR,"manifestYear"); FSET(RELEASE_VERSION,"releaseVersion"); #undef FSET cson_object_set( jobj, "releaseVersionNumber", cson_value_new_integer(RELEASE_VERSION_NUMBER) ); cson_object_set( jobj, "resultCodeParanoiaLevel", cson_value_new_integer(g.json.errorDetailParanoia) ); return jval; } /* ** Returns the current user's capabilities string as a String value. ** Returned value is owned by the caller, and will only be NULL if ** g.userUid is invalid or an out of memory error. Or, it turns out, ** in CLI mode (where there is no logged-in user). */ cson_value * json_cap_value(){ if(g.userUid<=0){ return NULL; }else{ Stmt q = empty_Stmt; cson_value * val = NULL; db_prepare(&q, "SELECT cap FROM user WHERE uid=%d", g.userUid); if( db_step(&q)==SQLITE_ROW ){ char const * str = (char const *)sqlite3_column_text(q.pStmt,0); if( str ){ val = json_new_string(str); } } db_finalize(&q); return val; } } /* ** Implementation for /json/cap ** ** Returned object contains details about the "capabilities" of the ** current user (what he may/may not do). ** ** This is primarily intended for debuggering, but may have ** a use in client code. (?) */ cson_value * json_page_cap(){ cson_value * payload = cson_value_new_object(); cson_value * sub = cson_value_new_object(); Stmt q; cson_object * obj = cson_value_get_object(payload); db_prepare(&q, "SELECT login, cap FROM user WHERE uid=%d", g.userUid); if( db_step(&q)==SQLITE_ROW ){ /* reminder: we don't use g.zLogin because it's 0 for the guest user and the HTML UI appears to currently allow the name to be changed (but doing so would break other code). */ char const * str = (char const *)sqlite3_column_text(q.pStmt,0); if( str ){ cson_object_set( obj, "name", cson_value_new_string(str,strlen(str)) ); } str = (char const *)sqlite3_column_text(q.pStmt,1); if( str ){ cson_object_set( obj, "capabilities", cson_value_new_string(str,strlen(str)) ); } } db_finalize(&q); cson_object_set( obj, "permissionFlags", sub ); obj = cson_value_get_object(sub); #define ADD(X,K) cson_object_set(obj, K, cson_value_new_bool(g.perm.X)) ADD(Setup,"setup"); ADD(Admin,"admin"); ADD(Delete,"delete"); ADD(Password,"password"); ADD(Query,"query"); /* don't think this one is actually used */ ADD(Write,"checkin"); ADD(Read,"checkout"); ADD(History,"history"); ADD(Clone,"clone"); ADD(RdWiki,"readWiki"); ADD(NewWiki,"createWiki"); ADD(ApndWiki,"appendWiki"); ADD(WrWiki,"editWiki"); ADD(RdTkt,"readTicket"); ADD(NewTkt,"createTicket"); ADD(ApndTkt,"appendTicket"); ADD(WrTkt,"editTicket"); ADD(Attach,"attachFile"); ADD(TktFmt,"createTicketReport"); ADD(RdAddr,"readPrivate"); ADD(Zip,"zip"); ADD(Private,"xferPrivate"); #undef ADD return payload; } /* ** Implementation of the /json/stat page/command. ** */ cson_value * json_page_stat(){ i64 t, fsize; int n, m; int full; const char *zDb; enum { BufLen = 1000 }; char zBuf[BufLen]; cson_value * jv = NULL; cson_object * jo = NULL; cson_value * jv2 = NULL; cson_object * jo2 = NULL; char * zTmp = NULL; if( !g.perm.Read ){ json_set_err(FSL_JSON_E_DENIED, "Requires 'o' permissions."); return NULL; } full = json_find_option_bool("full",NULL,"f",0); #define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf))); jv = cson_value_new_object(); jo = cson_value_get_object(jv); zTmp = db_get("project-name",NULL); cson_object_set(jo, "projectName", json_new_string(zTmp)); free(zTmp); zTmp = db_get("project-description",NULL); cson_object_set(jo, "projectDescription", json_new_string(zTmp)); free(zTmp); zTmp = NULL; fsize = file_size(g.zRepositoryName); cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize)); if(full){ n = db_int(0, "SELECT count(*) FROM blob"); m = db_int(0, "SELECT count(*) FROM delta"); cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n)); cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m)); if( n>0 ){ int a, b; Stmt q; db_prepare(&q, "SELECT total(size), avg(size), max(size)" " FROM blob WHERE size>0"); db_step(&q); t = db_column_int64(&q, 0); cson_object_set(jo, "uncompressedArtifactSize", cson_value_new_integer((cson_int_t)t)); cson_object_set(jo, "averageArtifactSize", cson_value_new_integer((cson_int_t)db_column_int(&q, 1))); cson_object_set(jo, "maxArtifactSize", cson_value_new_integer((cson_int_t)db_column_int(&q, 2))); db_finalize(&q); if( t/fsize < 5 ){ b = 10; fsize /= 10; }else{ b = 1; } a = t/fsize; sqlite3_snprintf(BufLen,zBuf, "%d:%d", a, b); SETBUF(jo, "compressionRatio"); } n = db_int(0, "SELECT count(distinct mid) FROM mlink /*scan*/"); cson_object_set(jo, "checkinCount", cson_value_new_integer((cson_int_t)n)); n = db_int(0, "SELECT count(*) FROM filename /*scan*/"); cson_object_set(jo, "fileCount", cson_value_new_integer((cson_int_t)n)); n = db_int(0, "SELECT count(*) FROM tag /*scan*/" " WHERE +tagname GLOB 'wiki-*'"); cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n)); n = db_int(0, "SELECT count(*) FROM tag /*scan*/" " WHERE +tagname GLOB 'tkt-*'"); cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n)); }/*full*/ n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)" " + 0.99"); cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n)); cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24)); sqlite3_snprintf(BufLen, zBuf, db_get("project-code","")); SETBUF(jo, "projectCode"); sqlite3_snprintf(BufLen, zBuf, db_get("server-code","")); SETBUF(jo, "serverCode"); cson_object_set(jo, "compiler", cson_value_new_string(COMPILER_NAME, strlen(COMPILER_NAME))); jv2 = cson_value_new_object(); jo2 = cson_value_get_object(jv2); cson_object_set(jo, "sqlite", jv2); sqlite3_snprintf(BufLen, zBuf, "%.19s [%.10s] (%s)", SQLITE_SOURCE_ID, &SQLITE_SOURCE_ID[20], SQLITE_VERSION); SETBUF(jo2, "version"); zDb = db_name("repository"); cson_object_set(jo2, "pageCount", cson_value_new_integer((cson_int_t)db_int(0, "PRAGMA %s.page_count", zDb))); cson_object_set(jo2, "pageSize", cson_value_new_integer((cson_int_t)db_int(0, "PRAGMA %s.page_size", zDb))); cson_object_set(jo2, "freeList", cson_value_new_integer((cson_int_t)db_int(0, "PRAGMA %s.freelist_count", zDb))); sqlite3_snprintf(BufLen, zBuf, "%s", db_text(0, "PRAGMA %s.encoding", zDb)); SETBUF(jo2, "encoding"); sqlite3_snprintf(BufLen, zBuf, "%s", db_text(0, "PRAGMA %s.journal_mode", zDb)); cson_object_set(jo2, "journalMode", *zBuf ? cson_value_new_string(zBuf, strlen(zBuf)) : cson_value_null()); return jv; #undef SETBUF } /* ** Creates a comma-separated list of command names ** taken from zPages. zPages must be an array of objects ** whose final entry MUST have a NULL name value or results ** are undefined. ** ** The list is appended to pOut. The number of items (not bytes) ** appended are returned. */ static int json_pagedefs_to_string(JsonPageDef const * zPages, Blob * pOut){ int i = 0; for( ; zPages->name; ++zPages, ++i ){ if(g.isHTTP && zPages->runMode < 0) continue; else if(zPages->runMode > 0) continue; blob_appendf(pOut, zPages->name, -1); if((zPages+1)->name){ blob_append(pOut, ", ",2); } } return i; } cson_value * json_page_dispatch_helper(JsonPageDef const * pages){ JsonPageDef const * def; char const * cmd = json_command_arg(1+g.json.dispatchDepth); assert( NULL != pages ); if( ! cmd ){ Blob cmdNames = empty_blob; json_pagedefs_to_string(pages, &cmdNames); json_set_err(FSL_JSON_E_MISSING_ARGS, "No subcommand specified. Try one of (%s).", blob_str(&cmdNames)); blob_reset(&cmdNames); return NULL; } def = json_handler_for_name( cmd, pages ); if(!def){ json_set_err(FSL_JSON_E_UNKNOWN_COMMAND, "Unknown subcommand: %s", cmd); return NULL; } else{ ++g.json.dispatchDepth; return (*def->func)(); } } /* ** Impl of /json/rebuild. Requires admin previleges. */ static cson_value * json_page_rebuild(){ if( !g.perm.Admin ){ json_set_err(FSL_JSON_E_DENIED,"Requires 'a' privileges."); return NULL; }else{ /* Reminder: the db_xxx() ops "should" fail via the fossil core error handlers, which will cause a JSON error and exit(). i.e. we don't handle the errors here. TODO: confirm that all these db routine fail gracefully in JSON mode. On large repos (e.g. fossil's) this operation is likely to take longer than the client timeout, which will cause it to fail (but it's sqlite3, so it'll fail gracefully). */ db_close(1); db_open_repository(g.zRepositoryName); db_begin_transaction(); rebuild_db(0, 0, 0); db_end_transaction(0); return NULL; } } /* ** Impl of /json/g. Requires admin/setup rights. */ static cson_value * json_page_g(){ if(!g.perm.Admin || !g.perm.Setup){ json_set_err(FSL_JSON_E_DENIED, "Requires 'a' or 's' privileges."); return NULL; } return json_g_to_json(); } /* Impl in json_login.c. */ cson_value * json_page_anon_password(); /* Impl in json_artifact.c. */ cson_value * json_page_artifact(); /* Impl in json_branch.c. */ cson_value * json_page_branch(); /* Impl in json_diff.c. */ cson_value * json_page_diff(); /* Impl in json_login.c. */ cson_value * json_page_login(); /* Impl in json_login.c. */ cson_value * json_page_logout(); /* Impl in json_query.c. */ cson_value * json_page_query(); /* Impl in json_report.c. */ cson_value * json_page_report(); /* Impl in json_tag.c. */ cson_value * json_page_tag(); /* Impl in json_user.c. */ cson_value * json_page_user(); /* Impl in json_config.c. */ cson_value * json_page_config(); /* ** Mapping of names to JSON pages/commands. Each name is a subpath of ** /json (in CGI mode) or a subcommand of the json command in CLI mode */ static const JsonPageDef JsonPageDefs[] = { /* please keep alphabetically sorted (case-insensitive) for maintenance reasons. */ {"anonymousPassword", json_page_anon_password, 0}, {"artifact", json_page_artifact, 0}, {"branch", json_page_branch,0}, {"cap", json_page_cap, 0}, {"config", json_page_config, 0 }, {"diff", json_page_diff, 0}, {"dir", json_page_nyi, 0}, {"g", json_page_g, 0}, {"HAI",json_page_version,0}, {"login",json_page_login,0}, {"logout",json_page_logout,0}, {"query",json_page_query,0}, {"rebuild",json_page_rebuild,0}, {"report", json_page_report, 0}, {"resultCodes", json_page_resultCodes,0}, {"stat",json_page_stat,0}, {"tag", json_page_tag,0}, {"ticket", json_page_nyi,0}, {"timeline", json_page_timeline,0}, {"user",json_page_user,0}, {"version",json_page_version,0}, {"whoami",json_page_whoami,0}, {"wiki",json_page_wiki,0}, /* Last entry MUST have a NULL name. */ {NULL,NULL,0} }; /* ** Mapping of /json/ticket/XXX commands/paths to callbacks. */ static const JsonPageDef JsonPageDefs_Ticket[] = { {"get", json_page_nyi, 0}, {"list", json_page_nyi, 0}, {"save", json_page_nyi, 1}, {"create", json_page_nyi, 1}, /* Last entry MUST have a NULL name. */ {NULL,NULL,0} }; /* ** Mapping of /json/artifact/XXX commands/paths to callbacks. */ static const JsonPageDef JsonPageDefs_Artifact[] = { {"vinfo", json_page_nyi, 0}, {"finfo", json_page_nyi, 0}, /* Last entry MUST have a NULL name. */ {NULL,NULL,0} }; /* ** Mapping of /json/tag/XXX commands/paths to callbacks. */ static const JsonPageDef JsonPageDefs_Tag[] = { {"list", json_page_nyi, 0}, {"create", json_page_nyi, 1}, /* Last entry MUST have a NULL name. */ {NULL,NULL,0} }; /* ** Internal helper for json_cmd_top() and json_page_top(). ** ** Searches JsonPageDefs for a command with the given name. If found, ** it is used to generate and output a JSON response. If not found, it ** generates a JSON-style error response. Returns 0 on success, non-0 ** on error. On error it will set g.json's error state. */ static int json_dispatch_root_command( char const * zCommand ){ int rc = 0; cson_value * payload = NULL; JsonPageDef const * pageDef = NULL; pageDef = json_handler_for_name(zCommand,&JsonPageDefs[0]); if( ! pageDef ){ rc = FSL_JSON_E_UNKNOWN_COMMAND; json_set_err( rc, "Unknown command: %s", zCommand ); }else if( pageDef->runMode < 0 /*CLI only*/){ rc = FSL_JSON_E_WRONG_MODE; }else if( (g.isHTTP && (pageDef->runMode < 0 /*CLI only*/)) || (!g.isHTTP && (pageDef->runMode > 0 /*HTTP only*/)) ){ rc = FSL_JSON_E_WRONG_MODE; } else{ rc = 0; g.json.dispatchDepth = 1; payload = (*pageDef->func)(); } payload = json_create_response(rc, rc ? g.zErrMsg : NULL, payload); json_send_response(payload); cson_value_free(payload); return rc; } #ifdef FOSSIL_ENABLE_JSON /* dupe ifdef needed for mkindex */ /* ** WEBPAGE: json ** ** Pages under /json/... must be entered into JsonPageDefs. ** This function dispatches them, and is the HTTP equivalent of ** json_cmd_top(). */ void json_page_top(void){ char const * zCommand; BEGIN_TIMER; json_mode_bootstrap(); zCommand = json_command_arg(1); if(!zCommand || !*zCommand){ goto usage; } json_dispatch_root_command( zCommand ); return; usage: { Blob cmdNames = empty_blob; blob_init(&cmdNames, "No command (sub-path) specified. Try one of: ", -1); json_pagedefs_to_string(&JsonPageDefs[0], &cmdNames); json_err(FSL_JSON_E_MISSING_ARGS, blob_str(&cmdNames), 0); blob_reset(&cmdNames); } } #endif /* FOSSIL_ENABLE_JSON for mkindex */ #ifdef FOSSIL_ENABLE_JSON /* dupe ifdef needed for mkindex */ /* ** This function dispatches json commands and is the CLI equivalent of ** json_page_top(). ** ** COMMAND: json ** ** Usage: %fossil json SUBCOMMAND ** ** The commands include: ** ** branch ** cap ** stat ** timeline ** version (alias: HAI) ** wiki ** ** ** TODOs: ** ** tag ** ticket ** ... ** */ void json_cmd_top(void){ char const * cmd = NULL; int rc = 0; BEGIN_TIMER; memset( &g.perm, 0xff, sizeof(g.perm) ) /* In CLI mode fossil does not use permissions and they all default to false. We enable them here because (A) fossil doesn't use them in local mode but (B) having them set gives us one less difference in the CLI/CGI/Server-mode JSON handling. */ ; json_main_bootstrap(); json_mode_bootstrap(); if( 2 > cson_array_length_get(g.json.cmd.a) ){ goto usage; } #if 0 json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing."); json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing again."); #endif cmd = json_command_arg(1); if( !cmd || !*cmd ){ goto usage; } rc = json_dispatch_root_command( cmd ); if(0 != rc){ /* FIXME: we need a way of passing this error back up to the routine which called this callback. e.g. add g.errCode. */ fossil_exit(1); } return; usage: { Blob cmdNames = empty_blob; blob_init(&cmdNames, "No subcommand specified. Try one of: ", -1); json_pagedefs_to_string(&JsonPageDefs[0], &cmdNames); json_err(FSL_JSON_E_MISSING_ARGS, blob_str(&cmdNames), 1); blob_reset(&cmdNames); fossil_exit(1); } } #endif /* FOSSIL_ENABLE_JSON for mkindex */ #endif /* FOSSIL_ENABLE_JSON */