示例#1
0
    def handle_error(self, req, client, addr, exc):
        request_start = datetime.now()
        addr = addr or ('', -1)  # unix socket case
        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
                InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
                LimitRequestLine, LimitRequestHeaders,
                InvalidProxyLine, ForbiddenProxyRequest,
                SSLError)):

            status_int = 400
            reason = "Bad Request"

            if isinstance(exc, InvalidRequestLine):
                mesg = "Invalid Request Line '%s'" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "Invalid Method '%s'" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "Invalid HTTP Version '%s'" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "%s" % str(exc)
                if not req and hasattr(exc, "req"):
                    req = exc.req  # for access log
            elif isinstance(exc, LimitRequestLine):
                mesg = "%s" % str(exc)
            elif isinstance(exc, LimitRequestHeaders):
                mesg = "Error parsing headers: '%s'" % str(exc)
            elif isinstance(exc, InvalidProxyLine):
                mesg = "'%s'" % str(exc)
            elif isinstance(exc, ForbiddenProxyRequest):
                reason = "Forbidden"
                mesg = "Request forbidden"
                status_int = 403
            elif isinstance(exc, SSLError):
                reason = "Forbidden"
                mesg = "'%s'" % str(exc)
                status_int = 403

            msg = "Invalid request from ip={ip}: {error}"
            self.log.debug(msg.format(ip=addr[0], error=str(exc)))
        else:
            if hasattr(req, "uri"):
                self.log.exception("Error handling request %s", req.uri)
            status_int = 500
            reason = "Internal Server Error"
            mesg = ""

        if req is not None:
            request_time = datetime.now() - request_start
            environ = default_environ(req, client, self.cfg)
            environ['REMOTE_ADDR'] = addr[0]
            environ['REMOTE_PORT'] = str(addr[1])
            resp = Response(req, client, self.cfg)
            resp.status = "%s %s" % (status_int, reason)
            resp.response_length = len(mesg)
            self.log.access(resp, req, environ, request_time)

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.debug("Failed to send error message.")
示例#2
0
文件: base.py 项目: WoLpH/gunicorn
    def handle_error(self, client, exc):
        self.log.exception("Error handling request")

        status_int = 500
        reason = "Internal Server Error"
        mesg = ""

        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
            InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):
            
            status_int = 400
            reason = "Bad Request"
            
            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "<p>Invalid Header '%s'</p>" % str(exc)
            
        if self.debug:
            tb =  traceback.format_exc()
            mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.warning("Failed to send error message.")
示例#3
0
    def handle_error(self, req, client, addr, exc):
        request_start = datetime.now()
        addr = addr or ('', -1)  # unix socket case
        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
                InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
                LimitRequestLine, LimitRequestHeaders,
                InvalidProxyLine, ForbiddenProxyRequest,
                SSLError)):

            status_int = 400
            reason = "Bad Request"

            if isinstance(exc, InvalidRequestLine):
                mesg = "Invalid Request Line '%s'" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "Invalid Method '%s'" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "Invalid HTTP Version '%s'" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "%s" % str(exc)
                if not req and hasattr(exc, "req"):
                    req = exc.req  # for access log
            elif isinstance(exc, LimitRequestLine):
                mesg = "%s" % str(exc)
            elif isinstance(exc, LimitRequestHeaders):
                mesg = "Error parsing headers: '%s'" % str(exc)
            elif isinstance(exc, InvalidProxyLine):
                mesg = "'%s'" % str(exc)
            elif isinstance(exc, ForbiddenProxyRequest):
                reason = "Forbidden"
                mesg = "Request forbidden"
                status_int = 403
            elif isinstance(exc, SSLError):
                reason = "Forbidden"
                mesg = "'%s'" % str(exc)
                status_int = 403

            msg = "Invalid request from ip={ip}: {error}"
            self.log.debug(msg.format(ip=addr[0], error=str(exc)))
        else:
            if hasattr(req, "uri"):
                self.log.exception("Error handling request %s", req.uri)
            status_int = 500
            reason = "Internal Server Error"
            mesg = ""

        if req is not None:
            request_time = datetime.now() - request_start
            environ = default_environ(req, client, self.cfg)
            environ['REMOTE_ADDR'] = addr[0]
            environ['REMOTE_PORT'] = str(addr[1])
            resp = Response(req, client, self.cfg)
            resp.status = "%s %s" % (status_int, reason)
            resp.response_length = len(mesg)
            self.log.access(resp, req, environ, request_time)

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.debug("Failed to send error message.")
示例#4
0
文件: sync.py 项目: gnublade/gunicorn
 def handle_request(self, req, client, addr):
     try:
         debug = self.cfg.debug or False
         self.cfg.pre_request(self, req)
         resp, environ = wsgi.create(req, client, addr,
                 self.address, self.cfg)
         # Force the connection closed until someone shows
         # a buffering proxy that supports Keep-Alive to
         # the backend.
         resp.force_close()
         self.nr += 1
         if self.nr >= self.max_requests:
             self.log.info("Autorestarting worker after current request.")
             self.alive = False
         respiter = self.wsgi(environ, resp.start_response)
         for item in respiter:
             resp.write(item)
         resp.close()
         if hasattr(respiter, "close"):
             respiter.close()
     except socket.error:
         raise
     except Exception, e:
         # Only send back traceback in HTTP in debug mode.
         if not self.debug:
             raise
         util.write_error(client, traceback.format_exc())
         return
示例#5
0
    def handle_error(self, client, exc):
        self.log.exception("Error hanlding request")

        status_int = 500
        reason = "Internal Server Error"
        mesg = ""

        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
            InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):
            
            status_int = 400
            reason = "Bad Request"
            
            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method'%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "<p>Invalid Header'%s'</p>" % str(exc)
            
        if self.debug:
            tb =  traceback.format_exc()
            mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.exception("Failed to send error message.")
示例#6
0
文件: base.py 项目: lericson/gunicorn
 def handle(self, client, addr):
      
     self.nb_connections += 1
     try:
         self.init_sock(client)
         while True:
             req = KeepaliveRequest(client, addr, self.address, self.conf)
             
             try:
                 environ = req.read()
                 if not environ or not req.parser.headers:
                     return
                 respiter = self.app(environ, req.start_response)
                 if respiter == ALREADY_HANDLED:
                     break
                 for item in respiter:
                     req.response.write(item)
                 req.response.close()
                 if hasattr(respiter, "close"):
                     respiter.close()
                 if req.parser.should_close:
                     break
             except Exception, e:
                 #Only send back traceback in HTTP in debug mode.
                 if not self.debug:
                     raise
                 util.write_error(client, traceback.format_exc())
                 break
     except socket.error, e:
         if e[0] != errno.EPIPE:
             self.log.exception("Error processing request.")
         else:
             self.log.warn("Ignoring EPIPE")
示例#7
0
    def handle(self, client, addr):
        try:
            req = http.Request(client, addr, self.address, self.conf)

            try:
                environ = req.read()
                if not environ or not req.parser.status_line:
                    return

                respiter = self.app(environ, req.start_response)
                for item in respiter:
                    req.response.write(item)
                req.response.close()
                if hasattr(respiter, "close"):
                    respiter.close()
            except socket.error:
                raise
            except Exception, e:
                # Only send back traceback in HTTP in debug mode.
                if not self.debug:
                    raise
                util.write_error(client, traceback.format_exc())
                return 

        except socket.error, e:
            if e[0] != errno.EPIPE:
                self.log.exception("Error processing request.")
            else:
                self.log.warn("Ignoring EPIPE")
示例#8
0
文件: base.py 项目: mw44118/gunicorn
    def handle_error(self, client, exc):
        
        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
            InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):
            
            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method'%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "<p>Invalid Header'%s'</p>" % str(exc)
            reason = "Bad Request"
            status_int = 400
        else:
            mesg = reason = "Internal Server reason"
            status_int = 500
            
        if self.debug:
            tb =  traceback.format_exc()
            mesg += "<h2>Traceback:</23><pre>%s</pre>" % tb

        try:
            util.write_error(client, mesg, status_int=status_int, 
                    reason=reason)
        except:
            self.log.warning("Unexpected error" % traceback.format_exc())
            pass
示例#9
0
 def handle_request(self, req, sock, addr):
     try:
         debug = self.cfg.debug or False
         self.cfg.pre_request(self, req)
         resp, environ = wsgi.create(req, sock, addr, self.address, self.cfg)
         self.nr += 1
         if self.alive and self.nr >= self.max_requests:
             self.log.info("Autorestarting worker after current request.")
             resp.force_close()
             self.alive = False
         respiter = self.wsgi(environ, resp.start_response)
         if respiter == ALREADY_HANDLED:
             return False
         for item in respiter:
             resp.write(item)
         resp.close()
         if hasattr(respiter, "close"):
             respiter.close()
         if req.should_close():
             raise StopIteration()
     except StopIteration:
         raise
     except Exception, e:
         #Only send back traceback in HTTP in debug mode.
         if not self.debug:
             raise
         util.write_error(sock, traceback.format_exc())
         return False
示例#10
0
    def handle_error(self, req, client, addr, exc):
        request_start = datetime.now()
        addr = addr or ('', -1) # unix socket case
        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
            InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
            LimitRequestLine, LimitRequestHeaders,)):

            status_int = 400
            reason = "Bad Request"

            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "<p>Invalid Header '%s'</p>" % str(exc)
            elif isinstance(exc, LimitRequestLine):
                mesg = "<p>%s</p>" % str(exc)
            elif isinstance(exc, LimitRequestHeaders):
                mesg = "<p>Error parsing headers: '%s'</p>" % str(exc)

            self.log.debug("Invalid request from ip={ip}: {error}"\
                           "".format(ip=addr[0],
                                     error=str(exc),
                                    )
                          )
        else:
            self.log.exception("Error handling request")

            status_int = 500
            reason = "Internal Server Error"
            mesg = ""

        if req is not None:
            request_time = datetime.now() - request_start
            environ = default_environ(req, client, self.cfg)
            environ['REMOTE_ADDR'] = addr[0]
            environ['REMOTE_PORT'] = str(addr[1])
            resp = Response(req, client)
            resp.status = "%s %s" % (status_int, reason)
            resp.response_length = len(mesg)
            self.log.access(resp, req, environ, request_time)

        if self.debug:
            tb = traceback.format_exc()
            mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.debug("Failed to send error message.")
示例#11
0
文件: worker.py 项目: davisp/gunicorn
    def handle(self, client, addr):
        util.close_on_exec(client)
        try:
            req = http.Request(client, addr, self.address, self.debug)

            try:
                response = self.app(req.read(), req.start_response)
            except Exception, e:
                util.write_error(client, traceback.format_exc())
                return 

            http.Response(client, response, req).send()
示例#12
0
文件: base.py 项目: rbranson/gunicorn
    def handle_error(self, req, client, addr, exc):
        request_start = datetime.now()
        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
            InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
            LimitRequestLine, LimitRequestHeaders,)):

            status_int = 400
            reason = "Bad Request"

            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "<p>Invalid Header '%s'</p>" % str(exc)
            elif isinstance(exc, LimitRequestLine):
                mesg = "<p>%s</p>" % str(exc)
            elif isinstance(exc, LimitRequestHeaders):
                mesg = "<p>Error parsing headers: '%s'</p>" % str(exc)

            self.log.debug("Invalid request from ip={ip}: {error}"\
                           "".format(ip=client.getpeername()[0],
                                     error=repr(exc),
                                    )
                          )
        else:
            self.log.exception("Error handling request")

            status_int = 500
            reason = "Internal Server Error"
            mesg = ""

        if req is not None:
            request_time = datetime.now() - request_start
            environ = default_environ(req, client, self.cfg)
            environ['REMOTE_ADDR'] = addr[0]
            environ['REMOTE_PORT'] = str(addr[1])
            resp = Response(req, client)
            resp.status = "%s %s" % (status_int, reason)
            resp.response_length = len(mesg)
            self.log.access(resp, req, environ, request_time)

        if self.debug:
            tb = traceback.format_exc()
            mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.warning("Failed to send error message.")
示例#13
0
文件: sync.py 项目: slideinc/gunicorn
 def handle_request(self, req, client, addr):
     try:
         debug = self.cfg.debug or False
         resp, environ = wsgi.create(req, client, addr, self.address, self.cfg)
         respiter = self.wsgi(environ, resp.start_response)
         for item in respiter:
             resp.write(item)
         resp.close()
         if hasattr(respiter, "close"):
             respiter.close()
     except socket.error:
         raise
     except Exception, e:
         # Only send back traceback in HTTP in debug mode.
         if not self.debug:
             raise
         util.write_error(client, traceback.format_exc())
         return
示例#14
0
文件: async.py 项目: timf/gunicorn
 def handle_request(self, req, sock, addr):
     try:
         debug = self.cfg.debug or False
         resp, environ = wsgi.create(req, sock, addr, self.address, debug)
         respiter = self.wsgi(environ, resp.start_response)
         if respiter == ALREADY_HANDLED:
             return False
         for item in respiter:
             resp.write(item)
         resp.close()
         if hasattr(respiter, "close"):
             respiter.close()
         if req.should_close():
             raise StopIteration()
     except Exception, e:
         #Only send back traceback in HTTP in debug mode.
         if not self.debug:
             raise
         util.write_error(sock, traceback.format_exc())
         return False
示例#15
0
    def handle_error(self, client, exc):
        if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
            InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):

            status_int = 400
            reason = "Bad Request"

            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
                mesg = "<p>Invalid Header '%s'</p>" % str(exc)
            elif isinstance(exc, LimitRequestLine):
                mesg = "<p>%s</p>" % str(exc)
            elif isinstance(exc, LimitRequestHeaders):
                mesg = "<p>Error parsing headers: '%s'</p>" % str(exc)

            self.log.debug("Invalid request from ip={ip}: {error}"\
                           "".format(ip=client.getpeername()[0],
                                     error=repr(exc),
                                    )
                          )
        else:
            self.log.exception("Error handling request")

            status_int = 500
            reason = "Internal Server Error"
            mesg = ""

        if self.debug:
            tb =  traceback.format_exc()
            mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.warning("Failed to send error message.")
示例#16
0
文件: base.py 项目: tanish2k/gunicorn
    def handle_error(self, req, client, addr, exc):
        request_start = datetime.now()
        addr = addr or ('', -1)  # unix socket case
        if isinstance(exc, (
                InvalidRequestLine,
                InvalidRequestMethod,
                InvalidHTTPVersion,
                InvalidHeader,
                InvalidHeaderName,
                LimitRequestLine,
                LimitRequestHeaders,
                InvalidProxyLine,
                ForbiddenProxyRequest,
        )):

            status_int = 400
            reason = "Bad Request"

            if isinstance(exc, InvalidRequestLine):
                mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidRequestMethod):
                mesg = "<p>Invalid Method '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidHTTPVersion):
                mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
            elif isinstance(exc, (
                    InvalidHeaderName,
                    InvalidHeader,
            )):
                mesg = "<p>%s</p>" % str(exc)
                if not req and hasattr(exc, "req"):
                    req = exc.req  # for access log
            elif isinstance(exc, LimitRequestLine):
                mesg = "<p>%s</p>" % str(exc)
            elif isinstance(exc, LimitRequestHeaders):
                mesg = "<p>Error parsing headers: '%s'</p>" % str(exc)
            elif isinstance(exc, InvalidProxyLine):
                mesg = "<p>'%s'</p>" % str(exc)
            elif isinstance(exc, ForbiddenProxyRequest):
                reason = "Forbidden"
                mesg = "<p>Request forbidden</p>"
                status_int = 403

            self.log.debug("Invalid request from ip={ip}: {error}"\
                           "".format(ip=addr[0],
                                     error=str(exc),
                                    )
                          )
        elif isinstance(exc, socket.timeout):
            status_int = 408
            reason = "Request Timeout"
            mesg = "<p>The server timed out handling for the request</p>"
        else:
            self.log.exception("Error handling request")

            status_int = 500
            reason = "Internal Server Error"
            mesg = ""

        if req is not None:
            request_time = datetime.now() - request_start
            environ = default_environ(req, client, self.cfg)
            environ['REMOTE_ADDR'] = addr[0]
            environ['REMOTE_PORT'] = str(addr[1])
            resp = Response(req, client)
            resp.status = "%s %s" % (status_int, reason)
            resp.response_length = len(mesg)
            self.log.access(resp, req, environ, request_time)

        if self.debug:
            tb = traceback.format_exc()
            mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb

        try:
            util.write_error(client, status_int, reason, mesg)
        except:
            self.log.debug("Failed to send error message.")