Fossil

Check-in [73591cc746]
Login

Check-in [73591cc746]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:fixed an inconsistency in the CLI/CGI args/path handling. Non-CGI server mode is still broken b/c we do not yet have the PATH_INFO (or equivalent) data.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 73591cc746f12189145481aa653a3a20b7d2bb7d
User & Date: stephan 2011-09-16 16:38:48.689
Context
2011-09-16
17:26
Consolidated server/cgi/cli path/arg handling (will break when add --options to CLI mode). ... (check-in: c5fbcced80 user: stephan tags: json)
16:38
fixed an inconsistency in the CLI/CGI args/path handling. Non-CGI server mode is still broken b/c we do not yet have the PATH_INFO (or equivalent) data. ... (check-in: 73591cc746 user: stephan tags: json)
13:39
more cson portability fixes, thanks to Robert Engelhardt and Joe Mistachkin. ... (check-in: afd36e987c user: stephan tags: json)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/json.c.
254
255
256
257
258
259
260

261
262
263
264



265
266
267
268
269
270
271

272
273
274
275
276
277
278
279



































280
281
282
283
284
285
286
** but does not perform any authentication here. It _might_
** (haven't tested this) die with an error if an auth cookie
** is malformed.
*/
static void json_mode_bootstrap(){
  g.json.isJsonMode = 1;
  g.json.resultCode = 0;

#if defined(NDEBUG)
  /* avoids debug messages on stderr in JSON mode */
  sqlite3_config(SQLITE_CONFIG_LOG, NULL, 0);
#endif



  g.json.reqPayload.v = cson_cgi_getenv( &g.json.cgiCx, "p", "payload" );
  if( g.json.reqPayload.v ){
    g.json.reqPayload.o = cson_value_get_object( g.json.reqPayload.v );
    if( ! g.json.reqPayload.o ){/* POST data is an array, which we can't use. */
      assert( cson_value_is_array( g.json.reqPayload.v ) );
      g.json.reqPayload.v = NULL;
    }

  }
  json_auth_token()/* will copy our auth token, if any, to fossil's core. */;
  if( g.isCGI ){
    login_check_credentials();
  }
  else{
    db_find_and_open_repository(OPEN_ANY_SCHEMA,0);
  }



































}

/*
** 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.cgiCx, and must NOT be cson_value_free()'d by the







>




>
>
>


|
<
|
|
<
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

272
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
** but does not perform any authentication here. It _might_
** (haven't tested this) die with an error if an auth cookie
** is malformed.
*/
static void json_mode_bootstrap(){
  g.json.isJsonMode = 1;
  g.json.resultCode = 0;
  g.json.cmdOffset = -1;
#if defined(NDEBUG)
  /* avoids debug messages on stderr in JSON mode */
  sqlite3_config(SQLITE_CONFIG_LOG, NULL, 0);
#endif
  /* g.json.reqPayload exists only to simplify some of our access to
     the request payload. We only use this in the context of Object
     payloads, not Arrays, strings, etc. */
  g.json.reqPayload.v = cson_cgi_getenv( &g.json.cgiCx, "p", "payload" );
  if( g.json.reqPayload.v ){
    g.json.reqPayload.o = cson_value_get_object( g.json.reqPayload.v )

        /* g.json.reqPayload.o may legally be NULL, which means only that
           g.json.reqPayload.v is-not-a Object.

        */;
  }
  json_auth_token()/* will copy our auth token, if any, to fossil's core. */;
  if( g.isCGI ){
    login_check_credentials();
  }
  else{
    db_find_and_open_repository(OPEN_ANY_SCHEMA,0);
  }

}

/*
** Returns the ndx'th item in the PATH_INFO 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.
*/
static char const * json_path_part(unsigned char ndx){
  if( g.json.cmdOffset < 0 ){
    /* first-time setup. */
    short i = g.isCGI ? 0 : 1;
#define NEXT (g.isCGI                               \
              ? cson_cgi_path_part_cstr( &g.json.cgiCx, i )     \
              : ((g.argc > i) ? g.argv[i] : NULL))
    char const * tok = NEXT;
    while( tok ){
      if( 0==strncmp("json",tok,4) ){
        g.json.cmdOffset = i;
        break;
      }
      ++i;
      tok = NEXT;
#undef NEXT
    }
  }
  if( g.json.cmdOffset < 0 ){
    return NULL;
  }else{
    ndx = g.json.cmdOffset + ndx;
    return g.isCGI
      ? cson_cgi_path_part_cstr( &g.json.cgiCx, g.json.cmdOffset + ndx )
      : ((ndx < g.argc) ? g.argv[ndx] : NULL)
      ;
  }
}

/*
** 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.cgiCx, and must NOT be cson_value_free()'d by the
437
438
439
440
441
442
443









444
445
446
447
448
449
450
      payload = NULL;
    }else{
      tmp = payload;
      SET("payload");
    }
  }
#undef SET









  goto ok;
  cleanup:
  cson_value_free(v);
  v = NULL;
  ok:
  return v;
}







>
>
>
>
>
>
>
>
>







475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
      payload = NULL;
    }else{
      tmp = payload;
      SET("payload");
    }
  }
#undef SET
  if(0){/*only for my own debuggering*/
    tmp = cson_cgi_env_get_val(&g.json.cgiCx,'e', 0);
    if(tmp){
      cson_object_set( o, "$ENV", tmp );
    }
    tmp = cson_value_new_integer( g.json.cmdOffset );
    cson_object_set( o, "cmdOffset", tmp );
  }

  goto ok;
  cleanup:
  cson_value_free(v);
  v = NULL;
  ok:
  return v;
}
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762

763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
{"version",json_page_version,0},
{"wiki",json_page_wiki,0},
/* Last entry MUST have a NULL name. */
{NULL,NULL}
};

/*
** WEBPAGE: /json
**
** Pages under /json/... must be entered into JsonPageDefs.
*/
void json_page_top(void){
  int rc = FSL_JSON_E_UNKNOWN_COMMAND;
  Blob buf = empty_blob;
  char const * cmd;
  cson_value * payload = NULL;
  cson_value * root = NULL;
  JsonPageDef const * pageDef = NULL;

  json_mode_bootstrap();
  cmd = cson_cgi_path_part_cstr(&g.json.cgiCx,1);
  pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]);
  cgi_set_content_type( cson_cgi_guess_content_type(&g.json.cgiCx) );
  if( ! pageDef ){
    json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 0 );
    return;
  }else if( pageDef->runMode < 0 /*CLI only*/) {
    rc = FSL_JSON_E_WRONG_MODE;
  }else{
    rc = 0;
    payload = (*pageDef->func)();
  }
  /* FIXME: we need a way of channeling resultCode values back here. */
  if( g.json.resultCode ){
    json_err(g.json.resultCode, NULL, 0);
  }else{
    blob_zero(&buf);
    root = json_response_skeleton(rc, payload, NULL);
    cson_output_Blob( root, &buf, NULL );
    cson_value_free(root);







|










>

|

<

|







<







792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813

814
815
816
817
818
819
820
821
822

823
824
825
826
827
828
829
{"version",json_page_version,0},
{"wiki",json_page_wiki,0},
/* Last entry MUST have a NULL name. */
{NULL,NULL}
};

/*
** WEBPAGE: json
**
** Pages under /json/... must be entered into JsonPageDefs.
*/
void json_page_top(void){
  int rc = FSL_JSON_E_UNKNOWN_COMMAND;
  Blob buf = empty_blob;
  char const * cmd;
  cson_value * payload = NULL;
  cson_value * root = NULL;
  JsonPageDef const * pageDef = NULL;
  cgi_set_content_type( cson_cgi_guess_content_type(&g.json.cgiCx) );
  json_mode_bootstrap();
  cmd = json_path_part(1);
  pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]);

  if( ! pageDef ){
    json_err( FSL_JSON_E_UNKNOWN_COMMAND, cmd, 0 );
    return;
  }else if( pageDef->runMode < 0 /*CLI only*/) {
    rc = FSL_JSON_E_WRONG_MODE;
  }else{
    rc = 0;
    payload = (*pageDef->func)();
  }

  if( g.json.resultCode ){
    json_err(g.json.resultCode, NULL, 0);
  }else{
    blob_zero(&buf);
    root = json_response_skeleton(rc, payload, NULL);
    cson_output_Blob( root, &buf, NULL );
    cson_value_free(root);
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
  cson_value * payload = NULL;
  JsonPageDef const * pageDef;
  json_mode_bootstrap();
  db_find_and_open_repository(0, 0);
  if( g.argc<3 ){
    goto usage;
  }
  cmd = g.argv[2];
  n = cmd ? strlen(cmd) : 0;
  if( n==0 ){
    goto usage;
  }
  cgi_set_content_type( cson_cgi_guess_content_type(&g.json.cgiCx) );
  pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]);
  if( ! pageDef ){
    json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 0 );
    return;
  }else if( pageDef->runMode > 0 /*HTTP only*/) {
    rc = FSL_JSON_E_WRONG_MODE;
  }else{
    rc = 0;
    payload = (pageDef->func)();
  }







|







|







857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
  cson_value * payload = NULL;
  JsonPageDef const * pageDef;
  json_mode_bootstrap();
  db_find_and_open_repository(0, 0);
  if( g.argc<3 ){
    goto usage;
  }
  cmd = json_path_part(1);
  n = cmd ? strlen(cmd) : 0;
  if( n==0 ){
    goto usage;
  }
  cgi_set_content_type( cson_cgi_guess_content_type(&g.json.cgiCx) );
  pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]);
  if( ! pageDef ){
    json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 1 );
    return;
  }else if( pageDef->runMode > 0 /*HTTP only*/) {
    rc = FSL_JSON_E_WRONG_MODE;
  }else{
    rc = 0;
    payload = (pageDef->func)();
  }
Changes to src/main.c.
171
172
173
174
175
176
177






178
179
180
181
182
183
184
  int allowSymlinks;             /* Cached "allow-symlinks" option */

  struct FossilJsonBits {
    int isJsonMode;            /* True if running in JSON mode, else false. This changes
                                  how errors are reported. In JSON mode we try to always
                                  output JSON-form error responses.
                               */






    int resultCode;            /* used for passing back specific codes from /json callbacks. */
    int errorDetailParanoia;   /* 0=full error codes, 1=%10, 2=%100, 3=%1000 */
    cson_cgi_cx cgiCx;         /* cson_cgi context */
    cson_output_opt outOpt;    /* formatting options for JSON mode. */
    cson_value * authToken;    /* authentication token */
    struct {
      cson_value * v;







>
>
>
>
>
>







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  int allowSymlinks;             /* Cached "allow-symlinks" option */

  struct FossilJsonBits {
    int isJsonMode;            /* True if running in JSON mode, else false. This changes
                                  how errors are reported. In JSON mode we try to always
                                  output JSON-form error responses.
                               */
    int cmdOffset;             /* Tells us which PATH_INFO/CLI args
                                  part holds the "json" command, so
                                  that we can account for sub-repos
                                  and path prefixes.  This is handled
                                  differently for CLI and CGI modes.
                               */
    int resultCode;            /* used for passing back specific codes from /json callbacks. */
    int errorDetailParanoia;   /* 0=full error codes, 1=%10, 2=%100, 3=%1000 */
    cson_cgi_cx cgiCx;         /* cson_cgi context */
    cson_output_opt outOpt;    /* formatting options for JSON mode. */
    cson_value * authToken;    /* authentication token */
    struct {
      cson_value * v;