<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from future . builtins import filter , str <EOL> from future import utils <EOL> import os <EOL> import sys <EOL> import ssl <EOL> import pprint <EOL> import socket <EOL> from future . backports . urllib import parse as urllib_parse <EOL> from future . backports . http . server import ( HTTPServer as _HTTPServer , <EOL> SimpleHTTPRequestHandler , BaseHTTPRequestHandler ) <EOL> from future . backports . test import support <EOL> threading = support . import_module ( "<STR_LIT>" ) <EOL> here = os . path . dirname ( __file__ ) <EOL> HOST = support . HOST <EOL> CERTFILE = os . path . join ( here , '<STR_LIT>' ) <EOL> class HTTPSServer ( _HTTPServer ) : <EOL> def __init__ ( self , server_address , handler_class , context ) : <EOL> _HTTPServer . __init__ ( self , server_address , handler_class ) <EOL> self . context = context <EOL> def __str__ ( self ) : <EOL> return ( '<STR_LIT>' % <EOL> ( self . __class__ . __name__ , <EOL> self . server_name , <EOL> self . server_port ) ) <EOL> def get_request ( self ) : <EOL> try : <EOL> sock , addr = self . socket . accept ( ) <EOL> sslconn = self . context . wrap_socket ( sock , server_side = True ) <EOL> except socket . error as e : <EOL> if support . verbose : <EOL> sys . stderr . write ( "<STR_LIT>" % e ) <EOL> raise <EOL> return sslconn , addr <EOL> class RootedHTTPRequestHandler ( SimpleHTTPRequestHandler ) : <EOL> server_version = "<STR_LIT>" <EOL> root = here <EOL> timeout = <NUM_LIT:5> <EOL> def translate_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> path = urllib . parse . urlparse ( path ) [ <NUM_LIT:2> ] <EOL> path = os . path . normpath ( urllib . parse . unquote ( path ) ) <EOL> words = path . split ( '<STR_LIT:/>' ) <EOL> words = filter ( None , words ) <EOL> path = self . root <EOL> for word in words : <EOL> drive , word = os . path . splitdrive ( word ) <EOL> head , word = os . path . split ( word ) <EOL> path = os . path . join ( path , word ) <EOL> return path <EOL> def log_message ( self , format , * args ) : <EOL> if support . verbose : <EOL> sys . stdout . write ( "<STR_LIT>" % <EOL> ( self . server . server_address , <EOL> self . server . server_port , <EOL> self . request . cipher ( ) , <EOL> self . log_date_time_string ( ) , <EOL> format % args ) ) <EOL> class StatsRequestHandler ( BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> server_version = "<STR_LIT>" <EOL> def do_GET ( self , send_body = True ) : <EOL> """<STR_LIT>""" <EOL> sock = self . rfile . raw . _sock <EOL> context = sock . context <EOL> stats = { <EOL> '<STR_LIT>' : context . session_stats ( ) , <EOL> '<STR_LIT>' : sock . cipher ( ) , <EOL> '<STR_LIT>' : sock . compression ( ) , <EOL> } <EOL> body = pprint . pformat ( stats ) <EOL> body = body . encode ( '<STR_LIT:utf-8>' ) <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , str ( len ( body ) ) ) <EOL> self . end_headers ( ) <EOL> if send_body : <EOL> self . wfile . write ( body ) <EOL> def do_HEAD ( self ) : <EOL> """<STR_LIT>""" <EOL> self . do_GET ( send_body = False ) <EOL> def log_request ( self , format , * args ) : <EOL> if support . verbose : <EOL> BaseHTTPRequestHandler . log_request ( self , format , * args ) <EOL> class HTTPSServerThread ( threading . Thread ) : <EOL> def __init__ ( self , context , host = HOST , handler_class = None ) : <EOL> self . flag = None <EOL> self . server = HTTPSServer ( ( host , <NUM_LIT:0> ) , <EOL> handler_class or RootedHTTPRequestHandler , <EOL> context ) <EOL> self . port = self . server . server_port <EOL> threading . Thread . __init__ ( self ) <EOL> self . daemon = True <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . server ) <EOL> def start ( self , flag = None ) : <EOL> self . flag = flag <EOL> threading . Thread . start ( self ) <EOL> def run ( self ) : <EOL> if self . flag : <EOL> self . flag . set ( ) <EOL> try : <EOL> self . server . serve_forever ( <NUM_LIT> ) <EOL> finally : <EOL> self . server . server_close ( ) <EOL> def stop ( self ) : <EOL> self . server . shutdown ( ) <EOL> def make_https_server ( case , certfile = CERTFILE , host = HOST , handler_class = None ) : <EOL> context = ssl . SSLContext ( ssl . PROTOCOL_SSLv23 ) <EOL> context . load_cert_chain ( certfile ) <EOL> server = HTTPSServerThread ( context , host , handler_class ) <EOL> flag = threading . Event ( ) <EOL> server . start ( flag ) <EOL> flag . wait ( ) <EOL> def cleanup ( ) : <EOL> if support . verbose : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> server . stop ( ) <EOL> if support . verbose : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> server . join ( ) <EOL> case . addCleanup ( cleanup ) <EOL> return server <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import argparse <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , default = <NUM_LIT> , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = str , <EOL> action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = str , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> support . verbose = args . verbose <EOL> if args . use_stats_handler : <EOL> handler_class = StatsRequestHandler <EOL> else : <EOL> handler_class = RootedHTTPRequestHandler <EOL> if utils . PY2 : <EOL> handler_class . root = os . getcwdu ( ) <EOL> else : <EOL> handler_class . root = os . getcwd ( ) <EOL> context = ssl . SSLContext ( ssl . PROTOCOL_TLSv1 ) <EOL> context . load_cert_chain ( CERTFILE ) <EOL> if args . curve_name : <EOL> context . set_ecdh_curve ( args . curve_name ) <EOL> if args . dh_file : <EOL> context . load_dh_params ( args . dh_file ) <EOL> server = HTTPSServer ( ( "<STR_LIT>" , args . port ) , handler_class , context ) <EOL> if args . verbose : <EOL> print ( "<STR_LIT>" . format ( args ) ) <EOL> server . serve_forever ( <NUM_LIT:0.1> ) </s>
