Fossil

Check-in [0bb82303]
Login

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

Overview
Comment:Refactored the special-case CLI-mode arg/path handling in the CGI/server-mode mechanism. Fixed: server-mode response timestamp was always the time the server process started.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 0bb823032fd4c1934f37d9f433b155c75a78ee34
User & Date: stephan 2011-09-16 18:39:04
Context
2011-09-16
18:55
style conformance touch-ups. check-in: 45cd04f8 user: stephan tags: json
18:39
Refactored the special-case CLI-mode arg/path handling in the CGI/server-mode mechanism. Fixed: server-mode response timestamp was always the time the server process started. check-in: 0bb82303 user: stephan tags: json
18:29
more cleanups in the json arg/path handling. check-in: 35b9edba user: stephan tags: json
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

279
280
281
282
283
284
285
286

287
288




289
290
291
292
293
294
295
...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
...
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
...
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
...
568
569
570
571
572
573
574
575

576
577
578
579
580
581
582
...
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
...
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
  }
#if defined(NDEBUG)
  /* avoids debug messages on stderr in JSON mode */
  sqlite3_config(SQLITE_CONFIG_LOG, NULL, 0);
#endif

  /*
    The following if/else block translates the PATH_INFO path into an

    internal list so that we can simplify command dispatching later
    on.




  */
  if( pathSplit ){
    /* cson_cgi already did this, so let's just re-use it. This does
       not happen in "plain server" mode, but does in CGI mode.
    */
    assert( g.isCGI && "g.isCGI should have been set by now." );
    cson_cgi_setenv( &g.json.cgiCx,
................................................................................
      }
    }else{
      /* assume CLI mode */
      int i;
      char const * arg;
      cson_value * part;
      assert( (!g.isCGI) && "g.isCGI set and we do not expect that to be the case here." );
      for(i = 1; i < g.argc; ++i ){
        arg = g.argv[i];
        if( !arg || !*arg ) continue;
        part = cson_value_new_string(arg,strlen(arg));
        cson_array_append(ar, part);
      }
    }
  }
................................................................................
** 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.
*/
static char const * json_path_part(unsigned char ndx){
  cson_array * ar = g.isCGI
    ? cson_value_get_array(cson_cgi_getenv(&g.json.cgiCx,
                                           "a",
                                           FossilJsonKeys.commandPath))
    : NULL;
  if( g.isCGI ){
    assert((NULL!=ar) && "Internal error.");
  }
  if( g.json.cmdOffset < 0 ){
    /* first-time setup. */
    short i = g.isCGI ? 0 : 1/*skip argv[0] in CLI mode*/;

#define PARTAT cson_string_cstr( \
                 cson_value_get_string(                     \
                   cson_array_get(ar,i)  \
                   ))
#define NEXT (g.isCGI                               \
              ? PARTAT    \
              : ((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
#undef PARTAT
  if( g.json.cmdOffset < 0 ){
    return NULL;
  }else{
    ndx = g.json.cmdOffset + ndx;
    return g.isCGI
      ? cson_string_cstr(cson_value_get_string(cson_array_get( ar, 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
................................................................................
    goto cleanup; \
  }while(0)

  tmp = cson_value_new_string(MANIFEST_UUID,strlen(MANIFEST_UUID));
  SET("fossil");
 
  {/* "timestamp" */
    time_t const t = (time_t)g.now;
    struct tm gt = *gmtime(&t);
    cson_int_t jsTime = (cson_int_t)mktime(&gt);
    tmp = cson_value_new_integer(jsTime);
    SET("timestamp");
  }
  if( 0 != resultCode ){
    if( ! pMsg ) pMsg = json_err_str(resultCode);
................................................................................
      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,'a', 0);
    if(tmp){
      cson_object_set( o, "$APP", tmp );
    }
    tmp = cson_value_new_integer( g.json.cmdOffset );
    cson_object_set( o, "cmdOffset", tmp );
    cson_object_set( o, "isCGI", cson_value_new_bool( g.isCGI ) );
................................................................................
  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);
  /*cgi_printf("{\"cmd\":\"%s\"}\n",cmd); return;*/
  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;
................................................................................
  cson_value * payload = NULL;
  JsonPageDef const * pageDef;
  json_mode_bootstrap();
  if( g.argc<3 ){
    goto usage;
  }
  db_find_and_open_repository(0, 0);
  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 ){







|
>
|
<
>
>
>
>







 







|







 







|
|
|
|
|
<
<
|
<


<
>
|



<
<
<











<




<
|
<
<







 







|







 







|
>







 







|







 







|







279
280
281
282
283
284
285
286
287
288

289
290
291
292
293
294
295
296
297
298
299
...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
...
368
369
370
371
372
373
374
375
376
377
378
379


380

381
382

383
384
385
386
387



388
389
390
391
392
393
394
395
396
397
398

399
400
401
402

403


404
405
406
407
408
409
410
...
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
...
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
...
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
  }
#if defined(NDEBUG)
  /* avoids debug messages on stderr in JSON mode */
  sqlite3_config(SQLITE_CONFIG_LOG, NULL, 0);
#endif

  /*
    The following if/else block translates the PATH_INFO path (in
    CLI/server modes) or g.argv (CLI mode) into an internal list so
    that we can simplify command dispatching later on.


    Note that translating g.argv this way is overkill but allows us to
    avoid CLI-only special-case handling in other code, e.g.
    json_command_arg().
  */
  if( pathSplit ){
    /* cson_cgi already did this, so let's just re-use it. This does
       not happen in "plain server" mode, but does in CGI mode.
    */
    assert( g.isCGI && "g.isCGI should have been set by now." );
    cson_cgi_setenv( &g.json.cgiCx,
................................................................................
      }
    }else{
      /* assume CLI mode */
      int i;
      char const * arg;
      cson_value * part;
      assert( (!g.isCGI) && "g.isCGI set and we do not expect that to be the case here." );
      for(i = 1/*skip argv[0]*/; i < g.argc; ++i ){
        arg = g.argv[i];
        if( !arg || !*arg ) continue;
        part = cson_value_new_string(arg,strlen(arg));
        cson_array_append(ar, part);
      }
    }
  }
................................................................................
** 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.
*/
static char const * json_command_arg(unsigned char ndx){
  cson_array * ar = cson_value_get_array(
                      cson_cgi_getenv(&g.json.cgiCx,
                                      "a",
                                      FossilJsonKeys.commandPath));


  assert((NULL!=ar) && "Internal error. Was json_mode_bootstrap() called?");

  if( g.json.cmdOffset < 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( 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 cson_string_cstr(cson_value_get_string(cson_array_get( ar, g.json.cmdOffset + 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
................................................................................
    goto cleanup; \
  }while(0)

  tmp = cson_value_new_string(MANIFEST_UUID,strlen(MANIFEST_UUID));
  SET("fossil");
 
  {/* "timestamp" */
    time_t const t = (time_t)time(0);
    struct tm gt = *gmtime(&t);
    cson_int_t jsTime = (cson_int_t)mktime(&gt);
    tmp = cson_value_new_integer(jsTime);
    SET("timestamp");
  }
  if( 0 != resultCode ){
    if( ! pMsg ) pMsg = json_err_str(resultCode);
................................................................................
      payload = NULL;
    }else{
      tmp = payload;
      SET("payload");
    }
  }
#undef SET

  if(0){/*Only for debuggering, add some info to the response.*/
    tmp = cson_cgi_env_get_val(&g.json.cgiCx,'a', 0);
    if(tmp){
      cson_object_set( o, "$APP", tmp );
    }
    tmp = cson_value_new_integer( g.json.cmdOffset );
    cson_object_set( o, "cmdOffset", tmp );
    cson_object_set( o, "isCGI", cson_value_new_bool( g.isCGI ) );
................................................................................
  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_command_arg(1);
  /*cgi_printf("{\"cmd\":\"%s\"}\n",cmd); return;*/
  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;
................................................................................
  cson_value * payload = NULL;
  JsonPageDef const * pageDef;
  json_mode_bootstrap();
  if( g.argc<3 ){
    goto usage;
  }
  db_find_and_open_repository(0, 0);
  cmd = json_command_arg(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 ){