Changeset 8e43aa8 for server


Ignore:
Timestamp:
Nov 6, 2010, 7:56:41 PM (13 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
Branches:
no-cups
Children:
aaa1da3
Parents:
478ca74
git-author:
Jessica B. Hamrick <jhamrick@…> (11/06/10 19:56:28)
git-committer:
Jessica B. Hamrick <jhamrick@…> (11/06/10 19:56:41)
Message:

Can correctly parse and pack IPP requests\!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/ipprequest.py

    r8403f61 r8e43aa8  
    8686        assert value is not None
    8787
    88         self.value_tag = hex(value_tag)
    89         self.value = str(value)
     88        self.value_tag = value_tag
     89        self.value = value
    9090
    9191class IPPAttribute():
     
    128128        Arguments:
    129129
    130         name -- the name of the attribute
    131 
    132         values -- a list of IPPValues.  May not be empty.
     130            name -- the name of the attribute
     131
     132            values -- a list of IPPValues.  May not be empty.
    133133        """
    134134
     
    141141        for value in values: assert isinstance(value, IPPValue)
    142142         
    143         self.name = str(name)
     143        self.name = name
    144144        self.values = values
    145145
     
    152152        values = []
    153153        for v, i in zip(self.values, xrange(len(self.values))):
     154            name_length = len(self.name)
     155            if i > 0: name_length = 0
     156            value_length = len(v.value)
     157
     158            logger.debug("dumping name_length : %i" % name_length)
     159            logger.debug("dumping name : %s" % self.name)
     160            logger.debug("dumping value_length : %i" % value_length)
     161            logger.debug("dumping value : %s" % v.value)
     162
     163            # the value tag in binary
     164            value_tag_bin = struct.pack('>b', v.value_tag)
     165
     166            # the name length in binary
     167            name_length_bin = struct.pack('>h', name_length)
     168
     169            # the name in binary
     170            name_bin = self.name
     171
     172            # the value length in binary
     173            value_length_bin = struct.pack('>h', value_length)
     174
     175            # the value in binary
     176            value_bin = v.value
     177
    154178            if i == 0:
    155                 name_length = sys.getsizeof(self.name)
    156                 value_length = sys.getsizeof(v.value)
    157                 values.append(struct.pack('>bh%ssh%ss' % (name_length, value_length),
    158                                           v.value_tag,
    159                                           name_length,
    160                                           self.name,
    161                                           value_length,
    162                                           v.value))
     179                values.append(''.join([value_tag_bin,
     180                                       name_length_bin,
     181                                       name_bin,
     182                                       value_length_bin,
     183                                       value_bin]))
    163184            else:
    164                 value_length = sys.getsizeof(v.value)
    165                 values.append(struct.pack('>bhh%ss' % (value_length),
    166                                           v.value_tag,
    167                                           name_length,
    168                                           value_length,
    169                                           v.value))
     185                values.append(''.join([value_tag_bin,
     186                                       name_length_bin,
     187                                       value_length_bin,
     188                                       value_bin]))
    170189               
    171190        # concatenate everything together and return it
    172191        return ''.join(values)
     192
     193class IPPAttributeGroup():
     194    """
     195    An IPPAttributeGroup consists of an attribute-group-tag, followed
     196    by a sequence of IPPAttributes.
     197    """
     198
     199    def __init__(self, attribute_group_tag, attributes=[]):
     200        """
     201        Initialize an IPPAttributeGroup.
     202
     203        Arguments:
     204
     205            attribute_group_tag -- a signed char, holds the tag of the
     206                                   attribute group
     207
     208            attributes -- (optional) a list of attributes
     209        """
     210
     211        # make sure attribute_group_tag isn't empty
     212        assert attribute_group_tag is not None
     213
     214        # make sure attributes is a list or tuple of IPPAttributes
     215        assert isinstance(attributes, (list, tuple))
     216        for a in attributes: assert isinstance(a, IPPAttribute)
     217
     218        self.attribute_group_tag = attribute_group_tag
     219        self.attributes = attributes
     220
     221    def toBinaryData(self):
     222        """
     223        Convert the IPPAttributeGroup to binary.
     224        """
     225
     226        # conver the attribute_group_tag to binary
     227        tag = struct.pack('>b', self.attribute_group_tag)
     228
     229        # convert each of the attributes to binary
     230        attributes = [a.toBinaryData() for a in self.attributes]
     231
     232        # concatenate everything and return
     233        return tag + ''.join(attributes)
    173234
    174235class IPPRequest():
     
    199260    # attribute_sequence, and data, or a file handler (request) which
    200261    # can be read from to get the request
    201     def __init__(self, version=None, operation_id=None, request_id=None, attributes=[], data=None, request=None):
     262    def __init__(self, version=None, operation_id=None, request_id=None,
     263                 attribute_groups=[], data=None, request=None):
    202264        """
    203265        Create an IPPRequest.  Takes either the segments of the
     
    218280                          request itself.
    219281
    220             attributes -- (optional) a list of IPPAttributes
     282            attribute_groups -- a list of IPPAttributes, at least length 1
    221283
    222284            data -- (optional) variable length, containing the actual
     
    239301            # make sure the request id isn't empty
    240302            assert request_id is not None
     303            # make sure attribute_groups is a list of IPPAttributes
     304            assert len(attribute_groups) > 0
     305            for a in attribute_groups: assert isinstance(a, IPPAttribute)
    241306           
    242307        # if the request isn't None, then we'll read directly from
     
    258323            # now we have to read in the attributes.  Each attribute
    259324            # has a tag (1 byte) and a sequence of values (n bytes)
    260             self.attributes    = []
     325            self.attribute_groups = []
    261326
    262327            # read in the next byte
     
    271336                logger.debug("attribute-tag : %i" % attribute_group_tag)
    272337
     338                attributes = []
     339
    273340                next_byte = struct.unpack('>b', request.read(1))[0]
    274341                logger.debug("next byte : 0x%X" % next_byte)
     
    299366                        # create a new IPPAttribute from the data we just
    300367                        # read in, and add it to our attributes list
    301                         self.attributes.append(IPPAttribute(name,
    302                                                             [IPPValue(value_tag, value)]))
     368                        attributes.append(IPPAttribute(name,
     369                                                       [IPPValue(value_tag, value)]))
    303370
    304371                    else:
     
    312379
    313380                        # add another value to the last attribute
    314                         self.attributes[-1].values.append(IPPValue(value_tag, value))
     381                        attributes[-1].values.append(IPPValue(value_tag, value))
    315382
    316383                    # read another byte
    317384                    next_byte = struct.unpack('>b', request.read(1))[0]
     385
     386                self.attribute_groups.append(IPPAttributeGroup(attribute_group_tag, attributes))
    318387
    319388            # once we hit the end-of-attributes tag, the only thing
     
    325394        # arguments passed in
    326395        else:
    327             self.version = (int(version[0]), int(version[1]))
    328             self.operation_id = int(operation_id)
    329             self.request_id = int(request_id)
    330             self.attributes = attributes
     396            self.version = (version[0], version[1])
     397            self.operation_id = operation_id
     398            self.request_id = request_id
     399            self.attribute_groups = attribute_groups
    331400            self.data = data
    332401
     
    343412                                    self.request_id)
    344413
    345         # convert the attributes to binary
    346         attributes = ''.join([a.toBinaryData() for a in self.attributes])
     414        # convert the attribute groups to binary
     415        attribute_groups = ''.join([a.toBinaryData() for a in self.attribute_groups])
    347416
    348417        # conver the end-of-attributes-tag to binary
     
    356425
    357426        # append everything together and return it
    358         return preattributes + attributes + end_of_attributes_tag + data
     427        return preattributes + attribute_groups + end_of_attributes_tag + data
Note: See TracChangeset for help on using the changeset viewer.