Make RFC 2231-encoded attachment names work. Patch approved by Ingo (the issues he had were corrected).

RFC 2231 defines an enhanced encoding for attachment filenames, and thunderbird
apparently implemented this encoding. RFC 2231 allows one field to be split 
across multiple numbered entries of the form fieldname*0=....; 
fieldname*1=...; fieldname*2=...; or fieldname*0=....; fieldname*1=...; fieldname*2=...; 
All these entries first need to be concatenated to form the full value of the field. 

Here's a real-life example:

--------------060807060608070200030605
Content-Type: application/vnd.ms-excel;
 name*0*=ISO-8859-15''%41%46%42%D6%20%42%65%73%65%74%7A%75%6E%67%73%6C%69;
 name*1*=%73%74%65%20%53%74%61%6E%64%20%32%30%30%36%2D%31%32%2D%31%39%2E;
 name*2*=%78%6C%73
 Content-Transfer-Encoding: base64
 Content-Disposition: inline;
 filename*0*=ISO-8859-15''%41%46%42%D6%20%42%65%73%65%74%7A%75%6E%67%73%6C;
 filename*1*=%69%73%74%65%20%53%74%61%6E%64%20%32%30%30%36%2D%31%32%2D%31;
 filename*2*=%39%2E%78%6C%73


As a result, KMail shows %39%2E%78%6C%73 as the file name in both the message 
preview panel as well as in the mime tree. 

With this patch, KMail correctly shows the proper filename.
The patch adds one static method to collect all parts of rfc 2231-encoded 
params into one single string. That method is then used in two different 
places for the name and the filename props.



One minor problem remains, though: As the mime library does not have support 
for rfc2231 encoded attachments, the message is not shown with the attachment 
icon in the message list. 


BUG:108091
BUG:113100



svn path=/branches/KDE/3.5/kdepim/; revision=622052
wilder-work
Reinhold Kainhofer 19 years ago
parent 3d6201425d
commit 60b62b6f42
  1. 12
      kmmessage.cpp
  2. 36
      kmmsgbase.cpp
  3. 5
      kmmsgbase.h
  4. 57
      kmmsgpart.cpp

@ -2866,6 +2866,15 @@ DwBodyPart * KMMessage::findDwBodyPart( int type, int subtype ) const
void applyHeadersToMessagePart( DwHeaders& headers, KMMessagePart* aPart )
{
// TODO: Instead of manually implementing RFC2231 header encoding (i.e.
// possibly multiple values given as paramname*0=..; parmaname*1=..;...
// or par as paramname*0*=..; parmaname*1*=..;..., which should be
// concatenated), use a generic method to decode the header, using RFC
// 2047 or 2231, or whatever future RFC might be appropriate!
// Right now, some fields are decoded, while others are not. E.g.
// Content-Disposition is not decoded here, rather only on demand in
// KMMsgPart::fileName; Name however is decoded here and stored as a
// decoded String in KMMsgPart...
// Content-type
QCString additionalCTypeParams;
if (headers.HasContentType())
@ -2880,8 +2889,7 @@ void applyHeadersToMessagePart( DwHeaders& headers, KMMessagePart* aPart )
if (!qstricmp(param->Attribute().c_str(), "charset"))
aPart->setCharset(QCString(param->Value().c_str()).lower());
else if (!qstrnicmp(param->Attribute().c_str(), "name*", 5))
aPart->setName(KMMsgBase::decodeRFC2231String(
param->Value().c_str()));
aPart->setName(KMMsgBase::decodeRFC2231String(KMMsgBase::extractRFC2231HeaderField( param->Value().c_str(), "name" )));
else {
additionalCTypeParams += ';';
additionalCTypeParams += param->AsString().c_str();

@ -948,6 +948,42 @@ QString KMMsgBase::decodeRFC2231String(const QCString& _str)
return codec->toUnicode( st );
}
QCString KMMsgBase::extractRFC2231HeaderField( const QCString &aStr, const QCString &field )
{
int n=-1;
QCString str;
bool found = false;
while ( n<=0 || found ) {
QString pattern( field );
pattern += "[*]"; // match a literal * after the fieldname, as defined by RFC 2231
if ( n>=0 ) { // If n<0, check for fieldname*=..., otherwise for fieldname*n=
pattern += QString::number(n) + "[*]?";
}
pattern += "=";
QRegExp fnamePart( pattern, FALSE );
int startPart = fnamePart.search( aStr );
int endPart;
found = ( startPart >= 0 );
if ( found ) {
startPart += fnamePart.matchedLength();
// Quoted values end at the ending quote
if ( aStr[startPart] == '"' ) {
startPart++; // the double quote isn't part of the filename
endPart = aStr.find('"', startPart) - 1;
}
else {
endPart = aStr.find(';', startPart) - 1;
}
if (endPart < 0)
endPart = 32767;
str += aStr.mid( startPart, endPart-startPart+1).stripWhiteSpace();
}
n++;
}
return str;
}
QString KMMsgBase::base64EncodedMD5( const QString & s, bool utf8 ) {
if (s.stripWhiteSpace().isEmpty()) return "";
if ( utf8 )

@ -355,6 +355,11 @@ public:
/** Decode given string as described in RFC2231 */
static QString decodeRFC2231String(const QCString& aStr);
/** Extract a given param from the RFC2231-encoded header field, in particular
concatenate possibly multiple entries, which are given as paramname*0=..;
paramname*1=..; ... or paramname*0*=..; paramname*1*=..; ... and return
their value as one string. That string will still be encoded */
static QCString extractRFC2231HeaderField( const QCString &aStr, const QCString &field );
/** Calculate the base64 encoded md5sum (sans the trailing equal
signs). If @p utf8 is false, uses QString::latin1() to calculate

@ -504,41 +504,44 @@ void KMMessagePart::setContentDescription(const QString &aStr)
//-----------------------------------------------------------------------------
QString KMMessagePart::fileName(void) const
{
bool bRFC2231encoded = false;
// search the start of the filename
int startOfFilename = mContentDisposition.find("filename*=", 0, FALSE);
if (startOfFilename >= 0) {
bRFC2231encoded = true;
startOfFilename += 10;
}
else {
startOfFilename = mContentDisposition.find("filename=", 0, FALSE);
QCString str;
// Allow for multiple filname*0, filename*1, ... params (defined by RFC 2231)
// in the Content-Disposision
if ( mContentDisposition.contains( "filename*", FALSE ) ) {
// It's RFC 2231 encoded, so extract the file name with the 2231 method
str = KMMsgBase::extractRFC2231HeaderField( mContentDisposition, "filename" );
return KMMsgBase::decodeRFC2231String(str);
} else {
// Standard RFC 2047-encoded
// search the start of the filename
int startOfFilename = mContentDisposition.find("filename=", 0, FALSE);
if (startOfFilename < 0)
return QString::null;
startOfFilename += 9;
}
// search the end of the filename
int endOfFilename;
if ( '"' == mContentDisposition[startOfFilename] ) {
startOfFilename++; // the double quote isn't part of the filename
endOfFilename = mContentDisposition.find('"', startOfFilename) - 1;
}
else {
endOfFilename = mContentDisposition.find(';', startOfFilename) - 1;
}
if (endOfFilename < 0)
endOfFilename = 32767;
// search the end of the filename
int endOfFilename;
if ( '"' == mContentDisposition[startOfFilename] ) {
startOfFilename++; // the double quote isn't part of the filename
endOfFilename = mContentDisposition.find('"', startOfFilename) - 1;
}
else {
endOfFilename = mContentDisposition.find(';', startOfFilename) - 1;
}
if (endOfFilename < 0)
endOfFilename = 32767;
const QCString str = mContentDisposition.mid(startOfFilename,
const QCString str = mContentDisposition.mid(startOfFilename,
endOfFilename-startOfFilename+1)
.stripWhiteSpace();
if (bRFC2231encoded)
return KMMsgBase::decodeRFC2231String(str);
else
return KMMsgBase::decodeRFC2047String(str, charset());
}
return QString::null;
}

Loading…
Cancel
Save