<s> """<STR_LIT>""" <EOL> import BaseHTTPServer <EOL> import cgi <EOL> import cStringIO <EOL> import pdb <EOL> import socket <EOL> import SocketServer <EOL> import threading <EOL> import ipaddr <EOL> import logging <EOL> from grr . lib import server_plugins <EOL> from grr . lib import aff4 <EOL> from grr . lib import communicator <EOL> from grr . lib import config_lib <EOL> from grr . lib import flags <EOL> from grr . lib import flow <EOL> from grr . lib import master <EOL> from grr . lib import rdfvalue <EOL> from grr . lib import startup <EOL> from grr . lib import stats <EOL> from grr . lib import type_info <EOL> from grr . lib import utils <EOL> from grr . lib . flows . general import file_finder <EOL> from grr . lib . rdfvalues import flows as rdf_flows <EOL> class GRRHTTPServerHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> statustext = { <NUM_LIT:200> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" } <EOL> active_counter_lock = threading . Lock ( ) <EOL> active_counter = <NUM_LIT:0> <EOL> def Send ( self , data , status = <NUM_LIT:200> , ctype = "<STR_LIT>" , <EOL> last_modified = <NUM_LIT:0> ) : <EOL> self . wfile . write ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> "<STR_LIT:%s>" ) % <EOL> ( self . statustext [ status ] , ctype , len ( data ) , <EOL> self . date_time_string ( last_modified ) , data ) ) <EOL> def do_GET ( self ) : <EOL> """<STR_LIT>""" <EOL> url_prefix = config_lib . CONFIG [ "<STR_LIT>" ] <EOL> if self . path . startswith ( "<STR_LIT>" ) : <EOL> self . ServerPem ( ) <EOL> elif self . path . startswith ( url_prefix ) : <EOL> path = self . path [ len ( url_prefix ) : ] <EOL> self . ServeStatic ( path ) <EOL> AFF4_READ_BLOCK_SIZE = <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> <EOL> def ServeStatic ( self , path ) : <EOL> static_aff4_prefix = config_lib . CONFIG [ "<STR_LIT>" ] <EOL> aff4_path = rdfvalue . RDFURN ( static_aff4_prefix ) . Add ( path ) <EOL> try : <EOL> logging . info ( "<STR_LIT>" , aff4_path ) <EOL> fd = aff4 . FACTORY . Open ( aff4_path , token = aff4 . FACTORY . root_token ) <EOL> while True : <EOL> data = fd . Read ( self . AFF4_READ_BLOCK_SIZE ) <EOL> if not data : <EOL> break <EOL> self . Send ( data ) <EOL> except ( IOError , AttributeError ) : <EOL> self . Send ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> def ServerPem ( self ) : <EOL> self . Send ( self . server . server_cert ) <EOL> RECV_BLOCK_SIZE = <NUM_LIT> <EOL> def _GetPOSTData ( self , length ) : <EOL> input_data = cStringIO . StringIO ( ) <EOL> while length >= <NUM_LIT:0> : <EOL> read_size = min ( self . RECV_BLOCK_SIZE , length ) <EOL> data = self . rfile . read ( read_size ) <EOL> if not data : <EOL> break <EOL> input_data . write ( data ) <EOL> length -= len ( data ) <EOL> return input_data . getvalue ( ) <EOL> def do_POST ( self ) : <EOL> """<STR_LIT>""" <EOL> self . Control ( ) <EOL> @ stats . Counted ( "<STR_LIT>" , fields = [ "<STR_LIT:http>" ] ) <EOL> @ stats . Timed ( "<STR_LIT>" , fields = [ "<STR_LIT:http>" ] ) <EOL> def Control ( self ) : <EOL> """<STR_LIT>""" <EOL> if not master . MASTER_WATCHER . IsMaster ( ) : <EOL> stats . STATS . IncrementCounter ( "<STR_LIT>" , <EOL> fields = [ "<STR_LIT:http>" ] ) <EOL> logging . info ( "<STR_LIT>" , <EOL> self . client_address [ <NUM_LIT:0> ] ) <EOL> try : <EOL> api_version = int ( cgi . parse_qs ( self . path . split ( "<STR_LIT:?>" ) [ <NUM_LIT:1> ] ) [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> except ( ValueError , KeyError , IndexError ) : <EOL> api_version = <NUM_LIT:3> <EOL> with GRRHTTPServerHandler . active_counter_lock : <EOL> GRRHTTPServerHandler . active_counter += <NUM_LIT:1> <EOL> stats . STATS . SetGaugeValue ( "<STR_LIT>" , self . active_counter , <EOL> fields = [ "<STR_LIT:http>" ] ) <EOL> try : <EOL> length = int ( self . headers . getheader ( "<STR_LIT>" ) ) <EOL> request_comms = rdf_flows . ClientCommunication ( self . _GetPOSTData ( length ) ) <EOL> if not request_comms . api_version : <EOL> request_comms . api_version = api_version <EOL> responses_comms = rdf_flows . ClientCommunication ( <EOL> api_version = request_comms . api_version ) <EOL> source_ip = ipaddr . IPAddress ( self . client_address [ <NUM_LIT:0> ] ) <EOL> if source_ip . version == <NUM_LIT:6> : <EOL> source_ip = source_ip . ipv4_mapped or source_ip <EOL> request_comms . orig_request = rdf_flows . HttpRequest ( <EOL> raw_headers = utils . SmartStr ( self . headers ) , <EOL> source_ip = utils . SmartStr ( source_ip ) ) <EOL> source , nr_messages = self . server . frontend . HandleMessageBundles ( <EOL> request_comms , responses_comms ) <EOL> logging . info ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> source , utils . SmartStr ( source_ip ) , length , nr_messages , <EOL> responses_comms . num_messages ) <EOL> self . Send ( responses_comms . SerializeToString ( ) ) <EOL> except communicator . UnknownClientCert : <EOL> self . Send ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> except Exception as e : <EOL> if flags . FLAGS . debug : <EOL> pdb . post_mortem ( ) <EOL> logging . error ( "<STR_LIT>" , e ) <EOL> self . Send ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> finally : <EOL> with GRRHTTPServerHandler . active_counter_lock : <EOL> GRRHTTPServerHandler . active_counter -= <NUM_LIT:1> <EOL> stats . STATS . SetGaugeValue ( "<STR_LIT>" , self . active_counter , <EOL> fields = [ "<STR_LIT:http>" ] ) <EOL> class GRRHTTPServer ( SocketServer . ThreadingMixIn , BaseHTTPServer . HTTPServer ) : <EOL> """<STR_LIT>""" <EOL> allow_reuse_address = True <EOL> request_queue_size = <NUM_LIT> <EOL> address_family = socket . AF_INET6 <EOL> def __init__ ( self , server_address , handler , frontend = None , * args , ** kwargs ) : <EOL> stats . STATS . SetGaugeValue ( "<STR_LIT>" , <EOL> self . request_queue_size ) <EOL> if frontend : <EOL> self . frontend = frontend <EOL> else : <EOL> self . frontend = flow . FrontEndServer ( <EOL> certificate = config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> private_key = config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> max_queue_size = config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> message_expiry_time = config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> max_retransmission_time = config_lib . CONFIG [ <EOL> "<STR_LIT>" ] ) <EOL> self . server_cert = config_lib . CONFIG [ "<STR_LIT>" ] <EOL> ( address , _ ) = server_address <EOL> version = ipaddr . IPAddress ( address ) . version <EOL> if version == <NUM_LIT:4> : <EOL> self . address_family = socket . AF_INET <EOL> elif version == <NUM_LIT:6> : <EOL> self . address_family = socket . AF_INET6 <EOL> logging . info ( "<STR_LIT>" , server_address ) <EOL> BaseHTTPServer . HTTPServer . __init__ ( self , server_address , handler , * args , <EOL> ** kwargs ) <EOL> def CreateServer ( frontend = None ) : <EOL> server_address = ( config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> config_lib . CONFIG [ "<STR_LIT>" ] ) <EOL> httpd = GRRHTTPServer ( server_address , GRRHTTPServerHandler , frontend = frontend ) <EOL> sa = httpd . socket . getsockname ( ) <EOL> logging . info ( "<STR_LIT>" , sa [ <NUM_LIT:0> ] , sa [ <NUM_LIT:1> ] ) <EOL> return httpd <EOL> def Serve ( server ) : <EOL> try : <EOL> server . serve_forever ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> def main ( unused_argv ) : <EOL> """<STR_LIT>""" <EOL> config_lib . CONFIG . AddContext ( "<STR_LIT>" ) <EOL> startup . Init ( ) <EOL> httpd = CreateServer ( ) <EOL> startup . DropPrivileges ( ) <EOL> try : <EOL> httpd . serve_forever ( ) <EOL> except KeyboardInterrupt : <EOL> print "<STR_LIT>" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> flags . StartMain ( main ) </s>
