Changeset d56a0bc
- Timestamp:
- Nov 6, 2010, 5:00:53 PM (14 years ago)
- Branches:
- no-cups
- Children:
- 8403f61
- Parents:
- 4ec7caa
- git-author:
- Jessica B. Hamrick <jhamrick@…> (11/06/10 17:00:53)
- git-committer:
- Jessica B. Hamrick <jhamrick@…> (11/06/10 17:00:53)
- Location:
- server/lib
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/lib/ipprequest.py
r4ec7caa rd56a0bc 10 10 11 11 # various tags 12 ZERO_NAME_LENGTH = 0 X0013 OPERATION_ATTRIBUTES_TAG = 0 X0114 JOB_ATTRIBUTES_TAG = 0 X0215 END_OF_ATTRIBUTES_TAG = 0 X0316 PRINTER_ATTRIBUTES_TAG = 0 X0417 UNSUPPORTED_ATTRIBUTES_TAG = 0 X0512 ZERO_NAME_LENGTH = 0x00 13 OPERATION_ATTRIBUTES_TAG = 0x01 14 JOB_ATTRIBUTES_TAG = 0x02 15 END_OF_ATTRIBUTES_TAG = 0x03 16 PRINTER_ATTRIBUTES_TAG = 0x04 17 UNSUPPORTED_ATTRIBUTES_TAG = 0x05 18 18 19 19 # "out of band" value tags 20 UNSUPPORTED = 0 X1021 DEFAULT = 0 X1122 UNKNOWN = 0 X1223 NO_VALUE = 0 X1320 UNSUPPORTED = 0x10 21 DEFAULT = 0x11 22 UNKNOWN = 0x12 23 NO_VALUE = 0x13 24 24 25 25 # integer value tags 26 GENERIC_INTEGER = 0 X2027 INTEGER = 0 X2128 BOOLEAN = 0 X2229 ENUM = 0 X2326 GENERIC_INTEGER = 0x20 27 INTEGER = 0x21 28 BOOLEAN = 0x22 29 ENUM = 0x23 30 30 31 31 # octetstring value tags 32 UNSPECIFIED_OCTETSTRING = 0 X3033 DATETIME = 0 X3134 RESOLUTION = 0 X3235 RANGEOFINTEGER = 0 X3336 COLLECTION = 0 X3437 TEXTWITHLANGUAGE = 0 X3538 NAMEWITHLANGUAGE = 0 X3632 UNSPECIFIED_OCTETSTRING = 0x30 33 DATETIME = 0x31 34 RESOLUTION = 0x32 35 RANGEOFINTEGER = 0x33 36 COLLECTION = 0x34 37 TEXTWITHLANGUAGE = 0x35 38 NAMEWITHLANGUAGE = 0x36 39 39 40 40 # character-string value tags 41 GENERIC_CHAR_STRING = 0 X4042 TEXTWITHOUTLANGUAGE = 0 X4143 NAMEWITHOUTLANGUAGE = 0 X4244 KEYWORD = 0 X4445 URI = 0 X4546 URISCHEME = 0 X4647 CHARSET = 0 X4748 NATURALLANGUAGE = 0 X4849 MIMEMEDIATYPE = 0 X4941 GENERIC_CHAR_STRING = 0x40 42 TEXTWITHOUTLANGUAGE = 0x41 43 NAMEWITHOUTLANGUAGE = 0x42 44 KEYWORD = 0x44 45 URI = 0x45 46 URISCHEME = 0x46 47 CHARSET = 0x47 48 NATURALLANGUAGE = 0x48 49 MIMEMEDIATYPE = 0x49 50 50 51 51 class IPPValue(): 52 52 """ 53 53 An IPP value consists of a tag and a value, and optionally, a name. 54 55 From RFC 2565: 56 ----------------------------------------------- 57 | value-tag | 1 byte 58 ----------------------------------------------- 59 | name-length (value is u) | 2 bytes 60 ----------------------------------------------- 61 | name | u bytes 62 ----------------------------------------------- 63 | value-length (value is v) | 2 bytes 64 ----------------------------------------------- 65 | value | v bytes 66 ----------------------------------------------- 54 67 """ 55 68 … … 78 91 self.name = str(name) 79 92 self.value = str(value) 93 94 def toBinaryData(self): 95 """ 96 Packs the value data into binary data. 97 """ 98 99 # get the length in bytes of the name 100 name_length = sys.getsizeof(name) 101 # get the length in bytes of the value 102 value_length = sys.getsizeof(value) 103 104 # if the name is of length zero, then don't include it in the 105 # binary data 106 if name_length == IPPTags.ZERO_NAME_LENGTH: 107 binary = struct.pack('>bhhs', 108 self.value_tag, 109 name_length, 110 value_length, 111 self.value) 112 else: 113 binary = struct.pack('>bhshs', 114 self.value_tag, 115 name_length, 116 self.name, 117 value_length, 118 self.value) 119 120 return binary 80 121 81 122 class IPPAttribute(): … … 135 176 self.attribute_tag = hex(attribute_tag) 136 177 self.values = values 178 179 def toBinaryData(self): 180 """ 181 Packs the attribute data into binary data. 182 """ 183 184 # pack the attribute tag 185 attribute_tag = struct.pack('>b', self.attribute_tag) 186 # get the binary data for all the values 187 values = [v.toBinaryData() for v in self.values] 188 189 # concatenate everything together and return it 190 return attribute_tag + ''.join(values) 137 191 138 192 class IPPRequest(): … … 216 270 if request is not None: 217 271 # read the version-number (two signed chars) 218 self.version = struct.unpack(' bb', request.read(2))272 self.version = struct.unpack('>bb', request.read(2)) 219 273 220 274 # read the operation-id (or status-code, but that's only 221 275 # for a response) (signed short) 222 self.operation_id = struct.unpack(' h', request.read(2))276 self.operation_id = struct.unpack('>h', request.read(2)) 223 277 224 278 # read the request-id (signed int) 225 self.request_id = struct.unpack(' i', request.read(4))279 self.request_id = struct.unpack('>i', request.read(4)) 226 280 227 281 # now we have to read in the attributes. Each attribute … … 230 284 231 285 # read in the next byte 232 next_byte = struct.unpack(' b', request.read(1))286 next_byte = struct.unpack('>b', request.read(1)) 233 287 234 288 # as long as the next byte isn't signaling the end of the … … 242 296 attribute_tag = next_byte 243 297 # read in the value tag (signed char) 244 value_tag = struct.unpack(' b', request.read(1))298 value_tag = struct.unpack('>b', request.read(1)) 245 299 # read in the length of the name (signed short) 246 name_length = struct.unpack(' h', request.read(2))300 name_length = struct.unpack('>h', request.read(2)) 247 301 # read the name (a string of name_length bytes) 248 name = struct.unpack(' s', request.read(name_length))302 name = struct.unpack('>s', request.read(name_length)) 249 303 # read in the length of the value (signed short) 250 value_length = struct.unpack(' h', request.read(2))304 value_length = struct.unpack('>h', request.read(2)) 251 305 # read in the value (string of value_length bytes) 252 value = struct.unpack(' b'*value_length, request.read(value_length))306 value = struct.unpack('>%sb' % value_length, request.read(value_length)) 253 307 254 308 # create a new IPPAttribute from the data we just … … 265 319 # read in the length of the name (two bytes) -- 266 320 # this should be 0x0 267 name_length = struct.unpack(' h', request.read(2))268 assert name_length == zero_name_length321 name_length = struct.unpack('>h', request.read(2)) 322 assert name_length == IPPTags.ZERO_NAME_LENGTH 269 323 # name should be empty 270 324 name = '' 271 325 # read in the length of the value (two bytes) 272 value_length = struct.unpack(' h', request.read(2))326 value_length = struct.unpack('>h', request.read(2)) 273 327 # read in the value (value_length bytes) 274 value = struct.unpack(' b'*value_length, request.read(value_length))328 value = struct.unpack('>%sb' % value_length, request.read(value_length)) 275 329 276 330 # add another value to the last attribute … … 278 332 279 333 # read another byte 280 next_byte = struct.unpack(' b', request.read(1))334 next_byte = struct.unpack('>b', request.read(1)) 281 335 282 336 # once we hit the end-of-attributes tag, the only thing 283 337 # left is the data, so go ahead and read all of it 284 338 buff = request.read() 285 self.data = struct.unpack(' b'*len(buff), sys.getsizeof(buff))339 self.data = struct.unpack('>%sb' % len(buff), sys.getsizeof(buff)) 286 340 287 341 # otherwise, just set the class variables to the keyword … … 293 347 self.attributes = attributes 294 348 self.data = data 349 350 def toBinaryData(self): 351 """ 352 Packs the value data into binary data. 353 """ 354 355 # convert the version, operation id, and request id to binary 356 preattributes = struct.pack('>bbhi', 357 self.version[0], 358 self.version[1], 359 self.operation_id, 360 self.request_id) 361 362 # convert the attributes to binary 363 attributes = ''.join([a.toBinaryData() for a in attributes]) 364 365 # conver the end-of-attributes-tag to binary 366 end_of_attributes_tag = struct.pack('>b', IPPTags.END_OF_ATTRIBUTES_TAG) 367 368 # convert the data to binary 369 data = ''.join([struct.pack('>b', x) for x in self.data]) 370 371 # append everything together and return it 372 return preattributes + attributes + end_of_attributes_tag + data
Note: See TracChangeset
for help on using the changeset viewer.