Fossil SCM
Improved error message when parsing JSON input request fail.
Commit
4dae79503fc61e40b1e045c2296fac5fa293e721
Parent
1a4ca414e90ea18…
1 file changed
+17
-2
+17
-2
| --- src/cgi.c | ||
| +++ src/cgi.c | ||
| @@ -738,16 +738,19 @@ | ||
| 738 | 738 | */ |
| 739 | 739 | void cgi_parse_POST_JSON( FILE * zIn, unsigned int contentLen ){ |
| 740 | 740 | cson_value * jv = NULL; |
| 741 | 741 | int rc; |
| 742 | 742 | CgiPostReadState state; |
| 743 | + cson_parse_opt popt = cson_parse_opt_empty; | |
| 744 | + cson_parse_info pinfo = cson_parse_info_empty; | |
| 745 | + popt.maxDepth = 15; | |
| 743 | 746 | state.fh = zIn; |
| 744 | 747 | state.len = contentLen; |
| 745 | 748 | state.pos = 0; |
| 746 | 749 | rc = cson_parse( &jv, |
| 747 | 750 | contentLen ? cson_data_source_FILE_n : cson_data_source_FILE, |
| 748 | - contentLen ? (void *)&state : (void *)zIn, NULL, NULL ); | |
| 751 | + contentLen ? (void *)&state : (void *)zIn, &popt, &pinfo ); | |
| 749 | 752 | if(rc){ |
| 750 | 753 | goto invalidRequest; |
| 751 | 754 | }else{ |
| 752 | 755 | json_gc_add( "POST.JSON", jv ); |
| 753 | 756 | g.json.post.v = jv; |
| @@ -757,11 +760,23 @@ | ||
| 757 | 760 | } |
| 758 | 761 | } |
| 759 | 762 | return; |
| 760 | 763 | invalidRequest: |
| 761 | 764 | cgi_set_content_type(json_guess_content_type()); |
| 762 | - json_err( FSL_JSON_E_INVALID_REQUEST, NULL, 1 ); | |
| 765 | + if(0 != pinfo.errorCode){ /* fancy error message */ | |
| 766 | + char * msg = mprintf("JSON parse error at line %u, column %u, " | |
| 767 | + "byte offset %u: %s", | |
| 768 | + pinfo.line, pinfo.col, pinfo.length, | |
| 769 | + cson_rc_string(pinfo.errorCode)); | |
| 770 | + json_err( FSL_JSON_E_INVALID_REQUEST, msg, 1 ); | |
| 771 | + free(msg); | |
| 772 | + }else if(jv && !g.json.post.o){ | |
| 773 | + json_err( FSL_JSON_E_INVALID_REQUEST, | |
| 774 | + "Request envelope must be a JSON Object (not array).", 1 ); | |
| 775 | + }else{ /* generic error message */ | |
| 776 | + json_err( FSL_JSON_E_INVALID_REQUEST, NULL, 1 ); | |
| 777 | + } | |
| 763 | 778 | fossil_exit( g.isHTTP ? 0 : 1); |
| 764 | 779 | } |
| 765 | 780 | #endif /* FOSSIL_ENABLE_JSON */ |
| 766 | 781 | |
| 767 | 782 | |
| 768 | 783 |
| --- src/cgi.c | |
| +++ src/cgi.c | |
| @@ -738,16 +738,19 @@ | |
| 738 | */ |
| 739 | void cgi_parse_POST_JSON( FILE * zIn, unsigned int contentLen ){ |
| 740 | cson_value * jv = NULL; |
| 741 | int rc; |
| 742 | CgiPostReadState state; |
| 743 | state.fh = zIn; |
| 744 | state.len = contentLen; |
| 745 | state.pos = 0; |
| 746 | rc = cson_parse( &jv, |
| 747 | contentLen ? cson_data_source_FILE_n : cson_data_source_FILE, |
| 748 | contentLen ? (void *)&state : (void *)zIn, NULL, NULL ); |
| 749 | if(rc){ |
| 750 | goto invalidRequest; |
| 751 | }else{ |
| 752 | json_gc_add( "POST.JSON", jv ); |
| 753 | g.json.post.v = jv; |
| @@ -757,11 +760,23 @@ | |
| 757 | } |
| 758 | } |
| 759 | return; |
| 760 | invalidRequest: |
| 761 | cgi_set_content_type(json_guess_content_type()); |
| 762 | json_err( FSL_JSON_E_INVALID_REQUEST, NULL, 1 ); |
| 763 | fossil_exit( g.isHTTP ? 0 : 1); |
| 764 | } |
| 765 | #endif /* FOSSIL_ENABLE_JSON */ |
| 766 | |
| 767 | |
| 768 |
| --- src/cgi.c | |
| +++ src/cgi.c | |
| @@ -738,16 +738,19 @@ | |
| 738 | */ |
| 739 | void cgi_parse_POST_JSON( FILE * zIn, unsigned int contentLen ){ |
| 740 | cson_value * jv = NULL; |
| 741 | int rc; |
| 742 | CgiPostReadState state; |
| 743 | cson_parse_opt popt = cson_parse_opt_empty; |
| 744 | cson_parse_info pinfo = cson_parse_info_empty; |
| 745 | popt.maxDepth = 15; |
| 746 | state.fh = zIn; |
| 747 | state.len = contentLen; |
| 748 | state.pos = 0; |
| 749 | rc = cson_parse( &jv, |
| 750 | contentLen ? cson_data_source_FILE_n : cson_data_source_FILE, |
| 751 | contentLen ? (void *)&state : (void *)zIn, &popt, &pinfo ); |
| 752 | if(rc){ |
| 753 | goto invalidRequest; |
| 754 | }else{ |
| 755 | json_gc_add( "POST.JSON", jv ); |
| 756 | g.json.post.v = jv; |
| @@ -757,11 +760,23 @@ | |
| 760 | } |
| 761 | } |
| 762 | return; |
| 763 | invalidRequest: |
| 764 | cgi_set_content_type(json_guess_content_type()); |
| 765 | if(0 != pinfo.errorCode){ /* fancy error message */ |
| 766 | char * msg = mprintf("JSON parse error at line %u, column %u, " |
| 767 | "byte offset %u: %s", |
| 768 | pinfo.line, pinfo.col, pinfo.length, |
| 769 | cson_rc_string(pinfo.errorCode)); |
| 770 | json_err( FSL_JSON_E_INVALID_REQUEST, msg, 1 ); |
| 771 | free(msg); |
| 772 | }else if(jv && !g.json.post.o){ |
| 773 | json_err( FSL_JSON_E_INVALID_REQUEST, |
| 774 | "Request envelope must be a JSON Object (not array).", 1 ); |
| 775 | }else{ /* generic error message */ |
| 776 | json_err( FSL_JSON_E_INVALID_REQUEST, NULL, 1 ); |
| 777 | } |
| 778 | fossil_exit( g.isHTTP ? 0 : 1); |
| 779 | } |
| 780 | #endif /* FOSSIL_ENABLE_JSON */ |
| 781 | |
| 782 | |
| 783 |