]> err.no Git - sope/blobdiff - sope-core/NGStreams/NGCTextStream.m
properly write HTTP headers on MacOS 10.5
[sope] / sope-core / NGStreams / NGCTextStream.m
index 3e40a3df85c4df3c225268f6cf328a2fe6e95fe2..cb9bee41f5747a0d9ae9fbba2d432aa34afe1e5e 100644 (file)
@@ -1,6 +1,6 @@
 /*
-  Copyright (C) 2000-2007 SKYRIX Software AG
-  Copyright (C) 2007      Helge Hess
+  Copyright (C) 2000-2008 SKYRIX Software AG
+  Copyright (C) 2007-2008 Helge Hess
 
   This file is part of SOPE.
 
@@ -110,8 +110,11 @@ static void _flushAtExit(void) {
     [self release];
     return nil;
   }
-  if ((self = [super init])) {
+  if ((self = [super init]) != nil) {
     self->source = [_stream retain];
+    
+    /* On MacOS 10.5 this is per default 30 aka MacOS Roman */
+    self->encoding = [NSString defaultCStringEncoding];
 
 #ifdef __APPLE__
     //#  warning no selector caching on MacOSX ...
@@ -256,7 +259,8 @@ static void _flushAtExit(void) {
                                          autorelease];
 }
 
-// NGTextOutputStream
+
+/* NGTextOutputStream */
 
 - (BOOL)writeCharacter:(unichar)_character {
   unsigned char c;
@@ -266,16 +270,16 @@ static void _flushAtExit(void) {
     // character is not in range of maximum system encoding
     [NSException raise:@"NGCTextStreamEncodingException"
                  format:
-                   @"called writeCharacter: with character code (0x%X) exceeding"
-                   @" the maximum system character code (0x%X)",
+                   @"called writeCharacter: with character code (0x%X)"
+                   @" exceeding the maximum system character code (0x%X)",
                    _character, ((sizeof(unsigned char) * 256) - 1)];
   }
 
   c = _character;
 
-  if (writeBytes) {
-    res = writeBytes(self->source, @selector(writeBytes:count:),
-                     &c, sizeof(unsigned char));
+  if (self->writeBytes != NULL) {
+    res = self->writeBytes(self->source, @selector(writeBytes:count:),
+                          &c, sizeof(unsigned char));
   }
   else
     res = [self->source writeBytes:&c count:sizeof(unsigned char)];
@@ -293,22 +297,30 @@ static void _flushAtExit(void) {
   unsigned toGo;
 
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
-  NSStringEncoding enc = [NSString defaultCStringEncoding];
-  
-  // TBD: better use -maximumLengthOf... and then search for \0
-  if ((toGo = [_string lengthOfBytesUsingEncoding:enc]) == 0)
+  if ((toGo = [_string maximumLengthOfBytesUsingEncoding:self->encoding]) == 0)
     return YES;
-
-  buf = str = calloc(toGo + 1, sizeof(unsigned char));
-  [_string getCString:(char *)str maxLength:toGo encoding:enc];
+  
+  buf = str = calloc(toGo + 2, sizeof(unsigned char));
+  // Note: maxLength INCLUDES the 0-terminator. And -getCString: does
+  //       0-terminate the buffer
+  if (![_string getCString:(char *)str maxLength:(toGo + 1)
+               encoding:self->encoding]) {
+    NSLog(@"ERROR(%s): failed to extract cString in defaultCStringEncoding(%i)"
+         @" from NSString: '%@'\n", __PRETTY_FUNCTION__,
+         self->encoding, _string);
+    return NO;
+  }
+  
+  // we need to update the *real* (not the max) length
+  toGo = strlen((char *)str);
 #else
   if ((toGo = [_string cStringLength]) == 0)
     return YES;
 
-  buf = str = calloc(toGo + 1, sizeof(unsigned char));
+  buf = str = calloc(toGo + 2, sizeof(unsigned char));
   [_string getCString:(char *)str];
-#endif
   str[toGo] = '\0';
+#endif
   
   NS_DURING {
     while (toGo > 0) {