'From Squeak5.3alpha of 7 May 2019 [latest update: #18560] on 7 May 2019 at 2:12:30 pm'!
HTTPDownloadRequest subclass: #PluginHTTPDownloadRequest
	instanceVariableNames: 'fileStream'
	classVariableNames: ''
	poolDictionaries: ''
	category: '60Deprecated-System-Download'!

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:07'!
httpPostDocument: url args: argsDict
	| argString |
	self deprecated: 'Browser plugin based http requests are not supported anymore.'.
	argString := argsDict
		ifNotNil: [argString := HTTPSocket argString: argsDict]
		ifNil: [''].
	^self post: argString url: url , argString ifError: [self halt]! !

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
httpPostMultipart: url args: argsDict 
	| mimeBorder argsStream crLf resultStream result |
	" do multipart/form-data encoding rather than x-www-urlencoded "

	crLf := String crlf.
	mimeBorder := '----squeak-', Time millisecondClockValue printString, '-stuff-----'.
	"encode the arguments dictionary"
	argsStream := WriteStream on: String new.
	argsDict associationsDo: [:assoc |
		assoc value do: [ :value | | fieldValue |
		"print the boundary"
		argsStream nextPutAll: '--', mimeBorder, crLf.
		" check if it's a non-text field "
		argsStream nextPutAll: 'Content-disposition: form-data; name="', assoc key, '"'.
		(value isKindOf: MIMEDocument)
			ifFalse: [fieldValue := value]
			ifTrue: [argsStream nextPutAll: ' filename="', value url pathForFile, '"', crLf, 'Content-Type: ', value contentType.
				fieldValue := (value content
					ifNil: [(FileStream fileNamed: value url pathForFile) contentsOfEntireFile]
					ifNotNil: [value content]) asString].
" Transcript show: 'field=', key, '; value=', fieldValue; cr. "
		argsStream nextPutAll: crLf, crLf, fieldValue, crLf.
	]].
	argsStream nextPutAll: '--', mimeBorder, '--'.

	resultStream := self
		post: 
			('Content-type: multipart/form-data; boundary=', mimeBorder, crLf,
			'Content-length: ', argsStream contents size printString, crLf, crLf, 
			argsStream contents)
		url: url ifError: [^'Error in post ' url asString].
	"get the header of the reply"
	result := resultStream upToEnd.
	^MIMEDocument content: result! !

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
post: data target: target url: url ifError: errorBlock 
	^self concreteStream new post: data target: target url: url ifError: errorBlock! !

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
post: data url: url ifError: errorBlock 
	^self post: data target: nil url: url ifError: errorBlock! !

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
requestURL: url target: target 
	"FileStream requestURL:'http://isgwww.cs.uni-magdeburg.de/~raab' target: ':=blank' "
	^self concreteStream new requestURL: url target: target! !

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
requestURLStream: url 
	"FileStream requestURLStream:'http://isgwww.cs.uni-magdeburg.de/~raab'"
	^self concreteStream new requestURLStream: url! !

!FileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
requestURLStream: url ifError: errorBlock 
	"FileStream requestURLStream:'http://isgwww.cs.uni-magdeburg.de/~raab'"
	^self concreteStream new requestURLStream: url ifError: errorBlock! !


!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
defaultBrowserReadyWait 
	^5000! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
post: data target: target url: url ifError: errorBlock 
	"Post data to the given URL. The returned file stream contains the reply of the server.
	If Squeak is not running in a browser evaluate errorBlock"

	self waitBrowserReadyFor: self defaultBrowserReadyWait ifFail: [^errorBlock value].
	Smalltalk newExternalSemaphoreDo: [ :semaphore :index |
		| request result |
		request := self primURLPost: url target: target data: data semaIndex: index.
		request ifNil: [
			Smalltalk unregisterExternalObject: semaphore.
			^errorBlock value ].
		[ semaphore wait. "until something happens"
			result := self primURLRequestState: request.
			result == nil ] whileTrue.
		result ifTrue: [ fileID := self primURLRequestFileHandle: request ].
		self primURLRequestDestroy: request.
		Smalltalk unregisterExternalObject: semaphore ].
	fileID ifNil: [ ^nil ].
	self register.
	name := url.
	rwmode := false.
	buffer1 := String new: 1.
	self enableReadBuffering! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
post: data url: url ifError: errorBlock 

	self post: data target: nil url: url ifError: errorBlock! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
primBrowserReady 
	<primitive:'primitivePluginBrowserReady'>
	^nil! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
primURLPost: url data: contents semaIndex: index 
	^self primURLPost: url target: nil data: contents semaIndex: index! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
primURLPost: url target: target data: contents semaIndex: index 
	"Post the data (url might be 'mailto:' etc)"
	<primitive:'primitivePluginPostURL'>
	^nil
 ! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
primURLRequest: url semaIndex: index 
	<primitive:'primitivePluginRequestURLStream'>
	^nil! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
primURLRequest: url target: target semaIndex: index 
	"target - String (frame, also ':=top', ':=parent' etc)"
	<primitive:'primitivePluginRequestURL'>
	^nil
 ! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
primURLRequestDestroy: request 
	<primitive:'primitivePluginDestroyRequest'>
	^nil! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
primURLRequestFileHandle: request 
	<primitive: 'primitivePluginRequestFileHandle'>
	^nil! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
primURLRequestState: request 
	<primitive:'primitivePluginRequestState'>
	^false! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
requestURL: url target: target 
	^self requestURL: url target: target ifError: [nil]! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
requestURL: url target: target ifError: errorBlock 
	"Request to go to the target for the given URL.
	If Squeak is not running in a browser evaluate errorBlock"

	self waitBrowserReadyFor: self defaultBrowserReadyWait ifFail: [^errorBlock value].
	Smalltalk newExternalSemaphoreDo: [ :semaphore :index |
		| request result |
		request := self primURLRequest: url target: target semaIndex: index.
		request ifNil: [
			Smalltalk unregisterExternalObject: semaphore.
			^errorBlock value ].
		[ semaphore wait. "until something happens"
			result := self primURLRequestState: request.
			result == nil ] whileTrue.
		self primURLRequestDestroy: request.
		Smalltalk unregisterExternalObject: semaphore ].
	fileID ifNil: [ ^nil ].
	self register.
	name := url.
	rwmode := false.
	buffer1 := String new: 1.
	self enableReadBuffering! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
requestURLStream: url 
	"FileStream requestURLStream:'http://www.squeak.org'"
	^self requestURLStream: url ifError:[nil]! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
requestURLStream: url ifError: errorBlock 
	"Request a FileStream for the given URL.
	If Squeak is not running in a browser evaluate errorBlock"
	"FileStream requestURLStream:'http://www.squeak.org'"

	self waitBrowserReadyFor: self defaultBrowserReadyWait ifFail: [^errorBlock value].
	Smalltalk newExternalSemaphoreDo: [ :semaphore :index |
		| request result |
		request := self primURLRequest: url semaIndex: index.
		request ifNil: [
			Smalltalk unregisterExternalObject: semaphore.
			^errorBlock value ].
		[ semaphore wait. "until something happens"
			result := self primURLRequestState: request.
			result == nil ] whileTrue.
		result ifTrue: [ fileID := self primURLRequestFileHandle: request ].
		self primURLRequestDestroy: request.
		Smalltalk unregisterExternalObject: semaphore ].
	fileID ifNil: [ ^nil ].
	self register.
	name := url.
	rwmode := false.
	buffer1 := String new: 1.
	self enableReadBuffering! !

!StandardFileStream methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:11'!
waitBrowserReadyFor: timeout ifFail: errorBlock 
	| startTime delay okay |
	okay := self primBrowserReady.
	okay ifNil:[^errorBlock value].
	okay ifTrue: [^true].
	startTime := Time millisecondClockValue.
	delay := Delay forMilliseconds: 100.
	[(Time millisecondsSince: startTime) < timeout]
		whileTrue: [
			delay wait.
			okay := self primBrowserReady.
			okay ifNil:[^errorBlock value].
			okay ifTrue: [^true]].
	^errorBlock value! !


!StandardFileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
isRunningAsBrowserPlugin 
	self new waitBrowserReadyFor: 1000 ifFail: [^false].
	^true! !

!StandardFileStream class methodsFor: '*60Deprecated-System-Support' stamp: 'pre 5/7/2019 14:10'!
privateCheckForBrowserPrimitives 
	<primitive:'primitivePluginBrowserReady'>
	^false! !

