Started Multipart-Support. multi
authorHeiko Schlittermann <hs@schlittermann.de>
Wed, 28 Jan 2009 06:23:51 +0100
branchmulti
changeset 7 83dad3fbc95e
parent 6 d63acb8bbdbd
child 9 9166982f8b19
Started Multipart-Support.
hlog
--- a/hlog	Tue Jan 27 23:47:51 2009 +0100
+++ b/hlog	Wed Jan 28 06:23:51 2009 +0100
@@ -33,6 +33,8 @@
 sub handle_request($);
 sub date1123(;$);
 sub http($@);
+sub multipart_start($);
+sub multipart_part(@);
 sub bad_request();
 
 MAIN: {
@@ -113,12 +115,16 @@
     seek($file{fh}, -($lines + 1) * $file{avglen}, 2);
     $file{fh}->getline;
 
-    $client->print(http "200 OK" => join "", <<__EOF, $file{fh}->getlines);
+    $client->print(
+        http "200 OK",
+	multipart_start "abcde");
+
+    $client->print(multipart_part <<__EOF, $file{fh}->getlines);
 # Proof of concept ;-)
 # see https://keller.schlittermann.de/hg/hlog
 #
 # FILE:    @{[sprintf "%s", $file{name}]}
-# LENGTH:  @{[sprintf "%5d", $file{size}]}
+# SIZE  :  @{[sprintf "%5d", $file{size}]}
 # LINES:   @{[sprintf "%5d approx", $file{lines}]}
 # LENGTH:  @{[sprintf "%5d approx", $file{avglen}]}
 # DISPLAY: @{[sprintf "%5d approx", $lines]}
@@ -156,18 +162,46 @@
     return %r;
 }
 
+{
+    my $boundary;
+sub multipart_start(@) {
+    $boundary = "--" . shift;
+    $boundary .= "\n" unless $boundary =~ /\n$/;
+
+    $boundary =~ /^--(.*)\n$/;
+
+    return "Content-Type: multipart/x-mixed-replace; boundary=\"$1\"\n",
+	    "\n";
+}
+
+sub multipart_part(@) {
+    return $boundary,
+	$_[0] =~ /^<!DOCTYPE HTML/i 
+	? "Content-Type: text/tml\n" 
+	: "Content-Type: text/plain\n",
+	"\n", @_, $boundary;
+}
+
+}
+
 sub http($@) {
     my $code = shift;
     my $date = date1123();
 
-    my $type = $_[0] =~ /^<!DOCTYPE HTML/ ? "text/html" : "text/plain";
+
+    if ($_[0] !~ /^Content-Type: /)  {
+	if ($_[0] =~ /^<!DOCTYPE HTML/i) {
+	    unshift @_, "Content-Type: text/html\n\n";
+	}
+	else {
+	    unshift @_, "Content-Type: text/plain\n\n";
+	}
+    }
 
     return <<__EOF, @_;
 HTTP/1.1 $code
 Date: $date
 Connection: close
-Content-Type: $type
-
 __EOF
 }