[Rt-commit] r7743 - in rt/branches/3.7-EXPERIMENTAL: .

ruz at bestpractical.com ruz at bestpractical.com
Wed May 2 11:58:46 EDT 2007


Author: ruz
Date: Wed May  2 11:58:45 2007
New Revision: 7743

Modified:
   rt/branches/3.7-EXPERIMENTAL/   (props changed)
   rt/branches/3.7-EXPERIMENTAL/bin/rt-mailgate.in

Log:
 r5204 at cubic-pc:  cubic | 2007-05-02 19:57:10 +0400
 * mailgate with fixed memory footprint


Modified: rt/branches/3.7-EXPERIMENTAL/bin/rt-mailgate.in
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/bin/rt-mailgate.in	(original)
+++ rt/branches/3.7-EXPERIMENTAL/bin/rt-mailgate.in	Wed May  2 11:58:45 2007
@@ -57,8 +57,11 @@
 
 use Getopt::Long;
 use LWP::UserAgent;
+use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
+$DYNAMIC_FILE_UPLOAD = 1;
 
 use constant EX_TEMPFAIL => 75;
+use constant BUFFER_SIZE => 8192;
 
 my %opts;
 GetOptions( \%opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s", "timeout=i" );
@@ -84,13 +87,6 @@
     SessionType => 'REST',    # Surpress login box
 );
 
-# Read the message in from STDIN
-$args{'message'} = do { local (@ARGV, $/); <> };
-unless ( $args{message} =~ /\S/ ) {
-    print STDERR "$0: no message passed on STDIN!\n";
-    exit 0;
-}
-
 if ( ($opts{'extension'} || '') =~ /^(?:action|queue|ticket)$/i ) {
     $args{ lc $opts{'extension'} } = $ENV{'EXTENSION'};
 } elsif ( $opts{'extension'} && $ENV{'EXTENSION'} ) {
@@ -106,12 +102,23 @@
     # make a correct multiline header field,
     # with tabs in the beginning of each line
     $value =~ s/(\r*\n)/$1\t/g;
-    $args{'message'} = "X-RT-Mail-Extension: $value\n"
-        . $args{'message'};
-    print $args{'message'};
+    $opts{'headers'} .= "X-RT-Mail-Extension: $value\n";
 }
 
-# Set up cookie here.
+# Read the message in from STDIN
+my %message = write_down_message();
+unless( $message{'filename'} ) {
+    $args{'message'} = [
+        undef, '',
+        'Content-Type' => 'application/octet-stream',
+        Content => ${ $message{'content'} },
+    ];
+} else {
+    $args{'message'} = [
+        $message{'filename'}, '',
+        'Content-Type' => 'application/octet-stream',
+    ];
+}
 
 my $full_url = $opts{'url'}. "/REST/1.0/NoAuth/mail-gateway";
 print STDERR "$0: connecting to $full_url\n" if $opts{'debug'};
@@ -141,6 +148,10 @@
 
 exit;
 
+END {
+    unlink $message{'filename'} if $message{'filename'};
+}
+
 
 sub check_failure {
     my $r = shift;
@@ -165,6 +176,49 @@
     exit EX_TEMPFAIL;
 }
 
+sub write_down_message {
+    use File::Temp qw(tempfile);
+
+    my ($fh, $filename) = tempfile('rt-mailgate-XXXX', SUFIX => '.bin');
+    unless( $fh ) {
+        print STDERR "$0: couldn't create temp file, using memory\n";
+
+        my $message = \do { local (@ARGV, $/); <> };
+        unless ( $$message =~ /\S/ ) {
+            print STDERR "$0: no message passed on STDIN\n";
+            exit 0;
+        }
+        $$message = $opts{'headers'} . $$message if $opts{'headers'};
+        return ( content => $message );
+    }
+
+    binmode $fh;
+    binmode \*STDIN;
+    
+    print $fh $opts{'headers'} if $opts{'headers'};
+
+    my $buf; my $empty = 1;
+    while(1) {
+        my $status = read \*STDIN, $buf, BUFFER_SIZE;
+        unless ( defined $status ) {
+            print STDERR "$0: couldn't read message: $!\n";
+            exit EX_TEMPFAIL;
+        } elsif ( !$status ) {
+            last;
+        }
+        $empty = 0 if $buf =~ /\S/;
+        print $fh $buf;
+    };
+    close $fh;
+
+    if ( $empty ) {
+        print STDERR "$0: no message passed on STDIN\n";
+        exit 0;
+    }
+    print STDERR "$0: temp file is '$filename'\n" if $opts{'debug'};
+    return (filename => $filename);
+}
+
 
 =head1 SYNOPSIS
 


More information about the Rt-commit mailing list