/*
- 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.
#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>
@implementation WOContext
+ (int)version {
- return 7;
+ return 8;
}
static Class WOContextClass = Nil;
- (id)initWithRequest:(WORequest *)_request {
if ((self = [super init])) {
- unsigned char buf[24];
- self->qpJoin = @"&";
+ char buf[24];
+ self->qpJoin = @"&";
- 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];
[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;
- (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 */
/* components */
-- (WOComponent *)component {
+- (id)component {
return (self->componentStackCount > 0)
? self->componentStack[self->componentStackCount - 1]
: nil;
[_page ensureAwakeInContext:self];
ASSIGN(self->page, _page);
}
-- (WOComponent *)page {
+- (id)page {
return self->page;
}
/* 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 {
return;
else if (WOGetKVCGetMethod(self, _key) == NULL) {
if (_value == nil)
- _value = [EONull null];
+ _value = [NSNull null];
if (self->variables == nil) {
self->variables =
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]];
}
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;
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];
}
// 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];
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:)];
}
else
[url setString:@"/"];
-
+
/*
Note: component actions *always* require sessions to be able to locate
the request 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;
}
/* 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;
}
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:@"_"];