'From Pharo1.0beta of 16 May 2008 [Latest update: #10466] on 24 October 2010 at 6:36:44 pm'! HTTPMessage subclass: #HTTPRequest instanceVariableNames: 'requestLine peer timestamp ip environmentData resolution encrypted authenticated host ' classVariableNames: '' poolDictionaries: '' category: 'Swazoo-Messages'! !HTTPRequest methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:37'! computeHost | value | value := self headers fieldOfClass: HTTPHostField ifPresent: [:field | field hostName] ifAbsent: [self requestLine requestURI hostname]. ^ value notNil ifTrue: [value] ifFalse: ['']! ! !HTTPRequest methodsFor: 'accessing-headers' stamp: 'PaoloBonzini 10/24/2010 17:37'! host host isNil ifTrue: [host := self computeHost]. ^host! ! !ResourceTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:38'! testLeafMatch self assert: (resource match: 'foo')! ! !ResourceTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:38'! testLeafMismatch self deny: (resource match: 'Foo')! ! !SiteTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:39'! testRequestMatch | request site | request := HTTPGet request: 'foo' from: 'myhosthost:1234' at: '1.2.3.4'. site := SwazooSite new host: 'myhosthost' ip: '1.2.3.4' port: 1234. self assert: (site match: request) ! ! !SiteTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:40'! testRequestMismatch | request site | request := HTTPGet request: 'foo' from: 'localhost:1234' at: '1.2.3.4'. site := SwazooSite new host: 'remotehost' ip: '1.2.3.4' port: 1234. self deny: (site match: request) ! ! !SwazooResource methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:25'! match: anIdentifier ^self uriPattern = anIdentifier! ! !CompositeResource methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:27'! match: anIdentifier ^self uriPattern match: anIdentifier! ! !SwazooSite methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:27'! addAlias: anAlias self uriPattern add: anAlias! ! !SwazooSite methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:27'! host ^self uriIdentifier host! ! !SwazooSite methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:28'! host: aHostString ip: anIPString port: aNumber "see comments in methods host and ip !! " "hostname must be unique!! " | site | site := SwazooServer singleton siteHostnamed: aHostString. (site notNil and: [site ~= self]) ifTrue: [^SwazooSiteError error: 'Site with that hostname already exist!!']. self uriIdentifier setIp: anIPString port: aNumber host: aHostString! ! !SwazooSite methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:29'! ip "IP address of this site. Swazoo can have virtual sites, that is, more than one site can share the same ip and port!! IP can be a number or full DNS name. For example: server.ibm.com or 234.12.45.66" ^self uriIdentifier ip! ! !SwazooSite methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:30'! port ^self uriIdentifier port! ! !SwazooSite methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:28'! host: aString ^self uriIdentifier host: aString! ! !SwazooSite methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:29'! ip: aString self uriIdentifier ip: aString! ! !SwazooSite methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:30'! match: aRequest self uriPattern detect: [:each | each requestMatch: aRequest] ifNone: [^false]. ^true! ! !SwazooSite methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:30'! port: aNumber self uriIdentifier port: aNumber! ! !SwazooSite methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:30'! printUrlOn: aWriteStream self uriIdentifier printUrlOn: aWriteStream! ! !SwazooSite methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:30'! uriIdentifier self uriPattern isEmpty ifTrue: [self uriPattern add: SiteIdentifier new]. ^self uriPattern first ! ! !SwazooSite methodsFor: 'initialize-release' stamp: 'PaoloBonzini 10/24/2010 17:28'! initUriPattern self uriPattern: OrderedCollection new.! ! !URIIdentifier methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:31'! requestMatch: aRequest ^self valueMatch: aRequest! ! !URIIdentifier methodsFor: 'private' stamp: 'PaoloBonzini 10/24/2010 17:32'! valueMatch: aRequestOrIdentifier ^self subclassResponsibility! ! !SiteIdentifier methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:33'! host host isNil ifTrue: [host := '*']. ^host! ! !SiteIdentifier methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:33'! ip ip isNil ifTrue: [ip := '*']. ^ip! ! !SiteIdentifier methodsFor: 'accessing' stamp: 'PaoloBonzini 10/24/2010 17:33'! port port isNil ifTrue: [port := self class defaultPort]. ^port! ! !SiteIdentifier methodsFor: 'private-comparing' stamp: 'PaoloBonzini 10/24/2010 17:34'! valueMatch: aRequestOrIdentifier ^(self portMatch: aRequestOrIdentifier) and: [(self ipMatch: aRequestOrIdentifier) and: [self hostMatch: aRequestOrIdentifier] ]! ! !SiteIdentifier class methodsFor: 'defaults' stamp: 'PaoloBonzini 10/24/2010 17:33'! defaultPort ^80! ! !URIResolution methodsFor: 'resolving' stamp: 'PaoloBonzini 10/24/2010 17:34'! resolveLeafResource: aResource (aResource canAnswer and: [aResource match: self currentIdentifier]) ifFalse: [^nil]. ^self getAnswerFrom: aResource! ! !URIResolution methodsFor: 'resolving' stamp: 'PaoloBonzini 10/24/2010 17:35'! resolveSite: aSite (aSite canAnswer and: [aSite match: self request]) ifFalse: [^nil]. ^self visitChildrenOf: aSite advancing: false! ! !URIResolutionTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:41'! testSiteAnswer | resource request response | resource := SwazooSite new port: 80. resource addResource: (HelloWorldResource uriPattern: '/'). request := HTTPGet request: '/' from: 'foo.com' at: '1.2.3.4'. response := URIResolution resolveRequest: request startingAt: resource. self assert: response code = 200. self assert: request resourcePath size = 1. self assert: request resourcePath first = '/'! ! !URIResolutionTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:42'! testSiteMatch | request site response | request := HTTPGet request: '/' from: 'myhosthost:1234' at: '1.2.3.4'. site := SwazooSite new host: 'myhosthost' ip: '1.2.3.4' port: 1234. site addResource: (HelloWorldResource uriPattern: '/'). response := URIResolution resolveRequest: request startingAt: site. self assert: response code = 200. ! ! !URIResolutionTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:45'! testSiteMismatch | request site response | request := HTTPGet request: '/' from: 'localhost:1234' at: '1.2.3.4'. site := SwazooSite new host: 'remotehost' ip: '1.2.3.4' port: 1234. site addResource: (HelloWorldResource uriPattern: '/'). response := URIResolution resolveRequest: request startingAt: site. self assert: response isNil! ! !URIResolutionTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:43'! testStringMatch | request resource response | request := HTTPGet request: 'foo'. resource := HelloWorldResource uriPattern: 'foo'. response := URIResolution resolveRequest: request startingAt: resource. self assert: response code = 200! ! !URIResolutionTest methodsFor: 'testing' stamp: 'PaoloBonzini 10/24/2010 17:45'! testStringMismatch | request resource response | request := HTTPGet request: 'foo'. resource := HelloWorldResource uriPattern: 'Foo'. response := URIResolution resolveRequest: request startingAt: resource. self assert: response isNil! ! URIResolution removeSelector: #siteMatch:! URIResolution removeSelector: #stringMatch:! SwazooSite removeSelector: #host:ip:port:sslPort:! HTTPMessage subclass: #HTTPRequest instanceVariableNames: 'requestLine peer timestamp ip environmentData resolution encrypted authenticated host' classVariableNames: '' poolDictionaries: '' category: 'Swazoo-Messages'!