[7bd1035] | 1 | from gutenbach.server.requests import GutenbachRequestHandler |
---|
[738d179] | 2 | import BaseHTTPServer |
---|
[7bd1035] | 3 | import gutenbach.ipp as ipp |
---|
| 4 | import gutenbach.ipp.constants as const |
---|
[738d179] | 5 | import logging |
---|
| 6 | import traceback |
---|
[7bd1035] | 7 | |
---|
| 8 | # initialize logger |
---|
| 9 | logger = logging.getLogger(__name__) |
---|
| 10 | |
---|
| 11 | class 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 | |
---|
| 31 | # Operation attributes -- typically the same for any request |
---|
| 32 | attributes = [ |
---|
| 33 | ipp.Attribute( |
---|
| 34 | 'attributes-charset', |
---|
| 35 | [ipp.Value(ipp.Tags.CHARSET, 'utf-8')]), |
---|
| 36 | ipp.Attribute( |
---|
| 37 | 'attributes-natural-language', |
---|
| 38 | [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, 'en-us')]) |
---|
| 39 | ] |
---|
| 40 | # Put the operation attributes in a group |
---|
| 41 | attribute_group = ipp.AttributeGroup( |
---|
| 42 | const.AttributeTags.OPERATION, |
---|
| 43 | attributes) |
---|
| 44 | |
---|
| 45 | # Set up the default response -- handlers will override these |
---|
| 46 | # values if they need to |
---|
| 47 | response_kwargs = {} |
---|
| 48 | response_kwargs['version'] = request.version |
---|
| 49 | response_kwargs['operation_id'] = const.StatusCodes.OK |
---|
| 50 | response_kwargs['request_id'] = request.request_id |
---|
| 51 | response_kwargs['attribute_groups'] = [attribute_group] |
---|
| 52 | response = ipp.Request(**response_kwargs) |
---|
| 53 | |
---|
| 54 | # Get the handler and pass it the request and response objects |
---|
| 55 | try: |
---|
| 56 | self.root.handle(request, response) |
---|
| 57 | except: |
---|
[c70a3c9] | 58 | response.operation_id = const.StatusCodes.INTERNAL_ERROR |
---|
[7bd1035] | 59 | logger.error(traceback.format_exc()) |
---|
| 60 | |
---|
| 61 | # Send the response across HTTP |
---|
| 62 | logger.debug("Sending response: %s" % repr(response)) |
---|
| 63 | self.send_response(200, "Gutenbach IPP Response") |
---|
| 64 | self.send_header("Content-Type", "application/ipp") |
---|
| 65 | self.send_header("Connection", "close") |
---|
| 66 | self.end_headers() |
---|
| 67 | self.wfile.write(response.packed_value) |
---|