]> err.no Git - sope/blobdiff - sope-appserver/NGObjWeb/WOContext.m
hotfix
[sope] / sope-appserver / NGObjWeb / WOContext.m
index f6c5c0de9663f8d09d49dce7b1c7238664716d5c..1b08905714c11bcb38125af53de59abf160276f1 100644 (file)
@@ -1,5 +1,6 @@
 /*
-  Copyright (C) 2000-2005 SKYRIX Software AG
+  Copyright (C) 2000-2006 SKYRIX Software AG
+  Copyright (C) 2006      Helge Hess
 
   This file is part of SOPE.
 
@@ -28,7 +29,7 @@
 #include <NGObjWeb/WORequest.h>
 #include <NGObjWeb/WOResponse.h>
 #include <NGObjWeb/WOSession.h>
-#import <EOControl/EONull.h>
+#include <Foundation/NSNull.h>
 #include "WOElementID.h"
 #include "common.h"
 #include <time.h>
@@ -49,7 +50,7 @@ static Class WOAppClass = Nil;
 @implementation WOContext
 
 + (int)version {
-  return 7;
+  return 8;
 }
 
 static Class    WOContextClass       = Nil;
@@ -98,12 +99,17 @@ static NSString *WOApplicationSuffix = nil;
 
 - (id)initWithRequest:(WORequest *)_request {
   if ((self = [super init])) {
-    unsigned char buf[24];
-    self->qpJoin = @"&";
+    char buf[24];
+    self->qpJoin = @"&amp;";
     
-    sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL), (int)self);
+    sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL),
+           (unsigned int)(unsigned long)self);
     self->ctxId = [[NSString alloc] initWithCString:buf];
     
+    /* per default close tags in XML style */
+    self->wcFlags.xmlStyleEmptyElements = 1;
+    self->wcFlags.allowEmptyAttributes  = 0;
+    
     self->elementID = [[WOElementID alloc] init];
     self->awakeComponents = [[NSMutableSet alloc] initWithCapacity:64];
     
@@ -284,19 +290,26 @@ static NSString *WOApplicationSuffix = nil;
   [super dealloc];
 }
 
+/* session */
+
 - (void)setSession:(WOSession *)_session {
   ASSIGN(self->session, _session);
 }
+- (void)setNewSession:(WOSession *)_session {
+  [self setSession:_session];
+  self->wcFlags.hasNewSession = 1;
+}
 
-- (WOSession *)session {
-  // in WO4 -session creates a new session if none is associated
+- (id)session {
+  /* in WO4 -session creates a new session if none is associated */
   
   if (self->session == nil) {
     [[self application] _initializeSessionInContext:self];
     
-    if (self->session == nil)
+    if (self->session == nil) {
       [self logWithFormat:@"%s: missing session for context ..",
               __PRETTY_FUNCTION__];
+    }
   }
   
   return self->session;
@@ -331,9 +344,14 @@ static NSString *WOApplicationSuffix = nil;
 - (BOOL)hasSession {
   return (self->session != nil) ? YES : NO;
 }
+- (BOOL)hasNewSession {
+  if (!self->wcFlags.hasNewSession)
+    return NO;
+  return [self hasSession];
+}
 
 - (BOOL)savePageRequired {
-  return self->savePageRequired;
+  return self->wcFlags.savePageRequired ? YES : NO;
 }
 
 /* cursors */
@@ -360,7 +378,7 @@ static NSString *WOApplicationSuffix = nil;
 
 /* components */
 
-- (WOComponent *)component {
+- (id)component {
   return (self->componentStackCount > 0)
     ? self->componentStack[self->componentStackCount - 1]
     : nil;
@@ -370,7 +388,7 @@ static NSString *WOApplicationSuffix = nil;
   [_page ensureAwakeInContext:self];
   ASSIGN(self->page, _page);
 }
-- (WOComponent *)page {
+- (id)page {
   return self->page;
 }
 
@@ -561,10 +579,10 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
 /* forms */
 
 - (void)setInForm:(BOOL)_form {
-  self->inForm = _form;
+  self->wcFlags.inForm = _form ? 1 : 0;
 }
 - (BOOL)isInForm {
-  return self->inForm;
+  return self->wcFlags.inForm ? YES : NO;
 }
 
 - (void)addActiveFormElement:(WOElement *)_formElement {
@@ -611,7 +629,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     return;
   else if (WOGetKVCGetMethod(self, _key) == NULL) {
     if (_value == nil)
-      _value = [EONull null];
+      _value = [NSNull null];
     
     if (self->variables == nil) {
       self->variables =
@@ -651,11 +669,11 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     sid = [[self session] sessionID];
   
   return [NSString stringWithFormat:
-                     @"<0x%08X[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
-                     (unsigned)self, NSStringFromClass([self class]),
+                     @"<0x%p[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
+                     self, NSStringFromClass([self class]),
                      [self  contextID],
                      [app name],
-                     sid ? sid : @"none",
+                    sid != nil ? sid : (NSString *)@"none",
                      [self  elementID],
                      [self  senderID]];
 }
@@ -706,6 +724,20 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   return self->qpJoin;
 }
 
+- (void)setGenerateXMLStyleEmptyElements:(BOOL)_flag {
+  self->wcFlags.xmlStyleEmptyElements = _flag ? 1 : 0;
+}
+- (BOOL)generateXMLStyleEmptyElements {
+  return self->wcFlags.xmlStyleEmptyElements ? YES : NO;
+}
+
+- (void)setGenerateEmptyAttributes:(BOOL)_flag {
+  self->wcFlags.allowEmptyAttributes = _flag ? 1 : 0;
+}
+- (BOOL)generateEmptyAttributes {
+  return self->wcFlags.allowEmptyAttributes ? YES : NO;
+}
+
 - (NSString *)queryStringFromDictionary:(NSDictionary *)_queryDict {
   NSEnumerator    *keys;
   NSString        *key;
@@ -714,21 +746,48 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   
   qs   = [MutableStrClass stringWithCapacity:256];
   keys = [_queryDict keyEnumerator];
-  for (isFirst = YES; (key = [keys nextObject]); ) {
-    NSString *value;
+  for (isFirst = YES; (key = [keys nextObject]) != nil; ) {
+    id value;
     
-    if (isFirst)
-      isFirst = NO;
-    else
-      [qs appendString:self->qpJoin];
+    value = [_queryDict objectForKey:key];
+
+    /* check for multi-value parameter */
+
+    if ([value isKindOfClass:[NSArray class]]) {
+      NSArray  *a = value;
+      unsigned i, count;
+      
+      for (i = 0, count = [a count]; i < count; i++) {
+       value = [a objectAtIndex:i];
+       
+       if (isFirst) isFirst = NO;
+       else [qs appendString:self->qpJoin];
+
+       // TODO: code duplication ...
+       value = ![value isNotNull] ? (NSString *)nil : [value stringValue];
+       key   = [key   stringByEscapingURL];
+       value = [value stringByEscapingURL];
+    
+       [qs appendString:key];
+       if (value != nil) {
+         [qs appendString:@"="];
+         [qs appendString:value];
+       }
+      }
+      continue;
+    }
+
+    /* regular, single-value parameter */
     
-    value = [[_queryDict objectForKey:key] stringValue];
+    if (isFirst) isFirst = NO;
+    else [qs appendString:self->qpJoin];
     
+    value = ![value isNotNull] ? (NSString *)nil : [value stringValue];
     key   = [key   stringByEscapingURL];
     value = [value stringByEscapingURL];
     
     [qs appendString:key];
-    if (value) {
+    if (value != nil) {
       [qs appendString:@"="];
       [qs appendString:value];
     }
@@ -766,11 +825,20 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   //   26% -urlWithRequestHandler...
   //   21% -elementID (was 40% !! :-)
   //   ~20% mutable string ops
+  
+  /* 
+     This makes the request handler save the page in the session at the
+     end of the request (only necessary if the page generates URLs which
+     refer the context).
+  */
+  self->wcFlags.savePageRequired = 1;
+  
   if (newCURLStyle) {
+    // TODO: who uses that? Its not enabled per default
+    // TODO: what does this do?
     NSMutableString *qs;
     NSString *p;
   
-    self->savePageRequired = YES;
     qs = [MutableStrClass stringWithCapacity:64];
   
     [qs appendString:WORequestValueSenderID];
@@ -801,8 +869,19 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     static NSMutableString *url = nil; // THREAD
     static IMP addStr = NULL;
     NSString *s;
-  
-    self->savePageRequired = YES;
+    NSString *coRqhKey;
+
+    coRqhKey = [WOAppClass componentRequestHandlerKey];
+
+    /* 
+       Optimization: use relative URL if the request already was a component
+                     action (with a valid session)
+    */
+    if (!self->wcFlags.hasNewSession) {
+      if ([[self->request requestHandlerKey] isEqualToString:coRqhKey])
+       return [self->elementID elementID];
+    }
+    
     if (url == nil) {
       url = [[MutableStrClass alloc] initWithCapacity:256];
       addStr = [url methodForSelector:@selector(appendString:)];
@@ -810,7 +889,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     }
     else
       [url setString:@"/"];
-  
+    
     /*
       Note: component actions *always* require sessions to be able to locate
       the request component !
@@ -819,8 +898,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     addStr(url, @selector(appendString:), @"/");
     addStr(url, @selector(appendString:), [self->elementID elementID]);
   
-    s = [self urlWithRequestHandlerKey:
-               [WOAppClass componentRequestHandlerKey]
+    s = [self urlWithRequestHandlerKey:coRqhKey
              path:url queryString:nil];
     return s;
   }
@@ -960,19 +1038,23 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
 /* languages for resource lookup (non-WO) */
 
 - (NSArray *)resourceLookupLanguages {
-  return [self hasSession] ? [[self session] languages]
-                           : [[self request] browserLanguages];
+  return [self hasSession] 
+    ? [[self session] languages]
+    : [[self request] browserLanguages];
 }
 
 
 /* DeprecatedMethodsInWO4 */
 
-- (WOApplication *)application {
+- (id)application {
   if (self->application == nil)
     self->application = [WOAppClass application];
 
-  if (self->application == nil)
-    NSLog(@"%s: missing application for context %@", __PRETTY_FUNCTION__, self);
+  if (self->application == nil) {
+    [self logWithFormat:
+           @"%s: missing application for context %@", 
+           __PRETTY_FUNCTION__, self];
+  }
   
   return self->application;
 }
@@ -1025,7 +1107,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     self->cycleContext = [[NSMutableArray alloc] initWithCapacity:8];
   
   /* add to cursor stack */
-  [self->cycleContext addObject:(_obj ? _obj : [NSNull null])];
+  [self->cycleContext addObject:(_obj != nil ? _obj : (id)[NSNull null])];
   
   /* set active cursor */
   [self setObject:_obj forKey:@"_"];