Fossil SCM

Add the /jtext webpage, intended for use by XHR.

drh 2021-09-07 19:09 trunk
Commit 5f7fcbabf01abf6fd4b7bc146e709c9d8d0fe9669abff8837aefdc0f7cb87eb9
1 file changed +57
+57
--- src/info.c
+++ src/info.c
@@ -1882,10 +1882,67 @@
18821882
}
18831883
g.isConst = 1;
18841884
deliver_artifact(rid, P("m"));
18851885
}
18861886
1887
+
1888
+/*
1889
+** WEBPAGE: jtext
1890
+** URL: /jtext/HASH?from=N&to=M
1891
+**
1892
+** Return lines of text from a file as a JSON array - one entry in the
1893
+** array for each line of text.
1894
+**
1895
+** This page is intended to be used in an XHR from javascript on a diff
1896
+** page, to return unseen context to fill in additional context when the
1897
+** user clicks on the appropriate button.
1898
+*/
1899
+void jtext_page(void){
1900
+ int rid = 0;
1901
+ const char *zName = PD("name", "");
1902
+ int iFrom = atoi(PD("from","0"));
1903
+ int iTo = atoi(PD("to","0"));
1904
+ int ln;
1905
+ const char *zSep;
1906
+ Blob content;
1907
+ Blob line;
1908
+ Blob *pOut;
1909
+
1910
+ login_check_credentials();
1911
+ if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
1912
+ rid = db_int(0, "SELECT rid FROM blob WHERE uuid=%Q", zName);
1913
+ if( rid==0 ){
1914
+ cgi_set_status(404, "Not Found");
1915
+ @ Unknown artifact: "%h(zName)"
1916
+ return;
1917
+ }
1918
+ if( iFrom<1 || iTo<iFrom ){
1919
+ cgi_set_status(500, "Bad Request");
1920
+ @ Invalid line range
1921
+ return;
1922
+ }
1923
+ content_get(rid, &content);
1924
+ g.isConst = 1;
1925
+ cgi_set_content_type("text/json");
1926
+ ln = 0;
1927
+ zSep = "[\n";
1928
+ while( ln<iFrom ){
1929
+ blob_line(&content, &line);
1930
+ ln++;
1931
+ }
1932
+ pOut = cgi_output_blob();
1933
+ while( ln<=iTo ){
1934
+ blob_append(pOut, zSep, 2);
1935
+ blob_trim(&line);
1936
+ blob_append_json_literal(pOut, blob_buffer(&line), blob_size(&line));
1937
+ zSep = ",\n";
1938
+ blob_line(&content, &line);
1939
+ ln++;
1940
+ }
1941
+ blob_appendf(pOut,"]\n");
1942
+ blob_reset(&content);
1943
+}
18871944
18881945
/*
18891946
** Generate a verbatim artifact as the result of an HTTP request.
18901947
** If zMime is not NULL, use it as the mimetype. If zMime is
18911948
** NULL, guess at the mimetype based on the filename
18921949
--- src/info.c
+++ src/info.c
@@ -1882,10 +1882,67 @@
1882 }
1883 g.isConst = 1;
1884 deliver_artifact(rid, P("m"));
1885 }
1886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1887
1888 /*
1889 ** Generate a verbatim artifact as the result of an HTTP request.
1890 ** If zMime is not NULL, use it as the mimetype. If zMime is
1891 ** NULL, guess at the mimetype based on the filename
1892
--- src/info.c
+++ src/info.c
@@ -1882,10 +1882,67 @@
1882 }
1883 g.isConst = 1;
1884 deliver_artifact(rid, P("m"));
1885 }
1886
1887
1888 /*
1889 ** WEBPAGE: jtext
1890 ** URL: /jtext/HASH?from=N&to=M
1891 **
1892 ** Return lines of text from a file as a JSON array - one entry in the
1893 ** array for each line of text.
1894 **
1895 ** This page is intended to be used in an XHR from javascript on a diff
1896 ** page, to return unseen context to fill in additional context when the
1897 ** user clicks on the appropriate button.
1898 */
1899 void jtext_page(void){
1900 int rid = 0;
1901 const char *zName = PD("name", "");
1902 int iFrom = atoi(PD("from","0"));
1903 int iTo = atoi(PD("to","0"));
1904 int ln;
1905 const char *zSep;
1906 Blob content;
1907 Blob line;
1908 Blob *pOut;
1909
1910 login_check_credentials();
1911 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
1912 rid = db_int(0, "SELECT rid FROM blob WHERE uuid=%Q", zName);
1913 if( rid==0 ){
1914 cgi_set_status(404, "Not Found");
1915 @ Unknown artifact: "%h(zName)"
1916 return;
1917 }
1918 if( iFrom<1 || iTo<iFrom ){
1919 cgi_set_status(500, "Bad Request");
1920 @ Invalid line range
1921 return;
1922 }
1923 content_get(rid, &content);
1924 g.isConst = 1;
1925 cgi_set_content_type("text/json");
1926 ln = 0;
1927 zSep = "[\n";
1928 while( ln<iFrom ){
1929 blob_line(&content, &line);
1930 ln++;
1931 }
1932 pOut = cgi_output_blob();
1933 while( ln<=iTo ){
1934 blob_append(pOut, zSep, 2);
1935 blob_trim(&line);
1936 blob_append_json_literal(pOut, blob_buffer(&line), blob_size(&line));
1937 zSep = ",\n";
1938 blob_line(&content, &line);
1939 ln++;
1940 }
1941 blob_appendf(pOut,"]\n");
1942 blob_reset(&content);
1943 }
1944
1945 /*
1946 ** Generate a verbatim artifact as the result of an HTTP request.
1947 ** If zMime is not NULL, use it as the mimetype. If zMime is
1948 ** NULL, guess at the mimetype based on the filename
1949

Keyboard Shortcuts

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