

def post(self):
    self.response.headers.add_header('Access-Control-Allow-Origin', '*')
    header = self.request.headers['Content-Type']
    logging.debug(("Content-Type: '%s'" % header))
    if str(header).startswith('text/plain'):
        self.isJson = True
        self.response.headers['Content-Type'] = 'text/plain'
        data_dict = json.loads(self.request.body)
    else:
        self.response.headers['Content-Type'] = 'application/octet-stream'
    STR_VERSERVER = '01060000'
    INT_VERCLIENT = 17170432
    STR_VERCLIENT = '1.6'
    if (not os.environ.has_key('HTTPS')):
        self.resp_simple(0, 'HTTPS environment variable not found')
        return
    if (not os.environ.has_key('CURRENT_VERSION_ID')):
        self.resp_simple(0, 'CURRENT_VERSION_ID environment variable not found')
        return
    HTTPS = os.environ.get('HTTPS', 'off')
    CURRENT_VERSION_ID = os.environ.get('CURRENT_VERSION_ID', STR_VERSERVER)
    if (HTTPS.__str__() != 'on'):
        self.resp_simple(0, 'Secure socket required.')
        return
    minlen = ((((4 + 4) + 4) + 4) + 4)
    data = self.request.body
    logging.debug(("in body '%s'" % data))
    size = str.__len__(data)
    logging.debug(('in size %d' % size))
    if (size < minlen):
        self.resp_simple(0, 'Request was formatted incorrectly.')
        return
    server = int(CURRENT_VERSION_ID[0:8], 16)
    if self.isJson:
        client = int(data_dict['ver_client'], 10)
    else:
        client = struct.unpack('!i', data[0:4])[0]
    logging.debug(('in client %d' % client))
    if self.isJson:
        usrid = int(data_dict['usrid'], 10)
    else:
        usrid = struct.unpack('!i', data[4:8])[0]
    logging.debug(('in usrid %d' % usrid))
    if self.isJson:
        usridlink = int(data_dict['usridlink'], 10)
    else:
        usridlink = struct.unpack('!i', data[8:12])[0]
    logging.debug(('in usridlink %d' % usridlink))
    if self.isJson:
        numEntry = data_dict['usrids'].__len__()
    else:
        numEntry = struct.unpack('!i', data[12:16])[0]
    logging.debug(('in numEntry %d' % numEntry))
    pos = 16
    expectedsize = ((((4 + 4) + 4) + 4) + (4 * numEntry))
    usrids = []
    i = 0
    while (numEntry > len(usrids)):
        if self.isJson:
            otherid = int(data_dict['usrids'][i], 10)
            i += 1
        else:
            otherid = struct.unpack('!i', data[pos:(pos + 4)])[0]
            pos += 4
        usrids.append(otherid)
        logging.debug(('in usrid known %i' % otherid))
    postSelf = False
    if self.isJson:
        if ('commit_b64' in data_dict):
            commit = base64.decodestring(data_dict['commit_b64'])
            postSelf = True
    elif (size > expectedsize):
        commit = data[pos:]
        postSelf = True
    if postSelf:
        logging.debug(("in commitment '%s'" % commit))
    if (client < INT_VERCLIENT):
        self.resp_simple(0, ('Client version mismatch; %s required.  Download latest client release first.' % STR_VERCLIENT))
        return
    query = member.Member.all()
    query.filter('usr_id =', usrid)
    num = query.count()
    if (num == 1):
        mem = query.get()
        if postSelf:
            mem.usr_id_link = usridlink
            mem.put()
            key = mem.key()
            if (not key.has_id_or_name()):
                self.resp_simple(0, 'Unable to update user.')
                return
        elif (mem.commitment == None):
            self.resp_simple(0, 'Request was formatted incorrectly.')
            return
        query = member.Member.all()
        query.filter('usr_id_link =', usridlink)
        mems = query.fetch(1000)
        if (not self.isJson):
            self.response.out.write(('%s' % struct.pack('!i', server)))
        logging.debug(('out server %i' % server))
        q = member.Member.all()
        q.filter('usr_id_link =', usridlink)
        m = q.fetch(1000)
        low_client = self.getLowestVersion(m)
        if (not self.isJson):
            self.response.out.write(('%s' % struct.pack('!i', low_client)))
        logging.debug(('out low_client %i' % low_client))
        total = 0
        for mem in mems:
            if (mem.commitment != None):
                total = (total + 1)
        if (not self.isJson):
            self.response.out.write(('%s' % struct.pack('!i', total)))
        logging.debug(('out total commits %i' % total))
        delta = 0
        for mem in mems:
            posted = False
            for known in usrids:
                if (known == mem.usr_id):
                    posted = True
            if ((not posted) & (mem.commitment != None)):
                delta = (delta + 1)
        if (not self.isJson):
            self.response.out.write(('%s' % struct.pack('!i', delta)))
        logging.debug(('out delta commits %i' % delta))
        deltas = []
        for mem in mems:
            posted = False
            for known in usrids:
                if (known == mem.usr_id):
                    posted = True
            if ((not posted) & (mem.commitment != None)):
                length = str.__len__(mem.commitment)
                if self.isJson:
                    deltas.append({
                        'usrid': str(mem.usr_id),
                        'commit_b64': base64.encodestring(mem.commitment),
                    })
                else:
                    self.response.out.write(('%s%s' % (struct.pack('!ii', mem.usr_id, length), mem.commitment)))
                logging.debug(('out mem.usr_id %i' % mem.usr_id))
                logging.debug(('out mem.commitment length %i' % length))
                logging.debug(("out mem.commitment '%s'" % mem.commitment))
    else:
        self.resp_simple(0, ('user %i does not exist' % usrid))
        return
    if self.isJson:
        json.dump({
            'ver_server': str(server),
            'ver_low_client': str(low_client),
            'com_total': str(total),
            'com_deltas': deltas,
        }, self.response.out)

dataset/ETHPy150Open SafeSlingerProject/SafeSlinger-AppEngine/safeslinger-demo/python/syncUsers.py SyncUsers.post/original