[738d179] | 1 | from gutenbach.server.printer import GutenbachPrinter |
---|
[b2e077a] | 2 | import gutenbach.ipp as ipp |
---|
[287d6ec] | 3 | import logging |
---|
[5e44432] | 4 | import traceback |
---|
[aded2d1] | 5 | import sys |
---|
[287d6ec] | 6 | |
---|
[d04a689] | 7 | # initialize logger |
---|
| 8 | logger = logging.getLogger(__name__) |
---|
[287d6ec] | 9 | |
---|
| 10 | def handler_for(operation): |
---|
| 11 | """A decorator method to mark a function with the operation id |
---|
| 12 | that it handles. This value will be stored in |
---|
| 13 | 'func.ipp_operation'. |
---|
| 14 | |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | def f(func): |
---|
| 18 | func.ipp_operation = operation |
---|
| 19 | return func |
---|
| 20 | return f |
---|
| 21 | |
---|
[1037115] | 22 | def make_empty_response(request): |
---|
| 23 | # Operation attributes -- typically the same for any request |
---|
| 24 | attribute_group = ipp.AttributeGroup( |
---|
| 25 | ipp.AttributeTags.OPERATION, |
---|
| 26 | [ipp.AttributesCharset('utf-8'), |
---|
| 27 | ipp.AttributesNaturalLanguage('en-us')]) |
---|
| 28 | |
---|
| 29 | # Set up the default response -- handlers will override these |
---|
| 30 | # values if they need to |
---|
| 31 | response_kwargs = {} |
---|
| 32 | response_kwargs['version'] = request.version |
---|
| 33 | response_kwargs['operation_id'] = ipp.StatusCodes.OK |
---|
| 34 | response_kwargs['request_id'] = request.request_id |
---|
| 35 | response_kwargs['attribute_groups'] = [attribute_group] |
---|
| 36 | response = ipp.Request(**response_kwargs) |
---|
| 37 | |
---|
| 38 | return response |
---|
| 39 | |
---|
[b2e077a] | 40 | class GutenbachRequestHandler(object): |
---|
[287d6ec] | 41 | |
---|
[b2e077a] | 42 | def __init__(self): |
---|
| 43 | self.printers = { |
---|
| 44 | "test": GutenbachPrinter(name="test") |
---|
| 45 | } |
---|
| 46 | self.default = "test" |
---|
[1037115] | 47 | |
---|
| 48 | def generic_handle(self, request): |
---|
| 49 | # check the IPP version number |
---|
| 50 | if request.version != (1, 1): |
---|
| 51 | raise ipp.errors.ServerErrorVersionNotSupported(str(request.version)) |
---|
| 52 | |
---|
| 53 | # make sure the operation attribute group has the correct tag |
---|
| 54 | operation = request.attribute_groups[0] |
---|
| 55 | if operation.tag != ipp.AttributeTags.OPERATION: |
---|
| 56 | raise ipp.errors.ClientErrorBadRequest( |
---|
| 57 | "Attribute group does not have OPERATION tag: 0x%x" % operation.tag) |
---|
| 58 | |
---|
| 59 | # check charset |
---|
| 60 | charset_attr = operation.attributes[0] |
---|
| 61 | expected = ipp.AttributesCharset(charset_attr.values[0].value) |
---|
| 62 | if charset_attr != expected: |
---|
| 63 | raise ipp.errors.ClientErrorBadRequest(str(charset_attr)) |
---|
| 64 | if charset_attr.values[0].value != 'utf-8': |
---|
| 65 | raise ipp.errors.ClientErrorAttributes(str(charset_attr)) |
---|
| 66 | |
---|
| 67 | # check for attributes-natural-language |
---|
| 68 | natlang_attr = operation.attributes[1] |
---|
| 69 | expected = ipp.AttributesNaturalLanguage(natlang_attr.values[0].value) |
---|
| 70 | if natlang_attr != expected: |
---|
| 71 | raise ipp.errors.ClientErrorBadRequest(str(natlang_attr)) |
---|
| 72 | if natlang_attr.values[0].value != 'en-us': |
---|
| 73 | raise ipp.errors.ClientErrorAttributes(str(natlang_attr)) |
---|
[287d6ec] | 74 | |
---|
[ef8df33] | 75 | def handle(self, request): |
---|
[b2e077a] | 76 | # look up the handler |
---|
| 77 | handler = None |
---|
[9eeab06] | 78 | handler_name = None |
---|
[287d6ec] | 79 | for d in dir(self): |
---|
| 80 | if getattr(getattr(self, d), "ipp_operation", None) == request.operation_id: |
---|
[9eeab06] | 81 | handler_name = d |
---|
[b2e077a] | 82 | break |
---|
[1037115] | 83 | |
---|
[b2e077a] | 84 | # we couldn't find a handler, so default to unknown operation |
---|
[9eeab06] | 85 | if handler_name is None: |
---|
| 86 | handler_name = "unknown_operation" |
---|
[1037115] | 87 | |
---|
| 88 | # actually get the handler |
---|
[9eeab06] | 89 | handler = getattr(self, handler_name) |
---|
[7bd1035] | 90 | logger.info("Handling request of type '%s'" % handler_name) |
---|
[aef164a] | 91 | |
---|
[1037115] | 92 | # try to handle the request |
---|
[aef164a] | 93 | try: |
---|
[1037115] | 94 | self.generic_handle(request) |
---|
| 95 | response = make_empty_response(request) |
---|
| 96 | handler(request, response) |
---|
[aef164a] | 97 | |
---|
| 98 | # Handle any errors that occur. If an exception occurs that |
---|
| 99 | # is an IPP error, then we can get the error code from the |
---|
| 100 | # exception itself. |
---|
| 101 | except ipp.errors.IPPException: |
---|
| 102 | exctype, excval, exctb = sys.exc_info() |
---|
[b828a96] | 103 | logger.error("%s: %s" % (exctype.__name__, excval.message)) |
---|
[1037115] | 104 | response = make_empty_response(request) |
---|
[aef164a] | 105 | excval.update_response(response) |
---|
| 106 | |
---|
| 107 | # If it wasn't an IPP error, then it's our fault, so mark it |
---|
| 108 | # as an internal server error |
---|
| 109 | except Exception: |
---|
| 110 | logger.error(traceback.format_exc()) |
---|
[1037115] | 111 | response = make_empty_response(request) |
---|
[aef164a] | 112 | response.operation_id = ipp.StatusCodes.INTERNAL_ERROR |
---|
| 113 | |
---|
[ef8df33] | 114 | return response |
---|
[287d6ec] | 115 | |
---|
[1037115] | 116 | def unknown_operation(self, request, response): |
---|
[9eeab06] | 117 | logger.warning("Received unknown operation 0x%x" % request.operation_id) |
---|
[1037115] | 118 | response = make_empty_response(request) |
---|
[793432f] | 119 | response.operation_id = ipp.StatusCodes.OPERATION_NOT_SUPPORTED |
---|
[ef8df33] | 120 | return response |
---|
[0ede474] | 121 | |
---|
[9eeab06] | 122 | ##### Printer Commands |
---|
| 123 | |
---|
[793432f] | 124 | @handler_for(ipp.OperationCodes.PRINT_JOB) |
---|
[1037115] | 125 | def print_job(self, request, response): |
---|
[7a7a09e] | 126 | """RFC 2911: 3.2.1 Print-Job Operation |
---|
| 127 | |
---|
| 128 | This REQUIRED operation allows a client to submit a print job |
---|
| 129 | with only one document and supply the document data (rather |
---|
| 130 | than just a reference to the data). See Section 15 for the |
---|
| 131 | suggested steps for processing create operations and their |
---|
| 132 | Operation and Job Template attributes. |
---|
| 133 | |
---|
[1037115] | 134 | Request |
---|
| 135 | ------- |
---|
| 136 | Group 1: Operation Attributes |
---|
| 137 | REQUIRED 'attributes-charset' |
---|
| 138 | REQUIRED 'attributes-natural-language' |
---|
| 139 | REQUIRED 'printer-uri' (uri) |
---|
| 140 | OPTIONAL 'requesting-user-name' (name(MAX)) |
---|
| 141 | OPTIONAL 'job-name' (name(MAX)) |
---|
| 142 | OPTIONAL 'ipp-attribute-fidelity' (boolean) |
---|
| 143 | OPTIONAL 'document-name' (name(MAX)) |
---|
| 144 | OPTIONAL 'compression' (type3 keyword) |
---|
| 145 | OPTIONAL 'document-format' (mimeMediaType) |
---|
| 146 | OPTIONAL 'document-natural-language' (naturalLanguage) |
---|
| 147 | OPTIONAL 'job-k-octets' (integer(0:MAX)) |
---|
| 148 | OPTIONAL 'job-impressions' (integer(0:MAX)) |
---|
| 149 | OPTIONAL 'job-media-sheets' (integer(0:MAX)) |
---|
| 150 | Group 2: Job Template Attributes |
---|
| 151 | Group 3: Document Content |
---|
| 152 | |
---|
| 153 | Response |
---|
| 154 | -------- |
---|
| 155 | Group 1: Operation Attributes |
---|
| 156 | OPTIONAL 'status-message' (text(255)) |
---|
| 157 | OPTIONAL 'detailed-status-message' (text(MAX)) |
---|
| 158 | REQUIRED 'attributes-charset' |
---|
| 159 | REQUIRED 'attributes-natural-language' |
---|
| 160 | Group 2: Unsupported Attributes |
---|
| 161 | Group 3: Job Object Attributes |
---|
| 162 | REQUIRED 'job-uri' (uri) |
---|
| 163 | REQUIRED 'job-id' (integer(1:MAX)) |
---|
| 164 | REQUIRED 'job-state' (type1 enum) |
---|
| 165 | REQUIRED 'job-state-reasons' (1setOf type2 keyword) |
---|
| 166 | OPTIONAL 'job-state-message' (text(MAX)) |
---|
| 167 | OPTIONAL 'number-of-intervening-jobs' (integer(0:MAX)) |
---|
| 168 | |
---|
[7a7a09e] | 169 | """ |
---|
| 170 | |
---|
[b828a96] | 171 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 172 | |
---|
[793432f] | 173 | @handler_for(ipp.OperationCodes.VALIDATE_JOB) |
---|
[1037115] | 174 | def validate_job(self, request, response): |
---|
[94a4825] | 175 | |
---|
[b828a96] | 176 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 177 | |
---|
[793432f] | 178 | @handler_for(ipp.OperationCodes.GET_JOBS) |
---|
[1037115] | 179 | def get_jobs(self, request, response): |
---|
| 180 | """3.2.6 Get-Jobs Operation |
---|
[6ed9d7a] | 181 | |
---|
| 182 | This REQUIRED operation allows a client to retrieve the list |
---|
| 183 | of Job objects belonging to the target Printer object. The |
---|
| 184 | client may also supply a list of Job attribute names and/or |
---|
| 185 | attribute group names. A group of Job object attributes will |
---|
| 186 | be returned for each Job object that is returned. |
---|
| 187 | |
---|
| 188 | This operation is similar to the Get-Job-Attributes operation, |
---|
| 189 | except that this Get-Jobs operation returns attributes from |
---|
| 190 | possibly more than one object. |
---|
| 191 | |
---|
[1037115] | 192 | Request |
---|
| 193 | ------- |
---|
| 194 | Group 1: Operation Attributes |
---|
| 195 | REQUIRED 'attributes-charset' |
---|
| 196 | REQUIRED 'attributes-natural-language' |
---|
| 197 | REQUIRED 'printer-uri' (uri) |
---|
| 198 | OPTIONAL 'requesting-user-name' (name(MAX)) |
---|
| 199 | OPTIONAL 'limit' (integer(1:MAX)) |
---|
| 200 | OPTIONAL 'requested-attributes' (1setOf type2 keyword) |
---|
| 201 | OPTIONAL 'which-jobs' (type2 keyword) |
---|
| 202 | OPTIONAL 'my-jobs' (boolean) |
---|
| 203 | |
---|
| 204 | Response |
---|
| 205 | -------- |
---|
| 206 | Group 1: Operation Attributes |
---|
| 207 | OPTIONAL 'status-message' (text(255)) |
---|
| 208 | OPTIONAL 'detailed-status-message' (text(MAX)) |
---|
| 209 | REQUIRED 'attributes-charset' |
---|
| 210 | REQUIRED 'attributes-natural-language' |
---|
| 211 | Group 2: Unsupported Attributes |
---|
| 212 | Groups 3 to N: Job Object Attributes |
---|
| 213 | |
---|
[6ed9d7a] | 214 | """ |
---|
[59a1d4a] | 215 | |
---|
[1037115] | 216 | operation = request.attribute_groups[0] |
---|
[59a1d4a] | 217 | |
---|
[1037115] | 218 | # requested printer uri |
---|
| 219 | if 'printer-uri' not in operation: |
---|
| 220 | raise ipp.errors.ClientErrorBadRequest("Missing 'printer-uri' attribute") |
---|
| 221 | uri_attr = operation['printer-uri'] |
---|
| 222 | printer_name = uri_attr.values[0].value.split("/")[-1] |
---|
| 223 | if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): |
---|
| 224 | raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) |
---|
[5fe360e] | 225 | if printer_name not in self.printers: |
---|
[1037115] | 226 | raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) |
---|
| 227 | jobs = self.printers[printer_name].get_jobs() |
---|
| 228 | |
---|
| 229 | # requesting username |
---|
| 230 | if 'requesting-user-name' in operation: |
---|
| 231 | username_attr = operation['requesting-user-name'] |
---|
| 232 | username = username_attr.values[0].value |
---|
| 233 | if username_attr != ipp.RequestingUserName(username): |
---|
| 234 | raise ipp.errors.ClientErrorBadRequest(str(username_attr)) |
---|
| 235 | |
---|
| 236 | # get the job attributes and add them to the response |
---|
| 237 | for job in self.printers[printer_name].get_jobs(): |
---|
| 238 | attrs = job.get_job_attributes(request) |
---|
| 239 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 240 | ipp.AttributeTags.JOB, attrs)) |
---|
[287d6ec] | 241 | |
---|
[793432f] | 242 | @handler_for(ipp.OperationCodes.PRINT_URI) |
---|
[1037115] | 243 | def print_uri(self, request, response): |
---|
[b828a96] | 244 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 245 | |
---|
[793432f] | 246 | @handler_for(ipp.OperationCodes.CREATE_JOB) |
---|
[1037115] | 247 | def create_job(self, request, response): |
---|
[94a4825] | 248 | """RFC 2911: 3.2.4 Create-Job Operation |
---|
| 249 | |
---|
| 250 | This OPTIONAL operation is similar to the Print-Job operation |
---|
| 251 | (section 3.2.1) except that in the Create-Job request, a |
---|
| 252 | client does not supply document data or any reference to |
---|
| 253 | document data. Also, the client does not supply any of the |
---|
| 254 | 'document-name', 'document- format', 'compression', or |
---|
| 255 | 'document-natural-language' operation attributes. This |
---|
| 256 | operation is followed by one or more Send-Document or Send-URI |
---|
| 257 | operations. In each of those operation requests, the client |
---|
| 258 | OPTIONALLY supplies the 'document-name', 'document-format', |
---|
| 259 | and 'document-natural-language' attributes for each document |
---|
| 260 | in the multi-document Job object. |
---|
| 261 | |
---|
[1037115] | 262 | Group 1: Operation Attributes |
---|
[94a4825] | 263 | |
---|
| 264 | """ |
---|
[9eeab06] | 265 | |
---|
[b828a96] | 266 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[94a4825] | 267 | |
---|
[793432f] | 268 | @handler_for(ipp.OperationCodes.PAUSE_PRINTER) |
---|
[1037115] | 269 | def pause_printer(self, request, response): |
---|
[b828a96] | 270 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 271 | |
---|
[793432f] | 272 | @handler_for(ipp.OperationCodes.RESUME_PRINTER) |
---|
[1037115] | 273 | def resume_printer(self, request, response): |
---|
[b828a96] | 274 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 275 | |
---|
[793432f] | 276 | @handler_for(ipp.OperationCodes.GET_PRINTER_ATTRIBUTES) |
---|
[1037115] | 277 | def get_printer_attributes(self, request, response): |
---|
[cad7502] | 278 | """RFC 2911: 3.2.5 Get-Printer-Attributes Operation |
---|
| 279 | |
---|
| 280 | This REQUIRED operation allows a client to request the values |
---|
| 281 | of the attributes of a Printer object. |
---|
| 282 | |
---|
| 283 | In the request, the client supplies the set of Printer |
---|
| 284 | attribute names and/or attribute group names in which the |
---|
| 285 | requester is interested. In the response, the Printer object |
---|
| 286 | returns a corresponding attribute set with the appropriate |
---|
| 287 | attribute values filled in. |
---|
| 288 | |
---|
[1037115] | 289 | Request |
---|
| 290 | ------- |
---|
[cad7502] | 291 | |
---|
[1037115] | 292 | Group 1: Operation Attributes |
---|
| 293 | REQUIRED 'attributes-charset' |
---|
| 294 | REQUIRED 'attributes-natural-language' |
---|
| 295 | REQUIRED 'printer-uri' (uri) |
---|
| 296 | OPTIONAL 'requesting-user-name' (name(MAX)) |
---|
| 297 | OPTIONAL 'requested-attributes' (1setOf type2 keyword) |
---|
| 298 | OPTIONAL 'document-format' (mimeMediaType): |
---|
[cad7502] | 299 | |
---|
[1037115] | 300 | Response |
---|
| 301 | -------- |
---|
| 302 | |
---|
| 303 | Group 1: Operation Attributes |
---|
| 304 | OPTIONAL 'status-message' (text(255)) |
---|
| 305 | OPTIONAL 'detailed-status-message' (text(MAX)) |
---|
| 306 | REQUIRED 'attributes-charset' |
---|
| 307 | REQUIRED 'attributes-natural-language' |
---|
| 308 | Group 2: Unsupported Attributes |
---|
| 309 | Group 3: Printer Object Attributes |
---|
[cad7502] | 310 | |
---|
| 311 | """ |
---|
| 312 | |
---|
[1037115] | 313 | operation = request.attribute_groups[0] |
---|
[59a1d4a] | 314 | |
---|
[1037115] | 315 | # requested printer uri |
---|
| 316 | if 'printer-uri' not in operation: |
---|
| 317 | raise ipp.errors.ClientErrorBadRequest("Missing 'printer-uri' attribute") |
---|
| 318 | uri_attr = operation['printer-uri'] |
---|
| 319 | printer_name = uri_attr.values[0].value.split("/")[-1] |
---|
| 320 | if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): |
---|
| 321 | raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) |
---|
[5fe360e] | 322 | if printer_name not in self.printers: |
---|
[1037115] | 323 | raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) |
---|
| 324 | printer = self.printers[printer_name] |
---|
[59a1d4a] | 325 | |
---|
[1037115] | 326 | # requesting username |
---|
| 327 | if 'requesting-user-name' in operation: |
---|
| 328 | username_attr = operation['requesting-user-name'] |
---|
| 329 | username = username_attr.values[0].value |
---|
| 330 | if username_attr != ipp.RequestingUserName(username): |
---|
| 331 | raise ipp.errors.ClientErrorBadRequest(str(username_attr)) |
---|
| 332 | |
---|
| 333 | # get attributes from the printer and add to response |
---|
| 334 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 335 | ipp.AttributeTags.PRINTER, printer.get_printer_attributes(request))) |
---|
[9eeab06] | 336 | |
---|
[793432f] | 337 | @handler_for(ipp.OperationCodes.SET_PRINTER_ATTRIBUTES) |
---|
[1037115] | 338 | def set_printer_attributes(self, request, response): |
---|
[b828a96] | 339 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 340 | |
---|
| 341 | ##### Job Commands |
---|
| 342 | |
---|
[793432f] | 343 | @handler_for(ipp.OperationCodes.CANCEL_JOB) |
---|
[1037115] | 344 | def cancel_job(self, request, response): |
---|
[b828a96] | 345 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 346 | |
---|
[793432f] | 347 | @handler_for(ipp.OperationCodes.SEND_DOCUMENT) |
---|
[1037115] | 348 | def send_document(self, request, response): |
---|
[b828a96] | 349 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 350 | |
---|
[793432f] | 351 | @handler_for(ipp.OperationCodes.SEND_URI) |
---|
[1037115] | 352 | def send_uri(self, request, response): |
---|
[b828a96] | 353 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 354 | |
---|
[793432f] | 355 | @handler_for(ipp.OperationCodes.GET_JOB_ATTRIBUTES) |
---|
[1037115] | 356 | def get_job_attributes(self, request, response): |
---|
[59a1d4a] | 357 | |
---|
[1037115] | 358 | operation = request.attribute_groups[0] |
---|
| 359 | |
---|
| 360 | # requested printer uri |
---|
| 361 | if 'printer-uri' not in operation: |
---|
| 362 | raise ipp.errors.ClientErrorBadRequest("Missing 'printer-uri' attribute") |
---|
| 363 | uri_attr = operation['printer-uri'] |
---|
| 364 | printer_name = uri_attr.values[0].value.split("/")[-1] |
---|
| 365 | if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): |
---|
| 366 | raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) |
---|
[5fe360e] | 367 | if printer_name not in self.printers: |
---|
[1037115] | 368 | raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) |
---|
| 369 | printer = self.printers[printer_name] |
---|
| 370 | |
---|
| 371 | if 'job-id' not in operation: |
---|
| 372 | raise ipp.errors.ClientErrorBadRequest("Missing 'job-id' attribute") |
---|
| 373 | job_id_attr = operation['job-id'] |
---|
| 374 | job_id = job_id_attr.values[0].value |
---|
| 375 | if job_id_attr != ipp.JobId(job_id_attr.values[0].value): |
---|
| 376 | raise ipp.errors.ClientErrorBadRequest(str(job_id_attr)) |
---|
| 377 | if job_id not in printer.jobs: |
---|
| 378 | raise ipp.errors.ClientErrorAttributes(str(job_id_attr)) |
---|
| 379 | job = printer.get_job(job_id) |
---|
| 380 | |
---|
| 381 | # requesting username |
---|
| 382 | if 'requesting-user-name' in operation: |
---|
| 383 | username_attr = operation['requesting-user-name'] |
---|
| 384 | username = username_attr.values[0].value |
---|
| 385 | if username_attr != ipp.RequestingUserName(username): |
---|
| 386 | raise ipp.errors.ClientErrorBadRequest(str(username_attr)) |
---|
| 387 | |
---|
| 388 | # get the job attributes and add them to the response |
---|
| 389 | attrs = job.get_job_attributes(request) |
---|
| 390 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 391 | ipp.AttributeTags.JOB, attrs)) |
---|
[9eeab06] | 392 | |
---|
[793432f] | 393 | @handler_for(ipp.OperationCodes.SET_JOB_ATTRIBUTES) |
---|
[1037115] | 394 | def set_job_attributes(self, request, response): |
---|
[b828a96] | 395 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 396 | |
---|
[793432f] | 397 | @handler_for(ipp.OperationCodes.RESTART_JOB) |
---|
[1037115] | 398 | def restart_job(self, request, response): |
---|
[b828a96] | 399 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 400 | |
---|
[793432f] | 401 | @handler_for(ipp.OperationCodes.PROMOTE_JOB) |
---|
[1037115] | 402 | def promote_job(self, request, response): |
---|
[b828a96] | 403 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[9eeab06] | 404 | |
---|
| 405 | ##### CUPS Specific Commands |
---|
| 406 | |
---|
[793432f] | 407 | @handler_for(ipp.OperationCodes.CUPS_GET_DOCUMENT) |
---|
[1037115] | 408 | def cups_get_document(self, request, response): |
---|
[b828a96] | 409 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
[e70c020] | 410 | |
---|
[793432f] | 411 | @handler_for(ipp.OperationCodes.CUPS_GET_DEFAULT) |
---|
[1037115] | 412 | def cups_get_default(self, request, response): |
---|
[0ede474] | 413 | """The CUPS-Get-Default operation (0x4001) returns the default |
---|
| 414 | printer URI and attributes. |
---|
| 415 | |
---|
[1037115] | 416 | Request |
---|
| 417 | ------- |
---|
| 418 | |
---|
| 419 | Group 1: Operation Attributes |
---|
| 420 | REQUIRED 'attributes-charset' |
---|
| 421 | REQUIRED 'attributes-natural-language' |
---|
| 422 | OPTIONAL 'requested-attributes' (1setOf type2 keyword) |
---|
| 423 | |
---|
| 424 | Response |
---|
| 425 | -------- |
---|
| 426 | |
---|
| 427 | Group 1: Operation Attributes |
---|
| 428 | OPTIONAL 'status-message' (text(255)) |
---|
| 429 | REQUIRED 'attributes-charset' |
---|
| 430 | REQUIRED 'attributes-natural-language' |
---|
| 431 | Group 2: Printer Object Attributes |
---|
| 432 | |
---|
[0ede474] | 433 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_DEFAULT ) |
---|
| 434 | |
---|
| 435 | """ |
---|
[287d6ec] | 436 | |
---|
[1037115] | 437 | operation = request.attribute_groups[0] |
---|
| 438 | printer = self.printers[self.default] |
---|
| 439 | |
---|
| 440 | # requesting username |
---|
| 441 | if 'requesting-user-name' in operation: |
---|
| 442 | username_attr = operation['requesting-user-name'] |
---|
| 443 | username = username_attr.values[0].value |
---|
| 444 | if username_attr != ipp.RequestingUserName(username): |
---|
| 445 | raise ipp.errors.ClientErrorBadRequest(str(username_attr)) |
---|
| 446 | |
---|
| 447 | # get attributes from the printer and add to response |
---|
| 448 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 449 | ipp.AttributeTags.PRINTER, printer.get_printer_attributes(request))) |
---|
[0ede474] | 450 | |
---|
[793432f] | 451 | @handler_for(ipp.OperationCodes.CUPS_GET_PRINTERS) |
---|
[1037115] | 452 | def cups_get_printers(self, request, response): |
---|
[ef8df33] | 453 | """The CUPS-Get-Printers operation (0x4002) returns the |
---|
| 454 | printer attributes for every printer known to the system. This |
---|
| 455 | may include printers that are not served directly by the |
---|
| 456 | server. |
---|
[0ede474] | 457 | |
---|
| 458 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_PRINTERS ) |
---|
| 459 | |
---|
| 460 | """ |
---|
| 461 | |
---|
[1037115] | 462 | operation = request.attribute_groups[0] |
---|
| 463 | |
---|
| 464 | # requesting username |
---|
| 465 | if 'requesting-user-name' in operation: |
---|
| 466 | username_attr = operation['requesting-user-name'] |
---|
| 467 | username = username_attr.values[0].value |
---|
| 468 | if username_attr != ipp.RequestingUserName(username): |
---|
| 469 | raise ipp.errors.ClientErrorBadRequest(str(username_attr)) |
---|
| 470 | |
---|
| 471 | # get attributes from the printer and add to response |
---|
| 472 | for printer in self.printers.values(): |
---|
| 473 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
| 474 | ipp.AttributeTags.PRINTER, printer.get_printer_attributes(request))) |
---|
[287d6ec] | 475 | |
---|
[793432f] | 476 | @handler_for(ipp.OperationCodes.CUPS_GET_CLASSES) |
---|
[1037115] | 477 | def cups_get_classes(self, request, response): |
---|
[0ede474] | 478 | """The CUPS-Get-Classes operation (0x4005) returns the printer |
---|
| 479 | attributes for every printer class known to the system. This |
---|
| 480 | may include printer classes that are not served directly by |
---|
| 481 | the server. |
---|
[1037115] | 482 | |
---|
| 483 | Request |
---|
| 484 | ------- |
---|
| 485 | |
---|
| 486 | Group 1: Operation Attributes |
---|
| 487 | REQUIRED 'attributes-charset' |
---|
| 488 | REQUIRED 'attributes-natural-language' |
---|
| 489 | OPTIONAL 'first-printer-name' (name(127)) CUPS 1.2/Mac OS X 10.5 |
---|
| 490 | OPTIONAL 'limit' (integer (1:MAX)) |
---|
| 491 | OPTIONAL 'printer-location' (text(127)) CUPS 1.1.7 |
---|
| 492 | OPTIONAL 'printer-type' (type2 enum) CUPS 1.1.7 |
---|
| 493 | OPTIONAL 'printer-type-mask' (type2 enum) CUPS 1.1.7 |
---|
| 494 | OPTIONAL 'requested-attributes' (1setOf keyword) |
---|
| 495 | OPTOINAL 'requested-user-name' (name(127)) CUPS 1.2/Mac OS X 10.5 |
---|
| 496 | OPTIONAL 'requested-attributes' (1setOf type2 keyword) |
---|
| 497 | |
---|
| 498 | Response |
---|
| 499 | -------- |
---|
| 500 | |
---|
| 501 | Group 1: Operation Attributes |
---|
| 502 | OPTIONAL 'status-message' (text(255)) |
---|
| 503 | REQUIRED 'attributes-charset' |
---|
| 504 | REQUIRED 'attributes-natural-language' |
---|
| 505 | Group 2: Printer Class Object Attributes |
---|
| 506 | |
---|
[0ede474] | 507 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_CLASSES ) |
---|
| 508 | |
---|
| 509 | """ |
---|
[ef8df33] | 510 | |
---|
[1037115] | 511 | raise ipp.errors.ServerErrorOperationNotSupported |
---|
| 512 | |
---|