Fossil SCM

When --httptrace is used with "fossil ui" or "fossil server", create log files containing the text of each HTTP request.

drh 2012-11-24 23:07 UTC ticket-enhancements
Commit 6f3d328fbf91fdee2291d1ce556bfb1947543193
1 file changed +32 -1
+32 -1
--- src/cgi.c
+++ src/cgi.c
@@ -783,10 +783,38 @@
783783
}
784784
fossil_exit( g.isHTTP ? 0 : 1);
785785
}
786786
#endif /* FOSSIL_ENABLE_JSON */
787787
788
+/*
789
+** Log HTTP traffic to a file. Begin the log on first use. Close the log
790
+** when the argument is NULL.
791
+*/
792
+void cgi_trace(const char *z){
793
+ static FILE *pLog = 0;
794
+ if( g.fHttpTrace==0 ) return;
795
+ if( z==0 ){
796
+ if( pLog ) fclose(pLog);
797
+ pLog = 0;
798
+ return;
799
+ }
800
+ if( pLog==0 ){
801
+ char zFile[50];
802
+ unsigned r;
803
+ sqlite3_randomness(sizeof(r), &r);
804
+ sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
805
+ pLog = fopen(zFile, "w");
806
+ if( pLog ){
807
+ fprintf(stderr, "# open log on %s\n", zFile);
808
+ }else{
809
+ fprintf(stderr, "# failed to open %s\n", zFile);
810
+ return;
811
+ }
812
+ }
813
+ fputs(z, pLog);
814
+}
815
+
788816
789817
/*
790818
** Initialize the query parameter database. Information is pulled from
791819
** the QUERY_STRING environment variable (if it exists), from standard
792820
** input if there is POST data, and from HTTP_COOKIE.
@@ -825,10 +853,11 @@
825853
if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
826854
|| strncmp(zType,"multipart/form-data",19)==0 ){
827855
z = fossil_malloc( len+1 );
828856
len = fread(z, 1, len, g.httpIn);
829857
z[len] = 0;
858
+ cgi_trace(z);
830859
if( zType[0]=='a' ){
831860
add_param_list(z, '&');
832861
}else{
833862
process_multipart_form_data(z, len);
834863
}
@@ -1145,10 +1174,11 @@
11451174
char zLine[2000]; /* A single line of input. */
11461175
g.fullHttpReply = 1;
11471176
if( fgets(zLine, sizeof(zLine),g.httpIn)==0 ){
11481177
malformed_request();
11491178
}
1179
+ cgi_trace(zLine);
11501180
zToken = extract_token(zLine, &z);
11511181
if( zToken==0 ){
11521182
malformed_request();
11531183
}
11541184
if( fossil_strcmp(zToken,"GET")!=0 && fossil_strcmp(zToken,"POST")!=0
@@ -1181,10 +1211,11 @@
11811211
*/
11821212
while( fgets(zLine,sizeof(zLine),g.httpIn) ){
11831213
char *zFieldName;
11841214
char *zVal;
11851215
1216
+ cgi_trace(zLine);
11861217
zFieldName = extract_token(zLine,&zVal);
11871218
if( zFieldName==0 || *zFieldName==0 ) break;
11881219
while( fossil_isspace(*zVal) ){ zVal++; }
11891220
i = strlen(zVal);
11901221
while( i>0 && fossil_isspace(zVal[i-1]) ){ i--; }
@@ -1212,12 +1243,12 @@
12121243
#endif
12131244
}else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
12141245
cgi_setenv("HTTP_USER_AGENT", zVal);
12151246
}
12161247
}
1217
-
12181248
cgi_init();
1249
+ cgi_trace(0);
12191250
}
12201251
12211252
#if INTERFACE
12221253
/*
12231254
** Bitmap values for the flags parameter to cgi_http_server().
12241255
--- src/cgi.c
+++ src/cgi.c
@@ -783,10 +783,38 @@
783 }
784 fossil_exit( g.isHTTP ? 0 : 1);
785 }
786 #endif /* FOSSIL_ENABLE_JSON */
787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788
789 /*
790 ** Initialize the query parameter database. Information is pulled from
791 ** the QUERY_STRING environment variable (if it exists), from standard
792 ** input if there is POST data, and from HTTP_COOKIE.
@@ -825,10 +853,11 @@
825 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
826 || strncmp(zType,"multipart/form-data",19)==0 ){
827 z = fossil_malloc( len+1 );
828 len = fread(z, 1, len, g.httpIn);
829 z[len] = 0;
 
830 if( zType[0]=='a' ){
831 add_param_list(z, '&');
832 }else{
833 process_multipart_form_data(z, len);
834 }
@@ -1145,10 +1174,11 @@
1145 char zLine[2000]; /* A single line of input. */
1146 g.fullHttpReply = 1;
1147 if( fgets(zLine, sizeof(zLine),g.httpIn)==0 ){
1148 malformed_request();
1149 }
 
1150 zToken = extract_token(zLine, &z);
1151 if( zToken==0 ){
1152 malformed_request();
1153 }
1154 if( fossil_strcmp(zToken,"GET")!=0 && fossil_strcmp(zToken,"POST")!=0
@@ -1181,10 +1211,11 @@
1181 */
1182 while( fgets(zLine,sizeof(zLine),g.httpIn) ){
1183 char *zFieldName;
1184 char *zVal;
1185
 
1186 zFieldName = extract_token(zLine,&zVal);
1187 if( zFieldName==0 || *zFieldName==0 ) break;
1188 while( fossil_isspace(*zVal) ){ zVal++; }
1189 i = strlen(zVal);
1190 while( i>0 && fossil_isspace(zVal[i-1]) ){ i--; }
@@ -1212,12 +1243,12 @@
1212 #endif
1213 }else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
1214 cgi_setenv("HTTP_USER_AGENT", zVal);
1215 }
1216 }
1217
1218 cgi_init();
 
1219 }
1220
1221 #if INTERFACE
1222 /*
1223 ** Bitmap values for the flags parameter to cgi_http_server().
1224
--- src/cgi.c
+++ src/cgi.c
@@ -783,10 +783,38 @@
783 }
784 fossil_exit( g.isHTTP ? 0 : 1);
785 }
786 #endif /* FOSSIL_ENABLE_JSON */
787
788 /*
789 ** Log HTTP traffic to a file. Begin the log on first use. Close the log
790 ** when the argument is NULL.
791 */
792 void cgi_trace(const char *z){
793 static FILE *pLog = 0;
794 if( g.fHttpTrace==0 ) return;
795 if( z==0 ){
796 if( pLog ) fclose(pLog);
797 pLog = 0;
798 return;
799 }
800 if( pLog==0 ){
801 char zFile[50];
802 unsigned r;
803 sqlite3_randomness(sizeof(r), &r);
804 sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
805 pLog = fopen(zFile, "w");
806 if( pLog ){
807 fprintf(stderr, "# open log on %s\n", zFile);
808 }else{
809 fprintf(stderr, "# failed to open %s\n", zFile);
810 return;
811 }
812 }
813 fputs(z, pLog);
814 }
815
816
817 /*
818 ** Initialize the query parameter database. Information is pulled from
819 ** the QUERY_STRING environment variable (if it exists), from standard
820 ** input if there is POST data, and from HTTP_COOKIE.
@@ -825,10 +853,11 @@
853 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
854 || strncmp(zType,"multipart/form-data",19)==0 ){
855 z = fossil_malloc( len+1 );
856 len = fread(z, 1, len, g.httpIn);
857 z[len] = 0;
858 cgi_trace(z);
859 if( zType[0]=='a' ){
860 add_param_list(z, '&');
861 }else{
862 process_multipart_form_data(z, len);
863 }
@@ -1145,10 +1174,11 @@
1174 char zLine[2000]; /* A single line of input. */
1175 g.fullHttpReply = 1;
1176 if( fgets(zLine, sizeof(zLine),g.httpIn)==0 ){
1177 malformed_request();
1178 }
1179 cgi_trace(zLine);
1180 zToken = extract_token(zLine, &z);
1181 if( zToken==0 ){
1182 malformed_request();
1183 }
1184 if( fossil_strcmp(zToken,"GET")!=0 && fossil_strcmp(zToken,"POST")!=0
@@ -1181,10 +1211,11 @@
1211 */
1212 while( fgets(zLine,sizeof(zLine),g.httpIn) ){
1213 char *zFieldName;
1214 char *zVal;
1215
1216 cgi_trace(zLine);
1217 zFieldName = extract_token(zLine,&zVal);
1218 if( zFieldName==0 || *zFieldName==0 ) break;
1219 while( fossil_isspace(*zVal) ){ zVal++; }
1220 i = strlen(zVal);
1221 while( i>0 && fossil_isspace(zVal[i-1]) ){ i--; }
@@ -1212,12 +1243,12 @@
1243 #endif
1244 }else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
1245 cgi_setenv("HTTP_USER_AGENT", zVal);
1246 }
1247 }
 
1248 cgi_init();
1249 cgi_trace(0);
1250 }
1251
1252 #if INTERFACE
1253 /*
1254 ** Bitmap values for the flags parameter to cgi_http_server().
1255

Keyboard Shortcuts

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