comparison modules/http.tp @ 153:075b1e71feff

A little more work on the HTTP module
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 21:01:11 -0700
parents 7f442b3e4448
children 55e0dca7d3d7
comparison
equal deleted inserted replaced
152:a6739206a9e3 153:075b1e71feff
2 client:usingPort <- :address :port{ 2 client:usingPort <- :address :port{
3 #{ 3 #{
4 get <- :path { 4 get <- :path {
5 sock <- socket connectTo: address onPort: port 5 sock <- socket connectTo: address onPort: port
6 sock send: "GET " . path . " HTTP/1.1\r\nHost: " . address . "\r\n\r\n" 6 sock send: "GET " . path . " HTTP/1.1\r\nHost: " . address . "\r\n\r\n"
7 resp <- sock recv 4096 7 resp <- ""
8 print: resp 8 waiting <- true
9 headerText <- ""
10 rest <- ""
11 while: { waiting } do: {
12 data <- sock recv 4096
13 resp <- resp . data
14 pos <- resp find: "\r\n\r\n" else: { -1 }
15 if: pos >= 0 {
16 waiting <- false
17 headerText <- resp from: 0 withLength: pos
18 rest <- resp from: pos + 4
19 }
20 }
21 print: "Headers:\n" . headerText . "\n"
22 print: "Rest:\n" . rest . "\n"
23 _headers <- (headerText split: "\r\n") fold: (dict linear) with: :acc curLine{
24 //TODO: support multiple headers with the same name
25 part <- curLine partitionOn: ":"
26 acc set: (part before) (trim: (part after))
27 }
28 _closed <- false
29 #{
30
31 }
9 sock close 32 sock close
33 rest
10 } 34 }
11 } 35 }
12 } 36 }
13 37
14 client <- :address { 38 client <- :address {