Subject: Wrapping MIME around an appledouble file

From: "Chris Garrigues" <[email-masked]>
Date: Thu, 15 Feb 2001 11:07:25 -0600

I need to grab a file saved on a server using netatalk from a perl CGI and 
transmit it to the user.

So far, all my attempts do is to crash netscape with an error of type 3.  This 
makes it kinda hard to debug.

Has anybody done this?  Is there anything special I have to do to the data?

Maybe I just have a dumb typo or thinko.  here's my code fragment:

	    open(FILE, "<$filename") 
		or die "Couldn't read $filename: $!";
	    open(RESOURCEFILE, "<$resourcefilename") 
		or die "Couldn't read $resourcefilename: $!";
	    print "Content-Type: multipart/appledouble; boundary=mac-part\n";
	    print "\n";
	    print "--mac-part\n";
	    print "Content-Type: application/applefile; name=\"$filepart\"\n";
	    print "Content-Transfer-Encoding: 8bit\n";
	    print "\n";
	    while (<RESOURCEFILE>) {
		print $_;
	    }
	    close(RESOURCEFILE);
	    print "\n";
	    print "--mac-part\n";
	    print "Content-Type: application/octet-stream\n";
	    print "Content-Transfer-Encoding: 8bit\n";
	    print "\n";
	    while (<FILE>) {
		print $_;
	    }
	    close(FILE);
	    print "\n";
	    print "--mac-part--\n";

Any help would be appreciated.

Chris

-- 
Chris Garrigues                 http://www.DeepEddy.Com/~cwg/
virCIO                          http://www.virCIO.Com
4314 Avenue C                   
Austin, TX  78751-3709		+1 512 374 0500

  My email address is an experiment in SPAM elimination.  For an
  explanation of what we're doing, see http://www.DeepEddy.Com/tms.html 

    Nobody ever got fired for buying Microsoft,
      but they could get fired for relying on Microsoft.

---

From: Bob Rogers <[email-masked]>
Date: Fri, 16 Feb 2001 12:08:39 -0500

From: "Chris Garrigues" <[email-masked]>
   Date: Thu, 15 Feb 2001 11:07:25 -0600

   So far, all my attempts do is to crash netscape with an error of type
   3.  This makes it kinda hard to debug.

My guess is that the .AppleDouble part is blowing Netscape's poor little
mind, since it is actually binary data rather than 8-bit text.  I would
try sending it using base64 encoding; you can pipe it through the
attached helper app, courtesy of the vm mail reader (version 6.89,
(http://www.wonderworks.com/vm).

   Deciding how to encode the data fork might be trickier . . . base64
should be sufficient, though probably overkill in many cases.

   And then there's the problem of saving these properly on the
receiving end.  Has anybody ever heard of a MIME standard for Mac files?
Failing that, you might want to try sending a Mac file from a Mac, and
see how Netscape (or whatever) encodes the data fork (if at all).  If it
does, and the format is not too hard to generate, that might be a better
solution.

					-- Bob Rogers

/* public domain */

/*
 * arbitrary data on stdin -> BASE64 data on stdout
 *
 * UNIX's newline convention is used, i.e. one ASCII control-j (10 decimal).
 */

#include <stdio.h>

#ifdef _WIN32
#ifndef WIN32
#define WIN32
#endif
#endif

#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif

unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int
main()
{
    int cols, bits, c, char_count;

#ifdef WIN32
    _setmode( _fileno(stdin), _O_BINARY);
#endif

    char_count = 0;
    bits = 0;
    cols = 0;
    while ((c = getchar()) != EOF) {
	if (c > 255) {
	    fprintf(stderr, "encountered char > 255 (decimal %d)", c);
	    exit(1);
	}
	bits += c;
	char_count++;
	if (char_count == 3) {
	    putchar(alphabet[bits >> 18]);
	    putchar(alphabet[(bits >> 12) & 0x3f]);
	    putchar(alphabet[(bits >> 6) & 0x3f]);
	    putchar(alphabet[bits & 0x3f]);
	    cols += 4;
	    if (cols == 72) {
		putchar('\n');
		cols = 0;
	    }
	    bits = 0;
	    char_count = 0;
	} else {
	    bits <<= 8;
	}
    }
    if (char_count != 0) {
	bits <<= 16 - (8 * char_count);
	putchar(alphabet[bits >> 18]);
	putchar(alphabet[(bits >> 12) & 0x3f]);
	if (char_count == 1) {
	    putchar('=');
	    putchar('=');
	} else {
	    putchar(alphabet[(bits >> 6) & 0x3f]);
	    putchar('=');
	}
	if (cols > 0)
	  putchar('\n');
    }

    exit(0);
}

---

From: "Chris Garrigues" <[email-masked]>
Date: Tue, 20 Feb 2001 17:05:29 -0600

> From:  Bob Rogers <[email-masked]>
> Date:  Fri, 16 Feb 2001 12:08:39 -0500
>

Nice thought, but it turns out that HTTP explicitly does not support 
content-transfer-encoding and assumes 8 bit data.  When I changed it to 
base64, the mac browser simply froze.

When I tried to transfer an ordinary file with a content-transfer-encoding: 
base64 to my unix box, it simply ignored the content-transfer-encoding as one 
would expect from what I said above.

So, I'm still stuck at the same point.  Any other theories why Nutscrape 
doesn't like what I'm doing.

I'm beginning to wonder if multipart/appledouble is even supported at all.  Do 
any of the Mac based web servers allow the download of mac files, and if so 
what mime type do they use?

How would i convert these two parts into an applesingle file so I could try 
sending them that way.

Chris

---

From: Steven Karel <[email-masked]>
Date: Tue, 20 Feb 2001 18:43:42 -0500

Why not just have perl call macbinary (part of the netatalk package) and
then transmit the resulting file?

macbinary somefile
then return somefile.bin

This is the standard thing that Mac FTP clients do.

see

man macbinary

steven

---

From: "Chris Garrigues" <[email-masked]>
Date: Wed, 21 Feb 2001 09:24:25 -0600

> From:  Steven Karel <[email-masked]>
> Date:  Tue, 20 Feb 2001 18:43:42 -0500
>

That worked.  Thanks.  Annoyingly, the RPM didn't include the macbinary alias.

Also, it would have been nice if macbinary could have sent it's output to 
stdout, but anyway, my problem is solved.

Chris

---

From: Bob Rogers <[email-masked]>
Date: Wed, 21 Feb 2001 11:08:28 -0500

From: "Chris Garrigues" <[email-masked]>
   Date: Tue, 20 Feb 2001 17:05:29 -0600

   Nice thought, but it turns out that HTTP explicitly does not support 
   content-transfer-encoding and assumes 8 bit data . . .

Oops; I assumed you were mailing it (though you did say "CGI").  Glad to
hear the other technique worked out.

					-- Bob