| 1 |
|
| 2 |
|
| 3 |
import argparse |
| 4 |
import sys |
| 5 |
import os |
| 6 |
from io import BytesIO |
| 7 |
from awscrt import io, http |
| 8 |
from urllib.parse import urlparse |
| 9 |
|
| 10 |
|
| 11 |
def print_header_list(headers): |
| 12 |
for name, value in headers: |
| 13 |
print('{}: {}'.format(name, value)) |
| 14 |
|
| 15 |
|
| 16 |
parser = argparse.ArgumentParser() |
| 17 |
parser.add_argument( |
| 18 |
'url', |
| 19 |
help='URL to make request to. HTTPS is assumed unless port 80 is specified or HTTP is specified in the scheme.') |
| 20 |
parser.add_argument('--cacert', required=False, help='FILE: path to a CA certificate file.') |
| 21 |
parser.add_argument('--capath', required=False, help='PATH: path to a directory containing CA files.') |
| 22 |
parser.add_argument('--cert', required=False, help='FILE: path to a PEM encoded certificate to use with mTLS') |
| 23 |
parser.add_argument('--key', required=False, help='FILE: Path to a PEM encoded private key that matches cert.') |
| 24 |
parser.add_argument( |
| 25 |
'--connect_timeout', |
| 26 |
required=False, |
| 27 |
type=int, |
| 28 |
help='INT: time in milliseconds to wait for a connection.', |
| 29 |
default=5000) |
| 30 |
parser.add_argument( |
| 31 |
'-H', |
| 32 |
'--header', |
| 33 |
required=False, |
| 34 |
help='STRING: line to send as a header in format "name:value". May be specified multiple times.', |
| 35 |
action='append') |
| 36 |
parser.add_argument('-d', '--data', required=False, help='STRING: Data to POST or PUT.') |
| 37 |
parser.add_argument('--data_file', required=False, help='FILE: File to read from file and POST or PUT') |
| 38 |
parser.add_argument( |
| 39 |
'-M', |
| 40 |
'--method', |
| 41 |
required=False, |
| 42 |
help='STRING: Http Method verb to use for the request', |
| 43 |
default='GET') |
| 44 |
parser.add_argument('-G', '--get', required=False, help='uses GET for the verb', action='store_true') |
| 45 |
parser.add_argument('-P', '--post', required=False, help='uses POST for the verb', action='store_true') |
| 46 |
parser.add_argument('-I', '--head', required=False, help='uses HEAD for the verb', action='store_true') |
| 47 |
parser.add_argument( |
| 48 |
'-i', |
| 49 |
'--include', |
| 50 |
required=False, |
| 51 |
help='Includes headers in output', |
| 52 |
action='store_true', |
| 53 |
default=False) |
| 54 |
parser.add_argument( |
| 55 |
'-k', |
| 56 |
'--insecure', |
| 57 |
required=False, |
| 58 |
help='Turns off x.509 validation', |
| 59 |
action='store_true', |
| 60 |
default=False) |
| 61 |
parser.add_argument('-o', '--output', required=False, help='FILE: dumps content-body to FILE instead of stdout.') |
| 62 |
parser.add_argument('-t', '--trace', required=False, help='FILE: dumps logs to FILE instead of stderr.') |
| 63 |
parser.add_argument( |
| 64 |
'-p', |
| 65 |
'--alpn', |
| 66 |
default=["h2", "http/1.1"], |
| 67 |
required=False, |
| 68 |
help='STRING: protocol for ALPN. May be specified multiple times.', |
| 69 |
action='append') |
| 70 |
parser.add_argument( |
| 71 |
'--http2', |
| 72 |
required=False, |
| 73 |
help='HTTP/2 connection required', |
| 74 |
action="store_true") |
| 75 |
parser.add_argument( |
| 76 |
'--http1_1', |
| 77 |
required=False, |
| 78 |
help='HTTP/1.1 connection required', |
| 79 |
action="store_true") |
| 80 |
parser.add_argument( |
| 81 |
'-v', |
| 82 |
'--verbose', |
| 83 |
required=False, |
| 84 |
help='ERROR|INFO|DEBUG|TRACE: log level to configure. Default is none.') |
| 85 |
|
| 86 |
args = parser.parse_args() |
| 87 |
|
| 88 |
output = getattr(sys.stdout, 'buffer', sys.stdout) |
| 89 |
|
| 90 |
if args.output: |
| 91 |
output = open(args.output, mode='wb') |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if args.verbose: |
| 96 |
log_level = io.LogLevel.NoLogs |
| 97 |
|
| 98 |
if args.verbose == 'ERROR': |
| 99 |
log_level = io.LogLevel.Error |
| 100 |
elif args.verbose == 'INFO': |
| 101 |
log_level = io.LogLevel.Info |
| 102 |
elif args.verbose == 'DEBUG': |
| 103 |
log_level = io.LogLevel.Debug |
| 104 |
elif args.verbose == 'TRACE': |
| 105 |
log_level = io.LogLevel.Trace |
| 106 |
else: |
| 107 |
print('{} unsupported value for the verbose option'.format(args.verbose)) |
| 108 |
exit(-1) |
| 109 |
|
| 110 |
log_output = 'stderr' |
| 111 |
|
| 112 |
if args.trace: |
| 113 |
log_output = args.trace |
| 114 |
|
| 115 |
io.init_logging(log_level, log_output) |
| 116 |
|
| 117 |
|
| 118 |
required_version = http.HttpVersion.Unknown |
| 119 |
if args.http1_1: |
| 120 |
required_version = http.HttpVersion.Http1_1 |
| 121 |
args.alpn = ["http/1.1"] |
| 122 |
if args.http2: |
| 123 |
required_version = http.HttpVersion.Http2 |
| 124 |
args.alpn = ["h2"] |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
event_loop_group = io.EventLoopGroup(1) |
| 130 |
|
| 131 |
host_resolver = io.DefaultHostResolver(event_loop_group) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) |
| 136 |
|
| 137 |
url = urlparse(args.url) |
| 138 |
port = 443 |
| 139 |
scheme = 'https' |
| 140 |
|
| 141 |
if url.scheme is not None and url.scheme == 'http': |
| 142 |
scheme = 'http' |
| 143 |
|
| 144 |
if url.port is not None: |
| 145 |
port = url.port |
| 146 |
else: |
| 147 |
if scheme == 'http': |
| 148 |
port = 80 |
| 149 |
if args.http2: |
| 150 |
sys.exit("Error, we don't support h2c, please use TLS for HTTP/2 connection") |
| 151 |
|
| 152 |
|
| 153 |
tls_connection_options = None |
| 154 |
|
| 155 |
if scheme == 'https': |
| 156 |
if args.cert is not None and args.key is not None: |
| 157 |
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(args.cert, args.key) |
| 158 |
else: |
| 159 |
tls_ctx_options = io.TlsContextOptions() |
| 160 |
|
| 161 |
if args.cacert is not None or args.capath is not None: |
| 162 |
tls_ctx_options.override_default_trust_store_from_path(args.capath, args.cacert) |
| 163 |
|
| 164 |
if args.insecure: |
| 165 |
tls_ctx_options.verify_peer = False |
| 166 |
|
| 167 |
tls_ctx = io.ClientTlsContext(tls_ctx_options) |
| 168 |
|
| 169 |
tls_connection_options = tls_ctx.new_connection_options() |
| 170 |
tls_connection_options.set_server_name(url.hostname) |
| 171 |
|
| 172 |
if args.alpn: |
| 173 |
tls_connection_options.set_alpn_list(args.alpn) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
def on_connection_shutdown(shutdown_future): |
| 179 |
print('connection close with error: {}'.format(shutdown_future.exception())) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
def on_incoming_body(http_stream, chunk, **kwargs): |
| 184 |
output.write(chunk) |
| 185 |
|
| 186 |
|
| 187 |
data_len = 0 |
| 188 |
data_stream = None |
| 189 |
|
| 190 |
if args.data: |
| 191 |
data_bytes = args.data.encode(encoding='utf-8') |
| 192 |
data_len = len(data_bytes) |
| 193 |
data_stream = BytesIO(data_bytes) |
| 194 |
elif args.data_file: |
| 195 |
data_len = os.stat(args.data_file).st_size |
| 196 |
data_stream = open(args.data_file, 'rb') |
| 197 |
|
| 198 |
|
| 199 |
socket_options = io.SocketOptions() |
| 200 |
socket_options.connect_timeout_ms = args.connect_timeout |
| 201 |
|
| 202 |
hostname = url.hostname |
| 203 |
connect_future = http.HttpClientConnection.new( |
| 204 |
host_name=hostname, |
| 205 |
port=port, |
| 206 |
socket_options=socket_options, |
| 207 |
tls_connection_options=tls_connection_options, |
| 208 |
bootstrap=client_bootstrap) |
| 209 |
|
| 210 |
connection = connect_future.result(10) |
| 211 |
connection.shutdown_future.add_done_callback(on_connection_shutdown) |
| 212 |
|
| 213 |
if required_version: |
| 214 |
if connection.version != required_version: |
| 215 |
error_msg = "Error. The requested HTTP version " + args.alpn[0] + " is not supported by the peer." |
| 216 |
sys.exit(error_msg) |
| 217 |
|
| 218 |
|
| 219 |
request = http.HttpRequest(args.method, body_stream=data_stream) |
| 220 |
|
| 221 |
if args.get: |
| 222 |
request.method = "GET" |
| 223 |
|
| 224 |
if args.post: |
| 225 |
request.method = "POST" |
| 226 |
|
| 227 |
if args.head: |
| 228 |
request.method = "HEAD" |
| 229 |
|
| 230 |
if url.path: |
| 231 |
request.path = url.path |
| 232 |
|
| 233 |
if url.query: |
| 234 |
request.path += '?' + url.query |
| 235 |
|
| 236 |
if connection.version == http.HttpVersion.Http2: |
| 237 |
request.headers.add(':authority', hostname) |
| 238 |
else: |
| 239 |
request.headers.add('host', hostname) |
| 240 |
request.headers.add('user-agent', 'elasticurl.py 1.0, Powered by the AWS Common Runtime.') |
| 241 |
|
| 242 |
if data_len != 0: |
| 243 |
request.headers.add('content-length', str(data_len)) |
| 244 |
|
| 245 |
if args.header: |
| 246 |
for i in args.header: |
| 247 |
name, value = i.split(':') |
| 248 |
request.headers.add(name.strip(), value.strip()) |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
def response_received_cb(http_stream, status_code, headers, **kwargs): |
| 254 |
if args.include: |
| 255 |
print('Response Code: {}'.format(status_code)) |
| 256 |
print_header_list(headers) |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
stream = connection.request(request, response_received_cb, on_incoming_body) |
| 261 |
stream.activate() |
| 262 |
|
| 263 |
|
| 264 |
stream.completion_future.result() |
| 265 |
stream = None |
| 266 |
connection = None |
| 267 |
|
| 268 |
if data_stream: |
| 269 |
data_stream.close() |
| 270 |
|
| 271 |
if args.output: |
| 272 |
output.close() |