class HttpReader: """ Reads an HTTP response from a BufferedReader """ def __init__(self, reader): self.reader = reader self.headers = None def read(self): # Parse the headers string into a Headers structure self.headers = Headers(self.reader.read_until(SECTION_STOP)) # Read the rest of the content depending on the transfer encoding if self.headers.get_token('Transfer-Encoding') == 'chunked': self._read_chunked() else: self._read_normal() @property def raw_content(self): """ Returns the read content as a byte array """ return self.reader.raw_content @property def content(self): """ Returns the read content as a string """ return self.raw_content.decode() def _read_chunked(self): # For data format, please see https://en.wikipedia.org/wiki/Chunked_transfer_encoding while True: chunk_size = self._read_chunk_size() # Read chunk data plus the trailing newline self.reader.read_bytes(chunk_size) self.reader.read_bytes(len(NEWLINE)) if chunk_size == 0: # Chunked data is terminated by a chunk of size 0 break def _read_normal(self): content_length_str = self.headers.get('Content-Length') if content_length_str != '': self.reader.read_bytes(int(content_length_str)) def _read_chunk_size(self): size_bytes = self.reader.read_until(NEWLINE) size_hex = size_bytes.decode() return int(size_hex, 16)
def read(self): # Parse the headers string into a Headers structure self.headers = Headers(self.reader.read_until(SECTION_STOP)) # Read the rest of the content depending on the transfer encoding if self.headers.get_token('Transfer-Encoding') == 'chunked': self._read_chunked() else: self._read_normal()
def __init__(self, name): AssetContainer.__init__(self, name) from Archive import Archive self.archive = Archive(name) from Headers import Headers self._headers = Headers(name) return
def do_GET(self): parsed_headers = Headers(headers=self.headers.items(), debug=True, ip=self.client_address[0], port = self.client_address[1]) returned_headers = parsed_headers.input_parsed_headers() self.send_response(200) self.end_headers() return
def __init__(self, unparsed_message): self.raw = unparsed_message try: headers, data = unparsed_message.split('\r\n\r\n', 1) #headers = headers.replace('\r', '') self.headers = Headers(headers) except (ValueError, HeaderFormatError): self.headers = Headers("") data = unparsed_message self.data = data self.send = True
def create_membership(creator, room, new_member): headers = Headers.get_auth_json_header(creator.access_token) data = {'roomId': '%s' % room['id'], 'personEmail': new_member.email} return Api.post(url='%s/memberships' % Config.get('services')[test_service_name], data=json.dumps(data), headers=headers)
def create_room(title='Default Title', access_token=None): headers = Headers.get_auth_json_header(access_token) data = {'title': title} return Api.post(url='%s/rooms' % Config.get('services')[test_service_name], data=json.dumps(data), headers=headers)
class Library(AssetContainer): def identify(self, inspector): return inspector.onLibrary(self) def __init__(self, name): AssetContainer.__init__(self, name) from Archive import Archive self.archive = Archive(name) from Headers import Headers self._headers = Headers(name) return def _getHeaders(self): return self._headers def _setHeaders(self, headers): from Header import Header headers = [ Header(name) for name in headers ] self._headers.append(headers) return def _getSources(self): return self.archive.sources def _setSources(self, files): self.archive.sources = files return headers = property(_getHeaders, _setHeaders, None, "") sources = property(_getSources, _setSources, None, "")
def delete_membership(user, membership): headers = Headers.get_auth_json_header(user.access_token) response = requests.delete( url='%s/memberships/%s' % (Config.get('services')[test_service_name], membership['id']), headers=headers) if 200 <= response.status_code < 300: logging.debug('Success') else: logging.error('Failed with error: %s', response.text)
def get_membership(room_id, access_token=None): headers = Headers.get_auth_json_header(access_token) response = requests.get( url='%s/memberships?roomId=%s' % (Config.get('services')[test_service_name], room_id), headers=headers) if 200 <= response.status_code < 300: logging.debug('Success') else: logging.error('Failed with error: %s', response.text)
def post_message(token, room_id, email, message): template = '<@personEmail:%s> %s' headers = Headers.get_auth_json_header(token) data = { 'roomId': '%s' % room_id, 'markdown': template % (email, message) } return Api.post(url='%s/messages' % Config.get('services')[test_service_name], data=data, headers=headers)
class Library(AssetContainer): def identify(self, inspector): try: return inspector.onLibrary(self) except AttributeError: return super(Library, self).identify(inspector) def __init__(self, name): AssetContainer.__init__(self, name) from Archive import Archive self.archive = Archive(name) from Headers import Headers self._headers = Headers(name) return def _getHeaders(self): return self._headers def _setHeaders(self, headers): from Header import Header headers = [Header(name) for name in headers] self._headers.append(headers) return def _getSources(self): return self.archive.sources def _setSources(self, files): self.archive.sources = files return headers = property(_getHeaders, _setHeaders, None, "") sources = property(_getSources, _setSources, None, "")
class HTTP_Message: def __init__(self, unparsed_message): self.raw = unparsed_message try: headers, data = unparsed_message.split('\r\n\r\n', 1) #headers = headers.replace('\r', '') self.headers = Headers(headers) except (ValueError, HeaderFormatError): self.headers = Headers("") data = unparsed_message self.data = data self.send = True def reform(self): message = self.headers.reform() + "\r\n\r\n" + self.data #message = message.replace('\n', '\n\r') return message def decompress(self): try: if self.headers.headers["Content-Encoding"]['value'] == 'gzip': d = "\r\n".join(self.data.split('\r\n')[1:-3]) self.data = gzip.GzipFile('', 'rb', 9, StringIO.StringIO(d)).read() del(self.headers.headers["Content-Encoding"]) del(self.headers.headers["Transfer-Encoding"]) except KeyError: # Not compressed. pass def recalculate_content_length(self): try: self.headers.headers["Content-Length"]['value'] = len(self.data) except KeyError: pass def inject_sunlight(self): sunlight = '<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>' sunlight += '<script src="https://sunlight.henryirish.com/jquery.hammer.min.js"></script>' sunlight += '<script src="https://sunlight.henryirish.com/sunlight.js"></script>' if "<html" in self.data and "</body>" in self.data: self.data = self.data.replace("</body>", sunlight + "</body>")
class Observation(Talker): '''Observation object store basic information about an observation of one object on one night.''' def __init__(self, filename, nods9=False, **kwargs): '''Initialize an observation object.''' # decide whether or not this creature is chatty Talker.__init__(self, **kwargs) self.readParameters(filename) self.fileprefixes = self.fileprefix(self.nNeeded) zachopy.utils.mkdir(self.workingDirectory) #self.display = Display(nods9=nods9) def loadHeaders(self, remake=False): self.headers = Headers(self, mute=self._mute, pithy=self._pithy) self.headers.load(remake=remake) def readParameters(self, filename): '''A function to read in a stored parameter file, with all details needed for extraction.''' self.speak( 'trying to read {0} for observation parameters'.format(filename)) file = open(filename) lines = file.readlines() dictionary = {} for i in range(len(lines)): if lines[i] != '\n' and lines[i][0] != '#': split = lines[i].split() key = split[0] entries = split[1:] if len(entries) == 1: entries = entries[0] dictionary[key] = entries self.name = dictionary['name'] self.night = dictionary['night'] self.grism = dictionary['grism'].lower() self.instrument = dictionary['instrument'] if "LDSS" in self.instrument: self.observatory = 'lco' self.baseDirectory = dictionary['baseDirectory'] if '/media/hannah/Seagate' in self.baseDirectory: self.baseDirectory = ' '.join(dictionary['baseDirectory']) # set up the wavelength calibration paths self.referenceDirectory = mosasaurusdirectory + 'data/' self.wavelength2pixelsFile = self.referenceDirectory + '{0}_wavelength_identifications.txt'.format( self.grism) self.wavelengthsFile = self.referenceDirectory + 'HeNeAr.txt' zachopy.utils.mkdir(self.baseDirectory + dictionary['workingDirectory']) self.workingDirectory = self.baseDirectory + dictionary[ 'workingDirectory'] + self.name + '_' + self.night + '/' zachopy.utils.mkdir(self.workingDirectory) self.dataDirectory = self.baseDirectory + dictionary[ 'dataDirectory'] + self.night + '/' zachopy.utils.mkdir(self.dataDirectory) self.extractionDirectory = self.workingDirectory + dictionary[ 'extractionDirectory'] zachopy.utils.mkdir(self.extractionDirectory) #self.extractionWidth = int(dictionary['extractionWidth']) self.narrowest = float(dictionary['narrowest']) self.widest = float(dictionary['widest']) self.numberofapertures = int(dictionary['numberofapertures']) self.skyGap = int(dictionary['skyGap']) self.skyWidth = int(dictionary['skyWidth']) self.cosmicThreshold = float(dictionary['cosmicThreshold']) self.nUndispersed = np.arange(int(dictionary['nUndispersed'][0]), int(dictionary['nUndispersed'][1]) + 1) self.nScience = np.arange(int(dictionary['nScience'][0]), int(dictionary['nScience'][1]) + 1) self.nHe = np.arange(int(dictionary['nHe'][0]), int(dictionary['nHe'][1]) + 1) self.nNe = np.arange(int(dictionary['nNe'][0]), int(dictionary['nNe'][1]) + 1) self.nAr = np.arange(int(dictionary['nAr'][0]), int(dictionary['nAr'][1]) + 1) self.nDark = np.arange(int(dictionary['nDark'][0]), int(dictionary['nDark'][1]) + 1) self.nWideFlat = np.arange(int(dictionary['nWideFlat'][0]), int(dictionary['nWideFlat'][1]) + 1) self.nWideMask = np.arange(int(dictionary['nWideMask'][0]), int(dictionary['nWideMask'][1]) + 1) self.nThinMask = np.arange(int(dictionary['nThinMask'][0]), int(dictionary['nThinMask'][1]) + 1) if len(dictionary['nFinder']) == 1: self.nFinder = np.array([int(dictionary['nFinder'])]) else: self.nFinder = np.arange(int(dictionary['nFinder'][0]), int(dictionary['nFinder'][1]) + 1) self.nBias = np.arange(int(dictionary['nBias'][0]), int(dictionary['nBias'][1]) + 1) self.nNeeded = np.concatenate( (self.nUndispersed, self.nScience, self.nHe, self.nNe, self.nAr, self.nWideFlat, self.nWideMask, self.nThinMask, self.nFinder)) self.cal_dictionary = { 'He': self.nHe, 'Ne': self.nNe, 'Ar': self.nAr, 'Undispersed': self.nUndispersed, 'WideFlat': self.nWideFlat, 'WideMask': self.nWideMask, 'ThinMask': self.nThinMask, 'Bias': self.nBias, 'Dark': self.nDark, 'Science': self.nScience, 'Finder': self.nFinder } self.traceOrder = int(dictionary['traceOrder']) self.nFWHM = float(dictionary['nFWHM']) self.blueward = int(dictionary['blueward']) self.redward = int(dictionary['redward']) self.target = [int(x) for x in dictionary['target']] self.goodComps = [int(x) for x in dictionary['comp']] self.dataleft = int(dictionary['dataleft']) self.dataright = int(dictionary['dataright']) self.databottom = int(dictionary['databottom']) self.datatop = int(dictionary['datatop']) self.namps = int(dictionary['namps']) self.xsize = self.namps * (self.dataright - self.dataleft) self.ysize = (self.datatop - self.databottom) self.ra = np.float(dictionary['ra']) self.dec = np.float(dictionary['dec']) self.binning = np.float(dictionary['binning']) self.subarray = np.float(dictionary['subarray']) try: self.gains = [float(x) for x in dictionary['gains']] except (ValueError, KeyError): self.gains = None try: self.slow = bool(dictionary['slow']) except KeyError: self.slow = False self.correlationAnchors = [ float(x) for x in dictionary['correlationAnchors'] ] self.correlationRange = [ float(x) for x in dictionary['correlationRange'] ] self.correlationSmooth = float(dictionary['correlationSmooth']) self.cosmicAbandon = float(dictionary['cosmicAbandon']) self.speak('observation parameters have been read and stored'.format( filename)) self.displayscale = 0.25 def fileprefix(self, n): '''Feed in a ccd number, spit out the file prefix for that CCD amplifier pair.''' try: return [self.dataDirectory + 'ccd{0:04}'.format(x) for x in n] except: return self.dataDirectory + 'ccd{0:04}'.format(n)
def BASIC(self, what): if what == None: self.what = "GET" else: self.what = what self.parse_query() # Comprobamos si nos están pidiendo nuestra URL # Por defecto no nos piden a nosotros selfquery = False ## TODO: aqui hay que cambiar que empiece por la cadena en vez de que sea la cadena ## TODO: cuando atacamos localhost:PUERTO (modo proxy), devuelve OK, pero cuando se ataca el servicio ## TODO: sin puerto, se devuelve como contenido las cabeceras if self.parsed_path.netloc in ["127.0.0.1", "localhost", ""]: # or '' ] selfquery = True # TODO:: DESCOMENTAR ## La siguiente linea fuerza el uso de modo proxy ## selfquery = False # TODO:: DESCOMENTAR # Si es una dirección local, atendemos la petición HTTP ####################################################### if selfquery: if DEBUG: pprint(self.parsed_path) print("Local, %s %s" % (selfquery, self.what)) self.Allowed = self.handle_HTTP_AUTH() if DEBUG: print("autorizado by HTTP_AUTH: %s " % self.Allowed) if self.Allowed == 1: # handler por defecto... service_handle_value = "NOOP" service_handle_parms = "" print("self.what: %s " % self.what) if self.what in ["GET", "POST", "HEAD"]: # Para cada uno de los servicios que atenderemos... (LocalServices es una lista de servicios vs handlers) for URL in self.LocalServices: if DEBUG: print("\tm%s" % URL.pattern) if URL.match(self.parsed_path.path): # print re.sub(URL.pattern.upper()) # Sustituimos el patron entre "/" y hacemos strip de los caracteres adicionales service_handle_value = re.sub(r"/[.]*$", "", re.sub(r"^/+", "", URL.pattern.upper())) service_handle_parms = re.sub(r"(?i)" + URL.pattern, "", self.parsed_path.path) # break # Llamamos a la funcion que se llame svc_hndl_$(PATRON) # Por defecto, se llama a la funcion NOOP pprint(self.parsed_path, indent=1, depth=80) pprint(object) self.ServiceHandle[service_handle_value]( self, service_handle_parms, self.parsed_path.query, Verb=self.what ) service_handle_value = "NOOP" else: self.do_OTHER() else: self.int_HEAD_HTTP_AUTH() # Tratamos la peticion como PROXY # ###################################################### else: if DEBUG: pprint(self.parsed_path) # Está autenticado? # Habilitamos la autenticacion o forzamos autenticado.. # TODO:: DESCOMENTAR self.Allowed = self.handle_PROXY_AUTH() if DEBUG: print("autorizado by PROXY_AUTH: %s " % self.Allowed) # self.Allowed = 1 # TODO:: DESCOMENTAR if self.Allowed == 1: self.client_headers = {} # print(">>>>>>>>") for k, v in self.headers.items(): # print('>>>> %s -> %s' %(k,v)) if k not in ["Cookie", "Proxy-Authorization"]: self.client_headers[k] = v # print("<<<<<<<<") try: if DEBUG: print("URL: %s: %s" % (self.client_address[1], self.path)) if DEBUG: print("try request") # Si la petición contiene datos... if "content-length" in self.headers: self.content = self.rfile.read(int(self.headers.getheader("content-length"))) if DEBUG: sys.stderr.write("\ncontent") pprint(self.content) # Filtramos el tipo de servicio y llamamos a un bloque o a otro... if self.what == "GET": # pprint(vars(self)) resp = requests.get(self.path, stream=True, timeout=5, allow_redirects=True) elif self.what == "POST": resp = requests.post( self.path, headers=self.headers, data=self.content, stream=True, allow_redirects=True ) elif self.what == "HEAD": resp = requests.head(self.path, stream=True, allow_redirects=True) except: print("excepcion en el request: %s %s" % (self.what, self.path)) respuesta = self.int_get_html_message( "Se ha producido una excepción accediendo a la URL<blockquote>%s</blockquote>" % (self.path) ) self.int_send_HEADERS(504, respuesta) self.int_send_BODY(respuesta) # self.send_response(504) # self.end_headers() else: if DEBUG: print("else") if DEBUG: pprint(vars(resp)) # Es un código http valido? if resp.status_code == requests.codes.ok: # enviamos el codigo try: self.send_response(resp.status_code) # Procesamos las cabeceras para reescribirlas parsed_headers = Headers( response=resp, debug=False, ip=self.client_address[0], port=self.client_address[1] ) returned_headers = parsed_headers.parsed_headers() # Enviamos todas las cabeceras reescritas for header in returned_headers: self.send_header(header[0], header[1]) # Fin de cabeceras self.end_headers() if self.what != "HEAD": self.wfile.write(resp.content) except: # si encontramos una excepcion... sys.stderr.write("excepcion a") pass # NO es codigo ok else: pass else: self.int_HEAD_PROXY_AUTH() if DEBUG: print("end") return
def headers(name): from Headers import Headers return Headers(name)
def loadHeaders(self, remake=False): self.headers = Headers(self, mute=self._mute, pithy=self._pithy) self.headers.load(remake=remake)