Fossil SCM
Refactored some duplicate json-mode command/path dispatching code into a function.
Commit
f81e3e7f45d8a6daf7ceae394b9959191b6c1dc8
Parent
cff9ecad0b6d061…
1 file changed
+50
-51
+50
-51
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -2263,10 +2263,55 @@ | ||
| 2263 | 2263 | {"create", json_page_nyi, 1}, |
| 2264 | 2264 | /* Last entry MUST have a NULL name. */ |
| 2265 | 2265 | {NULL,NULL,0} |
| 2266 | 2266 | }; |
| 2267 | 2267 | |
| 2268 | +/* | |
| 2269 | +** Internal helper for json_cmd_top() and json_page_top(). | |
| 2270 | +** | |
| 2271 | +** Searches JsonPageDefs for a command with the given name. If found, | |
| 2272 | +** it is used to generate and output JSON response. If not found, it | |
| 2273 | +** generates a JSON-style error response. | |
| 2274 | +*/ | |
| 2275 | +static void json_dispatch_root_command( char const * zCommand ){ | |
| 2276 | + int rc = FSL_JSON_E_UNKNOWN_COMMAND; | |
| 2277 | + cson_value * payload = NULL; | |
| 2278 | + JsonPageDef const * pageDef = NULL; | |
| 2279 | + /*cgi_printf("{\"cmd\":\"%s\"}\n",cmd); return;*/ | |
| 2280 | + pageDef = json_handler_for_name(zCommand,&JsonPageDefs[0]); | |
| 2281 | + if( ! pageDef ){ | |
| 2282 | + json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 0 ); | |
| 2283 | + return; | |
| 2284 | + }else if( pageDef->runMode < 0 /*CLI only*/){ | |
| 2285 | + rc = FSL_JSON_E_WRONG_MODE; | |
| 2286 | + }else if( (g.isHTTP && (pageDef->runMode < 0 /*CLI only*/)) | |
| 2287 | + || | |
| 2288 | + (!g.isHTTP && (pageDef->runMode > 0 /*HTTP only*/)) | |
| 2289 | + ){ | |
| 2290 | + rc = FSL_JSON_E_WRONG_MODE; | |
| 2291 | + } | |
| 2292 | + else{ | |
| 2293 | + rc = 0; | |
| 2294 | + g.json.dispatchDepth = 1; | |
| 2295 | + payload = (*pageDef->func)(); | |
| 2296 | + } | |
| 2297 | + if( g.json.resultCode /*can be set via pageDef->func()*/ ){ | |
| 2298 | + cson_value_free(payload); | |
| 2299 | + json_err(g.json.resultCode, NULL, 0); | |
| 2300 | + }else{ | |
| 2301 | + payload = json_create_response(rc, NULL, payload); | |
| 2302 | + json_send_response(payload); | |
| 2303 | + cson_value_free(payload); | |
| 2304 | + if((0 != rc) && !g.isHTTP){ | |
| 2305 | + /* FIXME: we need a way of passing this error back | |
| 2306 | + up to the routine which called this callback. | |
| 2307 | + e.g. add g.errCode. | |
| 2308 | + */ | |
| 2309 | + fossil_exit(1); | |
| 2310 | + } | |
| 2311 | + } | |
| 2312 | +} | |
| 2268 | 2313 | |
| 2269 | 2314 | #ifdef FOSSIL_ENABLE_JSON /* dupe ifdef needed for mkindex */ |
| 2270 | 2315 | /* |
| 2271 | 2316 | ** WEBPAGE: json |
| 2272 | 2317 | ** |
| @@ -2274,40 +2319,20 @@ | ||
| 2274 | 2319 | ** This function dispatches them, and is the HTTP equivalent of |
| 2275 | 2320 | ** json_cmd_top(). |
| 2276 | 2321 | */ |
| 2277 | 2322 | void json_page_top(void){ |
| 2278 | 2323 | int rc = FSL_JSON_E_UNKNOWN_COMMAND; |
| 2279 | - char const * cmd; | |
| 2324 | + char const * zCommand; | |
| 2280 | 2325 | cson_value * payload = NULL; |
| 2281 | 2326 | JsonPageDef const * pageDef = NULL; |
| 2282 | 2327 | BEGIN_TIMER; |
| 2283 | 2328 | json_mode_bootstrap(); |
| 2284 | - cmd = json_command_arg(1); | |
| 2285 | - if(!cmd || !*cmd){ | |
| 2329 | + zCommand = json_command_arg(1); | |
| 2330 | + if(!zCommand || !*zCommand){ | |
| 2286 | 2331 | goto usage; |
| 2287 | 2332 | } |
| 2288 | - /*cgi_printf("{\"cmd\":\"%s\"}\n",cmd); return;*/ | |
| 2289 | - pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); | |
| 2290 | - if( ! pageDef ){ | |
| 2291 | - json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 0 ); | |
| 2292 | - return; | |
| 2293 | - }else if( pageDef->runMode < 0 /*CLI only*/){ | |
| 2294 | - rc = FSL_JSON_E_WRONG_MODE; | |
| 2295 | - }else{ | |
| 2296 | - rc = 0; | |
| 2297 | - g.json.dispatchDepth = 1; | |
| 2298 | - payload = (*pageDef->func)(); | |
| 2299 | - } | |
| 2300 | - if( g.json.resultCode ){ | |
| 2301 | - cson_value_free(payload); | |
| 2302 | - json_err(g.json.resultCode, NULL, 0); | |
| 2303 | - }else{ | |
| 2304 | - cson_value * root = json_create_response(rc, NULL, payload); | |
| 2305 | - json_send_response(root); | |
| 2306 | - cson_value_free(root); | |
| 2307 | - } | |
| 2308 | - | |
| 2333 | + json_dispatch_root_command( zCommand ); | |
| 2309 | 2334 | return; |
| 2310 | 2335 | usage: |
| 2311 | 2336 | { |
| 2312 | 2337 | Blob cmdNames = empty_blob; |
| 2313 | 2338 | blob_init(&cmdNames, |
| @@ -2366,45 +2391,19 @@ | ||
| 2366 | 2391 | json_main_bootstrap(); |
| 2367 | 2392 | json_mode_bootstrap(); |
| 2368 | 2393 | if( 2 > cson_array_length_get(g.json.cmd.a) ){ |
| 2369 | 2394 | goto usage; |
| 2370 | 2395 | } |
| 2371 | - db_find_and_open_repository(0, 0); | |
| 2372 | 2396 | #if 0 |
| 2373 | 2397 | json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing."); |
| 2374 | 2398 | json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing again."); |
| 2375 | 2399 | #endif |
| 2376 | 2400 | cmd = json_command_arg(1); |
| 2377 | 2401 | if( !cmd || !*cmd ){ |
| 2378 | 2402 | goto usage; |
| 2379 | 2403 | } |
| 2380 | - pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); | |
| 2381 | - if( ! pageDef ){ | |
| 2382 | - json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 1 ); | |
| 2383 | - return; | |
| 2384 | - }else if( pageDef->runMode > 0 /*HTTP only*/){ | |
| 2385 | - rc = FSL_JSON_E_WRONG_MODE; | |
| 2386 | - }else{ | |
| 2387 | - rc = 0; | |
| 2388 | - g.json.dispatchDepth = 1; | |
| 2389 | - payload = (*pageDef->func)(); | |
| 2390 | - } | |
| 2391 | - if( g.json.resultCode ){ | |
| 2392 | - cson_value_free(payload); | |
| 2393 | - json_err(g.json.resultCode, NULL, 1); | |
| 2394 | - }else{ | |
| 2395 | - payload = json_create_response(rc, NULL, payload); | |
| 2396 | - json_send_response(payload); | |
| 2397 | - cson_value_free( payload ); | |
| 2398 | - if((0 != rc) && !g.isHTTP){ | |
| 2399 | - /* FIXME: we need a way of passing this error back | |
| 2400 | - up to the routine which called this callback. | |
| 2401 | - e.g. add g.errCode. | |
| 2402 | - */ | |
| 2403 | - fossil_exit(1); | |
| 2404 | - } | |
| 2405 | - } | |
| 2404 | + json_dispatch_root_command( cmd ); | |
| 2406 | 2405 | return; |
| 2407 | 2406 | usage: |
| 2408 | 2407 | { |
| 2409 | 2408 | Blob cmdNames = empty_blob; |
| 2410 | 2409 | blob_init(&cmdNames, |
| 2411 | 2410 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -2263,10 +2263,55 @@ | |
| 2263 | {"create", json_page_nyi, 1}, |
| 2264 | /* Last entry MUST have a NULL name. */ |
| 2265 | {NULL,NULL,0} |
| 2266 | }; |
| 2267 | |
| 2268 | |
| 2269 | #ifdef FOSSIL_ENABLE_JSON /* dupe ifdef needed for mkindex */ |
| 2270 | /* |
| 2271 | ** WEBPAGE: json |
| 2272 | ** |
| @@ -2274,40 +2319,20 @@ | |
| 2274 | ** This function dispatches them, and is the HTTP equivalent of |
| 2275 | ** json_cmd_top(). |
| 2276 | */ |
| 2277 | void json_page_top(void){ |
| 2278 | int rc = FSL_JSON_E_UNKNOWN_COMMAND; |
| 2279 | char const * cmd; |
| 2280 | cson_value * payload = NULL; |
| 2281 | JsonPageDef const * pageDef = NULL; |
| 2282 | BEGIN_TIMER; |
| 2283 | json_mode_bootstrap(); |
| 2284 | cmd = json_command_arg(1); |
| 2285 | if(!cmd || !*cmd){ |
| 2286 | goto usage; |
| 2287 | } |
| 2288 | /*cgi_printf("{\"cmd\":\"%s\"}\n",cmd); return;*/ |
| 2289 | pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); |
| 2290 | if( ! pageDef ){ |
| 2291 | json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 0 ); |
| 2292 | return; |
| 2293 | }else if( pageDef->runMode < 0 /*CLI only*/){ |
| 2294 | rc = FSL_JSON_E_WRONG_MODE; |
| 2295 | }else{ |
| 2296 | rc = 0; |
| 2297 | g.json.dispatchDepth = 1; |
| 2298 | payload = (*pageDef->func)(); |
| 2299 | } |
| 2300 | if( g.json.resultCode ){ |
| 2301 | cson_value_free(payload); |
| 2302 | json_err(g.json.resultCode, NULL, 0); |
| 2303 | }else{ |
| 2304 | cson_value * root = json_create_response(rc, NULL, payload); |
| 2305 | json_send_response(root); |
| 2306 | cson_value_free(root); |
| 2307 | } |
| 2308 | |
| 2309 | return; |
| 2310 | usage: |
| 2311 | { |
| 2312 | Blob cmdNames = empty_blob; |
| 2313 | blob_init(&cmdNames, |
| @@ -2366,45 +2391,19 @@ | |
| 2366 | json_main_bootstrap(); |
| 2367 | json_mode_bootstrap(); |
| 2368 | if( 2 > cson_array_length_get(g.json.cmd.a) ){ |
| 2369 | goto usage; |
| 2370 | } |
| 2371 | db_find_and_open_repository(0, 0); |
| 2372 | #if 0 |
| 2373 | json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing."); |
| 2374 | json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing again."); |
| 2375 | #endif |
| 2376 | cmd = json_command_arg(1); |
| 2377 | if( !cmd || !*cmd ){ |
| 2378 | goto usage; |
| 2379 | } |
| 2380 | pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); |
| 2381 | if( ! pageDef ){ |
| 2382 | json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 1 ); |
| 2383 | return; |
| 2384 | }else if( pageDef->runMode > 0 /*HTTP only*/){ |
| 2385 | rc = FSL_JSON_E_WRONG_MODE; |
| 2386 | }else{ |
| 2387 | rc = 0; |
| 2388 | g.json.dispatchDepth = 1; |
| 2389 | payload = (*pageDef->func)(); |
| 2390 | } |
| 2391 | if( g.json.resultCode ){ |
| 2392 | cson_value_free(payload); |
| 2393 | json_err(g.json.resultCode, NULL, 1); |
| 2394 | }else{ |
| 2395 | payload = json_create_response(rc, NULL, payload); |
| 2396 | json_send_response(payload); |
| 2397 | cson_value_free( payload ); |
| 2398 | if((0 != rc) && !g.isHTTP){ |
| 2399 | /* FIXME: we need a way of passing this error back |
| 2400 | up to the routine which called this callback. |
| 2401 | e.g. add g.errCode. |
| 2402 | */ |
| 2403 | fossil_exit(1); |
| 2404 | } |
| 2405 | } |
| 2406 | return; |
| 2407 | usage: |
| 2408 | { |
| 2409 | Blob cmdNames = empty_blob; |
| 2410 | blob_init(&cmdNames, |
| 2411 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -2263,10 +2263,55 @@ | |
| 2263 | {"create", json_page_nyi, 1}, |
| 2264 | /* Last entry MUST have a NULL name. */ |
| 2265 | {NULL,NULL,0} |
| 2266 | }; |
| 2267 | |
| 2268 | /* |
| 2269 | ** Internal helper for json_cmd_top() and json_page_top(). |
| 2270 | ** |
| 2271 | ** Searches JsonPageDefs for a command with the given name. If found, |
| 2272 | ** it is used to generate and output JSON response. If not found, it |
| 2273 | ** generates a JSON-style error response. |
| 2274 | */ |
| 2275 | static void json_dispatch_root_command( char const * zCommand ){ |
| 2276 | int rc = FSL_JSON_E_UNKNOWN_COMMAND; |
| 2277 | cson_value * payload = NULL; |
| 2278 | JsonPageDef const * pageDef = NULL; |
| 2279 | /*cgi_printf("{\"cmd\":\"%s\"}\n",cmd); return;*/ |
| 2280 | pageDef = json_handler_for_name(zCommand,&JsonPageDefs[0]); |
| 2281 | if( ! pageDef ){ |
| 2282 | json_err( FSL_JSON_E_UNKNOWN_COMMAND, NULL, 0 ); |
| 2283 | return; |
| 2284 | }else if( pageDef->runMode < 0 /*CLI only*/){ |
| 2285 | rc = FSL_JSON_E_WRONG_MODE; |
| 2286 | }else if( (g.isHTTP && (pageDef->runMode < 0 /*CLI only*/)) |
| 2287 | || |
| 2288 | (!g.isHTTP && (pageDef->runMode > 0 /*HTTP only*/)) |
| 2289 | ){ |
| 2290 | rc = FSL_JSON_E_WRONG_MODE; |
| 2291 | } |
| 2292 | else{ |
| 2293 | rc = 0; |
| 2294 | g.json.dispatchDepth = 1; |
| 2295 | payload = (*pageDef->func)(); |
| 2296 | } |
| 2297 | if( g.json.resultCode /*can be set via pageDef->func()*/ ){ |
| 2298 | cson_value_free(payload); |
| 2299 | json_err(g.json.resultCode, NULL, 0); |
| 2300 | }else{ |
| 2301 | payload = json_create_response(rc, NULL, payload); |
| 2302 | json_send_response(payload); |
| 2303 | cson_value_free(payload); |
| 2304 | if((0 != rc) && !g.isHTTP){ |
| 2305 | /* FIXME: we need a way of passing this error back |
| 2306 | up to the routine which called this callback. |
| 2307 | e.g. add g.errCode. |
| 2308 | */ |
| 2309 | fossil_exit(1); |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | #ifdef FOSSIL_ENABLE_JSON /* dupe ifdef needed for mkindex */ |
| 2315 | /* |
| 2316 | ** WEBPAGE: json |
| 2317 | ** |
| @@ -2274,40 +2319,20 @@ | |
| 2319 | ** This function dispatches them, and is the HTTP equivalent of |
| 2320 | ** json_cmd_top(). |
| 2321 | */ |
| 2322 | void json_page_top(void){ |
| 2323 | int rc = FSL_JSON_E_UNKNOWN_COMMAND; |
| 2324 | char const * zCommand; |
| 2325 | cson_value * payload = NULL; |
| 2326 | JsonPageDef const * pageDef = NULL; |
| 2327 | BEGIN_TIMER; |
| 2328 | json_mode_bootstrap(); |
| 2329 | zCommand = json_command_arg(1); |
| 2330 | if(!zCommand || !*zCommand){ |
| 2331 | goto usage; |
| 2332 | } |
| 2333 | json_dispatch_root_command( zCommand ); |
| 2334 | return; |
| 2335 | usage: |
| 2336 | { |
| 2337 | Blob cmdNames = empty_blob; |
| 2338 | blob_init(&cmdNames, |
| @@ -2366,45 +2391,19 @@ | |
| 2391 | json_main_bootstrap(); |
| 2392 | json_mode_bootstrap(); |
| 2393 | if( 2 > cson_array_length_get(g.json.cmd.a) ){ |
| 2394 | goto usage; |
| 2395 | } |
| 2396 | #if 0 |
| 2397 | json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing."); |
| 2398 | json_warn(FSL_JSON_W_ROW_TO_JSON_FAILED, "Just testing again."); |
| 2399 | #endif |
| 2400 | cmd = json_command_arg(1); |
| 2401 | if( !cmd || !*cmd ){ |
| 2402 | goto usage; |
| 2403 | } |
| 2404 | json_dispatch_root_command( cmd ); |
| 2405 | return; |
| 2406 | usage: |
| 2407 | { |
| 2408 | Blob cmdNames = empty_blob; |
| 2409 | blob_init(&cmdNames, |
| 2410 |