/*
- Copyright (C) 2000-2003 SKYRIX Software AG
+ Copyright (C) 2000-2004 SKYRIX Software AG
- This file is part of OGo
+ This file is part of OpenGroupware.org.
OGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
-// $Id$
-#import "common.h"
-#import "NGHttpBodyParser.h"
-#import "NGUrlFormCoder.h"
+#include "NGHttpBodyParser.h"
+#include "NGUrlFormCoder.h"
+#include "common.h"
@implementation NGFormUrlBodyParser
-- (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data delegate:(id)_d {
+- (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data
+ delegate:(id)_d
+{
const char *bytes;
unsigned len;
id body;
+ [self logWithFormat:@"parse part %@ data: %@", _part, _data];
+
len = [_data length];
bytes = [_data bytes];
-
- // cut off spaces at the end
+
+ /* cut off spaces at the end */
while (len > 0) {
if ((bytes[len - 1] == '\r') || (bytes[len - 1] == '\n'))
len--;
break;
}
if (len == 0) return nil;
-
+
body = NGDecodeUrlFormParameters(bytes, len);
return [body autorelease];
}
return YES;
}
-- (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data delegate:(id)_d {
+- (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data
+ delegate:(id)_d
+{
NGMimeMultipartBody *body;
body = [super parseBodyOfPart:_part data:_data delegate:_d];
/*
- Copyright (C) 2000-2003 SKYRIX Software AG
+ Copyright (C) 2000-2004 SKYRIX Software AG
- This file is part of OGo
+ This file is part of OpenGroupware.org.
OGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
-// $Id$
#ifndef __NGHttp_NGUrlFormCoder_H__
#define __NGHttp_NGUrlFormCoder_H__
The _buffer parameter starts with the string after the '?' in a URI, that is,
the buffer is _not_ the complete URI.
The function returns a retained hashmap.
+
+ TODO: should be moved to NGExtensions
*/
-NGHashMap *NGDecodeUrlFormParameters(const char *_buffer, unsigned _len);
+NGHashMap *NGDecodeUrlFormParameters(const unsigned char *_buf, unsigned _len);
#if 0 /* do not use, use NGExtensions/NSString+misc.h ... */
@interface NSString(FormURLCoding)
/*
- Copyright (C) 2000-2003 SKYRIX Software AG
+ Copyright (C) 2000-2004 SKYRIX Software AG
- This file is part of OGo
+ This file is part of OpenGroupware.org.
OGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
-// $Id$
-#import "common.h"
-#import "NGUrlFormCoder.h"
+#include "NGUrlFormCoder.h"
+#include "common.h"
-static inline BOOL isURLSafeChar(unsigned char _c) {
- if ((_c > 64) && (_c < 91))
- return YES;
+static BOOL debugDecoding = NO;
- if ((_c > 96) && (_c < 123))
- return YES;
-
- if (_c == 95)
- return YES;
-
- if ((_c > 47) && (_c < 58))
- return YES;
-
- return NO;
-}
-
-static inline int _valueOfHexChar(unsigned char _c) {
+static __inline__ int _valueOfHexChar(unsigned char _c) {
switch (_c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
}
}
-static inline unsigned _unescapeUrl(const char *_src, unsigned _len, char *_dest) {
+static __inline__ unsigned
+_unescapeUrl(const char *_src, unsigned _len, char *_dest)
+{
register unsigned i, i2;
for (i = 0, i2 = 0; i < _len; i++, i2++) {
return i2; // return unescaped length
}
-NGHashMap *NGDecodeUrlFormParameters(const char *_buffer, unsigned _len) {
- Class StrClass = [NSString class];
+static Class StrClass = Nil;
+
+static __inline__ NSString *urlStringFromBuffer(const unsigned char *buffer,
+ unsigned len)
+{
+ // TODO: we assume ISO-Latin-1/Unicode encoding, which might be wrong
+#if LIB_FOUNDATION_LIBRARY
+ return [[StrClass alloc] initWithCString:buffer length:len];
+#else
+ register signed int i;
+ unichar *s;
+ NSString *value;
+
+ s = malloc((len + 2) * sizeof(unichar));
+ for (i = len - 1; i >= 0; i--)
+ s[i] = buffer[i];
+ value = [[StrClass alloc] initWithCharacters:s length:len];
+ if (s != NULL) free(s);
+
+ if (debugDecoding) {
+ NSLog(@"decoded data len %d value (len=%d): %@",
+ len, [value length], value);
+ }
+ return value;
+#endif
+}
+
+NGHashMap *NGDecodeUrlFormParameters(const unsigned char *_buffer,
+ unsigned _len)
+{
NGMutableHashMap *dict = nil;
unsigned pos = 0;
if (_len == 0) return nil;
-
+
+ if (StrClass == Nil) StrClass = [NSString class];
dict = [[NGMutableHashMap alloc] initWithCapacity:16];
do {
pos++;
len = _unescapeUrl(&(_buffer[tmp]), (pos - tmp), buffer);
- if (len > 0) {
- key = [[StrClass allocWithZone:[dict zone]]
- initWithCString:buffer length:len];
- }
- else
- key = @"";
-
- //NSLog(@"read key %@", key);
-
+ key = len > 0 ? urlStringFromBuffer(buffer, len) : @"";
+
if (pos < _len) { // value pending
NSCAssert(_buffer[pos] == '=', @"invalid parser state ..");
pos++; // skip '='
pos++;
}
- len = _unescapeUrl(&(_buffer[tmp]), (pos - tmp), buffer);
-
- if (len > 0) {
- value = [[StrClass allocWithZone:[dict zone]]
- initWithCString:buffer length:len];
- }
- else
- value = @"";
-
- //NSLog(@"read value: %@", value);
+ len = _unescapeUrl(&(_buffer[tmp]), (pos - tmp), buffer);
+ value = len > 0 ? urlStringFromBuffer(buffer, len) : @"";
// skip '&'
if (_buffer[pos] == '&' || _buffer[pos] == '?') pos++;
if (key)
[dict addObject:value forKey:key];
- RELEASE(key); key = nil;
- RELEASE(value); value = nil;
+ [key release]; key = nil;
+ [value release]; value = nil;
}
while (pos < _len);
@implementation NSString(FormURLCoding)
- (NSString *)stringByApplyingURLEncoding {
-#if 1
/* NGExtensions/NSString+misc.h */
+ NSLog(@"Note: Called deprecated -stringByApplyingURLEncoding method "
+ @"(use -stringByEscapingURL instead)", __PRETTY_FUNCTION__);
return [self stringByEscapingURL];
-#else
- char *buf, *encBuf;
- unsigned clen, i, j;
-
- if ((clen = [self cStringLength]) == 0)
- return self;
-
- buf = malloc(clen + 1);
- [self getCString:buf]; buf[clen] = '\0';
-
- encBuf = malloc(clen * 3 + 1);
-
- for (i = 0, j = 0; i < clen; i++) {
- register unsigned char c = buf[i];
-
- if (isURLSafeChar(c)) {
- encBuf[j] = c;
- j++;
- }
- else if (c == ' ') {
- encBuf[j] = '+';
- j++;
- }
- else {
- sprintf(&(encBuf[j]), "%%%02X", (int)c);
- j += 3;
- }
- }
- return [NSString stringWithCString:encBuf length:j];
- /* was buggy, does not release encbuf .. */
-#endif
}
@end /* NSString(FormURLCoding) */