InternetAddress class>>fromString:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

InternetAddress class>>fromString:

Steve Alan Waring
The algorithm used in InternetAddress class>>fromString: is not correct.

Instead of checking if the first character is a digit, the first character
of the rightmost domain label should be checked. See section 3.2.2 of
http://www.ietf.org/rfc/rfc2396.txt

Below is a proposed fix and test:

!InternetAddress class methodsFor!
fromString: aString
"Instantiate a new instance of the receiver from aString. This may contain
either a host name
or an IP address."
#swChanged.
(self isIPString: aString) ifTrue: [^self fromIPString: aString].
^self host: aString! !
!InternetAddress class categoriesFor: #fromString:!instance creation!public!
!
!InternetAddress class methodsFor!
isIPString: aString
"Answer if aString is a IPv4 address, based on the test described in
Section 3.2.2 of <http://www.ietf.org/rfc/rfc2396.txt>"
| index |
(index := aString findLast: [:each | each = $.]) = 0 ifTrue: [^false].
^index = aString size
ifTrue:
["More accurate, but not needed in most situations
self isIPString: aString allButLast"
false]
ifFalse: [(aString at: index + 1) isDigit]! !
!InternetAddress class categoriesFor: #isIPString:!public!testing! !
!SptHTTPLooseTest methodsFor!
testIsIPString
self deny: (InternetAddress isIPString: '').
self deny: (InternetAddress isIPString: '.').
self deny: (InternetAddress isIPString: 'a').
self deny: (InternetAddress isIPString: 'a.').
self deny: (InternetAddress isIPString: 'invalid').
self deny: (InternetAddress isIPString: 'www.foo.org').
self deny: (InternetAddress isIPString: 'www.foo.org.').
self deny: (InternetAddress isIPString: 'www.foo.or1.').
self deny: (InternetAddress isIPString: '1foo.or1.').
self deny: (InternetAddress isIPString: '1.2.3.-').
self assert: (InternetAddress isIPString:
'invalid.invalid.invalid.4invalid').
self assert: (InternetAddress isIPString: '1.2.3.4')! !
!SptHTTPLooseTest categoriesFor: #testIsIPString!public!unit tests! !