def __call__(self, *a, **kw): __hidetraceback__ = True import webapi as web if 'headers' in web.ctx and self.content_type: web.header('Content-Type', self.content_type, unique=True) return BaseTemplate.__call__(self, *a, **kw)
def __call__(self, *a, **kw): import webapi as web if "headers" in web.ctx and self.content_type: web.header("Content-Type", self.content_type, unique=True) return BaseTemplate.__call__(self, *a, **kw)
def modified(date=None, etag=None): """ Checks to see if the page has been modified since the version in the requester's cache. When you publish pages, you can include `Last-Modified` and `ETag` with the date the page was last modified and an opaque token for the particular version, respectively. When readers reload the page, the browser sends along the modification date and etag value for the version it has in its cache. If the page hasn't changed, the server can just return `304 Not Modified` and not have to send the whole page again. This function takes the last-modified date `date` and the ETag `etag` and checks the headers to see if they match. If they do, it returns `True` and sets the response status to `304 Not Modified`. It also sets `Last-Modified and `ETag` output headers. """ n = set(x.strip('" ') for x in web.ctx.env.get('HTTP_IF_NONE_MATCH', '').split(',')) m = net.parsehttpdate(web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0]) validate = False if etag: if '*' in n or etag in n: validate = True if date and m: # we subtract a second because # HTTP dates don't have sub-second precision if date-datetime.timedelta(seconds=1) <= m: validate = True if validate: web.ctx.status = '304 Not Modified' lastmodified(date) web.header('ETag', '"' + etag + '"') return not validate
def nomethod(cls): """Returns a `405 Method Not Allowed` error for `cls`.""" web.ctx.status = '405 Method Not Allowed' web.header('Content-Type', 'text/html') web.header('Allow', \ ', '.join([method for method in \ ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'] \ if hasattr(cls, method)]))
def expires(delta): """ Outputs an `Expires` header for `delta` from now. `delta` is a `timedelta` object or a number of seconds. """ if isinstance(delta, (int, long)): delta = datetime.timedelta(seconds=delta) date_obj = datetime.datetime.utcnow() + delta web.header('Expires', net.httpdate(date_obj))
def redirect(url, status='301 Moved Permanently'): """ Returns a `status` redirect to the new URL. `url` is joined with the base URL so that things like `redirect("about") will work properly. """ newloc = urlparse.urljoin(web.ctx.home + web.ctx.path, url) web.ctx.status = status web.ctx.output = '' web.header('Content-Type', 'text/html') web.header('Location', newloc)
def f(*args, **kw): x = fn(*args, **kw) web.header('Content-Type', 'text/plain; charset=UTF-8') try: if x and (type(x) is not dict) \ and ((hasattr(x, '__iter__') or hasattr(x, 'next'))): x = list(x) yield get_encoder(web.ctx._out_format)(x) except: yield 'not json :(\n' yield x
def __call__(self, *a, **kw): d = self.globals.copy() d.update(self._parseargs(a, kw)) f = Fill(self.tree, d=d) if self.filter: f.filter = self.filter import webapi as web if 'headers' in web.ctx and self.filename: content_type = self.find_content_type() if content_type: web.header('Content-Type', content_type, unique=True) return f.go()
def render(template, terms=None, asTemplate=False, base=None, isString=False): """ Renders a template, caching where it can. `template` is the name of a file containing the a template in the `templates/` folder, unless `isString`, in which case it's the template itself. `terms` is a dictionary used to fill the template. If it's None, then the caller's local variables are used instead, plus context, if it's not already set, is set to `context`. If asTemplate is False, it `output`s the template directly. Otherwise, it returns the template object. If the template is a potential base template (that is, something other templates) can extend, then base should be a string with the name of the template. The template will be cached and made available for future calls to `render`. Requires [Cheetah](http://cheetahtemplate.org/). """ # terms=['var1', 'var2'] means grab those variables if isinstance(terms, list): new = {} old = upvars() for k in terms: new[k] = old[k] terms = new # default: grab all locals elif terms is None: terms = {'context': ctx, 'ctx':ctx} terms.update(sys._getframe(1).f_locals) # terms=d means use d as the searchList if not isinstance(terms, tuple): terms = (terms,) if 'headers' in ctx and not isString and template.endswith('.html'): header('Content-Type','text/html; charset=utf-8', unique=True) if loadhooks.has_key('reloader'): compiled_tmpl = __compiletemplate(template, base=base, isString=isString) else: compiled_tmpl = _compiletemplate(template, base=base, isString=isString) compiled_tmpl = compiled_tmpl(searchList=terms, filter=WebSafe) if asTemplate: return compiled_tmpl else: return output(str(compiled_tmpl))
def wsgifunc(self, *middleware): """Returns a WSGI-compatible function for this application.""" def peep(iterator): """Peeps into an iterator by doing an iteration and returns an equivalent iterator. """ # wsgi requires the headers first # so we need to do an iteration # and save the result for later try: firstchunk = iterator.next() except StopIteration: firstchunk = '' return itertools.chain([firstchunk], iterator) def is_generator(x): return x and hasattr(x, 'next') def wsgi(env, start_resp): stime = time.time() # clear threadlocal to avoid inteference of previous requests self._cleanup() self.load(env) try: # allow uppercase methods only if web.ctx.method.upper() != web.ctx.method: raise web.nomethod() result = self.handle_with_processors() if is_generator(result): result = peep(result) elif not hasattr(result, '__iter__'): result = [result] except web.HTTPError, e: result = [e.data] result = web.utf8(iter(result)) status, headers = web.ctx.status, web.ctx.headers web.header('X-TIME', time.time() - stime) del stime start_resp(status, headers) def cleanup(): self._cleanup() yield '' # force this function to be a generator return itertools.chain(result, cleanup())
def redirect(url, status='301 Moved Permanently'): """ Returns a `status` redirect to the new URL. `url` is joined with the base URL so that things like `redirect("about") will work properly. """ if url.startswith("/"): newloc = web.ctx.homepath + url else: newloc = url web.ctx.status = status web.ctx.output = '' web.header('Content-Type', 'text/html') web.header('Location', newloc)
def modified(date=None, etag=None): """ Checks to see if the page has been modified since the version in the requester's cache. When you publish pages, you can include `Last-Modified` and `ETag` with the date the page was last modified and an opaque token for the particular version, respectively. When readers reload the page, the browser sends along the modification date and etag value for the version it has in its cache. If the page hasn't changed, the server can just return `304 Not Modified` and not have to send the whole page again. This function takes the last-modified date `date` and the ETag `etag` and checks the headers to see if they match. If they do, it returns `True`, or otherwise it raises NotModified error. It also sets `Last-Modified` and `ETag` output headers. """ try: from __builtin__ import set except ImportError: from sets import Set as set n = set([x.strip('" ') for x in web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(',')]) m = net.parsehttpdate( web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0]) validate = False if etag: if '*' in n or etag in n: validate = True if date and m: if date - datetime.timedelta(seconds=1) <= m: validate = True if date: lastmodified(date) if etag: web.header('ETag', '"' + etag + '"') if validate: raise web.NotModified() else: return True
def write(cgi_response): """ Converts a standard CGI-style string response into `header` and `output` calls. """ cgi_response = str(cgi_response) cgi_response.replace('\r\n', '\n') head, body = cgi_response.split('\n\n', 1) lines = head.split('\n') for line in lines: if line.isspace(): continue hdr, value = line.split(":", 1) value = value.strip() if hdr.lower() == "status": web.ctx.status = value else: web.header(hdr, value) web.output(body)
def _do(self, name, filter=None): if self.cache is False or name not in self.cache: p = glob.glob(self.loc + name + '.*') if not p and os.path.isdir(self.loc + name): return render(self.loc + name + '/', cache=self.cache) elif not p: raise AttributeError, 'no template named ' + name p = p[0] c = Template(open(p).read()) if self.cache is not False: self.cache[name] = (p, c) if self.cache is not False: p, c = self.cache[name] if p.endswith('.html'): import webapi as web if 'headers' in web.ctx: web.header('Content-Type', 'text/html; charset=utf-8', unique=True) if not filter: c.filter = websafe elif p.endswith('.xml'): if not filter: c.filter = websafe return c
try: # allow uppercase methods only if web.ctx.method.upper() != web.ctx.method: raise web.nomethod() result = self.handle_with_processors() except NotFound: web.ctx.status = "404 Not Found" result = self.notfound() except web.HTTPError, e: result = e.data except: print >> web.debug, traceback.format_exc() web.ctx.status = '500 Internal Server Error' web.header('Content-Type', 'text/html') result = self.internalerror() if is_generator(result): result = peep(result) else: result = [utils.utf8(result)] status, headers = web.ctx.status, web.ctx.headers start_resp(status, headers) #@@@ # Since the CherryPy Webserver uses thread pool, the thread-local state is never cleared. # This interferes with the other requests. # clearing the thread-local storage to avoid that. # see utils.ThreadedDict for details
def __call__(self, *a, **kw): import webapi as web if 'headers' in web.ctx and self.content_type: web.header('Content-Type', self.content_type) return BaseTemplate.__call__(self, *a, **kw)
def lastmodified(date_obj): """Outputs a `Last-Modified` header for `datetime`.""" web.header('Last-Modified', net.httpdate(date_obj))
try: # allow uppercase methods only if web.ctx.method.upper() != web.ctx.method: raise web.nomethod() result = self.handle_with_processors() except NotFound: web.ctx.status = "404 Not Found" result = self.notfound() except web.HTTPError, e: result = e.data except: print >>web.debug, traceback.format_exc() web.ctx.status = "500 Internal Server Error" web.header("Content-Type", "text/html") result = self.internalerror() if is_generator(result): result = peep(result) else: result = [utils.utf8(result)] status, headers = web.ctx.status, web.ctx.headers start_resp(status, headers) # @@@ # Since the CherryPy Webserver uses thread pool, the thread-local state is never cleared. # This interferes with the other requests. # clearing the thread-local storage to avoid that. # see utils.ThreadedDict for details