1 | from gutenbach.server.exceptions import MalformedIPPRequestException |
---|
2 | from gutenbach.server.printer import GutenbachPrinter |
---|
3 | import gutenbach.ipp as ipp |
---|
4 | import gutenbach.ipp.constants as const |
---|
5 | import logging |
---|
6 | |
---|
7 | # initialize logger |
---|
8 | logger = logging.getLogger(__name__) |
---|
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 | |
---|
22 | class GutenbachRequestHandler(object): |
---|
23 | |
---|
24 | def __init__(self): |
---|
25 | self.printers = { |
---|
26 | "test": GutenbachPrinter(name="test") |
---|
27 | } |
---|
28 | self.default = "test" |
---|
29 | |
---|
30 | def handle(self, request, response): |
---|
31 | # look up the handler |
---|
32 | handler = None |
---|
33 | handler_name = None |
---|
34 | for d in dir(self): |
---|
35 | if getattr(getattr(self, d), "ipp_operation", None) == request.operation_id: |
---|
36 | handler_name = d |
---|
37 | break |
---|
38 | # we couldn't find a handler, so default to unknown operation |
---|
39 | if handler_name is None: |
---|
40 | handler_name = "unknown_operation" |
---|
41 | # call the handler |
---|
42 | handler = getattr(self, handler_name) |
---|
43 | logger.info("Handling request of type '%s'" % handler_name) |
---|
44 | handler(request, response) |
---|
45 | |
---|
46 | def unknown_operation(self, request, response): |
---|
47 | logger.warning("Received unknown operation 0x%x" % request.operation_id) |
---|
48 | response.operation_id = const.StatusCodes.OPERATION_NOT_SUPPORTED |
---|
49 | |
---|
50 | ##### Helper functions |
---|
51 | |
---|
52 | def _get_printer_attributes(self, printer, request, response): |
---|
53 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
54 | const.AttributeTags.PRINTER, |
---|
55 | printer.get_printer_attributes(request))) |
---|
56 | |
---|
57 | def _get_job_attributes(self, job_id, printer, request, response): |
---|
58 | response.attribute_groups.append(ipp.AttributeGroup( |
---|
59 | const.AttributeTags.JOB, |
---|
60 | job.get_job_attributes(request))) |
---|
61 | |
---|
62 | def _get_printer_name(self, request): |
---|
63 | # make sure the first group is an OPERATION group |
---|
64 | group_tag = request.attribute_groups[0].tag |
---|
65 | if group_tag != const.AttributeTags.OPERATION: |
---|
66 | raise MalformedIPPRequestException, \ |
---|
67 | "Expected OPERATION group tag, got %d\n", group_tag |
---|
68 | |
---|
69 | # make sure the printer-uri value is appropriate |
---|
70 | printer_name_attr = request.attribute_groups[0]['printer-uri'] |
---|
71 | printer_name_value_tag = printer_name_attr.values[0].value_tag |
---|
72 | if printer_name_value_tag != const.CharacterStringTags.URI: |
---|
73 | raise MalformedIPPRequestException, \ |
---|
74 | "Expected URI value tag, got %s" % printer_name_value_tag |
---|
75 | |
---|
76 | # actually get the printer name |
---|
77 | printer_name_value = printer_name_attr.values[0].value |
---|
78 | # XXX: hack -- CUPS will strip the port from the request, so |
---|
79 | # we can't do an exact comparison (also the hostname might be |
---|
80 | # different, depending on the CNAME or whether it's localhost) |
---|
81 | printer_name = printer_name_value.split("/")[-1] |
---|
82 | |
---|
83 | # make sure the printer name is valid |
---|
84 | if printer_name not in self.printers: |
---|
85 | raise ValueError, "Invalid printer uri: %s" % printer_name_value |
---|
86 | |
---|
87 | return printer_name |
---|
88 | |
---|
89 | def _get_job_id(self, request): |
---|
90 | pass |
---|
91 | |
---|
92 | ##### Printer Commands |
---|
93 | |
---|
94 | def print_job(self, request, response): |
---|
95 | pass |
---|
96 | |
---|
97 | def validate_job(self, request, response): |
---|
98 | pass |
---|
99 | |
---|
100 | @handler_for(const.Operations.GET_JOBS) |
---|
101 | def get_jobs(self, request, response): |
---|
102 | printer_name = self._get_printer_name(request) |
---|
103 | # Each job will append a new job attribute group. |
---|
104 | for job in self.printers[printer_name].get_jobs(): |
---|
105 | self._get_job_attributes(job, request, response) |
---|
106 | |
---|
107 | def print_uri(self, request, response): |
---|
108 | pass |
---|
109 | |
---|
110 | def create_job(self, request, response): |
---|
111 | pass |
---|
112 | |
---|
113 | def pause_printer(self, request, response): |
---|
114 | pass |
---|
115 | |
---|
116 | def resume_printer(self, request, response): |
---|
117 | pass |
---|
118 | |
---|
119 | @handler_for(const.Operations.GET_PRINTER_ATTRIBUTES) |
---|
120 | def get_printer_attributes(self, request, response): |
---|
121 | # this is just like cups_get_default, except the printer name |
---|
122 | # is given |
---|
123 | printer_name = self._get_printer_name(request) |
---|
124 | self._get_printer_attributes(self.printers[printer_name], request, response) |
---|
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 | """The CUPS-Get-Default operation (0x4001) returns the default |
---|
164 | printer URI and attributes. |
---|
165 | |
---|
166 | CUPS-Get-Default Request |
---|
167 | ------------------------ |
---|
168 | |
---|
169 | The following groups of attributes are supplied as part of the |
---|
170 | CUPS-Get-Default request: |
---|
171 | |
---|
172 | Group 1: Operation Attributes |
---|
173 | Natural Language and Character Set: |
---|
174 | The 'attributes-charset' and |
---|
175 | 'attributes-natural-language' attributes as described |
---|
176 | in section 3.1.4.1 of the IPP Model and Semantics |
---|
177 | document. |
---|
178 | 'requested-attributes' (1setOf keyword): |
---|
179 | The client OPTIONALLY supplies a set of attribute |
---|
180 | names and/or attribute group names in whose values the |
---|
181 | requester is interested. If the client omits this |
---|
182 | attribute, the server responds as if this attribute |
---|
183 | had been supplied with a value of 'all'. |
---|
184 | |
---|
185 | CUPS-Get-Default Response |
---|
186 | ------------------------- |
---|
187 | |
---|
188 | The following groups of attributes are send as part of the |
---|
189 | CUPS-Get-Default Response: |
---|
190 | |
---|
191 | Group 1: Operation Attributes |
---|
192 | Status Message: |
---|
193 | The standard response status message. |
---|
194 | Natural Language and Character Set: |
---|
195 | The 'attributes-charset' and |
---|
196 | 'attributes-natural-language' attributes as described |
---|
197 | in section 3.1.4.2 of the IPP Model and Semantics |
---|
198 | document. |
---|
199 | |
---|
200 | Group 2: Printer Object Attributes |
---|
201 | The set of requested attributes and their current values. |
---|
202 | |
---|
203 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_DEFAULT ) |
---|
204 | |
---|
205 | """ |
---|
206 | |
---|
207 | self._get_printer_attributes(self.printers[self.default], request, response) |
---|
208 | |
---|
209 | @handler_for(const.Operations.CUPS_GET_PRINTERS) |
---|
210 | def cups_get_printers(self, request, response): |
---|
211 | """ |
---|
212 | The CUPS-Get-Printers operation (0x4002) returns the printer |
---|
213 | attributes for every printer known to the system. This may |
---|
214 | include printers that are not served directly by the server. |
---|
215 | |
---|
216 | CUPS-Get-Printers Request |
---|
217 | ------------------------- |
---|
218 | |
---|
219 | The following groups of attributes are supplied as part of the |
---|
220 | CUPS-Get-Printers request: |
---|
221 | |
---|
222 | Group 1: Operation Attributes |
---|
223 | Natural Language and Character Set: |
---|
224 | The 'attributes-charset' and |
---|
225 | 'attributes-natural-language' attributes as described |
---|
226 | in section 3.1.4.1 of the IPP Model and Semantics |
---|
227 | document. |
---|
228 | 'first-printer-name' (name(127)):CUPS 1.2/Mac OS X 10.5 |
---|
229 | The client OPTIONALLY supplies this attribute to |
---|
230 | select the first printer that is returned. |
---|
231 | 'limit' (integer (1:MAX)): |
---|
232 | The client OPTIONALLY supplies this attribute limiting |
---|
233 | the number of printers that are returned. |
---|
234 | 'printer-location' (text(127)): CUPS 1.1.7 |
---|
235 | The client OPTIONALLY supplies this attribute to |
---|
236 | select which printers are returned. |
---|
237 | 'printer-type' (type2 enum): CUPS 1.1.7 |
---|
238 | The client OPTIONALLY supplies a printer type |
---|
239 | enumeration to select which printers are returned. |
---|
240 | 'printer-type-mask' (type2 enum): CUPS 1.1.7 |
---|
241 | The client OPTIONALLY supplies a printer type mask |
---|
242 | enumeration to select which bits are used in the |
---|
243 | 'printer-type' attribute. |
---|
244 | 'requested-attributes' (1setOf keyword) : |
---|
245 | The client OPTIONALLY supplies a set of attribute |
---|
246 | names and/or attribute group names in whose values the |
---|
247 | requester is interested. If the client omits this |
---|
248 | attribute, the server responds as if this attribute |
---|
249 | had been supplied with a value of 'all'. |
---|
250 | 'requested-user-name' (name(127)) : CUPS 1.2/Mac OS X 10.5 |
---|
251 | The client OPTIONALLY supplies a user name that is |
---|
252 | used to filter the returned printers. |
---|
253 | |
---|
254 | CUPS-Get-Printers Response |
---|
255 | -------------------------- |
---|
256 | |
---|
257 | The following groups of attributes are send as part of the |
---|
258 | CUPS-Get-Printers Response: |
---|
259 | |
---|
260 | Group 1: Operation Attributes |
---|
261 | Status Message: |
---|
262 | The standard response status message. |
---|
263 | Natural Language and Character Set: |
---|
264 | The 'attributes-charset' and |
---|
265 | 'attributes-natural-language' attributes as described |
---|
266 | in section 3.1.4.2 of the IPP Model and Semantics |
---|
267 | document. |
---|
268 | |
---|
269 | Group 2: Printer Object Attributes |
---|
270 | The set of requested attributes and their current values |
---|
271 | for each printer. |
---|
272 | |
---|
273 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_PRINTERS ) |
---|
274 | |
---|
275 | """ |
---|
276 | |
---|
277 | # Each printer will append a new printer attribute group. |
---|
278 | for printer in self.printers: |
---|
279 | self._get_printer_attributes(self.printers[printer], request, response) |
---|
280 | |
---|
281 | @handler_for(const.Operations.CUPS_GET_CLASSES) |
---|
282 | def cups_get_classes(self, request, response): |
---|
283 | """The CUPS-Get-Classes operation (0x4005) returns the printer |
---|
284 | attributes for every printer class known to the system. This |
---|
285 | may include printer classes that are not served directly by |
---|
286 | the server. |
---|
287 | |
---|
288 | CUPS-Get-Classes Request |
---|
289 | ------------------------ |
---|
290 | |
---|
291 | The following groups of attributes are supplied as part of the |
---|
292 | CUPS-Get-Classes request: |
---|
293 | |
---|
294 | Group 1: Operation Attributes |
---|
295 | Natural Language and Character Set: |
---|
296 | The 'attributes-charset' and |
---|
297 | 'attributes-natural-language' attributes as described |
---|
298 | in section 3.1.4.1 of the IPP Model and Semantics |
---|
299 | document. |
---|
300 | 'first-printer-name' (name(127)):CUPS 1.2/Mac OS X 10.5 |
---|
301 | The client OPTIONALLY supplies this attribute to |
---|
302 | select the first printer that is returned. |
---|
303 | 'limit' (integer (1:MAX)): |
---|
304 | The client OPTIONALLY supplies this attribute limiting |
---|
305 | the number of printer classes that are returned. |
---|
306 | 'printer-location' (text(127)): CUPS 1.1.7 |
---|
307 | The client OPTIONALLY supplies this attribute to |
---|
308 | select which printer classes are returned. |
---|
309 | 'printer-type' (type2 enum): CUPS 1.1.7 |
---|
310 | The client OPTIONALLY supplies a printer type |
---|
311 | enumeration to select which printer classes are |
---|
312 | returned. |
---|
313 | 'printer-type-mask' (type2 enum): CUPS 1.1.7 |
---|
314 | The client OPTIONALLY supplies a printer type mask |
---|
315 | enumeration to select which bits are used in the |
---|
316 | 'printer-type' attribute. |
---|
317 | 'requested-attributes' (1setOf keyword) : |
---|
318 | The client OPTIONALLY supplies a set of attribute |
---|
319 | names and/or attribute group names in whose values the |
---|
320 | requester is interested. If the client omits this |
---|
321 | attribute, the server responds as if this attribute |
---|
322 | had been supplied with a value of 'all'. |
---|
323 | 'requested-user-name' (name(127)) : CUPS 1.2/Mac OS X 10.5 |
---|
324 | The client OPTIONALLY supplies a user name that is |
---|
325 | used to filter the returned printers. |
---|
326 | |
---|
327 | CUPS-Get-Classes Response |
---|
328 | ------------------------- |
---|
329 | |
---|
330 | The following groups of attributes are send as part of the |
---|
331 | CUPS-Get-Classes Response: |
---|
332 | |
---|
333 | Group 1: Operation Attributes |
---|
334 | Status Message: |
---|
335 | The standard response status message. |
---|
336 | Natural Language and Character Set: |
---|
337 | The 'attributes-charset' and |
---|
338 | 'attributes-natural-language' attributes as described |
---|
339 | in section 3.1.4.2 of the IPP Model and Semantics |
---|
340 | document. |
---|
341 | |
---|
342 | Group 2: Printer Class Object Attributes |
---|
343 | The set of requested attributes and their current values |
---|
344 | for each printer class. |
---|
345 | |
---|
346 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_CLASSES ) |
---|
347 | |
---|
348 | """ |
---|
349 | |
---|
350 | # We have no printer classes, so we don't need to do anything |
---|
351 | pass |
---|