|
136 | 136 | host, port = request.path.split(":", 2)
|
137 | 137 | endpoint = Async::IO::Endpoint.tcp(host, port)
|
138 | 138 |
|
| 139 | + Async.logger.debug(self) {"Making connection to #{endpoint}..."} |
| 140 | + |
139 | 141 | Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
|
140 | 142 | upstream = Async::IO::Stream.new(endpoint.connect)
|
| 143 | + Async.logger.debug(self) {"Connected to #{upstream}..."} |
141 | 144 |
|
142 | 145 | reader = Async do
|
143 | 146 | while chunk = upstream.read_partial
|
144 | 147 | stream.write(chunk)
|
145 | 148 | stream.flush
|
146 | 149 | end
|
147 | 150 |
|
| 151 | + ensure |
| 152 | + Async.logger.debug(self) {"Finished reading from upstream..."} |
148 | 153 | stream.close_write
|
149 | 154 | end
|
150 | 155 |
|
151 | 156 | writer = Async do
|
152 |
| - begin |
153 |
| - while chunk = stream.read_partial |
154 |
| - upstream.write(chunk) |
155 |
| - upstream.flush |
156 |
| - end |
157 |
| - |
158 |
| - upstream.close_write |
159 |
| - rescue Async::Wrapper::Cancelled |
160 |
| - #ignore |
| 157 | + while chunk = stream.read_partial |
| 158 | + upstream.write(chunk) |
| 159 | + upstream.flush |
161 | 160 | end
|
162 |
| - end |
163 |
| - |
164 |
| - begin |
165 |
| - reader.wait |
166 |
| - writer.wait |
167 |
| - |
| 161 | + |
| 162 | + rescue Async::Wrapper::Cancelled |
| 163 | + #ignore |
168 | 164 | ensure
|
169 |
| - upstream.close |
170 |
| - stream.close |
| 165 | + Async.logger.debug(self) {"Finished writing to upstream..."} |
| 166 | + upstream.close_write |
171 | 167 | end
|
| 168 | + |
| 169 | + reader.wait |
| 170 | + writer.wait |
| 171 | + ensure |
| 172 | + upstream.close |
| 173 | + stream.close |
172 | 174 | end
|
173 | 175 | end
|
174 | 176 | end
|
175 | 177 |
|
176 | 178 | it 'can get insecure website' do
|
177 |
| - endpoint = Async::HTTP::Endpoint.parse("http://www.codeotaku.com") |
| 179 | + endpoint = Async::HTTP::Endpoint.parse("http://www.google.com") |
178 | 180 | proxy_client = client.proxied_client(endpoint)
|
179 | 181 |
|
180 |
| - response = proxy_client.get("/index") |
| 182 | + response = proxy_client.get("/search") |
181 | 183 | expect(response).to_not be_failure
|
182 | 184 |
|
183 | 185 | # The response would be a redirect:
|
|
191 | 193 | end
|
192 | 194 |
|
193 | 195 | it 'can get secure website' do
|
194 |
| - endpoint = Async::HTTP::Endpoint.parse("https://www.codeotaku.com") |
| 196 | + endpoint = Async::HTTP::Endpoint.parse("https://www.google.com") |
195 | 197 | proxy_client = client.proxied_client(endpoint)
|
196 | 198 |
|
197 |
| - response = proxy_client.get("/index") |
| 199 | + response = proxy_client.get("/search") |
198 | 200 |
|
199 | 201 | expect(response).to_not be_failure
|
200 | 202 | expect(response.read).to_not be_empty
|
|
0 commit comments