def copystream_progress(request, chunk_size=10 ** 5): """ copies request.env.wsgi_input into request.body and stores progress upload status in cache_ram X-Progress-ID:length and X-Progress-ID:uploaded """ env = request.env if not env.get('CONTENT_LENGTH', None): return cStringIO.StringIO() source = env['wsgi.input'] try: size = int(env['CONTENT_LENGTH']) except ValueError: raise HTTP(400, "Invalid Content-Length header") try: # Android requires this dest = tempfile.NamedTemporaryFile() except NotImplementedError: # and GAE this dest = tempfile.TemporaryFile() if not 'X-Progress-ID' in request.get_vars: copystream(source, dest, size, chunk_size) return dest cache_key = 'X-Progress-ID:' + request.get_vars['X-Progress-ID'] cache_ram = CacheInRam(request) # same as cache.ram because meta_storage cache_ram(cache_key + ':length', lambda: size, 0) cache_ram(cache_key + ':uploaded', lambda: 0, 0) while size > 0: if size < chunk_size: data = source.read(size) cache_ram.increment(cache_key + ':uploaded', size) else: data = source.read(chunk_size) cache_ram.increment(cache_key + ':uploaded', chunk_size) length = len(data) if length > size: (data, length) = (data[:size], size) size -= length if length == 0: break dest.write(data) if length < chunk_size: break dest.seek(0) cache_ram(cache_key + ':length', None) cache_ram(cache_key + ':uploaded', None) return dest
def copystream_progress(request, chunk_size=10**5): """ copies request.env.wsgi_input into request.body and stores progress upload status in cache_ram X-Progress-ID:length and X-Progress-ID:uploaded """ env = request.env if not env.get('CONTENT_LENGTH', None): return cStringIO.StringIO() source = env['wsgi.input'] try: size = int(env['CONTENT_LENGTH']) except ValueError: raise HTTP(400, "Invalid Content-Length header") try: # Android requires this dest = tempfile.NamedTemporaryFile() except NotImplementedError: # and GAE this dest = tempfile.TemporaryFile() if not 'X-Progress-ID' in request.get_vars: copystream(source, dest, size, chunk_size) return dest cache_key = 'X-Progress-ID:' + request.get_vars['X-Progress-ID'] cache_ram = CacheInRam(request) # same as cache.ram because meta_storage cache_ram(cache_key + ':length', lambda: size, 0) cache_ram(cache_key + ':uploaded', lambda: 0, 0) while size > 0: if size < chunk_size: data = source.read(size) cache_ram.increment(cache_key + ':uploaded', size) else: data = source.read(chunk_size) cache_ram.increment(cache_key + ':uploaded', chunk_size) length = len(data) if length > size: (data, length) = (data[:size], size) size -= length if length == 0: break dest.write(data) if length < chunk_size: break dest.seek(0) cache_ram(cache_key + ':length', None) cache_ram(cache_key + ':uploaded', None) return dest
def copystream_progress(request, chunk_size=10 ** 5): """ copies request.env.wsgi_input into request.body and stores progress upload status in cache_ram X-Progress-ID:length and X-Progress-ID:uploaded """ env = request.env if not env.content_length: return cStringIO.StringIO() source = env.wsgi_input try: size = int(env.content_length) except ValueError: raise HTTP(400, "Invalid Content-Length header") dest = tempfile.TemporaryFile() if not "X-Progress-ID" in request.vars: copystream(source, dest, size, chunk_size) return dest cache_key = "X-Progress-ID:" + request.vars["X-Progress-ID"] cache_ram = CacheInRam(request) # same as cache.ram because meta_storage cache_ram(cache_key + ":length", lambda: size, 0) cache_ram(cache_key + ":uploaded", lambda: 0, 0) while size > 0: if size < chunk_size: data = source.read(size) cache_ram.increment(cache_key + ":uploaded", size) else: data = source.read(chunk_size) cache_ram.increment(cache_key + ":uploaded", chunk_size) length = len(data) if length > size: (data, length) = (data[:size], size) size -= length if length == 0: break dest.write(data) if length < chunk_size: break dest.seek(0) cache_ram(cache_key + ":length", None) cache_ram(cache_key + ":uploaded", None) return dest
def copystream_progress(request, chunk_size=10**5): """ copies request.env.wsgi_input into request.body and stores progress upload status in cache.ram X-Progress-ID:length and X-Progress-ID:uploaded """ env = request.env if not env.content_length: return cStringIO.StringIO() source = env.wsgi_input try: size = int(env.content_length) except ValueError: raise HTTP(400, "Invalid Content-Length header") dest = tempfile.TemporaryFile() if not 'X-Progress-ID' in request.vars: copystream(source, dest, size, chunk_size) return dest cache_key = 'X-Progress-ID:' + request.vars['X-Progress-ID'] cache = Cache(request) cache.ram(cache_key + ':length', lambda: size, 0) cache.ram(cache_key + ':uploaded', lambda: 0, 0) while size > 0: if size < chunk_size: data = source.read(size) cache.ram.increment(cache_key + ':uploaded', size) else: data = source.read(chunk_size) cache.ram.increment(cache_key + ':uploaded', chunk_size) length = len(data) if length > size: (data, length) = (data[:size], size) size -= length if length == 0: break dest.write(data) if length < chunk_size: break dest.seek(0) cache.ram(cache_key + ':length', None) cache.ram(cache_key + ':uploaded', None) return dest
def copystream_progress(request, chunk_size= 10**5): """ copies request.env.wsgi_input into request.body and stores progress upload status in cache.ram X-Progress-ID:length and X-Progress-ID:uploaded """ env = request.env if not env.content_length: return cStringIO.StringIO() source = env.wsgi_input size = int(env.content_length) dest = tempfile.TemporaryFile() if not 'X-Progress-ID' in request.vars: copystream(source, dest, size, chunk_size) return dest cache_key = 'X-Progress-ID:'+request.vars['X-Progress-ID'] cache = Cache(request) cache.ram(cache_key+':length', lambda: size, 0) cache.ram(cache_key+':uploaded', lambda: 0, 0) while size > 0: if size < chunk_size: data = source.read(size) cache.ram.increment(cache_key+':uploaded', size) else: data = source.read(chunk_size) cache.ram.increment(cache_key+':uploaded', chunk_size) length = len(data) if length > size: (data, length) = (data[:size], size) size -= length if length == 0: break dest.write(data) if length < chunk_size: break dest.seek(0) cache.ram(cache_key+':length', None) cache.ram(cache_key+':uploaded', None) return dest
def wsgibase(environ, responder): """ this is the gluon wsgi application. the furst function called when a page is requested (static or dynamical). it can be called by paste.httpserver or by apache mod_wsgi. """ request = Request() response = Response() session = Session() try: try: session_file = None session_new = False # ################################################## # parse the environment variables - DONE # ################################################## for (key, value) in environ.items(): request.env[key.lower().replace('.', '_')] = value request.env.web2py_path = web2py_path request.env.web2py_version = web2py_version # ################################################## # valudate the path in url # ################################################## if not request.env.path_info and request.env.request_uri: # for fcgi, decode path_info and query_string items = request.env.request_uri.split('?') request.env.path_info = items[0] if len(items) > 1: request.env.query_string = items[1] else: request.env.query_string = '' path = request.env.path_info[1:].replace('\\', '/') path = regex_space.sub('_', path) if not regex_url.match(path): raise HTTP(400, error_message, web2py_error='invalid path') items = path.split('/') # ################################################## # serve if a static file # ################################################## if len(items) > 1 and items[1] == 'static': if len(items) < 3 or not items[2]: raise HTTP(400, error_message) static_file = os.path.join(request.env.web2py_path, 'applications', items[0], 'static', '/'.join(items[2:])) response.stream(static_file, request=request) # ################################################## # parse application, controller and function # ################################################## if len(items) and items[-1] == '': del items[-1] if len(items) == 0: items = ['init'] if len(items) == 1: items.append('default') if len(items) == 2: items.append('index') if len(items) > 3: (items, request.args) = (items[:3], items[3:]) if request.args == None: request.args = [] request.application = items[0] request.controller = items[1] request.function = items[2] request.client = get_client(request.env) request.folder = os.path.join(request.env.web2py_path, 'applications', request.application) + '/' # ################################################## # access the requested application # ################################################## if not os.path.exists(request.folder): if items == ['init', 'default', 'index']: items[0] = 'welcome' redirect(html.URL(*items)) raise HTTP(400, error_message, web2py_error='invalid application') # ################################################## # get the GET and POST data -DONE # ################################################## request.body = tempfile.TemporaryFile() if request.env.content_length: copystream(request.env.wsgi_input, request.body, int(request.env.content_length)) # ## parse GET vars, even if POST dget = cgi.parse_qsl(request.env.query_string, keep_blank_values=1) for (key, value) in dget: if request.vars.has_key(key): if isinstance(request.vars[key], list): request.vars[key].append(value) else: request.vars[key] = [request.vars[key], value] else: request.vars[key] = value request.get_vars[key] = request.vars[key] # ## parse POST vars if any if request.env.request_method in ['POST', 'BOTH']: dpost = cgi.FieldStorage(fp=request.body, environ=environ, keep_blank_values=1) request.body.seek(0) try: keys = dpost.keys() except TypeError: keys = [] for key in keys: dpk = dpost[key] if isinstance(dpk, list): value = [x.value for x in dpk] elif not dpk.filename: value = dpk.value else: value = dpk request.post_vars[key] = request.vars[key] = value # ################################################## # load cookies # ################################################## request.cookies = Cookie.SimpleCookie() response.cookies = Cookie.SimpleCookie() if request.env.http_cookie: request.cookies.load(request.env.http_cookie) # ################################################## # try load session or create new session file # ################################################## session.connect(request, response) # ################################################## # set no-cache headers # ################################################## response.headers['Cache-Control'] = \ 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' response.headers['Expires'] = \ time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()) response.headers['Pragma'] = 'no-cache' # ################################################## # run controller # ################################################## if not items[1] == 'static': serve_controller(request, response, session) except HTTP, http_response: # ################################################## # on sucess, try store session in database # ################################################## session._try_store_in_db(request, response) # ################################################## # on sucess, committ database # ################################################## if response._custom_commit: response._custom_commit() else: SQLDB.close_all_instances(SQLDB.commit) # ################################################## # if session not in db try store session on filesystem # ################################################## session._try_store_on_disk(request, response) # ################################################## # store cookies in headers # ################################################## if session._secure: response.cookies[response.session_id_name]['secure'] = \ True http_response.headers['Set-Cookie'] = [ str(cookie)[11:] for cookie in response.cookies.values() ] # ################################################## # whatever happens return the intended HTTP response # ################################################## session._unlock(response) return http_response.to(responder) except RestrictedError, e: # ################################################## # on application error, rollback database # ################################################## if response._custom_rollback: response._custom_rollback() else: SQLDB.close_all_instances(SQLDB.rollback) try: ticket = e.log(request) except: ticket = 'unknown' logging.error(e.traceback) session._unlock(response) http_error_status = check_error_route(500, items[0]) return HTTP(http_error_status, error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket).to(responder)
def wsgibase(environ, responder): """ this is the gluon wsgi application. the furst function called when a page is requested (static or dynamical). it can be called by paste.httpserver or by apache mod_wsgi. """ request = Request() response = Response() session = Session() try: try: session_file = None session_new = False # ################################################## # parse the environment variables - DONE # ################################################## for (key, value) in environ.items(): request.env[key.lower().replace('.', '_')] = value request.env.web2py_path = web2py_path request.env.web2py_version = web2py_version # ################################################## # valudate the path in url # ################################################## if not request.env.path_info and request.env.request_uri: # for fcgi, decode path_info and query_string items = request.env.request_uri.split('?') request.env.path_info = items[0] if len(items) > 1: request.env.query_string = items[1] else: request.env.query_string = '' path = request.env.path_info[1:].replace('\\', '/') path = regex_space.sub('_', path) if not regex_url.match(path): raise HTTP(400, error_message, web2py_error='invalid path') items = path.split('/') # ################################################## # serve if a static file # ################################################## if len(items) > 1 and items[1] == 'static': if len(items) < 3 or not items[2]: raise HTTP(400, error_message) static_file = os.path.join(request.env.web2py_path, 'applications', items[0], 'static', '/'.join(items[2:])) response.stream(static_file, request=request) # ################################################## # parse application, controller and function # ################################################## if len(items) and items[-1] == '': del items[-1] if len(items) == 0: items = ['init'] if len(items) == 1: items.append('default') if len(items) == 2: items.append('index') if len(items) > 3: (items, request.args) = (items[:3], items[3:]) if request.args == None: request.args = [] request.application = items[0] request.controller = items[1] request.function = items[2] request.client = get_client(request.env) request.folder = os.path.join(request.env.web2py_path, 'applications', request.application) + '/' # ################################################## # access the requested application # ################################################## if not os.path.exists(request.folder): if items == ['init', 'default', 'index']: items[0] = 'welcome' redirect(html.URL(*items)) raise HTTP(400, error_message, web2py_error='invalid application') # ################################################## # get the GET and POST data -DONE # ################################################## request.body = tempfile.TemporaryFile() if request.env.content_length: copystream(request.env.wsgi_input, request.body, int(request.env.content_length)) # ## parse GET vars, even if POST dget = cgi.parse_qsl(request.env.query_string, keep_blank_values=1) for (key, value) in dget: if request.vars.has_key(key): if isinstance(request.vars[key], list): request.vars[key].append(value) else: request.vars[key] = [request.vars[key], value] else: request.vars[key] = value request.get_vars[key] = request.vars[key] # ## parse POST vars if any if request.env.request_method in ['POST', 'BOTH']: dpost = cgi.FieldStorage(fp=request.body, environ=environ, keep_blank_values=1) request.body.seek(0) try: keys = dpost.keys() except TypeError: keys = [] for key in keys: dpk = dpost[key] if isinstance(dpk, list): value = [x.value for x in dpk] elif not dpk.filename: value = dpk.value else: value = dpk request.post_vars[key] = request.vars[key] = value # ################################################## # load cookies # ################################################## request.cookies = Cookie.SimpleCookie() response.cookies = Cookie.SimpleCookie() if request.env.http_cookie: request.cookies.load(request.env.http_cookie) # ################################################## # try load session or create new session file # ################################################## session.connect(request, response) # ################################################## # set no-cache headers # ################################################## response.headers['Cache-Control'] = \ 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' response.headers['Expires'] = \ time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()) response.headers['Pragma'] = 'no-cache' # ################################################## # run controller # ################################################## if not items[1] == 'static': serve_controller(request, response, session) except HTTP, http_response: # ################################################## # on sucess, try store session in database # ################################################## session._try_store_in_db(request, response) # ################################################## # on sucess, committ database # ################################################## if response._custom_commit: response._custom_commit() else: SQLDB.close_all_instances(SQLDB.commit) # ################################################## # if session not in db try store session on filesystem # ################################################## session._try_store_on_disk(request, response) # ################################################## # store cookies in headers # ################################################## if session._secure: response.cookies[response.session_id_name]['secure'] = \ True http_response.headers['Set-Cookie'] = [str(cookie)[11:] for cookie in response.cookies.values()] # ################################################## # whatever happens return the intended HTTP response # ################################################## session._unlock(response) return http_response.to(responder) except RestrictedError, e: # ################################################## # on application error, rollback database # ################################################## if response._custom_rollback: response._custom_rollback() else: SQLDB.close_all_instances(SQLDB.rollback) try: ticket = e.log(request) except: ticket = 'unknown' logging.error(e.traceback) session._unlock(response) http_error_status = check_error_route(500, items[0]) return HTTP(http_error_status, error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket).to(responder)