Mercurial > repos > tabletprog
changeset 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 | a6739206a9e3 |
children | 6e579a75a0a9 18598163e3ef |
files | modules/http.tp samples/http.tp |
diffstat | 2 files changed, 32 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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 } } }
--- 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: "/")) } }