def file(self): if self._file is not None: return self._file self._file = self._build_file_name(href=self.url) max_byte_size = self.max_input_size() # Create request try: reference_file = self._openurl(self.url, self.post_data) data_size = reference_file.headers.get('Content-Length', 0) except Exception as e: raise NoApplicableCode('File reference error: {}'.format(e)) error_message = 'File size for input "{}" exceeded. Maximum allowed: {} megabytes'.format( self.inpt.get('identifier', '?'), max_byte_size) if int(data_size) > int(max_byte_size): raise FileSizeExceeded(error_message) try: with open(self._file, 'wb') as f: data_size = 0 for chunk in reference_file.iter_content(chunk_size=1024): data_size += len(chunk) if int(data_size) > int(max_byte_size): raise FileSizeExceeded(error_message) f.write(chunk) except Exception as e: raise NoApplicableCode(e) return self._file
def href_handler(complexinput, datain): """<wps:Reference /> handler""" tmp_dir = config.get_config_value('server', 'workdir') # save the reference input in workdir tmp_file = tempfile.mkstemp(dir=complexinput.workdir)[1] try: (reference_file, reference_file_data) = _openurl(href) data_size = reference_file.headers.get('Content-Length', 0) except Exception as e: raise NoApplicableCode('File reference error: %s' % e) # if the response did not return a 'Content-Length' header then calculate the size if data_size == 0: data_size = _get_datasize(reference_file_data) # check if input file size was not exceeded complexinput.calculate_max_input_size() byte_size = complexinput.max_size * 1024 * 1024 if int(data_size) > int(byte_size): raise FileSizeExceeded('File size for input exceeded.' ' Maximum allowed: %i megabytes' %\ complexinput.max_size, complexinput.get('identifier')) try: with open(tmp_file, 'w') as f: f.write(reference_file_data) f.close() except Exception as e: raise NoApplicableCode(e) complexinput.file = tmp_file complexinput.url = href complexinput.as_reference = True
def _post_request(self): """HTTP GET request parser """ # check if input file size was not exceeded maxsize = configuration.get_config_value('server', 'maxrequestsize') maxsize = configuration.get_size_mb(maxsize) * 1024 * 1024 if self.http_request.content_length > maxsize: raise FileSizeExceeded( 'File size for input exceeded.' ' Maximum request size allowed: {} megabytes'.format( maxsize / 1024 / 1024)) try: doc = lxml.etree.fromstring(self.http_request.get_data()) except Exception as e: if PY2: raise NoApplicableCode(e.message) else: raise NoApplicableCode(e.msg) operation = doc.tag version = get_version_from_ns(doc.nsmap[doc.prefix]) self.set_version(version) request_parser = self._post_request_parser(operation) request_parser(doc)
def href_handler(complexinput, datain): """<wps:Reference /> handler""" # save the reference input in workdir tmp_file = _build_input_file_name( href=datain.get('href'), workdir=complexinput.workdir, extension=_extension(complexinput)) try: reference_file = _openurl(datain) data_size = reference_file.headers.get('Content-Length', 0) except Exception as e: raise NoApplicableCode('File reference error: %s' % e) # if the response did not return a 'Content-Length' header then # calculate the size if data_size == 0: LOGGER.debug('no Content-Length, calculating size') # check if input file size was not exceeded complexinput.calculate_max_input_size() max_byte_size = complexinput.max_size * 1024 * 1024 if int(data_size) > int(max_byte_size): raise FileSizeExceeded( 'File size for input exceeded.' ' Maximum allowed: %i megabytes' % complexinput.max_size, complexinput.identifier) try: with open(tmp_file, 'wb') as f: data_size = 0 for chunk in reference_file.iter_content(chunk_size=1024): data_size += len(chunk) if int(data_size) > int(max_byte_size): raise FileSizeExceeded( 'File size for input exceeded.' ' Maximum allowed: %i megabytes' % complexinput.max_size, complexinput.identifier) f.write(chunk) except Exception as e: raise NoApplicableCode(e) complexinput.file = tmp_file complexinput.url = datain.get('href') complexinput.as_reference = True