Skip to content
Permalink
Browse files

Adds user agent and ip address to APNS subscription table (for debugg…

…ing)

git-svn-id: https://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk@9510 e27351fd-9f3e-4f54-a53b-843176b1656c
  • Loading branch information...
m0rgen committed Jul 31, 2012
1 parent 07df3d3 commit a314a467bdaa9d11fb561e9a68e27f82ef3ed8c9
@@ -770,6 +770,9 @@ def processSubscription(self, request):
token = request.args.get("token", ("",))[0].replace(" ", "").lower()
key = request.args.get("key", ("",))[0]

userAgent = request.headers.getHeader("user-agent", "-")
host = request.remoteAddr.host

if not (key and token):
code = responsecode.BAD_REQUEST
msg = "Invalid request: both 'token' and 'key' must be provided"
@@ -782,7 +785,7 @@ def processSubscription(self, request):
principal = self.principalFromRequest(request)
uid = principal.record.uid
try:
yield self.addSubscription(token, key, uid)
yield self.addSubscription(token, key, uid, userAgent, host)
code = responsecode.OK
msg = None
except InvalidSubscriptionValues:
@@ -792,7 +795,7 @@ def processSubscription(self, request):
returnValue((code, msg))

@inlineCallbacks
def addSubscription(self, token, key, uid):
def addSubscription(self, token, key, uid, userAgent, host):
"""
Add a subscription (or update its timestamp if already there).
@@ -804,10 +807,16 @@ def addSubscription(self, token, key, uid):
@param uid: The uid of the subscriber principal
@type uid: C{str}
@param userAgent: The user-agent requesting the subscription
@type key: C{str}
@param host: The host requesting the subscription
@type key: C{str}
"""
now = int(time.time()) # epoch seconds
txn = self.store.newTransaction()
yield txn.addAPNSubscription(token, key, now, uid)
yield txn.addAPNSubscription(token, key, now, uid, userAgent, host)
yield txn.commit()

def renderResponse(self, code, body=None):
@@ -72,11 +72,11 @@ def test_ApplePushNotifierService(self):

# Ensure empty values don't get through
try:
yield txn.addAPNSubscription("", "", "", "")
yield txn.addAPNSubscription("", "", "", "", "", "")
except InvalidSubscriptionValues:
pass
try:
yield txn.addAPNSubscription("", "1", "2", "3")
yield txn.addAPNSubscription("", "1", "2", "3", "", "")
except InvalidSubscriptionValues:
pass

@@ -85,12 +85,14 @@ def test_ApplePushNotifierService(self):
key1 = "/CalDAV/calendars.example.com/user01/calendar/"
timestamp1 = 1000
uid = "D2256BCC-48E2-42D1-BD89-CBA1E4CCDFFB"
yield txn.addAPNSubscription(token, key1, timestamp1, uid)
yield txn.addAPNSubscription(token2, key1, timestamp1, uid)
userAgent = "test agent"
ipAddr = "127.0.0.1"
yield txn.addAPNSubscription(token, key1, timestamp1, uid, userAgent, ipAddr)
yield txn.addAPNSubscription(token2, key1, timestamp1, uid, userAgent, ipAddr)

key2 = "/CalDAV/calendars.example.com/user02/calendar/"
timestamp2 = 3000
yield txn.addAPNSubscription(token, key2, timestamp2, uid)
yield txn.addAPNSubscription(token, key2, timestamp2, uid, userAgent, ipAddr)

subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid))
self.assertTrue([token, key1, timestamp1] in subscriptions)
@@ -101,14 +103,14 @@ def test_ApplePushNotifierService(self):
# the new uid
timestamp3 = 5000
uid2 = "D8FFB335-9D36-4CE8-A3B9-D1859E38C0DA"
yield txn.addAPNSubscription(token, key2, timestamp3, uid2)
yield txn.addAPNSubscription(token, key2, timestamp3, uid2, userAgent, ipAddr)
subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid))
self.assertTrue([token, key1, timestamp1] in subscriptions)
self.assertFalse([token, key2, timestamp3] in subscriptions)
subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid2))
self.assertTrue([token, key2, timestamp3] in subscriptions)
# Change it back
yield txn.addAPNSubscription(token, key2, timestamp2, uid)
yield txn.addAPNSubscription(token, key2, timestamp2, uid, userAgent, ipAddr)

yield txn.commit()

@@ -270,8 +272,8 @@ def feedbackTestFunction(timestamp, token):
# Create two subscriptions, one old and one new
txn = self.store.newTransaction()
now = int(time.time())
yield txn.addAPNSubscription(token2, key1, now - 2 * 24 * 60 * 60, uid) # old
yield txn.addAPNSubscription(token2, key2, now, uid) # recent
yield txn.addAPNSubscription(token2, key1, now - 2 * 24 * 60 * 60, uid, userAgent, ipAddr) # old
yield txn.addAPNSubscription(token2, key2, now, uid, userAgent, ipAddr) # recent
yield txn.commit()

# Purge old subscriptions
@@ -251,7 +251,7 @@ def notificationsWithUID(self, uid, home=None):


# File-based storage of APN subscriptions not implementated.
def addAPNSubscription(self, token, key, timestamp, subscriber):
def addAPNSubscription(self, token, key, timestamp, subscriber, userAgent, ipAddr):
return NotImplementedError

def removeAPNSubscription(self, token, key):
@@ -455,14 +455,18 @@ def _insertAPNSubscriptionQuery(cls): #@NoSelf
return Insert({apn.TOKEN: Parameter("token"),
apn.RESOURCE_KEY: Parameter("resourceKey"),
apn.MODIFIED: Parameter("modified"),
apn.SUBSCRIBER_GUID: Parameter("subscriber")})
apn.SUBSCRIBER_GUID: Parameter("subscriber"),
apn.USER_AGENT : Parameter("userAgent"),
apn.IP_ADDR : Parameter("ipAddr")})


@classproperty
def _updateAPNSubscriptionQuery(cls): #@NoSelf
apn = schema.APN_SUBSCRIPTIONS
return Update({apn.MODIFIED: Parameter("modified"),
apn.SUBSCRIBER_GUID: Parameter("subscriber")},
apn.SUBSCRIBER_GUID: Parameter("subscriber"),
apn.USER_AGENT: Parameter("userAgent"),
apn.IP_ADDR : Parameter("ipAddr")},
Where=(apn.TOKEN == Parameter("token")).And(
apn.RESOURCE_KEY == Parameter("resourceKey")))

@@ -479,17 +483,23 @@ def _selectAPNSubscriptionQuery(cls): #@NoSelf


@inlineCallbacks
def addAPNSubscription(self, token, key, timestamp, subscriber):
def addAPNSubscription(self, token, key, timestamp, subscriber,
userAgent, ipAddr):
if not (token and key and timestamp and subscriber):
raise InvalidSubscriptionValues()

# Cap these values at 255 characters
userAgent = userAgent[:255]
ipAddr = ipAddr[:255]

row = yield self._selectAPNSubscriptionQuery.on(self,
token=token, resourceKey=key)
if not row: # Subscription does not yet exist
try:
yield self._insertAPNSubscriptionQuery.on(self,
token=token, resourceKey=key, modified=timestamp,
subscriber=subscriber)
subscriber=subscriber, userAgent=userAgent,
ipAddr=ipAddr)
except Exception:
# Subscription may have been added by someone else, which is fine
pass
@@ -498,7 +508,8 @@ def addAPNSubscription(self, token, key, timestamp, subscriber):
try:
yield self._updateAPNSubscriptionQuery.on(self,
token=token, resourceKey=key, modified=timestamp,
subscriber=subscriber)
subscriber=subscriber, userAgent=userAgent,
ipAddr=ipAddr)
except Exception:
# Subscription may have been added by someone else, which is fine
pass
@@ -250,6 +250,8 @@ create table APN_SUBSCRIPTIONS (
"RESOURCE_KEY" nvarchar2(255),
"MODIFIED" integer not null,
"SUBSCRIBER_GUID" nvarchar2(255),
"USER_AGENT" nvarchar2(255) default null,
"IP_ADDR" nvarchar2(255) default null,
primary key("TOKEN", "RESOURCE_KEY")
);

@@ -478,6 +478,8 @@ create table APN_SUBSCRIPTIONS (
RESOURCE_KEY varchar(255) not null,
MODIFIED integer not null,
SUBSCRIBER_GUID varchar(255) not null,
USER_AGENT varchar(255) default null,
IP_ADDR varchar(255) default null,

primary key(TOKEN, RESOURCE_KEY) -- implicit index
);
@@ -495,6 +497,6 @@ create table CALENDARSERVER (
VALUE varchar(255)
);

insert into CALENDARSERVER values ('VERSION', '10');
insert into CALENDARSERVER values ('VERSION', '11');
insert into CALENDARSERVER values ('CALENDAR-DATAVERSION', '3');
insert into CALENDARSERVER values ('ADDRESSBOOK-DATAVERSION', '1');

0 comments on commit a314a46

Please sign in to comment.
You can’t perform that action at this time.