Fossil SCM

Refactoring, phase 1, move httpCmd.

mistachkin 2013-10-14 05:10 UTC tkt-change-hook
Commit d1b4d1b6304ab4a6edc73c79fc734d0787fc56f3
2 files changed +90 -90
--- src/th_main.c
+++ src/th_main.c
@@ -828,10 +828,99 @@
828828
rc = TH_ERROR;
829829
}
830830
re_free(pRe);
831831
return rc;
832832
}
833
+
834
+/*
835
+** TH command: http -async URL ?PAYLOAD?
836
+**
837
+** Do a HTTP request to specified URL. If PAYLOAD is present
838
+** it will be POST'ed as text/plain, otherwise it's a GET
839
+*/
840
+static int httpCmd(
841
+ Th_Interp *interp,
842
+ void *p,
843
+ int argc,
844
+ const char **argv,
845
+ int *argl
846
+){
847
+ int i;
848
+ const char *zSep, *type, *regexp, *params;
849
+ Blob hdr, payload;
850
+ ReCompiled *pRe = 0;
851
+
852
+ if( (argc>1) && strcmp(argv[1],"-async") ){
853
+ Th_ErrorMessage(interp, "synchronous http requests not yet implemented", 0, 0);
854
+ return TH_ERROR;
855
+ }
856
+ ++argv;
857
+ --argc;
858
+ blob_zero(&payload);
859
+ if( argc!=2 ){
860
+ if( argc != 3 ){
861
+ return Th_WrongNumArgs(interp, "http -async url ?payload?");
862
+ }
863
+ blob_append(&payload, argv[2], -1);
864
+ type = "POST";
865
+ }else{
866
+ type = "GET";
867
+ }
868
+ params = strrchr(argv[1], '?');
869
+ url_parse(argv[1], 0);
870
+ if( g.urlIsSsh || g.urlIsFile ){
871
+ Th_ErrorMessage(interp, "url must be http:// or https://", 0, 0);
872
+ return TH_ERROR;
873
+ }
874
+ regexp = db_get("th1-uri-regexp", 0);
875
+ if( regexp && regexp[0] ){
876
+ const char * zErr = re_compile(&pRe, regexp, 0);
877
+ if( zErr ){
878
+ Th_SetResult(interp, zErr, -1);
879
+ return TH_ERROR;
880
+ }
881
+ }
882
+ if (!pRe || !re_match(pRe, (const unsigned char *)argv[1], -1) ){
883
+ Th_SetResult(interp, "url not allowed", -1);
884
+ return TH_ERROR;
885
+ }
886
+ re_free(pRe);
887
+ if( transport_open() ){
888
+ Th_ErrorMessage(interp, transport_errmsg(), 0, 0);
889
+ return TH_ERROR;
890
+ }
891
+ blob_zero(&hdr);
892
+ i = strlen(g.urlPath);
893
+ if( (i>0) && (params!=argv[1]) ){
894
+ zSep = "";
895
+ }else{
896
+ zSep = "/";
897
+ }
898
+ blob_appendf(&hdr, "%s %s%s%s HTTP/1.0\r\n", type, zSep, g.urlPath, params?params:"");
899
+ if( g.urlProxyAuth ){
900
+ blob_appendf(&hdr, "Proxy-Authorization: %s\r\n", g.urlProxyAuth);
901
+ }
902
+ if( g.urlPasswd && g.urlUser && g.urlPasswd[0]=='#' ){
903
+ char *zCredentials = mprintf("%s:%s", g.urlUser, &g.urlPasswd[1]);
904
+ char *zEncoded = encode64(zCredentials, -1);
905
+ blob_appendf(&hdr, "Authorization: Basic %s\r\n", zEncoded);
906
+ fossil_free(zEncoded);
907
+ fossil_free(zCredentials);
908
+ }
909
+ blob_appendf(&hdr, "Host: %s\r\n", g.urlHostname);
910
+ blob_appendf(&hdr, "User-Agent: Fossil/" RELEASE_VERSION
911
+ " (" MANIFEST_DATE " " MANIFEST_VERSION ")\r\n");
912
+ blob_appendf(&hdr, "Content-Type: text/plain\r\n");
913
+ blob_appendf(&hdr, "Content-Length: %d\r\n\r\n", blob_size(&payload));
914
+
915
+ transport_send(&hdr);
916
+ transport_send(&payload);
917
+ transport_close();
918
+ g.urlProtocol=0; /* Make sure the url is not re-used. */
919
+ Th_SetResult(interp, "", -1);
920
+ return TH_OK;
921
+}
833922
834923
/*
835924
** Make sure the interpreter has been initialized. Initialize it if
836925
** it has not been already.
837926
**
@@ -856,10 +945,11 @@
856945
{"enable_output", enableOutputCmd, 0},
857946
{"hascap", hascapCmd, 0},
858947
{"hasfeature", hasfeatureCmd, 0},
859948
{"html", putsCmd, (void*)&aFlags[0]},
860949
{"htmlize", htmlizeCmd, 0},
950
+ {"http", httpCmd, 0},
861951
{"linecount", linecntCmd, 0},
862952
{"puts", putsCmd, (void*)&aFlags[1]},
863953
{"query", queryCmd, 0},
864954
{"randhex", randhexCmd, 0},
865955
{"regexp", regexpCmd, 0},
866956
--- src/th_main.c
+++ src/th_main.c
@@ -828,10 +828,99 @@
828 rc = TH_ERROR;
829 }
830 re_free(pRe);
831 return rc;
832 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
833
834 /*
835 ** Make sure the interpreter has been initialized. Initialize it if
836 ** it has not been already.
837 **
@@ -856,10 +945,11 @@
856 {"enable_output", enableOutputCmd, 0},
857 {"hascap", hascapCmd, 0},
858 {"hasfeature", hasfeatureCmd, 0},
859 {"html", putsCmd, (void*)&aFlags[0]},
860 {"htmlize", htmlizeCmd, 0},
 
861 {"linecount", linecntCmd, 0},
862 {"puts", putsCmd, (void*)&aFlags[1]},
863 {"query", queryCmd, 0},
864 {"randhex", randhexCmd, 0},
865 {"regexp", regexpCmd, 0},
866
--- src/th_main.c
+++ src/th_main.c
@@ -828,10 +828,99 @@
828 rc = TH_ERROR;
829 }
830 re_free(pRe);
831 return rc;
832 }
833
834 /*
835 ** TH command: http -async URL ?PAYLOAD?
836 **
837 ** Do a HTTP request to specified URL. If PAYLOAD is present
838 ** it will be POST'ed as text/plain, otherwise it's a GET
839 */
840 static int httpCmd(
841 Th_Interp *interp,
842 void *p,
843 int argc,
844 const char **argv,
845 int *argl
846 ){
847 int i;
848 const char *zSep, *type, *regexp, *params;
849 Blob hdr, payload;
850 ReCompiled *pRe = 0;
851
852 if( (argc>1) && strcmp(argv[1],"-async") ){
853 Th_ErrorMessage(interp, "synchronous http requests not yet implemented", 0, 0);
854 return TH_ERROR;
855 }
856 ++argv;
857 --argc;
858 blob_zero(&payload);
859 if( argc!=2 ){
860 if( argc != 3 ){
861 return Th_WrongNumArgs(interp, "http -async url ?payload?");
862 }
863 blob_append(&payload, argv[2], -1);
864 type = "POST";
865 }else{
866 type = "GET";
867 }
868 params = strrchr(argv[1], '?');
869 url_parse(argv[1], 0);
870 if( g.urlIsSsh || g.urlIsFile ){
871 Th_ErrorMessage(interp, "url must be http:// or https://", 0, 0);
872 return TH_ERROR;
873 }
874 regexp = db_get("th1-uri-regexp", 0);
875 if( regexp && regexp[0] ){
876 const char * zErr = re_compile(&pRe, regexp, 0);
877 if( zErr ){
878 Th_SetResult(interp, zErr, -1);
879 return TH_ERROR;
880 }
881 }
882 if (!pRe || !re_match(pRe, (const unsigned char *)argv[1], -1) ){
883 Th_SetResult(interp, "url not allowed", -1);
884 return TH_ERROR;
885 }
886 re_free(pRe);
887 if( transport_open() ){
888 Th_ErrorMessage(interp, transport_errmsg(), 0, 0);
889 return TH_ERROR;
890 }
891 blob_zero(&hdr);
892 i = strlen(g.urlPath);
893 if( (i>0) && (params!=argv[1]) ){
894 zSep = "";
895 }else{
896 zSep = "/";
897 }
898 blob_appendf(&hdr, "%s %s%s%s HTTP/1.0\r\n", type, zSep, g.urlPath, params?params:"");
899 if( g.urlProxyAuth ){
900 blob_appendf(&hdr, "Proxy-Authorization: %s\r\n", g.urlProxyAuth);
901 }
902 if( g.urlPasswd && g.urlUser && g.urlPasswd[0]=='#' ){
903 char *zCredentials = mprintf("%s:%s", g.urlUser, &g.urlPasswd[1]);
904 char *zEncoded = encode64(zCredentials, -1);
905 blob_appendf(&hdr, "Authorization: Basic %s\r\n", zEncoded);
906 fossil_free(zEncoded);
907 fossil_free(zCredentials);
908 }
909 blob_appendf(&hdr, "Host: %s\r\n", g.urlHostname);
910 blob_appendf(&hdr, "User-Agent: Fossil/" RELEASE_VERSION
911 " (" MANIFEST_DATE " " MANIFEST_VERSION ")\r\n");
912 blob_appendf(&hdr, "Content-Type: text/plain\r\n");
913 blob_appendf(&hdr, "Content-Length: %d\r\n\r\n", blob_size(&payload));
914
915 transport_send(&hdr);
916 transport_send(&payload);
917 transport_close();
918 g.urlProtocol=0; /* Make sure the url is not re-used. */
919 Th_SetResult(interp, "", -1);
920 return TH_OK;
921 }
922
923 /*
924 ** Make sure the interpreter has been initialized. Initialize it if
925 ** it has not been already.
926 **
@@ -856,10 +945,11 @@
945 {"enable_output", enableOutputCmd, 0},
946 {"hascap", hascapCmd, 0},
947 {"hasfeature", hasfeatureCmd, 0},
948 {"html", putsCmd, (void*)&aFlags[0]},
949 {"htmlize", htmlizeCmd, 0},
950 {"http", httpCmd, 0},
951 {"linecount", linecntCmd, 0},
952 {"puts", putsCmd, (void*)&aFlags[1]},
953 {"query", queryCmd, 0},
954 {"randhex", randhexCmd, 0},
955 {"regexp", regexpCmd, 0},
956
-90
--- src/xfer.c
+++ src/xfer.c
@@ -819,99 +819,10 @@
819819
*/
820820
static void server_private_xfer_not_authorized(void){
821821
@ error not\sauthorized\sto\ssync\sprivate\scontent
822822
}
823823
824
-/*
825
-** TH command: http -async URL ?PAYLOAD?
826
-**
827
-** Do a HTTP request to specified URL. If PAYLOAD is present
828
-** it will be POST'ed as text/plain, otherwise it's a GET
829
-*/
830
-static int httpCmd(
831
- Th_Interp *interp,
832
- void *p,
833
- int argc,
834
- const char **argv,
835
- int *argl
836
-){
837
- int i;
838
- const char *zSep, *type, *regexp, *params;
839
- Blob hdr, payload;
840
- ReCompiled *pRe = 0;
841
-
842
- if( (argc>1) && strcmp(argv[1],"-async") ){
843
- Th_ErrorMessage(interp, "synchronous http requests not yet implemented", 0, 0);
844
- return TH_ERROR;
845
- }
846
- ++argv;
847
- --argc;
848
- blob_zero(&payload);
849
- if( argc!=2 ){
850
- if( argc != 3 ){
851
- return Th_WrongNumArgs(interp, "http -async url ?payload?");
852
- }
853
- blob_append(&payload, argv[2], -1);
854
- type = "POST";
855
- }else{
856
- type = "GET";
857
- }
858
- params = strrchr(argv[1], '?');
859
- url_parse(argv[1], 0);
860
- if( g.urlIsSsh || g.urlIsFile ){
861
- Th_ErrorMessage(interp, "url must be http:// or https://", 0, 0);
862
- return TH_ERROR;
863
- }
864
- regexp = db_get("th1-uri-regexp", 0);
865
- if( regexp && regexp[0] ){
866
- const char * zErr = re_compile(&pRe, regexp, 0);
867
- if( zErr ){
868
- Th_SetResult(interp, zErr, -1);
869
- return TH_ERROR;
870
- }
871
- }
872
- if (!pRe || !re_match(pRe, (const unsigned char *)argv[1], -1) ){
873
- Th_SetResult(interp, "url not allowed", -1);
874
- return TH_ERROR;
875
- }
876
- re_free(pRe);
877
- if( transport_open() ){
878
- Th_ErrorMessage(interp, transport_errmsg(), 0, 0);
879
- return TH_ERROR;
880
- }
881
- blob_zero(&hdr);
882
- i = strlen(g.urlPath);
883
- if( (i>0) && (params!=argv[1]) ){
884
- zSep = "";
885
- }else{
886
- zSep = "/";
887
- }
888
- blob_appendf(&hdr, "%s %s%s%s HTTP/1.0\r\n", type, zSep, g.urlPath, params?params:"");
889
- if( g.urlProxyAuth ){
890
- blob_appendf(&hdr, "Proxy-Authorization: %s\r\n", g.urlProxyAuth);
891
- }
892
- if( g.urlPasswd && g.urlUser && g.urlPasswd[0]=='#' ){
893
- char *zCredentials = mprintf("%s:%s", g.urlUser, &g.urlPasswd[1]);
894
- char *zEncoded = encode64(zCredentials, -1);
895
- blob_appendf(&hdr, "Authorization: Basic %s\r\n", zEncoded);
896
- fossil_free(zEncoded);
897
- fossil_free(zCredentials);
898
- }
899
- blob_appendf(&hdr, "Host: %s\r\n", g.urlHostname);
900
- blob_appendf(&hdr, "User-Agent: Fossil/" RELEASE_VERSION
901
- " (" MANIFEST_DATE " " MANIFEST_VERSION ")\r\n");
902
- blob_appendf(&hdr, "Content-Type: text/plain\r\n");
903
- blob_appendf(&hdr, "Content-Length: %d\r\n\r\n", blob_size(&payload));
904
-
905
- transport_send(&hdr);
906
- transport_send(&payload);
907
- transport_close();
908
- g.urlProtocol=0; /* Make sure the url is not re-used. */
909
- Th_SetResult(interp, "", -1);
910
- return TH_OK;
911
-}
912
-
913824
static int commonScriptRan = 0;
914825
915826
/*
916827
** Run the specified TH1 script, if any, and returns 1 on error.
917828
*/
@@ -944,11 +855,10 @@
944855
result = run_script("xfer-common-script", 0);
945856
if( result == TH_ERROR ){
946857
/* Error message is left in th interpreter. */
947858
commonScriptRan = 2;
948859
}
949
- Th_CreateCommand(g.interp, "http", httpCmd, 0, 0);
950860
}
951861
return result;
952862
}
953863
954864
/*
955865
--- src/xfer.c
+++ src/xfer.c
@@ -819,99 +819,10 @@
819 */
820 static void server_private_xfer_not_authorized(void){
821 @ error not\sauthorized\sto\ssync\sprivate\scontent
822 }
823
824 /*
825 ** TH command: http -async URL ?PAYLOAD?
826 **
827 ** Do a HTTP request to specified URL. If PAYLOAD is present
828 ** it will be POST'ed as text/plain, otherwise it's a GET
829 */
830 static int httpCmd(
831 Th_Interp *interp,
832 void *p,
833 int argc,
834 const char **argv,
835 int *argl
836 ){
837 int i;
838 const char *zSep, *type, *regexp, *params;
839 Blob hdr, payload;
840 ReCompiled *pRe = 0;
841
842 if( (argc>1) && strcmp(argv[1],"-async") ){
843 Th_ErrorMessage(interp, "synchronous http requests not yet implemented", 0, 0);
844 return TH_ERROR;
845 }
846 ++argv;
847 --argc;
848 blob_zero(&payload);
849 if( argc!=2 ){
850 if( argc != 3 ){
851 return Th_WrongNumArgs(interp, "http -async url ?payload?");
852 }
853 blob_append(&payload, argv[2], -1);
854 type = "POST";
855 }else{
856 type = "GET";
857 }
858 params = strrchr(argv[1], '?');
859 url_parse(argv[1], 0);
860 if( g.urlIsSsh || g.urlIsFile ){
861 Th_ErrorMessage(interp, "url must be http:// or https://", 0, 0);
862 return TH_ERROR;
863 }
864 regexp = db_get("th1-uri-regexp", 0);
865 if( regexp && regexp[0] ){
866 const char * zErr = re_compile(&pRe, regexp, 0);
867 if( zErr ){
868 Th_SetResult(interp, zErr, -1);
869 return TH_ERROR;
870 }
871 }
872 if (!pRe || !re_match(pRe, (const unsigned char *)argv[1], -1) ){
873 Th_SetResult(interp, "url not allowed", -1);
874 return TH_ERROR;
875 }
876 re_free(pRe);
877 if( transport_open() ){
878 Th_ErrorMessage(interp, transport_errmsg(), 0, 0);
879 return TH_ERROR;
880 }
881 blob_zero(&hdr);
882 i = strlen(g.urlPath);
883 if( (i>0) && (params!=argv[1]) ){
884 zSep = "";
885 }else{
886 zSep = "/";
887 }
888 blob_appendf(&hdr, "%s %s%s%s HTTP/1.0\r\n", type, zSep, g.urlPath, params?params:"");
889 if( g.urlProxyAuth ){
890 blob_appendf(&hdr, "Proxy-Authorization: %s\r\n", g.urlProxyAuth);
891 }
892 if( g.urlPasswd && g.urlUser && g.urlPasswd[0]=='#' ){
893 char *zCredentials = mprintf("%s:%s", g.urlUser, &g.urlPasswd[1]);
894 char *zEncoded = encode64(zCredentials, -1);
895 blob_appendf(&hdr, "Authorization: Basic %s\r\n", zEncoded);
896 fossil_free(zEncoded);
897 fossil_free(zCredentials);
898 }
899 blob_appendf(&hdr, "Host: %s\r\n", g.urlHostname);
900 blob_appendf(&hdr, "User-Agent: Fossil/" RELEASE_VERSION
901 " (" MANIFEST_DATE " " MANIFEST_VERSION ")\r\n");
902 blob_appendf(&hdr, "Content-Type: text/plain\r\n");
903 blob_appendf(&hdr, "Content-Length: %d\r\n\r\n", blob_size(&payload));
904
905 transport_send(&hdr);
906 transport_send(&payload);
907 transport_close();
908 g.urlProtocol=0; /* Make sure the url is not re-used. */
909 Th_SetResult(interp, "", -1);
910 return TH_OK;
911 }
912
913 static int commonScriptRan = 0;
914
915 /*
916 ** Run the specified TH1 script, if any, and returns 1 on error.
917 */
@@ -944,11 +855,10 @@
944 result = run_script("xfer-common-script", 0);
945 if( result == TH_ERROR ){
946 /* Error message is left in th interpreter. */
947 commonScriptRan = 2;
948 }
949 Th_CreateCommand(g.interp, "http", httpCmd, 0, 0);
950 }
951 return result;
952 }
953
954 /*
955
--- src/xfer.c
+++ src/xfer.c
@@ -819,99 +819,10 @@
819 */
820 static void server_private_xfer_not_authorized(void){
821 @ error not\sauthorized\sto\ssync\sprivate\scontent
822 }
823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824 static int commonScriptRan = 0;
825
826 /*
827 ** Run the specified TH1 script, if any, and returns 1 on error.
828 */
@@ -944,11 +855,10 @@
855 result = run_script("xfer-common-script", 0);
856 if( result == TH_ERROR ){
857 /* Error message is left in th interpreter. */
858 commonScriptRan = 2;
859 }
 
860 }
861 return result;
862 }
863
864 /*
865

Keyboard Shortcuts

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