def httpin_delivery(self, rcpt, objname, recordid): name = objname item = get_item(name, True) if item is None: #could not find item to look up its deliverer. return an error logging.error( 'Error, Paid item %s not found yet was found before. Requested by %s using %s.' % (name, self.request.headers['X-SecondLife-Owner-Name'], self.request.headers['X-SecondLife-Object-Name'])) self.error(403) return name_version = "%s - %s" % (name, item['version']) if item['givers'] == []: logging.error('Error, Paid item %s does not have http urls.' % (name)) self.error(403) return givers = item['givers'] #need to add a way to rotate the urls url_token = 'url_%s' % name url = '' url_num = 0 for _i in range(3): #will move the code here to compact it instead of the else and ifs url_num = memcache.incr(url_token, initial_value=0) url_num = url_num % ((len(givers))) url_cached = True count_token = 'item_count_%s' % name memcache.incr(count_token, initial_value=0) deliv_url_token = 'deliv_url_%s' % givers[url_num] cacheddist = memcache.get(deliv_url_token) if cacheddist is None: deliv = Deliver.get_by_key_name("Deliv:%s" % givers[url_num]) if deliv is None: #could not find item deliverer. remove_item_url(name, givers[url_num]) continue else: url_cached = False url = deliv.url memcache.set(deliv_url_token, deliv.url) else: url = cacheddist logging.debug("Tring to send %s to %s using deliver %s" % (name, rcpt, givers[url_num])) result = urlfetch.fetch(url, method="POST", payload="%s|%s" % (name_version, rcpt), headers={}, deadline=10) if result.content == "sent": #record = Purchases(purchaser = rcpt, item = name, seller = self.request.headers['X-SecondLife-Owner-Key'], item_reciver = 'request', loc = '%s %s' % (self.request.headers['X-SecondLife-Region'], self.request.headers['X-SecondLife-Local-Position']), vender_name = self.request.headers['X-SecondLife-Object-Name'], amount_paid = paid) #have the vendor transfer the money return 'sent|%s|%s|%s' % (rcpt, name_version, recordid ) #do we need all of this? elif result.status_code == 410: #the item is not in that box remove the box from that item remove_item_url(name, givers[url_num]) elif result.status_code == 404: if url_cached: deliv = Deliver.get_by_key_name("Deliv:%s" % givers[url_num]) if deliv is None: #check for no results. remove_item_url(name, givers[url_num]) elif url != deliv.url: #try again as the url was out of date url_cached = False memcache.set(deliv_url_token, deliv.url) url = deliv.url else: remove_item_url(name, givers[url_num]) else: remove_item_url(name, givers[url_num]) else: logging.error( 'Error, item %s did not get sent by %s may try again. Status %s Message from vendor: %s' % (name_version, givers[url_num], result.status_code, result.content)) else: logging.error( 'Error, Paid item %s did not get sent. Status %s Message from vendor: %s' % (name_version, result.status_code, result.content)) self.error(403)
def httpin_delivery(self, rcpt, objname, recordid): name = objname item = get_item(name, True) if item is None: #could not find item to look up its deliverer. return an error logging.error('Error, Paid item %s not found yet was found before. Requested by %s using %s.' % (name, self.request.headers['X-SecondLife-Owner-Name'], self.request.headers['X-SecondLife-Object-Name'])) self.error(403) return name_version = "%s - %s" % (name, item['version']) if item['givers'] == []: logging.error('Error, Paid item %s does not have http urls.' % (name)) self.error(403) return givers = item['givers'] #need to add a way to rotate the urls url_token = 'url_%s' % name url = '' url_num = 0 for _i in range(3): #will move the code here to compact it instead of the else and ifs url_num = memcache.incr(url_token, initial_value=0) url_num = url_num % ((len(givers))) url_cached = True count_token = 'item_count_%s' % name memcache.incr(count_token, initial_value=0) deliv_url_token = 'deliv_url_%s' % givers[url_num] cacheddist = memcache.get(deliv_url_token) if cacheddist is None: deliv = Deliver.get_by_key_name("Deliv:%s" % givers[url_num]) if deliv is None: #could not find item deliverer. remove_item_url(name, givers[url_num]) continue else: url_cached = False url = deliv.url memcache.set(deliv_url_token, deliv.url) else: url = cacheddist logging.debug("Tring to send %s to %s using deliver %s" % (name, rcpt, givers[url_num])) result = urlfetch.fetch(url, method="POST", payload="%s|%s" % (name_version, rcpt) , headers={}, deadline = 10) if result.content == "sent": #record = Purchases(purchaser = rcpt, item = name, seller = self.request.headers['X-SecondLife-Owner-Key'], item_reciver = 'request', loc = '%s %s' % (self.request.headers['X-SecondLife-Region'], self.request.headers['X-SecondLife-Local-Position']), vender_name = self.request.headers['X-SecondLife-Object-Name'], amount_paid = paid) #have the vendor transfer the money return 'sent|%s|%s|%s' % (rcpt, name_version, recordid)#do we need all of this? elif result.status_code == 410: #the item is not in that box remove the box from that item remove_item_url(name, givers[url_num]) elif result.status_code == 404: if url_cached: deliv = Deliver.get_by_key_name("Deliv:%s" % givers[url_num]) if deliv is None: #check for no results. remove_item_url(name, givers[url_num]) elif url != deliv.url: #try again as the url was out of date url_cached = False memcache.set(deliv_url_token, deliv.url) url = deliv.url else: remove_item_url(name, givers[url_num]) else: remove_item_url(name, givers[url_num]) else: logging.error('Error, item %s did not get sent by %s may try again. Status %s Message from vendor: %s' % (name_version, givers[url_num], result.status_code, result.content)) else: logging.error('Error, Paid item %s did not get sent. Status %s Message from vendor: %s' % (name_version, result.status_code, result.content)) self.error(403)
def get(self): if lindenip.inrange(os.environ['REMOTE_ADDR']) != 'Production': self.error(403) elif self.request.headers['X-SecondLife-Shard'] != 'Production': logging.warning("Attempt while on beta grid %s" % (self.request.headers['X-SecondLife-Shard'])) self.response.set_status(305) elif not distributors.Contributor_authorized(self.request.headers['X-SecondLife-Owner-Key']): logging.warning("Illegal attempt to update item from %s, box %s located in %s at %s" % (self.request.headers['X-SecondLife-Owner-Name'], self.request.headers['X-SecondLife-Object-Name'], self.request.headers['X-SecondLife-Region'], self.request.headers['X-SecondLife-Local-Position'])) self.error(403) else: self.response.headers['Content-Type'] = 'text/plain' name = cgi.escape(self.request.get('object')) version = cgi.escape(self.request.get('version')) giverkey = self.request.headers['X-SecondLife-Object-Key'] avname = self.request.headers['X-SecondLife-Owner-Name'] location = '%s @ %s' % (self.request.headers['X-SecondLife-Region'], self.request.headers['X-SecondLife-Local-Position']) timestring = datetime.datetime.utcnow() url = cgi.escape(self.request.get('url', default_value='None')) if url == 'None': giverurl = [] else: giverurl = [unicode(giverkey)] deliv_box = Deliver.get_or_insert("Deliv:%s" % giverkey, timedate=0, url='None') update_dist = False if url != deliv_box.url: update_dist = True elif timestring - deliv_box.timedate > datetime.timedelta(0, 60): update_dist = True if update_dist: deliv_box.url = url deliv_box.timedate = timestring deliv_box.owner = avname deliv_box.location = location deliv_box.put() # If no version provided, set a default version = version or 'NOVERSION' #look for an existing item with that name items = FreebieItem.gql("WHERE freebie_name = :1", name) item = items.get() if (item == None): newitem = FreebieItem(freebie_name = name, freebie_version = version, freebie_giver = giverkey, givers = giverurl, freebie_owner = avname, freebie_timedate = timestring, freebie_location = location) newitem.put() item = newitem # Version check removed because 3.7x and onwards does not include # version numbers in object names. So this means that it's # possible to downgrade and switch to delivering an older version. -Nan # TODO: Instead of having items identified only by name and # version, allow them to be identified by name and UUID. This will # allow us to have unversioned items that are still delivered by # multiple dist boxes, because we can tell if a box is out of sync # by the item's UUID. else: item.givers = [] logging.info("Clearing box list for item %s" % name) # will be 'NOVERSION' for 3.7x collars item.freebie_version = version item.freebie_giver = giverkey if giverurl != []: if giverkey in item.givers: #item.givers[item.givers.index(giverkey)+1] = url logging.info("Box in list") else: item.givers[:0] = giverurl logging.info("Adding box") elif giverkey in item.givers: index = item.givers.index(giverkey) item.givers[index:index] = [] logging.warning("Removing box") item.freebie_owner = avname item.freebie_timedate = timestring item.freebie_location = location item.put() #update item in memcache token = 'item_%s' % name token2 = 'paid_item_%s' % name data = yaml.safe_dump({ "name":name, "version":item.freebie_version, "giver":item.freebie_giver, "givers":item.givers, "designer_key":item.designer_key, "designer_cut":item.designer_cut, "baseprice":item.baseprice }) if item.baseprice == 0: memcache.set(token, data) memcache.set(token2, data) self.response.out.write('saved') logging.info('saved item %s version %s by %s with urls %s' % (name, item.freebie_version, avname, item.givers))
def get(self): if lindenip.inrange(os.environ['REMOTE_ADDR']) != 'Production': self.error(403) elif self.request.headers['X-SecondLife-Shard'] != 'Production': logging.warning("Attempt while on beta grid %s" % (self.request.headers['X-SecondLife-Shard'])) self.response.set_status(305) elif not distributors.Contributor_authorized( self.request.headers['X-SecondLife-Owner-Key']): logging.warning( "Illegal attempt to update item from %s, box %s located in %s at %s" % (self.request.headers['X-SecondLife-Owner-Name'], self.request.headers['X-SecondLife-Object-Name'], self.request.headers['X-SecondLife-Region'], self.request.headers['X-SecondLife-Local-Position'])) self.error(403) else: self.response.headers['Content-Type'] = 'text/plain' name = cgi.escape(self.request.get('object')) version = cgi.escape(self.request.get('version')) giverkey = self.request.headers['X-SecondLife-Object-Key'] avname = self.request.headers['X-SecondLife-Owner-Name'] location = '%s @ %s' % ( self.request.headers['X-SecondLife-Region'], self.request.headers['X-SecondLife-Local-Position']) timestring = datetime.datetime.utcnow() url = cgi.escape(self.request.get('url', default_value='None')) if url == 'None': giverurl = [] else: giverurl = [unicode(giverkey)] deliv_box = Deliver.get_or_insert("Deliv:%s" % giverkey, timedate=0, url='None') update_dist = False if url != deliv_box.url: update_dist = True elif timestring - deliv_box.timedate > datetime.timedelta( 0, 60): update_dist = True if update_dist: deliv_box.url = url deliv_box.timedate = timestring deliv_box.owner = avname deliv_box.location = location deliv_box.put() # If no version provided, set a default version = version or 'NOVERSION' #look for an existing item with that name items = FreebieItem.gql("WHERE freebie_name = :1", name) item = items.get() if (item == None): newitem = FreebieItem(freebie_name=name, freebie_version=version, freebie_giver=giverkey, givers=giverurl, freebie_owner=avname, freebie_timedate=timestring, freebie_location=location) newitem.put() item = newitem # Version check removed because 3.7x and onwards does not include # version numbers in object names. So this means that it's # possible to downgrade and switch to delivering an older version. -Nan # TODO: Instead of having items identified only by name and # version, allow them to be identified by name and UUID. This will # allow us to have unversioned items that are still delivered by # multiple dist boxes, because we can tell if a box is out of sync # by the item's UUID. else: item.givers = [] logging.info("Clearing box list for item %s" % name) # will be 'NOVERSION' for 3.7x collars item.freebie_version = version item.freebie_giver = giverkey if giverurl != []: if giverkey in item.givers: #item.givers[item.givers.index(giverkey)+1] = url logging.info("Box in list") else: item.givers[:0] = giverurl logging.info("Adding box") elif giverkey in item.givers: index = item.givers.index(giverkey) item.givers[index:index] = [] logging.warning("Removing box") item.freebie_owner = avname item.freebie_timedate = timestring item.freebie_location = location item.put() #update item in memcache token = 'item_%s' % name token2 = 'paid_item_%s' % name data = yaml.safe_dump({ "name": name, "version": item.freebie_version, "giver": item.freebie_giver, "givers": item.givers, "designer_key": item.designer_key, "designer_cut": item.designer_cut, "baseprice": item.baseprice }) if item.baseprice == 0: memcache.set(token, data) memcache.set(token2, data) self.response.out.write('saved') logging.info('saved item %s version %s by %s with urls %s' % (name, item.freebie_version, avname, item.givers))