git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jakub Narebski <jnareb@gmail.com>
Date
Dec 3, 2010, 19:25 UTC
Message-ID
<1291404335-25541-3-git-send-email-jnareb@gmail.com>
In-Reply-To
<1291404335-25541-1-git-send-email-jnareb@gmail.com>
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.

[jn: modified reset_output to silence 'gitweb.perl: Name "main::STDOUT_REAL"
 used only once: possible typo at ../gitweb/gitweb.perl line 1130.' warning]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
Reminder: it uses
  open STDOUT, ">&", \*STDOUT_REAL;
rather than
  open(STDOUT,">&STDOUT_REAL");
to silence spurious (invalid) warning
  gitweb.perl: Name "main::STDOUT_REAL" used only once: possible typo
  at ../gitweb/gitweb.perl line 1130.
This version is identical to 
  [PATCHv7.1 2/4] gitweb: add output buffering and associated functions
  Message-Id: <1288607092-31458-3-git-send-email-jnareb@gmail.com>
  http://article.gmane.org/gmane.comp.version-control.git/160475
 gitweb/gitweb.perl |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index cfa511c..cae0e34 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -39,6 +39,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;
@@ -1134,6 +1137,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();
 
@@ -1145,7 +1167,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;
@@ -3655,6 +3680,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
Previous: Jakub NarebskiNext: Jakub Narebski
Message 3 of 6 in “[PATCHv7.4 0/4] Gitweb caching v7.2”
  1. Jakub NarebskiDec 3, 2010
  2. 1/4 gitweb: Prepare for splitting gitwebJakub Narebski, Dec 3, 2010
  3. 2/4 gitweb: add output buffering and associated functionsJakub Narebski, Dec 3, 2010
  4. 3/4 gitweb: File based caching layer (from git.kernel.org)Jakub Narebski, Dec 3, 2010
  5. 4/4 gitweb: Minimal testing of gitweb cachingJakub Narebski, Dec 3, 2010
  6. Jakub NarebskiDec 3, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.