Beispiel #1
0
    def application(self, environ, start_response):
        req_webob = Request(environ)
        res_webob = Response()

        #addr tuple as glastopf expects it
        remote_addr = (req_webob.remote_addr, int(environ["REMOTE_PORT"]))
        if "SERVER_NAME" in environ and "SERVER_PORT" in environ:
            # we could use socket.gethostbyname to get the ip...
            sensor_addr = (environ["SERVER_NAME"], environ["SERVER_PORT"])
        else:
            sensor_addr = ("", "")

        header, body = self.honeypot.handle_request(req_webob.as_text(),
                                                         remote_addr, sensor_addr)

        header_list = header.splitlines()
        try:
            # format: http_version status_code description
            res_webob.status_code = int(header_list[0].split()[1])
        except ValueError:
            # ['User-agent: *', 'Disallow:']
            # default 200 OK
            pass
        for h in header_list:
            if ":" in h:
                h, v = h.split(":", 1)
                res_webob.headers[str(h.strip())] = str(v.strip())
        # this will adjust content-length header
        res_webob.charset = "utf8"
        res_webob.text = body.decode("utf-8", "ignore")

        #WSGI applications are not allowed to create or modify hop-by-hop headers
        self.remove_hop_by_hop_headers(res_webob.headers)
        return res_webob(environ, start_response)
Beispiel #2
0
def jresponse(status,msg,req,status_int,headers=None,statusstr='',param=None):
    
    data = {'status':str(status),'msg':str(msg)}
    
    if param:
        data.update(param)
        
    container_list = json.dumps(data)
    if headers:
        ret = Response(body=container_list, request=req,headers=headers)
    else:
        ret = Response(body=container_list, request=req)
        
    ret.content_type = 'application/json'
    ret.charset = 'utf-8'
    ret.status_int = status_int
    if statusstr:
        ret.status = statusstr
        
    if status != '0' and req.method == 'PUT': 
        pass
        # syslog.syslog(syslog.LOG_ERR,'jresponse:   status: ' + str(status_int)+'  path: '+str(req.path)+'  msg: '+str(msg) + '  tx_id:  '+ req.environ.get('swift.trans_id',''))
        # print 'jresponse:   status: ' + str(status_int)+'  path: '+str(req.path)+'  msg: '+str(msg) + '  tx_id:  '+ req.environ.get('swift.trans_id','') + '  method:  '+str(req.method) + '  headers:  '+str(req.headers) + '  params:  '+str(req.GET)
        # traceback.print_stack() 
        # syslog.syslog(syslog.LOG_ERR,'jresponse:   '+str(traceback.extract_stack() ))     
        # syslog.syslog(syslog.LOG_ERR,'jresponse:   '+str(traceback.print_stack() ))
    return ret
Beispiel #3
0
    def application(self, environ, start_response):
        req_webob = Request(environ)
        res_webob = Response()

        #addr tuple as glastopf expects it
        remote_addr = (req_webob.remote_addr, int(environ["REMOTE_PORT"]))
        if "SERVER_NAME" in environ and "SERVER_PORT" in environ:
            # we could use socket.gethostbyname to get the ip...
            sensor_addr = (environ["SERVER_NAME"], environ["SERVER_PORT"])
        else:
            sensor_addr = ("", "")

        header, body = self.honeypot.handle_request(req_webob.as_text(),
                                                         remote_addr, sensor_addr)
        for h in header.splitlines():
            if ":" in h:
                h, v = h.split(":", 1)
                res_webob.headers[str(h.strip())] = str(v.strip())
        # this will adjust content-length header
        res_webob.charset = "utf8"
        res_webob.text = body.decode("utf-8", "ignore")

        #WSGI applications are not allowed to create or modify hop-by-hop headers
        self.remove_hop_by_hop_headers(res_webob.headers)
        return res_webob(environ, start_response)
Beispiel #4
0
    def json(self, req):
        resp = Response()
        resp.content_type = 'text/javascript'
        resp.charset = 'utf-8'
        alias = req.params.get('alias')
        url = req.params.get('url')

        path_info = req.path_info.lstrip('/')
        if path_info.startswith('json/stats'):
            if alias:
                c = get_stats(alias)
            else:
                c = Params(error='You must provide an alias !')
        else:
            if url:
                c = self.add(req, url, alias)
            else:
                c = Params(error='You must provide an url !')

        callback =req.params.get('callback')
        if callback:
            callback = str(callback)
            arg = req.params.get('arg')
            if arg:
                resp.body = '%s(%s, %s);' % (callback, json.dumps(arg), json.dumps(c))
            else:
                resp.body = '%s(%s);' % (callback, json.dumps(c))
        else:
            resp.body = json.dumps(c)
        return resp
Beispiel #5
0
 def __call__(self, environ, start_response):
     req = Request(environ)
     path_info = req.path_info.lstrip('/')
     if path_info.startswith('json'):
         resp = self.json(req)
     elif path_info.startswith('stats'):
         resp = Response()
         alias = [p for p in path_info.split('/')[1:] if p]
         if alias:
             alias = '/'.join(alias)
             resp.body = self.stats.render(c=get_stats(alias))
     elif path_info and path_info != 'new':
         resp = self.redirect(req)
     elif not path_info and self.redirect_url:
         resp = exc.HTTPFound(location=self.redirect_url)
     else:
         resp = Response()
         resp.content_type = 'text/html'
         resp.charset = 'utf-8'
         if req.GET.get('url'):
             # save
             alias = req.GET.get('alias')
             alias = alias and alias or None
             url = req.GET.get('url')
             if url:
                 c = self.add(req, url, alias)
             else:
                 c = Params(error='You must provide an url !')
         else:
             c = Params(url=req.GET.get('post',''))
             c.title = req.params.get('title', '')
         c.plugin = req.params.get('p', False)
         resp.body = self.index.render(c=c)
     return resp(environ, start_response)
Beispiel #6
0
def _render_atom_feed(request, mb, messages):    
    entries = []
    for message in messages:
        renderer = _get_atom_renderer(message, request)
        if renderer is not None:
            entries.append(renderer)

    info = MailboxInfo.get(mb)
    
    # absolute url for requested feed
    feed_url = request.url
    template_info = TemplateContext(request,
          {'id': feed_url,
           'self_link': feed_url,
           'updated': datetime.utcnow(), # XXX
           'title': info.title or request.context.get_mailbox_slug(mb.name),
           'entries': entries,
          })

    res = HttpResponse(content_type='application/atom+xml')
    res.charset = 'utf-8'
    res.unicode_body = request.context.render('radar/atom/atom.xml', template_info)
    res.headers['etag'] = get_mailbox_etag(mb)
    
    return res
Beispiel #7
0
    def get_version_info(self, req, file="version"):
        resp = Response()
        resp.charset = 'UTF-8'
        if utils.is_xml_response(req):
            resp_file = os.path.join(POSSIBLE_TOPDIR,
                "keystone/content/%s.xml.tpl" % file)
            resp.content_type = "application/xml"
        elif utils.is_atom_response(req):
            resp_file = os.path.join(POSSIBLE_TOPDIR,
                "keystone/content/%s.atom.tpl" % file)
            resp.content_type = "application/atom+xml"
        else:
            resp_file = os.path.join(POSSIBLE_TOPDIR,
                "keystone/content/%s.json.tpl" % file)
            resp.content_type = "application/json"

        hostname = req.environ.get("SERVER_NAME")
        port = req.environ.get("SERVER_PORT")
        if 'HTTPS' in req.environ:
            protocol = 'https'
        else:
            protocol = 'http'

        resp.unicode_body = template.template(resp_file,
            PROTOCOL=protocol,
            HOST=hostname,
            PORT=port,
            API_VERSION=version.API_VERSION,
            API_VERSION_STATUS=version.API_VERSION_STATUS,
            API_VERSION_DATE=version.API_VERSION_DATE)

        return resp
	def __call__(self, env, start_response):
		res = Response (content_type = 'text/plain', status = 200)
		req = Request (env)
		url = req.path_info
		
		additional_params = dict(req.params)
		additional_params['request'] = req
		additional_params['response'] = res
		additional_params['method'] = req.method
		additional_params['headers'] = req.headers
		
		found = False
		for handler in UrlCallableFunction.request_handlers:
			result = handler.call_if_matches (url, additional_params)
			if result: 
				found = True
				if isinstance (result, str):
					res.text = result
				elif isinstance (result, dict):
					if 'body' in result: res.text = result['body']
					if 'charset' in result: res.charset = result['charset']
					if 'content_type' in result: res.content_type = result['content_type']
					if 'additional_headers' in result: res.headers.update(result['additional_headers'])
					if 'status' in result: res.status = result['status']
				break
		
		if not found:
			res.status = 400
			
		return res (env, start_response)
Beispiel #9
0
def make_response(obj, content_type='application/json'):
    res = Response(content_type=content_type)
    if(content_type=="application/json"):
        res.charset = 'utf8'
        res.body = dumps(obj)
    else:
        res.body = obj
    return res
def test_charset_set_charset_is_None():
    res = Response()
    res.charset = 'utf-8'
    res._app_iter = ['123']
    del res.app_iter
    eq_(res._app_iter, None)
    eq_(res._body, None)
    eq_(res.content_length, None)
Beispiel #11
0
    def __call__(self, environ, start_response):
        res = Response()
        res.text = text_type(self.body)
        res.status = '200 OK'
        res.content_type = 'application/json'
        res.charset = 'utf-8'
        res.headers.add('Content-description', 'dods_version')
        res.headers.add('XDODS-Server', 'pydap/%s' % __version__)

        return res(environ, start_response)
Beispiel #12
0
def render_template_to_response(path, **kw):
    """ Render a zope.pagetemplate (ZPT) template at the package-relative path
    (may also be absolute) using the kwargs in ``*kw`` as top-level
    names and return a Response object. """
    
    result = render_template(path, **kw)
    #return Response(result)
    response = Response(result)
    response.charset = 'utf-8' # Make IE happy
    return response
Beispiel #13
0
    def __call__(self, environ, start_response):
        res = Response()
        res.text = text_type(self.body)
        res.status = "200 OK"
        res.content_type = "application/json"
        res.charset = "utf-8"
        res.headers.add("Content-description", "dods_version")
        res.headers.add("XDODS-Server", "pydap/%s" % __version__)

        return res(environ, start_response)
Beispiel #14
0
 def challenge(self, environ, status, app_headers, forget_headers):
     '''
     The challenger.
     '''
     res = Response()
     res.headerlist = [('Content-type', 'application/json')]
     res.charset = 'utf8'
     res.unicode_body = u"{error:'wrong credentials'}"
     res.status = 403
     return res
Beispiel #15
0
    def __call__(self, environ, start_response):
        res = Response()
        res.body = self.body
        res.status='500 Internal Error'
        res.content_type = 'text/plain'
        res.charset = 'utf-8'
        res.headers.add('Content-description', 'dods_error')
        res.headers.add('XDODS-Server', 'pydap/%s' % '.'.join(map(str, __version__)))

        return res(environ, start_response)
Beispiel #16
0
def test_unicode_body():
    res = Response()
    res.charset = 'utf-8'
    bbody = 'La Pe\xc3\xb1a' # binary string
    ubody = unicode(bbody, 'utf-8') # unicode string
    res.body = bbody
    eq_(res.unicode_body, ubody)
    res.ubody = ubody
    eq_(res.body, bbody)
    del res.ubody
    eq_(res.body, '')
Beispiel #17
0
 def __call__(self, environ, start_response):
     req = Request(environ)
     resp = Response()
     filename = req.path_info.strip('/')
     lfilename = filename.lower()
     if not req.path_info.strip('/') or os.path.isdir(filename):
         if filename:
             filename = path(filename, 'index.html')
         else:
             filename = 'index.html'
         body = open(filename, 'rb').read()
         resp.body = body
     elif os.path.isfile(filename):
         if req.method.lower() == 'delete':
             sh.rm(filename + '*', shell=True)
             resp = exc.HTTPNoContent()
             return resp(environ, start_response)
         if req.path_info.endswith('.metadata'):
             cfg = ConfigObject(filename=filename)
             if req.method.lower() == 'get':
                 resp.content_type = 'application/json'
             elif req.method.lower() == 'put':
                 data = json.loads(req.body)
                 cfg.metadata.update(data)
                 cfg.write()
             metadata = dict(cfg.metadata.items())
             metadata.update(tags=cfg.metadata.tags.as_list())
             resp.body = json.dumps(metadata)
         elif req.path_info.endswith('.js'):
             resp.content_type = 'text/javascript'
         elif req.path_info.endswith('.json'):
             resp.content_type = 'application/json'
         elif req.path_info.endswith('.css'):
             resp.content_type = 'text/css'
         elif lfilename.endswith('.jpg'):
             resp.charset = None
             resp.content_type = 'image/jpeg'
         print(filename)
         if not resp.content_length:
             resp.app_iter = fd(filename)
     elif req.path_info.startswith('/delete/'):
         filename = req.path_info[8:-1]
         self.delete(filename)
         resp.status_int = 302
         resp.location = '/' + path.dirname(filename)
     else:
         resp.body = str(req.path_info)
     resp.last_modified = datetime.now()
     return resp(environ, start_response)
Beispiel #18
0
    def backend(self, request, environ):
        """
        WSGI Response producer for HTTP POST Git Smart HTTP requests.
        Reads commands and data from HTTP POST's body.
        returns an iterator obj with contents of git command's
        response to stdout
        """
        _git_path = kallithea.CONFIG.get('git_path', 'git')
        git_command = self._get_fixedpath(request.path_info)
        if git_command not in self.commands:
            log.debug('command %s not allowed', git_command)
            return exc.HTTPMethodNotAllowed()

        if 'CONTENT_LENGTH' in environ:
            inputstream = FileWrapper(environ['wsgi.input'],
                                      request.content_length)
        else:
            inputstream = environ['wsgi.input']

        gitenv = dict(os.environ)
        # forget all configs
        gitenv['GIT_CONFIG_NOGLOBAL'] = '1'
        cmd = [_git_path, git_command[4:], '--stateless-rpc', self.content_path]
        log.debug('handling cmd %s', cmd)
        try:
            out = subprocessio.SubprocessIOChunker(
                cmd,
                inputstream=inputstream,
                env=gitenv,
                cwd=self.content_path,
            )
        except EnvironmentError as e:
            log.error(traceback.format_exc())
            raise exc.HTTPExpectationFailed()

        if git_command in [u'git-receive-pack']:
            # updating refs manually after each push.
            # Needed for pre-1.7.0.4 git clients using regular HTTP mode.
            from kallithea.lib.vcs import get_repo
            from dulwich.server import update_server_info
            repo = get_repo(self.content_path)
            if repo:
                update_server_info(repo._repo)

        resp = Response()
        resp.content_type = 'application/x-%s-result' % git_command.encode('utf8')
        resp.charset = None
        resp.app_iter = out
        return resp
Beispiel #19
0
def markup_parser(request):
    resp = Response()
    # markup preview helper
    resp.charset = 'utf-8'
    markup = request.GET.get('markup', 'textile')
    value = request.POST.get('data', '')
    if markup == 'textile':
        value = textile(value)
    elif markup == 'markdown':
        value = markdown.markdown(value)
    elif markup == 'bbcode':
        value = render_bbcode(value)
    if isinstance(value, unicode):
        value = value.encode('utf-8')
    resp.body = value
    return resp
Beispiel #20
0
def feed_links_opml(request):
    try:
        opmldata = request.POST['opmlfile']
        result = {}
        result['links'] = [{'url': url, 'title': title} for (url, title) in 
                           _feeds_in_opml(opmldata.file.read()).items()];
        result['error'] = False
    except: 
        log.error("error parsing opml: %s" % traceback.format_exc())
        result = {'links': [], 'error': True}

    template_info = TemplateContext(request,
        {'result': json.dumps(result)})
    res = HttpResponse(content_type='text/html')
    res.charset = 'utf-8'
    res.unicode_body = request.context.render('radar/ajax_upload.html', template_info)
    return res
Beispiel #21
0
def application(env, start_response):
    res = None
    path = env['PATH_INFO'].lstrip('/')
    method = env['REQUEST_METHOD']
    args = MultiDict(parse_qsl(env.get('QUERY_STRING', '')))

    parts = path.split('/')
    volume = args.get('volume')
    if not volume:
        volumes = cluster.get_volumes(args['key'])
        for v in volumes:
            if v.node is current_node and v.id in current_node.volumes:
                volume = v.id
                break

    if volume:
        v = current_node.volumes[volume]
        collection = parts[0]
        if method == 'PUT':
            return put_file(env, start_response,
                            v, collection, args['key'])
        elif method == 'GET':
            fname, meta = v.get(collection, args['key'])
            res = Response()
            res.charset = None
            res.headers['X-Sendfile'] = fname
            res.headers['X-Crc'] = str(meta['crc'])
            res.content_length = os.path.getsize(fname)
            if 'ct' in meta:
                res.content_type = meta['ct']
    else:
        collection = parts[0]
        key = args['key']
        v = volumes[0]
        res = HTTPTemporaryRedirect(
            location='http://{}/{}?key={}&volume={}'.format(v.node.id, collection, key, v.id))
        for v in volumes:
            res.headers.add(
                'X-Location',
                'http://{}/{}?key={}&volume={}'.format(v.node.id, collection, key, v.id))

    if not res:
        res = HTTPNotFound()

    return res(env, start_response)
Beispiel #22
0
    def get_version_info(self, req):

        resp = Response()
        resp.charset = "UTF-8"
        if utils.is_xml_response(req):
            resp_file = os.path.join(POSSIBLE_TOPDIR, "keystone/content/version.xml.tpl")
            resp.content_type = "application/xml"
        else:
            resp_file = os.path.join(POSSIBLE_TOPDIR, "keystone/content/version.json.tpl")
            resp.content_type = "application/json"

        hostname = req.environ.get("SERVER_NAME")
        port = req.environ.get("SERVER_PORT")

        resp.unicode_body = template.template(
            resp_file, HOST=hostname, PORT=port, VERSION_STATUS=VERSION_STATUS, VERSION_DATE=VERSION_DATE
        )
        return resp
Beispiel #23
0
    def application(self, environ, start_response):
        req_webob = Request(environ)
        res_webob = Response()

        #addr tuple as glastopf expects it
        remote_addr = (req_webob.remote_addr, int(environ['REMOTE_PORT']))
        header, body = self.honeypot.handle_request(req_webob.as_text(),
                                                         remote_addr, None)
        for h in header.splitlines():
            if ':' in h:
                h, v = h.split(':', 1)
                res_webob.headers[str(h.strip())] = str(v.strip())
        #this will adjust content-length header
        res_webob.charset = 'utf8'
        res_webob.text = unicode(body)

        #WSGI applications are not allowed to create or modify hop-by-hop headers
        self.remove_hop_by_hop_headers(res_webob.headers)
        return res_webob(environ, start_response)
    def inforefs(self, request, unused_environ):
        """
        WSGI Response producer for HTTP GET Git Smart
        HTTP /info/refs request.
        """

        git_command = request.GET.get('service')
        if git_command not in self.commands:
            log.debug('command %s not allowed', git_command)
            return exc.HTTPForbidden()

        # please, resist the urge to add '\n' to git capture and increment
        # line count by 1.
        # by git docs: Documentation/technical/http-protocol.txt#L214 \n is
        # a part of protocol.
        # The code in Git client not only does NOT need '\n', but actually
        # blows up if you sprinkle "flush" (0000) as "0001\n".
        # It reads binary, per number of bytes specified.
        # if you do add '\n' as part of data, count it.
        server_advert = '# service=%s\n' % git_command
        packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower()
        try:
            gitenv = dict(os.environ)
            # forget all configs
            gitenv['RC_SCM_DATA'] = json.dumps(self.extras)
            command = [self.git_path, git_command[4:], '--stateless-rpc',
                       '--advertise-refs', self.content_path]
            out = subprocessio.SubprocessIOChunker(
                command,
                env=gitenv,
                starting_values=[packet_len + server_advert + '0000'],
                shell=False
            )
        except EnvironmentError:
            log.exception('Error processing command')
            raise exc.HTTPExpectationFailed()

        resp = Response()
        resp.content_type = 'application/x-%s-advertisement' % str(git_command)
        resp.charset = None
        resp.app_iter = out

        return resp
def csv_response(rows, encoding='ISO-8859-1', filename='response.csv'):
    """Create a downloadable webob.Response instance with the given
    row data as a CSV file.
    """
    stream = StringIO()
    writer = csv.writer(stream, dialect='excel')
    for row in rows:
        # Encode all columns in the row since the csv.writer cannot
        # handle Unicode objects.
        writer.writerow([unicode(s).encode(encoding) for s in row])

    response = Response()
    response.body = stream.getvalue()
    stream.close()

    # Set Response headers.
    response.charset = encoding
    response.content_type = 'text/csv'
    response.content_disposition = 'attachment; filename={0}'.format(filename)

    return response
Beispiel #26
0
 def __call__(self, environ, start_response):
     req = Request(environ)
     if '/markup_parser.html' in req.path_info:
         resp = Response()
         # markup preview helper
         resp.charset = 'utf-8'
         markup = req.GET.get('markup', 'textile')
         value = req.POST.get('data')
         if markup == 'textile':
             value = textile(value)
         elif markup == 'markdown':
             value = markdown.markdown(value)
         elif markup == 'bbcode':
             value = render_bbcode(value)
         if isinstance(value, unicode):
             value = value.encode('utf-8')
         resp.body = value
     else:
         resp = req.get_response(self.static_app)
     resp.headers['X-Salade'] = 'none'
     return resp(environ, start_response)
Beispiel #27
0
    def get_multiple_choice(self, req, file="multiple_choice", path=None):
        """ Returns a multiple-choices response based on API spec

        Response will include in it only one choice, which is for the
        current API version. The response is a 300 Multiple Choice
        response with either an XML or JSON body.

        """
        if path is None:
            path = ''
        logger.debug("300 Multiple Choices response: %s" % path)
        resp = Response(status="300 Multiple Choices")
        resp.charset = 'UTF-8'
        if utils.is_xml_response(req):
            resp_file = os.path.join(POSSIBLE_TOPDIR,
                "keystone/content/%s.xml.tpl" % file)
            resp.content_type = "application/xml"
        else:
            resp_file = os.path.join(POSSIBLE_TOPDIR,
                "keystone/content/%s.json.tpl" % file)
            resp.content_type = "application/json"

        hostname = req.environ.get("SERVER_NAME")
        port = req.environ.get("SERVER_PORT")
        if 'HTTPS' in req.environ:
            protocol = 'https'
        else:
            protocol = 'http'

        resp.unicode_body = template.template(resp_file,
            PROTOCOL=protocol,
            HOST=hostname,
            PORT=port,
            API_VERSION=version.API_VERSION,
            API_VERSION_STATUS=version.API_VERSION_STATUS,
            API_VERSION_DATE=version.API_VERSION_DATE,
            RESOURCE_PATH=path
            )

        return resp
Beispiel #28
0
    def inforefs(self, req, environ):
        """
        WSGI Response producer for HTTP GET Git Smart
        HTTP /info/refs request.
        """

        git_command = req.GET.get('service')
        if git_command not in self.commands:
            log.debug('command %s not allowed', git_command)
            return exc.HTTPMethodNotAllowed()

        # From Documentation/technical/http-protocol.txt shipped with Git:
        #
        # Clients MUST verify the first pkt-line is `# service=$servicename`.
        # Servers MUST set $servicename to be the request parameter value.
        # Servers SHOULD include an LF at the end of this line.
        # Clients MUST ignore an LF at the end of the line.
        #
        #  smart_reply     =  PKT-LINE("# service=$servicename" LF)
        #                     ref_list
        #                     "0000"
        server_advert = '# service=%s\n' % git_command
        packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower()
        _git_path = kallithea.CONFIG.get('git_path', 'git')
        cmd = [_git_path, git_command[4:],
               '--stateless-rpc', '--advertise-refs', self.content_path]
        log.debug('handling cmd %s', cmd)
        try:
            out = subprocessio.SubprocessIOChunker(cmd,
                starting_values=[packet_len + server_advert + '0000']
            )
        except EnvironmentError as e:
            log.error(traceback.format_exc())
            raise exc.HTTPExpectationFailed()
        resp = Response()
        resp.content_type = 'application/x-%s-advertisement' % str(git_command)
        resp.charset = None
        resp.app_iter = out
        return resp
Beispiel #29
0
    def  get_version_info(self, req, file="version"):
        resp = Response()
        resp.charset = 'UTF-8'
        if utils.is_xml_response(req):
            resp_file = os.path.join(possible_topdir,
                "keystone/content/%s.xml.tpl" % file)
            resp.content_type = "application/xml"
        else:
            resp_file = os.path.join(possible_topdir,
                "keystone/content/%s.json.tpl" % file)
            resp.content_type = "application/json"

        hostname = req.environ.get("SERVER_NAME")
        port = req.environ.get("SERVER_PORT")

        resp.unicode_body = template.template(resp_file,
            HOST=hostname,
            PORT=port,
            VERSION_STATUS=config.VERSION_STATUS,
            VERSION_DATE=config.VERSION_DATE)

        return resp
Beispiel #30
0
    def inforefs(self, request, environ):
        """
        WSGI Response producer for HTTP GET Git Smart
        HTTP /info/refs request.
        """

        git_command = request.GET.get('service')
        if git_command not in self.commands:
            log.debug('command %s not allowed', git_command)
            return exc.HTTPMethodNotAllowed()

        # note to self:
        # please, resist the urge to add '\n' to git capture and increment
        # line count by 1.
        # The code in Git client not only does NOT need '\n', but actually
        # blows up if you sprinkle "flush" (0000) as "0001\n".
        # It reads binary, per number of bytes specified.
        # if you do add '\n' as part of data, count it.
        server_advert = '# service=%s' % git_command
        packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower()
        _git_path = kallithea.CONFIG.get('git_path', 'git')
        cmd = [_git_path, git_command[4:],
               '--stateless-rpc', '--advertise-refs', self.content_path]
        log.debug('handling cmd %s', cmd)
        try:
            out = subprocessio.SubprocessIOChunker(cmd,
                starting_values=[packet_len + server_advert + '0000']
            )
        except EnvironmentError as e:
            log.error(traceback.format_exc())
            raise exc.HTTPExpectationFailed()
        resp = Response()
        resp.content_type = 'application/x-%s-advertisement' % str(git_command)
        resp.charset = None
        resp.app_iter = out
        return resp
def test_text_set_not_unicode():
    res = Response()
    res.charset = 'utf-8'
    assert_raises(TypeError, res.__setattr__, 'text',
                  'La Pe\xc3\xb1a')
Beispiel #32
0
def showpython(request: Request):
    res = Response()
    res.content_type = 'text/plain'
    res.charset = 'gb2312'
    res.body = 'Hello there, you are so beautiful'.encode()
    return res
Beispiel #33
0
                    content_type = saxutils.escape(content_type)
                    xml_output.append('<object><name>%s</name><hash>%s</hash>'\
                           '<bytes>%d</bytes><content_type>%s</content_type>'\
                           '<last_modified>%s</last_modified></object>' % \
                           (name, etag, size, content_type, created_at))
            container_list = ''.join([
                '<?xml version="1.0" encoding="UTF-8"?>\n',
                '<container name=%s>' % saxutils.quoteattr(container),
                ''.join(xml_output), '</container>'])
        else:
            if not container_list:
                return HTTPNoContent(request=req, headers=resp_headers)
            container_list = '\n'.join(r[0] for r in container_list) + '\n'
        ret = Response(body=container_list, request=req, headers=resp_headers)
        ret.content_type = out_content_type
        ret.charset = 'utf8'
        return ret

    def REPLICATE(self, req):
        """
        Handle HTTP REPLICATE request (json-encoded RPC calls for replication.)
        """
        try:
            post_args = split_path(unquote(req.path), 3)
        except ValueError, err:
            return HTTPBadRequest(body=str(err), content_type='text/plain',
                                request=req)
        drive, partition, hash = post_args
        if self.mount_check and not check_mount(self.root, drive):
            return Response(status='507 %s is not mounted' % drive)
        try:
Beispiel #34
0
            log.error(traceback.format_exc())
            raise exc.HTTPExpectationFailed()

        if git_command in [u'git-receive-pack']:
            # updating refs manually after each push.
            # Needed for pre-1.7.0.4 git clients using regular HTTP mode.
            _git_path = rhodecode.CONFIG.get('git_path', 'git')
            cmd = (u'%s --git-dir "%s" '
                   'update-server-info' % (_git_path, self.content_path))
            log.debug('handling cmd %s' % cmd)
            subprocess.call(cmd, shell=True)

        resp = Response()
        resp.content_type = 'application/x-%s-result' % git_command.encode(
            'utf8')
        resp.charset = None
        resp.app_iter = out
        return resp

    def __call__(self, environ, start_response):
        request = Request(environ)
        _path = self._get_fixedpath(request.path_info)
        if _path.startswith('info/refs'):
            app = self.inforefs
        elif [a for a in self.valid_accepts if a in request.accept]:
            app = self.backend
        try:
            resp = app(request, environ)
        except exc.HTTPException, e:
            resp = e
            log.error(traceback.format_exc())
def test_text_get_decode():
    res = Response()
    res.charset = 'utf-8'
    res.body = 'La Pe\xc3\xb1a'
    eq_(res.text, unicode('La Pe\xc3\xb1a', 'utf-8'))
Beispiel #36
0
    def backend(self, request, environ):
        """
        WSGI Response producer for HTTP POST Git Smart HTTP requests.
        Reads commands and data from HTTP POST's body.
        returns an iterator obj with contents of git command's
        response to stdout
        """
        # TODO(skreft): think how we could detect an HTTPLockedException, as
        # we probably want to have the same mechanism used by mercurial and
        # simplevcs.
        # For that we would need to parse the output of the command looking for
        # some signs of the HTTPLockedError, parse the data and reraise it in
        # pygrack. However, that would interfere with the streaming.
        #
        # Now the output of a blocked push is:
        # Pushing to http://test_regular:[email protected]:5001/vcs_test_git
        # POST git-receive-pack (1047 bytes)
        # remote: ERROR: Repository `vcs_test_git` locked by user `test_admin`. Reason:`lock_auto`
        # To http://test_regular:[email protected]:5001/vcs_test_git
        # ! [remote rejected] master -> master (pre-receive hook declined)
        # error: failed to push some refs to 'http://*****:*****@127.0.0.1:5001/vcs_test_git'

        git_command = self._get_fixedpath(request.path_info)
        if git_command not in self.commands:
            log.debug('command %s not allowed', git_command)
            return exc.HTTPForbidden()

        capabilities = None
        if git_command == 'git-upload-pack':
            capabilities = self._get_want_capabilities(request)

        if 'CONTENT_LENGTH' in environ:
            inputstream = FileWrapper(request.body_file_seekable,
                                      request.content_length)
        else:
            inputstream = request.body_file_seekable

        resp = Response()
        resp.content_type = ('application/x-%s-result' %
                             git_command.encode('utf8'))
        resp.charset = None

        pre_pull_messages = ''
        if git_command == 'git-upload-pack':
            status, pre_pull_messages = hooks.git_pre_pull(self.extras)
            if status != 0:
                resp.app_iter = self._build_failed_pre_pull_response(
                    capabilities, pre_pull_messages)
                return resp

        gitenv = dict(os.environ)
        # forget all configs
        gitenv['GIT_CONFIG_NOGLOBAL'] = '1'
        gitenv['RC_SCM_DATA'] = json.dumps(self.extras)
        cmd = [
            self.git_path, git_command[4:], '--stateless-rpc',
            self.content_path
        ]
        log.debug('handling cmd %s', cmd)

        out = subprocessio.SubprocessIOChunker(cmd,
                                               inputstream=inputstream,
                                               env=gitenv,
                                               cwd=self.content_path,
                                               shell=False,
                                               fail_on_stderr=False,
                                               fail_on_return_code=False)

        if self.update_server_info and git_command == 'git-receive-pack':
            # We need to fully consume the iterator here, as the
            # update-server-info command needs to be run after the push.
            out = list(out)

            # Updating refs manually after each push.
            # This is required as some clients are exposing Git repos internally
            # with the dumb protocol.
            cmd = [self.git_path, 'update-server-info']
            log.debug('handling cmd %s', cmd)
            output = subprocessio.SubprocessIOChunker(
                cmd,
                inputstream=inputstream,
                env=gitenv,
                cwd=self.content_path,
                shell=False,
                fail_on_stderr=False,
                fail_on_return_code=False)
            # Consume all the output so the subprocess finishes
            for _ in output:
                pass

        if git_command == 'git-upload-pack':
            unused_status, post_pull_messages = hooks.git_post_pull(
                self.extras)
            resp.app_iter = self._inject_messages_to_response(
                out, capabilities, pre_pull_messages, post_pull_messages)
        else:
            resp.app_iter = out

        return resp
Beispiel #37
0
def pythonhandler(request: Request):
    res = Response()
    res.charset = 'utf-8'
    res.body = 'welcom to ma python'.encode()
    return res
Beispiel #38
0
def pythonhandler(request):
    res = Response()
    res.charset = 'utf-8'
    res.body = '<h1> Welcome to Python</h1>'.encode()
    return res
Beispiel #39
0
new_req = Request(req.environ)
new_req.some_attr
req.environ['webob.adhoc_attrs']

# response
res = Response()
res.status
res.headerlist
res.body
res.status = 404
res.status
res.status_code
res.headerlist = [('Content-type', 'text/html')]
res.body = 'test'
print res

# response headers
res.headers
res = Response(content_type='text/plain', charset=None)
f = res.body_file
f.write('hey')
f.write(u'test')
f.encoding
res.charset = 'utf8'
f.encoding
f.write(u'test')
res.app_iter
res.body

# Header Getters
Beispiel #40
0
    def GETorHEAD_base(self, req, server_type, partition, nodes, path,
                       attempts):
        """
        Base handler for HTTP GET or HEAD requests.

        :param req: webob.Request object
        :param server_type: server type
        :param partition: partition
        :param nodes: nodes
        :param path: path for the request
        :param attempts: number of attempts to try
        :returns: webob.Response object
        """
        statuses = []
        reasons = []
        bodies = []
        source = None
        sources = []
        newest = req.headers.get('x-newest', 'f').lower() in TRUE_VALUES
        nodes = iter(nodes)
        while len(statuses) < attempts:
            try:
                node = nodes.next()
            except StopIteration:
                break
            if self.error_limited(node):
                continue
            try:
                with ConnectionTimeout(self.app.conn_timeout):
                    headers = dict(req.headers)
                    headers['Connection'] = 'close'
                    conn = http_connect(node['ip'], node['port'],
                        node['device'], partition, req.method, path,
                        headers=headers,
                        query_string=req.query_string)
                with Timeout(self.app.node_timeout):
                    possible_source = conn.getresponse()
                    # See NOTE: swift_conn at top of file about this.
                    possible_source.swift_conn = conn
            except (Exception, Timeout):
                self.exception_occurred(node, server_type,
                    _('Trying to %(method)s %(path)s') %
                    {'method': req.method, 'path': req.path})
                continue
            if possible_source.status == HTTP_INSUFFICIENT_STORAGE:
                self.error_limit(node)
                continue
            if is_success(possible_source.status) or \
               is_redirection(possible_source.status):
                # 404 if we know we don't have a synced copy
                if not float(possible_source.getheader('X-PUT-Timestamp', 1)):
                    statuses.append(HTTP_NOT_FOUND)
                    reasons.append('')
                    bodies.append('')
                    possible_source.read()
                    continue
                if newest:
                    if sources:
                        ts = float(source.getheader('x-put-timestamp') or
                                   source.getheader('x-timestamp') or 0)
                        pts = float(
                            possible_source.getheader('x-put-timestamp') or
                            possible_source.getheader('x-timestamp') or 0)
                        if pts > ts:
                            sources.insert(0, possible_source)
                        else:
                            sources.append(possible_source)
                    else:
                        sources.insert(0, possible_source)
                    source = sources[0]
                    statuses.append(source.status)
                    reasons.append(source.reason)
                    bodies.append('')
                    continue
                else:
                    source = possible_source
                    break
            statuses.append(possible_source.status)
            reasons.append(possible_source.reason)
            bodies.append(possible_source.read())
            if is_server_error(possible_source.status):
                self.error_occurred(node, _('ERROR %(status)d %(body)s ' \
                    'From %(type)s Server') %
                    {'status': possible_source.status,
                    'body': bodies[-1][:1024], 'type': server_type})
        if source:
            if req.method == 'GET' and \
               source.status in (HTTP_OK, HTTP_PARTIAL_CONTENT):
                if newest:
                    # we need to close all hanging swift_conns
                    sources.pop(0)
                    for src in sources:
                        self.close_swift_conn(src)

                res = Response(request=req, conditional_response=True)
                res.app_iter = self._make_app_iter(node, source, res)
                # See NOTE: swift_conn at top of file about this.
                res.swift_conn = source.swift_conn
                update_headers(res, source.getheaders())
                # Used by container sync feature
                if res.environ is None:
                    res.environ = dict()
                res.environ['swift_x_timestamp'] = \
                    source.getheader('x-timestamp')
                update_headers(res, {'accept-ranges': 'bytes'})
                res.status = source.status
                res.content_length = source.getheader('Content-Length')
                if source.getheader('Content-Type'):
                    res.charset = None
                    res.content_type = source.getheader('Content-Type')
                return res
            elif is_success(source.status) or is_redirection(source.status):
                res = status_map[source.status](request=req)
                update_headers(res, source.getheaders())
                # Used by container sync feature
                if res.environ is None:
                    res.environ = dict()
                res.environ['swift_x_timestamp'] = \
                    source.getheader('x-timestamp')
                update_headers(res, {'accept-ranges': 'bytes'})
                res.content_length = source.getheader('Content-Length')
                if source.getheader('Content-Type'):
                    res.charset = None
                    res.content_type = source.getheader('Content-Type')
                return res
        return self.best_response(req, statuses, reasons, bodies,
                                  '%s %s' % (server_type, req.method))
Beispiel #41
0
def showpython(request: Request):
    res = Response()
    res.content_type = 'text/plain'
    res.charset = 'gb2312'
    res.text = 'Hello there. Nice to meet you. Sunshine boys and beautiful girls.'
    return res
def test_text_set_no_charset():
    res = Response()
    res.charset = None
    assert_raises(AttributeError, res.__setattr__, 'text', 'abc')
Beispiel #43
0
 def test_app_error_with_bytes_message(self):
     resp = Response(six.u('\xe9').encode('utf8'))
     resp.charset = 'utf8'
     err = webtest.AppError(to_bytes('message %s'), resp)
     self.assertEqual(err.args, (six.u('message \xe9'), ))
Beispiel #44
0
def showpython(request: Request) -> Response:
    res = Response()
    res.content_type = 'text/plain'
    res.charset = 'utf-8'
    res.body = '<h1>Hello there, sunshine boys and beautiful girls, how are you?</h1>'
    return res
Beispiel #45
0
 def test_app_error_misc(self):
     resp = Response(six.u('\xe9').encode('utf8'))
     resp.charset = ''
     # dont check the output. just make sure it doesn't fail
     webtest.AppError(to_bytes('message %s'), resp)
     webtest.AppError(six.u('messag\xe9 %s'), six.b('\xe9'))
Beispiel #46
0
def show_python(request: Request):
    res = Response()
    res.content_type = 'test/plain'
    res.charset = 'utf-8'
    res.body = '<h1>welcome to python</h1>'.encode()
    return res
Beispiel #47
0
                if is_subdir:
                    output_list.append('<subdir name="%s" />' % name)
                else:
                    item = '<container><name>%s</name><count>%s</count>' \
                           '<bytes>%s</bytes></container>' % \
                           (name, object_count, bytes_used)
                    output_list.append(item)
            output_list.append('</account>')
            account_list = '\n'.join(output_list)
        else:
            if not account_list:
                return HTTPNoContent(request=req, headers=resp_headers)
            account_list = '\n'.join(r[0] for r in account_list) + '\n'
        ret = Response(body=account_list, request=req, headers=resp_headers)
        ret.content_type = out_content_type
        ret.charset = 'utf-8'
        return ret

    def REPLICATE(self, req):
        """
        Handle HTTP REPLICATE request.
        Handler for RPC calls for account replication.
        """
        try:
            post_args = split_path(unquote(req.path), 3)
        except ValueError, err:
            return HTTPBadRequest(body=str(err),
                                  content_type='text/plain',
                                  request=req)
        drive, partition, hash = post_args
        if self.mount_check and not check_mount(self.root, drive):
Beispiel #48
0
def showpython(request: Request):
    res = Response()
    res.content_type = 'text/plain'
    res.charset = 'gb2312'
    res.body = '<h1>Hello there.</h1>'
    return res
Beispiel #49
0
def test_unicode_body_set_no_charset():
    res = Response()
    res.charset = None
    assert_raises(AttributeError, res.__setattr__, 'unicode_body', 'abc')