00001 /***************************************************************************** 00002 * This file is copyright 2005 Jim Kent, but license is hereby 00003 * granted for all use - public, private or commercial. 00004 *****************************************************************************/ 00005 /* mime.h - parses MIME messages, especially from a cgi from a multipart web form */ 00006 00007 #ifndef HASH_H 00008 #include "hash.h" 00009 #endif 00010 00011 #define MIMEBUFSIZE 32*1024 /* size of buffer for mime input */ 00012 00013 struct mimePart 00014 /* structure for an element of a MIME (multipart) message */ 00015 { 00016 struct mimePart *next; /* next (sibling) if is part of multipart */ 00017 struct hash *hdr; /* hash of part headers */ 00018 off_t size; /* determines if local mem or saved to tempfile */ 00019 /* only one of the next 3 pointers will be non-null, and that is the type */ 00020 char* data; /* if size< MAXPARTSIZE and does not contain null */ 00021 char* fileName; /* if size>=MAXPARTSIZE or data contains null */ 00022 boolean binary; /* if contains 0 chars, cannot store as a c-string */ 00023 struct mimePart *multi;/* points to head of child list if itself contains multiparts */ 00024 }; 00025 00026 struct mimeBuf 00027 /* structure for buffering a MIME message during parsing */ 00028 { 00029 int d; /* descriptor (file,socket,etc) */ 00030 char buf[MIMEBUFSIZE]; /* actual buffer */ 00031 char *i; /* index into buffer, current location */ 00032 char *eop; /* end of part or -1 */ 00033 char *boundary; /* boundary pattern for marking end of mime part */ 00034 int blen; /* boundary pattern length (strlen) */ 00035 char *eod; /* end of data = eoi-(blen-1) */ 00036 char *eoi; /* end of input or -1 */ 00037 char *eom; /* end of memory just buf+MIMEBUFSIZE */ 00038 }; 00039 00040 char *getMimeHeaderMainVal(char *header); 00041 /* Parse a typical mime header line returning the first 00042 * main value up to whitespace, punctuation, or end. 00043 * freeMem the returned string when done */ 00044 00045 char *getMimeHeaderFieldVal(char *header, char *field); 00046 /* Parse a typical mime header line looking for field= 00047 * and return the value which may be quoted. 00048 * freeMem the returned string when done */ 00049 00050 struct mimeBuf * initMimeBuf(int d); 00051 /* d is a descriptor for a file or socket or some other descriptor 00052 that the MIME input can be read from. 00053 Initializes the mimeBuf structure. */ 00054 00055 struct mimePart *parseMultiParts(struct mimeBuf *b, char *altHeader); 00056 /* This is a recursive function. It parses multipart MIME messages. 00057 Data that are binary or too large will be saved in mimePart->filename 00058 otherwise saved as a c-string in mimePart->data. If multipart, 00059 then first child is mimePart->child, subsequent sibs are in child->next. 00060 altHeader is a string of headers that can be fed in if the headers have 00061 already been read off the stream by an earlier process, i.e. apache. 00062 */
1.5.2