Fossil

Check-in [4a9b5164]
Login

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

Overview
Comment:added POST.payload to json_getenv() list. Re-organized /json/branch/list CLI/HTTP arg handling to behave sanely in CLI mode. Minor typo fix in main.c.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 4a9b51649d6499d33a5fa26fef31d068025d399d
User & Date: stephan 2011-09-21 19:31:58
Context
2011-09-21
20:03
added current branch name to /json/branch/list payload when in an opened checkout. check-in: 7592fe93 user: stephan tags: json
19:31
added POST.payload to json_getenv() list. Re-organized /json/branch/list CLI/HTTP arg handling to behave sanely in CLI mode. Minor typo fix in main.c. check-in: 4a9b5164 user: stephan tags: json
18:55
s/branch_prepare_query/branch_prepare_list_query/g check-in: 267739dd user: stephan tags: json
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

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
....
1561
1562
1563
1564
1565
1566
1567













1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578














1579

1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
....
1601
1602
1603
1604
1605
1606
1607


1608

1609
1610
1611
1612
1613
1614
1615
....
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
*/
cson_value * json_rc_string( int code ){
  return cson_value_new_string( json_rc_cstr(code), 11 );
}


/*
** Gets a POST/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: GET, COOKIE, POST, ENV. COOKIE _should_ be after POST
** but currently is not for internal order-of-init reasons. Since


** fossil only uses one cookie, this is not a real/high-priority
** problem.

*/
cson_value * json_getenv( char const * zKey ){
  cson_value * rc;






  rc = cson_object_get( g.json.param.o, zKey );
  if( rc ){
    return rc;
  }else{

    rc = cson_object_get( g.json.post.o, zKey );
    if(rc){
      return rc;
    }else{
      char const * cv = PD(zKey,NULL);
      if(!cv){
        cv = getenv(zKey);
      }
      if(cv){/*transform it to JSON for later use.*/



        rc = cson_value_new_string(cv,strlen(cv));
        cson_object_set( g.json.param.o, zKey, rc );

        return rc;
      }
    }
  }
  return NULL;
}

/*
** Wrapper around json_getenv() which...
................................................................................

/*
** Impl for /json/branch/list
**
** TODO: change how the "range" of branches is specified.
** Take a string arg in the form ("open","all","closed")
** and decide based off of that.













*/
static cson_value * json_branch_list(unsigned int depth){
  cson_value * payV = cson_value_new_object();
  cson_object * pay = cson_value_get_object(payV);
  cson_value * listV = cson_value_new_array();
  cson_array * list = cson_value_get_array(listV);
  char const * range = NULL;
  int showAll = json_getenv_int("all",0);
  int showClosed = showAll ? 0 : json_getenv_int("closed",0);
  int which = 0;
  Stmt q;














  range = json_getenv_cstr("range");

  if(!range || !*range){
    range = showAll
      ? "all"
      : (showClosed?"closed":"open");
  }
  assert( (NULL != range) && *range );
  switch(*range){
    case 'c':
      range = "closed";
      which = -1;
      break;
................................................................................
  branch_prepare_list_query(&q, which);
  cson_object_set(pay,"branches",listV);
  while((SQLITE_ROW==db_step(&q))){
    cson_value * v = cson_sqlite3_column_to_value(q.pStmt,0);
    if(v){
      cson_array_append(list,v);
    }else{


      json_warn(FSL_JSON_W_COL_TO_JSON_FAILED,NULL);

    }
  }
  return payV;
}

static cson_value * json_timeline_ci(unsigned int depth);
/*
................................................................................

/*
** /json/timeline/ci
**
** Far from complete.
*/
static cson_value * json_timeline_ci(unsigned int depth){
  static const int defaultLimit = 10;
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  cson_value * tmp = NULL;
  cson_value * listV = NULL;
  cson_array * list = NULL;
  int limit = json_getenv_int("n",defaultLimit);
  int check = 0;







|
|
|



|
|
>
>
|
<
>



>
>
>
>
>
>



<
>
|
|
|
|
|
|
|
|
|
>
>
>
|
<
>
|
<







 







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







<
<


>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>

|
<
<







 







>
>
|
>







 







|







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
325
326
327
328
....
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597


1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617


1618
1619
1620
1621
1622
1623
1624
....
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
....
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
*/
cson_value * json_rc_string( int code ){
  return cson_value_new_string( json_rc_cstr(code), 11 );
}


/*
** 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, POST, ENV.
**
** 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){
      cv = getenv(zKey);
    }
    if(cv){/*transform it to JSON for later use.*/
      /* TODO: use sscanf() to figure out if it's an int,
         and transform it to JSON int if it is.
      */
      rc = cson_value_new_string(cv,strlen(cv));

      json_setenv( zKey, rc );
      return rc;

    }
  }
  return NULL;
}

/*
** Wrapper around json_getenv() which...
................................................................................

/*
** Impl for /json/branch/list
**
** TODO: change how the "range" of branches is specified.
** Take a string arg in the form ("open","all","closed")
** and decide based off of that.
**
**
** CLI mode options:
**
**  --range X | -r X, where X is one of (open,closed,all)
**    (only the first letter is significant, default=open).
**  -a (same as --range a)
**  -c (same as --range c)
**
** HTTP mode options:
**
** "range" GET/POST.payload parameter. FIXME: currently we also use
** POST, but really want to restrict this to POST.payload.
*/
static cson_value * json_branch_list(unsigned int depth){
  cson_value * payV = cson_value_new_object();
  cson_object * pay = cson_value_get_object(payV);
  cson_value * listV = cson_value_new_array();
  cson_array * list = cson_value_get_array(listV);
  char const * range = NULL;


  int which = 0;
  Stmt q;
  if(!g.isHTTP){
    range = find_option("range","r",1);
    if(!range||!*range){
      range = find_option("all","a",0);
      if(range && *range){
        range = "a";
      }else{
        range = find_option("closed","c",0);
        if(range&&*range){
          range = "c";
        }
      }
    }
  }else{
    range = json_getenv_cstr("range");
  }
  if(!range || !*range){
    range = "o";


  }
  assert( (NULL != range) && *range );
  switch(*range){
    case 'c':
      range = "closed";
      which = -1;
      break;
................................................................................
  branch_prepare_list_query(&q, which);
  cson_object_set(pay,"branches",listV);
  while((SQLITE_ROW==db_step(&q))){
    cson_value * v = cson_sqlite3_column_to_value(q.pStmt,0);
    if(v){
      cson_array_append(list,v);
    }else{
      char * msg = mprintf("Column-to-json failed @ %s:%d",
                           __FILE__,__LINE__);
      json_warn(FSL_JSON_W_COL_TO_JSON_FAILED,msg);
      free(msg);
    }
  }
  return payV;
}

static cson_value * json_timeline_ci(unsigned int depth);
/*
................................................................................

/*
** /json/timeline/ci
**
** Far from complete.
*/
static cson_value * json_timeline_ci(unsigned int depth){
  static const int defaultLimit = 20;
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  cson_value * tmp = NULL;
  cson_value * listV = NULL;
  cson_array * list = NULL;
  int limit = json_getenv_int("n",defaultLimit);
  int check = 0;

Changes to src/main.c.

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
                                  differently for CLI and CGI modes.
                               */
    } cmd;
    struct {                   /* JSON POST data. */
      cson_value * v;
      cson_object * o;
    } post;
    struct {                   /* GET/COOKIE params in JSON form. */
      cson_value * v;
      cson_object * o;
    } param;
    struct {
      cson_value * v;
      cson_object * o;
    } reqPayload;              /* request payload object (if any) */







|







198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
                                  differently for CLI and CGI modes.
                               */
    } cmd;
    struct {                   /* JSON POST data. */
      cson_value * v;
      cson_object * o;
    } post;
    struct {                   /* GET/COOKIE params in JSON mode. */
      cson_value * v;
      cson_object * o;
    } param;
    struct {
      cson_value * v;
      cson_object * o;
    } reqPayload;              /* request payload object (if any) */