]> err.no Git - sope/blobdiff - sope-appserver/NGObjWeb/WOElement.m
improved query string handling
[sope] / sope-appserver / NGObjWeb / WOElement.m
index 236483ee79c841bfb18f7e20cb4232f6fa72513c..2461122de9d8a073df17e989eb090181047fb45e 100644 (file)
@@ -150,9 +150,7 @@ static id numStrings[100];
                      NSStringFromClass([self class]), self];
 }
 
-@end /* WOElement */
-
-@implementation WOElement(QueryString)
+/* QueryString */
 
 - (NSString *)queryStringForQueryDictionary:(NSDictionary *)_queryDict
   andQueryParameters:(NSDictionary *)_paras
@@ -174,11 +172,11 @@ static id numStrings[100];
   
   isFirst = YES;
   paraKeys = [_paras allKeys];
-
+  
   /* ?style parameters */
   
   keys = [_paras keyEnumerator];
-  while ((key = [keys nextObject])) {
+  while ((key = [keys nextObject]) != nil) {
     value = [[_paras objectForKey:key] stringValueInComponent:sComponent];
     value = value ? [value stringByEscapingURL] : @"";
     key   = key   ? [key   stringByEscapingURL] : @"";
@@ -192,15 +190,40 @@ static id numStrings[100];
   }
   
   keys = [_queryDict keyEnumerator];
-  while ((key = [keys nextObject])) {
-    if(![paraKeys containsObject:key]) {
-      value = [[_queryDict objectForKey:key] stringValue];
+  while ((key = [keys nextObject]) != nil) {
+    if([paraKeys containsObject:key])
+      /* overridden by a query parameter (?abc=abc) */
+      continue;
+    
+    value = [_queryDict objectForKey:key];
+    if ([value isKindOfClass:[NSArray class]]) {
+      /* if we bind the queryDictionary to request.formValues */
+      NSArray  *values;
+      unsigned i, count;
+
+      values = (NSArray *)value;
+      for (i = 0, count = [values count]; i < count; i++) {
+       value = [values objectAtIndex:i];
+        value = [value stringValue];
+        value = value ? [value stringByEscapingURL] : @"";
+        key   = key   ? [key   stringByEscapingURL] : @"";
+        
+        if (isFirst) isFirst = NO;
+        else [str appendString:@"&"];
+       
+        [str appendString:key];
+        [str appendString:@"="];
+        [str appendString:value];
+      }
+    }
+    else {
+      value = [value stringValue];
       value = value ? [value stringByEscapingURL] : @"";
       key   = key   ? [key   stringByEscapingURL] : @"";
       
       if (isFirst) isFirst = NO;
       else [str appendString:@"&"];
-      
+        
       [str appendString:key];
       [str appendString:@"="];
       [str appendString:value];
@@ -210,14 +233,14 @@ static id numStrings[100];
   return [str length] > 0 ? str : nil;
 }
 
-@end /* WOElement(QueryString) */
+@end /* WOElement */
 
 NGObjWeb_DECLARE id OWGetProperty(NSDictionary *_set, NSString *_name) {
-  id propValue = [_set objectForKey:_name];
-
-  if (propValue) {
-    propValue = RETAIN(propValue);
-    [(id)_set removeObjectForKey:_name];
+  register id propValue;
+  
+  if ((propValue = [_set objectForKey:_name]) != nil) {
+    propValue = [propValue retain];
+    [(NSMutableDictionary *)_set removeObjectForKey:_name];
   }
   return propValue;
 }