def request(method, url, response_handler, params, headers): tgt = urlparse(url) ssl = (tgt.scheme == "https") port = (80 if not ssl else 443) client = vertx.create_http_client(ssl=ssl, port=port, host=tgt.hostname) params_encoded = [] for key in sorted(params.iterkeys()): params_encoded.append(quote(key) + "=" + quote("%s" % params[key])) query_str = '&'.join(params_encoded) if method == 'GET': req = client.get(tgt.path +'?'+ query_str, response_handler) else: req = client.post(tgt.path, response_handler) if len(query_str) > 0: req.put_header('Content-Length', str(len(query_str))) req.put_header('Content-Type', "application/x-www-form-urlencoded") req.write_str(query_str) for key, val in headers.items(): req.put_header(key, val) req.put_header('User-Agent', "stt") req.end()
def listen_handler(err, server): VertxAssert.assertNull(err) # The server is listening so send an HTTP request vertx.create_http_client().set_port(8181).get_now("/", resp_handler)
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx from core.file_system import FileSystem from core.streams import Pump client = vertx.create_http_client() client.port = 8080 client.host = "localhost" fs = vertx.file_system() def response_handler(resp): print "Response %d" % resp.status_code req = client.put("/someurl", response_handler) filename = "upload/upload.txt" def file_props(err, props):
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx from test_utils import TestUtils from core.http import RouteMatcher tu = TestUtils() server = vertx.create_http_server() rm = RouteMatcher() server.request_handler(rm) server.listen(8080) client = vertx.create_http_client() client.port = 8080; class RouteMatcherTest(object): def __init__(self): self.params = { "name" : "foo", "version" : "v0.1"} self.re_params = { "param0" : "foo", "param1" : "v0.1"} self.regex = "\\/([^\\/]+)\\/([^\\/]+)" def test_get_with_pattern(self): route('get', False, "/:name/:version", self.params, "/foo/v0.1") def test_get_with_regex(self): route('get', True, self.regex, self.re_params, "/foo/v0.1")
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx client = vertx.create_http_client(port=8282, host="localhost") server = vertx.create_http_server() @server.request_handler def request_handler(req): print "Proxying request: %s"% req.uri def response_handler(c_res): print "Proxying response: %s"% c_res.status_code req.response.chunked = True req.response.status_code = c_res.status_code for header in c_res.headers.keys(): req.response.put_header(header, c_res.headers[header]) def data_handler(data): print "Proxying response body: %s"% data req.response.write_buffer(data) c_res.data_handler(data_handler)
def request(self, response_handler, method="GET", path="/", query=None): """ Make an oauth signed request """ if path[0] != "/": path = "/" + path # Data required for a sgned request auth_params = { "oauth_consumer_key": self.consumer_key, "oauth_nonce": "".join([str(random.randint(0, 9)) for i in range(32)]) if self.nonce is None else self.nonce, "oauth_signature_method": "HMAC-SHA1", "oauth_timestamp": int(time.time()) if self.timestamp is None else self.timestamp, "oauth_token": self.oauth_token, "oauth_version": "1.0", } # Build signature sig_data = method + "&" + self.quote(self.api_endpoint.scheme + "://" + self.api_endpoint.hostname + path) params_encoded = [] for key in sorted(auth_params.iterkeys()): params_encoded.append(self.quote(key) + "=" + self.quote("%s" % auth_params[key])) if query is not None: for key in sorted(query.iterkeys()): params_encoded.append(self.quote(key) + "=" + self.quote("%s" % query[key])) sig_data = sig_data + "&" + self.quote("&".join(sorted(params_encoded))) signing_key = self.quote(self.consumer_secret) + "&" + self.quote(self.oauth_token_secret) hashed = hmac.new(signing_key, sig_data, sha1) signature = binascii.b2a_base64(hashed.digest())[:-1] auth_params["oauth_signature"] = signature # Build oauth header auth_params_encoded = [] for key in sorted(auth_params.iterkeys()): auth_params_encoded.append('%s="%s"' % (self.quote(key), self.quote("%s" % auth_params[key]))) auth = "OAuth " + ", ".join(auth_params_encoded) # Build request query_params = [] query_str = "" if query is not None: for key in query: query_params.append("%s=%s" % (self.quote(key), self.quote("%s" % query[key]))) if len(query_params) > 0: query_str = "&".join(query_params) client = vertx.create_http_client() client.host = self.api_endpoint.hostname ssl = True if self.api_endpoint.scheme == "https" else False client.ssl = ssl if self.api_endpoint.port is None: client.port = 80 if not ssl else 443 else: client.port = self.api_endpoint.port if self.debug: print "Compare this output to twitters OAuth tool" print "Signature base string:\n" + sig_data print "Authorization header:\nAuthorization: " + auth print "cURL command:\ncurl %s '%s://%s%s' --data '%s' --header 'Authorization: %s' --verbose" % ( "--get" if method == "GET" else "--request '%s'" % method, self.api_endpoint.scheme, self.api_endpoint.netloc, path, query_str, auth, ) if method == "GET": request = client.get(path + ("?" + query_str if len(query_str) > 0 else ""), response_handler) else: request = client.post(path, response_handler) if len(query_str) > 0: request.put_header("Content-Length", len(query_str)) request.put_header("Content-Type", "application/x-www-form-urlencoded") request.write_str(query_str) request.put_header("Authorization", auth) request.end()
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx client = vertx.create_http_client(port=8282, host="localhost") server = vertx.create_http_server() @server.request_handler def request_handler(req): print "Proxying request: %s" % req.uri def response_handler(c_res): print "Proxying response: %s" % c_res.status_code req.response.chunked = True req.response.status_code = c_res.status_code for header in c_res.headers.keys(): req.response.put_header(header, c_res.headers[header]) def data_handler(data):
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx client = vertx.create_http_client(port=4443, host="localhost", ssl=True, trust_all=True) def handle_body(body): print "Got data %s" % body def handle_response(resp): print "Got response %s\n" % resp.status_code resp.body_handler(handle_body) client.get_now("/", handle_response)
# Copyright 2011 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx client = vertx.create_http_client(port=4443, host="localhost", ssl=True, trust_all=True) def handle_body(body): print "Got data %s"% body def handle_response(resp): print "Got response %s\n" % resp.status_code resp.body_handler(handle_body) client.get_now("/", handle_response)