[d04a689] | 1 | import BaseHTTPServer |
---|
[b2e077a] | 2 | import gutenbach.ipp as ipp |
---|
[287d6ec] | 3 | import gutenbach.ipp.constants as const |
---|
[b2e077a] | 4 | from gutenbach.server.printer import GutenbachPrinter |
---|
| 5 | from gutenbach.server.exceptions import MalformedIPPRequestException |
---|
[287d6ec] | 6 | import logging |
---|
| 7 | |
---|
[d04a689] | 8 | # initialize logger |
---|
| 9 | logger = logging.getLogger(__name__) |
---|
[287d6ec] | 10 | |
---|
| 11 | def handler_for(operation): |
---|
| 12 | """A decorator method to mark a function with the operation id |
---|
| 13 | that it handles. This value will be stored in |
---|
| 14 | 'func.ipp_operation'. |
---|
| 15 | |
---|
| 16 | """ |
---|
| 17 | |
---|
| 18 | def f(func): |
---|
| 19 | func.ipp_operation = operation |
---|
| 20 | return func |
---|
| 21 | return f |
---|
| 22 | |
---|
[b2e077a] | 23 | class GutenbachRequestHandler(object): |
---|
[287d6ec] | 24 | |
---|
[b2e077a] | 25 | def __init__(self): |
---|
| 26 | self.printers = { |
---|
| 27 | "test": GutenbachPrinter(name="test") |
---|
| 28 | } |
---|
| 29 | self.default = "test" |
---|
[287d6ec] | 30 | |
---|
[b2e077a] | 31 | def handle(self, request, response): |
---|
| 32 | # look up the handler |
---|
| 33 | handler = None |
---|
[9eeab06] | 34 | handler_name = None |
---|
[287d6ec] | 35 | for d in dir(self): |
---|
| 36 | if getattr(getattr(self, d), "ipp_operation", None) == request.operation_id: |
---|
[9eeab06] | 37 | handler_name = d |
---|
[b2e077a] | 38 | break |
---|
| 39 | # we couldn't find a handler, so default to unknown operation |
---|
[9eeab06] | 40 | if handler_name is None: |
---|
| 41 | handler_name = "unknown_operation" |
---|
[b2e077a] | 42 | # call the handler |
---|
[9eeab06] | 43 | handler = getattr(self, handler_name) |
---|
| 44 | logger.debug("Sending request to handler '%s'" % handler_name) |
---|
[b2e077a] | 45 | handler(request, response) |
---|
[287d6ec] | 46 | |
---|
| 47 | def unknown_operation(self, request, response): |
---|
[9eeab06] | 48 | logger.warning("Received unknown operation 0x%x" % request.operation_id) |
---|
[287d6ec] | 49 | response.operation_id = const.StatusCodes.OPERATION_NOT_SUPPORTED |
---|
| 50 | |
---|
[9eeab06] | 51 | ##### Helper functions |
---|
| 52 | |
---|
[b2e077a] | 53 | def _get_printer_attributes(self, printer, request, response): |
---|
| 54 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 55 | const.AttributeTags.PRINTER, |
---|
| 56 | printer.get_printer_attributes(request))) |
---|
[287d6ec] | 57 | |
---|
[b2e077a] | 58 | def _get_job_attributes(self, job_id, printer, request, response): |
---|
| 59 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 60 | const.AttributeTags.JOB, |
---|
| 61 | job.get_job_attributes(request))) |
---|
| 62 | |
---|
| 63 | def _get_printer_name(self, request): |
---|
| 64 | # make sure the first group is an OPERATION group |
---|
| 65 | group_tag = request.attribute_groups[0].tag |
---|
| 66 | if group_tag != const.AttributeTags.OPERATION: |
---|
| 67 | raise MalformedIPPRequestException, \ |
---|
| 68 | "Expected OPERATION group tag, got %d\n", group_tag |
---|
| 69 | |
---|
| 70 | # make sure the printer-uri value is appropriate |
---|
[9eeab06] | 71 | printer_name_attr = request.attribute_groups[0]['printer-uri'] |
---|
| 72 | printer_name_value_tag = printer_name_attr.values[0].value_tag |
---|
| 73 | if printer_name_value_tag != const.CharacterStringTags.URI: |
---|
[b2e077a] | 74 | raise MalformedIPPRequestException, \ |
---|
[9eeab06] | 75 | "Expected URI value tag, got %s" % printer_name_value_tag |
---|
[b2e077a] | 76 | |
---|
| 77 | # actually get the printer name |
---|
[9eeab06] | 78 | printer_name_value = printer_name_attr.values[0].value |
---|
[b2e077a] | 79 | # XXX: hack -- CUPS will strip the port from the request, so |
---|
| 80 | # we can't do an exact comparison (also the hostname might be |
---|
| 81 | # different, depending on the CNAME or whether it's localhost) |
---|
[9eeab06] | 82 | printer_name = printer_name_value.split("/")[-1] |
---|
[b2e077a] | 83 | |
---|
[9eeab06] | 84 | # make sure the printer name is valid |
---|
| 85 | if printer_name not in self.printers: |
---|
| 86 | raise ValueError, "Invalid printer uri: %s" % printer_name_value |
---|
[b2e077a] | 87 | |
---|
[9eeab06] | 88 | return printer_name |
---|
[b2e077a] | 89 | |
---|
[9eeab06] | 90 | ##### Printer Commands |
---|
| 91 | |
---|
| 92 | def print_job(self, request, response): |
---|
| 93 | pass |
---|
| 94 | |
---|
| 95 | def validate_job(self, request, response): |
---|
| 96 | pass |
---|
| 97 | |
---|
| 98 | @handler_for(const.Operations.GET_JOBS) |
---|
| 99 | def get_jobs(self, request, response): |
---|
| 100 | printer_name = self._get_printer_name(request) |
---|
| 101 | # Each job will append a new job attribute group. |
---|
| 102 | for job in self.printers[printer_name].get_jobs(): |
---|
| 103 | self._get_job_attributes(job, request, response) |
---|
[b2e077a] | 104 | response.operation_id = const.StatusCodes.OK |
---|
[287d6ec] | 105 | |
---|
[9eeab06] | 106 | def print_uri(self, request, response): |
---|
| 107 | pass |
---|
| 108 | |
---|
| 109 | def create_job(self, request, response): |
---|
| 110 | pass |
---|
| 111 | |
---|
| 112 | def pause_printer(self, request, response): |
---|
| 113 | pass |
---|
| 114 | |
---|
| 115 | def resume_printer(self, request, response): |
---|
| 116 | pass |
---|
| 117 | |
---|
[287d6ec] | 118 | @handler_for(const.Operations.GET_PRINTER_ATTRIBUTES) |
---|
| 119 | def get_printer_attributes(self, request, response): |
---|
[b2e077a] | 120 | # this is just like cups_get_default, except the printer name |
---|
| 121 | # is given |
---|
[9eeab06] | 122 | printer_name = self._get_printer_name(request) |
---|
| 123 | self._get_printer_attributes(self.printers[printer_name], request, response) |
---|
| 124 | response.operation_id = const.StatusCodes.OK |
---|
| 125 | |
---|
| 126 | def set_printer_attributes(self, request, response): |
---|
| 127 | pass |
---|
| 128 | |
---|
| 129 | ##### Job Commands |
---|
| 130 | |
---|
| 131 | def cancel_job(self, request, response): |
---|
| 132 | pass |
---|
| 133 | |
---|
| 134 | def send_document(self, request, response): |
---|
| 135 | pass |
---|
| 136 | |
---|
| 137 | def send_uri(self, request, response): |
---|
| 138 | pass |
---|
| 139 | |
---|
| 140 | def get_job_attributes(self, request, response): |
---|
| 141 | printer_name = self._get_printer_name(request) |
---|
| 142 | job_id = self._get_job_id(request) |
---|
| 143 | self._get_job_attributes( |
---|
| 144 | self.printers[printer_name].get_job(job_id), request, response) |
---|
| 145 | |
---|
| 146 | def set_job_attributes(self, request, response): |
---|
| 147 | pass |
---|
| 148 | |
---|
| 149 | def cups_get_document(self, request, response): |
---|
| 150 | pass |
---|
| 151 | |
---|
| 152 | def restart_job(self, request, response): |
---|
| 153 | pass |
---|
| 154 | |
---|
| 155 | def promote_job(self, request, response): |
---|
| 156 | pass |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | ##### CUPS Specific Commands |
---|
| 160 | |
---|
| 161 | @handler_for(const.Operations.CUPS_GET_DEFAULT) |
---|
| 162 | def cups_get_default(self, request, response): |
---|
| 163 | self._get_printer_attributes(self.printers[self.default], request, response) |
---|
[b2e077a] | 164 | response.operation_id = const.StatusCodes.OK |
---|
[287d6ec] | 165 | |
---|
| 166 | @handler_for(const.Operations.CUPS_GET_PRINTERS) |
---|
| 167 | def cups_get_printers(self, request, response): |
---|
| 168 | # Each printer will append a new printer attribute group. |
---|
[b2e077a] | 169 | for printer in self.printers: |
---|
| 170 | self._get_printer_attributes(self.printers[printer], request, response) |
---|
| 171 | response.operation_id = const.StatusCodes.OK |
---|
[287d6ec] | 172 | |
---|
| 173 | @handler_for(const.Operations.CUPS_GET_CLASSES) |
---|
| 174 | def cups_get_classes(self, request, response): |
---|
| 175 | response.operation_id = const.StatusCodes.OK |
---|
| 176 | # We have no printer classes, so nothing to return. |
---|
| 177 | |
---|
[b2e077a] | 178 | class GutenbachIPPServer(BaseHTTPServer.BaseHTTPRequestHandler): |
---|
[287d6ec] | 179 | def setup(self): |
---|
[b2e077a] | 180 | self.root = GutenbachRequestHandler() |
---|
[287d6ec] | 181 | BaseHTTPServer.BaseHTTPRequestHandler.setup(self) |
---|
| 182 | |
---|
| 183 | def handle_one_request(self): |
---|
| 184 | self.raw_requestline = self.rfile.readline() |
---|
| 185 | if not self.raw_requestline: |
---|
| 186 | self.close_connection = 1 |
---|
| 187 | return |
---|
| 188 | if not self.parse_request(): # An error code has been sent, just exit |
---|
| 189 | return |
---|
| 190 | self.handle_ipp() |
---|
| 191 | |
---|
| 192 | def handle_ipp(self): |
---|
| 193 | # Receive a request |
---|
| 194 | length = int(self.headers.getheader('content-length', 0)) |
---|
| 195 | request = ipp.Request(request=self.rfile, length=length) |
---|
[9eeab06] | 196 | logger.debug("Received request: %s" % repr(request)) |
---|
[287d6ec] | 197 | |
---|
| 198 | # Attributes |
---|
| 199 | attributes = [ |
---|
| 200 | ipp.Attribute( |
---|
| 201 | 'attributes-charset', |
---|
| 202 | [ipp.Value(ipp.Tags.CHARSET, 'utf-8')]), |
---|
| 203 | ipp.Attribute( |
---|
| 204 | 'attributes-natural-language', |
---|
| 205 | [ipp.Value(ipp.Tags.NATURAL_LANGUAGE, 'en-us')]) |
---|
| 206 | ] |
---|
| 207 | # Attribute group |
---|
| 208 | attribute_group = ipp.AttributeGroup( |
---|
| 209 | const.AttributeTags.OPERATION, |
---|
| 210 | attributes) |
---|
| 211 | |
---|
| 212 | # Set up the response |
---|
| 213 | response_kwargs = {} |
---|
| 214 | response_kwargs['version'] = request.version |
---|
| 215 | response_kwargs['operation_id'] = const.StatusCodes.INTERNAL_ERROR |
---|
| 216 | response_kwargs['request_id'] = request.request_id |
---|
| 217 | response_kwargs['attribute_groups'] = [attribute_group] |
---|
| 218 | response = ipp.Request(**response_kwargs) |
---|
| 219 | |
---|
| 220 | # Get the handler and pass it the request and response objects |
---|
[b2e077a] | 221 | self.root.handle(request, response) |
---|
[9eeab06] | 222 | logger.debug("Sending response: %s" % repr(response)) |
---|
[287d6ec] | 223 | |
---|
| 224 | # Send the response across HTTP |
---|
| 225 | self.send_response(200, "o hai") |
---|
| 226 | self.send_header("Content-Type", "application/ipp") |
---|
| 227 | self.send_header("Connection", "close") |
---|
| 228 | self.end_headers() |
---|
| 229 | self.wfile.write(response.packed_value) |
---|