source: server/lib/gutenbach/server/server.py @ 6effd50

no-cups
Last change on this file since 6effd50 was 6effd50, checked in by Jessica B. Hamrick <jhamrick@…>, 12 years ago

Fix some errors in server/, handle fatal errors better

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[7bd1035]1from gutenbach.server.requests import GutenbachRequestHandler
[738d179]2import BaseHTTPServer
[7bd1035]3import gutenbach.ipp as ipp
[738d179]4import logging
5import traceback
[dc40fe9]6import sys
[7bd1035]7
8# initialize logger
9logger = logging.getLogger(__name__)
10
11class GutenbachIPPServer(BaseHTTPServer.BaseHTTPRequestHandler):
12    def setup(self):
13        self.root = GutenbachRequestHandler()
14        BaseHTTPServer.BaseHTTPRequestHandler.setup(self)
15
16    def handle_one_request(self):
17        self.raw_requestline = self.rfile.readline()
18        if not self.raw_requestline:
19            self.close_connection = 1
20            return
21        if not self.parse_request(): # An error code has been sent, just exit
22            return
23        self.handle_ipp()
24
25    def handle_ipp(self):
26        # Receive a request
27        length = int(self.headers.getheader('content-length', 0))
28        request = ipp.Request(request=self.rfile, length=length)
29        logger.debug("Received request: %s" % repr(request))
30
[dc40fe9]31        # Create an empty response object
32        response = ipp.ops.make_empty_response(request)
[7bd1035]33
[dc40fe9]34        # Get the handler and pass it the request and response
35        # objects.  It will fill in values for the response object or
36        # thrown an error.
[7bd1035]37        try:
38            self.root.handle(request, response)
[dc40fe9]39           
40        # Handle any errors that occur.  If an exception occurs that
41        # is an IPP error, then we can get the error code from the
42        # exception itself.
[6effd50]43        except ipp.errors.IPPException:
[dc40fe9]44            exctype, excval, exctb = sys.exc_info()
45            excval.update_response(response)
46            logger.error(traceback.format_exc())
47
48        # If it wasn't an IPP error, then it's our fault, so mark it
49        # as an internal server error
50        except Exception:
[6effd50]51            response.operation_id = ipp.StatusCodes.INTERNAL_ERROR
[7bd1035]52            logger.error(traceback.format_exc())
53
54        # Send the response across HTTP
55        logger.debug("Sending response: %s" % repr(response))
[6effd50]56        try:
57            binary = response.packed_value
58        except:
59            logger.fatal(traceback.format_exc())
60            sys.exit(1)
61           
[7bd1035]62        self.send_response(200, "Gutenbach IPP Response")
63        self.send_header("Content-Type", "application/ipp")
64        self.send_header("Connection", "close")
65        self.end_headers()
[6effd50]66        self.wfile.write(binary)
Note: See TracBrowser for help on using the repository browser.