Mercurial > repos > tabletprog
comparison modules/http.tp @ 160:729dc894e61c
Add post support to HTTP client
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 10 Aug 2013 18:26:14 -0700 |
parents | d81de309a51f |
children | f594e6836c44 |
comparison
equal
deleted
inserted
replaced
159:d81de309a51f | 160:729dc894e61c |
---|---|
71 _open? <- false | 71 _open? <- false |
72 } | 72 } |
73 } | 73 } |
74 } | 74 } |
75 } | 75 } |
76 _handleResponse <- :sock { | |
77 resp <- "" | |
78 waiting <- true | |
79 headerText <- "" | |
80 rest <- "" | |
81 status <- "" | |
82 while: { waiting } do: { | |
83 data <- sock recv 4096 | |
84 resp <- resp . data | |
85 pos <- resp find: "\r\n\r\n" else: { -1 } | |
86 if: pos >= 0 { | |
87 waiting <- false | |
88 statusEnd <- resp find: "\r\n" else: { 0 } | |
89 statusStart <- (resp find: " " else: { 0 }) + 1 | |
90 status <- resp from: statusStart withLength: (statusEnd - statusStart) | |
91 headerText <- resp from: statusEnd + 2 withLength: pos - (statusEnd + 2) | |
92 rest <- resp from: pos + 4 | |
93 } | |
94 } | |
95 headers <- (headerText splitOn: "\r\n") fold: (dict linear) with: :acc curLine{ | |
96 //TODO: support multiple headers with the same name | |
97 part <- curLine partitionOn: ":" | |
98 acc set: (trim: (part before)) (trim: (part after)) | |
99 } | |
100 | |
101 response: headers status sock rest | |
102 } | |
76 #{ | 103 #{ |
77 client:usingPort <- :address :port{ | 104 client:usingPort <- :address :port{ |
78 #{ | 105 #{ |
79 get <- :path { | 106 get <- :path { |
80 sock <- socket connectTo: address onPort: port | 107 sock <- socket connectTo: address onPort: port |
81 sock send: "GET " . path . " HTTP/1.1\r\nHost: " . address . "\r\n\r\n" | 108 sock send: "GET " . path . " HTTP/1.1\r\nHost: " . address . "\r\n\r\n" |
82 resp <- "" | 109 _handleResponse: sock |
83 waiting <- true | 110 } |
84 headerText <- "" | 111 post:toPath:withType <- :body :path :type { |
85 rest <- "" | 112 sock <- socket connectTo: address onPort: port |
86 status <- "" | 113 sock send: "POST " . path . " HTTP/1.1\r\nHost: " . address . "\r\nContent-Type: " . type . "\r\nContent-Length: " . (string: (body byte_length)) . "\r\n\r\n" |
87 while: { waiting } do: { | 114 sock send: body |
88 data <- sock recv 4096 | 115 _handleResponse: sock |
89 resp <- resp . data | |
90 pos <- resp find: "\r\n\r\n" else: { -1 } | |
91 if: pos >= 0 { | |
92 waiting <- false | |
93 statusEnd <- resp find: "\r\n" else: { 0 } | |
94 statusStart <- (resp find: " " else: { 0 }) + 1 | |
95 status <- resp from: statusStart withLength: (statusEnd - statusStart) | |
96 headerText <- resp from: statusEnd + 2 withLength: pos - (statusEnd + 2) | |
97 rest <- resp from: pos + 4 | |
98 } | |
99 } | |
100 headers <- (headerText splitOn: "\r\n") fold: (dict linear) with: :acc curLine{ | |
101 //TODO: support multiple headers with the same name | |
102 part <- curLine partitionOn: ":" | |
103 acc set: (trim: (part before)) (trim: (part after)) | |
104 } | |
105 | |
106 | |
107 response: headers status sock rest | |
108 } | 116 } |
109 } | 117 } |
110 } | 118 } |
111 | 119 |
112 client <- :address { | 120 client <- :address { |