# HG changeset patch # User Mike Pavone # Date 1376107271 25200 # Node ID 075b1e71feff37013b13ab9f35b1c437a9d29bfd # Parent a6739206a9e362c7cb55e3f999f0e56a1c896e4a A little more work on the HTTP module diff -r a6739206a9e3 -r 075b1e71feff modules/http.tp --- a/modules/http.tp Fri Aug 09 20:12:04 2013 -0700 +++ b/modules/http.tp Fri Aug 09 21:01:11 2013 -0700 @@ -4,9 +4,33 @@ get <- :path { sock <- socket connectTo: address onPort: port sock send: "GET " . path . " HTTP/1.1\r\nHost: " . address . "\r\n\r\n" - resp <- sock recv 4096 - print: resp + resp <- "" + waiting <- true + headerText <- "" + rest <- "" + while: { waiting } do: { + data <- sock recv 4096 + resp <- resp . data + pos <- resp find: "\r\n\r\n" else: { -1 } + if: pos >= 0 { + waiting <- false + headerText <- resp from: 0 withLength: pos + rest <- resp from: pos + 4 + } + } + print: "Headers:\n" . headerText . "\n" + print: "Rest:\n" . rest . "\n" + _headers <- (headerText split: "\r\n") fold: (dict linear) with: :acc curLine{ + //TODO: support multiple headers with the same name + part <- curLine partitionOn: ":" + acc set: (part before) (trim: (part after)) + } + _closed <- false + #{ + + } sock close + rest } } } diff -r a6739206a9e3 -r 075b1e71feff samples/http.tp --- a/samples/http.tp Fri Aug 09 20:12:04 2013 -0700 +++ b/samples/http.tp Fri Aug 09 21:01:11 2013 -0700 @@ -1,6 +1,10 @@ #{ - main <- { - cli <- http client: "rhope.retrodev.com" + main <- :args { + server <- "rhope.retrodev.com" + if: (args length) > 1 { + server <- args get: 1 + } + cli <- http client: server print: (string: (cli get: "/")) } }