[PATCHv7.1 2/4] gitweb: add output buffering and associated functions

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Jakub Narebski
Date: Monday, November 1, 2010 - 3:24 am

From: John 'Warthog9' Hawley <warthog9@eaglescrag.net>

This adds output buffering for gitweb, mainly in preparation for
caching support.  This is a dramatic change to how caching was being
done, mainly in passing around the variable manually and such.

This centrally flips the entire STDOUT to a variable, which after the
completion of the run, flips it back and does a print on the resulting
data.

This should save on the previous 10K line patch (or so) that adds more
explicit output passing.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
Commit message unchanged.

I have modified reset_output to silence the following warning from t9500:

  gitweb.perl: Name "main::STDOUT_REAL" used only once: possible typo
  at ../gitweb/gitweb.perl line 1130.

Here is the interdiff for gitweb.perl from 2e5c355 (J.H. patch in 'pu'):

  diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
  index b2b0a23..91e274f 100755
  --- a/gitweb/gitweb.perl
  +++ b/gitweb/gitweb.perl
  @@ -32,7 +39,7 @@ BEGIN {
   our $version = "++GIT_VERSION++";
   
   # Output buffer variable
  -$output = "";
  +our $output = "";
   
   our ($my_url, $my_uri, $base_url, $path_info, $home_link);
   sub evaluate_uri {
  @@ -1143,13 +1138,12 @@ sub change_output {
   }
   
   sub reset_output {
  -	# This basically takes STDOUT_REAL and puts it back as STDOUTl
  -	open(STDOUT,">&STDOUT_REAL");
  +	# This basically takes STDOUT_REAL and puts it back as STDOUT
  +	open STDOUT, ">&", \*STDOUT_REAL;
   }
   

 gitweb/gitweb.perl |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index e4c08ba..91e274f 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -38,6 +38,9 @@ BEGIN {
 
 our $version = "++GIT_VERSION++";
 
+# Output buffer variable
+our $output = "";
+
 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
 sub evaluate_uri {
 	our $cgi;
@@ -1120,6 +1123,25 @@ sub evaluate_argv {
 	);
 }
 
+sub change_output {
+	our $output;
+
+	# Trap the 'proper' STDOUT to STDOUT_REAL for things like error messages and such
+	open(STDOUT_REAL,">&STDOUT") or die "Unable to capture STDOUT $!\n";
+
+	# Close STDOUT, so that it isn't being used anymore.
+	close STDOUT;
+
+	# Trap STDOUT to the $output variable, which is what I was using in the original
+	# patch anyway.
+	open(STDOUT,">", \$output) || die "Unable to open STDOUT: $!"; #open STDOUT handle to use $var
+}
+
+sub reset_output {
+	# This basically takes STDOUT_REAL and puts it back as STDOUT
+	open STDOUT, ">&", \*STDOUT_REAL;
+}
+
 sub run {
 	evaluate_argv();
 
@@ -1131,7 +1153,10 @@ sub run {
 		$pre_dispatch_hook->()
 			if $pre_dispatch_hook;
 
+		change_output();
 		run_request();
+		reset_output();
+		print $output;
 
 		$post_dispatch_hook->()
 			if $post_dispatch_hook;
@@ -3640,6 +3665,10 @@ sub die_error {
 		500 => '500 Internal Server Error',
 		503 => '503 Service Unavailable',
 	);
+	# Reset the output so that we are actually going to STDOUT as opposed
+	# to buffering the output.
+	reset_output();
+
 	git_header_html($http_responses{$status}, undef, %opts);
 	print <<EOF;
 <div class="page_body">
-- 
1.7.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 0/3] Gitweb caching v7, John 'Warthog9' Hawley, (Wed Oct 27, 5:42 pm)
[PATCH 1/3] gitweb: Add option to force version match, John 'Warthog9' Hawley, (Wed Oct 27, 5:42 pm)
[PATCH 2/3] gitweb: add output buffering and associated fu ..., John 'Warthog9' Hawley, (Wed Oct 27, 5:42 pm)
[PATCH 3/3] gitweb: File based caching layer (from git.ker ..., John 'Warthog9' Hawley, (Wed Oct 27, 5:42 pm)
Re: [PATCH 1/3] gitweb: Add option to force version match, Jakub Narebski, (Thu Oct 28, 2:52 am)
Re: [PATCH 1/3] gitweb: Add option to force version match, Junio C Hamano, (Thu Oct 28, 3:08 pm)
Re: [PATCH 0/3] Gitweb caching v7, Junio C Hamano, (Thu Oct 28, 3:29 pm)
Re: [PATCH 0/3] Gitweb caching v7, Junio C Hamano, (Fri Oct 29, 3:25 pm)
Re: [PATCH 0/3] Gitweb caching v7, Jakub Narebski, (Sat Oct 30, 1:58 am)
Re: [PATCH 0/3] Gitweb caching v7, Junio C Hamano, (Sat Oct 30, 9:24 pm)
Re: [PATCH 0/3] Gitweb caching v7, Jakub Narebski, (Sun Oct 31, 2:21 am)
[PATCHv7.1 0/4] Gitweb caching v7.1, Jakub Narebski, (Mon Nov 1, 3:24 am)
[PATCHv7.1 1/4] gitweb: Prepare for splitting gitweb, Jakub Narebski, (Mon Nov 1, 3:24 am)
[PATCHv7.1 2/4] gitweb: add output buffering and associate ..., Jakub Narebski, (Mon Nov 1, 3:24 am)
[PATCHv7.1 4/4] gitweb: Minimal testing of gitweb caching, Jakub Narebski, (Mon Nov 1, 3:24 am)
[PATCHv7.1b 1/4] gitweb: Prepare for splitting gitweb, Jakub Narebski, (Mon Nov 1, 11:50 am)
[RFC/PATCHv7.2 0/4] Gitweb caching v7.2, Jakub Narebski, (Fri Nov 12, 4:35 pm)
[PATCHv7.2 1/4] gitweb: Prepare for splitting gitweb, Jakub Narebski, (Fri Nov 12, 4:41 pm)
[PATCHv7.2 4/4] gitweb: Minimal testing of gitweb caching, Jakub Narebski, (Fri Nov 12, 5:01 pm)
Re: [PATCHv7.2 1/4] gitweb: Prepare for splitting gitweb, Junio C Hamano, (Wed Nov 17, 4:10 pm)
Re: [PATCHv7.2 1/4] gitweb: Prepare for splitting gitweb, Jakub Narebski, (Thu Nov 18, 6:37 am)