From ad198bec7800213ac6e7a40585805398f83767d2 Mon Sep 17 00:00:00 2001 From: CaS Date: Wed, 4 May 2005 17:19:11 +0000 Subject: [PATCH] Try to be more tolerant handling text/xml git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@21189 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++++ Source/Additions/GSMime.m | 66 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 26968f0ea..2f743fe9a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2005-05-04 Richard Frith-Macdonald + + * Source/Additions/GSMime.m: If given content of type text/xml without + a charset, try to get the charset from the encoding="..." part of the + header. + 2005-04-29 Richard Frith-Macdonald * Source/GSHTTPURLHandle.m: Remove self as an observer of our file diff --git a/Source/Additions/GSMime.m b/Source/Additions/GSMime.m index f3e40d063..1746f7fdb 100644 --- a/Source/Additions/GSMime.m +++ b/Source/Additions/GSMime.m @@ -2300,8 +2300,58 @@ NSDebugMLLog(@"GSMime", @"Header parsed - %@", info); else { NSString *charset; + NSString *sub; charset = [typeInfo parameterForKey: @"charset"]; + + /* + * For 'text/xml' content where charset is not supplied, + * we may be able to get the information from the + * header encoding="..." section. + */ + if (charset == nil + && (sub = [typeInfo objectForKey: @"Subtype"]) != nil + && [sub isEqualToString: @"xml"] == YES) + { + const unsigned char *ptr = [data bytes]; + unsigned int length = [data length]; + unsigned int i; + + for (i = 0; i < length - 10 && i < 500; i++) + { + if (memcmp(ptr + i, "encoding=\"", 10) == 0) + { + unsigned start = i + 10; + unsigned end = start; + + for (i = start; i < length; i++) + { + if (ptr[i] == '"') + { + end = i; + break; + } + } + if (end > start) + { + NSData *d; + NSString *c; + + d = [NSData dataWithBytes: ptr + start + length: end - start]; + c = [NSString alloc]; + c = [c initWithData: d + encoding: NSASCIIStringEncoding]; + if (c != nil) + { + charset = [c lowercaseString]; + } + } + break; + } + } + } + stringEncoding = [documentClass encodingFromCharset: charset]; } @@ -2311,8 +2361,15 @@ NSDebugMLLog(@"GSMime", @"Header parsed - %@", info); string = [NSStringClass allocWithZone: NSDefaultMallocZone()]; string = [string initWithData: data encoding: stringEncoding]; - [document setContent: string]; - RELEASE(string); + if (string == nil) + { + [document setContent: data]; // Can't make string + } + else + { + [document setContent: string]; + RELEASE(string); + } } else { @@ -4786,8 +4843,9 @@ static NSCharacterSet *tokenSet = nil; else { [NSException raise: NSInvalidArgumentException - format: @"[%@ -%@] passed bad content", - NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + format: @"[%@ -%@] passed bad content: %@", + NSStringFromClass([self class]), NSStringFromSelector(_cmd), + newContent]; } }