Sending WinSqueak Code: Dumb but Critical Question

Georg Gollmann gollmann at edvz.tuwien.ac.at
Thu Jan 22 15:42:10 UTC 1998


>>>(2) We've built a turnIn and grading system in the Pluggable WebServer so
>>>that students can fileOut their code, open up the fileOut, select-all and
>>>copy, then paste it into a text area in an HTML form.  Again, all tabs and
>>>line endings are gone, so we can't fileIn.
>>
>>Why not send the "fileout" directly to the PWS without the detour to the
>>local file system ?
>
>That's an interesting solution (we're also trying Andreas' solution of the
>CrLfFileStream, and Ian's of binary FTP).  I haven't tried to build a forms
>submit HTTP request before.  Do you have any example code that we could
>peek at?  I'm certainly willing to try it!

Here´s what works for me:

!WebRequest class methodsFor: 'HTML Reply' stamp: 'go 1/22/98 16:33'!
echoForm: aRequest
	"Echo any form data sent to me."

	| formData |
	formData := WriteStream on: #().
	aRequest fields associationsDo: [ :ass | formData nextPut: ass key;
nextPut: ass value ].
	^aRequest title: 'Test Returns'; itemize: formData contents! !


!WebRequest class methodsFor: 'Testing' stamp: 'go 1/22/98 16:28'!
encode: aString

	| encoded clear char |
	encoded := WriteStream on: ''.
	clear := ReadStream on: aString.
	[ clear atEnd ] whileFalse: [
		char := clear next.
		char isAlphaNumeric ifTrue: [ encoded nextPut: char ]
ifFalse: [
			char = $  ifTrue: [ encoded nextPut: $+ ] ifFalse: [
				encoded nextPut: $%; nextPutAll: (char
asciiValue hex copyFrom: 4 to: 5)]]].
	^encoded contents! !

!WebRequest class methodsFor: 'Testing' stamp: 'go 1/22/98 16:31'!
post: pairs toHost: hostName port: portNumber path: pathString
	"Test generating of form messages."

	| encoded sock |
	encoded := WriteStream on: ''.
	pairs pairsDo: [ :field :value | encoded
		nextPutAll: field; nextPut: $=; nextPutAll: (WebRequest
encode: value); nextPut: $& ].
	encoded := encoded contents.
	Socket initializeNetwork.
	(sock := Socket new)
	 connectTo: (NetNameResolver addressForName: hostName timeout: 20)
port: portNumber;
	 waitForConnectionUntil: (Socket standardDeadline);
	 sendData: ('POST ', pathString, ' HTTP/1.0', CrLf,
	 'CONTENT-LENGTH: ', encoded size asString, CrLf, CrLf, encoded).
	encoded := sock getData.
	sock close.
	^encoded! !

Then do something like

WebRequest post: #('field1' 'value one' 'field2' 'value two')
 toHost: 'your.host.name'
 port: yourPort
 path: '/Server.echoForm'


Good luck
Georg


----
Dipl.Ing. Georg Gollmann                   TU-Wien, EDV-Zentrum

phon:(+43-1) 58801 - 5848
fax: (+43-1) 587 42 11
mail:gollmann at edvz.tuwien.ac.at
http://ftp.tuwien.ac.at/~go/Gollmann.html





More information about the Squeak-dev mailing list