/** GSWRequest.m - GSWeb: Class GSWRequest Copyright (C) 1999-2004 Free Software Foundation, Inc. Written by: Manuel Guesdon Date: Jan 1999 $Revision$ $Date$ $Id$ This file is part of the GNUstep Web Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ #include "config.h" RCS_ID("$Id$") #include "GSWeb.h" #include #include "GSWInputStreamData.h" #include "GSWPrivate.h" //==================================================================== @implementation GSWValueQualityHeaderPart /** Returns an array of values ordered by quality descending **/ +(NSArray*)valuesFromHeaderString:(NSString*)string { NSArray* values=nil; NSArray* valuesAndQualities=nil; int count=0; LOGObjectFnStart(); valuesAndQualities=[string componentsSeparatedByString:@","]; count=[valuesAndQualities count]; NSDebugMLog(@"string=%@",string); if (count>0) { int i=0; NSMutableArray* qvs=[NSMutableArray array]; for(i=0;i0) [qvs addObject:qv]; }; count=[qvs count]; if (count>0) { unsigned int i; //Sor oon quality desc [qvs sortUsingSelector:@selector(compareOnQualityDesc:)]; //Remove Duplicates for(i=0;i0) _quality=[qualityString floatValue]; else _quality=1; }; return self; }; -(void)dealloc { DESTROY(_value); [super dealloc]; }; -(NSString*)description { return [NSString stringWithFormat: @"<%s %p : %@: %.1f>", object_get_class_name(self), (void*)self, _value, _quality]; } -(NSString*)value { return _value; }; -(float)quality { return _quality; }; -(int)compareOnQualityDesc:(GSWValueQualityHeaderPart*)qv { float quality=[self quality]; float qvQuality=[qv quality]; if (quality>qvQuality) return NSOrderedAscending; else if (quality_method,_method); ASSIGNCOPY(clone->_uri,_uri); clone->_defaultFormValueEncoding=_defaultFormValueEncoding; clone->_formValueEncoding=_formValueEncoding; ASSIGNCOPY(clone->_formValues,_formValues); ASSIGNCOPY(clone->_uriElements,_uriElements); ASSIGNCOPY(clone->_cookie,_cookie); ASSIGNCOPY(clone->_applicationURLPrefix,_applicationURLPrefix); ASSIGNCOPY(clone->_requestHandlerPathArray,_requestHandlerPathArray); ASSIGNCOPY(clone->_browserLanguages,_browserLanguages); ASSIGNCOPY(clone->_browserAcceptedEncodings,_browserAcceptedEncodings); clone->_requestType=_requestType; clone->_isUsingWebServer=_isUsingWebServer; clone->_formValueEncodingDetectionEnabled=_formValueEncodingDetectionEnabled; clone->_applicationNumber=_applicationNumber; }; return clone; }; //-------------------------------------------------------------------- -(GSWContext*)_context { return _context; } //-------------------------------------------------------------------- -(void)_setContext:(GSWContext*)context { _context = context;//Don't retain because request is retained by context } //-------------------------------------------------------------------- // method // GET or PUT -(NSString*)method { return _method; }; //-------------------------------------------------------------------- // uri -(NSString*)uri { return (NSString*)_uri; }; //-------------------------------------------------------------------- //NDFN -(NSString*)urlProtocol { NSString* urlProtocol=[_uri urlProtocol]; if (!urlProtocol) { urlProtocol=[self headerForKey:GSWHTTPHeader_RequestScheme[GSWebNamingConv]]; if (!urlProtocol) { urlProtocol=[self headerForKey:GSWHTTPHeader_RequestScheme[GSWebNamingConvInversed]]; if (!urlProtocol) urlProtocol=GSWProtocol_HTTP; }; }; return urlProtocol; }; //-------------------------------------------------------------------- //NDFN -(NSString*)_remoteAddress { NSString* remoteAddress=nil; remoteAddress = [self headerForKey:GSWHTTPHeader_RemoteAddress[GSWebNamingConv]]; if (!remoteAddress) { remoteAddress = [self headerForKey:GSWHTTPHeader_RemoteAddress[GSWebNamingConvInversed]]; if (!remoteAddress) remoteAddress = [self headerForKey:@"remote_addr"]; }; return remoteAddress; }; //-------------------------------------------------------------------- //NDFN -(NSString*)remoteAddress { return [self _remoteAddress]; }; //-------------------------------------------------------------------- //NDFN -(NSString*)_remoteHost { NSString* remoteHost=nil; remoteHost = [self headerForKey:GSWHTTPHeader_RemoteHost[GSWebNamingConv]]; if (!remoteHost) { remoteHost = [self headerForKey:GSWHTTPHeader_RemoteHost[GSWebNamingConvInversed]]; if (!remoteHost) remoteHost = [self headerForKey:@"remote_host"]; }; return remoteHost; }; //-------------------------------------------------------------------- //NDFN -(NSString*)remoteHost { return [self _remoteHost]; }; //-------------------------------------------------------------------- //NDFN -(NSString*)_serverName { NSString* serverName=nil; if ([self _isUsingWebServer]) { serverName=[self headerForKey:GSWHTTPHeader_ServerName[GSWebNamingConv]]; if (!serverName) { serverName=[self headerForKey:GSWHTTPHeader_ServerName[GSWebNamingConvInversed]]; if (!serverName) { serverName=[self headerForKey:@"server_name"]; if (!serverName) { serverName=[self headerForKey:@"host"]; if (!serverName) ExceptionRaise(@"GSWRequest",@"No server name"); }; }; }; } else { serverName = [GSWApplication host]; } return serverName; } //-------------------------------------------------------------------- //NDFN -(NSString*)urlHost { NSString* urlHost=[_uri urlHost]; if (!urlHost) { urlHost=[self headerForKey:GSWHTTPHeader_ServerName[GSWebNamingConv]]; if (!urlHost) urlHost=[self headerForKey:GSWHTTPHeader_ServerName[GSWebNamingConvInversed]]; }; return urlHost; }; //-------------------------------------------------------------------- -(NSString*)_serverPort { NSString* serverPort=nil; if ([self _isUsingWebServer]) { serverPort = [self headerForKey:GSWHTTPHeader_ServerPort[GSWebNamingConv]]; if (!serverPort) serverPort=[self headerForKey:GSWHTTPHeader_ServerPort[GSWebNamingConvInversed]]; } else { NSArray* adaptors = [[GSWApplication application]adaptors]; if ([adaptors count]>0) serverPort = GSWIntToNSString([(GSWAdaptor*)[adaptors objectAtIndex:0] port]); } return serverPort; } //-------------------------------------------------------------------- //NDFN -(NSString*)urlPortString { NSString* urlPortString=[_uri urlPortString]; if (!urlPortString) { urlPortString=[self headerForKey:GSWHTTPHeader_ServerPort[GSWebNamingConv]]; if (!urlPortString) urlPortString=[self headerForKey:GSWHTTPHeader_ServerPort[GSWebNamingConvInversed]]; }; return urlPortString; }; //-------------------------------------------------------------------- //NDFN -(int)urlPort { int port=[_uri urlPort]; if (!port) { port=[[self headerForKey:GSWHTTPHeader_ServerPort[GSWebNamingConv]]intValue]; if (!port) port=[[self headerForKey:GSWHTTPHeader_ServerPort[GSWebNamingConvInversed]]intValue]; }; return port; }; //-------------------------------------------------------------------- //NDFN -(NSString*)urlProtocolHostPort { return [_uri urlProtocolHostPort]; }; //-------------------------------------------------------------------- //NDFN -(BOOL)isSecure { return ([[self urlProtocol] caseInsensitiveCompare:GSWProtocol_HTTPS]==NSOrderedSame); }; //-------------------------------------------------------------------- -(NSString*)userAgent { NSString* userAgent=nil; LOGObjectFnStart(); userAgent=[self headerForKey:GSWHTTPHeader_UserAgent]; LOGObjectFnStop(); return userAgent; }; //-------------------------------------------------------------------- -(NSString*)referer { NSString* referer=nil; LOGObjectFnStart(); referer=[self headerForKey:GSWHTTPHeader_Referer]; LOGObjectFnStop(); return referer; }; //-------------------------------------------------------------------- -(NSArray*)browserLanguages { //OK LOGObjectFnStart(); if (!_browserLanguages) { NSMutableArray* browserLanguages=nil; NSString* header=[self headerForKey:GSWHTTPHeader_AcceptLanguage]; NSDebugMLLog(@"requests",@"lang header:%@",header); if (header) { NSArray* languages=[GSWValueQualityHeaderPart valuesFromHeaderString:header]; if (!languages) { LOGError0(@"No languages"); }; browserLanguages=(NSMutableArray*)[GSWResourceManager GSLanguagesFromISOLanguages:languages]; NSDebugMLLog(@"requests",@"browserLanguages:%@",browserLanguages); if (browserLanguages) { //Remove Duplicates int i=0; int browserLanguagesCount=0; browserLanguages=[browserLanguages mutableCopy]; browserLanguagesCount=[browserLanguages count]; for(i=0;ii;j--) { NSString* language2=[browserLanguages objectAtIndex:j]; if ([language2 isEqual:language]) { [browserLanguages removeObjectAtIndex:j]; browserLanguagesCount--; }; }; }; }; } else { LOGError0(@"No languages header"); }; if (!browserLanguages) { LOGError0(@"No known languages"); browserLanguages=(NSMutableArray*)[NSArray array]; }; ASSIGN(_browserLanguages,browserLanguages); NSDebugMLLog(@"requests",@"browserLanguages:%@",_browserLanguages); }; LOGObjectFnStop(); return _browserLanguages; }; //-------------------------------------------------------------------- -(NSArray*)browserAcceptedEncodings { //OK LOGObjectFnStart(); if (!_browserAcceptedEncodings) { NSString* header=[self headerForKey:GSWHTTPHeader_AcceptEncoding]; NSDebugMLLog(@"requests",@"accept encoding header:%@",header); if (header) { NSArray* values=[GSWValueQualityHeaderPart valuesFromHeaderString:header]; if (!values) values=[NSArray array]; ASSIGN(_browserAcceptedEncodings,values); }; NSDebugMLLog(@"requests",@"browserAcceptedEncodings:%@",_browserAcceptedEncodings); }; LOGObjectFnStop(); return _browserAcceptedEncodings; }; //-------------------------------------------------------------------- -(NSArray*)requestHandlerPathArray { if (!_requestHandlerPathArray) { NSString* urlRequestHandlerPath=[_uri urlRequestHandlerPath]; ASSIGN(_requestHandlerPathArray, [urlRequestHandlerPath componentsSeparatedByString:@"/"]); }; return _requestHandlerPathArray; }; //-------------------------------------------------------------------- -(NSString*)description { return [NSString stringWithFormat:@"<%s %p - method=%@, uri=%@, httpVersion=%@, headers=%@, content=%@, userInfo=%@, defaultFormValueEncoding=%u, formValueEncoding=%u, formValues=%@, uriElements=%@, cookie=%@, applicationURLPrefix=%@, requestHandlerPathArray=%@, browserLanguages=%@, requestType=%d, isUsingWebServer=%s, formValueEncodingDetectionEnabled=%s, applicationNumber=%d", object_get_class_name(self), (void*)self, _method, _uri, _httpVersion, _headers, _contentData, _userInfo, _defaultFormValueEncoding, _formValueEncoding, _formValues, _uriElements, _cookie, _applicationURLPrefix, _requestHandlerPathArray, _browserLanguages, _requestType, _isUsingWebServer ? "YES" : "NO", _formValueEncodingDetectionEnabled ? "YES" : "NO", _applicationNumber]; }; @end //==================================================================== @implementation GSWRequest (GSWFormValueReporting) //-------------------------------------------------------------------- // setDefaultFormValueEncoding: -(void)setDefaultFormValueEncoding:(NSStringEncoding)encoding { _defaultFormValueEncoding=encoding; }; //-------------------------------------------------------------------- // defaultFormValueEncoding -(NSStringEncoding)defaultFormValueEncoding { return _defaultFormValueEncoding; }; //-------------------------------------------------------------------- // setFormValueEncodingDetectionEnabled: -(void)setFormValueEncodingDetectionEnabled:(BOOL)flag { _formValueEncodingDetectionEnabled=flag; }; //-------------------------------------------------------------------- // isFormValueEncodingDetectionEnabled -(BOOL)isFormValueEncodingDetectionEnabled { return _formValueEncodingDetectionEnabled; }; //-------------------------------------------------------------------- // formValueEncoding -(NSStringEncoding)formValueEncoding { return _formValueEncoding; }; //-------------------------------------------------------------------- // formValueKeys -(NSArray*)formValueKeys { NSDictionary* formValues=nil; NSArray* formValueKeys=nil; LOGObjectFnStart(); NS_DURING { formValues=[self _formValues]; } NS_HANDLER { localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException, @"GSWRequest formValueKeys"); LOGException(@"%@ (%@)",localException,[localException reason]); [localException raise]; }; NS_ENDHANDLER; formValueKeys=[formValues allKeys]; LOGObjectFnStop(); return formValueKeys; }; //-------------------------------------------------------------------- // formValuesForKey: -(NSArray*)formValuesForKey:(NSString*)key { NSArray* formValuesForKey=nil; NSDictionary* formValues=nil; LOGObjectFnStart(); NS_DURING { formValues=[self _formValues]; } NS_HANDLER { localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException,@"GSWRequest formValuesForKey"); LOGException(@"%@ (%@)",localException,[localException reason]); [localException raise]; }; NS_ENDHANDLER; formValuesForKey=[formValues objectForKey:key]; LOGObjectFnStop(); return formValuesForKey; }; //-------------------------------------------------------------------- // formValueForKey: // return id because GSWFileUpload -(id)formValueForKey:(NSString*)key { id formValue=nil; NSArray* formValuesForKey=nil; LOGObjectFnStart(); formValuesForKey=[self formValuesForKey:key]; NSAssert3(!formValuesForKey || [formValuesForKey isKindOfClass:[NSArray class]],@"formValues:%@ ForKey:%@ is not a NSArray it's a %@", formValuesForKey, key, [formValuesForKey class]); if (formValuesForKey && [formValuesForKey count]>0) formValue=[formValuesForKey objectAtIndex:0]; LOGObjectFnStop(); return formValue; }; //-------------------------------------------------------------------- -(NSString*)stringFormValueForKey:(NSString*)key { id value=nil; LOGObjectFnStart(); value=[self formValueForKey:key]; if (value && ![value isKindOfClass:[NSString class]]) value=[value description]; LOGObjectFnStop(); return value; } //-------------------------------------------------------------------- -(NSNumber*)numberFormValueForKey:(NSString*)key withFormatter:(NSNumberFormatter*)formatter { NSNumber* value=nil; NSString* stringValue=nil; LOGObjectFnStart(); stringValue=[self stringFormValueForKey:key]; if (stringValue && formatter) { NSString* errorDscr=nil; if (![formatter getObjectValue:&value forString:stringValue errorDescription:&errorDscr]) { NSLog(@"Error: %@",errorDscr); }; }; LOGObjectFnStop(); return value; } //-------------------------------------------------------------------- -(NSCalendarDate*)dateFormValueForKey:(NSString*)key withFormatter:(NSDateFormatter*)formatter { NSCalendarDate* value=nil; NSString* stringValue=nil; LOGObjectFnStart(); stringValue= [self stringFormValueForKey:key]; if (stringValue && formatter) { NSString* errorDscr=nil; if (![formatter getObjectValue:&value forString:stringValue errorDescription:&errorDscr]) { NSLog(@"Error: %@",errorDscr); }; }; LOGObjectFnStop(); return value; } //-------------------------------------------------------------------- // formValues -(NSDictionary*)formValues { NSDictionary* formValues=nil; LOGObjectFnStart(); NS_DURING { formValues=[self _formValues]; } NS_HANDLER { localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException, @"GSWRequest formValues"); LOGException(@"%@ (%@)",localException,[localException reason]); [localException raise]; }; NS_ENDHANDLER; LOGObjectFnStop(); return formValues; }; -(void)appendFormValue:(id)value forKey:(NSString*)key { LOGObjectFnStart(); if (value) { NSMutableDictionary* formValues=nil; NSMutableArray* keyValues=nil; formValues=(NSMutableDictionary*)[self _formValues]; if (formValues) { if (![formValues isKindOfClass:[NSMutableDictionary class]]) { formValues=[[formValues mutableCopy] autorelease]; ASSIGN(_formValues,formValues); }; } else { formValues=(NSMutableDictionary*)[NSMutableDictionary dictionary]; ASSIGN(_formValues,formValues); }; keyValues=[formValues objectForKey:key]; if (keyValues) { if (![keyValues isKindOfClass:[NSMutableArray class]]) { keyValues=[[formValues mutableCopy] autorelease]; [formValues setObject:keyValues forKey:key]; }; } else { keyValues=(NSMutableArray*)[NSMutableArray array]; [formValues setObject:keyValues forKey:key]; }; [keyValues addObject:value]; }; LOGObjectFnStop(); }; -(void)appendFormValues:(NSArray*)values forKey:(NSString*)key { LOGObjectFnStart(); if (values) { NSMutableDictionary* formValues=nil; NSMutableArray* keyValues=nil; formValues=(NSMutableDictionary*)[self _formValues]; if (formValues) { if (![formValues isKindOfClass:[NSMutableDictionary class]]) { formValues=[[formValues mutableCopy] autorelease]; ASSIGN(_formValues,formValues); }; } else { formValues=(NSMutableDictionary*)[NSMutableDictionary dictionary]; ASSIGN(_formValues,formValues); }; keyValues=[formValues objectForKey:key]; if (keyValues) { if (![keyValues isKindOfClass:[NSMutableArray class]]) { keyValues=[[formValues mutableCopy] autorelease]; [formValues setObject:keyValues forKey:key]; }; } else { keyValues=(NSMutableArray*)[NSMutableArray array]; [formValues setObject:keyValues forKey:key]; }; [keyValues addObjectsFromArray:values]; }; LOGObjectFnStop(); }; @end //==================================================================== @implementation GSWRequest (GSWURIElementReporting) //-------------------------------------------------------------------- // uriValueKeys -(NSArray*)uriElementKeys { NSDictionary* uriElements=nil; NSArray* uriElementKeys=nil; LOGObjectFnStart(); NS_DURING { uriElements=[self _uriElements]; } NS_HANDLER { localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException, @"GSWRequest uriElementKeys"); LOGException(@"%@ (%@)",localException,[localException reason]); [localException raise]; }; NS_ENDHANDLER; uriElementKeys=[uriElements allKeys]; LOGObjectFnStop(); return uriElementKeys; }; //-------------------------------------------------------------------- // uriElementForKey: -(NSString*)uriElementForKey:(NSString*)key { NSString* uriElement=nil; NSDictionary* uriElements=nil; LOGObjectFnStart(); NS_DURING { uriElements=[self _uriElements]; } NS_HANDLER { localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException,@"GSWRequest uriElementForKey:"); LOGException(@"%@ (%@)",localException,[localException reason]); [localException raise]; }; NS_ENDHANDLER; uriElement=[uriElements objectForKey:key]; LOGObjectFnStop(); return uriElement; }; //-------------------------------------------------------------------- // uriElements -(NSDictionary*)uriElements { NSDictionary* uriElements=nil; LOGObjectFnStart(); NS_DURING { uriElements=[self _uriElements]; } NS_HANDLER { localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException, @"GSWRequest uriElements"); LOGException(@"%@ (%@)",localException,[localException reason]); [localException raise]; }; NS_ENDHANDLER; LOGObjectFnStop(); return uriElements; }; @end //==================================================================== @implementation GSWRequest (GSWRequestTypeReporting) //-------------------------------------------------------------------- // isFromClientComponent -(BOOL)isFromClientComponent { //OK NSString* remoteInvocationPost=nil; BOOL isFromClientComponent=NO; LOGObjectFnStart(); remoteInvocationPost=[self formValueForKey:GSWFormValue_RemoteInvocationPost[GSWebNamingConv]]; isFromClientComponent=(remoteInvocationPost!=nil); LOGObjectFnStop(); return isFromClientComponent; }; @end //==================================================================== @implementation GSWRequest (Cookies) //-------------------------------------------------------------------- -(void)setCookieFromHeaders { NSDictionary* cookie=nil; NSString* cookieHeader=nil; LOGObjectFnStart(); cookieHeader=[self headerForKey:GSWHTTPHeader_Cookie]; if (cookieHeader) { NSDictionary* cookieStrings=[cookieHeader dictionaryWithSep1:@"; " withSep2:@"=" withOptionUnescape:NO]; if (cookieStrings) { NSMutableDictionary* cookieTmp=[NSMutableDictionary dictionary]; NSEnumerator* enumerator = [cookieStrings keyEnumerator]; id key; id value; NSArray* newValue; id prevValue; NSDebugMLLog(@"requests",@"enumerator=%@ cookieTmp=%@",enumerator,cookieTmp); while ((key = [enumerator nextObject])) { value=[cookieStrings objectForKey:key]; if (value) { id cookieValue=nil; int index=0; int valueCount=[value count]; for(index=0;index0) cookieValueForKey=[values objectAtIndex:0]; NSDebugMLLog(@"requests",@"cookieValueForKey:%@=%@",key,cookieValueForKey); LOGObjectFnStop(); return cookieValueForKey; }; //-------------------------------------------------------------------- // cookieValues -(NSDictionary*)cookieValues { NSDictionary* cookieValues=nil; LOGObjectFnStart(); cookieValues=[self _initCookieDictionary]; LOGObjectFnStop(); return cookieValues; }; //-------------------------------------------------------------------- // Dictionary of ccokie name / cookie value -(NSDictionary*)_initCookieDictionary { LOGObjectFnStart(); NSDebugMLLog(@"low",@"cookies=%@",_cookie); if (!_cookie) { NSString* cookieDescription=[self _cookieDescription]; NSArray* cookiesArray=[cookieDescription componentsSeparatedByString:@"; "]; NSMutableDictionary* cookies=[NSMutableDictionary dictionary]; NSString* cookieString=nil; NSArray* cookie=nil; NSString* cookieName=nil; NSString* cookieValue=nil; NSArray* cookieArrayValue=nil; NSArray* cookiePrevValue=nil; int i=0; int cookiesArrayCount=[cookiesArray count]; NSDebugMLLog(@"low",@"cookieDescription=%@",cookieDescription); NSDebugMLLog(@"low",@"cookiesArray=%@",cookiesArray); for(i=0;i0) { cookieName=[cookie objectAtIndex:0]; if (cookieCount>1) cookieValue=[cookie objectAtIndex:1]; else cookieValue=[NSString string]; cookiePrevValue=[cookies objectForKey:cookieName]; if (cookiePrevValue) cookieArrayValue=[cookiePrevValue arrayByAddingObject:cookieValue]; else cookieArrayValue=[NSArray arrayWithObject:cookieValue]; [cookies setObject:cookieArrayValue forKey:cookieName]; }; }; ASSIGN(_cookie,[NSDictionary dictionaryWithDictionary:cookies]); }; LOGObjectFnStop(); return _cookie; }; //-------------------------------------------------------------------- -(NSDictionary*)_cookieDictionary { return [self _initCookieDictionary]; }; //-------------------------------------------------------------------- -(NSString*)_cookieDescription { //OK NSString* cookieHeader=nil; LOGObjectFnStart(); cookieHeader=[self headerForKey:GSWHTTPHeader_Cookie]; if (!cookieHeader) cookieHeader=[self headerForKey:GSWHTTPHeader_CookieStupidIIS];// God damn it LOGObjectFnStop(); return cookieHeader; }; -(NSArray*)cookies { // build super->cookies if (!_cookies) { NSDictionary* cookies = nil; NSEnumerator* keysEnum = nil; NSString* key = nil; [self _initCookies]; // super cookies init cookies=[self cookieValues]; keysEnum=[cookies keyEnumerator]; key=nil; while((key=[keysEnum nextObject])) { NSString* value=[cookies objectForKey:key]; [_cookies addObject:[GSWCookie cookieWithName:key value:value]]; } }; return _cookies; }; @end //==================================================================== @implementation GSWRequest (GSWRequestA) //-------------------------------------------------------------------- -(NSString*)sessionIDFromValuesOrCookie { return [self sessionIDFromValuesOrCookieByLookingForCookieFirst:[[self class]_lookForIDsInCookiesFirst]]; }; //-------------------------------------------------------------------- -(NSString*)sessionIDFromValuesOrCookieByLookingForCookieFirst:(BOOL)lookCookieFirst { NSString* sessionID=nil; LOGObjectFnStart(); sessionID=[self uriOrFormOrCookiesElementForKey:GSWKey_SessionID[GSWebNamingConv] byLookingForCookieFirst:lookCookieFirst]; if (!sessionID) { sessionID=[self uriOrFormOrCookiesElementForKey:GSWKey_SessionID[GSWebNamingConvInversed] byLookingForCookieFirst:lookCookieFirst]; }; LOGObjectFnStop(); return sessionID; }; //-------------------------------------------------------------------- // sessionID // nil if first request of session -(NSString*)sessionID { return [self sessionIDFromValuesOrCookie]; }; //-------------------------------------------------------------------- -(NSString*)requestHandlerPath { return [_uri urlRequestHandlerPath]; }; //-------------------------------------------------------------------- // adaptorPrefix -(NSString*)adaptorPrefix { return [_uri urlPrefix]; }; //-------------------------------------------------------------------- // applicationName -(NSString*)applicationName { return [_uri urlApplicationName]; }; //-------------------------------------------------------------------- // applicationNumber // nil if request can be handled by any instance -(int)applicationNumber { //OK if (_applicationNumber==-9999) { NSDictionary* uriElements=[self uriOrFormOrCookiesElements]; NSString* applicationNumber=[uriElements objectForKey:GSWKey_InstanceID[GSWebNamingConv]]; if (!applicationNumber) applicationNumber=[uriElements objectForKey:GSWKey_InstanceID[GSWebNamingConvInversed]]; _applicationNumber=[applicationNumber intValue]; }; return _applicationNumber; }; //-------------------------------------------------------------------- -(NSString*)requestHandlerKey { NSString* requestHandlerKey=[_uri urlRequestHandlerKey]; return requestHandlerKey; }; @end //==================================================================== @implementation GSWRequest (GSWRequestB) //-------------------------------------------------------------------- -(NSDictionary*)_extractValuesFromFormData:(NSData*)aFormData withEncoding:(NSStringEncoding)encoding { NSArray* allKeys=nil; NSDictionary* tmpFormData=nil; NSString* formString=nil; int allKeysCount=0; LOGObjectFnStart(); NSDebugMLLog(@"requests",@"aFormData=%@",aFormData); NSDebugMLLog(@"requests",@"encoding=%ld",(long)encoding); #warning we should use ACSII encoding here? dave@turbocat.de // according to the the standard http://www.w3.org/International/O-URL-code.html, // URIs are encoded in NSASCIIStringEncoding with escape sequences cooresponding // to the hexadecimal value of the UTF-8 encoding. Therefore the encoding should // only be relevant for -dictionaryQueryString and not for formString. // Yet it seems that browsers do not use UTF-8 consistently but the encoding // specified by the response. formString=[[[NSString alloc]initWithData:aFormData encoding:encoding] autorelease]; NSDebugMLLog(@"requests",@"formString=%@",formString); tmpFormData=[formString dictionaryQueryStringWithEncoding: encoding]; NSDebugMLLog(@"requests",@"tmpFormData=%@",tmpFormData); allKeys=[tmpFormData allKeys]; NSDebugMLLog(@"requests",@"allKeys=%@",allKeys); allKeysCount=[allKeys count]; NSDebugMLLog(@"requests",@"allKeys count=%d",allKeysCount); if (allKeysCount>0) { int i=0; NSString* key=nil; BOOL ismapCoordsFound=NO; NSArray* value=nil; for(i=0;i0) { contentType=[contentType substringToIndex:range.location]; NSDebugMLLog(@"requests",@"contentType=%@",contentType); }; }; LOGObjectFnStop(); return contentType; }; //-------------------------------------------------------------------- -(NSString*)_urlQueryString { //OK NSString* urlQueryString=nil; LOGObjectFnStart(); NSDebugMLLog(@"requests",@"uri=%@",_uri); NSDebugMLLog(@"requests",@"uri class=%@",[_uri class]); urlQueryString=[_uri urlQueryString]; LOGObjectFnStop(); return urlQueryString; }; @end //==================================================================== @implementation GSWRequest (GSWRequestF) //-------------------------------------------------------------------- -(BOOL)_isUsingWebServer { return _isUsingWebServer; }; //-------------------------------------------------------------------- -(void)_setIsUsingWebServer:(BOOL)flag { _isUsingWebServer=flag; }; @end //==================================================================== @implementation GSWRequest (GSWRequestG) //-------------------------------------------------------------------- -(BOOL)_isSessionIDInRequest { id ID=nil; NSDictionary* uriElements=[self uriElements]; ID=[uriElements objectForKey:GSWKey_SessionID[GSWebNamingConv]]; if (!ID) ID=[uriElements objectForKey:GSWKey_SessionID[GSWebNamingConvInversed]]; return (ID!=nil); }; //-------------------------------------------------------------------- -(BOOL)_isSessionIDInCookies { id ID=nil; ID=[self cookieValueForKey:GSWKey_SessionID[GSWebNamingConv]]; if (!ID) ID=[self cookieValueForKey:GSWKey_SessionID[GSWebNamingConvInversed]]; return (ID!=nil); }; //-------------------------------------------------------------------- -(BOOL)_isSessionIDInFormValues { id ID=nil; ID=[self formValueForKey:GSWKey_SessionID[GSWebNamingConv]]; if (!ID) ID=[self formValueForKey:GSWKey_SessionID[GSWebNamingConvInversed]]; return (ID!=nil); }; //-------------------------------------------------------------------- -(id)_completeURLWithRequestHandlerKey:(NSString*)key path:(NSString*)path queryString:(NSString*)queryString isSecure:(BOOL)isSecure port:(int)port { LOGObjectFnNotImplemented(); //TODOFN return nil; }; //-------------------------------------------------------------------- /** urlPrefix will prefix url (before the /GSWeb) **/ -(GSWDynamicURLString*)_urlWithURLPrefix:(NSString*)urlPrefix requestHandlerKey:(NSString*)key path:(NSString*)path queryString:(NSString*)queryString { GSWDynamicURLString* url=nil; LOGObjectFnStart(); url=[self _applicationURLPrefix]; if (urlPrefix) [url setURLPrefix:[urlPrefix stringByAppendingString:[url urlPrefix]]]; [url setURLRequestHandlerKey:key]; [url setURLRequestHandlerPath:path]; [url setURLQueryString:queryString]; LOGObjectFnStop(); return url; }; //-------------------------------------------------------------------- -(GSWDynamicURLString*)_urlWithRequestHandlerKey:(NSString*)key path:(NSString*)path queryString:(NSString*)queryString { GSWDynamicURLString* url=nil; LOGObjectFnStart(); url=[self _urlWithURLPrefix:nil requestHandlerKey:key path:path queryString:queryString]; LOGObjectFnStop(); return url; }; //-------------------------------------------------------------------- -(GSWDynamicURLString*)_applicationURLPrefix { GSWDynamicURLString* applicationURLPrefix=[[_uri copy] autorelease]; [applicationURLPrefix setURLRequestHandlerKey:nil]; [applicationURLPrefix setURLRequestHandlerPath:nil]; [applicationURLPrefix setURLQueryString:nil]; return applicationURLPrefix; }; //-------------------------------------------------------------------- -(NSDictionary*)_formValues { //OK LOGObjectFnStart(); if (!_formValues || !_finishedParsingMultipartFormData) { NSString* contentType=[self _contentType]; if (!contentType || [contentType isEqualToString:GSWHTTPHeader_FormURLEncoded]) { [self _getFormValuesFromURLEncoding]; } else if ([contentType isEqualToString:GSWHTTPHeader_MultipartFormData]) { [self _getFormValuesFromMultipartFormData]; } else { NSDebugMLLog(@"requests",@"contentType=%@",contentType); }; NSDebugMLLog(@"requests",@"formValues=%@",_formValues); _finishedParsingMultipartFormData=YES; }; LOGObjectFnStop(); return _formValues; }; //-------------------------------------------------------------------- -(void)_getFormValuesFromURLEncoding { NSData* formData=nil; LOGObjectFnStart(); formData=[self _formData]; NSDebugMLLog(@"requests",@"formData=%@",formData); if (formData) { NSStringEncoding formValueEncoding=[self _formValueEncodingFromFormData:formData]; NSDictionary* formValues=nil; NSDebugMLLog(@"requests",@"formValueEncoding=%d",(int)formValueEncoding); formValues=[self _extractValuesFromFormData:formData withEncoding:formValueEncoding]; ASSIGN(_formValues,formValues); NSDebugMLLog(@"requests",@"formValues=%@",_formValues); }; LOGObjectFnStop(); }; //-------------------------------------------------------------------- +(BOOL)_lookForIDsInCookiesFirst { return NO; } //-------------------------------------------------------------------- -(BOOL)_hasFormValues { NSDictionary* formValues=[self _formValues]; return [formValues count]>0; }; @end //==================================================================== @implementation GSWRequest (GSWRequestH) //-------------------------------------------------------------------- -(void)_getFormValuesFromMultipartFormDataOld { NSMutableDictionary* formValues=nil; NSArray* contentTypes=nil; int contentTypeIndex=0; int contentTypeCount=0; NSString* contentType=nil; NSData* tmpContentData=nil; LOGObjectFnStart(); formValues=(NSMutableDictionary*)[NSMutableDictionary dictionary]; contentTypes=[self headersForKey:GSWHTTPHeader_ContentType]; contentTypeIndex=0; contentTypeCount=[contentTypes count]; NSDebugMLLog(@"requests",@"contentTypes=%@",contentTypes); tmpContentData=[self content]; NS_DURING { for(contentTypeIndex=0;contentTypeIndex // ) parsedPartsCount=[parsedParts count]; if (parsedPartsCount==0) { LOGError(@"parsedPartsCount==0 decodedPart=%@",decodedPart); //TODO error } else { NSDictionary* partInfo=nil; NSString* parsedPartsContentType=nil; NSString* parsedPartsContentDisposition=nil; NSDictionary* parsedContentDispositionOfParsedPart=nil; NSEnumerator* anEnumerator=nil; NSString* aName=nil; NSString* dscrKey=nil; id descrValue=nil; partInfo=[parsedParts objectAtIndex:0]; NSDebugMLLog(@"requests",@"partInfo=%@", partInfo); NSAssert1([partInfo isKindOfClass:[NSDictionary class]], @"partInfo %@ is not a dictionary",partInfo); parsedPartsContentType=[[partInfo objectForKey:GSWHTTPHeader_ContentType] lowercaseString]; NSDebugMLLog(@"requests",@"parsedPartsContentType=%@", parsedPartsContentType); parsedPartsContentDisposition=[partInfo objectForKey:@"content-disposition"]; NSDebugMLLog(@"requests",@"parsedPartsContentDisposition=%@", parsedPartsContentDisposition); //Convert: "form-data; name=\"9.1\"; filename=\"C:\\TEMP\\zahn.txt\""; // into: {filename = "C:\\TEMP\\zahn.txt"; "form-data" = "form-data"; name = 9.1; } parsedContentDispositionOfParsedPart=[self _parseOneHeader:parsedPartsContentDisposition]; NSDebugMLLog(@"requests",@"parsedContentDispositionOfParsedPart=%@", parsedContentDispositionOfParsedPart); anEnumerator=[parsedContentDispositionOfParsedPart keyEnumerator]; aName=[parsedContentDispositionOfParsedPart objectForKey:@"name"]; NSDebugMLLog(@"requests",@"aName=%@", aName); if (!aName) { ExceptionRaise(@"GSWRequest", @"GSWRequest: No name \n%@\n", parsedContentDispositionOfParsedPart); }; while((dscrKey=[anEnumerator nextObject])) { NSDebugMLLog(@"requests",@"dscrKey=%@",dscrKey); if (![dscrKey isEqualToString:@"name"] && ![dscrKey isEqualToString:@"form-data"]) { NSString* _key=nil; descrValue=[parsedContentDispositionOfParsedPart objectForKey:dscrKey]; NSDebugMLLog(@"requests",@"descrValue=%@",descrValue); _key=[NSString stringWithFormat:@"%@.%@",aName,dscrKey]; NSDebugMLLog(@"requests",@"_key=%@",_key); //NSLog(@"getFormValues... _key=%@ descrValue=%@",_key,descrValue); [formValues setObject:[NSArray arrayWithObject:descrValue] forKey:_key]; }; }; if (parsedPartsCount>1) { NSArray* values=[parsedParts subarrayWithRange:NSMakeRange(1,[parsedParts count]-1)]; NSMutableArray* valuesNew=[NSMutableArray array]; NSMutableArray* addedValues = nil; NSDebugMLLog(@"requests",@"values=%@", values); NSDebugMLLog(@"requests",@"parsedPartsContentType=%@", parsedPartsContentType); if (!parsedPartsContentType || [parsedPartsContentType isEqualToString:GSWHTTPHeader_MimeType_TextPlain]) { int valueIndex=0; int valuesCount=[values count]; id value=nil; for(valueIndex=0;valueIndex // ); // 9.1.filename = ("C:\\TEMP\\zahn.txt"); // 9.3 = (submit); // } LOGObjectFnStop(); }; -(void)_getFormValuesFromMultipartFormData { NSMutableDictionary* formValues=nil; GSMimeParser* parser=nil; id key=nil; NSData* headersData=nil; NSMutableString* headersString=[NSMutableString string]; NSDictionary* headers=nil; NSEnumerator* enumerator=nil; IMP headersString_appendStringIMP=NULL; NSStringEncoding e; LOGObjectFnStart(); formValues=(NSMutableDictionary*)[NSMutableDictionary dictionary]; // Append Each Header headers=[self headers]; enumerator=[headers keyEnumerator]; while((key=[enumerator nextObject])) { NSArray* value=[headers objectForKey:key]; int i=0; int count=[value count]; for(i=0;i0) { parts=[parts subarrayWithRange:NSMakeRange(1,partsCount-1)]; partsCount=[parts count]; }; // Now deleting last \r\n of each object parts=[parts mutableCopy]; for(i=0;i // Into: // ( // { // "content-disposition" = "form-data; name=\"9.1\"; filename=\"C:\\TEMP\\zahn.txt\""; // "content-type" = text/plain; // }, // <41514541 41415177 4d444179 666f3054 6c4e2b58 58684357 69314b50 51635159 73573677 426d336f 52617247 36584633 4c7a6455 5637664e 39654b6b 764b4a43 71715059 67417250 59374863 78397944 36506b66 774a7550 465a4141 2f303463 446c5072 48525670 537a4135 67664738 62364572 44314158 372b7067 734c5075 304b4d77 0d0a0d0a > // ) // convert: // <436f6e74 656e742d 44697370 6f736974 696f6e3a 20666f72 6d2d6461 74613b20 6e616d65 3d22392e 33220d0a 0d0a7375 626d6974 > // Into: // ( // { // "content-disposition" = "form-data; name=\"9.3\""; // }, // <7375626d 6974> // ) -(NSArray*)_parseData:(NSData*)aData { NSArray* parsedData=nil; NSMutableDictionary* tmpHeaders=[NSMutableDictionary dictionary]; NSData* tmpData=nil; LOGObjectFnStart(); if (aData) { unsigned int tmpDataLength=[aData length]; const unsigned char* bytes=(unsigned char*)[aData bytes]; BOOL tmpHeadersEnd=NO; int start=0; int i=0; for(i=0;i0) { key=[tmpHeaderString substringToIndex:range.location]; key=[key lowercaseString]; if (range.location+1<[tmpHeaderString length]) { value=[tmpHeaderString substringFromIndex:range.location+1]; value=[value stringByTrimmingSpaces]; }; }; [tmpHeaders setObject:value forKey:key]; }; i++; //Pass the '\n' start=i+1; }; }; if (!tmpHeadersEnd) { //TODO error NSDebugMLog(@"Error"); } else { NSDebugMLLog(@"requests",@"i=%d tmpDataLength=%d tmpDataLength-i=%d", i,tmpDataLength,(tmpDataLength-i)); tmpData=[aData subdataWithRange:NSMakeRange(i,tmpDataLength-i)]; //I'm not sure this is good but it avoid 2 bytes datas on an empty input type=file located t the end of the request) //It may be better to deal with this few lines up, around (tmpHeadersEnd=YES;) if ([tmpData length]==2) { const unsigned char* bytes=(unsigned char*)[tmpData bytes]; if (bytes[0]=='\r' && bytes[1]=='\n') tmpData=[NSData data]; }; }; tmpHeaders=[NSDictionary dictionaryWithDictionary:tmpHeaders]; parsedData=[NSArray arrayWithObjects:tmpHeaders,tmpData,nil]; NSDebugMLLog(@"requests",@"tmpHeaders=%@",tmpHeaders); NSDebugMLLog(@"requests",@"tmpData %p (length=%d)=%@", tmpData,[tmpData length],tmpData); NSDebugMLLog(@"requests",@"parsedData %p =%@", parsedData,parsedData); }; LOGObjectFnStop(); return parsedData; }; //-------------------------------------------------------------------- /* - convert "multipart/form-data; boundary=---------------------------1810101926251" into { boundary = "---------------------------1810101926251"; "multipart/form-data" = "multipart/form-data"; } - convert form-data; name="9.1"; filename="C:\TEMP\zahn.txt" into {filename = "C:\\TEMP\\zahn.txt"; "form-data" = "form-data"; name = 9.1; } - convert form-data; name="9.3" into {"form-data" = "form-data"; name = 9.3; } */ -(NSDictionary*)_parseOneHeader:(NSString*)aHeader { //TODO Process quoted string ! NSMutableDictionary* parsedParts=nil; NSArray* headerParts=nil; int partIndex=0; int partCount=0; NSString* part=nil; LOGObjectFnStart(); NSDebugMLLog(@"requests",@"aHeader=%@",aHeader); parsedParts=(NSMutableDictionary*)[NSMutableDictionary dictionary]; NSDebugMLLog(@"requests",@"parsedParts=%@",parsedParts); headerParts=[aHeader componentsSeparatedByString:@";"]; NSDebugMLLog(@"requests",@"headerParts=%@",headerParts); partCount=[headerParts count]; for(partIndex=0;partIndexindex) { tmpString=[requestHandlerPathArray objectAtIndex:index]; NSDebugMLLog(@"requests",@"rquest %p: tmpString=%@", self,tmpString); if ([tmpString hasSuffix:GSWPagePSuffix[GSWebNamingConv]]) { gswpage=[tmpString stringByDeletingSuffix:GSWPagePSuffix[GSWebNamingConv]]; NSDebugMLLog(@"requests",@"request %p: gswpage=%@", self,gswpage); index++; } else if ([tmpString hasSuffix:GSWPagePSuffix[GSWebNamingConvInversed]]) { gswpage=[tmpString stringByDeletingSuffix:GSWPagePSuffix[GSWebNamingConvInversed]]; NSDebugMLLog(@"requests",@"request %p: gswpage=%@", self,gswpage); index++; }; if (requestHandlerPathArrayCount>index) { gswsid=[requestHandlerPathArray objectAtIndex:index]; NSDebugMLLog(@"requests",@"request %p: gswsid=%@", self,gswsid); index++; if (requestHandlerPathArrayCount>index) { NSString* senderID=[requestHandlerPathArray objectAtIndex:index]; NSDebugMLLog(@"requests",@"senderID=%@",senderID); index++; if (senderID && [senderID length]>0) { NSArray* senderIDParts=[senderID componentsSeparatedByString:@"."]; NSDebugMLLog(@"requests",@"request %p: senderIDParts=%@", self,senderIDParts); if ([senderIDParts count]>0) { tmpString=[senderIDParts objectAtIndex:0]; NSDebugMLLog(@"requests",@"request %p: tmpString=%@", self,tmpString); if (tmpString && [tmpString length]>0) gswcid=tmpString; if ([senderIDParts count]>1) { tmpString=[[senderIDParts subarrayWithRange: NSMakeRange(1,[senderIDParts count]-1)] componentsJoinedByString:@"."]; NSDebugMLLog(@"requests",@"request %p: tmpString=%@", self,tmpString); if (tmpString && [tmpString length]>0) { gsweid=tmpString; NSDebugMLLog(@"requests",@"request %p: gsweid=%@", self,gsweid); }; }; }; }; }; }; }; }; if (gswpage) [dict setObject:gswpage forKey:GSWKey_PageName[GSWebNamingConv]]; if (gswsid) [dict setObject:gswsid forKey:GSWKey_SessionID[GSWebNamingConv]]; if (gswcid) [dict setObject:gswcid forKey:GSWKey_ContextID[GSWebNamingConv]]; if (gsweid) [dict setObject:gsweid forKey:GSWKey_ElementID[GSWebNamingConv]]; applicationNumber=[_uri urlApplicationNumber]; if (applicationNumber<0) { NSString* tmpString2=[self cookieValueForKey:GSWKey_InstanceID[GSWebNamingConv]]; if (!tmpString2) tmpString2=[self cookieValueForKey:GSWKey_InstanceID[GSWebNamingConvInversed]]; if (tmpString2) applicationNumber=[gswinst intValue]; }; if (applicationNumber>=0) [dict setObject:GSWIntToNSString(applicationNumber) forKey:GSWKey_InstanceID[GSWebNamingConv]]; ASSIGN(_uriElements,[NSDictionary dictionaryWithDictionary:dict]); NSDebugMLLog(@"requests",@"request %@: _uriElements=%@", self,_uriElements); }; LOGObjectFnStop(); return _uriElements; }; @end //==================================================================== @implementation GSWRequest (GSWRequestL) //-------------------------------------------------------------------- -(void)_validateAPI { LOGObjectFnNotImplemented(); //TODOFN }; @end