/*
- Copyright (C) 2000-2005 SKYRIX Software AG
+ Copyright (C) 2000-2007 SKYRIX Software AG
+ Copyright (C) 2007 Helge Hess
This file is part of SOPE.
#include "NGLdapConnection.h"
#include "NGLdapEntry.h"
#include "EOQualifier+LDAP.h"
+#include <NGExtensions/NSString+misc.h>
#include "common.h"
#include <string.h>
-static inline BOOL isUrlAlpha(unsigned char _c) {
- return
- (((_c >= 'a') && (_c <= 'z')) ||
- ((_c >= 'A') && (_c <= 'Z')))
- ? YES : NO;
-}
-static inline BOOL isUrlDigit(unsigned char _c) {
- return ((_c >= '0') && (_c <= '9')) ? YES : NO;
-}
-static inline BOOL isUrlSafeChar(unsigned char _c) {
- switch (_c) {
- case '$': case '-': case '_': case '@':
- case '.': case '&': case '+':
- return YES;
-
- default:
- return NO;
- }
-}
-static inline BOOL isUrlExtraChar(unsigned char _c) {
- switch (_c) {
- case '!': case '*': case '"': case '\'':
- case '|': case ',':
- return YES;
- }
- return NO;
-}
-static inline BOOL isUrlEscapeChar(unsigned char _c) {
- return (_c == '%') ? YES : NO;
-}
-static inline BOOL isUrlReservedChar(unsigned char _c) {
- switch (_c) {
- case '=': case ';': case '/':
- case '#': case '?': case ':':
- case ' ':
- return YES;
- }
- return NO;
-}
-
-static inline BOOL isUrlXalpha(unsigned char _c) {
- if (isUrlAlpha(_c)) return YES;
- if (isUrlDigit(_c)) return YES;
- if (isUrlSafeChar(_c)) return YES;
- if (isUrlExtraChar(_c)) return YES;
- if (isUrlEscapeChar(_c)) return YES;
- return NO;
-}
-
-static inline BOOL isUrlHexChar(unsigned char _c) {
- if (isUrlDigit(_c))
- return YES;
- if ((_c >= 'a') && (_c <= 'f'))
- return YES;
- if ((_c >= 'A') && (_c <= 'F'))
- return YES;
- return NO;
-}
-
-static inline BOOL isUrlAlphaNum(unsigned char _c) {
- return (isUrlAlpha(_c) || isUrlDigit(_c)) ? YES : NO;
-}
-
-static inline BOOL isToBeEscaped(unsigned char _c) {
- return (isUrlAlphaNum(_c) || (_c == '_')) ? NO : YES;
-}
-
-static BOOL NGContainsUrlInvalidCharacters(const unsigned char *_buffer) {
- while (*_buffer) {
- if (isToBeEscaped(*_buffer))
- return YES;
- _buffer++;
- }
- return NO;
-}
-static void NGEscapeUrlBuffer
-(const unsigned char *_source, unsigned char *_dest) {
- register const unsigned char *src = (void*)_source;
- while (*src) {
- //if (*src == ' ') { // a ' ' becomes a '+'
- // *_dest = '+'; _dest++;
- //}
- if (!isToBeEscaped(*src)) {
- *_dest = *src;
- _dest++;
- }
- else { // any other char is escaped ..
- *_dest = '%'; _dest++;
- sprintf((char *)_dest, "%02X", (unsigned)*src);
- _dest += 2;
- }
- src++;
- }
- *_dest = '\0';
-}
-static NSString *NGEscapeUrlString(NSString *_source) {
- unsigned len;
- char *cstr;
- NSString *s;
-
- if ((len = [_source cStringLength]) == 0)
- return _source;
-
- cstr = malloc(len + 3);
- [_source getCString:cstr];
- cstr[len] = '\0';
-
- if (NGContainsUrlInvalidCharacters((unsigned char *)cstr)) {
- // needs to be escaped ?
- char *buffer = NULL;
-
- buffer = NGMallocAtomic([_source cStringLength] * 3 + 2);
- NGEscapeUrlBuffer((unsigned char *)cstr, (unsigned char *)buffer);
-
- s = [[[NSString alloc]
- initWithCStringNoCopy:buffer
- length:strlen(buffer)
- freeWhenDone:YES] autorelease];
- }
- else
- s = [[_source copy] autorelease];
-
- free(cstr);
- return s;
-}
-
@implementation NGLdapURL
+ (id)ldapURLWithString:(NSString *)_url {
- (NSString *)urlString {
NSMutableString *s;
- NSString *is;
-
+ NSString *r;
+
s = [[NSMutableString alloc] initWithCapacity:200];
[s appendString:@"ldap://"];
if (self->port > 0) [s appendFormat:@":%i", self->port];
[s appendString:@"/"];
- is = self->base;
- is = NGEscapeUrlString(is);
- [s appendString:is];
+ [s appendString:[self->base stringByEscapingURL]];
- if ((self->attributes != nil) || (self->scope != -1) || (self->filter != nil)){
+ if ((self->attributes != nil) || (self->scope!=-1) || (self->filter != nil)){
+ NSString *is;
[s appendString:@"?"];
is = [self->attributes componentsJoinedByString:@","];
- is = NGEscapeUrlString(is);
- [s appendString:is];
+ [s appendString:[is stringByEscapingURL]];
}
if ((self->scope != -1) || (self->filter != nil)) {
[s appendString:@"?"];
break;
}
}
- if (self->filter) {
+ if ([self->filter isNotEmpty]) {
[s appendString:@"?"];
- is = self->filter;
- is = NGEscapeUrlString(is);
- [s appendString:is];
+ [s appendString:[self->filter stringByEscapingURL]];
}
- is = [s copy];
- [s release];
- return [is autorelease];
+ r = [[s copy] autorelease];
+ [s release]; s = nil;
+ return r;
}
/* NSCopying */