Fossil SCM

Added todo notes for adding loginExpiryTime in /json/login.

stephan 2011-11-23 00:36 trunk
Commit 5fdf14ba1cfe8de695b29a2f2a757894d66bfb2a
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -1396,10 +1396,11 @@
13961396
/* end file parser/JSON_parser.c */
13971397
/* begin file ./cson.c */
13981398
#include <assert.h>
13991399
#include <stdlib.h> /* malloc()/free() */
14001400
#include <string.h>
1401
+#include <errno.h>
14011402
14021403
#ifdef _MSC_VER
14031404
# if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
14041405
# pragma warning( push )
14051406
# pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */
@@ -4950,10 +4951,81 @@
49504951
}
49514952
else continue;
49524953
}
49534954
return 0;
49544955
}
4956
+
4957
+static cson_value * cson_guess_arg_type(char const *arg){
4958
+ char * end = NULL;
4959
+ if(('0'<=*arg) && ('9'>=*arg)){
4960
+ goto do_string;
4961
+ }
4962
+ {
4963
+ long const val = strtol(arg, &end, 10);
4964
+ if(!*end){
4965
+ return cson_value_new_integer( (cson_int_t)val);
4966
+ }
4967
+ }
4968
+ {
4969
+ double const val = strtod(arg, &end);
4970
+ if(!*end){
4971
+ return cson_value_new_double(val);
4972
+ }
4973
+ }
4974
+
4975
+
4976
+ do_string:
4977
+ return cson_value_new_string(arg, strlen(arg));
4978
+}
4979
+
4980
+
4981
+int cson_parse_argv_flags( int argc, char const * const * argv,
4982
+ cson_object ** tgt, unsigned int * count ){
4983
+ cson_object * o = NULL;
4984
+ int rc = 0;
4985
+ int i = 0;
4986
+ if(argc<1 || !argc || !tgt) return cson_rc.ArgError;
4987
+ o = *tgt ? *tgt : cson_new_object();
4988
+ if(count) *count = 0;
4989
+ for( i = 0; i < argc; ++i ){
4990
+ char const * arg = argv[i];
4991
+ char const * key = arg;
4992
+ char const * pos;
4993
+ cson_string * k = NULL;
4994
+ cson_value * v = NULL;
4995
+ if('-' != *arg) continue;
4996
+ while('-'==*key) ++key;
4997
+ if(!*key) continue;
4998
+ pos = key;
4999
+ while( *pos && ('=' != *pos)) ++pos;
5000
+ k = cson_new_string(key, pos-key);
5001
+ if(!k){
5002
+ rc = cson_rc.AllocError;
5003
+ break;
5004
+ }
5005
+ if(!*pos){ /** --key */
5006
+ v = cson_value_true();
5007
+ }else{ /** --key=...*/
5008
+ assert('=' == *pos);
5009
+ ++pos /*skip '='*/;
5010
+ v = *pos
5011
+ ? cson_guess_arg_type(pos)
5012
+ : cson_value_null();
5013
+ }
5014
+ if(0 != (rc=cson_object_set_s(o, k, v))){
5015
+ cson_free_string(k);
5016
+ cson_value_free(v);
5017
+ break;
5018
+ }
5019
+ else if(count) ++*count;
5020
+ }
5021
+ if(o != *tgt){
5022
+ if(rc) cson_free_object(o);
5023
+ else *tgt = o;
5024
+ }
5025
+ return rc;
5026
+}
49555027
49565028
#if defined(__cplusplus)
49575029
} /*extern "C"*/
49585030
#endif
49595031
49605032
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -1396,10 +1396,11 @@
1396 /* end file parser/JSON_parser.c */
1397 /* begin file ./cson.c */
1398 #include <assert.h>
1399 #include <stdlib.h> /* malloc()/free() */
1400 #include <string.h>
 
1401
1402 #ifdef _MSC_VER
1403 # if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
1404 # pragma warning( push )
1405 # pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */
@@ -4950,10 +4951,81 @@
4950 }
4951 else continue;
4952 }
4953 return 0;
4954 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4955
4956 #if defined(__cplusplus)
4957 } /*extern "C"*/
4958 #endif
4959
4960
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -1396,10 +1396,11 @@
1396 /* end file parser/JSON_parser.c */
1397 /* begin file ./cson.c */
1398 #include <assert.h>
1399 #include <stdlib.h> /* malloc()/free() */
1400 #include <string.h>
1401 #include <errno.h>
1402
1403 #ifdef _MSC_VER
1404 # if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
1405 # pragma warning( push )
1406 # pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */
@@ -4950,10 +4951,81 @@
4951 }
4952 else continue;
4953 }
4954 return 0;
4955 }
4956
4957 static cson_value * cson_guess_arg_type(char const *arg){
4958 char * end = NULL;
4959 if(('0'<=*arg) && ('9'>=*arg)){
4960 goto do_string;
4961 }
4962 {
4963 long const val = strtol(arg, &end, 10);
4964 if(!*end){
4965 return cson_value_new_integer( (cson_int_t)val);
4966 }
4967 }
4968 {
4969 double const val = strtod(arg, &end);
4970 if(!*end){
4971 return cson_value_new_double(val);
4972 }
4973 }
4974
4975
4976 do_string:
4977 return cson_value_new_string(arg, strlen(arg));
4978 }
4979
4980
4981 int cson_parse_argv_flags( int argc, char const * const * argv,
4982 cson_object ** tgt, unsigned int * count ){
4983 cson_object * o = NULL;
4984 int rc = 0;
4985 int i = 0;
4986 if(argc<1 || !argc || !tgt) return cson_rc.ArgError;
4987 o = *tgt ? *tgt : cson_new_object();
4988 if(count) *count = 0;
4989 for( i = 0; i < argc; ++i ){
4990 char const * arg = argv[i];
4991 char const * key = arg;
4992 char const * pos;
4993 cson_string * k = NULL;
4994 cson_value * v = NULL;
4995 if('-' != *arg) continue;
4996 while('-'==*key) ++key;
4997 if(!*key) continue;
4998 pos = key;
4999 while( *pos && ('=' != *pos)) ++pos;
5000 k = cson_new_string(key, pos-key);
5001 if(!k){
5002 rc = cson_rc.AllocError;
5003 break;
5004 }
5005 if(!*pos){ /** --key */
5006 v = cson_value_true();
5007 }else{ /** --key=...*/
5008 assert('=' == *pos);
5009 ++pos /*skip '='*/;
5010 v = *pos
5011 ? cson_guess_arg_type(pos)
5012 : cson_value_null();
5013 }
5014 if(0 != (rc=cson_object_set_s(o, k, v))){
5015 cson_free_string(k);
5016 cson_value_free(v);
5017 break;
5018 }
5019 else if(count) ++*count;
5020 }
5021 if(o != *tgt){
5022 if(rc) cson_free_object(o);
5023 else *tgt = o;
5024 }
5025 return rc;
5026 }
5027
5028 #if defined(__cplusplus)
5029 } /*extern "C"*/
5030 #endif
5031
5032
--- src/cson_amalgamation.h
+++ src/cson_amalgamation.h
@@ -2108,10 +2108,64 @@
21082108
necessarily increase when a new item is inserted into it. An interesting
21092109
side-effect of this is that when cson_clone()ing an array or object, the
21102110
size of the clone can actually be less than the original.
21112111
*/
21122112
unsigned int cson_value_msize(cson_value const * v);
2113
+
2114
+/**
2115
+ Parses command-line-style arguments into a JSON object.
2116
+
2117
+ It expects arguments to be in any of these forms, and any number
2118
+ of leading dashes are treated identically:
2119
+
2120
+ --key : Treats key as a boolean with a true value.
2121
+
2122
+ --key=VAL : Treats VAL as either a double, integer, or string.
2123
+
2124
+ --key= : Treats key as a JSON null (not literal NULL) value.
2125
+
2126
+ Arguments not starting with a dash are skipped.
2127
+
2128
+ Each key/value pair is inserted into an object. If a given key
2129
+ appears more than once then only the final entry is actually
2130
+ stored.
2131
+
2132
+ argc and argv are expected to be values from main() (or similar,
2133
+ possibly adjusted to remove argv[0]).
2134
+
2135
+ tgt must be either a pointer to NULL or a pointer to a
2136
+ client-provided Object. If (NULL==*tgt) then this function
2137
+ allocates a new object and on success it stores the new object in
2138
+ *tgt (it is owned by the caller). If (NULL!=*tgt) then it is
2139
+ assumed to be a properly allocated object. DO NOT pass a pointer to
2140
+ an unitialized pointer, as that will fool this function into
2141
+ thinking it is a valid object and Undefined Behaviour will ensue.
2142
+
2143
+ If count is not NULL then the number of arugments parsed by this
2144
+ function are assigned to it. On error, count will be the number of
2145
+ options successfully parsed before the error was encountered.
2146
+
2147
+ On success:
2148
+
2149
+ - 0 is returned.
2150
+
2151
+ - If (*tgt==NULL) then *tgt is assigned to a newly-allocated
2152
+ object, owned by the caller. Note that even if no arguments are
2153
+ parsed, the object is still created.
2154
+
2155
+ On error:
2156
+
2157
+ - non-0 is returned
2158
+
2159
+ - If (*tgt==NULL) then it is not modified.
2160
+
2161
+ - If (*tgt!=NULL) (i.e., the caller provides his own object) then
2162
+ it might contain partial results.
2163
+*/
2164
+int cson_parse_argv_flags( int argc, char const * const * argv,
2165
+ cson_object ** tgt, unsigned int * count );
2166
+
21132167
21142168
/* LICENSE
21152169
21162170
This software's source code, including accompanying documentation and
21172171
demonstration applications, are licensed under the following
@@ -2382,7 +2436,6 @@
23822436
#endif
23832437
23842438
#endif /* CSON_ENABLE_SQLITE3 */
23852439
#endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */
23862440
/* end file include/wh/cson/cson_sqlite3.h */
2387
-
23882441
#endif /* FOSSIL_ENABLE_JSON */
23892442
--- src/cson_amalgamation.h
+++ src/cson_amalgamation.h
@@ -2108,10 +2108,64 @@
2108 necessarily increase when a new item is inserted into it. An interesting
2109 side-effect of this is that when cson_clone()ing an array or object, the
2110 size of the clone can actually be less than the original.
2111 */
2112 unsigned int cson_value_msize(cson_value const * v);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2113
2114 /* LICENSE
2115
2116 This software's source code, including accompanying documentation and
2117 demonstration applications, are licensed under the following
@@ -2382,7 +2436,6 @@
2382 #endif
2383
2384 #endif /* CSON_ENABLE_SQLITE3 */
2385 #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */
2386 /* end file include/wh/cson/cson_sqlite3.h */
2387
2388 #endif /* FOSSIL_ENABLE_JSON */
2389
--- src/cson_amalgamation.h
+++ src/cson_amalgamation.h
@@ -2108,10 +2108,64 @@
2108 necessarily increase when a new item is inserted into it. An interesting
2109 side-effect of this is that when cson_clone()ing an array or object, the
2110 size of the clone can actually be less than the original.
2111 */
2112 unsigned int cson_value_msize(cson_value const * v);
2113
2114 /**
2115 Parses command-line-style arguments into a JSON object.
2116
2117 It expects arguments to be in any of these forms, and any number
2118 of leading dashes are treated identically:
2119
2120 --key : Treats key as a boolean with a true value.
2121
2122 --key=VAL : Treats VAL as either a double, integer, or string.
2123
2124 --key= : Treats key as a JSON null (not literal NULL) value.
2125
2126 Arguments not starting with a dash are skipped.
2127
2128 Each key/value pair is inserted into an object. If a given key
2129 appears more than once then only the final entry is actually
2130 stored.
2131
2132 argc and argv are expected to be values from main() (or similar,
2133 possibly adjusted to remove argv[0]).
2134
2135 tgt must be either a pointer to NULL or a pointer to a
2136 client-provided Object. If (NULL==*tgt) then this function
2137 allocates a new object and on success it stores the new object in
2138 *tgt (it is owned by the caller). If (NULL!=*tgt) then it is
2139 assumed to be a properly allocated object. DO NOT pass a pointer to
2140 an unitialized pointer, as that will fool this function into
2141 thinking it is a valid object and Undefined Behaviour will ensue.
2142
2143 If count is not NULL then the number of arugments parsed by this
2144 function are assigned to it. On error, count will be the number of
2145 options successfully parsed before the error was encountered.
2146
2147 On success:
2148
2149 - 0 is returned.
2150
2151 - If (*tgt==NULL) then *tgt is assigned to a newly-allocated
2152 object, owned by the caller. Note that even if no arguments are
2153 parsed, the object is still created.
2154
2155 On error:
2156
2157 - non-0 is returned
2158
2159 - If (*tgt==NULL) then it is not modified.
2160
2161 - If (*tgt!=NULL) (i.e., the caller provides his own object) then
2162 it might contain partial results.
2163 */
2164 int cson_parse_argv_flags( int argc, char const * const * argv,
2165 cson_object ** tgt, unsigned int * count );
2166
2167
2168 /* LICENSE
2169
2170 This software's source code, including accompanying documentation and
2171 demonstration applications, are licensed under the following
@@ -2382,7 +2436,6 @@
2436 #endif
2437
2438 #endif /* CSON_ENABLE_SQLITE3 */
2439 #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */
2440 /* end file include/wh/cson/cson_sqlite3.h */
 
2441 #endif /* FOSSIL_ENABLE_JSON */
2442
--- src/json_login.c
+++ src/json_login.c
@@ -159,10 +159,25 @@
159159
cson_object_set(po, "name", json_new_string(name));
160160
cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q", name);
161161
cson_object_set(po, "capabilities", cap ? json_new_string(cap) : cson_value_null() );
162162
free(cap);
163163
cson_object_set(po, "loginCookieName", json_new_string( login_cookie_name() ) );
164
+ /* TODO: add loginExpiryTime to the payload. To do this properly
165
+ we "should" add an ([unsigned] int *) to
166
+ login_set_user_cookie() and login_set_anon_cookie(), to which
167
+ the expiry time is assigned. (Remember that JSON doesn't do
168
+ unsigned int.)
169
+
170
+ For non-anonymous users we could also simply query the
171
+ user.cexpire db field after calling login_set_user_cookie(),
172
+ but for anonymous we need to get the time when the cookie is
173
+ set because anon does not get a db entry like normal users
174
+ do. Anonyous cookies currently have a hard-coded lifetime in
175
+ login_set_anon_cookie() (currently 6 hours), which we "should
176
+ arguably" change to use the time configured for non-anonymous
177
+ users (see login_set_user_cookie() for details).
178
+ */
164179
return payload;
165180
}
166181
}
167182
168183
/*
169184
--- src/json_login.c
+++ src/json_login.c
@@ -159,10 +159,25 @@
159 cson_object_set(po, "name", json_new_string(name));
160 cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q", name);
161 cson_object_set(po, "capabilities", cap ? json_new_string(cap) : cson_value_null() );
162 free(cap);
163 cson_object_set(po, "loginCookieName", json_new_string( login_cookie_name() ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164 return payload;
165 }
166 }
167
168 /*
169
--- src/json_login.c
+++ src/json_login.c
@@ -159,10 +159,25 @@
159 cson_object_set(po, "name", json_new_string(name));
160 cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q", name);
161 cson_object_set(po, "capabilities", cap ? json_new_string(cap) : cson_value_null() );
162 free(cap);
163 cson_object_set(po, "loginCookieName", json_new_string( login_cookie_name() ) );
164 /* TODO: add loginExpiryTime to the payload. To do this properly
165 we "should" add an ([unsigned] int *) to
166 login_set_user_cookie() and login_set_anon_cookie(), to which
167 the expiry time is assigned. (Remember that JSON doesn't do
168 unsigned int.)
169
170 For non-anonymous users we could also simply query the
171 user.cexpire db field after calling login_set_user_cookie(),
172 but for anonymous we need to get the time when the cookie is
173 set because anon does not get a db entry like normal users
174 do. Anonyous cookies currently have a hard-coded lifetime in
175 login_set_anon_cookie() (currently 6 hours), which we "should
176 arguably" change to use the time configured for non-anonymous
177 users (see login_set_user_cookie() for details).
178 */
179 return payload;
180 }
181 }
182
183 /*
184

Keyboard Shortcuts

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