Hi all,
the attached filein fixes a problem with the handling of redirects in the
http post document method.
The original implementation issued a post to the received redirection address
instead of a get.
Using the get to my understanding is still not completely compliant with the
RFC, but browsers seem to behave that way.
Does anybody have a more substantiated answer to this problem?
Michael
--
"To improve is to change, to be perfect is to change often."
Winston Churchill
+------------------------------------------------------------+
| Michael Rueger |
| Phone: ++1 (818) 623 3283 Fax: ++1 (818) 623 3559 |
+---------- [EMAIL PROTECTED] ------------------+
'From ''Squeak2.8alpha'' of 26 January 2000 update 1855 [latest update:
#''Squeak2.8alpha'' of 26 January 2000 update 1855] on 2 March 2000 at 4:14:53 pm'!
!HTTPSocket class methodsFor: 'get the page' stamp: 'mir 3/2/2000 16:14'!
httpPostDocument: url args: argsDict accept: mimeType request: requestString
"like httpGET, except it does a POST instead of a GET. POST allows data to be
uploaded"
| s header length page list firstData aStream argsStream first type newUrl
httpUrl |
Socket initializeNetwork.
httpUrl _ Url absoluteFromText: url.
page _ httpUrl toText.
"add arguments"
argsDict ifNotNil: [page _ page, (self argString: argsDict) ].
"encode the arguments dictionary"
argsStream _ WriteStream on: String new.
first _ true.
argsDict associationsDo: [ :assoc |
assoc value do: [ :value |
first ifTrue: [ first _ false ] ifFalse: [ argsStream nextPut:
$& ].
argsStream nextPutAll: assoc key encodeForHTTP.
argsStream nextPut: $=.
argsStream nextPutAll: value encodeForHTTP.
] ].
s _ HTTPSocket new.
s _ self initHTTPSocket: httpUrl wait: (self deadlineSecs: 30) ifError:
[:errorString | ^errorString].
Transcript cr; show: url; cr.
s sendCommand: 'POST ', page, ' HTTP/1.0', CrLf,
(mimeType ifNotNil: ['ACCEPT: ', mimeType, CrLf] ifNil: ['']),
'ACCEPT: text/html', CrLf, "Always accept plain text"
HTTPBlabEmail, "may be empty"
requestString, "extra user request. Authorization"
'User-Agent: Squeak 1.31', CrLf,
'Content-type: application/x-www-form-urlencoded', CrLf,
'Content-length: ', argsStream contents size printString, CrLf,
'Host: ', httpUrl authority, CrLf. "blank line automatically added"
s sendCommand: argsStream contents.
"get the header of the reply"
list _ s getResponseUpTo: CrLf, CrLf. "list = header, CrLf, CrLf,
beginningOfData"
header _ list at: 1.
"Transcript show: page; cr; show: argsStream contents; cr; show: header; cr."
firstData _ list at: 3.
"dig out some headers"
s header: header.
length _ s getHeader: 'content-length'.
length ifNotNil: [ length _ length asNumber ].
type _ s getHeader: 'content-type'.
s responseCode first = $3 ifTrue: [
newUrl _ s getHeader: 'location'.
newUrl ifNotNil: [
Transcript show: 'Response: ' , s responseCode.
Transcript show: ' redirecting to: ', newUrl; cr.
s destroy.
"^self httpPostDocument: newUrl args: argsDict accept:
mimeType"
^self httpGetDocument: newUrl accept: mimeType ] ].
aStream _ s getRestOfBuffer: firstData totalLength: length.
s responseCode = '401' ifTrue: [^ header, aStream contents].
s destroy. "Always OK to destroy!!"
^ MIMEDocument contentType: type content: aStream contents url: url! !