The Trunk: Network-ar.87.mcz

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

The Trunk: Network-ar.87.mcz

commits-2
Andreas Raab uploaded a new version of Network to project The Trunk:
http://source.squeak.org/trunk/Network-ar.87.mcz

==================== Summary ====================

Name: Network-ar.87
Author: ar
Time: 4 September 2010, 11:04:42.245 am
UUID: be2e83d6-1f4d-2246-aaf3-6d485f4f6578
Ancestors: Network-fbs.86

Do not encode incoming urls in httpRequest:... since they are assumed to be encoded already.

=============== Diff against Network-fbs.86 ===============

Item was changed:
  ----- Method: HTTPSocket class>>httpRequest:url:headers:content:response: (in category 'get the page') -----
  httpRequest: method url: urlString headers: hdrs content: contentOrNil response: responseBlock
 
  "Sends an HTTP request to the server. Returns a MIMEDocument if successful,
  a string indicating the error otherwise. If a response block is provided, the
+ response is fed into into so that the sender can see all the headers.
+ The url string is assumed to be properly escaped by the sender."
- response is fed into into so that the sender can see all the headers."
 
  | index serverAndPort server port rawUrl stream resp code headers
   contentLength contentType contentStream |
 
  (urlString beginsWith: 'http://') ifFalse:[self error: 'Not a http url'].
 
  "Extract server, port, and url"
  index := urlString indexOf: $/ startingAt: 8 ifAbsent:[urlString size+1]. "past http://"
  serverAndPort := urlString copyFrom: 8 to: index-1.
  server := serverAndPort copyUpTo: $:.
  port := ((serverAndPort copyAfter: $:) ifEmpty:['80']) asNumber.
 
  "Prepare the request URI"
  rawUrl := urlString copyFrom: index to: urlString size.
  (rawUrl beginsWith: '/') ifFalse:[rawUrl := '/', rawUrl].
- rawUrl := rawUrl encodeForHTTPWithTextEncoding: 'utf-8'
- conditionBlock: [:c | c isSafeForHTTP or:['/;&=\?' includes: c]].
 
  "Check for proxy"
  (self shouldUseProxy: server) ifTrue:[
  self httpProxyServer ifNotEmpty:[
  rawUrl := 'http://', serverAndPort, rawUrl. "per RFC 2616"
  server := self httpProxyServer.
  port := self httpProxyPort.
  ].
  ].
 
  "Fire off the request"
  stream := SocketStream openConnectionToHostNamed: server port: port.
  stream nextPutAll: method; space; nextPutAll: rawUrl; space; nextPutAll: 'HTTP/1.0'; crlf.
  stream nextPutAll: 'Host: ', serverAndPort; crlf.
  stream nextPutAll: 'Connection: close'; crlf.
  stream nextPutAll: 'User-Agent: ', self userAgentString; crlf.
  stream nextPutAll: hdrs.
  stream crlf.
 
  contentOrNil ifNotNil:[
  "Upload request content"
  contentStream := contentOrNil readStream.
  [contentStream atEnd] whileFalse:[
  (HTTPProgress new) total: contentOrNil size;
  amount: contentStream position; signal: 'Uploading...'.
  stream nextPutAll: (contentStream next: 4096).
  stream flush.
  ].
  ].
 
  stream flush.
 
  "Read the response"
  resp := stream upToAll: String crlfcrlf.
  "Extract the response code"
  code := ((resp copyUpTo: String cr) findTokens: ' ') second asNumber.
  "And the response headers"
  headers := Dictionary new.
  resp lines allButFirst allButLast do:[:nextLine|
  headers at: (nextLine copyUpTo: $:) asLowercase
  put: (nextLine copyAfter: $:) withBlanksTrimmed.
  ].
 
  "Read response content"
  contentLength := headers at: 'content-length' ifAbsent:[nil].
  contentType := headers at: 'content-type' ifAbsent:['application/octet-stream'].
  "Fixme - Provide HTTProgress"
  contentLength
  ifNil:[contentStream := WriteStream with: stream upToEnd]
  ifNotNil:[
  contentLength := contentLength asNumber.
  contentStream := (String new: contentLength) writeStream.
  [contentStream position < contentLength] whileTrue:[
  contentStream nextPutAll:
  (stream next: (contentLength - contentStream position min: 4096)).
  (HTTPProgress new) total: contentLength;
  amount: contentStream position; signal: 'Downloading...'.
  ].
  ].
  stream close.
 
  responseBlock ifNotNil:[responseBlock value: resp].
 
  ^(code between: 200 and: 299)
  ifTrue:[MIMEDocument contentType: contentType
  content: contentStream contents url: urlString]
  ifFalse:[resp asString, contentStream contents].
  !