diff --git a/apps/categories/views.py b/apps/categories/views.py index 683218c82..b55b04b89 100644 --- a/apps/categories/views.py +++ b/apps/categories/views.py @@ -15,7 +15,7 @@ def subscribe(request): user = request.user categories = MCategory.serialize() category_titles = [c['title'] for c in categories['categories']] - subscribe_category_titles = request.REQUEST.getlist('category') + subscribe_category_titles = request.REQUEST.getlist('category') or request.REQUEST.getlist('category[]') invalid_category_title = False for category_title in subscribe_category_titles: diff --git a/apps/newsletters/models.py b/apps/newsletters/models.py index a4dd9d8a7..611a49073 100644 --- a/apps/newsletters/models.py +++ b/apps/newsletters/models.py @@ -179,8 +179,8 @@ class EmailNewsletter: r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) listeners_count = r.publish("%s:story" % feed.pk, 'story:new:%s' % story_hash) if listeners_count: - logging.debug(" ---> [%-30s] ~FMPublished to %s subscribers" % (feed.title[:30], listeners_count)) + logging.debug(" ---> [%-30s] ~FMPublished to %s subscribers" % (feed.log_title[:30], listeners_count)) except redis.ConnectionError: - logging.debug(" ***> [%-30s] ~BMRedis is unavailable for real-time." % (feed.title[:30],)) + logging.debug(" ***> [%-30s] ~BMRedis is unavailable for real-time." % (feed.log_title[:30],)) \ No newline at end of file diff --git a/apps/notifications/models.py b/apps/notifications/models.py index 057f1a125..7d17696ce 100644 --- a/apps/notifications/models.py +++ b/apps/notifications/models.py @@ -5,6 +5,7 @@ import mongoengine as mongo import boto from django.conf import settings from django.contrib.auth.models import User +from django.contrib.sites.models import Site from django.template.loader import render_to_string from django.core.mail import EmailMultiAlternatives # from django.utils.html import strip_tags @@ -17,8 +18,9 @@ from utils import log as logging from utils import mongoengine_fields from HTMLParser import HTMLParser from vendor.apns import APNs, Payload -from BeautifulSoup import BeautifulSoup +from BeautifulSoup import BeautifulSoup, Tag import types +import urlparse class NotificationFrequency(enum.Enum): immediately = 1 @@ -191,7 +193,7 @@ class MUserFeedNotification(mongo.Document): soup = BeautifulSoup(story['story_content'].strip()) # print story['story_content'] body = replace_with_newlines(soup) - body = truncate_chars(body.strip(), 1600) + body = truncate_chars(body.strip(), 1200) return title, subtitle, body @@ -223,9 +225,10 @@ class MUserFeedNotification(mongo.Document): def send_ios(self, story, user, usersub): if not self.is_ios: return - apns = APNs(use_sandbox=True, - cert_file='/srv/newsblur/config/certificates/aps_development.pem', - key_file='/srv/newsblur/config/certificates/aps_development.pem') + apns = APNs(use_sandbox=False, + cert_file='/srv/newsblur/config/certificates/aps.pem', + key_file='/srv/newsblur/config/certificates/aps.pem', + enhanced=True) tokens = MUserNotificationTokens.get_tokens_for_user(self.user_id) title, subtitle, body = self.title_and_body(story, usersub) @@ -255,9 +258,11 @@ class MUserFeedNotification(mongo.Document): def send_email(self, story, usersub): if not self.is_email: return feed = usersub.feed + story_content = self.sanitize_story(story['story_content']) params = { "story": story, + "story_content": story_content, "feed": feed, "feed_title": usersub.user_title or feed.feed_title, "favicon_border": feed.favicon_color, @@ -278,6 +283,33 @@ class MUserFeedNotification(mongo.Document): logging.user(usersub.user, '~BMStory notification by email error: ~FR%s' % e) logging.user(usersub.user, '~BMStory notification by email: ~FY~SB%s~SN~BM~FY/~SB%s' % (story['story_title'][:50], usersub.feed.feed_title[:50])) + + def sanitize_story(self, story_content): + soup = BeautifulSoup(story_content.strip()) + fqdn = Site.objects.get_current().domain + + for iframe in soup("iframe"): + url = dict(iframe.attrs).get('src', "") + youtube_id = self.extract_youtube_id(url) + if youtube_id: + a = Tag(soup, 'a', [('href', url)]) + img = Tag(soup, 'img', [('style', "display: block; 'background-image': \"url(https://%s/img/reader/youtube_play.png), url(http://img.youtube.com/vi/%s/0.jpg)\"" % (fqdn, youtube_id)), ('src', 'http://img.youtube.com/vi/%s/0.jpg' % youtube_id)]) + a.insert(0, img) + iframe.replaceWith(a) + else: + iframe.extract() + + return unicode(soup) + + def extract_youtube_id(self, url): + youtube_id = None + + if 'youtube.com' in url: + youtube_parts = urlparse.urlparse(url) + if '/embed/' in youtube_parts.path: + youtube_id = youtube_parts.path.replace('/embed/', '') + + return youtube_id def story_score(self, story, classifiers): score = compute_story_score(story, classifier_titles=classifiers.get('titles', []), diff --git a/apps/notifications/views.py b/apps/notifications/views.py index b4786607c..d01f10a6f 100644 --- a/apps/notifications/views.py +++ b/apps/notifications/views.py @@ -22,7 +22,7 @@ def notifications_by_feed(request): def set_notifications_for_feed(request): user = get_user(request) feed_id = request.POST['feed_id'] - notification_types = request.POST.getlist('notification_types') + notification_types = request.POST.getlist('notification_types') or request.POST.getlist('notification_types[]') notification_filter = request.POST.get('notification_filter') try: diff --git a/apps/profile/models.py b/apps/profile/models.py index d03ed3d90..5ab2cf453 100644 --- a/apps/profile/models.py +++ b/apps/profile/models.py @@ -355,7 +355,7 @@ class Profile(models.Model): def cancel_premium(self): paypal_cancel = self.cancel_premium_paypal() stripe_cancel = self.cancel_premium_stripe() - return paypal_cancel or stripe_cancel + return stripe_cancel or paypal_cancel def cancel_premium_paypal(self, second_most_recent_only=False): transactions = PayPalIPN.objects.filter(custom=self.user.username, @@ -408,6 +408,14 @@ class Profile(models.Model): return True + @property + def latest_paypal_email(self): + ipn = PayPalIPN.objects.filter(custom=self.user.username) + if not len(ipn): + return + + return ipn[0].payer_email + @classmethod def clear_dead_spammers(self, days=30, confirm=False): users = User.objects.filter(date_joined__gte=datetime.datetime.now()-datetime.timedelta(days=days)).order_by('-date_joined') @@ -441,7 +449,7 @@ class Profile(models.Model): if verbose: feed = Feed.get_by_id(feed_id) - logging.debug(" ---> [%-30s] ~SN~FBCounting subscribers for feed:~SB~FM%s~SN~FB user:~SB~FM%s" % (feed.title[:30], feed_id, user_id)) + logging.debug(" ---> [%-30s] ~SN~FBCounting subscribers for feed:~SB~FM%s~SN~FB user:~SB~FM%s" % (feed.log_title[:30], feed_id, user_id)) if feed_id: feed_ids = [feed_id] @@ -503,7 +511,7 @@ class Profile(models.Model): r.expire(premium_key, settings.SUBSCRIBER_EXPIRE*24*60*60) logging.info(" ---> [%-30s] ~SN~FBCounting subscribers, storing in ~SBredis~SN: ~FMt:~SB~FM%s~SN a:~SB%s~SN p:~SB%s~SN ap:~SB%s" % - (feed.title[:30], total, active, premium, active_premium)) + (feed.log_title[:30], total, active, premium, active_premium)) @classmethod def count_all_feed_subscribers_for_user(self, user): diff --git a/apps/profile/views.py b/apps/profile/views.py index eaee71299..f48b351cb 100644 --- a/apps/profile/views.py +++ b/apps/profile/views.py @@ -413,6 +413,7 @@ def payment_history(request): "last_seen_ip": user.profile.last_seen_ip, "timezone": unicode(user.profile.timezone), "stripe_id": user.profile.stripe_id, + "paypal_email": user.profile.latest_paypal_email, "profile": user.profile, "feeds": UserSubscription.objects.filter(user=user).count(), "email": user.email, diff --git a/apps/reader/models.py b/apps/reader/models.py index 13cdeebc2..2d7165791 100644 --- a/apps/reader/models.py +++ b/apps/reader/models.py @@ -2,6 +2,7 @@ import datetime import time import re import redis +import pymongo from operator import itemgetter from pprint import pprint from utils import log as logging @@ -753,9 +754,13 @@ class UserSubscription(models.Model): cutoff_date=self.user.profile.unread_cutoff) if not stories: - stories_db = MStory.objects(story_hash__in=unread_story_hashes) - stories = Feed.format_stories(stories_db, self.feed_id) - + try: + stories_db = MStory.objects(story_hash__in=unread_story_hashes) + stories = Feed.format_stories(stories_db, self.feed_id) + except pymongo.errors.OperationFailure, e: + stories_db = MStory.objects(story_hash__in=unread_story_hashes)[:100] + stories = Feed.format_stories(stories_db, self.feed_id) + unread_stories = [] for story in stories: if story['story_date'] < date_delta: diff --git a/apps/reader/urls.py b/apps/reader/urls.py index cfdd7ed75..3b0223c19 100644 --- a/apps/reader/urls.py +++ b/apps/reader/urls.py @@ -58,4 +58,6 @@ urlpatterns = patterns('', url(r'^send_story_email', views.send_story_email, name='send-story-email'), url(r'^retrain_all_sites', views.retrain_all_sites, name='retrain-all-sites'), url(r'^load_tutorial', views.load_tutorial, name='load-tutorial'), + url(r'^save_search', views.save_search, name='save-search'), + url(r'^delete_search', views.delete_search, name='delete-search'), ) diff --git a/apps/reader/views.py b/apps/reader/views.py index 724ff80f7..5faacd81d 100644 --- a/apps/reader/views.py +++ b/apps/reader/views.py @@ -35,7 +35,7 @@ from apps.analyzer.models import get_classifiers_for_user, sort_classifiers_by_f from apps.profile.models import Profile from apps.reader.models import UserSubscription, UserSubscriptionFolders, RUserStory, Feature from apps.reader.forms import SignupForm, LoginForm, FeatureForm -from apps.rss_feeds.models import MFeedIcon, MStarredStoryCounts +from apps.rss_feeds.models import MFeedIcon, MStarredStoryCounts, MSavedSearch from apps.notifications.models import MUserFeedNotification from apps.search.models import MUserSearch from apps.statistics.models import MStatistics @@ -279,6 +279,8 @@ def load_feeds(request): if not starred_count and len(starred_counts): starred_count = MStarredStory.objects(user_id=user.pk).count() + saved_searches = MSavedSearch.user_searches(user.pk) + social_params = { 'user_id': user.pk, 'include_favicon': include_favicons, @@ -306,6 +308,7 @@ def load_feeds(request): 'folders': json.decode(folders.folders), 'starred_count': starred_count, 'starred_counts': starred_counts, + 'saved_searches': saved_searches, 'categories': categories } return data @@ -313,7 +316,7 @@ def load_feeds(request): @json.json_view def load_feed_favicons(request): user = get_user(request) - feed_ids = request.REQUEST.getlist('feed_ids') + feed_ids = request.REQUEST.getlist('feed_ids') or request.REQUEST.getlist('feed_ids[]') if not feed_ids: user_subs = UserSubscription.objects.select_related('feed').filter(user=user, active=True) @@ -328,6 +331,7 @@ def load_feeds_flat(request): include_favicons = is_true(request.REQUEST.get('include_favicons', False)) update_counts = is_true(request.REQUEST.get('update_counts', True)) include_inactive = is_true(request.REQUEST.get('include_inactive', False)) + background_ios = is_true(request.REQUEST.get('background_ios', False)) feeds = {} inactive_feeds = {} @@ -403,8 +407,9 @@ def load_feeds_flat(request): if not user_subs: categories = MCategory.serialize() - logging.user(request, "~FB~SBLoading ~FY%s~FB/~FM%s~FB/~FR%s~FB feeds/socials/inactive ~FMflat~FB%s" % ( - len(feeds.keys()), len(social_feeds), len(inactive_feeds), '. ~FCUpdating counts.' if update_counts else '')) + logging.user(request, "~FB~SBLoading ~FY%s~FB/~FM%s~FB/~FR%s~FB feeds/socials/inactive ~FMflat~FB%s%s" % ( + len(feeds.keys()), len(social_feeds), len(inactive_feeds), '. ~FCUpdating counts.' if update_counts else '', + ' ~BB(background fetch)' if background_ios else '')) data = { "flat_folders": flat_folders, @@ -434,9 +439,9 @@ def load_feeds_flat(request): def refresh_feeds(request): start = datetime.datetime.now() user = get_user(request) - feed_ids = request.REQUEST.getlist('feed_id') + feed_ids = request.REQUEST.getlist('feed_id') or request.REQUEST.getlist('feed_id[]') check_fetch_status = request.REQUEST.get('check_fetch_status') - favicons_fetching = request.REQUEST.getlist('favicons_fetching') + favicons_fetching = request.REQUEST.getlist('favicons_fetching') or request.REQUEST.getlist('favicons_fetching[]') social_feed_ids = [feed_id for feed_id in feed_ids if 'social:' in feed_id] feed_ids = list(set(feed_ids) - set(social_feed_ids)) @@ -514,7 +519,7 @@ def interactions_count(request): @json.json_view def feed_unread_count(request): user = request.user - feed_ids = request.REQUEST.getlist('feed_id') + feed_ids = request.REQUEST.getlist('feed_id') or request.REQUEST.getlist('feed_id[]') force = request.REQUEST.get('force', False) social_feed_ids = [feed_id for feed_id in feed_ids if 'social:' in feed_id] feed_ids = list(set(feed_ids) - set(social_feed_ids)) @@ -766,7 +771,7 @@ def load_single_feed(request, feed_id): # if page <= 3: # import random # # time.sleep(random.randint(2, 7) / 10.0) - # time.sleep(random.randint(10, 14)) + # time.sleep(random.randint(1, 10)) # if page == 2: # assert False @@ -798,7 +803,7 @@ def load_feed_page(request, feed_id): if settings.BACKED_BY_AWS['pages_on_s3'] and feed.s3_page: if settings.PROXY_S3_PAGES: - key = settings.S3_PAGES_BUCKET.get_key(feed.s3_pages_key) + key = settings.S3_CONN.get_bucket(settings.S3_PAGES_BUCKET_NAME).get_key(feed.s3_pages_key) if key: compressed_data = key.get_contents_as_string() response = HttpResponse(compressed_data, mimetype="text/html; charset=utf-8") @@ -832,7 +837,8 @@ def load_starred_stories(request): query = request.REQUEST.get('query', '').strip() order = request.REQUEST.get('order', 'newest') tag = request.REQUEST.get('tag') - story_hashes = request.REQUEST.getlist('h')[:100] + story_hashes = request.REQUEST.getlist('h') or request.REQUEST.getlist('h[]') + story_hashes = story_hashes[:100] version = int(request.REQUEST.get('v', 1)) now = localtime_for_timezone(datetime.datetime.now(), user.profile.timezone) message = None @@ -1216,10 +1222,13 @@ def load_river_stories__redis(request): start = time.time() user = get_user(request) message = None - feed_ids = [int(feed_id) for feed_id in request.REQUEST.getlist('feeds') if feed_id] + feed_ids = request.REQUEST.getlist('feeds') or request.REQUEST.getlist('feeds[]') + feed_ids = [int(feed_id) for feed_id in feed_ids if feed_id] if not feed_ids: + feed_ids = request.REQUEST.getlist('f') or request.REQUEST.getlist('f[]') feed_ids = [int(feed_id) for feed_id in request.REQUEST.getlist('f') if feed_id] - story_hashes = request.REQUEST.getlist('h')[:100] + story_hashes = request.REQUEST.getlist('h') or request.REQUEST.getlist('h[]') + story_hashes = story_hashes[:100] original_feed_ids = list(feed_ids) page = int(request.REQUEST.get('page', 1)) order = request.REQUEST.get('order', 'newest') @@ -1423,7 +1432,8 @@ def load_river_stories__redis(request): @json.json_view def complete_river(request): user = get_user(request) - feed_ids = [int(feed_id) for feed_id in request.POST.getlist('feeds') if feed_id] + feed_ids = request.POST.getlist('feeds') or request.POST.getlist('feeds[]') + feed_ids = [int(feed_id) for feed_id in feed_ids if feed_id] page = int(request.POST.get('page', 1)) read_filter = request.POST.get('read_filter', 'unread') stories_truncated = 0 @@ -1441,7 +1451,8 @@ def complete_river(request): @json.json_view def unread_story_hashes__old(request): user = get_user(request) - feed_ids = [int(feed_id) for feed_id in request.REQUEST.getlist('feed_id') if feed_id] + feed_ids = request.REQUEST.getlist('feed_id') or request.REQUEST.getlist('feed_id[]') + feed_ids = [int(feed_id) for feed_id in feed_ids if feed_id] include_timestamps = is_true(request.REQUEST.get('include_timestamps', False)) usersubs = {} @@ -1480,7 +1491,8 @@ def unread_story_hashes__old(request): @json.json_view def unread_story_hashes(request): user = get_user(request) - feed_ids = [int(feed_id) for feed_id in request.REQUEST.getlist('feed_id') if feed_id] + feed_ids = request.REQUEST.getlist('feed_id') or request.REQUEST.getlist('feed_id[]') + feed_ids = [int(feed_id) for feed_id in feed_ids if feed_id] include_timestamps = is_true(request.REQUEST.get('include_timestamps', False)) order = request.REQUEST.get('order', 'newest') read_filter = request.REQUEST.get('read_filter', 'unread') @@ -1526,7 +1538,7 @@ def mark_all_as_read(request): @ajax_login_required @json.json_view def mark_story_as_read(request): - story_ids = request.REQUEST.getlist('story_id') + story_ids = request.REQUEST.getlist('story_id') or request.REQUEST.getlist('story_id[]') try: feed_id = int(get_argument_or_404(request, 'feed_id')) except ValueError: @@ -1561,7 +1573,7 @@ def mark_story_as_read(request): def mark_story_hashes_as_read(request): r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) try: - story_hashes = request.REQUEST.getlist('story_hash') + story_hashes = request.REQUEST.getlist('story_hash') or request.REQUEST.getlist('story_hash[]') except UnreadablePostError: return dict(code=-1, message="Missing `story_hash` list parameter.") @@ -1766,7 +1778,7 @@ def mark_story_hash_as_unread(request): @json.json_view def mark_feed_as_read(request): r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) - feed_ids = request.REQUEST.getlist('feed_id') + feed_ids = request.POST.getlist('feed_id') or request.POST.getlist('feed_id[]') cutoff_timestamp = int(request.REQUEST.get('cutoff_timestamp', 0)) direction = request.REQUEST.get('direction', 'older') multiple = len(feed_ids) > 1 @@ -1959,7 +1971,8 @@ def delete_feed_by_url(request): def delete_folder(request): folder_to_delete = request.POST.get('folder_name') or request.POST.get('folder_to_delete') in_folder = request.POST.get('in_folder', None) - feed_ids_in_folder = [int(f) for f in request.REQUEST.getlist('feed_id') if f] + feed_ids_in_folder = request.REQUEST.getlist('feed_id') or request.REQUEST.getlist('feed_id[]') + feed_ids_in_folder = [int(f) for f in feed_ids_in_folder if f] request.user.profile.send_opml_export_email(reason="You have deleted an entire folder of feeds, so here's a backup of all of your subscriptions just in case.") @@ -2033,8 +2046,8 @@ def rename_folder(request): @json.json_view def move_feed_to_folders(request): feed_id = int(request.POST['feed_id']) - in_folders = request.POST.getlist('in_folders', '') - to_folders = request.POST.getlist('to_folders', '') + in_folders = request.POST.getlist('in_folders', '') or request.POST.getlist('in_folders[]', '') + to_folders = request.POST.getlist('to_folders', '') or request.POST.getlist('to_folders[]', '') user_sub_folders = get_object_or_404(UserSubscriptionFolders, user=request.user) user_sub_folders = user_sub_folders.move_feed_to_folders(feed_id, in_folders=in_folders, @@ -2176,7 +2189,8 @@ def feeds_trainer(request): @json.json_view def save_feed_chooser(request): is_premium = request.user.profile.is_premium - approved_feeds = [int(feed_id) for feed_id in request.POST.getlist('approved_feeds') if feed_id] + approved_feeds = request.POST.getlist('approved_feeds') or request.POST.getlist('approved_feeds[]') + approved_feeds = [int(feed_id) for feed_id in approved_feeds if feed_id] approve_all = False if not is_premium: approved_feeds = approved_feeds[:64] @@ -2274,7 +2288,7 @@ def _mark_story_as_starred(request): feed_id = int(request.REQUEST.get('feed_id', 0)) story_id = request.REQUEST.get('story_id', None) story_hash = request.REQUEST.get('story_hash', None) - user_tags = request.REQUEST.getlist('user_tags') + user_tags = request.REQUEST.getlist('user_tags') or request.REQUEST.getlist('user_tags[]') message = "" if story_hash: story, _ = MStory.find_story(story_hash=story_hash) @@ -2480,3 +2494,31 @@ def load_tutorial(request): return { 'newsblur_feed': newsblur_feed.canonical() } + +@required_params('query', 'feed_id') +@json.json_view +def save_search(request): + feed_id = request.POST['feed_id'] + query = request.POST['query'] + + MSavedSearch.save_search(user_id=request.user.pk, feed_id=feed_id, query=query) + + saved_searches = MSavedSearch.user_searches(request.user.pk) + + return { + 'saved_searches': saved_searches, + } + +@required_params('query', 'feed_id') +@json.json_view +def delete_search(request): + feed_id = request.POST['feed_id'] + query = request.POST['query'] + + MSavedSearch.delete_search(user_id=request.user.pk, feed_id=feed_id, query=query) + + saved_searches = MSavedSearch.user_searches(request.user.pk) + + return { + 'saved_searches': saved_searches, + } diff --git a/apps/rss_feeds/icon_importer.py b/apps/rss_feeds/icon_importer.py index d176d5526..d134d3390 100644 --- a/apps/rss_feeds/icon_importer.py +++ b/apps/rss_feeds/icon_importer.py @@ -67,8 +67,8 @@ class IconImporter(object): self.feed_icon.icon_url != icon_url or self.feed_icon.not_found or (settings.BACKED_BY_AWS.get('icons_on_s3') and not self.feed.s3_icon))): - logging.debug(" ---> [%-30s] ~SN~FBIcon difference:~FY color:%s (%s/%s) data:%s url:%s notfound:%s no-s3:%s" % ( - self.feed, + logging.debug(" ---> [%-30s] ~SN~FBIcon difference:~FY color:%s (%s/%s) data:%s url:%s notfound:%s no-s3:%s" % ( + self.feed.log_title[:30], self.feed_icon.color != color, self.feed_icon.color, color, self.feed_icon.data != image_str, self.feed_icon.icon_url != icon_url, @@ -97,7 +97,7 @@ class IconImporter(object): def save_to_s3(self, image_str): expires = datetime.datetime.now() + datetime.timedelta(days=60) expires = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") - k = Key(settings.S3_ICONS_BUCKET) + k = Key(settings.S3_CONN.get_bucket(settings.S3_ICONS_BUCKET_NAME)) k.key = self.feed.s3_icons_key k.set_metadata('Content-Type', 'image/png') k.set_metadata('Expires', expires) @@ -195,7 +195,7 @@ class IconImporter(object): if self.page_data: content = self.page_data elif settings.BACKED_BY_AWS.get('pages_on_s3') and self.feed.s3_page: - key = settings.S3_PAGES_BUCKET.get_key(self.feed.s3_pages_key) + key = settings.S3_CONN.get_bucket(settings.S3_PAGES_BUCKET_NAME).get_key(self.feed.s3_pages_key) compressed_content = key.get_contents_as_string() stream = StringIO(compressed_content) gz = gzip.GzipFile(fileobj=stream) @@ -237,7 +237,10 @@ class IconImporter(object): if not force: url = self.feed_icon.icon_url if not url and self.feed.feed_link and len(self.feed.feed_link) > 6: - url = urlparse.urljoin(self.feed.feed_link, 'favicon.ico') + try: + url = urlparse.urljoin(self.feed.feed_link, 'favicon.ico') + except ValueError: + url = None if not url: return None, None, None diff --git a/apps/rss_feeds/models.py b/apps/rss_feeds/models.py index a94884ebb..dc08b11a3 100644 --- a/apps/rss_feeds/models.py +++ b/apps/rss_feeds/models.py @@ -95,13 +95,14 @@ class Feed(models.Model): if not self.feed_title: self.feed_title = "[Untitled]" self.save() - return "%s (%s - %s/%s/%s)%s" % ( - self.feed_title, + return "%s%s: %s - %s/%s/%s" % ( self.pk, + (" [B: %s]" % self.branch_from_feed.pk if self.branch_from_feed else ""), + self.feed_title, self.num_subscribers, self.active_subscribers, self.active_premium_subscribers, - (" [B: %s]" % self.branch_from_feed.pk if self.branch_from_feed else "")) + ) @property def title(self): @@ -110,6 +111,10 @@ class Feed(models.Model): title = "%s*" % title[:29] return title + @property + def log_title(self): + return self.__unicode__() + @property def permalink(self): return "%s/site/%s/%s" % (settings.NEWSBLUR_URL, self.pk, slugify(self.feed_title.lower()[:50])) @@ -246,7 +251,7 @@ class Feed(models.Model): if not duplicate_feeds: # Feed has been deleted. Just ignore it. logging.debug(" ***> Changed to: %s - %s: %s" % (self.feed_address, self.feed_link, duplicate_feeds)) - logging.debug(' ***> [%-30s] Feed deleted (%s).' % (unicode(self)[:30], self.pk)) + logging.debug(' ***> [%-30s] Feed deleted (%s).' % (self.log_title[:30], self.pk)) return for duplicate_feed in duplicate_feeds: @@ -602,17 +607,18 @@ class Feed(models.Model): try: feed_address, feed = _1() except TimeoutError, e: - logging.debug(' ---> [%-30s] Feed address check timed out...' % (unicode(self)[:30])) + logging.debug(' ---> [%-30s] Feed address check timed out...' % (self.log_title[:30])) self.save_feed_history(505, 'Timeout', e) feed = self feed_address = None return bool(feed_address), feed - def save_feed_history(self, status_code, message, exception=None): + def save_feed_history(self, status_code, message, exception=None, date=None): fetch_history = MFetchHistory.add(feed_id=self.pk, fetch_type='feed', code=int(status_code), + date=date, message=message, exception=exception) @@ -626,10 +632,11 @@ class Feed(models.Model): self.active = True self.save() - def save_page_history(self, status_code, message, exception=None): + def save_page_history(self, status_code, message, exception=None, date=None): fetch_history = MFetchHistory.add(feed_id=self.pk, fetch_type='page', code=int(status_code), + date=date, message=message, exception=exception) @@ -640,6 +647,13 @@ class Feed(models.Model): self.has_page = True self.active = True self.save() + + def save_raw_feed(self, raw_feed, fetch_date): + MFetchHistory.add(feed_id=self.pk, + fetch_type='raw_feed', + code=200, + message=raw_feed, + date=fetch_date) def count_errors_in_history(self, exception_type='feed', status_code=None, fetch_history=None): if not fetch_history: @@ -667,12 +681,12 @@ class Feed(models.Model): self.save() logging.debug(' ---> [%-30s] ~FBCounting any errors in history: %s (%s non errors)' % - (unicode(self)[:30], len(errors), len(non_errors))) + (self.log_title[:30], len(errors), len(non_errors))) return errors, non_errors def count_redirects_in_history(self, fetch_type='feed', fetch_history=None): - logging.debug(' ---> [%-30s] Counting redirects in history...' % (unicode(self)[:30])) + logging.debug(' ---> [%-30s] Counting redirects in history...' % (self.log_title[:30])) if not fetch_history: fetch_history = MFetchHistory.feed(self.pk) fh = fetch_history[fetch_type+'_fetch_history'] @@ -703,7 +717,7 @@ class Feed(models.Model): return True elif last_recount: logging.info(" ---> [%-30s] ~SN~FBFeed has expired redis subscriber counts (%s < %s), clearing..." % ( - unicode(self)[:30], last_recount, subscriber_expire)) + self.log_title[:30], last_recount, subscriber_expire)) r.delete(total_key, -1) r.delete(premium_key, -1) @@ -753,7 +767,7 @@ class Feed(models.Model): original_premium_subscribers = self.premium_subscribers original_active_premium_subscribers = self.active_premium_subscribers logging.info(" ---> [%-30s] ~SN~FBCounting subscribers from ~FCredis~FB: ~FMt:~SB~FM%s~SN a:~SB%s~SN p:~SB%s~SN ap:~SB%s ~SN~FC%s" % - (self.title[:30], total, active, premium, active_premium, "(%s branches)" % (len(feed_ids)-1) if len(feed_ids)>1 else "")) + (self.log_title[:30], total, active, premium, active_premium, "(%s branches)" % (len(feed_ids)-1) if len(feed_ids)>1 else "")) else: from apps.reader.models import UserSubscription @@ -786,7 +800,7 @@ class Feed(models.Model): original_active_premium_subscribers = self.active_premium_subscribers active_premium = active_premium_subscribers.count() logging.debug(" ---> [%-30s] ~SN~FBCounting subscribers from ~FYpostgres~FB: ~FMt:~SB~FM%s~SN a:~SB%s~SN p:~SB%s~SN ap:~SB%s" % - (self.title[:30], total, active, premium, active_premium)) + (self.log_title[:30], total, active, premium, active_premium)) # If any counts have changed, save them self.num_subscribers = total @@ -1148,7 +1162,7 @@ class Feed(models.Model): if settings.DEBUG or verbose: logging.debug(" ---> [%-30s] ~FBChecking ~SB%s~SN new/updated against ~SB%s~SN stories" % ( - self.title[:30], + self.log_title[:30], len(stories), len(existing_stories.keys()))) @timelimit(2) @@ -1160,7 +1174,7 @@ class Feed(models.Model): for story in stories: if verbose: logging.debug(" ---> [%-30s] ~FBChecking ~SB%s~SN / ~SB%s" % ( - self.title[:30], + self.log_title[:30], story.get('title'), story.get('guid'))) if not story.get('title'): @@ -1179,7 +1193,7 @@ class Feed(models.Model): existing_story, story_has_changed = _1(story, story_content, existing_stories, new_story_hashes) except TimeoutError, e: - logging.debug(' ---> [%-30s] ~SB~FRExisting story check timed out...' % (unicode(self)[:30])) + logging.debug(' ---> [%-30s] ~SB~FRExisting story check timed out...' % (self.log_title[:30])) existing_story = None story_has_changed = False @@ -1424,9 +1438,9 @@ class Feed(models.Model): original_cutoff = cutoff cutoff = min(cutoff, 10) try: - logging.debug(" ---> [%-30s] ~FBTrimming down to ~SB%s (instead of %s)~SN stories (~FM%s~FB)" % (self, cutoff, original_cutoff, self.last_story_date.strftime("%Y-%m-%d") if self.last_story_date else "No last story date")) + logging.debug(" ---> [%-30s] ~FBTrimming down to ~SB%s (instead of %s)~SN stories (~FM%s~FB)" % (self.log_title[:30], cutoff, original_cutoff, self.last_story_date.strftime("%Y-%m-%d") if self.last_story_date else "No last story date")) except ValueError, e: - logging.debug(" ***> [%-30s] Error trimming: %s" % (self, e)) + logging.debug(" ***> [%-30s] Error trimming: %s" % (self.log_title[:30], e)) pass return cutoff @@ -2035,7 +2049,7 @@ class Feed(models.Model): if verbose: logging.debug(" ---> [%-30s] Fetched every %s min - Subs: %s/%s/%s Stories/day: %s" % ( - unicode(self)[:30], total, + self.log_title[:30], total, self.num_subscribers, self.active_subscribers, self.active_premium_subscribers, @@ -2053,7 +2067,7 @@ class Feed(models.Model): if verbose: logging.debug(' ---> [%-30s] ~FBScheduling feed fetch geometrically: ' '~SB%s errors. Time: %s min' % ( - unicode(self)[:30], self.errors_since_good, total)) + self.log_title[:30], self.errors_since_good, total)) random_factor = random.randint(0, total) / 4 next_scheduled_update = datetime.datetime.utcnow() + datetime.timedelta( @@ -2085,11 +2099,11 @@ class Feed(models.Model): def schedule_feed_fetch_immediately(self, verbose=True): r = redis.Redis(connection_pool=settings.REDIS_FEED_UPDATE_POOL) if not self.num_subscribers: - logging.debug(' ---> [%-30s] Not scheduling feed fetch immediately, no subs.' % (unicode(self)[:30])) + logging.debug(' ---> [%-30s] Not scheduling feed fetch immediately, no subs.' % (self.log_title[:30])) return if verbose: - logging.debug(' ---> [%-30s] Scheduling feed fetch immediately...' % (unicode(self)[:30])) + logging.debug(' ---> [%-30s] Scheduling feed fetch immediately...' % (self.log_title[:30])) self.next_scheduled_update = datetime.datetime.utcnow() r.zadd('scheduled_updates', self.pk, self.next_scheduled_update.strftime('%s')) @@ -2116,7 +2130,7 @@ class Feed(models.Model): if queue_size > 1000: self.schedule_feed_fetch_immediately() else: - logging.debug(' ---> [%-30s] [%s] ~FB~SBQueuing pushed stories, last pushed %s...' % (unicode(self)[:30], self.pk, latest_push_date_delta)) + logging.debug(' ---> [%-30s] [%s] ~FB~SBQueuing pushed stories, last pushed %s...' % (self.log_title[:30], self.pk, latest_push_date_delta)) self.set_next_scheduled_update() PushFeeds.apply_async(args=(self.pk, xml), queue='push_feeds') @@ -2575,7 +2589,7 @@ class MStory(mongo.Document): r.delete('zF:%s' % story_feed_id) # r2.delete('zF:%s' % story_feed_id) - logging.info(" ---> [%-30s] ~FMSyncing ~SB%s~SN stories to redis" % (feed and feed.title[:30] or story_feed_id, stories.count())) + logging.info(" ---> [%-30s] ~FMSyncing ~SB%s~SN stories to redis" % (feed and feed.log_title[:30] or story_feed_id, stories.count())) p = r.pipeline() # p2 = r2.pipeline() for story in stories: @@ -2597,24 +2611,35 @@ class MStory(mongo.Document): self.share_user_ids = [s['user_id'] for s in shares] self.save() - def extract_image_urls(self, force=False): - if self.image_urls and not force: + def extract_image_urls(self, force=False, text=False): + if self.image_urls and not force and not text: return self.image_urls - story_content = self.story_content - if not story_content and self.story_content_z: - story_content = zlib.decompress(self.story_content_z) + story_content = None + if not text: + story_content = self.story_content + if not story_content and self.story_content_z: + story_content = zlib.decompress(self.story_content_z) + elif text: + if self.original_text_z: + story_content = zlib.decompress(self.original_text_z) if not story_content: return try: soup = BeautifulSoup(story_content) except ValueError: - return - + if not text: + return self.extract_image_urls(force=force, text=True) + else: + return + images = soup.findAll('img') if not images: - return + if not text: + return self.extract_image_urls(force=force, text=True) + else: + return image_urls = [] for image in images: @@ -2626,8 +2651,11 @@ class MStory(mongo.Document): image_urls.append(image_url) if not image_urls: - return - + if not text: + return self.extract_image_urls(force=force, text=True) + else: + return + self.image_urls = image_urls return self.image_urls @@ -2638,6 +2666,8 @@ class MStory(mongo.Document): feed = Feed.get_by_id(self.story_feed_id) ti = TextImporter(self, feed=feed, request=request, debug=debug) original_text = ti.fetch() + self.extract_image_urls(force=force, text=True) + self.save() else: logging.user(request, "~FYFetching ~FGoriginal~FY story text, ~SBfound.") original_text = zlib.decompress(original_text_z) @@ -2921,12 +2951,81 @@ class MStarredStoryCounts(mongo.Document): if story_count and story_count.count <= 0: story_count.delete() +class MSavedSearch(mongo.Document): + user_id = mongo.IntField() + query = mongo.StringField(max_length=1024) + feed_id = mongo.StringField() + slug = mongo.StringField(max_length=128) + meta = { + 'collection': 'saved_searches', + 'indexes': ['user_id', + {'fields': ['user_id', 'feed_id', 'query'], + 'unique': True, + 'types': False, }], + 'ordering': ['query'], + 'allow_inheritance': False, + } + + @property + def rss_url(self, secret_token=None): + if not secret_token: + user = User.objects.select_related('profile').get(pk=self.user_id) + secret_token = user.profile.secret_token + + slug = self.slug if self.slug else "" + return "%s/reader/saved_search/%s/%s/%s" % (settings.NEWSBLUR_URL, self.user_id, + secret_token, slug) + + @classmethod + def user_searches(cls, user_id): + searches = cls.objects.filter(user_id=user_id) + searches = sorted([{'query': s.query, + 'feed_address': s.rss_url, + 'feed_id': s.feed_id, + } for s in searches], + key=lambda x: (x.get('query', '') or '').lower()) + return searches + + @classmethod + def save_search(cls, user_id, feed_id, query): + user = User.objects.get(pk=user_id) + params = dict(user_id=user_id, + feed_id=feed_id, + query=query, + slug=slugify(query)) + try: + saved_search = cls.objects.get(**params) + logging.user(user, "~FRSaved search already exists: ~SB%s" % query) + except cls.DoesNotExist: + logging.user(user, "~FCCreating a saved search: ~SB%s~SN/~SB%s" % (feed_id, query)) + saved_search = cls.objects.create(**params) + + return saved_search + + @classmethod + def delete_search(cls, user_id, feed_id, query): + user = User.objects.get(pk=user_id) + params = dict(user_id=user_id, + feed_id=feed_id, + query=query) + try: + saved_search = cls.objects.get(**params) + logging.user(user, "~FCDeleting saved search: ~SB%s" % query) + saved_search.delete() + except cls.DoesNotExist: + logging.user(user, "~FRCan't delete saved search, missing: ~SB%s~SN/~SB%s" % (feed_id, query)) + except cls.MultipleObjectsReturned: + logging.user(user, "~FRFound multiple saved searches, deleting: ~SB%s~SN/~SB%s" % (feed_id, query)) + cls.objects(**params).delete() + + class MFetchHistory(mongo.Document): feed_id = mongo.IntField(unique=True) feed_fetch_history = mongo.DynamicField() page_fetch_history = mongo.DynamicField() push_history = mongo.DynamicField() + raw_feed_history = mongo.DynamicField() meta = { 'db_alias': 'nbanalytics', @@ -2974,11 +3073,15 @@ class MFetchHistory(mongo.Document): history = fetch_history.page_fetch_history or [] elif fetch_type == 'push': history = fetch_history.push_history or [] + elif fetch_type == 'raw_feed': + history = fetch_history.raw_feed_history or [] history = [[date, code, message]] + history any_exceptions = any([c for d, c, m in history if c not in [200, 304]]) if any_exceptions: history = history[:25] + elif fetch_type == 'raw_feed': + history = history[:10] else: history = history[:5] @@ -2988,6 +3091,8 @@ class MFetchHistory(mongo.Document): fetch_history.page_fetch_history = history elif fetch_type == 'push': fetch_history.push_history = history + elif fetch_type == 'raw_feed': + fetch_history.raw_feed_history = history fetch_history.save() diff --git a/apps/rss_feeds/page_importer.py b/apps/rss_feeds/page_importer.py index 5b50328df..eb080258b 100644 --- a/apps/rss_feeds/page_importer.py +++ b/apps/rss_feeds/page_importer.py @@ -63,7 +63,7 @@ class PageImporter(object): try: self.fetch_page_timeout(urllib_fallback=urllib_fallback, requests_exception=requests_exception) except TimeoutError: - logging.user(self.request, ' ***> [%-30s] ~FBPage fetch ~SN~FRfailed~FB due to timeout' % (self.feed)) + logging.user(self.request, ' ***> [%-30s] ~FBPage fetch ~SN~FRfailed~FB due to timeout' % (self.feed.log_title[:30])) @timelimit(10) def fetch_page_timeout(self, urllib_fallback=False, requests_exception=None): @@ -95,7 +95,7 @@ class PageImporter(object): except requests.exceptions.TooManyRedirects: response = requests.get(feed_link) except (AttributeError, SocketError, OpenSSLError, PyAsn1Error, TypeError), e: - logging.debug(' ***> [%-30s] Page fetch failed using requests: %s' % (self.feed, e)) + logging.debug(' ***> [%-30s] Page fetch failed using requests: %s' % (self.feed.log_title[:30], e)) self.save_no_page() return # try: @@ -127,14 +127,14 @@ class PageImporter(object): fp = feedparser.parse(self.feed.feed_address) feed_link = fp.feed.get('link', "") self.feed.save() - logging.debug(' ***> [%-30s] Page fetch failed: %s' % (self.feed, e)) + logging.debug(' ***> [%-30s] Page fetch failed: %s' % (self.feed.log_title[:30], e)) except (urllib2.HTTPError), e: self.feed.save_page_history(e.code, e.msg, e.fp.read()) except (httplib.IncompleteRead), e: self.feed.save_page_history(500, "IncompleteRead", e) except (requests.exceptions.RequestException, requests.packages.urllib3.exceptions.HTTPError), e: - logging.debug(' ***> [%-30s] Page fetch failed using requests: %s' % (self.feed, e)) + logging.debug(' ***> [%-30s] Page fetch failed using requests: %s' % (self.feed.log_title[:30], e)) # mail_feed_error_to_admin(self.feed, e, local_vars=locals()) return self.fetch_page(urllib_fallback=True, requests_exception=e) except Exception, e: @@ -183,7 +183,7 @@ class PageImporter(object): except requests.exceptions.TooManyRedirects: response = requests.get(story_permalink) except (AttributeError, SocketError, OpenSSLError, PyAsn1Error, requests.exceptions.ConnectionError), e: - logging.debug(' ***> [%-30s] Original story fetch failed using requests: %s' % (self.feed, e)) + logging.debug(' ***> [%-30s] Original story fetch failed using requests: %s' % (self.feed.log_title[:30], e)) return try: data = response.text @@ -213,7 +213,7 @@ class PageImporter(object): def save_no_page(self): - logging.debug(' ---> [%-30s] ~FYNo original page: %s' % (self.feed, self.feed.feed_link)) + logging.debug(' ---> [%-30s] ~FYNo original page: %s' % (self.feed.log_title[:30], self.feed.feed_link)) self.feed.has_page = False self.feed.save() self.feed.save_page_history(404, "Feed has no original page.") @@ -275,7 +275,7 @@ class PageImporter(object): feed_page = MFeedPage.objects.get(feed_id=self.feed.pk) # feed_page.page_data = html.encode('utf-8') if feed_page.page() == html: - logging.debug(' ---> [%-30s] ~FYNo change in page data: %s' % (self.feed.title[:30], self.feed.feed_link)) + logging.debug(' ---> [%-30s] ~FYNo change in page data: %s' % (self.feed.log_title[:30], self.feed.feed_link)) else: feed_page.page_data = html feed_page.save() @@ -296,7 +296,7 @@ class PageImporter(object): return True def save_page_s3(self, html): - k = Key(settings.S3_PAGES_BUCKET) + k = Key(settings.S3_CONN.get_bucket(settings.S3_PAGES_BUCKET_NAME)) k.key = self.feed.s3_pages_key k.set_metadata('Content-Encoding', 'gzip') k.set_metadata('Content-Type', 'text/html') @@ -307,7 +307,7 @@ class PageImporter(object): try: feed_page = MFeedPage.objects.get(feed_id=self.feed.pk) feed_page.delete() - logging.debug(' ---> [%-30s] ~FYTransfering page data to S3...' % (self.feed)) + logging.debug(' ---> [%-30s] ~FYTransfering page data to S3...' % (self.feed.log_title[:30])) except MFeedPage.DoesNotExist: pass @@ -318,7 +318,7 @@ class PageImporter(object): return True def delete_page_s3(self): - k = Key(settings.S3_PAGES_BUCKET) + k = Key(settings.S3_CONN.get_bucket(settings.S3_PAGES_BUCKET_NAME)) k.key = self.feed.s3_pages_key k.delete() diff --git a/apps/rss_feeds/text_importer.py b/apps/rss_feeds/text_importer.py index 152bbc73e..051a3bcd9 100644 --- a/apps/rss_feeds/text_importer.py +++ b/apps/rss_feeds/text_importer.py @@ -10,6 +10,7 @@ from utils.feed_functions import timelimit, TimeoutError from OpenSSL.SSL import Error as OpenSSLError from pyasn1.error import PyAsn1Error from django.utils.encoding import smart_str +from BeautifulSoup import BeautifulSoup BROKEN_URLS = [ "gamespot.com", @@ -87,13 +88,17 @@ class TextImporter: except TypeError: title = "" url = resp.url - + + if content: + content = self.rewrite_content(content) + if content: if self.story and not skip_save: self.story.original_text_z = zlib.compress(smart_str(content)) try: self.story.save() - except NotUniqueError: + except NotUniqueError, e: + logging.user(self.request, ("~SN~FYFetched ~FGoriginal text~FY: %s" % (e)), warn_color=False) pass logging.user(self.request, ("~SN~FYFetched ~FGoriginal text~FY: now ~SB%s bytes~SN vs. was ~SB%s bytes" % ( len(content), @@ -109,6 +114,15 @@ class TextImporter: return content + def rewrite_content(self, content): + soup = BeautifulSoup(content) + + for noscript in soup.findAll('noscript'): + if len(noscript.contents) > 0: + noscript.replaceWith(noscript.contents[0]) + + return unicode(soup) + @timelimit(10) def fetch_request(self): url = self.story_url diff --git a/apps/rss_feeds/views.py b/apps/rss_feeds/views.py index 05eca1cf5..b92d4de6a 100644 --- a/apps/rss_feeds/views.py +++ b/apps/rss_feeds/views.py @@ -489,7 +489,6 @@ def status(request): 'feeds': feeds }, context_instance=RequestContext(request)) -@required_params('story_id', feed_id=int) @json.json_view def original_text(request): story_id = request.REQUEST.get('story_id') @@ -510,8 +509,9 @@ def original_text(request): original_text = story.fetch_original_text(force=force, request=request, debug=debug) return { - 'feed_id': feed_id, - 'story_id': story_id, + 'feed_id': story.story_feed_id, + 'story_hash': story.story_hash, + 'story_id': story.story_guid, 'original_text': original_text, 'failed': not original_text or len(original_text) < 100, } diff --git a/apps/social/views.py b/apps/social/views.py index 5c26448a9..e1b4d208e 100644 --- a/apps/social/views.py +++ b/apps/social/views.py @@ -189,7 +189,8 @@ def load_river_blurblog(request): limit = 10 start = time.time() user = get_user(request) - social_user_ids = [int(uid) for uid in request.REQUEST.getlist('social_user_ids') if uid] + social_user_ids = request.REQUEST.getlist('social_user_ids') or request.REQUEST.getlist('social_user_ids[]') + social_user_ids = [int(uid) for uid in social_user_ids if uid] original_user_ids = list(social_user_ids) page = int(request.REQUEST.get('page', 1)) order = request.REQUEST.get('order', 'newest') @@ -546,7 +547,7 @@ def mark_story_as_shared(request): comments = request.POST.get('comments', '') source_user_id = request.POST.get('source_user_id') relative_user_id = request.POST.get('relative_user_id') or request.user.pk - post_to_services = request.POST.getlist('post_to_services') + post_to_services = request.POST.getlist('post_to_services') or request.POST.getlist('post_to_services[]') format = request.REQUEST.get('format', 'json') now = datetime.datetime.now() nowtz = localtime_for_timezone(now, request.user.profile.timezone) @@ -574,7 +575,7 @@ def mark_story_as_shared(request): 'message': 'Only premium users can share multiple stories per day from the same site.' }) - quota = 50 + quota = 100 if not request.user.profile.is_premium: quota = 3 if MSharedStory.feed_quota(request.user.pk, story.story_hash, quota=quota): @@ -909,7 +910,7 @@ def shared_stories_public(request, username): def profile(request): user = get_user(request.user) user_id = int(request.GET.get('user_id', user.pk)) - categories = request.GET.getlist('category') + categories = request.GET.getlist('category') or request.GET.getlist('category[]') include_activities_html = request.REQUEST.get('include_activities_html', None) user_profile = MSocialProfile.get_user(user_id) @@ -1422,7 +1423,7 @@ def load_social_settings(request, social_user_id, username=None): @ajax_login_required def load_interactions(request): user_id = request.REQUEST.get('user_id', None) - categories = request.GET.getlist('category') + categories = request.GET.getlist('category') or request.GET.getlist('category[]') if not user_id or 'null' in user_id: user_id = get_user(request).pk page = max(1, int(request.REQUEST.get('page', 1))) @@ -1448,7 +1449,7 @@ def load_interactions(request): @ajax_login_required def load_activities(request): user_id = request.REQUEST.get('user_id', None) - categories = request.GET.getlist('category') + categories = request.GET.getlist('category') or request.GET.getlist('category[]') if user_id and 'null' not in user_id: user_id = int(user_id) user = User.objects.get(pk=user_id) diff --git a/clients/android/NewsBlur/AndroidManifest.xml b/clients/android/NewsBlur/AndroidManifest.xml index 0a4075291..a9c405929 100644 --- a/clients/android/NewsBlur/AndroidManifest.xml +++ b/clients/android/NewsBlur/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="136" + android:versionName="5.2.0b1" > http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 21/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import -#import -@class ASIHTTPRequest; - -typedef enum _ASIAuthenticationType { - ASIStandardAuthenticationType = 0, - ASIProxyAuthenticationType = 1 -} ASIAuthenticationType; - -@interface ASIAutorotatingViewController : UIViewController -@end - -@interface ASIAuthenticationDialog : ASIAutorotatingViewController { - ASIHTTPRequest *request; - ASIAuthenticationType type; - UITableView *tableView; - UIViewController *presentingController; - BOOL didEnableRotationNotifications; -} -+ (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request; -+ (void)dismiss; - -@property (atomic, retain) ASIHTTPRequest *request; -@property (atomic, assign) ASIAuthenticationType type; -@property (atomic, assign) BOOL didEnableRotationNotifications; -@property (retain, nonatomic) UIViewController *presentingController; -@end diff --git a/clients/ios/ASI/ASIAuthenticationDialog.m b/clients/ios/ASI/ASIAuthenticationDialog.m deleted file mode 100755 index d52e6217e..000000000 --- a/clients/ios/ASI/ASIAuthenticationDialog.m +++ /dev/null @@ -1,521 +0,0 @@ -// -// ASIAuthenticationDialog.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 21/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASIAuthenticationDialog.h" -#import "ASIHTTPRequest.h" -#import - -static ASIAuthenticationDialog *sharedDialog = nil; -BOOL isDismissing = NO; -static NSMutableArray *requestsNeedingAuthentication = nil; - -static const NSUInteger kUsernameRow = 0; -static const NSUInteger kUsernameSection = 0; -static const NSUInteger kPasswordRow = 1; -static const NSUInteger kPasswordSection = 0; -static const NSUInteger kDomainRow = 0; -static const NSUInteger kDomainSection = 1; - - -@implementation ASIAutorotatingViewController - -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation -{ - return YES; -} - -@end - - -@interface ASIAuthenticationDialog () -- (void)showTitle; -- (void)show; -- (NSArray *)requestsRequiringTheseCredentials; -- (void)presentNextDialog; -- (void)keyboardWillShow:(NSNotification *)notification; -- (void)orientationChanged:(NSNotification *)notification; -- (void)cancelAuthenticationFromDialog:(id)sender; -- (void)loginWithCredentialsFromDialog:(id)sender; -@property (atomic, retain) UITableView *tableView; -@end - -@implementation ASIAuthenticationDialog - -#pragma mark init / dealloc - -+ (void)initialize -{ - if (self == [ASIAuthenticationDialog class]) { - requestsNeedingAuthentication = [[NSMutableArray array] retain]; - } -} - -+ (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)theRequest -{ - // No need for a lock here, this will always be called on the main thread - if (!sharedDialog) { - sharedDialog = [[self alloc] init]; - [sharedDialog setRequest:theRequest]; - if ([theRequest authenticationNeeded] == ASIProxyAuthenticationNeeded) { - [sharedDialog setType:ASIProxyAuthenticationType]; - } else { - [sharedDialog setType:ASIStandardAuthenticationType]; - } - [sharedDialog show]; - } else { - [requestsNeedingAuthentication addObject:theRequest]; - } -} - -- (id)init -{ - if ((self = [self initWithNibName:nil bundle:nil])) { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { -#endif - if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) { - [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; - [self setDidEnableRotationNotifications:YES]; - } - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - } -#endif - } - return self; -} - -- (void)dealloc -{ - if ([self didEnableRotationNotifications]) { - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; - } - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; - - [request release]; - [tableView release]; - [presentingController.view removeFromSuperview]; - [presentingController release]; - [super dealloc]; -} - -#pragma mark keyboard notifications - -- (void)keyboardWillShow:(NSNotification *)notification -{ -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { -#endif -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2 - NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; -#else - NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]; -#endif - CGRect keyboardBounds; - [keyboardBoundsValue getValue:&keyboardBounds]; - UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); - [[self tableView] setScrollIndicatorInsets:e]; - [[self tableView] setContentInset:e]; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - } -#endif -} - -// Manually handles orientation changes on iPhone -- (void)orientationChanged:(NSNotification *)notification -{ - [self showTitle]; - - UIInterfaceOrientation o = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation]; - CGFloat angle = 0; - switch (o) { - case UIDeviceOrientationLandscapeLeft: angle = 90; break; - case UIDeviceOrientationLandscapeRight: angle = -90; break; - case UIDeviceOrientationPortraitUpsideDown: angle = 180; break; - default: break; - } - - CGRect f = [[UIScreen mainScreen] applicationFrame]; - - // Swap the frame height and width if necessary - if (UIInterfaceOrientationIsLandscape(o)) { - CGFloat t; - t = f.size.width; - f.size.width = f.size.height; - f.size.height = t; - } - - CGAffineTransform previousTransform = self.view.layer.affineTransform; - CGAffineTransform newTransform = CGAffineTransformMakeRotation((CGFloat)(angle * M_PI / 180.0)); - - // Reset the transform so we can set the size - self.view.layer.affineTransform = CGAffineTransformIdentity; - self.view.frame = (CGRect){ { 0, 0 }, f.size}; - - // Revert to the previous transform for correct animation - self.view.layer.affineTransform = previousTransform; - - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationDuration:0.3]; - - // Set the new transform - self.view.layer.affineTransform = newTransform; - - // Fix the view origin - self.view.frame = (CGRect){ { f.origin.x, f.origin.y },self.view.frame.size}; - [UIView commitAnimations]; -} - -#pragma mark utilities - -- (UIViewController *)presentingController -{ - if (!presentingController) { - presentingController = [[ASIAutorotatingViewController alloc] initWithNibName:nil bundle:nil]; - - // Attach to the window, but don't interfere. - UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; - [window addSubview:[presentingController view]]; - [[presentingController view] setFrame:CGRectZero]; - [[presentingController view] setUserInteractionEnabled:NO]; - } - - return presentingController; -} - -- (UITextField *)textFieldInRow:(NSUInteger)row section:(NSUInteger)section -{ - return [[[[[self tableView] cellForRowAtIndexPath: - [NSIndexPath indexPathForRow:(NSInteger)row inSection:(NSInteger)section]] - contentView] subviews] objectAtIndex:0]; -} - -- (UITextField *)usernameField -{ - return [self textFieldInRow:kUsernameRow section:kUsernameSection]; -} - -- (UITextField *)passwordField -{ - return [self textFieldInRow:kPasswordRow section:kPasswordSection]; -} - -- (UITextField *)domainField -{ - return [self textFieldInRow:kDomainRow section:kDomainSection]; -} - -#pragma mark show / dismiss - -+ (void)dismiss -{ - UIViewController* dismisser = nil; - if ([sharedDialog respondsToSelector:@selector(presentingViewController)]){ - dismisser = [sharedDialog presentingViewController]; - }else{ - dismisser = [sharedDialog parentViewController]; - } - if([dismisser respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]){ - [dismisser dismissViewControllerAnimated:YES completion:nil]; - }else{ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [dismisser dismissModalViewControllerAnimated:YES]; -#pragma clang diagnostic pop - } -} - -- (void)viewDidDisappear:(BOOL)animated -{ - [super viewDidDisappear:animated]; - [self retain]; - [sharedDialog release]; - sharedDialog = nil; - [self performSelector:@selector(presentNextDialog) withObject:nil afterDelay:0]; - [self release]; -} - -- (void)dismiss -{ - if (self == sharedDialog) { - [[self class] dismiss]; - } else { - UIViewController* dismisser = nil; - if ([self respondsToSelector:@selector(presentingViewController)]){ - dismisser = [self presentingViewController]; - }else{ - dismisser = [self parentViewController]; - } - if([dismisser respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]){ - [dismisser dismissViewControllerAnimated:YES completion:nil]; - }else{ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [dismisser dismissModalViewControllerAnimated:YES]; -#pragma clang diagnostic pop - } - } -} - -- (void)showTitle -{ - UINavigationBar *navigationBar = [[[self view] subviews] objectAtIndex:0]; - UINavigationItem *navItem = [[navigationBar items] objectAtIndex:0]; - if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { - // Setup the title - if ([self type] == ASIProxyAuthenticationType) { - [navItem setPrompt:@"Login to this secure proxy server."]; - } else { - [navItem setPrompt:@"Login to this secure server."]; - } - } else { - [navItem setPrompt:nil]; - } - [navigationBar sizeToFit]; - CGRect f = [[self view] bounds]; - f.origin.y = [navigationBar frame].size.height; - f.size.height -= f.origin.y; - [[self tableView] setFrame:f]; -} - -- (void)show -{ - // Remove all subviews - UIView *v; - while ((v = [[[self view] subviews] lastObject])) { - [v removeFromSuperview]; - } - - // Setup toolbar - UINavigationBar *bar = [[[UINavigationBar alloc] init] autorelease]; - [bar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - - UINavigationItem *navItem = [[[UINavigationItem alloc] init] autorelease]; - bar.items = [NSArray arrayWithObject:navItem]; - - [[self view] addSubview:bar]; - - [self showTitle]; - - // Setup toolbar buttons - if ([self type] == ASIProxyAuthenticationType) { - [navItem setTitle:[[self request] proxyHost]]; - } else { - [navItem setTitle:[[[self request] url] host]]; - } - - [navItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAuthenticationFromDialog:)] autorelease]]; - [navItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleDone target:self action:@selector(loginWithCredentialsFromDialog:)] autorelease]]; - - // We show the login form in a table view, similar to Safari's authentication dialog - [bar sizeToFit]; - CGRect f = [[self view] bounds]; - f.origin.y = [bar frame].size.height; - f.size.height -= f.origin.y; - - [self setTableView:[[[UITableView alloc] initWithFrame:f style:UITableViewStyleGrouped] autorelease]]; - [[self tableView] setDelegate:self]; - [[self tableView] setDataSource:self]; - [[self tableView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [[self view] addSubview:[self tableView]]; - - // Force reload the table content, and focus the first field to show the keyboard - [[self tableView] reloadData]; - [[[[[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].contentView subviews] objectAtIndex:0] becomeFirstResponder]; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [self setModalPresentationStyle:UIModalPresentationFormSheet]; - } -#endif - - if([[self presentingController] respondsToSelector:@selector(presentViewController:animated:completion:)]){ - [[self presentingController] presentViewController:self animated:YES completion:nil]; - }else{ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [[self presentingController] presentModalViewController:self animated:YES]; -#pragma clang diagnostic pop - } -} - -#pragma mark button callbacks - -- (void)cancelAuthenticationFromDialog:(id)sender -{ - for (ASIHTTPRequest *theRequest in [self requestsRequiringTheseCredentials]) { - [theRequest cancelAuthentication]; - [requestsNeedingAuthentication removeObject:theRequest]; - } - [self dismiss]; -} - -- (NSArray *)requestsRequiringTheseCredentials -{ - NSMutableArray *requestsRequiringTheseCredentials = [NSMutableArray array]; - NSURL *requestURL = [[self request] url]; - for (ASIHTTPRequest *otherRequest in requestsNeedingAuthentication) { - NSURL *theURL = [otherRequest url]; - if (([otherRequest authenticationNeeded] == [[self request] authenticationNeeded]) && [[theURL host] isEqualToString:[requestURL host]] && ([theURL port] == [requestURL port] || ([requestURL port] && [[theURL port] isEqualToNumber:[requestURL port]])) && [[theURL scheme] isEqualToString:[requestURL scheme]] && ((![otherRequest authenticationRealm] && ![[self request] authenticationRealm]) || ([otherRequest authenticationRealm] && [[self request] authenticationRealm] && [[[self request] authenticationRealm] isEqualToString:[otherRequest authenticationRealm]]))) { - [requestsRequiringTheseCredentials addObject:otherRequest]; - } - } - [requestsRequiringTheseCredentials addObject:[self request]]; - return requestsRequiringTheseCredentials; -} - -- (void)presentNextDialog -{ - if ([requestsNeedingAuthentication count]) { - ASIHTTPRequest *nextRequest = [requestsNeedingAuthentication objectAtIndex:0]; - [requestsNeedingAuthentication removeObjectAtIndex:0]; - [[self class] presentAuthenticationDialogForRequest:nextRequest]; - } -} - - -- (void)loginWithCredentialsFromDialog:(id)sender -{ - for (ASIHTTPRequest *theRequest in [self requestsRequiringTheseCredentials]) { - - NSString *username = [[self usernameField] text]; - NSString *password = [[self passwordField] text]; - - if (username == nil) { username = @""; } - if (password == nil) { password = @""; } - - if ([self type] == ASIProxyAuthenticationType) { - [theRequest setProxyUsername:username]; - [theRequest setProxyPassword:password]; - } else { - [theRequest setUsername:username]; - [theRequest setPassword:password]; - } - - // Handle NTLM domains - NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; - if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { - NSString *domain = [[self domainField] text]; - if ([self type] == ASIProxyAuthenticationType) { - [theRequest setProxyDomain:domain]; - } else { - [theRequest setDomain:domain]; - } - } - - [theRequest retryUsingSuppliedCredentials]; - [requestsNeedingAuthentication removeObject:theRequest]; - } - [self dismiss]; -} - -#pragma mark table view data source - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView -{ - NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; - if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { - return 2; - } - return 1; -} - -- (CGFloat)tableView:(UITableView *)aTableView heightForFooterInSection:(NSInteger)section -{ - if (section == [self numberOfSectionsInTableView:aTableView]-1) { - return 30; - } - return 0; -} - -- (CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section -{ - if (section == 0) { -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - return 54; - } -#endif - return 30; - } - return 0; -} - -- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section -{ - if (section == 0) { - return [[self request] authenticationRealm]; - } - return nil; -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0 - UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; -#else - UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:nil] autorelease]; -#endif - - [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; - - CGRect f = CGRectInset([cell bounds], 10, 10); - UITextField *textField = [[[UITextField alloc] initWithFrame:f] autorelease]; - [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; - [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; - - NSInteger s = [indexPath section]; - NSInteger r = [indexPath row]; - - if (s == kUsernameSection && r == kUsernameRow) { - [textField setPlaceholder:@"User"]; - } else if (s == kPasswordSection && r == kPasswordRow) { - [textField setPlaceholder:@"Password"]; - [textField setSecureTextEntry:YES]; - } else if (s == kDomainSection && r == kDomainRow) { - [textField setPlaceholder:@"Domain"]; - } - [cell.contentView addSubview:textField]; - - return cell; -} - -- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section -{ - if (section == 0) { - return 2; - } else { - return 1; - } -} - -- (NSString *)tableView:(UITableView *)aTableView titleForFooterInSection:(NSInteger)section -{ - if (section == [self numberOfSectionsInTableView:aTableView]-1) { - // If we're using Basic authentication and the connection is not using SSL, we'll show the plain text message - if ([[[self request] authenticationScheme] isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeBasic] && ![[[[self request] url] scheme] isEqualToString:@"https"]) { - return @"Password will be sent in the clear."; - // We are using Digest, NTLM, or any scheme over SSL - } else { - return @"Password will be sent securely."; - } - } - return nil; -} - -#pragma mark - - -@synthesize request; -@synthesize type; -@synthesize tableView; -@synthesize didEnableRotationNotifications; -@synthesize presentingController; -@end diff --git a/clients/ios/ASI/ASICacheDelegate.h b/clients/ios/ASI/ASICacheDelegate.h deleted file mode 100755 index 060cda591..000000000 --- a/clients/ios/ASI/ASICacheDelegate.h +++ /dev/null @@ -1,103 +0,0 @@ -// -// ASICacheDelegate.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 01/05/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import -@class ASIHTTPRequest; - -// Cache policies control the behaviour of a cache and how requests use the cache -// When setting a cache policy, you can use a combination of these values as a bitmask -// For example: [request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy|ASIDoNotWriteToCacheCachePolicy]; -// Note that some of the behaviours below are mutally exclusive - you cannot combine ASIAskServerIfModifiedWhenStaleCachePolicy and ASIAskServerIfModifiedCachePolicy, for example. -typedef enum _ASICachePolicy { - - // The default cache policy. When you set a request to use this, it will use the cache's defaultCachePolicy - // ASIDownloadCache's default cache policy is 'ASIAskServerIfModifiedWhenStaleCachePolicy' - ASIUseDefaultCachePolicy = 0, - - // Tell the request not to read from the cache - ASIDoNotReadFromCacheCachePolicy = 1, - - // The the request not to write to the cache - ASIDoNotWriteToCacheCachePolicy = 2, - - // Ask the server if there is an updated version of this resource (using a conditional GET) ONLY when the cached data is stale - ASIAskServerIfModifiedWhenStaleCachePolicy = 4, - - // Always ask the server if there is an updated version of this resource (using a conditional GET) - ASIAskServerIfModifiedCachePolicy = 8, - - // If cached data exists, use it even if it is stale. This means requests will not talk to the server unless the resource they are requesting is not in the cache - ASIOnlyLoadIfNotCachedCachePolicy = 16, - - // If cached data exists, use it even if it is stale. If cached data does not exist, stop (will not set an error on the request) - ASIDontLoadCachePolicy = 32, - - // Specifies that cached data may be used if the request fails. If cached data is used, the request will succeed without error. Usually used in combination with other options above. - ASIFallbackToCacheIfLoadFailsCachePolicy = 64 -} ASICachePolicy; - -// Cache storage policies control whether cached data persists between application launches (ASICachePermanentlyCacheStoragePolicy) or not (ASICacheForSessionDurationCacheStoragePolicy) -// Calling [ASIHTTPRequest clearSession] will remove any data stored using ASICacheForSessionDurationCacheStoragePolicy -typedef enum _ASICacheStoragePolicy { - ASICacheForSessionDurationCacheStoragePolicy = 0, - ASICachePermanentlyCacheStoragePolicy = 1 -} ASICacheStoragePolicy; - - -@protocol ASICacheDelegate - -@required - -// Should return the cache policy that will be used when requests have their cache policy set to ASIUseDefaultCachePolicy -- (ASICachePolicy)defaultCachePolicy; - -// Returns the date a cached response should expire on. Pass a non-zero max age to specify a custom date. -- (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; - -// Updates cached response headers with a new expiry date. Pass a non-zero max age to specify a custom date. -- (void)updateExpiryForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; - -// Looks at the request's cache policy and any cached headers to determine if the cache data is still valid -- (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request; - -// Removes cached data for a particular request -- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request; - -// Should return YES if the cache considers its cached response current for the request -// Should return NO is the data is not cached, or (for example) if the cached headers state the request should have expired -- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request; - -// Should store the response for the passed request in the cache -// When a non-zero maxAge is passed, it should be used as the expiry time for the cached response -- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; - -// Removes cached data for a particular url -- (void)removeCachedDataForURL:(NSURL *)url; - -// Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache -- (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url; - -// Should return the cached body of a response for the passed URL, if it is stored in the cache -- (NSData *)cachedResponseDataForURL:(NSURL *)url; - -// Returns a path to the cached response data, if it exists -- (NSString *)pathToCachedResponseDataForURL:(NSURL *)url; - -// Returns a path to the cached response headers, if they url -- (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url; - -// Returns the location to use to store cached response headers for a particular request -- (NSString *)pathToStoreCachedResponseHeadersForRequest:(ASIHTTPRequest *)request; - -// Returns the location to use to store a cached response body for a particular request -- (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request; - -// Clear cached data stored for the passed storage policy -- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)cachePolicy; - -@end diff --git a/clients/ios/ASI/ASIDataCompressor.h b/clients/ios/ASI/ASIDataCompressor.h deleted file mode 100755 index fb57a1a98..000000000 --- a/clients/ios/ASI/ASIDataCompressor.h +++ /dev/null @@ -1,42 +0,0 @@ -// -// ASIDataCompressor.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 17/08/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -// This is a helper class used by ASIHTTPRequest to handle deflating (compressing) data in memory and on disk -// You may also find it helpful if you need to deflate data and files yourself - see the class methods below -// Most of the zlib stuff is based on the sample code by Mark Adler available at http://zlib.net - -#import -#import - -@interface ASIDataCompressor : NSObject { - BOOL streamReady; - z_stream zStream; -} - -// Convenience constructor will call setupStream for you -+ (id)compressor; - -// Compress the passed chunk of data -// Passing YES for shouldFinish will finalize the deflated data - you must pass YES when you are on the last chunk of data -- (NSData *)compressBytes:(Bytef *)bytes length:(NSUInteger)length error:(NSError **)err shouldFinish:(BOOL)shouldFinish; - -// Convenience method - pass it some data, and you'll get deflated data back -+ (NSData *)compressData:(NSData*)uncompressedData error:(NSError **)err; - -// Convenience method - pass it a file containing the data to compress in sourcePath, and it will write deflated data to destinationPath -+ (BOOL)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath error:(NSError **)err; - -// Sets up zlib to handle the inflating. You only need to call this yourself if you aren't using the convenience constructor 'compressor' -- (NSError *)setupStream; - -// Tells zlib to clean up. You need to call this if you need to cancel deflating part way through -// If deflating finishes or fails, this method will be called automatically -- (NSError *)closeStream; - -@property (atomic, assign, readonly) BOOL streamReady; -@end diff --git a/clients/ios/ASI/ASIDataCompressor.m b/clients/ios/ASI/ASIDataCompressor.m deleted file mode 100755 index 381d3a9d6..000000000 --- a/clients/ios/ASI/ASIDataCompressor.m +++ /dev/null @@ -1,219 +0,0 @@ -// -// ASIDataCompressor.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 17/08/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import "ASIDataCompressor.h" -#import "ASIHTTPRequest.h" - -#define DATA_CHUNK_SIZE 262144 // Deal with gzipped data in 256KB chunks -#define COMPRESSION_AMOUNT Z_DEFAULT_COMPRESSION - -@interface ASIDataCompressor () -+ (NSError *)deflateErrorWithCode:(int)code; -@end - -@implementation ASIDataCompressor - -+ (id)compressor -{ - ASIDataCompressor *compressor = [[[self alloc] init] autorelease]; - [compressor setupStream]; - return compressor; -} - -- (void)dealloc -{ - if (streamReady) { - [self closeStream]; - } - [super dealloc]; -} - -- (NSError *)setupStream -{ - if (streamReady) { - return nil; - } - // Setup the inflate stream - zStream.zalloc = Z_NULL; - zStream.zfree = Z_NULL; - zStream.opaque = Z_NULL; - zStream.avail_in = 0; - zStream.next_in = 0; - int status = deflateInit2(&zStream, COMPRESSION_AMOUNT, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY); - if (status != Z_OK) { - return [[self class] deflateErrorWithCode:status]; - } - streamReady = YES; - return nil; -} - -- (NSError *)closeStream -{ - if (!streamReady) { - return nil; - } - // Close the deflate stream - streamReady = NO; - int status = deflateEnd(&zStream); - if (status != Z_OK) { - return [[self class] deflateErrorWithCode:status]; - } - return nil; -} - -- (NSData *)compressBytes:(Bytef *)bytes length:(NSUInteger)length error:(NSError **)err shouldFinish:(BOOL)shouldFinish -{ - if (length == 0) return nil; - - NSUInteger halfLength = length/2; - - // We'll take a guess that the compressed data will fit in half the size of the original (ie the max to compress at once is half DATA_CHUNK_SIZE), if not, we'll increase it below - NSMutableData *outputData = [NSMutableData dataWithLength:length/2]; - - int status; - - zStream.next_in = bytes; - zStream.avail_in = (unsigned int)length; - zStream.avail_out = 0; - - NSUInteger bytesProcessedAlready = zStream.total_out; - while (zStream.avail_out == 0) { - - if (zStream.total_out-bytesProcessedAlready >= [outputData length]) { - [outputData increaseLengthBy:halfLength]; - } - - zStream.next_out = (Bytef*)[outputData mutableBytes] + zStream.total_out-bytesProcessedAlready; - zStream.avail_out = (unsigned int)([outputData length] - (zStream.total_out-bytesProcessedAlready)); - status = deflate(&zStream, shouldFinish ? Z_FINISH : Z_NO_FLUSH); - - if (status == Z_STREAM_END) { - break; - } else if (status != Z_OK) { - if (err) { - *err = [[self class] deflateErrorWithCode:status]; - } - return nil; - } - } - - // Set real length - [outputData setLength: zStream.total_out-bytesProcessedAlready]; - return outputData; -} - - -+ (NSData *)compressData:(NSData*)uncompressedData error:(NSError **)err -{ - NSError *theError = nil; - NSData *outputData = [[ASIDataCompressor compressor] compressBytes:(Bytef *)[uncompressedData bytes] length:[uncompressedData length] error:&theError shouldFinish:YES]; - if (theError) { - if (err) { - *err = theError; - } - return nil; - } - return outputData; -} - - - -+ (BOOL)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath error:(NSError **)err -{ - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - // Create an empty file at the destination path - if (![fileManager createFileAtPath:destinationPath contents:[NSData data] attributes:nil]) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of %@ failed because we were to create a file at %@",sourcePath,destinationPath],NSLocalizedDescriptionKey,nil]]; - } - return NO; - } - - // Ensure the source file exists - if (![fileManager fileExistsAtPath:sourcePath]) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of %@ failed the file does not exist",sourcePath],NSLocalizedDescriptionKey,nil]]; - } - return NO; - } - - UInt8 inputData[DATA_CHUNK_SIZE]; - NSData *outputData; - NSInteger readLength; - NSError *theError = nil; - - ASIDataCompressor *compressor = [ASIDataCompressor compressor]; - - NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:sourcePath]; - [inputStream open]; - NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:destinationPath append:NO]; - [outputStream open]; - - while ([compressor streamReady]) { - - // Read some data from the file - readLength = [inputStream read:inputData maxLength:DATA_CHUNK_SIZE]; - - // Make sure nothing went wrong - if ([inputStream streamStatus] == NSStreamStatusError) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of %@ failed because we were unable to read from the source data file",sourcePath],NSLocalizedDescriptionKey,[inputStream streamError],NSUnderlyingErrorKey,nil]]; - } - [compressor closeStream]; - return NO; - } - // Have we reached the end of the input data? - if (!readLength) { - break; - } - - // Attempt to deflate the chunk of data - outputData = [compressor compressBytes:inputData length:(NSUInteger)readLength error:&theError shouldFinish:readLength < DATA_CHUNK_SIZE]; - if (theError) { - if (err) { - *err = theError; - } - [compressor closeStream]; - return NO; - } - - // Write the deflated data out to the destination file - [outputStream write:(const uint8_t *)[outputData bytes] maxLength:[outputData length]]; - - // Make sure nothing went wrong - if ([inputStream streamStatus] == NSStreamStatusError) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of %@ failed because we were unable to write to the destination data file at %@",sourcePath,destinationPath],NSLocalizedDescriptionKey,[outputStream streamError],NSUnderlyingErrorKey,nil]]; - } - [compressor closeStream]; - return NO; - } - - } - [inputStream close]; - [outputStream close]; - - NSError *error = [compressor closeStream]; - if (error) { - if (err) { - *err = error; - } - return NO; - } - - return YES; -} - -+ (NSError *)deflateErrorWithCode:(int)code -{ - return [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Compression of data failed with code %d",code],NSLocalizedDescriptionKey,nil]]; -} - -@synthesize streamReady; -@end diff --git a/clients/ios/ASI/ASIDataDecompressor.h b/clients/ios/ASI/ASIDataDecompressor.h deleted file mode 100755 index 32a40a94e..000000000 --- a/clients/ios/ASI/ASIDataDecompressor.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// ASIDataDecompressor.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 17/08/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -// This is a helper class used by ASIHTTPRequest to handle inflating (decompressing) data in memory and on disk -// You may also find it helpful if you need to inflate data and files yourself - see the class methods below -// Most of the zlib stuff is based on the sample code by Mark Adler available at http://zlib.net - -#import -#import - -@interface ASIDataDecompressor : NSObject { - BOOL streamReady; - z_stream zStream; -} - -// Convenience constructor will call setupStream for you -+ (id)decompressor; - -// Uncompress the passed chunk of data -- (NSData *)uncompressBytes:(Bytef *)bytes length:(NSUInteger)length error:(NSError **)err; - -// Convenience method - pass it some deflated data, and you'll get inflated data back -+ (NSData *)uncompressData:(NSData*)compressedData error:(NSError **)err; - -// Convenience method - pass it a file containing deflated data in sourcePath, and it will write inflated data to destinationPath -+ (BOOL)uncompressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath error:(NSError **)err; - -// Sets up zlib to handle the inflating. You only need to call this yourself if you aren't using the convenience constructor 'decompressor' -- (NSError *)setupStream; - -// Tells zlib to clean up. You need to call this if you need to cancel inflating part way through -// If inflating finishes or fails, this method will be called automatically -- (NSError *)closeStream; - -@property (atomic, assign, readonly) BOOL streamReady; -@end diff --git a/clients/ios/ASI/ASIDataDecompressor.m b/clients/ios/ASI/ASIDataDecompressor.m deleted file mode 100755 index fa0ef0139..000000000 --- a/clients/ios/ASI/ASIDataDecompressor.m +++ /dev/null @@ -1,218 +0,0 @@ -// -// ASIDataDecompressor.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 17/08/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import "ASIDataDecompressor.h" -#import "ASIHTTPRequest.h" - -#define DATA_CHUNK_SIZE 262144 // Deal with gzipped data in 256KB chunks - -@interface ASIDataDecompressor () -+ (NSError *)inflateErrorWithCode:(int)code; -@end; - -@implementation ASIDataDecompressor - -+ (id)decompressor -{ - ASIDataDecompressor *decompressor = [[[self alloc] init] autorelease]; - [decompressor setupStream]; - return decompressor; -} - -- (void)dealloc -{ - if (streamReady) { - [self closeStream]; - } - [super dealloc]; -} - -- (NSError *)setupStream -{ - if (streamReady) { - return nil; - } - // Setup the inflate stream - zStream.zalloc = Z_NULL; - zStream.zfree = Z_NULL; - zStream.opaque = Z_NULL; - zStream.avail_in = 0; - zStream.next_in = 0; - int status = inflateInit2(&zStream, (15+32)); - if (status != Z_OK) { - return [[self class] inflateErrorWithCode:status]; - } - streamReady = YES; - return nil; -} - -- (NSError *)closeStream -{ - if (!streamReady) { - return nil; - } - // Close the inflate stream - streamReady = NO; - int status = inflateEnd(&zStream); - if (status != Z_OK) { - return [[self class] inflateErrorWithCode:status]; - } - return nil; -} - -- (NSData *)uncompressBytes:(Bytef *)bytes length:(NSUInteger)length error:(NSError **)err -{ - if (length == 0) return nil; - - NSUInteger halfLength = length/2; - NSMutableData *outputData = [NSMutableData dataWithLength:length+halfLength]; - - int status; - - zStream.next_in = bytes; - zStream.avail_in = (unsigned int)length; - zStream.avail_out = 0; - - NSUInteger bytesProcessedAlready = zStream.total_out; - while (zStream.avail_in != 0) { - - if (zStream.total_out-bytesProcessedAlready >= [outputData length]) { - [outputData increaseLengthBy:halfLength]; - } - - zStream.next_out = (Bytef*)[outputData mutableBytes] + zStream.total_out-bytesProcessedAlready; - zStream.avail_out = (unsigned int)([outputData length] - (zStream.total_out-bytesProcessedAlready)); - - status = inflate(&zStream, Z_NO_FLUSH); - - if (status == Z_STREAM_END) { - break; - } else if (status != Z_OK) { - if (err) { - *err = [[self class] inflateErrorWithCode:status]; - } - return nil; - } - } - - // Set real length - [outputData setLength: zStream.total_out-bytesProcessedAlready]; - return outputData; -} - - -+ (NSData *)uncompressData:(NSData*)compressedData error:(NSError **)err -{ - NSError *theError = nil; - NSData *outputData = [[ASIDataDecompressor decompressor] uncompressBytes:(Bytef *)[compressedData bytes] length:[compressedData length] error:&theError]; - if (theError) { - if (err) { - *err = theError; - } - return nil; - } - return outputData; -} - -+ (BOOL)uncompressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath error:(NSError **)err -{ - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - // Create an empty file at the destination path - if (![fileManager createFileAtPath:destinationPath contents:[NSData data] attributes:nil]) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed because we were to create a file at %@",sourcePath,destinationPath],NSLocalizedDescriptionKey,nil]]; - } - return NO; - } - - // Ensure the source file exists - if (![fileManager fileExistsAtPath:sourcePath]) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed the file does not exist",sourcePath],NSLocalizedDescriptionKey,nil]]; - } - return NO; - } - - UInt8 inputData[DATA_CHUNK_SIZE]; - NSData *outputData; - NSInteger readLength; - NSError *theError = nil; - - - ASIDataDecompressor *decompressor = [ASIDataDecompressor decompressor]; - - NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:sourcePath]; - [inputStream open]; - NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:destinationPath append:NO]; - [outputStream open]; - - while ([decompressor streamReady]) { - - // Read some data from the file - readLength = [inputStream read:inputData maxLength:DATA_CHUNK_SIZE]; - - // Make sure nothing went wrong - if ([inputStream streamStatus] == NSStreamStatusError) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed because we were unable to read from the source data file",sourcePath],NSLocalizedDescriptionKey,[inputStream streamError],NSUnderlyingErrorKey,nil]]; - } - [decompressor closeStream]; - return NO; - } - // Have we reached the end of the input data? - if (!readLength) { - break; - } - - // Attempt to inflate the chunk of data - outputData = [decompressor uncompressBytes:inputData length:(NSUInteger)readLength error:&theError]; - if (theError) { - if (err) { - *err = theError; - } - [decompressor closeStream]; - return NO; - } - - // Write the inflated data out to the destination file - [outputStream write:(Bytef*)[outputData bytes] maxLength:[outputData length]]; - - // Make sure nothing went wrong - if ([inputStream streamStatus] == NSStreamStatusError) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed because we were unable to write to the destination data file at %@",sourcePath,destinationPath],NSLocalizedDescriptionKey,[outputStream streamError],NSUnderlyingErrorKey,nil]]; - } - [decompressor closeStream]; - return NO; - } - - } - - [inputStream close]; - [outputStream close]; - - NSError *error = [decompressor closeStream]; - if (error) { - if (err) { - *err = error; - } - return NO; - } - - return YES; -} - - -+ (NSError *)inflateErrorWithCode:(int)code -{ - return [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of data failed with code %d",code],NSLocalizedDescriptionKey,nil]]; -} - -@synthesize streamReady; -@end diff --git a/clients/ios/ASI/ASIDownloadCache.h b/clients/ios/ASI/ASIDownloadCache.h deleted file mode 100755 index 967ddbe8b..000000000 --- a/clients/ios/ASI/ASIDownloadCache.h +++ /dev/null @@ -1,46 +0,0 @@ -// -// ASIDownloadCache.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 01/05/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import -#import "ASICacheDelegate.h" - -@interface ASIDownloadCache : NSObject { - - // The default cache policy for this cache - // Requests that store data in the cache will use this cache policy if their cache policy is set to ASIUseDefaultCachePolicy - // Defaults to ASIAskServerIfModifiedWhenStaleCachePolicy - ASICachePolicy defaultCachePolicy; - - // The directory in which cached data will be stored - // Defaults to a directory called 'ASIHTTPRequestCache' in the temporary directory - NSString *storagePath; - - // Mediates access to the cache - NSRecursiveLock *accessLock; - - // When YES, the cache will look for cache-control / pragma: no-cache headers, and won't reuse store responses if it finds them - BOOL shouldRespectCacheControlHeaders; -} - -// Returns a static instance of an ASIDownloadCache -// In most circumstances, it will make sense to use this as a global cache, rather than creating your own cache -// To make ASIHTTPRequests use it automatically, use [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; -+ (id)sharedCache; - -// A helper function that determines if the server has requested data should not be cached by looking at the request's response headers -+ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request; - -// A list of file extensions that we know won't be readable by a webview when accessed locally -// If we're asking for a path to cache a particular url and it has one of these extensions, we change it to '.html' -+ (NSArray *)fileExtensionsToHandleAsHTML; - -@property (assign, nonatomic) ASICachePolicy defaultCachePolicy; -@property (retain, nonatomic) NSString *storagePath; -@property (atomic, retain) NSRecursiveLock *accessLock; -@property (atomic, assign) BOOL shouldRespectCacheControlHeaders; -@end diff --git a/clients/ios/ASI/ASIDownloadCache.m b/clients/ios/ASI/ASIDownloadCache.m deleted file mode 100755 index 93da36fbd..000000000 --- a/clients/ios/ASI/ASIDownloadCache.m +++ /dev/null @@ -1,514 +0,0 @@ -// -// ASIDownloadCache.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 01/05/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import "ASIDownloadCache.h" -#import "ASIHTTPRequest.h" -#import - -static ASIDownloadCache *sharedCache = nil; - -static NSString *sessionCacheFolder = @"SessionStore"; -static NSString *permanentCacheFolder = @"PermanentStore"; -static NSArray *fileExtensionsToHandleAsHTML = nil; - -@interface ASIDownloadCache () -+ (NSString *)keyForURL:(NSURL *)url; -- (NSString *)pathToFile:(NSString *)file; -@end - -@implementation ASIDownloadCache - -+ (void)initialize -{ - if (self == [ASIDownloadCache class]) { - // Obviously this is not an exhaustive list, but hopefully these are the most commonly used and this will 'just work' for the widest range of people - // I imagine many web developers probably use url rewriting anyway - fileExtensionsToHandleAsHTML = [[NSArray alloc] initWithObjects:@"asp",@"aspx",@"jsp",@"php",@"rb",@"py",@"pl",@"cgi", nil]; - } -} - -- (id)init -{ - self = [super init]; - [self setShouldRespectCacheControlHeaders:YES]; - [self setDefaultCachePolicy:ASIUseDefaultCachePolicy]; - [self setAccessLock:[[[NSRecursiveLock alloc] init] autorelease]]; - return self; -} - -+ (id)sharedCache -{ - if (!sharedCache) { - @synchronized(self) { - if (!sharedCache) { - sharedCache = [[self alloc] init]; - [sharedCache setStoragePath:[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"ASIHTTPRequestCache"]]; - } - } - } - return sharedCache; -} - -- (void)dealloc -{ - [storagePath release]; - [accessLock release]; - [super dealloc]; -} - -- (NSString *)storagePath -{ - [[self accessLock] lock]; - NSString *p = [[storagePath retain] autorelease]; - [[self accessLock] unlock]; - return p; -} - - -- (void)setStoragePath:(NSString *)path -{ - [[self accessLock] lock]; - [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; - [storagePath release]; - storagePath = [path retain]; - - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - BOOL isDirectory = NO; - NSArray *directories = [NSArray arrayWithObjects:path,[path stringByAppendingPathComponent:sessionCacheFolder],[path stringByAppendingPathComponent:permanentCacheFolder],nil]; - for (NSString *directory in directories) { - BOOL exists = [fileManager fileExistsAtPath:directory isDirectory:&isDirectory]; - if (exists && !isDirectory) { - [[self accessLock] unlock]; - [NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists",directory]; - } else if (!exists) { - [fileManager createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:nil]; - if (![fileManager fileExistsAtPath:directory]) { - [[self accessLock] unlock]; - [NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'",directory]; - } - } - } - [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; - [[self accessLock] unlock]; -} - -- (void)updateExpiryForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge -{ - NSString *headerPath = [self pathToStoreCachedResponseHeadersForRequest:request]; - NSMutableDictionary *cachedHeaders = [NSMutableDictionary dictionaryWithContentsOfFile:headerPath]; - if (!cachedHeaders) { - return; - } - NSDate *expires = [self expiryDateForRequest:request maxAge:maxAge]; - if (!expires) { - return; - } - [cachedHeaders setObject:[NSNumber numberWithDouble:[expires timeIntervalSince1970]] forKey:@"X-ASIHTTPRequest-Expires"]; - [cachedHeaders writeToFile:headerPath atomically:NO]; -} - -- (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge -{ - return [ASIHTTPRequest expiryDateForRequest:request maxAge:maxAge]; -} - -- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge -{ - [[self accessLock] lock]; - - if ([request error] || ![request responseHeaders] || ([request cachePolicy] & ASIDoNotWriteToCacheCachePolicy)) { - [[self accessLock] unlock]; - return; - } - - // We only cache 200/OK or redirect reponses (redirect responses are cached so the cache works better with no internet connection) - int responseCode = [request responseStatusCode]; - if (responseCode != 200 && responseCode != 301 && responseCode != 302 && responseCode != 303 && responseCode != 307) { - [[self accessLock] unlock]; - return; - } - - if ([self shouldRespectCacheControlHeaders] && ![[self class] serverAllowsResponseCachingForRequest:request]) { - [[self accessLock] unlock]; - return; - } - - NSString *headerPath = [self pathToStoreCachedResponseHeadersForRequest:request]; - NSString *dataPath = [self pathToStoreCachedResponseDataForRequest:request]; - - NSMutableDictionary *responseHeaders = [NSMutableDictionary dictionaryWithDictionary:[request responseHeaders]]; - if ([request isResponseCompressed]) { - [responseHeaders removeObjectForKey:@"Content-Encoding"]; - } - - // Create a special 'X-ASIHTTPRequest-Expires' header - // This is what we use for deciding if cached data is current, rather than parsing the expires / max-age headers individually each time - // We store this as a timestamp to make reading it easier as NSDateFormatter is quite expensive - - NSDate *expires = [self expiryDateForRequest:request maxAge:maxAge]; - if (expires) { - [responseHeaders setObject:[NSNumber numberWithDouble:[expires timeIntervalSince1970]] forKey:@"X-ASIHTTPRequest-Expires"]; - } - - // Store the response code in a custom header so we can reuse it later - - // We'll change 304/Not Modified to 200/OK because this is likely to be us updating the cached headers with a conditional GET - int statusCode = [request responseStatusCode]; - if (statusCode == 304) { - statusCode = 200; - } - [responseHeaders setObject:[NSNumber numberWithInt:statusCode] forKey:@"X-ASIHTTPRequest-Response-Status-Code"]; - - [responseHeaders writeToFile:headerPath atomically:NO]; - - if ([request responseData]) { - [[request responseData] writeToFile:dataPath atomically:NO]; - } else if ([request downloadDestinationPath] && ![[request downloadDestinationPath] isEqualToString:dataPath]) { - NSError *error = nil; - NSFileManager* manager = [[NSFileManager alloc] init]; - if ([manager fileExistsAtPath:dataPath]) { - [manager removeItemAtPath:dataPath error:&error]; - } - [manager copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error]; - [manager release]; - } - [[self accessLock] unlock]; -} - -- (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url -{ - NSString *path = [self pathToCachedResponseHeadersForURL:url]; - if (path) { - return [NSDictionary dictionaryWithContentsOfFile:path]; - } - return nil; -} - -- (NSData *)cachedResponseDataForURL:(NSURL *)url -{ - NSString *path = [self pathToCachedResponseDataForURL:url]; - if (path) { - return [NSData dataWithContentsOfFile:path]; - } - return nil; -} - -- (NSString *)pathToCachedResponseDataForURL:(NSURL *)url -{ - // Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view - NSString *extension = [[url path] pathExtension]; - - // If the url doesn't have an extension, we'll add one so a webview can read it when locally cached - // If the url has the extension of a common web scripting language, we'll change the extension on the cached path to html for the same reason - if (![extension length] || [[[self class] fileExtensionsToHandleAsHTML] containsObject:[extension lowercaseString]]) { - extension = @"html"; - } - return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]]; -} - -+ (NSArray *)fileExtensionsToHandleAsHTML -{ - return fileExtensionsToHandleAsHTML; -} - - -- (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url -{ - return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]]; -} - -- (NSString *)pathToFile:(NSString *)file -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return nil; - } - - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - // Look in the session store - NSString *dataPath = [[[self storagePath] stringByAppendingPathComponent:sessionCacheFolder] stringByAppendingPathComponent:file]; - if ([fileManager fileExistsAtPath:dataPath]) { - [[self accessLock] unlock]; - return dataPath; - } - // Look in the permanent store - dataPath = [[[self storagePath] stringByAppendingPathComponent:permanentCacheFolder] stringByAppendingPathComponent:file]; - if ([fileManager fileExistsAtPath:dataPath]) { - [[self accessLock] unlock]; - return dataPath; - } - [[self accessLock] unlock]; - return nil; -} - - -- (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return nil; - } - - NSString *path = [[self storagePath] stringByAppendingPathComponent:([request cacheStoragePolicy] == ASICacheForSessionDurationCacheStoragePolicy ? sessionCacheFolder : permanentCacheFolder)]; - - // Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view - NSString *extension = [[[request url] path] pathExtension]; - - // If the url doesn't have an extension, we'll add one so a webview can read it when locally cached - // If the url has the extension of a common web scripting language, we'll change the extension on the cached path to html for the same reason - if (![extension length] || [[[self class] fileExtensionsToHandleAsHTML] containsObject:[extension lowercaseString]]) { - extension = @"html"; - } - path = [path stringByAppendingPathComponent:[[[self class] keyForURL:[request url]] stringByAppendingPathExtension:extension]]; - [[self accessLock] unlock]; - return path; -} - -- (NSString *)pathToStoreCachedResponseHeadersForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return nil; - } - NSString *path = [[self storagePath] stringByAppendingPathComponent:([request cacheStoragePolicy] == ASICacheForSessionDurationCacheStoragePolicy ? sessionCacheFolder : permanentCacheFolder)]; - path = [path stringByAppendingPathComponent:[[[self class] keyForURL:[request url]] stringByAppendingPathExtension:@"cachedheaders"]]; - [[self accessLock] unlock]; - return path; -} - -- (void)removeCachedDataForURL:(NSURL *)url -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return; - } - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - NSString *path = [self pathToCachedResponseHeadersForURL:url]; - if (path) { - [fileManager removeItemAtPath:path error:NULL]; - } - - path = [self pathToCachedResponseDataForURL:url]; - if (path) { - [fileManager removeItemAtPath:path error:NULL]; - } - [[self accessLock] unlock]; -} - -- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request -{ - [self removeCachedDataForURL:[request url]]; -} - -- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return NO; - } - NSDictionary *cachedHeaders = [self cachedResponseHeadersForURL:[request url]]; - if (!cachedHeaders) { - [[self accessLock] unlock]; - return NO; - } - NSString *dataPath = [self pathToCachedResponseDataForURL:[request url]]; - if (!dataPath) { - [[self accessLock] unlock]; - return NO; - } - - // New content is not different - if ([request responseStatusCode] == 304) { - [[self accessLock] unlock]; - return YES; - } - - // If we already have response headers for this request, check to see if the new content is different - // We check [request complete] so that we don't end up comparing response headers from a redirection with these - if ([request responseHeaders] && [request complete]) { - - // If the Etag or Last-Modified date are different from the one we have, we'll have to fetch this resource again - NSArray *headersToCompare = [NSArray arrayWithObjects:@"Etag",@"Last-Modified",nil]; - for (NSString *header in headersToCompare) { - if (![[[request responseHeaders] objectForKey:header] isEqualToString:[cachedHeaders objectForKey:header]]) { - [[self accessLock] unlock]; - return NO; - } - } - } - - if ([self shouldRespectCacheControlHeaders]) { - - // Look for X-ASIHTTPRequest-Expires header to see if the content is out of date - NSNumber *expires = [cachedHeaders objectForKey:@"X-ASIHTTPRequest-Expires"]; - if (expires) { - if ([[NSDate dateWithTimeIntervalSince1970:[expires doubleValue]] timeIntervalSinceNow] >= 0) { - [[self accessLock] unlock]; - return YES; - } - } - - // No explicit expiration time sent by the server - [[self accessLock] unlock]; - return NO; - } - - - [[self accessLock] unlock]; - return YES; -} - -- (ASICachePolicy)defaultCachePolicy -{ - [[self accessLock] lock]; - ASICachePolicy cp = defaultCachePolicy; - [[self accessLock] unlock]; - return cp; -} - - -- (void)setDefaultCachePolicy:(ASICachePolicy)cachePolicy -{ - [[self accessLock] lock]; - if (!cachePolicy) { - defaultCachePolicy = ASIAskServerIfModifiedWhenStaleCachePolicy; - } else { - defaultCachePolicy = cachePolicy; - } - [[self accessLock] unlock]; -} - -- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)storagePolicy -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return; - } - NSString *path = [[self storagePath] stringByAppendingPathComponent:(storagePolicy == ASICacheForSessionDurationCacheStoragePolicy ? sessionCacheFolder : permanentCacheFolder)]; - - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - BOOL isDirectory = NO; - BOOL exists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory]; - if (!exists || !isDirectory) { - [[self accessLock] unlock]; - return; - } - NSError *error = nil; - NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:path error:&error]; - if (error) { - [[self accessLock] unlock]; - [NSException raise:@"FailedToTraverseCacheDirectory" format:@"Listing cache directory failed at path '%@'",path]; - } - for (NSString *file in cacheFiles) { - [fileManager removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error]; - if (error) { - [[self accessLock] unlock]; - [NSException raise:@"FailedToRemoveCacheFile" format:@"Failed to remove cached data at path '%@'",path]; - } - } - [[self accessLock] unlock]; -} - -+ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request -{ - NSString *cacheControl = [[[request responseHeaders] objectForKey:@"Cache-Control"] lowercaseString]; - if (cacheControl) { - if ([cacheControl isEqualToString:@"no-cache"] || [cacheControl isEqualToString:@"no-store"]) { - return NO; - } - } - NSString *pragma = [[[request responseHeaders] objectForKey:@"Pragma"] lowercaseString]; - if (pragma) { - if ([pragma isEqualToString:@"no-cache"]) { - return NO; - } - } - return YES; -} - -+ (NSString *)keyForURL:(NSURL *)url -{ - NSString *urlString = [url absoluteString]; - if ([urlString length] == 0) { - return nil; - } - - // Strip trailing slashes so http://allseeing-i.com/ASIHTTPRequest/ is cached the same as http://allseeing-i.com/ASIHTTPRequest - if ([[urlString substringFromIndex:[urlString length]-1] isEqualToString:@"/"]) { - urlString = [urlString substringToIndex:[urlString length]-1]; - } - - // Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa - const char *cStr = [urlString UTF8String]; - unsigned char result[16]; - CC_MD5(cStr, (CC_LONG)strlen(cStr), result); - return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]]; -} - -- (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request -{ - // Ensure the request is allowed to read from the cache - if ([request cachePolicy] & ASIDoNotReadFromCacheCachePolicy) { - return NO; - - // If we don't want to load the request whatever happens, always pretend we have cached data even if we don't - } else if ([request cachePolicy] & ASIDontLoadCachePolicy) { - return YES; - } - - NSDictionary *headers = [self cachedResponseHeadersForURL:[request url]]; - if (!headers) { - return NO; - } - NSString *dataPath = [self pathToCachedResponseDataForURL:[request url]]; - if (!dataPath) { - return NO; - } - - // If we get here, we have cached data - - // If we have cached data, we can use it - if ([request cachePolicy] & ASIOnlyLoadIfNotCachedCachePolicy) { - return YES; - - // If we want to fallback to the cache after an error - } else if ([request complete] && [request cachePolicy] & ASIFallbackToCacheIfLoadFailsCachePolicy) { - return YES; - - // If we have cached data that is current, we can use it - } else if ([request cachePolicy] & ASIAskServerIfModifiedWhenStaleCachePolicy) { - if ([self isCachedDataCurrentForRequest:request]) { - return YES; - } - - // If we've got headers from a conditional GET and the cached data is still current, we can use it - } else if ([request cachePolicy] & ASIAskServerIfModifiedCachePolicy) { - if (![request responseHeaders]) { - return NO; - } else if ([self isCachedDataCurrentForRequest:request]) { - return YES; - } - } - return NO; -} - -@synthesize storagePath; -@synthesize defaultCachePolicy; -@synthesize accessLock; -@synthesize shouldRespectCacheControlHeaders; -@end diff --git a/clients/ios/ASI/ASIFormDataRequest.h b/clients/ios/ASI/ASIFormDataRequest.h deleted file mode 100755 index 151389017..000000000 --- a/clients/ios/ASI/ASIFormDataRequest.h +++ /dev/null @@ -1,75 +0,0 @@ -// -// ASIFormDataRequest.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import -#import "ASIHTTPRequest.h" -#import "ASIHTTPRequestConfig.h" - -typedef enum _ASIPostFormat { - ASIMultipartFormDataPostFormat = 0, - ASIURLEncodedPostFormat = 1 - -} ASIPostFormat; - -@interface ASIFormDataRequest : ASIHTTPRequest { - - // Parameters that will be POSTed to the url - NSMutableArray *postData; - - // Files that will be POSTed to the url - NSMutableArray *fileData; - - ASIPostFormat postFormat; - - NSStringEncoding stringEncoding; - -#if DEBUG_FORM_DATA_REQUEST - // Will store a string version of the request body that will be printed to the console when ASIHTTPREQUEST_DEBUG is set in GCC_PREPROCESSOR_DEFINITIONS - NSString *debugBodyString; -#endif - -} - -#pragma mark utilities -- (NSString*)encodeURL:(NSString *)string; - -#pragma mark setup request - -// Add a POST variable to the request -- (void)addPostValue:(id )value forKey:(NSString *)key; - -// Set a POST variable for this request, clearing any others with the same key -- (void)setPostValue:(id )value forKey:(NSString *)key; - -// Add the contents of a local file to the request -- (void)addFile:(NSString *)filePath forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)addFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -// Add the contents of a local file to the request, clearing any others with the same key -- (void)setFile:(NSString *)filePath forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)setFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -// Add the contents of an NSData object to the request -- (void)addData:(NSData *)data forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -// Add the contents of an NSData object to the request, clearing any others with the same key -- (void)setData:(NSData *)data forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -@property (atomic, assign) ASIPostFormat postFormat; -@property (atomic, assign) NSStringEncoding stringEncoding; -@end diff --git a/clients/ios/ASI/ASIFormDataRequest.m b/clients/ios/ASI/ASIFormDataRequest.m deleted file mode 100755 index 47f2e2945..000000000 --- a/clients/ios/ASI/ASIFormDataRequest.m +++ /dev/null @@ -1,362 +0,0 @@ -// -// ASIFormDataRequest.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASIFormDataRequest.h" - - -// Private stuff -@interface ASIFormDataRequest () -- (void)buildMultipartFormDataPostBody; -- (void)buildURLEncodedPostBody; -- (void)appendPostString:(NSString *)string; - -@property (atomic, retain) NSMutableArray *postData; -@property (atomic, retain) NSMutableArray *fileData; - -#if DEBUG_FORM_DATA_REQUEST -- (void)addToDebugBody:(NSString *)string; -@property (retain, nonatomic) NSString *debugBodyString; -#endif - -@end - -@implementation ASIFormDataRequest - -#pragma mark utilities -- (NSString*)encodeURL:(NSString *)string -{ - NSString *newString = [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding]))) autorelease]; - if (newString) { - return newString; - } - return @""; -} - -#pragma mark init / dealloc - -+ (id)requestWithURL:(NSURL *)newURL -{ - return [[[self alloc] initWithURL:newURL] autorelease]; -} - -- (id)initWithURL:(NSURL *)newURL -{ - self = [super initWithURL:newURL]; - [self setPostFormat:ASIURLEncodedPostFormat]; - [self setStringEncoding:NSUTF8StringEncoding]; - [self setRequestMethod:@"POST"]; - return self; -} - -- (void)dealloc -{ -#if DEBUG_FORM_DATA_REQUEST - [debugBodyString release]; -#endif - - [postData release]; - [fileData release]; - [super dealloc]; -} - -#pragma mark setup request - -- (void)addPostValue:(id )value forKey:(NSString *)key -{ - if (!key) { - return; - } - if (![self postData]) { - [self setPostData:[NSMutableArray array]]; - } - NSMutableDictionary *keyValuePair = [NSMutableDictionary dictionaryWithCapacity:2]; - [keyValuePair setValue:key forKey:@"key"]; - [keyValuePair setValue:[value description] forKey:@"value"]; - [[self postData] addObject:keyValuePair]; -} - -- (void)setPostValue:(id )value forKey:(NSString *)key -{ - // Remove any existing value - NSUInteger i; - for (i=0; i<[[self postData] count]; i++) { - NSDictionary *val = [[self postData] objectAtIndex:i]; - if ([[val objectForKey:@"key"] isEqualToString:key]) { - [[self postData] removeObjectAtIndex:i]; - i--; - } - } - [self addPostValue:value forKey:key]; -} - - -- (void)addFile:(NSString *)filePath forKey:(NSString *)key -{ - [self addFile:filePath withFileName:nil andContentType:nil forKey:key]; -} - -- (void)addFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - BOOL isDirectory = NO; - BOOL fileExists = [[[[NSFileManager alloc] init] autorelease] fileExistsAtPath:filePath isDirectory:&isDirectory]; - if (!fileExists || isDirectory) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"No file exists at %@",filePath],NSLocalizedDescriptionKey,nil]]]; - } - - // If the caller didn't specify a custom file name, we'll use the file name of the file we were passed - if (!fileName) { - fileName = [filePath lastPathComponent]; - } - - // If we were given the path to a file, and the user didn't specify a mime type, we can detect it from the file extension - if (!contentType) { - contentType = [ASIHTTPRequest mimeTypeForFileAtPath:filePath]; - } - [self addData:filePath withFileName:fileName andContentType:contentType forKey:key]; -} - -- (void)setFile:(NSString *)filePath forKey:(NSString *)key -{ - [self setFile:filePath withFileName:nil andContentType:nil forKey:key]; -} - -- (void)setFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - // Remove any existing value - NSUInteger i; - for (i=0; i<[[self fileData] count]; i++) { - NSDictionary *val = [[self fileData] objectAtIndex:i]; - if ([[val objectForKey:@"key"] isEqualToString:key]) { - [[self fileData] removeObjectAtIndex:i]; - i--; - } - } - [self addFile:data withFileName:fileName andContentType:contentType forKey:key]; -} - -- (void)addData:(NSData *)data forKey:(NSString *)key -{ - [self addData:data withFileName:@"file" andContentType:nil forKey:key]; -} - -- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - if (![self fileData]) { - [self setFileData:[NSMutableArray array]]; - } - if (!contentType) { - contentType = @"application/octet-stream"; - } - - NSMutableDictionary *fileInfo = [NSMutableDictionary dictionaryWithCapacity:4]; - [fileInfo setValue:key forKey:@"key"]; - [fileInfo setValue:fileName forKey:@"fileName"]; - [fileInfo setValue:contentType forKey:@"contentType"]; - [fileInfo setValue:data forKey:@"data"]; - - [[self fileData] addObject:fileInfo]; -} - -- (void)setData:(NSData *)data forKey:(NSString *)key -{ - [self setData:data withFileName:@"file" andContentType:nil forKey:key]; -} - -- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - // Remove any existing value - NSUInteger i; - for (i=0; i<[[self fileData] count]; i++) { - NSDictionary *val = [[self fileData] objectAtIndex:i]; - if ([[val objectForKey:@"key"] isEqualToString:key]) { - [[self fileData] removeObjectAtIndex:i]; - i--; - } - } - [self addData:data withFileName:fileName andContentType:contentType forKey:key]; -} - -- (void)buildPostBody -{ - if ([self haveBuiltPostBody]) { - return; - } - -#if DEBUG_FORM_DATA_REQUEST - [self setDebugBodyString:@""]; -#endif - - if (![self postData] && ![self fileData]) { - [super buildPostBody]; - return; - } - if ([[self fileData] count] > 0) { - [self setShouldStreamPostDataFromDisk:YES]; - } - - if ([self postFormat] == ASIURLEncodedPostFormat) { - [self buildURLEncodedPostBody]; - } else { - [self buildMultipartFormDataPostBody]; - } - - [super buildPostBody]; - -#if DEBUG_FORM_DATA_REQUEST - ASI_DEBUG_LOG(@"%@",[self debugBodyString]); - [self setDebugBodyString:nil]; -#endif -} - - -- (void)buildMultipartFormDataPostBody -{ -#if DEBUG_FORM_DATA_REQUEST - [self addToDebugBody:@"\r\n==== Building a multipart/form-data body ====\r\n"]; -#endif - - NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); - - // We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. - CFUUIDRef uuid = CFUUIDCreate(nil); - NSString *uuidString = [(NSString*)CFUUIDCreateString(nil, uuid) autorelease]; - CFRelease(uuid); - NSString *stringBoundary = [NSString stringWithFormat:@"0xKhTmLbOuNdArY-%@",uuidString]; - - [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; - - [self appendPostString:[NSString stringWithFormat:@"--%@\r\n",stringBoundary]]; - - // Adds post data - NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary]; - NSUInteger i=0; - for (NSDictionary *val in [self postData]) { - [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",[val objectForKey:@"key"]]]; - [self appendPostString:[val objectForKey:@"value"]]; - i++; - if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body - [self appendPostString:endItemBoundary]; - } - } - - // Adds files to upload - i=0; - for (NSDictionary *val in [self fileData]) { - - [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", [val objectForKey:@"key"], [val objectForKey:@"fileName"]]]; - [self appendPostString:[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [val objectForKey:@"contentType"]]]; - - id data = [val objectForKey:@"data"]; - if ([data isKindOfClass:[NSString class]]) { - [self appendPostDataFromFile:data]; - } else { - [self appendPostData:data]; - } - i++; - // Only add the boundary if this is not the last item in the post body - if (i != [[self fileData] count]) { - [self appendPostString:endItemBoundary]; - } - } - - [self appendPostString:[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary]]; - -#if DEBUG_FORM_DATA_REQUEST - [self addToDebugBody:@"==== End of multipart/form-data body ====\r\n"]; -#endif -} - -- (void)buildURLEncodedPostBody -{ - - // We can't post binary data using application/x-www-form-urlencoded - if ([[self fileData] count] > 0) { - [self setPostFormat:ASIMultipartFormDataPostFormat]; - [self buildMultipartFormDataPostBody]; - return; - } - -#if DEBUG_FORM_DATA_REQUEST - [self addToDebugBody:@"\r\n==== Building an application/x-www-form-urlencoded body ====\r\n"]; -#endif - - - NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); - - [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset]]; - - - NSUInteger i=0; - NSUInteger count = [[self postData] count]-1; - for (NSDictionary *val in [self postData]) { - NSString *data = [NSString stringWithFormat:@"%@=%@%@", [self encodeURL:[val objectForKey:@"key"]], [self encodeURL:[val objectForKey:@"value"]],(i -#if TARGET_OS_IPHONE - #import - #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - #import // Necessary for background task support - #endif -#endif - -#import -#import "ASIHTTPRequestConfig.h" -#import "ASIHTTPRequestDelegate.h" -#import "ASIProgressDelegate.h" -#import "ASICacheDelegate.h" - -@class ASIDataDecompressor; - -extern NSString *ASIHTTPRequestVersion; - -// Make targeting different platforms more reliable -// See: http://www.blumtnwerx.com/blog/2009/06/cross-sdk-code-hygiene-in-xcode/ -#ifndef __IPHONE_3_2 - #define __IPHONE_3_2 30200 -#endif -#ifndef __IPHONE_4_0 - #define __IPHONE_4_0 40000 -#endif -#ifndef __MAC_10_5 - #define __MAC_10_5 1050 -#endif -#ifndef __MAC_10_6 - #define __MAC_10_6 1060 -#endif - -typedef enum _ASIAuthenticationState { - ASINoAuthenticationNeededYet = 0, - ASIHTTPAuthenticationNeeded = 1, - ASIProxyAuthenticationNeeded = 2 -} ASIAuthenticationState; - -typedef enum _ASINetworkErrorType { - ASIConnectionFailureErrorType = 1, - ASIRequestTimedOutErrorType = 2, - ASIAuthenticationErrorType = 3, - ASIRequestCancelledErrorType = 4, - ASIUnableToCreateRequestErrorType = 5, - ASIInternalErrorWhileBuildingRequestType = 6, - ASIInternalErrorWhileApplyingCredentialsType = 7, - ASIFileManagementError = 8, - ASITooMuchRedirectionErrorType = 9, - ASIUnhandledExceptionError = 10, - ASICompressionError = 11 - -} ASINetworkErrorType; - - -// The error domain that all errors generated by ASIHTTPRequest use -extern NSString* const NetworkRequestErrorDomain; - -// You can use this number to throttle upload and download bandwidth in iPhone OS apps send or receive a large amount of data -// This may help apps that might otherwise be rejected for inclusion into the app store for using excessive bandwidth -// This number is not official, as far as I know there is no officially documented bandwidth limit -extern unsigned long const ASIWWANBandwidthThrottleAmount; - -#if NS_BLOCKS_AVAILABLE -typedef void (^ASIBasicBlock)(void); -typedef void (^ASIHeadersBlock)(NSDictionary *responseHeaders); -typedef void (^ASISizeBlock)(long long size); -typedef void (^ASIProgressBlock)(unsigned long long size, unsigned long long total); -typedef void (^ASIDataBlock)(NSData *data); -#endif - -@interface ASIHTTPRequest : NSOperation { - - // The url for this operation, should include GET params in the query string where appropriate - NSURL *url; - - // Will always contain the original url used for making the request (the value of url can change when a request is redirected) - NSURL *originalURL; - - // Temporarily stores the url we are about to redirect to. Will be nil again when we do redirect - NSURL *redirectURL; - - // The delegate - will be notified of various changes in state via the ASIHTTPRequestDelegate protocol - id delegate; - - // Another delegate that is also notified of request status changes and progress updates - // Generally, you won't use this directly, but ASINetworkQueue sets itself as the queue so it can proxy updates to its own delegates - // NOTE: WILL BE RETAINED BY THE REQUEST - id queue; - - // HTTP method to use (eg: GET / POST / PUT / DELETE / HEAD etc). Defaults to GET - NSString *requestMethod; - - // Request body - only used when the whole body is stored in memory (shouldStreamPostDataFromDisk is false) - NSMutableData *postBody; - - // gzipped request body used when shouldCompressRequestBody is YES - NSData *compressedPostBody; - - // When true, post body will be streamed from a file on disk, rather than loaded into memory at once (useful for large uploads) - // Automatically set to true in ASIFormDataRequests when using setFile:forKey: - BOOL shouldStreamPostDataFromDisk; - - // Path to file used to store post body (when shouldStreamPostDataFromDisk is true) - // You can set this yourself - useful if you want to PUT a file from local disk - NSString *postBodyFilePath; - - // Path to a temporary file used to store a deflated post body (when shouldCompressPostBody is YES) - NSString *compressedPostBodyFilePath; - - // Set to true when ASIHTTPRequest automatically created a temporary file containing the request body (when true, the file at postBodyFilePath will be deleted at the end of the request) - BOOL didCreateTemporaryPostDataFile; - - // Used when writing to the post body when shouldStreamPostDataFromDisk is true (via appendPostData: or appendPostDataFromFile:) - NSOutputStream *postBodyWriteStream; - - // Used for reading from the post body when sending the request - NSInputStream *postBodyReadStream; - - // Dictionary for custom HTTP request headers - NSMutableDictionary *requestHeaders; - - // Set to YES when the request header dictionary has been populated, used to prevent this happening more than once - BOOL haveBuiltRequestHeaders; - - // Will be populated with HTTP response headers from the server - NSDictionary *responseHeaders; - - // Can be used to manually insert cookie headers to a request, but it's more likely that sessionCookies will do this for you - NSMutableArray *requestCookies; - - // Will be populated with cookies - NSArray *responseCookies; - - // If use useCookiePersistence is true, network requests will present valid cookies from previous requests - BOOL useCookiePersistence; - - // If useKeychainPersistence is true, network requests will attempt to read credentials from the keychain, and will save them in the keychain when they are successfully presented - BOOL useKeychainPersistence; - - // If useSessionPersistence is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called) - BOOL useSessionPersistence; - - // If allowCompressedResponse is true, requests will inform the server they can accept compressed data, and will automatically decompress gzipped responses. Default is true. - BOOL allowCompressedResponse; - - // If shouldCompressRequestBody is true, the request body will be gzipped. Default is false. - // You will probably need to enable this feature on your webserver to make this work. Tested with apache only. - BOOL shouldCompressRequestBody; - - // When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location - // If downloadDestinationPath is not set, download data will be stored in memory - NSString *downloadDestinationPath; - - // The location that files will be downloaded to. Once a download is complete, files will be decompressed (if necessary) and moved to downloadDestinationPath - NSString *temporaryFileDownloadPath; - - // If the response is gzipped and shouldWaitToInflateCompressedResponses is NO, a file will be created at this path containing the inflated response as it comes in - NSString *temporaryUncompressedDataDownloadPath; - - // Used for writing data to a file when downloadDestinationPath is set - NSOutputStream *fileDownloadOutputStream; - - NSOutputStream *inflatedFileDownloadOutputStream; - - // When the request fails or completes successfully, complete will be true - BOOL complete; - - // external "finished" indicator, subject of KVO notifications; updates after 'complete' - BOOL finished; - - // True if our 'cancel' selector has been called - BOOL cancelled; - - // If an error occurs, error will contain an NSError - // If error code is = ASIConnectionFailureErrorType (1, Connection failure occurred) - inspect [[error userInfo] objectForKey:NSUnderlyingErrorKey] for more information - NSError *error; - - // Username and password used for authentication - NSString *username; - NSString *password; - - // User-Agent for this request - NSString *userAgentString; - - // Domain used for NTLM authentication - NSString *domain; - - // Username and password used for proxy authentication - NSString *proxyUsername; - NSString *proxyPassword; - - // Domain used for NTLM proxy authentication - NSString *proxyDomain; - - // Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) - id uploadProgressDelegate; - - // Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) - id downloadProgressDelegate; - - // Whether we've seen the headers of the response yet - BOOL haveExaminedHeaders; - - // Data we receive will be stored here. Data may be compressed unless allowCompressedResponse is false - you should use [request responseData] instead in most cases - NSMutableData *rawResponseData; - - // Used for sending and receiving data - CFHTTPMessageRef request; - NSInputStream *readStream; - - // Used for authentication - CFHTTPAuthenticationRef requestAuthentication; - NSDictionary *requestCredentials; - - // Used during NTLM authentication - int authenticationRetryCount; - - // Authentication scheme (Basic, Digest, NTLM) - // If you are using Basic authentication and want to force ASIHTTPRequest to send an authorization header without waiting for a 401, you must set this to (NSString *)kCFHTTPAuthenticationSchemeBasic - NSString *authenticationScheme; - - // Realm for authentication when credentials are required - NSString *authenticationRealm; - - // When YES, ASIHTTPRequest will present a dialog allowing users to enter credentials when no-matching credentials were found for a server that requires authentication - // The dialog will not be shown if your delegate responds to authenticationNeededForRequest: - // Default is NO. - BOOL shouldPresentAuthenticationDialog; - - // When YES, ASIHTTPRequest will present a dialog allowing users to enter credentials when no-matching credentials were found for a proxy server that requires authentication - // The dialog will not be shown if your delegate responds to proxyAuthenticationNeededForRequest: - // Default is YES (basically, because most people won't want the hassle of adding support for authenticating proxies to their apps) - BOOL shouldPresentProxyAuthenticationDialog; - - // Used for proxy authentication - CFHTTPAuthenticationRef proxyAuthentication; - NSDictionary *proxyCredentials; - - // Used during authentication with an NTLM proxy - int proxyAuthenticationRetryCount; - - // Authentication scheme for the proxy (Basic, Digest, NTLM) - NSString *proxyAuthenticationScheme; - - // Realm for proxy authentication when credentials are required - NSString *proxyAuthenticationRealm; - - // HTTP status code, eg: 200 = OK, 404 = Not found etc - int responseStatusCode; - - // Description of the HTTP status code - NSString *responseStatusMessage; - - // Size of the response - unsigned long long contentLength; - - // Size of the partially downloaded content - unsigned long long partialDownloadSize; - - // Size of the POST payload - unsigned long long postLength; - - // The total amount of downloaded data - unsigned long long totalBytesRead; - - // The total amount of uploaded data - unsigned long long totalBytesSent; - - // Last amount of data read (used for incrementing progress) - unsigned long long lastBytesRead; - - // Last amount of data sent (used for incrementing progress) - unsigned long long lastBytesSent; - - // This lock prevents the operation from being cancelled at an inopportune moment - NSRecursiveLock *cancelledLock; - - // Called on the delegate (if implemented) when the request starts. Default is requestStarted: - SEL didStartSelector; - - // Called on the delegate (if implemented) when the request receives response headers. Default is request:didReceiveResponseHeaders: - SEL didReceiveResponseHeadersSelector; - - // Called on the delegate (if implemented) when the request receives a Location header and shouldRedirect is YES - // The delegate can then change the url if needed, and can restart the request by calling [request redirectToURL:], or simply cancel it - SEL willRedirectSelector; - - // Called on the delegate (if implemented) when the request completes successfully. Default is requestFinished: - SEL didFinishSelector; - - // Called on the delegate (if implemented) when the request fails. Default is requestFailed: - SEL didFailSelector; - - // Called on the delegate (if implemented) when the request receives data. Default is request:didReceiveData: - // If you set this and implement the method in your delegate, you must handle the data yourself - ASIHTTPRequest will not populate responseData or write the data to downloadDestinationPath - SEL didReceiveDataSelector; - - // Used for recording when something last happened during the request, we will compare this value with the current date to time out requests when appropriate - NSDate *lastActivityTime; - - // Number of seconds to wait before timing out - default is 10 - NSTimeInterval timeOutSeconds; - - // Will be YES when a HEAD request will handle the content-length before this request starts - BOOL shouldResetUploadProgress; - BOOL shouldResetDownloadProgress; - - // Used by HEAD requests when showAccurateProgress is YES to preset the content-length for this request - ASIHTTPRequest *mainRequest; - - // When NO, this request will only update the progress indicator when it completes - // When YES, this request will update the progress indicator according to how much data it has received so far - // The default for requests is YES - // Also see the comments in ASINetworkQueue.h - BOOL showAccurateProgress; - - // Used to ensure the progress indicator is only incremented once when showAccurateProgress = NO - BOOL updatedProgress; - - // Prevents the body of the post being built more than once (largely for subclasses) - BOOL haveBuiltPostBody; - - // Used internally, may reflect the size of the internal buffer used by CFNetwork - // POST / PUT operations with body sizes greater than uploadBufferSize will not timeout unless more than uploadBufferSize bytes have been sent - // Likely to be 32KB on iPhone 3.0, 128KB on Mac OS X Leopard and iPhone 2.2.x - unsigned long long uploadBufferSize; - - // Text encoding for responses that do not send a Content-Type with a charset value. Defaults to NSISOLatin1StringEncoding - NSStringEncoding defaultResponseEncoding; - - // The text encoding of the response, will be defaultResponseEncoding if the server didn't specify. Can't be set. - NSStringEncoding responseEncoding; - - // Tells ASIHTTPRequest not to delete partial downloads, and allows it to use an existing file to resume a download. Defaults to NO. - BOOL allowResumeForFileDownloads; - - // Custom user information associated with the request (not sent to the server) - NSDictionary *userInfo; - NSInteger tag; - - // Use HTTP 1.0 rather than 1.1 (defaults to false) - BOOL useHTTPVersionOne; - - // When YES, requests will automatically redirect when they get a HTTP 30x header (defaults to YES) - BOOL shouldRedirect; - - // Used internally to tell the main loop we need to stop and retry with a new url - BOOL needsRedirect; - - // Incremented every time this request redirects. When it reaches 5, we give up - int redirectCount; - - // When NO, requests will not check the secure certificate is valid (use for self-signed certificates during development, DO NOT USE IN PRODUCTION) Default is YES - BOOL validatesSecureCertificate; - - // If not nil and the URL scheme is https, CFNetwork configured to supply a client certificate - SecIdentityRef clientCertificateIdentity; - NSArray *clientCertificates; - - // Details on the proxy to use - you could set these yourself, but it's probably best to let ASIHTTPRequest detect the system proxy settings - NSString *proxyHost; - int proxyPort; - - // ASIHTTPRequest will assume kCFProxyTypeHTTP if the proxy type could not be automatically determined - // Set to kCFProxyTypeSOCKS if you are manually configuring a SOCKS proxy - NSString *proxyType; - - // URL for a PAC (Proxy Auto Configuration) file. If you want to set this yourself, it's probably best if you use a local file - NSURL *PACurl; - - // See ASIAuthenticationState values above. 0 == default == No authentication needed yet - ASIAuthenticationState authenticationNeeded; - - // When YES, ASIHTTPRequests will present credentials from the session store for requests to the same server before being asked for them - // This avoids an extra round trip for requests after authentication has succeeded, which is much for efficient for authenticated requests with large bodies, or on slower connections - // Set to NO to only present credentials when explicitly asked for them - // This only affects credentials stored in the session cache when useSessionPersistence is YES. Credentials from the keychain are never presented unless the server asks for them - // Default is YES - // For requests using Basic authentication, set authenticationScheme to (NSString *)kCFHTTPAuthenticationSchemeBasic, and credentials can be sent on the very first request when shouldPresentCredentialsBeforeChallenge is YES - BOOL shouldPresentCredentialsBeforeChallenge; - - // YES when the request hasn't finished yet. Will still be YES even if the request isn't doing anything (eg it's waiting for delegate authentication). READ-ONLY - BOOL inProgress; - - // Used internally to track whether the stream is scheduled on the run loop or not - // Bandwidth throttling can unschedule the stream to slow things down while a request is in progress - BOOL readStreamIsScheduled; - - // Set to allow a request to automatically retry itself on timeout - // Default is zero - timeout will stop the request - int numberOfTimesToRetryOnTimeout; - - // The number of times this request has retried (when numberOfTimesToRetryOnTimeout > 0) - int retryCount; - - // Temporarily set to YES when a closed connection forces a retry (internally, this stops ASIHTTPRequest cleaning up a temporary post body) - BOOL willRetryRequest; - - // When YES, requests will keep the connection to the server alive for a while to allow subsequent requests to re-use it for a substantial speed-boost - // Persistent connections will not be used if the server explicitly closes the connection - // Default is YES - BOOL shouldAttemptPersistentConnection; - - // Number of seconds to keep an inactive persistent connection open on the client side - // Default is 60 - // If we get a keep-alive header, this is this value is replaced with how long the server told us to keep the connection around - // A future date is created from this and used for expiring the connection, this is stored in connectionInfo's expires value - NSTimeInterval persistentConnectionTimeoutSeconds; - - // Set to yes when an appropriate keep-alive header is found - BOOL connectionCanBeReused; - - // Stores information about the persistent connection that is currently in use. - // It may contain: - // * The id we set for a particular connection, incremented every time we want to specify that we need a new connection - // * The date that connection should expire - // * A host, port and scheme for the connection. These are used to determine whether that connection can be reused by a subsequent request (all must match the new request) - // * An id for the request that is currently using the connection. This is used for determining if a connection is available or not (we store a number rather than a reference to the request so we don't need to hang onto a request until the connection expires) - // * A reference to the stream that is currently using the connection. This is necessary because we need to keep the old stream open until we've opened a new one. - // The stream will be closed + released either when another request comes to use the connection, or when the timer fires to tell the connection to expire - NSMutableDictionary *connectionInfo; - - // When set to YES, 301 and 302 automatic redirects will use the original method and and body, according to the HTTP 1.1 standard - // Default is NO (to follow the behaviour of most browsers) - BOOL shouldUseRFC2616RedirectBehaviour; - - // Used internally to record when a request has finished downloading data - BOOL downloadComplete; - - // An ID that uniquely identifies this request - primarily used for debugging persistent connections - NSNumber *requestID; - - // Will be ASIHTTPRequestRunLoopMode for synchronous requests, NSDefaultRunLoopMode for all other requests - NSString *runLoopMode; - - // This timer checks up on the request every 0.25 seconds, and updates progress - NSTimer *statusTimer; - - // The download cache that will be used for this request (use [ASIHTTPRequest setDefaultCache:cache] to configure a default cache - id downloadCache; - - // The cache policy that will be used for this request - See ASICacheDelegate.h for possible values - ASICachePolicy cachePolicy; - - // The cache storage policy that will be used for this request - See ASICacheDelegate.h for possible values - ASICacheStoragePolicy cacheStoragePolicy; - - // Will be true when the response was pulled from the cache rather than downloaded - BOOL didUseCachedResponse; - - // Set secondsToCache to use a custom time interval for expiring the response when it is stored in a cache - NSTimeInterval secondsToCache; - - #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - BOOL shouldContinueWhenAppEntersBackground; - UIBackgroundTaskIdentifier backgroundTask; - #endif - - // When downloading a gzipped response, the request will use this helper object to inflate the response - ASIDataDecompressor *dataDecompressor; - - // Controls how responses with a gzipped encoding are inflated (decompressed) - // When set to YES (This is the default): - // * gzipped responses for requests without a downloadDestinationPath will be inflated only when [request responseData] / [request responseString] is called - // * gzipped responses for requests with a downloadDestinationPath set will be inflated only when the request completes - // - // When set to NO - // All requests will inflate the response as it comes in - // * If the request has no downloadDestinationPath set, the raw (compressed) response is discarded and rawResponseData will contain the decompressed response - // * If the request has a downloadDestinationPath, the raw response will be stored in temporaryFileDownloadPath as normal, the inflated response will be stored in temporaryUncompressedDataDownloadPath - // Once the request completes successfully, the contents of temporaryUncompressedDataDownloadPath are moved into downloadDestinationPath - // - // Setting this to NO may be especially useful for users using ASIHTTPRequest in conjunction with a streaming parser, as it will allow partial gzipped responses to be inflated and passed on to the parser while the request is still running - BOOL shouldWaitToInflateCompressedResponses; - - // Will be YES if this is a request created behind the scenes to download a PAC file - these requests do not attempt to configure their own proxies - BOOL isPACFileRequest; - - // Used for downloading PAC files from http / https webservers - ASIHTTPRequest *PACFileRequest; - - // Used for asynchronously reading PAC files from file:// URLs - NSInputStream *PACFileReadStream; - - // Used for storing PAC data from file URLs as it is downloaded - NSMutableData *PACFileData; - - // Set to YES in startSynchronous. Currently used by proxy detection to download PAC files synchronously when appropriate - BOOL isSynchronous; - - #if NS_BLOCKS_AVAILABLE - //block to execute when request starts - ASIBasicBlock startedBlock; - - //block to execute when headers are received - ASIHeadersBlock headersReceivedBlock; - - //block to execute when request completes successfully - ASIBasicBlock completionBlock; - - //block to execute when request fails - ASIBasicBlock failureBlock; - - //block for when bytes are received - ASIProgressBlock bytesReceivedBlock; - - //block for when bytes are sent - ASIProgressBlock bytesSentBlock; - - //block for when download size is incremented - ASISizeBlock downloadSizeIncrementedBlock; - - //block for when upload size is incremented - ASISizeBlock uploadSizeIncrementedBlock; - - //block for handling raw bytes received - ASIDataBlock dataReceivedBlock; - - //block for handling authentication - ASIBasicBlock authenticationNeededBlock; - - //block for handling proxy authentication - ASIBasicBlock proxyAuthenticationNeededBlock; - - //block for handling redirections, if you want to - ASIBasicBlock requestRedirectedBlock; - #endif -} - -#pragma mark init / dealloc - -// Should be an HTTP or HTTPS url, may include username and password if appropriate -- (id)initWithURL:(NSURL *)newURL; - -// Convenience constructor -+ (id)requestWithURL:(NSURL *)newURL; - -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache; -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache andCachePolicy:(ASICachePolicy)policy; - -#if NS_BLOCKS_AVAILABLE -- (void)setStartedBlock:(ASIBasicBlock)aStartedBlock; -- (void)setHeadersReceivedBlock:(ASIHeadersBlock)aReceivedBlock; -- (void)setCompletionBlock:(ASIBasicBlock)aCompletionBlock; -- (void)setFailedBlock:(ASIBasicBlock)aFailedBlock; -- (void)setBytesReceivedBlock:(ASIProgressBlock)aBytesReceivedBlock; -- (void)setBytesSentBlock:(ASIProgressBlock)aBytesSentBlock; -- (void)setDownloadSizeIncrementedBlock:(ASISizeBlock) aDownloadSizeIncrementedBlock; -- (void)setUploadSizeIncrementedBlock:(ASISizeBlock) anUploadSizeIncrementedBlock; -- (void)setDataReceivedBlock:(ASIDataBlock)aReceivedBlock; -- (void)setAuthenticationNeededBlock:(ASIBasicBlock)anAuthenticationBlock; -- (void)setProxyAuthenticationNeededBlock:(ASIBasicBlock)aProxyAuthenticationBlock; -- (void)setRequestRedirectedBlock:(ASIBasicBlock)aRedirectBlock; -#endif - -#pragma mark setup request - -// Add a custom header to the request -- (void)addRequestHeader:(NSString *)header value:(NSString *)value; - -// Called during buildRequestHeaders and after a redirect to create a cookie header from request cookies and the global store -- (void)applyCookieHeader; - -// Populate the request headers dictionary. Called before a request is started, or by a HEAD request that needs to borrow them -- (void)buildRequestHeaders; - -// Used to apply authorization header to a request before it is sent (when shouldPresentCredentialsBeforeChallenge is YES) -- (void)applyAuthorizationHeader; - - -// Create the post body -- (void)buildPostBody; - -// Called to add data to the post body. Will append to postBody when shouldStreamPostDataFromDisk is false, or write to postBodyWriteStream when true -- (void)appendPostData:(NSData *)data; -- (void)appendPostDataFromFile:(NSString *)file; - -#pragma mark get information about this request - -// Returns the contents of the result as an NSString (not appropriate for binary data - used responseData instead) -- (NSString *)responseString; - -// Response data, automatically uncompressed where appropriate -- (NSData *)responseData; - -// Returns true if the response was gzip compressed -- (BOOL)isResponseCompressed; - -#pragma mark running a request - - -// Run a request synchronously, and return control when the request completes or fails -- (void)startSynchronous; - -// Run request in the background -- (void)startAsynchronous; - -// Clears all delegates and blocks, then cancels the request -- (void)clearDelegatesAndCancel; - -#pragma mark HEAD request - -// Used by ASINetworkQueue to create a HEAD request appropriate for this request with the same headers (though you can use it yourself) -- (ASIHTTPRequest *)HEADRequest; - -#pragma mark upload/download progress - -// Called approximately every 0.25 seconds to update the progress delegates -- (void)updateProgressIndicators; - -// Updates upload progress (notifies the queue and/or uploadProgressDelegate of this request) -- (void)updateUploadProgress; - -// Updates download progress (notifies the queue and/or uploadProgressDelegate of this request) -- (void)updateDownloadProgress; - -// Called when authorisation is needed, as we only find out we don't have permission to something when the upload is complete -- (void)removeUploadProgressSoFar; - -// Called when we get a content-length header and shouldResetDownloadProgress is true -- (void)incrementDownloadSizeBy:(long long)length; - -// Called when a request starts and shouldResetUploadProgress is true -// Also called (with a negative length) to remove the size of the underlying buffer used for uploading -- (void)incrementUploadSizeBy:(long long)length; - -// Helper method for interacting with progress indicators to abstract the details of different APIS (NSProgressIndicator and UIProgressView) -+ (void)updateProgressIndicator:(id *)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total; - -// Helper method used for performing invocations on the main thread (used for progress) -+ (void)performSelector:(SEL)selector onTarget:(id *)target withObject:(id)object amount:(void *)amount callerToRetain:(id)caller; - -#pragma mark talking to delegates - -// Called when a request starts, lets the delegate know via didStartSelector -- (void)requestStarted; - -// Called when a request receives response headers, lets the delegate know via didReceiveResponseHeadersSelector -- (void)requestReceivedResponseHeaders:(NSDictionary *)newHeaders; - -// Called when a request completes successfully, lets the delegate know via didFinishSelector -- (void)requestFinished; - -// Called when a request fails, and lets the delegate know via didFailSelector -- (void)failWithError:(NSError *)theError; - -// Called to retry our request when our persistent connection is closed -// Returns YES if we haven't already retried, and connection will be restarted -// Otherwise, returns NO, and nothing will happen -- (BOOL)retryUsingNewConnection; - -// Can be called by delegates from inside their willRedirectSelector implementations to restart the request with a new url -- (void)redirectToURL:(NSURL *)newURL; - -#pragma mark parsing HTTP response headers - -// Reads the response headers to find the content length, encoding, cookies for the session -// Also initiates request redirection when shouldRedirect is true -// And works out if HTTP auth is required -- (void)readResponseHeaders; - -// Attempts to set the correct encoding by looking at the Content-Type header, if this is one -- (void)parseStringEncodingFromHeaders; - -+ (void)parseMimeType:(NSString **)mimeType andResponseEncoding:(NSStringEncoding *)stringEncoding fromContentType:(NSString *)contentType; - -#pragma mark http authentication stuff - -// Apply credentials to this request -- (BOOL)applyCredentials:(NSDictionary *)newCredentials; -- (BOOL)applyProxyCredentials:(NSDictionary *)newCredentials; - -// Attempt to obtain credentials for this request from the URL, username and password or keychain -- (NSMutableDictionary *)findCredentials; -- (NSMutableDictionary *)findProxyCredentials; - -// Unlock (unpause) the request thread so it can resume the request -// Should be called by delegates when they have populated the authentication information after an authentication challenge -- (void)retryUsingSuppliedCredentials; - -// Should be called by delegates when they wish to cancel authentication and stop -- (void)cancelAuthentication; - -// Apply authentication information and resume the request after an authentication challenge -- (void)attemptToApplyCredentialsAndResume; -- (void)attemptToApplyProxyCredentialsAndResume; - -// Attempt to show the built-in authentication dialog, returns YES if credentials were supplied, NO if user cancelled dialog / dialog is disabled / running on main thread -// Currently only used on iPhone OS -- (BOOL)showProxyAuthenticationDialog; -- (BOOL)showAuthenticationDialog; - -// Construct a basic authentication header from the username and password supplied, and add it to the request headers -// Used when shouldPresentCredentialsBeforeChallenge is YES -- (void)addBasicAuthenticationHeaderWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword; - -#pragma mark stream status handlers - -// CFnetwork event handlers -- (void)handleNetworkEvent:(CFStreamEventType)type; -- (void)handleBytesAvailable; -- (void)handleStreamComplete; -- (void)handleStreamError; - -#pragma mark cleanup - -// Cleans up and lets the queue know this operation is finished. -// Appears in this header for subclassing only, do not call this method from outside your request! -- (void)markAsFinished; - -// Cleans up temporary files. There's normally no reason to call these yourself, they are called automatically when a request completes or fails - -// Clean up the temporary file used to store the downloaded data when it comes in (if downloadDestinationPath is set) -- (BOOL)removeTemporaryDownloadFile; - -// Clean up the temporary file used to store data that is inflated (decompressed) as it comes in -- (BOOL)removeTemporaryUncompressedDownloadFile; - -// Clean up the temporary file used to store the request body (when shouldStreamPostDataFromDisk is YES) -- (BOOL)removeTemporaryUploadFile; - -// Clean up the temporary file used to store a deflated (compressed) request body when shouldStreamPostDataFromDisk is YES -- (BOOL)removeTemporaryCompressedUploadFile; - -// Remove a file on disk, returning NO and populating the passed error pointer if it fails -+ (BOOL)removeFileAtPath:(NSString *)path error:(NSError **)err; - -#pragma mark persistent connections - -// Get the ID of the connection this request used (only really useful in tests and debugging) -- (NSNumber *)connectionID; - -// Called automatically when a request is started to clean up any persistent connections that have expired -+ (void)expirePersistentConnections; - -#pragma mark default time out - -+ (NSTimeInterval)defaultTimeOutSeconds; -+ (void)setDefaultTimeOutSeconds:(NSTimeInterval)newTimeOutSeconds; - -#pragma mark client certificate - -- (void)setClientCertificateIdentity:(SecIdentityRef)anIdentity; - -#pragma mark session credentials - -+ (NSMutableArray *)sessionProxyCredentialsStore; -+ (NSMutableArray *)sessionCredentialsStore; - -+ (void)storeProxyAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials; -+ (void)storeAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials; - -+ (void)removeProxyAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials; -+ (void)removeAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials; - -- (NSDictionary *)findSessionProxyAuthenticationCredentials; -- (NSDictionary *)findSessionAuthenticationCredentials; - -#pragma mark keychain storage - -// Save credentials for this request to the keychain -- (void)saveCredentialsToKeychain:(NSDictionary *)newCredentials; - -// Save credentials to the keychain -+ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; -+ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm; - -// Return credentials from the keychain -+ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; -+ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; - -// Remove credentials from the keychain -+ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; -+ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm; - -// We keep track of any cookies we accept, so that we can remove them from the persistent store later -+ (void)setSessionCookies:(NSMutableArray *)newSessionCookies; -+ (NSMutableArray *)sessionCookies; - -// Adds a cookie to our list of cookies we've accepted, checking first for an old version of the same cookie and removing that -+ (void)addSessionCookie:(NSHTTPCookie *)newCookie; - -// Dump all session data (authentication and cookies) -+ (void)clearSession; - -#pragma mark get user agent - -// Will be used as a user agent if requests do not specify a custom user agent -// Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist -+ (NSString *)defaultUserAgentString; -+ (void)setDefaultUserAgentString:(NSString *)agent; - -#pragma mark mime-type detection - -// Return the mime type for a file -+ (NSString *)mimeTypeForFileAtPath:(NSString *)path; - -#pragma mark bandwidth measurement / throttling - -// The maximum number of bytes ALL requests can send / receive in a second -// This is a rough figure. The actual amount used will be slightly more, this does not include HTTP headers -+ (unsigned long)maxBandwidthPerSecond; -+ (void)setMaxBandwidthPerSecond:(unsigned long)bytes; - -// Get a rough average (for the last 5 seconds) of how much bandwidth is being used, in bytes -+ (unsigned long)averageBandwidthUsedPerSecond; - -- (void)performThrottling; - -// Will return YES is bandwidth throttling is currently in use -+ (BOOL)isBandwidthThrottled; - -// Used internally to record bandwidth use, and by ASIInputStreams when uploading. It's probably best if you don't mess with this. -+ (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes; - -// On iPhone, ASIHTTPRequest can automatically turn throttling on and off as the connection type changes between WWAN and WiFi - -#if TARGET_OS_IPHONE -// Set to YES to automatically turn on throttling when WWAN is connected, and automatically turn it off when it isn't -+ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle; - -// Turns on throttling automatically when WWAN is connected using a custom limit, and turns it off automatically when it isn't -+ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit; - -#pragma mark reachability - -// Returns YES when an iPhone OS device is connected via WWAN, false when connected via WIFI or not connected -+ (BOOL)isNetworkReachableViaWWAN; - -#endif - -#pragma mark queue - -// Returns the shared queue -+ (NSOperationQueue *)sharedQueue; - -#pragma mark cache - -+ (void)setDefaultCache:(id )cache; -+ (id )defaultCache; - -// Returns the maximum amount of data we can read as part of the current measurement period, and sleeps this thread if our allowance is used up -+ (unsigned long)maxUploadReadLength; - -#pragma mark network activity - -+ (BOOL)isNetworkInUse; - -+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate; - -// Shows the network activity spinner thing on iOS. You may wish to override this to do something else in Mac projects -+ (void)showNetworkActivityIndicator; - -// Hides the network activity spinner thing on iOS -+ (void)hideNetworkActivityIndicator; - -#pragma mark miscellany - -// Used for generating Authorization header when using basic authentication when shouldPresentCredentialsBeforeChallenge is true -// And also by ASIS3Request -+ (NSString *)base64forData:(NSData *)theData; - -// Returns the expiration date for the request. -// Calculated from the Expires response header property, unless maxAge is non-zero or -// there exists a non-zero max-age property in the Cache-Control response header. -+ (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; - -// Returns a date from a string in RFC1123 format -+ (NSDate *)dateFromRFC1123String:(NSString *)string; - - -// Used for detecting multitasking support at runtime (for backgrounding requests) -#if TARGET_OS_IPHONE -+ (BOOL)isMultitaskingSupported; -#endif - -#pragma mark threading behaviour - -// In the default implementation, all requests run in a single background thread -// Advanced users only: Override this method in a subclass for a different threading behaviour -// Eg: return [NSThread mainThread] to run all requests in the main thread -// Alternatively, you can create a thread on demand, or manage a pool of threads -// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun()) -// Requests will stop the runloop when they complete -// If you have multiple requests sharing the thread you'll need to restart the runloop when this happens -+ (NSThread *)threadForRequest:(ASIHTTPRequest *)request; - - -#pragma mark === - -@property (atomic, retain) NSString *username; -@property (atomic, retain) NSString *password; -@property (atomic, retain) NSString *userAgentString; -@property (atomic, retain) NSString *domain; - -@property (atomic, retain) NSString *proxyUsername; -@property (atomic, retain) NSString *proxyPassword; -@property (atomic, retain) NSString *proxyDomain; - -@property (atomic, retain) NSString *proxyHost; -@property (atomic, assign) int proxyPort; -@property (atomic, retain) NSString *proxyType; - -@property (retain,setter=setURL:, nonatomic) NSURL *url; -@property (atomic, retain) NSURL *originalURL; -@property (assign, nonatomic) id delegate; -@property (retain, nonatomic) id queue; -@property (assign, nonatomic) id uploadProgressDelegate; -@property (assign, nonatomic) id downloadProgressDelegate; -@property (atomic, assign) BOOL useKeychainPersistence; -@property (atomic, assign) BOOL useSessionPersistence; -@property (atomic, retain) NSString *downloadDestinationPath; -@property (atomic, retain) NSString *temporaryFileDownloadPath; -@property (atomic, retain) NSString *temporaryUncompressedDataDownloadPath; -@property (atomic, assign) SEL didStartSelector; -@property (atomic, assign) SEL didReceiveResponseHeadersSelector; -@property (atomic, assign) SEL willRedirectSelector; -@property (atomic, assign) SEL didFinishSelector; -@property (atomic, assign) SEL didFailSelector; -@property (atomic, assign) SEL didReceiveDataSelector; -@property (atomic, retain,readonly) NSString *authenticationRealm; -@property (atomic, retain,readonly) NSString *proxyAuthenticationRealm; -@property (atomic, retain) NSError *error; -@property (atomic, assign,readonly) BOOL complete; -@property (atomic, retain) NSDictionary *responseHeaders; -@property (atomic, retain) NSMutableDictionary *requestHeaders; -@property (atomic, retain) NSMutableArray *requestCookies; -@property (atomic, retain,readonly) NSArray *responseCookies; -@property (atomic, assign) BOOL useCookiePersistence; -@property (atomic, retain) NSDictionary *requestCredentials; -@property (atomic, retain) NSDictionary *proxyCredentials; -@property (atomic, assign,readonly) int responseStatusCode; -@property (atomic, retain,readonly) NSString *responseStatusMessage; -@property (atomic, retain) NSMutableData *rawResponseData; -@property (atomic, assign) NSTimeInterval timeOutSeconds; -@property (retain, nonatomic) NSString *requestMethod; -@property (atomic, retain) NSMutableData *postBody; -@property (atomic, assign) unsigned long long contentLength; -@property (atomic, assign) unsigned long long postLength; -@property (atomic, assign) BOOL shouldResetDownloadProgress; -@property (atomic, assign) BOOL shouldResetUploadProgress; -@property (atomic, assign) ASIHTTPRequest *mainRequest; -@property (atomic, assign) BOOL showAccurateProgress; -@property (atomic, assign) unsigned long long totalBytesRead; -@property (atomic, assign) unsigned long long totalBytesSent; -@property (atomic, assign) NSStringEncoding defaultResponseEncoding; -@property (atomic, assign) NSStringEncoding responseEncoding; -@property (atomic, assign) BOOL allowCompressedResponse; -@property (atomic, assign) BOOL allowResumeForFileDownloads; -@property (atomic, retain) NSDictionary *userInfo; -@property (atomic, assign) NSInteger tag; -@property (atomic, retain) NSString *postBodyFilePath; -@property (atomic, assign) BOOL shouldStreamPostDataFromDisk; -@property (atomic, assign) BOOL didCreateTemporaryPostDataFile; -@property (atomic, assign) BOOL useHTTPVersionOne; -@property (atomic, assign, readonly) unsigned long long partialDownloadSize; -@property (atomic, assign) BOOL shouldRedirect; -@property (atomic, assign) BOOL validatesSecureCertificate; -@property (atomic, assign) BOOL shouldCompressRequestBody; -@property (atomic, retain) NSURL *PACurl; -@property (atomic, retain) NSString *authenticationScheme; -@property (atomic, retain) NSString *proxyAuthenticationScheme; -@property (atomic, assign) BOOL shouldPresentAuthenticationDialog; -@property (atomic, assign) BOOL shouldPresentProxyAuthenticationDialog; -@property (atomic, assign, readonly) ASIAuthenticationState authenticationNeeded; -@property (atomic, assign) BOOL shouldPresentCredentialsBeforeChallenge; -@property (atomic, assign, readonly) int authenticationRetryCount; -@property (atomic, assign, readonly) int proxyAuthenticationRetryCount; -@property (atomic, assign) BOOL haveBuiltRequestHeaders; -@property (assign, nonatomic) BOOL haveBuiltPostBody; -@property (atomic, assign, readonly) BOOL inProgress; -@property (atomic, assign) int numberOfTimesToRetryOnTimeout; -@property (atomic, assign, readonly) int retryCount; -@property (atomic, assign) BOOL shouldAttemptPersistentConnection; -@property (atomic, atomic, assign) NSTimeInterval persistentConnectionTimeoutSeconds; -@property (atomic, assign) BOOL shouldUseRFC2616RedirectBehaviour; -@property (atomic, assign, readonly) BOOL connectionCanBeReused; -@property (atomic, retain, readonly) NSNumber *requestID; -@property (atomic, assign) id downloadCache; -@property (atomic, assign) ASICachePolicy cachePolicy; -@property (atomic, assign) ASICacheStoragePolicy cacheStoragePolicy; -@property (atomic, assign, readonly) BOOL didUseCachedResponse; -@property (atomic, assign) NSTimeInterval secondsToCache; -@property (atomic, retain) NSArray *clientCertificates; -#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 -@property (atomic, assign) BOOL shouldContinueWhenAppEntersBackground; -#endif -@property (atomic, retain) ASIDataDecompressor *dataDecompressor; -@property (atomic, assign) BOOL shouldWaitToInflateCompressedResponses; - -@end diff --git a/clients/ios/ASI/ASIHTTPRequest.m b/clients/ios/ASI/ASIHTTPRequest.m deleted file mode 100755 index be2cfe1ce..000000000 --- a/clients/ios/ASI/ASIHTTPRequest.m +++ /dev/null @@ -1,5141 +0,0 @@ -// -// ASIHTTPRequest.m -// -// Created by Ben Copsey on 04/10/2007. -// Copyright 2007-2011 All-Seeing Interactive. All rights reserved. -// -// A guide to the main features is available at: -// http://allseeing-i.com/ASIHTTPRequest -// -// Portions are based on the ImageClient example from Apple: -// See: http://developer.apple.com/samplecode/ImageClient/listing37.html - -#import "ASIHTTPRequest.h" - -#if TARGET_OS_IPHONE -#import "Reachability.h" -#import "ASIAuthenticationDialog.h" -#import -#else -#import -#endif -#import "ASIInputStream.h" -#import "ASIDataDecompressor.h" -#import "ASIDataCompressor.h" - -// Automatically set on build -NSString *ASIHTTPRequestVersion = @"v1.8.1-61 2011-09-19"; - -static NSString *defaultUserAgent = nil; - -NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; - -static NSString *ASIHTTPRequestRunLoopMode = @"ASIHTTPRequestRunLoopMode"; - -static const CFOptionFlags kNetworkEvents = kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred; - -// In memory caches of credentials, used on when useSessionPersistence is YES -static NSMutableArray *sessionCredentialsStore = nil; -static NSMutableArray *sessionProxyCredentialsStore = nil; - -// This lock mediates access to session credentials -static NSRecursiveLock *sessionCredentialsLock = nil; - -// We keep track of cookies we have received here so we can remove them from the sharedHTTPCookieStorage later -static NSMutableArray *sessionCookies = nil; - -// The number of times we will allow requests to redirect before we fail with a redirection error -const int RedirectionLimit = 5; - -// The default number of seconds to use for a timeout -static NSTimeInterval defaultTimeOutSeconds = 10; - -static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventType type, void *clientCallBackInfo) { - [((ASIHTTPRequest*)clientCallBackInfo) handleNetworkEvent: type]; -} - -// This lock prevents the operation from being cancelled while it is trying to update the progress, and vice versa -static NSRecursiveLock *progressLock; - -static NSError *ASIRequestCancelledError; -static NSError *ASIRequestTimedOutError; -static NSError *ASIAuthenticationError; -static NSError *ASIUnableToCreateRequestError; -static NSError *ASITooMuchRedirectionError; - -static NSMutableArray *bandwidthUsageTracker = nil; -static unsigned long averageBandwidthUsedPerSecond = 0; - -// These are used for queuing persistent connections on the same connection - -// Incremented every time we specify we want a new connection -static unsigned int nextConnectionNumberToCreate = 0; - -// An array of connectionInfo dictionaries. -// When attempting a persistent connection, we look here to try to find an existing connection to the same server that is currently not in use -static NSMutableArray *persistentConnectionsPool = nil; - -// Mediates access to the persistent connections pool -static NSRecursiveLock *connectionsLock = nil; - -// Each request gets a new id, we store this rather than a ref to the request itself in the connectionInfo dictionary. -// We do this so we don't have to keep the request around while we wait for the connection to expire -static unsigned int nextRequestID = 0; - -// Records how much bandwidth all requests combined have used in the last second -static unsigned long bandwidthUsedInLastSecond = 0; - -// A date one second in the future from the time it was created -static NSDate *bandwidthMeasurementDate = nil; - -// Since throttling variables are shared among all requests, we'll use a lock to mediate access -static NSLock *bandwidthThrottlingLock = nil; - -// the maximum number of bytes that can be transmitted in one second -static unsigned long maxBandwidthPerSecond = 0; - -// A default figure for throttling bandwidth on mobile devices -unsigned long const ASIWWANBandwidthThrottleAmount = 14800; - -#if TARGET_OS_IPHONE -// YES when bandwidth throttling is active -// This flag does not denote whether throttling is turned on - rather whether it is currently in use -// It will be set to NO when throttling was turned on with setShouldThrottleBandwidthForWWAN, but a WI-FI connection is active -static BOOL isBandwidthThrottled = NO; - -// When YES, bandwidth will be automatically throttled when using WWAN (3G/Edge/GPRS) -// Wifi will not be throttled -static BOOL shouldThrottleBandwidthForWWANOnly = NO; -#endif - -// Mediates access to the session cookies so requests -static NSRecursiveLock *sessionCookiesLock = nil; - -// This lock ensures delegates only receive one notification that authentication is required at once -// When using ASIAuthenticationDialogs, it also ensures only one dialog is shown at once -// If a request can't acquire the lock immediately, it means a dialog is being shown or a delegate is handling the authentication challenge -// Once it gets the lock, it will try to look for existing credentials again rather than showing the dialog / notifying the delegate -// This is so it can make use of any credentials supplied for the other request, if they are appropriate -static NSRecursiveLock *delegateAuthenticationLock = nil; - -// When throttling bandwidth, Set to a date in future that we will allow all requests to wake up and reschedule their streams -static NSDate *throttleWakeUpTime = nil; - -static id defaultCache = nil; - -// Used for tracking when requests are using the network -static unsigned int runningRequestCount = 0; - -// You can use [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO] if you want to manage it yourself -// Alternatively, override showNetworkActivityIndicator / hideNetworkActivityIndicator -// By default this does nothing on Mac OS X, but again override the above methods for a different behaviour -static BOOL shouldUpdateNetworkActivityIndicator = YES; - -// The thread all requests will run on -// Hangs around forever, but will be blocked unless there are requests underway -static NSThread *networkThread = nil; - -static NSOperationQueue *sharedQueue = nil; - -// Private stuff -@interface ASIHTTPRequest () - -- (void)cancelLoad; - -- (void)destroyReadStream; -- (void)scheduleReadStream; -- (void)unscheduleReadStream; - -- (BOOL)willAskDelegateForCredentials; -- (BOOL)willAskDelegateForProxyCredentials; -- (void)askDelegateForProxyCredentials; -- (void)askDelegateForCredentials; -- (void)failAuthentication; - -+ (void)measureBandwidthUsage; -+ (void)recordBandwidthUsage; - -- (void)startRequest; -- (void)updateStatus:(NSTimer *)timer; -- (void)checkRequestStatus; -- (void)reportFailure; -- (void)reportFinished; -- (void)markAsFinished; -- (void)performRedirect; -- (BOOL)shouldTimeOut; -- (BOOL)willRedirect; -- (BOOL)willAskDelegateToConfirmRedirect; - -+ (void)performInvocation:(NSInvocation *)invocation onTarget:(id *)target releasingObject:(id)objectToRelease; -+ (void)hideNetworkActivityIndicatorAfterDelay; -+ (void)hideNetworkActivityIndicatorIfNeeeded; -+ (void)runRequests; - -// Handling Proxy autodetection and PAC file downloads -- (BOOL)configureProxies; -- (void)fetchPACFile; -- (void)finishedDownloadingPACFile:(ASIHTTPRequest *)theRequest; -- (void)runPACScript:(NSString *)script; -- (void)timeOutPACRead; - -- (void)useDataFromCache; - -// Called to update the size of a partial download when starting a request, or retrying after a timeout -- (void)updatePartialDownloadSize; - -#if TARGET_OS_IPHONE -+ (void)registerForNetworkReachabilityNotifications; -+ (void)unsubscribeFromNetworkReachabilityNotifications; -// Called when the status of the network changes -+ (void)reachabilityChanged:(NSNotification *)note; -#endif - -#if NS_BLOCKS_AVAILABLE -- (void)performBlockOnMainThread:(ASIBasicBlock)block; -- (void)releaseBlocksOnMainThread; -+ (void)releaseBlocks:(NSArray *)blocks; -- (void)callBlock:(ASIBasicBlock)block; -#endif - - - - - -@property (assign) BOOL complete; -@property (retain) NSArray *responseCookies; -@property (assign) int responseStatusCode; -@property (retain, nonatomic) NSDate *lastActivityTime; - -@property (assign) unsigned long long partialDownloadSize; -@property (assign, nonatomic) unsigned long long uploadBufferSize; -@property (retain, nonatomic) NSOutputStream *postBodyWriteStream; -@property (retain, nonatomic) NSInputStream *postBodyReadStream; -@property (assign, nonatomic) unsigned long long lastBytesRead; -@property (assign, nonatomic) unsigned long long lastBytesSent; -@property (atomic, retain) NSRecursiveLock *cancelledLock; -@property (retain, nonatomic) NSOutputStream *fileDownloadOutputStream; -@property (retain, nonatomic) NSOutputStream *inflatedFileDownloadOutputStream; -@property (assign) int authenticationRetryCount; -@property (assign) int proxyAuthenticationRetryCount; -@property (assign, nonatomic) BOOL updatedProgress; -@property (assign, nonatomic) BOOL needsRedirect; -@property (assign, nonatomic) int redirectCount; -@property (retain, nonatomic) NSData *compressedPostBody; -@property (retain, nonatomic) NSString *compressedPostBodyFilePath; -@property (retain) NSString *authenticationRealm; -@property (retain) NSString *proxyAuthenticationRealm; -@property (retain) NSString *responseStatusMessage; -@property (assign) BOOL inProgress; -@property (assign) int retryCount; -@property (atomic, assign) BOOL willRetryRequest; -@property (assign) BOOL connectionCanBeReused; -@property (retain, nonatomic) NSMutableDictionary *connectionInfo; -@property (retain, nonatomic) NSInputStream *readStream; -@property (assign) ASIAuthenticationState authenticationNeeded; -@property (assign, nonatomic) BOOL readStreamIsScheduled; -@property (assign, nonatomic) BOOL downloadComplete; -@property (retain) NSNumber *requestID; -@property (assign, nonatomic) NSString *runLoopMode; -@property (retain, nonatomic) NSTimer *statusTimer; -@property (assign) BOOL didUseCachedResponse; -@property (retain, nonatomic) NSURL *redirectURL; - -@property (assign, nonatomic) BOOL isPACFileRequest; -@property (retain, nonatomic) ASIHTTPRequest *PACFileRequest; -@property (retain, nonatomic) NSInputStream *PACFileReadStream; -@property (retain, nonatomic) NSMutableData *PACFileData; - -@property (assign, nonatomic, setter=setSynchronous:) BOOL isSynchronous; -@end - - -@implementation ASIHTTPRequest - -#pragma mark init / dealloc - -+ (void)initialize -{ - if (self == [ASIHTTPRequest class]) { - persistentConnectionsPool = [[NSMutableArray alloc] init]; - connectionsLock = [[NSRecursiveLock alloc] init]; - progressLock = [[NSRecursiveLock alloc] init]; - bandwidthThrottlingLock = [[NSLock alloc] init]; - sessionCookiesLock = [[NSRecursiveLock alloc] init]; - sessionCredentialsLock = [[NSRecursiveLock alloc] init]; - delegateAuthenticationLock = [[NSRecursiveLock alloc] init]; - bandwidthUsageTracker = [[NSMutableArray alloc] initWithCapacity:5]; - ASIRequestTimedOutError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIRequestTimedOutErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request timed out",NSLocalizedDescriptionKey,nil]]; - ASIAuthenticationError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIAuthenticationErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Authentication needed",NSLocalizedDescriptionKey,nil]]; - ASIRequestCancelledError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIRequestCancelledErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request was cancelled",NSLocalizedDescriptionKey,nil]]; - ASIUnableToCreateRequestError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIUnableToCreateRequestErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create request (bad url?)",NSLocalizedDescriptionKey,nil]]; - ASITooMuchRedirectionError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASITooMuchRedirectionErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request failed because it redirected too many times",NSLocalizedDescriptionKey,nil]]; - sharedQueue = [[NSOperationQueue alloc] init]; - [sharedQueue setMaxConcurrentOperationCount:4]; - - } -} - - -- (id)initWithURL:(NSURL *)newURL -{ - self = [self init]; - [self setRequestMethod:@"GET"]; - - [self setRunLoopMode:NSDefaultRunLoopMode]; - [self setShouldAttemptPersistentConnection:YES]; - [self setPersistentConnectionTimeoutSeconds:60.0]; - [self setShouldPresentCredentialsBeforeChallenge:YES]; - [self setShouldRedirect:YES]; - [self setShowAccurateProgress:YES]; - [self setShouldResetDownloadProgress:YES]; - [self setShouldResetUploadProgress:YES]; - [self setAllowCompressedResponse:YES]; - [self setShouldWaitToInflateCompressedResponses:YES]; - [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; - [self setShouldPresentProxyAuthenticationDialog:YES]; - - [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]]; - [self setUseSessionPersistence:YES]; - [self setUseCookiePersistence:YES]; - [self setValidatesSecureCertificate:YES]; - [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]]; - [self setDidStartSelector:@selector(requestStarted:)]; - [self setDidReceiveResponseHeadersSelector:@selector(request:didReceiveResponseHeaders:)]; - [self setWillRedirectSelector:@selector(request:willRedirectToURL:)]; - [self setDidFinishSelector:@selector(requestFinished:)]; - [self setDidFailSelector:@selector(requestFailed:)]; - [self setDidReceiveDataSelector:@selector(request:didReceiveData:)]; - [self setURL:newURL]; - [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]]; - [self setDownloadCache:[[self class] defaultCache]]; - return self; -} - -+ (id)requestWithURL:(NSURL *)newURL -{ - return [[[self alloc] initWithURL:newURL] autorelease]; -} - -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache -{ - return [self requestWithURL:newURL usingCache:cache andCachePolicy:ASIUseDefaultCachePolicy]; -} - -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache andCachePolicy:(ASICachePolicy)policy -{ - ASIHTTPRequest *request = [[[self alloc] initWithURL:newURL] autorelease]; - [request setDownloadCache:cache]; - [request setCachePolicy:policy]; - return request; -} - -- (void)dealloc -{ - [self setAuthenticationNeeded:ASINoAuthenticationNeededYet]; - if (requestAuthentication) { - CFRelease(requestAuthentication); - } - if (proxyAuthentication) { - CFRelease(proxyAuthentication); - } - if (request) { - CFRelease(request); - } - if (clientCertificateIdentity) { - CFRelease(clientCertificateIdentity); - } - [self cancelLoad]; - [redirectURL release]; - [statusTimer invalidate]; - [statusTimer release]; - [queue release]; - [userInfo release]; - [postBody release]; - [compressedPostBody release]; - [error release]; - [requestHeaders release]; - [requestCookies release]; - [downloadDestinationPath release]; - [temporaryFileDownloadPath release]; - [temporaryUncompressedDataDownloadPath release]; - [fileDownloadOutputStream release]; - [inflatedFileDownloadOutputStream release]; - [username release]; - [password release]; - [domain release]; - [authenticationRealm release]; - [authenticationScheme release]; - [requestCredentials release]; - [proxyHost release]; - [proxyType release]; - [proxyUsername release]; - [proxyPassword release]; - [proxyDomain release]; - [proxyAuthenticationRealm release]; - [proxyAuthenticationScheme release]; - [proxyCredentials release]; - [url release]; - [originalURL release]; - [lastActivityTime release]; - [responseCookies release]; - [rawResponseData release]; - [responseHeaders release]; - [requestMethod release]; - [cancelledLock release]; - [postBodyFilePath release]; - [compressedPostBodyFilePath release]; - [postBodyWriteStream release]; - [postBodyReadStream release]; - [PACurl release]; - [clientCertificates release]; - [responseStatusMessage release]; - [connectionInfo release]; - [requestID release]; - [dataDecompressor release]; - [userAgentString release]; - - #if NS_BLOCKS_AVAILABLE - [self releaseBlocksOnMainThread]; - #endif - - [super dealloc]; -} - -#if NS_BLOCKS_AVAILABLE -- (void)releaseBlocksOnMainThread -{ - NSMutableArray *blocks = [NSMutableArray array]; - if (completionBlock) { - [blocks addObject:completionBlock]; - [completionBlock release]; - completionBlock = nil; - } - if (failureBlock) { - [blocks addObject:failureBlock]; - [failureBlock release]; - failureBlock = nil; - } - if (startedBlock) { - [blocks addObject:startedBlock]; - [startedBlock release]; - startedBlock = nil; - } - if (headersReceivedBlock) { - [blocks addObject:headersReceivedBlock]; - [headersReceivedBlock release]; - headersReceivedBlock = nil; - } - if (bytesReceivedBlock) { - [blocks addObject:bytesReceivedBlock]; - [bytesReceivedBlock release]; - bytesReceivedBlock = nil; - } - if (bytesSentBlock) { - [blocks addObject:bytesSentBlock]; - [bytesSentBlock release]; - bytesSentBlock = nil; - } - if (downloadSizeIncrementedBlock) { - [blocks addObject:downloadSizeIncrementedBlock]; - [downloadSizeIncrementedBlock release]; - downloadSizeIncrementedBlock = nil; - } - if (uploadSizeIncrementedBlock) { - [blocks addObject:uploadSizeIncrementedBlock]; - [uploadSizeIncrementedBlock release]; - uploadSizeIncrementedBlock = nil; - } - if (dataReceivedBlock) { - [blocks addObject:dataReceivedBlock]; - [dataReceivedBlock release]; - dataReceivedBlock = nil; - } - if (proxyAuthenticationNeededBlock) { - [blocks addObject:proxyAuthenticationNeededBlock]; - [proxyAuthenticationNeededBlock release]; - proxyAuthenticationNeededBlock = nil; - } - if (authenticationNeededBlock) { - [blocks addObject:authenticationNeededBlock]; - [authenticationNeededBlock release]; - authenticationNeededBlock = nil; - } - if (requestRedirectedBlock) { - [blocks addObject:requestRedirectedBlock]; - [requestRedirectedBlock release]; - requestRedirectedBlock = nil; - } - [[self class] performSelectorOnMainThread:@selector(releaseBlocks:) withObject:blocks waitUntilDone:[NSThread isMainThread]]; -} -// Always called on main thread -+ (void)releaseBlocks:(NSArray *)blocks -{ - // Blocks will be released when this method exits -} -#endif - - -#pragma mark setup request - -- (void)addRequestHeader:(NSString *)header value:(NSString *)value -{ - if (!requestHeaders) { - [self setRequestHeaders:[NSMutableDictionary dictionaryWithCapacity:1]]; - } - [requestHeaders setObject:value forKey:header]; -} - -// This function will be called either just before a request starts, or when postLength is needed, whichever comes first -// postLength must be set by the time this function is complete -- (void)buildPostBody -{ - - if ([self haveBuiltPostBody]) { - return; - } - - // Are we submitting the request body from a file on disk - if ([self postBodyFilePath]) { - - // If we were writing to the post body via appendPostData or appendPostDataFromFile, close the write stream - if ([self postBodyWriteStream]) { - [[self postBodyWriteStream] close]; - [self setPostBodyWriteStream:nil]; - } - - - NSString *path; - if ([self shouldCompressRequestBody]) { - if (![self compressedPostBodyFilePath]) { - [self setCompressedPostBodyFilePath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - - NSError *err = nil; - if (![ASIDataCompressor compressDataFromFile:[self postBodyFilePath] toFile:[self compressedPostBodyFilePath] error:&err]) { - [self failWithError:err]; - return; - } - } - path = [self compressedPostBodyFilePath]; - } else { - path = [self postBodyFilePath]; - } - NSError *err = nil; - [self setPostLength:[[[[[NSFileManager alloc] init] autorelease] attributesOfItemAtPath:path error:&err] fileSize]]; - if (err) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to get attributes for file at path '%@'",path],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; - return; - } - - // Otherwise, we have an in-memory request body - } else { - if ([self shouldCompressRequestBody]) { - NSError *err = nil; - NSData *compressedBody = [ASIDataCompressor compressData:[self postBody] error:&err]; - if (err) { - [self failWithError:err]; - return; - } - [self setCompressedPostBody:compressedBody]; - [self setPostLength:[[self compressedPostBody] length]]; - } else { - [self setPostLength:[[self postBody] length]]; - } - } - - if ([self postLength] > 0) { - if ([requestMethod isEqualToString:@"GET"] || [requestMethod isEqualToString:@"DELETE"] || [requestMethod isEqualToString:@"HEAD"]) { - [self setRequestMethod:@"POST"]; - } - [self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",[self postLength]]]; - } - [self setHaveBuiltPostBody:YES]; - -} - -// Sets up storage for the post body -- (void)setupPostBody -{ - if ([self shouldStreamPostDataFromDisk]) { - if (![self postBodyFilePath]) { - [self setPostBodyFilePath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - [self setDidCreateTemporaryPostDataFile:YES]; - } - if (![self postBodyWriteStream]) { - [self setPostBodyWriteStream:[[[NSOutputStream alloc] initToFileAtPath:[self postBodyFilePath] append:NO] autorelease]]; - [[self postBodyWriteStream] open]; - } - } else { - if (![self postBody]) { - [self setPostBody:[[[NSMutableData alloc] init] autorelease]]; - } - } -} - -- (void)appendPostData:(NSData *)data -{ - [self setupPostBody]; - if ([data length] == 0) { - return; - } - if ([self shouldStreamPostDataFromDisk]) { - [[self postBodyWriteStream] write:[data bytes] maxLength:[data length]]; - } else { - [[self postBody] appendData:data]; - } -} - -- (void)appendPostDataFromFile:(NSString *)file -{ - [self setupPostBody]; - NSInputStream *stream = [[[NSInputStream alloc] initWithFileAtPath:file] autorelease]; - [stream open]; - while ([stream hasBytesAvailable]) { - - unsigned char buffer[1024*256]; - NSInteger bytesRead = [stream read:buffer maxLength:sizeof(buffer)]; - if (bytesRead == 0) { - // 0 indicates that the end of the buffer was reached. - break; - } else if (bytesRead < 0) { - // A negative number means that the operation failed. - break; - } - if ([self shouldStreamPostDataFromDisk]) { - [[self postBodyWriteStream] write:buffer maxLength:(NSUInteger)bytesRead]; - } else { - [[self postBody] appendData:[NSData dataWithBytes:buffer length:(NSUInteger)bytesRead]]; - } - } - [stream close]; -} - -- (NSString *)requestMethod -{ - [[self cancelledLock] lock]; - NSString *m = requestMethod; - [[self cancelledLock] unlock]; - return m; -} - -- (void)setRequestMethod:(NSString *)newRequestMethod -{ - [[self cancelledLock] lock]; - if (requestMethod != newRequestMethod) { - [requestMethod release]; - requestMethod = [newRequestMethod retain]; - if ([requestMethod isEqualToString:@"POST"] || [requestMethod isEqualToString:@"PUT"] || [postBody length] || postBodyFilePath) { - [self setShouldAttemptPersistentConnection:NO]; - } - } - [[self cancelledLock] unlock]; -} - -- (NSURL *)url -{ - [[self cancelledLock] lock]; - NSURL *u = url; - [[self cancelledLock] unlock]; - return u; -} - - -- (void)setURL:(NSURL *)newURL -{ - [[self cancelledLock] lock]; - if ([newURL isEqual:[self url]]) { - [[self cancelledLock] unlock]; - return; - } - [url release]; - url = [newURL retain]; - if (requestAuthentication) { - CFRelease(requestAuthentication); - requestAuthentication = NULL; - } - if (proxyAuthentication) { - CFRelease(proxyAuthentication); - proxyAuthentication = NULL; - } - if (request) { - CFRelease(request); - request = NULL; - } - [self setRedirectURL:nil]; - [[self cancelledLock] unlock]; -} - -- (id)delegate -{ - [[self cancelledLock] lock]; - id d = delegate; - [[self cancelledLock] unlock]; - return d; -} - -- (void)setDelegate:(id)newDelegate -{ - [[self cancelledLock] lock]; - delegate = newDelegate; - [[self cancelledLock] unlock]; -} - -- (id)queue -{ - [[self cancelledLock] lock]; - id q = queue; - [[self cancelledLock] unlock]; - return q; -} - - -- (void)setQueue:(id)newQueue -{ - [[self cancelledLock] lock]; - if (newQueue != queue) { - [queue release]; - queue = [newQueue retain]; - } - [[self cancelledLock] unlock]; -} - -#pragma mark get information about this request - -// cancel the request - this must be run on the same thread as the request is running on -- (void)cancelOnRequestThread -{ - #if DEBUG_REQUEST_STATUS - ASI_DEBUG_LOG(@"[STATUS] Request cancelled: %@",self); - #endif - - [[self cancelledLock] lock]; - - if ([self isCancelled] || [self complete]) { - [[self cancelledLock] unlock]; - return; - } - [self failWithError:ASIRequestCancelledError]; - [self setComplete:YES]; - [self cancelLoad]; - - CFRetain(self); - [self willChangeValueForKey:@"isCancelled"]; - cancelled = YES; - [self didChangeValueForKey:@"isCancelled"]; - - [[self cancelledLock] unlock]; - CFRelease(self); -} - -- (void)cancel -{ - [self performSelector:@selector(cancelOnRequestThread) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; -} - -- (void)clearDelegatesAndCancel -{ - [[self cancelledLock] lock]; - - // Clear delegates - [self setDelegate:nil]; - [self setQueue:nil]; - [self setDownloadProgressDelegate:nil]; - [self setUploadProgressDelegate:nil]; - - #if NS_BLOCKS_AVAILABLE - // Clear blocks - [self releaseBlocksOnMainThread]; - #endif - - [[self cancelledLock] unlock]; - [self cancel]; -} - - -- (BOOL)isCancelled -{ - BOOL result; - - [[self cancelledLock] lock]; - result = cancelled; - [[self cancelledLock] unlock]; - - return result; -} - -// Call this method to get the received data as an NSString. Don't use for binary data! -- (NSString *)responseString -{ - NSData *data = [self responseData]; - if (!data) { - return nil; - } - - return [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease]; -} - -- (BOOL)isResponseCompressed -{ - NSString *encoding = [[self responseHeaders] objectForKey:@"Content-Encoding"]; - return encoding && [encoding rangeOfString:@"gzip"].location != NSNotFound; -} - -- (NSData *)responseData -{ - if ([self isResponseCompressed] && [self shouldWaitToInflateCompressedResponses]) { - return [ASIDataDecompressor uncompressData:[self rawResponseData] error:NULL]; - } else { - return [self rawResponseData]; - } - return nil; -} - -#pragma mark running a request - -- (void)startSynchronous -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[STATUS] Starting synchronous request %@",self); -#endif - [self setSynchronous:YES]; - [self setRunLoopMode:ASIHTTPRequestRunLoopMode]; - [self setInProgress:YES]; - - if (![self isCancelled] && ![self complete]) { - [self main]; - while (!complete) { - [[NSRunLoop currentRunLoop] runMode:[self runLoopMode] beforeDate:[NSDate distantFuture]]; - } - } - - [self setInProgress:NO]; -} - -- (void)start -{ - [self setInProgress:YES]; - [self performSelector:@selector(main) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; -} - -- (void)startAsynchronous -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[STATUS] Starting asynchronous request %@",self); -#endif - [sharedQueue addOperation:self]; -} - -#pragma mark concurrency - -- (BOOL)isConcurrent -{ - return YES; -} - -- (BOOL)isFinished -{ - return finished; -} - -- (BOOL)isExecuting { - return [self inProgress]; -} - -#pragma mark request logic - -// Create the request -- (void)main -{ - @try { - - [[self cancelledLock] lock]; - - #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - if ([ASIHTTPRequest isMultitaskingSupported] && [self shouldContinueWhenAppEntersBackground]) { - if (!backgroundTask || backgroundTask == UIBackgroundTaskInvalid) { - backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ - // Synchronize the cleanup call on the main thread in case - // the task actually finishes at around the same time. - dispatch_async(dispatch_get_main_queue(), ^{ - if (backgroundTask != UIBackgroundTaskInvalid) - { - [[UIApplication sharedApplication] endBackgroundTask:backgroundTask]; - backgroundTask = UIBackgroundTaskInvalid; - [self cancel]; - } - }); - }]; - } - } - #endif - - - // A HEAD request generated by an ASINetworkQueue may have set the error already. If so, we should not proceed. - if ([self error]) { - [self setComplete:YES]; - [self markAsFinished]; - return; - } - - [self setComplete:NO]; - [self setDidUseCachedResponse:NO]; - - if (![self url]) { - [self failWithError:ASIUnableToCreateRequestError]; - return; - } - - // Must call before we create the request so that the request method can be set if needs be - if (![self mainRequest]) { - [self buildPostBody]; - } - - if (![[self requestMethod] isEqualToString:@"GET"]) { - [self setDownloadCache:nil]; - } - - - // If we're redirecting, we'll already have a CFHTTPMessageRef - if (request) { - CFRelease(request); - } - - // Create a new HTTP request. - request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)[self requestMethod], (CFURLRef)[self url], [self useHTTPVersionOne] ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1); - if (!request) { - [self failWithError:ASIUnableToCreateRequestError]; - return; - } - - //If this is a HEAD request generated by an ASINetworkQueue, we need to let the main request generate its headers first so we can use them - if ([self mainRequest]) { - [[self mainRequest] buildRequestHeaders]; - } - - // Even if this is a HEAD request with a mainRequest, we still need to call to give subclasses a chance to add their own to HEAD requests (ASIS3Request does this) - [self buildRequestHeaders]; - - if ([self downloadCache]) { - - // If this request should use the default policy, set its policy to the download cache's default policy - if (![self cachePolicy]) { - [self setCachePolicy:[[self downloadCache] defaultCachePolicy]]; - } - - // If have have cached data that is valid for this request, use that and stop - if ([[self downloadCache] canUseCachedDataForRequest:self]) { - [self useDataFromCache]; - return; - } - - // If cached data is stale, or we have been told to ask the server if it has been modified anyway, we need to add headers for a conditional GET - if ([self cachePolicy] & (ASIAskServerIfModifiedWhenStaleCachePolicy|ASIAskServerIfModifiedCachePolicy)) { - - NSDictionary *cachedHeaders = [[self downloadCache] cachedResponseHeadersForURL:[self url]]; - if (cachedHeaders) { - NSString *etag = [cachedHeaders objectForKey:@"Etag"]; - if (etag) { - [[self requestHeaders] setObject:etag forKey:@"If-None-Match"]; - } - NSString *lastModified = [cachedHeaders objectForKey:@"Last-Modified"]; - if (lastModified) { - [[self requestHeaders] setObject:lastModified forKey:@"If-Modified-Since"]; - } - } - } - } - - [self applyAuthorizationHeader]; - - - NSString *header; - for (header in [self requestHeaders]) { - CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)header, (CFStringRef)[[self requestHeaders] objectForKey:header]); - } - - // If we immediately have access to proxy settings, start the request - // Otherwise, we'll start downloading the proxy PAC file, and call startRequest once that process is complete - if ([self configureProxies]) { - [self startRequest]; - } - - } @catch (NSException *exception) { - NSError *underlyingError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnhandledExceptionError userInfo:[exception userInfo]]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnhandledExceptionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[exception name],NSLocalizedDescriptionKey,[exception reason],NSLocalizedFailureReasonErrorKey,underlyingError,NSUnderlyingErrorKey,nil]]]; - - } @finally { - [[self cancelledLock] unlock]; - } -} - -- (void)applyAuthorizationHeader -{ - // Do we want to send credentials before we are asked for them? - if (![self shouldPresentCredentialsBeforeChallenge]) { - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will not send credentials to the server until it asks for them",self); - #endif - return; - } - - NSDictionary *credentials = nil; - - // Do we already have an auth header? - if (![[self requestHeaders] objectForKey:@"Authorization"]) { - - // If we have basic authentication explicitly set and a username and password set on the request, add a basic auth header - if ([self username] && [self password] && [[self authenticationScheme] isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeBasic]) { - [self addBasicAuthenticationHeaderWithUsername:[self username] andPassword:[self password]]; - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ has a username and password set, and was manually configured to use BASIC. Will send credentials without waiting for an authentication challenge",self); - #endif - - } else { - - // See if we have any cached credentials we can use in the session store - if ([self useSessionPersistence]) { - credentials = [self findSessionAuthenticationCredentials]; - - if (credentials) { - - // When the Authentication key is set, the credentials were stored after an authentication challenge, so we can let CFNetwork apply them - // (credentials for Digest and NTLM will always be stored like this) - if ([credentials objectForKey:@"Authentication"]) { - - // If we've already talked to this server and have valid credentials, let's apply them to the request - if (CFHTTPMessageApplyCredentialDictionary(request, (CFHTTPAuthenticationRef)[credentials objectForKey:@"Authentication"], (CFDictionaryRef)[credentials objectForKey:@"Credentials"], NULL)) { - [self setAuthenticationScheme:[credentials objectForKey:@"AuthenticationScheme"]]; - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ found cached credentials (%@), will reuse without waiting for an authentication challenge",self,[credentials objectForKey:@"AuthenticationScheme"]); - #endif - } else { - [[self class] removeAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Failed to apply cached credentials to request %@. These will be removed from the session store, and this request will wait for an authentication challenge",self); - #endif - } - - // If the Authentication key is not set, these credentials were stored after a username and password set on a previous request passed basic authentication - // When this happens, we'll need to create the Authorization header ourselves - } else { - NSDictionary *usernameAndPassword = [credentials objectForKey:@"Credentials"]; - [self addBasicAuthenticationHeaderWithUsername:[usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationUsername] andPassword:[usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationPassword]]; - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ found cached BASIC credentials from a previous request. Will send credentials without waiting for an authentication challenge",self); - #endif - } - } - } - } - } - - // Apply proxy authentication credentials - if ([self useSessionPersistence]) { - credentials = [self findSessionProxyAuthenticationCredentials]; - if (credentials) { - if (!CFHTTPMessageApplyCredentialDictionary(request, (CFHTTPAuthenticationRef)[credentials objectForKey:@"Authentication"], (CFDictionaryRef)[credentials objectForKey:@"Credentials"], NULL)) { - [[self class] removeProxyAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - } - } - } -} - -- (void)applyCookieHeader -{ - // Add cookies from the persistent (mac os global) store - if ([self useCookiePersistence]) { - NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[[self url] absoluteURL]]; - if (cookies) { - [[self requestCookies] addObjectsFromArray:cookies]; - } - } - - // Apply request cookies - NSArray *cookies; - if ([self mainRequest]) { - cookies = [[self mainRequest] requestCookies]; - } else { - cookies = [self requestCookies]; - } - if ([cookies count] > 0) { - NSHTTPCookie *cookie; - NSString *cookieHeader = nil; - for (cookie in cookies) { - if (!cookieHeader) { - cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]]; - } else { - cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]]; - } - } - if (cookieHeader) { - [self addRequestHeader:@"Cookie" value:cookieHeader]; - } - } -} - -- (void)buildRequestHeaders -{ - if ([self haveBuiltRequestHeaders]) { - return; - } - [self setHaveBuiltRequestHeaders:YES]; - - if ([self mainRequest]) { - for (NSString *header in [[self mainRequest] requestHeaders]) { - [self addRequestHeader:header value:[[[self mainRequest] requestHeaders] valueForKey:header]]; - } - return; - } - - [self applyCookieHeader]; - - // Build and set the user agent string if the request does not already have a custom user agent specified - if (![[self requestHeaders] objectForKey:@"User-Agent"]) { - NSString *tempUserAgentString = [self userAgentString]; - if (!tempUserAgentString) { - tempUserAgentString = [ASIHTTPRequest defaultUserAgentString]; - } - if (tempUserAgentString) { - [self addRequestHeader:@"User-Agent" value:tempUserAgentString]; - } - } - - - // Accept a compressed response - if ([self allowCompressedResponse]) { - [self addRequestHeader:@"Accept-Encoding" value:@"gzip"]; - } - - // Configure a compressed request body - if ([self shouldCompressRequestBody]) { - [self addRequestHeader:@"Content-Encoding" value:@"gzip"]; - } - - // Should this request resume an existing download? - [self updatePartialDownloadSize]; - if ([self partialDownloadSize]) { - [self addRequestHeader:@"Range" value:[NSString stringWithFormat:@"bytes=%llu-",[self partialDownloadSize]]]; - } -} - -- (void)updatePartialDownloadSize -{ - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - if ([self allowResumeForFileDownloads] && [self downloadDestinationPath] && [self temporaryFileDownloadPath] && [fileManager fileExistsAtPath:[self temporaryFileDownloadPath]]) { - NSError *err = nil; - [self setPartialDownloadSize:[[fileManager attributesOfItemAtPath:[self temporaryFileDownloadPath] error:&err] fileSize]]; - if (err) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to get attributes for file at path '%@'",[self temporaryFileDownloadPath]],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; - return; - } - } -} - -- (void)startRequest -{ - if ([self isCancelled]) { - return; - } - - [self performSelectorOnMainThread:@selector(requestStarted) withObject:nil waitUntilDone:[NSThread isMainThread]]; - - [self setDownloadComplete:NO]; - [self setComplete:NO]; - [self setTotalBytesRead:0]; - [self setLastBytesRead:0]; - - if ([self redirectCount] == 0) { - [self setOriginalURL:[self url]]; - } - - // If we're retrying a request, let's remove any progress we made - if ([self lastBytesSent] > 0) { - [self removeUploadProgressSoFar]; - } - - [self setLastBytesSent:0]; - [self setContentLength:0]; - [self setResponseHeaders:nil]; - if (![self downloadDestinationPath]) { - [self setRawResponseData:[[[NSMutableData alloc] init] autorelease]]; - } - - - // - // Create the stream for the request - // - - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - [self setReadStreamIsScheduled:NO]; - - // Do we need to stream the request body from disk - if ([self shouldStreamPostDataFromDisk] && [self postBodyFilePath] && [fileManager fileExistsAtPath:[self postBodyFilePath]]) { - - // Are we gzipping the request body? - if ([self compressedPostBodyFilePath] && [fileManager fileExistsAtPath:[self compressedPostBodyFilePath]]) { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithFileAtPath:[self compressedPostBodyFilePath] request:self]]; - } else { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithFileAtPath:[self postBodyFilePath] request:self]]; - } - [self setReadStream:[NSMakeCollectable(CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream])) autorelease]]; - } else { - - // If we have a request body, we'll stream it from memory using our custom stream, so that we can measure bandwidth use and it can be bandwidth-throttled if necessary - if ([self postBody] && [[self postBody] length] > 0) { - if ([self shouldCompressRequestBody] && [self compressedPostBody]) { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithData:[self compressedPostBody] request:self]]; - } else if ([self postBody]) { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithData:[self postBody] request:self]]; - } - [self setReadStream:[NSMakeCollectable(CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream])) autorelease]]; - - } else { - [self setReadStream:[NSMakeCollectable(CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request)) autorelease]]; - } - } - - if (![self readStream]) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create read stream",NSLocalizedDescriptionKey,nil]]]; - return; - } - - - - - // - // Handle SSL certificate settings - // - - if([[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) { - - // Tell CFNetwork not to validate SSL certificates - if (![self validatesSecureCertificate]) { - // see: http://iphonedevelopment.blogspot.com/2010/05/nsstream-tcp-and-ssl.html - - NSDictionary *sslProperties = [[NSDictionary alloc] initWithObjectsAndKeys: - // Next two lines commented out, replaced by kCFStreamSSLValidatesCertificateChain -// [NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates, -// [NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot, - [NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain, - kCFNull,kCFStreamSSLPeerName, - nil]; - - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], - kCFStreamPropertySSLSettings, - (CFTypeRef)sslProperties); - [sslProperties release]; - } - - // Tell CFNetwork to use a client certificate - if (clientCertificateIdentity) { - NSMutableDictionary *sslProperties = [NSMutableDictionary dictionaryWithCapacity:1]; - - NSMutableArray *certificates = [NSMutableArray arrayWithCapacity:[clientCertificates count]+1]; - - // The first object in the array is our SecIdentityRef - [certificates addObject:(id)clientCertificateIdentity]; - - // If we've added any additional certificates, add them too - for (id cert in clientCertificates) { - [certificates addObject:cert]; - } - - [sslProperties setObject:certificates forKey:(NSString *)kCFStreamSSLCertificates]; - - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySSLSettings, sslProperties); - } - - } - - // - // Handle proxy settings - // - - if ([self proxyHost] && [self proxyPort]) { - NSString *hostKey; - NSString *portKey; - - if (![self proxyType]) { - [self setProxyType:(NSString *)kCFProxyTypeHTTP]; - } - - if ([[self proxyType] isEqualToString:(NSString *)kCFProxyTypeSOCKS]) { - hostKey = (NSString *)kCFStreamPropertySOCKSProxyHost; - portKey = (NSString *)kCFStreamPropertySOCKSProxyPort; - } else { - hostKey = (NSString *)kCFStreamPropertyHTTPProxyHost; - portKey = (NSString *)kCFStreamPropertyHTTPProxyPort; - if ([[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) { - hostKey = (NSString *)kCFStreamPropertyHTTPSProxyHost; - portKey = (NSString *)kCFStreamPropertyHTTPSProxyPort; - } - } - NSMutableDictionary *proxyToUse = [NSMutableDictionary dictionaryWithObjectsAndKeys:[self proxyHost],hostKey,[NSNumber numberWithInt:[self proxyPort]],portKey,nil]; - - if ([[self proxyType] isEqualToString:(NSString *)kCFProxyTypeSOCKS]) { - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySOCKSProxy, proxyToUse); - } else { - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPProxy, proxyToUse); - } - } - - - // - // Handle persistent connections - // - - [ASIHTTPRequest expirePersistentConnections]; - - [connectionsLock lock]; - - - if (![[self url] host] || ![[self url] scheme]) { - [self setConnectionInfo:nil]; - [self setShouldAttemptPersistentConnection:NO]; - } - - // Will store the old stream that was using this connection (if there was one) so we can clean it up once we've opened our own stream - NSInputStream *oldStream = nil; - - // Use a persistent connection if possible - if ([self shouldAttemptPersistentConnection]) { - - - // If we are redirecting, we will re-use the current connection only if we are connecting to the same server - if ([self connectionInfo]) { - - if (![[[self connectionInfo] objectForKey:@"host"] isEqualToString:[[self url] host]] || ![[[self connectionInfo] objectForKey:@"scheme"] isEqualToString:[[self url] scheme]] || [(NSNumber *)[[self connectionInfo] objectForKey:@"port"] intValue] != [[[self url] port] intValue]) { - [self setConnectionInfo:nil]; - - // Check if we should have expired this connection - } else if ([[[self connectionInfo] objectForKey:@"expires"] timeIntervalSinceNow] < 0) { - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Not re-using connection #%i because it has expired",[[[self connectionInfo] objectForKey:@"id"] intValue]); - #endif - [persistentConnectionsPool removeObject:[self connectionInfo]]; - [self setConnectionInfo:nil]; - - } else if ([[self connectionInfo] objectForKey:@"request"] != nil) { - //Some other request reused this connection already - we'll have to create a new one - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"%@ - Not re-using connection #%i for request #%i because it is already used by request #%i",self,[[[self connectionInfo] objectForKey:@"id"] intValue],[[self requestID] intValue],[[[self connectionInfo] objectForKey:@"request"] intValue]); - #endif - [self setConnectionInfo:nil]; - } - } - - - - if (![self connectionInfo] && [[self url] host] && [[self url] scheme]) { // We must have a proper url with a host and scheme, or this will explode - - // Look for a connection to the same server in the pool - for (NSMutableDictionary *existingConnection in persistentConnectionsPool) { - if (![existingConnection objectForKey:@"request"] && [[existingConnection objectForKey:@"host"] isEqualToString:[[self url] host]] && [[existingConnection objectForKey:@"scheme"] isEqualToString:[[self url] scheme]] && [(NSNumber *)[existingConnection objectForKey:@"port"] intValue] == [[[self url] port] intValue]) { - [self setConnectionInfo:existingConnection]; - } - } - } - - if ([[self connectionInfo] objectForKey:@"stream"]) { - oldStream = [[[self connectionInfo] objectForKey:@"stream"] retain]; - - } - - // No free connection was found in the pool matching the server/scheme/port we're connecting to, we'll need to create a new one - if (![self connectionInfo]) { - [self setConnectionInfo:[NSMutableDictionary dictionary]]; - nextConnectionNumberToCreate++; - [[self connectionInfo] setObject:[NSNumber numberWithInt:(int)nextConnectionNumberToCreate] forKey:@"id"]; - [[self connectionInfo] setObject:[[self url] host] forKey:@"host"]; - [[self connectionInfo] setObject:[NSNumber numberWithInt:[[[self url] port] intValue]] forKey:@"port"]; - [[self connectionInfo] setObject:[[self url] scheme] forKey:@"scheme"]; - [persistentConnectionsPool addObject:[self connectionInfo]]; - } - - // If we are retrying this request, it will already have a requestID - if (![self requestID]) { - nextRequestID++; - [self setRequestID:[NSNumber numberWithUnsignedInt:nextRequestID]]; - } - [[self connectionInfo] setObject:[self requestID] forKey:@"request"]; - [[self connectionInfo] setObject:[self readStream] forKey:@"stream"]; - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue); - - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Request #%@ will use connection #%i",[self requestID],[[[self connectionInfo] objectForKey:@"id"] intValue]); - #endif - - - // Tag the stream with an id that tells it which connection to use behind the scenes - // See http://lists.apple.com/archives/macnetworkprog/2008/Dec/msg00001.html for details on this approach - - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], CFSTR("ASIStreamID"), [[self connectionInfo] objectForKey:@"id"]); - - } else { - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Request %@ will not use a persistent connection",self); - #endif - } - - [connectionsLock unlock]; - - // Schedule the stream - if (![self readStreamIsScheduled] && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) { - [self scheduleReadStream]; - } - - BOOL streamSuccessfullyOpened = NO; - - - // Start the HTTP connection - CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL}; - if (CFReadStreamSetClient((CFReadStreamRef)[self readStream], kNetworkEvents, ReadStreamClientCallBack, &ctxt)) { - if (CFReadStreamOpen((CFReadStreamRef)[self readStream])) { - streamSuccessfullyOpened = YES; - } - } - - // Here, we'll close the stream that was previously using this connection, if there was one - // We've kept it open until now (when we've just opened a new stream) so that the new stream can make use of the old connection - // http://lists.apple.com/archives/Macnetworkprog/2006/Mar/msg00119.html - if (oldStream) { - [oldStream close]; - [oldStream release]; - oldStream = nil; - } - - if (!streamSuccessfullyOpened) { - [self setConnectionCanBeReused:NO]; - [self destroyReadStream]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]]; - return; - } - - if (![self mainRequest]) { - if ([self shouldResetUploadProgress]) { - if ([self showAccurateProgress]) { - [self incrementUploadSizeBy:(long long)[self postLength]]; - } else { - [self incrementUploadSizeBy:1]; - } - [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:0 ofTotal:1]; - } - if ([self shouldResetDownloadProgress] && ![self partialDownloadSize]) { - [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:0 ofTotal:1]; - } - } - - - // Record when the request started, so we can timeout if nothing happens - [self setLastActivityTime:[NSDate date]]; - [self setStatusTimer:[NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES]]; - [[NSRunLoop currentRunLoop] addTimer:[self statusTimer] forMode:[self runLoopMode]]; -} - -- (void)setStatusTimer:(NSTimer *)timer -{ - CFRetain(self); - // We must invalidate the old timer here, not before we've created and scheduled a new timer - // This is because the timer may be the only thing retaining an asynchronous request - if (statusTimer && timer != statusTimer) { - [statusTimer invalidate]; - [statusTimer release]; - } - statusTimer = [timer retain]; - CFRelease(self); -} - -// This gets fired every 1/4 of a second to update the progress and work out if we need to timeout -- (void)updateStatus:(NSTimer*)timer -{ - [self checkRequestStatus]; - if (![self inProgress]) { - [self setStatusTimer:nil]; - } -} - -- (void)performRedirect -{ - [self setURL:[self redirectURL]]; - [self setComplete:YES]; - [self setNeedsRedirect:NO]; - [self setRedirectCount:[self redirectCount]+1]; - - if ([self redirectCount] > RedirectionLimit) { - // Some naughty / badly coded website is trying to force us into a redirection loop. This is not cool. - [self failWithError:ASITooMuchRedirectionError]; - [self setComplete:YES]; - } else { - // Go all the way back to the beginning and build the request again, so that we can apply any new cookies - [self main]; - } -} - -// Called by delegate to resume loading with a new url after the delegate received request:willRedirectToURL: -- (void)redirectToURL:(NSURL *)newURL -{ - [self setRedirectURL:newURL]; - [self performSelector:@selector(performRedirect) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; -} - -- (BOOL)shouldTimeOut -{ - NSTimeInterval secondsSinceLastActivity = [[NSDate date] timeIntervalSinceDate:lastActivityTime]; - // See if we need to timeout - if ([self readStream] && [self readStreamIsScheduled] && [self lastActivityTime] && [self timeOutSeconds] > 0 && secondsSinceLastActivity > [self timeOutSeconds]) { - - // We have no body, or we've sent more than the upload buffer size,so we can safely time out here - if ([self postLength] == 0 || ([self uploadBufferSize] > 0 && [self totalBytesSent] > [self uploadBufferSize])) { - return YES; - - // ***Black magic warning*** - // We have a body, but we've taken longer than timeOutSeconds to upload the first small chunk of data - // Since there's no reliable way to track upload progress for the first 32KB (iPhone) or 128KB (Mac) with CFNetwork, we'll be slightly more forgiving on the timeout, as there's a strong chance our connection is just very slow. - } else if (secondsSinceLastActivity > [self timeOutSeconds]*1.5) { - return YES; - } - } - return NO; -} - -- (void)checkRequestStatus -{ - // We won't let the request cancel while we're updating progress / checking for a timeout - [[self cancelledLock] lock]; - // See if our NSOperationQueue told us to cancel - if ([self isCancelled] || [self complete]) { - [[self cancelledLock] unlock]; - return; - } - - [self performThrottling]; - - if ([self shouldTimeOut]) { - // Do we need to auto-retry this request? - if ([self numberOfTimesToRetryOnTimeout] > [self retryCount]) { - - // If we are resuming a download, we may need to update the Range header to take account of data we've just downloaded - [self updatePartialDownloadSize]; - if ([self partialDownloadSize]) { - CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)@"Range", (CFStringRef)[NSString stringWithFormat:@"bytes=%llu-",[self partialDownloadSize]]); - } - [self setRetryCount:[self retryCount]+1]; - [self unscheduleReadStream]; - [[self cancelledLock] unlock]; - [self startRequest]; - return; - } - [self failWithError:ASIRequestTimedOutError]; - [self cancelLoad]; - [self setComplete:YES]; - [[self cancelledLock] unlock]; - return; - } - - // readStream will be null if we aren't currently running (perhaps we're waiting for a delegate to supply credentials) - if ([self readStream]) { - - // If we have a post body - if ([self postLength]) { - - [self setLastBytesSent:totalBytesSent]; - - // Find out how much data we've uploaded so far - [self setTotalBytesSent:[[NSMakeCollectable(CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPRequestBytesWrittenCount)) autorelease] unsignedLongLongValue]]; - if (totalBytesSent > lastBytesSent) { - - // We've uploaded more data, reset the timeout - [self setLastActivityTime:[NSDate date]]; - [ASIHTTPRequest incrementBandwidthUsedInLastSecond:(unsigned long)(totalBytesSent-lastBytesSent)]; - - #if DEBUG_REQUEST_STATUS - if ([self totalBytesSent] == [self postLength]) { - ASI_DEBUG_LOG(@"[STATUS] Request %@ finished uploading data",self); - } - #endif - } - } - - [self updateProgressIndicators]; - - } - - [[self cancelledLock] unlock]; -} - - -// Cancel loading and clean up. DO NOT USE THIS TO CANCEL REQUESTS - use [request cancel] instead -- (void)cancelLoad -{ - // If we're in the middle of downloading a PAC file, let's stop that first - if (PACFileReadStream) { - [PACFileReadStream setDelegate:nil]; - [PACFileReadStream close]; - [self setPACFileReadStream:nil]; - [self setPACFileData:nil]; - } else if (PACFileRequest) { - [PACFileRequest setDelegate:nil]; - [PACFileRequest cancel]; - [self setPACFileRequest:nil]; - } - - [self destroyReadStream]; - - [[self postBodyReadStream] close]; - [self setPostBodyReadStream:nil]; - - if ([self rawResponseData]) { - if (![self complete]) { - [self setRawResponseData:nil]; - } - // If we were downloading to a file - } else if ([self temporaryFileDownloadPath]) { - [[self fileDownloadOutputStream] close]; - [self setFileDownloadOutputStream:nil]; - - [[self inflatedFileDownloadOutputStream] close]; - [self setInflatedFileDownloadOutputStream:nil]; - - // If we haven't said we might want to resume, let's remove the temporary file too - if (![self complete]) { - if (![self allowResumeForFileDownloads]) { - [self removeTemporaryDownloadFile]; - } - [self removeTemporaryUncompressedDownloadFile]; - } - } - - // Clean up any temporary file used to store request body for streaming - if (![self authenticationNeeded] && ![self willRetryRequest] && [self didCreateTemporaryPostDataFile]) { - [self removeTemporaryUploadFile]; - [self removeTemporaryCompressedUploadFile]; - [self setDidCreateTemporaryPostDataFile:NO]; - } -} - -#pragma mark HEAD request - -// Used by ASINetworkQueue to create a HEAD request appropriate for this request with the same headers (though you can use it yourself) -- (ASIHTTPRequest *)HEADRequest -{ - ASIHTTPRequest *headRequest = [[self class] requestWithURL:[self url]]; - - // Copy the properties that make sense for a HEAD request - [headRequest setRequestHeaders:[[[self requestHeaders] mutableCopy] autorelease]]; - [headRequest setRequestCookies:[[[self requestCookies] mutableCopy] autorelease]]; - [headRequest setUseCookiePersistence:[self useCookiePersistence]]; - [headRequest setUseKeychainPersistence:[self useKeychainPersistence]]; - [headRequest setUseSessionPersistence:[self useSessionPersistence]]; - [headRequest setAllowCompressedResponse:[self allowCompressedResponse]]; - [headRequest setUsername:[self username]]; - [headRequest setPassword:[self password]]; - [headRequest setDomain:[self domain]]; - [headRequest setProxyUsername:[self proxyUsername]]; - [headRequest setProxyPassword:[self proxyPassword]]; - [headRequest setProxyDomain:[self proxyDomain]]; - [headRequest setProxyHost:[self proxyHost]]; - [headRequest setProxyPort:[self proxyPort]]; - [headRequest setProxyType:[self proxyType]]; - [headRequest setShouldPresentAuthenticationDialog:[self shouldPresentAuthenticationDialog]]; - [headRequest setShouldPresentProxyAuthenticationDialog:[self shouldPresentProxyAuthenticationDialog]]; - [headRequest setTimeOutSeconds:[self timeOutSeconds]]; - [headRequest setUseHTTPVersionOne:[self useHTTPVersionOne]]; - [headRequest setValidatesSecureCertificate:[self validatesSecureCertificate]]; - [headRequest setClientCertificateIdentity:clientCertificateIdentity]; - [headRequest setClientCertificates:[[clientCertificates copy] autorelease]]; - [headRequest setPACurl:[self PACurl]]; - [headRequest setShouldPresentCredentialsBeforeChallenge:[self shouldPresentCredentialsBeforeChallenge]]; - [headRequest setNumberOfTimesToRetryOnTimeout:[self numberOfTimesToRetryOnTimeout]]; - [headRequest setShouldUseRFC2616RedirectBehaviour:[self shouldUseRFC2616RedirectBehaviour]]; - [headRequest setShouldAttemptPersistentConnection:[self shouldAttemptPersistentConnection]]; - [headRequest setPersistentConnectionTimeoutSeconds:[self persistentConnectionTimeoutSeconds]]; - - [headRequest setMainRequest:self]; - [headRequest setRequestMethod:@"HEAD"]; - return headRequest; -} - - -#pragma mark upload/download progress - - -- (void)updateProgressIndicators -{ - //Only update progress if this isn't a HEAD request used to preset the content-length - if (![self mainRequest]) { - if ([self showAccurateProgress] || ([self complete] && ![self updatedProgress])) { - [self updateUploadProgress]; - [self updateDownloadProgress]; - } - } -} - -- (id)uploadProgressDelegate -{ - [[self cancelledLock] lock]; - id d = [[uploadProgressDelegate retain] autorelease]; - [[self cancelledLock] unlock]; - return d; -} - -- (void)setUploadProgressDelegate:(id)newDelegate -{ - [[self cancelledLock] lock]; - uploadProgressDelegate = newDelegate; - - #if !TARGET_OS_IPHONE - // If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can update it as if it were a UIProgressView - double max = 1.0; - [ASIHTTPRequest performSelector:@selector(setMaxValue:) onTarget:&uploadProgressDelegate withObject:nil amount:&max callerToRetain:nil]; - #endif - [[self cancelledLock] unlock]; -} - -- (id)downloadProgressDelegate -{ - [[self cancelledLock] lock]; - id d = [[downloadProgressDelegate retain] autorelease]; - [[self cancelledLock] unlock]; - return d; -} - -- (void)setDownloadProgressDelegate:(id)newDelegate -{ - [[self cancelledLock] lock]; - downloadProgressDelegate = newDelegate; - - #if !TARGET_OS_IPHONE - // If the downloadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can update it as if it were a UIProgressView - double max = 1.0; - [ASIHTTPRequest performSelector:@selector(setMaxValue:) onTarget:&downloadProgressDelegate withObject:nil amount:&max callerToRetain:nil]; - #endif - [[self cancelledLock] unlock]; -} - - -- (void)updateDownloadProgress -{ - // We won't update download progress until we've examined the headers, since we might need to authenticate - if (![self responseHeaders] || [self needsRedirect] || !([self contentLength] || [self complete])) { - return; - } - - unsigned long long bytesReadSoFar = [self totalBytesRead]+[self partialDownloadSize]; - unsigned long long value = 0; - - if ([self showAccurateProgress] && [self contentLength]) { - value = bytesReadSoFar-[self lastBytesRead]; - if (value == 0) { - return; - } - } else { - value = 1; - [self setUpdatedProgress:YES]; - } - if (!value) { - return; - } - - [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&queue withObject:self amount:&value callerToRetain:self]; - [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&downloadProgressDelegate withObject:self amount:&value callerToRetain:self]; - - [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]]; - - #if NS_BLOCKS_AVAILABLE - if (bytesReceivedBlock) { - unsigned long long totalSize = [self contentLength] + [self partialDownloadSize]; - [self performBlockOnMainThread:^{ if (bytesReceivedBlock) { bytesReceivedBlock(value, totalSize); }}]; - } - #endif - [self setLastBytesRead:bytesReadSoFar]; -} - -- (void)updateUploadProgress -{ - if ([self isCancelled] || [self totalBytesSent] == 0) { - return; - } - - // If this is the first time we've written to the buffer, totalBytesSent will be the size of the buffer (currently seems to be 128KB on both Leopard and iPhone 2.2.1, 32KB on iPhone 3.0) - // If request body is less than the buffer size, totalBytesSent will be the total size of the request body - // We will remove this from any progress display, as kCFStreamPropertyHTTPRequestBytesWrittenCount does not tell us how much data has actually be written - if ([self uploadBufferSize] == 0 && [self totalBytesSent] != [self postLength]) { - [self setUploadBufferSize:[self totalBytesSent]]; - [self incrementUploadSizeBy:-(long long)[self uploadBufferSize]]; - } - - unsigned long long value = 0; - - if ([self showAccurateProgress]) { - if ([self totalBytesSent] == [self postLength] || [self lastBytesSent] > 0) { - value = [self totalBytesSent]-[self lastBytesSent]; - } else { - return; - } - } else { - value = 1; - [self setUpdatedProgress:YES]; - } - - if (!value) { - return; - } - - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&queue withObject:self amount:&value callerToRetain:self]; - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&uploadProgressDelegate withObject:self amount:&value callerToRetain:self]; - [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]-[self uploadBufferSize]]; - - #if NS_BLOCKS_AVAILABLE - if(bytesSentBlock){ - unsigned long long totalSize = [self postLength]; - [self performBlockOnMainThread:^{ if (bytesSentBlock) { bytesSentBlock(value, totalSize); }}]; - } - #endif -} - - -- (void)incrementDownloadSizeBy:(long long)length -{ - [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&queue withObject:self amount:&length callerToRetain:self]; - [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&downloadProgressDelegate withObject:self amount:&length callerToRetain:self]; - - #if NS_BLOCKS_AVAILABLE - if(downloadSizeIncrementedBlock){ - [self performBlockOnMainThread:^{ if (downloadSizeIncrementedBlock) { downloadSizeIncrementedBlock(length); }}]; - } - #endif -} - -- (void)incrementUploadSizeBy:(long long)length -{ - [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&queue withObject:self amount:&length callerToRetain:self]; - [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&uploadProgressDelegate withObject:self amount:&length callerToRetain:self]; - - #if NS_BLOCKS_AVAILABLE - if(uploadSizeIncrementedBlock) { - [self performBlockOnMainThread:^{ if (uploadSizeIncrementedBlock) { uploadSizeIncrementedBlock(length); }}]; - } - #endif -} - - --(void)removeUploadProgressSoFar -{ - long long progressToRemove = -(long long)[self totalBytesSent]; - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&queue withObject:self amount:&progressToRemove callerToRetain:self]; - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&uploadProgressDelegate withObject:self amount:&progressToRemove callerToRetain:self]; - [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:0 ofTotal:[self postLength]]; - - #if NS_BLOCKS_AVAILABLE - if(bytesSentBlock){ - unsigned long long totalSize = [self postLength]; - [self performBlockOnMainThread:^{ if (bytesSentBlock) { bytesSentBlock((unsigned long long)progressToRemove, totalSize); }}]; - } - #endif -} - -#if NS_BLOCKS_AVAILABLE -- (void)performBlockOnMainThread:(ASIBasicBlock)block -{ - [self performSelectorOnMainThread:@selector(callBlock:) withObject:[[block copy] autorelease] waitUntilDone:[NSThread isMainThread]]; -} - -- (void)callBlock:(ASIBasicBlock)block -{ - block(); -} -#endif - - -+ (void)performSelector:(SEL)selector onTarget:(id *)target withObject:(id)object amount:(void *)amount callerToRetain:(id)callerToRetain -{ - if ([*target respondsToSelector:selector]) { - NSMethodSignature *signature = nil; - signature = [*target methodSignatureForSelector:selector]; - NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; - - [invocation setSelector:selector]; - - int argumentNumber = 2; - - // If we got an object parameter, we pass a pointer to the object pointer - if (object) { - [invocation setArgument:&object atIndex:argumentNumber]; - argumentNumber++; - } - - // For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer to it - if (amount) { - [invocation setArgument:amount atIndex:argumentNumber]; - } - - SEL callback = @selector(performInvocation:onTarget:releasingObject:); - NSMethodSignature *cbSignature = [ASIHTTPRequest methodSignatureForSelector:callback]; - NSInvocation *cbInvocation = [NSInvocation invocationWithMethodSignature:cbSignature]; - [cbInvocation setSelector:callback]; - [cbInvocation setTarget:self]; - [cbInvocation setArgument:&invocation atIndex:2]; - [cbInvocation setArgument:&target atIndex:3]; - if (callerToRetain) { - [cbInvocation setArgument:&callerToRetain atIndex:4]; - } - - CFRetain(invocation); - - // Used to pass in a request that we must retain until after the call - // We're using CFRetain rather than [callerToRetain retain] so things to avoid earthquakes when using garbage collection - if (callerToRetain) { - CFRetain(callerToRetain); - } - [cbInvocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:[NSThread isMainThread]]; - } -} - -+ (void)performInvocation:(NSInvocation *)invocation onTarget:(id *)target releasingObject:(id)objectToRelease -{ - if (*target && [*target respondsToSelector:[invocation selector]]) { - [invocation invokeWithTarget:*target]; - } - CFRelease(invocation); - if (objectToRelease) { - CFRelease(objectToRelease); - } -} - - -+ (void)updateProgressIndicator:(id *)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total -{ - #if TARGET_OS_IPHONE - // Cocoa Touch: UIProgressView - SEL selector = @selector(setProgress:); - float progressAmount = (float)((progress*1.0)/(total*1.0)); - - #else - // Cocoa: NSProgressIndicator - double progressAmount = progressAmount = (progress*1.0)/(total*1.0); - SEL selector = @selector(setDoubleValue:); - #endif - - if (![*indicator respondsToSelector:selector]) { - return; - } - - [progressLock lock]; - [ASIHTTPRequest performSelector:selector onTarget:indicator withObject:nil amount:&progressAmount callerToRetain:nil]; - [progressLock unlock]; -} - - -#pragma mark talking to delegates / calling blocks - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)requestStarted -{ - if ([self error] || [self mainRequest]) { - return; - } - if (delegate && [delegate respondsToSelector:didStartSelector]) { - [delegate performSelector:didStartSelector withObject:self]; - } - #if NS_BLOCKS_AVAILABLE - if(startedBlock){ - startedBlock(); - } - #endif - if (queue && [queue respondsToSelector:@selector(requestStarted:)]) { - [queue performSelector:@selector(requestStarted:) withObject:self]; - } -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)requestRedirected -{ - if ([self error] || [self mainRequest]) { - return; - } - - if([[self delegate] respondsToSelector:@selector(requestRedirected:)]){ - [[self delegate] performSelector:@selector(requestRedirected:) withObject:self]; - } - - #if NS_BLOCKS_AVAILABLE - if(requestRedirectedBlock){ - requestRedirectedBlock(); - } - #endif -} - - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders -{ - if ([self error] || [self mainRequest]) { - return; - } - - if (delegate && [delegate respondsToSelector:didReceiveResponseHeadersSelector]) { - [delegate performSelector:didReceiveResponseHeadersSelector withObject:self withObject:newResponseHeaders]; - } - - #if NS_BLOCKS_AVAILABLE - if(headersReceivedBlock){ - headersReceivedBlock(newResponseHeaders); - } - #endif - - if (queue && [queue respondsToSelector:@selector(request:didReceiveResponseHeaders:)]) { - [queue performSelector:@selector(request:didReceiveResponseHeaders:) withObject:self withObject:newResponseHeaders]; - } -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)requestWillRedirectToURL:(NSURL *)newURL -{ - if ([self error] || [self mainRequest]) { - return; - } - if (delegate && [delegate respondsToSelector:willRedirectSelector]) { - [delegate performSelector:willRedirectSelector withObject:self withObject:newURL]; - } - if (queue && [queue respondsToSelector:@selector(request:willRedirectToURL:)]) { - [queue performSelector:@selector(request:willRedirectToURL:) withObject:self withObject:newURL]; - } -} - -// Subclasses might override this method to process the result in the same thread -// If you do this, don't forget to call [super requestFinished] to let the queue / delegate know we're done -- (void)requestFinished -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[STATUS] Request finished: %@",self); -#endif - if ([self error] || [self mainRequest]) { - return; - } - if ([self isPACFileRequest]) { - [self reportFinished]; - } else { - [self performSelectorOnMainThread:@selector(reportFinished) withObject:nil waitUntilDone:[NSThread isMainThread]]; - } -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)reportFinished -{ - if (delegate && [delegate respondsToSelector:didFinishSelector]) { - [delegate performSelector:didFinishSelector withObject:self]; - } - - #if NS_BLOCKS_AVAILABLE - if(completionBlock){ - completionBlock(); - } - #endif - - if (queue && [queue respondsToSelector:@selector(requestFinished:)]) { - [queue performSelector:@selector(requestFinished:) withObject:self]; - } -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)reportFailure -{ - if (delegate && [delegate respondsToSelector:didFailSelector]) { - [delegate performSelector:didFailSelector withObject:self]; - } - - #if NS_BLOCKS_AVAILABLE - if(failureBlock){ - failureBlock(); - } - #endif - - if (queue && [queue respondsToSelector:@selector(requestFailed:)]) { - [queue performSelector:@selector(requestFailed:) withObject:self]; - } -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)passOnReceivedData:(NSData *)data -{ - if (delegate && [delegate respondsToSelector:didReceiveDataSelector]) { - [delegate performSelector:didReceiveDataSelector withObject:self withObject:data]; - } - - #if NS_BLOCKS_AVAILABLE - if (dataReceivedBlock) { - dataReceivedBlock(data); - } - #endif -} - -// Subclasses might override this method to perform error handling in the same thread -// If you do this, don't forget to call [super failWithError:] to let the queue / delegate know we're done -- (void)failWithError:(NSError *)theError -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[STATUS] Request %@: %@",self,(theError == ASIRequestCancelledError ? @"Cancelled" : @"Failed")); -#endif - [self setComplete:YES]; - - // Invalidate the current connection so subsequent requests don't attempt to reuse it - if (theError && [theError code] != ASIAuthenticationErrorType && [theError code] != ASITooMuchRedirectionErrorType) { - [connectionsLock lock]; - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Request #%@ failed and will invalidate connection #%@",[self requestID],[[self connectionInfo] objectForKey:@"id"]); - #endif - [[self connectionInfo] removeObjectForKey:@"request"]; - [persistentConnectionsPool removeObject:[self connectionInfo]]; - [connectionsLock unlock]; - [self destroyReadStream]; - } - if ([self connectionCanBeReused]) { - [[self connectionInfo] setObject:[NSDate dateWithTimeIntervalSinceNow:[self persistentConnectionTimeoutSeconds]] forKey:@"expires"]; - } - - if ([self isCancelled] || [self error]) { - return; - } - - // If we have cached data, use it and ignore the error when using ASIFallbackToCacheIfLoadFailsCachePolicy - if ([self downloadCache] && ([self cachePolicy] & ASIFallbackToCacheIfLoadFailsCachePolicy)) { - if ([[self downloadCache] canUseCachedDataForRequest:self]) { - [self useDataFromCache]; - return; - } - } - - - [self setError:theError]; - - ASIHTTPRequest *failedRequest = self; - - // If this is a HEAD request created by an ASINetworkQueue or compatible queue delegate, make the main request fail - if ([self mainRequest]) { - failedRequest = [self mainRequest]; - [failedRequest setError:theError]; - } - - if ([self isPACFileRequest]) { - [failedRequest reportFailure]; - } else { - [failedRequest performSelectorOnMainThread:@selector(reportFailure) withObject:nil waitUntilDone:[NSThread isMainThread]]; - } - - if (!inProgress) - { - // if we're not in progress, we can't notify the queue we've finished (doing so can cause a crash later on) - // "markAsFinished" will be at the start of main() when we are started - return; - } - [self markAsFinished]; -} - -#pragma mark parsing HTTP response headers - -- (void)readResponseHeaders -{ - [self setAuthenticationNeeded:ASINoAuthenticationNeededYet]; - - CFHTTPMessageRef message = (CFHTTPMessageRef)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPResponseHeader); - if (!message) { - return; - } - - // Make sure we've received all the headers - if (!CFHTTPMessageIsHeaderComplete(message)) { - CFRelease(message); - return; - } - - #if DEBUG_REQUEST_STATUS - if ([self totalBytesSent] == [self postLength]) { - ASI_DEBUG_LOG(@"[STATUS] Request %@ received response headers",self); - } - #endif - - [self setResponseHeaders:[NSMakeCollectable(CFHTTPMessageCopyAllHeaderFields(message)) autorelease]]; - [self setResponseStatusCode:(int)CFHTTPMessageGetResponseStatusCode(message)]; - [self setResponseStatusMessage:[NSMakeCollectable(CFHTTPMessageCopyResponseStatusLine(message)) autorelease]]; - - if ([self downloadCache] && ([[self downloadCache] canUseCachedDataForRequest:self])) { - - // Update the expiry date - [[self downloadCache] updateExpiryForRequest:self maxAge:[self secondsToCache]]; - - // Read the response from the cache - [self useDataFromCache]; - - CFRelease(message); - return; - } - - // Is the server response a challenge for credentials? - if ([self responseStatusCode] == 401) { - [self setAuthenticationNeeded:ASIHTTPAuthenticationNeeded]; - } else if ([self responseStatusCode] == 407) { - [self setAuthenticationNeeded:ASIProxyAuthenticationNeeded]; - } else { - #if DEBUG_HTTP_AUTHENTICATION - if ([self authenticationScheme]) { - ASI_DEBUG_LOG(@"[AUTH] Request %@ has passed %@ authentication",self,[self authenticationScheme]); - } - #endif - } - - // Authentication succeeded, or no authentication was required - if (![self authenticationNeeded]) { - - // Did we get here without an authentication challenge? (which can happen when shouldPresentCredentialsBeforeChallenge is YES and basic auth was successful) - if (!requestAuthentication && [[self authenticationScheme] isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeBasic] && [self username] && [self password] && [self useSessionPersistence]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ passed BASIC authentication, and will save credentials in the session store for future use",self); - #endif - - NSMutableDictionary *newCredentials = [NSMutableDictionary dictionaryWithCapacity:2]; - [newCredentials setObject:[self username] forKey:(NSString *)kCFHTTPAuthenticationUsername]; - [newCredentials setObject:[self password] forKey:(NSString *)kCFHTTPAuthenticationPassword]; - - // Store the credentials in the session - NSMutableDictionary *sessionCredentials = [NSMutableDictionary dictionary]; - [sessionCredentials setObject:newCredentials forKey:@"Credentials"]; - [sessionCredentials setObject:[self url] forKey:@"URL"]; - [sessionCredentials setObject:(NSString *)kCFHTTPAuthenticationSchemeBasic forKey:@"AuthenticationScheme"]; - [[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials]; - } - } - - // Read response textEncoding - [self parseStringEncodingFromHeaders]; - - // Handle cookies - NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[self responseHeaders] forURL:[self url]]; - [self setResponseCookies:newCookies]; - - if ([self useCookiePersistence]) { - - // Store cookies in global persistent store - [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:[self url] mainDocumentURL:nil]; - - // We also keep any cookies in the sessionCookies array, so that we have a reference to them if we need to remove them later - NSHTTPCookie *cookie; - for (cookie in newCookies) { - [ASIHTTPRequest addSessionCookie:cookie]; - } - } - - // Do we need to redirect? - if (![self willRedirect]) { - // See if we got a Content-length header - NSString *cLength = [responseHeaders valueForKey:@"Content-Length"]; - ASIHTTPRequest *theRequest = self; - if ([self mainRequest]) { - theRequest = [self mainRequest]; - } - - if (cLength) { - unsigned long long length = strtoull([cLength UTF8String], NULL, 0); - - // Workaround for Apache HEAD requests for dynamically generated content returning the wrong Content-Length when using gzip - if ([self mainRequest] && [self allowCompressedResponse] && length == 20 && [self showAccurateProgress] && [self shouldResetDownloadProgress]) { - [[self mainRequest] setShowAccurateProgress:NO]; - [[self mainRequest] incrementDownloadSizeBy:1]; - - } else { - [theRequest setContentLength:length]; - if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) { - [theRequest incrementDownloadSizeBy:(long long)[theRequest contentLength]+(long long)[theRequest partialDownloadSize]]; - } - } - - } else if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) { - [theRequest setShowAccurateProgress:NO]; - [theRequest incrementDownloadSizeBy:1]; - } - } - - // Handle connection persistence - if ([self shouldAttemptPersistentConnection]) { - - NSString *connectionHeader = [[[self responseHeaders] objectForKey:@"Connection"] lowercaseString]; - - NSString *httpVersion = [NSMakeCollectable(CFHTTPMessageCopyVersion(message)) autorelease]; - - // Don't re-use the connection if the server is HTTP 1.0 and didn't send Connection: Keep-Alive - if (![httpVersion isEqualToString:(NSString *)kCFHTTPVersion1_0] || [connectionHeader isEqualToString:@"keep-alive"]) { - - // See if server explicitly told us to close the connection - if (![connectionHeader isEqualToString:@"close"]) { - - NSString *keepAliveHeader = [[self responseHeaders] objectForKey:@"Keep-Alive"]; - - // If we got a keep alive header, we'll reuse the connection for as long as the server tells us - if (keepAliveHeader) { - int timeout = 0; - int max = 0; - NSScanner *scanner = [NSScanner scannerWithString:keepAliveHeader]; - [scanner scanString:@"timeout=" intoString:NULL]; - [scanner scanInt:&timeout]; - [scanner scanUpToString:@"max=" intoString:NULL]; - [scanner scanString:@"max=" intoString:NULL]; - [scanner scanInt:&max]; - if (max > 5) { - [self setConnectionCanBeReused:YES]; - [self setPersistentConnectionTimeoutSeconds:timeout]; - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Got a keep-alive header, will keep this connection open for %f seconds", [self persistentConnectionTimeoutSeconds]); - #endif - } - - // Otherwise, we'll assume we can keep this connection open - } else { - [self setConnectionCanBeReused:YES]; - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Got no keep-alive header, will keep this connection open for %f seconds", [self persistentConnectionTimeoutSeconds]); - #endif - } - } - } - } - - CFRelease(message); - [self performSelectorOnMainThread:@selector(requestReceivedResponseHeaders:) withObject:[[[self responseHeaders] copy] autorelease] waitUntilDone:[NSThread isMainThread]]; -} - -- (BOOL)willRedirect -{ - // Do we need to redirect? - if (![self shouldRedirect] || ![responseHeaders valueForKey:@"Location"]) { - return NO; - } - - // Note that ASIHTTPRequest does not currently support 305 Use Proxy - int responseCode = [self responseStatusCode]; - if (responseCode != 301 && responseCode != 302 && responseCode != 303 && responseCode != 307) { - return NO; - } - - [self performSelectorOnMainThread:@selector(requestRedirected) withObject:nil waitUntilDone:[NSThread isMainThread]]; - - // By default, we redirect 301 and 302 response codes as GET requests - // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen - // See also: - // http://allseeing-i.lighthouseapp.com/projects/27881/tickets/27-302-redirection-issue - - if (responseCode != 307 && (![self shouldUseRFC2616RedirectBehaviour] || responseCode == 303)) { - [self setRequestMethod:@"GET"]; - [self setPostBody:nil]; - [self setPostLength:0]; - - // Perhaps there are other headers we should be preserving, but it's hard to know what we need to keep and what to throw away. - NSString *userAgentHeader = [[self requestHeaders] objectForKey:@"User-Agent"]; - NSString *acceptHeader = [[self requestHeaders] objectForKey:@"Accept"]; - [self setRequestHeaders:nil]; - if (userAgentHeader) { - [self addRequestHeader:@"User-Agent" value:userAgentHeader]; - } - if (acceptHeader) { - [self addRequestHeader:@"Accept" value:acceptHeader]; - } - [self setHaveBuiltRequestHeaders:NO]; - - } else { - // Force rebuild the cookie header incase we got some new cookies from this request - // All other request headers will remain as they are for 301 / 302 redirects - [self applyCookieHeader]; - } - - // Force the redirected request to rebuild the request headers (if not a 303, it will re-use old ones, and add any new ones) - [self setRedirectURL:[[NSURL URLWithString:[responseHeaders valueForKey:@"Location"] relativeToURL:[self url]] absoluteURL]]; - [self setNeedsRedirect:YES]; - - // Clear the request cookies - // This means manually added cookies will not be added to the redirect request - only those stored in the global persistent store - // But, this is probably the safest option - we might be redirecting to a different domain - [self setRequestCookies:[NSMutableArray array]]; - - #if DEBUG_REQUEST_STATUS - ASI_DEBUG_LOG(@"[STATUS] Request will redirect (code: %i): %@",responseCode,self); - #endif - - return YES; -} - -- (void)parseStringEncodingFromHeaders -{ - // Handle response text encoding - NSStringEncoding charset = 0; - NSString *mimeType = nil; - [[self class] parseMimeType:&mimeType andResponseEncoding:&charset fromContentType:[[self responseHeaders] valueForKey:@"Content-Type"]]; - if (charset != 0) { - [self setResponseEncoding:charset]; - } else { - [self setResponseEncoding:[self defaultResponseEncoding]]; - } -} - -#pragma mark http authentication - -- (void)saveProxyCredentialsToKeychain:(NSDictionary *)newCredentials -{ - NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; - if (authenticationCredentials) { - [ASIHTTPRequest saveCredentials:authenticationCredentials forProxy:[self proxyHost] port:[self proxyPort] realm:[self proxyAuthenticationRealm]]; - } -} - - -- (void)saveCredentialsToKeychain:(NSDictionary *)newCredentials -{ - NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; - - if (authenticationCredentials) { - [ASIHTTPRequest saveCredentials:authenticationCredentials forHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; - } -} - -- (BOOL)applyProxyCredentials:(NSDictionary *)newCredentials -{ - [self setProxyAuthenticationRetryCount:[self proxyAuthenticationRetryCount]+1]; - - if (newCredentials && proxyAuthentication && request) { - - // Apply whatever credentials we've built up to the old request - if (CFHTTPMessageApplyCredentialDictionary(request, proxyAuthentication, (CFMutableDictionaryRef)newCredentials, NULL)) { - - //If we have credentials and they're ok, let's save them to the keychain - if (useKeychainPersistence) { - [self saveProxyCredentialsToKeychain:newCredentials]; - } - if (useSessionPersistence) { - NSMutableDictionary *sessionProxyCredentials = [NSMutableDictionary dictionary]; - [sessionProxyCredentials setObject:(id)proxyAuthentication forKey:@"Authentication"]; - [sessionProxyCredentials setObject:newCredentials forKey:@"Credentials"]; - [sessionProxyCredentials setObject:[self proxyHost] forKey:@"Host"]; - [sessionProxyCredentials setObject:[NSNumber numberWithInt:[self proxyPort]] forKey:@"Port"]; - [sessionProxyCredentials setObject:[self proxyAuthenticationScheme] forKey:@"AuthenticationScheme"]; - [[self class] storeProxyAuthenticationCredentialsInSessionStore:sessionProxyCredentials]; - } - [self setProxyCredentials:newCredentials]; - return YES; - } else { - [[self class] removeProxyAuthenticationCredentialsFromSessionStore:newCredentials]; - } - } - return NO; -} - -- (BOOL)applyCredentials:(NSDictionary *)newCredentials -{ - [self setAuthenticationRetryCount:[self authenticationRetryCount]+1]; - - if (newCredentials && requestAuthentication && request) { - // Apply whatever credentials we've built up to the old request - if (CFHTTPMessageApplyCredentialDictionary(request, requestAuthentication, (CFMutableDictionaryRef)newCredentials, NULL)) { - - //If we have credentials and they're ok, let's save them to the keychain - if (useKeychainPersistence) { - [self saveCredentialsToKeychain:newCredentials]; - } - if (useSessionPersistence) { - - NSMutableDictionary *sessionCredentials = [NSMutableDictionary dictionary]; - [sessionCredentials setObject:(id)requestAuthentication forKey:@"Authentication"]; - [sessionCredentials setObject:newCredentials forKey:@"Credentials"]; - [sessionCredentials setObject:[self url] forKey:@"URL"]; - [sessionCredentials setObject:[self authenticationScheme] forKey:@"AuthenticationScheme"]; - if ([self authenticationRealm]) { - [sessionCredentials setObject:[self authenticationRealm] forKey:@"AuthenticationRealm"]; - } - [[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials]; - - } - [self setRequestCredentials:newCredentials]; - return YES; - } else { - [[self class] removeAuthenticationCredentialsFromSessionStore:newCredentials]; - } - } - return NO; -} - -- (NSMutableDictionary *)findProxyCredentials -{ - NSMutableDictionary *newCredentials = [[[NSMutableDictionary alloc] init] autorelease]; - - NSString *user = nil; - NSString *pass = nil; - - ASIHTTPRequest *theRequest = [self mainRequest]; - // If this is a HEAD request generated by an ASINetworkQueue, we'll try to use the details from the main request - if ([theRequest proxyUsername] && [theRequest proxyPassword]) { - user = [theRequest proxyUsername]; - pass = [theRequest proxyPassword]; - - // Let's try to use the ones set in this object - } else if ([self proxyUsername] && [self proxyPassword]) { - user = [self proxyUsername]; - pass = [self proxyPassword]; - } - - // When we connect to a website using NTLM via a proxy, we will use the main credentials - if ((!user || !pass) && [self proxyAuthenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM) { - user = [self username]; - pass = [self password]; - } - - - - // Ok, that didn't work, let's try the keychain - // For authenticating proxies, we'll look in the keychain regardless of the value of useKeychainPersistence - if ((!user || !pass)) { - NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForProxy:[self proxyHost] port:[self proxyPort] protocol:[[self url] scheme] realm:[self proxyAuthenticationRealm]]; - if (authenticationCredentials) { - user = [authenticationCredentials user]; - pass = [authenticationCredentials password]; - } - - } - - // Handle NTLM, which requires a domain to be set too - if (CFHTTPAuthenticationRequiresAccountDomain(proxyAuthentication)) { - - NSString *ntlmDomain = [self proxyDomain]; - - // If we have no domain yet - if (!ntlmDomain || [ntlmDomain length] == 0) { - - // Let's try to extract it from the username - NSArray* ntlmComponents = [user componentsSeparatedByString:@"\\"]; - if ([ntlmComponents count] == 2) { - ntlmDomain = [ntlmComponents objectAtIndex:0]; - user = [ntlmComponents objectAtIndex:1]; - - // If we are connecting to a website using NTLM, but we are connecting via a proxy, the string we need may be in the domain property - } else { - ntlmDomain = [self domain]; - } - if (!ntlmDomain) { - ntlmDomain = @""; - } - } - [newCredentials setObject:ntlmDomain forKey:(NSString *)kCFHTTPAuthenticationAccountDomain]; - } - - - // If we have a username and password, let's apply them to the request and continue - if (user && pass) { - [newCredentials setObject:user forKey:(NSString *)kCFHTTPAuthenticationUsername]; - [newCredentials setObject:pass forKey:(NSString *)kCFHTTPAuthenticationPassword]; - return newCredentials; - } - return nil; -} - - -- (NSMutableDictionary *)findCredentials -{ - NSMutableDictionary *newCredentials = [[[NSMutableDictionary alloc] init] autorelease]; - - // First, let's look at the url to see if the username and password were included - NSString *user = [[self url] user]; - NSString *pass = [[self url] password]; - - if (user && pass) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will use credentials set on its url",self); - #endif - - } else { - - // If this is a HEAD request generated by an ASINetworkQueue, we'll try to use the details from the main request - if ([self mainRequest] && [[self mainRequest] username] && [[self mainRequest] password]) { - user = [[self mainRequest] username]; - pass = [[self mainRequest] password]; - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will use credentials from its parent request",self); - #endif - - // Let's try to use the ones set in this object - } else if ([self username] && [self password]) { - user = [self username]; - pass = [self password]; - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will use username and password properties as credentials",self); - #endif - } - } - - // Ok, that didn't work, let's try the keychain - if ((!user || !pass) && useKeychainPersistence) { - NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; - if (authenticationCredentials) { - user = [authenticationCredentials user]; - pass = [authenticationCredentials password]; - #if DEBUG_HTTP_AUTHENTICATION - if (user && pass) { - ASI_DEBUG_LOG(@"[AUTH] Request %@ will use credentials from the keychain",self); - } - #endif - } - } - - // Handle NTLM, which requires a domain to be set too - if (CFHTTPAuthenticationRequiresAccountDomain(requestAuthentication)) { - - NSString *ntlmDomain = [self domain]; - - // If we have no domain yet, let's try to extract it from the username - if (!ntlmDomain || [ntlmDomain length] == 0) { - ntlmDomain = @""; - NSArray* ntlmComponents = [user componentsSeparatedByString:@"\\"]; - if ([ntlmComponents count] == 2) { - ntlmDomain = [ntlmComponents objectAtIndex:0]; - user = [ntlmComponents objectAtIndex:1]; - } - } - [newCredentials setObject:ntlmDomain forKey:(NSString *)kCFHTTPAuthenticationAccountDomain]; - } - - // If we have a username and password, let's apply them to the request and continue - if (user && pass) { - [newCredentials setObject:user forKey:(NSString *)kCFHTTPAuthenticationUsername]; - [newCredentials setObject:pass forKey:(NSString *)kCFHTTPAuthenticationPassword]; - return newCredentials; - } - return nil; -} - -// Called by delegate or authentication dialog to resume loading once authentication info has been populated -- (void)retryUsingSuppliedCredentials -{ - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ received credentials from its delegate or an ASIAuthenticationDialog, will retry",self); - #endif - //If the url was changed by the delegate, our CFHTTPMessageRef will be NULL and we'll go back to the start - if (!request) { - [self performSelector:@selector(main) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; - return; - } - [self performSelector:@selector(attemptToApplyCredentialsAndResume) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; -} - -// Called by delegate or authentication dialog to cancel authentication -- (void)cancelAuthentication -{ - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ had authentication cancelled by its delegate or an ASIAuthenticationDialog",self); - #endif - [self performSelector:@selector(failAuthentication) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; -} - -- (void)failAuthentication -{ - [self failWithError:ASIAuthenticationError]; -} - -- (BOOL)showProxyAuthenticationDialog -{ - if ([self isSynchronous]) { - return NO; - } - - // Mac authentication dialog coming soon! - #if TARGET_OS_IPHONE - if ([self shouldPresentProxyAuthenticationDialog]) { - [ASIAuthenticationDialog performSelectorOnMainThread:@selector(presentAuthenticationDialogForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; - return YES; - } - return NO; - #else - return NO; - #endif -} - - -- (BOOL)willAskDelegateForProxyCredentials -{ - if ([self isSynchronous]) { - return NO; - } - - // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. - // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate - id authenticationDelegate = [self delegate]; - if (!authenticationDelegate) { - authenticationDelegate = [self queue]; - } - - BOOL delegateOrBlockWillHandleAuthentication = NO; - - if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - delegateOrBlockWillHandleAuthentication = YES; - } - - #if NS_BLOCKS_AVAILABLE - if(proxyAuthenticationNeededBlock){ - delegateOrBlockWillHandleAuthentication = YES; - } - #endif - - if (delegateOrBlockWillHandleAuthentication) { - [self performSelectorOnMainThread:@selector(askDelegateForProxyCredentials) withObject:nil waitUntilDone:NO]; - } - - return delegateOrBlockWillHandleAuthentication; -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)askDelegateForProxyCredentials -{ - id authenticationDelegate = [self delegate]; - if (!authenticationDelegate) { - authenticationDelegate = [self queue]; - } - if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - [authenticationDelegate performSelector:@selector(proxyAuthenticationNeededForRequest:) withObject:self]; - return; - } - #if NS_BLOCKS_AVAILABLE - if(proxyAuthenticationNeededBlock){ - proxyAuthenticationNeededBlock(); - } - #endif -} - - -- (BOOL)willAskDelegateForCredentials -{ - if ([self isSynchronous]) { - return NO; - } - - // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. - // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate - id authenticationDelegate = [self delegate]; - if (!authenticationDelegate) { - authenticationDelegate = [self queue]; - } - - BOOL delegateOrBlockWillHandleAuthentication = NO; - - if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) { - delegateOrBlockWillHandleAuthentication = YES; - } - - #if NS_BLOCKS_AVAILABLE - if (authenticationNeededBlock) { - delegateOrBlockWillHandleAuthentication = YES; - } - #endif - - if (delegateOrBlockWillHandleAuthentication) { - [self performSelectorOnMainThread:@selector(askDelegateForCredentials) withObject:nil waitUntilDone:NO]; - } - return delegateOrBlockWillHandleAuthentication; -} - -/* ALWAYS CALLED ON MAIN THREAD! */ -- (void)askDelegateForCredentials -{ - id authenticationDelegate = [self delegate]; - if (!authenticationDelegate) { - authenticationDelegate = [self queue]; - } - - if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) { - [authenticationDelegate performSelector:@selector(authenticationNeededForRequest:) withObject:self]; - return; - } - - #if NS_BLOCKS_AVAILABLE - if (authenticationNeededBlock) { - authenticationNeededBlock(); - } - #endif -} - -- (void)attemptToApplyProxyCredentialsAndResume -{ - - if ([self error] || [self isCancelled]) { - return; - } - - // Read authentication data - if (!proxyAuthentication) { - CFHTTPMessageRef responseHeader = (CFHTTPMessageRef) CFReadStreamCopyProperty((CFReadStreamRef)[self readStream],kCFStreamPropertyHTTPResponseHeader); - proxyAuthentication = CFHTTPAuthenticationCreateFromResponse(NULL, responseHeader); - CFRelease(responseHeader); - [self setProxyAuthenticationScheme:[NSMakeCollectable(CFHTTPAuthenticationCopyMethod(proxyAuthentication)) autorelease]]; - } - - // If we haven't got a CFHTTPAuthenticationRef by now, something is badly wrong, so we'll have to give up - if (!proxyAuthentication) { - [self cancelLoad]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; - return; - } - - // Get the authentication realm - [self setProxyAuthenticationRealm:nil]; - if (!CFHTTPAuthenticationRequiresAccountDomain(proxyAuthentication)) { - [self setProxyAuthenticationRealm:[NSMakeCollectable(CFHTTPAuthenticationCopyRealm(proxyAuthentication)) autorelease]]; - } - - // See if authentication is valid - CFStreamError err; - if (!CFHTTPAuthenticationIsValid(proxyAuthentication, &err)) { - - CFRelease(proxyAuthentication); - proxyAuthentication = NULL; - - // check for bad credentials, so we can give the delegate a chance to replace them - if (err.domain == kCFStreamErrorDomainHTTP && (err.error == kCFStreamErrorHTTPAuthenticationBadUserName || err.error == kCFStreamErrorHTTPAuthenticationBadPassword)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // We know the credentials we just presented are bad, we should remove them from the session store too - [[self class] removeProxyAuthenticationCredentialsFromSessionStore:proxyCredentials]; - [self setProxyCredentials:nil]; - - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - [delegateAuthenticationLock unlock]; - return; - } - - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionProxyAuthenticationCredentials]; - if (credentials && [self applyProxyCredentials:[credentials objectForKey:@"Credentials"]]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - [self setLastActivityTime:nil]; - - if ([self willAskDelegateForProxyCredentials]) { - [self attemptToApplyProxyCredentialsAndResume]; - [delegateAuthenticationLock unlock]; - return; - } - if ([self showProxyAuthenticationDialog]) { - [self attemptToApplyProxyCredentialsAndResume]; - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - } - [self cancelLoad]; - [self failWithError:ASIAuthenticationError]; - return; - } - - [self cancelLoad]; - - if (proxyCredentials) { - - // We use startRequest rather than starting all over again in load request because NTLM requires we reuse the request - if ((([self proxyAuthenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM) || [self proxyAuthenticationRetryCount] < 2) && [self applyProxyCredentials:proxyCredentials]) { - [self startRequest]; - - // We've failed NTLM authentication twice, we should assume our credentials are wrong - } else if ([self proxyAuthenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM && [self proxyAuthenticationRetryCount] == 2) { - [self failWithError:ASIAuthenticationError]; - - // Something went wrong, we'll have to give up - } else { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply proxy credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - - // Are a user name & password needed? - } else if (CFHTTPAuthenticationRequiresUserNameAndPassword(proxyAuthentication)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - [delegateAuthenticationLock unlock]; - return; - } - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionProxyAuthenticationCredentials]; - if (credentials && [self applyProxyCredentials:[credentials objectForKey:@"Credentials"]]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - NSMutableDictionary *newCredentials = [self findProxyCredentials]; - - //If we have some credentials to use let's apply them to the request and continue - if (newCredentials) { - - if ([self applyProxyCredentials:newCredentials]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - } else { - [delegateAuthenticationLock unlock]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply proxy credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - - return; - } - - if ([self willAskDelegateForProxyCredentials]) { - [delegateAuthenticationLock unlock]; - return; - } - - if ([self showProxyAuthenticationDialog]) { - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - - // The delegate isn't interested and we aren't showing the authentication dialog, we'll have to give up - [self failWithError:ASIAuthenticationError]; - return; - } - -} - -- (BOOL)showAuthenticationDialog -{ - if ([self isSynchronous]) { - return NO; - } - // Mac authentication dialog coming soon! - #if TARGET_OS_IPHONE - if ([self shouldPresentAuthenticationDialog]) { - [ASIAuthenticationDialog performSelectorOnMainThread:@selector(presentAuthenticationDialogForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; - return YES; - } - return NO; - #else - return NO; - #endif -} - -- (void)attemptToApplyCredentialsAndResume -{ - if ([self error] || [self isCancelled]) { - return; - } - - // Do we actually need to authenticate with a proxy? - if ([self authenticationNeeded] == ASIProxyAuthenticationNeeded) { - [self attemptToApplyProxyCredentialsAndResume]; - return; - } - - // Read authentication data - if (!requestAuthentication) { - CFHTTPMessageRef responseHeader = (CFHTTPMessageRef) CFReadStreamCopyProperty((CFReadStreamRef)[self readStream],kCFStreamPropertyHTTPResponseHeader); - requestAuthentication = CFHTTPAuthenticationCreateFromResponse(NULL, responseHeader); - CFRelease(responseHeader); - [self setAuthenticationScheme:[NSMakeCollectable(CFHTTPAuthenticationCopyMethod(requestAuthentication)) autorelease]]; - } - - if (!requestAuthentication) { - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ failed to read authentication information from response headers",self); - #endif - - [self cancelLoad]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; - return; - } - - // Get the authentication realm - [self setAuthenticationRealm:nil]; - if (!CFHTTPAuthenticationRequiresAccountDomain(requestAuthentication)) { - [self setAuthenticationRealm:[NSMakeCollectable(CFHTTPAuthenticationCopyRealm(requestAuthentication)) autorelease]]; - } - - #if DEBUG_HTTP_AUTHENTICATION - NSString *realm = [self authenticationRealm]; - if (realm) { - realm = [NSString stringWithFormat:@" (Realm: %@)",realm]; - } else { - realm = @""; - } - if ([self authenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM || [self authenticationRetryCount] == 0) { - ASI_DEBUG_LOG(@"[AUTH] Request %@ received 401 challenge and must authenticate using %@%@",self,[self authenticationScheme],realm); - } else { - ASI_DEBUG_LOG(@"[AUTH] Request %@ NTLM handshake step %i",self,[self authenticationRetryCount]+1); - } - #endif - - // See if authentication is valid - CFStreamError err; - if (!CFHTTPAuthenticationIsValid(requestAuthentication, &err)) { - - CFRelease(requestAuthentication); - requestAuthentication = NULL; - - // check for bad credentials, so we can give the delegate a chance to replace them - if (err.domain == kCFStreamErrorDomainHTTP && (err.error == kCFStreamErrorHTTPAuthenticationBadUserName || err.error == kCFStreamErrorHTTPAuthenticationBadPassword)) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ had bad credentials, will remove them from the session store if they are cached",self); - #endif - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // We know the credentials we just presented are bad, we should remove them from the session store too - [[self class] removeAuthenticationCredentialsFromSessionStore:requestCredentials]; - [self setRequestCredentials:nil]; - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ failed or was cancelled while waiting to access credentials",self); - #endif - - [delegateAuthenticationLock unlock]; - return; - } - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionAuthenticationCredentials]; - if (credentials && [self applyCredentials:[credentials objectForKey:@"Credentials"]]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will reuse cached credentials from the session (%@)",self,[credentials objectForKey:@"AuthenticationScheme"]); - #endif - - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - [self setLastActivityTime:nil]; - - if ([self willAskDelegateForCredentials]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will ask its delegate for credentials to use",self); - #endif - - [delegateAuthenticationLock unlock]; - return; - } - if ([self showAuthenticationDialog]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will ask ASIAuthenticationDialog for credentials",self); - #endif - - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - } - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ has no credentials to present and must give up",self); - #endif - - [self cancelLoad]; - [self failWithError:ASIAuthenticationError]; - return; - } - - [self cancelLoad]; - - if (requestCredentials) { - - if ((([self authenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM) || [self authenticationRetryCount] < 2) && [self applyCredentials:requestCredentials]) { - [self startRequest]; - - // We've failed NTLM authentication twice, we should assume our credentials are wrong - } else if ([self authenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM && [self authenticationRetryCount ] == 2) { - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ has failed NTLM authentication",self); - #endif - - [self failWithError:ASIAuthenticationError]; - - } else { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ had credentials and they were not marked as bad, but we got a 401 all the same.",self); - #endif - - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - - // Are a user name & password needed? - } else if (CFHTTPAuthenticationRequiresUserNameAndPassword(requestAuthentication)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ failed or was cancelled while waiting to access credentials",self); - #endif - - [delegateAuthenticationLock unlock]; - return; - } - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionAuthenticationCredentials]; - if (credentials && [self applyCredentials:[credentials objectForKey:@"Credentials"]]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will reuse cached credentials from the session (%@)",self,[credentials objectForKey:@"AuthenticationScheme"]); - #endif - - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - - NSMutableDictionary *newCredentials = [self findCredentials]; - - //If we have some credentials to use let's apply them to the request and continue - if (newCredentials) { - - if ([self applyCredentials:newCredentials]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - } else { - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ failed to apply credentials",self); - #endif - [delegateAuthenticationLock unlock]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - return; - } - if ([self willAskDelegateForCredentials]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will ask its delegate for credentials to use",self); - #endif - - [delegateAuthenticationLock unlock]; - return; - } - if ([self showAuthenticationDialog]) { - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ will ask ASIAuthenticationDialog for credentials",self); - #endif - - [delegateAuthenticationLock unlock]; - return; - } - - #if DEBUG_HTTP_AUTHENTICATION - ASI_DEBUG_LOG(@"[AUTH] Request %@ has no credentials to present and must give up",self); - #endif - [delegateAuthenticationLock unlock]; - [self failWithError:ASIAuthenticationError]; - return; - } - -} - -- (void)addBasicAuthenticationHeaderWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword -{ - [self addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:@"%@:%@",theUsername,thePassword] dataUsingEncoding:NSUTF8StringEncoding]]]]; - [self setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; - -} - - -#pragma mark stream status handlers - -- (void)handleNetworkEvent:(CFStreamEventType)type -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - [[self cancelledLock] lock]; - - if ([self complete] || [self isCancelled]) { - [[self cancelledLock] unlock]; - [pool drain]; - return; - } - - CFRetain(self); - - // Dispatch the stream events. - switch (type) { - case kCFStreamEventHasBytesAvailable: - [self handleBytesAvailable]; - break; - - case kCFStreamEventEndEncountered: - [self handleStreamComplete]; - break; - - case kCFStreamEventErrorOccurred: - [self handleStreamError]; - break; - - default: - break; - } - - [self performThrottling]; - - [[self cancelledLock] unlock]; - - if ([self downloadComplete] && [self needsRedirect]) { - if (![self willAskDelegateToConfirmRedirect]) { - [self performRedirect]; - } - } else if ([self downloadComplete] && [self authenticationNeeded]) { - [self attemptToApplyCredentialsAndResume]; - } - - CFRelease(self); - [pool drain]; -} - -- (BOOL)willAskDelegateToConfirmRedirect -{ - // We must lock to ensure delegate / queue aren't changed while we check them - [[self cancelledLock] lock]; - - // Here we perform an initial check to see if either the delegate or the queue wants to be asked about the redirect, because if not we should redirect straight away - // We will check again on the main thread later - BOOL needToAskDelegateAboutRedirect = (([self delegate] && [[self delegate] respondsToSelector:[self willRedirectSelector]]) || ([self queue] && [[self queue] respondsToSelector:@selector(request:willRedirectToURL:)])); - - [[self cancelledLock] unlock]; - - // Either the delegate or the queue's delegate is interested in being told when we are about to redirect - if (needToAskDelegateAboutRedirect) { - NSURL *newURL = [[[self redirectURL] copy] autorelease]; - [self setRedirectURL:nil]; - [self performSelectorOnMainThread:@selector(requestWillRedirectToURL:) withObject:newURL waitUntilDone:[NSThread isMainThread]]; - return true; - } - return false; -} - -- (void)handleBytesAvailable -{ - if (![self responseHeaders]) { - [self readResponseHeaders]; - } - - // If we've cancelled the load part way through (for example, after deciding to use a cached version) - if ([self complete]) { - return; - } - - // In certain (presumably very rare) circumstances, handleBytesAvailable seems to be called when there isn't actually any data available - // We'll check that there is actually data available to prevent blocking on CFReadStreamRead() - // So far, I've only seen this in the stress tests, so it might never happen in real-world situations. - if (!CFReadStreamHasBytesAvailable((CFReadStreamRef)[self readStream])) { - return; - } - - long long bufferSize = 16384; - if (contentLength > 262144) { - bufferSize = 262144; - } else if (contentLength > 65536) { - bufferSize = 65536; - } - - // Reduce the buffer size if we're receiving data too quickly when bandwidth throttling is active - // This just augments the throttling done in measureBandwidthUsage to reduce the amount we go over the limit - - if ([[self class] isBandwidthThrottled]) { - [bandwidthThrottlingLock lock]; - if (maxBandwidthPerSecond > 0) { - long long maxiumumSize = (long long)maxBandwidthPerSecond-(long long)bandwidthUsedInLastSecond; - if (maxiumumSize < 0) { - // We aren't supposed to read any more data right now, but we'll read a single byte anyway so the CFNetwork's buffer isn't full - bufferSize = 1; - } else if (maxiumumSize/4 < bufferSize) { - // We were going to fetch more data that we should be allowed, so we'll reduce the size of our read - bufferSize = maxiumumSize/4; - } - } - if (bufferSize < 1) { - bufferSize = 1; - } - [bandwidthThrottlingLock unlock]; - } - - - UInt8 buffer[bufferSize]; - NSInteger bytesRead = [[self readStream] read:buffer maxLength:sizeof(buffer)]; - - // Less than zero is an error - if (bytesRead < 0) { - [self handleStreamError]; - - // If zero bytes were read, wait for the EOF to come. - } else if (bytesRead) { - - // If we are inflating the response on the fly - NSData *inflatedData = nil; - if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) { - if (![self dataDecompressor]) { - [self setDataDecompressor:[ASIDataDecompressor decompressor]]; - } - NSError *err = nil; - inflatedData = [[self dataDecompressor] uncompressBytes:buffer length:(NSUInteger)bytesRead error:&err]; - if (err) { - [self failWithError:err]; - return; - } - } - - [self setTotalBytesRead:[self totalBytesRead]+(NSUInteger)bytesRead]; - [self setLastActivityTime:[NSDate date]]; - - // For bandwidth measurement / throttling - [ASIHTTPRequest incrementBandwidthUsedInLastSecond:(NSUInteger)bytesRead]; - - // If we need to redirect, and have automatic redirect on, and might be resuming a download, let's do nothing with the content - if ([self needsRedirect] && [self shouldRedirect] && [self allowResumeForFileDownloads]) { - return; - } - - BOOL dataWillBeHandledExternally = NO; - if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) { - dataWillBeHandledExternally = YES; - } - #if NS_BLOCKS_AVAILABLE - if (dataReceivedBlock) { - dataWillBeHandledExternally = YES; - } - #endif - // Does the delegate want to handle the data manually? - if (dataWillBeHandledExternally) { - - NSData *data = nil; - if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) { - data = inflatedData; - } else { - data = [NSData dataWithBytes:buffer length:(NSUInteger)bytesRead]; - } - [self performSelectorOnMainThread:@selector(passOnReceivedData:) withObject:data waitUntilDone:[NSThread isMainThread]]; - - // Are we downloading to a file? - } else if ([self downloadDestinationPath]) { - BOOL append = NO; - if (![self fileDownloadOutputStream]) { - if (![self temporaryFileDownloadPath]) { - [self setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - } else if ([self allowResumeForFileDownloads] && [[self requestHeaders] objectForKey:@"Range"]) { - if ([[self responseHeaders] objectForKey:@"Content-Range"]) { - append = YES; - } else { - [self incrementDownloadSizeBy:-(long long)[self partialDownloadSize]]; - [self setPartialDownloadSize:0]; - } - } - - [self setFileDownloadOutputStream:[[[NSOutputStream alloc] initToFileAtPath:[self temporaryFileDownloadPath] append:append] autorelease]]; - [[self fileDownloadOutputStream] open]; - - } - [[self fileDownloadOutputStream] write:buffer maxLength:(NSUInteger)bytesRead]; - - if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) { - - if (![self inflatedFileDownloadOutputStream]) { - if (![self temporaryUncompressedDataDownloadPath]) { - [self setTemporaryUncompressedDataDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - } - - [self setInflatedFileDownloadOutputStream:[[[NSOutputStream alloc] initToFileAtPath:[self temporaryUncompressedDataDownloadPath] append:append] autorelease]]; - [[self inflatedFileDownloadOutputStream] open]; - } - - [[self inflatedFileDownloadOutputStream] write:[inflatedData bytes] maxLength:[inflatedData length]]; - } - - - //Otherwise, let's add the data to our in-memory store - } else { - if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) { - [rawResponseData appendData:inflatedData]; - } else { - [rawResponseData appendBytes:buffer length:(NSUInteger)bytesRead]; - } - } - } -} - -- (void)handleStreamComplete -{ - -#if DEBUG_REQUEST_STATUS - ASI_DEBUG_LOG(@"[STATUS] Request %@ finished downloading data (%qu bytes)",self, [self totalBytesRead]); -#endif - [self setStatusTimer:nil]; - [self setDownloadComplete:YES]; - - if (![self responseHeaders]) { - [self readResponseHeaders]; - } - - [progressLock lock]; - // Find out how much data we've uploaded so far - [self setLastBytesSent:totalBytesSent]; - [self setTotalBytesSent:[[NSMakeCollectable(CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPRequestBytesWrittenCount)) autorelease] unsignedLongLongValue]]; - [self setComplete:YES]; - if (![self contentLength]) { - [self setContentLength:[self totalBytesRead]]; - } - [self updateProgressIndicators]; - - - [[self postBodyReadStream] close]; - [self setPostBodyReadStream:nil]; - - [self setDataDecompressor:nil]; - - NSError *fileError = nil; - - // Delete up the request body temporary file, if it exists - if ([self didCreateTemporaryPostDataFile] && ![self authenticationNeeded]) { - [self removeTemporaryUploadFile]; - [self removeTemporaryCompressedUploadFile]; - } - - // Close the output stream as we're done writing to the file - if ([self temporaryFileDownloadPath]) { - - [[self fileDownloadOutputStream] close]; - [self setFileDownloadOutputStream:nil]; - - [[self inflatedFileDownloadOutputStream] close]; - [self setInflatedFileDownloadOutputStream:nil]; - - // If we are going to redirect and we are resuming, let's ignore this download - if ([self shouldRedirect] && [self needsRedirect] && [self allowResumeForFileDownloads]) { - - } else if ([self isResponseCompressed]) { - - // Decompress the file directly to the destination path - if ([self shouldWaitToInflateCompressedResponses]) { - [ASIDataDecompressor uncompressDataFromFile:[self temporaryFileDownloadPath] toFile:[self downloadDestinationPath] error:&fileError]; - - // Response should already have been inflated, move the temporary file to the destination path - } else { - NSError *moveError = nil; - [[[[NSFileManager alloc] init] autorelease] moveItemAtPath:[self temporaryUncompressedDataDownloadPath] toPath:[self downloadDestinationPath] error:&moveError]; - if (moveError) { - fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to move file from '%@' to '%@'",[self temporaryFileDownloadPath],[self downloadDestinationPath]],NSLocalizedDescriptionKey,moveError,NSUnderlyingErrorKey,nil]]; - } - [self setTemporaryUncompressedDataDownloadPath:nil]; - - } - [self removeTemporaryDownloadFile]; - - } else { - - //Remove any file at the destination path - NSError *moveError = nil; - if (![[self class] removeFileAtPath:[self downloadDestinationPath] error:&moveError]) { - fileError = moveError; - - } - - //Move the temporary file to the destination path - if (!fileError) { - [[[[NSFileManager alloc] init] autorelease] moveItemAtPath:[self temporaryFileDownloadPath] toPath:[self downloadDestinationPath] error:&moveError]; - if (moveError) { - fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to move file from '%@' to '%@'",[self temporaryFileDownloadPath],[self downloadDestinationPath]],NSLocalizedDescriptionKey,moveError,NSUnderlyingErrorKey,nil]]; - } - [self setTemporaryFileDownloadPath:nil]; - } - - } - } - - // Save to the cache - if ([self downloadCache] && ![self didUseCachedResponse]) { - [[self downloadCache] storeResponseForRequest:self maxAge:[self secondsToCache]]; - } - - [progressLock unlock]; - - - [connectionsLock lock]; - if (![self connectionCanBeReused]) { - [self unscheduleReadStream]; - } - #if DEBUG_PERSISTENT_CONNECTIONS - if ([self requestID]) { - ASI_DEBUG_LOG(@"[CONNECTION] Request #%@ finished using connection #%@",[self requestID], [[self connectionInfo] objectForKey:@"id"]); - } - #endif - [[self connectionInfo] removeObjectForKey:@"request"]; - [[self connectionInfo] setObject:[NSDate dateWithTimeIntervalSinceNow:[self persistentConnectionTimeoutSeconds]] forKey:@"expires"]; - [connectionsLock unlock]; - - if (![self authenticationNeeded]) { - [self destroyReadStream]; - } - - - if (![self needsRedirect] && ![self authenticationNeeded] && ![self didUseCachedResponse]) { - - if (fileError) { - [self failWithError:fileError]; - } else { - [self requestFinished]; - } - - [self markAsFinished]; - - // If request has asked delegate or ASIAuthenticationDialog for credentials - } else if ([self authenticationNeeded]) { - // Do nothing. - } - -} - -- (void)markAsFinished -{ - // Autoreleased requests may well be dealloced here otherwise - CFRetain(self); - - // dealloc won't be called when running with GC, so we'll clean these up now - if (request) { - CFRelease(request); - request = nil; - } - if (requestAuthentication) { - CFRelease(requestAuthentication); - requestAuthentication = nil; - } - if (proxyAuthentication) { - CFRelease(proxyAuthentication); - proxyAuthentication = nil; - } - - BOOL wasInProgress = inProgress; - BOOL wasFinished = finished; - - if (!wasFinished) - [self willChangeValueForKey:@"isFinished"]; - if (wasInProgress) - [self willChangeValueForKey:@"isExecuting"]; - - [self setInProgress:NO]; - finished = YES; - - if (wasInProgress) - [self didChangeValueForKey:@"isExecuting"]; - if (!wasFinished) - [self didChangeValueForKey:@"isFinished"]; - - #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - if ([ASIHTTPRequest isMultitaskingSupported] && [self shouldContinueWhenAppEntersBackground]) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (backgroundTask != UIBackgroundTaskInvalid) { - [[UIApplication sharedApplication] endBackgroundTask:backgroundTask]; - backgroundTask = UIBackgroundTaskInvalid; - } - }); - } - #endif - CFRelease(self); -} - -- (void)useDataFromCache -{ - NSDictionary *headers = [[self downloadCache] cachedResponseHeadersForURL:[self url]]; - NSString *dataPath = [[self downloadCache] pathToCachedResponseDataForURL:[self url]]; - - ASIHTTPRequest *theRequest = self; - if ([self mainRequest]) { - theRequest = [self mainRequest]; - } - - if (headers && dataPath) { - - [self setResponseStatusCode:[[headers objectForKey:@"X-ASIHTTPRequest-Response-Status-Code"] intValue]]; - [self setDidUseCachedResponse:YES]; - [theRequest setResponseHeaders:headers]; - - if ([theRequest downloadDestinationPath]) { - [theRequest setDownloadDestinationPath:dataPath]; - } else { - [theRequest setRawResponseData:[NSMutableData dataWithData:[[self downloadCache] cachedResponseDataForURL:[self url]]]]; - } - [theRequest setContentLength:(unsigned long long)[[[self responseHeaders] objectForKey:@"Content-Length"] longLongValue]]; - [theRequest setTotalBytesRead:[self contentLength]]; - - [theRequest parseStringEncodingFromHeaders]; - - [theRequest setResponseCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:[self url]]]; - - // See if we need to redirect - if ([self willRedirect]) { - if (![self willAskDelegateToConfirmRedirect]) { - [self performRedirect]; - } - return; - } - } - - [theRequest setComplete:YES]; - [theRequest setDownloadComplete:YES]; - - // If we're pulling data from the cache without contacting the server at all, we won't have set originalURL yet - if ([self redirectCount] == 0) { - [theRequest setOriginalURL:[theRequest url]]; - } - - [theRequest updateProgressIndicators]; - [theRequest requestFinished]; - [theRequest markAsFinished]; - if ([self mainRequest]) { - [self markAsFinished]; - } -} - -- (BOOL)retryUsingNewConnection -{ - if ([self retryCount] == 0) { - - [self setWillRetryRequest:YES]; - [self cancelLoad]; - [self setWillRetryRequest:NO]; - - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Request attempted to use connection #%@, but it has been closed - will retry with a new connection", [[self connectionInfo] objectForKey:@"id"]); - #endif - [connectionsLock lock]; - [[self connectionInfo] removeObjectForKey:@"request"]; - [persistentConnectionsPool removeObject:[self connectionInfo]]; - [self setConnectionInfo:nil]; - [connectionsLock unlock]; - [self setRetryCount:[self retryCount]+1]; - [self startRequest]; - return YES; - } - #if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Request attempted to use connection #%@, but it has been closed - we have already retried with a new connection, so we must give up", [[self connectionInfo] objectForKey:@"id"]); - #endif - return NO; -} - -- (void)handleStreamError - -{ - NSError *underlyingError = [NSMakeCollectable(CFReadStreamCopyError((CFReadStreamRef)[self readStream])) autorelease]; - - if (![self error]) { // We may already have handled this error - - // First, check for a 'socket not connected', 'broken pipe' or 'connection lost' error - // This may occur when we've attempted to reuse a connection that should have been closed - // If we get this, we need to retry the request - // We'll only do this once - if it happens again on retry, we'll give up - // -1005 = kCFURLErrorNetworkConnectionLost - this doesn't seem to be declared on Mac OS 10.5 - if (([[underlyingError domain] isEqualToString:NSPOSIXErrorDomain] && ([underlyingError code] == ENOTCONN || [underlyingError code] == EPIPE)) - || ([[underlyingError domain] isEqualToString:(NSString *)kCFErrorDomainCFNetwork] && [underlyingError code] == -1005)) { - if ([self retryUsingNewConnection]) { - return; - } - } - - NSString *reason = @"A connection failure occurred"; - - // We'll use a custom error message for SSL errors, but you should always check underlying error if you want more details - // For some reason SecureTransport.h doesn't seem to be available on iphone, so error codes hard-coded - // Also, iPhone seems to handle errors differently from Mac OS X - a self-signed certificate returns a different error code on each platform, so we'll just provide a general error - if ([[underlyingError domain] isEqualToString:NSOSStatusErrorDomain]) { - if ([underlyingError code] <= -9800 && [underlyingError code] >= -9818) { - reason = [NSString stringWithFormat:@"%@: SSL problem (Possible causes may include a bad/expired/self-signed certificate, clock set to wrong date)",reason]; - } - } - [self cancelLoad]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIConnectionFailureErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:reason,NSLocalizedDescriptionKey,underlyingError,NSUnderlyingErrorKey,nil]]]; - } else { - [self cancelLoad]; - } - [self checkRequestStatus]; -} - -#pragma mark managing the read stream - -- (void)destroyReadStream -{ - if ([self readStream]) { - [self unscheduleReadStream]; - if (![self connectionCanBeReused]) { - [[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [[self readStream] close]; - } - [self setReadStream:nil]; - } -} - -- (void)scheduleReadStream -{ - if ([self readStream] && ![self readStreamIsScheduled]) { - - [connectionsLock lock]; - runningRequestCount++; - if (shouldUpdateNetworkActivityIndicator) { - [[self class] showNetworkActivityIndicator]; - } - [connectionsLock unlock]; - - // Reset the timeout - [self setLastActivityTime:[NSDate date]]; - CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL}; - CFReadStreamSetClient((CFReadStreamRef)[self readStream], kNetworkEvents, ReadStreamClientCallBack, &ctxt); - [[self readStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [self setReadStreamIsScheduled:YES]; - } -} - - -- (void)unscheduleReadStream -{ - if ([self readStream] && [self readStreamIsScheduled]) { - - [connectionsLock lock]; - runningRequestCount--; - if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) { - // This call will wait half a second before turning off the indicator - // This can prevent flicker when you have a single request finish and then immediately start another request - // We run this on the main thread because we have no guarantee this thread will have a runloop in 0.5 seconds time - // We don't bother the cancel this call if we start a new request, because we'll check if requests are running before we hide it - [[self class] performSelectorOnMainThread:@selector(hideNetworkActivityIndicatorAfterDelay) withObject:nil waitUntilDone:[NSThread isMainThread]]; - } - [connectionsLock unlock]; - - CFReadStreamSetClient((CFReadStreamRef)[self readStream], kCFStreamEventNone, NULL, NULL); - [[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [self setReadStreamIsScheduled:NO]; - } -} - -#pragma mark cleanup - -- (BOOL)removeTemporaryDownloadFile -{ - NSError *err = nil; - if ([self temporaryFileDownloadPath]) { - if (![[self class] removeFileAtPath:[self temporaryFileDownloadPath] error:&err]) { - [self failWithError:err]; - } - [self setTemporaryFileDownloadPath:nil]; - } - return (!err); -} - -- (BOOL)removeTemporaryUncompressedDownloadFile -{ - NSError *err = nil; - if ([self temporaryUncompressedDataDownloadPath]) { - if (![[self class] removeFileAtPath:[self temporaryUncompressedDataDownloadPath] error:&err]) { - [self failWithError:err]; - } - [self setTemporaryUncompressedDataDownloadPath:nil]; - } - return (!err); -} - -- (BOOL)removeTemporaryUploadFile -{ - NSError *err = nil; - if ([self postBodyFilePath]) { - if (![[self class] removeFileAtPath:[self postBodyFilePath] error:&err]) { - [self failWithError:err]; - } - [self setPostBodyFilePath:nil]; - } - return (!err); -} - -- (BOOL)removeTemporaryCompressedUploadFile -{ - NSError *err = nil; - if ([self compressedPostBodyFilePath]) { - if (![[self class] removeFileAtPath:[self compressedPostBodyFilePath] error:&err]) { - [self failWithError:err]; - } - [self setCompressedPostBodyFilePath:nil]; - } - return (!err); -} - -+ (BOOL)removeFileAtPath:(NSString *)path error:(NSError **)err -{ - NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; - - if ([fileManager fileExistsAtPath:path]) { - NSError *removeError = nil; - [fileManager removeItemAtPath:path error:&removeError]; - if (removeError) { - if (err) { - *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",path],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]; - } - return NO; - } - } - return YES; -} - -#pragma mark Proxies - -- (BOOL)configureProxies -{ - // Have details of the proxy been set on this request - if (![self isPACFileRequest] && (![self proxyHost] && ![self proxyPort])) { - - // If not, we need to figure out what they'll be - NSArray *proxies = nil; - - // Have we been given a proxy auto config file? - if ([self PACurl]) { - - // If yes, we'll need to fetch the PAC file asynchronously, so we stop this request to wait until we have the proxy details. - [self fetchPACFile]; - return NO; - - // Detect proxy settings and apply them - } else { - -#if TARGET_OS_IPHONE - NSDictionary *proxySettings = [NSMakeCollectable(CFNetworkCopySystemProxySettings()) autorelease]; -#else - NSDictionary *proxySettings = [NSMakeCollectable(SCDynamicStoreCopyProxies(NULL)) autorelease]; -#endif - - proxies = [NSMakeCollectable(CFNetworkCopyProxiesForURL((CFURLRef)[self url], (CFDictionaryRef)proxySettings)) autorelease]; - - // Now check to see if the proxy settings contained a PAC url, we need to run the script to get the real list of proxies if so - NSDictionary *settings = [proxies objectAtIndex:0]; - if ([settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]) { - [self setPACurl:[settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]]; - [self fetchPACFile]; - return NO; - } - } - - if (!proxies) { - [self setReadStream:nil]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to obtain information on proxy servers needed for request",NSLocalizedDescriptionKey,nil]]]; - return NO; - } - // I don't really understand why the dictionary returned by CFNetworkCopyProxiesForURL uses different key names from CFNetworkCopySystemProxySettings/SCDynamicStoreCopyProxies - // and why its key names are documented while those we actually need to use don't seem to be (passing the kCF* keys doesn't seem to work) - if ([proxies count] > 0) { - NSDictionary *settings = [proxies objectAtIndex:0]; - [self setProxyHost:[settings objectForKey:(NSString *)kCFProxyHostNameKey]]; - [self setProxyPort:[[settings objectForKey:(NSString *)kCFProxyPortNumberKey] intValue]]; - [self setProxyType:[settings objectForKey:(NSString *)kCFProxyTypeKey]]; - } - } - return YES; -} - - - -// Attempts to download a PAC (Proxy Auto-Configuration) file -// PAC files at file://, http:// and https:// addresses are supported -- (void)fetchPACFile -{ - // For file:// urls, we'll use an async NSInputStream (ASIHTTPRequest does not support file:// urls) - if ([[self PACurl] isFileURL]) { - NSInputStream *stream = [[[NSInputStream alloc] initWithFileAtPath:[[self PACurl] path]] autorelease]; - [self setPACFileReadStream:stream]; - [stream setDelegate:(id)self]; - [stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [stream open]; - // If it takes more than timeOutSeconds to read the PAC, we'll just give up and assume no proxies - // We won't bother to handle cases where the first part of the PAC is read within timeOutSeconds, but the whole thing takes longer - // Either our PAC file is in easy reach, or it's going to slow things down to the point that it's probably better requests fail - [self performSelector:@selector(timeOutPACRead) withObject:nil afterDelay:[self timeOutSeconds]]; - return; - } - - NSString *scheme = [[[self PACurl] scheme] lowercaseString]; - if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"]) { - // Don't know how to read data from this URL, we'll have to give up - // We'll simply assume no proxies, and start the request as normal - [self startRequest]; - return; - } - - // Create an ASIHTTPRequest to fetch the PAC file - ASIHTTPRequest *PACRequest = [ASIHTTPRequest requestWithURL:[self PACurl]]; - - // Will prevent this request attempting to configure proxy settings for itself - [PACRequest setIsPACFileRequest:YES]; - - [PACRequest setTimeOutSeconds:[self timeOutSeconds]]; - - // If we're a synchronous request, we'll download the PAC file synchronously - if ([self isSynchronous]) { - [PACRequest startSynchronous]; - if (![PACRequest error] && [PACRequest responseString]) { - [self runPACScript:[PACRequest responseString]]; - } - [self startRequest]; - return; - } - - [self setPACFileRequest:PACRequest]; - - // Force this request to run before others in the shared queue - [PACRequest setQueuePriority:NSOperationQueuePriorityHigh]; - - // We'll treat failure to download the PAC file the same as success - if we were unable to fetch a PAC file, we proceed as if we have no proxy server and let this request fail itself if necessary - [PACRequest setDelegate:self]; - [PACRequest setDidFinishSelector:@selector(finishedDownloadingPACFile:)]; - [PACRequest setDidFailSelector:@selector(finishedDownloadingPACFile:)]; - [PACRequest startAsynchronous]; - - // Temporarily increase the number of operations in the shared queue to give our request a chance to run - [connectionsLock lock]; - [sharedQueue setMaxConcurrentOperationCount:[sharedQueue maxConcurrentOperationCount]+1]; - [connectionsLock unlock]; -} - -// Called as we read the PAC file from a file:// url -- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode -{ - if (![self PACFileReadStream]) { - return; - } - if (eventCode == NSStreamEventHasBytesAvailable) { - - if (![self PACFileData]) { - [self setPACFileData:[NSMutableData data]]; - } - // If your PAC file is larger than 16KB, you're just being cruel. - uint8_t buf[16384]; - NSInteger len = [(NSInputStream *)stream read:buf maxLength:16384]; - // Append only if something was actually read. - if (len > 0) { - [[self PACFileData] appendBytes:(const void *)buf length:(NSUInteger)len]; - } - - } else if (eventCode == NSStreamEventErrorOccurred || eventCode == NSStreamEventEndEncountered) { - - [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeOutPACRead) object:nil]; - - [stream close]; - [stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [self setPACFileReadStream:nil]; - - if (eventCode == NSStreamEventEndEncountered) { - // It sounds as though we have no idea what encoding a PAC file will use - static NSStringEncoding encodingsToTry[2] = {NSUTF8StringEncoding,NSISOLatin1StringEncoding}; - NSUInteger i; - for (i=0; i<2; i++) { - NSString *pacScript = [[[NSString alloc] initWithBytes:[[self PACFileData] bytes] length:[[self PACFileData] length] encoding:encodingsToTry[i]] autorelease]; - if (pacScript) { - [self runPACScript:pacScript]; - break; - } - } - } - [self setPACFileData:nil]; - [self startRequest]; - } -} - -// Called if it takes longer than timeOutSeconds to read the whole PAC file (when reading from a file:// url) -- (void)timeOutPACRead -{ - [self stream:[self PACFileReadStream] handleEvent:NSStreamEventErrorOccurred]; -} - -// Runs the downloaded PAC script -- (void)runPACScript:(NSString *)script -{ - if (script) { - // From: http://developer.apple.com/samplecode/CFProxySupportTool/listing1.html - // Work around . This dummy call to - // CFNetworkCopyProxiesForURL initialise some state within CFNetwork - // that is required by CFNetworkCopyProxiesForAutoConfigurationScript. - CFDictionaryRef proxySettings = CFNetworkCopySystemProxySettings(); - CFRelease(CFNetworkCopyProxiesForURL((CFURLRef)[self url], proxySettings)); - CFRelease(proxySettings); - - // Obtain the list of proxies by running the autoconfiguration script - CFErrorRef err = NULL; - NSArray *proxies = [NSMakeCollectable(CFNetworkCopyProxiesForAutoConfigurationScript((CFStringRef)script,(CFURLRef)[self url], &err)) autorelease]; - if (!err && [proxies count] > 0) { - NSDictionary *settings = [proxies objectAtIndex:0]; - [self setProxyHost:[settings objectForKey:(NSString *)kCFProxyHostNameKey]]; - [self setProxyPort:[[settings objectForKey:(NSString *)kCFProxyPortNumberKey] intValue]]; - [self setProxyType:[settings objectForKey:(NSString *)kCFProxyTypeKey]]; - } - } -} - -// Called if we successfully downloaded a PAC file from a webserver -- (void)finishedDownloadingPACFile:(ASIHTTPRequest *)theRequest -{ - if (![theRequest error] && [theRequest responseString]) { - [self runPACScript:[theRequest responseString]]; - } - - // Set the shared queue's maxConcurrentOperationCount back to normal - [connectionsLock lock]; - [sharedQueue setMaxConcurrentOperationCount:[sharedQueue maxConcurrentOperationCount]-1]; - [connectionsLock unlock]; - - // We no longer need our PAC file request - [self setPACFileRequest:nil]; - - // Start the request - [self startRequest]; -} - - -#pragma mark persistent connections - -- (NSNumber *)connectionID -{ - return [[self connectionInfo] objectForKey:@"id"]; -} - -+ (void)expirePersistentConnections -{ - [connectionsLock lock]; - NSUInteger i; - for (i=0; i<[persistentConnectionsPool count]; i++) { - NSDictionary *existingConnection = [persistentConnectionsPool objectAtIndex:i]; - if (![existingConnection objectForKey:@"request"] && [[existingConnection objectForKey:@"expires"] timeIntervalSinceNow] <= 0) { -#if DEBUG_PERSISTENT_CONNECTIONS - ASI_DEBUG_LOG(@"[CONNECTION] Closing connection #%i because it has expired",[[existingConnection objectForKey:@"id"] intValue]); -#endif - NSInputStream *stream = [existingConnection objectForKey:@"stream"]; - if (stream) { - [stream close]; - } - [persistentConnectionsPool removeObject:existingConnection]; - i--; - } - } - [connectionsLock unlock]; -} - -#pragma mark NSCopying -- (id)copyWithZone:(NSZone *)zone -{ - // Don't forget - this will return a retained copy! - ASIHTTPRequest *newRequest = [[[self class] alloc] initWithURL:[self url]]; - [newRequest setDelegate:[self delegate]]; - [newRequest setRequestMethod:[self requestMethod]]; - [newRequest setPostBody:[self postBody]]; - [newRequest setShouldStreamPostDataFromDisk:[self shouldStreamPostDataFromDisk]]; - [newRequest setPostBodyFilePath:[self postBodyFilePath]]; - [newRequest setRequestHeaders:[[[self requestHeaders] mutableCopyWithZone:zone] autorelease]]; - [newRequest setRequestCookies:[[[self requestCookies] mutableCopyWithZone:zone] autorelease]]; - [newRequest setUseCookiePersistence:[self useCookiePersistence]]; - [newRequest setUseKeychainPersistence:[self useKeychainPersistence]]; - [newRequest setUseSessionPersistence:[self useSessionPersistence]]; - [newRequest setAllowCompressedResponse:[self allowCompressedResponse]]; - [newRequest setDownloadDestinationPath:[self downloadDestinationPath]]; - [newRequest setTemporaryFileDownloadPath:[self temporaryFileDownloadPath]]; - [newRequest setUsername:[self username]]; - [newRequest setPassword:[self password]]; - [newRequest setDomain:[self domain]]; - [newRequest setProxyUsername:[self proxyUsername]]; - [newRequest setProxyPassword:[self proxyPassword]]; - [newRequest setProxyDomain:[self proxyDomain]]; - [newRequest setProxyHost:[self proxyHost]]; - [newRequest setProxyPort:[self proxyPort]]; - [newRequest setProxyType:[self proxyType]]; - [newRequest setUploadProgressDelegate:[self uploadProgressDelegate]]; - [newRequest setDownloadProgressDelegate:[self downloadProgressDelegate]]; - [newRequest setShouldPresentAuthenticationDialog:[self shouldPresentAuthenticationDialog]]; - [newRequest setShouldPresentProxyAuthenticationDialog:[self shouldPresentProxyAuthenticationDialog]]; - [newRequest setPostLength:[self postLength]]; - [newRequest setHaveBuiltPostBody:[self haveBuiltPostBody]]; - [newRequest setDidStartSelector:[self didStartSelector]]; - [newRequest setDidFinishSelector:[self didFinishSelector]]; - [newRequest setDidFailSelector:[self didFailSelector]]; - [newRequest setTimeOutSeconds:[self timeOutSeconds]]; - [newRequest setShouldResetDownloadProgress:[self shouldResetDownloadProgress]]; - [newRequest setShouldResetUploadProgress:[self shouldResetUploadProgress]]; - [newRequest setShowAccurateProgress:[self showAccurateProgress]]; - [newRequest setDefaultResponseEncoding:[self defaultResponseEncoding]]; - [newRequest setAllowResumeForFileDownloads:[self allowResumeForFileDownloads]]; - [newRequest setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]]; - [newRequest setTag:[self tag]]; - [newRequest setUseHTTPVersionOne:[self useHTTPVersionOne]]; - [newRequest setShouldRedirect:[self shouldRedirect]]; - [newRequest setValidatesSecureCertificate:[self validatesSecureCertificate]]; - [newRequest setClientCertificateIdentity:clientCertificateIdentity]; - [newRequest setClientCertificates:[[clientCertificates copy] autorelease]]; - [newRequest setPACurl:[self PACurl]]; - [newRequest setShouldPresentCredentialsBeforeChallenge:[self shouldPresentCredentialsBeforeChallenge]]; - [newRequest setNumberOfTimesToRetryOnTimeout:[self numberOfTimesToRetryOnTimeout]]; - [newRequest setShouldUseRFC2616RedirectBehaviour:[self shouldUseRFC2616RedirectBehaviour]]; - [newRequest setShouldAttemptPersistentConnection:[self shouldAttemptPersistentConnection]]; - [newRequest setPersistentConnectionTimeoutSeconds:[self persistentConnectionTimeoutSeconds]]; - [newRequest setAuthenticationScheme:[self authenticationScheme]]; - return newRequest; -} - -#pragma mark default time out - -+ (NSTimeInterval)defaultTimeOutSeconds -{ - return defaultTimeOutSeconds; -} - -+ (void)setDefaultTimeOutSeconds:(NSTimeInterval)newTimeOutSeconds -{ - defaultTimeOutSeconds = newTimeOutSeconds; -} - - -#pragma mark client certificate - -- (void)setClientCertificateIdentity:(SecIdentityRef)anIdentity { - if(clientCertificateIdentity) { - CFRelease(clientCertificateIdentity); - } - - clientCertificateIdentity = anIdentity; - - if (clientCertificateIdentity) { - CFRetain(clientCertificateIdentity); - } -} - - -#pragma mark session credentials - -+ (NSMutableArray *)sessionProxyCredentialsStore -{ - [sessionCredentialsLock lock]; - if (!sessionProxyCredentialsStore) { - sessionProxyCredentialsStore = [[NSMutableArray alloc] init]; - } - [sessionCredentialsLock unlock]; - return sessionProxyCredentialsStore; -} - -+ (NSMutableArray *)sessionCredentialsStore -{ - [sessionCredentialsLock lock]; - if (!sessionCredentialsStore) { - sessionCredentialsStore = [[NSMutableArray alloc] init]; - } - [sessionCredentialsLock unlock]; - return sessionCredentialsStore; -} - -+ (void)storeProxyAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - [self removeProxyAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - [[[self class] sessionProxyCredentialsStore] addObject:credentials]; - [sessionCredentialsLock unlock]; -} - -+ (void)storeAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - [self removeAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - [[[self class] sessionCredentialsStore] addObject:credentials]; - [sessionCredentialsLock unlock]; -} - -+ (void)removeProxyAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionProxyCredentialsStore]; - NSUInteger i; - for (i=0; i<[sessionCredentialsList count]; i++) { - NSDictionary *theCredentials = [sessionCredentialsList objectAtIndex:i]; - if ([theCredentials objectForKey:@"Credentials"] == credentials) { - [sessionCredentialsList removeObjectAtIndex:i]; - [sessionCredentialsLock unlock]; - return; - } - } - [sessionCredentialsLock unlock]; -} - -+ (void)removeAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionCredentialsStore]; - NSUInteger i; - for (i=0; i<[sessionCredentialsList count]; i++) { - NSDictionary *theCredentials = [sessionCredentialsList objectAtIndex:i]; - if ([theCredentials objectForKey:@"Credentials"] == credentials) { - [sessionCredentialsList removeObjectAtIndex:i]; - [sessionCredentialsLock unlock]; - return; - } - } - [sessionCredentialsLock unlock]; -} - -- (NSDictionary *)findSessionProxyAuthenticationCredentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionProxyCredentialsStore]; - for (NSDictionary *theCredentials in sessionCredentialsList) { - if ([[theCredentials objectForKey:@"Host"] isEqualToString:[self proxyHost]] && [[theCredentials objectForKey:@"Port"] intValue] == [self proxyPort]) { - [sessionCredentialsLock unlock]; - return theCredentials; - } - } - [sessionCredentialsLock unlock]; - return nil; -} - - -- (NSDictionary *)findSessionAuthenticationCredentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionCredentialsStore]; - NSURL *requestURL = [self url]; - - BOOL haveFoundExactMatch; - NSDictionary *closeMatch = nil; - - // Loop through all the cached credentials we have, looking for the best match for this request - for (NSDictionary *theCredentials in sessionCredentialsList) { - - haveFoundExactMatch = NO; - NSURL *cachedCredentialsURL = [theCredentials objectForKey:@"URL"]; - - // Find an exact match (same url) - if ([cachedCredentialsURL isEqual:[self url]]) { - haveFoundExactMatch = YES; - - // This is not an exact match for the url, and we already have a close match we can use - } else if (closeMatch) { - continue; - - // Find a close match (same host, scheme and port) - } else if ([[cachedCredentialsURL host] isEqualToString:[requestURL host]] && ([cachedCredentialsURL port] == [requestURL port] || ([requestURL port] && [[cachedCredentialsURL port] isEqualToNumber:[requestURL port]])) && [[cachedCredentialsURL scheme] isEqualToString:[requestURL scheme]]) { - } else { - continue; - } - - // Just a sanity check to ensure we never choose credentials from a different realm. Can't really do more than that, as either this request or the stored credentials may not have a realm when the other does - if ([self authenticationRealm] && ([theCredentials objectForKey:@"AuthenticationRealm"] && ![[theCredentials objectForKey:@"AuthenticationRealm"] isEqualToString:[self authenticationRealm]])) { - continue; - } - - // If we have a username and password set on the request, check that they are the same as the cached ones - if ([self username] && [self password]) { - NSDictionary *usernameAndPassword = [theCredentials objectForKey:@"Credentials"]; - NSString *storedUsername = [usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationUsername]; - NSString *storedPassword = [usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationPassword]; - if (![storedUsername isEqualToString:[self username]] || ![storedPassword isEqualToString:[self password]]) { - continue; - } - } - - // If we have an exact match for the url, use those credentials - if (haveFoundExactMatch) { - [sessionCredentialsLock unlock]; - return theCredentials; - } - - // We have no exact match, let's remember that we have a good match for this server, and we'll use it at the end if we don't find an exact match - closeMatch = theCredentials; - } - [sessionCredentialsLock unlock]; - - // Return credentials that matched on host, port and scheme, or nil if we didn't find any - return closeMatch; -} - -#pragma mark keychain storage - -+ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; -} - -+ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; -} - -+ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; -} - -+ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; -} - -+ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; - if (credential) { - [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace]; - } -} - -+ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; - if (credential) { - [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace]; - } -} - -+ (NSMutableArray *)sessionCookies -{ - [sessionCookiesLock lock]; - if (!sessionCookies) { - [ASIHTTPRequest setSessionCookies:[NSMutableArray array]]; - } - NSMutableArray *cookies = [[sessionCookies retain] autorelease]; - [sessionCookiesLock unlock]; - return cookies; -} - -+ (void)setSessionCookies:(NSMutableArray *)newSessionCookies -{ - [sessionCookiesLock lock]; - // Remove existing cookies from the persistent store - for (NSHTTPCookie *cookie in sessionCookies) { - [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; - } - [sessionCookies release]; - sessionCookies = [newSessionCookies retain]; - [sessionCookiesLock unlock]; -} - -+ (void)addSessionCookie:(NSHTTPCookie *)newCookie -{ - [sessionCookiesLock lock]; - NSHTTPCookie *cookie; - NSUInteger i; - NSUInteger max = [[ASIHTTPRequest sessionCookies] count]; - for (i=0; i 0) { - if ([self readStreamIsScheduled]) { - [self unscheduleReadStream]; - #if DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[THROTTLING] Sleeping request %@ until after %@",self,throttleWakeUpTime); - #endif - } - } else { - if (![self readStreamIsScheduled]) { - [self scheduleReadStream]; - #if DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[THROTTLING] Waking up request %@",self); - #endif - } - } - } - [bandwidthThrottlingLock unlock]; - - // Bandwidth throttling must have been turned off since we last looked, let's re-schedule the stream - } else if (![self readStreamIsScheduled]) { - [self scheduleReadStream]; - } -} - -+ (BOOL)isBandwidthThrottled -{ -#if TARGET_OS_IPHONE - [bandwidthThrottlingLock lock]; - - BOOL throttle = isBandwidthThrottled || (!shouldThrottleBandwidthForWWANOnly && (maxBandwidthPerSecond > 0)); - [bandwidthThrottlingLock unlock]; - return throttle; -#else - [bandwidthThrottlingLock lock]; - BOOL throttle = (maxBandwidthPerSecond > 0); - [bandwidthThrottlingLock unlock]; - return throttle; -#endif -} - -+ (unsigned long)maxBandwidthPerSecond -{ - [bandwidthThrottlingLock lock]; - unsigned long amount = maxBandwidthPerSecond; - [bandwidthThrottlingLock unlock]; - return amount; -} - -+ (void)setMaxBandwidthPerSecond:(unsigned long)bytes -{ - [bandwidthThrottlingLock lock]; - maxBandwidthPerSecond = bytes; - [bandwidthThrottlingLock unlock]; -} - -+ (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes -{ - [bandwidthThrottlingLock lock]; - bandwidthUsedInLastSecond += bytes; - [bandwidthThrottlingLock unlock]; -} - -+ (void)recordBandwidthUsage -{ - if (bandwidthUsedInLastSecond == 0) { - [bandwidthUsageTracker removeAllObjects]; - } else { - NSTimeInterval interval = [bandwidthMeasurementDate timeIntervalSinceNow]; - while ((interval < 0 || [bandwidthUsageTracker count] > 5) && [bandwidthUsageTracker count] > 0) { - [bandwidthUsageTracker removeObjectAtIndex:0]; - interval++; - } - } - #if DEBUG_THROTTLING - ASI_DEBUG_LOG(@"[THROTTLING] ===Used: %u bytes of bandwidth in last measurement period===",bandwidthUsedInLastSecond); - #endif - [bandwidthUsageTracker addObject:[NSNumber numberWithUnsignedLong:bandwidthUsedInLastSecond]]; - [bandwidthMeasurementDate release]; - bandwidthMeasurementDate = [[NSDate dateWithTimeIntervalSinceNow:1] retain]; - bandwidthUsedInLastSecond = 0; - - NSUInteger measurements = [bandwidthUsageTracker count]; - unsigned long totalBytes = 0; - for (NSNumber *bytes in bandwidthUsageTracker) { - totalBytes += [bytes unsignedLongValue]; - } - if (measurements > 0) - averageBandwidthUsedPerSecond = totalBytes/measurements; -} - -+ (unsigned long)averageBandwidthUsedPerSecond -{ - [bandwidthThrottlingLock lock]; - unsigned long amount = averageBandwidthUsedPerSecond; - [bandwidthThrottlingLock unlock]; - return amount; -} - -+ (void)measureBandwidthUsage -{ - // Other requests may have to wait for this lock if we're sleeping, but this is fine, since in that case we already know they shouldn't be sending or receiving data - [bandwidthThrottlingLock lock]; - - if (!bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < -0) { - [ASIHTTPRequest recordBandwidthUsage]; - } - - // Are we performing bandwidth throttling? - if ( - #if TARGET_OS_IPHONE - isBandwidthThrottled || (!shouldThrottleBandwidthForWWANOnly && (maxBandwidthPerSecond)) - #else - maxBandwidthPerSecond - #endif - ) { - // How much data can we still send or receive this second? - long long bytesRemaining = (long long)maxBandwidthPerSecond - (long long)bandwidthUsedInLastSecond; - - // Have we used up our allowance? - if (bytesRemaining < 0) { - - // Yes, put this request to sleep until a second is up, with extra added punishment sleeping time for being very naughty (we have used more bandwidth than we were allowed) - double extraSleepyTime = (-bytesRemaining/(maxBandwidthPerSecond*1.0)); - [throttleWakeUpTime release]; - throttleWakeUpTime = [[NSDate alloc] initWithTimeInterval:extraSleepyTime sinceDate:bandwidthMeasurementDate]; - } - } - [bandwidthThrottlingLock unlock]; -} - -+ (unsigned long)maxUploadReadLength -{ - [bandwidthThrottlingLock lock]; - - // We'll split our bandwidth allowance into 4 (which is the default for an ASINetworkQueue's max concurrent operations count) to give all running requests a fighting chance of reading data this cycle - long long toRead = maxBandwidthPerSecond/4; - if (maxBandwidthPerSecond > 0 && (bandwidthUsedInLastSecond + toRead > maxBandwidthPerSecond)) { - toRead = (long long)maxBandwidthPerSecond-(long long)bandwidthUsedInLastSecond; - if (toRead < 0) { - toRead = 0; - } - } - - if (toRead == 0 || !bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < -0) { - [throttleWakeUpTime release]; - throttleWakeUpTime = [bandwidthMeasurementDate retain]; - } - [bandwidthThrottlingLock unlock]; - return (unsigned long)toRead; -} - - -#if TARGET_OS_IPHONE -+ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle -{ - if (throttle) { - [ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount]; - } else { - [ASIHTTPRequest unsubscribeFromNetworkReachabilityNotifications]; - [ASIHTTPRequest setMaxBandwidthPerSecond:0]; - [bandwidthThrottlingLock lock]; - isBandwidthThrottled = NO; - shouldThrottleBandwidthForWWANOnly = NO; - [bandwidthThrottlingLock unlock]; - } -} - -+ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit -{ - [bandwidthThrottlingLock lock]; - shouldThrottleBandwidthForWWANOnly = YES; - maxBandwidthPerSecond = limit; - [ASIHTTPRequest registerForNetworkReachabilityNotifications]; - [bandwidthThrottlingLock unlock]; - [ASIHTTPRequest reachabilityChanged:nil]; -} - -#pragma mark reachability - -+ (void)registerForNetworkReachabilityNotifications -{ - [[Reachability reachabilityForInternetConnection] startNotifier]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; -} - - -+ (void)unsubscribeFromNetworkReachabilityNotifications -{ - [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil]; -} - -+ (BOOL)isNetworkReachableViaWWAN -{ - return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN); -} - -+ (void)reachabilityChanged:(NSNotification *)note -{ - [bandwidthThrottlingLock lock]; - isBandwidthThrottled = [ASIHTTPRequest isNetworkReachableViaWWAN]; - [bandwidthThrottlingLock unlock]; -} -#endif - -#pragma mark queue - -// Returns the shared queue -+ (NSOperationQueue *)sharedQueue -{ - return [[sharedQueue retain] autorelease]; -} - -#pragma mark cache - -+ (void)setDefaultCache:(id )cache -{ - @synchronized (self) { - [cache retain]; - [defaultCache release]; - defaultCache = cache; - } -} - -+ (id )defaultCache -{ - @synchronized(self) { - return [[defaultCache retain] autorelease]; - } - return nil; -} - - -#pragma mark network activity - -+ (BOOL)isNetworkInUse -{ - [connectionsLock lock]; - BOOL inUse = (runningRequestCount > 0); - [connectionsLock unlock]; - return inUse; -} - -+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate -{ - [connectionsLock lock]; - shouldUpdateNetworkActivityIndicator = shouldUpdate; - [connectionsLock unlock]; -} - -+ (void)showNetworkActivityIndicator -{ -#if TARGET_OS_IPHONE - [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; -#endif -} - -+ (void)hideNetworkActivityIndicator -{ -#if TARGET_OS_IPHONE - [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; -#endif -} - - -/* Always called on main thread */ -+ (void)hideNetworkActivityIndicatorAfterDelay -{ - [self performSelector:@selector(hideNetworkActivityIndicatorIfNeeeded) withObject:nil afterDelay:0.5]; -} - -+ (void)hideNetworkActivityIndicatorIfNeeeded -{ - [connectionsLock lock]; - if (runningRequestCount == 0) { - [self hideNetworkActivityIndicator]; - } - [connectionsLock unlock]; -} - - -#pragma mark threading behaviour - -// In the default implementation, all requests run in a single background thread -// Advanced users only: Override this method in a subclass for a different threading behaviour -// Eg: return [NSThread mainThread] to run all requests in the main thread -// Alternatively, you can create a thread on demand, or manage a pool of threads -// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun()) -// Requests will stop the runloop when they complete -// If you have multiple requests sharing the thread or you want to re-use the thread, you'll need to restart the runloop -+ (NSThread *)threadForRequest:(ASIHTTPRequest *)request -{ - if (networkThread == nil) { - @synchronized(self) { - if (networkThread == nil) { - networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(runRequests) object:nil]; - [networkThread start]; - } - } - } - return networkThread; -} - -+ (void)runRequests -{ - // Should keep the runloop from exiting - CFRunLoopSourceContext context = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; - CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context); - CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); - - BOOL runAlways = YES; // Introduced to cheat Static Analyzer - while (runAlways) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, true); - [pool drain]; - } - - // Should never be called, but anyway - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); - CFRelease(source); -} - -#pragma mark miscellany - -#if TARGET_OS_IPHONE -+ (BOOL)isMultitaskingSupported -{ - BOOL multiTaskingSupported = NO; - if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { - multiTaskingSupported = [(id)[UIDevice currentDevice] isMultitaskingSupported]; - } - return multiTaskingSupported; -} -#endif - -// From: http://www.cocoadev.com/index.pl?BaseSixtyFour - -+ (NSString*)base64forData:(NSData*)theData { - - const uint8_t* input = (const uint8_t*)[theData bytes]; - NSUInteger length = [theData length]; - - static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - - NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; - uint8_t* output = (uint8_t*)data.mutableBytes; - - NSUInteger i,i2; - for (i=0; i < length; i += 3) { - NSInteger value = 0; - for (i2=0; i2<3; i2++) { - value <<= 8; - if (i+i2 < length) { - value |= (0xFF & input[i+i2]); - } - } - - NSInteger theIndex = (i / 3) * 4; - output[theIndex + 0] = (uint8_t)table[(value >> 18) & 0x3F]; - output[theIndex + 1] = (uint8_t)table[(value >> 12) & 0x3F]; - output[theIndex + 2] = (i + 1) < length ? (uint8_t)table[(value >> 6) & 0x3F] : '='; - output[theIndex + 3] = (i + 2) < length ? (uint8_t)table[(value >> 0) & 0x3F] : '='; - } - - return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease]; -} - -+ (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge -{ - NSDictionary *responseHeaders = [request responseHeaders]; - - // If we weren't given a custom max-age, lets look for one in the response headers - if (!maxAge) { - NSString *cacheControl = [[responseHeaders objectForKey:@"Cache-Control"] lowercaseString]; - if (cacheControl) { - NSScanner *scanner = [NSScanner scannerWithString:cacheControl]; - [scanner scanUpToString:@"max-age" intoString:NULL]; - if ([scanner scanString:@"max-age" intoString:NULL]) { - [scanner scanString:@"=" intoString:NULL]; - [scanner scanDouble:&maxAge]; - } - } - } - - // RFC 2612 says max-age must override any Expires header - if (maxAge) { - NSDate *date = [NSDate date]; - if ([date respondsToSelector:@selector(dateByAddingTimeInterval:)]) { - return [date dateByAddingTimeInterval:maxAge]; - } else { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - return [date addTimeInterval:maxAge]; -#pragma clang diagnostic pop - } - } else { - NSString *expires = [responseHeaders objectForKey:@"Expires"]; - if (expires) { - return [ASIHTTPRequest dateFromRFC1123String:expires]; - } - } - return nil; -} - -// Based on hints from http://stackoverflow.com/questions/1850824/parsing-a-rfc-822-date-with-nsdateformatter -+ (NSDate *)dateFromRFC1123String:(NSString *)string -{ - NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; - [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; - // Does the string include a week day? - NSString *day = @""; - if ([string rangeOfString:@","].location != NSNotFound) { - day = @"EEE, "; - } - // Does the string include seconds? - NSString *seconds = @""; - if ([[string componentsSeparatedByString:@":"] count] == 3) { - seconds = @":ss"; - } - [formatter setDateFormat:[NSString stringWithFormat:@"%@dd MMM yyyy HH:mm%@ z",day,seconds]]; - return [formatter dateFromString:string]; -} - -+ (void)parseMimeType:(NSString **)mimeType andResponseEncoding:(NSStringEncoding *)stringEncoding fromContentType:(NSString *)contentType -{ - if (!contentType) { - return; - } - NSScanner *charsetScanner = [NSScanner scannerWithString: contentType]; - if (![charsetScanner scanUpToString:@";" intoString:mimeType] || [charsetScanner scanLocation] == [contentType length]) { - *mimeType = [contentType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - return; - } - *mimeType = [*mimeType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - NSString *charsetSeparator = @"charset="; - NSString *IANAEncoding = nil; - - if ([charsetScanner scanUpToString: charsetSeparator intoString: NULL] && [charsetScanner scanLocation] < [contentType length]) { - [charsetScanner setScanLocation: [charsetScanner scanLocation] + [charsetSeparator length]]; - [charsetScanner scanUpToString: @";" intoString: &IANAEncoding]; - } - - if (IANAEncoding) { - CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)IANAEncoding); - if (cfEncoding != kCFStringEncodingInvalidId) { - *stringEncoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding); - } - } -} - -#pragma mark - -#pragma mark blocks -#if NS_BLOCKS_AVAILABLE -- (void)setStartedBlock:(ASIBasicBlock)aStartedBlock -{ - [startedBlock release]; - startedBlock = [aStartedBlock copy]; -} - -- (void)setHeadersReceivedBlock:(ASIHeadersBlock)aReceivedBlock -{ - [headersReceivedBlock release]; - headersReceivedBlock = [aReceivedBlock copy]; -} - -- (void)setCompletionBlock:(ASIBasicBlock)aCompletionBlock -{ - [completionBlock release]; - completionBlock = [aCompletionBlock copy]; -} - -- (void)setFailedBlock:(ASIBasicBlock)aFailedBlock -{ - [failureBlock release]; - failureBlock = [aFailedBlock copy]; -} - -- (void)setBytesReceivedBlock:(ASIProgressBlock)aBytesReceivedBlock -{ - [bytesReceivedBlock release]; - bytesReceivedBlock = [aBytesReceivedBlock copy]; -} - -- (void)setBytesSentBlock:(ASIProgressBlock)aBytesSentBlock -{ - [bytesSentBlock release]; - bytesSentBlock = [aBytesSentBlock copy]; -} - -- (void)setDownloadSizeIncrementedBlock:(ASISizeBlock)aDownloadSizeIncrementedBlock{ - [downloadSizeIncrementedBlock release]; - downloadSizeIncrementedBlock = [aDownloadSizeIncrementedBlock copy]; -} - -- (void)setUploadSizeIncrementedBlock:(ASISizeBlock)anUploadSizeIncrementedBlock -{ - [uploadSizeIncrementedBlock release]; - uploadSizeIncrementedBlock = [anUploadSizeIncrementedBlock copy]; -} - -- (void)setDataReceivedBlock:(ASIDataBlock)aReceivedBlock -{ - [dataReceivedBlock release]; - dataReceivedBlock = [aReceivedBlock copy]; -} - -- (void)setAuthenticationNeededBlock:(ASIBasicBlock)anAuthenticationBlock -{ - [authenticationNeededBlock release]; - authenticationNeededBlock = [anAuthenticationBlock copy]; -} -- (void)setProxyAuthenticationNeededBlock:(ASIBasicBlock)aProxyAuthenticationBlock -{ - [proxyAuthenticationNeededBlock release]; - proxyAuthenticationNeededBlock = [aProxyAuthenticationBlock copy]; -} -- (void)setRequestRedirectedBlock:(ASIBasicBlock)aRedirectBlock -{ - [requestRedirectedBlock release]; - requestRedirectedBlock = [aRedirectBlock copy]; -} -#endif - -#pragma mark === - -@synthesize username; -@synthesize password; -@synthesize userAgentString; -@synthesize domain; -@synthesize proxyUsername; -@synthesize proxyPassword; -@synthesize proxyDomain; -@synthesize url; -@synthesize originalURL; -@synthesize delegate; -@synthesize queue; -@synthesize uploadProgressDelegate; -@synthesize downloadProgressDelegate; -@synthesize useKeychainPersistence; -@synthesize useSessionPersistence; -@synthesize useCookiePersistence; -@synthesize downloadDestinationPath; -@synthesize temporaryFileDownloadPath; -@synthesize temporaryUncompressedDataDownloadPath; -@synthesize didStartSelector; -@synthesize didReceiveResponseHeadersSelector; -@synthesize willRedirectSelector; -@synthesize didFinishSelector; -@synthesize didFailSelector; -@synthesize didReceiveDataSelector; -@synthesize authenticationRealm; -@synthesize proxyAuthenticationRealm; -@synthesize error; -@synthesize complete; -@synthesize requestHeaders; -@synthesize responseHeaders; -@synthesize responseCookies; -@synthesize requestCookies; -@synthesize requestCredentials; -@synthesize responseStatusCode; -@synthesize rawResponseData; -@synthesize lastActivityTime; -@synthesize timeOutSeconds; -@synthesize requestMethod; -@synthesize postBody; -@synthesize compressedPostBody; -@synthesize contentLength; -@synthesize partialDownloadSize; -@synthesize postLength; -@synthesize shouldResetDownloadProgress; -@synthesize shouldResetUploadProgress; -@synthesize mainRequest; -@synthesize totalBytesRead; -@synthesize totalBytesSent; -@synthesize showAccurateProgress; -@synthesize uploadBufferSize; -@synthesize defaultResponseEncoding; -@synthesize responseEncoding; -@synthesize allowCompressedResponse; -@synthesize allowResumeForFileDownloads; -@synthesize userInfo; -@synthesize tag; -@synthesize postBodyFilePath; -@synthesize compressedPostBodyFilePath; -@synthesize postBodyWriteStream; -@synthesize postBodyReadStream; -@synthesize shouldStreamPostDataFromDisk; -@synthesize didCreateTemporaryPostDataFile; -@synthesize useHTTPVersionOne; -@synthesize lastBytesRead; -@synthesize lastBytesSent; -@synthesize cancelledLock; -@synthesize haveBuiltPostBody; -@synthesize fileDownloadOutputStream; -@synthesize inflatedFileDownloadOutputStream; -@synthesize authenticationRetryCount; -@synthesize proxyAuthenticationRetryCount; -@synthesize updatedProgress; -@synthesize shouldRedirect; -@synthesize validatesSecureCertificate; -@synthesize needsRedirect; -@synthesize redirectCount; -@synthesize shouldCompressRequestBody; -@synthesize proxyCredentials; -@synthesize proxyHost; -@synthesize proxyPort; -@synthesize proxyType; -@synthesize PACurl; -@synthesize authenticationScheme; -@synthesize proxyAuthenticationScheme; -@synthesize shouldPresentAuthenticationDialog; -@synthesize shouldPresentProxyAuthenticationDialog; -@synthesize authenticationNeeded; -@synthesize responseStatusMessage; -@synthesize shouldPresentCredentialsBeforeChallenge; -@synthesize haveBuiltRequestHeaders; -@synthesize inProgress; -@synthesize numberOfTimesToRetryOnTimeout; -@synthesize retryCount; -@synthesize willRetryRequest; -@synthesize shouldAttemptPersistentConnection; -@synthesize persistentConnectionTimeoutSeconds; -@synthesize connectionCanBeReused; -@synthesize connectionInfo; -@synthesize readStream; -@synthesize readStreamIsScheduled; -@synthesize shouldUseRFC2616RedirectBehaviour; -@synthesize downloadComplete; -@synthesize requestID; -@synthesize runLoopMode; -@synthesize statusTimer; -@synthesize downloadCache; -@synthesize cachePolicy; -@synthesize cacheStoragePolicy; -@synthesize didUseCachedResponse; -@synthesize secondsToCache; -@synthesize clientCertificates; -@synthesize redirectURL; -#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 -@synthesize shouldContinueWhenAppEntersBackground; -#endif -@synthesize dataDecompressor; -@synthesize shouldWaitToInflateCompressedResponses; - -@synthesize isPACFileRequest; -@synthesize PACFileRequest; -@synthesize PACFileReadStream; -@synthesize PACFileData; - -@synthesize isSynchronous; -@end diff --git a/clients/ios/ASI/ASIHTTPRequestConfig.h b/clients/ios/ASI/ASIHTTPRequestConfig.h deleted file mode 100755 index 3f6c58704..000000000 --- a/clients/ios/ASI/ASIHTTPRequestConfig.h +++ /dev/null @@ -1,43 +0,0 @@ -// -// ASIHTTPRequestConfig.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 14/12/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - - -// ====== -// Debug output configuration options -// ====== - -// If defined will use the specified function for debug logging -// Otherwise use NSLog -#ifndef ASI_DEBUG_LOG - #define ASI_DEBUG_LOG NSLog -#endif - -// When set to 1 ASIHTTPRequests will print information about what a request is doing -#ifndef DEBUG_REQUEST_STATUS - #define DEBUG_REQUEST_STATUS 0 -#endif - -// When set to 1, ASIFormDataRequests will print information about the request body to the console -#ifndef DEBUG_FORM_DATA_REQUEST - #define DEBUG_FORM_DATA_REQUEST 0 -#endif - -// When set to 1, ASIHTTPRequests will print information about bandwidth throttling to the console -#ifndef DEBUG_THROTTLING - #define DEBUG_THROTTLING 0 -#endif - -// When set to 1, ASIHTTPRequests will print information about persistent connections to the console -#ifndef DEBUG_PERSISTENT_CONNECTIONS - #define DEBUG_PERSISTENT_CONNECTIONS 0 -#endif - -// When set to 1, ASIHTTPRequests will print information about HTTP authentication (Basic, Digest or NTLM) to the console -#ifndef DEBUG_HTTP_AUTHENTICATION - #define DEBUG_HTTP_AUTHENTICATION 0 -#endif diff --git a/clients/ios/ASI/ASIHTTPRequestDelegate.h b/clients/ios/ASI/ASIHTTPRequestDelegate.h deleted file mode 100755 index c495a275c..000000000 --- a/clients/ios/ASI/ASIHTTPRequestDelegate.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// ASIHTTPRequestDelegate.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 13/04/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -@class ASIHTTPRequest; - -@protocol ASIHTTPRequestDelegate - -@optional - -// These are the default delegate methods for request status -// You can use different ones by setting didStartSelector / didFinishSelector / didFailSelector -- (void)requestStarted:(ASIHTTPRequest *)request; -- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders; -- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL; -- (void)requestFinished:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; -- (void)requestRedirected:(ASIHTTPRequest *)request; - -// When a delegate implements this method, it is expected to process all incoming data itself -// This means that responseData / responseString / downloadDestinationPath etc are ignored -// You can have the request call a different method by setting didReceiveDataSelector -- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data; - -// If a delegate implements one of these, it will be asked to supply credentials when none are available -// The delegate can then either restart the request ([request retryUsingSuppliedCredentials]) once credentials have been set -// or cancel it ([request cancelAuthentication]) -- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request; -- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request; - -@end diff --git a/clients/ios/ASI/ASIInputStream.h b/clients/ios/ASI/ASIInputStream.h deleted file mode 100755 index 7b9f93ed2..000000000 --- a/clients/ios/ASI/ASIInputStream.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// ASIInputStream.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 10/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import - -@class ASIHTTPRequest; - -// This is a wrapper for NSInputStream that pretends to be an NSInputStream itself -// Subclassing NSInputStream seems to be tricky, and may involve overriding undocumented methods, so we'll cheat instead. -// It is used by ASIHTTPRequest whenever we have a request body, and handles measuring and throttling the bandwidth used for uploading - -@interface ASIInputStream : NSObject { - NSInputStream *stream; - ASIHTTPRequest *request; -} -+ (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)request; -+ (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)request; - -@property (retain, nonatomic) NSInputStream *stream; -@property (assign, nonatomic) ASIHTTPRequest *request; -@end diff --git a/clients/ios/ASI/ASIInputStream.m b/clients/ios/ASI/ASIInputStream.m deleted file mode 100755 index dc998eb99..000000000 --- a/clients/ios/ASI/ASIInputStream.m +++ /dev/null @@ -1,138 +0,0 @@ -// -// ASIInputStream.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 10/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASIInputStream.h" -#import "ASIHTTPRequest.h" - -// Used to ensure only one request can read data at once -static NSLock *readLock = nil; - -@implementation ASIInputStream - -+ (void)initialize -{ - if (self == [ASIInputStream class]) { - readLock = [[NSLock alloc] init]; - } -} - -+ (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)theRequest -{ - ASIInputStream *theStream = [[[self alloc] init] autorelease]; - [theStream setRequest:theRequest]; - [theStream setStream:[NSInputStream inputStreamWithFileAtPath:path]]; - return theStream; -} - -+ (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)theRequest -{ - ASIInputStream *theStream = [[[self alloc] init] autorelease]; - [theStream setRequest:theRequest]; - [theStream setStream:[NSInputStream inputStreamWithData:data]]; - return theStream; -} - -- (void)dealloc -{ - [stream release]; - [super dealloc]; -} - -// Called when CFNetwork wants to read more of our request body -// When throttling is on, we ask ASIHTTPRequest for the maximum amount of data we can read -- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len -{ - [readLock lock]; - unsigned long toRead = len; - if ([ASIHTTPRequest isBandwidthThrottled]) { - toRead = [ASIHTTPRequest maxUploadReadLength]; - if (toRead > len) { - toRead = len; - } else if (toRead == 0) { - toRead = 1; - } - [request performThrottling]; - } - [readLock unlock]; - NSInteger rv = [stream read:buffer maxLength:toRead]; - if (rv > 0) - [ASIHTTPRequest incrementBandwidthUsedInLastSecond:(NSUInteger)rv]; - return rv; -} - -/* - * Implement NSInputStream mandatory methods to make sure they are implemented - * (necessary for MacRuby for example) and avoid the overhead of method - * forwarding for these common methods. - */ -- (void)open -{ - [stream open]; -} - -- (void)close -{ - [stream close]; -} - -- (id)delegate -{ - return [stream delegate]; -} - -- (void)setDelegate:(id)delegate -{ - [stream setDelegate:delegate]; -} - -- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode -{ - [stream scheduleInRunLoop:aRunLoop forMode:mode]; -} - -- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode -{ - [stream removeFromRunLoop:aRunLoop forMode:mode]; -} - -- (id)propertyForKey:(NSString *)key -{ - return [stream propertyForKey:key]; -} - -- (BOOL)setProperty:(id)property forKey:(NSString *)key -{ - return [stream setProperty:property forKey:key]; -} - -- (NSStreamStatus)streamStatus -{ - return [stream streamStatus]; -} - -- (NSError *)streamError -{ - return [stream streamError]; -} - -// If we get asked to perform a method we don't have (probably internal ones), -// we'll just forward the message to our stream - -- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector -{ - return [stream methodSignatureForSelector:aSelector]; -} - -- (void)forwardInvocation:(NSInvocation *)anInvocation -{ - [anInvocation invokeWithTarget:stream]; -} - -@synthesize stream; -@synthesize request; -@end diff --git a/clients/ios/ASI/ASINetworkQueue.h b/clients/ios/ASI/ASINetworkQueue.h deleted file mode 100755 index 6829b56af..000000000 --- a/clients/ios/ASI/ASINetworkQueue.h +++ /dev/null @@ -1,108 +0,0 @@ -// -// ASINetworkQueue.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import -#import "ASIHTTPRequestDelegate.h" -#import "ASIProgressDelegate.h" - -@interface ASINetworkQueue : NSOperationQueue { - - // Delegate will get didFail + didFinish messages (if set) - id delegate; - - // Will be called when a request starts with the request as the argument - SEL requestDidStartSelector; - - // Will be called when a request receives response headers - // Should take the form request:didRecieveResponseHeaders:, where the first argument is the request, and the second the headers dictionary - SEL requestDidReceiveResponseHeadersSelector; - - // Will be called when a request is about to redirect - // Should take the form request:willRedirectToURL:, where the first argument is the request, and the second the new url - SEL requestWillRedirectSelector; - - // Will be called when a request completes with the request as the argument - SEL requestDidFinishSelector; - - // Will be called when a request fails with the request as the argument - SEL requestDidFailSelector; - - // Will be called when the queue finishes with the queue as the argument - SEL queueDidFinishSelector; - - // Upload progress indicator, probably an NSProgressIndicator or UIProgressView - id uploadProgressDelegate; - - // Total amount uploaded so far for all requests in this queue - unsigned long long bytesUploadedSoFar; - - // Total amount to be uploaded for all requests in this queue - requests add to this figure as they work out how much data they have to transmit - unsigned long long totalBytesToUpload; - - // Download progress indicator, probably an NSProgressIndicator or UIProgressView - id downloadProgressDelegate; - - // Total amount downloaded so far for all requests in this queue - unsigned long long bytesDownloadedSoFar; - - // Total amount to be downloaded for all requests in this queue - requests add to this figure as they receive Content-Length headers - unsigned long long totalBytesToDownload; - - // When YES, the queue will cancel all requests when a request fails. Default is YES - BOOL shouldCancelAllRequestsOnFailure; - - //Number of real requests (excludes HEAD requests created to manage showAccurateProgress) - int requestsCount; - - // When NO, this request will only update the progress indicator when it completes - // When YES, this request will update the progress indicator according to how much data it has received so far - // When YES, the queue will first perform HEAD requests for all GET requests in the queue, so it can calculate the total download size before it starts - // NO means better performance, because it skips this step for GET requests, and it won't waste time updating the progress indicator until a request completes - // Set to YES if the size of a requests in the queue varies greatly for much more accurate results - // Default for requests in the queue is NO - BOOL showAccurateProgress; - - // Storage container for additional queue information. - NSDictionary *userInfo; - -} - -// Convenience constructor -+ (id)queue; - -// Call this to reset a queue - it will cancel all operations, clear delegates, and suspend operation -- (void)reset; - -// Used internally to manage HEAD requests when showAccurateProgress is YES, do not use! -- (void)addHEADOperation:(NSOperation *)operation; - -// All ASINetworkQueues are paused when created so that total size can be calculated before the queue starts -// This method will start the queue -- (void)go; - -@property (assign, nonatomic, setter=setUploadProgressDelegate:) id uploadProgressDelegate; -@property (assign, nonatomic, setter=setDownloadProgressDelegate:) id downloadProgressDelegate; - -@property (assign, atomic) SEL requestDidStartSelector; -@property (assign, atomic) SEL requestDidReceiveResponseHeadersSelector; -@property (assign, atomic) SEL requestWillRedirectSelector; -@property (assign, atomic) SEL requestDidFinishSelector; -@property (assign, atomic) SEL requestDidFailSelector; -@property (assign, atomic) SEL queueDidFinishSelector; -@property (assign, atomic) BOOL shouldCancelAllRequestsOnFailure; -@property (assign, atomic) id delegate; -@property (assign, atomic) BOOL showAccurateProgress; -@property (assign, atomic, readonly) int requestsCount; -@property (retain, atomic) NSDictionary *userInfo; - -@property (assign, atomic) unsigned long long bytesUploadedSoFar; -@property (assign, atomic) unsigned long long totalBytesToUpload; -@property (assign, atomic) unsigned long long bytesDownloadedSoFar; -@property (assign, atomic) unsigned long long totalBytesToDownload; - -@end diff --git a/clients/ios/ASI/ASINetworkQueue.m b/clients/ios/ASI/ASINetworkQueue.m deleted file mode 100755 index fd8a94964..000000000 --- a/clients/ios/ASI/ASINetworkQueue.m +++ /dev/null @@ -1,343 +0,0 @@ -// -// ASINetworkQueue.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASINetworkQueue.h" -#import "ASIHTTPRequest.h" - -// Private stuff -@interface ASINetworkQueue () - - (void)resetProgressDelegate:(id *)progressDelegate; - @property (assign) int requestsCount; -@end - -@implementation ASINetworkQueue - -- (id)init -{ - self = [super init]; - [self setShouldCancelAllRequestsOnFailure:YES]; - [self setMaxConcurrentOperationCount:4]; - [self setSuspended:YES]; - - return self; -} - -+ (id)queue -{ - return [[[self alloc] init] autorelease]; -} - -- (void)dealloc -{ - //We need to clear the queue on any requests that haven't got around to cleaning up yet, as otherwise they'll try to let us know if something goes wrong, and we'll be long gone by then - for (ASIHTTPRequest *request in [self operations]) { - [request setQueue:nil]; - } - [userInfo release]; - [super dealloc]; -} - -- (void)setSuspended:(BOOL)suspend -{ - [super setSuspended:suspend]; -} - -- (void)reset -{ - [self cancelAllOperations]; - [self setDelegate:nil]; - [self setDownloadProgressDelegate:nil]; - [self setUploadProgressDelegate:nil]; - [self setRequestDidStartSelector:NULL]; - [self setRequestDidReceiveResponseHeadersSelector:NULL]; - [self setRequestDidFailSelector:NULL]; - [self setRequestDidFinishSelector:NULL]; - [self setQueueDidFinishSelector:NULL]; - [self setSuspended:YES]; -} - - -- (void)go -{ - [self setSuspended:NO]; -} - -- (void)cancelAllOperations -{ - [self setBytesUploadedSoFar:0]; - [self setTotalBytesToUpload:0]; - [self setBytesDownloadedSoFar:0]; - [self setTotalBytesToDownload:0]; - [super cancelAllOperations]; -} - -- (void)setUploadProgressDelegate:(id)newDelegate -{ - uploadProgressDelegate = newDelegate; - [self resetProgressDelegate:&uploadProgressDelegate]; - -} - -- (void)setDownloadProgressDelegate:(id)newDelegate -{ - downloadProgressDelegate = newDelegate; - [self resetProgressDelegate:&downloadProgressDelegate]; -} - -- (void)resetProgressDelegate:(id *)progressDelegate -{ -#if !TARGET_OS_IPHONE - // If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can treat it similarly to UIProgressViews - SEL selector = @selector(setMaxValue:); - if ([*progressDelegate respondsToSelector:selector]) { - double max = 1.0; - [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&max callerToRetain:nil]; - } - selector = @selector(setDoubleValue:); - if ([*progressDelegate respondsToSelector:selector]) { - double value = 0.0; - [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value callerToRetain:nil]; - } -#else - SEL selector = @selector(setProgress:); - if ([*progressDelegate respondsToSelector:selector]) { - float value = 0.0f; - [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value callerToRetain:nil]; - } -#endif -} - -- (void)addHEADOperation:(NSOperation *)operation -{ - if ([operation isKindOfClass:[ASIHTTPRequest class]]) { - - ASIHTTPRequest *request = (ASIHTTPRequest *)operation; - [request setRequestMethod:@"HEAD"]; - [request setQueuePriority:10]; - [request setShowAccurateProgress:YES]; - [request setQueue:self]; - - // Important - we are calling NSOperation's add method - we don't want to add this as a normal request! - [super addOperation:request]; - } -} - -// Only add ASIHTTPRequests to this queue!! -- (void)addOperation:(NSOperation *)operation -{ - if (![operation isKindOfClass:[ASIHTTPRequest class]]) { - [NSException raise:@"AttemptToAddInvalidRequest" format:@"Attempted to add an object that was not an ASIHTTPRequest to an ASINetworkQueue"]; - } - - [self setRequestsCount:[self requestsCount]+1]; - - ASIHTTPRequest *request = (ASIHTTPRequest *)operation; - - if ([self showAccurateProgress]) { - - // Force the request to build its body (this may change requestMethod) - [request buildPostBody]; - - // If this is a GET request and we want accurate progress, perform a HEAD request first to get the content-length - // We'll only do this before the queue is started - // If requests are added after the queue is started they will probably move the overall progress backwards anyway, so there's no value performing the HEAD requests first - // Instead, they'll update the total progress if and when they receive a content-length header - if ([[request requestMethod] isEqualToString:@"GET"]) { - if ([self isSuspended]) { - ASIHTTPRequest *HEADRequest = [request HEADRequest]; - [self addHEADOperation:HEADRequest]; - [request addDependency:HEADRequest]; - if ([request shouldResetDownloadProgress]) { - [self resetProgressDelegate:&downloadProgressDelegate]; - [request setShouldResetDownloadProgress:NO]; - } - } - } - [request buildPostBody]; - [self request:nil incrementUploadSizeBy:(long long)[request postLength]]; - - - } else { - [self request:nil incrementDownloadSizeBy:1]; - [self request:nil incrementUploadSizeBy:1]; - } - // Tell the request not to increment the upload size when it starts, as we've already added its length - if ([request shouldResetUploadProgress]) { - [self resetProgressDelegate:&uploadProgressDelegate]; - [request setShouldResetUploadProgress:NO]; - } - - [request setShowAccurateProgress:[self showAccurateProgress]]; - - [request setQueue:self]; - [super addOperation:request]; - -} - -- (void)requestStarted:(ASIHTTPRequest *)request -{ - if ([self requestDidStartSelector]) { - [[self delegate] performSelector:[self requestDidStartSelector] withObject:request]; - } -} - -- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders -{ - if ([self requestDidReceiveResponseHeadersSelector]) { - [[self delegate] performSelector:[self requestDidReceiveResponseHeadersSelector] withObject:request withObject:responseHeaders]; - } -} - -- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL -{ - if ([self requestWillRedirectSelector]) { - [[self delegate] performSelector:[self requestWillRedirectSelector] withObject:request withObject:newURL]; - } -} - -- (void)requestFinished:(ASIHTTPRequest *)request -{ - [self setRequestsCount:[self requestsCount]-1]; - if ([self requestDidFinishSelector]) { - [[self delegate] performSelector:[self requestDidFinishSelector] withObject:request]; - } - if ([self requestsCount] == 0) { - if ([self queueDidFinishSelector]) { - [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self]; - } - } -} - -- (void)requestFailed:(ASIHTTPRequest *)request -{ - [self setRequestsCount:[self requestsCount]-1]; - if ([self requestDidFailSelector]) { - [[self delegate] performSelector:[self requestDidFailSelector] withObject:request]; - } - if ([self requestsCount] == 0) { - if ([self queueDidFinishSelector]) { - [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self]; - } - } - if ([self shouldCancelAllRequestsOnFailure] && [self requestsCount] > 0) { - [self cancelAllOperations]; - } - -} - - -- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes -{ - [self setBytesDownloadedSoFar:[self bytesDownloadedSoFar]+(unsigned long long)bytes]; - if ([self downloadProgressDelegate]) { - [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self bytesDownloadedSoFar] ofTotal:[self totalBytesToDownload]]; - } -} - -- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes -{ - [self setBytesUploadedSoFar:[self bytesUploadedSoFar]+(unsigned long long)bytes]; - if ([self uploadProgressDelegate]) { - [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:[self bytesUploadedSoFar] ofTotal:[self totalBytesToUpload]]; - } -} - -- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength -{ - [self setTotalBytesToDownload:[self totalBytesToDownload]+(unsigned long long)newLength]; -} - -- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength -{ - [self setTotalBytesToUpload:[self totalBytesToUpload]+(unsigned long long)newLength]; -} - - -// Since this queue takes over as the delegate for all requests it contains, it should forward authorisation requests to its own delegate -- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request -{ - if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) { - [[self delegate] performSelector:@selector(authenticationNeededForRequest:) withObject:request]; - } -} - -- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request -{ - if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - [[self delegate] performSelector:@selector(proxyAuthenticationNeededForRequest:) withObject:request]; - } -} - - -- (BOOL)respondsToSelector:(SEL)selector -{ - // We handle certain methods differently because whether our delegate implements them or not can affect how the request should behave - - // If the delegate implements this, the request will stop to wait for credentials - if (selector == @selector(authenticationNeededForRequest:)) { - if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) { - return YES; - } - return NO; - - // If the delegate implements this, the request will to wait for credentials - } else if (selector == @selector(proxyAuthenticationNeededForRequest:)) { - if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - return YES; - } - return NO; - - // If the delegate implements requestWillRedirectSelector, the request will stop to allow the delegate to change the url - } else if (selector == @selector(request:willRedirectToURL:)) { - if ([self requestWillRedirectSelector] && [[self delegate] respondsToSelector:[self requestWillRedirectSelector]]) { - return YES; - } - return NO; - } - return [super respondsToSelector:selector]; -} - -#pragma mark NSCopying - -- (id)copyWithZone:(NSZone *)zone -{ - ASINetworkQueue *newQueue = [[[self class] alloc] init]; - [newQueue setDelegate:[self delegate]]; - [newQueue setRequestDidStartSelector:[self requestDidStartSelector]]; - [newQueue setRequestWillRedirectSelector:[self requestWillRedirectSelector]]; - [newQueue setRequestDidReceiveResponseHeadersSelector:[self requestDidReceiveResponseHeadersSelector]]; - [newQueue setRequestDidFinishSelector:[self requestDidFinishSelector]]; - [newQueue setRequestDidFailSelector:[self requestDidFailSelector]]; - [newQueue setQueueDidFinishSelector:[self queueDidFinishSelector]]; - [newQueue setUploadProgressDelegate:[self uploadProgressDelegate]]; - [newQueue setDownloadProgressDelegate:[self downloadProgressDelegate]]; - [newQueue setShouldCancelAllRequestsOnFailure:[self shouldCancelAllRequestsOnFailure]]; - [newQueue setShowAccurateProgress:[self showAccurateProgress]]; - [newQueue setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]]; - return newQueue; -} - - -@synthesize requestsCount; -@synthesize bytesUploadedSoFar; -@synthesize totalBytesToUpload; -@synthesize bytesDownloadedSoFar; -@synthesize totalBytesToDownload; -@synthesize shouldCancelAllRequestsOnFailure; -@synthesize uploadProgressDelegate; -@synthesize downloadProgressDelegate; -@synthesize requestDidStartSelector; -@synthesize requestDidReceiveResponseHeadersSelector; -@synthesize requestWillRedirectSelector; -@synthesize requestDidFinishSelector; -@synthesize requestDidFailSelector; -@synthesize queueDidFinishSelector; -@synthesize delegate; -@synthesize showAccurateProgress; -@synthesize userInfo; -@end diff --git a/clients/ios/ASI/ASIProgressDelegate.h b/clients/ios/ASI/ASIProgressDelegate.h deleted file mode 100755 index e2bb0cf49..000000000 --- a/clients/ios/ASI/ASIProgressDelegate.h +++ /dev/null @@ -1,38 +0,0 @@ -// -// ASIProgressDelegate.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 13/04/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -@class ASIHTTPRequest; - -@protocol ASIProgressDelegate - -@optional - -// These methods are used to update UIProgressViews (iPhone OS) or NSProgressIndicators (Mac OS X) -// If you are using a custom progress delegate, you may find it easier to implement didReceiveBytes / didSendBytes instead -#if TARGET_OS_IPHONE -- (void)setProgress:(float)newProgress; -#else -- (void)setDoubleValue:(double)newProgress; -- (void)setMaxValue:(double)newMax; -#endif - -// Called when the request receives some data - bytes is the length of that data -- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes; - -// Called when the request sends some data -// The first 32KB (128KB on older platforms) of data sent is not included in this amount because of limitations with the CFNetwork API -// bytes may be less than zero if a request needs to remove upload progress (probably because the request needs to run again) -- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes; - -// Called when a request needs to change the length of the content to download -- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength; - -// Called when a request needs to change the length of the content to upload -// newLength may be less than zero when a request needs to remove the size of the internal buffer from progress tracking -- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength; -@end diff --git a/clients/ios/Classes/ActivityModule.h b/clients/ios/Classes/ActivityModule.h index e47035e88..53588defe 100644 --- a/clients/ios/Classes/ActivityModule.h +++ b/clients/ios/Classes/ActivityModule.h @@ -9,14 +9,12 @@ #import @class NewsBlurAppDelegate; -@class ASIHTTPRequest; @interface ActivityModule : UIView { NewsBlurAppDelegate *appDelegate; UITableView *activitiesTable; - UIPopoverController *popoverController; BOOL pageFetching; BOOL pageFinished; @@ -25,7 +23,6 @@ @property (nonatomic) NewsBlurAppDelegate *appDelegate; @property (nonatomic, strong) UITableView *activitiesTable; -@property (nonatomic, strong) UIPopoverController *popoverController; @property (nonatomic, readwrite) BOOL pageFetching; @property (nonatomic, readwrite) BOOL pageFinished; @@ -34,8 +31,6 @@ - (void)refreshWithActivities:(NSArray *)activities; - (void)fetchActivitiesDetail:(int)page; -- (void)finishLoadActivities:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)checkScroll; diff --git a/clients/ios/Classes/ActivityModule.m b/clients/ios/Classes/ActivityModule.m index 8e0125b2a..960467240 100644 --- a/clients/ios/Classes/ActivityModule.m +++ b/clients/ios/Classes/ActivityModule.m @@ -11,7 +11,6 @@ #import "NewsBlurAppDelegate.h" #import "UserProfileViewController.h" #import -#import "ASIHTTPRequest.h" #import "ActivityCell.h" #import "SmallActivityCell.h" @@ -19,7 +18,6 @@ @synthesize appDelegate; @synthesize activitiesTable; -@synthesize popoverController; @synthesize pageFetching; @synthesize pageFinished; @synthesize activitiesPage; @@ -31,7 +29,7 @@ { self = [super initWithFrame:frame]; if (self) { - // initialize code here + self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate]; } return self; } @@ -49,7 +47,6 @@ } - (void)refreshWithActivities:(NSArray *)activities { - self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.userActivitiesArray = activities; [self.activitiesTable reloadData]; @@ -102,26 +99,17 @@ [appDelegate.dictSocialProfile objectForKey:@"user_id"], page]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setDidFinishSelector:@selector(finishLoadActivities:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setDelegate:self]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishLoadActivities:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [appDelegate informError:error]; + }]; } } -- (void)finishLoadActivities:(ASIHTTPRequest *)request { +- (void)finishLoadActivities:(NSDictionary *)results { self.pageFetching = NO; - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - + // check for last page if (![[results objectForKey:@"has_next_page"] intValue]) { self.pageFinished = YES; @@ -152,12 +140,6 @@ [self refreshWithActivities:appDelegate.userActivitiesArray]; } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [appDelegate informError:error]; -} - #pragma mark - #pragma mark Table View - Interactions List diff --git a/clients/ios/Classes/AddSiteViewController.h b/clients/ios/Classes/AddSiteViewController.h index 2b9459463..d74d203b5 100644 --- a/clients/ios/Classes/AddSiteViewController.h +++ b/clients/ios/Classes/AddSiteViewController.h @@ -8,16 +8,16 @@ #import #import "NewsBlurAppDelegate.h" -#import "ASIHTTPRequest.h" @class NewsBlurAppDelegate; -@interface AddSiteViewController : UIViewController - +@interface AddSiteViewController : BaseViewController + { + NewsBlurAppDelegate *appDelegate; +} - (void)reload; - (IBAction)addSite; -- (void)autocompleteSite:(ASIHTTPRequest *)request; - (IBAction)doCancelButton; - (IBAction)doAddButton; - (NSString *)extractParentFolder; @@ -26,7 +26,6 @@ - (IBAction)toggleAddFolder:(id)sender; - (NSArray *)folders; -@property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate; @property (nonatomic) IBOutlet UITextField *inFolderInput; @property (nonatomic) IBOutlet UITextField *addFolderInput; @property (nonatomic) IBOutlet UITextField *siteAddressInput; diff --git a/clients/ios/Classes/AddSiteViewController.m b/clients/ios/Classes/AddSiteViewController.m index 7c139946b..5096714b2 100644 --- a/clients/ios/Classes/AddSiteViewController.m +++ b/clients/ios/Classes/AddSiteViewController.m @@ -10,8 +10,6 @@ #import "AddSiteAutocompleteCell.h" #import "NewsBlurAppDelegate.h" #import "NewsBlurViewController.h" -#import "ASIHTTPRequest.h" -#import "ASIFormDataRequest.h" #import "NBContainerViewController.h" #import "MenuViewController.h" #import "SBJson4.h" @@ -29,11 +27,14 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { + } return self; } - (void)viewDidLoad { + appDelegate = [NewsBlurAppDelegate sharedAppDelegate]; + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(doCancelButton)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add Site" style:UIBarButtonItemStyleDone target:self action:@selector(addSite)]; @@ -45,12 +46,12 @@ [self.inFolderInput setLeftViewMode:UITextFieldViewModeAlways]; // If you want to show a disclosure arrow; don't really need it, though. -// UIImageView *disclosureImage = [[UIImageView alloc] -// initWithImage:[UIImage imageNamed:@"accessory_disclosure.png"]]; -// disclosureImage.frame = CGRectMake(0, 0, 24, 20); -// [disclosureImage setContentMode:UIViewContentModeLeft]; -// [inFolderInput setRightView:disclosureImage]; -// [inFolderInput setRightViewMode:UITextFieldViewModeAlways]; + // UIImageView *disclosureImage = [[UIImageView alloc] + // initWithImage:[UIImage imageNamed:@"accessory_disclosure.png"]]; + // disclosureImage.frame = CGRectMake(0, 0, 24, 20); + // [disclosureImage setContentMode:UIViewContentModeLeft]; + // [inFolderInput setRightView:disclosureImage]; + // [inFolderInput setRightViewMode:UITextFieldViewModeAlways]; UIImageView *folderImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"g_icn_folder_rss_sm.png"]]; @@ -82,7 +83,7 @@ self.siteTable.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR); // eliminate extra separators at bottom of site table (e.g., while animating) self.siteTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; - + [super viewWillAppear:animated]; } @@ -122,9 +123,9 @@ - (IBAction)doCancelButton { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [self.appDelegate hidePopover]; + [appDelegate hidePopover]; } else { - [self.appDelegate hidePopoverAnimated:YES]; + [appDelegate hidePopoverAnimated:YES]; } } @@ -160,10 +161,10 @@ if (self.siteAddressInput.returnKeyType == UIReturnKeySearch) { [self checkSiteAddress]; } else { - [self addSite]; + [self addSite]; } } - return YES; + return YES; } - (IBAction)checkSiteAddress { @@ -196,47 +197,31 @@ [self.siteActivityIndicator startAnimating]; NSString *urlString = [NSString stringWithFormat:@"%@/rss_feeds/feed_autocomplete?term=%@&v=2", - self.appDelegate.url, [phrase stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(autocompleteSite:)]; - [request startAsynchronous]; -} - -- (void)autocompleteSite:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - - - NSString *query = [NSString stringWithFormat:@"%@", [results objectForKey:@"term"]]; - NSString *phrase = self.siteAddressInput.text; - - // cache the results - [self.searchResults_ setValue:[results objectForKey:@"feeds"] forKey:query]; - - if ([phrase isEqualToString:query]) { - self.autocompleteResults = [results objectForKey:@"feeds"]; - [self reloadSearchResults]; - } - -// NSRange range = [query rangeOfString : activeTerm_]; -// BOOL found = (range.location != NSNotFound); + appDelegate.url, [phrase stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSString *query = [NSString stringWithFormat:@"%@", [responseObject objectForKey:@"term"]]; + NSString *phrase = self.siteAddressInput.text; + + // cache the results + [self.searchResults_ setValue:[responseObject objectForKey:@"feeds"] forKey:query]; + + if ([phrase isEqualToString:query]) { + self.autocompleteResults = [responseObject objectForKey:@"feeds"]; + [self reloadSearchResults]; + } + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self.siteActivityIndicator stopAnimating]; + }]; } - (void)reloadSearchResults { if ([self.siteAddressInput.text length] > 0 && [self.autocompleteResults count] > 0) { - [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionAllowUserInteraction + [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ [self.siteScrollView setAlpha:1]; } completion:nil]; } else { - [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionAllowUserInteraction + [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ [self.siteScrollView setAlpha:0]; } completion:nil]; @@ -257,47 +242,45 @@ [self.errorLabel setHidden:YES]; [self.activityIndicator startAnimating]; NSString *urlString = [NSString stringWithFormat:@"%@/reader/add_url", - self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + appDelegate.url]; + + NSMutableDictionary *params = [NSMutableDictionary dictionary]; NSString *parent_folder = [self extractParentFolder]; - [request setPostValue:parent_folder forKey:@"folder"]; - [request setPostValue:[self.siteAddressInput text] forKey:@"url"]; + [params setObject:parent_folder forKey:@"folder"]; + [params setObject:[self.siteAddressInput text] forKey:@"url"]; if (self.addFolderButton.selected && [self.addFolderInput.text length]) { - [request setPostValue:[self.addFolderInput text] forKey:@"new_folder"]; - } - [request setDelegate:self]; - [request setDidFinishSelector:@selector(requestFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; -} - - -- (void)requestFinished:(ASIHTTPRequest *)request { - [self.addingLabel setHidden:YES]; - [self.activityIndicator stopAnimating]; - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; - int code = [[results valueForKey:@"code"] intValue]; - if (code == -1) { - [self.errorLabel setText:[results valueForKey:@"message"]]; - [self.errorLabel setHidden:NO]; - } else { - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [self.appDelegate hidePopover]; - } else { - [self.appDelegate hidePopoverAnimated:YES]; - } - [self.appDelegate reloadFeedsView:NO]; + [params setObject:[self.addFolderInput text] forKey:@"new_folder"]; } + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + + [self.addingLabel setHidden:YES]; + [self.activityIndicator stopAnimating]; + + int code = [[responseObject valueForKey:@"code"] intValue]; + if (code == -1) { + [self.errorLabel setText:[responseObject valueForKey:@"message"]]; + [self.errorLabel setHidden:NO]; + } else { + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [appDelegate hidePopover]; + } else { + [appDelegate hidePopoverAnimated:YES]; + } + [appDelegate reloadFeedsView:NO]; + } + + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + + [self.addingLabel setHidden:YES]; + [self.errorLabel setHidden:NO]; + [self.activityIndicator stopAnimating]; + NSLog(@"Error: %@", error); + [self.errorLabel setText:error.localizedDescription]; + self.siteTable.hidden = YES; + [self preferredContentSize]; + + }]; } - (NSString *)extractParentFolder { @@ -327,7 +310,7 @@ self.view.frame.size.width, self.siteScrollView.frame.size.height); } completion:nil]; - + } else { self.addFolderButton.selected = NO; [UIView animateWithDuration:.35 delay:0 options:UIViewAnimationOptionAllowUserInteraction @@ -343,19 +326,8 @@ [self preferredContentSize]; } -- (void)requestFailed:(ASIHTTPRequest *)request { - [self.addingLabel setHidden:YES]; - [self.errorLabel setHidden:NO]; - [self.activityIndicator stopAnimating]; - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [self.errorLabel setText:error.localizedDescription]; - self.siteTable.hidden = YES; - [self preferredContentSize]; -} - - (NSArray *)folders { - return _.without([self.appDelegate dictFoldersArray], + return _.without([appDelegate dictFoldersArray], @[@"saved_stories", @"read_stories", @"river_blurblogs", @@ -398,7 +370,7 @@ viewController.checkedRow = [folders indexOfObject:self.inFolderInput.text] + 1; } - [self.appDelegate.addSiteNavigationController pushViewController:viewController animated:YES]; + [appDelegate.addSiteNavigationController pushViewController:viewController animated:YES]; } #pragma mark - @@ -409,13 +381,13 @@ return [self.autocompleteResults count]; } -- (UITableViewCell *)tableView:(UITableView *)tableView +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *AddSiteAutocompleteCellIdentifier = @"AddSiteAutocompleteCellIdentifier"; - AddSiteAutocompleteCell *cell = (AddSiteAutocompleteCell *)[tableView dequeueReusableCellWithIdentifier:AddSiteAutocompleteCellIdentifier]; - if (cell == nil) { - NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AddSiteAutocompleteCell" + AddSiteAutocompleteCell *cell = (AddSiteAutocompleteCell *)[tableView dequeueReusableCellWithIdentifier:AddSiteAutocompleteCellIdentifier]; + if (cell == nil) { + NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AddSiteAutocompleteCell" owner:self options:nil]; for (id oneObject in nib) { @@ -427,13 +399,13 @@ if (cell == nil) { cell = [AddSiteAutocompleteCell new]; } - } + } NSDictionary *result = [self.autocompleteResults objectAtIndex:indexPath.row]; int subs = [[result objectForKey:@"num_subscribers"] intValue]; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; - [numberFormatter setPositiveFormat:@"#,###"]; - NSNumber *theScore = [NSNumber numberWithInt:subs]; + [numberFormatter setPositiveFormat:@"#,###"]; + NSNumber *theScore = [NSNumber numberWithInt:subs]; NSString *favicon = [result objectForKey:@"favicon"]; UIImage *faviconImage; if ((NSNull *)favicon != [NSNull null] && [favicon length] > 0) { @@ -442,13 +414,13 @@ } else { faviconImage = [UIImage imageNamed:@"world.png"]; } - + cell.feedTitle.text = [result objectForKey:@"label"]; cell.feedTitle.textColor = UIColorFromRGB(NEWSBLUR_BLACK_COLOR); cell.feedUrl.text = [result objectForKey:@"value"]; cell.feedUrl.textColor = UIColorFromFixedRGB(NEWSBLUR_LINK_COLOR); cell.feedSubs.text = [[NSString stringWithFormat:@"%@ subscriber%@", - [NSString stringWithFormat:@"%@", [numberFormatter stringFromNumber:theScore]], subs == 1 ? @"" : @"s"] uppercaseString]; + [NSString stringWithFormat:@"%@", [numberFormatter stringFromNumber:theScore]], subs == 1 ? @"" : @"s"] uppercaseString]; cell.feedSubs.textColor = UIColorFromRGB(0x808080); cell.feedFavicon.image = faviconImage; cell.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR); @@ -456,7 +428,7 @@ return cell; } -- (void)tableView:(UITableView *)tableView +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *result = [self.autocompleteResults objectAtIndex:indexPath.row]; [self.siteAddressInput setText:[result objectForKey:@"value"]]; diff --git a/clients/ios/Classes/AuthorizeServicesViewController.m b/clients/ios/Classes/AuthorizeServicesViewController.m index e9b60576b..0a598cb12 100644 --- a/clients/ios/Classes/AuthorizeServicesViewController.m +++ b/clients/ios/Classes/AuthorizeServicesViewController.m @@ -100,13 +100,7 @@ }]; } else { [self.navigationController popViewControllerAnimated:YES]; - if ([type isEqualToString:@"google"]) { - if (error.length) { - [appDelegate.firstTimeUserAddSitesViewController importFromGoogleReaderFailed:error]; - } else { - [appDelegate.firstTimeUserAddSitesViewController importFromGoogleReader]; - } - } else if ([type isEqualToString:@"facebook"]) { + if ([type isEqualToString:@"facebook"]) { if (error.length) { [self showError:error]; } else { diff --git a/clients/ios/Classes/BaseViewController.h b/clients/ios/Classes/BaseViewController.h index 5a5d5ae63..577155ebe 100644 --- a/clients/ios/Classes/BaseViewController.h +++ b/clients/ios/Classes/BaseViewController.h @@ -1,20 +1,9 @@ #import -#import "ASIHTTPRequest.h" -#import "ASIFormDataRequest.h" #import "MBProgressHUD.h" @interface BaseViewController : UIViewController { - - NSMutableArray* requests; - } -- (ASIHTTPRequest*) requestWithURL:(NSString*) s; -- (ASIFormDataRequest*) formRequestWithURL:(NSString*) s; -- (void) addRequest:(ASIHTTPRequest*)request; -- (void) clearFinishedRequests; -- (void) cancelRequests; - - (void)informError:(id)error; - (void)informError:(id)error details:(NSString *)details; - (void)informMessage:(NSString *)message; diff --git a/clients/ios/Classes/BaseViewController.m b/clients/ios/Classes/BaseViewController.m index 4b82ba255..cdfdd6781 100644 --- a/clients/ios/Classes/BaseViewController.m +++ b/clients/ios/Classes/BaseViewController.m @@ -6,48 +6,12 @@ #pragma mark - #pragma mark HTTP requests -- (ASIHTTPRequest*) requestWithURL:(NSString*) s { - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:s]]; - [request setValidatesSecureCertificate:NO]; - [self addRequest:request]; - return request; -} +- (instancetype)init { + if (self = [super init]) { -- (ASIFormDataRequest*) formRequestWithURL:(NSString*) s { - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:s]]; - [self addRequest:request]; - return request; -} - -- (void) addRequest:(ASIHTTPRequest*)request { - [request setDelegate:self]; - if (!requests) { - requests = [[NSMutableArray alloc] initWithCapacity:3]; - } else { - [self clearFinishedRequests]; - } - [requests addObject:request]; -} - -- (void) clearFinishedRequests { - NSMutableArray* toremove = [[NSMutableArray alloc] initWithCapacity:[requests count]]; - for (ASIHTTPRequest* r in requests) { - if ([r isFinished]) { - [toremove addObject:r]; - } - } - - for (ASIHTTPRequest* r in toremove) { - [requests removeObject:r]; - } -} - -- (void) cancelRequests { - for (ASIHTTPRequest* r in requests) { - r.delegate = nil; - [r cancel]; - } - [requests removeAllObjects]; + } + + return self; } #pragma mark - @@ -146,13 +110,4 @@ } } -#pragma mark - -#pragma mark Memory management - -- (void)dealloc { - [self cancelRequests]; - -} - - @end diff --git a/clients/ios/Classes/DashboardViewController.m b/clients/ios/Classes/DashboardViewController.m index f872afb29..ca9b308a8 100644 --- a/clients/ios/Classes/DashboardViewController.m +++ b/clients/ios/Classes/DashboardViewController.m @@ -12,7 +12,7 @@ #import "InteractionsModule.h" #import "FeedDetailViewController.h" #import "UserProfileViewController.h" -#import "TMCache.h" +#import "PINCache.h" #import "StoriesCollection.h" #import "UISearchBar+Field.h" @@ -170,7 +170,7 @@ #pragma mark - Stories - (void)refreshStories { - [appDelegate.cachedStoryImages removeAllObjects:^(TMCache *cache) { + [appDelegate.cachedStoryImages removeAllObjects:^(PINCache * _Nonnull cache) { dispatch_async(dispatch_get_main_queue(), ^{ [appDelegate loadRiverFeedDetailView:self.storiesModule withFolder:@"everything"]; appDelegate.inFeedDetail = NO; @@ -213,4 +213,4 @@ shouldStartLoadWithRequest:(NSURLRequest *)request return NO; } } -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/FeedChooserViewController.h b/clients/ios/Classes/FeedChooserViewController.h index 8f585c2d4..47c7c6855 100644 --- a/clients/ios/Classes/FeedChooserViewController.h +++ b/clients/ios/Classes/FeedChooserViewController.h @@ -7,6 +7,7 @@ // #import +#import "NewsBlurAppDelegate.h" typedef NS_ENUM(NSUInteger, FeedChooserOperation) { @@ -15,7 +16,9 @@ typedef NS_ENUM(NSUInteger, FeedChooserOperation) }; -@interface FeedChooserViewController : UIViewController +@interface FeedChooserViewController : BaseViewController { + NewsBlurAppDelegate *appDelegate; +} @property (weak) IBOutlet UITableView *tableView; diff --git a/clients/ios/Classes/FeedChooserViewController.m b/clients/ios/Classes/FeedChooserViewController.m index 7325fa7e1..029ef5341 100644 --- a/clients/ios/Classes/FeedChooserViewController.m +++ b/clients/ios/Classes/FeedChooserViewController.m @@ -42,6 +42,8 @@ static const CGFloat kFolderTitleHeight = 36.0; - (void)viewDidLoad { [super viewDidLoad]; + + appDelegate = [NewsBlurAppDelegate sharedAppDelegate]; UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)]; self.optionsItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_icn_settings.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showOptionsMenu)]; @@ -96,21 +98,14 @@ static const CGFloat kFolderTitleHeight = 36.0; } NSString *urlString = [NSString stringWithFormat:@"%@/reader/feeds?flat=true&update_counts=false&include_inactive=true", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - request.delegate = self; - request.didFinishSelector = @selector(finishLoadingInactiveFeeds:); - request.didFailSelector = @selector(finishedWithError:); - request.timeOutSeconds = 30; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishLoadingInactiveFeeds:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self finishedWithError:error]; + }]; } -- (void)finishLoadingInactiveFeeds:(ASIHTTPRequest *)request { - NSString *responseString = request.responseString; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error = nil; - NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; - +- (void)finishLoadingInactiveFeeds:(NSDictionary *)results { self.dictFolders = results[@"flat_folders_with_inactive"]; self.inactiveFeeds = results[@"inactive_feeds"]; @@ -126,8 +121,7 @@ static const CGFloat kFolderTitleHeight = 36.0; [MBProgressHUD hideHUDForView:self.view animated:YES]; } -- (void)finishedWithError:(ASIHTTPRequest *)request { - NSError *error = request.error; +- (void)finishedWithError:(NSError *)error { NSLog(@"informError: %@", error); NSString *errorMessage = [error localizedDescription]; @@ -138,11 +132,11 @@ static const CGFloat kFolderTitleHeight = 36.0; [HUD setMode:MBProgressHUDModeCustomView]; HUD.labelText = errorMessage; [HUD hide:YES afterDelay:1]; + [self rebuildItemsAnimated:YES]; } - (void)rebuildItemsAnimated:(BOOL)animated { - NewsBlurAppDelegate *appDelegate = self.appDelegate; FeedChooserItem *section = nil; NSMutableArray *sections = [NSMutableArray array]; @@ -464,17 +458,16 @@ static const CGFloat kFolderTitleHeight = 36.0; } NSString *urlString = [NSString stringWithFormat:@"%@/reader/move_feeds_by_folder_to_folder", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:feedsByFolder.JSONRepresentation forKey:@"feeds_by_folder"]; - [request setPostValue:toFolder.identifier forKey:@"to_folder"]; - [request setCompletionBlock:^(void) { + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:feedsByFolder.JSONRepresentation forKey:@"feeds_by_folder"]; + [params setObject:toFolder.identifier forKey:@"to_folder"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { HUD.labelText = @"Reloading..."; [self.appDelegate reloadFeedsView:YES]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self finishedWithError:error]; }]; - request.didFailSelector = @selector(finishedWithError:); - request.timeOutSeconds = 30; - [request startAsynchronous]; + } - (void)showMoveMenu { @@ -519,16 +512,14 @@ static const CGFloat kFolderTitleHeight = 36.0; } NSString *urlString = [NSString stringWithFormat:@"%@/reader/delete_feeds_by_folder", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:feedsByFolder.JSONRepresentation forKey:@"feeds_by_folder"]; - [request setCompletionBlock:^(void) { + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:feedsByFolder.JSONRepresentation forKey:@"feeds_by_folder"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { HUD.labelText = @"Reloading..."; [self.appDelegate reloadFeedsView:YES]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self finishedWithError:error]; }]; - request.didFailSelector = @selector(finishedWithError:); - request.timeOutSeconds = 30; - [request startAsynchronous]; } - (void)deleteFeeds { @@ -568,23 +559,23 @@ static const CGFloat kFolderTitleHeight = 36.0; HUD.labelText = @"Updating..."; NSString *urlString = [NSString stringWithFormat:@"%@/reader/save_feed_chooser", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; NSArray *mutedIndexPaths = self.tableView.indexPathsForSelectedRows; + NSMutableArray *feeds = [NSMutableArray array]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; [self enumerateAllRowsUsingBlock:^(NSIndexPath *indexPath, FeedChooserItem *item) { if (![mutedIndexPaths containsObject:indexPath]) { - [request addPostValue:item.identifier forKey:@"approved_feeds"]; + [feeds addObject:item.identifier]; } }]; - [request setCompletionBlock:^(void) { + [params setObject:feeds forKey:@"approved_feeds"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [self.appDelegate reloadFeedsView:YES]; [self dismissViewControllerAnimated:YES completion:nil]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self finishedWithError:error]; }]; - request.didFailSelector = @selector(finishedWithError:); - request.timeOutSeconds = 30; - [request startAsynchronous]; } - (BOOL)didChangeActiveFeeds { diff --git a/clients/ios/Classes/FeedDetailTableCell.m b/clients/ios/Classes/FeedDetailTableCell.m index a318f81c0..52e399e57 100644 --- a/clients/ios/Classes/FeedDetailTableCell.m +++ b/clients/ios/Classes/FeedDetailTableCell.m @@ -15,7 +15,7 @@ #import "UIImageView+AFNetworking.h" #import "Utilities.h" #import "MCSwipeTableViewCell.h" -#import "TMCache.h" +#import "PINCache.h" static UIFont *textFont = nil; static UIFont *indicatorFont = nil; diff --git a/clients/ios/Classes/FeedDetailViewController.h b/clients/ios/Classes/FeedDetailViewController.h index dc4835366..f4aeffa62 100644 --- a/clients/ios/Classes/FeedDetailViewController.h +++ b/clients/ios/Classes/FeedDetailViewController.h @@ -8,7 +8,6 @@ #import #import "NewsBlurAppDelegate.h" -#import "ASIHTTPRequest.h" #import "BaseViewController.h" #import "Utilities.h" #import "NBNotifier.h" @@ -19,8 +18,8 @@ @class MCSwipeTableViewCell; @interface FeedDetailViewController : BaseViewController - { NewsBlurAppDelegate *appDelegate; @@ -72,7 +71,6 @@ - (void)loadOfflineStories; - (void)fetchRiver; - (void)fetchRiverPage:(int)page withCallback:(void(^)())callback; -- (void)finishedLoadingFeed:(ASIHTTPRequest *)request; - (void)testForTryFeed; - (void)cacheStoryImages:(NSArray *)storyImageUrls; - (void)showStoryImage:(NSString *)imageUrl; @@ -106,12 +104,10 @@ - (void)changeActiveStoryTitleCellLayout; - (void)loadFaviconsFromActiveFeed; - (void)markFeedsReadFromTimestamp:(NSInteger)cutoffTimestamp andOlder:(BOOL)older; -- (void)saveAndDrawFavicons:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsSaved:(ASIFormDataRequest *)request; -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsUnsaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsUnread:(ASIFormDataRequest *)request; +- (void)finishMarkAsSaved:(NSDictionary *)params; +- (void)failedMarkAsSaved:(NSDictionary *)params; +- (void)finishMarkAsUnsaved:(NSDictionary *)params; +- (void)failedMarkAsUnsaved:(NSDictionary *)params; +- (void)failedMarkAsUnread:(NSDictionary *)params; @end diff --git a/clients/ios/Classes/FeedDetailViewController.m b/clients/ios/Classes/FeedDetailViewController.m index 977de23f8..40e37d9ab 100644 --- a/clients/ios/Classes/FeedDetailViewController.m +++ b/clients/ios/Classes/FeedDetailViewController.m @@ -12,7 +12,6 @@ #import "NBContainerViewController.h" #import "NewsBlurViewController.h" #import "FeedDetailTableCell.h" -#import "ASIFormDataRequest.h" #import "UserProfileViewController.h" #import "StoryDetailViewController.h" #import "StoryPageControl.h" @@ -31,8 +30,7 @@ #import "FMDatabase.h" #import "NBBarButtonItem.h" #import "UIImage+Resize.h" -#import "TMCache.h" -#import "AFHTTPRequestOperation.h" +#import "PINCache.h" #import "DashboardViewController.h" #import "StoriesCollection.h" #import "NSNull+JSON.h" @@ -522,7 +520,7 @@ storiesCollection.searchQuery = nil; [self.searchBar setText:@""]; [self.notifier hideIn:0]; - [self cancelRequests]; +// [self cancelRequests]; [self beginOfflineTimer]; [appDelegate.cacheImagesOperationQueue cancelAllOperations]; } @@ -565,37 +563,35 @@ - (void)cacheStoryImages:(NSArray *)storyImageUrls { NSBlockOperation *cacheImagesOperation = [NSBlockOperation blockOperationWithBlock:^{ + AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; + [manager.requestSerializer setTimeoutInterval:5]; + manager.responseSerializer = [AFImageResponseSerializer serializer]; + for (NSString *storyImageUrl in storyImageUrls) { // NSLog(@"Fetching image: %@", storyImageUrl); - NSMutableURLRequest *request = [NSMutableURLRequest - requestWithURL:[NSURL URLWithString:storyImageUrl]]; - [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; - [request setTimeoutInterval:5.0]; - AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] - initWithRequest:request]; - [requestOperation setResponseSerializer:[AFImageResponseSerializer serializer]]; - [requestOperation start]; - [requestOperation waitUntilFinished]; - - UIImage *image = (UIImage *)requestOperation.responseObject; - - if (!image || image.size.height < 50 || image.size.width < 50) { - [appDelegate.cachedStoryImages setObject:[NSNull null] + [manager GET:storyImageUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + UIImage *image = (UIImage *)responseObject; + + if (!image || image.size.height < 50 || image.size.width < 50) { + [appDelegate.cachedStoryImages setObject:[NSNull null] + forKey:storyImageUrl]; + return; + } + + CGSize maxImageSize = CGSizeMake(300, 300); + image = [image imageByScalingAndCroppingForSize:maxImageSize]; + [appDelegate.cachedStoryImages setObject:image forKey:storyImageUrl]; - continue; - } - - CGSize maxImageSize = CGSizeMake(300, 300); - image = [image imageByScalingAndCroppingForSize:maxImageSize]; - [appDelegate.cachedStoryImages setObject:image - forKey:storyImageUrl]; - if (self.isDashboardModule) { - [appDelegate.dashboardViewController.storiesModule - showStoryImage:storyImageUrl]; - } else { - [appDelegate.feedDetailViewController - showStoryImage:storyImageUrl]; - } + if (self.isDashboardModule) { + [appDelegate.dashboardViewController.storiesModule + showStoryImage:storyImageUrl]; + } else { + [appDelegate.feedDetailViewController + showStoryImage:storyImageUrl]; + } + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + + }]; } }]; [cacheImagesOperation setQualityOfService:NSQualityOfServiceBackground]; @@ -704,39 +700,34 @@ if (storiesCollection.inSearch && storiesCollection.searchQuery) { theFeedDetailURL = [NSString stringWithFormat:@"%@&query=%@", theFeedDetailURL, - [storiesCollection.searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + [storiesCollection.searchQuery stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; } - [self cancelRequests]; - __weak ASIHTTPRequest *request = [self requestWithURL:theFeedDetailURL]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setValidatesSecureCertificate:NO]; - [request setUserInfo:@{@"feedPage": [NSNumber numberWithInt:storiesCollection.feedPage]}]; - [request setFailedBlock:^(void) { - NSLog(@"in failed block %@", request); - if (request.isCancelled) { - NSLog(@"Cancelled"); - return; - } else { - self.isOnline = NO; - storiesCollection.feedPage = 1; - [self loadOfflineStories]; - [self showOfflineNotifier]; - } - [self.storyTitlesTable reloadData]; - }]; - [request setCompletionBlock:^(void) { +// [self cancelRequests]; + NSString *feedId = [NSString stringWithFormat:@"%@", [[storiesCollection activeFeed] objectForKey:@"id"]]; + NSInteger feedPage = storiesCollection.feedPage; + [appDelegate.networkManager GET:theFeedDetailURL parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { if (!storiesCollection.activeFeed) return; - [self finishedLoadingFeed:request]; + [self finishedLoadingFeed:responseObject feedPage:feedPage feedId:feedId]; if (callback) { callback(); } + } failure:^(NSURLSessionTask *operation, NSError *error) { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)operation.response; + NSLog(@"in failed block %@", operation); + self.isOnline = NO; + self.isShowingFetching = NO; + // storiesCollection.feedPage = 1; + [self loadOfflineStories]; + [self showOfflineNotifier]; + if (httpResponse.statusCode == 503) { + [self informError:@"In maintenance mode"]; + self.pageFinished = YES; + } else if (httpResponse.statusCode >= 500) { + [self informError:@"The server barfed."]; + } + + [self.storyTitlesTable reloadData]; }]; - [request setTimeOutSeconds:30]; - [request setTag:[[[storiesCollection activeFeed] objectForKey:@"id"] intValue]]; - [request startAsynchronous]; - [requests addObject:request]; } - (void)loadOfflineStories { @@ -921,72 +912,41 @@ if (storiesCollection.inSearch && storiesCollection.searchQuery) { theFeedDetailURL = [NSString stringWithFormat:@"%@&query=%@", theFeedDetailURL, - [storiesCollection.searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + [storiesCollection.searchQuery stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]; } - [self cancelRequests]; - __weak ASIHTTPRequest *request = [self requestWithURL:theFeedDetailURL]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setUserInfo:@{@"feedPage": [NSNumber numberWithInt:storiesCollection.feedPage]}]; - [request setFailedBlock:^(void) { - if (request.isCancelled) { - NSLog(@"Cancelled"); - return; - } else { - self.isOnline = NO; - self.isShowingFetching = NO; -// storiesCollection.feedPage = 1; - [self loadOfflineStories]; - [self showOfflineNotifier]; - } - }]; - [request setCompletionBlock:^(void) { - [self finishedLoadingFeed:request]; +// [self cancelRequests]; + + + [appDelegate.networkManager GET:theFeedDetailURL parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { + [self finishedLoadingFeed:responseObject feedPage:storiesCollection.feedPage feedId:nil]; if (callback) { callback(); } + } failure:^(NSURLSessionTask *operation, NSError *error) { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)operation.response; + self.isOnline = NO; + self.isShowingFetching = NO; + // storiesCollection.feedPage = 1; + [self loadOfflineStories]; + [self showOfflineNotifier]; + if (httpResponse.statusCode == 503) { + [self informError:@"In maintenance mode"]; + self.pageFinished = YES; + } else if (httpResponse.statusCode >= 500) { + [self informError:@"The server barfed."]; + } }]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; } #pragma mark - #pragma mark Processing Stories -- (void)finishedLoadingFeed:(ASIHTTPRequest *)request { - if (request.isCancelled) { - NSLog(@"Cancelled"); - return; - } else if ([request responseStatusCode] >= 500 || [request responseStatusCode] == 404) { - self.isOnline = NO; - self.isShowingFetching = NO; -// storiesCollection.feedPage = 1; - [self loadOfflineStories]; - [self showOfflineNotifier]; - if ([request responseStatusCode] == 503) { - [self informError:@"In maintenance mode"]; - self.pageFinished = YES; - } else { - [self informError:@"The server barfed."]; - } - [self.storyTitlesTable reloadData]; - - return; - } +- (void)finishedLoadingFeed:(NSDictionary *)results feedPage:(NSInteger)feedPage feedId:(NSString *)sentFeedId { appDelegate.hasLoadedFeedDetail = YES; self.isOnline = YES; self.isShowingFetching = NO; - storiesCollection.feedPage = [[request.userInfo objectForKey:@"feedPage"] intValue]; - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; + // storiesCollection.feedPage = [[request.userInfo objectForKey:@"feedPage"] intValue]; // if (storiesCollection.isSavedView && // ![[results objectForKey:@"stories"] count] && @@ -994,15 +954,14 @@ // [results objectForKey:@"message"]) { // [self informError:nil details:[results objectForKey:@"message"]]; // } - id feedId = [results objectForKey:@"feed_id"]; - NSString *feedIdStr = [NSString stringWithFormat:@"%@",feedId]; + NSString *receivedFeedId = [NSString stringWithFormat:@"%@", [results objectForKey:@"feed_id"]]; if (!(storiesCollection.isRiverView || storiesCollection.isSavedView || storiesCollection.isReadView || storiesCollection.isSocialView || storiesCollection.isSocialRiverView) - && request.tag != [feedId intValue]) { + && ![receivedFeedId isEqualToString:sentFeedId]) { return; } if (storiesCollection.isSocialView || @@ -1028,7 +987,7 @@ [storiesCollection.activeClassifiers setObject:[newClassifiers objectForKey:key] forKey:key]; } } else if (newClassifiers) { - [storiesCollection.activeClassifiers setObject:newClassifiers forKey:feedIdStr]; + [storiesCollection.activeClassifiers setObject:newClassifiers forKey:receivedFeedId]; } storiesCollection.activePopularAuthors = [results objectForKey:@"feed_authors"]; storiesCollection.activePopularTags = [results objectForKey:@"feed_tags"]; @@ -1161,7 +1120,8 @@ NSMutableArray *storyImageUrls = [NSMutableArray array]; for (NSDictionary *story in newStories) { if ([story objectForKey:@"image_urls"] && [[story objectForKey:@"image_urls"] count]) { - [storyImageUrls addObject:[[story objectForKey:@"image_urls"] objectAtIndex:0]]; + [storyImageUrls addObject:[[[story objectForKey:@"image_urls"] objectAtIndex:0] + stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; } } [self performSelector:@selector(cacheStoryImages:) withObject:storyImageUrls afterDelay:0.2]; @@ -1791,9 +1751,8 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state - (void)markFeedsReadFromTimestamp:(NSInteger)cutoffTimestamp andOlder:(BOOL)older { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_feed_as_read", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; NSMutableArray *feedIds = [NSMutableArray array]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; if (storiesCollection.isRiverView) { if ([storiesCollection.activeFolder isEqual:@"everything"]) { @@ -1813,34 +1772,18 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state [feedIds addObject:[storiesCollection.activeFeed objectForKey:@"id"]]; } - for (id feedId in feedIds) { - [request addPostValue:feedId forKey:@"feed_id"]; - } - + [params setObject:feedIds forKey:@"feed_id"]; + [params setObject:@(cutoffTimestamp) forKey:@"cutoff_timestamp"]; NSString *direction = older ? @"older" : @"newest"; - [request setPostValue:@(cutoffTimestamp) forKey:@"cutoff_timestamp"]; - [request setPostValue:direction forKey:@"direction"]; - [request setDidFinishSelector:@selector(finishMarkOlderNewerAsRead:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setUserInfo:@{@"feeds" : feedIds, @"cutoffTimestamp" : @(cutoffTimestamp), @"older" : @(older)}]; - [request setDelegate:self]; - [request startAsynchronous]; -} - -- (void)finishMarkOlderNewerAsRead:(ASIFormDataRequest *)request { - if (request.responseStatusCode != 200) { - [self requestFailed:request]; - return; - } + [params setObject:direction forKey:@"direction"]; - if ([request.userInfo objectForKey:@"feeds"]) { - [appDelegate markFeedReadInCache:request.userInfo[@"feeds"] cutoffTimestamp:[request.userInfo[@"cutoffTimestamp"] integerValue] older:[request.userInfo[@"older"] boolValue]]; - } - - // is there a better way to refresh the detail view? - [self reloadStories]; -// [appDelegate reloadFeedsView:YES]; -// [appDelegate loadFeedDetailView]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) { + [appDelegate markFeedReadInCache:feedIds cutoffTimestamp:cutoffTimestamp older:older]; + // is there a better way to refresh the detail view? + [self reloadStories]; + } failure:^(NSURLSessionTask *operation, NSError *error) { + [self requestFailed:error]; + }]; } - (IBAction)doOpenMarkReadMenu:(id)sender { @@ -1925,16 +1868,6 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state [self.appDelegate.feedDetailMenuNavigationController pushViewController:viewController animated:YES]; } -- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { - if (alertView.tag == 1) { - // Rename - if (buttonIndex != alertView.cancelButtonIndex) { - NSString *newTitle = [[alertView textFieldAtIndex:0] text]; - [self renameTo:newTitle]; - } - } -} - - (void)renameTo:(NSString *)newTitle { [MBProgressHUD hideHUDForView:self.view animated:YES]; MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; @@ -1944,20 +1877,18 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state if (storiesCollection.isRiverView) { urlString = [NSString stringWithFormat:@"%@/reader/rename_folder", self.appDelegate.url]; } - NSURL *url = [NSURL URLWithString:urlString]; - - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setDelegate:self]; + + NSMutableDictionary *params = [NSMutableDictionary dictionary]; if (storiesCollection.isRiverView) { - [request addPostValue:[appDelegate extractFolderName:storiesCollection.activeFolder] forKey:@"folder_name"]; - [request addPostValue:[appDelegate extractParentFolderName:storiesCollection.activeFolder] forKey:@"in_folder"]; - [request addPostValue:newTitle forKey:@"new_folder_name"]; + [params setObject:[appDelegate extractFolderName:storiesCollection.activeFolder] forKey:@"folder_name"]; + [params setObject:[appDelegate extractParentFolderName:storiesCollection.activeFolder] forKey:@"in_folder"]; + [params setObject:newTitle forKey:@"new_folder_name"]; } else { - [request addPostValue:[storiesCollection.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; - [request addPostValue:newTitle forKey:@"feed_title"]; + [params setObject:[storiesCollection.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; + [params setObject:newTitle forKey:@"feed_title"]; } - [request setDidFailSelector:@selector(requestFailed:)]; - [request setCompletionBlock:^(void) { + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [appDelegate reloadFeedsView:YES]; if (storiesCollection.isRiverView) { [appDelegate renameFolder:newTitle]; @@ -1972,11 +1903,9 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state } [self.navigationController.view setNeedsDisplay]; [MBProgressHUD hideHUDForView:self.view animated:YES]; - + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; }]; - [request setTimeOutSeconds:30]; - [request setTag:[[storiesCollection.activeFeed objectForKey:@"id"] intValue]]; - [request startAsynchronous]; } - (void)deleteSite { @@ -1984,26 +1913,22 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.labelText = @"Deleting..."; - NSString *theFeedDetailURL = [NSString stringWithFormat:@"%@/reader/delete_feed", + NSString *urlString = [NSString stringWithFormat:@"%@/reader/delete_feed", self.appDelegate.url]; - NSURL *urlFeedDetail = [NSURL URLWithString:theFeedDetailURL]; - - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlFeedDetail]; - [request setDelegate:self]; - [request addPostValue:[storiesCollection.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; - [request addPostValue:[appDelegate extractFolderName:storiesCollection.activeFolder] forKey:@"in_folder"]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setCompletionBlock:^(void) { + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[storiesCollection.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; + [params setObject:[appDelegate extractFolderName:storiesCollection.activeFolder] forKey:@"in_folder"]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [appDelegate reloadFeedsView:YES]; - [appDelegate.navigationController - popToViewController:[appDelegate.navigationController.viewControllers - objectAtIndex:0] + [appDelegate.navigationController + popToViewController:[appDelegate.navigationController.viewControllers + objectAtIndex:0] animated:YES]; [MBProgressHUD hideHUDForView:self.view animated:YES]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; }]; - [request setTimeOutSeconds:30]; - [request setTag:[[storiesCollection.activeFeed objectForKey:@"id"] intValue]]; - [request startAsynchronous]; } - (void)deleteFolder { @@ -2011,28 +1936,25 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.labelText = @"Deleting..."; - NSString *theFeedDetailURL = [NSString stringWithFormat:@"%@/reader/delete_folder", + NSString *urlString = [NSString stringWithFormat:@"%@/reader/delete_folder", self.appDelegate.url]; - NSURL *urlFeedDetail = [NSURL URLWithString:theFeedDetailURL]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[appDelegate extractFolderName:storiesCollection.activeFolder] + forKey:@"folder_to_delete"]; + [params setObject:[appDelegate extractFolderName:[appDelegate + extractParentFolderName:storiesCollection.activeFolder]] + forKey:@"in_folder"]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlFeedDetail]; - [request setDelegate:self]; - [request addPostValue:[appDelegate extractFolderName:storiesCollection.activeFolder] - forKey:@"folder_to_delete"]; - [request addPostValue:[appDelegate extractFolderName:[appDelegate - extractParentFolderName:storiesCollection.activeFolder]] - forKey:@"in_folder"]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setCompletionBlock:^(void) { + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [appDelegate reloadFeedsView:YES]; - [appDelegate.navigationController - popToViewController:[appDelegate.navigationController.viewControllers - objectAtIndex:0] + [appDelegate.navigationController + popToViewController:[appDelegate.navigationController.viewControllers + objectAtIndex:0] animated:YES]; [MBProgressHUD hideHUDForView:self.view animated:YES]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; }]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; } - (void)muteSite { @@ -2044,80 +1966,59 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state NSString *thisIdentifier = [NSString stringWithFormat:@"%@", storiesCollection.activeFeed[@"id"]]; [activeIdentifiers removeObject:thisIdentifier]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; NSString *urlString = [NSString stringWithFormat:@"%@/reader/save_feed_chooser", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - for (id identifier in activeIdentifiers) { - [request addPostValue:identifier forKey:@"approved_feeds"]; - } - [request setCompletionBlock:^(void) { + + [params setObject:activeIdentifiers forKey:@"approved_feeds"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [self.appDelegate reloadFeedsView:YES]; [self.appDelegate.navigationController popToViewController:[appDelegate.navigationController.viewControllers objectAtIndex:0] - animated:YES]; + animated:YES]; [MBProgressHUD hideHUDForView:self.view animated:YES]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; }]; - request.didFailSelector = @selector(requestFailed:); - request.timeOutSeconds = 30; - [request startAsynchronous]; } - (void)performMoveToFolder:(id)toFolder { [MBProgressHUD hideHUDForView:self.view animated:YES]; MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; - ASIFormDataRequest *request = nil; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + NSString *urlString; if (self.appDelegate.storiesCollection.isRiverView) { HUD.labelText = @"Moving folder..."; - NSString *urlString = [NSString stringWithFormat:@"%@/reader/move_folder_to_folder", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - request = [ASIFormDataRequest requestWithURL:url]; + urlString = [NSString stringWithFormat:@"%@/reader/move_folder_to_folder", self.appDelegate.url]; NSString *activeFolder = self.appDelegate.storiesCollection.activeFolder; NSString *parentFolderName = [self.appDelegate extractParentFolderName:activeFolder]; NSString *fromFolder = [self.appDelegate extractFolderName:parentFolderName]; NSString *toFolderIdentifier = [self.appDelegate extractFolderName:toFolder]; NSString *folderName = [self.appDelegate extractFolderName:activeFolder]; - [request setPostValue:fromFolder forKey:@"in_folder"]; - [request setPostValue:toFolderIdentifier forKey:@"to_folder"]; - [request setPostValue:folderName forKey:@"folder_name"]; + [params setObject:fromFolder forKey:@"in_folder"]; + [params setObject:toFolderIdentifier forKey:@"to_folder"]; + [params setObject:folderName forKey:@"folder_name"]; } else { HUD.labelText = @"Moving site..."; - NSString *urlString = [NSString stringWithFormat:@"%@/reader/move_feed_to_folder", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - request = [ASIFormDataRequest requestWithURL:url]; + urlString = [NSString stringWithFormat:@"%@/reader/move_feed_to_folder", self.appDelegate.url]; NSString *fromFolder = [self.appDelegate extractFolderName:self.appDelegate.storiesCollection.activeFolder]; NSString *toFolderIdentifier = [self.appDelegate extractFolderName:toFolder]; NSString *feedIdentifier = [self.appDelegate.storiesCollection.activeFeed objectForKey:@"id"]; - [request setPostValue:fromFolder forKey:@"in_folder"]; - [request setPostValue:toFolderIdentifier forKey:@"to_folder"]; - [request setPostValue:feedIdentifier forKey:@"feed_id"]; + [params setObject:fromFolder forKey:@"in_folder"]; + [params setObject:toFolderIdentifier forKey:@"to_folder"]; + [params setObject:feedIdentifier forKey:@"feed_id"]; } - [request setDelegate:self]; - [request setDidFinishSelector:@selector(moveToFolderFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setUserInfo:@{@"toFolder" : toFolder}]; - [request startAsynchronous]; -} - -- (void)moveToFolderFinished:(ASIHTTPRequest *)request { - if ([request responseStatusCode] >= 500) { - return [self requestFailed:request]; - } - - [MBProgressHUD hideHUDForView:self.view animated:YES]; - - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - int code = [[results valueForKey:@"code"] intValue]; - if (code != -1) { - self.appDelegate.storiesCollection.activeFolder = request.userInfo[@"toFolder"]; - [self.appDelegate reloadFeedsView:NO]; - } + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [MBProgressHUD hideHUDForView:self.view animated:YES]; + + int code = [[responseObject valueForKey:@"code"] intValue]; + if (code != -1) { + self.appDelegate.storiesCollection.activeFolder = toFolder; + [self.appDelegate reloadFeedsView:NO]; + } + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } - (void)openMoveView { @@ -2168,17 +2069,29 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state [appDelegate extractFolderName:appDelegate.storiesCollection.activeFolder] : [appDelegate.storiesCollection.activeFeed objectForKey:@"feed_title"]]; NSString *subtitle = (appDelegate.storiesCollection.isRiverView ? nil : [appDelegate.storiesCollection.activeFeed objectForKey:@"feed_address"]); - UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title - message:subtitle - delegate:self - cancelButtonTitle:@"Cancel" - otherButtonTitles:@"Rename", nil]; - [alertView setTag:1]; - [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput]; - [[alertView textFieldAtIndex:0] setText:appDelegate.storiesCollection.isRiverView ? - [appDelegate extractFolderName:appDelegate.storiesCollection.activeFolder] : - [appDelegate.storiesCollection.activeFeed objectForKey:@"feed_title"]]; - [alertView show]; + UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:subtitle preferredStyle:UIAlertControllerStyleAlert]; + [alert setModalPresentationStyle:UIModalPresentationPopover]; + UIAlertAction *rename = [UIAlertAction actionWithTitle:@"Rename" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + NSString *newTitle = alert.textFields[0].text; + [self renameTo:newTitle]; + }]; + UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { + + }]; + [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { + textField.text = appDelegate.storiesCollection.isRiverView ? + [appDelegate extractFolderName:appDelegate.storiesCollection.activeFolder] : + [appDelegate.storiesCollection.activeFeed objectForKey:@"feed_title"]; + }]; + [alert addAction:rename]; + [alert addAction:cancel]; + + if (self.presentedViewController) { + [self.presentedViewController dismissViewControllerAnimated:NO completion:^{ + [self presentViewController:alert animated:YES completion:nil]; + }]; + } +// [self.appDelegate showAlert:alert withViewController:self]; } - (void)showUserProfile { @@ -2248,27 +2161,27 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state #pragma mark - #pragma mark Story Actions - save -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request { +- (void)finishMarkAsSaved:(NSDictionary *)params { } -- (void)failedMarkAsSaved:(ASIFormDataRequest *)request { +- (void)failedMarkAsSaved:(NSDictionary *)params { [self informError:@"Failed to save story"]; [self.storyTitlesTable reloadData]; } -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request { +- (void)finishMarkAsUnsaved:(NSDictionary *)params { } -- (void)failedMarkAsUnsaved:(ASIFormDataRequest *)request { +- (void)failedMarkAsUnsaved:(NSDictionary *)params { [self informError:@"Failed to unsave story"]; [self.storyTitlesTable reloadData]; } -- (void)failedMarkAsUnread:(ASIFormDataRequest *)request { +- (void)failedMarkAsUnread:(NSDictionary *)params { [self informError:@"Failed to unread story"]; [self.storyTitlesTable reloadData]; @@ -2284,16 +2197,14 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state stringWithFormat:@"%@/reader/refresh_feed/%@", self.appDelegate.url, [storiesCollection.activeFeed objectForKey:@"id"]]; - [self cancelRequests]; - ASIHTTPRequest *request = [self requestWithURL:urlString]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setDidFinishSelector:@selector(finishedRefreshingFeed:)]; - [request setDidFailSelector:@selector(failRefreshingFeed:)]; - [request setTimeOutSeconds:60]; - [request startAsynchronous]; +// [self cancelRequests]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { + [self renderStories:[responseObject objectForKey:@"stories"]]; + } failure:^(NSURLSessionTask *operation, NSError *error) { + NSLog(@"Fail: %@", error); + [self informError:[operation error]]; + [self fetchFeedDetail:1 withCallback:nil]; + }]; [storiesCollection setStories:nil]; storiesCollection.feedPage = 1; @@ -2302,24 +2213,6 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state [storyTitlesTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; } -- (void)finishedRefreshingFeed:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - - [self renderStories:[results objectForKey:@"stories"]]; -} - -- (void)failRefreshingFeed:(ASIHTTPRequest *)request { - NSLog(@"Fail: %@", request); - [self informError:[request error]]; - [self fetchFeedDetail:1 withCallback:nil]; -} - #pragma mark - #pragma mark loadSocial Feeds @@ -2336,25 +2229,15 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state NSString *urlString = [NSString stringWithFormat:@"%@/reader/favicons%@", self.appDelegate.url, feedIdsQuery]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setDidFinishSelector:@selector(saveAndDrawFavicons:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setDelegate:self]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self saveAndDrawFavicons:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)saveAndDrawFavicons:(ASIHTTPRequest *)request { - - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - +- (void)saveAndDrawFavicons:(NSDictionary *)results { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0ul); dispatch_async(queue, ^{ for (id feed_id in results) { @@ -2377,9 +2260,8 @@ didEndSwipingSwipingWithState:(MCSwipeTableViewCellState)state } -- (void)requestFailed:(ASIHTTPRequest *)request { +- (void)requestFailed:(NSError *)error { [MBProgressHUD hideHUDForView:self.view animated:YES]; - NSError *error = [request error]; NSLog(@"Error: %@", error); [appDelegate informError:error]; } diff --git a/clients/ios/Classes/FeedsMenuViewController.h b/clients/ios/Classes/FeedsMenuViewController.h index 535a6864c..92a84f0eb 100644 --- a/clients/ios/Classes/FeedsMenuViewController.h +++ b/clients/ios/Classes/FeedsMenuViewController.h @@ -7,19 +7,18 @@ // #import +#import "NewsBlurAppDelegate.h" @class NewsBlurAppDelegate; -@interface FeedsMenuViewController : UIViewController - { +@interface FeedsMenuViewController : BaseViewController + { NewsBlurAppDelegate *appDelegate; - UIAlertView *loginAsAlert; } @property (nonatomic, strong) NSArray *menuOptions; @property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate; @property (nonatomic) IBOutlet UITableView *menuTableView; -@property (nonatomic) IBOutlet UIAlertView *loginAsAlert; @property (nonatomic) IBOutlet UISegmentedControl *themeSegmentedControl; - (IBAction)changeTheme:(id)sender; diff --git a/clients/ios/Classes/FeedsMenuViewController.m b/clients/ios/Classes/FeedsMenuViewController.m index 731a852b8..050d3401f 100644 --- a/clients/ios/Classes/FeedsMenuViewController.m +++ b/clients/ios/Classes/FeedsMenuViewController.m @@ -20,7 +20,7 @@ @synthesize appDelegate; @synthesize menuOptions; @synthesize menuTableView; -@synthesize loginAsAlert; + - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { @@ -170,6 +170,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [self.appDelegate hidePopover]; + } else { + [self.appDelegate hidePopoverAnimated:YES]; + } + switch (indexPath.row) { case 0: [appDelegate showPreferences]; @@ -203,11 +209,6 @@ break; } - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [self.appDelegate hidePopover]; - } else { - [self.appDelegate hidePopoverAnimated:YES]; - } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } @@ -216,46 +217,29 @@ #pragma mark Menu Options - (void)showLoginAsDialog { - loginAsAlert = [[UIAlertView alloc] initWithTitle:@"Login as..." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil]; - loginAsAlert.alertViewStyle = UIAlertViewStylePlainTextInput; - UITextField * alertTextField = [loginAsAlert textFieldAtIndex:0]; - alertTextField.keyboardType = UIKeyboardTypeAlphabet; - alertTextField.placeholder = @"Username"; - [loginAsAlert show]; -} + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Login as..." message:nil preferredStyle:UIAlertControllerStyleAlert]; + [alertController addTextFieldWithConfigurationHandler:nil]; + [alertController addAction:[UIAlertAction actionWithTitle: @"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { + [alertController dismissViewControllerAnimated:YES completion:nil]; + NSString *username = alertController.textFields[0].text; + NSString *urlString = [NSString stringWithFormat:@"%@/reader/login_as?user=%@", + self.appDelegate.url, username]; -- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { - UITextField * alertTextField = [loginAsAlert textFieldAtIndex:0]; - if ([alertTextField.text length] <= 0 || buttonIndex == 0){ - return; - } - if (buttonIndex == 1) { - NSString *urlS = [NSString stringWithFormat:@"%@/reader/login_as?user=%@", - self.appDelegate.url, alertTextField.text]; - NSURL *url = [NSURL URLWithString:urlS]; - - __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setFailedBlock:^(void) { - [MBProgressHUD hideHUDForView:self.view animated:YES]; - }]; - [request setCompletionBlock:^(void) { - NSLog(@"Login as %@ successful", alertTextField.text); + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSLog(@"Login as %@ successful", username); [MBProgressHUD hideHUDForView:self.view animated:YES]; [appDelegate reloadFeedsView:YES]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; }]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; - - [ASIHTTPRequest setSessionCookies:nil]; [MBProgressHUD hideHUDForView:self.view animated:YES]; MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:appDelegate.feedsViewController.view animated:YES]; - HUD.labelText = [NSString stringWithFormat:@"Login: %@", alertTextField.text]; - } + HUD.labelText = [NSString stringWithFormat:@"Login: %@", username]; + }]]; + [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" + style:UIAlertActionStyleCancel handler:nil]]; + [appDelegate.feedsViewController presentViewController:alertController animated:YES completion:nil]; } #pragma mark - Theme Options diff --git a/clients/ios/Classes/FirstTimeUserAddFriendsViewController.h b/clients/ios/Classes/FirstTimeUserAddFriendsViewController.h index e1abc8850..44dbc5f5e 100644 --- a/clients/ios/Classes/FirstTimeUserAddFriendsViewController.h +++ b/clients/ios/Classes/FirstTimeUserAddFriendsViewController.h @@ -10,9 +10,7 @@ #import "NewsBlurAppDelegate.h" -@class ASIHTTPRequest; - -@interface FirstTimeUserAddFriendsViewController : UIViewController { +@interface FirstTimeUserAddFriendsViewController : BaseViewController { NewsBlurAppDelegate *appDelegate; } @@ -32,14 +30,10 @@ - (IBAction)toggleAutoFollowFriends:(id)sender; - (void)connectToSocial; -- (void)finishConnectFromSocial:(ASIHTTPRequest *)request; - (void)finishTwitterConnect; - (void)finishFacebookConnect; -- (void)finishedWithError:(ASIHTTPRequest *)request; -- (void)finishToggleAutoFollowFriends:(ASIHTTPRequest *)request; - - (void)changeMessaging:(NSString *)msg; -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/FirstTimeUserAddFriendsViewController.m b/clients/ios/Classes/FirstTimeUserAddFriendsViewController.m index 38e1f05b1..1499ea6ab 100644 --- a/clients/ios/Classes/FirstTimeUserAddFriendsViewController.m +++ b/clients/ios/Classes/FirstTimeUserAddFriendsViewController.m @@ -9,7 +9,6 @@ #import "FirstTimeUserAddFriendsViewController.h" #import "FirstTimeUserAddNewsBlurViewController.h" #import "AuthorizeServicesViewController.h" -#import "ASIHTTPRequest.h" @interface FirstTimeUserAddFriendsViewController () @@ -116,30 +115,20 @@ - (void)connectToSocial { NSString *urlString = [NSString stringWithFormat:@"%@/social/load_user_friends", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishConnectFromSocial:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishConnectFromSocial:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; +- (void)requestFailed:(NSError *)error { NSLog(@"Error: %@", error); - [appDelegate informError:error]; + [self informError:error]; } -- (void)finishConnectFromSocial:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - NSLog(@"results are %@", results); +- (void)finishConnectFromSocial:(NSDictionary *)results { + NSLog(@"Connect to social results: %@", results); BOOL facebookSync = [[[[results objectForKey:@"services"] objectForKey:@"facebook"] objectForKey:@"syncing"] boolValue]; BOOL twitterSync = [[[[results objectForKey:@"services"] objectForKey:@"twitter"] objectForKey:@"syncing"] boolValue]; @@ -197,49 +186,23 @@ - (IBAction)toggleAutoFollowFriends:(id)sender { UISwitch *button = (UISwitch *)sender; - NSURL *preferenceURL = [NSURL URLWithString: - [NSString stringWithFormat:@"%@/profile/set_preference", - self.appDelegate.url]]; - - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:preferenceURL]; - [[NSHTTPCookieStorage sharedHTTPCookieStorage] - setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; - + NSString *urlString = [NSString stringWithFormat:@"%@/profile/set_preference", self.appDelegate.url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + if (button.on) { - [request setPostValue:@"false" forKey:@"autofollow_friends"]; + [params setObject:@"false" forKey:@"autofollow_friends"]; } else { - [request setPostValue:@"true" forKey:@"autofollow_friends"]; + [params setObject:@"true" forKey:@"autofollow_friends"]; } - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setDidFinishSelector:@selector(finishToggleAutoFollowFriends:)]; - [request setDidFailSelector:@selector(finishedWithError:)]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishToggleAutoFollowFriends:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)finishedWithError:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - NSLog(@"results are %@", results); - -} - -- (void)finishToggleAutoFollowFriends:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; +- (void)finishToggleAutoFollowFriends:(NSDictionary *)results { NSLog(@"results are %@", results); } diff --git a/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.h b/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.h index 74e8f0ad3..864387655 100644 --- a/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.h +++ b/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.h @@ -7,10 +7,9 @@ // #import - #import "NewsBlurAppDelegate.h" -@interface FirstTimeUserAddNewsBlurViewController : UIViewController { +@interface FirstTimeUserAddNewsBlurViewController : BaseViewController { NewsBlurAppDelegate *appDelegate; } @@ -22,8 +21,6 @@ - (IBAction)tapNewsBlurButton:(id)sender; - (IBAction)tapPopularButton:(id)sender; -- (void)finishAddSite:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)addSite:(NSString *)siteUrl; - (void)addPopular; -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.m b/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.m index a4ae48776..7d1402b8b 100644 --- a/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.m +++ b/clients/ios/Classes/FirstTimeUserAddNewsBlurViewController.m @@ -112,45 +112,34 @@ - (void)addPopular { NSString *urlString = [NSString stringWithFormat:@"%@/social/follow/", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; - [request setPostValue:@"social:popular" forKey:@"user_id"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishAddSite:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [params setObject:@"social:popular" forKey:@"user_id"]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishAddSite:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } - (void)addSite:(NSString *)siteUrl { NSString *urlString = [NSString stringWithFormat:@"%@/reader/add_url/", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - - [request setPostValue:siteUrl forKey:@"url"]; - [request setPostValue:@"true" forKey:@"auto_active"]; - [request setPostValue:@"true" forKey:@"skip_fetch"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishAddSite:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:siteUrl forKey:@"url"]; + [params setObject:@"true" forKey:@"auto_active"]; + [params setObject:@"true" forKey:@"skip_fetch"]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishAddSite:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [appDelegate informError:error]; -} - -- (void)finishAddSite:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; +- (void)finishAddSite:(NSDictionary *)results { NSLog(@"results are %@", results); } diff --git a/clients/ios/Classes/FirstTimeUserAddSitesViewController.h b/clients/ios/Classes/FirstTimeUserAddSitesViewController.h index 50a7b9bd3..ef936b3b9 100644 --- a/clients/ios/Classes/FirstTimeUserAddSitesViewController.h +++ b/clients/ios/Classes/FirstTimeUserAddSitesViewController.h @@ -9,7 +9,8 @@ #import #import "NewsBlurAppDelegate.h" -@interface FirstTimeUserAddSitesViewController : UIViewController { +@interface FirstTimeUserAddSitesViewController : BaseViewController + { NewsBlurAppDelegate *appDelegate; } @@ -25,9 +26,7 @@ - (void)tapGoogleReaderButton; - (void)addCategory:(id)sender; -- (void)importFromGoogleReader; -- (void)importFromGoogleReaderFailed:(NSString *)error; - (void)updateSites; - (CGFloat)tableViewHeight; -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/FirstTimeUserAddSitesViewController.m b/clients/ios/Classes/FirstTimeUserAddSitesViewController.m index 3559f26d0..9aa2dcf9f 100644 --- a/clients/ios/Classes/FirstTimeUserAddSitesViewController.m +++ b/clients/ios/Classes/FirstTimeUserAddSitesViewController.m @@ -114,31 +114,22 @@ if (self.selectedCategories_.count) { NSString *urlString = [NSString stringWithFormat:@"%@/categories/subscribe", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + NSMutableArray *categories = [NSMutableArray array]; - for(NSObject *category in self.selectedCategories_) { - [request addPostValue:category forKey:@"category"]; + for (NSObject *category in self.selectedCategories_) { + [categories addObject:category]; } + [params setObject:categories forKey:@"category"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishAddingCategories:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } } -- (void)finishAddingCategories:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - NSLog(@"results are %@", results); -} - #pragma mark - #pragma mark Import Google Reader @@ -163,29 +154,22 @@ [self.activityIndicator startAnimating]; NSString *urlString = [NSString stringWithFormat:@"%@/import/import_from_google_reader/", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:@"true" forKey:@"auto_active"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishImportFromGoogleReader:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:@"true" forKey:@"auto_active"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishImportFromGoogleReader:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self importFromGoogleReaderFailed:error]; + }]; } -- (void)importFromGoogleReaderFailed:(NSString *)error { +- (void)importFromGoogleReaderFailed:(NSError *)error { [self.googleReaderButton setTitle:@"Retry Google Reader" forState:UIControlStateNormal]; self.instructionLabel.textColor = [UIColor redColor]; - self.instructionLabel.text = error; + self.instructionLabel.text = error.localizedDescription; } -- (void)finishImportFromGoogleReader:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; +- (void)finishImportFromGoogleReader:(NSDictionary *)results { NSLog(@"results are %@", results); self.importedFeedCount_ = [[results objectForKey:@"feed_count"] intValue]; @@ -242,31 +226,20 @@ [self.categoriesTable reloadData]; } -- (void)finishAddFolder:(ASIHTTPRequest *)request { - NSLog(@"Successfully added."); -} - -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [appDelegate informError:error]; -} - #pragma mark - #pragma mark Add Site - (void)addSite:(NSString *)siteUrl { NSString *urlString = [NSString stringWithFormat:@"%@/reader/add_url", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:siteUrl forKey:@"url"]; - [request setPostValue:siteUrl forKey:@"url"]; - - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishAddFolder:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } #pragma mark - diff --git a/clients/ios/Classes/FirstTimeUserViewController.m b/clients/ios/Classes/FirstTimeUserViewController.m index b965fde9a..5b6abf131 100644 --- a/clients/ios/Classes/FirstTimeUserViewController.m +++ b/clients/ios/Classes/FirstTimeUserViewController.m @@ -8,7 +8,6 @@ #import "FirstTimeUserViewController.h" #import "NewsBlurAppDelegate.h" -#import "ASIHTTPRequest.h" #import "FirstTimeUserAddSitesViewController.h" #import diff --git a/clients/ios/Classes/FolderTitleView.m b/clients/ios/Classes/FolderTitleView.m index d6b35c247..9df368d27 100644 --- a/clients/ios/Classes/FolderTitleView.m +++ b/clients/ios/Classes/FolderTitleView.m @@ -47,7 +47,7 @@ } NSString *collapseKey = [NSString stringWithFormat:@"folderCollapsed:%@", folderName]; bool isFolderCollapsed = [userPreferences boolForKey:collapseKey]; - int countWidth = 0; + NSInteger countWidth = 0; NSString *accessibilityCount = @""; if ([folderName isEqual:@"saved_stories"]) { diff --git a/clients/ios/Classes/FontListViewController.m b/clients/ios/Classes/FontListViewController.m index 6e4a939bd..67046c4bf 100644 --- a/clients/ios/Classes/FontListViewController.m +++ b/clients/ios/Classes/FontListViewController.m @@ -66,7 +66,7 @@ NSString *fontStyle = [[NSUserDefaults standardUserDefaults] stringForKey:@"fontStyle"]; if (!fontStyle) { - fontStyle = @"NB-helvetica"; + fontStyle = @"GothamNarrow-Book"; } if ([font[@"style"] isEqualToString:fontStyle]) { diff --git a/clients/ios/Classes/FontSettingsViewController.m b/clients/ios/Classes/FontSettingsViewController.m index aa22df49b..8f9999158 100644 --- a/clients/ios/Classes/FontSettingsViewController.m +++ b/clients/ios/Classes/FontSettingsViewController.m @@ -276,7 +276,7 @@ } else if (indexPath.row == 5) { NSString *fontStyle = [[NSUserDefaults standardUserDefaults] stringForKey:@"fontStyle"]; if (!fontStyle) { - fontStyle = @"NB-helvetica"; + fontStyle = @"GothamNarrow-Book"; } NSUInteger idx = [self.fonts indexOfObjectPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) { return [obj[@"style"] isEqualToString:fontStyle]; diff --git a/clients/ios/Classes/FriendsListViewController.h b/clients/ios/Classes/FriendsListViewController.h index bc5e63095..00654d71d 100644 --- a/clients/ios/Classes/FriendsListViewController.h +++ b/clients/ios/Classes/FriendsListViewController.h @@ -7,11 +7,11 @@ // #import +#import "NewsBlurAppDelegate.h" @class NewsBlurAppDelegate; -@class ASIHTTPRequest; -@interface FriendsListViewController : UIViewController { +@interface FriendsListViewController : BaseViewController { NewsBlurAppDelegate *appDelegate; UISearchBar *friendSearchBar; UITableView *friendsTable; @@ -29,9 +29,6 @@ - (void)doCancelButton; - (void)loadFriendsList:(NSString *)query; -- (void)requestFinished:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)loadSuggestedFriendsList; -- (void)loadSuggestedFriendsListFinished:(ASIHTTPRequest *)request; - (void)hideUserProfileModal; @end diff --git a/clients/ios/Classes/FriendsListViewController.m b/clients/ios/Classes/FriendsListViewController.m index 8a2c4adca..c930ff9d8 100644 --- a/clients/ios/Classes/FriendsListViewController.m +++ b/clients/ios/Classes/FriendsListViewController.m @@ -9,7 +9,6 @@ #import "FriendsListViewController.h" #import "NewsBlurAppDelegate.h" #import "UserProfileViewController.h" -#import "ASIHTTPRequest.h" #import "ProfileBadge.h" #import "MBProgressHUD.h" #import "UISearchBar+Field.h" @@ -119,57 +118,36 @@ NSString *urlString = [NSString stringWithFormat:@"%@/social/find_friends?query=%@&limit=10", self.appDelegate.url, query]; - NSURL *url = [NSURL URLWithString:urlString]; - - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(requestFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self requestFinished:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } - (void)loadSuggestedFriendsList { NSString *urlString = [NSString stringWithFormat:@"%@/social/load_user_friends", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(loadSuggestedFriendsListFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self loadSuggestedFriendsListFinished:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } -- (void)loadSuggestedFriendsListFinished:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData= [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; +- (void)loadSuggestedFriendsListFinished:(NSDictionary *)results { int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { return; } + self.suggestedUserProfiles = [results objectForKey:@"recommended_users"]; [self.friendsTable reloadData]; } -- (void)requestFinished:(ASIHTTPRequest *)request { +- (void)requestFinished:(NSDictionary *)results { if (self.inSearch_) { [MBProgressHUD hideHUDForView:self.view animated:YES]; - NSString *responseString = [request responseString]; - NSData *responseData= [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { return; @@ -177,18 +155,11 @@ self.userProfiles = [results objectForKey:@"profiles"]; - [self.friendsTable reloadData]; } } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [appDelegate informError:error]; -} - - (BOOL)disablesAutomaticKeyboardDismissal { return NO; } diff --git a/clients/ios/Classes/InteractionsModule.h b/clients/ios/Classes/InteractionsModule.h index 8103b5250..8b01eade3 100644 --- a/clients/ios/Classes/InteractionsModule.h +++ b/clients/ios/Classes/InteractionsModule.h @@ -7,15 +7,14 @@ // #import +#import "NewsBlurAppDelegate.h" @class NewsBlurAppDelegate; -@class ASIHTTPRequest; @interface InteractionsModule : UIView { NewsBlurAppDelegate *appDelegate; UITableView *interactionsTable; NSMutableArray *interactionsArray; - UIPopoverController *popoverController; BOOL pageFetching; BOOL pageFinished; @@ -25,7 +24,6 @@ @property (nonatomic) NewsBlurAppDelegate *appDelegate; @property (nonatomic, strong) UITableView *interactionsTable; @property (nonatomic) NSArray *interactionsArray; -@property (nonatomic, strong) UIPopoverController *popoverController; @property (nonatomic, readwrite) BOOL pageFetching; @property (nonatomic, readwrite) BOOL pageFinished; @@ -34,9 +32,7 @@ - (void)refreshWithInteractions:(NSArray *)interactions; - (void)fetchInteractionsDetail:(int)page; -- (void)finishLoadInteractions:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)checkScroll; -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/InteractionsModule.m b/clients/ios/Classes/InteractionsModule.m index 495b63016..0a5e9a0a5 100644 --- a/clients/ios/Classes/InteractionsModule.m +++ b/clients/ios/Classes/InteractionsModule.m @@ -11,7 +11,6 @@ #import "InteractionCell.h" #import "SmallInteractionCell.h" #import -#import "ASIHTTPRequest.h" #import "UserProfileViewController.h" #import "DashboardViewController.h" @@ -23,7 +22,6 @@ @synthesize appDelegate; @synthesize interactionsTable; @synthesize interactionsArray; -@synthesize popoverController; @synthesize pageFetching; @synthesize pageFinished; @synthesize interactionsPage; @@ -32,7 +30,7 @@ { self = [super initWithFrame:frame]; if (self) { - + appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate]; } return self; } @@ -75,8 +73,6 @@ #pragma mark Get Interactions - (void)fetchInteractionsDetail:(int)page { - self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate]; - // if there is no social profile, we are DONE // if ([[appDelegate.dictSocialProfile allKeys] count] == 0) { // self.pageFinished = YES; @@ -105,26 +101,16 @@ [appDelegate.dictSocialProfile objectForKey:@"user_id"], page]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - - [request setDidFinishSelector:@selector(finishLoadInteractions:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setDelegate:self]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishLoadInteractions:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [appDelegate informError:error]; + }]; } } -- (void)finishLoadInteractions:(ASIHTTPRequest *)request { +- (void)finishLoadInteractions:(NSDictionary *)results { self.pageFetching = NO; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; NSArray *newInteractions = [results objectForKey:@"interactions"]; @@ -158,12 +144,6 @@ [self refreshWithInteractions:appDelegate.userInteractionsArray]; } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [appDelegate informError:error]; -} - #pragma mark - #pragma mark Table View - Interactions List @@ -333,4 +313,4 @@ [self checkScroll]; } -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/LoginViewController.h b/clients/ios/Classes/LoginViewController.h index 439a6d2f7..c6aa3150a 100644 --- a/clients/ios/Classes/LoginViewController.h +++ b/clients/ios/Classes/LoginViewController.h @@ -8,12 +8,10 @@ #import #import "NewsBlurAppDelegate.h" -#import "ASIHTTPRequest.h" #define LANDSCAPE_MARGIN 128 -@interface LoginViewController : UIViewController - { +@interface LoginViewController : BaseViewController { NewsBlurAppDelegate *appDelegate; BOOL isOnSignUpScreen; diff --git a/clients/ios/Classes/LoginViewController.m b/clients/ios/Classes/LoginViewController.m index ace73624e..b4a600307 100644 --- a/clients/ios/Classes/LoginViewController.m +++ b/clients/ios/Classes/LoginViewController.m @@ -7,7 +7,6 @@ // #import "LoginViewController.h" -#import "ASIFormDataRequest.h" #import "../Other Sources/OnePasswordExtension/OnePasswordExtension.h" //#import @@ -199,46 +198,35 @@ NSString *urlString = [NSString stringWithFormat:@"%@/api/login", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:[usernameInput text] forKey:@"username"]; - [request setPostValue:[passwordInput text] forKey:@"password"]; - [request setPostValue:@"login" forKey:@"submit"]; - [request setPostValue:@"1" forKey:@"api"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(requestFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; -} - - -- (void)requestFinished:(ASIHTTPRequest *)request { - [MBProgressHUD hideHUDForView:self.view animated:YES]; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; - int code = [[results valueForKey:@"code"] intValue]; - if (code == -1) { - NSDictionary *errors = [results valueForKey:@"errors"]; - if ([errors valueForKey:@"username"]) { - [self showError:[[errors valueForKey:@"username"] firstObject]]; - } else if ([errors valueForKey:@"__all__"]) { - [self showError:[[errors valueForKey:@"__all__"] firstObject]]; - } - } else { - [self.passwordInput setText:@""]; - [self.signUpPasswordInput setText:@""]; - [appDelegate reloadFeedsView:YES]; - [self dismissViewControllerAnimated:YES completion:nil]; - } + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[usernameInput text] forKey:@"username"]; + [params setObject:[passwordInput text] forKey:@"password"]; + [params setObject:@"login" forKey:@"submit"]; + [params setObject:@"1" forKey:@"api"]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [MBProgressHUD hideHUDForView:self.view animated:YES]; + + int code = [[responseObject valueForKey:@"code"] intValue]; + if (code == -1) { + NSDictionary *errors = [responseObject valueForKey:@"errors"]; + if ([errors valueForKey:@"username"]) { + [self showError:[[errors valueForKey:@"username"] firstObject]]; + } else if ([errors valueForKey:@"__all__"]) { + [self showError:[[errors valueForKey:@"__all__"] firstObject]]; + } + } else { + [self.passwordInput setText:@""]; + [self.signUpPasswordInput setText:@""]; + [appDelegate reloadFeedsView:YES]; + [self dismissViewControllerAnimated:YES completion:nil]; + } + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } @@ -249,59 +237,48 @@ [self showError:nil]; NSString *urlString = [NSString stringWithFormat:@"%@/api/signup", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + + NSMutableDictionary *params = [NSMutableDictionary dictionary]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [request setPostValue:[signUpUsernameInput text] forKey:@"username"]; - [request setPostValue:[signUpPasswordInput text] forKey:@"password"]; + [params setObject:[signUpUsernameInput text] forKey:@"username"]; + [params setObject:[signUpPasswordInput text] forKey:@"password"]; } else { - [request setPostValue:[usernameInput text] forKey:@"username"]; - [request setPostValue:[passwordInput text] forKey:@"password"]; + [params setObject:[usernameInput text] forKey:@"username"]; + [params setObject:[passwordInput text] forKey:@"password"]; } - [request setPostValue:[emailInput text] forKey:@"email"]; - [request setPostValue:@"login" forKey:@"submit"]; - [request setPostValue:@"1" forKey:@"api"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishRegistering:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; -} - -- (void)finishRegistering:(ASIHTTPRequest *)request { - [MBProgressHUD hideHUDForView:self.view animated:YES]; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; + [params setObject:[emailInput text] forKey:@"email"]; + [params setObject:@"login" forKey:@"submit"]; + [params setObject:@"1" forKey:@"api"]; - int code = [[results valueForKey:@"code"] intValue]; - if (code == -1) { - NSDictionary *errors = [results valueForKey:@"errors"]; - if ([errors valueForKey:@"email"]) { - [self showError:[[errors valueForKey:@"email"] objectAtIndex:0]]; - } else if ([errors valueForKey:@"username"]) { - [self showError:[[errors valueForKey:@"username"] objectAtIndex:0]]; - } else if ([errors valueForKey:@"__all__"]) { - [self showError:[[errors valueForKey:@"__all__"] objectAtIndex:0]]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [MBProgressHUD hideHUDForView:self.view animated:YES]; + + int code = [[responseObject valueForKey:@"code"] intValue]; + if (code == -1) { + NSDictionary *errors = [responseObject valueForKey:@"errors"]; + if ([errors valueForKey:@"email"]) { + [self showError:[[errors valueForKey:@"email"] objectAtIndex:0]]; + } else if ([errors valueForKey:@"username"]) { + [self showError:[[errors valueForKey:@"username"] objectAtIndex:0]]; + } else if ([errors valueForKey:@"__all__"]) { + [self showError:[[errors valueForKey:@"__all__"] objectAtIndex:0]]; + } + } else { + [self.passwordInput setText:@""]; + [self.signUpPasswordInput setText:@""]; + // [appDelegate showFirstTimeUser]; + [appDelegate reloadFeedsView:YES]; + [self dismissViewControllerAnimated:YES completion:nil]; } - } else { - [self.passwordInput setText:@""]; - [self.signUpPasswordInput setText:@""]; -// [appDelegate showFirstTimeUser]; - [appDelegate reloadFeedsView:YES]; - [self dismissViewControllerAnimated:YES completion:nil]; - } - + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; + } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; +- (void)requestFailed:(NSError *)error { NSLog(@"Error: %@", error); [appDelegate informError:error]; diff --git a/clients/ios/Classes/MoveSiteViewController.h b/clients/ios/Classes/MoveSiteViewController.h index 7c713c348..f8fb7e6be 100644 --- a/clients/ios/Classes/MoveSiteViewController.h +++ b/clients/ios/Classes/MoveSiteViewController.h @@ -8,7 +8,6 @@ #import #import "NewsBlurAppDelegate.h" -#import "ASIHTTPRequest.h" @class NewsBlurAppDelegate; @@ -16,8 +15,8 @@ @end -@interface MoveSiteViewController : UIViewController - { +@interface MoveSiteViewController : BaseViewController + { NewsBlurAppDelegate *appDelegate; } diff --git a/clients/ios/Classes/MoveSiteViewController.m b/clients/ios/Classes/MoveSiteViewController.m index d6beaf8a7..833f21f70 100644 --- a/clients/ios/Classes/MoveSiteViewController.m +++ b/clients/ios/Classes/MoveSiteViewController.m @@ -8,8 +8,6 @@ #import "MoveSiteViewController.h" #import "NewsBlurAppDelegate.h" -#import "ASIHTTPRequest.h" -#import "ASIFormDataRequest.h" #import "StringHelper.h" #import "StoriesCollection.h" @@ -141,34 +139,23 @@ [self.activityIndicator startAnimating]; NSString *urlString = [NSString stringWithFormat:@"%@/reader/move_feed_to_folder", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; NSString *fromFolder = [appDelegate extractFolderName:[fromFolderInput text]]; NSString *toFolder = [appDelegate extractFolderName:[toFolderInput text]]; - [request setPostValue:fromFolder forKey:@"in_folder"]; - [request setPostValue:toFolder forKey:@"to_folder"]; - [request setPostValue:[appDelegate.storiesCollection.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(requestFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [params setObject:fromFolder forKey:@"in_folder"]; + [params setObject:toFolder forKey:@"to_folder"]; + [params setObject:[appDelegate.storiesCollection.activeFeed objectForKey:@"id"] forKey:@"feed_id"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self requestFinished:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)requestFinished:(ASIHTTPRequest *)request { - if ([request responseStatusCode] >= 500) { - return [self requestFailed:request]; - } - +- (void)requestFinished:(NSDictionary *)results { [self.movingLabel setHidden:YES]; [self.activityIndicator stopAnimating]; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; + int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { [self.errorLabel setText:[results valueForKey:@"message"]]; @@ -190,31 +177,24 @@ [self.activityIndicator startAnimating]; NSString *urlString = [NSString stringWithFormat:@"%@/reader/move_folder_to_folder", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; NSString *folderName = [appDelegate extractFolderName:appDelegate.storiesCollection.activeFolder]; NSString *fromFolder = [appDelegate extractFolderName:[fromFolderInput text]]; NSString *toFolder = [appDelegate extractFolderName:[toFolderInput text]]; - [request setPostValue:fromFolder forKey:@"in_folder"]; - [request setPostValue:toFolder forKey:@"to_folder"]; - [request setPostValue:folderName forKey:@"folder_name"]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishMoveFolder:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [params setObject:fromFolder forKey:@"in_folder"]; + [params setObject:toFolder forKey:@"to_folder"]; + [params setObject:folderName forKey:@"folder_name"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMoveFolder:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)finishMoveFolder:(ASIHTTPRequest *)request { +- (void)finishMoveFolder:(NSDictionary *)results { [self.movingLabel setHidden:YES]; [self.activityIndicator stopAnimating]; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; + int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { [self.errorLabel setText:[results valueForKey:@"message"]]; @@ -226,13 +206,11 @@ } -- (void)requestFailed:(ASIHTTPRequest *)request { +- (void)requestFailed:(NSError *)error { [self.movingLabel setHidden:YES]; [self.errorLabel setHidden:NO]; [self.activityIndicator stopAnimating]; - NSError *error = [request error]; NSLog(@"Error: %@", error); - NSLog(@"Error: %@", [request responseString]); [self.errorLabel setText:error.localizedDescription]; } @@ -311,4 +289,4 @@ numberOfRowsInComponent:(NSInteger)component { return CGRectInset(bounds, 24, 0); } -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/NBContainerViewController.h b/clients/ios/Classes/NBContainerViewController.h index b516146de..aa8b93748 100644 --- a/clients/ios/Classes/NBContainerViewController.h +++ b/clients/ios/Classes/NBContainerViewController.h @@ -8,6 +8,7 @@ #import #import +#import "NewsBlurAppDelegate.h" @class NewsBlurAppDelegate; diff --git a/clients/ios/Classes/NBContainerViewController.m b/clients/ios/Classes/NBContainerViewController.m index e3cf65d7e..05b1d3202 100644 --- a/clients/ios/Classes/NBContainerViewController.m +++ b/clients/ios/Classes/NBContainerViewController.m @@ -361,8 +361,7 @@ FeedDetailTableCell *cell = (FeedDetailTableCell *)sender; [self.appDelegate showPopoverWithViewController:self.appDelegate.notificationsViewController contentSize:CGSizeMake(420, 382) sourceView:cell sourceRect:cell.bounds]; } else { - CGRect frame = [sender CGRectValue]; - [self.appDelegate showPopoverWithViewController:self.appDelegate.notificationsViewController contentSize:CGSizeMake(420, 382) sourceView:self.storyPageControl.view sourceRect:frame]; + [self.appDelegate showPopoverWithViewController:self.appDelegate.notificationsViewController contentSize:CGSizeMake(420, 382) barButtonItem:appDelegate.feedsViewController.settingsBarButton]; } } diff --git a/clients/ios/Classes/NewsBlurAppDelegate.h b/clients/ios/Classes/NewsBlurAppDelegate.h index a17590366..8360b4be4 100644 --- a/clients/ios/Classes/NewsBlurAppDelegate.h +++ b/clients/ios/Classes/NewsBlurAppDelegate.h @@ -11,6 +11,7 @@ #import "BaseViewController.h" #import "FMDatabaseQueue.h" #import "EventWindow.h" +#import "AFNetworking.h" #define FEED_DETAIL_VIEW_TAG 1000001 #define STORY_DETAIL_VIEW_TAG 1000002 @@ -49,10 +50,10 @@ @class IASKAppSettingsViewController; @class UnreadCounts; @class StoriesCollection; -@class TMCache; +@class PINCache; @interface NewsBlurAppDelegate : BaseViewController - { EventWindow *window; UINavigationController *ftuxNavigationController; @@ -91,6 +92,8 @@ SFSafariViewControllerDelegate> { UserProfileViewController *userProfileViewController; IASKAppSettingsViewController *preferencesViewController; + AFHTTPSessionManager *networkManager; + NSString * activeUsername; NSString * activeUserProfileId; NSString * activeUserProfileName; @@ -109,16 +112,16 @@ SFSafariViewControllerDelegate> { NSURL * activeOriginalStoryURL; NSString * activeShareType; NSDictionary * activeComment; - int feedDetailPortraitYCoordinate; - int originalStoryCount; + NSInteger feedDetailPortraitYCoordinate; + NSInteger originalStoryCount; NSInteger selectedIntelligence; - int savedStoriesCount; - int totalUnfetchedStoryCount; - int remainingUnfetchedStoryCount; - int latestFetchedStoryDate; - int latestCachedImageDate; - int totalUncachedImagesCount; - int remainingUncachedImagesCount; + NSInteger savedStoriesCount; + NSInteger totalUnfetchedStoryCount; + NSInteger remainingUnfetchedStoryCount; + NSInteger latestFetchedStoryDate; + NSInteger latestCachedImageDate; + NSInteger totalUncachedImagesCount; + NSInteger remainingUncachedImagesCount; NSMutableDictionary * recentlyReadStories; NSMutableSet * recentlyReadFeeds; NSMutableArray * readStories; @@ -149,8 +152,8 @@ SFSafariViewControllerDelegate> { UIImageView *splashView; NSMutableDictionary *activeCachedImages; - TMCache *cachedFavicons; - TMCache *cachedStoryImages; + PINCache *cachedFavicons; + PINCache *cachedStoryImages; } @property (nonatomic) IBOutlet EventWindow *window; @@ -192,9 +195,10 @@ SFSafariViewControllerDelegate> { @property (nonatomic) IBOutlet FirstTimeUserAddFriendsViewController *firstTimeUserAddFriendsViewController; @property (nonatomic) IBOutlet FirstTimeUserAddNewsBlurViewController *firstTimeUserAddNewsBlurViewController; +@property (nonatomic) AFHTTPSessionManager *networkManager; @property (nonatomic, readwrite) StoriesCollection *storiesCollection; -@property (nonatomic, readwrite) TMCache *cachedFavicons; -@property (nonatomic, readwrite) TMCache *cachedStoryImages; +@property (nonatomic, readwrite) PINCache *cachedFavicons; +@property (nonatomic, readwrite) PINCache *cachedStoryImages; @property (nonatomic, readonly) NSString *url; @property (nonatomic, readonly) NSString *host; @@ -217,15 +221,15 @@ SFSafariViewControllerDelegate> { @property (readwrite) NSURL * activeOriginalStoryURL; @property (readwrite) NSDictionary * activeComment; @property (readwrite) NSString * activeShareType; -@property (readwrite) int feedDetailPortraitYCoordinate; -@property (readwrite) int originalStoryCount; -@property (readwrite) int savedStoriesCount; -@property (readwrite) int totalUnfetchedStoryCount; -@property (readwrite) int remainingUnfetchedStoryCount; -@property (readwrite) int totalUncachedImagesCount; -@property (readwrite) int remainingUncachedImagesCount; -@property (readwrite) int latestFetchedStoryDate; -@property (readwrite) int latestCachedImageDate; +@property (readwrite) NSInteger feedDetailPortraitYCoordinate; +@property (readwrite) NSInteger originalStoryCount; +@property (readwrite) NSInteger savedStoriesCount; +@property (readwrite) NSInteger totalUnfetchedStoryCount; +@property (readwrite) NSInteger remainingUnfetchedStoryCount; +@property (readwrite) NSInteger totalUncachedImagesCount; +@property (readwrite) NSInteger remainingUncachedImagesCount; +@property (readwrite) NSInteger latestFetchedStoryDate; +@property (readwrite) NSInteger latestCachedImageDate; @property (readwrite) NSInteger selectedIntelligence; @property (readwrite) NSMutableDictionary * recentlyReadStories; @property (readwrite) NSMutableSet * recentlyReadFeeds; @@ -297,6 +301,8 @@ SFSafariViewControllerDelegate> { - (void)openTrainSite; - (void)openNotificationsWithFeed:(NSString *)feedId; - (void)openNotificationsWithFeed:(NSString *)feedId sender:(id)sender; +- (void)updateNotifications:(NSDictionary *)params feed:(NSString *)feedId; +- (void)checkForFeedNotifications; - (void)openTrainSiteWithFeedLoaded:(BOOL)feedLoaded from:(id)sender; - (void)openTrainStory:(id)sender; - (void)openUserTagsStory:(id)sender; @@ -313,6 +319,7 @@ SFSafariViewControllerDelegate> { - (void)adjustStoryDetailWebView; - (void)calibrateStoryTitles; - (void)recalculateIntelligenceScores:(id)feedId; +- (void)cancelRequests; - (void)reloadFeedsView:(BOOL)showLoader; - (void)setTitle:(NSString *)title; - (void)showOriginalStory:(NSURL *)url; @@ -331,6 +338,7 @@ SFSafariViewControllerDelegate> { - (BOOL)isPortrait; - (void)confirmLogout; - (void)showConnectToService:(NSString *)serviceName; +- (void)showAlert:(UIAlertController *)alert withViewController:(UIViewController *)vc; - (void)refreshUserProfile:(void(^)())callback; - (void)refreshFeedCount:(id)feedId; @@ -354,15 +362,13 @@ SFSafariViewControllerDelegate> { - (void)markFeedReadInCache:(NSArray *)feedIds cutoffTimestamp:(NSInteger)cutoff; - (void)markFeedReadInCache:(NSArray *)feedIds cutoffTimestamp:(NSInteger)cutoff older:(BOOL)older; - (void)markStoriesRead:(NSDictionary *)stories inFeeds:(NSArray *)feeds cutoffTimestamp:(NSInteger)cutoff; -- (void)requestFailedMarkStoryRead:(ASIFormDataRequest *)request; -- (void)finishMarkAllAsRead:(ASIHTTPRequest *)request; - (void)finishMarkAsRead:(NSDictionary *)story; - (void)finishMarkAsUnread:(NSDictionary *)story; -- (void)failedMarkAsUnread:(ASIFormDataRequest *)request; -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsSaved:(ASIFormDataRequest *)request; -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsUnsaved:(ASIFormDataRequest *)request; +- (void)failedMarkAsUnread:(NSDictionary *)params; +- (void)finishMarkAsSaved:(NSDictionary *)params; +- (void)failedMarkAsSaved:(NSDictionary *)params; +- (void)finishMarkAsUnsaved:(NSDictionary *)params; +- (void)failedMarkAsUnsaved:(NSDictionary *)params; - (NSInteger)adjustSavedStoryCount:(NSString *)tagName direction:(NSInteger)direction; - (NSArray *)updateStarredStoryCounts:(NSDictionary *)results; - (void)renameFeed:(NSString *)newTitle; @@ -400,11 +406,10 @@ SFSafariViewControllerDelegate> { - (void)toggleTagClassifier:(NSString *)tag feedId:(NSString *)feedId; - (void)toggleTitleClassifier:(NSString *)title feedId:(NSString *)feedId score:(NSInteger)score; - (void)toggleFeedClassifier:(NSString *)feedId; -- (void)requestClassifierResponse:(ASIHTTPRequest *)request withFeed:(NSString *)feedId; - (NSInteger)databaseSchemaVersion:(FMDatabase *)db; - (void)createDatabaseConnection; -- (void)setupDatabase:(FMDatabase *)db; +- (void)setupDatabase:(FMDatabase *)db force:(BOOL)force; - (void)cancelOfflineQueue; - (void)startOfflineQueue; - (void)startOfflineFetchStories; diff --git a/clients/ios/Classes/NewsBlurAppDelegate.m b/clients/ios/Classes/NewsBlurAppDelegate.m index 417e0f4f0..c1bb01e73 100644 --- a/clients/ios/Classes/NewsBlurAppDelegate.m +++ b/clients/ios/Classes/NewsBlurAppDelegate.m @@ -29,8 +29,6 @@ #import "FontSettingsViewController.h" #import "FeedChooserViewController.h" #import "UserProfileViewController.h" -#import "AFHTTPRequestOperation.h" -#import "ASINetworkQueue.h" #import "InteractionsModule.h" #import "ActivityModule.h" #import "FirstTimeUserViewController.h" @@ -56,7 +54,7 @@ #import "OfflineFetchImages.h" #import "OfflineCleanImages.h" #import "NBBarButtonItem.h" -#import "TMCache.h" +#import "PINCache.h" #import "StoriesCollection.h" #import "NSString+HTML.h" #import "UIView+ViewController.h" @@ -65,6 +63,7 @@ #import "NSNull+JSON.h" #import "UISearchBar+Field.h" #import "UIViewController+HidePopover.h" +#import "PINCache.h" #import #import @@ -116,6 +115,7 @@ @synthesize firstTimeUserAddFriendsViewController; @synthesize firstTimeUserAddNewsBlurViewController; +@synthesize networkManager; @synthesize feedDetailPortraitYCoordinate; @synthesize cachedFavicons; @synthesize cachedStoryImages; @@ -189,8 +189,6 @@ } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - NSString *currentiPhoneVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; - [self registerDefaultsFromSettingsBundle]; self.navigationController.delegate = self; @@ -198,24 +196,21 @@ self.storiesCollection = [StoriesCollection new]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [ASIHTTPRequest setDefaultUserAgentString:[NSString stringWithFormat:@"NewsBlur iPad App v%@", - currentiPhoneVersion]]; [window addSubview:self.masterContainerViewController.view]; self.window.rootViewController = self.masterContainerViewController; } else { - [ASIHTTPRequest setDefaultUserAgentString:[NSString stringWithFormat:@"NewsBlur iPhone App v%@", - currentiPhoneVersion]]; [window addSubview:self.navigationController.view]; self.window.rootViewController = self.navigationController; } + [self clearNetworkManager]; [window makeKeyAndVisible]; [[ThemeManager themeManager] prepareForWindow:self.window]; [self createDatabaseConnection]; - [self.cachedStoryImages removeAllObjects:^(TMCache *cache) {}]; + [self.cachedStoryImages removeAllObjects:nil]; [feedsViewController loadOfflineFeeds:NO]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { @@ -230,8 +225,8 @@ // [self showFirstTimeUser]; - cachedFavicons = [[TMCache alloc] initWithName:@"NBFavicons"]; - cachedStoryImages = [[TMCache alloc] initWithName:@"NBStoryImages"]; + cachedFavicons = [[PINCache alloc] initWithName:@"NBFavicons"]; + cachedStoryImages = [[PINCache alloc] initWithName:@"NBStoryImages"]; NBURLCache *urlCache = [[NBURLCache alloc] init]; [NSURLCache setSharedURLCache:urlCache]; @@ -314,8 +309,8 @@ - (void)finishBackground { if (!backgroundCompletionHandler) return; - NSLog(@"Background fetch complete. Found data: %d/%d = %d", - self.totalUnfetchedStoryCount, self.totalUncachedImagesCount, + NSLog(@"Background fetch complete. Found data: %ld/%ld = %d", + (long)self.totalUnfetchedStoryCount, (long)self.totalUncachedImagesCount, self.totalUnfetchedStoryCount || self.totalUncachedImagesCount); if (self.totalUnfetchedStoryCount || self.totalUncachedImagesCount) { backgroundCompletionHandler(UIBackgroundFetchResultNewData); @@ -430,31 +425,14 @@ } NSLog(@" -> APNS token: %@", token); - NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/notifications/apns_token/", - self.url]]; - ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url]; - __weak ASIFormDataRequest *request = _request; - [request setValidatesSecureCertificate:NO]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setPostValue:token - forKey:@"apns_token"]; - [request setFailedBlock:^(void) { + NSString *url = [NSString stringWithFormat:@"%@/notifications/apns_token/", self.url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:token forKey:@"apns_token"]; + [networkManager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSLog(@" -> APNS: %@", responseObject); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed to set APNS token"); }]; - [request setCompletionBlock:^(void) { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - NSLog(@" -> APNS: %@/%@", results, error); - }]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; - } - (BOOL)application:(UIApplication *)application @@ -682,6 +660,7 @@ } + - (void)showMuteSites { [self showFeedChooserForOperation:FeedChooserOperationMuteSites]; } @@ -1016,8 +995,6 @@ UINavigationController *navController = self.navigationController; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - // trainerViewController.modalPresentationStyle=UIModalPresentationFormSheet; - // [navController presentViewController:trainerViewController animated:YES completion:nil]; [self.masterContainerViewController showNotificationsPopoverWithFeed:feedId sender:sender]; } else { if (self.notificationsNavigationController == nil) { @@ -1030,6 +1007,40 @@ } } +- (void)updateNotifications:(NSDictionary *)params feed:(NSString *)feedId { + NSMutableDictionary *feed = [[self.dictFeeds objectForKey:feedId] mutableCopy]; + + [feed setObject:params[@"notification_types"] forKey:@"notification_types"]; + [feed setObject:params[@"notification_filter"] forKey:@"notification_filter"]; + + [self.dictFeeds setObject:feed forKey:feedId]; +} + +- (void)checkForFeedNotifications { + NSMutableArray *foundNotificationFeedIds = [NSMutableArray array]; + + for (NSDictionary *feed in self.dictFeeds.allValues) { + NSArray *types = [feed objectForKey:@"notification_types"]; + if (types) { + for (NSString *notificationType in types) { + if ([notificationType isEqualToString:@"ios"]) { + [self registerForRemoteNotifications]; + } + } + if ([types count]) { + [foundNotificationFeedIds addObject:[feed objectForKey:@"id"]]; + } + } + } + + self.notificationFeedIds = [foundNotificationFeedIds sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { + NSString *feed1Title = [[[self.dictFeeds objectForKey:[NSString stringWithFormat:@"%@", obj1]] objectForKey:@"feed_title"] lowercaseString]; + NSString *feed2Title = [[[self.dictFeeds objectForKey:[NSString stringWithFormat:@"%@", obj2]] objectForKey:@"feed_title"] lowercaseString]; + + return [feed1Title compare:feed2Title]; + }]; +} + - (void)openUserTagsStory:(id)sender { if (!self.userTagsViewController) { self.userTagsViewController = [[UserTagsViewController alloc] init]; @@ -1050,6 +1061,30 @@ [self.navigationController.topViewController becomeFirstResponder]; } +#pragma mark - Network + +- (void)cancelRequests { + [self clearNetworkManager]; +} + +- (void)clearNetworkManager { + [networkManager invalidateSessionCancelingTasks:YES]; + networkManager = [AFHTTPSessionManager manager]; + networkManager.responseSerializer = [AFJSONResponseSerializer serializer]; + [networkManager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; + + NSString *currentiPhoneVersion = [[[NSBundle mainBundle] infoDictionary] + objectForKey:@"CFBundleVersion"]; + NSString *UA; + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + UA = [NSString stringWithFormat:@"NewsBlur iPad App v%@", currentiPhoneVersion]; + } else { + UA = [NSString stringWithFormat:@"NewsBlur iPhone App v%@", currentiPhoneVersion]; + } + [networkManager.requestSerializer setValue:UA forHTTPHeaderField:@"User-Agent"]; +} + + #pragma mark - - (void)reloadFeedsView:(BOOL)showLoader { @@ -1264,13 +1299,26 @@ } - (void)confirmLogout { - UIAlertView *logoutConfirm = [[UIAlertView alloc] initWithTitle:@"Positive?" - message:nil - delegate:self - cancelButtonTitle:@"Cancel" - otherButtonTitles:@"Logout", nil]; - [logoutConfirm show]; - [logoutConfirm setTag:1]; + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Positive?" message:nil preferredStyle:UIAlertControllerStyleAlert]; + [alertController addAction:[UIAlertAction actionWithTitle: @"Logout" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { + [alertController dismissViewControllerAnimated:YES completion:nil]; + NSLog(@"Logging out..."); + NSString *urlString = [NSString stringWithFormat:@"%@/reader/logout?api=1", + self.url]; + [networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [MBProgressHUD hideHUDForView:self.view animated:YES]; + [self showLogin]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [MBProgressHUD hideHUDForView:self.view animated:YES]; + }]; + + [MBProgressHUD hideHUDForView:self.view animated:YES]; + MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; + HUD.labelText = @"Logging out..."; + }]]; + [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" + style:UIAlertActionStyleCancel handler:nil]]; + [self.feedsViewController presentViewController:alertController animated:YES completion:nil]; } - (void)showConnectToService:(NSString *)serviceName { @@ -1292,74 +1340,31 @@ } } +- (void)showAlert:(UIAlertController *)alert withViewController:(UIViewController *)vc { + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [self.masterContainerViewController presentViewController:alert animated:YES completion:nil]; + } else { + [vc presentViewController:alert animated:YES completion:nil]; + } +} + - (void)refreshUserProfile:(void(^)())callback { - NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/social/load_user_profile", - self.url]]; - ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:url]; - __weak ASIHTTPRequest *request = _request; - [request setValidatesSecureCertificate:NO]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setFailedBlock:^(void) { + NSString *urlString = [NSString stringWithFormat:@"%@/social/load_user_profile", + self.url]; + [networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + self.dictUserProfile = [responseObject objectForKey:@"user_profile"]; + self.dictSocialServices = [responseObject objectForKey:@"services"]; + callback(); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed user profile"); callback(); }]; - [request setCompletionBlock:^(void) { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - - self.dictUserProfile = [results objectForKey:@"user_profile"]; - self.dictSocialServices = [results objectForKey:@"services"]; - callback(); - }]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; } - (void)refreshFeedCount:(id)feedId { [feedsViewController fadeFeed:feedId]; } -- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { - if (alertView.tag == 1) { // this is logout - if (buttonIndex == 0) { - return; - } else { - NSLog(@"Logging out..."); - NSString *urlS = [NSString stringWithFormat:@"%@/reader/logout?api=1", - self.url]; - NSURL *url = [NSURL URLWithString:urlS]; - - __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setFailedBlock:^(void) { - [MBProgressHUD hideHUDForView:self.view animated:YES]; - }]; - [request setCompletionBlock:^(void) { - NSLog(@"Logout successful"); - [MBProgressHUD hideHUDForView:self.view animated:YES]; - [self showLogin]; - }]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; - - [ASIHTTPRequest setSessionCookies:nil]; - - [MBProgressHUD hideHUDForView:self.view animated:YES]; - MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; - HUD.labelText = @"Logging out..."; - } - } -} - - (void)loadRiverFeedDetailView:(FeedDetailViewController *)feedDetailView withFolder:(NSString *)folder { self.readStories = [NSMutableArray array]; NSMutableArray *feeds = [NSMutableArray array]; @@ -1635,14 +1640,15 @@ NSString *storyBrowser = [preferences stringForKey:@"story_browser"]; if ([storyBrowser isEqualToString:@"safari"]) { - [[UIApplication sharedApplication] openURL:url]; + [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; +// [[UIApplication sharedApplication] openURL:url]; return; } else if ([storyBrowser isEqualToString:@"chrome"] && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome-x-callback://"]]) { - NSString *openingURL = [url.absoluteString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *openingURL = [url.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSURL *callbackURL = [NSURL URLWithString:@"newsblur://"]; - NSString *callback = [callbackURL.absoluteString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; - NSString *sourceName = [[[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleName"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *callback = [callbackURL.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; + NSString *sourceName = [[[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleName"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSURL *activityURL = [NSURL URLWithString: [NSString stringWithFormat:@"googlechrome-x-callback://x-callback-url/open/?url=%@&x-success=%@&x-source=%@", @@ -1650,7 +1656,7 @@ callback, sourceName]]; - [[UIApplication sharedApplication] openURL:activityURL]; + [[UIApplication sharedApplication] openURL:activityURL options:@{} completionHandler:nil]; return; } else if ([storyBrowser isEqualToString:@"opera_mini"] && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"opera-http://"]]) { @@ -1664,12 +1670,12 @@ withString: @"opera-http"]; } - [[UIApplication sharedApplication] openURL:[NSURL URLWithString:operaURL]]; + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:operaURL] options:@{} completionHandler:nil]; return; } else if ([storyBrowser isEqualToString:@"firefox"]) { NSString *encodedURL = [url.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSString *firefoxURL = [NSString stringWithFormat:@"%@%@", @"firefox://?url=", encodedURL]; - [[UIApplication sharedApplication] openURL:[NSURL URLWithString:firefoxURL]]; + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:firefoxURL] options:@{} completionHandler:nil]; } else if ([storyBrowser isEqualToString:@"inappsafari"]) { self.safariViewController = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:NO]; @@ -1783,7 +1789,9 @@ } - (BOOL)isFeedInTextView:(id)feedId { - return [self.dictTextFeeds objectForKey:feedId]; + id text = [self.dictTextFeeds objectForKey:feedId]; + if (text != nil) return YES; + return NO; } - (void)toggleFeedTextView:(id)feedId { @@ -2129,42 +2137,34 @@ - (void)markStoryAsRead:(NSString *)storyHash inFeed:(NSString *)feed withCallback:(void(^)())callback { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_story_hashes_as_read", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:storyHash - forKey:@"story_hash"]; - [request setCompletionBlock:^{ + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:storyHash forKey:@"story_hash"]; + + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Marked as read: %@", storyHash); callback(); - }]; - [request setFailedBlock:^{ + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed marked as read, queueing: %@", storyHash); NSMutableDictionary *stories = [NSMutableDictionary dictionary]; [stories setObject:@[storyHash] forKey:feed]; [self queueReadStories:stories]; callback(); }]; - [request setDelegate:self]; - [request startAsynchronous]; } - (void)markStoryAsStarred:(NSString *)storyHash withCallback:(void(^)())callback { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_story_hash_as_starred", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:storyHash - forKey:@"story_hash"]; - [request setCompletionBlock:^{ + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:storyHash forKey:@"story_hash"]; + + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Marked as starred: %@", storyHash); callback(); - }]; - [request setFailedBlock:^{ + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed marked as starred: %@", storyHash); callback(); }]; - [request setDelegate:self]; - [request startAsynchronous]; } - (void)markStoriesRead:(NSDictionary *)stories inFeeds:(NSArray *)feeds cutoffTimestamp:(NSInteger)cutoff { @@ -2213,20 +2213,6 @@ } } -- (void)requestFailedMarkStoryRead:(ASIFormDataRequest *)request { - // [self informError:@"Failed to mark story as read"]; - NSArray *feedIds = [request.userInfo objectForKey:@"feeds"]; - NSDictionary *stories = [request.userInfo objectForKey:@"stories"]; - - [self markStoriesRead:stories inFeeds:feedIds cutoffTimestamp:0]; -} - -- (void)finishMarkAllAsRead:(ASIFormDataRequest *)request { - if (request.responseStatusCode != 200) { - [self requestFailedMarkStoryRead:request]; - } -} - - (void)finishMarkAsRead:(NSDictionary *)story { if (!storyPageControl.previousPage || !storyPageControl.currentPage || !storyPageControl.nextPage) return; for (StoryDetailViewController *page in @[storyPageControl.previousPage, @@ -2255,38 +2241,38 @@ originalStoryCount += 1; } -- (void)failedMarkAsUnread:(ASIFormDataRequest *)request { - if (![storyPageControl failedMarkAsUnread:request]) { - [feedDetailViewController failedMarkAsUnread:request]; - [dashboardViewController.storiesModule failedMarkAsUnread:request]; +- (void)failedMarkAsUnread:(NSDictionary *)params { + if (![storyPageControl failedMarkAsUnread:params]) { + [feedDetailViewController failedMarkAsUnread:params]; + [dashboardViewController.storiesModule failedMarkAsUnread:params]; } [feedDetailViewController reloadData]; [dashboardViewController.storiesModule reloadData]; } -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request { - [storyPageControl finishMarkAsSaved:request]; - [feedDetailViewController finishMarkAsSaved:request]; +- (void)finishMarkAsSaved:(NSDictionary *)params { + [storyPageControl finishMarkAsSaved:params]; + [feedDetailViewController finishMarkAsSaved:params]; } -- (void)failedMarkAsSaved:(ASIFormDataRequest *)request { - if (![storyPageControl failedMarkAsSaved:request]) { - [feedDetailViewController failedMarkAsSaved:request]; - [dashboardViewController.storiesModule failedMarkAsSaved:request]; +- (void)failedMarkAsSaved:(NSDictionary *)params { + if (![storyPageControl failedMarkAsSaved:params]) { + [feedDetailViewController failedMarkAsSaved:params]; + [dashboardViewController.storiesModule failedMarkAsSaved:params]; } [feedDetailViewController reloadData]; [dashboardViewController.storiesModule reloadData]; } -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request { - [storyPageControl finishMarkAsUnsaved:request]; - [feedDetailViewController finishMarkAsUnsaved:request]; +- (void)finishMarkAsUnsaved:(NSDictionary *)params { + [storyPageControl finishMarkAsUnsaved:params]; + [feedDetailViewController finishMarkAsUnsaved:params]; } -- (void)failedMarkAsUnsaved:(ASIFormDataRequest *)request { - if (![storyPageControl failedMarkAsUnsaved:request]) { - [feedDetailViewController failedMarkAsUnsaved:request]; - [dashboardViewController.storiesModule failedMarkAsUnsaved:request]; +- (void)failedMarkAsUnsaved:(NSDictionary *)params { + if (![storyPageControl failedMarkAsUnsaved:params]) { + [feedDetailViewController failedMarkAsUnsaved:params]; + [dashboardViewController.storiesModule failedMarkAsUnsaved:params]; } [feedDetailViewController reloadData]; [dashboardViewController.storiesModule reloadData]; @@ -2839,6 +2825,22 @@ #pragma mark - #pragma mark Classifiers +- (void)failedClassifierSave:(NSURLSessionDataTask *)task { + BaseViewController *view; + if (self.trainerViewController.isViewLoaded && self.trainerViewController.view.window) { + view = self.trainerViewController; + } else { + view = self.storyPageControl.currentPage; + } + + NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response; + if (response.statusCode == 503) { + [view informError:@"In maintenance mode"]; + } else { + [view informError:@"The server barfed!"]; + } +} + - (void)toggleAuthorClassifier:(NSString *)author feedId:(NSString *)feedId { int authorScore = [[[[storiesCollection.activeClassifiers objectForKey:feedId] objectForKey:@"authors"] @@ -2863,23 +2865,19 @@ NSString *urlString = [NSString stringWithFormat:@"%@/classifier/save", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - __weak ASIFormDataRequest *_request = request; - [request setPostValue:author - forKey:authorScore >= 1 ? @"like_author" : - authorScore <= -1 ? @"dislike_author" : - @"remove_like_author"]; - [request setPostValue:feedId forKey:@"feed_id"]; - [request setCompletionBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setFailedBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setDelegate:self]; - [request startAsynchronous]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:author + forKey:authorScore >= 1 ? @"like_author" : + authorScore <= -1 ? @"dislike_author" : + @"remove_like_author"]; + [params setObject:feedId forKey:@"feed_id"]; + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self.feedsViewController refreshFeedList:feedId]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedClassifierSave:task]; + }]; + [self recalculateIntelligenceScores:feedId]; [self.feedDetailViewController.storyTitlesTable reloadData]; } @@ -2911,22 +2909,18 @@ NSString *urlString = [NSString stringWithFormat:@"%@/classifier/save", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - __weak ASIFormDataRequest *_request = request; - [request setPostValue:tag - forKey:tagScore >= 1 ? @"like_tag" : - tagScore <= -1 ? @"dislike_tag" : - @"remove_like_tag"]; - [request setPostValue:feedId forKey:@"feed_id"]; - [request setCompletionBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:tag + forKey:tagScore >= 1 ? @"like_tag" : + tagScore <= -1 ? @"dislike_tag" : + @"remove_like_tag"]; + [params setObject:feedId forKey:@"feed_id"]; + + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self.feedsViewController refreshFeedList:feedId]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedClassifierSave:task]; }]; - [request setFailedBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setDelegate:self]; - [request startAsynchronous]; [self recalculateIntelligenceScores:feedId]; [self.feedDetailViewController.storyTitlesTable reloadData]; @@ -2963,23 +2957,19 @@ NSString *urlString = [NSString stringWithFormat:@"%@/classifier/save", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - __weak ASIFormDataRequest *_request = request; - [request setPostValue:title - forKey:titleScore >= 1 ? @"like_title" : - titleScore <= -1 ? @"dislike_title" : - @"remove_like_title"]; - [request setPostValue:feedId forKey:@"feed_id"]; - [request setCompletionBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setFailedBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setDelegate:self]; - [request startAsynchronous]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:title + forKey:titleScore >= 1 ? @"like_title" : + titleScore <= -1 ? @"dislike_title" : + @"remove_like_title"]; + [params setObject:feedId forKey:@"feed_id"]; + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self.feedsViewController refreshFeedList:feedId]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedClassifierSave:task]; + }]; + [self recalculateIntelligenceScores:feedId]; [self.feedDetailViewController.storyTitlesTable reloadData]; } @@ -3009,47 +2999,36 @@ NSString *urlString = [NSString stringWithFormat:@"%@/classifier/save", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - __weak ASIFormDataRequest *_request = request; - [request setPostValue:feedId + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:feedId forKey:feedScore >= 1 ? @"like_feed" : feedScore <= -1 ? @"dislike_feed" : @"remove_like_feed"]; - [request setPostValue:feedId forKey:@"feed_id"]; - [request setCompletionBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setFailedBlock:^{ - [self requestClassifierResponse:_request withFeed:feedId]; - }]; - [request setDelegate:self]; - [request startAsynchronous]; + [params setObject:feedId forKey:@"feed_id"]; + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self.feedsViewController refreshFeedList:feedId]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedRequest:task.response]; + }]; + [self recalculateIntelligenceScores:feedId]; [self.feedDetailViewController.storyTitlesTable reloadData]; } -- (void)requestClassifierResponse:(ASIHTTPRequest *)request withFeed:(NSString *)feedId { +- (void)failedRequest:(NSURLResponse *)response { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; BaseViewController *view; if (self.trainerViewController.isViewLoaded && self.trainerViewController.view.window) { view = self.trainerViewController; } else { view = self.storyPageControl.currentPage; } - if ([request responseStatusCode] == 503) { + if (httpResponse.statusCode == 503) { return [view informError:@"In maintenance mode"]; - } else if ([request responseStatusCode] != 200) { + } else if (httpResponse.statusCode != 200) { return [view informError:@"The server barfed!"]; } - - [self.feedsViewController refreshFeedList:feedId]; -} - -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; - NSLog(@"Error: %@", error); - [self informError:error]; } #pragma mark - @@ -3106,12 +3085,12 @@ database = [FMDatabaseQueue databaseQueueWithPath:path]; [database inDatabase:^(FMDatabase *db) { // db.traceExecution = YES; - [self setupDatabase:db]; + [self setupDatabase:db force:NO]; }]; } -- (void)setupDatabase:(FMDatabase *)db { - if ([self databaseSchemaVersion:db] < CURRENT_DB_VERSION) { +- (void)setupDatabase:(FMDatabase *)db force:(BOOL)force { + if ([self databaseSchemaVersion:db] < CURRENT_DB_VERSION || force) { // FMDB cannot execute this query because FMDB tries to use prepared statements [db closeOpenResultSets]; [db executeUpdate:@"drop table if exists `stories`"]; @@ -3412,38 +3391,27 @@ - (void)syncQueuedReadStories:(FMDatabase *)db withStories:(NSDictionary *)hashes withCallback:(void(^)())callback { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_feed_stories_as_read", self.url]; - NSURL *url = [NSURL URLWithString:urlString]; NSMutableArray *completedHashes = [NSMutableArray array]; for (NSArray *storyHashes in [hashes allValues]) { [completedHashes addObjectsFromArray:storyHashes]; } NSLog(@"Marking %lu queued read stories as read...", (unsigned long)[completedHashes count]); NSString *completedHashesStr = [completedHashes componentsJoinedByString:@"\",\""]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - __weak ASIHTTPRequest *_request = request; - [request setPostValue:[hashes JSONRepresentation] forKey:@"feeds_stories"]; - [request setDelegate:self]; - [request setValidatesSecureCertificate:NO]; - [request setCompletionBlock:^{ - if ([_request responseStatusCode] == 200) { - NSLog(@"Completed clearing %@ hashes", completedHashesStr); - [db executeUpdate:[NSString stringWithFormat:@"DELETE FROM queued_read_hashes " - "WHERE story_hash in (\"%@\")", completedHashesStr]]; - [self pruneQueuedReadHashes]; - } else { - NSLog(@"Failed mark read queued."); - self.hasQueuedReadStories = YES; - [self pruneQueuedReadHashes]; - } + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[hashes JSONRepresentation] forKey:@"feeds_stories"]; + + [networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSLog(@"Completed clearing %@ hashes", completedHashesStr); + [db executeUpdate:[NSString stringWithFormat:@"DELETE FROM queued_read_hashes " + "WHERE story_hash in (\"%@\")", completedHashesStr]]; + [self pruneQueuedReadHashes]; if (callback) callback(); - }]; - [request setFailedBlock:^{ + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed mark read queued."); self.hasQueuedReadStories = YES; [self pruneQueuedReadHashes]; if (callback) callback(); }]; - [request startAsynchronous]; } - (void)pruneQueuedReadHashes { @@ -3502,6 +3470,32 @@ } - (void)deleteAllCachedImages { + NSUInteger memorySize = 1024 * 1024 * 64; + NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:memorySize diskCapacity:memorySize diskPath:nil]; + [NSURLCache setSharedURLCache:sharedCache]; + NSLog(@"cap: %ld", (unsigned long)[[NSURLCache sharedURLCache] diskCapacity]); + + NSInteger sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage]; + float sizeInMB = sizeInteger / (1024.0f * 1024.0f); + NSLog(@"size: %ld, %f", (long)sizeInteger, sizeInMB); + + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + + sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage]; + sizeInMB = sizeInteger / (1024.0f * 1024.0f); + NSLog(@"size: %ld, %f", (long)sizeInteger, sizeInMB); + + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + + sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage]; + sizeInMB = sizeInteger / (1024.0f * 1024.0f); + NSLog(@"size: %ld, %f", (long)sizeInteger, sizeInMB); + + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + + [[PINCache sharedCache] removeAllObjects]; + [self.cachedStoryImages removeAllObjects]; + NSFileManager *fileManager = [[NSFileManager alloc] init]; NSError *error = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); @@ -3521,8 +3515,9 @@ } NSLog(@"Deleted %d images.", removed); + + } - @end #pragma mark - diff --git a/clients/ios/Classes/NewsBlurViewController.h b/clients/ios/Classes/NewsBlurViewController.h index b2946d808..4f70bde09 100644 --- a/clients/ios/Classes/NewsBlurViewController.h +++ b/clients/ios/Classes/NewsBlurViewController.h @@ -9,7 +9,6 @@ #import #import "NewsBlurAppDelegate.h" #import "FolderTitleView.h" -#import "ASIHTTPRequest.h" #import "BaseViewController.h" #import "NBNotifier.h" #import "IASKAppSettingsViewController.h" @@ -19,8 +18,7 @@ @interface NewsBlurViewController : BaseViewController { - (void)layoutForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; - (void)returnToApp; - (void)fetchFeedList:(BOOL)showLoader; -- (void)finishedWithError:(ASIHTTPRequest *)request; -- (void)finishLoadingFeedList:(ASIHTTPRequest *)request; - (void)finishLoadingFeedListWithDict:(NSDictionary *)results finished:(BOOL)finished; -- (void)finishRefreshingFeedList:(ASIHTTPRequest *)request; - (void)didSelectSectionHeader:(UIButton *)button; - (void)didSelectSectionHeaderWithTag:(NSInteger)tag; - (IBAction)selectIntelligence; @@ -95,8 +90,6 @@ UIGestureRecognizerDelegate> { - (void)markFeedsRead:(NSArray *)feedIds cutoffDays:(NSInteger)days; - (void)markEverythingReadWithDays:(NSInteger)days; - (void)markVisibleStoriesRead; -- (void)requestFailedMarkStoryRead:(ASIFormDataRequest *)request; -- (void)finishMarkAllAsRead:(ASIHTTPRequest *)request; - (void)didCollapseFolder:(UIButton *)button; - (BOOL)isFeedVisible:(id)feedId; - (void)changeToAllMode; @@ -109,8 +102,6 @@ UIGestureRecognizerDelegate> { + (int)computeMaxScoreForFeed:(NSDictionary *)feed; - (void)loadFavicons; - (void)loadAvatars; -- (void)saveAndDrawFavicons:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)refreshFeedList; - (void)refreshFeedList:(id)feedId; - (void)loadOfflineFeeds:(BOOL)failed; @@ -131,8 +122,8 @@ UIGestureRecognizerDelegate> { - (void)showRefreshNotifier; - (void)showCountingNotifier; - (void)showSyncingNotifier; -- (void)showSyncingNotifier:(float)progress hoursBack:(int)days; -- (void)showCachingNotifier:(float)progress hoursBack:(int)hours; +- (void)showSyncingNotifier:(float)progress hoursBack:(NSInteger)days; +- (void)showCachingNotifier:(float)progress hoursBack:(NSInteger)hours; - (void)showOfflineNotifier; - (void)showDoneNotifier; - (void)hideNotifier; diff --git a/clients/ios/Classes/NewsBlurViewController.m b/clients/ios/Classes/NewsBlurViewController.m index d9bff0306..dc74ae9a1 100644 --- a/clients/ios/Classes/NewsBlurViewController.m +++ b/clients/ios/Classes/NewsBlurViewController.m @@ -20,8 +20,6 @@ #import "UserProfileViewController.h" #import "StoryDetailViewController.h" #import "StoryPageControl.h" -#import "ASIHTTPRequest.h" -#import "AFHTTPRequestOperation.h" #import "MBProgressHUD.h" #import "Base64.h" #import "SBJson4.h" @@ -414,65 +412,44 @@ static UIFont *userLabelFont; } -(void)fetchFeedList:(BOOL)showLoader { - NSURL *urlFeedList; + NSString *urlFeedList; NSLog(@"Fetching feed list"); [appDelegate cancelOfflineQueue]; if (self.inPullToRefresh_) { - urlFeedList = [NSURL URLWithString: - [NSString stringWithFormat:@"%@/reader/feeds?flat=true&update_counts=true", - self.appDelegate.url]]; + urlFeedList = [NSString stringWithFormat:@"%@/reader/feeds?flat=true&update_counts=true", + self.appDelegate.url]; } else { - urlFeedList = [NSURL URLWithString: - [NSString stringWithFormat:@"%@/reader/feeds?flat=true&update_counts=false", - self.appDelegate.url]]; + urlFeedList = [NSString stringWithFormat:@"%@/reader/feeds?flat=true&update_counts=false", + self.appDelegate.url]; } - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlFeedList]; - [[NSHTTPCookieStorage sharedHTTPCookieStorage] - setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - [request setDidFinishSelector:@selector(finishLoadingFeedList:)]; - [request setDidFailSelector:@selector(finishedWithError:)]; - [request setTimeOutSeconds:15]; - [request startAsynchronous]; + if (appDelegate.backgroundCompletionHandler) { + urlFeedList = [urlFeedList stringByAppendingString:@"&background_ios=true"]; + } + + [appDelegate.networkManager GET:urlFeedList parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishLoadingFeedList:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response; + [self finishedWithError:error statusCode:httpResponse.statusCode]; + }]; self.lastUpdate = [NSDate date]; if (showLoader) { - [self.notifier hide]; +// [self.notifier hide]; } [self showRefreshNotifier]; } -- (void)finishedWithError:(ASIHTTPRequest *)request { - [MBProgressHUD hideHUDForView:self.view animated:YES]; - - // User clicking on another link before the page loads is OK. - [self informError:[request error]]; - [self finishRefresh]; - - self.isOffline = YES; - - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [appDelegate.dashboardViewController refreshStories]; - } - - [self showOfflineNotifier]; - [self loadNotificationStory]; - [[NSNotificationCenter defaultCenter] postNotificationName:@"FinishedLoadingFeedsNotification" object:nil]; -} - -- (void)finishLoadingFeedList:(ASIHTTPRequest *)request { - if ([request responseStatusCode] == 403) { +- (void)finishedWithError:(NSError *)error statusCode:(NSInteger)statusCode { + if (statusCode == 403) { NSLog(@"Showing login"); return [appDelegate showLogin]; - } else if ([request responseStatusCode] >= 400) { - if ([request responseStatusCode] == 429) { + } else if (statusCode >= 400) { + if (statusCode == 429) { [self informError:@"Slow down. You're rate-limited."]; - } else if ([request responseStatusCode] == 503) { + } else if (statusCode == 503) { [self informError:@"In maintenance mode"]; } else { [self informError:@"The server barfed!"]; @@ -486,19 +463,30 @@ static UIFont *userLabelFont; } return; } + + [MBProgressHUD hideHUDForView:self.view animated:YES]; + // User clicking on another link before the page loads is OK. + [self informError:error]; + [self finishRefresh]; + + self.isOffline = YES; + + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [appDelegate.dashboardViewController refreshStories]; + } + + [self showOfflineNotifier]; + [self loadNotificationStory]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"FinishedLoadingFeedsNotification" object:nil]; +} + +- (void)finishLoadingFeedList:(NSDictionary *)results { appDelegate.hasNoSites = NO; appDelegate.recentlyReadStories = [NSMutableDictionary dictionary]; appDelegate.unreadStoryHashes = [NSMutableDictionary dictionary]; self.isOffline = NO; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; appDelegate.activeUsername = [results objectForKey:@"user"]; @@ -742,7 +730,7 @@ static UIFont *userLabelFont; [self showExplainerOnEmptyFeedlist]; [self layoutHeaderCounts:0]; [self refreshHeaderCounts]; - [self checkForFeedNotifications]; + [appDelegate checkForFeedNotifications]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && finished) { [appDelegate.dashboardViewController refreshStories]; @@ -976,6 +964,8 @@ static UIFont *userLabelFont; self.feedTitlesTable.backgroundColor = UIColorFromRGB(0xf4f4f4); [self.feedTitlesTable reloadData]; + + [self resetupGestures]; } - (void)updateThemeBrightness { @@ -1026,19 +1016,15 @@ static UIFont *userLabelFont; dispatch_sync(dispatch_get_main_queue(), ^{ [[NSUserDefaults standardUserDefaults] setObject:@"Deleting..." forKey:specifier.key]; }); - [appDelegate.database inTransaction:^(FMDatabase *db, BOOL *rollback) { - [db executeUpdate:@"DROP TABLE unread_hashes"]; - [db executeUpdate:@"DROP TABLE unread_counts"]; - [db executeUpdate:@"DROP TABLE accounts"]; - [db executeUpdate:@"DROP TABLE stories"]; - [db executeUpdate:@"DROP TABLE cached_images"]; - [appDelegate setupDatabase:db]; + [appDelegate.database inDatabase:^(FMDatabase *db) { + [db executeUpdate:@"VACUUM"]; + [appDelegate setupDatabase:db force:YES]; + [appDelegate deleteAllCachedImages]; + dispatch_sync(dispatch_get_main_queue(), ^{ + [[NSUserDefaults standardUserDefaults] setObject:@"Cleared all stories and images!" + forKey:specifier.key]; + }); }]; - [appDelegate deleteAllCachedImages]; - dispatch_sync(dispatch_get_main_queue(), ^{ - [[NSUserDefaults standardUserDefaults] setObject:@"Cleared all stories and images!" - forKey:specifier.key]; - }); }); } } @@ -1388,21 +1374,17 @@ heightForHeaderInSection:(NSInteger)section { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_feed_as_read", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - for (NSString *feedId in feedIds) { - [request addPostValue:feedId forKey:@"feed_id"]; - } + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:feedIds forKey:@"feed_id"]; if (days) { - [request setPostValue:[NSNumber numberWithInteger:cutoffTimestamp] + [params setObject:[NSNumber numberWithInteger:cutoffTimestamp] forKey:@"cutoff_timestamp"]; } - [request setDidFinishSelector:@selector(finishMarkAllAsRead:)]; - [request setDidFailSelector:@selector(requestFailedMarkStoryRead:)]; - [request setUserInfo:@{@"feeds": feedIds, - @"cutoffTimestamp": [NSNumber numberWithInteger:cutoffTimestamp]}]; - [request setDelegate:self]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAllAsRead:params]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailedMarkStoryRead:error withParams:params]; + }]; if (!days) { for (NSString *feedId in feedIds) { @@ -1414,22 +1396,20 @@ heightForHeaderInSection:(NSInteger)section { } - (void)markEverythingReadWithDays:(NSInteger)days { - NSTimeInterval cutoffTimestamp = [[NSDate date] timeIntervalSince1970]; - cutoffTimestamp -= (days * 60*60*24); NSArray *feedIds = [appDelegate allFeedIds]; NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_all_as_read", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:[NSNumber numberWithInteger:days] - forKey:@"days"]; - [request setDidFinishSelector:@selector(finishMarkAllAsRead:)]; - [request setDidFailSelector:@selector(requestFailedMarkStoryRead:)]; - [request setUserInfo:@{@"feeds": feedIds, - @"cutoffTimestamp": [NSNumber numberWithInteger:cutoffTimestamp]}]; - [request setDelegate:self]; - [request startAsynchronous]; + + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[NSNumber numberWithInteger:days] + forKey:@"days"]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAllAsRead:params]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailedMarkStoryRead:error withParams:params]; + }]; if (!days) { for (NSString *feedId in feedIds) { @@ -1444,41 +1424,37 @@ heightForHeaderInSection:(NSInteger)section { NSDictionary *feedsStories = [appDelegate markVisibleStoriesRead]; NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_feed_stories_as_read", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:[feedsStories JSONRepresentation] forKey:@"feeds_stories"]; - [request setDelegate:self]; - [request setUserInfo:@{@"stories": feedsStories}]; - [request setDidFinishSelector:@selector(finishMarkAllAsRead:)]; - [request setDidFailSelector:@selector(requestFailedMarkStoryRead:)]; - [request startAsynchronous]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[feedsStories JSONRepresentation] forKey:@"feeds_stories"]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAllAsRead:params]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailedMarkStoryRead:error withParams:params]; + }]; } -- (void)requestFailedMarkStoryRead:(ASIFormDataRequest *)request { - [appDelegate markStoriesRead:[request.userInfo objectForKey:@"stories"] - inFeeds:[request.userInfo objectForKey:@"feeds"] - cutoffTimestamp:[[request.userInfo objectForKey:@"cutoffTimestamp"] integerValue]]; +- (void)requestFailedMarkStoryRead:(NSError *)error withParams:(NSDictionary *)params { + [appDelegate markStoriesRead:[params objectForKey:@"stories"] + inFeeds:[params objectForKey:@"feed_id"] + cutoffTimestamp:[[params objectForKey:@"cutoff_timestamp"] integerValue]]; [self showOfflineNotifier]; self.isOffline = YES; [self.feedTitlesTable reloadData]; } -- (void)finishMarkAllAsRead:(ASIFormDataRequest *)request { - if (request.responseStatusCode != 200) { - [self requestFailedMarkStoryRead:request]; - return; - } - +- (void)finishMarkAllAsRead:(NSDictionary *)params { + // This seems fishy post-ASI rewrite. This needs to know about a cutoff timestamp which it is never given. self.isOffline = NO; - if ([[request.userInfo objectForKey:@"cutoffTimestamp"] integerValue]) { + if ([[params objectForKey:@"cutoff_timestamp"] integerValue]) { id feed; - if ([[request.userInfo objectForKey:@"feeds"] count] == 1) { - feed = [[request.userInfo objectForKey:@"feeds"] objectAtIndex:0]; + if ([[params objectForKey:@"feed_id"] count] == 1) { + feed = [[params objectForKey:@"feed_id"] objectAtIndex:0]; } [self refreshFeedList:feed]; - } else if ([request.userInfo objectForKey:@"feeds"]) { - [appDelegate markFeedReadInCache:[request.userInfo objectForKey:@"feeds"]]; + } else if ([params objectForKey:@"feed_id"]) { + [appDelegate markFeedReadInCache:[params objectForKey:@"feed_id"]]; } } @@ -1738,13 +1714,12 @@ heightForHeaderInSection:(NSInteger)section { - (void)loadFavicons { NSString *urlString = [NSString stringWithFormat:@"%@/reader/favicons", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - - [request setDidFinishSelector:@selector(saveAndDrawFavicons:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setDelegate:self]; - [request startAsynchronous]; + + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self saveAndDrawFavicons:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } - (void)loadAvatars { @@ -1770,18 +1745,9 @@ heightForHeaderInSection:(NSInteger)section { -- (void)saveAndDrawFavicons:(ASIHTTPRequest *)request { - __block NSString *responseString = [request responseString]; - +- (void)saveAndDrawFavicons:(NSDictionary *)results { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0ul); dispatch_async(queue, ^{ - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - for (id feed_id in results) { // NSMutableDictionary *feed = [[appDelegate.dictFeeds objectForKey:feed_id] mutableCopy]; // [feed setValue:[results objectForKey:feed_id] forKey:@"favicon"]; @@ -1801,11 +1767,9 @@ heightForHeaderInSection:(NSInteger)section { [self loadAvatars]; }); }); - } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSError *error = [request error]; +- (void)requestFailed:(NSError *)error { NSLog(@"Error: %@", error); [appDelegate informError:error]; } @@ -1838,26 +1802,26 @@ heightForHeaderInSection:(NSInteger)section { urlString = [NSString stringWithFormat:@"%@/reader/refresh_feeds", self.appDelegate.url]; } - NSURL *urlFeedList = [NSURL URLWithString:urlString]; if (!feedId) { [self.appDelegate cancelOfflineQueue]; } - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlFeedList]; - [[NSHTTPCookieStorage sharedHTTPCookieStorage] - setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; - [request setValidatesSecureCertificate:NO]; - [request setDelegate:self]; - [request setResponseEncoding:NSUTF8StringEncoding]; - [request setDefaultResponseEncoding:NSUTF8StringEncoding]; - if (feedId) { - [request setUserInfo:@{@"feedId": [NSString stringWithFormat:@"%@", feedId]}]; - } - [request setDidFinishSelector:@selector(finishRefreshingFeedList:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setTimeOutSeconds:30]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishRefreshingFeedList:responseObject feedId:feedId]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response; + + if ([httpResponse statusCode] == 403) { + NSLog(@"Showing login after refresh"); + return [appDelegate showLogin]; + } else if ([httpResponse statusCode] == 503) { + return [self informError:@"In maintenance mode"]; + } else if ([httpResponse statusCode] >= 500) { + return [self informError:@"The server barfed!"]; + } + [self requestFailed:error]; + }]; dispatch_async(dispatch_get_main_queue(), ^{ if (!feedId) { @@ -1867,26 +1831,9 @@ heightForHeaderInSection:(NSInteger)section { } -- (void)finishRefreshingFeedList:(ASIHTTPRequest *)request { - if ([request responseStatusCode] == 403) { - NSLog(@"Showing login after refresh"); - return [appDelegate showLogin]; - } else if ([request responseStatusCode] == 503) { - return [self informError:@"In maintenance mode"]; - } else if ([request responseStatusCode] >= 500) { - return [self informError:@"The server barfed!"]; - } - +- (void)finishRefreshingFeedList:(NSDictionary *)results feedId:(NSString *)feedId { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - NSDictionary *newFeedCounts = [results objectForKey:@"feeds"]; NSInteger intelligenceLevel = [appDelegate selectedIntelligence]; for (id feed in newFeedCounts) { @@ -1945,7 +1892,7 @@ heightForHeaderInSection:(NSInteger)section { [appDelegate.folderCountCache removeAllObjects]; [self.feedTitlesTable reloadData]; [self refreshHeaderCounts]; - if (![request.userInfo objectForKey:@"feedId"]) { + if (!feedId) { [self.appDelegate startOfflineQueue]; } [self loadFavicons]; @@ -2058,31 +2005,6 @@ heightForHeaderInSection:(NSInteger)section { userInfoBarButton, nil]; } -- (void)checkForFeedNotifications { - NSMutableArray *notificationFeedIds = [NSMutableArray array]; - - for (NSDictionary *feed in appDelegate.dictFeeds.allValues) { - NSArray *types = [feed objectForKey:@"notification_types"]; - if (types) { - for (NSString *notificationType in types) { - if ([notificationType isEqualToString:@"ios"]) { - [appDelegate registerForRemoteNotifications]; - } - } - if ([types count]) { - [notificationFeedIds addObject:[feed objectForKey:@"id"]]; - } - } - } - - appDelegate.notificationFeedIds = [notificationFeedIds sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { - NSString *feed1Title = [[[appDelegate.dictFeeds objectForKey:[NSString stringWithFormat:@"%@", obj1]] objectForKey:@"feed_title"] lowercaseString]; - NSString *feed2Title = [[[appDelegate.dictFeeds objectForKey:[NSString stringWithFormat:@"%@", obj2]] objectForKey:@"feed_title"] lowercaseString]; - - return [feed1Title compare:feed2Title]; - }]; -} - - (void)refreshHeaderCounts { if (!appDelegate.activeUsername) { userAvatarButton.customView.hidden = YES; @@ -2146,13 +2068,13 @@ heightForHeaderInSection:(NSInteger)section { [self finishRefresh]; } -- (void)showSyncingNotifier:(float)progress hoursBack:(int)hours { +- (void)showSyncingNotifier:(float)progress hoursBack:(NSInteger)hours { // [self.notifier hide]; self.notifier.style = NBSyncingProgressStyle; if (hours < 2) { self.notifier.title = @"Storing past hour"; } else if (hours < 24) { - self.notifier.title = [NSString stringWithFormat:@"Storing past %d hours", hours]; + self.notifier.title = [NSString stringWithFormat:@"Storing past %ld hours", (long)hours]; } else if (hours < 48) { self.notifier.title = @"Storing yesterday"; } else { @@ -2163,13 +2085,13 @@ heightForHeaderInSection:(NSInteger)section { [self.notifier show]; } -- (void)showCachingNotifier:(float)progress hoursBack:(int)hours { +- (void)showCachingNotifier:(float)progress hoursBack:(NSInteger)hours { // [self.notifier hide]; self.notifier.style = NBSyncingProgressStyle; if (hours < 2) { self.notifier.title = @"Images from last hour"; } else if (hours < 24) { - self.notifier.title = [NSString stringWithFormat:@"Images from %d hours ago", hours]; + self.notifier.title = [NSString stringWithFormat:@"Images from %ld hours ago", (long)hours]; } else if (hours < 48) { self.notifier.title = @"Images from yesterday"; } else { diff --git a/clients/ios/Classes/NotificationFeedCell.m b/clients/ios/Classes/NotificationFeedCell.m index e12ac58f6..c127b9ae5 100644 --- a/clients/ios/Classes/NotificationFeedCell.m +++ b/clients/ios/Classes/NotificationFeedCell.m @@ -33,6 +33,7 @@ [selectedBackground setBackgroundColor:UIColorFromRGB(0xECEEEA)]; [self setSelectedBackgroundView:selectedBackground]; + NSDictionary *controlAttrs = @{NSForegroundColorAttributeName: [UIColor lightGrayColor]}; self.filterControl = [[UISegmentedControl alloc] initWithItems:@[@"Unread Stories", @"Focus Stories"]]; self.filterControl.tintColor = UIColorFromRGB(0x8F918B); @@ -44,6 +45,7 @@ [self.filterControl setImage:[UIImage imageNamed:@"unread_green.png"] forSegmentAtIndex:1]; [self.filterControl setWidth:CGRectGetWidth(self.frame)*0.46 forSegmentAtIndex:0]; [self.filterControl setWidth:CGRectGetWidth(self.frame)*0.46 forSegmentAtIndex:1]; + [self.filterControl setTitleTextAttributes:controlAttrs forState:UIControlStateNormal]; self.filterControl.frame = CGRectMake(36, 38, CGRectGetWidth(self.frame), 28); [self.contentView addSubview:self.filterControl]; @@ -65,6 +67,7 @@ [self.notificationTypeControl setWidth:CGRectGetWidth(self.frame)*0.23 forSegmentAtIndex:1]; [self.notificationTypeControl setWidth:CGRectGetWidth(self.frame)*0.23 forSegmentAtIndex:2]; [self.notificationTypeControl setWidth:CGRectGetWidth(self.frame)*0.23 forSegmentAtIndex:3]; + [self.notificationTypeControl setTitleTextAttributes:controlAttrs forState:UIControlStateNormal]; self.notificationTypeControl.frame = CGRectMake(36, 76, CGRectGetWidth(self.frame), 28); [self.contentView addSubview:self.notificationTypeControl]; } @@ -75,20 +78,26 @@ - (void)layoutSubviews { [super layoutSubviews]; - self.imageView.frame = CGRectMake(10.0, 10.0, 16.0, 16.0); - self.imageView.contentMode = UIViewContentModeScaleAspectFit; - - CGRect frame = self.textLabel.frame; - frame.origin.x = 35.0; - frame.size.width = self.detailTextLabel.frame.origin.x - self.textLabel.frame.origin.x; - self.textLabel.frame = frame; - self.textLabel.backgroundColor = [UIColor clearColor]; self.textLabel.textColor = UIColorFromRGB(0x303030); self.textLabel.shadowColor = UIColorFromRGB(0xF0F0F0); self.textLabel.shadowOffset = CGSizeMake(0, 1); + self.textLabel.highlightedTextColor = UIColorFromRGB(0x303030); + self.detailTextLabel.highlightedTextColor = UIColorFromRGB(0x505050); + self.detailTextLabel.textColor = UIColorFromRGB(0x505050); + self.backgroundColor = UIColorFromRGB(0xFFFFFF); + self.backgroundView.backgroundColor = UIColorFromRGB(0xFFFFFF); + self.selectedBackgroundView.backgroundColor = UIColorFromRGB(0xECEEEA); - CGRect textFrame = self.textLabel.frame; + self.imageView.frame = CGRectMake(10.0, 10.0, 16.0, 16.0); + self.imageView.contentMode = UIViewContentModeScaleAspectFit; + + [self.textLabel sizeToFit]; + CGRect frame = self.textLabel.frame; + frame.origin.x = 35.0; + frame.size.width = self.detailTextLabel.frame.origin.x - self.textLabel.frame.origin.x; + self.textLabel.frame = frame; + CGRect textFrame = self.textLabel.frame; textFrame.origin.y = 10; self.textLabel.frame = textFrame; @@ -96,15 +105,6 @@ detailFrame.origin.y = 10; self.detailTextLabel.frame = detailFrame; - self.textLabel.highlightedTextColor = UIColorFromRGB(0x303030); - self.detailTextLabel.highlightedTextColor = UIColorFromRGB(0x505050); - - self.detailTextLabel.textColor = UIColorFromRGB(0x505050); - - self.backgroundColor = UIColorFromRGB(0xFFFFFF); - self.backgroundView.backgroundColor = UIColorFromRGB(0xFFFFFF); - self.selectedBackgroundView.backgroundColor = UIColorFromRGB(0xECEEEA); - CGFloat detailTextLabelWidth = self.detailTextLabel.attributedText.size.width; CGRect detailTextLabelFrame = self.detailTextLabel.frame; CGFloat detailTextLabelExtraWidth = detailTextLabelWidth - detailTextLabelFrame.size.width; @@ -114,7 +114,7 @@ self.detailTextLabel.frame = detailTextLabelFrame; CGRect textLabelFrame = self.textLabel.frame; - textLabelFrame.size.width -= detailTextLabelExtraWidth; + textLabelFrame.size.width = self.detailTextLabel.frame.origin.x - self.textLabel.frame.origin.x; self.textLabel.frame = textLabelFrame; } @@ -153,21 +153,23 @@ NSString *urlString = [NSString stringWithFormat:@"%@/notifications/feed/", appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:self.feedId forKey:@"feed_id"]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:self.feedId forKey:@"feed_id"]; + NSMutableArray *notifications = [NSMutableArray array]; for (NSString *notificationType in notificationTypes) { - [request addPostValue:notificationType forKey:@"notification_types"]; + [notifications addObject:notificationType]; } - [request setPostValue:notificationFilter forKey:@"notification_filter"]; - [request setCompletionBlock:^{ + [params setObject:notifications forKey:@"notification_types"]; + [params setObject:notificationFilter forKey:@"notification_filter"]; + + [appDelegate updateNotifications:params feed:self.feedId]; + + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Saved notifications %@: %@ / %@", self.feedId, notificationTypes, notificationFilter); - }]; - [request setFailedBlock:^{ + [appDelegate checkForFeedNotifications]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed to save notifications: %@ / %@", notificationTypes, notificationFilter); }]; - [request setDelegate:self]; - [request startAsynchronous]; } @end diff --git a/clients/ios/Classes/NotificationsViewController.h b/clients/ios/Classes/NotificationsViewController.h index 1d7191377..b138ddd5b 100644 --- a/clients/ios/Classes/NotificationsViewController.h +++ b/clients/ios/Classes/NotificationsViewController.h @@ -13,6 +13,7 @@ @interface NotificationsViewController : UIViewController { NewsBlurAppDelegate *appDelegate; + NSArray *notificationFeedIds; } @property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate; diff --git a/clients/ios/Classes/NotificationsViewController.m b/clients/ios/Classes/NotificationsViewController.m index f1d7f5a59..4213c3dc1 100644 --- a/clients/ios/Classes/NotificationsViewController.m +++ b/clients/ios/Classes/NotificationsViewController.m @@ -38,6 +38,7 @@ notificationsTable.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);; notificationsTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; notificationsTable.separatorStyle = UITableViewCellSeparatorStyleNone; + [self.view addSubview:notificationsTable]; } @@ -53,8 +54,17 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; + + notificationsTable.backgroundColor = UIColorFromRGB(0xECEEEA); + notificationsTable.separatorColor = UIColorFromRGB(0xF0F0F0); + notificationsTable.sectionIndexColor = UIColorFromRGB(0x303030); + notificationsTable.sectionIndexBackgroundColor = UIColorFromRGB(0xDCDFD6); + + notificationFeedIds = [appDelegate.notificationFeedIds copy]; [notificationsTable reloadData]; + [notificationsTable setContentOffset:CGPointZero]; + self.view.backgroundColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR); } @@ -104,11 +114,11 @@ viewForHeaderInSection:(NSInteger)section { customView.opaque = NO; headerLabel.backgroundColor = [UIColor clearColor]; headerLabel.opaque = NO; - headerLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0]; + headerLabel.textColor = UIColorFromRGB(0x4C4C4C); headerLabel.highlightedTextColor = UIColorFromRGB(NEWSBLUR_WHITE_COLOR); headerLabel.font = [UIFont boldSystemFontOfSize:11]; headerLabel.frame = CGRectMake(36.0, 1.0, 286.0, headerLabelHeight); - headerLabel.shadowColor = [UIColor colorWithRed:.94 green:0.94 blue:0.97 alpha:1.0]; + headerLabel.shadowColor = UIColorFromRGB(0xF0F0F7); headerLabel.shadowOffset = CGSizeMake(0.0, 1.0); if (self.feedId && section == 0) { headerLabel.text = @"SITE NOTIFICATIONS"; @@ -152,11 +162,31 @@ viewForHeaderInSection:(NSInteger)section { if (self.feedId && section == 0) { return 1; } - return MAX(appDelegate.notificationFeedIds.count, 1); + return MAX(notificationFeedIds.count, 1); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - CGRect vb = self.view.bounds; + BOOL feedSection = NO; + if (self.feedId != nil && indexPath.section == 0) feedSection = YES; + if (notificationFeedIds.count == 0 && !feedSection) { + UITableViewCell *cell = [[UITableViewCell alloc] init]; + cell.selectionStyle = UITableViewCellSelectionStyleNone; + cell.backgroundColor = [UIColor clearColor]; + CGRect vb = self.view.bounds; + CGFloat height = [self tableView:tableView heightForRowAtIndexPath:indexPath]; + UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, vb.size.width, height)]; + [cell.contentView addSubview:msg]; + msg.text = @"No notifications yet."; + msg.textColor = UIColorFromRGB(0x7a7a7a); + if (vb.size.width > 320) { + msg.font = [UIFont fontWithName:@"Helvetica-Bold" size: 20.0]; + } else { + msg.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14.0]; + } + msg.textAlignment = NSTextAlignmentCenter; + + return cell; + } static NSString *CellIdentifier = @"NotificationFeedCellIdentifier"; NotificationFeedCell *cell = [tableView @@ -167,37 +197,24 @@ viewForHeaderInSection:(NSInteger)section { initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } - - if (appDelegate.notificationFeedIds.count == 0) { - UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, vb.size.width, 140)]; - [cell.contentView addSubview:msg]; - msg.text = @"No results."; - msg.textColor = UIColorFromRGB(0x7a7a7a); - if (vb.size.width > 320) { - msg.font = [UIFont fontWithName:@"Helvetica-Bold" size: 20.0]; - } else { - msg.font = [UIFont fontWithName:@"Helvetica-Bold" size: 14.0]; - } - msg.textAlignment = NSTextAlignmentCenter; - } else { - NSDictionary *feed; - NSString *feedIdStr; - if (self.feedId && indexPath.section == 0) { - feedIdStr = feedId; - feed = [appDelegate.dictFeeds objectForKey:feedId]; - } else { - feedIdStr = [NSString stringWithFormat:@"%@", - appDelegate.notificationFeedIds[indexPath.row]]; - feed = [appDelegate.dictFeeds objectForKey:feedIdStr]; - } - cell.feedId = feedIdStr; - cell.textLabel.text = [feed objectForKey:@"feed_title"]; - cell.imageView.image = [self.appDelegate getFavicon:feedIdStr isSocial:NO isSaved:NO]; - cell.detailTextLabel.text = [NSString localizedStringWithFormat:NSLocalizedString(@"%@ stories/month", @"average stories per month"), feed[@"average_stories_per_month"]]; - } - + cell.selectionStyle = UITableViewCellSelectionStyleNone; + NSDictionary *feed; + NSString *feedIdStr; + if (self.feedId && indexPath.section == 0) { + feedIdStr = feedId; + feed = [appDelegate.dictFeeds objectForKey:feedId]; + } else { + feedIdStr = [NSString stringWithFormat:@"%@", + notificationFeedIds[indexPath.row]]; + feed = [appDelegate.dictFeeds objectForKey:feedIdStr]; + } + cell.feedId = feedIdStr; + cell.textLabel.text = [feed objectForKey:@"feed_title"]; + cell.imageView.image = [self.appDelegate getFavicon:feedIdStr isSocial:NO isSaved:NO]; + cell.detailTextLabel.text = [NSString localizedStringWithFormat:NSLocalizedString(@"%@ stories/month", @"average stories per month"), feed[@"average_stories_per_month"]]; + return cell; } diff --git a/clients/ios/Classes/OriginalStoryViewController.m b/clients/ios/Classes/OriginalStoryViewController.m index d420802e5..851c3523c 100644 --- a/clients/ios/Classes/OriginalStoryViewController.m +++ b/clients/ios/Classes/OriginalStoryViewController.m @@ -6,7 +6,6 @@ // Copyright 2010 NewsBlur. All rights reserved. // -#import "NewsBlurAppDelegate.h" #import "NBContainerViewController.h" #import "OriginalStoryViewController.h" #import "NSString+HTML.h" diff --git a/clients/ios/Classes/ProfileBadge.h b/clients/ios/Classes/ProfileBadge.h index 644d5bc10..8e78e8789 100644 --- a/clients/ios/Classes/ProfileBadge.h +++ b/clients/ios/Classes/ProfileBadge.h @@ -9,7 +9,6 @@ #import @class NewsBlurAppDelegate; -@class ASIHTTPRequest; @interface ProfileBadge : UITableViewCell { NewsBlurAppDelegate *appDelegate; @@ -40,9 +39,6 @@ - (void)refreshWithProfile:(NSDictionary *)profile showStats:(BOOL)showStats withWidth:(int)newWidth; - (IBAction)doFollowButton:(id)sender; -- (void)finishFollowing:(ASIHTTPRequest *)request; -- (void)finishUnfollowing:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)initProfile; @end diff --git a/clients/ios/Classes/ProfileBadge.m b/clients/ios/Classes/ProfileBadge.m index 8479c02ce..d429e1029 100644 --- a/clients/ios/Classes/ProfileBadge.m +++ b/clients/ios/Classes/ProfileBadge.m @@ -9,7 +9,6 @@ #import "ProfileBadge.h" #import "NewsBlurAppDelegate.h" #import "Utilities.h" -#import "ASIHTTPRequest.h" #import "UIImageView+AFNetworking.h" #import @@ -325,31 +324,22 @@ self.appDelegate.url]; } - NSURL *url = [NSURL URLWithString:urlString]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[self.activeProfile objectForKey:@"user_id"] forKey:@"user_id"]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setDelegate:self]; - [request setPostValue:[self.activeProfile objectForKey:@"user_id"] forKey:@"user_id"]; - if ([self.followButton.currentTitle isEqualToString:@"Follow"]) { - [request setDidFinishSelector:@selector(finishFollowing:)]; - } else { - [request setDidFinishSelector:@selector(finishUnfollowing:)]; - } - [request setDidFailSelector:@selector(requestFailed:)]; - - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + if ([self.followButton.currentTitle isEqualToString:@"Follow"]) { + [self finishFollowing:responseObject]; + } else { + [self finishUnfollowing:responseObject]; + } + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)finishFollowing:(ASIHTTPRequest *)request { - +- (void)finishFollowing:(NSDictionary *)results { [self.activityIndicator stopAnimating]; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { @@ -373,15 +363,9 @@ } -- (void)finishUnfollowing:(ASIHTTPRequest *)request { +- (void)finishUnfollowing:(NSDictionary *)results { [self.activityIndicator stopAnimating]; - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; + int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { NSLog(@"ERROR"); @@ -405,9 +389,8 @@ [self refreshWithProfile:newProfile showStats:self.shouldShowStats withWidth:0]; } -- (void)requestFailed:(ASIHTTPRequest *)request { +- (void)requestFailed:(NSError *)error { [self.activityIndicator stopAnimating]; - NSError *error = [request error]; NSLog(@"Error: %@", error); [appDelegate informError:error]; } diff --git a/clients/ios/Classes/ShareViewController.h b/clients/ios/Classes/ShareViewController.h index 608127c22..a92959b38 100644 --- a/clients/ios/Classes/ShareViewController.h +++ b/clients/ios/Classes/ShareViewController.h @@ -9,8 +9,7 @@ #import #import "NewsBlurAppDelegate.h" -@interface ShareViewController : BaseViewController { - NewsBlurAppDelegate *appDelegate; +@interface ShareViewController : BaseViewController { NSString *activeReplyId; } @@ -34,9 +33,6 @@ - (IBAction)doToggleButton:(id)sender; - (IBAction)doShareThisStory:(id)sender; - (IBAction)doReplyToComment:(id)sender; -- (void)finishShareThisStory:(ASIHTTPRequest *)request; -- (void)finishAddReply:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)replaceStory:(NSDictionary *)newStory withReplyId:(NSString *)replyId; - (void)adjustShareButtons; - (void)adjustCommentField:(CGSize)kbSize; diff --git a/clients/ios/Classes/ShareViewController.m b/clients/ios/Classes/ShareViewController.m index 122bdc0ba..585a4199e 100644 --- a/clients/ios/Classes/ShareViewController.m +++ b/clients/ios/Classes/ShareViewController.m @@ -14,7 +14,6 @@ #import #import "Utilities.h" #import "DataUtilities.h" -#import "ASIHTTPRequest.h" #import "StoriesCollection.h" #import "NSString+HTML.h" @@ -354,64 +353,51 @@ [appDelegate.storyPageControl showShareHUD:@"Sharing"]; NSString *urlString = [NSString stringWithFormat:@"%@/social/share_story", self.appDelegate.url]; - - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + NSMutableArray *services = [NSMutableArray array]; NSString *feedIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]]; NSString *storyIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"id"]]; - [request setPostValue:feedIdStr forKey:@"feed_id"]; - [request setPostValue:storyIdStr forKey:@"story_id"]; + [params setObject:feedIdStr forKey:@"feed_id"]; + [params setObject:storyIdStr forKey:@"story_id"]; if (facebookButton.selected) { - [request addPostValue:@"facebook" forKey:@"post_to_services"]; + [services addObject:@"facebook"]; } if (twitterButton.selected) { - [request addPostValue:@"twitter" forKey:@"post_to_services"]; - } - if (appdotnetButton.selected) { - [request addPostValue:@"appdotnet" forKey:@"post_to_services"]; + [services addObject:@"twitter"]; } + [params setObject:services forKey:@"post_to_services"]; if (appDelegate.storiesCollection.isSocialRiverView) { if ([[appDelegate.activeStory objectForKey:@"friend_user_ids"] count] > 0) { - [request setPostValue:[NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"friend_user_ids"][0]] forKey:@"source_user_id"]; + [params setObject:[NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"friend_user_ids"][0]] forKey:@"source_user_id"]; } else if ([[appDelegate.activeStory objectForKey:@"public_user_ids"] count] > 0) { - [request setPostValue:[NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"public_user_ids"][0]] forKey:@"source_user_id"]; + [params setObject:[NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"public_user_ids"][0]] forKey:@"source_user_id"]; } } else { if ([appDelegate.activeStory objectForKey:@"social_user_id"] != nil) { NSString *sourceUserIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"social_user_id"]]; - [request setPostValue:sourceUserIdStr forKey:@"source_user_id"]; + [params setObject:sourceUserIdStr forKey:@"source_user_id"]; } } NSString *comments = commentField.text; if ([comments length]) { - [request setPostValue:comments forKey:@"comments"]; + [params setObject:comments forKey:@"comments"]; } - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishShareThisStory:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishShareThisStory:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; + [appDelegate hideShareView:YES]; } -- (void)finishShareThisStory:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - - if (request.responseStatusCode != 200) { - return [self requestFailed:request]; - } - +- (void)finishShareThisStory:(NSDictionary *)results { NSArray *userProfiles = [results objectForKey:@"user_profiles"]; appDelegate.storiesCollection.activeFeedUserProfiles = [DataUtilities updateUserProfiles:appDelegate.storiesCollection.activeFeedUserProfiles @@ -437,53 +423,36 @@ NSString *feedIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"story_feed_id"]]; NSString *storyIdStr = [NSString stringWithFormat:@"%@", [appDelegate.activeStory objectForKey:@"id"]]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - [request setPostValue:feedIdStr forKey:@"story_feed_id"]; - [request setPostValue:storyIdStr forKey:@"story_id"]; - [request setPostValue:[appDelegate.activeComment objectForKey:@"user_id"] forKey:@"comment_user_id"]; - [request setPostValue:commentField.text forKey:@"reply_comments"]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:feedIdStr forKey:@"story_feed_id"]; + [params setObject:storyIdStr forKey:@"story_id"]; + [params setObject:[appDelegate.activeComment objectForKey:@"user_id"] forKey:@"comment_user_id"]; + [params setObject:commentField.text forKey:@"reply_comments"]; if (self.activeReplyId) { - [request setPostValue:activeReplyId forKey:@"reply_id"]; + [params setObject:activeReplyId forKey:@"reply_id"]; } - [request setDelegate:self]; - [request setDidFinishSelector:@selector(finishAddReply:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishAddReply:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; + [appDelegate hideShareView:NO]; } -- (void)finishAddReply:(ASIHTTPRequest *)request { +- (void)finishAddReply:(NSDictionary *)results { NSLog(@"Successfully added."); - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - if (request.responseStatusCode != 200) { - return [self requestFailed:request]; - } - // add the comment into the activeStory dictionary NSDictionary *newStory = [DataUtilities updateComment:results for:appDelegate]; [self replaceStory:newStory withReplyId:[results objectForKey:@"reply_id"]]; } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSString *error; - +- (void)requestFailed:(NSError *)error { [MBProgressHUD hideHUDForView:appDelegate.storyPageControl.view animated:NO]; - - if ([request error]) { - error = [NSString stringWithFormat:@"%@", [request error]]; - } else { - error = @"The server barfed!"; - } + NSLog(@"Error: %@", error); [appDelegate.storyPageControl.currentPage informError:error]; } diff --git a/clients/ios/Classes/StoriesCollection.h b/clients/ios/Classes/StoriesCollection.h index de4dc0ef6..541f68538 100644 --- a/clients/ios/Classes/StoriesCollection.h +++ b/clients/ios/Classes/StoriesCollection.h @@ -104,10 +104,6 @@ - (void)toggleStorySaved; - (BOOL)toggleStorySaved:(NSDictionary *)story; - (void)syncStoryAsSaved:(NSDictionary *)story; -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsSaved:(ASIFormDataRequest *)request; - (void)syncStoryAsUnsaved:(NSDictionary *)story; -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request; -- (void)failedMarkAsUnsaved:(ASIFormDataRequest *)request; @end diff --git a/clients/ios/Classes/StoriesCollection.m b/clients/ios/Classes/StoriesCollection.m index 6fd94bce0..2eb15f7cb 100644 --- a/clients/ios/Classes/StoriesCollection.m +++ b/clients/ios/Classes/StoriesCollection.m @@ -343,28 +343,26 @@ - (void)syncStoryAsRead:(NSDictionary *)story { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_story_hashes_as_read", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[story objectForKey:@"story_hash"] + forKey:@"story_hash"]; + [params setObject:[story objectForKey:@"story_feed_id"] + forKey:@"story_feed_id"]; - [request setPostValue:[story objectForKey:@"story_hash"] - forKey:@"story_hash"]; - - [request setDidFinishSelector:@selector(finishMarkAsRead:)]; - [request setDidFailSelector:@selector(failedMarkAsRead:)]; - [request setDelegate:self]; - [request setUserInfo:story]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAsRead:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedMarkAsRead:params]; + }]; } -- (void)finishMarkAsRead:(ASIFormDataRequest *)request { - if ([request responseStatusCode] != 200) { - return [self failedMarkAsRead:request]; - } +- (void)finishMarkAsRead:(NSDictionary *)results { + } -- (void)failedMarkAsRead:(ASIFormDataRequest *)request { - NSString *storyFeedId = [request.userInfo objectForKey:@"story_feed_id"]; - NSString *storyHash = [request.userInfo objectForKey:@"story_hash"]; +- (void)failedMarkAsRead:(NSDictionary *)params { + NSString *storyFeedId = [params objectForKey:@"story_feed_id"]; + NSString *storyHash = [params objectForKey:@"story_hash"]; [appDelegate queueReadStories:@{storyFeedId: @[storyHash]}]; } @@ -372,38 +370,27 @@ - (void)syncStoryAsUnread:(NSDictionary *)story { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_story_as_unread", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; - [request setPostValue:[story objectForKey:@"story_hash"] + [params setObject:[story objectForKey:@"story_hash"] forKey:@"story_id"]; - [request setPostValue:[story objectForKey:@"story_feed_id"] + [params setObject:[story objectForKey:@"story_feed_id"] forKey:@"feed_id"]; - [request setDidFinishSelector:@selector(finishMarkAsUnread:)]; - [request setDidFailSelector:@selector(failedMarkAsUnread:)]; - [request setDelegate:self]; - [request setUserInfo:story]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAsUnread:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedMarkAsUnread:params]; + }]; } -- (void)finishMarkAsUnread:(ASIFormDataRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; +- (void)finishMarkAsUnread:(NSDictionary *)results { - if ([request responseStatusCode] != 200 || [[results objectForKey:@"code"] integerValue] < 0) { - return [self failedMarkAsUnread:request]; - } } -- (void)failedMarkAsUnread:(ASIFormDataRequest *)request { - NSString *storyFeedId = [request.userInfo objectForKey:@"story_feed_id"]; - NSString *storyHash = [request.userInfo objectForKey:@"story_hash"]; +- (void)failedMarkAsUnread:(NSDictionary *)params { + NSString *storyFeedId = [params objectForKey:@"story_feed_id"]; + NSString *storyHash = [params objectForKey:@"story_hash"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { @@ -413,7 +400,7 @@ // Offline means can't unread a story unless it was read while offline. [self markStoryRead:storyHash feedId:storyFeedId]; // [self.storyTitlesTable reloadData]; - [appDelegate failedMarkAsUnread:request]; + [appDelegate failedMarkAsUnread:params]; } else { // Offline but read story while offline, so it never touched the server. [appDelegate.unreadStoryHashes setObject:[NSNumber numberWithBool:YES] forKey:storyHash]; @@ -723,93 +710,66 @@ - (void)syncStoryAsSaved:(NSDictionary *)story { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_story_as_starred", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; - [request setPostValue:[story objectForKey:@"story_hash"] + [params setObject:[story objectForKey:@"story_hash"] forKey:@"story_id"]; - [request setPostValue:[story objectForKey:@"story_feed_id"] + [params setObject:[story objectForKey:@"story_feed_id"] forKey:@"feed_id"]; + + NSMutableArray *tags = [NSMutableArray array]; for (NSString *userTag in [story objectForKey:@"user_tags"]) { - [request addPostValue:userTag - forKey:@"user_tags"]; + [tags addObject:userTag]; } + [params setObject:tags forKey:@"user_tags"]; - [request setDidFinishSelector:@selector(finishMarkAsSaved:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setUserInfo:@{@"story_feed_id":[story - objectForKey:@"story_feed_id"], - @"story_hash":[story - objectForKey:@"story_hash"]}]; - [request setDelegate:self]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAsSaved:responseObject withParams:params]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self markStory:params asSaved:NO]; + [appDelegate failedMarkAsSaved:params]; + }]; } -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request { - if ([request responseStatusCode] != 200) { - return [self failedMarkAsSaved:request]; - } +- (void)finishMarkAsSaved:(NSDictionary *)results withParams:(NSDictionary *)params { + [self updateSavedStoryCounts:results withParams:params]; - [self updateSavedStoryCounts:request]; - - [appDelegate finishMarkAsSaved:request]; + [appDelegate finishMarkAsSaved:params]; } -- (void)updateSavedStoryCounts:(ASIFormDataRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; +- (void)updateSavedStoryCounts:(NSDictionary *)results withParams:(NSDictionary *)params { NSArray *savedStories = [appDelegate updateStarredStoryCounts:results]; NSMutableDictionary *allFolders = [appDelegate.dictFolders mutableCopy]; [allFolders setValue:savedStories forKey:@"saved_stories"]; appDelegate.dictFolders = allFolders; } -- (void)failedMarkAsSaved:(ASIFormDataRequest *)request { - [self markStory:request.userInfo asSaved:NO]; - - [appDelegate failedMarkAsSaved:request]; -} - -- (void)syncStoryAsUnsaved:(NSDictionary *)story { +- (void)syncStoryAsUnsaved:(NSDictionary *)story { NSString *urlString = [NSString stringWithFormat:@"%@/reader/mark_story_as_unstarred", self.appDelegate.url]; - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - - [request setPostValue:[story - objectForKey:@"story_hash"] - forKey:@"story_id"]; - [request setPostValue:[story - objectForKey:@"story_feed_id"] - forKey:@"feed_id"]; - - [request setDidFinishSelector:@selector(finishMarkAsUnsaved:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setUserInfo:@{@"story_feed_id":[story - objectForKey:@"story_feed_id"], - @"story_hash":[story - objectForKey:@"story_hash"]}]; - [request setDelegate:self]; - [request startAsynchronous]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[story + objectForKey:@"story_hash"] + forKey:@"story_id"]; + [params setObject:[story + objectForKey:@"story_feed_id"] + forKey:@"feed_id"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishMarkAsUnsaved:responseObject withParams:params]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self markStory:params asSaved:YES]; + [appDelegate failedMarkAsUnsaved:params]; + }]; } -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request { - if ([request responseStatusCode] != 200) { - return [self failedMarkAsUnsaved:request]; - } - - [self updateSavedStoryCounts:request]; - [appDelegate finishMarkAsUnsaved:request]; +- (void)finishMarkAsUnsaved:(NSDictionary *)results withParams:(NSDictionary *)params { + [self updateSavedStoryCounts:results withParams:params]; + [appDelegate finishMarkAsUnsaved:params]; } -- (void)failedMarkAsUnsaved:(ASIFormDataRequest *)request { - [self markStory:request.userInfo asSaved:YES]; - [appDelegate failedMarkAsUnsaved:request]; +- (void)failedMarkAsUnsaved:(NSDictionary *)params { + [self markStory:params asSaved:YES]; + [appDelegate failedMarkAsUnsaved:params]; } @end diff --git a/clients/ios/Classes/StoryDetailViewController.h b/clients/ios/Classes/StoryDetailViewController.h index 5a1db4e94..ccd450e82 100644 --- a/clients/ios/Classes/StoryDetailViewController.h +++ b/clients/ios/Classes/StoryDetailViewController.h @@ -11,7 +11,6 @@ #import "BaseViewController.h" @class NewsBlurAppDelegate; -@class ASIHTTPRequest; @interface StoryDetailViewController : BaseViewController { BOOL inDoubleTap; BOOL hasScrolled; NSURL *activeLongPressUrl; - NSInteger actionSheetViewImageIndex; - NSInteger actionSheetCopyImageIndex; - NSInteger actionSheetSaveImageIndex; CGSize preRotateSize; CGFloat scrollPct; @@ -80,10 +76,7 @@ UIActionSheetDelegate> { - (void)openShareDialog; - (void)openTrainingDialog:(int)x yCoordinate:(int)y width:(int)width height:(int)height; - (void)openUserTagsDialog:(int)x yCoordinate:(int)y width:(int)width height:(int)height; -- (void)finishLikeComment:(ASIHTTPRequest *)request; - (void)subscribeToBlurblog; -- (void)finishSubscribeToBlurblog:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)setActiveStoryAtIndex:(NSInteger)activeStoryIndex; - (NSString *)getHeader; - (NSString *)getShareBar; @@ -99,6 +92,5 @@ UIActionSheetDelegate> { - (void)showTextOrStoryView; - (void)showStoryView; - (void)fetchTextView; -- (void)finishFetchTextView:(ASIHTTPRequest *)request; @end diff --git a/clients/ios/Classes/StoryDetailViewController.m b/clients/ios/Classes/StoryDetailViewController.m index a416c0c99..b0f3acb76 100644 --- a/clients/ios/Classes/StoryDetailViewController.m +++ b/clients/ios/Classes/StoryDetailViewController.m @@ -7,6 +7,7 @@ // #import +#import #import "StoryDetailViewController.h" #import "NewsBlurAppDelegate.h" #import "NewsBlurViewController.h" @@ -15,9 +16,6 @@ #import "UserProfileViewController.h" #import "ShareViewController.h" #import "StoryPageControl.h" -#import "ASIHTTPRequest.h" -#import "ASIFormDataRequest.h" -#import "AFHTTPRequestOperation.h" #import "Base64.h" #import "Utilities.h" #import "NSString+HTML.h" @@ -293,9 +291,11 @@ - (void)viewWillLayoutSubviews { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; [super viewWillLayoutSubviews]; - [appDelegate.storyPageControl layoutForInterfaceOrientation:orientation]; - [self changeWebViewWidth]; - [self drawFeedGradient]; + dispatch_async(dispatch_get_main_queue(), ^{ + [appDelegate.storyPageControl layoutForInterfaceOrientation:orientation]; + [self changeWebViewWidth]; + [self drawFeedGradient]; + }); // NSLog(@"viewWillLayoutSubviews: %.2f", self.webView.scrollView.bounds.size.width); } @@ -375,7 +375,7 @@ fontStyleClass = [userPreferences stringForKey:@"fontStyle"]; if (!fontStyleClass) { - fontStyleClass = @"NB-helvetica"; + fontStyleClass = @"GothamNarrow-Book"; } fontSizeClass = [fontSizeClass stringByAppendingString:[userPreferences stringForKey:@"story_font_size"]]; @@ -1537,7 +1537,7 @@ shouldStartLoadWithRequest:(NSURLRequest *)request return NO; } } else if ([url.host hasSuffix:@"itunes.apple.com"]) { - [[UIApplication sharedApplication] openURL:url]; + [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; return NO; } @@ -1716,39 +1716,19 @@ shouldStartLoadWithRequest:(NSURLRequest *)request self.appDelegate.url]; } - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[self.activeStory objectForKey:@"id"] forKey:@"story_id"]; + [params setObject:[self.activeStory objectForKey:@"story_feed_id"] forKey:@"story_feed_id"]; + [params setObject:[appDelegate.activeComment objectForKey:@"user_id"] forKey:@"comment_user_id"]; - - [request setPostValue:[self.activeStory - objectForKey:@"id"] - forKey:@"story_id"]; - [request setPostValue:[self.activeStory - objectForKey:@"story_feed_id"] - forKey:@"story_feed_id"]; - - - [request setPostValue:[appDelegate.activeComment objectForKey:@"user_id"] forKey:@"comment_user_id"]; - - [request setDidFinishSelector:@selector(finishLikeComment:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setDelegate:self]; - [request startAsynchronous]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishLikeComment:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; } -- (void)finishLikeComment:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - - if (request.responseStatusCode != 200) { - return [self requestFailed:request]; - } - +- (void)finishLikeComment:(NSDictionary *)results { // add the comment into the activeStory dictionary NSDictionary *newStory = [DataUtilities updateComment:results for:appDelegate]; @@ -1776,17 +1756,11 @@ shouldStartLoadWithRequest:(NSURLRequest *)request } -- (void)requestFailed:(ASIHTTPRequest *)request { - NSLog(@"Error in story detail: %@", [request error]); - NSString *error; +- (void)requestFailed:(NSError *)error { + NSLog(@"Error in story detail: %@", error); [MBProgressHUD hideHUDForView:appDelegate.storyPageControl.view animated:NO]; - - if ([request error]) { - error = [NSString stringWithFormat:@"%@", [request error]]; - } else { - error = @"The server barfed!"; - } + [self informError:error]; } @@ -1909,17 +1883,28 @@ shouldStartLoadWithRequest:(NSURLRequest *)request title = title.length ? title : alt; activeLongPressUrl = [NSURL URLWithString:src]; - UIActionSheet *actions = [[UIActionSheet alloc] initWithTitle:title.length ? title : nil - delegate:self - cancelButtonTitle:@"Done" - destructiveButtonTitle:nil - otherButtonTitles:nil]; - actionSheetViewImageIndex = [actions addButtonWithTitle:@"View and zoom"]; - actionSheetCopyImageIndex = [actions addButtonWithTitle:@"Copy image"]; - actionSheetSaveImageIndex = [actions addButtonWithTitle:@"Save to camera roll"]; - [actions showFromRect:CGRectMake(pt.x, pt.y, 1, 1) - inView:appDelegate.storyPageControl.view animated:YES]; -// [actions showInView:appDelegate.storyPageControl.view]; + UIAlertController *alert = [UIAlertController alertControllerWithTitle:title.length ? title : nil + message:nil + preferredStyle:UIAlertControllerStyleActionSheet]; + [alert addAction:[UIAlertAction actionWithTitle:@"View and zoom" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [appDelegate showOriginalStory:activeLongPressUrl]; + }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Copy image" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [self fetchImage:activeLongPressUrl copy:YES save:NO]; + }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Save to camera roll" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [self fetchImage:activeLongPressUrl copy:NO save:YES]; + }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { + + }]]; + + [alert setModalPresentationStyle:UIModalPresentationPopover]; + + UIPopoverPresentationController *popover = [alert popoverPresentationController]; + popover.sourceRect = CGRectMake(pt.x, pt.y, 1, 1); + popover.sourceView = appDelegate.storyPageControl.view; + [self presentViewController:alert animated:YES completion:nil]; } - (void)showLinkContextMenu:(CGPoint)pt { @@ -1986,38 +1971,38 @@ shouldStartLoadWithRequest:(NSURLRequest *)request return pt; } -- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { - if (buttonIndex == actionSheetViewImageIndex) { - [appDelegate showOriginalStory:activeLongPressUrl]; - } else if (buttonIndex == actionSheetCopyImageIndex || - buttonIndex == actionSheetSaveImageIndex) { - [self fetchImage:activeLongPressUrl buttonIndex:buttonIndex]; - } -} - -- (void)fetchImage:(NSURL *)url buttonIndex:(NSInteger)buttonIndex { +- (void)fetchImage:(NSURL *)url copy:(BOOL)copy save:(BOOL)save { [MBProgressHUD hideHUDForView:self.webView animated:YES]; - [appDelegate.storyPageControl showShareHUD:buttonIndex == actionSheetCopyImageIndex ? + [appDelegate.storyPageControl showShareHUD:copy ? @"Copying..." : @"Saving..."]; - NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; - AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] - initWithRequest:urlRequest]; - [requestOperation setResponseSerializer:[AFImageResponseSerializer serializer]]; - [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; + [manager setResponseSerializer:[AFImageResponseSerializer serializer]]; + [manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { UIImage *image = responseObject; - if (buttonIndex == actionSheetCopyImageIndex) { + if (copy) { [UIPasteboard generalPasteboard].image = image; [self flashCheckmarkHud:@"copied"]; - } else if (buttonIndex == actionSheetSaveImageIndex) { - UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); - [self flashCheckmarkHud:@"saved"]; } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (save) { + [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ + PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; + changeRequest.creationDate = [NSDate date]; + } completionHandler:^(BOOL success, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (success) { + [self flashCheckmarkHud:@"saved"]; + } else { + [MBProgressHUD hideHUDForView:self.webView animated:NO]; + [self informError:error]; + } + }); + }]; + } + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [MBProgressHUD hideHUDForView:self.webView animated:YES]; [self informError:@"Could not fetch image"]; }]; - [requestOperation start]; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { @@ -2033,21 +2018,19 @@ shouldStartLoadWithRequest:(NSURLRequest *)request [appDelegate.storyPageControl showShareHUD:@"Following"]; NSString *urlString = [NSString stringWithFormat:@"%@/social/follow", self.appDelegate.url]; - - NSURL *url = [NSURL URLWithString:urlString]; - ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; - - [request setPostValue:[appDelegate.storiesCollection.activeFeed + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[appDelegate.storiesCollection.activeFeed objectForKey:@"user_id"] forKey:@"user_id"]; - [request setDidFinishSelector:@selector(finishSubscribeToBlurblog:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request setDelegate:self]; - [request startAsynchronous]; -} + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishSubscribeToBlurblog:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self requestFailed:error]; + }]; +} -- (void)finishSubscribeToBlurblog:(ASIHTTPRequest *)request { +- (void)finishSubscribeToBlurblog:(NSDictionary *)results { [MBProgressHUD hideHUDForView:appDelegate.storyPageControl.view animated:NO]; self.storyHUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES]; self.storyHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]]; @@ -2293,17 +2276,18 @@ shouldStartLoadWithRequest:(NSURLRequest *)request NSString *urlString = [NSString stringWithFormat:@"%@/rss_feeds/original_text", self.appDelegate.url]; - ASIFormDataRequest *request = [self formRequestWithURL:urlString]; - [request addPostValue:[self.activeStory objectForKey:@"id"] forKey:@"story_id"]; - [request addPostValue:[self.activeStory objectForKey:@"story_feed_id"] forKey:@"feed_id"]; - [request setUserInfo:@{@"storyId": [self.activeStory objectForKey:@"id"]}]; - [request setDidFinishSelector:@selector(finishFetchTextView:)]; - [request setDidFailSelector:@selector(failedFetchText:)]; - [request setDelegate:self]; - [request startAsynchronous]; + NSMutableDictionary *params = [NSMutableDictionary dictionary]; + [params setObject:[self.activeStory objectForKey:@"id"] forKey:@"story_id"]; + [params setObject:[self.activeStory objectForKey:@"story_feed_id"] forKey:@"feed_id"]; + NSString *storyId = [self.activeStory objectForKey:@"id"]; + [appDelegate.networkManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self finishFetchTextView:responseObject storyId:storyId]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self failedFetchText:error]; + }]; } -- (void)failedFetchText:(ASIHTTPRequest *)request { +- (void)failedFetchText:(NSError *)error { [self.appDelegate.storyPageControl hideNotifier]; [MBProgressHUD hideHUDForView:self.webView animated:YES]; if (self.activeStory == appDelegate.storyPageControl.currentPage.activeStory) { @@ -2313,22 +2297,13 @@ shouldStartLoadWithRequest:(NSURLRequest *)request [appDelegate.storyPageControl setTextButton:self]; } -- (void)finishFetchTextView:(ASIHTTPRequest *)request { - NSString *responseString = [request responseString]; - NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - +- (void)finishFetchTextView:(NSDictionary *)results storyId:(NSString *)storyId { if ([[results objectForKey:@"failed"] boolValue]) { - [self failedFetchText:request]; + [self failedFetchText:nil]; return; } - if (![[request.userInfo objectForKey:@"storyId"] - isEqualToString:[self.activeStory objectForKey:@"id"]]) { + if (![storyId isEqualToString:[self.activeStory objectForKey:@"id"]]) { [self.appDelegate.storyPageControl hideNotifier]; [MBProgressHUD hideHUDForView:self.webView animated:YES]; self.inTextView = NO; diff --git a/clients/ios/Classes/StoryPageControl.h b/clients/ios/Classes/StoryPageControl.h index 94fd4b511..d92563094 100644 --- a/clients/ios/Classes/StoryPageControl.h +++ b/clients/ios/Classes/StoryPageControl.h @@ -8,18 +8,13 @@ #import #import "BaseViewController.h" -#import "NewsBlurAppDelegate.h" #import "THCircularProgressView.h" #import "NBNotifier.h" - -@class NewsBlurAppDelegate; -@class ASIHTTPRequest; +#import "StoryDetailViewController.h" @interface StoryPageControl : BaseViewController { - NewsBlurAppDelegate *appDelegate; - THCircularProgressView *circularProgressView; UIButton *buttonPrevious; UIButton *buttonNext; @@ -38,7 +33,6 @@ CGFloat scrollPct; } -@property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate; @property (nonatomic) StoryDetailViewController *currentPage; @property (nonatomic) StoryDetailViewController *nextPage; @property (nonatomic) StoryDetailViewController *previousPage; @@ -96,16 +90,10 @@ - (void)animateIntoPlace:(BOOL)animated; - (void)changePage:(NSInteger)pageIndex; - (void)changePage:(NSInteger)pageIndex animated:(BOOL)animated; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)setNextPreviousButtons; - (void)setTextButton; - (void)setTextButton:(StoryDetailViewController *)storyViewController; -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request; -- (BOOL)failedMarkAsSaved:(ASIFormDataRequest *)request; -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request; -- (BOOL)failedMarkAsUnsaved:(ASIFormDataRequest *)request; -- (BOOL)failedMarkAsUnread:(ASIFormDataRequest *)request; - (void)subscribeToBlurblog; - (IBAction)toggleFontSize:(id)sender; @@ -127,4 +115,10 @@ - (IBAction)tapProgressBar:(id)sender; - (IBAction)toggleTextView:(id)sender; +- (void)finishMarkAsSaved:(NSDictionary *)params; +- (BOOL)failedMarkAsSaved:(NSDictionary *)params; +- (void)finishMarkAsUnsaved:(NSDictionary *)params; +- (BOOL)failedMarkAsUnsaved:(NSDictionary *)params; +- (BOOL)failedMarkAsUnread:(NSDictionary *)params; + @end diff --git a/clients/ios/Classes/StoryPageControl.m b/clients/ios/Classes/StoryPageControl.m index 575497b5f..6f648b411 100644 --- a/clients/ios/Classes/StoryPageControl.m +++ b/clients/ios/Classes/StoryPageControl.m @@ -14,8 +14,6 @@ #import "FontSettingsViewController.h" #import "UserProfileViewController.h" #import "ShareViewController.h" -#import "ASIHTTPRequest.h" -#import "ASIFormDataRequest.h" #import "Base64.h" #import "Utilities.h" #import "NSString+HTML.h" @@ -29,7 +27,6 @@ @implementation StoryPageControl -@synthesize appDelegate; @synthesize currentPage, nextPage, previousPage; @synthesize circularProgressView; @synthesize separatorBarButton; @@ -67,6 +64,7 @@ - (void)viewDidLoad { [super viewDidLoad]; + appDelegate = [NewsBlurAppDelegate sharedAppDelegate]; currentPage = [[StoryDetailViewController alloc] initWithNibName:@"StoryDetailViewController" bundle:nil]; @@ -559,7 +557,7 @@ } - (BOOL)isPhoneOrCompact { - return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone || self.appDelegate.isCompactWidth; + return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone || appDelegate.isCompactWidth; } - (void)updateTraverseBackground { @@ -623,15 +621,15 @@ if (newIndex > 0 && newIndex >= [appDelegate.storiesCollection.activeFeedStoryLocations count]) { pageController.pageIndex = -2; - if (self.appDelegate.storiesCollection.feedPage < 100 && - !self.appDelegate.feedDetailViewController.pageFinished && - !self.appDelegate.feedDetailViewController.pageFetching) { - [self.appDelegate.feedDetailViewController fetchNextPage:^() { + if (appDelegate.storiesCollection.feedPage < 100 && + !appDelegate.feedDetailViewController.pageFinished && + !appDelegate.feedDetailViewController.pageFetching) { + [appDelegate.feedDetailViewController fetchNextPage:^() { // NSLog(@"Fetched next page, %d stories", [appDelegate.activeFeedStoryLocations count]); [self applyNewIndex:newIndex pageController:pageController]; }]; - } else if (!self.appDelegate.feedDetailViewController.pageFinished && - !self.appDelegate.feedDetailViewController.pageFetching) { + } else if (!appDelegate.feedDetailViewController.pageFinished && + !appDelegate.feedDetailViewController.pageFetching) { [appDelegate.navigationController popToViewController:[appDelegate.navigationController.viewControllers objectAtIndex:0] @@ -981,7 +979,7 @@ buttonNext.enabled = YES; NSInteger nextIndex = [appDelegate.storiesCollection indexOfNextUnreadStory]; NSInteger unreadCount = [appDelegate unreadCount]; - BOOL pageFinished = self.appDelegate.feedDetailViewController.pageFinished; + BOOL pageFinished = appDelegate.feedDetailViewController.pageFinished; if ((nextIndex == -1 && unreadCount > 0 && !pageFinished) || nextIndex != -1) { [buttonNext setTitle:[@"Next" uppercaseString] forState:UIControlStateNormal]; @@ -1043,18 +1041,14 @@ [appDelegate openTrainStory:self.fontSettingsButton]; } -- (void)finishMarkAsSaved:(ASIFormDataRequest *)request { - if ([request responseStatusCode] != 200) { - return [self requestFailed:request]; - } - +- (void)finishMarkAsSaved:(NSDictionary *)params { [appDelegate.feedDetailViewController redrawUnreadStory]; [self refreshHeaders]; [self.currentPage flashCheckmarkHud:@"saved"]; } -- (BOOL)failedMarkAsSaved:(ASIFormDataRequest *)request { - if (![[request.userInfo objectForKey:@"story_hash"] +- (BOOL)failedMarkAsSaved:(NSDictionary *)params { + if (![[params objectForKey:@"story_hash"] isEqualToString:[currentPage.activeStory objectForKey:@"story_hash"]]) { return NO; } @@ -1063,20 +1057,16 @@ return YES; } -- (void)finishMarkAsUnsaved:(ASIFormDataRequest *)request { - if ([request responseStatusCode] != 200) { - return [self requestFailed:request]; - } - - [appDelegate.storiesCollection markStory:[request.userInfo objectForKey:@"story"] asSaved:NO]; +- (void)finishMarkAsUnsaved:(NSDictionary *)params { + [appDelegate.storiesCollection markStory:[params objectForKey:@"story"] asSaved:NO]; [appDelegate.feedDetailViewController redrawUnreadStory]; [self refreshHeaders]; [self.currentPage flashCheckmarkHud:@"unsaved"]; } -- (BOOL)failedMarkAsUnsaved:(ASIFormDataRequest *)request { - if (![[request.userInfo objectForKey:@"story_hash"] +- (BOOL)failedMarkAsUnsaved:(NSDictionary *)params { + if (![[params objectForKey:@"story_hash"] isEqualToString:[currentPage.activeStory objectForKey:@"story_hash"]]) { return NO; } @@ -1085,8 +1075,8 @@ return YES; } -- (BOOL)failedMarkAsUnread:(ASIFormDataRequest *)request { - if (![[request.userInfo objectForKey:@"story_hash"] +- (BOOL)failedMarkAsUnread:(NSDictionary *)params { + if (![[params objectForKey:@"story_hash"] isEqualToString:[currentPage.activeStory objectForKey:@"story_hash"]]) { return NO; } @@ -1163,10 +1153,10 @@ - (IBAction)toggleFontSize:(id)sender { - UINavigationController *fontSettingsNavigationController = self.appDelegate.fontSettingsNavigationController; + UINavigationController *fontSettingsNavigationController = appDelegate.fontSettingsNavigationController; [fontSettingsNavigationController popToRootViewControllerAnimated:NO]; - [self.appDelegate showPopoverWithViewController:fontSettingsNavigationController contentSize:CGSizeZero barButtonItem:self.fontSettingsButton]; + [appDelegate showPopoverWithViewController:fontSettingsNavigationController contentSize:CGSizeZero barButtonItem:self.fontSettingsButton]; } - (void)setFontStyle:(NSString *)fontStyle { @@ -1230,10 +1220,10 @@ #pragma mark Story Traversal - (IBAction)doNextUnreadStory:(id)sender { - FeedDetailViewController *fdvc = self.appDelegate.feedDetailViewController; + FeedDetailViewController *fdvc = appDelegate.feedDetailViewController; NSInteger nextLocation = [appDelegate.storiesCollection locationOfNextUnreadStory]; NSInteger unreadCount = [appDelegate unreadCount]; - BOOL pageFinished = self.appDelegate.feedDetailViewController.pageFinished; + BOOL pageFinished = appDelegate.feedDetailViewController.pageFinished; [self.loadingIndicator stopAnimating]; diff --git a/clients/ios/Classes/ThemeManager.m b/clients/ios/Classes/ThemeManager.m index e735e115f..9623b0ea2 100644 --- a/clients/ios/Classes/ThemeManager.m +++ b/clients/ios/Classes/ThemeManager.m @@ -203,7 +203,7 @@ NSString * const ThemeStyleDark = @"dark"; colors = [NSMutableSet set]; } - [colors addObject:[NSString stringWithFormat:@"0x%06lX", rgbValue]]; + [colors addObject:[NSString stringWithFormat:@"0x%06lX", (long)rgbValue]]; NSLog(@"all unique colors: %@", [[colors allObjects] sortedArrayUsingSelector:@selector(compare:)]); // log } diff --git a/clients/ios/Classes/TrainerViewController.h b/clients/ios/Classes/TrainerViewController.h index 789d392f7..9e420d025 100644 --- a/clients/ios/Classes/TrainerViewController.h +++ b/clients/ios/Classes/TrainerViewController.h @@ -53,4 +53,4 @@ - (IBAction)doCloseDialog:(id)sender; -@end \ No newline at end of file +@end diff --git a/clients/ios/Classes/TrainerViewController.m b/clients/ios/Classes/TrainerViewController.m index a4ca0977d..15661b3b5 100644 --- a/clients/ios/Classes/TrainerViewController.m +++ b/clients/ios/Classes/TrainerViewController.m @@ -80,37 +80,29 @@ MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; HUD.labelText = @"Loading trainer..."; NSString *feedId = [self feedId]; - NSURL *url = [NSURL URLWithString:[NSString - stringWithFormat:@"%@/reader/feeds_trainer?feed_id=%@", - self.appDelegate.url, feedId]]; - - __weak __typeof(&*self)weakSelf = self; - AFHTTPRequestOperation *request = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]]; - [request setResponseSerializer:[AFJSONResponseSerializer serializer]]; - [request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { - __strong __typeof(&*weakSelf)strongSelf = weakSelf; - if (!strongSelf) return; - [MBProgressHUD hideHUDForView:strongSelf.view animated:YES]; - NSDictionary *results = [responseObject objectAtIndex:0]; + NSString *urlString = [NSString stringWithFormat:@"%@/reader/feeds_trainer?feed_id=%@", + self.appDelegate.url, feedId]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [MBProgressHUD hideHUDForView:self.view animated:YES]; + NSDictionary *results = responseObject; NSMutableDictionary *newClassifiers = [[results objectForKey:@"classifiers"] mutableCopy]; [appDelegate.storiesCollection.activeClassifiers setObject:newClassifiers forKey:feedId]; appDelegate.storiesCollection.activePopularAuthors = [results objectForKey:@"feed_authors"]; appDelegate.storiesCollection.activePopularTags = [results objectForKey:@"feed_tags"]; [self renderTrainer]; - } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed fetch trainer: %@", error); [self informError:@"Could not load trainer"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^() { - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [appDelegate hidePopover]; - } else { - [appDelegate.navigationController dismissViewControllerAnimated:YES completion:nil]; - } - }); + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [appDelegate hidePopover]; + } else { + [appDelegate.navigationController dismissViewControllerAnimated:YES completion:nil]; + } + }); }]; - [request start]; } else { [self renderTrainer]; } diff --git a/clients/ios/Classes/UnreadCountView.h b/clients/ios/Classes/UnreadCountView.h index 3089d819f..01b716d6f 100644 --- a/clients/ios/Classes/UnreadCountView.h +++ b/clients/ios/Classes/UnreadCountView.h @@ -21,17 +21,17 @@ typedef enum { } NBFeedListType; @property (nonatomic) NewsBlurAppDelegate *appDelegate; -@property (assign, nonatomic) int psWidth; -@property (assign, nonatomic) int psPadding; -@property (assign, nonatomic) int ntWidth; -@property (assign, nonatomic) int ntPadding; -@property (assign, nonatomic) int psCount; -@property (assign, nonatomic) int ntCount; -@property (assign, nonatomic) int blueCount; +@property (assign, nonatomic) NSInteger psWidth; +@property (assign, nonatomic) NSInteger psPadding; +@property (assign, nonatomic) NSInteger ntWidth; +@property (assign, nonatomic) NSInteger ntPadding; +@property (assign, nonatomic) NSInteger psCount; +@property (assign, nonatomic) NSInteger ntCount; +@property (assign, nonatomic) NSInteger blueCount; @property (assign, nonatomic) CGRect rect; -- (void)drawInRect:(CGRect)r ps:(int)ps nt:(int)nt listType:(NBFeedListType)listType; -- (void)calculateOffsets:(int)ps nt:(int)nt; -- (int)offsetWidth; +- (void)drawInRect:(CGRect)r ps:(NSInteger)ps nt:(NSInteger)nt listType:(NBFeedListType)listType; +- (void)calculateOffsets:(NSInteger)ps nt:(NSInteger)nt; +- (NSInteger)offsetWidth; @end diff --git a/clients/ios/Classes/UnreadCountView.m b/clients/ios/Classes/UnreadCountView.m index e6f80c14c..543d16613 100644 --- a/clients/ios/Classes/UnreadCountView.m +++ b/clients/ios/Classes/UnreadCountView.m @@ -32,7 +32,7 @@ const int COUNT_HEIGHT = 15; return [self drawInRect:r ps:psCount nt:ntCount listType:NBFeedListFolder]; } -- (void)drawInRect:(CGRect)r ps:(int)ps nt:(int)nt listType:(NBFeedListType)listType { +- (void)drawInRect:(CGRect)r ps:(NSInteger)ps nt:(NSInteger)nt listType:(NBFeedListType)listType { rect = CGRectInset(r, 12, 12); rect.size.width -= 18; // Scrollbar padding @@ -47,8 +47,8 @@ const int COUNT_HEIGHT = 15; } [self calculateOffsets:ps nt:nt]; - int psOffset = ps == 0 ? 0 : psWidth - 20; - int ntOffset = nt == 0 ? 0 : ntWidth - 20; + NSInteger psOffset = ps == 0 ? 0 : psWidth - 20; + NSInteger ntOffset = nt == 0 ? 0 : ntWidth - 20; if (ps > 0 || blueCount) { CGRect rr; @@ -81,7 +81,7 @@ const int COUNT_HEIGHT = 15; [UIView drawRoundRectangleInRect:rr withRadius:4]; - NSString *psStr = [NSString stringWithFormat:@"%d", ps]; + NSString *psStr = [NSString stringWithFormat:@"%ld", (long)ps]; CGSize size = [psStr sizeWithAttributes:@{NSFontAttributeName: indicatorFont}]; float x_pos = (rr.size.width - size.width) / 2; float y_pos = (rr.size.height - size.height) / 2; @@ -125,7 +125,7 @@ const int COUNT_HEIGHT = 15; [UIColorFromLightDarkRGB(0xB3B6AD, 0xA3A69D) set]; [UIView drawRoundRectangleInRect:rr withRadius:4]; - NSString *ntStr = [NSString stringWithFormat:@"%d", nt]; + NSString *ntStr = [NSString stringWithFormat:@"%ld", (long)nt]; CGSize size = [ntStr sizeWithAttributes:@{NSFontAttributeName: indicatorFont}]; float x_pos = (rr.size.width - size.width) / 2; float y_pos = (rr.size.height - size.height) / 2; @@ -142,7 +142,7 @@ const int COUNT_HEIGHT = 15; } } -- (void)calculateOffsets:(int)ps nt:(int)nt { +- (void)calculateOffsets:(NSInteger)ps nt:(NSInteger)nt { psWidth = ps == 0 ? 0 : ps < 10 ? 14 : ps < 100 ? 22 : ps < 1000 ? 28 : ps < 10000 ? 34 : 40; ntWidth = nt == 0 ? 0 : nt < 10 ? 14 : nt < 100 ? 22 : nt < 1000 ? 28 : nt < 10000 ? 34 : 40; @@ -150,8 +150,8 @@ const int COUNT_HEIGHT = 15; ntPadding = nt == 0 ? 0 : 2; } -- (int)offsetWidth { - int width = 0; +- (NSInteger)offsetWidth { + NSInteger width = 0; if (self.psCount > 0) { width += psWidth + psPadding; } diff --git a/clients/ios/Classes/UserProfileViewController.h b/clients/ios/Classes/UserProfileViewController.h index 8cb93adf0..98f4d2c33 100644 --- a/clients/ios/Classes/UserProfileViewController.h +++ b/clients/ios/Classes/UserProfileViewController.h @@ -7,13 +7,13 @@ // #import -#import "ASIHTTPRequest.h" +#import "NewsBlurAppDelegate.h" @class NewsBlurAppDelegate; @class ProfileBadge; -@interface UserProfileViewController : UIViewController - { +@interface UserProfileViewController : BaseViewController + { NewsBlurAppDelegate *appDelegate; UILabel *followingCount; @@ -31,11 +31,8 @@ @property (nonatomic) NSArray *activitiesArray; @property (nonatomic) NSString *activitiesUsername; @property (nonatomic) NSDictionary *userProfile; -@property (nonatomic) ASIHTTPRequest *request; - (void)getUserProfile; -- (void)requestFinished:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - (void)doCancelButton; @end diff --git a/clients/ios/Classes/UserProfileViewController.m b/clients/ios/Classes/UserProfileViewController.m index ef6aff9b1..7b89382a1 100644 --- a/clients/ios/Classes/UserProfileViewController.m +++ b/clients/ios/Classes/UserProfileViewController.m @@ -11,7 +11,6 @@ #import "ProfileBadge.h" #import "SmallActivityCell.h" #import "FollowGrid.h" -#import "ASIHTTPRequest.h" #import "Utilities.h" #import "MBProgressHUD.h" #import @@ -24,7 +23,6 @@ @synthesize activitiesArray; @synthesize activitiesUsername; @synthesize userProfile; -@synthesize request; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { @@ -38,8 +36,6 @@ - (void)dealloc { self.profileTable.dataSource = nil; self.profileTable.delegate = nil; - request.delegate = nil; - [request cancel]; } - (void)viewDidLoad { @@ -114,27 +110,17 @@ "&category=follow&category=comment_reply&category=comment_like&category=sharedstory", self.appDelegate.url, appDelegate.activeUserProfileId]; - NSURL *url = [NSURL URLWithString:urlString]; - request = [ASIHTTPRequest requestWithURL:url]; - - [request setDelegate:self]; - [request setDidFinishSelector:@selector(requestFinished:)]; - [request setDidFailSelector:@selector(requestFailed:)]; - [request startAsynchronous]; + [appDelegate.networkManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self requestFinished:responseObject]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self informError:error]; + }]; } -- (void)requestFinished:(ASIHTTPRequest *)_request { +- (void)requestFinished:(NSDictionary *)results { [MBProgressHUD hideHUDForView:self.view animated:YES]; - NSString *responseString = [_request responseString]; - NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error; - NSDictionary *results = [NSJSONSerialization - JSONObjectWithData:responseData - options:kNilOptions - error:&error]; - // int statusCode = [request responseStatusCode]; int code = [[results valueForKey:@"code"] intValue]; if (code == -1) { NSLog(@"ERROR"); @@ -161,12 +147,6 @@ [self.view addSubview:self.profileTable]; } -- (void)requestFailed:(ASIHTTPRequest *)_request { - NSError *error = [_request error]; - NSLog(@"Error: %@", error); - [appDelegate informError:error]; -} - #pragma mark - #pragma mark Table View - Profile Modules List diff --git a/clients/ios/Classes/offline/OfflineFetchImages.h b/clients/ios/Classes/offline/OfflineFetchImages.h index d15236d44..cbbca53ef 100644 --- a/clients/ios/Classes/offline/OfflineFetchImages.h +++ b/clients/ios/Classes/offline/OfflineFetchImages.h @@ -9,17 +9,12 @@ #import #import "NewsBlurAppDelegate.h" #import "FMDatabaseQueue.h" -#import "ASINetworkQueue.h" @interface OfflineFetchImages : NSOperation @property (nonatomic) NewsBlurAppDelegate *appDelegate; -@property (readwrite) ASINetworkQueue *imageDownloadOperationQueue; - (BOOL)fetchImages; - (NSArray *)uncachedImageUrls; -- (void)storeCachedImage:(ASIHTTPRequest *)request; -- (void)storeFailedImage:(ASIHTTPRequest *)request; -- (void)cachedImageQueueFinished:(ASINetworkQueue *)queue; @end diff --git a/clients/ios/Classes/offline/OfflineFetchImages.m b/clients/ios/Classes/offline/OfflineFetchImages.m index e08cbc438..4dddf722c 100644 --- a/clients/ios/Classes/offline/OfflineFetchImages.m +++ b/clients/ios/Classes/offline/OfflineFetchImages.m @@ -14,7 +14,6 @@ #import "Utilities.h" @implementation OfflineFetchImages -@synthesize imageDownloadOperationQueue; @synthesize appDelegate; - (void)main { @@ -29,27 +28,18 @@ - (BOOL)fetchImages { if (self.isCancelled) { NSLog(@"Images cancelled."); - [imageDownloadOperationQueue cancelAllOperations]; return NO; } -// NSLog(@"Fetching images..."); + NSLog(@"Fetching images..."); NSArray *urls = [self uncachedImageUrls]; - if (imageDownloadOperationQueue) { - [imageDownloadOperationQueue cancelAllOperations]; - imageDownloadOperationQueue = nil; - } - imageDownloadOperationQueue = [[ASINetworkQueue alloc] init]; - imageDownloadOperationQueue.maxConcurrentOperationCount = 8; - imageDownloadOperationQueue.delegate = self; - if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"offline_image_download"] boolValue] || ![[[NSUserDefaults standardUserDefaults] objectForKey:@"offline_allowed"] boolValue] || [urls count] == 0) { - NSLog(@"Finished caching images. %d total", appDelegate.totalUncachedImagesCount); + NSLog(@"Finished caching images. %ld total", (long)appDelegate.totalUncachedImagesCount); dispatch_async(dispatch_get_main_queue(), ^{ [appDelegate.feedsViewController showDoneNotifier]; [appDelegate.feedsViewController hideNotifier]; @@ -68,31 +58,38 @@ } - NSMutableArray *downloadRequests = [NSMutableArray array]; - for (NSArray *urlArray in urls) { - NSURL *url = [NSURL URLWithString:[urlArray objectAtIndex:0]]; - NSString *storyHash = [urlArray objectAtIndex:1]; - NSString *storyTimestamp = [urlArray objectAtIndex:2]; - - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; - [request setValidatesSecureCertificate:NO]; - [request setUserInfo:@{@"story_hash": storyHash, @"story_timestamp": storyTimestamp}]; - [request setDelegate:self]; - [request setDidFinishSelector:@selector(storeCachedImage:)]; - [request setDidFailSelector:@selector(storeFailedImage:)]; - [request setTimeOutSeconds:5]; - [downloadRequests addObject:request]; - } - [imageDownloadOperationQueue setQueueDidFinishSelector:@selector(cachedImageQueueFinished:)]; - [imageDownloadOperationQueue setShouldCancelAllRequestsOnFailure:NO]; - [imageDownloadOperationQueue go]; - [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; - [imageDownloadOperationQueue addOperations:downloadRequests waitUntilFinished:YES]; + NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; + AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration]; + [manager.requestSerializer setTimeoutInterval:5]; + manager.responseSerializer = [AFImageResponseSerializer serializer]; + manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); + + dispatch_group_t group = dispatch_group_create(); - for (ASIHTTPRequest *request in downloadRequests) { - [request setRawResponseData:nil]; + for (NSArray *urlArray in urls) { + NSString *urlString = [[urlArray objectAtIndex:0] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + NSString *storyHash = [urlArray objectAtIndex:1]; + NSInteger storyTimestamp = [[urlArray objectAtIndex:2] integerValue]; + dispatch_group_enter(group); +// NSLog(@" ---> Fetching offline image: %@", urlString); + [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { +// NSLog(@" ---> Fetched %@: %@", storyHash, urlString); + UIImage *image = (UIImage *)responseObject; + [self storeCachedImage:urlString withImage:image storyHash:storyHash storyTimestamp:storyTimestamp]; + dispatch_group_leave(group); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { +// NSLog(@" ---> Failed to fetch image %@: %@", storyHash, urlString); + [self storeFailedImage:storyHash]; + dispatch_group_leave(group); + }]; } - [imageDownloadOperationQueue reset]; + + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; + dispatch_group_wait(group, DISPATCH_TIME_FOREVER); + NSLog(@"Queue finished: %ld total (%ld remaining)", (long)appDelegate.totalUncachedImagesCount, (long)appDelegate.remainingUncachedImagesCount); + [self updateProgress]; + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + // dispatch_async(dispatch_get_main_queue(), ^{ // [appDelegate.feedsViewController hideNotifier]; // }); @@ -131,7 +128,6 @@ } [cursor close]; - [self updateProgress]; }]; return urls; @@ -140,9 +136,9 @@ - (void)updateProgress { if (self.isCancelled) return; - int start = (int)[[NSDate date] timeIntervalSince1970]; - int end = appDelegate.latestCachedImageDate; - int seconds = start - (end ? end : start); + NSInteger start = (NSInteger)[[NSDate date] timeIntervalSince1970]; + NSInteger end = appDelegate.latestCachedImageDate; + NSInteger seconds = start - (end ? end : start); __block int hours = (int)round(seconds / 60.f / 60.f); __block float progress = 0.f; @@ -155,33 +151,25 @@ }); } -- (void)storeCachedImage:(ASIHTTPRequest *)request { +- (void)storeCachedImage:(NSString *)imageUrl withImage:(UIImage *)image storyHash:(NSString *)storyHash storyTimestamp:(NSInteger)storyTimestamp { if (self.isCancelled) { NSLog(@"Image cancelled."); - [request clearDelegatesAndCancel]; return; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, (unsigned long)NULL), ^{ - NSString *storyHash = [[request userInfo] objectForKey:@"story_hash"]; - int storyTimestamp = [[[request userInfo] objectForKey:@"story_timestamp"] intValue]; - - if ([request responseStatusCode] == 200) { - NSData *responseData = [request responseData]; - NSString *md5Url = [Utilities md5:[[request originalURL] absoluteString]]; + NSData *responseData = UIImageJPEGRepresentation(image, 0.6); + NSString *md5Url = [Utilities md5:imageUrl]; // NSLog(@"Storing image: %@ (%d bytes - %d in queue)", storyHash, [responseData length], [imageDownloadOperationQueue requestsCount]); - - NSFileManager *fileManager = [NSFileManager defaultManager]; - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"]; - NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", md5Url, [[[request originalURL] absoluteString] pathExtension]]]; - - [fileManager createFileAtPath:fullPath contents:responseData attributes:nil]; - } else { - NSLog(@"Failed to fetch: %@ / %@", [[request originalURL] absoluteString], storyHash); - } + + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"]; + NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", md5Url, [imageUrl pathExtension]]]; + + [fileManager createFileAtPath:fullPath contents:responseData attributes:nil]; [appDelegate.database inDatabase:^(FMDatabase *db) { [db executeUpdate:@"UPDATE cached_images SET " @@ -199,16 +187,16 @@ } } - appDelegate.remainingUncachedImagesCount--; - if (appDelegate.remainingUncachedImagesCount % 10 == 0) { - [self updateProgress]; + @synchronized (self) { + appDelegate.remainingUncachedImagesCount--; + if (appDelegate.remainingUncachedImagesCount % 10 == 0) { + [self updateProgress]; + } } }); } -- (void)storeFailedImage:(ASIHTTPRequest *)request { - NSString *storyHash = [[request userInfo] objectForKey:@"story_hash"]; - +- (void)storeFailedImage:(NSString *)storyHash { [appDelegate.database inDatabase:^(FMDatabase *db) { [db executeUpdate:@"UPDATE cached_images SET " "image_cached = 1, failed = 1 WHERE story_hash = ?", @@ -216,13 +204,4 @@ }]; } -- (void)cachedImageQueueFinished:(ASINetworkQueue *)queue { - NSLog(@"Queue finished: %d total (%d remaining)", appDelegate.totalUncachedImagesCount, appDelegate.remainingUncachedImagesCount); - [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; - [self fetchImages]; - // dispatch_async(dispatch_get_main_queue(), ^{ - // [self.feedsViewController hideNotifier]; - // }); -} - @end diff --git a/clients/ios/Classes/offline/OfflineFetchStories.h b/clients/ios/Classes/offline/OfflineFetchStories.h index a55d2d04f..c96bb47bc 100644 --- a/clients/ios/Classes/offline/OfflineFetchStories.h +++ b/clients/ios/Classes/offline/OfflineFetchStories.h @@ -7,11 +7,10 @@ // #import -#import "AFHTTPRequestOperation.h" #import "NewsBlurAppDelegate.h" @interface OfflineFetchStories : NSOperation { - AFHTTPRequestOperation *request; + } @property (nonatomic) NewsBlurAppDelegate *appDelegate; diff --git a/clients/ios/Classes/offline/OfflineFetchStories.m b/clients/ios/Classes/offline/OfflineFetchStories.m index 619923a28..81127ba43 100644 --- a/clients/ios/Classes/offline/OfflineFetchStories.m +++ b/clients/ios/Classes/offline/OfflineFetchStories.m @@ -11,7 +11,6 @@ #import "NewsBlurViewController.h" #import "FMDatabase.h" #import "FMDatabaseAdditions.h" -#import "AFHTTPRequestOperation.h" #import "SBJson4.h" #import "NSObject+SBJSON.h" @@ -66,31 +65,23 @@ } __block NSCondition *lock = [NSCondition new]; - __weak __typeof(&*self)weakSelf = self; [lock lock]; - NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/reader/river_stories?include_hidden=true&page=0&h=%@", - self.appDelegate.url, [hashes componentsJoinedByString:@"&h="]]]; - if (request) request = nil; - - request = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]]; - [request setResponseSerializer:[AFJSONResponseSerializer serializer]]; - [request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { - __strong __typeof(&*weakSelf)strongSelf = weakSelf; - if (!strongSelf) return; - [strongSelf storeAllUnreadStories:responseObject withHashes:hashes]; + NSString *urlString = [NSString stringWithFormat:@"%@/reader/river_stories?include_hidden=true&page=0&h=%@", + self.appDelegate.url, [hashes componentsJoinedByString:@"&h="]]; + AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; + manager.responseSerializer = [AFJSONResponseSerializer serializer]; + manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); + + [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [self storeAllUnreadStories:responseObject withHashes:hashes]; [lock signal]; - } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed fetch all unreads: %@", error); [lock signal]; }]; - [request setCompletionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, (unsigned long)NULL)]; - - [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; - [request start]; - [request waitUntilFinished]; - [request.outputStream close]; + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [lock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:30]]; [lock unlock]; @@ -139,10 +130,10 @@ - (void)updateProgress { if (self.isCancelled) return; - int start = (int)[[NSDate date] timeIntervalSince1970]; - int end = appDelegate.latestFetchedStoryDate; - int seconds = start - (end ? end : start); - __block int hours = (int)round(seconds / 60.f / 60.f); + NSInteger start = (int)[[NSDate date] timeIntervalSince1970]; + NSInteger end = appDelegate.latestFetchedStoryDate; + NSInteger seconds = start - (end ? end : start); + __block NSInteger hours = (int)round(seconds / 60.f / 60.f); __block float progress = 0.f; if (appDelegate.totalUnfetchedStoryCount) { diff --git a/clients/ios/Classes/offline/OfflineSyncUnreads.h b/clients/ios/Classes/offline/OfflineSyncUnreads.h index 5a9a6d5cf..36cefc044 100644 --- a/clients/ios/Classes/offline/OfflineSyncUnreads.h +++ b/clients/ios/Classes/offline/OfflineSyncUnreads.h @@ -9,12 +9,10 @@ #import #import "NewsBlurAppDelegate.h" #import "FMDatabaseQueue.h" -#import "AFHTTPRequestOperation.h" @interface OfflineSyncUnreads : NSOperation @property (nonatomic) NewsBlurAppDelegate *appDelegate; -@property (nonatomic) AFHTTPRequestOperation *request; - (void)storeUnreadHashes:(NSDictionary *)results; diff --git a/clients/ios/Classes/offline/OfflineSyncUnreads.m b/clients/ios/Classes/offline/OfflineSyncUnreads.m index e9a40808f..3199b27ff 100644 --- a/clients/ios/Classes/offline/OfflineSyncUnreads.m +++ b/clients/ios/Classes/offline/OfflineSyncUnreads.m @@ -11,12 +11,10 @@ #import "NewsBlurViewController.h" #import "FMResultSet.h" #import "FMDatabase.h" -#import "AFHTTPRequestOperation.h" @implementation OfflineSyncUnreads @synthesize appDelegate; -@synthesize request; - (void)main { appDelegate = [NewsBlurAppDelegate sharedAppDelegate]; @@ -27,27 +25,22 @@ }); __block NSCondition *lock = [NSCondition new]; - __weak __typeof(&*self)weakSelf = self; [lock lock]; - NSURL *url = [NSURL URLWithString:[NSString - stringWithFormat:@"%@/reader/unread_story_hashes?include_timestamps=true", - self.appDelegate.url]]; - request = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]]; - [request setResponseSerializer:[AFJSONResponseSerializer serializer]]; - [request setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { - __strong __typeof(&*weakSelf)strongSelf = weakSelf; - NSLog(@"Syncing stories success: %@-%@", weakSelf, strongSelf); - if (!strongSelf) return; - [strongSelf storeUnreadHashes:responseObject]; + NSString *urlString = [NSString stringWithFormat:@"%@/reader/unread_story_hashes?include_timestamps=true", + self.appDelegate.url]; + AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; + manager.responseSerializer = [AFJSONResponseSerializer serializer]; + manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); + [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSLog(@"Syncing stories success"); + [self storeUnreadHashes:responseObject]; [lock signal]; - } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Failed fetch all story hashes: %@", error); [lock signal]; }]; - [request setCompletionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL)]; - [request start]; - + [lock waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:30]]; [lock unlock]; @@ -57,14 +50,14 @@ - (void)storeUnreadHashes:(NSDictionary *)results { if (self.isCancelled) { // NSLog(@"Canceled storing unread hashes"); - [request cancel]; +// [request cancel]; return; } [appDelegate.database inTransaction:^(FMDatabase *db, BOOL *rollback) { // NSLog(@"Storing unread story hashes..."); [db executeUpdate:@"DROP TABLE unread_hashes"]; - [appDelegate setupDatabase:db]; + [appDelegate setupDatabase:db force:NO]; NSDictionary *hashes = [results objectForKey:@"unread_feed_story_hashes"]; for (NSString *feed in [hashes allKeys]) { NSArray *story_hashes = [hashes objectForKey:feed]; diff --git a/clients/ios/NewsBlur-iPhone-Info.plist b/clients/ios/NewsBlur-iPhone-Info.plist index fc284431e..d6546071c 100644 --- a/clients/ios/NewsBlur-iPhone-Info.plist +++ b/clients/ios/NewsBlur-iPhone-Info.plist @@ -19,7 +19,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 6.1.1 + 7.0.0 CFBundleSignature ???? CFBundleURLTypes @@ -58,7 +58,7 @@ CFBundleVersion - 68 + 75 FacebookAppID 230426707030569 ITSAppUsesNonExemptEncryption @@ -94,6 +94,8 @@ MainWindow NSMainNibFile~ipad MainWindow~ipad + NSPhotoLibraryUsageDescription + NewsBlur can save photos to your camera roll by long pressing them. UIAppFonts WhitneySSm-MediumItalic-Bas.otf diff --git a/clients/ios/NewsBlur.xcodeproj/project.pbxproj b/clients/ios/NewsBlur.xcodeproj/project.pbxproj index b1a3aee32..6c448f83e 100755 --- a/clients/ios/NewsBlur.xcodeproj/project.pbxproj +++ b/clients/ios/NewsBlur.xcodeproj/project.pbxproj @@ -207,12 +207,6 @@ 43F44B1C159D8DBC00F48F8A /* FeedTableCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 43F44B1B159D8D7200F48F8A /* FeedTableCell.m */; }; 43F6A79D15B0CDC60092EE91 /* ActivityCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 43F6A79C15B0CDC60092EE91 /* ActivityCell.m */; }; 43F6A7A915B0E1ED0092EE91 /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43F6A7A815B0E1ED0092EE91 /* CoreText.framework */; }; - 78095E34128EF30C00230C8E /* ASIAuthenticationDialog.m in Sources */ = {isa = PBXBuildFile; fileRef = 78095E25128EF30C00230C8E /* ASIAuthenticationDialog.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - 78095E35128EF30D00230C8E /* ASIDownloadCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 78095E28128EF30C00230C8E /* ASIDownloadCache.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - 78095E36128EF30D00230C8E /* ASIFormDataRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 78095E2A128EF30C00230C8E /* ASIFormDataRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - 78095E37128EF30D00230C8E /* ASIHTTPRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 78095E2C128EF30C00230C8E /* ASIHTTPRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - 78095E38128EF30D00230C8E /* ASIInputStream.m in Sources */ = {isa = PBXBuildFile; fileRef = 78095E30128EF30C00230C8E /* ASIInputStream.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - 78095E39128EF30D00230C8E /* ASINetworkQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = 78095E32128EF30C00230C8E /* ASINetworkQueue.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; 78095E3F128EF35400230C8E /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78095E3E128EF35400230C8E /* CFNetwork.framework */; }; 78095E43128EF37E00230C8E /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78095E42128EF37E00230C8E /* MobileCoreServices.framework */; }; 78095E45128EF37E00230C8E /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 78095E44128EF37E00230C8E /* SystemConfiguration.framework */; }; @@ -249,9 +243,6 @@ FF1660CD16D6FD8A00AF8541 /* SmallInteractionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1660CC16D6FD8A00AF8541 /* SmallInteractionCell.m */; }; FF191E4F18A323F400473252 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FF191E4E18A323F400473252 /* Images.xcassets */; }; FF1C4E171A3FB1F4000995E3 /* NBActivityItemSource.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1C4E161A3FB1F4000995E3 /* NBActivityItemSource.m */; }; - FF1F13D318A9C2BE00FDA816 /* TMCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13CE18A9C2BE00FDA816 /* TMCache.m */; }; - FF1F13D418A9C2BE00FDA816 /* TMDiskCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13D018A9C2BE00FDA816 /* TMDiskCache.m */; }; - FF1F13D518A9C2BE00FDA816 /* TMMemoryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13D218A9C2BE00FDA816 /* TMMemoryCache.m */; }; FF1F13D818AAC97900FDA816 /* UIImage+Resize.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13D718AAC97900FDA816 /* UIImage+Resize.m */; }; FF21B1111C821E150053938A /* disclosure_border_dark@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF21B10F1C821E150053938A /* disclosure_border_dark@2x.png */; }; FF21B1121C821E150053938A /* disclosure_border_dark.png in Resources */ = {isa = PBXBuildFile; fileRef = FF21B1101C821E150053938A /* disclosure_border_dark.png */; }; @@ -282,6 +273,24 @@ FF22FE7716E557D80046165A /* toolbar_tall_background@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF22FE7516E557D80046165A /* toolbar_tall_background@2x.png */; }; FF26125418C00FEC0055FF4D /* AddressBook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF26125318C00FEC0055FF4D /* AddressBook.framework */; }; FF265F12187B6D4F0080C332 /* fitvid.js in Resources */ = {isa = PBXBuildFile; fileRef = FF265F10187B6B230080C332 /* fitvid.js */; }; + FF2924BF1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF29249F1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.m */; }; + FF2924C01E92DF7C00FCFA63 /* AFHTTPSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924A11E92DF7C00FCFA63 /* AFHTTPSessionManager.m */; }; + FF2924C11E92DF7C00FCFA63 /* AFImageDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924A31E92DF7C00FCFA63 /* AFImageDownloader.m */; }; + FF2924C21E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924A51E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.m */; }; + FF2924C31E92DF7C00FCFA63 /* AFNetworkReachabilityManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924A81E92DF7C00FCFA63 /* AFNetworkReachabilityManager.m */; }; + FF2924C41E92DF7C00FCFA63 /* AFSecurityPolicy.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924AA1E92DF7C00FCFA63 /* AFSecurityPolicy.m */; }; + FF2924C51E92DF7C00FCFA63 /* AFURLRequestSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924AC1E92DF7C00FCFA63 /* AFURLRequestSerialization.m */; }; + FF2924C61E92DF7C00FCFA63 /* AFURLResponseSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924AE1E92DF7C00FCFA63 /* AFURLResponseSerialization.m */; }; + FF2924C71E92DF7C00FCFA63 /* AFURLSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924B01E92DF7C00FCFA63 /* AFURLSessionManager.m */; }; + FF2924C81E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924B21E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.m */; }; + FF2924C91E92DF7C00FCFA63 /* UIButton+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924B41E92DF7C00FCFA63 /* UIButton+AFNetworking.m */; }; + FF2924CA1E92DF7C00FCFA63 /* UIImageView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924B71E92DF7C00FCFA63 /* UIImageView+AFNetworking.m */; }; + FF2924CB1E92DF7C00FCFA63 /* UIProgressView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924BA1E92DF7C00FCFA63 /* UIProgressView+AFNetworking.m */; }; + FF2924CC1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924BC1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.m */; }; + FF2924CD1E92DF7C00FCFA63 /* UIWebView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924BE1E92DF7C00FCFA63 /* UIWebView+AFNetworking.m */; }; + FF2924E51E932D2900FCFA63 /* PINCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924DF1E932D2900FCFA63 /* PINCache.m */; }; + FF2924E61E932D2900FCFA63 /* PINDiskCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924E21E932D2900FCFA63 /* PINDiskCache.m */; }; + FF2924E71E932D2900FCFA63 /* PINMemoryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2924E41E932D2900FCFA63 /* PINMemoryCache.m */; }; FF29708B16DD7AA400E92F85 /* segment_active.png in Resources */ = {isa = PBXBuildFile; fileRef = FF29708916DD7AA400E92F85 /* segment_active.png */; }; FF29708C16DD7AA400E92F85 /* segment_inactive.png in Resources */ = {isa = PBXBuildFile; fileRef = FF29708A16DD7AA400E92F85 /* segment_inactive.png */; }; FF29708E16DD7C8A00E92F85 /* segment_left_selected.png in Resources */ = {isa = PBXBuildFile; fileRef = FF29708D16DD7C8A00E92F85 /* segment_left_selected.png */; }; @@ -295,27 +304,29 @@ FF2D8B351487250C00057B80 /* MessageUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF2D8B341487250C00057B80 /* MessageUI.framework */; }; FF2D8C6C1487F05100057B80 /* Twitter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF2D8C6B1487F05100057B80 /* Twitter.framework */; settings = {ATTRIBUTES = (Weak, ); }; }; FF2D8CE514893BC000057B80 /* MoveSiteViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2D8CE314893BBF00057B80 /* MoveSiteViewController.m */; }; - FF2EB7B01AA65504002549A7 /* IASKAppSettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7811AA65504002549A7 /* IASKAppSettingsViewController.m */; }; - FF2EB7B11AA65504002549A7 /* IASKAppSettingsWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7831AA65504002549A7 /* IASKAppSettingsWebViewController.m */; }; - FF2EB7B21AA65504002549A7 /* IASKMultipleValueSelection.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7851AA65504002549A7 /* IASKMultipleValueSelection.m */; }; - FF2EB7B31AA65504002549A7 /* IASKSpecifierValuesViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7871AA65504002549A7 /* IASKSpecifierValuesViewController.m */; }; - FF2EB7B41AA65504002549A7 /* IASKSettingsReader.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB78B1AA65504002549A7 /* IASKSettingsReader.m */; }; - FF2EB7B51AA65504002549A7 /* IASKSettingsStore.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB78D1AA65504002549A7 /* IASKSettingsStore.m */; }; - FF2EB7B61AA65504002549A7 /* IASKSettingsStoreFile.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB78F1AA65504002549A7 /* IASKSettingsStoreFile.m */; }; - FF2EB7B71AA65504002549A7 /* IASKSettingsStoreUserDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7911AA65504002549A7 /* IASKSettingsStoreUserDefaults.m */; }; - FF2EB7B81AA65504002549A7 /* IASKSpecifier.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7931AA65504002549A7 /* IASKSpecifier.m */; }; - FF2EB7B91AA65504002549A7 /* IASKLocalizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = FF2EB7951AA65504002549A7 /* IASKLocalizable.strings */; }; - FF2EB7BA1AA65504002549A7 /* IASKPSSliderSpecifierViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7A71AA65504002549A7 /* IASKPSSliderSpecifierViewCell.m */; }; - FF2EB7BB1AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7A91AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.m */; }; - FF2EB7BC1AA65504002549A7 /* IASKSlider.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7AB1AA65504002549A7 /* IASKSlider.m */; }; - FF2EB7BD1AA65504002549A7 /* IASKSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7AD1AA65504002549A7 /* IASKSwitch.m */; }; - FF2EB7BE1AA65504002549A7 /* IASKTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = FF2EB7AF1AA65504002549A7 /* IASKTextField.m */; }; FF308A7B18C01014009F986A /* AssetsLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF308A7A18C01014009F986A /* AssetsLibrary.framework */; }; FF308A7D18C01025009F986A /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF308A7C18C01025009F986A /* CoreMotion.framework */; }; FF308A7F18C01037009F986A /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF308A7E18C01037009F986A /* CoreLocation.framework */; }; FF308A8118C01043009F986A /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF308A8018C01043009F986A /* MediaPlayer.framework */; }; FF322235185BC1AA004078AA /* logo_58.png in Resources */ = {isa = PBXBuildFile; fileRef = FF322234185BC1AA004078AA /* logo_58.png */; }; FF322237185BC1CE004078AA /* logo_80.png in Resources */ = {isa = PBXBuildFile; fileRef = FF322236185BC1CE004078AA /* logo_80.png */; }; + FF34FD601E9D93CB0062F8ED /* IASKAppSettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD2D1E9D93CB0062F8ED /* IASKAppSettingsViewController.m */; }; + FF34FD611E9D93CB0062F8ED /* IASKAppSettingsWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD2F1E9D93CB0062F8ED /* IASKAppSettingsWebViewController.m */; }; + FF34FD621E9D93CB0062F8ED /* IASKMultipleValueSelection.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD311E9D93CB0062F8ED /* IASKMultipleValueSelection.m */; }; + FF34FD631E9D93CB0062F8ED /* IASKSpecifierValuesViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD331E9D93CB0062F8ED /* IASKSpecifierValuesViewController.m */; }; + FF34FD641E9D93CB0062F8ED /* IASKSettingsReader.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD371E9D93CB0062F8ED /* IASKSettingsReader.m */; }; + FF34FD651E9D93CB0062F8ED /* IASKSettingsStore.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD391E9D93CB0062F8ED /* IASKSettingsStore.m */; }; + FF34FD661E9D93CB0062F8ED /* IASKSettingsStoreFile.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD3B1E9D93CB0062F8ED /* IASKSettingsStoreFile.m */; }; + FF34FD671E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD3D1E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.m */; }; + FF34FD681E9D93CB0062F8ED /* IASKSpecifier.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD3F1E9D93CB0062F8ED /* IASKSpecifier.m */; }; + FF34FD691E9D93CB0062F8ED /* IASKLocalizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = FF34FD411E9D93CB0062F8ED /* IASKLocalizable.strings */; }; + FF34FD6A1E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD531E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.m */; }; + FF34FD6B1E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD551E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.m */; }; + FF34FD6C1E9D93CB0062F8ED /* IASKSlider.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD571E9D93CB0062F8ED /* IASKSlider.m */; }; + FF34FD6D1E9D93CB0062F8ED /* IASKSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD591E9D93CB0062F8ED /* IASKSwitch.m */; }; + FF34FD6E1E9D93CB0062F8ED /* IASKTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD5B1E9D93CB0062F8ED /* IASKTextField.m */; }; + FF34FD6F1E9D93CB0062F8ED /* IASKTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD5D1E9D93CB0062F8ED /* IASKTextView.m */; }; + FF34FD701E9D93CB0062F8ED /* IASKTextViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF34FD5F1E9D93CB0062F8ED /* IASKTextViewCell.m */; }; FF3964BC192BED0A004BEE1A /* tag.png in Resources */ = {isa = PBXBuildFile; fileRef = FF3964BB192BED0A004BEE1A /* tag.png */; }; FF3A3E091BFBBB5100ADC01A /* ChronicleSSm-Book.otf in Resources */ = {isa = PBXBuildFile; fileRef = FF3A3DEF1BFBBAC600ADC01A /* ChronicleSSm-Book.otf */; }; FF3A3E0A1BFBBB5400ADC01A /* ChronicleSSm-BookItalic.otf in Resources */ = {isa = PBXBuildFile; fileRef = FF3A3DF01BFBBAC600ADC01A /* ChronicleSSm-BookItalic.otf */; }; @@ -392,8 +403,6 @@ FF753CD0175858FC00344EC9 /* FMDatabasePool.m in Sources */ = {isa = PBXBuildFile; fileRef = FF753CC8175858FC00344EC9 /* FMDatabasePool.m */; }; FF753CD1175858FC00344EC9 /* FMDatabaseQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = FF753CCA175858FC00344EC9 /* FMDatabaseQueue.m */; }; FF753CD3175858FC00344EC9 /* FMResultSet.m in Sources */ = {isa = PBXBuildFile; fileRef = FF753CCD175858FC00344EC9 /* FMResultSet.m */; }; - FF793E1B13F1A9F700F282D2 /* ASIDataCompressor.m in Sources */ = {isa = PBXBuildFile; fileRef = FF793E1813F1A9F700F282D2 /* ASIDataCompressor.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - FF793E1C13F1A9F700F282D2 /* ASIDataDecompressor.m in Sources */ = {isa = PBXBuildFile; fileRef = FF793E1A13F1A9F700F282D2 /* ASIDataDecompressor.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; FF8364BB1755759A008F5C58 /* traverse_text.png in Resources */ = {isa = PBXBuildFile; fileRef = FF8364B91755759A008F5C58 /* traverse_text.png */; }; FF8364BC1755759A008F5C58 /* traverse_text@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF8364BA1755759A008F5C58 /* traverse_text@2x.png */; }; FF8364BF1756949E008F5C58 /* traverse_text_on.png in Resources */ = {isa = PBXBuildFile; fileRef = FF8364BD1756949E008F5C58 /* traverse_text_on.png */; }; @@ -487,16 +496,6 @@ FFCDD90117F65A71000C6483 /* NBSwipeableCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FFCDD90017F65A71000C6483 /* NBSwipeableCell.m */; }; FFD1D7311459B63500E46F89 /* BaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD1D7301459B63500E46F89 /* BaseViewController.m */; }; FFD6604C1BACA45D006E4B8D /* THCircularProgressView.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD6604B1BACA45D006E4B8D /* THCircularProgressView.m */; }; - FFD660611BACA46D006E4B8D /* AFHTTPRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD6604F1BACA46D006E4B8D /* AFHTTPRequestOperation.m */; }; - FFD660621BACA46D006E4B8D /* AFHTTPRequestOperationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD660511BACA46D006E4B8D /* AFHTTPRequestOperationManager.m */; }; - FFD660631BACA46D006E4B8D /* AFHTTPSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD660531BACA46D006E4B8D /* AFHTTPSessionManager.m */; }; - FFD660641BACA46D006E4B8D /* AFNetworkReachabilityManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD660561BACA46D006E4B8D /* AFNetworkReachabilityManager.m */; }; - FFD660651BACA46D006E4B8D /* AFSecurityPolicy.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD660581BACA46D006E4B8D /* AFSecurityPolicy.m */; }; - FFD660661BACA46D006E4B8D /* AFURLConnectionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD6605A1BACA46D006E4B8D /* AFURLConnectionOperation.m */; }; - FFD660671BACA46D006E4B8D /* AFURLRequestSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD6605C1BACA46D006E4B8D /* AFURLRequestSerialization.m */; }; - FFD660681BACA46D006E4B8D /* AFURLResponseSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD6605E1BACA46D006E4B8D /* AFURLResponseSerialization.m */; }; - FFD660691BACA46D006E4B8D /* AFURLSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD660601BACA46D006E4B8D /* AFURLSessionManager.m */; }; - FFD6606C1BACA49C006E4B8D /* UIImageView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD6606B1BACA49C006E4B8D /* UIImageView+AFNetworking.m */; }; FFD887F01445F1E800385399 /* AddSiteAutocompleteCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FFD887EE1445F1E800385399 /* AddSiteAutocompleteCell.m */; }; FFDCA0BB16E80877000D8E0C /* GCOAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = FFDCA0B716E80877000D8E0C /* GCOAuth.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; FFDCA0BC16E80877000D8E0C /* NSData+Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = FFDCA0B916E80877000D8E0C /* NSData+Base64.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; @@ -603,7 +602,7 @@ 17432C7D1C533FBC003F8FD6 /* MenuViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MenuViewController.h; sourceTree = ""; }; 17432C7E1C533FBC003F8FD6 /* MenuViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MenuViewController.m; sourceTree = ""; }; 17432C811C53438D003F8FD6 /* FeedChooserViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeedChooserViewController.h; sourceTree = ""; }; - 17432C821C53438D003F8FD6 /* FeedChooserViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FeedChooserViewController.m; sourceTree = ""; }; + 17432C821C53438D003F8FD6 /* FeedChooserViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FeedChooserViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; 17432C841C5343C0003F8FD6 /* FeedChooserTitleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeedChooserTitleView.h; sourceTree = ""; }; 17432C851C5343C0003F8FD6 /* FeedChooserTitleView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FeedChooserTitleView.m; sourceTree = ""; }; 17432C871C534BC6003F8FD6 /* FeedChooserViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeedChooserViewCell.h; sourceTree = ""; }; @@ -812,35 +811,19 @@ 43D818A115B940C200733444 /* DataUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DataUtilities.h; sourceTree = ""; }; 43D818A215B940C200733444 /* DataUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DataUtilities.m; sourceTree = ""; }; 43E8381515BC73EA000553BE /* FirstTimeUserAddFriendsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstTimeUserAddFriendsViewController.h; sourceTree = ""; }; - 43E8381615BC73EA000553BE /* FirstTimeUserAddFriendsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstTimeUserAddFriendsViewController.m; sourceTree = ""; }; + 43E8381615BC73EA000553BE /* FirstTimeUserAddFriendsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FirstTimeUserAddFriendsViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; 43E8381715BC73EB000553BE /* FirstTimeUserAddFriendsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FirstTimeUserAddFriendsViewController.xib; sourceTree = ""; }; 43E8381815BC73EB000553BE /* FirstTimeUserAddNewsBlurViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstTimeUserAddNewsBlurViewController.h; sourceTree = ""; }; - 43E8381915BC73EB000553BE /* FirstTimeUserAddNewsBlurViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstTimeUserAddNewsBlurViewController.m; sourceTree = ""; }; + 43E8381915BC73EB000553BE /* FirstTimeUserAddNewsBlurViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FirstTimeUserAddNewsBlurViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; 43E8381A15BC73EB000553BE /* FirstTimeUserAddNewsBlurViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FirstTimeUserAddNewsBlurViewController.xib; sourceTree = ""; }; 43E8381B15BC73EB000553BE /* FirstTimeUserAddSitesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstTimeUserAddSitesViewController.h; sourceTree = ""; }; - 43E8381C15BC73EB000553BE /* FirstTimeUserAddSitesViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstTimeUserAddSitesViewController.m; sourceTree = ""; }; + 43E8381C15BC73EB000553BE /* FirstTimeUserAddSitesViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FirstTimeUserAddSitesViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; 43E8381D15BC73EB000553BE /* FirstTimeUserAddSitesViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FirstTimeUserAddSitesViewController.xib; sourceTree = ""; }; 43F44B1A159D8D7200F48F8A /* FeedTableCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FeedTableCell.h; sourceTree = ""; }; 43F44B1B159D8D7200F48F8A /* FeedTableCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FeedTableCell.m; sourceTree = ""; }; 43F6A79B15B0CDC60092EE91 /* ActivityCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActivityCell.h; sourceTree = ""; }; 43F6A79C15B0CDC60092EE91 /* ActivityCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ActivityCell.m; sourceTree = ""; }; 43F6A7A815B0E1ED0092EE91 /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; }; - 78095E24128EF30C00230C8E /* ASIAuthenticationDialog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIAuthenticationDialog.h; sourceTree = ""; }; - 78095E25128EF30C00230C8E /* ASIAuthenticationDialog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIAuthenticationDialog.m; sourceTree = ""; }; - 78095E26128EF30C00230C8E /* ASICacheDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASICacheDelegate.h; sourceTree = ""; }; - 78095E27128EF30C00230C8E /* ASIDownloadCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIDownloadCache.h; sourceTree = ""; }; - 78095E28128EF30C00230C8E /* ASIDownloadCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIDownloadCache.m; sourceTree = ""; }; - 78095E29128EF30C00230C8E /* ASIFormDataRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIFormDataRequest.h; sourceTree = ""; }; - 78095E2A128EF30C00230C8E /* ASIFormDataRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIFormDataRequest.m; sourceTree = ""; }; - 78095E2B128EF30C00230C8E /* ASIHTTPRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIHTTPRequest.h; sourceTree = ""; }; - 78095E2C128EF30C00230C8E /* ASIHTTPRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIHTTPRequest.m; sourceTree = ""; }; - 78095E2D128EF30C00230C8E /* ASIHTTPRequestConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIHTTPRequestConfig.h; sourceTree = ""; }; - 78095E2E128EF30C00230C8E /* ASIHTTPRequestDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIHTTPRequestDelegate.h; sourceTree = ""; }; - 78095E2F128EF30C00230C8E /* ASIInputStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIInputStream.h; sourceTree = ""; }; - 78095E30128EF30C00230C8E /* ASIInputStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIInputStream.m; sourceTree = ""; }; - 78095E31128EF30C00230C8E /* ASINetworkQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASINetworkQueue.h; sourceTree = ""; }; - 78095E32128EF30C00230C8E /* ASINetworkQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASINetworkQueue.m; sourceTree = ""; }; - 78095E33128EF30C00230C8E /* ASIProgressDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIProgressDelegate.h; sourceTree = ""; }; 78095E3E128EF35400230C8E /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; }; 78095E42128EF37E00230C8E /* MobileCoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MobileCoreServices.framework; path = System/Library/Frameworks/MobileCoreServices.framework; sourceTree = SDKROOT; }; 78095E44128EF37E00230C8E /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; @@ -892,12 +875,6 @@ FF191E4E18A323F400473252 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = NewsBlur/Images.xcassets; sourceTree = SOURCE_ROOT; }; FF1C4E151A3FB1F4000995E3 /* NBActivityItemSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBActivityItemSource.h; sourceTree = ""; }; FF1C4E161A3FB1F4000995E3 /* NBActivityItemSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NBActivityItemSource.m; sourceTree = ""; }; - FF1F13CD18A9C2BE00FDA816 /* TMCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TMCache.h; sourceTree = ""; }; - FF1F13CE18A9C2BE00FDA816 /* TMCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TMCache.m; sourceTree = ""; }; - FF1F13CF18A9C2BE00FDA816 /* TMDiskCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TMDiskCache.h; sourceTree = ""; }; - FF1F13D018A9C2BE00FDA816 /* TMDiskCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TMDiskCache.m; sourceTree = ""; }; - FF1F13D118A9C2BE00FDA816 /* TMMemoryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TMMemoryCache.h; sourceTree = ""; }; - FF1F13D218A9C2BE00FDA816 /* TMMemoryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TMMemoryCache.m; sourceTree = ""; }; FF1F13D618AAC97900FDA816 /* UIImage+Resize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+Resize.h"; path = "Other Sources/UIImage+Resize.h"; sourceTree = ""; }; FF1F13D718AAC97900FDA816 /* UIImage+Resize.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+Resize.m"; path = "Other Sources/UIImage+Resize.m"; sourceTree = ""; }; FF21B10F1C821E150053938A /* disclosure_border_dark@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "disclosure_border_dark@2x.png"; sourceTree = ""; }; @@ -935,6 +912,47 @@ FF22FE7516E557D80046165A /* toolbar_tall_background@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "toolbar_tall_background@2x.png"; sourceTree = ""; }; FF26125318C00FEC0055FF4D /* AddressBook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBook.framework; path = System/Library/Frameworks/AddressBook.framework; sourceTree = SDKROOT; }; FF265F10187B6B230080C332 /* fitvid.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = fitvid.js; path = static/fitvid.js; sourceTree = SOURCE_ROOT; }; + FF29249E1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFAutoPurgingImageCache.h; sourceTree = ""; }; + FF29249F1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFAutoPurgingImageCache.m; sourceTree = ""; }; + FF2924A01E92DF7C00FCFA63 /* AFHTTPSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPSessionManager.h; sourceTree = ""; }; + FF2924A11E92DF7C00FCFA63 /* AFHTTPSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFHTTPSessionManager.m; sourceTree = ""; }; + FF2924A21E92DF7C00FCFA63 /* AFImageDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFImageDownloader.h; sourceTree = ""; }; + FF2924A31E92DF7C00FCFA63 /* AFImageDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFImageDownloader.m; sourceTree = ""; }; + FF2924A41E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFNetworkActivityIndicatorManager.h; sourceTree = ""; }; + FF2924A51E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFNetworkActivityIndicatorManager.m; sourceTree = ""; }; + FF2924A61E92DF7C00FCFA63 /* AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFNetworking.h; sourceTree = ""; }; + FF2924A71E92DF7C00FCFA63 /* AFNetworkReachabilityManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFNetworkReachabilityManager.h; sourceTree = ""; }; + FF2924A81E92DF7C00FCFA63 /* AFNetworkReachabilityManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFNetworkReachabilityManager.m; sourceTree = ""; }; + FF2924A91E92DF7C00FCFA63 /* AFSecurityPolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFSecurityPolicy.h; sourceTree = ""; }; + FF2924AA1E92DF7C00FCFA63 /* AFSecurityPolicy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFSecurityPolicy.m; sourceTree = ""; }; + FF2924AB1E92DF7C00FCFA63 /* AFURLRequestSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLRequestSerialization.h; sourceTree = ""; }; + FF2924AC1E92DF7C00FCFA63 /* AFURLRequestSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLRequestSerialization.m; sourceTree = ""; }; + FF2924AD1E92DF7C00FCFA63 /* AFURLResponseSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLResponseSerialization.h; sourceTree = ""; }; + FF2924AE1E92DF7C00FCFA63 /* AFURLResponseSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLResponseSerialization.m; sourceTree = ""; }; + FF2924AF1E92DF7C00FCFA63 /* AFURLSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLSessionManager.h; sourceTree = ""; }; + FF2924B01E92DF7C00FCFA63 /* AFURLSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLSessionManager.m; sourceTree = ""; }; + FF2924B11E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIActivityIndicatorView+AFNetworking.h"; sourceTree = ""; }; + FF2924B21E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIActivityIndicatorView+AFNetworking.m"; sourceTree = ""; }; + FF2924B31E92DF7C00FCFA63 /* UIButton+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+AFNetworking.h"; sourceTree = ""; }; + FF2924B41E92DF7C00FCFA63 /* UIButton+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+AFNetworking.m"; sourceTree = ""; }; + FF2924B51E92DF7C00FCFA63 /* UIImage+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+AFNetworking.h"; sourceTree = ""; }; + FF2924B61E92DF7C00FCFA63 /* UIImageView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+AFNetworking.h"; sourceTree = ""; }; + FF2924B71E92DF7C00FCFA63 /* UIImageView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+AFNetworking.m"; sourceTree = ""; }; + FF2924B81E92DF7C00FCFA63 /* UIKit+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIKit+AFNetworking.h"; sourceTree = ""; }; + FF2924B91E92DF7C00FCFA63 /* UIProgressView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIProgressView+AFNetworking.h"; sourceTree = ""; }; + FF2924BA1E92DF7C00FCFA63 /* UIProgressView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIProgressView+AFNetworking.m"; sourceTree = ""; }; + FF2924BB1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIRefreshControl+AFNetworking.h"; sourceTree = ""; }; + FF2924BC1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIRefreshControl+AFNetworking.m"; sourceTree = ""; }; + FF2924BD1E92DF7C00FCFA63 /* UIWebView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+AFNetworking.h"; sourceTree = ""; }; + FF2924BE1E92DF7C00FCFA63 /* UIWebView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+AFNetworking.m"; sourceTree = ""; }; + FF2924DD1E932D2900FCFA63 /* Nullability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Nullability.h; sourceTree = ""; }; + FF2924DE1E932D2900FCFA63 /* PINCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PINCache.h; sourceTree = ""; }; + FF2924DF1E932D2900FCFA63 /* PINCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PINCache.m; sourceTree = ""; }; + FF2924E01E932D2900FCFA63 /* PINCacheObjectSubscripting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PINCacheObjectSubscripting.h; sourceTree = ""; }; + FF2924E11E932D2900FCFA63 /* PINDiskCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PINDiskCache.h; sourceTree = ""; }; + FF2924E21E932D2900FCFA63 /* PINDiskCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PINDiskCache.m; sourceTree = ""; }; + FF2924E31E932D2900FCFA63 /* PINMemoryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PINMemoryCache.h; sourceTree = ""; }; + FF2924E41E932D2900FCFA63 /* PINMemoryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PINMemoryCache.m; sourceTree = ""; }; FF29708916DD7AA400E92F85 /* segment_active.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = segment_active.png; sourceTree = ""; }; FF29708A16DD7AA400E92F85 /* segment_inactive.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = segment_inactive.png; sourceTree = ""; }; FF29708D16DD7C8A00E92F85 /* segment_left_selected.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = segment_left_selected.png; sourceTree = ""; }; @@ -949,56 +967,60 @@ FF2D8C6B1487F05100057B80 /* Twitter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Twitter.framework; path = System/Library/Frameworks/Twitter.framework; sourceTree = SDKROOT; }; FF2D8CE214893BBF00057B80 /* MoveSiteViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MoveSiteViewController.h; sourceTree = ""; }; FF2D8CE314893BBF00057B80 /* MoveSiteViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MoveSiteViewController.m; sourceTree = ""; }; - FF2EB7801AA65504002549A7 /* IASKAppSettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKAppSettingsViewController.h; sourceTree = ""; }; - FF2EB7811AA65504002549A7 /* IASKAppSettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKAppSettingsViewController.m; sourceTree = ""; }; - FF2EB7821AA65504002549A7 /* IASKAppSettingsWebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKAppSettingsWebViewController.h; sourceTree = ""; }; - FF2EB7831AA65504002549A7 /* IASKAppSettingsWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKAppSettingsWebViewController.m; sourceTree = ""; }; - FF2EB7841AA65504002549A7 /* IASKMultipleValueSelection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKMultipleValueSelection.h; sourceTree = ""; }; - FF2EB7851AA65504002549A7 /* IASKMultipleValueSelection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKMultipleValueSelection.m; sourceTree = ""; }; - FF2EB7861AA65504002549A7 /* IASKSpecifierValuesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSpecifierValuesViewController.h; sourceTree = ""; }; - FF2EB7871AA65504002549A7 /* IASKSpecifierValuesViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSpecifierValuesViewController.m; sourceTree = ""; }; - FF2EB7881AA65504002549A7 /* IASKViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKViewController.h; sourceTree = ""; }; - FF2EB78A1AA65504002549A7 /* IASKSettingsReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsReader.h; sourceTree = ""; }; - FF2EB78B1AA65504002549A7 /* IASKSettingsReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsReader.m; sourceTree = ""; }; - FF2EB78C1AA65504002549A7 /* IASKSettingsStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsStore.h; sourceTree = ""; }; - FF2EB78D1AA65504002549A7 /* IASKSettingsStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsStore.m; sourceTree = ""; }; - FF2EB78E1AA65504002549A7 /* IASKSettingsStoreFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsStoreFile.h; sourceTree = ""; }; - FF2EB78F1AA65504002549A7 /* IASKSettingsStoreFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsStoreFile.m; sourceTree = ""; }; - FF2EB7901AA65504002549A7 /* IASKSettingsStoreUserDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsStoreUserDefaults.h; sourceTree = ""; }; - FF2EB7911AA65504002549A7 /* IASKSettingsStoreUserDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsStoreUserDefaults.m; sourceTree = ""; }; - FF2EB7921AA65504002549A7 /* IASKSpecifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSpecifier.h; sourceTree = ""; }; - FF2EB7931AA65504002549A7 /* IASKSpecifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSpecifier.m; sourceTree = ""; }; - FF2EB7961AA65504002549A7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7971AA65504002549A7 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7981AA65504002549A7 /* el */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = el; path = el.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7991AA65504002549A7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB79A1AA65504002549A7 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB79B1AA65504002549A7 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB79C1AA65504002549A7 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB79D1AA65504002549A7 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB79E1AA65504002549A7 /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB79F1AA65504002549A7 /* pt-PT */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-PT"; path = "pt-PT.lproj/IASKLocalizable.strings"; sourceTree = ""; }; - FF2EB7A01AA65504002549A7 /* pt */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pt; path = pt.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7A11AA65504002549A7 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7A21AA65504002549A7 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7A31AA65504002549A7 /* th */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = th; path = th.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7A41AA65504002549A7 /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/IASKLocalizable.strings; sourceTree = ""; }; - FF2EB7A61AA65504002549A7 /* IASKPSSliderSpecifierViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKPSSliderSpecifierViewCell.h; sourceTree = ""; }; - FF2EB7A71AA65504002549A7 /* IASKPSSliderSpecifierViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKPSSliderSpecifierViewCell.m; sourceTree = ""; }; - FF2EB7A81AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKPSTextFieldSpecifierViewCell.h; sourceTree = ""; }; - FF2EB7A91AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKPSTextFieldSpecifierViewCell.m; sourceTree = ""; }; - FF2EB7AA1AA65504002549A7 /* IASKSlider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSlider.h; sourceTree = ""; }; - FF2EB7AB1AA65504002549A7 /* IASKSlider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSlider.m; sourceTree = ""; }; - FF2EB7AC1AA65504002549A7 /* IASKSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSwitch.h; sourceTree = ""; }; - FF2EB7AD1AA65504002549A7 /* IASKSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSwitch.m; sourceTree = ""; }; - FF2EB7AE1AA65504002549A7 /* IASKTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKTextField.h; sourceTree = ""; }; - FF2EB7AF1AA65504002549A7 /* IASKTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKTextField.m; sourceTree = ""; }; FF308A7A18C01014009F986A /* AssetsLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AssetsLibrary.framework; path = System/Library/Frameworks/AssetsLibrary.framework; sourceTree = SDKROOT; }; FF308A7C18C01025009F986A /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = System/Library/Frameworks/CoreMotion.framework; sourceTree = SDKROOT; }; FF308A7E18C01037009F986A /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; }; FF308A8018C01043009F986A /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; }; FF322234185BC1AA004078AA /* logo_58.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logo_58.png; sourceTree = ""; }; FF322236185BC1CE004078AA /* logo_80.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logo_80.png; sourceTree = ""; }; + FF34FD2C1E9D93CB0062F8ED /* IASKAppSettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKAppSettingsViewController.h; sourceTree = ""; }; + FF34FD2D1E9D93CB0062F8ED /* IASKAppSettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKAppSettingsViewController.m; sourceTree = ""; }; + FF34FD2E1E9D93CB0062F8ED /* IASKAppSettingsWebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKAppSettingsWebViewController.h; sourceTree = ""; }; + FF34FD2F1E9D93CB0062F8ED /* IASKAppSettingsWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKAppSettingsWebViewController.m; sourceTree = ""; }; + FF34FD301E9D93CB0062F8ED /* IASKMultipleValueSelection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKMultipleValueSelection.h; sourceTree = ""; }; + FF34FD311E9D93CB0062F8ED /* IASKMultipleValueSelection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKMultipleValueSelection.m; sourceTree = ""; }; + FF34FD321E9D93CB0062F8ED /* IASKSpecifierValuesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSpecifierValuesViewController.h; sourceTree = ""; }; + FF34FD331E9D93CB0062F8ED /* IASKSpecifierValuesViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSpecifierValuesViewController.m; sourceTree = ""; }; + FF34FD341E9D93CB0062F8ED /* IASKViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKViewController.h; sourceTree = ""; }; + FF34FD361E9D93CB0062F8ED /* IASKSettingsReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsReader.h; sourceTree = ""; }; + FF34FD371E9D93CB0062F8ED /* IASKSettingsReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsReader.m; sourceTree = ""; }; + FF34FD381E9D93CB0062F8ED /* IASKSettingsStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsStore.h; sourceTree = ""; }; + FF34FD391E9D93CB0062F8ED /* IASKSettingsStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsStore.m; sourceTree = ""; }; + FF34FD3A1E9D93CB0062F8ED /* IASKSettingsStoreFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsStoreFile.h; sourceTree = ""; }; + FF34FD3B1E9D93CB0062F8ED /* IASKSettingsStoreFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsStoreFile.m; sourceTree = ""; }; + FF34FD3C1E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSettingsStoreUserDefaults.h; sourceTree = ""; }; + FF34FD3D1E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSettingsStoreUserDefaults.m; sourceTree = ""; }; + FF34FD3E1E9D93CB0062F8ED /* IASKSpecifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSpecifier.h; sourceTree = ""; }; + FF34FD3F1E9D93CB0062F8ED /* IASKSpecifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSpecifier.m; sourceTree = ""; }; + FF34FD421E9D93CB0062F8ED /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD431E9D93CB0062F8ED /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD441E9D93CB0062F8ED /* el */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = el; path = el.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD451E9D93CB0062F8ED /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD461E9D93CB0062F8ED /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD471E9D93CB0062F8ED /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD481E9D93CB0062F8ED /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD491E9D93CB0062F8ED /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD4A1E9D93CB0062F8ED /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD4B1E9D93CB0062F8ED /* pt-PT */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-PT"; path = "pt-PT.lproj/IASKLocalizable.strings"; sourceTree = ""; }; + FF34FD4C1E9D93CB0062F8ED /* pt */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pt; path = pt.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD4D1E9D93CB0062F8ED /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD4E1E9D93CB0062F8ED /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD4F1E9D93CB0062F8ED /* th */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = th; path = th.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD501E9D93CB0062F8ED /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/IASKLocalizable.strings; sourceTree = ""; }; + FF34FD521E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKPSSliderSpecifierViewCell.h; sourceTree = ""; }; + FF34FD531E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKPSSliderSpecifierViewCell.m; sourceTree = ""; }; + FF34FD541E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKPSTextFieldSpecifierViewCell.h; sourceTree = ""; }; + FF34FD551E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKPSTextFieldSpecifierViewCell.m; sourceTree = ""; }; + FF34FD561E9D93CB0062F8ED /* IASKSlider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSlider.h; sourceTree = ""; }; + FF34FD571E9D93CB0062F8ED /* IASKSlider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSlider.m; sourceTree = ""; }; + FF34FD581E9D93CB0062F8ED /* IASKSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKSwitch.h; sourceTree = ""; }; + FF34FD591E9D93CB0062F8ED /* IASKSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKSwitch.m; sourceTree = ""; }; + FF34FD5A1E9D93CB0062F8ED /* IASKTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKTextField.h; sourceTree = ""; }; + FF34FD5B1E9D93CB0062F8ED /* IASKTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKTextField.m; sourceTree = ""; }; + FF34FD5C1E9D93CB0062F8ED /* IASKTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKTextView.h; sourceTree = ""; }; + FF34FD5D1E9D93CB0062F8ED /* IASKTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKTextView.m; sourceTree = ""; }; + FF34FD5E1E9D93CB0062F8ED /* IASKTextViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IASKTextViewCell.h; sourceTree = ""; }; + FF34FD5F1E9D93CB0062F8ED /* IASKTextViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IASKTextViewCell.m; sourceTree = ""; }; FF3964BB192BED0A004BEE1A /* tag.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tag.png; sourceTree = ""; }; FF3A3DED1BFBBAC600ADC01A /* ChronicleSSm-Bold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "ChronicleSSm-Bold.otf"; sourceTree = ""; }; FF3A3DEE1BFBBAC600ADC01A /* ChronicleSSm-BoldItalic.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "ChronicleSSm-BoldItalic.otf"; sourceTree = ""; }; @@ -1109,10 +1131,6 @@ FF753CCB175858FC00344EC9 /* fmdb.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = fmdb.m; sourceTree = ""; }; FF753CCC175858FC00344EC9 /* FMResultSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMResultSet.h; sourceTree = ""; }; FF753CCD175858FC00344EC9 /* FMResultSet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMResultSet.m; sourceTree = ""; }; - FF793E1713F1A9F700F282D2 /* ASIDataCompressor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIDataCompressor.h; sourceTree = ""; }; - FF793E1813F1A9F700F282D2 /* ASIDataCompressor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIDataCompressor.m; sourceTree = ""; }; - FF793E1913F1A9F700F282D2 /* ASIDataDecompressor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIDataDecompressor.h; sourceTree = ""; }; - FF793E1A13F1A9F700F282D2 /* ASIDataDecompressor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIDataDecompressor.m; sourceTree = ""; }; FF81FB1C1DDE9BF9003FA6B8 /* NewsBlur.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; name = NewsBlur.entitlements; path = NewsBlur/NewsBlur.entitlements; sourceTree = ""; }; FF8364B91755759A008F5C58 /* traverse_text.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = traverse_text.png; sourceTree = ""; }; FF8364BA1755759A008F5C58 /* traverse_text@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "traverse_text@2x.png"; sourceTree = ""; }; @@ -1235,27 +1253,6 @@ FFD1D7301459B63500E46F89 /* BaseViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaseViewController.m; sourceTree = ""; }; FFD6604A1BACA45D006E4B8D /* THCircularProgressView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = THCircularProgressView.h; path = "Other Sources/THCircularProgressView.h"; sourceTree = ""; }; FFD6604B1BACA45D006E4B8D /* THCircularProgressView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = THCircularProgressView.m; path = "Other Sources/THCircularProgressView.m"; sourceTree = ""; }; - FFD6604E1BACA46D006E4B8D /* AFHTTPRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPRequestOperation.h; sourceTree = ""; }; - FFD6604F1BACA46D006E4B8D /* AFHTTPRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFHTTPRequestOperation.m; sourceTree = ""; }; - FFD660501BACA46D006E4B8D /* AFHTTPRequestOperationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPRequestOperationManager.h; sourceTree = ""; }; - FFD660511BACA46D006E4B8D /* AFHTTPRequestOperationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFHTTPRequestOperationManager.m; sourceTree = ""; }; - FFD660521BACA46D006E4B8D /* AFHTTPSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPSessionManager.h; sourceTree = ""; }; - FFD660531BACA46D006E4B8D /* AFHTTPSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFHTTPSessionManager.m; sourceTree = ""; }; - FFD660541BACA46D006E4B8D /* AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFNetworking.h; sourceTree = ""; }; - FFD660551BACA46D006E4B8D /* AFNetworkReachabilityManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFNetworkReachabilityManager.h; sourceTree = ""; }; - FFD660561BACA46D006E4B8D /* AFNetworkReachabilityManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFNetworkReachabilityManager.m; sourceTree = ""; }; - FFD660571BACA46D006E4B8D /* AFSecurityPolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFSecurityPolicy.h; sourceTree = ""; }; - FFD660581BACA46D006E4B8D /* AFSecurityPolicy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFSecurityPolicy.m; sourceTree = ""; }; - FFD660591BACA46D006E4B8D /* AFURLConnectionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLConnectionOperation.h; sourceTree = ""; }; - FFD6605A1BACA46D006E4B8D /* AFURLConnectionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLConnectionOperation.m; sourceTree = ""; }; - FFD6605B1BACA46D006E4B8D /* AFURLRequestSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLRequestSerialization.h; sourceTree = ""; }; - FFD6605C1BACA46D006E4B8D /* AFURLRequestSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLRequestSerialization.m; sourceTree = ""; }; - FFD6605D1BACA46D006E4B8D /* AFURLResponseSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLResponseSerialization.h; sourceTree = ""; }; - FFD6605E1BACA46D006E4B8D /* AFURLResponseSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLResponseSerialization.m; sourceTree = ""; }; - FFD6605F1BACA46D006E4B8D /* AFURLSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLSessionManager.h; sourceTree = ""; }; - FFD660601BACA46D006E4B8D /* AFURLSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLSessionManager.m; sourceTree = ""; }; - FFD6606A1BACA49C006E4B8D /* UIImageView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+AFNetworking.h"; sourceTree = ""; }; - FFD6606B1BACA49C006E4B8D /* UIImageView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+AFNetworking.m"; sourceTree = ""; }; FFD887ED1445F1E800385399 /* AddSiteAutocompleteCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AddSiteAutocompleteCell.h; sourceTree = ""; }; FFD887EE1445F1E800385399 /* AddSiteAutocompleteCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AddSiteAutocompleteCell.m; sourceTree = ""; }; FFDCA0B616E80877000D8E0C /* GCOAuth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GCOAuth.h; sourceTree = ""; }; @@ -1477,10 +1474,10 @@ FF03AFE619F87F2E0063002A /* TUSafariActivity */, FFA045AD19CA49D700618DC4 /* SloppySwiper */, FFCDD8FB17F6368F000C6483 /* MCSwipeTableViewCell */, - FF2EB77E1AA65504002549A7 /* InAppSettingsKit */, FF753CC2175858FC00344EC9 /* fmdb */, FF22FE5316E53ADC0046165A /* Underscore */, - FF1F13CC18A9C2BE00FDA816 /* TMCache */, + FF2924CE1E93293F00FCFA63 /* PINCache */, + FF34FD2A1E9D93CB0062F8ED /* InAppSettingsKit */, 43A4C3E615B0099B008787B5 /* NewsBlur_Prefix.pch */, 43A4C3B915B00966008787B5 /* ABTableViewCell.h */, 43A4C3BA15B00966008787B5 /* ABTableViewCell.m */, @@ -1552,7 +1549,6 @@ 78095E44128EF37E00230C8E /* SystemConfiguration.framework */, FF2D8C6B1487F05100057B80 /* Twitter.framework */, 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */, - 78095E23128EF30C00230C8E /* ASI */, FF8D1EBC1BAA311000725D8A /* SBJSON */, FF8D1EA41BAA304E00725D8A /* Reachability */, ); @@ -2069,33 +2065,6 @@ name = Models; sourceTree = ""; }; - 78095E23128EF30C00230C8E /* ASI */ = { - isa = PBXGroup; - children = ( - 78095E24128EF30C00230C8E /* ASIAuthenticationDialog.h */, - 78095E25128EF30C00230C8E /* ASIAuthenticationDialog.m */, - FF793E1713F1A9F700F282D2 /* ASIDataCompressor.h */, - FF793E1813F1A9F700F282D2 /* ASIDataCompressor.m */, - FF793E1913F1A9F700F282D2 /* ASIDataDecompressor.h */, - FF793E1A13F1A9F700F282D2 /* ASIDataDecompressor.m */, - 78095E26128EF30C00230C8E /* ASICacheDelegate.h */, - 78095E27128EF30C00230C8E /* ASIDownloadCache.h */, - 78095E28128EF30C00230C8E /* ASIDownloadCache.m */, - 78095E29128EF30C00230C8E /* ASIFormDataRequest.h */, - 78095E2A128EF30C00230C8E /* ASIFormDataRequest.m */, - 78095E2B128EF30C00230C8E /* ASIHTTPRequest.h */, - 78095E2C128EF30C00230C8E /* ASIHTTPRequest.m */, - 78095E2D128EF30C00230C8E /* ASIHTTPRequestConfig.h */, - 78095E2E128EF30C00230C8E /* ASIHTTPRequestDelegate.h */, - 78095E2F128EF30C00230C8E /* ASIInputStream.h */, - 78095E30128EF30C00230C8E /* ASIInputStream.m */, - 78095E31128EF30C00230C8E /* ASINetworkQueue.h */, - 78095E32128EF30C00230C8E /* ASINetworkQueue.m */, - 78095E33128EF30C00230C8E /* ASIProgressDelegate.h */, - ); - path = ASI; - sourceTree = ""; - }; FF03AFE619F87F2E0063002A /* TUSafariActivity */ = { isa = PBXGroup; children = ( @@ -2139,20 +2108,6 @@ path = "Other Sources/NJKWebViewProgress"; sourceTree = ""; }; - FF1F13CC18A9C2BE00FDA816 /* TMCache */ = { - isa = PBXGroup; - children = ( - FF1F13CD18A9C2BE00FDA816 /* TMCache.h */, - FF1F13CE18A9C2BE00FDA816 /* TMCache.m */, - FF1F13CF18A9C2BE00FDA816 /* TMDiskCache.h */, - FF1F13D018A9C2BE00FDA816 /* TMDiskCache.m */, - FF1F13D118A9C2BE00FDA816 /* TMMemoryCache.h */, - FF1F13D218A9C2BE00FDA816 /* TMMemoryCache.m */, - ); - name = TMCache; - path = "Other Sources/TMCache"; - sourceTree = ""; - }; FF22FE5216E52BAD0046165A /* Foundation */ = { isa = PBXGroup; children = ( @@ -2211,6 +2166,22 @@ path = Underscore; sourceTree = ""; }; + FF2924CE1E93293F00FCFA63 /* PINCache */ = { + isa = PBXGroup; + children = ( + FF2924DD1E932D2900FCFA63 /* Nullability.h */, + FF2924DE1E932D2900FCFA63 /* PINCache.h */, + FF2924DF1E932D2900FCFA63 /* PINCache.m */, + FF2924E01E932D2900FCFA63 /* PINCacheObjectSubscripting.h */, + FF2924E11E932D2900FCFA63 /* PINDiskCache.h */, + FF2924E21E932D2900FCFA63 /* PINDiskCache.m */, + FF2924E31E932D2900FCFA63 /* PINMemoryCache.h */, + FF2924E41E932D2900FCFA63 /* PINMemoryCache.m */, + ); + name = PINCache; + path = "Other Sources/PINCache"; + sourceTree = ""; + }; FF29708816DD7AA400E92F85 /* segmented */ = { isa = PBXGroup; children = ( @@ -2223,72 +2194,76 @@ path = segmented; sourceTree = ""; }; - FF2EB77E1AA65504002549A7 /* InAppSettingsKit */ = { + FF34FD2A1E9D93CB0062F8ED /* InAppSettingsKit */ = { isa = PBXGroup; children = ( - FF2EB77F1AA65504002549A7 /* Controllers */, - FF2EB7891AA65504002549A7 /* Models */, - FF2EB7941AA65504002549A7 /* Resources */, - FF2EB7A51AA65504002549A7 /* Views */, + FF34FD2B1E9D93CB0062F8ED /* Controllers */, + FF34FD351E9D93CB0062F8ED /* Models */, + FF34FD401E9D93CB0062F8ED /* Resources */, + FF34FD511E9D93CB0062F8ED /* Views */, ); name = InAppSettingsKit; path = "Other Sources/InAppSettingsKit"; sourceTree = ""; }; - FF2EB77F1AA65504002549A7 /* Controllers */ = { + FF34FD2B1E9D93CB0062F8ED /* Controllers */ = { isa = PBXGroup; children = ( - FF2EB7801AA65504002549A7 /* IASKAppSettingsViewController.h */, - FF2EB7811AA65504002549A7 /* IASKAppSettingsViewController.m */, - FF2EB7821AA65504002549A7 /* IASKAppSettingsWebViewController.h */, - FF2EB7831AA65504002549A7 /* IASKAppSettingsWebViewController.m */, - FF2EB7841AA65504002549A7 /* IASKMultipleValueSelection.h */, - FF2EB7851AA65504002549A7 /* IASKMultipleValueSelection.m */, - FF2EB7861AA65504002549A7 /* IASKSpecifierValuesViewController.h */, - FF2EB7871AA65504002549A7 /* IASKSpecifierValuesViewController.m */, - FF2EB7881AA65504002549A7 /* IASKViewController.h */, + FF34FD2C1E9D93CB0062F8ED /* IASKAppSettingsViewController.h */, + FF34FD2D1E9D93CB0062F8ED /* IASKAppSettingsViewController.m */, + FF34FD2E1E9D93CB0062F8ED /* IASKAppSettingsWebViewController.h */, + FF34FD2F1E9D93CB0062F8ED /* IASKAppSettingsWebViewController.m */, + FF34FD301E9D93CB0062F8ED /* IASKMultipleValueSelection.h */, + FF34FD311E9D93CB0062F8ED /* IASKMultipleValueSelection.m */, + FF34FD321E9D93CB0062F8ED /* IASKSpecifierValuesViewController.h */, + FF34FD331E9D93CB0062F8ED /* IASKSpecifierValuesViewController.m */, + FF34FD341E9D93CB0062F8ED /* IASKViewController.h */, ); path = Controllers; sourceTree = ""; }; - FF2EB7891AA65504002549A7 /* Models */ = { + FF34FD351E9D93CB0062F8ED /* Models */ = { isa = PBXGroup; children = ( - FF2EB78A1AA65504002549A7 /* IASKSettingsReader.h */, - FF2EB78B1AA65504002549A7 /* IASKSettingsReader.m */, - FF2EB78C1AA65504002549A7 /* IASKSettingsStore.h */, - FF2EB78D1AA65504002549A7 /* IASKSettingsStore.m */, - FF2EB78E1AA65504002549A7 /* IASKSettingsStoreFile.h */, - FF2EB78F1AA65504002549A7 /* IASKSettingsStoreFile.m */, - FF2EB7901AA65504002549A7 /* IASKSettingsStoreUserDefaults.h */, - FF2EB7911AA65504002549A7 /* IASKSettingsStoreUserDefaults.m */, - FF2EB7921AA65504002549A7 /* IASKSpecifier.h */, - FF2EB7931AA65504002549A7 /* IASKSpecifier.m */, + FF34FD361E9D93CB0062F8ED /* IASKSettingsReader.h */, + FF34FD371E9D93CB0062F8ED /* IASKSettingsReader.m */, + FF34FD381E9D93CB0062F8ED /* IASKSettingsStore.h */, + FF34FD391E9D93CB0062F8ED /* IASKSettingsStore.m */, + FF34FD3A1E9D93CB0062F8ED /* IASKSettingsStoreFile.h */, + FF34FD3B1E9D93CB0062F8ED /* IASKSettingsStoreFile.m */, + FF34FD3C1E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.h */, + FF34FD3D1E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.m */, + FF34FD3E1E9D93CB0062F8ED /* IASKSpecifier.h */, + FF34FD3F1E9D93CB0062F8ED /* IASKSpecifier.m */, ); path = Models; sourceTree = ""; }; - FF2EB7941AA65504002549A7 /* Resources */ = { + FF34FD401E9D93CB0062F8ED /* Resources */ = { isa = PBXGroup; children = ( - FF2EB7951AA65504002549A7 /* IASKLocalizable.strings */, + FF34FD411E9D93CB0062F8ED /* IASKLocalizable.strings */, ); path = Resources; sourceTree = ""; }; - FF2EB7A51AA65504002549A7 /* Views */ = { + FF34FD511E9D93CB0062F8ED /* Views */ = { isa = PBXGroup; children = ( - FF2EB7A61AA65504002549A7 /* IASKPSSliderSpecifierViewCell.h */, - FF2EB7A71AA65504002549A7 /* IASKPSSliderSpecifierViewCell.m */, - FF2EB7A81AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.h */, - FF2EB7A91AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.m */, - FF2EB7AA1AA65504002549A7 /* IASKSlider.h */, - FF2EB7AB1AA65504002549A7 /* IASKSlider.m */, - FF2EB7AC1AA65504002549A7 /* IASKSwitch.h */, - FF2EB7AD1AA65504002549A7 /* IASKSwitch.m */, - FF2EB7AE1AA65504002549A7 /* IASKTextField.h */, - FF2EB7AF1AA65504002549A7 /* IASKTextField.m */, + FF34FD521E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.h */, + FF34FD531E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.m */, + FF34FD541E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.h */, + FF34FD551E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.m */, + FF34FD561E9D93CB0062F8ED /* IASKSlider.h */, + FF34FD571E9D93CB0062F8ED /* IASKSlider.m */, + FF34FD581E9D93CB0062F8ED /* IASKSwitch.h */, + FF34FD591E9D93CB0062F8ED /* IASKSwitch.m */, + FF34FD5A1E9D93CB0062F8ED /* IASKTextField.h */, + FF34FD5B1E9D93CB0062F8ED /* IASKTextField.m */, + FF34FD5C1E9D93CB0062F8ED /* IASKTextView.h */, + FF34FD5D1E9D93CB0062F8ED /* IASKTextView.m */, + FF34FD5E1E9D93CB0062F8ED /* IASKTextViewCell.h */, + FF34FD5F1E9D93CB0062F8ED /* IASKTextViewCell.m */, ); path = Views; sourceTree = ""; @@ -2439,27 +2414,39 @@ FFD6604D1BACA46D006E4B8D /* AFNetworking */ = { isa = PBXGroup; children = ( - FFD6606A1BACA49C006E4B8D /* UIImageView+AFNetworking.h */, - FFD6606B1BACA49C006E4B8D /* UIImageView+AFNetworking.m */, - FFD6604E1BACA46D006E4B8D /* AFHTTPRequestOperation.h */, - FFD6604F1BACA46D006E4B8D /* AFHTTPRequestOperation.m */, - FFD660501BACA46D006E4B8D /* AFHTTPRequestOperationManager.h */, - FFD660511BACA46D006E4B8D /* AFHTTPRequestOperationManager.m */, - FFD660521BACA46D006E4B8D /* AFHTTPSessionManager.h */, - FFD660531BACA46D006E4B8D /* AFHTTPSessionManager.m */, - FFD660541BACA46D006E4B8D /* AFNetworking.h */, - FFD660551BACA46D006E4B8D /* AFNetworkReachabilityManager.h */, - FFD660561BACA46D006E4B8D /* AFNetworkReachabilityManager.m */, - FFD660571BACA46D006E4B8D /* AFSecurityPolicy.h */, - FFD660581BACA46D006E4B8D /* AFSecurityPolicy.m */, - FFD660591BACA46D006E4B8D /* AFURLConnectionOperation.h */, - FFD6605A1BACA46D006E4B8D /* AFURLConnectionOperation.m */, - FFD6605B1BACA46D006E4B8D /* AFURLRequestSerialization.h */, - FFD6605C1BACA46D006E4B8D /* AFURLRequestSerialization.m */, - FFD6605D1BACA46D006E4B8D /* AFURLResponseSerialization.h */, - FFD6605E1BACA46D006E4B8D /* AFURLResponseSerialization.m */, - FFD6605F1BACA46D006E4B8D /* AFURLSessionManager.h */, - FFD660601BACA46D006E4B8D /* AFURLSessionManager.m */, + FF29249E1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.h */, + FF29249F1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.m */, + FF2924A01E92DF7C00FCFA63 /* AFHTTPSessionManager.h */, + FF2924A11E92DF7C00FCFA63 /* AFHTTPSessionManager.m */, + FF2924A21E92DF7C00FCFA63 /* AFImageDownloader.h */, + FF2924A31E92DF7C00FCFA63 /* AFImageDownloader.m */, + FF2924A41E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.h */, + FF2924A51E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.m */, + FF2924A61E92DF7C00FCFA63 /* AFNetworking.h */, + FF2924A71E92DF7C00FCFA63 /* AFNetworkReachabilityManager.h */, + FF2924A81E92DF7C00FCFA63 /* AFNetworkReachabilityManager.m */, + FF2924A91E92DF7C00FCFA63 /* AFSecurityPolicy.h */, + FF2924AA1E92DF7C00FCFA63 /* AFSecurityPolicy.m */, + FF2924AB1E92DF7C00FCFA63 /* AFURLRequestSerialization.h */, + FF2924AC1E92DF7C00FCFA63 /* AFURLRequestSerialization.m */, + FF2924AD1E92DF7C00FCFA63 /* AFURLResponseSerialization.h */, + FF2924AE1E92DF7C00FCFA63 /* AFURLResponseSerialization.m */, + FF2924AF1E92DF7C00FCFA63 /* AFURLSessionManager.h */, + FF2924B01E92DF7C00FCFA63 /* AFURLSessionManager.m */, + FF2924B11E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.h */, + FF2924B21E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.m */, + FF2924B31E92DF7C00FCFA63 /* UIButton+AFNetworking.h */, + FF2924B41E92DF7C00FCFA63 /* UIButton+AFNetworking.m */, + FF2924B51E92DF7C00FCFA63 /* UIImage+AFNetworking.h */, + FF2924B61E92DF7C00FCFA63 /* UIImageView+AFNetworking.h */, + FF2924B71E92DF7C00FCFA63 /* UIImageView+AFNetworking.m */, + FF2924B81E92DF7C00FCFA63 /* UIKit+AFNetworking.h */, + FF2924B91E92DF7C00FCFA63 /* UIProgressView+AFNetworking.h */, + FF2924BA1E92DF7C00FCFA63 /* UIProgressView+AFNetworking.m */, + FF2924BB1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.h */, + FF2924BC1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.m */, + FF2924BD1E92DF7C00FCFA63 /* UIWebView+AFNetworking.h */, + FF2924BE1E92DF7C00FCFA63 /* UIWebView+AFNetworking.m */, ); name = AFNetworking; path = "Other Sources/AFNetworking"; @@ -2522,7 +2509,7 @@ 29B97313FDCFA39411CA2CEA /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0820; ORGANIZATIONNAME = NewsBlur; TargetAttributes = { 1D6058900D05DD3D006BFB54 = { @@ -2754,7 +2741,6 @@ FF41309D162CEC7100DDB6A7 /* time.png in Resources */, FF4130A0162CECAE00DDB6A7 /* email.png in Resources */, FF6A23391644957800E15989 /* StoryPageControl.xib in Resources */, - FF2EB7B91AA65504002549A7 /* IASKLocalizable.strings in Resources */, FF67D3B7168977690057A7DA /* TrainerViewController.xib in Resources */, FF67D3BB168A70630057A7DA /* trainer.css in Resources */, FF3FA8951BB26B6A001F7C32 /* copy_link@2x.png in Resources */, @@ -2830,6 +2816,7 @@ 17CBD3C21BF6ED2C003FCCAE /* menu_icn_markread.png in Resources */, FFB7050E1925921F0052101C /* line_spacing_xs.png in Resources */, FF22FE4E16E41EB40046165A /* disclosure_down.png in Resources */, + FF34FD691E9D93CB0062F8ED /* IASKLocalizable.strings in Resources */, FF21B1111C821E150053938A /* disclosure_border_dark@2x.png in Resources */, 176129611C630AEB00702FE4 /* mute_feed_off@2x.png in Resources */, 17BE5A741C5DDA8C0075F92C /* barbutton_sort_asc@3x.png in Resources */, @@ -2982,60 +2969,48 @@ FF0FAEB117B084D2008651F9 /* OfflineCleanImages.m in Sources */, 43F44B1C159D8DBC00F48F8A /* FeedTableCell.m in Sources */, 1D3623260D0F684500981E51 /* NewsBlurAppDelegate.m in Sources */, - FF2EB7B81AA65504002549A7 /* IASKSpecifier.m in Sources */, 17432C861C5343C0003F8FD6 /* FeedChooserTitleView.m in Sources */, 28D7ACF80DDB3853001CB0EB /* NewsBlurViewController.m in Sources */, FFFF683D19D628000081904A /* NBURLCache.m in Sources */, - FF2EB7B61AA65504002549A7 /* IASKSettingsStoreFile.m in Sources */, 787A0CDB11CE65330056422D /* FeedDetailViewController.m in Sources */, - FF2EB7B51AA65504002549A7 /* IASKSettingsStore.m in Sources */, 7842ECF811D44A530066CF9D /* StoryDetailViewController.m in Sources */, - FFD6606C1BACA49C006E4B8D /* UIImageView+AFNetworking.m in Sources */, + FF2924E61E932D2900FCFA63 /* PINDiskCache.m in Sources */, 7843F50511EEB1A000675F64 /* FeedDetailTableCell.m in Sources */, 784B50ED127E3F68008F90EA /* LoginViewController.m in Sources */, - 78095E34128EF30C00230C8E /* ASIAuthenticationDialog.m in Sources */, - 78095E35128EF30D00230C8E /* ASIDownloadCache.m in Sources */, FF5ACC241DE5F0C000FBD044 /* NotificationsViewController.m in Sources */, - FF2EB7B41AA65504002549A7 /* IASKSettingsReader.m in Sources */, - 78095E36128EF30D00230C8E /* ASIFormDataRequest.m in Sources */, - FFD660691BACA46D006E4B8D /* AFURLSessionManager.m in Sources */, - 78095E37128EF30D00230C8E /* ASIHTTPRequest.m in Sources */, FFCDD8FE17F6368F000C6483 /* MCSwipeTableViewCell.m in Sources */, - FFD660681BACA46D006E4B8D /* AFURLResponseSerialization.m in Sources */, - 78095E38128EF30D00230C8E /* ASIInputStream.m in Sources */, - FFD660611BACA46D006E4B8D /* AFHTTPRequestOperation.m in Sources */, - 78095E39128EF30D00230C8E /* ASINetworkQueue.m in Sources */, 78095EC9128F30B500230C8E /* OriginalStoryViewController.m in Sources */, FF1C4E171A3FB1F4000995E3 /* NBActivityItemSource.m in Sources */, 17C4954B1C129863004805A7 /* UISearchBar+Field.m in Sources */, - FF793E1B13F1A9F700F282D2 /* ASIDataCompressor.m in Sources */, FF3A3E171BFC3F1300ADC01A /* NSNull+JSON.m in Sources */, - FF793E1C13F1A9F700F282D2 /* ASIDataDecompressor.m in Sources */, + FF34FD6C1E9D93CB0062F8ED /* IASKSlider.m in Sources */, FF5EA47F143B691000B7563D /* AddSiteViewController.m in Sources */, FF1F13D818AAC97900FDA816 /* UIImage+Resize.m in Sources */, FFD887F01445F1E800385399 /* AddSiteAutocompleteCell.m in Sources */, FF8D1ECD1BAA311000725D8A /* SBJson4StreamParser.m in Sources */, FFE5322F144C8AC300ACFDE0 /* Utilities.m in Sources */, FFD1D7311459B63500E46F89 /* BaseViewController.m in Sources */, + FF34FD6D1E9D93CB0062F8ED /* IASKSwitch.m in Sources */, FF9B8BB217F2351A0036A41C /* NBBarButtonItem.m in Sources */, FF2D8CE514893BC000057B80 /* MoveSiteViewController.m in Sources */, 433323CD158968ED0025064D /* FirstTimeUserViewController.m in Sources */, + FF2924C41E92DF7C00FCFA63 /* AFSecurityPolicy.m in Sources */, + FF34FD6B1E9D93CB0062F8ED /* IASKPSTextFieldSpecifierViewCell.m in Sources */, FF8D1ED01BAA311000725D8A /* SBJson4StreamWriter.m in Sources */, 43763AD1158F90B100B3DBE2 /* FontSettingsViewController.m in Sources */, + FF34FD601E9D93CB0062F8ED /* IASKAppSettingsViewController.m in Sources */, 439DAB201590DA350019B0EB /* FeedsMenuViewController.m in Sources */, 17CBD3BF1BF66B6C003FCCAE /* MarkReadMenuViewController.m in Sources */, FFA045B519CA49D700618DC4 /* SSWAnimator.m in Sources */, 437AA8CA159394E2005463F5 /* ShareViewController.m in Sources */, FF8D1ECC1BAA311000725D8A /* SBJson4Parser.m in Sources */, - FFD660661BACA46D006E4B8D /* AFURLConnectionOperation.m in Sources */, - FF2EB7B01AA65504002549A7 /* IASKAppSettingsViewController.m in Sources */, 431B856815A0C45200DCE497 /* FriendsListViewController.m in Sources */, - FFD660621BACA46D006E4B8D /* AFHTTPRequestOperationManager.m in Sources */, 431B856E15A0D91E00DCE497 /* UserProfileViewController.m in Sources */, FF8D1EDB1BAA3CCD00725D8A /* JNWThrottledBlock.m in Sources */, - FF2EB7B31AA65504002549A7 /* IASKSpecifierValuesViewController.m in Sources */, 431B858115A23C6B00DCE497 /* ProfileBadge.m in Sources */, 437F974C15ACA0ED0007136B /* DashboardViewController.m in Sources */, + FF2924C71E92DF7C00FCFA63 /* AFURLSessionManager.m in Sources */, + FF2924CD1E92DF7C00FCFA63 /* UIWebView+AFNetworking.m in Sources */, 437F976F15AE21290007136B /* ActivityModule.m in Sources */, 437F977215AE70E30007136B /* InteractionsModule.m in Sources */, 43A4C3D715B00966008787B5 /* ABTableViewCell.m in Sources */, @@ -3043,65 +3018,72 @@ 43A4C3DA15B00966008787B5 /* GTMNString+HTML.m in Sources */, 43A4C3DC15B00966008787B5 /* main.m in Sources */, 43A4C3DD15B00966008787B5 /* MBProgressHUD.m in Sources */, + FF2924CA1E92DF7C00FCFA63 /* UIImageView+AFNetworking.m in Sources */, 43A4C3E115B00966008787B5 /* NSString+HTML.m in Sources */, + FF34FD641E9D93CB0062F8ED /* IASKSettingsReader.m in Sources */, FF6282151A11613900271FDB /* UserTagsViewController.m in Sources */, - FF2EB7BD1AA65504002549A7 /* IASKSwitch.m in Sources */, 43A4C3E315B00966008787B5 /* StringHelper.m in Sources */, - FF1F13D518A9C2BE00FDA816 /* TMMemoryCache.m in Sources */, + FF2924C91E92DF7C00FCFA63 /* UIButton+AFNetworking.m in Sources */, 43A4C3E415B00966008787B5 /* TransparentToolbar.m in Sources */, - FFD660651BACA46D006E4B8D /* AFSecurityPolicy.m in Sources */, FFD6604C1BACA45D006E4B8D /* THCircularProgressView.m in Sources */, + FF34FD681E9D93CB0062F8ED /* IASKSpecifier.m in Sources */, + FF2924C61E92DF7C00FCFA63 /* AFURLResponseSerialization.m in Sources */, FFA0484419CA73B700618DC4 /* UIView+ViewController.m in Sources */, + FF2924E51E932D2900FCFA63 /* PINCache.m in Sources */, 17432C891C534BC6003F8FD6 /* FeedChooserViewCell.m in Sources */, 43A4C3E515B00966008787B5 /* UIView+TKCategory.m in Sources */, + FF2924C11E92DF7C00FCFA63 /* AFImageDownloader.m in Sources */, + FF34FD631E9D93CB0062F8ED /* IASKSpecifierValuesViewController.m in Sources */, + FF2924C31E92DF7C00FCFA63 /* AFNetworkReachabilityManager.m in Sources */, 43F6A79D15B0CDC60092EE91 /* ActivityCell.m in Sources */, FF3FA88D1BB2677C001F7C32 /* NBCopyLinkActivity.m in Sources */, 17432C831C53438D003F8FD6 /* FeedChooserViewController.m in Sources */, FF03B00919F987E00063002A /* NJKWebViewProgress.m in Sources */, 010EDEFA1B2386B7003B79DE /* OnePasswordExtension.m in Sources */, 43ABBCAA15B53A1400EA3111 /* InteractionCell.m in Sources */, - FF2EB7BE1AA65504002549A7 /* IASKTextField.m in Sources */, + FF34FD611E9D93CB0062F8ED /* IASKAppSettingsWebViewController.m in Sources */, FF8D1ECF1BAA311000725D8A /* SBJson4StreamTokeniser.m in Sources */, - FFD660641BACA46D006E4B8D /* AFNetworkReachabilityManager.m in Sources */, FF5ACC2E1DE63C6D00FBD044 /* MultiSelectSegmentedControl.m in Sources */, FF8D1ED81BAA33BA00725D8A /* NSObject+SBJSON.m in Sources */, - FF2EB7B11AA65504002549A7 /* IASKAppSettingsWebViewController.m in Sources */, - FF1F13D418A9C2BE00FDA816 /* TMDiskCache.m in Sources */, - FF2EB7BB1AA65504002549A7 /* IASKPSTextFieldSpecifierViewCell.m in Sources */, - FFD660631BACA46D006E4B8D /* AFHTTPSessionManager.m in Sources */, + FF34FD651E9D93CB0062F8ED /* IASKSettingsStore.m in Sources */, 43D818A315B940C200733444 /* DataUtilities.m in Sources */, FF03AFFD19F881380063002A /* ARChromeActivity.m in Sources */, 4383DCD615BB8B88007E6611 /* SmallActivityCell.m in Sources */, 43E8381E15BC73EB000553BE /* FirstTimeUserAddFriendsViewController.m in Sources */, + FF34FD661E9D93CB0062F8ED /* IASKSettingsStoreFile.m in Sources */, 43E8382015BC73EB000553BE /* FirstTimeUserAddNewsBlurViewController.m in Sources */, 43E8382215BC73EB000553BE /* FirstTimeUserAddSitesViewController.m in Sources */, 436ACA8D15BF1088004E01CC /* NBContainerViewController.m in Sources */, FFA0483E19CA5B8400618DC4 /* EventWindow.m in Sources */, 432EBD2315D1D4070000729D /* AddSiteTableCell.m in Sources */, - FF2EB7BC1AA65504002549A7 /* IASKSlider.m in Sources */, 438FEDE815D5B15F00E3B3C9 /* FollowGrid.m in Sources */, FF8D1ED21BAA311000725D8A /* SBJson4Writer.m in Sources */, 43B232C015D5F61700D035B4 /* AuthorizeServicesViewController.m in Sources */, - FF2EB7B71AA65504002549A7 /* IASKSettingsStoreUserDefaults.m in Sources */, - FF2EB7BA1AA65504002549A7 /* IASKPSSliderSpecifierViewCell.m in Sources */, + FF2924E71E932D2900FCFA63 /* PINMemoryCache.m in Sources */, + FF2924BF1E92DF7C00FCFA63 /* AFAutoPurgingImageCache.m in Sources */, + FF2924C21E92DF7C00FCFA63 /* AFNetworkActivityIndicatorManager.m in Sources */, 43CE0F5F15DADB7F00608ED8 /* SiteCell.m in Sources */, FFA045B419CA49D700618DC4 /* SloppySwiper.m in Sources */, E160F0571C9DAC2C00CB96DF /* UIViewController+HidePopover.m in Sources */, FFDE35CC161B8F870034BFDE /* FolderTitleView.m in Sources */, FFDE35DA161D12250034BFDE /* UnreadCountView.m in Sources */, FFDE35EA162799B90034BFDE /* FeedDetailMenuViewController.m in Sources */, + FF2924C01E92DF7C00FCFA63 /* AFHTTPSessionManager.m in Sources */, + FF34FD621E9D93CB0062F8ED /* IASKMultipleValueSelection.m in Sources */, FF4130A3162E10CF00DDB6A7 /* MenuTableViewCell.m in Sources */, 17432C7F1C533FBC003F8FD6 /* MenuViewController.m in Sources */, 17E635AF1C548C580075338E /* FeedChooserItem.m in Sources */, FF6A233216448E0700E15989 /* StoryPageControl.m in Sources */, FF67D3B2168924C40057A7DA /* TrainerViewController.m in Sources */, FF1660CD16D6FD8A00AF8541 /* SmallInteractionCell.m in Sources */, + FF34FD6E1E9D93CB0062F8ED /* IASKTextField.m in Sources */, FF8D1ECE1BAA311000725D8A /* SBJson4StreamParserState.m in Sources */, + FF2924CB1E92DF7C00FCFA63 /* UIProgressView+AFNetworking.m in Sources */, FF4151C016DED9660013E84B /* UIBarButtonItem+Image.m in Sources */, FF8D1ED11BAA311000725D8A /* SBJson4StreamWriterState.m in Sources */, FF5ACC271DE5FA3E00FBD044 /* NotificationFeedCell.m in Sources */, FFA045B619CA49D700618DC4 /* SSWDirectionalPanGestureRecognizer.m in Sources */, - FF1F13D318A9C2BE00FDA816 /* TMCache.m in Sources */, + FF2924CC1E92DF7C00FCFA63 /* UIRefreshControl+AFNetworking.m in Sources */, FF22FE5E16E53ADC0046165A /* Underscore+Functional.m in Sources */, 17C074961C14C46B00CFCDB7 /* ThemeManager.m in Sources */, FF22FE5F16E53ADC0046165A /* Underscore.m in Sources */, @@ -3113,20 +3095,24 @@ FF753CCF175858FC00344EC9 /* FMDatabaseAdditions.m in Sources */, FF753CD0175858FC00344EC9 /* FMDatabasePool.m in Sources */, FF753CD1175858FC00344EC9 /* FMDatabaseQueue.m in Sources */, + FF2924C81E92DF7C00FCFA63 /* UIActivityIndicatorView+AFNetworking.m in Sources */, FF753CD3175858FC00344EC9 /* FMResultSet.m in Sources */, - FFD660671BACA46D006E4B8D /* AFURLRequestSerialization.m in Sources */, FF6618C8176184560039913B /* NBNotifier.m in Sources */, FF03AFF319F87F2E0063002A /* TUSafariActivity.m in Sources */, FF03B00A19F987E00063002A /* NJKWebViewProgressView.m in Sources */, - FF2EB7B21AA65504002549A7 /* IASKMultipleValueSelection.m in Sources */, FF11045F176950F900502C29 /* NBLoadingCell.m in Sources */, FFA0484119CA5F5B00618DC4 /* UIWebView+Offsets.m in Sources */, + FF34FD701E9D93CB0062F8ED /* IASKTextViewCell.m in Sources */, FFAD89C218AC45A100D68567 /* StoriesCollection.m in Sources */, + FF34FD6F1E9D93CB0062F8ED /* IASKTextView.m in Sources */, FF855B5B1794B0670098D48A /* OfflineSyncUnreads.m in Sources */, + FF34FD6A1E9D93CB0062F8ED /* IASKPSSliderSpecifierViewCell.m in Sources */, FF855B5E1794B0760098D48A /* OfflineFetchStories.m in Sources */, FF855B611794B0830098D48A /* OfflineFetchImages.m in Sources */, + FF2924C51E92DF7C00FCFA63 /* AFURLRequestSerialization.m in Sources */, FF8D1EA71BAA304E00725D8A /* Reachability.m in Sources */, FFCDD90117F65A71000C6483 /* NBSwipeableCell.m in Sources */, + FF34FD671E9D93CB0062F8ED /* IASKSettingsStoreUserDefaults.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -3149,24 +3135,24 @@ /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ - FF2EB7951AA65504002549A7 /* IASKLocalizable.strings */ = { + FF34FD411E9D93CB0062F8ED /* IASKLocalizable.strings */ = { isa = PBXVariantGroup; children = ( - FF2EB7961AA65504002549A7 /* Base */, - FF2EB7971AA65504002549A7 /* de */, - FF2EB7981AA65504002549A7 /* el */, - FF2EB7991AA65504002549A7 /* en */, - FF2EB79A1AA65504002549A7 /* es */, - FF2EB79B1AA65504002549A7 /* fr */, - FF2EB79C1AA65504002549A7 /* it */, - FF2EB79D1AA65504002549A7 /* ja */, - FF2EB79E1AA65504002549A7 /* nl */, - FF2EB79F1AA65504002549A7 /* pt-PT */, - FF2EB7A01AA65504002549A7 /* pt */, - FF2EB7A11AA65504002549A7 /* ru */, - FF2EB7A21AA65504002549A7 /* sv */, - FF2EB7A31AA65504002549A7 /* th */, - FF2EB7A41AA65504002549A7 /* tr */, + FF34FD421E9D93CB0062F8ED /* Base */, + FF34FD431E9D93CB0062F8ED /* de */, + FF34FD441E9D93CB0062F8ED /* el */, + FF34FD451E9D93CB0062F8ED /* en */, + FF34FD461E9D93CB0062F8ED /* es */, + FF34FD471E9D93CB0062F8ED /* fr */, + FF34FD481E9D93CB0062F8ED /* it */, + FF34FD491E9D93CB0062F8ED /* ja */, + FF34FD4A1E9D93CB0062F8ED /* nl */, + FF34FD4B1E9D93CB0062F8ED /* pt-PT */, + FF34FD4C1E9D93CB0062F8ED /* pt */, + FF34FD4D1E9D93CB0062F8ED /* ru */, + FF34FD4E1E9D93CB0062F8ED /* sv */, + FF34FD4F1E9D93CB0062F8ED /* th */, + FF34FD501E9D93CB0062F8ED /* tr */, ); name = IASKLocalizable.strings; sourceTree = ""; @@ -3266,7 +3252,7 @@ C01FCF4F08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_ENABLE_MODULES = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; @@ -3291,7 +3277,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; HEADER_SEARCH_PATHS = "$(BUILT_PRODUCTS_DIR)/**"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; ONLY_ACTIVE_ARCH = YES; OTHER_LDFLAGS = "-ObjC"; PROVISIONING_PROFILE = ""; @@ -3305,7 +3291,7 @@ C01FCF5008A954540054247B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_ENABLE_MODULES = YES; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; @@ -3329,7 +3315,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; HEADER_SEARCH_PATHS = "$(BUILT_PRODUCTS_DIR)/**"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; ONLY_ACTIVE_ARCH = NO; OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; OTHER_LDFLAGS = "-ObjC"; diff --git a/clients/ios/Other Sources/AFNetworking/AFAutoPurgingImageCache.h b/clients/ios/Other Sources/AFNetworking/AFAutoPurgingImageCache.h new file mode 100755 index 000000000..9bdc15cc7 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/AFAutoPurgingImageCache.h @@ -0,0 +1,149 @@ +// AFAutoPurgingImageCache.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import +#import + +#if TARGET_OS_IOS || TARGET_OS_TV +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + The `AFImageCache` protocol defines a set of APIs for adding, removing and fetching images from a cache synchronously. + */ +@protocol AFImageCache + +/** + Adds the image to the cache with the given identifier. + + @param image The image to cache. + @param identifier The unique identifier for the image in the cache. + */ +- (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier; + +/** + Removes the image from the cache matching the given identifier. + + @param identifier The unique identifier for the image in the cache. + + @return A BOOL indicating whether or not the image was removed from the cache. + */ +- (BOOL)removeImageWithIdentifier:(NSString *)identifier; + +/** + Removes all images from the cache. + + @return A BOOL indicating whether or not all images were removed from the cache. + */ +- (BOOL)removeAllImages; + +/** + Returns the image in the cache associated with the given identifier. + + @param identifier The unique identifier for the image in the cache. + + @return An image for the matching identifier, or nil. + */ +- (nullable UIImage *)imageWithIdentifier:(NSString *)identifier; +@end + + +/** + The `ImageRequestCache` protocol extends the `ImageCache` protocol by adding methods for adding, removing and fetching images from a cache given an `NSURLRequest` and additional identifier. + */ +@protocol AFImageRequestCache + +/** + Adds the image to the cache using an identifier created from the request and additional identifier. + + @param image The image to cache. + @param request The unique URL request identifing the image asset. + @param identifier The additional identifier to apply to the URL request to identify the image. + */ +- (void)addImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier; + +/** + Removes the image from the cache using an identifier created from the request and additional identifier. + + @param request The unique URL request identifing the image asset. + @param identifier The additional identifier to apply to the URL request to identify the image. + + @return A BOOL indicating whether or not all images were removed from the cache. + */ +- (BOOL)removeImageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier; + +/** + Returns the image from the cache associated with an identifier created from the request and additional identifier. + + @param request The unique URL request identifing the image asset. + @param identifier The additional identifier to apply to the URL request to identify the image. + + @return An image for the matching request and identifier, or nil. + */ +- (nullable UIImage *)imageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier; + +@end + +/** + The `AutoPurgingImageCache` in an in-memory image cache used to store images up to a given memory capacity. When the memory capacity is reached, the image cache is sorted by last access date, then the oldest image is continuously purged until the preferred memory usage after purge is met. Each time an image is accessed through the cache, the internal access date of the image is updated. + */ +@interface AFAutoPurgingImageCache : NSObject + +/** + The total memory capacity of the cache in bytes. + */ +@property (nonatomic, assign) UInt64 memoryCapacity; + +/** + The preferred memory usage after purge in bytes. During a purge, images will be purged until the memory capacity drops below this limit. + */ +@property (nonatomic, assign) UInt64 preferredMemoryUsageAfterPurge; + +/** + The current total memory usage in bytes of all images stored within the cache. + */ +@property (nonatomic, assign, readonly) UInt64 memoryUsage; + +/** + Initialies the `AutoPurgingImageCache` instance with default values for memory capacity and preferred memory usage after purge limit. `memoryCapcity` defaults to `100 MB`. `preferredMemoryUsageAfterPurge` defaults to `60 MB`. + + @return The new `AutoPurgingImageCache` instance. + */ +- (instancetype)init; + +/** + Initialies the `AutoPurgingImageCache` instance with the given memory capacity and preferred memory usage + after purge limit. + + @param memoryCapacity The total memory capacity of the cache in bytes. + @param preferredMemoryCapacity The preferred memory usage after purge in bytes. + + @return The new `AutoPurgingImageCache` instance. + */ +- (instancetype)initWithMemoryCapacity:(UInt64)memoryCapacity preferredMemoryCapacity:(UInt64)preferredMemoryCapacity; + +@end + +NS_ASSUME_NONNULL_END + +#endif + diff --git a/clients/ios/Other Sources/AFNetworking/AFAutoPurgingImageCache.m b/clients/ios/Other Sources/AFNetworking/AFAutoPurgingImageCache.m new file mode 100755 index 000000000..1f40715c3 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/AFAutoPurgingImageCache.m @@ -0,0 +1,201 @@ +// AFAutoPurgingImageCache.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import "AFAutoPurgingImageCache.h" + +@interface AFCachedImage : NSObject + +@property (nonatomic, strong) UIImage *image; +@property (nonatomic, strong) NSString *identifier; +@property (nonatomic, assign) UInt64 totalBytes; +@property (nonatomic, strong) NSDate *lastAccessDate; +@property (nonatomic, assign) UInt64 currentMemoryUsage; + +@end + +@implementation AFCachedImage + +-(instancetype)initWithImage:(UIImage *)image identifier:(NSString *)identifier { + if (self = [self init]) { + self.image = image; + self.identifier = identifier; + + CGSize imageSize = CGSizeMake(image.size.width * image.scale, image.size.height * image.scale); + CGFloat bytesPerPixel = 4.0; + CGFloat bytesPerSize = imageSize.width * imageSize.height; + self.totalBytes = (UInt64)bytesPerPixel * (UInt64)bytesPerSize; + self.lastAccessDate = [NSDate date]; + } + return self; +} + +- (UIImage*)accessImage { + self.lastAccessDate = [NSDate date]; + return self.image; +} + +- (NSString *)description { + NSString *descriptionString = [NSString stringWithFormat:@"Idenfitier: %@ lastAccessDate: %@ ", self.identifier, self.lastAccessDate]; + return descriptionString; + +} + +@end + +@interface AFAutoPurgingImageCache () +@property (nonatomic, strong) NSMutableDictionary *cachedImages; +@property (nonatomic, assign) UInt64 currentMemoryUsage; +@property (nonatomic, strong) dispatch_queue_t synchronizationQueue; +@end + +@implementation AFAutoPurgingImageCache + +- (instancetype)init { + return [self initWithMemoryCapacity:100 * 1024 * 1024 preferredMemoryCapacity:60 * 1024 * 1024]; +} + +- (instancetype)initWithMemoryCapacity:(UInt64)memoryCapacity preferredMemoryCapacity:(UInt64)preferredMemoryCapacity { + if (self = [super init]) { + self.memoryCapacity = memoryCapacity; + self.preferredMemoryUsageAfterPurge = preferredMemoryCapacity; + self.cachedImages = [[NSMutableDictionary alloc] init]; + + NSString *queueName = [NSString stringWithFormat:@"com.alamofire.autopurgingimagecache-%@", [[NSUUID UUID] UUIDString]]; + self.synchronizationQueue = dispatch_queue_create([queueName cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_CONCURRENT); + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(removeAllImages) + name:UIApplicationDidReceiveMemoryWarningNotification + object:nil]; + + } + return self; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (UInt64)memoryUsage { + __block UInt64 result = 0; + dispatch_sync(self.synchronizationQueue, ^{ + result = self.currentMemoryUsage; + }); + return result; +} + +- (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier { + dispatch_barrier_async(self.synchronizationQueue, ^{ + AFCachedImage *cacheImage = [[AFCachedImage alloc] initWithImage:image identifier:identifier]; + + AFCachedImage *previousCachedImage = self.cachedImages[identifier]; + if (previousCachedImage != nil) { + self.currentMemoryUsage -= previousCachedImage.totalBytes; + } + + self.cachedImages[identifier] = cacheImage; + self.currentMemoryUsage += cacheImage.totalBytes; + }); + + dispatch_barrier_async(self.synchronizationQueue, ^{ + if (self.currentMemoryUsage > self.memoryCapacity) { + UInt64 bytesToPurge = self.currentMemoryUsage - self.preferredMemoryUsageAfterPurge; + NSMutableArray *sortedImages = [NSMutableArray arrayWithArray:self.cachedImages.allValues]; + NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastAccessDate" + ascending:YES]; + [sortedImages sortUsingDescriptors:@[sortDescriptor]]; + + UInt64 bytesPurged = 0; + + for (AFCachedImage *cachedImage in sortedImages) { + [self.cachedImages removeObjectForKey:cachedImage.identifier]; + bytesPurged += cachedImage.totalBytes; + if (bytesPurged >= bytesToPurge) { + break ; + } + } + self.currentMemoryUsage -= bytesPurged; + } + }); +} + +- (BOOL)removeImageWithIdentifier:(NSString *)identifier { + __block BOOL removed = NO; + dispatch_barrier_sync(self.synchronizationQueue, ^{ + AFCachedImage *cachedImage = self.cachedImages[identifier]; + if (cachedImage != nil) { + [self.cachedImages removeObjectForKey:identifier]; + self.currentMemoryUsage -= cachedImage.totalBytes; + removed = YES; + } + }); + return removed; +} + +- (BOOL)removeAllImages { + __block BOOL removed = NO; + dispatch_barrier_sync(self.synchronizationQueue, ^{ + if (self.cachedImages.count > 0) { + [self.cachedImages removeAllObjects]; + self.currentMemoryUsage = 0; + removed = YES; + } + }); + return removed; +} + +- (nullable UIImage *)imageWithIdentifier:(NSString *)identifier { + __block UIImage *image = nil; + dispatch_sync(self.synchronizationQueue, ^{ + AFCachedImage *cachedImage = self.cachedImages[identifier]; + image = [cachedImage accessImage]; + }); + return image; +} + +- (void)addImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier { + [self addImage:image withIdentifier:[self imageCacheKeyFromURLRequest:request withAdditionalIdentifier:identifier]]; +} + +- (BOOL)removeImageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier { + return [self removeImageWithIdentifier:[self imageCacheKeyFromURLRequest:request withAdditionalIdentifier:identifier]]; +} + +- (nullable UIImage *)imageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier { + return [self imageWithIdentifier:[self imageCacheKeyFromURLRequest:request withAdditionalIdentifier:identifier]]; +} + +- (NSString *)imageCacheKeyFromURLRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)additionalIdentifier { + NSString *key = request.URL.absoluteString; + if (additionalIdentifier != nil) { + key = [key stringByAppendingString:additionalIdentifier]; + } + return key; +} + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperation.h b/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperation.h deleted file mode 100755 index cf6def4e3..000000000 --- a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperation.h +++ /dev/null @@ -1,70 +0,0 @@ -// AFHTTPRequestOperation.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#import -#import "AFURLConnectionOperation.h" - -NS_ASSUME_NONNULL_BEGIN - -/** - `AFHTTPRequestOperation` is a subclass of `AFURLConnectionOperation` for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request. - */ -@interface AFHTTPRequestOperation : AFURLConnectionOperation - -///------------------------------------------------ -/// @name Getting HTTP URL Connection Information -///------------------------------------------------ - -/** - The last HTTP response received by the operation's connection. - */ -@property (readonly, nonatomic, strong, nullable) NSHTTPURLResponse *response; - -/** - Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an AFHTTPResponse serializer, which uses the raw data as its response object. The serializer validates the status code to be in the `2XX` range, denoting success. If the response serializer generates an error in `-responseObjectForResponse:data:error:`, the `failure` callback of the session task or request operation will be executed; otherwise, the `success` callback will be executed. - - @warning `responseSerializer` must not be `nil`. Setting a response serializer will clear out any cached value - */ -@property (nonatomic, strong) AFHTTPResponseSerializer * responseSerializer; - -/** - An object constructed by the `responseSerializer` from the response and response data. Returns `nil` unless the operation `isFinished`, has a `response`, and has `responseData` with non-zero content length. If an error occurs during serialization, `nil` will be returned, and the `error` property will be populated with the serialization error. - */ -@property (readonly, nonatomic, strong, nullable) id responseObject; - -///----------------------------------------------------------- -/// @name Setting Completion Block Success / Failure Callbacks -///----------------------------------------------------------- - -/** - Sets the `completionBlock` property with a block that executes either the specified success or failure block, depending on the state of the request on completion. If `error` returns a value, which can be caused by an unacceptable status code or content type, then `failure` is executed. Otherwise, `success` is executed. - - This method should be overridden in subclasses in order to specify the response object passed into the success block. - - @param success The block to be executed on the completion of a successful request. This block has no return value and takes two arguments: the receiver operation and the object constructed from the response data of the request. - @param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes two arguments: the receiver operation and the error that occurred during the request. - */ -- (void)setCompletionBlockWithSuccess:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -@end - -NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperation.m b/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperation.m deleted file mode 100755 index b8deda839..000000000 --- a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperation.m +++ /dev/null @@ -1,206 +0,0 @@ -// AFHTTPRequestOperation.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#import "AFHTTPRequestOperation.h" - -static dispatch_queue_t http_request_operation_processing_queue() { - static dispatch_queue_t af_http_request_operation_processing_queue; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - af_http_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.http-request.processing", DISPATCH_QUEUE_CONCURRENT); - }); - - return af_http_request_operation_processing_queue; -} - -static dispatch_group_t http_request_operation_completion_group() { - static dispatch_group_t af_http_request_operation_completion_group; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - af_http_request_operation_completion_group = dispatch_group_create(); - }); - - return af_http_request_operation_completion_group; -} - -#pragma mark - - -@interface AFURLConnectionOperation () -@property (readwrite, nonatomic, strong) NSURLRequest *request; -@property (readwrite, nonatomic, strong) NSURLResponse *response; -@end - -@interface AFHTTPRequestOperation () -@property (readwrite, nonatomic, strong) NSHTTPURLResponse *response; -@property (readwrite, nonatomic, strong) id responseObject; -@property (readwrite, nonatomic, strong) NSError *responseSerializationError; -@property (readwrite, nonatomic, strong) NSRecursiveLock *lock; -@end - -@implementation AFHTTPRequestOperation -@dynamic response; -@dynamic lock; - -- (instancetype)initWithRequest:(NSURLRequest *)urlRequest { - self = [super initWithRequest:urlRequest]; - if (!self) { - return nil; - } - - self.responseSerializer = [AFHTTPResponseSerializer serializer]; - - return self; -} - -- (void)setResponseSerializer:(AFHTTPResponseSerializer *)responseSerializer { - NSParameterAssert(responseSerializer); - - [self.lock lock]; - _responseSerializer = responseSerializer; - self.responseObject = nil; - self.responseSerializationError = nil; - [self.lock unlock]; -} - -- (id)responseObject { - [self.lock lock]; - if (!_responseObject && [self isFinished] && !self.error) { - NSError *error = nil; - self.responseObject = [self.responseSerializer responseObjectForResponse:self.response data:self.responseData error:&error]; - if (error) { - self.responseSerializationError = error; - } - } - [self.lock unlock]; - - return _responseObject; -} - -- (NSError *)error { - if (_responseSerializationError) { - return _responseSerializationError; - } else { - return [super error]; - } -} - -#pragma mark - AFHTTPRequestOperation - -- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - // completionBlock is manually nilled out in AFURLConnectionOperation to break the retain cycle. -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Warc-retain-cycles" -#pragma clang diagnostic ignored "-Wgnu" - self.completionBlock = ^{ - if (self.completionGroup) { - dispatch_group_enter(self.completionGroup); - } - - dispatch_async(http_request_operation_processing_queue(), ^{ - if (self.error) { - if (failure) { - dispatch_group_async(self.completionGroup ?: http_request_operation_completion_group(), self.completionQueue ?: dispatch_get_main_queue(), ^{ - failure(self, self.error); - }); - } - } else { - id responseObject = self.responseObject; - if (self.error) { - if (failure) { - dispatch_group_async(self.completionGroup ?: http_request_operation_completion_group(), self.completionQueue ?: dispatch_get_main_queue(), ^{ - failure(self, self.error); - }); - } - } else { - if (success) { - dispatch_group_async(self.completionGroup ?: http_request_operation_completion_group(), self.completionQueue ?: dispatch_get_main_queue(), ^{ - success(self, responseObject); - }); - } - } - } - - if (self.completionGroup) { - dispatch_group_leave(self.completionGroup); - } - }); - }; -#pragma clang diagnostic pop -} - -#pragma mark - AFURLRequestOperation - -- (void)pause { - [super pause]; - - u_int64_t offset = 0; - if ([self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey]) { - offset = [(NSNumber *)[self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey] unsignedLongLongValue]; - } else { - offset = [(NSData *)[self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey] length]; - } - - NSMutableURLRequest *mutableURLRequest = [self.request mutableCopy]; - if ([self.response respondsToSelector:@selector(allHeaderFields)] && [[self.response allHeaderFields] valueForKey:@"ETag"]) { - [mutableURLRequest setValue:[[self.response allHeaderFields] valueForKey:@"ETag"] forHTTPHeaderField:@"If-Range"]; - } - [mutableURLRequest setValue:[NSString stringWithFormat:@"bytes=%llu-", offset] forHTTPHeaderField:@"Range"]; - self.request = mutableURLRequest; -} - -#pragma mark - NSSecureCoding - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (id)initWithCoder:(NSCoder *)decoder { - self = [super initWithCoder:decoder]; - if (!self) { - return nil; - } - - self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))]; - - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder { - [super encodeWithCoder:coder]; - - [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))]; -} - -#pragma mark - NSCopying - -- (id)copyWithZone:(NSZone *)zone { - AFHTTPRequestOperation *operation = [super copyWithZone:zone]; - - operation.responseSerializer = [self.responseSerializer copyWithZone:zone]; - operation.completionQueue = self.completionQueue; - operation.completionGroup = self.completionGroup; - - return operation; -} - -@end diff --git a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperationManager.h b/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperationManager.h deleted file mode 100755 index d2385edc9..000000000 --- a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperationManager.h +++ /dev/null @@ -1,326 +0,0 @@ -// AFHTTPRequestOperationManager.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#import -#import -#import - -#if __IPHONE_OS_VERSION_MIN_REQUIRED -#import -#else -#import -#endif - -#import "AFHTTPRequestOperation.h" -#import "AFURLResponseSerialization.h" -#import "AFURLRequestSerialization.h" -#import "AFSecurityPolicy.h" -#import "AFNetworkReachabilityManager.h" - -#ifndef NS_DESIGNATED_INITIALIZER -#if __has_attribute(objc_designated_initializer) -#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer)) -#else -#define NS_DESIGNATED_INITIALIZER -#endif -#endif - -NS_ASSUME_NONNULL_BEGIN - -/** - `AFHTTPRequestOperationManager` encapsulates the common patterns of communicating with a web application over HTTP, including request creation, response serialization, network reachability monitoring, and security, as well as request operation management. - - ## Subclassing Notes - - Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass `AFHTTPSessionManager`, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application. - - For developers targeting iOS 6 or Mac OS X 10.8 or earlier, `AFHTTPRequestOperationManager` may be used to similar effect. - - ## Methods to Override - - To change the behavior of all request operation construction for an `AFHTTPRequestOperationManager` subclass, override `HTTPRequestOperationWithRequest:success:failure`. - - ## Serialization - - Requests created by an HTTP client will contain default headers and encode parameters according to the `requestSerializer` property, which is an object conforming to ``. - - Responses received from the server are automatically validated and serialized by the `responseSerializers` property, which is an object conforming to `` - - ## URL Construction Using Relative Paths - - For HTTP convenience methods, the request serializer constructs URLs from the path relative to the `-baseURL`, using `NSURL +URLWithString:relativeToURL:`, when provided. If `baseURL` is `nil`, `path` needs to resolve to a valid `NSURL` object using `NSURL +URLWithString:`. - - Below are a few examples of how `baseURL` and relative paths interact: - - NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"]; - [NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo - [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz - [NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo - [NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo - [NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/ - [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/ - - Also important to note is that a trailing slash will be added to any `baseURL` without one. This would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash. - - ## Network Reachability Monitoring - - Network reachability status and change monitoring is available through the `reachabilityManager` property. Applications may choose to monitor network reachability conditions in order to prevent or suspend any outbound requests. See `AFNetworkReachabilityManager` for more details. - - ## NSSecureCoding & NSCopying Caveats - - `AFHTTPRequestOperationManager` conforms to the `NSSecureCoding` and `NSCopying` protocols, allowing operations to be archived to disk, and copied in memory, respectively. There are a few minor caveats to keep in mind, however: - - - Archives and copies of HTTP clients will be initialized with an empty operation queue. - - NSSecureCoding cannot serialize / deserialize block properties, so an archive of an HTTP client will not include any reachability callback block that may be set. - */ -@interface AFHTTPRequestOperationManager : NSObject - -/** - The URL used to monitor reachability, and construct requests from relative paths in methods like `requestWithMethod:URLString:parameters:`, and the `GET` / `POST` / et al. convenience methods. - */ -@property (readonly, nonatomic, strong, nullable) NSURL *baseURL; - -/** - Requests created with `requestWithMethod:URLString:parameters:` & `multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:` are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of `AFHTTPRequestSerializer`, which serializes query string parameters for `GET`, `HEAD`, and `DELETE` requests, or otherwise URL-form-encodes HTTP message bodies. - - @warning `requestSerializer` must not be `nil`. - */ -@property (nonatomic, strong) AFHTTPRequestSerializer * requestSerializer; - -/** - Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to a JSON serializer, which serializes data from responses with a `application/json` MIME type, and falls back to the raw data object. The serializer validates the status code to be in the `2XX` range, denoting success. If the response serializer generates an error in `-responseObjectForResponse:data:error:`, the `failure` callback of the session task or request operation will be executed; otherwise, the `success` callback will be executed. - - @warning `responseSerializer` must not be `nil`. - */ -@property (nonatomic, strong) AFHTTPResponseSerializer * responseSerializer; - -/** - The operation queue on which request operations are scheduled and run. - */ -@property (nonatomic, strong) NSOperationQueue *operationQueue; - -///------------------------------- -/// @name Managing URL Credentials -///------------------------------- - -/** - Whether request operations should consult the credential storage for authenticating the connection. `YES` by default. - - @see AFURLConnectionOperation -shouldUseCredentialStorage - */ -@property (nonatomic, assign) BOOL shouldUseCredentialStorage; - -/** - The credential used by request operations for authentication challenges. - - @see AFURLConnectionOperation -credential - */ -@property (nonatomic, strong, nullable) NSURLCredential *credential; - -///------------------------------- -/// @name Managing Security Policy -///------------------------------- - -/** - The security policy used by created request operations to evaluate server trust for secure connections. `AFHTTPRequestOperationManager` uses the `defaultPolicy` unless otherwise specified. - */ -@property (nonatomic, strong) AFSecurityPolicy *securityPolicy; - -///------------------------------------ -/// @name Managing Network Reachability -///------------------------------------ - -/** - The network reachability manager. `AFHTTPRequestOperationManager` uses the `sharedManager` by default. - */ -@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager; - -///------------------------------- -/// @name Managing Callback Queues -///------------------------------- - -/** - The dispatch queue for the `completionBlock` of request operations. If `NULL` (default), the main queue is used. - */ -#if OS_OBJECT_HAVE_OBJC_SUPPORT -@property (nonatomic, strong, nullable) dispatch_queue_t completionQueue; -#else -@property (nonatomic, assign, nullable) dispatch_queue_t completionQueue; -#endif - -/** - The dispatch group for the `completionBlock` of request operations. If `NULL` (default), a private dispatch group is used. - */ -#if OS_OBJECT_HAVE_OBJC_SUPPORT -@property (nonatomic, strong, nullable) dispatch_group_t completionGroup; -#else -@property (nonatomic, assign, nullable) dispatch_group_t completionGroup; -#endif - -///--------------------------------------------- -/// @name Creating and Initializing HTTP Clients -///--------------------------------------------- - -/** - Creates and returns an `AFHTTPRequestOperationManager` object. - */ -+ (instancetype)manager; - -/** - Initializes an `AFHTTPRequestOperationManager` object with the specified base URL. - - This is the designated initializer. - - @param url The base URL for the HTTP client. - - @return The newly-initialized HTTP client - */ -- (instancetype)initWithBaseURL:(nullable NSURL *)url NS_DESIGNATED_INITIALIZER; - -///--------------------------------------- -/// @name Managing HTTP Request Operations -///--------------------------------------- - -/** - Creates an `AFHTTPRequestOperation`, and sets the response serializers to that of the HTTP client. - - @param request The request object to be loaded asynchronously during execution of the operation. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred. - */ -- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -///--------------------------- -/// @name Making HTTP Requests -///--------------------------- - -/** - Creates and runs an `AFHTTPRequestOperation` with a `GET` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)GET:(NSString *)URLString - parameters:(nullable id)parameters - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -/** - Creates and runs an `AFHTTPRequestOperation` with a `HEAD` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single arguments: the request operation. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)HEAD:(NSString *)URLString - parameters:(nullable id)parameters - success:(nullable void (^)(AFHTTPRequestOperation *operation))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -/** - Creates and runs an `AFHTTPRequestOperation` with a `POST` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)POST:(NSString *)URLString - parameters:(nullable id)parameters - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -/** - Creates and runs an `AFHTTPRequestOperation` with a multipart `POST` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param block A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the `AFMultipartFormData` protocol. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)POST:(NSString *)URLString - parameters:(nullable id)parameters - constructingBodyWithBlock:(nullable void (^)(id formData))block - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -/** - Creates and runs an `AFHTTPRequestOperation` with a `PUT` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)PUT:(NSString *)URLString - parameters:(nullable id)parameters - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -/** - Creates and runs an `AFHTTPRequestOperation` with a `PATCH` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)PATCH:(NSString *)URLString - parameters:(nullable id)parameters - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -/** - Creates and runs an `AFHTTPRequestOperation` with a `DELETE` request. - - @param URLString The URL string used to create the request URL. - @param parameters The parameters to be encoded according to the client request serializer. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the response object created by the client response serializer. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the request operation and the error describing the network or parsing error that occurred. - - @see -HTTPRequestOperationWithRequest:success:failure: - */ -- (nullable AFHTTPRequestOperation *)DELETE:(NSString *)URLString - parameters:(nullable id)parameters - success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; - -@end - -NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperationManager.m b/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperationManager.m deleted file mode 100755 index 60739e5f0..000000000 --- a/clients/ios/Other Sources/AFNetworking/AFHTTPRequestOperationManager.m +++ /dev/null @@ -1,284 +0,0 @@ -// AFHTTPRequestOperationManager.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#import - -#import "AFHTTPRequestOperationManager.h" -#import "AFHTTPRequestOperation.h" - -#import -#import - -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) -#import -#endif - -@interface AFHTTPRequestOperationManager () -@property (readwrite, nonatomic, strong) NSURL *baseURL; -@end - -@implementation AFHTTPRequestOperationManager - -+ (instancetype)manager { - return [[self alloc] initWithBaseURL:nil]; -} - -- (instancetype)init { - return [self initWithBaseURL:nil]; -} - -- (instancetype)initWithBaseURL:(NSURL *)url { - self = [super init]; - if (!self) { - return nil; - } - - // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected - if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) { - url = [url URLByAppendingPathComponent:@""]; - } - - self.baseURL = url; - - self.requestSerializer = [AFHTTPRequestSerializer serializer]; - self.responseSerializer = [AFJSONResponseSerializer serializer]; - - self.securityPolicy = [AFSecurityPolicy defaultPolicy]; - - self.reachabilityManager = [AFNetworkReachabilityManager sharedManager]; - - self.operationQueue = [[NSOperationQueue alloc] init]; - - self.shouldUseCredentialStorage = YES; - - return self; -} - -#pragma mark - - -#ifdef _SYSTEMCONFIGURATION_H -#endif - -- (void)setRequestSerializer:(AFHTTPRequestSerializer *)requestSerializer { - NSParameterAssert(requestSerializer); - - _requestSerializer = requestSerializer; -} - -- (void)setResponseSerializer:(AFHTTPResponseSerializer *)responseSerializer { - NSParameterAssert(responseSerializer); - - _responseSerializer = responseSerializer; -} - -#pragma mark - - -- (AFHTTPRequestOperation *)HTTPRequestOperationWithHTTPMethod:(NSString *)method - URLString:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - NSError *serializationError = nil; - NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError]; - if (serializationError) { - if (failure) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" - dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{ - failure(nil, serializationError); - }); -#pragma clang diagnostic pop - } - - return nil; - } - - return [self HTTPRequestOperationWithRequest:request success:success failure:failure]; -} - -- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; - operation.responseSerializer = self.responseSerializer; - operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage; - operation.credential = self.credential; - operation.securityPolicy = self.securityPolicy; - - [operation setCompletionBlockWithSuccess:success failure:failure]; - operation.completionQueue = self.completionQueue; - operation.completionGroup = self.completionGroup; - - return operation; -} - -#pragma mark - - -- (AFHTTPRequestOperation *)GET:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -- (AFHTTPRequestOperation *)HEAD:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) { - if (success) { - success(requestOperation); - } - } failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -- (AFHTTPRequestOperation *)POST:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -- (AFHTTPRequestOperation *)POST:(NSString *)URLString - parameters:(id)parameters - constructingBodyWithBlock:(void (^)(id formData))block - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - NSError *serializationError = nil; - NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError]; - if (serializationError) { - if (failure) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" - dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{ - failure(nil, serializationError); - }); -#pragma clang diagnostic pop - } - - return nil; - } - - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -- (AFHTTPRequestOperation *)PUT:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -- (AFHTTPRequestOperation *)PATCH:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters success:success failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -- (AFHTTPRequestOperation *)DELETE:(NSString *)URLString - parameters:(id)parameters - success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure -{ - AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters success:success failure:failure]; - - [self.operationQueue addOperation:operation]; - - return operation; -} - -#pragma mark - NSObject - -- (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.operationQueue]; -} - -#pragma mark - NSSecureCoding - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (id)initWithCoder:(NSCoder *)decoder { - NSURL *baseURL = [decoder decodeObjectForKey:NSStringFromSelector(@selector(baseURL))]; - - self = [self initWithBaseURL:baseURL]; - if (!self) { - return nil; - } - - self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))]; - self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))]; - - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder { - [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))]; - [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))]; - [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))]; -} - -#pragma mark - NSCopying - -- (id)copyWithZone:(NSZone *)zone { - AFHTTPRequestOperationManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL]; - - HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone]; - HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone]; - - return HTTPClient; -} - -@end diff --git a/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.h b/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.h index e516e6d66..64ee5a814 100755 --- a/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.h +++ b/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.h @@ -1,5 +1,5 @@ // AFHTTPSessionManager.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,9 +23,9 @@ #if !TARGET_OS_WATCH #import #endif -#import +#import -#if __IPHONE_OS_VERSION_MIN_REQUIRED +#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV #import #else #import @@ -33,14 +33,6 @@ #import "AFURLSessionManager.h" -#ifndef NS_DESIGNATED_INITIALIZER -#if __has_attribute(objc_designated_initializer) -#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer)) -#else -#define NS_DESIGNATED_INITIALIZER -#endif -#endif - /** `AFHTTPSessionManager` is a subclass of `AFURLSessionManager` with convenience methods for making HTTP requests. When a `baseURL` is provided, requests made with the `GET` / `POST` / et al. convenience methods can be made with relative paths. @@ -52,7 +44,7 @@ ## Methods to Override - To change the behavior of all data task operation construction, which is also used in the `GET` / `POST` / et al. convenience methods, override `dataTaskWithRequest:completionHandler:`. + To change the behavior of all data task operation construction, which is also used in the `GET` / `POST` / et al. convenience methods, override `dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:`. ## Serialization @@ -79,8 +71,6 @@ @warning Managers for background sessions must be owned for the duration of their use. This can be accomplished by creating an application-wide or shared singleton instance. */ -#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || TARGET_OS_WATCH - NS_ASSUME_NONNULL_BEGIN @interface AFHTTPSessionManager : AFURLSessionManager @@ -104,6 +94,15 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, strong) AFHTTPResponseSerializer * responseSerializer; +///------------------------------- +/// @name Managing Security Policy +///------------------------------- + +/** + The security policy used by created session to evaluate server trust for secure connections. `AFURLSessionManager` uses the `defaultPolicy` unless otherwise specified. A security policy configured with `AFSSLPinningModePublicKey` or `AFSSLPinningModeCertificate` can only be applied on a session manager initialized with a secure base URL (i.e. https). Applying a security policy with pinning enabled on an insecure session manager throws an `Invalid Security Policy` exception. + */ +@property (nonatomic, strong) AFSecurityPolicy *securityPolicy; + ///--------------------- /// @name Initialization ///--------------------- @@ -151,8 +150,26 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(nullable id)parameters - success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE; + + +/** + Creates and runs an `NSURLSessionDataTask` with a `GET` request. + + @param URLString The URL string used to create the request URL. + @param parameters The parameters to be encoded according to the client request serializer. + @param downloadProgress A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue. + @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer. + @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred. + + @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler: + */ +- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString + parameters:(nullable id)parameters + progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /** Creates and runs an `NSURLSessionDataTask` with a `HEAD` request. @@ -167,7 +184,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(nullable id)parameters success:(nullable void (^)(NSURLSessionDataTask *task))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /** Creates and runs an `NSURLSessionDataTask` with a `POST` request. @@ -181,8 +198,25 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(nullable id)parameters - success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE; + +/** + Creates and runs an `NSURLSessionDataTask` with a `POST` request. + + @param URLString The URL string used to create the request URL. + @param parameters The parameters to be encoded according to the client request serializer. + @param uploadProgress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue. + @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer. + @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred. + + @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler: + */ +- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString + parameters:(nullable id)parameters + progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /** Creates and runs an `NSURLSessionDataTask` with a multipart `POST` request. @@ -198,8 +232,27 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(nullable id)parameters constructingBodyWithBlock:(nullable void (^)(id formData))block - success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE; + +/** + Creates and runs an `NSURLSessionDataTask` with a multipart `POST` request. + + @param URLString The URL string used to create the request URL. + @param parameters The parameters to be encoded according to the client request serializer. + @param block A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the `AFMultipartFormData` protocol. + @param uploadProgress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue. + @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer. + @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred. + + @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler: + */ +- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString + parameters:(nullable id)parameters + constructingBodyWithBlock:(nullable void (^)(id formData))block + progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /** Creates and runs an `NSURLSessionDataTask` with a `PUT` request. @@ -213,8 +266,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(nullable id)parameters - success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /** Creates and runs an `NSURLSessionDataTask` with a `PATCH` request. @@ -228,8 +281,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(nullable id)parameters - success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /** Creates and runs an `NSURLSessionDataTask` with a `DELETE` request. @@ -243,11 +296,9 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(nullable id)parameters - success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure; + success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; @end NS_ASSUME_NONNULL_END - -#endif diff --git a/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.m b/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.m index bd9163faa..cab11c28f 100755 --- a/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.m +++ b/clients/ios/Other Sources/AFNetworking/AFHTTPSessionManager.m @@ -1,5 +1,5 @@ // AFHTTPSessionManager.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,23 +21,20 @@ #import "AFHTTPSessionManager.h" -#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || TARGET_WATCH_OS - #import "AFURLRequestSerialization.h" #import "AFURLResponseSerialization.h" #import +#import #import -#ifdef _SYSTEMCONFIGURATION_H #import #import #import #import #import -#endif -#if TARGET_OS_IOS +#if TARGET_OS_IOS || TARGET_OS_TV #import #elif TARGET_OS_WATCH #import @@ -89,9 +86,6 @@ #pragma mark - -#ifdef _SYSTEMCONFIGURATION_H -#endif - - (void)setRequestSerializer:(AFHTTPRequestSerializer *)requestSerializer { NSParameterAssert(requestSerializer); @@ -104,6 +98,23 @@ [super setResponseSerializer:responseSerializer]; } +@dynamic securityPolicy; + +- (void)setSecurityPolicy:(AFSecurityPolicy *)securityPolicy { + if (securityPolicy.SSLPinningMode != AFSSLPinningModeNone && ![self.baseURL.scheme isEqualToString:@"https"]) { + NSString *pinningMode = @"Unknown Pinning Mode"; + switch (securityPolicy.SSLPinningMode) { + case AFSSLPinningModeNone: pinningMode = @"AFSSLPinningModeNone"; break; + case AFSSLPinningModeCertificate: pinningMode = @"AFSSLPinningModeCertificate"; break; + case AFSSLPinningModePublicKey: pinningMode = @"AFSSLPinningModePublicKey"; break; + } + NSString *reason = [NSString stringWithFormat:@"A security policy configured with `%@` can only be applied on a manager with a secure base URL (i.e. https)", pinningMode]; + @throw [NSException exceptionWithName:@"Invalid Security Policy" reason:reason userInfo:nil]; + } + + [super setSecurityPolicy:securityPolicy]; +} + #pragma mark - - (NSURLSessionDataTask *)GET:(NSString *)URLString @@ -111,7 +122,24 @@ success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure]; + + return [self GET:URLString parameters:parameters progress:nil success:success failure:failure]; +} + +- (NSURLSessionDataTask *)GET:(NSString *)URLString + parameters:(id)parameters + progress:(void (^)(NSProgress * _Nonnull))downloadProgress + success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success + failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure +{ + + NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET" + URLString:URLString + parameters:parameters + uploadProgress:nil + downloadProgress:downloadProgress + success:success + failure:failure]; [dataTask resume]; @@ -123,7 +151,7 @@ success:(void (^)(NSURLSessionDataTask *task))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters success:^(NSURLSessionDataTask *task, __unused id responseObject) { + NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"HEAD" URLString:URLString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, __unused id responseObject) { if (success) { success(task); } @@ -139,16 +167,35 @@ success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters success:success failure:failure]; + return [self POST:URLString parameters:parameters progress:nil success:success failure:failure]; +} + +- (NSURLSessionDataTask *)POST:(NSString *)URLString + parameters:(id)parameters + progress:(void (^)(NSProgress * _Nonnull))uploadProgress + success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success + failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure +{ + NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure]; [dataTask resume]; return dataTask; } +- (NSURLSessionDataTask *)POST:(NSString *)URLString + parameters:(nullable id)parameters + constructingBodyWithBlock:(nullable void (^)(id _Nonnull))block + success:(nullable void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success + failure:(nullable void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure +{ + return [self POST:URLString parameters:parameters constructingBodyWithBlock:block progress:nil success:success failure:failure]; +} + - (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id formData))block + progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { @@ -156,18 +203,15 @@ NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError]; if (serializationError) { if (failure) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{ failure(nil, serializationError); }); -#pragma clang diagnostic pop } return nil; } - __block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { + __block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:uploadProgress completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { if (error) { if (failure) { failure(task, error); @@ -189,7 +233,7 @@ success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure]; + NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters uploadProgress:nil downloadProgress:nil success:success failure:failure]; [dataTask resume]; @@ -201,7 +245,7 @@ success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters success:success failure:failure]; + NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PATCH" URLString:URLString parameters:parameters uploadProgress:nil downloadProgress:nil success:success failure:failure]; [dataTask resume]; @@ -213,7 +257,7 @@ success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters success:success failure:failure]; + NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"DELETE" URLString:URLString parameters:parameters uploadProgress:nil downloadProgress:nil success:success failure:failure]; [dataTask resume]; @@ -223,6 +267,8 @@ - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters + uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress + downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { @@ -230,19 +276,19 @@ NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError]; if (serializationError) { if (failure) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{ failure(nil, serializationError); }); -#pragma clang diagnostic pop } return nil; } __block NSURLSessionDataTask *dataTask = nil; - dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { + dataTask = [self dataTaskWithRequest:request + uploadProgress:uploadProgress + downloadProgress:downloadProgress + completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { if (error) { if (failure) { failure(dataTask, error); @@ -269,7 +315,7 @@ return YES; } -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { NSURL *baseURL = [decoder decodeObjectOfClass:[NSURL class] forKey:NSStringFromSelector(@selector(baseURL))]; NSURLSessionConfiguration *configuration = [decoder decodeObjectOfClass:[NSURLSessionConfiguration class] forKey:@"sessionConfiguration"]; if (!configuration) { @@ -290,6 +336,10 @@ self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))]; self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))]; + AFSecurityPolicy *decodedPolicy = [decoder decodeObjectOfClass:[AFSecurityPolicy class] forKey:NSStringFromSelector(@selector(securityPolicy))]; + if (decodedPolicy) { + self.securityPolicy = decodedPolicy; + } return self; } @@ -305,19 +355,18 @@ } [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))]; [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))]; + [coder encodeObject:self.securityPolicy forKey:NSStringFromSelector(@selector(securityPolicy))]; } #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFHTTPSessionManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL sessionConfiguration:self.session.configuration]; HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone]; HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone]; - + HTTPClient.securityPolicy = [self.securityPolicy copyWithZone:zone]; return HTTPClient; } @end - -#endif diff --git a/clients/ios/Other Sources/AFNetworking/AFImageDownloader.h b/clients/ios/Other Sources/AFNetworking/AFImageDownloader.h new file mode 100755 index 000000000..3903eec23 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/AFImageDownloader.h @@ -0,0 +1,157 @@ +// AFImageDownloader.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import +#import "AFAutoPurgingImageCache.h" +#import "AFHTTPSessionManager.h" + +NS_ASSUME_NONNULL_BEGIN + +typedef NS_ENUM(NSInteger, AFImageDownloadPrioritization) { + AFImageDownloadPrioritizationFIFO, + AFImageDownloadPrioritizationLIFO +}; + +/** + The `AFImageDownloadReceipt` is an object vended by the `AFImageDownloader` when starting a data task. It can be used to cancel active tasks running on the `AFImageDownloader` session. As a general rule, image data tasks should be cancelled using the `AFImageDownloadReceipt` instead of calling `cancel` directly on the `task` itself. The `AFImageDownloader` is optimized to handle duplicate task scenarios as well as pending versus active downloads. + */ +@interface AFImageDownloadReceipt : NSObject + +/** + The data task created by the `AFImageDownloader`. +*/ +@property (nonatomic, strong) NSURLSessionDataTask *task; + +/** + The unique identifier for the success and failure blocks when duplicate requests are made. + */ +@property (nonatomic, strong) NSUUID *receiptID; +@end + +/** The `AFImageDownloader` class is responsible for downloading images in parallel on a prioritized queue. Incoming downloads are added to the front or back of the queue depending on the download prioritization. Each downloaded image is cached in the underlying `NSURLCache` as well as the in-memory image cache. By default, any download request with a cached image equivalent in the image cache will automatically be served the cached image representation. + */ +@interface AFImageDownloader : NSObject + +/** + The image cache used to store all downloaded images in. `AFAutoPurgingImageCache` by default. + */ +@property (nonatomic, strong, nullable) id imageCache; + +/** + The `AFHTTPSessionManager` used to download images. By default, this is configured with an `AFImageResponseSerializer`, and a shared `NSURLCache` for all image downloads. + */ +@property (nonatomic, strong) AFHTTPSessionManager *sessionManager; + +/** + Defines the order prioritization of incoming download requests being inserted into the queue. `AFImageDownloadPrioritizationFIFO` by default. + */ +@property (nonatomic, assign) AFImageDownloadPrioritization downloadPrioritizaton; + +/** + The shared default instance of `AFImageDownloader` initialized with default values. + */ ++ (instancetype)defaultInstance; + +/** + Creates a default `NSURLCache` with common usage parameter values. + + @returns The default `NSURLCache` instance. + */ ++ (NSURLCache *)defaultURLCache; + +/** + Default initializer + + @return An instance of `AFImageDownloader` initialized with default values. + */ +- (instancetype)init; + +/** + Initializes the `AFImageDownloader` instance with the given session manager, download prioritization, maximum active download count and image cache. + + @param sessionManager The session manager to use to download images. + @param downloadPrioritization The download prioritization of the download queue. + @param maximumActiveDownloads The maximum number of active downloads allowed at any given time. Recommend `4`. + @param imageCache The image cache used to store all downloaded images in. + + @return The new `AFImageDownloader` instance. + */ +- (instancetype)initWithSessionManager:(AFHTTPSessionManager *)sessionManager + downloadPrioritization:(AFImageDownloadPrioritization)downloadPrioritization + maximumActiveDownloads:(NSInteger)maximumActiveDownloads + imageCache:(nullable id )imageCache; + +/** + Creates a data task using the `sessionManager` instance for the specified URL request. + + If the same data task is already in the queue or currently being downloaded, the success and failure blocks are + appended to the already existing task. Once the task completes, all success or failure blocks attached to the + task are executed in the order they were added. + + @param request The URL request. + @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. + @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + + @return The image download receipt for the data task if available. `nil` if the image is stored in the cache. + cache and the URL request cache policy allows the cache to be used. + */ +- (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure; + +/** + Creates a data task using the `sessionManager` instance for the specified URL request. + + If the same data task is already in the queue or currently being downloaded, the success and failure blocks are + appended to the already existing task. Once the task completes, all success or failure blocks attached to the + task are executed in the order they were added. + + @param request The URL request. + @param receiptID The identifier to use for the download receipt that will be created for this request. This must be a unique identifier that does not represent any other request. + @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. + @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + + @return The image download receipt for the data task if available. `nil` if the image is stored in the cache. + cache and the URL request cache policy allows the cache to be used. + */ +- (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request + withReceiptID:(NSUUID *)receiptID + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure; + +/** + Cancels the data task in the receipt by removing the corresponding success and failure blocks and cancelling the data task if necessary. + + If the data task is pending in the queue, it will be cancelled if no other success and failure blocks are registered with the data task. If the data task is currently executing or is already completed, the success and failure blocks are removed and will not be called when the task finishes. + + @param imageDownloadReceipt The image download receipt to cancel. + */ +- (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt; + +@end + +#endif + +NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFImageDownloader.m b/clients/ios/Other Sources/AFNetworking/AFImageDownloader.m new file mode 100755 index 000000000..5bb28ef13 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/AFImageDownloader.m @@ -0,0 +1,403 @@ +// AFImageDownloader.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import "AFImageDownloader.h" +#import "AFHTTPSessionManager.h" + +@interface AFImageDownloaderResponseHandler : NSObject +@property (nonatomic, strong) NSUUID *uuid; +@property (nonatomic, copy) void (^successBlock)(NSURLRequest*, NSHTTPURLResponse*, UIImage*); +@property (nonatomic, copy) void (^failureBlock)(NSURLRequest*, NSHTTPURLResponse*, NSError*); +@end + +@implementation AFImageDownloaderResponseHandler + +- (instancetype)initWithUUID:(NSUUID *)uuid + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure { + if (self = [self init]) { + self.uuid = uuid; + self.successBlock = success; + self.failureBlock = failure; + } + return self; +} + +- (NSString *)description { + return [NSString stringWithFormat: @"UUID: %@", [self.uuid UUIDString]]; +} + +@end + +@interface AFImageDownloaderMergedTask : NSObject +@property (nonatomic, strong) NSString *URLIdentifier; +@property (nonatomic, strong) NSUUID *identifier; +@property (nonatomic, strong) NSURLSessionDataTask *task; +@property (nonatomic, strong) NSMutableArray *responseHandlers; + +@end + +@implementation AFImageDownloaderMergedTask + +- (instancetype)initWithURLIdentifier:(NSString *)URLIdentifier identifier:(NSUUID *)identifier task:(NSURLSessionDataTask *)task { + if (self = [self init]) { + self.URLIdentifier = URLIdentifier; + self.task = task; + self.identifier = identifier; + self.responseHandlers = [[NSMutableArray alloc] init]; + } + return self; +} + +- (void)addResponseHandler:(AFImageDownloaderResponseHandler*)handler { + [self.responseHandlers addObject:handler]; +} + +- (void)removeResponseHandler:(AFImageDownloaderResponseHandler*)handler { + [self.responseHandlers removeObject:handler]; +} + +@end + +@implementation AFImageDownloadReceipt + +- (instancetype)initWithReceiptID:(NSUUID *)receiptID task:(NSURLSessionDataTask *)task { + if (self = [self init]) { + self.receiptID = receiptID; + self.task = task; + } + return self; +} + +@end + +@interface AFImageDownloader () + +@property (nonatomic, strong) dispatch_queue_t synchronizationQueue; +@property (nonatomic, strong) dispatch_queue_t responseQueue; + +@property (nonatomic, assign) NSInteger maximumActiveDownloads; +@property (nonatomic, assign) NSInteger activeRequestCount; + +@property (nonatomic, strong) NSMutableArray *queuedMergedTasks; +@property (nonatomic, strong) NSMutableDictionary *mergedTasks; + +@end + + +@implementation AFImageDownloader + ++ (NSURLCache *)defaultURLCache { + // It's been discovered that a crash will occur on certain versions + // of iOS if you customize the cache. + // + // More info can be found here: https://devforums.apple.com/message/1102182#1102182 + // + // When iOS 7 support is dropped, this should be modified to use + // NSProcessInfo methods instead. + if ([[[UIDevice currentDevice] systemVersion] compare:@"8.2" options:NSNumericSearch] == NSOrderedAscending) { + return [NSURLCache sharedURLCache]; + } + return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024 + diskCapacity:150 * 1024 * 1024 + diskPath:@"com.alamofire.imagedownloader"]; +} + ++ (NSURLSessionConfiguration *)defaultURLSessionConfiguration { + NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; + + //TODO set the default HTTP headers + + configuration.HTTPShouldSetCookies = YES; + configuration.HTTPShouldUsePipelining = NO; + + configuration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy; + configuration.allowsCellularAccess = YES; + configuration.timeoutIntervalForRequest = 60.0; + configuration.URLCache = [AFImageDownloader defaultURLCache]; + + return configuration; +} + +- (instancetype)init { + NSURLSessionConfiguration *defaultConfiguration = [self.class defaultURLSessionConfiguration]; + AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:defaultConfiguration]; + sessionManager.responseSerializer = [AFImageResponseSerializer serializer]; + + return [self initWithSessionManager:sessionManager + downloadPrioritization:AFImageDownloadPrioritizationFIFO + maximumActiveDownloads:4 + imageCache:[[AFAutoPurgingImageCache alloc] init]]; +} + +- (instancetype)initWithSessionManager:(AFHTTPSessionManager *)sessionManager + downloadPrioritization:(AFImageDownloadPrioritization)downloadPrioritization + maximumActiveDownloads:(NSInteger)maximumActiveDownloads + imageCache:(id )imageCache { + if (self = [super init]) { + self.sessionManager = sessionManager; + + self.downloadPrioritizaton = downloadPrioritization; + self.maximumActiveDownloads = maximumActiveDownloads; + self.imageCache = imageCache; + + self.queuedMergedTasks = [[NSMutableArray alloc] init]; + self.mergedTasks = [[NSMutableDictionary alloc] init]; + self.activeRequestCount = 0; + + NSString *name = [NSString stringWithFormat:@"com.alamofire.imagedownloader.synchronizationqueue-%@", [[NSUUID UUID] UUIDString]]; + self.synchronizationQueue = dispatch_queue_create([name cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL); + + name = [NSString stringWithFormat:@"com.alamofire.imagedownloader.responsequeue-%@", [[NSUUID UUID] UUIDString]]; + self.responseQueue = dispatch_queue_create([name cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_CONCURRENT); + } + + return self; +} + ++ (instancetype)defaultInstance { + static AFImageDownloader *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [[self alloc] init]; + }); + return sharedInstance; +} + +- (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request + success:(void (^)(NSURLRequest * _Nonnull, NSHTTPURLResponse * _Nullable, UIImage * _Nonnull))success + failure:(void (^)(NSURLRequest * _Nonnull, NSHTTPURLResponse * _Nullable, NSError * _Nonnull))failure { + return [self downloadImageForURLRequest:request withReceiptID:[NSUUID UUID] success:success failure:failure]; +} + +- (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request + withReceiptID:(nonnull NSUUID *)receiptID + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure { + __block NSURLSessionDataTask *task = nil; + dispatch_sync(self.synchronizationQueue, ^{ + NSString *URLIdentifier = request.URL.absoluteString; + if (URLIdentifier == nil) { + if (failure) { + NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + failure(request, nil, error); + }); + } + return; + } + + // 1) Append the success and failure blocks to a pre-existing request if it already exists + AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier]; + if (existingMergedTask != nil) { + AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID success:success failure:failure]; + [existingMergedTask addResponseHandler:handler]; + task = existingMergedTask.task; + return; + } + + // 2) Attempt to load the image from the image cache if the cache policy allows it + switch (request.cachePolicy) { + case NSURLRequestUseProtocolCachePolicy: + case NSURLRequestReturnCacheDataElseLoad: + case NSURLRequestReturnCacheDataDontLoad: { + UIImage *cachedImage = [self.imageCache imageforRequest:request withAdditionalIdentifier:nil]; + if (cachedImage != nil) { + if (success) { + dispatch_async(dispatch_get_main_queue(), ^{ + success(request, nil, cachedImage); + }); + } + return; + } + break; + } + default: + break; + } + + // 3) Create the request and set up authentication, validation and response serialization + NSUUID *mergedTaskIdentifier = [NSUUID UUID]; + NSURLSessionDataTask *createdTask; + __weak __typeof__(self) weakSelf = self; + + createdTask = [self.sessionManager + dataTaskWithRequest:request + uploadProgress:nil + downloadProgress:nil + completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { + dispatch_async(self.responseQueue, ^{ + __strong __typeof__(weakSelf) strongSelf = weakSelf; + AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier]; + if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) { + mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier]; + if (error) { + for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) { + if (handler.failureBlock) { + dispatch_async(dispatch_get_main_queue(), ^{ + handler.failureBlock(request, (NSHTTPURLResponse*)response, error); + }); + } + } + } else { + [strongSelf.imageCache addImage:responseObject forRequest:request withAdditionalIdentifier:nil]; + + for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) { + if (handler.successBlock) { + dispatch_async(dispatch_get_main_queue(), ^{ + handler.successBlock(request, (NSHTTPURLResponse*)response, responseObject); + }); + } + } + + } + } + [strongSelf safelyDecrementActiveTaskCount]; + [strongSelf safelyStartNextTaskIfNecessary]; + }); + }]; + + // 4) Store the response handler for use when the request completes + AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID + success:success + failure:failure]; + AFImageDownloaderMergedTask *mergedTask = [[AFImageDownloaderMergedTask alloc] + initWithURLIdentifier:URLIdentifier + identifier:mergedTaskIdentifier + task:createdTask]; + [mergedTask addResponseHandler:handler]; + self.mergedTasks[URLIdentifier] = mergedTask; + + // 5) Either start the request or enqueue it depending on the current active request count + if ([self isActiveRequestCountBelowMaximumLimit]) { + [self startMergedTask:mergedTask]; + } else { + [self enqueueMergedTask:mergedTask]; + } + + task = mergedTask.task; + }); + if (task) { + return [[AFImageDownloadReceipt alloc] initWithReceiptID:receiptID task:task]; + } else { + return nil; + } +} + +- (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt { + dispatch_sync(self.synchronizationQueue, ^{ + NSString *URLIdentifier = imageDownloadReceipt.task.originalRequest.URL.absoluteString; + AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier]; + NSUInteger index = [mergedTask.responseHandlers indexOfObjectPassingTest:^BOOL(AFImageDownloaderResponseHandler * _Nonnull handler, __unused NSUInteger idx, __unused BOOL * _Nonnull stop) { + return handler.uuid == imageDownloadReceipt.receiptID; + }]; + + if (index != NSNotFound) { + AFImageDownloaderResponseHandler *handler = mergedTask.responseHandlers[index]; + [mergedTask removeResponseHandler:handler]; + NSString *failureReason = [NSString stringWithFormat:@"ImageDownloader cancelled URL request: %@",imageDownloadReceipt.task.originalRequest.URL.absoluteString]; + NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey:failureReason}; + NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]; + if (handler.failureBlock) { + dispatch_async(dispatch_get_main_queue(), ^{ + handler.failureBlock(imageDownloadReceipt.task.originalRequest, nil, error); + }); + } + } + + if (mergedTask.responseHandlers.count == 0 && mergedTask.task.state == NSURLSessionTaskStateSuspended) { + [mergedTask.task cancel]; + [self removeMergedTaskWithURLIdentifier:URLIdentifier]; + } + }); +} + +- (AFImageDownloaderMergedTask*)safelyRemoveMergedTaskWithURLIdentifier:(NSString *)URLIdentifier { + __block AFImageDownloaderMergedTask *mergedTask = nil; + dispatch_sync(self.synchronizationQueue, ^{ + mergedTask = [self removeMergedTaskWithURLIdentifier:URLIdentifier]; + }); + return mergedTask; +} + +//This method should only be called from safely within the synchronizationQueue +- (AFImageDownloaderMergedTask *)removeMergedTaskWithURLIdentifier:(NSString *)URLIdentifier { + AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier]; + [self.mergedTasks removeObjectForKey:URLIdentifier]; + return mergedTask; +} + +- (void)safelyDecrementActiveTaskCount { + dispatch_sync(self.synchronizationQueue, ^{ + if (self.activeRequestCount > 0) { + self.activeRequestCount -= 1; + } + }); +} + +- (void)safelyStartNextTaskIfNecessary { + dispatch_sync(self.synchronizationQueue, ^{ + if ([self isActiveRequestCountBelowMaximumLimit]) { + while (self.queuedMergedTasks.count > 0) { + AFImageDownloaderMergedTask *mergedTask = [self dequeueMergedTask]; + if (mergedTask.task.state == NSURLSessionTaskStateSuspended) { + [self startMergedTask:mergedTask]; + break; + } + } + } + }); +} + +- (void)startMergedTask:(AFImageDownloaderMergedTask *)mergedTask { + [mergedTask.task resume]; + ++self.activeRequestCount; +} + +- (void)enqueueMergedTask:(AFImageDownloaderMergedTask *)mergedTask { + switch (self.downloadPrioritizaton) { + case AFImageDownloadPrioritizationFIFO: + [self.queuedMergedTasks addObject:mergedTask]; + break; + case AFImageDownloadPrioritizationLIFO: + [self.queuedMergedTasks insertObject:mergedTask atIndex:0]; + break; + } +} + +- (AFImageDownloaderMergedTask *)dequeueMergedTask { + AFImageDownloaderMergedTask *mergedTask = nil; + mergedTask = [self.queuedMergedTasks firstObject]; + [self.queuedMergedTasks removeObject:mergedTask]; + return mergedTask; +} + +- (BOOL)isActiveRequestCountBelowMaximumLimit { + return self.activeRequestCount < self.maximumActiveDownloads; +} + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/AFNetworkActivityIndicatorManager.h b/clients/ios/Other Sources/AFNetworking/AFNetworkActivityIndicatorManager.h new file mode 100755 index 000000000..3bcf28956 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/AFNetworkActivityIndicatorManager.h @@ -0,0 +1,103 @@ +// AFNetworkActivityIndicatorManager.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import + +#if TARGET_OS_IOS + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + `AFNetworkActivityIndicatorManager` manages the state of the network activity indicator in the status bar. When enabled, it will listen for notifications indicating that a session task has started or finished, and start or stop animating the indicator accordingly. The number of active requests is incremented and decremented much like a stack or a semaphore, and the activity indicator will animate so long as that number is greater than zero. + + You should enable the shared instance of `AFNetworkActivityIndicatorManager` when your application finishes launching. In `AppDelegate application:didFinishLaunchingWithOptions:` you can do so with the following code: + + [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; + + By setting `enabled` to `YES` for `sharedManager`, the network activity indicator will show and hide automatically as requests start and finish. You should not ever need to call `incrementActivityCount` or `decrementActivityCount` yourself. + + See the Apple Human Interface Guidelines section about the Network Activity Indicator for more information: + http://developer.apple.com/library/iOS/#documentation/UserExperience/Conceptual/MobileHIG/UIElementGuidelines/UIElementGuidelines.html#//apple_ref/doc/uid/TP40006556-CH13-SW44 + */ +NS_EXTENSION_UNAVAILABLE_IOS("Use view controller based solutions where appropriate instead.") +@interface AFNetworkActivityIndicatorManager : NSObject + +/** + A Boolean value indicating whether the manager is enabled. + + If YES, the manager will change status bar network activity indicator according to network operation notifications it receives. The default value is NO. + */ +@property (nonatomic, assign, getter = isEnabled) BOOL enabled; + +/** + A Boolean value indicating whether the network activity indicator manager is currently active. +*/ +@property (readonly, nonatomic, assign, getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible; + +/** + A time interval indicating the minimum duration of networking activity that should occur before the activity indicator is displayed. The default value 1 second. If the network activity indicator should be displayed immediately when network activity occurs, this value should be set to 0 seconds. + + Apple's HIG describes the following: + + > Display the network activity indicator to provide feedback when your app accesses the network for more than a couple of seconds. If the operation finishes sooner than that, you don’t have to show the network activity indicator, because the indicator is likely to disappear before users notice its presence. + + */ +@property (nonatomic, assign) NSTimeInterval activationDelay; + +/** + A time interval indicating the duration of time of no networking activity required before the activity indicator is disabled. This allows for continuous display of the network activity indicator across multiple requests. The default value is 0.17 seconds. + */ + +@property (nonatomic, assign) NSTimeInterval completionDelay; + +/** + Returns the shared network activity indicator manager object for the system. + + @return The systemwide network activity indicator manager. + */ ++ (instancetype)sharedManager; + +/** + Increments the number of active network requests. If this number was zero before incrementing, this will start animating the status bar network activity indicator. + */ +- (void)incrementActivityCount; + +/** + Decrements the number of active network requests. If this number becomes zero after decrementing, this will stop animating the status bar network activity indicator. + */ +- (void)decrementActivityCount; + +/** + Set the a custom method to be executed when the network activity indicator manager should be hidden/shown. By default, this is null, and the UIApplication Network Activity Indicator will be managed automatically. If this block is set, it is the responsiblity of the caller to manager the network activity indicator going forward. + + @param block A block to be executed when the network activity indicator status changes. + */ +- (void)setNetworkingActivityActionWithBlock:(nullable void (^)(BOOL networkActivityIndicatorVisible))block; + +@end + +NS_ASSUME_NONNULL_END + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/AFNetworkActivityIndicatorManager.m b/clients/ios/Other Sources/AFNetworking/AFNetworkActivityIndicatorManager.m new file mode 100755 index 000000000..e6f9b65ea --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/AFNetworkActivityIndicatorManager.m @@ -0,0 +1,259 @@ +// AFNetworkActivityIndicatorManager.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "AFNetworkActivityIndicatorManager.h" + +#if TARGET_OS_IOS +#import "AFURLSessionManager.h" + +typedef NS_ENUM(NSInteger, AFNetworkActivityManagerState) { + AFNetworkActivityManagerStateNotActive, + AFNetworkActivityManagerStateDelayingStart, + AFNetworkActivityManagerStateActive, + AFNetworkActivityManagerStateDelayingEnd +}; + +static NSTimeInterval const kDefaultAFNetworkActivityManagerActivationDelay = 1.0; +static NSTimeInterval const kDefaultAFNetworkActivityManagerCompletionDelay = 0.17; + +static NSURLRequest * AFNetworkRequestFromNotification(NSNotification *notification) { + if ([[notification object] respondsToSelector:@selector(originalRequest)]) { + return [(NSURLSessionTask *)[notification object] originalRequest]; + } else { + return nil; + } +} + +typedef void (^AFNetworkActivityActionBlock)(BOOL networkActivityIndicatorVisible); + +@interface AFNetworkActivityIndicatorManager () +@property (readwrite, nonatomic, assign) NSInteger activityCount; +@property (readwrite, nonatomic, strong) NSTimer *activationDelayTimer; +@property (readwrite, nonatomic, strong) NSTimer *completionDelayTimer; +@property (readonly, nonatomic, getter = isNetworkActivityOccurring) BOOL networkActivityOccurring; +@property (nonatomic, copy) AFNetworkActivityActionBlock networkActivityActionBlock; +@property (nonatomic, assign) AFNetworkActivityManagerState currentState; +@property (nonatomic, assign, getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible; + +- (void)updateCurrentStateForNetworkActivityChange; +@end + +@implementation AFNetworkActivityIndicatorManager + ++ (instancetype)sharedManager { + static AFNetworkActivityIndicatorManager *_sharedManager = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + _sharedManager = [[self alloc] init]; + }); + + return _sharedManager; +} + +- (instancetype)init { + self = [super init]; + if (!self) { + return nil; + } + self.currentState = AFNetworkActivityManagerStateNotActive; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkRequestDidStart:) name:AFNetworkingTaskDidResumeNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkRequestDidFinish:) name:AFNetworkingTaskDidSuspendNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkRequestDidFinish:) name:AFNetworkingTaskDidCompleteNotification object:nil]; + self.activationDelay = kDefaultAFNetworkActivityManagerActivationDelay; + self.completionDelay = kDefaultAFNetworkActivityManagerCompletionDelay; + + return self; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [_activationDelayTimer invalidate]; + [_completionDelayTimer invalidate]; +} + +- (void)setEnabled:(BOOL)enabled { + _enabled = enabled; + if (enabled == NO) { + [self setCurrentState:AFNetworkActivityManagerStateNotActive]; + } +} + +- (void)setNetworkingActivityActionWithBlock:(void (^)(BOOL networkActivityIndicatorVisible))block { + self.networkActivityActionBlock = block; +} + +- (BOOL)isNetworkActivityOccurring { + @synchronized(self) { + return self.activityCount > 0; + } +} + +- (void)setNetworkActivityIndicatorVisible:(BOOL)networkActivityIndicatorVisible { + if (_networkActivityIndicatorVisible != networkActivityIndicatorVisible) { + [self willChangeValueForKey:@"networkActivityIndicatorVisible"]; + @synchronized(self) { + _networkActivityIndicatorVisible = networkActivityIndicatorVisible; + } + [self didChangeValueForKey:@"networkActivityIndicatorVisible"]; + if (self.networkActivityActionBlock) { + self.networkActivityActionBlock(networkActivityIndicatorVisible); + } else { + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:networkActivityIndicatorVisible]; + } + } +} + +- (void)setActivityCount:(NSInteger)activityCount { + @synchronized(self) { + _activityCount = activityCount; + } + + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateCurrentStateForNetworkActivityChange]; + }); +} + +- (void)incrementActivityCount { + [self willChangeValueForKey:@"activityCount"]; + @synchronized(self) { + _activityCount++; + } + [self didChangeValueForKey:@"activityCount"]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateCurrentStateForNetworkActivityChange]; + }); +} + +- (void)decrementActivityCount { + [self willChangeValueForKey:@"activityCount"]; + @synchronized(self) { + _activityCount = MAX(_activityCount - 1, 0); + } + [self didChangeValueForKey:@"activityCount"]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateCurrentStateForNetworkActivityChange]; + }); +} + +- (void)networkRequestDidStart:(NSNotification *)notification { + if ([AFNetworkRequestFromNotification(notification) URL]) { + [self incrementActivityCount]; + } +} + +- (void)networkRequestDidFinish:(NSNotification *)notification { + if ([AFNetworkRequestFromNotification(notification) URL]) { + [self decrementActivityCount]; + } +} + +#pragma mark - Internal State Management +- (void)setCurrentState:(AFNetworkActivityManagerState)currentState { + @synchronized(self) { + if (_currentState != currentState) { + [self willChangeValueForKey:@"currentState"]; + _currentState = currentState; + switch (currentState) { + case AFNetworkActivityManagerStateNotActive: + [self cancelActivationDelayTimer]; + [self cancelCompletionDelayTimer]; + [self setNetworkActivityIndicatorVisible:NO]; + break; + case AFNetworkActivityManagerStateDelayingStart: + [self startActivationDelayTimer]; + break; + case AFNetworkActivityManagerStateActive: + [self cancelCompletionDelayTimer]; + [self setNetworkActivityIndicatorVisible:YES]; + break; + case AFNetworkActivityManagerStateDelayingEnd: + [self startCompletionDelayTimer]; + break; + } + [self didChangeValueForKey:@"currentState"]; + } + + } +} + +- (void)updateCurrentStateForNetworkActivityChange { + if (self.enabled) { + switch (self.currentState) { + case AFNetworkActivityManagerStateNotActive: + if (self.isNetworkActivityOccurring) { + [self setCurrentState:AFNetworkActivityManagerStateDelayingStart]; + } + break; + case AFNetworkActivityManagerStateDelayingStart: + //No op. Let the delay timer finish out. + break; + case AFNetworkActivityManagerStateActive: + if (!self.isNetworkActivityOccurring) { + [self setCurrentState:AFNetworkActivityManagerStateDelayingEnd]; + } + break; + case AFNetworkActivityManagerStateDelayingEnd: + if (self.isNetworkActivityOccurring) { + [self setCurrentState:AFNetworkActivityManagerStateActive]; + } + break; + } + } +} + +- (void)startActivationDelayTimer { + self.activationDelayTimer = [NSTimer + timerWithTimeInterval:self.activationDelay target:self selector:@selector(activationDelayTimerFired) userInfo:nil repeats:NO]; + [[NSRunLoop mainRunLoop] addTimer:self.activationDelayTimer forMode:NSRunLoopCommonModes]; +} + +- (void)activationDelayTimerFired { + if (self.networkActivityOccurring) { + [self setCurrentState:AFNetworkActivityManagerStateActive]; + } else { + [self setCurrentState:AFNetworkActivityManagerStateNotActive]; + } +} + +- (void)startCompletionDelayTimer { + [self.completionDelayTimer invalidate]; + self.completionDelayTimer = [NSTimer timerWithTimeInterval:self.completionDelay target:self selector:@selector(completionDelayTimerFired) userInfo:nil repeats:NO]; + [[NSRunLoop mainRunLoop] addTimer:self.completionDelayTimer forMode:NSRunLoopCommonModes]; +} + +- (void)completionDelayTimerFired { + [self setCurrentState:AFNetworkActivityManagerStateNotActive]; +} + +- (void)cancelActivationDelayTimer { + [self.activationDelayTimer invalidate]; +} + +- (void)cancelCompletionDelayTimer { + [self.completionDelayTimer invalidate]; +} + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.h b/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.h index 5a445075e..72296d496 100755 --- a/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.h +++ b/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.h @@ -1,5 +1,5 @@ // AFNetworkReachabilityManager.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -24,14 +24,6 @@ #if !TARGET_OS_WATCH #import -#ifndef NS_DESIGNATED_INITIALIZER -#if __has_attribute(objc_designated_initializer) -#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer)) -#else -#define NS_DESIGNATED_INITIALIZER -#endif -#endif - typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) { AFNetworkReachabilityStatusUnknown = -1, AFNetworkReachabilityStatusNotReachable = 0, @@ -46,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN Reachability can be used to determine background information about why a network operation failed, or to trigger a network operation retrying when a connection is established. It should not be used to prevent a user from initiating a network request, as it's possible that an initial request may be required to establish reachability. - See Apple's Reachability Sample Code (https://developer.apple.com/library/ios/samplecode/reachability/) + See Apple's Reachability Sample Code ( https://developer.apple.com/library/ios/samplecode/reachability/ ) @warning Instances of `AFNetworkReachabilityManager` must be started with `-startMonitoring` before reachability status can be determined. */ @@ -81,6 +73,13 @@ NS_ASSUME_NONNULL_BEGIN */ + (instancetype)sharedManager; +/** + Creates and returns a network reachability manager with the default socket address. + + @return An initialized network reachability manager, actively monitoring the default socket address. + */ ++ (instancetype)manager; + /** Creates and returns a network reachability manager for the specified domain. @@ -93,7 +92,7 @@ NS_ASSUME_NONNULL_BEGIN /** Creates and returns a network reachability manager for the socket address. - @param address The socket address (`sockaddr_in`) used to evaluate network reachability. + @param address The socket address (`sockaddr_in6`) used to evaluate network reachability. @return An initialized network reachability manager, actively monitoring the specified socket address. */ @@ -108,6 +107,13 @@ NS_ASSUME_NONNULL_BEGIN */ - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability NS_DESIGNATED_INITIALIZER; +/** + * Initializes an instance of a network reachability manager + * + * @return nil as this method is unavailable + */ +- (nullable instancetype)init NS_UNAVAILABLE; + ///-------------------------------------------------- /// @name Starting & Stopping Reachability Monitoring ///-------------------------------------------------- @@ -191,8 +197,8 @@ NS_ASSUME_NONNULL_BEGIN @warning In order for network reachability to be monitored, include the `SystemConfiguration` framework in the active target's "Link Binary With Library" build phase, and add `#import ` to the header prefix of the project (`Prefix.pch`). */ -extern NSString * const AFNetworkingReachabilityDidChangeNotification; -extern NSString * const AFNetworkingReachabilityNotificationStatusItem; +FOUNDATION_EXPORT NSString * const AFNetworkingReachabilityDidChangeNotification; +FOUNDATION_EXPORT NSString * const AFNetworkingReachabilityNotificationStatusItem; ///-------------------- /// @name Functions @@ -201,7 +207,7 @@ extern NSString * const AFNetworkingReachabilityNotificationStatusItem; /** Returns a localized string representation of an `AFNetworkReachabilityStatus` value. */ -extern NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status); +FOUNDATION_EXPORT NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status); NS_ASSUME_NONNULL_END #endif diff --git a/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.m b/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.m index 93bebfd2c..d45836485 100755 --- a/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.m +++ b/clients/ios/Other Sources/AFNetworking/AFNetworkReachabilityManager.m @@ -1,5 +1,5 @@ // AFNetworkReachabilityManager.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -33,12 +33,6 @@ NSString * const AFNetworkingReachabilityNotificationStatusItem = @"AFNetworking typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status); -typedef NS_ENUM(NSUInteger, AFNetworkReachabilityAssociation) { - AFNetworkReachabilityForAddress = 1, - AFNetworkReachabilityForAddressPair = 2, - AFNetworkReachabilityForName = 3, -}; - NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusNotReachable: @@ -76,22 +70,31 @@ static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetwork return status; } -static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) { +/** + * Queue a status change notification for the main thread. + * + * This is done to ensure that the notifications are received in the same order + * as they are sent. If notifications are sent directly, it is possible that + * a queued notification (for an earlier status condition) is processed after + * the later update, resulting in the listener being left in the wrong state. + */ +static void AFPostReachabilityStatusChange(SCNetworkReachabilityFlags flags, AFNetworkReachabilityStatusBlock block) { AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); - AFNetworkReachabilityStatusBlock block = (__bridge AFNetworkReachabilityStatusBlock)info; - if (block) { - block(status); - } - - dispatch_async(dispatch_get_main_queue(), ^{ + if (block) { + block(status); + } NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; NSDictionary *userInfo = @{ AFNetworkingReachabilityNotificationStatusItem: @(status) }; [notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:userInfo]; }); - } +static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) { + AFPostReachabilityStatusChange(flags, (__bridge AFNetworkReachabilityStatusBlock)info); +} + + static const void * AFNetworkReachabilityRetainCallback(const void *info) { return Block_copy(info); } @@ -103,8 +106,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { } @interface AFNetworkReachabilityManager () -@property (readwrite, nonatomic, strong) id networkReachability; -@property (readwrite, nonatomic, assign) AFNetworkReachabilityAssociation networkReachabilityAssociation; +@property (readonly, nonatomic, assign) SCNetworkReachabilityRef networkReachability; @property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus; @property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock; @end @@ -115,12 +117,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { static AFNetworkReachabilityManager *_sharedManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - struct sockaddr_in address; - bzero(&address, sizeof(address)); - address.sin_len = sizeof(address); - address.sin_family = AF_INET; - - _sharedManager = [self managerForAddress:&address]; + _sharedManager = [self manager]; }); return _sharedManager; @@ -130,24 +127,37 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [domain UTF8String]); AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability]; - manager.networkReachabilityAssociation = AFNetworkReachabilityForName; CFRelease(reachability); - + return manager; } + (instancetype)managerForAddress:(const void *)address { SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)address); - AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability]; - manager.networkReachabilityAssociation = AFNetworkReachabilityForAddress; - + CFRelease(reachability); return manager; } ++ (instancetype)manager +{ +#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) + struct sockaddr_in6 address; + bzero(&address, sizeof(address)); + address.sin6_len = sizeof(address); + address.sin6_family = AF_INET6; +#else + struct sockaddr_in address; + bzero(&address, sizeof(address)); + address.sin_len = sizeof(address); + address.sin_family = AF_INET; +#endif + return [self managerForAddress:&address]; +} + - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability { self = [super init]; if (!self) { @@ -167,6 +177,10 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { - (void)dealloc { [self stopMonitoring]; + + if (_networkReachability != NULL) { + CFRelease(_networkReachability); + } } #pragma mark - @@ -203,33 +217,16 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { }; - id networkReachability = self.networkReachability; SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL}; - SCNetworkReachabilitySetCallback((__bridge SCNetworkReachabilityRef)networkReachability, AFNetworkReachabilityCallback, &context); - SCNetworkReachabilityScheduleWithRunLoop((__bridge SCNetworkReachabilityRef)networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); + SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context); + SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); - switch (self.networkReachabilityAssociation) { - case AFNetworkReachabilityForName: - break; - case AFNetworkReachabilityForAddress: - case AFNetworkReachabilityForAddressPair: - default: { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{ - SCNetworkReachabilityFlags flags; - SCNetworkReachabilityGetFlags((__bridge SCNetworkReachabilityRef)networkReachability, &flags); - AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); - dispatch_async(dispatch_get_main_queue(), ^{ - callback(status); - - NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; - [notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:@{ AFNetworkingReachabilityNotificationStatusItem: @(status) }]; - - - }); - }); + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{ + SCNetworkReachabilityFlags flags; + if (SCNetworkReachabilityGetFlags(self.networkReachability, &flags)) { + AFPostReachabilityStatusChange(flags, callback); } - break; - } + }); } - (void)stopMonitoring { @@ -237,7 +234,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { return; } - SCNetworkReachabilityUnscheduleFromRunLoop((__bridge SCNetworkReachabilityRef)self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); + SCNetworkReachabilityUnscheduleFromRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); } #pragma mark - diff --git a/clients/ios/Other Sources/AFNetworking/AFNetworking.h b/clients/ios/Other Sources/AFNetworking/AFNetworking.h index 6d442bb14..e2fb2f44e 100755 --- a/clients/ios/Other Sources/AFNetworking/AFNetworking.h +++ b/clients/ios/Other Sources/AFNetworking/AFNetworking.h @@ -22,6 +22,7 @@ #import #import +#import #ifndef _AFNETWORKING_ #define _AFNETWORKING_ @@ -29,18 +30,12 @@ #import "AFURLRequestSerialization.h" #import "AFURLResponseSerialization.h" #import "AFSecurityPolicy.h" + #if !TARGET_OS_WATCH #import "AFNetworkReachabilityManager.h" - #import "AFURLConnectionOperation.h" - #import "AFHTTPRequestOperation.h" - #import "AFHTTPRequestOperationManager.h" #endif -#if ( ( defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || \ - ( defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 ) || \ - TARGET_OS_WATCH ) #import "AFURLSessionManager.h" #import "AFHTTPSessionManager.h" -#endif #endif /* _AFNETWORKING_ */ diff --git a/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.h b/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.h index 3c38da830..c005efa81 100755 --- a/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.h +++ b/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.h @@ -1,5 +1,5 @@ // AFSecurityPolicy.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -36,7 +36,7 @@ typedef NS_ENUM(NSUInteger, AFSSLPinningMode) { NS_ASSUME_NONNULL_BEGIN -@interface AFSecurityPolicy : NSObject +@interface AFSecurityPolicy : NSObject /** The criteria by which server trust should be evaluated against the pinned SSL certificates. Defaults to `AFSSLPinningModeNone`. @@ -44,9 +44,13 @@ NS_ASSUME_NONNULL_BEGIN @property (readonly, nonatomic, assign) AFSSLPinningMode SSLPinningMode; /** - The certificates used to evaluate server trust according to the SSL pinning mode. By default, this property is set to any (`.cer`) certificates included in the app bundle. Note that if you create an array with duplicate certificates, the duplicate certificates will be removed. Note that if pinning is enabled, `evaluateServerTrust:forDomain:` will return true if any pinned certificate matches. + The certificates used to evaluate server trust according to the SSL pinning mode. + + By default, this property is set to any (`.cer`) certificates included in the target compiling AFNetworking. Note that if you are using AFNetworking as embedded framework, no certificates will be pinned by default. Use `certificatesInBundle` to load certificates from your target, and then create a new policy by calling `policyWithPinningMode:withPinnedCertificates`. + + Note that if pinning is enabled, `evaluateServerTrust:forDomain:` will return true if any pinned certificate matches. */ -@property (nonatomic, strong, nullable) NSArray *pinnedCertificates; +@property (nonatomic, strong, nullable) NSSet *pinnedCertificates; /** Whether or not to trust servers with an invalid or expired SSL certificates. Defaults to `NO`. @@ -58,6 +62,17 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, assign) BOOL validatesDomainName; +///----------------------------------------- +/// @name Getting Certificates from the Bundle +///----------------------------------------- + +/** + Returns any certificates included in the bundle. If you are using AFNetworking as an embedded framework, you must use this method to find the certificates you have included in your app bundle, and use them when creating your security policy by calling `policyWithPinningMode:withPinnedCertificates`. + + @return The certificates included in the given bundle. + */ ++ (NSSet *)certificatesInBundle:(NSBundle *)bundle; + ///----------------------------------------- /// @name Getting Specific Security Policies ///----------------------------------------- @@ -82,23 +97,20 @@ NS_ASSUME_NONNULL_BEGIN */ + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode; +/** + Creates and returns a security policy with the specified pinning mode. + + @param pinningMode The SSL pinning mode. + @param pinnedCertificates The certificates to pin against. + + @return A new security policy. + */ ++ (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode withPinnedCertificates:(NSSet *)pinnedCertificates; + ///------------------------------ /// @name Evaluating Server Trust ///------------------------------ -/** - Whether or not the specified server trust should be accepted, based on the security policy. - - This method should be used when responding to an authentication challenge from a server. - - @param serverTrust The X.509 certificate trust of the server. - - @return Whether or not to trust the server. - - @warning This method has been deprecated in favor of `-evaluateServerTrust:forDomain:`. - */ -- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust DEPRECATED_ATTRIBUTE; - /** Whether or not the specified server trust should be accepted, based on the security policy. diff --git a/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.m b/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.m index e8eaa65f1..043441a03 100755 --- a/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.m +++ b/clients/ios/Other Sources/AFNetworking/AFSecurityPolicy.m @@ -1,5 +1,5 @@ // AFSecurityPolicy.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,7 @@ #import -#if !TARGET_OS_IOS && !TARGET_OS_WATCH +#if !TARGET_OS_IOS && !TARGET_OS_WATCH && !TARGET_OS_TV static NSData * AFSecKeyGetData(SecKeyRef key) { CFDataRef data = NULL; @@ -41,7 +41,7 @@ _out: #endif static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { -#if TARGET_OS_IOS || TARGET_OS_WATCH +#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV return [(__bridge id)key1 isEqual:(__bridge id)key2]; #else return [AFSecKeyGetData(key1) isEqual:AFSecKeyGetData(key2)]; @@ -51,8 +51,6 @@ static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { static id AFPublicKeyForCertificate(NSData *certificate) { id allowedPublicKey = nil; SecCertificateRef allowedCertificate; - SecCertificateRef allowedCertificates[1]; - CFArrayRef tempCertificates = nil; SecPolicyRef policy = nil; SecTrustRef allowedTrust = nil; SecTrustResultType result; @@ -60,11 +58,8 @@ static id AFPublicKeyForCertificate(NSData *certificate) { allowedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificate); __Require_Quiet(allowedCertificate != NULL, _out); - allowedCertificates[0] = allowedCertificate; - tempCertificates = CFArrayCreate(NULL, (const void **)allowedCertificates, 1, NULL); - policy = SecPolicyCreateBasicX509(); - __Require_noErr_Quiet(SecTrustCreateWithCertificates(tempCertificates, policy, &allowedTrust), _out); + __Require_noErr_Quiet(SecTrustCreateWithCertificates(allowedCertificate, policy, &allowedTrust), _out); __Require_noErr_Quiet(SecTrustEvaluate(allowedTrust, &result), _out); allowedPublicKey = (__bridge_transfer id)SecTrustCopyPublicKey(allowedTrust); @@ -78,10 +73,6 @@ _out: CFRelease(policy); } - if (tempCertificates) { - CFRelease(tempCertificates); - } - if (allowedCertificate) { CFRelease(allowedCertificate); } @@ -150,25 +141,29 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { @interface AFSecurityPolicy() @property (readwrite, nonatomic, assign) AFSSLPinningMode SSLPinningMode; -@property (readwrite, nonatomic, strong) NSArray *pinnedPublicKeys; +@property (readwrite, nonatomic, strong) NSSet *pinnedPublicKeys; @end @implementation AFSecurityPolicy -+ (NSArray *)defaultPinnedCertificates { - static NSArray *_defaultPinnedCertificates = nil; ++ (NSSet *)certificatesInBundle:(NSBundle *)bundle { + NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory:@"."]; + + NSMutableSet *certificates = [NSMutableSet setWithCapacity:[paths count]]; + for (NSString *path in paths) { + NSData *certificateData = [NSData dataWithContentsOfFile:path]; + [certificates addObject:certificateData]; + } + + return [NSSet setWithSet:certificates]; +} + ++ (NSSet *)defaultPinnedCertificates { + static NSSet *_defaultPinnedCertificates = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSBundle *bundle = [NSBundle bundleForClass:[self class]]; - NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory:@"."]; - - NSMutableArray *certificates = [NSMutableArray arrayWithCapacity:[paths count]]; - for (NSString *path in paths) { - NSData *certificateData = [NSData dataWithContentsOfFile:path]; - [certificates addObject:certificateData]; - } - - _defaultPinnedCertificates = [[NSArray alloc] initWithArray:certificates]; + _defaultPinnedCertificates = [self certificatesInBundle:bundle]; }); return _defaultPinnedCertificates; @@ -182,15 +177,19 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { } + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode { + return [self policyWithPinningMode:pinningMode withPinnedCertificates:[self defaultPinnedCertificates]]; +} + ++ (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode withPinnedCertificates:(NSSet *)pinnedCertificates { AFSecurityPolicy *securityPolicy = [[self alloc] init]; securityPolicy.SSLPinningMode = pinningMode; - [securityPolicy setPinnedCertificates:[self defaultPinnedCertificates]]; + [securityPolicy setPinnedCertificates:pinnedCertificates]; return securityPolicy; } -- (id)init { +- (instancetype)init { self = [super init]; if (!self) { return nil; @@ -201,11 +200,11 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { return self; } -- (void)setPinnedCertificates:(NSArray *)pinnedCertificates { - _pinnedCertificates = [[NSOrderedSet orderedSetWithArray:pinnedCertificates] array]; +- (void)setPinnedCertificates:(NSSet *)pinnedCertificates { + _pinnedCertificates = pinnedCertificates; if (self.pinnedCertificates) { - NSMutableArray *mutablePinnedPublicKeys = [NSMutableArray arrayWithCapacity:[self.pinnedCertificates count]]; + NSMutableSet *mutablePinnedPublicKeys = [NSMutableSet setWithCapacity:[self.pinnedCertificates count]]; for (NSData *certificate in self.pinnedCertificates) { id publicKey = AFPublicKeyForCertificate(certificate); if (!publicKey) { @@ -213,7 +212,7 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { } [mutablePinnedPublicKeys addObject:publicKey]; } - self.pinnedPublicKeys = [NSArray arrayWithArray:mutablePinnedPublicKeys]; + self.pinnedPublicKeys = [NSSet setWithSet:mutablePinnedPublicKeys]; } else { self.pinnedPublicKeys = nil; } @@ -221,10 +220,6 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { #pragma mark - -- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust { - return [self evaluateServerTrust:serverTrust forDomain:nil]; -} - - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(NSString *)domain { @@ -251,16 +246,11 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies); if (self.SSLPinningMode == AFSSLPinningModeNone) { - if (self.allowInvalidCertificates || AFServerTrustIsValid(serverTrust)){ - return YES; - } else { - return NO; - } + return self.allowInvalidCertificates || AFServerTrustIsValid(serverTrust); } else if (!AFServerTrustIsValid(serverTrust) && !self.allowInvalidCertificates) { return NO; } - NSArray *serverCertificates = AFCertificateTrustChainForServerTrust(serverTrust); switch (self.SSLPinningMode) { case AFSSLPinningModeNone: default: @@ -276,13 +266,16 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { return NO; } - NSUInteger trustedCertificateCount = 0; - for (NSData *trustChainCertificate in serverCertificates) { + // obtain the chain after being validated, which *should* contain the pinned certificate in the last position (if it's the Root CA) + NSArray *serverCertificates = AFCertificateTrustChainForServerTrust(serverTrust); + + for (NSData *trustChainCertificate in [serverCertificates reverseObjectEnumerator]) { if ([self.pinnedCertificates containsObject:trustChainCertificate]) { - trustedCertificateCount++; + return YES; } } - return trustedCertificateCount > 0; + + return NO; } case AFSSLPinningModePublicKey: { NSUInteger trustedPublicKeyCount = 0; @@ -308,4 +301,44 @@ static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) { return [NSSet setWithObject:@"pinnedCertificates"]; } +#pragma mark - NSSecureCoding + ++ (BOOL)supportsSecureCoding { + return YES; +} + +- (instancetype)initWithCoder:(NSCoder *)decoder { + + self = [self init]; + if (!self) { + return nil; + } + + self.SSLPinningMode = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(SSLPinningMode))] unsignedIntegerValue]; + self.allowInvalidCertificates = [decoder decodeBoolForKey:NSStringFromSelector(@selector(allowInvalidCertificates))]; + self.validatesDomainName = [decoder decodeBoolForKey:NSStringFromSelector(@selector(validatesDomainName))]; + self.pinnedCertificates = [decoder decodeObjectOfClass:[NSArray class] forKey:NSStringFromSelector(@selector(pinnedCertificates))]; + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder { + [coder encodeObject:[NSNumber numberWithUnsignedInteger:self.SSLPinningMode] forKey:NSStringFromSelector(@selector(SSLPinningMode))]; + [coder encodeBool:self.allowInvalidCertificates forKey:NSStringFromSelector(@selector(allowInvalidCertificates))]; + [coder encodeBool:self.validatesDomainName forKey:NSStringFromSelector(@selector(validatesDomainName))]; + [coder encodeObject:self.pinnedCertificates forKey:NSStringFromSelector(@selector(pinnedCertificates))]; +} + +#pragma mark - NSCopying + +- (instancetype)copyWithZone:(NSZone *)zone { + AFSecurityPolicy *securityPolicy = [[[self class] allocWithZone:zone] init]; + securityPolicy.SSLPinningMode = self.SSLPinningMode; + securityPolicy.allowInvalidCertificates = self.allowInvalidCertificates; + securityPolicy.validatesDomainName = self.validatesDomainName; + securityPolicy.pinnedCertificates = [self.pinnedCertificates copyWithZone:zone]; + + return securityPolicy; +} + @end diff --git a/clients/ios/Other Sources/AFNetworking/AFURLConnectionOperation.h b/clients/ios/Other Sources/AFNetworking/AFURLConnectionOperation.h deleted file mode 100755 index 3ea87fe2c..000000000 --- a/clients/ios/Other Sources/AFNetworking/AFURLConnectionOperation.h +++ /dev/null @@ -1,344 +0,0 @@ -// AFURLConnectionOperation.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#import - -#import -#import "AFURLRequestSerialization.h" -#import "AFURLResponseSerialization.h" -#import "AFSecurityPolicy.h" - -#ifndef NS_DESIGNATED_INITIALIZER -#if __has_attribute(objc_designated_initializer) -#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer)) -#else -#define NS_DESIGNATED_INITIALIZER -#endif -#endif - -/** - `AFURLConnectionOperation` is a subclass of `NSOperation` that implements `NSURLConnection` delegate methods. - - ## Subclassing Notes - - This is the base class of all network request operations. You may wish to create your own subclass in order to implement additional `NSURLConnection` delegate methods (see "`NSURLConnection` Delegate Methods" below), or to provide additional properties and/or class constructors. - - If you are creating a subclass that communicates over the HTTP or HTTPS protocols, you may want to consider subclassing `AFHTTPRequestOperation` instead, as it supports specifying acceptable content types or status codes. - - ## NSURLConnection Delegate Methods - - `AFURLConnectionOperation` implements the following `NSURLConnection` delegate methods: - - - `connection:didReceiveResponse:` - - `connection:didReceiveData:` - - `connectionDidFinishLoading:` - - `connection:didFailWithError:` - - `connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:` - - `connection:willCacheResponse:` - - `connectionShouldUseCredentialStorage:` - - `connection:needNewBodyStream:` - - `connection:willSendRequestForAuthenticationChallenge:` - - If any of these methods are overridden in a subclass, they _must_ call the `super` implementation first. - - ## Callbacks and Completion Blocks - - The built-in `completionBlock` provided by `NSOperation` allows for custom behavior to be executed after the request finishes. It is a common pattern for class constructors in subclasses to take callback block parameters, and execute them conditionally in the body of its `completionBlock`. Make sure to handle cancelled operations appropriately when setting a `completionBlock` (i.e. returning early before parsing response data). See the implementation of any of the `AFHTTPRequestOperation` subclasses for an example of this. - - Subclasses are strongly discouraged from overriding `setCompletionBlock:`, as `AFURLConnectionOperation`'s implementation includes a workaround to mitigate retain cycles, and what Apple rather ominously refers to as ["The Deallocation Problem"](http://developer.apple.com/library/ios/#technotes/tn2109/). - - ## SSL Pinning - - Relying on the CA trust model to validate SSL certificates exposes your app to security vulnerabilities, such as man-in-the-middle attacks. For applications that connect to known servers, SSL certificate pinning provides an increased level of security, by checking server certificate validity against those specified in the app bundle. - - SSL with certificate pinning is strongly recommended for any application that transmits sensitive information to an external webservice. - - Connections will be validated on all matching certificates with a `.cer` extension in the bundle root. - - ## NSCoding & NSCopying Conformance - - `AFURLConnectionOperation` conforms to the `NSCoding` and `NSCopying` protocols, allowing operations to be archived to disk, and copied in memory, respectively. However, because of the intrinsic limitations of capturing the exact state of an operation at a particular moment, there are some important caveats to keep in mind: - - ### NSCoding Caveats - - - Encoded operations do not include any block or stream properties. Be sure to set `completionBlock`, `outputStream`, and any callback blocks as necessary when using `-initWithCoder:` or `NSKeyedUnarchiver`. - - Operations are paused on `encodeWithCoder:`. If the operation was encoded while paused or still executing, its archived state will return `YES` for `isReady`. Otherwise, the state of an operation when encoding will remain unchanged. - - ### NSCopying Caveats - - - `-copy` and `-copyWithZone:` return a new operation with the `NSURLRequest` of the original. So rather than an exact copy of the operation at that particular instant, the copying mechanism returns a completely new instance, which can be useful for retrying operations. - - A copy of an operation will not include the `outputStream` of the original. - - Operation copies do not include `completionBlock`, as it often strongly captures a reference to `self`, which would otherwise have the unintuitive side-effect of pointing to the _original_ operation when copied. - */ - -NS_ASSUME_NONNULL_BEGIN - -@interface AFURLConnectionOperation : NSOperation - -///------------------------------- -/// @name Accessing Run Loop Modes -///------------------------------- - -/** - The run loop modes in which the operation will run on the network thread. By default, this is a single-member set containing `NSRunLoopCommonModes`. - */ -@property (nonatomic, strong) NSSet *runLoopModes; - -///----------------------------------------- -/// @name Getting URL Connection Information -///----------------------------------------- - -/** - The request used by the operation's connection. - */ -@property (readonly, nonatomic, strong) NSURLRequest *request; - -/** - The last response received by the operation's connection. - */ -@property (readonly, nonatomic, strong, nullable) NSURLResponse *response; - -/** - The error, if any, that occurred in the lifecycle of the request. - */ -@property (readonly, nonatomic, strong, nullable) NSError *error; - -///---------------------------- -/// @name Getting Response Data -///---------------------------- - -/** - The data received during the request. - */ -@property (readonly, nonatomic, strong, nullable) NSData *responseData; - -/** - The string representation of the response data. - */ -@property (readonly, nonatomic, copy, nullable) NSString *responseString; - -/** - The string encoding of the response. - - If the response does not specify a valid string encoding, `responseStringEncoding` will return `NSUTF8StringEncoding`. - */ -@property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding; - -///------------------------------- -/// @name Managing URL Credentials -///------------------------------- - -/** - Whether the URL connection should consult the credential storage for authenticating the connection. `YES` by default. - - This is the value that is returned in the `NSURLConnectionDelegate` method `-connectionShouldUseCredentialStorage:`. - */ -@property (nonatomic, assign) BOOL shouldUseCredentialStorage; - -/** - The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`. - - This will be overridden by any shared credentials that exist for the username or password of the request URL, if present. - */ -@property (nonatomic, strong, nullable) NSURLCredential *credential; - -///------------------------------- -/// @name Managing Security Policy -///------------------------------- - -/** - The security policy used to evaluate server trust for secure connections. - */ -@property (nonatomic, strong) AFSecurityPolicy *securityPolicy; - -///------------------------ -/// @name Accessing Streams -///------------------------ - -/** - The input stream used to read data to be sent during the request. - - This property acts as a proxy to the `HTTPBodyStream` property of `request`. - */ -@property (nonatomic, strong) NSInputStream *inputStream; - -/** - The output stream that is used to write data received until the request is finished. - - By default, data is accumulated into a buffer that is stored into `responseData` upon completion of the request, with the intermediary `outputStream` property set to `nil`. When `outputStream` is set, the data will not be accumulated into an internal buffer, and as a result, the `responseData` property of the completed request will be `nil`. The output stream will be scheduled in the network thread runloop upon being set. - */ -@property (nonatomic, strong, nullable) NSOutputStream *outputStream; - -///--------------------------------- -/// @name Managing Callback Queues -///--------------------------------- - -/** - The dispatch queue for `completionBlock`. If `NULL` (default), the main queue is used. - */ -#if OS_OBJECT_HAVE_OBJC_SUPPORT -@property (nonatomic, strong, nullable) dispatch_queue_t completionQueue; -#else -@property (nonatomic, assign, nullable) dispatch_queue_t completionQueue; -#endif - -/** - The dispatch group for `completionBlock`. If `NULL` (default), a private dispatch group is used. - */ -#if OS_OBJECT_HAVE_OBJC_SUPPORT -@property (nonatomic, strong, nullable) dispatch_group_t completionGroup; -#else -@property (nonatomic, assign, nullable) dispatch_group_t completionGroup; -#endif - -///--------------------------------------------- -/// @name Managing Request Operation Information -///--------------------------------------------- - -/** - The user info dictionary for the receiver. - */ -@property (nonatomic, strong) NSDictionary *userInfo; -// FIXME: It doesn't seem that this userInfo is used anywhere in the implementation. - -///------------------------------------------------------ -/// @name Initializing an AFURLConnectionOperation Object -///------------------------------------------------------ - -/** - Initializes and returns a newly allocated operation object with a url connection configured with the specified url request. - - This is the designated initializer. - - @param urlRequest The request object to be used by the operation connection. - */ -- (instancetype)initWithRequest:(NSURLRequest *)urlRequest NS_DESIGNATED_INITIALIZER; - -///---------------------------------- -/// @name Pausing / Resuming Requests -///---------------------------------- - -/** - Pauses the execution of the request operation. - - A paused operation returns `NO` for `-isReady`, `-isExecuting`, and `-isFinished`. As such, it will remain in an `NSOperationQueue` until it is either cancelled or resumed. Pausing a finished, cancelled, or paused operation has no effect. - */ -- (void)pause; - -/** - Whether the request operation is currently paused. - - @return `YES` if the operation is currently paused, otherwise `NO`. - */ -- (BOOL)isPaused; - -/** - Resumes the execution of the paused request operation. - - Pause/Resume behavior varies depending on the underlying implementation for the operation class. In its base implementation, resuming a paused requests restarts the original request. However, since HTTP defines a specification for how to request a specific content range, `AFHTTPRequestOperation` will resume downloading the request from where it left off, instead of restarting the original request. - */ -- (void)resume; - -///---------------------------------------------- -/// @name Configuring Backgrounding Task Behavior -///---------------------------------------------- - -/** - Specifies that the operation should continue execution after the app has entered the background, and the expiration handler for that background task. - - @param handler A handler to be called shortly before the application’s remaining background time reaches 0. The handler is wrapped in a block that cancels the operation, and cleans up and marks the end of execution, unlike the `handler` parameter in `UIApplication -beginBackgroundTaskWithExpirationHandler:`, which expects this to be done in the handler itself. The handler is called synchronously on the main thread, thus blocking the application’s suspension momentarily while the application is notified. - */ -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) -- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(nullable void (^)(void))handler NS_EXTENSION_UNAVAILABLE_IOS("Not available in app extensions."); -#endif - -///--------------------------------- -/// @name Setting Progress Callbacks -///--------------------------------- - -/** - Sets a callback to be called when an undetermined number of bytes have been uploaded to the server. - - @param block A block object to be called when an undetermined number of bytes have been uploaded to the server. This block has no return value and takes three arguments: the number of bytes written since the last time the upload progress block was called, the total bytes written, and the total bytes expected to be written during the request, as initially determined by the length of the HTTP body. This block may be called multiple times, and will execute on the main thread. - */ -- (void)setUploadProgressBlock:(nullable void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block; - -/** - Sets a callback to be called when an undetermined number of bytes have been downloaded from the server. - - @param block A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread. - */ -- (void)setDownloadProgressBlock:(nullable void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block; - -///------------------------------------------------- -/// @name Setting NSURLConnection Delegate Callbacks -///------------------------------------------------- - -/** - Sets a block to be executed when the connection will authenticate a challenge in order to download its request, as handled by the `NSURLConnectionDelegate` method `connection:willSendRequestForAuthenticationChallenge:`. - - @param block A block object to be executed when the connection will authenticate a challenge in order to download its request. The block has no return type and takes two arguments: the URL connection object, and the challenge that must be authenticated. This block must invoke one of the challenge-responder methods (NSURLAuthenticationChallengeSender protocol). - - If `allowsInvalidSSLCertificate` is set to YES, `connection:willSendRequestForAuthenticationChallenge:` will attempt to have the challenge sender use credentials with invalid SSL certificates. - */ -- (void)setWillSendRequestForAuthenticationChallengeBlock:(nullable void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block; - -/** - Sets a block to be executed when the server redirects the request from one URL to another URL, or when the request URL changed by the `NSURLProtocol` subclass handling the request in order to standardize its format, as handled by the `NSURLConnectionDataDelegate` method `connection:willSendRequest:redirectResponse:`. - - @param block A block object to be executed when the request URL was changed. The block returns an `NSURLRequest` object, the URL request to redirect, and takes three arguments: the URL connection object, the the proposed redirected request, and the URL response that caused the redirect. - */ -- (void)setRedirectResponseBlock:(nullable NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block; - - -/** - Sets a block to be executed to modify the response a connection will cache, if any, as handled by the `NSURLConnectionDelegate` method `connection:willCacheResponse:`. - - @param block A block object to be executed to determine what response a connection will cache, if any. The block returns an `NSCachedURLResponse` object, the cached response to store in memory or `nil` to prevent the response from being cached, and takes two arguments: the URL connection object, and the cached response provided for the request. - */ -- (void)setCacheResponseBlock:(nullable NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block; - -/// - -/** - - */ -+ (NSArray *)batchOfRequestOperations:(nullable NSArray *)operations - progressBlock:(nullable void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock - completionBlock:(nullable void (^)(NSArray *operations))completionBlock; - -@end - -///-------------------- -/// @name Notifications -///-------------------- - -/** - Posted when an operation begins executing. - */ -extern NSString * const AFNetworkingOperationDidStartNotification; - -/** - Posted when an operation finishes. - */ -extern NSString * const AFNetworkingOperationDidFinishNotification; - -NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFURLConnectionOperation.m b/clients/ios/Other Sources/AFNetworking/AFURLConnectionOperation.m deleted file mode 100755 index d28d69a3a..000000000 --- a/clients/ios/Other Sources/AFNetworking/AFURLConnectionOperation.m +++ /dev/null @@ -1,792 +0,0 @@ -// AFURLConnectionOperation.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#import "AFURLConnectionOperation.h" - -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) -#import -#endif - -#if !__has_feature(objc_arc) -#error AFNetworking must be built with ARC. -// You can turn on ARC for only AFNetworking files by adding -fobjc-arc to the build phase for each of its files. -#endif - -typedef NS_ENUM(NSInteger, AFOperationState) { - AFOperationPausedState = -1, - AFOperationReadyState = 1, - AFOperationExecutingState = 2, - AFOperationFinishedState = 3, -}; - -static dispatch_group_t url_request_operation_completion_group() { - static dispatch_group_t af_url_request_operation_completion_group; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - af_url_request_operation_completion_group = dispatch_group_create(); - }); - - return af_url_request_operation_completion_group; -} - -static dispatch_queue_t url_request_operation_completion_queue() { - static dispatch_queue_t af_url_request_operation_completion_queue; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - af_url_request_operation_completion_queue = dispatch_queue_create("com.alamofire.networking.operation.queue", DISPATCH_QUEUE_CONCURRENT ); - }); - - return af_url_request_operation_completion_queue; -} - -static NSString * const kAFNetworkingLockName = @"com.alamofire.networking.operation.lock"; - -NSString * const AFNetworkingOperationDidStartNotification = @"com.alamofire.networking.operation.start"; -NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.networking.operation.finish"; - -typedef void (^AFURLConnectionOperationProgressBlock)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected); -typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge); -typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse); -typedef NSURLRequest * (^AFURLConnectionOperationRedirectResponseBlock)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse); -typedef void (^AFURLConnectionOperationBackgroundTaskCleanupBlock)(); - -static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { - switch (state) { - case AFOperationReadyState: - return @"isReady"; - case AFOperationExecutingState: - return @"isExecuting"; - case AFOperationFinishedState: - return @"isFinished"; - case AFOperationPausedState: - return @"isPaused"; - default: { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunreachable-code" - return @"state"; -#pragma clang diagnostic pop - } - } -} - -static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperationState toState, BOOL isCancelled) { - switch (fromState) { - case AFOperationReadyState: - switch (toState) { - case AFOperationPausedState: - case AFOperationExecutingState: - return YES; - case AFOperationFinishedState: - return isCancelled; - default: - return NO; - } - case AFOperationExecutingState: - switch (toState) { - case AFOperationPausedState: - case AFOperationFinishedState: - return YES; - default: - return NO; - } - case AFOperationFinishedState: - return NO; - case AFOperationPausedState: - return toState == AFOperationReadyState; - default: { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunreachable-code" - switch (toState) { - case AFOperationPausedState: - case AFOperationReadyState: - case AFOperationExecutingState: - case AFOperationFinishedState: - return YES; - default: - return NO; - } - } -#pragma clang diagnostic pop - } -} - -@interface AFURLConnectionOperation () -@property (readwrite, nonatomic, assign) AFOperationState state; -@property (readwrite, nonatomic, strong) NSRecursiveLock *lock; -@property (readwrite, nonatomic, strong) NSURLConnection *connection; -@property (readwrite, nonatomic, strong) NSURLRequest *request; -@property (readwrite, nonatomic, strong) NSURLResponse *response; -@property (readwrite, nonatomic, strong) NSError *error; -@property (readwrite, nonatomic, strong) NSData *responseData; -@property (readwrite, nonatomic, copy) NSString *responseString; -@property (readwrite, nonatomic, assign) NSStringEncoding responseStringEncoding; -@property (readwrite, nonatomic, assign) long long totalBytesRead; -@property (readwrite, nonatomic, copy) AFURLConnectionOperationBackgroundTaskCleanupBlock backgroundTaskCleanup; -@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress; -@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress; -@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge; -@property (readwrite, nonatomic, copy) AFURLConnectionOperationCacheResponseBlock cacheResponse; -@property (readwrite, nonatomic, copy) AFURLConnectionOperationRedirectResponseBlock redirectResponse; - -- (void)operationDidStart; -- (void)finish; -- (void)cancelConnection; -@end - -@implementation AFURLConnectionOperation -@synthesize outputStream = _outputStream; - -+ (void)networkRequestThreadEntryPoint:(id)__unused object { - @autoreleasepool { - [[NSThread currentThread] setName:@"AFNetworking"]; - - NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; - [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; - [runLoop run]; - } -} - -+ (NSThread *)networkRequestThread { - static NSThread *_networkRequestThread = nil; - static dispatch_once_t oncePredicate; - dispatch_once(&oncePredicate, ^{ - _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; - [_networkRequestThread start]; - }); - - return _networkRequestThread; -} - -- (instancetype)initWithRequest:(NSURLRequest *)urlRequest { - NSParameterAssert(urlRequest); - - self = [super init]; - if (!self) { - return nil; - } - - _state = AFOperationReadyState; - - self.lock = [[NSRecursiveLock alloc] init]; - self.lock.name = kAFNetworkingLockName; - - self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes]; - - self.request = urlRequest; - - self.shouldUseCredentialStorage = YES; - - self.securityPolicy = [AFSecurityPolicy defaultPolicy]; - - return self; -} - -- (instancetype)init NS_UNAVAILABLE -{ - return nil; -} - -- (void)dealloc { - if (_outputStream) { - [_outputStream close]; - _outputStream = nil; - } - - if (_backgroundTaskCleanup) { - _backgroundTaskCleanup(); - } -} - -#pragma mark - - -- (void)setResponseData:(NSData *)responseData { - [self.lock lock]; - if (!responseData) { - _responseData = nil; - } else { - _responseData = [NSData dataWithBytes:responseData.bytes length:responseData.length]; - } - [self.lock unlock]; -} - -- (NSString *)responseString { - [self.lock lock]; - if (!_responseString && self.response && self.responseData) { - self.responseString = [[NSString alloc] initWithData:self.responseData encoding:self.responseStringEncoding]; - } - [self.lock unlock]; - - return _responseString; -} - -- (NSStringEncoding)responseStringEncoding { - [self.lock lock]; - if (!_responseStringEncoding && self.response) { - NSStringEncoding stringEncoding = NSUTF8StringEncoding; - if (self.response.textEncodingName) { - CFStringEncoding IANAEncoding = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)self.response.textEncodingName); - if (IANAEncoding != kCFStringEncodingInvalidId) { - stringEncoding = CFStringConvertEncodingToNSStringEncoding(IANAEncoding); - } - } - - self.responseStringEncoding = stringEncoding; - } - [self.lock unlock]; - - return _responseStringEncoding; -} - -- (NSInputStream *)inputStream { - return self.request.HTTPBodyStream; -} - -- (void)setInputStream:(NSInputStream *)inputStream { - NSMutableURLRequest *mutableRequest = [self.request mutableCopy]; - mutableRequest.HTTPBodyStream = inputStream; - self.request = mutableRequest; -} - -- (NSOutputStream *)outputStream { - if (!_outputStream) { - self.outputStream = [NSOutputStream outputStreamToMemory]; - } - - return _outputStream; -} - -- (void)setOutputStream:(NSOutputStream *)outputStream { - [self.lock lock]; - if (outputStream != _outputStream) { - if (_outputStream) { - [_outputStream close]; - } - - _outputStream = outputStream; - } - [self.lock unlock]; -} - -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) -- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler { - [self.lock lock]; - if (!self.backgroundTaskCleanup) { - UIApplication *application = [UIApplication sharedApplication]; - UIBackgroundTaskIdentifier __block backgroundTaskIdentifier = UIBackgroundTaskInvalid; - __weak __typeof(self)weakSelf = self; - - self.backgroundTaskCleanup = ^(){ - if (backgroundTaskIdentifier != UIBackgroundTaskInvalid) { - [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier]; - backgroundTaskIdentifier = UIBackgroundTaskInvalid; - } - }; - - backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ - __strong __typeof(weakSelf)strongSelf = weakSelf; - - if (handler) { - handler(); - } - - if (strongSelf) { - [strongSelf cancel]; - strongSelf.backgroundTaskCleanup(); - } - }]; - } - [self.lock unlock]; -} -#endif - -#pragma mark - - -- (void)setState:(AFOperationState)state { - if (!AFStateTransitionIsValid(self.state, state, [self isCancelled])) { - return; - } - - [self.lock lock]; - NSString *oldStateKey = AFKeyPathFromOperationState(self.state); - NSString *newStateKey = AFKeyPathFromOperationState(state); - - [self willChangeValueForKey:newStateKey]; - [self willChangeValueForKey:oldStateKey]; - _state = state; - [self didChangeValueForKey:oldStateKey]; - [self didChangeValueForKey:newStateKey]; - [self.lock unlock]; -} - -- (void)pause { - if ([self isPaused] || [self isFinished] || [self isCancelled]) { - return; - } - - [self.lock lock]; - if ([self isExecuting]) { - [self performSelector:@selector(operationDidPause) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; - - dispatch_async(dispatch_get_main_queue(), ^{ - NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; - [notificationCenter postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; - }); - } - - self.state = AFOperationPausedState; - [self.lock unlock]; -} - -- (void)operationDidPause { - [self.lock lock]; - [self.connection cancel]; - [self.lock unlock]; -} - -- (BOOL)isPaused { - return self.state == AFOperationPausedState; -} - -- (void)resume { - if (![self isPaused]) { - return; - } - - [self.lock lock]; - self.state = AFOperationReadyState; - - [self start]; - [self.lock unlock]; -} - -#pragma mark - - -- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block { - self.uploadProgress = block; -} - -- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block { - self.downloadProgress = block; -} - -- (void)setWillSendRequestForAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block { - self.authenticationChallenge = block; -} - -- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block { - self.cacheResponse = block; -} - -- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block { - self.redirectResponse = block; -} - -#pragma mark - NSOperation - -- (void)setCompletionBlock:(void (^)(void))block { - [self.lock lock]; - if (!block) { - [super setCompletionBlock:nil]; - } else { - __weak __typeof(self)weakSelf = self; - [super setCompletionBlock:^ { - __strong __typeof(weakSelf)strongSelf = weakSelf; - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" - dispatch_group_t group = strongSelf.completionGroup ?: url_request_operation_completion_group(); - dispatch_queue_t queue = strongSelf.completionQueue ?: dispatch_get_main_queue(); -#pragma clang diagnostic pop - - dispatch_group_async(group, queue, ^{ - block(); - }); - - dispatch_group_notify(group, url_request_operation_completion_queue(), ^{ - [strongSelf setCompletionBlock:nil]; - }); - }]; - } - [self.lock unlock]; -} - -- (BOOL)isReady { - return self.state == AFOperationReadyState && [super isReady]; -} - -- (BOOL)isExecuting { - return self.state == AFOperationExecutingState; -} - -- (BOOL)isFinished { - return self.state == AFOperationFinishedState; -} - -- (BOOL)isConcurrent { - return YES; -} - -- (void)start { - [self.lock lock]; - if ([self isCancelled]) { - [self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; - } else if ([self isReady]) { - self.state = AFOperationExecutingState; - - [self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; - } - [self.lock unlock]; -} - -- (void)operationDidStart { - [self.lock lock]; - if (![self isCancelled]) { - self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO]; - - NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; - for (NSString *runLoopMode in self.runLoopModes) { - [self.connection scheduleInRunLoop:runLoop forMode:runLoopMode]; - [self.outputStream scheduleInRunLoop:runLoop forMode:runLoopMode]; - } - - [self.outputStream open]; - [self.connection start]; - } - [self.lock unlock]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self]; - }); -} - -- (void)finish { - [self.lock lock]; - self.state = AFOperationFinishedState; - [self.lock unlock]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; - }); -} - -- (void)cancel { - [self.lock lock]; - if (![self isFinished] && ![self isCancelled]) { - [super cancel]; - - if ([self isExecuting]) { - [self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; - } - } - [self.lock unlock]; -} - -- (void)cancelConnection { - NSDictionary *userInfo = nil; - if ([self.request URL]) { - userInfo = @{NSURLErrorFailingURLErrorKey : [self.request URL]}; - } - NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]; - - if (![self isFinished]) { - if (self.connection) { - [self.connection cancel]; - [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:error]; - } else { - // Accommodate race condition where `self.connection` has not yet been set before cancellation - self.error = error; - [self finish]; - } - } -} - -#pragma mark - - -+ (NSArray *)batchOfRequestOperations:(NSArray *)operations - progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock - completionBlock:(void (^)(NSArray *operations))completionBlock -{ - if (!operations || [operations count] == 0) { - return @[[NSBlockOperation blockOperationWithBlock:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (completionBlock) { - completionBlock(@[]); - } - }); - }]]; - } - - __block dispatch_group_t group = dispatch_group_create(); - NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{ - dispatch_group_notify(group, dispatch_get_main_queue(), ^{ - if (completionBlock) { - completionBlock(operations); - } - }); - }]; - - for (AFURLConnectionOperation *operation in operations) { - operation.completionGroup = group; - void (^originalCompletionBlock)(void) = [operation.completionBlock copy]; - __weak __typeof(operation)weakOperation = operation; - operation.completionBlock = ^{ - __strong __typeof(weakOperation)strongOperation = weakOperation; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" - dispatch_queue_t queue = strongOperation.completionQueue ?: dispatch_get_main_queue(); -#pragma clang diagnostic pop - dispatch_group_async(group, queue, ^{ - if (originalCompletionBlock) { - originalCompletionBlock(); - } - - NSUInteger numberOfFinishedOperations = [[operations indexesOfObjectsPassingTest:^BOOL(id op, NSUInteger __unused idx, BOOL __unused *stop) { - return [op isFinished]; - }] count]; - - if (progressBlock) { - progressBlock(numberOfFinishedOperations, [operations count]); - } - - dispatch_group_leave(group); - }); - }; - - dispatch_group_enter(group); - [batchedOperation addDependency:operation]; - } - - return [operations arrayByAddingObject:batchedOperation]; -} - -#pragma mark - NSObject - -- (NSString *)description { - [self.lock lock]; - NSString *description = [NSString stringWithFormat:@"<%@: %p, state: %@, cancelled: %@ request: %@, response: %@>", NSStringFromClass([self class]), self, AFKeyPathFromOperationState(self.state), ([self isCancelled] ? @"YES" : @"NO"), self.request, self.response]; - [self.lock unlock]; - return description; -} - -#pragma mark - NSURLConnectionDelegate - -- (void)connection:(NSURLConnection *)connection -willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge -{ - if (self.authenticationChallenge) { - self.authenticationChallenge(connection, challenge); - return; - } - - if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { - if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) { - NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; - [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; - } else { - [[challenge sender] cancelAuthenticationChallenge:challenge]; - } - } else { - if ([challenge previousFailureCount] == 0) { - if (self.credential) { - [[challenge sender] useCredential:self.credential forAuthenticationChallenge:challenge]; - } else { - [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; - } - } else { - [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; - } - } -} - -- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection __unused *)connection { - return self.shouldUseCredentialStorage; -} - -- (NSURLRequest *)connection:(NSURLConnection *)connection - willSendRequest:(NSURLRequest *)request - redirectResponse:(NSURLResponse *)redirectResponse -{ - if (self.redirectResponse) { - return self.redirectResponse(connection, request, redirectResponse); - } else { - return request; - } -} - -- (void)connection:(NSURLConnection __unused *)connection - didSendBodyData:(NSInteger)bytesWritten - totalBytesWritten:(NSInteger)totalBytesWritten -totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite -{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.uploadProgress) { - self.uploadProgress((NSUInteger)bytesWritten, totalBytesWritten, totalBytesExpectedToWrite); - } - }); -} - -- (void)connection:(NSURLConnection __unused *)connection -didReceiveResponse:(NSURLResponse *)response -{ - self.response = response; -} - -- (void)connection:(NSURLConnection __unused *)connection - didReceiveData:(NSData *)data -{ - NSUInteger length = [data length]; - while (YES) { - NSInteger totalNumberOfBytesWritten = 0; - if ([self.outputStream hasSpaceAvailable]) { - const uint8_t *dataBuffer = (uint8_t *)[data bytes]; - - NSInteger numberOfBytesWritten = 0; - while (totalNumberOfBytesWritten < (NSInteger)length) { - numberOfBytesWritten = [self.outputStream write:&dataBuffer[(NSUInteger)totalNumberOfBytesWritten] maxLength:(length - (NSUInteger)totalNumberOfBytesWritten)]; - if (numberOfBytesWritten == -1) { - break; - } - - totalNumberOfBytesWritten += numberOfBytesWritten; - } - - break; - } - - if (self.outputStream.streamError) { - [self.connection cancel]; - [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:self.outputStream.streamError]; - return; - } - } - - dispatch_async(dispatch_get_main_queue(), ^{ - self.totalBytesRead += (long long)length; - - if (self.downloadProgress) { - self.downloadProgress(length, self.totalBytesRead, self.response.expectedContentLength); - } - }); -} - -- (void)connectionDidFinishLoading:(NSURLConnection __unused *)connection { - self.responseData = [self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; - - [self.outputStream close]; - if (self.responseData) { - self.outputStream = nil; - } - - self.connection = nil; - - [self finish]; -} - -- (void)connection:(NSURLConnection __unused *)connection - didFailWithError:(NSError *)error -{ - self.error = error; - - [self.outputStream close]; - if (self.responseData) { - self.outputStream = nil; - } - - self.connection = nil; - - [self finish]; -} - -- (NSCachedURLResponse *)connection:(NSURLConnection *)connection - willCacheResponse:(NSCachedURLResponse *)cachedResponse -{ - if (self.cacheResponse) { - return self.cacheResponse(connection, cachedResponse); - } else { - if ([self isCancelled]) { - return nil; - } - - return cachedResponse; - } -} - -#pragma mark - NSSecureCoding - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (id)initWithCoder:(NSCoder *)decoder { - NSURLRequest *request = [decoder decodeObjectOfClass:[NSURLRequest class] forKey:NSStringFromSelector(@selector(request))]; - - self = [self initWithRequest:request]; - if (!self) { - return nil; - } - - self.state = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(state))] integerValue]; - self.response = [decoder decodeObjectOfClass:[NSHTTPURLResponse class] forKey:NSStringFromSelector(@selector(response))]; - self.error = [decoder decodeObjectOfClass:[NSError class] forKey:NSStringFromSelector(@selector(error))]; - self.responseData = [decoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(responseData))]; - self.totalBytesRead = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(totalBytesRead))] longLongValue]; - - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder { - [self pause]; - - [coder encodeObject:self.request forKey:NSStringFromSelector(@selector(request))]; - - switch (self.state) { - case AFOperationExecutingState: - case AFOperationPausedState: - [coder encodeInteger:AFOperationReadyState forKey:NSStringFromSelector(@selector(state))]; - break; - default: - [coder encodeInteger:self.state forKey:NSStringFromSelector(@selector(state))]; - break; - } - - [coder encodeObject:self.response forKey:NSStringFromSelector(@selector(response))]; - [coder encodeObject:self.error forKey:NSStringFromSelector(@selector(error))]; - [coder encodeObject:self.responseData forKey:NSStringFromSelector(@selector(responseData))]; - [coder encodeInt64:self.totalBytesRead forKey:NSStringFromSelector(@selector(totalBytesRead))]; -} - -#pragma mark - NSCopying - -- (id)copyWithZone:(NSZone *)zone { - AFURLConnectionOperation *operation = [(AFURLConnectionOperation *)[[self class] allocWithZone:zone] initWithRequest:self.request]; - - operation.uploadProgress = self.uploadProgress; - operation.downloadProgress = self.downloadProgress; - operation.authenticationChallenge = self.authenticationChallenge; - operation.cacheResponse = self.cacheResponse; - operation.redirectResponse = self.redirectResponse; - operation.completionQueue = self.completionQueue; - operation.completionGroup = self.completionGroup; - - return operation; -} - -@end diff --git a/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.h b/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.h index 513334b4b..694696b9a 100755 --- a/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.h +++ b/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.h @@ -1,5 +1,5 @@ // AFURLRequestSerialization.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -20,7 +20,9 @@ // THE SOFTWARE. #import -#if TARGET_OS_IOS +#import + +#if TARGET_OS_IOS || TARGET_OS_TV #import #elif TARGET_OS_WATCH #import @@ -28,6 +30,31 @@ NS_ASSUME_NONNULL_BEGIN +/** + Returns a percent-escaped string following RFC 3986 for a query string key or value. + RFC 3986 states that the following characters are "reserved" characters. + - General Delimiters: ":", "#", "[", "]", "@", "?", "/" + - Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "=" + + In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow + query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/" + should be percent-escaped in the query string. + + @param string The string to be percent-escaped. + + @return The percent-escaped string. + */ +FOUNDATION_EXPORT NSString * AFPercentEscapedStringFromString(NSString *string); + +/** + A helper method to generate encoded url query parameters for appending to the end of a URL. + + @param parameters A dictionary of key/values to be encoded. + + @return A url encoded query string + */ +FOUNDATION_EXPORT NSString * AFQueryStringFromParameters(NSDictionary *parameters); + /** The `AFURLRequestSerialization` protocol is adopted by an object that encodes parameters for a specified HTTP requests. Request serializers may encode parameters as query strings, HTTP bodies, setting the appropriate HTTP header fields as necessary. @@ -46,7 +73,7 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request withParameters:(nullable id)parameters - error:(NSError * __nullable __autoreleasing *)error; + error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW; @end @@ -127,7 +154,7 @@ typedef NS_ENUM(NSUInteger, AFHTTPRequestQueryStringSerializationStyle) { @discussion To add or remove default request headers, use `setValue:forHTTPHeaderField:`. */ -@property (readonly, nonatomic, strong) NSDictionary *HTTPRequestHeaders; +@property (readonly, nonatomic, strong) NSDictionary *HTTPRequestHeaders; /** Creates and returns a serializer with default configuration. @@ -161,12 +188,6 @@ forHTTPHeaderField:(NSString *)field; - (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username password:(NSString *)password; -/** - @deprecated This method has been deprecated. Use -setValue:forHTTPHeaderField: instead. - */ -- (void)setAuthorizationHeaderFieldWithToken:(NSString *)token DEPRECATED_ATTRIBUTE; - - /** Clears any existing value for the "Authorization" HTTP header. */ @@ -179,7 +200,7 @@ forHTTPHeaderField:(NSString *)field; /** HTTP methods for which serialized requests will encode parameters as a query string. `GET`, `HEAD`, and `DELETE` by default. */ -@property (nonatomic, strong) NSSet *HTTPMethodsEncodingParametersInURI; +@property (nonatomic, strong) NSSet *HTTPMethodsEncodingParametersInURI; /** Set the method of query string serialization according to one of the pre-defined styles. @@ -201,13 +222,6 @@ forHTTPHeaderField:(NSString *)field; /// @name Creating Request Objects ///------------------------------- -/** - @deprecated This method has been deprecated. Use -requestWithMethod:URLString:parameters:error: instead. - */ -- (NSMutableURLRequest *)requestWithMethod:(NSString *)method - URLString:(NSString *)URLString - parameters:(id)parameters DEPRECATED_ATTRIBUTE; - /** Creates an `NSMutableURLRequest` object with the specified HTTP method and URL string. @@ -223,15 +237,7 @@ forHTTPHeaderField:(NSString *)field; - (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(nullable id)parameters - error:(NSError * __nullable __autoreleasing *)error; - -/** - @deprecated This method has been deprecated. Use -multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error: instead. - */ -- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method - URLString:(NSString *)URLString - parameters:(NSDictionary *)parameters - constructingBodyWithBlock:(void (^)(id formData))block DEPRECATED_ATTRIBUTE; + error:(NSError * _Nullable __autoreleasing *)error; /** Creates an `NSMutableURLRequest` object with the specified HTTP method and URLString, and constructs a `multipart/form-data` HTTP body, using the specified parameters and multipart form data block. See http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2 @@ -248,9 +254,9 @@ forHTTPHeaderField:(NSString *)field; */ - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method URLString:(NSString *)URLString - parameters:(nullable NSDictionary *)parameters + parameters:(nullable NSDictionary *)parameters constructingBodyWithBlock:(nullable void (^)(id formData))block - error:(NSError * __nullable __autoreleasing *)error; + error:(NSError * _Nullable __autoreleasing *)error; /** Creates an `NSMutableURLRequest` by removing the `HTTPBodyStream` from a request, and asynchronously writing its contents into the specified file, invoking the completion handler when finished. @@ -265,7 +271,7 @@ forHTTPHeaderField:(NSString *)field; */ - (NSMutableURLRequest *)requestWithMultipartFormRequest:(NSURLRequest *)request writingStreamContentsToFile:(NSURL *)fileURL - completionHandler:(nullable void (^)(NSError * __nullable error))handler; + completionHandler:(nullable void (^)(NSError * _Nullable error))handler; @end @@ -289,7 +295,7 @@ forHTTPHeaderField:(NSString *)field; */ - (BOOL)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name - error:(NSError * __nullable __autoreleasing *)error; + error:(NSError * _Nullable __autoreleasing *)error; /** Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary. @@ -306,7 +312,7 @@ forHTTPHeaderField:(NSString *)field; name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType - error:(NSError * __nullable __autoreleasing *)error; + error:(NSError * _Nullable __autoreleasing *)error; /** Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the data from the input stream and the multipart form boundary. @@ -353,7 +359,7 @@ forHTTPHeaderField:(NSString *)field; @param headers The HTTP headers to be appended to the form data. @param body The data to be encoded and appended to the form data. This parameter must not be `nil`. */ -- (void)appendPartWithHeaders:(nullable NSDictionary *)headers +- (void)appendPartWithHeaders:(nullable NSDictionary *)headers body:(NSData *)body; /** @@ -438,7 +444,7 @@ forHTTPHeaderField:(NSString *)field; `AFURLRequestSerializationErrorDomain` AFURLRequestSerializer errors. Error codes for `AFURLRequestSerializationErrorDomain` correspond to codes in `NSURLErrorDomain`. */ -extern NSString * const AFURLRequestSerializationErrorDomain; +FOUNDATION_EXPORT NSString * const AFURLRequestSerializationErrorDomain; /** ## User info dictionary keys @@ -452,7 +458,7 @@ extern NSString * const AFURLRequestSerializationErrorDomain; `AFNetworkingOperationFailingURLRequestErrorKey` The corresponding value is an `NSURLRequest` containing the request of the operation associated with an error. This key is only present in the `AFURLRequestSerializationErrorDomain`. */ -extern NSString * const AFNetworkingOperationFailingURLRequestErrorKey; +FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLRequestErrorKey; /** ## Throttling Bandwidth for HTTP Request Input Streams @@ -467,7 +473,7 @@ extern NSString * const AFNetworkingOperationFailingURLRequestErrorKey; `kAFUploadStream3GSuggestedDelay` Duration of delay each time a packet is read. Equal to 0.2 seconds. */ -extern NSUInteger const kAFUploadStream3GSuggestedPacketSize; -extern NSTimeInterval const kAFUploadStream3GSuggestedDelay; +FOUNDATION_EXPORT NSUInteger const kAFUploadStream3GSuggestedPacketSize; +FOUNDATION_EXPORT NSTimeInterval const kAFUploadStream3GSuggestedDelay; NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.m b/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.m index 84317616f..68c2c2d7b 100755 --- a/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.m +++ b/clients/ios/Other Sources/AFNetworking/AFURLRequestSerialization.m @@ -1,5 +1,5 @@ // AFURLRequestSerialization.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,7 +21,7 @@ #import "AFURLRequestSerialization.h" -#if __IPHONE_OS_VERSION_MIN_REQUIRED +#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV #import #else #import @@ -32,35 +32,6 @@ NSString * const AFNetworkingOperationFailingURLRequestErrorKey = @"com.alamofir typedef NSString * (^AFQueryStringSerializationBlock)(NSURLRequest *request, id parameters, NSError *__autoreleasing *error); -static NSString * AFBase64EncodedStringFromString(NSString *string) { - NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; - NSUInteger length = [data length]; - NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; - - uint8_t *input = (uint8_t *)[data bytes]; - uint8_t *output = (uint8_t *)[mutableData mutableBytes]; - - for (NSUInteger i = 0; i < length; i += 3) { - NSUInteger value = 0; - for (NSUInteger j = i; j < (i + 3); j++) { - value <<= 8; - if (j < length) { - value |= (0xFF & input[j]); - } - } - - static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - NSUInteger idx = (i / 3) * 4; - output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F]; - output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F]; - output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '='; - output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '='; - } - - return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding]; -} - /** Returns a percent-escaped string following RFC 3986 for a query string key or value. RFC 3986 states that the following characters are "reserved" characters. @@ -73,14 +44,36 @@ static NSString * AFBase64EncodedStringFromString(NSString *string) { - parameter string: The string to be percent-escaped. - returns: The percent-escaped string. */ -static NSString * AFPercentEscapedStringFromString(NSString *string) { +NSString * AFPercentEscapedStringFromString(NSString *string) { static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4 static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;="; NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy]; [allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]]; - return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet]; + // FIXME: https://github.com/AFNetworking/AFNetworking/pull/3028 + // return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet]; + + static NSUInteger const batchSize = 50; + + NSUInteger index = 0; + NSMutableString *escaped = @"".mutableCopy; + + while (index < string.length) { + NSUInteger length = MIN(string.length - index, batchSize); + NSRange range = NSMakeRange(index, length); + + // To avoid breaking up character sequences such as 👴🏻👮🏽 + range = [string rangeOfComposedCharacterSequencesForRange:range]; + + NSString *substring = [string substringWithRange:range]; + NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet]; + [escaped appendString:encoded]; + + index += range.length; + } + + return escaped; } #pragma mark - @@ -89,14 +82,14 @@ static NSString * AFPercentEscapedStringFromString(NSString *string) { @property (readwrite, nonatomic, strong) id field; @property (readwrite, nonatomic, strong) id value; -- (id)initWithField:(id)field value:(id)value; +- (instancetype)initWithField:(id)field value:(id)value; - (NSString *)URLEncodedStringValue; @end @implementation AFQueryStringPair -- (id)initWithField:(id)field value:(id)value { +- (instancetype)initWithField:(id)field value:(id)value { self = [super init]; if (!self) { return nil; @@ -120,10 +113,10 @@ static NSString * AFPercentEscapedStringFromString(NSString *string) { #pragma mark - -extern NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary); -extern NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value); +FOUNDATION_EXPORT NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary); +FOUNDATION_EXPORT NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value); -static NSString * AFQueryStringFromParameters(NSDictionary *parameters) { +NSString * AFQueryStringFromParameters(NSDictionary *parameters) { NSMutableArray *mutablePairs = [NSMutableArray array]; for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) { [mutablePairs addObject:[pair URLEncodedStringValue]]; @@ -193,6 +186,7 @@ static void *AFHTTPRequestSerializerObserverContext = &AFHTTPRequestSerializerOb @interface AFHTTPRequestSerializer () @property (readwrite, nonatomic, strong) NSMutableSet *mutableObservedChangedKeyPaths; @property (readwrite, nonatomic, strong) NSMutableDictionary *mutableHTTPRequestHeaders; +@property (readwrite, nonatomic, strong) dispatch_queue_t requestHeaderModificationQueue; @property (readwrite, nonatomic, assign) AFHTTPRequestQueryStringSerializationStyle queryStringSerializationStyle; @property (readwrite, nonatomic, copy) AFQueryStringSerializationBlock queryStringSerialization; @end @@ -212,6 +206,7 @@ static void *AFHTTPRequestSerializerObserverContext = &AFHTTPRequestSerializerOb self.stringEncoding = NSUTF8StringEncoding; self.mutableHTTPRequestHeaders = [NSMutableDictionary dictionary]; + self.requestHeaderModificationQueue = dispatch_queue_create("requestHeaderModificationQueue", DISPATCH_QUEUE_CONCURRENT); // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 NSMutableArray *acceptLanguagesComponents = [NSMutableArray array]; @@ -223,8 +218,6 @@ static void *AFHTTPRequestSerializerObserverContext = &AFHTTPRequestSerializerOb [self setValue:[acceptLanguagesComponents componentsJoinedByString:@", "] forHTTPHeaderField:@"Accept-Language"]; NSString *userAgent = nil; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" #if TARGET_OS_IOS // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43 userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]]; @@ -234,7 +227,6 @@ static void *AFHTTPRequestSerializerObserverContext = &AFHTTPRequestSerializerOb #elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]]; #endif -#pragma clang diagnostic pop if (userAgent) { if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) { NSMutableString *mutableUserAgent = [userAgent mutableCopy]; @@ -310,32 +302,41 @@ static void *AFHTTPRequestSerializerObserverContext = &AFHTTPRequestSerializerOb #pragma mark - - (NSDictionary *)HTTPRequestHeaders { - return [NSDictionary dictionaryWithDictionary:self.mutableHTTPRequestHeaders]; + NSDictionary __block *value; + dispatch_sync(self.requestHeaderModificationQueue, ^{ + value = [NSDictionary dictionaryWithDictionary:self.mutableHTTPRequestHeaders]; + }); + return value; } - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field { - [self.mutableHTTPRequestHeaders setValue:value forKey:field]; + dispatch_barrier_async(self.requestHeaderModificationQueue, ^{ + [self.mutableHTTPRequestHeaders setValue:value forKey:field]; + }); } - (NSString *)valueForHTTPHeaderField:(NSString *)field { - return [self.mutableHTTPRequestHeaders valueForKey:field]; + NSString __block *value; + dispatch_sync(self.requestHeaderModificationQueue, ^{ + value = [self.mutableHTTPRequestHeaders valueForKey:field]; + }); + return value; } - (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username password:(NSString *)password { - NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password]; - [self setValue:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)] forHTTPHeaderField:@"Authorization"]; -} - -- (void)setAuthorizationHeaderFieldWithToken:(NSString *)token { - [self setValue:[NSString stringWithFormat:@"Token token=\"%@\"", token] forHTTPHeaderField:@"Authorization"]; + NSData *basicAuthCredentials = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding]; + NSString *base64AuthCredentials = [basicAuthCredentials base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0]; + [self setValue:[NSString stringWithFormat:@"Basic %@", base64AuthCredentials] forHTTPHeaderField:@"Authorization"]; } - (void)clearAuthorizationHeader { - [self.mutableHTTPRequestHeaders removeObjectForKey:@"Authorization"]; + dispatch_barrier_async(self.requestHeaderModificationQueue, ^{ + [self.mutableHTTPRequestHeaders removeObjectForKey:@"Authorization"]; + }); } #pragma mark - @@ -351,13 +352,6 @@ forHTTPHeaderField:(NSString *)field #pragma mark - -- (NSMutableURLRequest *)requestWithMethod:(NSString *)method - URLString:(NSString *)URLString - parameters:(id)parameters -{ - return [self requestWithMethod:method URLString:URLString parameters:parameters error:nil]; -} - - (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters @@ -384,14 +378,6 @@ forHTTPHeaderField:(NSString *)field return mutableRequest; } -- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method - URLString:(NSString *)URLString - parameters:(NSDictionary *)parameters - constructingBodyWithBlock:(void (^)(id formData))block -{ - return [self multipartFormRequestWithMethod:method URLString:URLString parameters:parameters constructingBodyWithBlock:block error:nil]; -} - - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(NSDictionary *)parameters @@ -499,8 +485,8 @@ forHTTPHeaderField:(NSString *)field } }]; + NSString *query = nil; if (parameters) { - NSString *query = nil; if (self.queryStringSerialization) { NSError *serializationError; query = self.queryStringSerialization(request, parameters, &serializationError); @@ -519,15 +505,21 @@ forHTTPHeaderField:(NSString *)field break; } } + } - if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { + if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { + if (query && query.length > 0) { mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query]]; - } else { - if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) { - [mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; - } - [mutableRequest setHTTPBody:[query dataUsingEncoding:self.stringEncoding]]; } + } else { + // #2864: an empty string is a valid x-www-form-urlencoded payload + if (!query) { + query = @""; + } + if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) { + [mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; + } + [mutableRequest setHTTPBody:[query dataUsingEncoding:self.stringEncoding]]; } return mutableRequest; @@ -563,28 +555,32 @@ forHTTPHeaderField:(NSString *)field return YES; } -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [self init]; if (!self) { return nil; } self.mutableHTTPRequestHeaders = [[decoder decodeObjectOfClass:[NSDictionary class] forKey:NSStringFromSelector(@selector(mutableHTTPRequestHeaders))] mutableCopy]; - self.queryStringSerializationStyle = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(queryStringSerializationStyle))] unsignedIntegerValue]; + self.queryStringSerializationStyle = (AFHTTPRequestQueryStringSerializationStyle)[[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(queryStringSerializationStyle))] unsignedIntegerValue]; return self; } - (void)encodeWithCoder:(NSCoder *)coder { - [coder encodeObject:self.mutableHTTPRequestHeaders forKey:NSStringFromSelector(@selector(mutableHTTPRequestHeaders))]; + dispatch_sync(self.requestHeaderModificationQueue, ^{ + [coder encodeObject:self.mutableHTTPRequestHeaders forKey:NSStringFromSelector(@selector(mutableHTTPRequestHeaders))]; + }); [coder encodeInteger:self.queryStringSerializationStyle forKey:NSStringFromSelector(@selector(queryStringSerializationStyle))]; } #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFHTTPRequestSerializer *serializer = [[[self class] allocWithZone:zone] init]; - serializer.mutableHTTPRequestHeaders = [self.mutableHTTPRequestHeaders mutableCopyWithZone:zone]; + dispatch_sync(self.requestHeaderModificationQueue, ^{ + serializer.mutableHTTPRequestHeaders = [self.mutableHTTPRequestHeaders mutableCopyWithZone:zone]; + }); serializer.queryStringSerializationStyle = self.queryStringSerializationStyle; serializer.queryStringSerialization = self.queryStringSerialization; @@ -614,7 +610,6 @@ static inline NSString * AFMultipartFormFinalBoundary(NSString *boundary) { } static inline NSString * AFContentTypeForPathExtension(NSString *extension) { -#ifdef __UTTYPE__ NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL); NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType); if (!contentType) { @@ -622,10 +617,6 @@ static inline NSString * AFContentTypeForPathExtension(NSString *extension) { } else { return contentType; } -#else -#pragma unused (extension) - return @"application/octet-stream"; -#endif } NSUInteger const kAFUploadStream3GSuggestedPacketSize = 1024 * 16; @@ -656,7 +647,7 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; @property (readonly, nonatomic, assign) unsigned long long contentLength; @property (readonly, nonatomic, assign, getter = isEmpty) BOOL empty; -- (id)initWithStringEncoding:(NSStringEncoding)encoding; +- (instancetype)initWithStringEncoding:(NSStringEncoding)encoding; - (void)setInitialAndFinalBoundaries; - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart; @end @@ -672,8 +663,8 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; @implementation AFStreamingMultipartFormData -- (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest - stringEncoding:(NSStringEncoding)encoding +- (instancetype)initWithURLRequest:(NSMutableURLRequest *)urlRequest + stringEncoding:(NSStringEncoding)encoding { self = [super init]; if (!self) { @@ -856,16 +847,13 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; @end @implementation AFMultipartBodyStream -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wimplicit-atomic-properties" #if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1100) @synthesize delegate; #endif @synthesize streamStatus; @synthesize streamError; -#pragma clang diagnostic pop -- (id)initWithStringEncoding:(NSStringEncoding)encoding { +- (instancetype)initWithStringEncoding:(NSStringEncoding)encoding { self = [super init]; if (!self) { return nil; @@ -909,15 +897,13 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; NSInteger totalNumberOfBytesRead = 0; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" while ((NSUInteger)totalNumberOfBytesRead < MIN(length, self.numberOfBytesInPacket)) { if (!self.currentHTTPBodyPart || ![self.currentHTTPBodyPart hasBytesAvailable]) { if (!(self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject])) { break; } } else { - NSUInteger maxLength = length - (NSUInteger)totalNumberOfBytesRead; + NSUInteger maxLength = MIN(length, self.numberOfBytesInPacket) - (NSUInteger)totalNumberOfBytesRead; NSInteger numberOfBytesRead = [self.currentHTTPBodyPart read:&buffer[totalNumberOfBytesRead] maxLength:maxLength]; if (numberOfBytesRead == -1) { self.streamError = self.currentHTTPBodyPart.inputStream.streamError; @@ -931,7 +917,6 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; } } } -#pragma clang diagnostic pop return totalNumberOfBytesRead; } @@ -1008,7 +993,7 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; #pragma mark - NSCopying --(id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFMultipartBodyStream *bodyStreamCopy = [[[self class] allocWithZone:zone] initWithStringEncoding:self.stringEncoding]; for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) { @@ -1045,7 +1030,7 @@ typedef enum { @implementation AFHTTPBodyPart -- (id)init { +- (instancetype)init { self = [super init]; if (!self) { return nil; @@ -1112,8 +1097,6 @@ typedef enum { return YES; } -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wcovered-switch-default" switch (self.inputStream.streamStatus) { case NSStreamStatusNotOpen: case NSStreamStatusOpening: @@ -1127,7 +1110,6 @@ typedef enum { default: return NO; } -#pragma clang diagnostic pop } - (NSInteger)read:(uint8_t *)buffer @@ -1172,11 +1154,8 @@ typedef enum { intoBuffer:(uint8_t *)buffer maxLength:(NSUInteger)length { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" NSRange range = NSMakeRange((NSUInteger)_phaseReadOffset, MIN([data length] - ((NSUInteger)_phaseReadOffset), length)); [data getBytes:buffer range:range]; -#pragma clang diagnostic pop _phaseReadOffset += range.length; @@ -1195,8 +1174,6 @@ typedef enum { return YES; } -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wcovered-switch-default" switch (_phase) { case AFEncapsulationBoundaryPhase: _phase = AFHeaderPhase; @@ -1216,14 +1193,13 @@ typedef enum { break; } _phaseReadOffset = 0; -#pragma clang diagnostic pop return YES; } #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFHTTPBodyPart *bodyPart = [[[self class] allocWithZone:zone] init]; bodyPart.stringEncoding = self.stringEncoding; @@ -1278,7 +1254,21 @@ typedef enum { [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; } - [mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]]; + if (![NSJSONSerialization isValidJSONObject:parameters]) { + if (error) { + NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedStringFromTable(@"The `parameters` argument is not valid JSON.", @"AFNetworking", nil)}; + *error = [[NSError alloc] initWithDomain:AFURLRequestSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo]; + } + return nil; + } + + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]; + + if (!jsonData) { + return nil; + } + + [mutableRequest setHTTPBody:jsonData]; } return mutableRequest; @@ -1286,7 +1276,7 @@ typedef enum { #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; @@ -1305,7 +1295,7 @@ typedef enum { #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFJSONRequestSerializer *serializer = [super copyWithZone:zone]; serializer.writingOptions = self.writingOptions; @@ -1357,7 +1347,13 @@ typedef enum { [mutableRequest setValue:@"application/x-plist" forHTTPHeaderField:@"Content-Type"]; } - [mutableRequest setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:self.format options:self.writeOptions error:error]]; + NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:parameters format:self.format options:self.writeOptions error:error]; + + if (!plistData) { + return nil; + } + + [mutableRequest setHTTPBody:plistData]; } return mutableRequest; @@ -1365,13 +1361,13 @@ typedef enum { #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; } - self.format = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(format))] unsignedIntegerValue]; + self.format = (NSPropertyListFormat)[[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(format))] unsignedIntegerValue]; self.writeOptions = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(writeOptions))] unsignedIntegerValue]; return self; @@ -1386,7 +1382,7 @@ typedef enum { #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFPropertyListRequestSerializer *serializer = [super copyWithZone:zone]; serializer.format = self.format; serializer.writeOptions = self.writeOptions; diff --git a/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.h b/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.h index e14dc8a9d..0f6d462b7 100755 --- a/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.h +++ b/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.h @@ -1,5 +1,5 @@ // AFURLResponseSerialization.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -42,7 +42,7 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable id)responseObjectForResponse:(nullable NSURLResponse *)response data:(nullable NSData *)data - error:(NSError * __nullable __autoreleasing *)error; + error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW; @end @@ -57,10 +57,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init; -/** - The string encoding used to serialize data received from the server, when no string encoding is specified by the response. `NSUTF8StringEncoding` by default. - */ -@property (nonatomic, assign) NSStringEncoding stringEncoding; +@property (nonatomic, assign) NSStringEncoding stringEncoding DEPRECATED_MSG_ATTRIBUTE("The string encoding is never used. AFHTTPResponseSerializer only validates status codes and content types but does not try to decode the received data in any way."); /** Creates and returns a serializer with default configuration. @@ -81,7 +78,7 @@ NS_ASSUME_NONNULL_BEGIN /** The acceptable MIME types for responses. When non-`nil`, responses with a `Content-Type` with MIME types that do not intersect with the set will result in an error during validation. */ -@property (nonatomic, copy, nullable) NSSet *acceptableContentTypes; +@property (nonatomic, copy, nullable) NSSet *acceptableContentTypes; /** Validates the specified response and data. @@ -96,7 +93,7 @@ NS_ASSUME_NONNULL_BEGIN */ - (BOOL)validateResponse:(nullable NSHTTPURLResponse *)response data:(nullable NSData *)data - error:(NSError * __nullable __autoreleasing *)error; + error:(NSError * _Nullable __autoreleasing *)error; @end @@ -235,7 +232,7 @@ NS_ASSUME_NONNULL_BEGIN */ @interface AFImageResponseSerializer : AFHTTPResponseSerializer -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH /** The scale factor used when interpreting the image data to construct `responseImage`. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property. This is set to the value of scale of the main screen by default, which automatically scales images for retina displays, for instance. */ @@ -259,14 +256,14 @@ NS_ASSUME_NONNULL_BEGIN /** The component response serializers. */ -@property (readonly, nonatomic, copy) NSArray *responseSerializers; +@property (readonly, nonatomic, copy) NSArray > *responseSerializers; /** Creates and returns a compound serializer comprised of the specified response serializers. @warning Each response serializer specified must be a subclass of `AFHTTPResponseSerializer`, and response to `-validateResponse:data:error:`. */ -+ (instancetype)compoundSerializerWithResponseSerializers:(NSArray *)responseSerializers; ++ (instancetype)compoundSerializerWithResponseSerializers:(NSArray > *)responseSerializers; @end @@ -286,7 +283,7 @@ NS_ASSUME_NONNULL_BEGIN `AFURLResponseSerializationErrorDomain` AFURLResponseSerializer errors. Error codes for `AFURLResponseSerializationErrorDomain` correspond to codes in `NSURLErrorDomain`. */ -extern NSString * const AFURLResponseSerializationErrorDomain; +FOUNDATION_EXPORT NSString * const AFURLResponseSerializationErrorDomain; /** ## User info dictionary keys @@ -304,8 +301,8 @@ extern NSString * const AFURLResponseSerializationErrorDomain; `AFNetworkingOperationFailingURLResponseDataErrorKey` The corresponding value is an `NSData` containing the original data of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`. */ -extern NSString * const AFNetworkingOperationFailingURLResponseErrorKey; +FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseErrorKey; -extern NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey; +FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey; NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.m b/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.m index f95834f1d..b7be3d403 100755 --- a/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.m +++ b/clients/ios/Other Sources/AFNetworking/AFURLResponseSerialization.m @@ -1,5 +1,5 @@ // AFURLResponseSerialization.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,6 +21,8 @@ #import "AFURLResponseSerialization.h" +#import + #if TARGET_OS_IOS #import #elif TARGET_OS_WATCH @@ -95,8 +97,6 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO return nil; } - self.stringEncoding = NSUTF8StringEncoding; - self.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)]; self.acceptableContentTypes = nil; @@ -113,7 +113,9 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO NSError *validationError = nil; if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) { - if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]]) { + if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]] && + !([response MIMEType] == nil && [data length] == 0)) { + if ([data length] > 0 && [response URL]) { NSMutableDictionary *mutableUserInfo = [@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]], @@ -171,7 +173,7 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO return YES; } -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [self init]; if (!self) { return nil; @@ -190,7 +192,7 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { AFHTTPResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; serializer.acceptableStatusCodes = [self.acceptableStatusCodes copyWithZone:zone]; serializer.acceptableContentTypes = [self.acceptableContentTypes copyWithZone:zone]; @@ -240,46 +242,26 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO // Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization. // See https://github.com/rails/rails/issues/1742 - NSStringEncoding stringEncoding = self.stringEncoding; - if (response.textEncodingName) { - CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName); - if (encoding != kCFStringEncodingInvalidId) { - stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding); - } + BOOL isSpace = [data isEqualToData:[NSData dataWithBytes:" " length:1]]; + + if (data.length == 0 || isSpace) { + return nil; } - - id responseObject = nil; + NSError *serializationError = nil; - @autoreleasepool { - NSString *responseString = [[NSString alloc] initWithData:data encoding:stringEncoding]; - if (responseString && ![responseString isEqualToString:@" "]) { - // Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character - // See http://stackoverflow.com/a/12843465/157142 - data = [responseString dataUsingEncoding:NSUTF8StringEncoding]; + + id responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError]; - if (data) { - if ([data length] > 0) { - responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError]; - } else { - return nil; - } - } else { - NSDictionary *userInfo = @{ - NSLocalizedDescriptionKey: NSLocalizedStringFromTable(@"Data failed decoding as a UTF-8 string", @"AFNetworking", nil), - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Could not decode string: %@", @"AFNetworking", nil), responseString] - }; - - serializationError = [NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo]; - } + if (!responseObject) + { + if (error) { + *error = AFErrorWithUnderlyingError(serializationError, *error); } + return nil; } - - if (self.removesKeysWithNullValues && responseObject) { - responseObject = AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions); - } - - if (error) { - *error = AFErrorWithUnderlyingError(serializationError, *error); + + if (self.removesKeysWithNullValues) { + return AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions); } return responseObject; @@ -287,7 +269,7 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; @@ -308,8 +290,8 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { - AFJSONResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; +- (instancetype)copyWithZone:(NSZone *)zone { + AFJSONResponseSerializer *serializer = [super copyWithZone:zone]; serializer.readingOptions = self.readingOptions; serializer.removesKeysWithNullValues = self.removesKeysWithNullValues; @@ -399,16 +381,20 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO NSError *serializationError = nil; NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:self.options error:&serializationError]; - if (error) { - *error = AFErrorWithUnderlyingError(serializationError, *error); + if (!document) + { + if (error) { + *error = AFErrorWithUnderlyingError(serializationError, *error); + } + return nil; } - + return document; } #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; @@ -427,8 +413,8 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { - AFXMLDocumentResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; +- (instancetype)copyWithZone:(NSZone *)zone { + AFXMLDocumentResponseSerializer *serializer = [super copyWithZone:zone]; serializer.options = self.options; return serializer; @@ -479,15 +465,20 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO } } - id responseObject; - NSError *serializationError = nil; - - if (data) { - responseObject = [NSPropertyListSerialization propertyListWithData:data options:self.readOptions format:NULL error:&serializationError]; + if (!data) { + return nil; } - - if (error) { - *error = AFErrorWithUnderlyingError(serializationError, *error); + + NSError *serializationError = nil; + + id responseObject = [NSPropertyListSerialization propertyListWithData:data options:self.readOptions format:NULL error:&serializationError]; + + if (!responseObject) + { + if (error) { + *error = AFErrorWithUnderlyingError(serializationError, *error); + } + return nil; } return responseObject; @@ -495,13 +486,13 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; } - self.format = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(format))] unsignedIntegerValue]; + self.format = (NSPropertyListFormat)[[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(format))] unsignedIntegerValue]; self.readOptions = [[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(readOptions))] unsignedIntegerValue]; return self; @@ -516,8 +507,8 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { - AFPropertyListResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; +- (instancetype)copyWithZone:(NSZone *)zone { + AFPropertyListResponseSerializer *serializer = [super copyWithZone:zone]; serializer.format = self.format; serializer.readOptions = self.readOptions; @@ -528,8 +519,9 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO #pragma mark - -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH #import +#import @interface UIImage (AFNetworkingSafeImageLoading) + (UIImage *)af_safeImageWithData:(NSData *)data; @@ -667,10 +659,10 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil]; -#if TARGET_OS_IOS +#if TARGET_OS_IOS || TARGET_OS_TV self.imageScale = [[UIScreen mainScreen] scale]; self.automaticallyInflatesResponseImage = YES; -#elif TARGET_OS_WATCH +#elif TARGET_OS_WATCH self.imageScale = [[WKInterfaceDevice currentDevice] screenScale]; self.automaticallyInflatesResponseImage = YES; #endif @@ -690,13 +682,13 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r } } -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH if (self.automaticallyInflatesResponseImage) { return AFInflatedImageFromResponseWithDataAtScale((NSHTTPURLResponse *)response, data, self.imageScale); } else { return AFImageWithDataAtScale(data, self.imageScale); } -#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) +#else // Ensure that the image is set to it's correct pixel width and height NSBitmapImageRep *bitimage = [[NSBitmapImageRep alloc] initWithData:data]; NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize([bitimage pixelsWide], [bitimage pixelsHigh])]; @@ -710,13 +702,13 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; } -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH NSNumber *imageScale = [decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(imageScale))]; #if CGFLOAT_IS_DOUBLE self.imageScale = [imageScale doubleValue]; @@ -733,7 +725,7 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r - (void)encodeWithCoder:(NSCoder *)coder { [super encodeWithCoder:coder]; -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH [coder encodeObject:@(self.imageScale) forKey:NSStringFromSelector(@selector(imageScale))]; [coder encodeBool:self.automaticallyInflatesResponseImage forKey:NSStringFromSelector(@selector(automaticallyInflatesResponseImage))]; #endif @@ -741,10 +733,10 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { - AFImageResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; +- (instancetype)copyWithZone:(NSZone *)zone { + AFImageResponseSerializer *serializer = [super copyWithZone:zone]; -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH serializer.imageScale = self.imageScale; serializer.automaticallyInflatesResponseImage = self.automaticallyInflatesResponseImage; #endif @@ -796,7 +788,7 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r #pragma mark - NSSecureCoding -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (!self) { return nil; @@ -815,8 +807,8 @@ static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *r #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { - AFCompoundResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; +- (instancetype)copyWithZone:(NSZone *)zone { + AFCompoundResponseSerializer *serializer = [super copyWithZone:zone]; serializer.responseSerializers = self.responseSerializers; return serializer; diff --git a/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.h b/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.h index a718d964c..3824736a7 100755 --- a/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.h +++ b/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.h @@ -1,5 +1,5 @@ // AFURLSessionManager.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -19,6 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. + #import #import "AFURLResponseSerialization.h" @@ -28,14 +29,6 @@ #import "AFNetworkReachabilityManager.h" #endif -#ifndef NS_DESIGNATED_INITIALIZER -#if __has_attribute(objc_designated_initializer) -#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer)) -#else -#define NS_DESIGNATED_INITIALIZER -#endif -#endif - /** `AFURLSessionManager` creates and manages an `NSURLSession` object based on a specified `NSURLSessionConfiguration` object, which conforms to ``, ``, ``, and ``. @@ -58,6 +51,7 @@ - `URLSession:willPerformHTTPRedirection:newRequest:completionHandler:` - `URLSession:task:didReceiveChallenge:completionHandler:` - `URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:` + - `URLSession:task:needNewBodyStream:` - `URLSession:task:didCompleteWithError:` ### `NSURLSessionDataDelegate` @@ -93,8 +87,6 @@ NS_ASSUME_NONNULL_BEGIN -#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || TARGET_OS_WATCH - @interface AFURLSessionManager : NSObject /** @@ -119,7 +111,7 @@ NS_ASSUME_NONNULL_BEGIN ///------------------------------- /** - The security policy used by created request operations to evaluate server trust for secure connections. `AFURLSessionManager` uses the `defaultPolicy` unless otherwise specified. + The security policy used by created session to evaluate server trust for secure connections. `AFURLSessionManager` uses the `defaultPolicy` unless otherwise specified. */ @property (nonatomic, strong) AFSecurityPolicy *securityPolicy; @@ -141,22 +133,22 @@ NS_ASSUME_NONNULL_BEGIN /** The data, upload, and download tasks currently run by the managed session. */ -@property (readonly, nonatomic, strong) NSArray *tasks; +@property (readonly, nonatomic, strong) NSArray *tasks; /** The data tasks currently run by the managed session. */ -@property (readonly, nonatomic, strong) NSArray *dataTasks; +@property (readonly, nonatomic, strong) NSArray *dataTasks; /** The upload tasks currently run by the managed session. */ -@property (readonly, nonatomic, strong) NSArray *uploadTasks; +@property (readonly, nonatomic, strong) NSArray *uploadTasks; /** The download tasks currently run by the managed session. */ -@property (readonly, nonatomic, strong) NSArray *downloadTasks; +@property (readonly, nonatomic, strong) NSArray *downloadTasks; ///------------------------------- /// @name Managing Callback Queues @@ -165,20 +157,12 @@ NS_ASSUME_NONNULL_BEGIN /** The dispatch queue for `completionBlock`. If `NULL` (default), the main queue is used. */ -#if OS_OBJECT_HAVE_OBJC_SUPPORT @property (nonatomic, strong, nullable) dispatch_queue_t completionQueue; -#else -@property (nonatomic, assign, nullable) dispatch_queue_t completionQueue; -#endif /** The dispatch group for `completionBlock`. If `NULL` (default), a private dispatch group is used. */ -#if OS_OBJECT_HAVE_OBJC_SUPPORT @property (nonatomic, strong, nullable) dispatch_group_t completionGroup; -#else -@property (nonatomic, assign, nullable) dispatch_group_t completionGroup; -#endif ///--------------------------------- /// @name Working Around System Bugs @@ -224,7 +208,20 @@ NS_ASSUME_NONNULL_BEGIN @param completionHandler A block object to be executed when the task finishes. This block has no return value and takes three arguments: the server response, the response object created by that serializer, and the error that occurred, if any. */ - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request - completionHandler:(nullable void (^)(NSURLResponse *response, id __nullable responseObject, NSError * __nullable error))completionHandler; + completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler DEPRECATED_ATTRIBUTE; + +/** + Creates an `NSURLSessionDataTask` with the specified request. + + @param request The HTTP request for the request. + @param uploadProgressBlock A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue. + @param downloadProgressBlock A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue. + @param completionHandler A block object to be executed when the task finishes. This block has no return value and takes three arguments: the server response, the response object created by that serializer, and the error that occurred, if any. + */ +- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request + uploadProgress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock + downloadProgress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock + completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler; ///--------------------------- /// @name Running Upload Tasks @@ -235,39 +232,39 @@ NS_ASSUME_NONNULL_BEGIN @param request The HTTP request for the request. @param fileURL A URL to the local file to be uploaded. - @param progress A progress object monitoring the current upload progress. + @param uploadProgressBlock A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue. @param completionHandler A block object to be executed when the task finishes. This block has no return value and takes three arguments: the server response, the response object created by that serializer, and the error that occurred, if any. @see `attemptsToRecreateUploadTasksForBackgroundSessions` */ - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - completionHandler:(nullable void (^)(NSURLResponse *response, id __nullable responseObject, NSError * __nullable error))completionHandler; + progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock + completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler; /** Creates an `NSURLSessionUploadTask` with the specified request for an HTTP body. @param request The HTTP request for the request. @param bodyData A data object containing the HTTP body to be uploaded. - @param progress A progress object monitoring the current upload progress. + @param uploadProgressBlock A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue. @param completionHandler A block object to be executed when the task finishes. This block has no return value and takes three arguments: the server response, the response object created by that serializer, and the error that occurred, if any. */ - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(nullable NSData *)bodyData - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - completionHandler:(nullable void (^)(NSURLResponse *response, id __nullable responseObject, NSError * __nullable error))completionHandler; + progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock + completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler; /** Creates an `NSURLSessionUploadTask` with the specified streaming request. @param request The HTTP request for the request. - @param progress A progress object monitoring the current upload progress. + @param uploadProgressBlock A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue. @param completionHandler A block object to be executed when the task finishes. This block has no return value and takes three arguments: the server response, the response object created by that serializer, and the error that occurred, if any. */ - (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - completionHandler:(nullable void (^)(NSURLResponse *response, id __nullable responseObject, NSError * __nullable error))completionHandler; + progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock + completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler; ///----------------------------- /// @name Running Download Tasks @@ -277,29 +274,29 @@ NS_ASSUME_NONNULL_BEGIN Creates an `NSURLSessionDownloadTask` with the specified request. @param request The HTTP request for the request. - @param progress A progress object monitoring the current download progress. + @param downloadProgressBlock A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue. @param destination A block object to be executed in order to determine the destination of the downloaded file. This block takes two arguments, the target path & the server response, and returns the desired file URL of the resulting download. The temporary file used during the download will be automatically deleted after being moved to the returned URL. @param completionHandler A block to be executed when a task finishes. This block has no return value and takes three arguments: the server response, the path of the downloaded file, and the error describing the network or parsing error that occurred, if any. @warning If using a background `NSURLSessionConfiguration` on iOS, these blocks will be lost when the app is terminated. Background sessions may prefer to use `-setDownloadTaskDidFinishDownloadingBlock:` to specify the URL for saving the downloaded file, rather than the destination block of this method. */ - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination - completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * __nullable filePath, NSError * __nullable error))completionHandler; + completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler; /** Creates an `NSURLSessionDownloadTask` with the specified resume data. @param resumeData The data used to resume downloading. - @param progress A progress object monitoring the current download progress. + @param downloadProgressBlock A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue. @param destination A block object to be executed in order to determine the destination of the downloaded file. This block takes two arguments, the target path & the server response, and returns the desired file URL of the resulting download. The temporary file used during the download will be automatically deleted after being moved to the returned URL. @param completionHandler A block to be executed when a task finishes. This block has no return value and takes three arguments: the server response, the path of the downloaded file, and the error describing the network or parsing error that occurred, if any. */ - (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination - completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * __nullable filePath, NSError * __nullable error))completionHandler; + completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler; ///--------------------------------- /// @name Getting Progress for Tasks @@ -308,20 +305,20 @@ NS_ASSUME_NONNULL_BEGIN /** Returns the upload progress of the specified task. - @param uploadTask The session upload task. Must not be `nil`. + @param task The session task. Must not be `nil`. @return An `NSProgress` object reporting the upload progress of a task, or `nil` if the progress is unavailable. */ -- (nullable NSProgress *)uploadProgressForTask:(NSURLSessionUploadTask *)uploadTask; +- (nullable NSProgress *)uploadProgressForTask:(NSURLSessionTask *)task; /** Returns the download progress of the specified task. - @param downloadTask The session download task. Must not be `nil`. + @param task The session task. Must not be `nil`. @return An `NSProgress` object reporting the download progress of a task, or `nil` if the progress is unavailable. */ -- (nullable NSProgress *)downloadProgressForTask:(NSURLSessionDownloadTask *)downloadTask; +- (nullable NSProgress *)downloadProgressForTask:(NSURLSessionTask *)task; ///----------------------------------------- /// @name Setting Session Delegate Callbacks @@ -339,7 +336,7 @@ NS_ASSUME_NONNULL_BEGIN @param block A block object to be executed when a connection level authentication challenge has occurred. The block returns the disposition of the authentication challenge, and takes three arguments: the session, the authentication challenge, and a pointer to the credential that should be used to resolve the challenge. */ -- (void)setSessionDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __nullable __autoreleasing * __nullable credential))block; +- (void)setSessionDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block; ///-------------------------------------- /// @name Setting Task Delegate Callbacks @@ -364,7 +361,7 @@ NS_ASSUME_NONNULL_BEGIN @param block A block object to be executed when a session task has received a request specific authentication challenge. The block returns the disposition of the authentication challenge, and takes four arguments: the session, the task, the authentication challenge, and a pointer to the credential that should be used to resolve the challenge. */ -- (void)setTaskDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential * __nullable __autoreleasing * __nullable credential))block; +- (void)setTaskDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block; /** Sets a block to be executed periodically to track upload progress, as handled by the `NSURLSessionTaskDelegate` method `URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:`. @@ -378,7 +375,7 @@ NS_ASSUME_NONNULL_BEGIN @param block A block object to be executed when a session task is completed. The block has no return value, and takes three arguments: the session, the task, and any error that occurred in the process of executing the task. */ -- (void)setTaskDidCompleteBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSError * __nullable error))block; +- (void)setTaskDidCompleteBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSError * _Nullable error))block; ///------------------------------------------- /// @name Setting Data Task Delegate Callbacks @@ -428,7 +425,7 @@ NS_ASSUME_NONNULL_BEGIN @param block A block object to be executed when a download task has completed. The block returns the URL the download should be moved to, and takes three arguments: the session, the download task, and the temporary location of the downloaded file. If the file manager encounters an error while attempting to move the temporary file to the destination, an `AFURLSessionDownloadTaskDidFailToMoveFileNotification` will be posted, with the download task as its object, and the user info of the error. */ -- (void)setDownloadTaskDidFinishDownloadingBlock:(nullable NSURL * (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block; +- (void)setDownloadTaskDidFinishDownloadingBlock:(nullable NSURL * _Nullable (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block; /** Sets a block to be executed periodically to track download progress, as handled by the `NSURLSessionDownloadDelegate` method `URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesWritten:totalBytesExpectedToWrite:`. @@ -446,109 +443,58 @@ NS_ASSUME_NONNULL_BEGIN @end -#endif - ///-------------------- /// @name Notifications ///-------------------- -/** - Posted when a task begins executing. - - @deprecated Use `AFNetworkingTaskDidResumeNotification` instead. - */ -extern NSString * const AFNetworkingTaskDidStartNotification DEPRECATED_ATTRIBUTE; - /** Posted when a task resumes. */ -extern NSString * const AFNetworkingTaskDidResumeNotification; - -/** - Posted when a task finishes executing. Includes a userInfo dictionary with additional information about the task. - - @deprecated Use `AFNetworkingTaskDidCompleteNotification` instead. - */ -extern NSString * const AFNetworkingTaskDidFinishNotification DEPRECATED_ATTRIBUTE; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidResumeNotification; /** Posted when a task finishes executing. Includes a userInfo dictionary with additional information about the task. */ -extern NSString * const AFNetworkingTaskDidCompleteNotification; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteNotification; /** Posted when a task suspends its execution. */ -extern NSString * const AFNetworkingTaskDidSuspendNotification; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidSuspendNotification; /** Posted when a session is invalidated. */ -extern NSString * const AFURLSessionDidInvalidateNotification; +FOUNDATION_EXPORT NSString * const AFURLSessionDidInvalidateNotification; /** Posted when a session download task encountered an error when moving the temporary download file to a specified destination. */ -extern NSString * const AFURLSessionDownloadTaskDidFailToMoveFileNotification; +FOUNDATION_EXPORT NSString * const AFURLSessionDownloadTaskDidFailToMoveFileNotification; /** - The raw response data of the task. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if response data exists for the task. - - @deprecated Use `AFNetworkingTaskDidCompleteResponseDataKey` instead. + The raw response data of the task. Included in the userInfo dictionary of the `AFNetworkingTaskDidCompleteNotification` if response data exists for the task. */ -extern NSString * const AFNetworkingTaskDidFinishResponseDataKey DEPRECATED_ATTRIBUTE; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteResponseDataKey; /** - The raw response data of the task. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if response data exists for the task. + The serialized response object of the task. Included in the userInfo dictionary of the `AFNetworkingTaskDidCompleteNotification` if the response was serialized. */ -extern NSString * const AFNetworkingTaskDidCompleteResponseDataKey; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey; /** - The serialized response object of the task. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if the response was serialized. - - @deprecated Use `AFNetworkingTaskDidCompleteSerializedResponseKey` instead. + The response serializer used to serialize the response. Included in the userInfo dictionary of the `AFNetworkingTaskDidCompleteNotification` if the task has an associated response serializer. */ -extern NSString * const AFNetworkingTaskDidFinishSerializedResponseKey DEPRECATED_ATTRIBUTE; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteResponseSerializerKey; /** - The serialized response object of the task. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if the response was serialized. + The file path associated with the download task. Included in the userInfo dictionary of the `AFNetworkingTaskDidCompleteNotification` if an the response data has been stored directly to disk. */ -extern NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteAssetPathKey; /** - The response serializer used to serialize the response. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if the task has an associated response serializer. - - @deprecated Use `AFNetworkingTaskDidCompleteResponseSerializerKey` instead. + Any error associated with the task, or the serialization of the response. Included in the userInfo dictionary of the `AFNetworkingTaskDidCompleteNotification` if an error exists. */ -extern NSString * const AFNetworkingTaskDidFinishResponseSerializerKey DEPRECATED_ATTRIBUTE; - -/** - The response serializer used to serialize the response. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if the task has an associated response serializer. - */ -extern NSString * const AFNetworkingTaskDidCompleteResponseSerializerKey; - -/** - The file path associated with the download task. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if an the response data has been stored directly to disk. - - @deprecated Use `AFNetworkingTaskDidCompleteAssetPathKey` instead. - */ -extern NSString * const AFNetworkingTaskDidFinishAssetPathKey DEPRECATED_ATTRIBUTE; - -/** - The file path associated with the download task. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if an the response data has been stored directly to disk. - */ -extern NSString * const AFNetworkingTaskDidCompleteAssetPathKey; - -/** - Any error associated with the task, or the serialization of the response. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if an error exists. - - @deprecated Use `AFNetworkingTaskDidCompleteErrorKey` instead. - */ -extern NSString * const AFNetworkingTaskDidFinishErrorKey DEPRECATED_ATTRIBUTE; - -/** - Any error associated with the task, or the serialization of the response. Included in the userInfo dictionary of the `AFNetworkingTaskDidFinishNotification` if an error exists. - */ -extern NSString * const AFNetworkingTaskDidCompleteErrorKey; +FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidCompleteErrorKey; NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.m b/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.m index 4057d03e2..2475595d7 100755 --- a/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.m +++ b/clients/ios/Other Sources/AFNetworking/AFURLSessionManager.m @@ -1,5 +1,5 @@ // AFURLSessionManager.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -22,7 +22,11 @@ #import "AFURLSessionManager.h" #import -#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) +#ifndef NSFoundationVersionNumber_iOS_8_0 +#define NSFoundationVersionNumber_With_Fixed_5871104061079552_bug 1140.11 +#else +#define NSFoundationVersionNumber_With_Fixed_5871104061079552_bug NSFoundationVersionNumber_iOS_8_0 +#endif static dispatch_queue_t url_session_manager_creation_queue() { static dispatch_queue_t af_url_session_manager_creation_queue; @@ -34,6 +38,17 @@ static dispatch_queue_t url_session_manager_creation_queue() { return af_url_session_manager_creation_queue; } +static void url_session_manager_create_task_safely(dispatch_block_t block) { + if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) { + // Fix of bug + // Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8) + // Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093 + dispatch_sync(url_session_manager_creation_queue(), block); + } else { + block(); + } +} + static dispatch_queue_t url_session_manager_processing_queue() { static dispatch_queue_t af_url_session_manager_processing_queue; static dispatch_once_t onceToken; @@ -60,27 +75,16 @@ NSString * const AFNetworkingTaskDidSuspendNotification = @"com.alamofire.networ NSString * const AFURLSessionDidInvalidateNotification = @"com.alamofire.networking.session.invalidate"; NSString * const AFURLSessionDownloadTaskDidFailToMoveFileNotification = @"com.alamofire.networking.session.download.file-manager-error"; -NSString * const AFNetworkingTaskDidStartNotification = @"com.alamofire.networking.task.resume"; // Deprecated -NSString * const AFNetworkingTaskDidFinishNotification = @"com.alamofire.networking.task.complete"; // Deprecated - NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse"; NSString * const AFNetworkingTaskDidCompleteResponseSerializerKey = @"com.alamofire.networking.task.complete.responseserializer"; NSString * const AFNetworkingTaskDidCompleteResponseDataKey = @"com.alamofire.networking.complete.finish.responsedata"; NSString * const AFNetworkingTaskDidCompleteErrorKey = @"com.alamofire.networking.task.complete.error"; NSString * const AFNetworkingTaskDidCompleteAssetPathKey = @"com.alamofire.networking.task.complete.assetpath"; -NSString * const AFNetworkingTaskDidFinishSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse"; // Deprecated -NSString * const AFNetworkingTaskDidFinishResponseSerializerKey = @"com.alamofire.networking.task.complete.responseserializer"; // Deprecated -NSString * const AFNetworkingTaskDidFinishResponseDataKey = @"com.alamofire.networking.complete.finish.responsedata"; // Deprecated -NSString * const AFNetworkingTaskDidFinishErrorKey = @"com.alamofire.networking.task.complete.error"; // Deprecated -NSString * const AFNetworkingTaskDidFinishAssetPathKey = @"com.alamofire.networking.task.complete.assetpath"; // Deprecated - static NSString * const AFURLSessionManagerLockName = @"com.alamofire.networking.session.manager.lock"; static NSUInteger const AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask = 3; -static void * AFTaskStateChangedContext = &AFTaskStateChangedContext; - typedef void (^AFURLSessionDidBecomeInvalidBlock)(NSURLSession *session, NSError *error); typedef NSURLSessionAuthChallengeDisposition (^AFURLSessionDidReceiveAuthenticationChallengeBlock)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential); @@ -100,53 +104,89 @@ typedef NSCachedURLResponse * (^AFURLSessionDataTaskWillCacheResponseBlock)(NSUR typedef NSURL * (^AFURLSessionDownloadTaskDidFinishDownloadingBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location); typedef void (^AFURLSessionDownloadTaskDidWriteDataBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite); typedef void (^AFURLSessionDownloadTaskDidResumeBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t fileOffset, int64_t expectedTotalBytes); +typedef void (^AFURLSessionTaskProgressBlock)(NSProgress *); typedef void (^AFURLSessionTaskCompletionHandler)(NSURLResponse *response, id responseObject, NSError *error); + #pragma mark - @interface AFURLSessionManagerTaskDelegate : NSObject +- (instancetype)initWithTask:(NSURLSessionTask *)task; @property (nonatomic, weak) AFURLSessionManager *manager; @property (nonatomic, strong) NSMutableData *mutableData; -@property (nonatomic, strong) NSProgress *progress; +@property (nonatomic, strong) NSProgress *uploadProgress; +@property (nonatomic, strong) NSProgress *downloadProgress; @property (nonatomic, copy) NSURL *downloadFileURL; @property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading; +@property (nonatomic, copy) AFURLSessionTaskProgressBlock uploadProgressBlock; +@property (nonatomic, copy) AFURLSessionTaskProgressBlock downloadProgressBlock; @property (nonatomic, copy) AFURLSessionTaskCompletionHandler completionHandler; @end @implementation AFURLSessionManagerTaskDelegate -- (instancetype)init { +- (instancetype)initWithTask:(NSURLSessionTask *)task { self = [super init]; if (!self) { return nil; } - - self.mutableData = [NSMutableData data]; - - self.progress = [NSProgress progressWithTotalUnitCount:0]; - + + _mutableData = [NSMutableData data]; + _uploadProgress = [[NSProgress alloc] initWithParent:nil userInfo:nil]; + _downloadProgress = [[NSProgress alloc] initWithParent:nil userInfo:nil]; + + __weak __typeof__(task) weakTask = task; + for (NSProgress *progress in @[ _uploadProgress, _downloadProgress ]) + { + progress.totalUnitCount = NSURLSessionTransferSizeUnknown; + progress.cancellable = YES; + progress.cancellationHandler = ^{ + [weakTask cancel]; + }; + progress.pausable = YES; + progress.pausingHandler = ^{ + [weakTask suspend]; + }; + if ([progress respondsToSelector:@selector(setResumingHandler:)]) { + progress.resumingHandler = ^{ + [weakTask resume]; + }; + } + [progress addObserver:self + forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) + options:NSKeyValueObservingOptionNew + context:NULL]; + } return self; } +- (void)dealloc { + [self.downloadProgress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted))]; + [self.uploadProgress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted))]; +} + +#pragma mark - NSProgress Tracking + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + if ([object isEqual:self.downloadProgress]) { + if (self.downloadProgressBlock) { + self.downloadProgressBlock(object); + } + } + else if ([object isEqual:self.uploadProgress]) { + if (self.uploadProgressBlock) { + self.uploadProgressBlock(object); + } + } +} + #pragma mark - NSURLSessionTaskDelegate -- (void)URLSession:(__unused NSURLSession *)session - task:(__unused NSURLSessionTask *)task - didSendBodyData:(__unused int64_t)bytesSent - totalBytesSent:(int64_t)totalBytesSent -totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend -{ - self.progress.totalUnitCount = totalBytesExpectedToSend; - self.progress.completedUnitCount = totalBytesSent; -} - - (void)URLSession:(__unused NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" __strong AFURLSessionManager *manager = self.manager; __block id responseObject = nil; @@ -208,57 +248,66 @@ didCompleteWithError:(NSError *)error }); }); } -#pragma clang diagnostic pop } -#pragma mark - NSURLSessionDataTaskDelegate +#pragma mark - NSURLSessionDataDelegate - (void)URLSession:(__unused NSURLSession *)session dataTask:(__unused NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { + self.downloadProgress.totalUnitCount = dataTask.countOfBytesExpectedToReceive; + self.downloadProgress.completedUnitCount = dataTask.countOfBytesReceived; + [self.mutableData appendData:data]; } -#pragma mark - NSURLSessionDownloadTaskDelegate +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task + didSendBodyData:(int64_t)bytesSent + totalBytesSent:(int64_t)totalBytesSent +totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{ + + self.uploadProgress.totalUnitCount = task.countOfBytesExpectedToSend; + self.uploadProgress.completedUnitCount = task.countOfBytesSent; +} + +#pragma mark - NSURLSessionDownloadDelegate + +- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask + didWriteData:(int64_t)bytesWritten + totalBytesWritten:(int64_t)totalBytesWritten +totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ + + self.downloadProgress.totalUnitCount = totalBytesExpectedToWrite; + self.downloadProgress.completedUnitCount = totalBytesWritten; +} + +- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask + didResumeAtOffset:(int64_t)fileOffset +expectedTotalBytes:(int64_t)expectedTotalBytes{ + + self.downloadProgress.totalUnitCount = expectedTotalBytes; + self.downloadProgress.completedUnitCount = fileOffset; +} - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { - NSError *fileManagerError = nil; self.downloadFileURL = nil; if (self.downloadTaskDidFinishDownloading) { self.downloadFileURL = self.downloadTaskDidFinishDownloading(session, downloadTask, location); if (self.downloadFileURL) { - [[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]; + NSError *fileManagerError = nil; - if (fileManagerError) { + if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]) { [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo]; } } } } -- (void)URLSession:(__unused NSURLSession *)session - downloadTask:(__unused NSURLSessionDownloadTask *)downloadTask - didWriteData:(__unused int64_t)bytesWritten - totalBytesWritten:(int64_t)totalBytesWritten -totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite -{ - self.progress.totalUnitCount = totalBytesExpectedToWrite; - self.progress.completedUnitCount = totalBytesWritten; -} - -- (void)URLSession:(__unused NSURLSession *)session - downloadTask:(__unused NSURLSessionDownloadTask *)downloadTask - didResumeAtOffset:(int64_t)fileOffset -expectedTotalBytes:(int64_t)expectedTotalBytes { - self.progress.totalUnitCount = expectedTotalBytes; - self.progress.completedUnitCount = fileOffset; -} - @end #pragma mark - @@ -272,14 +321,14 @@ expectedTotalBytes:(int64_t)expectedTotalBytes { * - https://github.com/AFNetworking/AFNetworking/pull/2702 */ -static inline void af_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) { - Method originalMethod = class_getInstanceMethod(class, originalSelector); - Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); +static inline void af_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector) { + Method originalMethod = class_getInstanceMethod(theClass, originalSelector); + Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector); method_exchangeImplementations(originalMethod, swizzledMethod); } -static inline BOOL af_addMethod(Class class, SEL selector, Method method) { - return class_addMethod(class, selector, method_getImplementation(method), method_getTypeEncoding(method)); +static inline BOOL af_addMethod(Class theClass, SEL selector, Method method) { + return class_addMethod(theClass, selector, method_getImplementation(method), method_getTypeEncoding(method)); } static NSString * const AFNSURLSessionTaskDidResumeNotification = @"com.alamofire.networking.nsurlsessiontask.resume"; @@ -324,11 +373,13 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi 7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods 8) Set the current class to the super class, and repeat steps 3-8 */ + NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; + NSURLSession * session = [NSURLSession sessionWithConfiguration:configuration]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnonnull" - NSURLSessionDataTask *localDataTask = [[NSURLSession sessionWithConfiguration:nil] dataTaskWithURL:nil]; + NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil]; #pragma clang diagnostic pop - IMP originalAFResumeIMP = method_getImplementation(class_getInstanceMethod([_AFURLSessionTaskSwizzling class], @selector(af_resume))); + IMP originalAFResumeIMP = method_getImplementation(class_getInstanceMethod([self class], @selector(af_resume))); Class currentClass = [localDataTask class]; while (class_getInstanceMethod(currentClass, @selector(resume))) { @@ -343,18 +394,21 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi } [localDataTask cancel]; + [session finishTasksAndInvalidate]; } } -+ (void)swizzleResumeAndSuspendMethodForClass:(Class)class { ++ (void)swizzleResumeAndSuspendMethodForClass:(Class)theClass { Method afResumeMethod = class_getInstanceMethod(self, @selector(af_resume)); Method afSuspendMethod = class_getInstanceMethod(self, @selector(af_suspend)); - - af_addMethod(class, @selector(af_resume), afResumeMethod); - af_addMethod(class, @selector(af_suspend), afSuspendMethod); - - af_swizzleSelector(class, @selector(resume), @selector(af_resume)); - af_swizzleSelector(class, @selector(suspend), @selector(af_suspend)); + + if (af_addMethod(theClass, @selector(af_resume), afResumeMethod)) { + af_swizzleSelector(theClass, @selector(resume), @selector(af_resume)); + } + + if (af_addMethod(theClass, @selector(af_suspend), afSuspendMethod)) { + af_swizzleSelector(theClass, @selector(suspend), @selector(af_suspend)); + } } - (NSURLSessionTaskState)state { @@ -447,7 +501,7 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) { for (NSURLSessionDataTask *task in dataTasks) { - [self addDelegateForDataTask:task completionHandler:nil]; + [self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil]; } for (NSURLSessionUploadTask *uploadTask in uploadTasks) { @@ -459,9 +513,6 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi } }]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidResume:) name:AFNSURLSessionTaskDidResumeNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidSuspend:) name:AFNSURLSessionTaskDidSuspendNotification object:nil]; - return self; } @@ -518,64 +569,47 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi [self.lock lock]; self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)] = delegate; + [self addNotificationObserverForTask:task]; [self.lock unlock]; } - (void)addDelegateForDataTask:(NSURLSessionDataTask *)dataTask + uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock + downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler { - AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init]; + AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] initWithTask:dataTask]; delegate.manager = self; delegate.completionHandler = completionHandler; dataTask.taskDescription = self.taskDescriptionForSessionTasks; [self setDelegate:delegate forTask:dataTask]; + + delegate.uploadProgressBlock = uploadProgressBlock; + delegate.downloadProgressBlock = downloadProgressBlock; } - (void)addDelegateForUploadTask:(NSURLSessionUploadTask *)uploadTask - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler { - AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init]; + AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] initWithTask:uploadTask]; delegate.manager = self; delegate.completionHandler = completionHandler; - int64_t totalUnitCount = uploadTask.countOfBytesExpectedToSend; - if(totalUnitCount == NSURLSessionTransferSizeUnknown) { - NSString *contentLength = [uploadTask.originalRequest valueForHTTPHeaderField:@"Content-Length"]; - if(contentLength) { - totalUnitCount = (int64_t)[contentLength longLongValue]; - } - } - - if (delegate.progress) { - delegate.progress.totalUnitCount = totalUnitCount; - } else { - delegate.progress = [NSProgress progressWithTotalUnitCount:totalUnitCount]; - } - - delegate.progress.pausingHandler = ^{ - [uploadTask suspend]; - }; - delegate.progress.cancellationHandler = ^{ - [uploadTask cancel]; - }; - - if (progress) { - *progress = delegate.progress; - } - uploadTask.taskDescription = self.taskDescriptionForSessionTasks; [self setDelegate:delegate forTask:uploadTask]; + + delegate.uploadProgressBlock = uploadProgressBlock; } - (void)addDelegateForDownloadTask:(NSURLSessionDownloadTask *)downloadTask - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler { - AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init]; + AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] initWithTask:downloadTask]; delegate.manager = self; delegate.completionHandler = completionHandler; @@ -585,29 +619,22 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi }; } - if (progress) { - *progress = delegate.progress; - } - downloadTask.taskDescription = self.taskDescriptionForSessionTasks; [self setDelegate:delegate forTask:downloadTask]; + + delegate.downloadProgressBlock = downloadProgressBlock; } - (void)removeDelegateForTask:(NSURLSessionTask *)task { NSParameterAssert(task); [self.lock lock]; + [self removeNotificationObserverForTask:task]; [self.mutableTaskDelegatesKeyedByTaskIdentifier removeObjectForKey:@(task.taskIdentifier)]; [self.lock unlock]; } -- (void)removeAllDelegates { - [self.lock lock]; - [self.mutableTaskDelegatesKeyedByTaskIdentifier removeAllObjects]; - [self.lock unlock]; -} - #pragma mark - - (NSArray *)tasksForKeyPath:(NSString *)keyPath { @@ -651,13 +678,11 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi #pragma mark - - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks { - dispatch_async(dispatch_get_main_queue(), ^{ - if (cancelPendingTasks) { - [self.session invalidateAndCancel]; - } else { - [self.session finishTasksAndInvalidate]; - } - }); + if (cancelPendingTasks) { + [self.session invalidateAndCancel]; + } else { + [self.session finishTasksAndInvalidate]; + } } #pragma mark - @@ -668,17 +693,36 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi _responseSerializer = responseSerializer; } +#pragma mark - +- (void)addNotificationObserverForTask:(NSURLSessionTask *)task { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidResume:) name:AFNSURLSessionTaskDidResumeNotification object:task]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidSuspend:) name:AFNSURLSessionTaskDidSuspendNotification object:task]; +} + +- (void)removeNotificationObserverForTask:(NSURLSessionTask *)task { + [[NSNotificationCenter defaultCenter] removeObserver:self name:AFNSURLSessionTaskDidSuspendNotification object:task]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:AFNSURLSessionTaskDidResumeNotification object:task]; +} + #pragma mark - - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler { + return [self dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:completionHandler]; +} + +- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request + uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock + downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock + completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler { + __block NSURLSessionDataTask *dataTask = nil; - dispatch_sync(url_session_manager_creation_queue(), ^{ + url_session_manager_create_task_safely(^{ dataTask = [self.session dataTaskWithRequest:request]; }); - [self addDelegateForDataTask:dataTask completionHandler:completionHandler]; + [self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler]; return dataTask; } @@ -687,54 +731,51 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler { __block NSURLSessionUploadTask *uploadTask = nil; - dispatch_sync(url_session_manager_creation_queue(), ^{ + url_session_manager_create_task_safely(^{ uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL]; }); + // uploadTask may be nil on iOS7 because uploadTaskWithRequest:fromFile: may return nil despite being documented as nonnull (https://devforums.apple.com/message/926113#926113) if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) { for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) { uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL]; } } - - if (!uploadTask) { - uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL]; - } - - [self addDelegateForUploadTask:uploadTask progress:progress completionHandler:completionHandler]; + + [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler]; return uploadTask; } - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler { __block NSURLSessionUploadTask *uploadTask = nil; - dispatch_sync(url_session_manager_creation_queue(), ^{ + url_session_manager_create_task_safely(^{ uploadTask = [self.session uploadTaskWithRequest:request fromData:bodyData]; }); - [self addDelegateForUploadTask:uploadTask progress:progress completionHandler:completionHandler]; + [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler]; return uploadTask; } - (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler { __block NSURLSessionUploadTask *uploadTask = nil; - dispatch_sync(url_session_manager_creation_queue(), ^{ + url_session_manager_create_task_safely(^{ uploadTask = [self.session uploadTaskWithStreamedRequest:request]; }); - [self addDelegateForUploadTask:uploadTask progress:progress completionHandler:completionHandler]; + [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler]; return uploadTask; } @@ -742,43 +783,42 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi #pragma mark - - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler { __block NSURLSessionDownloadTask *downloadTask = nil; - dispatch_sync(url_session_manager_creation_queue(), ^{ + url_session_manager_create_task_safely(^{ downloadTask = [self.session downloadTaskWithRequest:request]; }); - [self addDelegateForDownloadTask:downloadTask progress:progress destination:destination completionHandler:completionHandler]; + [self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler]; return downloadTask; } - (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData - progress:(NSProgress * __autoreleasing *)progress + progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler { __block NSURLSessionDownloadTask *downloadTask = nil; - dispatch_sync(url_session_manager_creation_queue(), ^{ + url_session_manager_create_task_safely(^{ downloadTask = [self.session downloadTaskWithResumeData:resumeData]; }); - [self addDelegateForDownloadTask:downloadTask progress:progress destination:destination completionHandler:completionHandler]; + [self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler]; return downloadTask; } #pragma mark - - -- (NSProgress *)uploadProgressForTask:(NSURLSessionUploadTask *)uploadTask { - return [[self delegateForTask:uploadTask] progress]; +- (NSProgress *)uploadProgressForTask:(NSURLSessionTask *)task { + return [[self delegateForTask:task] uploadProgress]; } -- (NSProgress *)downloadProgressForTask:(NSURLSessionDownloadTask *)downloadTask { - return [[self delegateForTask:downloadTask] progress]; +- (NSProgress *)downloadProgressForTask:(NSURLSessionTask *)task { + return [[self delegateForTask:task] downloadProgress]; } #pragma mark - @@ -878,7 +918,6 @@ didBecomeInvalidWithError:(NSError *)error self.sessionDidBecomeInvalid(session, error); } - [self removeAllDelegates]; [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDidInvalidateNotification object:session]; } @@ -991,9 +1030,12 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend totalUnitCount = (int64_t) [contentLength longLongValue]; } } - + AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task]; - [delegate URLSession:session task:task didSendBodyData:bytesSent totalBytesSent:totalBytesSent totalBytesExpectedToSend:totalUnitCount]; + + if (delegate) { + [delegate URLSession:session task:task didSendBodyData:bytesSent totalBytesSent:totalBytesSent totalBytesExpectedToSend:totalBytesExpectedToSend]; + } if (self.taskDidSendBodyData) { self.taskDidSendBodyData(session, task, bytesSent, totalBytesSent, totalUnitCount); @@ -1016,7 +1058,6 @@ didCompleteWithError:(NSError *)error if (self.taskDidComplete) { self.taskDidComplete(session, task, error); } - } #pragma mark - NSURLSessionDataDelegate @@ -1056,6 +1097,7 @@ didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { + AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:dataTask]; [delegate URLSession:session dataTask:dataTask didReceiveData:data]; @@ -1100,8 +1142,8 @@ didFinishDownloadingToURL:(NSURL *)location if (fileURL) { delegate.downloadFileURL = fileURL; NSError *error = nil; - [[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error]; - if (error) { + + if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error]) { [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo]; } @@ -1120,8 +1162,12 @@ didFinishDownloadingToURL:(NSURL *)location totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { + AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:downloadTask]; - [delegate URLSession:session downloadTask:downloadTask didWriteData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite]; + + if (delegate) { + [delegate URLSession:session downloadTask:downloadTask didWriteData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite]; + } if (self.downloadTaskDidWriteData) { self.downloadTaskDidWriteData(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite); @@ -1133,8 +1179,12 @@ totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { + AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:downloadTask]; - [delegate URLSession:session downloadTask:downloadTask didResumeAtOffset:fileOffset expectedTotalBytes:expectedTotalBytes]; + + if (delegate) { + [delegate URLSession:session downloadTask:downloadTask didResumeAtOffset:fileOffset expectedTotalBytes:expectedTotalBytes]; + } if (self.downloadTaskDidResume) { self.downloadTaskDidResume(session, downloadTask, fileOffset, expectedTotalBytes); @@ -1147,7 +1197,7 @@ expectedTotalBytes:(int64_t)expectedTotalBytes return YES; } -- (id)initWithCoder:(NSCoder *)decoder { +- (instancetype)initWithCoder:(NSCoder *)decoder { NSURLSessionConfiguration *configuration = [decoder decodeObjectOfClass:[NSURLSessionConfiguration class] forKey:@"sessionConfiguration"]; self = [self initWithSessionConfiguration:configuration]; @@ -1164,10 +1214,8 @@ expectedTotalBytes:(int64_t)expectedTotalBytes #pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone { +- (instancetype)copyWithZone:(NSZone *)zone { return [[[self class] allocWithZone:zone] initWithSessionConfiguration:self.session.configuration]; } @end - -#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIActivityIndicatorView+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIActivityIndicatorView+AFNetworking.h new file mode 100755 index 000000000..d424c9b86 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIActivityIndicatorView+AFNetworking.h @@ -0,0 +1,48 @@ +// UIActivityIndicatorView+AFNetworking.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import + +/** + This category adds methods to the UIKit framework's `UIActivityIndicatorView` class. The methods in this category provide support for automatically starting and stopping animation depending on the loading state of a session task. + */ +@interface UIActivityIndicatorView (AFNetworking) + +///---------------------------------- +/// @name Animating for Session Tasks +///---------------------------------- + +/** + Binds the animating state to the state of the specified task. + + @param task The task. If `nil`, automatic updating from any previously specified operation will be disabled. + */ +- (void)setAnimatingWithStateOfTask:(nullable NSURLSessionTask *)task; + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIActivityIndicatorView+AFNetworking.m b/clients/ios/Other Sources/AFNetworking/UIActivityIndicatorView+AFNetworking.m new file mode 100755 index 000000000..602a72d07 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIActivityIndicatorView+AFNetworking.m @@ -0,0 +1,114 @@ +// UIActivityIndicatorView+AFNetworking.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "UIActivityIndicatorView+AFNetworking.h" +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import "AFURLSessionManager.h" + +@interface AFActivityIndicatorViewNotificationObserver : NSObject +@property (readonly, nonatomic, weak) UIActivityIndicatorView *activityIndicatorView; +- (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView; + +- (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task; + +@end + +@implementation UIActivityIndicatorView (AFNetworking) + +- (AFActivityIndicatorViewNotificationObserver *)af_notificationObserver { + AFActivityIndicatorViewNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver)); + if (notificationObserver == nil) { + notificationObserver = [[AFActivityIndicatorViewNotificationObserver alloc] initWithActivityIndicatorView:self]; + objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } + return notificationObserver; +} + +- (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task { + [[self af_notificationObserver] setAnimatingWithStateOfTask:task]; +} + +@end + +@implementation AFActivityIndicatorViewNotificationObserver + +- (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView +{ + self = [super init]; + if (self) { + _activityIndicatorView = activityIndicatorView; + } + return self; +} + +- (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task { + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + + [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil]; + + if (task) { + if (task.state != NSURLSessionTaskStateCompleted) { + UIActivityIndicatorView *activityIndicatorView = self.activityIndicatorView; + if (task.state == NSURLSessionTaskStateRunning) { + [activityIndicatorView startAnimating]; + } else { + [activityIndicatorView stopAnimating]; + } + + [notificationCenter addObserver:self selector:@selector(af_startAnimating) name:AFNetworkingTaskDidResumeNotification object:task]; + [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidCompleteNotification object:task]; + [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidSuspendNotification object:task]; + } + } +} + +#pragma mark - + +- (void)af_startAnimating { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.activityIndicatorView startAnimating]; + }); +} + +- (void)af_stopAnimating { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.activityIndicatorView stopAnimating]; + }); +} + +#pragma mark - + +- (void)dealloc { + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + + [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil]; +} + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIButton+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIButton+AFNetworking.h new file mode 100755 index 000000000..d33e0d4a3 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIButton+AFNetworking.h @@ -0,0 +1,175 @@ +// UIButton+AFNetworking.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import + +NS_ASSUME_NONNULL_BEGIN + +@class AFImageDownloader; + +/** + This category adds methods to the UIKit framework's `UIButton` class. The methods in this category provide support for loading remote images and background images asynchronously from a URL. + + @warning Compound values for control `state` (such as `UIControlStateHighlighted | UIControlStateDisabled`) are unsupported. + */ +@interface UIButton (AFNetworking) + +///------------------------------------ +/// @name Accessing the Image Downloader +///------------------------------------ + +/** + Set the shared image downloader used to download images. + + @param imageDownloader The shared image downloader used to download images. +*/ ++ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader; + +/** + The shared image downloader used to download images. + */ ++ (AFImageDownloader *)sharedImageDownloader; + +///-------------------- +/// @name Setting Image +///-------------------- + +/** + Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled. + + If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + @param state The control state. + @param url The URL used for the image request. + */ +- (void)setImageForState:(UIControlState)state + withURL:(NSURL *)url; + +/** + Asynchronously downloads an image from the specified URL, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled. + + If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + @param state The control state. + @param url The URL used for the image request. + @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the button will not change its image until the image request finishes. + */ +- (void)setImageForState:(UIControlState)state + withURL:(NSURL *)url + placeholderImage:(nullable UIImage *)placeholderImage; + +/** + Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled. + + If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + If a success block is specified, it is the responsibility of the block to set the image of the button before returning. If no success block is specified, the default behavior of setting the image with `setImage:forState:` is applied. + + @param state The control state. + @param urlRequest The URL request used for the image request. + @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the button will not change its image until the image request finishes. + @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. + @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + */ +- (void)setImageForState:(UIControlState)state + withURLRequest:(NSURLRequest *)urlRequest + placeholderImage:(nullable UIImage *)placeholderImage + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure; + + +///------------------------------- +/// @name Setting Background Image +///------------------------------- + +/** + Asynchronously downloads an image from the specified URL, and sets it as the background image for the specified state once the request is finished. Any previous background image request for the receiver will be cancelled. + + If the background image is cached locally, the background image is set immediately, otherwise the specified placeholder background image will be set immediately, and then the remote background image will be set once the request is finished. + + @param state The control state. + @param url The URL used for the background image request. + */ +- (void)setBackgroundImageForState:(UIControlState)state + withURL:(NSURL *)url; + +/** + Asynchronously downloads an image from the specified URL, and sets it as the background image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled. + + If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + @param state The control state. + @param url The URL used for the background image request. + @param placeholderImage The background image to be set initially, until the background image request finishes. If `nil`, the button will not change its background image until the background image request finishes. + */ +- (void)setBackgroundImageForState:(UIControlState)state + withURL:(NSURL *)url + placeholderImage:(nullable UIImage *)placeholderImage; + +/** + Asynchronously downloads an image from the specified URL request, and sets it as the image for the specified state once the request is finished. Any previous image request for the receiver will be cancelled. + + If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + If a success block is specified, it is the responsibility of the block to set the image of the button before returning. If no success block is specified, the default behavior of setting the image with `setBackgroundImage:forState:` is applied. + + @param state The control state. + @param urlRequest The URL request used for the image request. + @param placeholderImage The background image to be set initially, until the background image request finishes. If `nil`, the button will not change its background image until the background image request finishes. + @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. + @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + */ +- (void)setBackgroundImageForState:(UIControlState)state + withURLRequest:(NSURLRequest *)urlRequest + placeholderImage:(nullable UIImage *)placeholderImage + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure; + + +///------------------------------ +/// @name Canceling Image Loading +///------------------------------ + +/** + Cancels any executing image task for the specified control state of the receiver, if one exists. + + @param state The control state. + */ +- (void)cancelImageDownloadTaskForState:(UIControlState)state; + +/** + Cancels any executing background image task for the specified control state of the receiver, if one exists. + + @param state The control state. + */ +- (void)cancelBackgroundImageDownloadTaskForState:(UIControlState)state; + +@end + +NS_ASSUME_NONNULL_END + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIButton+AFNetworking.m b/clients/ios/Other Sources/AFNetworking/UIButton+AFNetworking.m new file mode 100755 index 000000000..2da601273 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIButton+AFNetworking.m @@ -0,0 +1,302 @@ +// UIButton+AFNetworking.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "UIButton+AFNetworking.h" + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import "UIImageView+AFNetworking.h" +#import "AFImageDownloader.h" + +@interface UIButton (_AFNetworking) +@end + +@implementation UIButton (_AFNetworking) + +#pragma mark - + +static char AFImageDownloadReceiptNormal; +static char AFImageDownloadReceiptHighlighted; +static char AFImageDownloadReceiptSelected; +static char AFImageDownloadReceiptDisabled; + +static const char * af_imageDownloadReceiptKeyForState(UIControlState state) { + switch (state) { + case UIControlStateHighlighted: + return &AFImageDownloadReceiptHighlighted; + case UIControlStateSelected: + return &AFImageDownloadReceiptSelected; + case UIControlStateDisabled: + return &AFImageDownloadReceiptDisabled; + case UIControlStateNormal: + default: + return &AFImageDownloadReceiptNormal; + } +} + +- (AFImageDownloadReceipt *)af_imageDownloadReceiptForState:(UIControlState)state { + return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, af_imageDownloadReceiptKeyForState(state)); +} + +- (void)af_setImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt + forState:(UIControlState)state +{ + objc_setAssociatedObject(self, af_imageDownloadReceiptKeyForState(state), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +#pragma mark - + +static char AFBackgroundImageDownloadReceiptNormal; +static char AFBackgroundImageDownloadReceiptHighlighted; +static char AFBackgroundImageDownloadReceiptSelected; +static char AFBackgroundImageDownloadReceiptDisabled; + +static const char * af_backgroundImageDownloadReceiptKeyForState(UIControlState state) { + switch (state) { + case UIControlStateHighlighted: + return &AFBackgroundImageDownloadReceiptHighlighted; + case UIControlStateSelected: + return &AFBackgroundImageDownloadReceiptSelected; + case UIControlStateDisabled: + return &AFBackgroundImageDownloadReceiptDisabled; + case UIControlStateNormal: + default: + return &AFBackgroundImageDownloadReceiptNormal; + } +} + +- (AFImageDownloadReceipt *)af_backgroundImageDownloadReceiptForState:(UIControlState)state { + return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, af_backgroundImageDownloadReceiptKeyForState(state)); +} + +- (void)af_setBackgroundImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt + forState:(UIControlState)state +{ + objc_setAssociatedObject(self, af_backgroundImageDownloadReceiptKeyForState(state), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +@end + +#pragma mark - + +@implementation UIButton (AFNetworking) + ++ (AFImageDownloader *)sharedImageDownloader { + + return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance]; +} + ++ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader { + objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +#pragma mark - + +- (void)setImageForState:(UIControlState)state + withURL:(NSURL *)url +{ + [self setImageForState:state withURL:url placeholderImage:nil]; +} + +- (void)setImageForState:(UIControlState)state + withURL:(NSURL *)url + placeholderImage:(UIImage *)placeholderImage +{ + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; + [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; + + [self setImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil]; +} + +- (void)setImageForState:(UIControlState)state + withURLRequest:(NSURLRequest *)urlRequest + placeholderImage:(nullable UIImage *)placeholderImage + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure +{ + if ([self isActiveTaskURLEqualToURLRequest:urlRequest forState:state]) { + return; + } + + [self cancelImageDownloadTaskForState:state]; + + AFImageDownloader *downloader = [[self class] sharedImageDownloader]; + id imageCache = downloader.imageCache; + + //Use the image from the image cache if it exists + UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil]; + if (cachedImage) { + if (success) { + success(urlRequest, nil, cachedImage); + } else { + [self setImage:cachedImage forState:state]; + } + [self af_setImageDownloadReceipt:nil forState:state]; + } else { + if (placeholderImage) { + [self setImage:placeholderImage forState:state]; + } + + __weak __typeof(self)weakSelf = self; + NSUUID *downloadID = [NSUUID UUID]; + AFImageDownloadReceipt *receipt; + receipt = [downloader + downloadImageForURLRequest:urlRequest + withReceiptID:downloadID + success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) { + if (success) { + success(request, response, responseObject); + } else if(responseObject) { + [strongSelf setImage:responseObject forState:state]; + } + [strongSelf af_setImageDownloadReceipt:nil forState:state]; + } + + } + failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) { + if (failure) { + failure(request, response, error); + } + [strongSelf af_setImageDownloadReceipt:nil forState:state]; + } + }]; + + [self af_setImageDownloadReceipt:receipt forState:state]; + } +} + +#pragma mark - + +- (void)setBackgroundImageForState:(UIControlState)state + withURL:(NSURL *)url +{ + [self setBackgroundImageForState:state withURL:url placeholderImage:nil]; +} + +- (void)setBackgroundImageForState:(UIControlState)state + withURL:(NSURL *)url + placeholderImage:(nullable UIImage *)placeholderImage +{ + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; + [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; + + [self setBackgroundImageForState:state withURLRequest:request placeholderImage:placeholderImage success:nil failure:nil]; +} + +- (void)setBackgroundImageForState:(UIControlState)state + withURLRequest:(NSURLRequest *)urlRequest + placeholderImage:(nullable UIImage *)placeholderImage + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure +{ + if ([self isActiveBackgroundTaskURLEqualToURLRequest:urlRequest forState:state]) { + return; + } + + [self cancelBackgroundImageDownloadTaskForState:state]; + + AFImageDownloader *downloader = [[self class] sharedImageDownloader]; + id imageCache = downloader.imageCache; + + //Use the image from the image cache if it exists + UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil]; + if (cachedImage) { + if (success) { + success(urlRequest, nil, cachedImage); + } else { + [self setBackgroundImage:cachedImage forState:state]; + } + [self af_setBackgroundImageDownloadReceipt:nil forState:state]; + } else { + if (placeholderImage) { + [self setBackgroundImage:placeholderImage forState:state]; + } + + __weak __typeof(self)weakSelf = self; + NSUUID *downloadID = [NSUUID UUID]; + AFImageDownloadReceipt *receipt; + receipt = [downloader + downloadImageForURLRequest:urlRequest + withReceiptID:downloadID + success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) { + if (success) { + success(request, response, responseObject); + } else if(responseObject) { + [strongSelf setBackgroundImage:responseObject forState:state]; + } + [strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state]; + } + + } + failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) { + if (failure) { + failure(request, response, error); + } + [strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state]; + } + }]; + + [self af_setBackgroundImageDownloadReceipt:receipt forState:state]; + } +} + +#pragma mark - + +- (void)cancelImageDownloadTaskForState:(UIControlState)state { + AFImageDownloadReceipt *receipt = [self af_imageDownloadReceiptForState:state]; + if (receipt != nil) { + [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:receipt]; + [self af_setImageDownloadReceipt:nil forState:state]; + } +} + +- (void)cancelBackgroundImageDownloadTaskForState:(UIControlState)state { + AFImageDownloadReceipt *receipt = [self af_backgroundImageDownloadReceiptForState:state]; + if (receipt != nil) { + [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:receipt]; + [self af_setBackgroundImageDownloadReceipt:nil forState:state]; + } +} + +- (BOOL)isActiveTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest forState:(UIControlState)state { + AFImageDownloadReceipt *receipt = [self af_imageDownloadReceiptForState:state]; + return [receipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString]; +} + +- (BOOL)isActiveBackgroundTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest forState:(UIControlState)state { + AFImageDownloadReceipt *receipt = [self af_backgroundImageDownloadReceiptForState:state]; + return [receipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString]; +} + + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIImage+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIImage+AFNetworking.h new file mode 100755 index 000000000..14744cddd --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIImage+AFNetworking.h @@ -0,0 +1,35 @@ +// +// UIImage+AFNetworking.h +// +// +// Created by Paulo Ferreira on 08/07/15. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import + +@interface UIImage (AFNetworking) + ++ (UIImage*) safeImageWithData:(NSData*)data; + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.h index e33d8a03d..8929252e5 100755 --- a/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.h +++ b/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.h @@ -1,5 +1,5 @@ // UIImageView+AFNetworking.h -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,47 +21,36 @@ #import -#import +#import -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV #import NS_ASSUME_NONNULL_BEGIN -@protocol AFURLResponseSerialization, AFImageCache; +@class AFImageDownloader; /** This category adds methods to the UIKit framework's `UIImageView` class. The methods in this category provide support for loading remote images asynchronously from a URL. */ @interface UIImageView (AFNetworking) -///---------------------------- -/// @name Accessing Image Cache -///---------------------------- - -/** - The image cache used to improve image loading performance on scroll views. By default, this is an `NSCache` subclass conforming to the `AFImageCache` protocol, which listens for notification warnings and evicts objects accordingly. -*/ -+ (id )sharedImageCache; - -/** - Set the cache used for image loading. - - @param imageCache The image cache. - */ -+ (void)setSharedImageCache:(id )imageCache; - ///------------------------------------ -/// @name Accessing Response Serializer +/// @name Accessing the Image Downloader ///------------------------------------ /** - The response serializer used to create an image representation from the server response and response data. By default, this is an instance of `AFImageResponseSerializer`. + Set the shared image downloader used to download images. - @discussion Subclasses of `AFImageResponseSerializer` could be used to perform post-processing, such as color correction, face detection, or other effects. See https://github.com/AFNetworking/AFCoreImageSerializer + @param imageDownloader The shared image downloader used to download images. */ -@property (nonatomic, strong) id imageResponseSerializer; ++ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader; + +/** + The shared image downloader used to download images. + */ ++ (AFImageDownloader *)sharedImageDownloader; ///-------------------- /// @name Setting Image @@ -100,47 +89,21 @@ NS_ASSUME_NONNULL_BEGIN @param urlRequest The URL request used for the image request. @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. - @param success A block to be executed when the image request operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. - @param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`. + @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. */ - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(nullable UIImage *)placeholderImage - success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success - failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; + success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success + failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure; /** Cancels any executing image operation for the receiver, if one exists. */ -- (void)cancelImageRequestOperation; +- (void)cancelImageDownloadTask; @end -#pragma mark - - -/** - The `AFImageCache` protocol is adopted by an object used to cache images loaded by the AFNetworking category on `UIImageView`. - */ -@protocol AFImageCache - -/** - Returns a cached image for the specified request, if available. - - @param request The image request. - - @return The cached image. - */ -- (nullable UIImage *)cachedImageForRequest:(NSURLRequest *)request; - -/** - Caches a particular image for the specified request. - - @param image The image to cache. - @param request The request to be used as a cache key. - */ -- (void)cacheImage:(UIImage *)image - forRequest:(NSURLRequest *)request; -@end - NS_ASSUME_NONNULL_END #endif diff --git a/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.m b/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.m index c1b0e1b8e..1f0a77861 100755 --- a/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.m +++ b/clients/ios/Other Sources/AFNetworking/UIImageView+AFNetworking.m @@ -1,5 +1,5 @@ // UIImageView+AFNetworking.m -// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/) +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,38 +23,22 @@ #import -#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#if TARGET_OS_IOS || TARGET_OS_TV -#import "AFHTTPRequestOperation.h" - -@interface AFImageCache : NSCache -@end - -#pragma mark - +#import "AFImageDownloader.h" @interface UIImageView (_AFNetworking) -@property (readwrite, nonatomic, strong, setter = af_setImageRequestOperation:) AFHTTPRequestOperation *af_imageRequestOperation; +@property (readwrite, nonatomic, strong, setter = af_setActiveImageDownloadReceipt:) AFImageDownloadReceipt *af_activeImageDownloadReceipt; @end @implementation UIImageView (_AFNetworking) -+ (NSOperationQueue *)af_sharedImageRequestOperationQueue { - static NSOperationQueue *_af_sharedImageRequestOperationQueue = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - _af_sharedImageRequestOperationQueue = [[NSOperationQueue alloc] init]; - _af_sharedImageRequestOperationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount; - }); - - return _af_sharedImageRequestOperationQueue; +- (AFImageDownloadReceipt *)af_activeImageDownloadReceipt { + return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, @selector(af_activeImageDownloadReceipt)); } -- (AFHTTPRequestOperation *)af_imageRequestOperation { - return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, @selector(af_imageRequestOperation)); -} - -- (void)af_setImageRequestOperation:(AFHTTPRequestOperation *)imageRequestOperation { - objc_setAssociatedObject(self, @selector(af_imageRequestOperation), imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +- (void)af_setActiveImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt { + objc_setAssociatedObject(self, @selector(af_activeImageDownloadReceipt), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end @@ -62,46 +46,13 @@ #pragma mark - @implementation UIImageView (AFNetworking) -@dynamic imageResponseSerializer; -+ (id )sharedImageCache { - static AFImageCache *_af_defaultImageCache = nil; - static dispatch_once_t oncePredicate; - dispatch_once(&oncePredicate, ^{ - _af_defaultImageCache = [[AFImageCache alloc] init]; - - [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * __unused notification) { - [_af_defaultImageCache removeAllObjects]; - }]; - }); - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" - return objc_getAssociatedObject(self, @selector(sharedImageCache)) ?: _af_defaultImageCache; -#pragma clang diagnostic pop ++ (AFImageDownloader *)sharedImageDownloader { + return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance]; } -+ (void)setSharedImageCache:(id )imageCache { - objc_setAssociatedObject(self, @selector(sharedImageCache), imageCache, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -#pragma mark - - -- (id )imageResponseSerializer { - static id _af_defaultImageResponseSerializer = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - _af_defaultImageResponseSerializer = [AFImageResponseSerializer serializer]; - }); - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu" - return objc_getAssociatedObject(self, @selector(imageResponseSerializer)) ?: _af_defaultImageResponseSerializer; -#pragma clang diagnostic pop -} - -- (void)setImageResponseSerializer:(id )serializer { - objc_setAssociatedObject(self, @selector(imageResponseSerializer), serializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); ++ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader { + objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } #pragma mark - @@ -121,93 +72,84 @@ - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage - success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success - failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure { - [self cancelImageRequestOperation]; - UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:urlRequest]; + if ([urlRequest URL] == nil) { + [self cancelImageDownloadTask]; + self.image = placeholderImage; + return; + } + + if ([self isActiveTaskURLEqualToURLRequest:urlRequest]){ + return; + } + + [self cancelImageDownloadTask]; + + AFImageDownloader *downloader = [[self class] sharedImageDownloader]; + id imageCache = downloader.imageCache; + + //Use the image from the image cache if it exists + UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil]; if (cachedImage) { if (success) { success(urlRequest, nil, cachedImage); } else { self.image = cachedImage; } - - self.af_imageRequestOperation = nil; + [self clearActiveDownloadInformation]; } else { if (placeholderImage) { self.image = placeholderImage; } __weak __typeof(self)weakSelf = self; - self.af_imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest]; - self.af_imageRequestOperation.responseSerializer = self.imageResponseSerializer; - [self.af_imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { - __strong __typeof(weakSelf)strongSelf = weakSelf; - if ([[urlRequest URL] isEqual:[strongSelf.af_imageRequestOperation.request URL]]) { - if (success) { - success(urlRequest, operation.response, responseObject); - } else if (responseObject) { - strongSelf.image = responseObject; - } + NSUUID *downloadID = [NSUUID UUID]; + AFImageDownloadReceipt *receipt; + receipt = [downloader + downloadImageForURLRequest:urlRequest + withReceiptID:downloadID + success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) { + if (success) { + success(request, response, responseObject); + } else if(responseObject) { + strongSelf.image = responseObject; + } + [strongSelf clearActiveDownloadInformation]; + } - if (operation == strongSelf.af_imageRequestOperation){ - strongSelf.af_imageRequestOperation = nil; - } - } + } + failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) { + if (failure) { + failure(request, response, error); + } + [strongSelf clearActiveDownloadInformation]; + } + }]; - [[[strongSelf class] sharedImageCache] cacheImage:responseObject forRequest:urlRequest]; - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - __strong __typeof(weakSelf)strongSelf = weakSelf; - if ([[urlRequest URL] isEqual:[strongSelf.af_imageRequestOperation.request URL]]) { - if (failure) { - failure(urlRequest, operation.response, error); - } - - if (operation == strongSelf.af_imageRequestOperation){ - strongSelf.af_imageRequestOperation = nil; - } - } - }]; - - [[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation]; + self.af_activeImageDownloadReceipt = receipt; } } -- (void)cancelImageRequestOperation { - [self.af_imageRequestOperation cancel]; - self.af_imageRequestOperation = nil; +- (void)cancelImageDownloadTask { + if (self.af_activeImageDownloadReceipt != nil) { + [[self.class sharedImageDownloader] cancelTaskForImageDownloadReceipt:self.af_activeImageDownloadReceipt]; + [self clearActiveDownloadInformation]; + } } -@end - -#pragma mark - - -static inline NSString * AFImageCacheKeyFromURLRequest(NSURLRequest *request) { - return [[request URL] absoluteString]; +- (void)clearActiveDownloadInformation { + self.af_activeImageDownloadReceipt = nil; } -@implementation AFImageCache - -- (UIImage *)cachedImageForRequest:(NSURLRequest *)request { - switch ([request cachePolicy]) { - case NSURLRequestReloadIgnoringCacheData: - case NSURLRequestReloadIgnoringLocalAndRemoteCacheData: - return nil; - default: - break; - } - - return [self objectForKey:AFImageCacheKeyFromURLRequest(request)]; -} - -- (void)cacheImage:(UIImage *)image - forRequest:(NSURLRequest *)request -{ - if (image && request) { - [self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)]; - } +- (BOOL)isActiveTaskURLEqualToURLRequest:(NSURLRequest *)urlRequest { + return [self.af_activeImageDownloadReceipt.task.originalRequest.URL.absoluteString isEqualToString:urlRequest.URL.absoluteString]; } @end diff --git a/clients/ios/Other Sources/AFNetworking/UIKit+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIKit+AFNetworking.h new file mode 100755 index 000000000..febacfc75 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIKit+AFNetworking.h @@ -0,0 +1,42 @@ +// UIKit+AFNetworking.h +// +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if TARGET_OS_IOS || TARGET_OS_TV +#import + +#ifndef _UIKIT_AFNETWORKING_ + #define _UIKIT_AFNETWORKING_ + +#if TARGET_OS_IOS + #import "AFAutoPurgingImageCache.h" + #import "AFImageDownloader.h" + #import "AFNetworkActivityIndicatorManager.h" + #import "UIRefreshControl+AFNetworking.h" + #import "UIWebView+AFNetworking.h" +#endif + + #import "UIActivityIndicatorView+AFNetworking.h" + #import "UIButton+AFNetworking.h" + #import "UIImageView+AFNetworking.h" + #import "UIProgressView+AFNetworking.h" +#endif /* _UIKIT_AFNETWORKING_ */ +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIProgressView+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIProgressView+AFNetworking.h new file mode 100755 index 000000000..8ea0a731d --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIProgressView+AFNetworking.h @@ -0,0 +1,64 @@ +// UIProgressView+AFNetworking.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import + +NS_ASSUME_NONNULL_BEGIN + + +/** + This category adds methods to the UIKit framework's `UIProgressView` class. The methods in this category provide support for binding the progress to the upload and download progress of a session task. + */ +@interface UIProgressView (AFNetworking) + +///------------------------------------ +/// @name Setting Session Task Progress +///------------------------------------ + +/** + Binds the progress to the upload progress of the specified session task. + + @param task The session task. + @param animated `YES` if the change should be animated, `NO` if the change should happen immediately. + */ +- (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task + animated:(BOOL)animated; + +/** + Binds the progress to the download progress of the specified session task. + + @param task The session task. + @param animated `YES` if the change should be animated, `NO` if the change should happen immediately. + */ +- (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task + animated:(BOOL)animated; + +@end + +NS_ASSUME_NONNULL_END + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIProgressView+AFNetworking.m b/clients/ios/Other Sources/AFNetworking/UIProgressView+AFNetworking.m new file mode 100755 index 000000000..2ae753eca --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIProgressView+AFNetworking.m @@ -0,0 +1,126 @@ +// UIProgressView+AFNetworking.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "UIProgressView+AFNetworking.h" + +#import + +#if TARGET_OS_IOS || TARGET_OS_TV + +#import "AFURLSessionManager.h" + +static void * AFTaskCountOfBytesSentContext = &AFTaskCountOfBytesSentContext; +static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedContext; + +#pragma mark - + +@implementation UIProgressView (AFNetworking) + +- (BOOL)af_uploadProgressAnimated { + return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_uploadProgressAnimated)) boolValue]; +} + +- (void)af_setUploadProgressAnimated:(BOOL)animated { + objc_setAssociatedObject(self, @selector(af_uploadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (BOOL)af_downloadProgressAnimated { + return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_downloadProgressAnimated)) boolValue]; +} + +- (void)af_setDownloadProgressAnimated:(BOOL)animated { + objc_setAssociatedObject(self, @selector(af_downloadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +#pragma mark - + +- (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task + animated:(BOOL)animated +{ + if (task.state == NSURLSessionTaskStateCompleted) { + return; + } + + [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext]; + [task addObserver:self forKeyPath:@"countOfBytesSent" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext]; + + [self af_setUploadProgressAnimated:animated]; +} + +- (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task + animated:(BOOL)animated +{ + if (task.state == NSURLSessionTaskStateCompleted) { + return; + } + + [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext]; + [task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext]; + + [self af_setDownloadProgressAnimated:animated]; +} + +#pragma mark - NSKeyValueObserving + +- (void)observeValueForKeyPath:(NSString *)keyPath + ofObject:(id)object + change:(__unused NSDictionary *)change + context:(void *)context +{ + if (context == AFTaskCountOfBytesSentContext || context == AFTaskCountOfBytesReceivedContext) { + if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesSent))]) { + if ([object countOfBytesExpectedToSend] > 0) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self setProgress:[object countOfBytesSent] / ([object countOfBytesExpectedToSend] * 1.0f) animated:self.af_uploadProgressAnimated]; + }); + } + } + + if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) { + if ([object countOfBytesExpectedToReceive] > 0) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self setProgress:[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f) animated:self.af_downloadProgressAnimated]; + }); + } + } + + if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) { + if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) { + @try { + [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))]; + + if (context == AFTaskCountOfBytesSentContext) { + [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))]; + } + + if (context == AFTaskCountOfBytesReceivedContext) { + [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))]; + } + } + @catch (NSException * __unused exception) {} + } + } + } +} + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIRefreshControl+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIRefreshControl+AFNetworking.h new file mode 100755 index 000000000..215eafcf7 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIRefreshControl+AFNetworking.h @@ -0,0 +1,53 @@ +// UIRefreshControl+AFNetworking.m +// +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import + +#if TARGET_OS_IOS + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + This category adds methods to the UIKit framework's `UIRefreshControl` class. The methods in this category provide support for automatically beginning and ending refreshing depending on the loading state of a session task. + */ +@interface UIRefreshControl (AFNetworking) + +///----------------------------------- +/// @name Refreshing for Session Tasks +///----------------------------------- + +/** + Binds the refreshing state to the state of the specified task. + + @param task The task. If `nil`, automatic updating from any previously specified operation will be disabled. + */ +- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task; + +@end + +NS_ASSUME_NONNULL_END + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIRefreshControl+AFNetworking.m b/clients/ios/Other Sources/AFNetworking/UIRefreshControl+AFNetworking.m new file mode 100755 index 000000000..cd46916a1 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIRefreshControl+AFNetworking.m @@ -0,0 +1,113 @@ +// UIRefreshControl+AFNetworking.m +// +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "UIRefreshControl+AFNetworking.h" +#import + +#if TARGET_OS_IOS + +#import "AFURLSessionManager.h" + +@interface AFRefreshControlNotificationObserver : NSObject +@property (readonly, nonatomic, weak) UIRefreshControl *refreshControl; +- (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl; + +- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task; + +@end + +@implementation UIRefreshControl (AFNetworking) + +- (AFRefreshControlNotificationObserver *)af_notificationObserver { + AFRefreshControlNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver)); + if (notificationObserver == nil) { + notificationObserver = [[AFRefreshControlNotificationObserver alloc] initWithActivityRefreshControl:self]; + objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } + return notificationObserver; +} + +- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task { + [[self af_notificationObserver] setRefreshingWithStateOfTask:task]; +} + +@end + +@implementation AFRefreshControlNotificationObserver + +- (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl +{ + self = [super init]; + if (self) { + _refreshControl = refreshControl; + } + return self; +} + +- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task { + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + + [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil]; + + if (task) { + UIRefreshControl *refreshControl = self.refreshControl; + if (task.state == NSURLSessionTaskStateRunning) { + [refreshControl beginRefreshing]; + + [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingTaskDidResumeNotification object:task]; + [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidCompleteNotification object:task]; + [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidSuspendNotification object:task]; + } else { + [refreshControl endRefreshing]; + } + } +} + +#pragma mark - + +- (void)af_beginRefreshing { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.refreshControl beginRefreshing]; + }); +} + +- (void)af_endRefreshing { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.refreshControl endRefreshing]; + }); +} + +#pragma mark - + +- (void)dealloc { + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + + [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil]; + [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil]; +} + +@end + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIWebView+AFNetworking.h b/clients/ios/Other Sources/AFNetworking/UIWebView+AFNetworking.h new file mode 100755 index 000000000..b9a56af4b --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIWebView+AFNetworking.h @@ -0,0 +1,80 @@ +// UIWebView+AFNetworking.h +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import + +#import + +#if TARGET_OS_IOS + +#import + +NS_ASSUME_NONNULL_BEGIN + +@class AFHTTPSessionManager; + +/** + This category adds methods to the UIKit framework's `UIWebView` class. The methods in this category provide increased control over the request cycle, including progress monitoring and success / failure handling. + + @discussion When using these category methods, make sure to assign `delegate` for the web view, which implements `–webView:shouldStartLoadWithRequest:navigationType:` appropriately. This allows for tapped links to be loaded through AFNetworking, and can ensure that `canGoBack` & `canGoForward` update their values correctly. + */ +@interface UIWebView (AFNetworking) + +/** + The session manager used to download all requests. + */ +@property (nonatomic, strong) AFHTTPSessionManager *sessionManager; + +/** + Asynchronously loads the specified request. + + @param request A URL request identifying the location of the content to load. This must not be `nil`. + @param progress A progress object monitoring the current download progress. + @param success A block object to be executed when the request finishes loading successfully. This block returns the HTML string to be loaded by the web view, and takes two arguments: the response, and the response string. + @param failure A block object to be executed when the data task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error that occurred. + */ +- (void)loadRequest:(NSURLRequest *)request + progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress + success:(nullable NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success + failure:(nullable void (^)(NSError *error))failure; + +/** + Asynchronously loads the data associated with a particular request with a specified MIME type and text encoding. + + @param request A URL request identifying the location of the content to load. This must not be `nil`. + @param MIMEType The MIME type of the content. Defaults to the content type of the response if not specified. + @param textEncodingName The IANA encoding name, as in `utf-8` or `utf-16`. Defaults to the response text encoding if not specified. +@param progress A progress object monitoring the current download progress. + @param success A block object to be executed when the request finishes loading successfully. This block returns the data to be loaded by the web view and takes two arguments: the response, and the downloaded data. + @param failure A block object to be executed when the data task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error that occurred. + */ +- (void)loadRequest:(NSURLRequest *)request + MIMEType:(nullable NSString *)MIMEType + textEncodingName:(nullable NSString *)textEncodingName + progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress + success:(nullable NSData * (^)(NSHTTPURLResponse *response, NSData *data))success + failure:(nullable void (^)(NSError *error))failure; + +@end + +NS_ASSUME_NONNULL_END + +#endif diff --git a/clients/ios/Other Sources/AFNetworking/UIWebView+AFNetworking.m b/clients/ios/Other Sources/AFNetworking/UIWebView+AFNetworking.m new file mode 100755 index 000000000..030c3e945 --- /dev/null +++ b/clients/ios/Other Sources/AFNetworking/UIWebView+AFNetworking.m @@ -0,0 +1,157 @@ +// UIWebView+AFNetworking.m +// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ ) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#import "UIWebView+AFNetworking.h" + +#import + +#if TARGET_OS_IOS + +#import "AFHTTPSessionManager.h" +#import "AFURLResponseSerialization.h" +#import "AFURLRequestSerialization.h" + +@interface UIWebView (_AFNetworking) +@property (readwrite, nonatomic, strong, setter = af_setURLSessionTask:) NSURLSessionDataTask *af_URLSessionTask; +@end + +@implementation UIWebView (_AFNetworking) + +- (NSURLSessionDataTask *)af_URLSessionTask { + return (NSURLSessionDataTask *)objc_getAssociatedObject(self, @selector(af_URLSessionTask)); +} + +- (void)af_setURLSessionTask:(NSURLSessionDataTask *)af_URLSessionTask { + objc_setAssociatedObject(self, @selector(af_URLSessionTask), af_URLSessionTask, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +@end + +#pragma mark - + +@implementation UIWebView (AFNetworking) + +- (AFHTTPSessionManager *)sessionManager { + static AFHTTPSessionManager *_af_defaultHTTPSessionManager = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _af_defaultHTTPSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; + _af_defaultHTTPSessionManager.requestSerializer = [AFHTTPRequestSerializer serializer]; + _af_defaultHTTPSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer]; + }); + + return objc_getAssociatedObject(self, @selector(sessionManager)) ?: _af_defaultHTTPSessionManager; +} + +- (void)setSessionManager:(AFHTTPSessionManager *)sessionManager { + objc_setAssociatedObject(self, @selector(sessionManager), sessionManager, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (AFHTTPResponseSerializer *)responseSerializer { + static AFHTTPResponseSerializer *_af_defaultResponseSerializer = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _af_defaultResponseSerializer = [AFHTTPResponseSerializer serializer]; + }); + + return objc_getAssociatedObject(self, @selector(responseSerializer)) ?: _af_defaultResponseSerializer; +} + +- (void)setResponseSerializer:(AFHTTPResponseSerializer *)responseSerializer { + objc_setAssociatedObject(self, @selector(responseSerializer), responseSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +#pragma mark - + +- (void)loadRequest:(NSURLRequest *)request + progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress + success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success + failure:(void (^)(NSError *error))failure +{ + [self loadRequest:request MIMEType:nil textEncodingName:nil progress:progress success:^NSData *(NSHTTPURLResponse *response, NSData *data) { + NSStringEncoding stringEncoding = NSUTF8StringEncoding; + if (response.textEncodingName) { + CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName); + if (encoding != kCFStringEncodingInvalidId) { + stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding); + } + } + + NSString *string = [[NSString alloc] initWithData:data encoding:stringEncoding]; + if (success) { + string = success(response, string); + } + + return [string dataUsingEncoding:stringEncoding]; + } failure:failure]; +} + +- (void)loadRequest:(NSURLRequest *)request + MIMEType:(NSString *)MIMEType + textEncodingName:(NSString *)textEncodingName + progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress + success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(request); + + if (self.af_URLSessionTask.state == NSURLSessionTaskStateRunning || self.af_URLSessionTask.state == NSURLSessionTaskStateSuspended) { + [self.af_URLSessionTask cancel]; + } + self.af_URLSessionTask = nil; + + __weak __typeof(self)weakSelf = self; + __block NSURLSessionDataTask *dataTask; + dataTask = [self.sessionManager + dataTaskWithRequest:request + uploadProgress:nil + downloadProgress:nil + completionHandler:^(NSURLResponse * _Nonnull response, id _Nonnull responseObject, NSError * _Nullable error) { + __strong __typeof(weakSelf) strongSelf = weakSelf; + if (error) { + if (failure) { + failure(error); + } + } else { + if (success) { + success((NSHTTPURLResponse *)response, responseObject); + } + [strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[dataTask.currentRequest URL]]; + + if ([strongSelf.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) { + [strongSelf.delegate webViewDidFinishLoad:strongSelf]; + } + } + }]; + self.af_URLSessionTask = dataTask; + if (progress != nil) { + *progress = [self.sessionManager downloadProgressForTask:dataTask]; + } + [self.af_URLSessionTask resume]; + + if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) { + [self.delegate webViewDidStartLoad:self]; + } +} + +@end + +#endif diff --git a/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.h b/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.h index ccc9744c9..4e8cb0ed4 100755 --- a/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.h +++ b/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.h @@ -15,15 +15,15 @@ @interface ARChromeActivity : UIActivity -// Empty by default. Either set this in the initializer, or set this property. -@property (strong, nonatomic) NSURL *callbackURL; - -// Uses the "CFBundleName" from your Info.plist by default. +/// Uses the "CFBundleName" from your Info.plist by default. @property (strong, nonatomic) NSString *callbackSource; -// The text beneath the icon. Defaults to "Chrome". +/// The text beneath the icon. Defaults to "Chrome". @property (strong, nonatomic) NSString *activityTitle; +/// Empty by default. Either set this in the initializer, or set this property. For iOS 9+, make sure you register the Chrome URL scheme in your Info.plist. See the demo project. +@property (strong, nonatomic) NSURL *callbackURL; + - (id)initWithCallbackURL:(NSURL *)callbackURL; @end diff --git a/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.m b/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.m index 554307dea..b1be0fdcf 100755 --- a/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.m +++ b/clients/ios/Other Sources/ARChromeActivity/ARChromeActivity.m @@ -22,7 +22,8 @@ @synthesize activityTitle = _activityTitle; static NSString *encodeByAddingPercentEscapes(NSString *input) { - NSString *encodedValue = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8)); + NSString *encodedValue = [input stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + return encodedValue; } @@ -49,7 +50,11 @@ static NSString *encodeByAddingPercentEscapes(NSString *input) { } - (UIImage *)activityImage { - return [UIImage imageNamed:@"ARChromeActivity"]; + if ([[UIImage class] respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)]) { + return [UIImage imageNamed:@"ARChromeActivity" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil]; + } else { + return [UIImage imageNamed:@"ARChromeActivity"]; + } } - (NSString *)activityType { @@ -57,24 +62,31 @@ static NSString *encodeByAddingPercentEscapes(NSString *input) { } - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems { - if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome-x-callback://"]]) { - return NO; - } - for (id item in activityItems){ - if ([item isKindOfClass:NSURL.class]){ - return YES; - } - } - return NO; + if (_callbackURL && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome-x-callback://"]]) { + return NO; + } + for (id item in activityItems){ + if ([item isKindOfClass:NSURL.class]){ + NSURL *url = (NSURL *)item; + if ([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) { + return YES; + } + } + } + return NO; } - (void)prepareWithActivityItems:(NSArray *)activityItems { - for (id item in activityItems) { - if ([item isKindOfClass:NSURL.class]) { - _activityURL = (NSURL *)item; - return; - } - } + for (id item in activityItems) { + if ([item isKindOfClass:NSURL.class]) { + NSURL *url = (NSURL *)item; + if ([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) { + _activityURL = (NSURL *)item; + return; + } + + } + } } - (void)performActivity { @@ -83,8 +95,8 @@ static NSString *encodeByAddingPercentEscapes(NSString *input) { NSString *sourceName = encodeByAddingPercentEscapes(self.callbackSource); NSURL *activityURL = [NSURL URLWithString:[NSString stringWithFormat:@"googlechrome-x-callback://x-callback-url/open/?url=%@&x-success=%@&x-source=%@", openingURL, callbackURL, sourceName]]; - [[UIApplication sharedApplication] openURL:activityURL]; + [[UIApplication sharedApplication] openURL:activityURL options:@{} completionHandler:nil]; [self activityDidFinish:YES]; } -@end \ No newline at end of file +@end diff --git a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.h b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.h index 47d671a48..33a21743a 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.h +++ b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.h @@ -28,8 +28,6 @@ - (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController*)sender; @optional -- (void)settingsViewControllerWillAppear:(IASKAppSettingsViewController*)sender; - #pragma mark - UITableView header customization - (CGFloat) settingsViewController:(id)settingsViewController tableView:(UITableView *)tableView diff --git a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m index 87859ae33..f900d23a7 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m +++ b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m @@ -25,6 +25,7 @@ #import "IASKSpecifier.h" #import "IASKSpecifierValuesViewController.h" #import "IASKTextField.h" +#import "IASKTextViewCell.h" #import "IASKMultipleValueSelection.h" #if !__has_feature(objc_arc) @@ -40,7 +41,7 @@ static NSString *kIASKCredits = @"Powered by InAppSettingsKit"; // Leave this as CGRect IASKCGRectSwap(CGRect rect); -@interface IASKAppSettingsViewController () { +@interface IASKAppSettingsViewController () { IASKSettingsReader *_settingsReader; id _settingsStore; @@ -52,6 +53,7 @@ CGRect IASKCGRectSwap(CGRect rect); } @property (nonatomic, strong) id currentFirstResponder; +@property (nonatomic, strong) NSMutableDictionary *rowHeights; - (void)_textChanged:(id)sender; - (void)synchronizeSettings; @@ -105,11 +107,10 @@ CGRect IASKCGRectSwap(CGRect rect); for (int i = 0; i < _settingsReader.numberOfSections; i++) { IASKSpecifier *specifier = [self.settingsReader headerSpecifierForSection:i]; if ([specifier.type isEqualToString:kIASKPSRadioGroupSpecifier]) { - IASKMultipleValueSelection *selection = [IASKMultipleValueSelection new]; + IASKMultipleValueSelection *selection = [[IASKMultipleValueSelection alloc] initWithSettingsStore:self.settingsStore]; selection.tableView = self.tableView; selection.specifier = specifier; selection.section = i; - selection.settingsStore = self.settingsStore; [sectionSelection addObject:selection]; } else { [sectionSelection addObject:[NSNull null]]; @@ -166,6 +167,7 @@ CGRect IASKCGRectSwap(CGRect rect); _reloadDisabled = NO; _showDoneButton = YES; _showCreditsFooter = YES; // display credits for InAppSettingsKit creators + self.rowHeights = [NSMutableDictionary dictionary]; } - (void)viewDidLoad { @@ -182,14 +184,20 @@ CGRect IASKCGRectSwap(CGRect rect); } - (void)viewWillAppear:(BOOL)animated { + NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; + + [super viewWillAppear:animated]; + // if there's something selected, the value might have changed // so reload that row - NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; if(selectedIndexPath) { - [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath] - withRowAnimation:UITableViewRowAnimationNone]; - // and reselect it, so we get the nice default deselect animation from UITableViewController - [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animated * UINavigationControllerHideShowBarDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath] + withRowAnimation:UITableViewRowAnimationNone]; + // and reselect it, so we get the nice default deselect animation from UITableViewController + [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; + [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES]; + }); } if (_showDoneButton) { @@ -209,15 +217,9 @@ CGRect IASKCGRectSwap(CGRect rect); [dc addObserver:self selector:@selector(didChangeSettingViaIASK:) name:kIASKAppSettingChanged object:nil]; [self userDefaultsDidChange]; // force update in case of changes while we were hidden } - - if (self.delegate && [self.delegate conformsToProtocol:@protocol(IASKSettingsDelegate)]) { - [self.delegate settingsViewControllerWillAppear:self]; - } - - [super viewWillAppear:animated]; } -- (CGSize)contentSizeForViewInPopover { +- (CGSize)preferredContentSize { return [[self view] sizeThatFits:CGSizeMake(320, 2000)]; } @@ -395,7 +397,7 @@ CGRect IASKCGRectSwap(CGRect rect); } } [[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged - object:[toggle key] + object:self userInfo:[NSDictionary dictionaryWithObject:[self.settingsStore objectForKey:[toggle key]] forKey:[toggle key]]]; } @@ -404,7 +406,7 @@ CGRect IASKCGRectSwap(CGRect rect); IASKSlider *slider = (IASKSlider*)sender; [self.settingsStore setFloat:[slider value] forKey:[slider key]]; [[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged - object:[slider key] + object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:[slider value]] forKey:[slider key]]]; } @@ -423,7 +425,10 @@ CGRect IASKCGRectSwap(CGRect rect); - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath]; - if ([[specifier type] isEqualToString:kIASKCustomViewSpecifier]) { + if ([specifier.type isEqualToString:kIASKTextViewSpecifier]) { + CGFloat height = (CGFloat)[self.rowHeights[specifier.key] doubleValue]; + return height > 0 ? height : UITableViewAutomaticDimension; + } else if ([[specifier type] isEqualToString:kIASKCustomViewSpecifier]) { if ([self.delegate respondsToSelector:@selector(tableView:heightForSpecifier:)]) { return [self.delegate tableView:tableView heightForSpecifier:specifier]; } else { @@ -437,7 +442,8 @@ CGRect IASKCGRectSwap(CGRect rect); UIContentSizeCategoryMedium: @(44), UIContentSizeCategoryLarge: @(44), UIContentSizeCategoryExtraLarge: @(47)}; - return (CGFloat)[rowHeights[UIApplication.sharedApplication.preferredContentSizeCategory] doubleValue] ? : 51; + CGFloat rowHeight = (CGFloat)[rowHeights[UIApplication.sharedApplication.preferredContentSizeCategory] doubleValue]; + return rowHeight != 0 ? rowHeight : 51; ); return 44; } @@ -509,6 +515,9 @@ CGRect IASKCGRectSwap(CGRect rect); cell = [[IASKPSTextFieldSpecifierViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIASKPSTextFieldSpecifier]; [((IASKPSTextFieldSpecifierViewCell*)cell).textField addTarget:self action:@selector(_textChanged:) forControlEvents:UIControlEventEditingChanged]; } + else if ([identifier hasPrefix:kIASKTextViewSpecifier]) { + cell = [[IASKTextViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIASKTextViewSpecifier]; + } else if ([identifier hasPrefix:kIASKPSSliderSpecifier]) { cell = [[IASKPSSliderSpecifierViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIASKPSSliderSpecifier]; } else if ([identifier hasPrefix:kIASKPSChildPaneSpecifier]) { @@ -590,6 +599,7 @@ CGRect IASKCGRectSwap(CGRect rect); } IASKTextField *textField = ((IASKPSTextFieldSpecifierViewCell*)cell).textField; textField.text = textValue; + textField.placeholder = specifier.placeholder; textField.key = specifier.key; textField.delegate = self; textField.secureTextEntry = [specifier isSecure]; @@ -603,6 +613,19 @@ CGRect IASKCGRectSwap(CGRect rect); textField.textAlignment = specifier.textAlignment; textField.adjustsFontSizeToFitWidth = specifier.adjustsFontSizeToFitWidth; } + else if ([specifier.type isEqualToString:kIASKTextViewSpecifier]) { + IASKTextViewCell *textCell = (id)cell; + NSString *value = [self.settingsStore objectForKey:specifier.key] != nil ? [self.settingsStore objectForKey:specifier.key] : specifier.defaultStringValue; + textCell.textView.text = value; + textCell.textView.delegate = self; + textCell.textView.key = specifier.key; + textCell.textView.keyboardType = specifier.keyboardType; + textCell.textView.autocapitalizationType = specifier.autocapitalizationType; + textCell.textView.autocorrectionType = specifier.autoCorrectionType; + dispatch_async(dispatch_get_main_queue(), ^{ + [self cacheRowHeightForTextView:textCell.textView]; + }); + } else if ([specifier.type isEqualToString:kIASKPSSliderSpecifier]) { if (specifier.minimumValueImage.length > 0) { ((IASKPSSliderSpecifierViewCell*)cell).minImage.image = [UIImage imageWithContentsOfFile:[_settingsReader pathForImageNamed:specifier.minimumValueImage]]; @@ -629,7 +652,7 @@ CGRect IASKCGRectSwap(CGRect rect); cell.accessoryType = (specifier.textAlignment == NSTextAlignmentLeft) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; } else if ([specifier.type isEqualToString:kIASKButtonSpecifier]) { NSString *value = [self.settingsStore objectForKey:specifier.key]; - cell.textLabel.text = [value isKindOfClass:[NSString class]] ? [self.settingsReader titleForStringId:value] : specifier.title; + cell.textLabel.text = ([value isKindOfClass:NSString.class] && [self.settingsReader titleForId:value].length) ? [self.settingsReader titleForId:value] : specifier.title; cell.detailTextLabel.text = specifier.subtitle; IASK_IF_IOS7_OR_GREATER (if (specifier.textAlignment != NSTextAlignmentLeft) { @@ -639,7 +662,7 @@ CGRect IASKCGRectSwap(CGRect rect); cell.accessoryType = (specifier.textAlignment == NSTextAlignmentLeft) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; } else if ([specifier.type isEqualToString:kIASKPSRadioGroupSpecifier]) { NSInteger index = [specifier.multipleValues indexOfObject:specifier.radioGroupValue]; - cell.textLabel.text = [self.settingsReader titleForStringId:specifier.multipleTitles[index]]; + cell.textLabel.text = [self.settingsReader titleForId:specifier.multipleTitles[index]]; [_selections[indexPath.section] updateSelectionInCell:cell indexPath:indexPath]; } else { cell.textLabel.text = specifier.title; @@ -648,7 +671,7 @@ CGRect IASKCGRectSwap(CGRect rect); cell.imageView.image = specifier.cellImage; cell.imageView.highlightedImage = specifier.highlightedCellImage; - if (![specifier.type isEqualToString:kIASKPSMultiValueSpecifier] && ![specifier.type isEqualToString:kIASKPSTitleValueSpecifier] && ![specifier.type isEqualToString:kIASKPSTextFieldSpecifier]) { + if (![specifier.type isEqualToString:kIASKPSMultiValueSpecifier] && ![specifier.type isEqualToString:kIASKPSTitleValueSpecifier] && ![specifier.type isEqualToString:kIASKPSTextFieldSpecifier] && ![specifier.type isEqualToString:kIASKTextViewSpecifier]) { cell.textLabel.textAlignment = specifier.textAlignment; } cell.detailTextLabel.textAlignment = specifier.textAlignment; @@ -690,9 +713,8 @@ CGRect IASKCGRectSwap(CGRect rect); } else if ([[specifier type] isEqualToString:kIASKPSTextFieldSpecifier]) { IASKPSTextFieldSpecifierViewCell *textFieldCell = (id)[tableView cellForRowAtIndexPath:indexPath]; - [textFieldCell.textField becomeFirstResponder]; - - } else if ([[specifier type] isEqualToString:kIASKPSChildPaneSpecifier]) { + [textFieldCell.textField becomeFirstResponder]; + } else if ([[specifier type] isEqualToString:kIASKPSChildPaneSpecifier]) { if ([specifier viewControllerStoryBoardID]){ NSString *storyBoardFileFromSpecifier = [specifier viewControllerStoryBoardFile]; storyBoardFileFromSpecifier = storyBoardFileFromSpecifier && storyBoardFileFromSpecifier.length > 0 ? storyBoardFileFromSpecifier : [[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"]; @@ -705,6 +727,11 @@ CGRect IASKCGRectSwap(CGRect rect); Class vcClass = [specifier viewControllerClass]; if (vcClass) { + if (vcClass == [NSNull class]) { + NSLog(@"class '%@' not found", [specifier localizedObjectForKey:kIASKViewControllerClass]); + [tableView deselectRowAtIndexPath:indexPath animated:YES]; + return; + } SEL initSelector = [specifier viewControllerSelector]; if (!initSelector) { initSelector = @selector(init); @@ -723,6 +750,17 @@ CGRect IASKCGRectSwap(CGRect rect); IASK_IF_IOS7_OR_GREATER(vc.view.tintColor = self.view.tintColor;) [self.navigationController pushViewController:vc animated:YES]; return; + } + + NSString *segueIdentifier = [specifier segueIdentifier]; + if (segueIdentifier) { + @try { + [self performSegueWithIdentifier:segueIdentifier sender:self]; + } @catch (NSException *exception) { + NSLog(@"segue with identifier '%@' not defined", segueIdentifier); + [tableView deselectRowAtIndexPath:indexPath animated:YES]; + } + return; } if (nil == [specifier file]) { @@ -749,7 +787,7 @@ CGRect IASKCGRectSwap(CGRect rect); } else if ([[specifier type] isEqualToString:kIASKOpenURLSpecifier]) { [tableView deselectRowAtIndexPath:indexPath animated:YES]; - [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[specifier localizedObjectForKey:kIASKFile]]]; + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[specifier localizedObjectForKey:kIASKFile]] options:@{} completionHandler:nil]; } else if ([[specifier type] isEqualToString:kIASKButtonSpecifier]) { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if ([self.delegate respondsToSelector:@selector(settingsViewController:buttonTappedForSpecifier:)]) { @@ -827,6 +865,8 @@ CGRect IASKCGRectSwap(CGRect rect); }]; } else { + IASK_IF_PRE_IOS8 + ( UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Mail not configured", @"InAppSettingsKit") message:NSLocalizedString(@"This device is not configured for sending Email. Please configure the Mail settings in the Settings app.", @"InAppSettingsKit") @@ -834,8 +874,17 @@ CGRect IASKCGRectSwap(CGRect rect); cancelButtonTitle:NSLocalizedString(@"OK", @"InAppSettingsKit") otherButtonTitles:nil]; [alert show]; + ) + IASK_IF_IOS8_OR_GREATER + ( + UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Mail not configured", @"InAppSettingsKit") + message:NSLocalizedString(@"This device is not configured for sending Email. Please configure the Mail settings in the Settings app.", @"InAppSettingsKit") + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"InAppSettingsKit") style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}]]; + [self presentViewController:alert animated:YES completion:nil]; + ) } - + } else if ([[specifier type] isEqualToString:kIASKCustomViewSpecifier] && [self.delegate respondsToSelector:@selector(settingsViewController:tableView:didSelectCustomViewSpecifier:)]) { [self.delegate settingsViewController:self tableView:tableView didSelectCustomViewSpecifier:specifier]; } else if ([[specifier type] isEqualToString:kIASKPSRadioGroupSpecifier]) { @@ -866,16 +915,15 @@ CGRect IASKCGRectSwap(CGRect rect); #pragma mark - #pragma mark UITextFieldDelegate Functions -- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { +- (void)textFieldDidBeginEditing:(UITextField *)textField { self.currentFirstResponder = textField; - return YES; } - (void)_textChanged:(id)sender { IASKTextField *text = sender; [_settingsStore setObject:[text text] forKey:[text key]]; [[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged - object:[text key] + object:self userInfo:[NSDictionary dictionaryWithObject:[text text] forKey:[text key]]]; } @@ -890,6 +938,40 @@ CGRect IASKCGRectSwap(CGRect rect); [self.tableView endEditing:NO]; } +#pragma mark - UITextViewDelegate + +- (void)textViewDidEndEditing:(UITextView *)textView { + self.currentFirstResponder = textView; +} + +- (void)textViewDidChange:(IASKTextView *)textView { + [self cacheRowHeightForTextView:textView]; + + CGRect visibleTableRect = UIEdgeInsetsInsetRect(self.tableView.bounds, self.tableView.contentInset); + NSIndexPath *indexPath = [self.settingsReader indexPathForKey:textView.key]; + CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath]; + + if (!CGRectContainsRect(visibleTableRect, cellFrame)) { + [self.tableView scrollRectToVisible:CGRectInset(cellFrame, 0, - 30) animated:YES]; + } + + [_settingsStore setObject:textView.text forKey:textView.key]; + [[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged + object:textView.key + userInfo:@{textView.key: textView.text}]; + +} + +- (void)cacheRowHeightForTextView:(IASKTextView *)textView { + CGFloat maxHeight = self.tableView.bounds.size.height - self.tableView.contentInset.top - self.tableView.contentInset.bottom - 60; + CGFloat contentHeight = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, 10000)].height + 16; + self.rowHeights[textView.key] = @(MAX(44, MIN(maxHeight, contentHeight))); + textView.scrollEnabled = contentHeight > maxHeight; + + [self.tableView beginUpdates]; + [self.tableView endUpdates]; +} + #pragma mark Notifications - (void)synchronizeSettings { @@ -924,7 +1006,8 @@ static NSDictionary *oldUserDefaults = nil; } - (void)didChangeSettingViaIASK:(NSNotification*)notification { - [oldUserDefaults setValue:[self.settingsStore objectForKey:notification.object] forKey:notification.object]; + NSString *key = notification.userInfo.allKeys.firstObject; + [oldUserDefaults setValue:notification.userInfo[key] forKey:key]; } - (void)reload { diff --git a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsWebViewController.m b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsWebViewController.m index 96ccec832..c7aefa896 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsWebViewController.m +++ b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKAppSettingsWebViewController.m @@ -91,10 +91,9 @@ NSString *key = [[keyValue objectAtIndex:0] lowercaseString]; NSString *value = [keyValue objectAtIndex:1]; - value = CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, - (CFStringRef)value, - CFSTR(""), - kCFStringEncodingUTF8)); + value = CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, + (CFStringRef)value, + CFSTR(""))); if ([key isEqualToString:@"subject"]) { [mailViewController setSubject:value]; @@ -137,7 +136,7 @@ if (![newURL host] || [[newURL host] isEqualToString:[self.url host]]) { return YES; } - [[UIApplication sharedApplication] openURL:newURL]; + [[UIApplication sharedApplication] openURL:newURL options:@{} completionHandler:nil]; return NO; } diff --git a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.h b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.h index 98e2a113b..3b142de60 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.h +++ b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.h @@ -13,6 +13,7 @@ @property (nonatomic, copy, readonly) NSIndexPath *checkedItem; @property (nonatomic, strong) id settingsStore; +- (id)initWithSettingsStore:(id)settingsStore; - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath; - (void)updateSelectionInCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath; diff --git a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.m b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.m index d413cdd1a..cba41a2bf 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.m +++ b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.m @@ -11,6 +11,13 @@ @synthesize settingsStore = _settingsStore; +- (id)initWithSettingsStore:(id)settingsStore { + if ((self = [super init])) { + self.settingsStore = settingsStore; + } + return self; +} + - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil]; } @@ -76,7 +83,7 @@ [self.settingsStore setObject:[values objectAtIndex:indexPath.row] forKey:[_specifier key]]; [self.settingsStore synchronize]; [[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged - object:[_specifier key] + object:self userInfo:@{ _specifier.key: values[indexPath.row] }]; diff --git a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKSpecifierValuesViewController.m b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKSpecifierValuesViewController.m index 6b6a7eca0..9d16c94d3 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKSpecifierValuesViewController.m +++ b/clients/ios/Other Sources/InAppSettingsKit/Controllers/IASKSpecifierValuesViewController.m @@ -24,7 +24,7 @@ @interface IASKSpecifierValuesViewController() @property (nonatomic, strong, readonly) IASKMultipleValueSelection *selection; - +@property (nonatomic) BOOL didFirstLayout; @end @implementation IASKSpecifierValuesViewController @@ -62,17 +62,28 @@ if (_tableView) { [_tableView reloadData]; - - // Make sure the currently checked item is visible - [_tableView scrollToRowAtIndexPath:_selection.checkedItem - atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; + _selection.tableView = _tableView; } + self.didFirstLayout = NO; [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { - [_tableView flashScrollIndicators]; [super viewDidAppear:animated]; + [_tableView flashScrollIndicators]; +} + +- (void)viewDidLayoutSubviews { + [super viewDidLayoutSubviews]; + + if (!self.didFirstLayout) { + // Make sure the currently checked item is visible + // this needs to be done as early as possible when pushing the view but after the first layout + // otherwise scrolling to the first entry doesn't respect tableView.contentInset + [_tableView scrollToRowAtIndexPath:_selection.checkedItem + atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; + self.didFirstLayout = YES; + } } - (void)viewDidDisappear:(BOOL)animated { @@ -113,7 +124,7 @@ [_selection updateSelectionInCell:cell indexPath:indexPath]; @try { - [[cell textLabel] setText:[self.settingsReader titleForStringId:[titles objectAtIndex:indexPath.row]]]; + [[cell textLabel] setText:[self.settingsReader titleForId:[titles objectAtIndex:indexPath.row]]]; } @catch (NSException * e) {} return cell; @@ -123,7 +134,7 @@ [_selection selectRowAtIndexPath:indexPath]; } -- (CGSize)contentSizeForViewInPopover { +- (CGSize)preferredContentSize { return [[self view] sizeThatFits:CGSizeMake(320, 2000)]; } diff --git a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.h b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.h index 055dc0559..7e3701279 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.h +++ b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.h @@ -40,10 +40,12 @@ #define kIASKShortTitles @"ShortTitles" #define kIASKSupportedUserInterfaceIdioms @"SupportedUserInterfaceIdioms" #define kIASKSubtitle @"IASKSubtitle" +#define kIASKPlaceholder @"IASKPlaceholder" #define kIASKViewControllerClass @"IASKViewControllerClass" #define kIASKViewControllerSelector @"IASKViewControllerSelector" #define kIASKViewControllerStoryBoardFile @"IASKViewControllerStoryBoardFile" #define kIASKViewControllerStoryBoardId @"IASKViewControllerStoryBoardId" +#define kIASKSegueIdentifier @"IASKSegueIdentifier" #define kIASKButtonClass @"IASKButtonClass" #define kIASKButtonAction @"IASKButtonAction" #define kIASKMailComposeToRecipents @"IASKMailComposeToRecipents" @@ -85,6 +87,7 @@ #define kIASKPSTitleValueSpecifier @"PSTitleValueSpecifier" #define kIASKPSTextFieldSpecifier @"PSTextFieldSpecifier" #define kIASKPSChildPaneSpecifier @"PSChildPaneSpecifier" +#define kIASKTextViewSpecifier @"IASKTextViewSpecifier" #define kIASKOpenURLSpecifier @"IASKOpenURLSpecifier" #define kIASKButtonSpecifier @"IASKButtonSpecifier" #define kIASKMailComposeSpecifier @"IASKMailComposeSpecifier" @@ -174,6 +177,20 @@ _Pragma("clang diagnostic pop") #define IASK_IF_PRE_IOS7(...) __VA_ARGS__ #endif +#ifdef __IPHONE_8_0 +#define IASK_IF_PRE_IOS8(...) \ +_Pragma("clang diagnostic push") \ +_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \ +if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_8_0) \ +{ \ +__VA_ARGS__ \ +} \ +_Pragma("clang diagnostic pop") +#else +#define IASK_IF_PRE_IOS8(...) __VA_ARGS__ +#endif + + #ifdef __IPHONE_8_0 #define IASK_IF_IOS8_OR_GREATER(...) \ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0) \ @@ -216,7 +233,7 @@ __VA_ARGS__ \ - (NSString*)titleForSection:(NSInteger)section; - (NSString*)keyForSection:(NSInteger)section; - (NSString*)footerTextForSection:(NSInteger)section; -- (NSString*)titleForStringId:(NSString*)stringId; +- (NSString*)titleForId:(NSObject*)titleId; - (NSString*)pathForImageNamed:(NSString*)image; ///the main application bundle. most often [NSBundle mainBundle] diff --git a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.m b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.m index 0d10774fd..4a98883df 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.m +++ b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSettingsReader.m @@ -99,12 +99,12 @@ } - (NSArray*)privacySettingsSpecifiers { - NSMutableDictionary *dict = [@{kIASKTitle: NSLocalizedStringFromTable(@"Privacy", @"IASKLocalizable", @"iOS 8+ Privacy cell: title"), + NSMutableDictionary *dict = [@{kIASKTitle: [[self getBundle] localizedStringForKey:@"Privacy" value:@"" table:@"IASKLocalizable"], kIASKKey: @"IASKPrivacySettingsCellKey", kIASKType: kIASKOpenURLSpecifier, kIASKFile: UIApplicationOpenSettingsURLString, } mutableCopy]; - NSString *subtitle = NSLocalizedStringWithDefaultValue(@"Open in Settings app", @"IASKLocalizable", [NSBundle mainBundle], @"", @"iOS 8+ Privacy cell: subtitle"); + NSString *subtitle = [[self getBundle] localizedStringForKey:@"Open in Settings app" value:@"" table:@"IASKLocalizable"]; if (subtitle.length) { dict [kIASKSubtitle] = subtitle; } @@ -113,6 +113,19 @@ [[IASKSpecifier alloc] initWithSpecifier:dict]]]; } +- (NSBundle*)getBundle { + NSURL *inAppSettingsBundlePath = [[NSBundle bundleForClass:[self class]] URLForResource:@"InAppSettingsKit" withExtension:@"bundle"]; + NSBundle *bundle; + + if (inAppSettingsBundlePath) { + bundle = [NSBundle bundleWithURL:inAppSettingsBundlePath]; + } else { + bundle = [NSBundle mainBundle]; + } + + return bundle; +} + - (void)_reinterpretBundle:(NSDictionary*)settingsBundle { NSArray *preferenceSpecifiers = [settingsBundle objectForKey:kIASKPreferenceSpecifiers]; NSMutableArray *dataSource = [NSMutableArray array]; @@ -222,7 +235,7 @@ } - (NSString*)titleForSection:(NSInteger)section { - return [self titleForStringId:[self headerSpecifierForSection:section].title]; + return [self titleForId:[self headerSpecifierForSection:section].title]; } - (NSString*)keyForSection:(NSInteger)section { @@ -230,11 +243,22 @@ } - (NSString*)footerTextForSection:(NSInteger)section { - return [self titleForStringId:[self headerSpecifierForSection:section].footerText]; + return [self titleForId:[self headerSpecifierForSection:section].footerText]; } -- (NSString*)titleForStringId:(NSString*)stringId { - return [self.settingsBundle localizedStringForKey:stringId value:stringId table:self.localizationTable]; +- (NSString*)titleForId:(NSObject*)titleId +{ + if([titleId isKindOfClass:[NSNumber class]]) { + NSNumber* numberTitleId = (NSNumber*)titleId; + NSNumberFormatter* formatter = [NSNumberFormatter new]; + [formatter setNumberStyle:NSNumberFormatterNoStyle]; + return [formatter stringFromNumber:numberTitleId]; + } + else + { + NSString* stringTitleId = (NSString*)titleId; + return [self.settingsBundle localizedStringForKey:stringTitleId value:stringTitleId table:self.localizationTable]; + } } - (NSString*)pathForImageNamed:(NSString*)image { diff --git a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.h b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.h index 080f3fa55..2edb94092 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.h +++ b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.h @@ -34,6 +34,7 @@ - (NSString*)localizedObjectForKey:(NSString*)key; - (NSString*)title; - (NSString*)subtitle; +- (NSString*)placeholder; - (NSString*)key; - (NSString*)type; - (NSString*)titleForCurrentValue:(id)currentValue; @@ -60,6 +61,7 @@ - (SEL)viewControllerSelector; - (NSString*)viewControllerStoryBoardFile; - (NSString*)viewControllerStoryBoardID; +- (NSString*)segueIdentifier; - (Class)buttonClass; - (SEL)buttonAction; - (UIImage *)cellImage; diff --git a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.m b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.m index a5124db4d..1ca76dc57 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.m +++ b/clients/ios/Other Sources/InAppSettingsKit/Models/IASKSpecifier.m @@ -95,7 +95,7 @@ static NSString *const valueKey = @"value"; IASKSettingsReader *strongSettingsReader = self.settingsReader; [titles enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - NSString *localizedTitle = [strongSettingsReader titleForStringId:obj]; + NSString *localizedTitle = [strongSettingsReader titleForId:obj]; [temporaryMappingsForSort addObject:@{titleKey : obj, valueKey : values[idx], localizedTitleKey : localizedTitle, @@ -152,7 +152,7 @@ - (NSString*)localizedObjectForKey:(NSString*)key { IASKSettingsReader *settingsReader = self.settingsReader; - return [settingsReader titleForStringId:[_specifierDict objectForKey:key]]; + return [settingsReader titleForId:[_specifierDict objectForKey:key]]; } - (NSString*)title { @@ -163,13 +163,18 @@ return [self localizedObjectForKey:kIASKSubtitle]; } +- (NSString *)placeholder { + return [self localizedObjectForKey:kIASKPlaceholder]; +} + - (NSString*)footerText { return [self localizedObjectForKey:kIASKFooterText]; } - (Class)viewControllerClass { [IASKAppSettingsWebViewController class]; // make sure this is linked into the binary/library - return [self classFromString:([_specifierDict objectForKey:kIASKViewControllerClass])]; + NSString *classString = [_specifierDict objectForKey:kIASKViewControllerClass]; + return classString ? ([self classFromString:classString] ?: [NSNull class]) : nil; } - (Class)classFromString:(NSString *)className { @@ -195,6 +200,10 @@ return [_specifierDict objectForKey:kIASKViewControllerStoryBoardId]; } +- (NSString*)segueIdentifier { + return [_specifierDict objectForKey:kIASKSegueIdentifier]; +} + - (Class)buttonClass { return NSClassFromString([_specifierDict objectForKey:kIASKButtonClass]); } @@ -226,7 +235,7 @@ } @try { IASKSettingsReader *strongSettingsReader = self.settingsReader; - return [strongSettingsReader titleForStringId:[titles objectAtIndex:keyIndex]]; + return [strongSettingsReader titleForId:[titles objectAtIndex:keyIndex]]; } @catch (NSException * e) {} return nil; @@ -393,7 +402,7 @@ } if ([self.type isEqualToString:kIASKButtonSpecifier] && !self.cellImage) { return NSTextAlignmentCenter; - } else if ([self.type isEqualToString:kIASKPSMultiValueSpecifier] || [self.type isEqualToString:kIASKPSTitleValueSpecifier]) { + } else if ([self.type isEqualToString:kIASKPSMultiValueSpecifier] || [self.type isEqualToString:kIASKPSTitleValueSpecifier] || [self.type isEqualToString:kIASKTextViewSpecifier]) { return NSTextAlignmentRight; } return NSTextAlignmentLeft; diff --git a/clients/ios/Other Sources/InAppSettingsKit/Resources/nl.lproj/IASKLocalizable.strings b/clients/ios/Other Sources/InAppSettingsKit/Resources/nl.lproj/IASKLocalizable.strings index 6089b876d..0c6fc425b 100755 --- a/clients/ios/Other Sources/InAppSettingsKit/Resources/nl.lproj/IASKLocalizable.strings +++ b/clients/ios/Other Sources/InAppSettingsKit/Resources/nl.lproj/IASKLocalizable.strings @@ -8,4 +8,4 @@ "Privacy" = "Privacy"; // iOS 8+ Privacy cell: title -"Open in Settings app" = ""; // iOS 8+ Privacy cell: subtitle (TODO) +"Open in Settings app" = "In “Instellingen” app openen"; // iOS 8+ Privacy cell: subtitle diff --git a/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextView.h b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextView.h new file mode 100755 index 000000000..af78cbe8c --- /dev/null +++ b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextView.h @@ -0,0 +1,23 @@ +// +// IASKTextView.h +// http://www.inappsettingskit.com +// +// Copyright (c) 2009-2015: +// Luc Vandal, Edovia Inc., http://www.edovia.com +// Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com +// All rights reserved. +// +// It is appreciated but not required that you give credit to Luc Vandal and Ortwin Gentz, +// as the original authors of this code. You can give credit in a blog post, a tweet or on +// a info page of your app. Also, the original authors appreciate letting them know if you use this code. +// +// This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php +// + +#import + +@interface IASKTextView : UITextView + +@property (nonatomic, copy) NSString *key; + +@end diff --git a/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextView.m b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextView.m new file mode 100755 index 000000000..78265f52b --- /dev/null +++ b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextView.m @@ -0,0 +1,21 @@ +// +// IASKTextView.m +// http://www.inappsettingskit.com +// +// Copyright (c) 2009-2015: +// Luc Vandal, Edovia Inc., http://www.edovia.com +// Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com +// All rights reserved. +// +// It is appreciated but not required that you give credit to Luc Vandal and Ortwin Gentz, +// as the original authors of this code. You can give credit in a blog post, a tweet or on +// a info page of your app. Also, the original authors appreciate letting them know if you use this code. +// +// This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php +// + +#import "IASKTextView.h" + +@implementation IASKTextView + +@end diff --git a/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextViewCell.h b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextViewCell.h new file mode 100755 index 000000000..34cc38bab --- /dev/null +++ b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextViewCell.h @@ -0,0 +1,24 @@ +// +// IASKTextViewCell.h +// http://www.inappsettingskit.com +// +// Copyright (c) 2009-2015: +// Luc Vandal, Edovia Inc., http://www.edovia.com +// Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com +// All rights reserved. +// +// It is appreciated but not required that you give credit to Luc Vandal and Ortwin Gentz, +// as the original authors of this code. You can give credit in a blog post, a tweet or on +// a info page of your app. Also, the original authors appreciate letting them know if you use this code. +// +// This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php +// + +#import +#import "IASKTextView.h" + +@interface IASKTextViewCell : UITableViewCell + +@property (nonatomic, strong) IASKTextView *textView; + +@end diff --git a/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextViewCell.m b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextViewCell.m new file mode 100755 index 000000000..0a92ea5ef --- /dev/null +++ b/clients/ios/Other Sources/InAppSettingsKit/Views/IASKTextViewCell.m @@ -0,0 +1,55 @@ +// +// IASKTextViewCell.m +// http://www.inappsettingskit.com +// +// Copyright (c) 2009-2015: +// Luc Vandal, Edovia Inc., http://www.edovia.com +// Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com +// All rights reserved. +// +// It is appreciated but not required that you give credit to Luc Vandal and Ortwin Gentz, +// as the original authors of this code. You can give credit in a blog post, a tweet or on +// a info page of your app. Also, the original authors appreciate letting them know if you use this code. +// +// This code is licensed under the BSD license that is available at: http://www.opensource.org/licenses/bsd-license.php +// + +#import "IASKTextViewCell.h" +#import "IASKSettingsReader.h" + +@implementation IASKTextViewCell + + +- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { + if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) { + self.selectionStyle = UITableViewCellSelectionStyleNone; + self.accessoryType = UITableViewCellAccessoryNone; + + IASKTextView *textView = [[IASKTextView alloc] initWithFrame:CGRectZero]; + textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + textView.scrollEnabled = NO; + textView.font = [UIFont systemFontOfSize:17.0]; + textView.backgroundColor = [UIColor whiteColor]; + [self.contentView addSubview:textView]; + + self.textView = textView; + } + return self; +} + +- (void)layoutSubviews { + [super layoutSubviews]; + + UIEdgeInsets padding = (UIEdgeInsets) { 0, kIASKPaddingLeft, 0, kIASKPaddingRight }; + if ([self respondsToSelector:@selector(layoutMargins)]) { + padding = self.layoutMargins; + padding.left -= 5; + padding.right -= 5; + padding.top -= 5; + padding.bottom -= 5; + } + + self.textView.frame = UIEdgeInsetsInsetRect(self.bounds, padding); +} + +@end diff --git a/clients/ios/Other Sources/PINCache/Nullability.h b/clients/ios/Other Sources/PINCache/Nullability.h new file mode 100755 index 000000000..437a16f3d --- /dev/null +++ b/clients/ios/Other Sources/PINCache/Nullability.h @@ -0,0 +1,20 @@ +// PINCache is a modified version of TMCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. + +#ifndef PINCache_nullability_h +#define PINCache_nullability_h + +#if !__has_feature(nullability) +#define NS_ASSUME_NONNULL_BEGIN +#define NS_ASSUME_NONNULL_END +#define nullable +#define nonnull +#define null_unspecified +#define null_resettable +#define __nullable +#define __nonnull +#define __null_unspecified +#endif + +#endif diff --git a/clients/ios/Other Sources/TMCache/TMCache.h b/clients/ios/Other Sources/PINCache/PINCache.h similarity index 57% rename from clients/ios/Other Sources/TMCache/TMCache.h rename to clients/ios/Other Sources/PINCache/PINCache.h index 05b1879b9..0028cbc47 100755 --- a/clients/ios/Other Sources/TMCache/TMCache.h +++ b/clients/ios/Other Sources/PINCache/PINCache.h @@ -1,27 +1,50 @@ +// PINCache is a modified version of TMCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. + +#import + +#import "PINDiskCache.h" +#import "PINMemoryCache.h" + +NS_ASSUME_NONNULL_BEGIN + +@class PINCache; + /** - `TMCache` is a thread safe key/value store designed for persisting temporary objects that are expensive to + A callback block which provides only the cache as an argument + */ +typedef void (^PINCacheBlock)(PINCache *cache); + +/** + A callback block which provides the cache, key and object as arguments + */ +typedef void (^PINCacheObjectBlock)(PINCache *cache, NSString *key, id __nullable object); + +/** + A callback block which provides a BOOL value as argument + */ +typedef void (^PINCacheObjectContainmentBlock)(BOOL containsObject); + + +/** + `PINCache` is a thread safe key/value store designed for persisting temporary objects that are expensive to reproduce, such as downloaded data or the results of slow processing. It is comprised of two self-similar - stores, one in memory () and one on disk (). + stores, one in memory () and one on disk (). - `TMCache` itself actually does very little; its main function is providing a front end for a common use case: + `PINCache` itself actually does very little; its main function is providing a front end for a common use case: a small, fast memory cache that asynchronously persists itself to a large, slow disk cache. When objects are removed from the memory cache in response to an "apocalyptic" event they remain in the disk cache and are - repopulated in memory the next time they are accessed. `TMCache` also does the tedious work of creating a + repopulated in memory the next time they are accessed. `PINCache` also does the tedious work of creating a dispatch group to wait for both caches to finish their operations without blocking each other. The parallel caches are accessible as public properties ( and ) and can be manipulated - separately if necessary. See the docs for and for more details. + separately if necessary. See the docs for and for more details. + + @warning when using in extension or watch extension, define PIN_APP_EXTENSIONS=1 */ -#import "TMDiskCache.h" -#import "TMMemoryCache.h" - -@class TMCache; - -typedef void (^TMCacheBlock)(TMCache *cache); -typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); - -@interface TMCache : NSObject +@interface PINCache : NSObject #pragma mark - /// @name Core @@ -34,7 +57,7 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); /** A concurrent queue on which blocks passed to the asynchronous access methods are run. */ -@property (readonly) dispatch_queue_t queue; +@property (readonly) dispatch_queue_t concurrentQueue; /** Synchronously retrieves the total byte count of the on the shared disk queue. @@ -42,14 +65,14 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); @property (readonly) NSUInteger diskByteCount; /** - The underlying disk cache, see for additional configuration and trimming options. + The underlying disk cache, see for additional configuration and trimming options. */ -@property (readonly) TMDiskCache *diskCache; +@property (readonly) PINDiskCache *diskCache; /** - The underlying memory cache, see for additional configuration and trimming options. + The underlying memory cache, see for additional configuration and trimming options. */ -@property (readonly) TMMemoryCache *memoryCache; +@property (readonly) PINMemoryCache *memoryCache; #pragma mark - /// @name Initialization @@ -61,6 +84,8 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); */ + (instancetype)sharedCache; +- (instancetype)init NS_UNAVAILABLE; + /** Multiple instances with the same name are allowed and can safely access the same data on disk thanks to the magic of seriality. Also used to create the . @@ -72,7 +97,7 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); - (instancetype)initWithName:(NSString *)name; /** - The designated initializer. Multiple instances with the same name are allowed and can safely access + Multiple instances with the same name are allowed and can safely access the same data on disk thanks to the magic of seriality. Also used to create the . @see name @@ -80,70 +105,92 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); @param rootPath The path of the cache on disk. @result A new cache with the specified name. */ -- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath; +- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath NS_DESIGNATED_INITIALIZER; #pragma mark - /// @name Asynchronous Methods +/** + This method determines whether an object is present for the given key in the cache. This method returns immediately + and executes the passed block after the object is available, potentially in parallel with other blocks on the + . + + @see containsObjectForKey: + @param key The key associated with the object. + @param block A block to be executed concurrently after the containment check happened + */ +- (void)containsObjectForKey:(NSString *)key block:(PINCacheObjectContainmentBlock)block; + /** Retrieves the object for the specified key. This method returns immediately and executes the passed - block after the object is available, potentially in parallel with other blocks on the . + block after the object is available, potentially in parallel with other blocks on the . @param key The key associated with the requested object. @param block A block to be executed concurrently when the object is available. */ -- (void)objectForKey:(NSString *)key block:(TMCacheObjectBlock)block; +- (void)objectForKey:(NSString *)key block:(PINCacheObjectBlock)block; /** Stores an object in the cache for the specified key. This method returns immediately and executes the - passed block after the object has been stored, potentially in parallel with other blocks on the . + passed block after the object has been stored, potentially in parallel with other blocks on the . @param object An object to store in the cache. @param key A key to associate with the object. This string will be copied. @param block A block to be executed concurrently after the object has been stored, or nil. */ -- (void)setObject:(id )object forKey:(NSString *)key block:(TMCacheObjectBlock)block; +- (void)setObject:(id )object forKey:(NSString *)key block:(nullable PINCacheObjectBlock)block; /** Removes the object for the specified key. This method returns immediately and executes the passed - block after the object has been removed, potentially in parallel with other blocks on the . + block after the object has been removed, potentially in parallel with other blocks on the . @param key The key associated with the object to be removed. @param block A block to be executed concurrently after the object has been removed, or nil. */ -- (void)removeObjectForKey:(NSString *)key block:(TMCacheObjectBlock)block; +- (void)removeObjectForKey:(NSString *)key block:(nullable PINCacheObjectBlock)block; /** Removes all objects from the cache that have not been used since the specified date. This method returns immediately and - executes the passed block after the cache has been trimmed, potentially in parallel with other blocks on the . + executes the passed block after the cache has been trimmed, potentially in parallel with other blocks on the . @param date Objects that haven't been accessed since this date are removed from the cache. @param block A block to be executed concurrently after the cache has been trimmed, or nil. */ -- (void)trimToDate:(NSDate *)date block:(TMCacheBlock)block; +- (void)trimToDate:(NSDate *)date block:(nullable PINCacheBlock)block; /** Removes all objects from the cache.This method returns immediately and executes the passed block after the - cache has been cleared, potentially in parallel with other blocks on the . + cache has been cleared, potentially in parallel with other blocks on the . @param block A block to be executed concurrently after the cache has been cleared, or nil. */ -- (void)removeAllObjects:(TMCacheBlock)block; +- (void)removeAllObjects:(nullable PINCacheBlock)block; #pragma mark - /// @name Synchronous Methods +/** + This method determines whether an object is present for the given key in the cache. + + @see containsObjectForKey:block: + @param key The key associated with the object. + @result YES if an object is present for the given key in the cache, otherwise NO. + */ +- (BOOL)containsObjectForKey:(NSString *)key; + /** Retrieves the object for the specified key. This method blocks the calling thread until the object is available. + Uses a semaphore to achieve synchronicity on the disk cache. @see objectForKey:block: @param key The key associated with the object. @result The object for the specified key. */ -- (id)objectForKey:(NSString *)key; +- (__nullable id)objectForKey:(NSString *)key; /** Stores an object in the cache for the specified key. This method blocks the calling thread until the object has been set. + Uses a semaphore to achieve synchronicity on the disk cache. @see setObject:forKey:block: @param object An object to store in the cache. @@ -154,6 +201,7 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); /** Removes the object for the specified key. This method blocks the calling thread until the object has been removed. + Uses a semaphore to achieve synchronicity on the disk cache. @param key The key associated with the object to be removed. */ @@ -162,6 +210,7 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); /** Removes all objects from the cache that have not been used since the specified date. This method blocks the calling thread until the cache has been trimmed. + Uses a semaphore to achieve synchronicity on the disk cache. @param date Objects that haven't been accessed since this date are removed from the cache. */ @@ -169,7 +218,10 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object); /** Removes all objects from the cache. This method blocks the calling thread until the cache has been cleared. + Uses a semaphore to achieve synchronicity on the disk cache. */ - (void)removeAllObjects; @end + +NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/PINCache/PINCache.m b/clients/ios/Other Sources/PINCache/PINCache.m new file mode 100755 index 000000000..e8e3e2cb2 --- /dev/null +++ b/clients/ios/Other Sources/PINCache/PINCache.m @@ -0,0 +1,384 @@ +// PINCache is a modified version of PINCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. + +#import "PINCache.h" + +static NSString * const PINCachePrefix = @"com.pinterest.PINCache"; +static NSString * const PINCacheSharedName = @"PINCacheShared"; + +@interface PINCache () +#if OS_OBJECT_USE_OBJC +@property (strong, nonatomic) dispatch_queue_t concurrentQueue; +#else +@property (assign, nonatomic) dispatch_queue_t concurrentQueue; +#endif +@end + +@implementation PINCache + +#pragma mark - Initialization - + +#if !OS_OBJECT_USE_OBJC +- (void)dealloc +{ + dispatch_release(_concurrentQueue); + _concurrentQueue = nil; +} +#endif + +- (instancetype)init +{ + @throw [NSException exceptionWithName:@"Must initialize with a name" reason:@"PINCache must be initialized with a name. Call initWithName: instead." userInfo:nil]; + return [self initWithName:@""]; +} + +- (instancetype)initWithName:(NSString *)name +{ + return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]]; +} + +- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath +{ + if (!name) + return nil; + + if (self = [super init]) { + _name = [name copy]; + + NSString *queueName = [[NSString alloc] initWithFormat:@"%@.%p", PINCachePrefix, (void *)self]; + _concurrentQueue = dispatch_queue_create([[NSString stringWithFormat:@"%@ Asynchronous Queue", queueName] UTF8String], DISPATCH_QUEUE_CONCURRENT); + + _diskCache = [[PINDiskCache alloc] initWithName:_name rootPath:rootPath]; + _memoryCache = [[PINMemoryCache alloc] init]; + } + return self; +} + +- (NSString *)description +{ + return [[NSString alloc] initWithFormat:@"%@.%@.%p", PINCachePrefix, _name, (void *)self]; +} + ++ (instancetype)sharedCache +{ + static id cache; + static dispatch_once_t predicate; + + dispatch_once(&predicate, ^{ + cache = [[self alloc] initWithName:PINCacheSharedName]; + }); + + return cache; +} + +#pragma mark - Public Asynchronous Methods - + +- (void)containsObjectForKey:(NSString *)key block:(PINCacheObjectContainmentBlock)block +{ + if (!key || !block) { + return; + } + + __weak PINCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + + BOOL containsObject = [strongSelf containsObjectForKey:key]; + block(containsObject); + }); +} + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wshadow" + +- (void)objectForKey:(NSString *)key block:(PINCacheObjectBlock)block +{ + if (!key || !block) + return; + + __weak PINCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (!strongSelf) + return; + [strongSelf->_memoryCache objectForKey:key block:^(PINMemoryCache *memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + PINCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + if (memoryCacheObject) { + [strongSelf->_diskCache fileURLForKey:memoryCacheKey block:^(PINDiskCache *diskCache, NSString *diskCacheKey, id diskCacheObject, NSURL *fileURL) { + // update the access time on disk + }]; + dispatch_async(strongSelf->_concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (strongSelf) + block(strongSelf, memoryCacheKey, memoryCacheObject); + }); + } else { + [strongSelf->_diskCache objectForKey:memoryCacheKey block:^(PINDiskCache *diskCache, NSString *diskCacheKey, id diskCacheObject, NSURL *fileURL) { + PINCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf->_memoryCache setObject:diskCacheObject forKey:diskCacheKey block:nil]; + + + dispatch_async(strongSelf->_concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (strongSelf) + block(strongSelf, diskCacheKey, diskCacheObject); + }); + }]; + } + }]; + }); +} + +#pragma clang diagnostic pop + +- (void)setObject:(id )object forKey:(NSString *)key block:(PINCacheObjectBlock)block +{ + if (!key || !object) + return; + + dispatch_group_t group = nil; + PINMemoryCacheObjectBlock memBlock = nil; + PINDiskCacheObjectBlock diskBlock = nil; + + if (block) { + group = dispatch_group_create(); + dispatch_group_enter(group); + dispatch_group_enter(group); + + memBlock = ^(PINMemoryCache *memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + dispatch_group_leave(group); + }; + + diskBlock = ^(PINDiskCache *diskCache, NSString *diskCacheKey, id memoryCacheObject, NSURL *memoryCacheFileURL) { + dispatch_group_leave(group); + }; + } + + [_memoryCache setObject:object forKey:key block:memBlock]; + [_diskCache setObject:object forKey:key block:diskBlock]; + + if (group) { + __weak PINCache *weakSelf = self; + dispatch_group_notify(group, _concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (strongSelf) + block(strongSelf, key, object); + }); + +#if !OS_OBJECT_USE_OBJC + dispatch_release(group); +#endif + } +} + +- (void)removeObjectForKey:(NSString *)key block:(PINCacheObjectBlock)block +{ + if (!key) + return; + + dispatch_group_t group = nil; + PINMemoryCacheObjectBlock memBlock = nil; + PINDiskCacheObjectBlock diskBlock = nil; + + if (block) { + group = dispatch_group_create(); + dispatch_group_enter(group); + dispatch_group_enter(group); + + memBlock = ^(PINMemoryCache *memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + dispatch_group_leave(group); + }; + + diskBlock = ^(PINDiskCache *diskCache, NSString *diskCacheKey, id memoryCacheObject, NSURL *memoryCacheFileURL) { + dispatch_group_leave(group); + }; + } + + [_memoryCache removeObjectForKey:key block:memBlock]; + [_diskCache removeObjectForKey:key block:diskBlock]; + + if (group) { + __weak PINCache *weakSelf = self; + dispatch_group_notify(group, _concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (strongSelf) + block(strongSelf, key, nil); + }); + +#if !OS_OBJECT_USE_OBJC + dispatch_release(group); +#endif + } +} + +- (void)removeAllObjects:(PINCacheBlock)block +{ + dispatch_group_t group = nil; + PINMemoryCacheBlock memBlock = nil; + PINDiskCacheBlock diskBlock = nil; + + if (block) { + group = dispatch_group_create(); + dispatch_group_enter(group); + dispatch_group_enter(group); + + memBlock = ^(PINMemoryCache *cache) { + dispatch_group_leave(group); + }; + + diskBlock = ^(PINDiskCache *cache) { + dispatch_group_leave(group); + }; + } + + [_memoryCache removeAllObjects:memBlock]; + [_diskCache removeAllObjects:diskBlock]; + + if (group) { + __weak PINCache *weakSelf = self; + dispatch_group_notify(group, _concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (strongSelf) + block(strongSelf); + }); + +#if !OS_OBJECT_USE_OBJC + dispatch_release(group); +#endif + } +} + +- (void)trimToDate:(NSDate *)date block:(PINCacheBlock)block +{ + if (!date) + return; + + dispatch_group_t group = nil; + PINMemoryCacheBlock memBlock = nil; + PINDiskCacheBlock diskBlock = nil; + + if (block) { + group = dispatch_group_create(); + dispatch_group_enter(group); + dispatch_group_enter(group); + + memBlock = ^(PINMemoryCache *cache) { + dispatch_group_leave(group); + }; + + diskBlock = ^(PINDiskCache *cache) { + dispatch_group_leave(group); + }; + } + + [_memoryCache trimToDate:date block:memBlock]; + [_diskCache trimToDate:date block:diskBlock]; + + if (group) { + __weak PINCache *weakSelf = self; + dispatch_group_notify(group, _concurrentQueue, ^{ + PINCache *strongSelf = weakSelf; + if (strongSelf) + block(strongSelf); + }); + +#if !OS_OBJECT_USE_OBJC + dispatch_release(group); +#endif + } +} + +#pragma mark - Public Synchronous Accessors - + +- (NSUInteger)diskByteCount +{ + __block NSUInteger byteCount = 0; + + [_diskCache synchronouslyLockFileAccessWhileExecutingBlock:^(PINDiskCache *diskCache) { + byteCount = diskCache.byteCount; + }]; + + return byteCount; +} + +- (BOOL)containsObjectForKey:(NSString *)key +{ + if (!key) + return NO; + + return [_memoryCache containsObjectForKey:key] || [_diskCache containsObjectForKey:key]; +} + +- (__nullable id)objectForKey:(NSString *)key +{ + if (!key) + return nil; + + __block id object = nil; + + object = [_memoryCache objectForKey:key]; + + if (object) { + // update the access time on disk + [_diskCache fileURLForKey:key block:NULL]; + } else { + object = [_diskCache objectForKey:key]; + [_memoryCache setObject:object forKey:key]; + } + + return object; +} + +- (void)setObject:(id )object forKey:(NSString *)key +{ + if (!key || !object) + return; + + [_memoryCache setObject:object forKey:key]; + [_diskCache setObject:object forKey:key]; +} + +- (id)objectForKeyedSubscript:(NSString *)key +{ + return [self objectForKey:key]; +} + +- (void)setObject:(id)obj forKeyedSubscript:(NSString *)key +{ + [self setObject:obj forKey:key]; +} + +- (void)removeObjectForKey:(NSString *)key +{ + if (!key) + return; + + [_memoryCache removeObjectForKey:key]; + [_diskCache removeObjectForKey:key]; +} + +- (void)trimToDate:(NSDate *)date +{ + if (!date) + return; + + [_memoryCache trimToDate:date]; + [_diskCache trimToDate:date]; +} + +- (void)removeAllObjects +{ + [_memoryCache removeAllObjects]; + [_diskCache removeAllObjects]; +} + +@end diff --git a/clients/ios/Other Sources/PINCache/PINCacheObjectSubscripting.h b/clients/ios/Other Sources/PINCache/PINCacheObjectSubscripting.h new file mode 100755 index 000000000..353851bc4 --- /dev/null +++ b/clients/ios/Other Sources/PINCache/PINCacheObjectSubscripting.h @@ -0,0 +1,31 @@ +// +// PINCacheObjectSubscripting.h +// PINCache +// +// Created by Rocir Marcos Leite Santiago on 4/2/16. +// Copyright © 2016 Pinterest. All rights reserved. +// + +#import + +@protocol PINCacheObjectSubscripting + +@required + +/** + This method enables using literals on the receiving object, such as `id object = cache[@"key"];`. + + @param key The key associated with the object. + @result The object for the specified key. + */ +- (id)objectForKeyedSubscript:(NSString *)key; + +/** + This method enables using literals on the receiving object, such as `cache[@"key"] = object;`. + + @param object An object to be assigned for the key. + @param key A key to associate with the object. This string will be copied. + */ +- (void)setObject:(id)obj forKeyedSubscript:(NSString *)key; + +@end diff --git a/clients/ios/Other Sources/TMCache/TMDiskCache.h b/clients/ios/Other Sources/PINCache/PINDiskCache.h similarity index 61% rename from clients/ios/Other Sources/TMCache/TMDiskCache.h rename to clients/ios/Other Sources/PINCache/PINDiskCache.h index 10648f56c..1b9d8e882 100755 --- a/clients/ios/Other Sources/TMCache/TMDiskCache.h +++ b/clients/ios/Other Sources/PINCache/PINDiskCache.h @@ -1,34 +1,58 @@ +// PINCache is a modified version of TMCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. + +#import +#import "Nullability.h" + +#import "PINCacheObjectSubscripting.h" + +NS_ASSUME_NONNULL_BEGIN + +@class PINDiskCache; + /** - `TMDiskCache` is a thread safe key/value store backed by the file system. It accepts any object conforming + A callback block which provides only the cache as an argument + */ +typedef void (^PINDiskCacheBlock)(PINDiskCache *cache); + +/** + A callback block which provides the cache, key and object as arguments + */ +typedef void (^PINDiskCacheObjectBlock)(PINDiskCache *cache, NSString *key, id __nullable object, NSURL * __nullable fileURL); + +/** + A callback block which provides a BOOL value as argument + */ +typedef void (^PINDiskCacheContainsBlock)(BOOL containsObject); + + +/** + `PINDiskCache` is a thread safe key/value store backed by the file system. It accepts any object conforming to the `NSCoding` protocol, which includes the basic Foundation data types and collection classes and also many UIKit classes, notably `UIImage`. All work is performed on a serial queue shared by all instances in the app, and archiving is handled by `NSKeyedArchiver`. This is a particular advantage for `UIImage` because it skips `UIImagePNGRepresentation()` and retains information like scale and orientation. - The designated initializer for `TMDiskCache` is . The string is used to create a directory - under Library/Caches that scopes disk access for any instance sharing this name. Multiple instances with the - same name are allowed because all disk access is serialized on the . The also appears in - stack traces and return value for `description:`. + The designated initializer for `PINDiskCache` is . The string is used to create a directory + under Library/Caches that scopes disk access for this instance. Multiple instances with the same name are *not* + allowed as they would conflict with each other. Unless otherwise noted, all properties and methods are safe to access from any thread at any time. All blocks will cause the queue to wait, making it safe to access and manipulate the actual cache files on disk for the - duration of the block. In addition, the can be set to target an existing serial I/O queue, should - your app already have one. + duration of the block. - Because this cache is bound by disk I/O it can be much slower than , although values stored in - `TMDiskCache` persist after application relaunch. Using is recommended over using `TMDiskCache` + Because this cache is bound by disk I/O it can be much slower than , although values stored in + `PINDiskCache` persist after application relaunch. Using is recommended over using `PINDiskCache` by itself, as it adds a fast layer of additional memory caching while still writing to disk. All access to the cache is dated so the that the least-used objects can be trimmed first. Setting an optional will trigger a GCD timer to periodically to trim the cache with . */ -@class TMDiskCache; +@interface PINDiskCache : NSObject -typedef void (^TMDiskCacheBlock)(TMDiskCache *cache); -typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL); -@interface TMDiskCache : NSObject #pragma mark - /// @name Core @@ -39,26 +63,28 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id . + @warning Do not interact with files under this URL except in or + . */ @property (readonly) NSURL *cacheURL; /** The total number of bytes used on disk, as reported by `NSURLTotalFileAllocatedSizeKey`. - @warning This property is technically safe to access from any thread, but it reflects the value *right now*, - not taking into account any pending operations. In most cases this value should only be read from a block on the - , which will ensure its accuracy and prevent it from changing during the lifetime of the block. + @warning This property should only be read from a call to or + its asynchronous equivalent For example: - // some background thread, not a block already running on the shared queue + // some background thread - dispatch_sync([TMDiskCache sharedQueue], ^{ - NSLog(@"accurate, unchanging byte count: %d", [[TMDiskCache sharedCache] byteCount]); - }); + __block NSUInteger byteCount = 0; + + [_diskCache synchronouslyLockFileAccessWhileExecutingBlock:^(PINDiskCache *diskCache) { + byteCount = diskCache.byteCount; + }]; */ @property (readonly) NSUInteger byteCount; @@ -66,7 +92,6 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id (including asynchronous method blocks). */ @property (assign) NSUInteger byteLimit; @@ -75,44 +100,68 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id . Setting it back to `0.0` will stop the timer. Defaults to `0.0`, meaning no limit. - @warning Do not read this property on the (including asynchronous method blocks). */ @property (assign) NSTimeInterval ageLimit; + +/** + The writing protection option used when writing a file on disk. This value is used every time an object is set. + NSDataWritingAtomic and NSDataWritingWithoutOverwriting are ignored if set + Defaults to NSDataWritingFileProtectionNone. + + @warning Only new files are affected by the new writing protection. If you need all files to be affected, + you'll have to purge and set the objects back to the cache + + Only available on iOS + */ +#if TARGET_OS_IPHONE +@property (assign) NSDataWritingOptions writingProtectionOption; +#endif + +/** + If ttlCache is YES, the cache behaves like a ttlCache. This means that once an object enters the + cache, it only lives as long as self.ageLimit. This has the following implications: + - Accessing an object in the cache does not extend that object's lifetime in the cache + - When attempting to access an object in the cache that has lived longer than self.ageLimit, + the cache will behave as if the object does not exist + + */ +@property (nonatomic, assign, getter=isTTLCache) BOOL ttlCache; + #pragma mark - /// @name Event Blocks /** A block to be executed just before an object is added to the cache. The queue waits during execution. */ -@property (copy) TMDiskCacheObjectBlock willAddObjectBlock; +@property (copy) PINDiskCacheObjectBlock __nullable willAddObjectBlock; /** A block to be executed just before an object is removed from the cache. The queue waits during execution. */ -@property (copy) TMDiskCacheObjectBlock willRemoveObjectBlock; +@property (copy) PINDiskCacheObjectBlock __nullable willRemoveObjectBlock; /** A block to be executed just before all objects are removed from the cache as a result of . The queue waits during execution. */ -@property (copy) TMDiskCacheBlock willRemoveAllObjectsBlock; +@property (copy) PINDiskCacheBlock __nullable willRemoveAllObjectsBlock; /** A block to be executed just after an object is added to the cache. The queue waits during execution. */ -@property (copy) TMDiskCacheObjectBlock didAddObjectBlock; +@property (copy) PINDiskCacheObjectBlock __nullable didAddObjectBlock; /** A block to be executed just after an object is removed from the cache. The queue waits during execution. */ -@property (copy) TMDiskCacheObjectBlock didRemoveObjectBlock; +@property (copy) PINDiskCacheObjectBlock __nullable didRemoveObjectBlock; /** A block to be executed just after all objects are removed from the cache as a result of . The queue waits during execution. */ -@property (copy) TMDiskCacheBlock didRemoveAllObjectsBlock; +@property (copy) PINDiskCacheBlock __nullable didRemoveAllObjectsBlock; #pragma mark - /// @name Initialization @@ -125,18 +174,11 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id . + Empties the trash with `DISPATCH_QUEUE_PRIORITY_BACKGROUND`. Does not use lock. */ + (void)emptyTrash; +- (instancetype)init NS_UNAVAILABLE; /** Multiple instances with the same name are allowed and can safely access @@ -157,34 +199,52 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id . + + @see containsObjectForKey: + @param key The key associated with the object. + @param block A block to be executed concurrently after the containment check happened + */ +- (void)containsObjectForKey:(NSString *)key block:(PINDiskCacheContainsBlock)block; /** Retrieves the object for the specified key. This method returns immediately and executes the passed - block as soon as the object is available on the serial . + block as soon as the object is available. @warning The fileURL is only valid for the duration of this block, do not use it after the block ends. @param key The key associated with the requested object. @param block A block to be executed serially when the object is available. */ -- (void)objectForKey:(NSString *)key block:(TMDiskCacheObjectBlock)block; +- (void)objectForKey:(NSString *)key block:(nullable PINDiskCacheObjectBlock)block; /** Retrieves the fileURL for the specified key without actually reading the data from disk. This method - returns immediately and executes the passed block as soon as the object is available on the serial - . + returns immediately and executes the passed block as soon as the object is available. @warning Access is protected for the duration of the block, but to maintain safe disk access do not - access this fileURL after the block has ended. Do all work on the . + access this fileURL after the block has ended. @param key The key associated with the requested object. @param block A block to be executed serially when the file URL is available. */ -- (void)fileURLForKey:(NSString *)key block:(TMDiskCacheObjectBlock)block; +- (void)fileURLForKey:(nullable NSString *)key block:(nullable PINDiskCacheObjectBlock)block; /** Stores an object in the cache for the specified key. This method returns immediately and executes the @@ -194,7 +254,7 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id )object forKey:(NSString *)key block:(TMDiskCacheObjectBlock)block; +- (void)setObject:(id )object forKey:(NSString *)key block:(nullable PINDiskCacheObjectBlock)block; /** Removes the object for the specified key. This method returns immediately and executes the passed block @@ -203,7 +263,7 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id )objectForKey:(NSString *)key; +- (__nullable id )objectForKey:(NSString *)key; /** Retrieves the file URL for the specified key. This method blocks the calling thread until the - url is available. Do not use this URL anywhere but on the . This method probably + url is available. Do not use this URL anywhere except with . This method probably shouldn't even exist, just use the asynchronous one. @see fileURLForKey:block: @param key The key associated with the object. @result The file URL for the specified key. */ -- (NSURL *)fileURLForKey:(NSString *)key; +- (nullable NSURL *)fileURLForKey:(nullable NSString *)key; /** Stores an object in the cache for the specified key. This method blocks the calling thread until @@ -296,10 +375,9 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id , etc.) Instead use the asynchronous version, . */ -- (void)enumerateObjectsWithBlock:(TMDiskCacheObjectBlock)block; +- (void)enumerateObjectsWithBlock:(nullable PINDiskCacheObjectBlock)block; @end + +NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/PINCache/PINDiskCache.m b/clients/ios/Other Sources/PINCache/PINDiskCache.m new file mode 100755 index 000000000..f233445cd --- /dev/null +++ b/clients/ios/Other Sources/PINCache/PINDiskCache.m @@ -0,0 +1,1256 @@ +// PINCache is a modified version of TMCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. + +#import "PINDiskCache.h" + +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 +#import +#endif + +#import + +#define PINDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ +[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ +__LINE__, [error localizedDescription]); } + +static NSString * const PINDiskCachePrefix = @"com.pinterest.PINDiskCache"; +static NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared"; + +@interface PINBackgroundTask : NSObject +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH +@property (atomic, assign) UIBackgroundTaskIdentifier taskID; +#endif ++ (instancetype)start; +- (void)end; +@end + +typedef NS_ENUM(NSUInteger, PINDiskCacheCondition) { + PINDiskCacheConditionNotReady = 0, + PINDiskCacheConditionReady = 1, +}; + +@interface PINDiskCache () { + NSConditionLock *_instanceLock; +} + +@property (assign) NSUInteger byteCount; +@property (strong, nonatomic) NSURL *cacheURL; +#if OS_OBJECT_USE_OBJC +@property (strong, nonatomic) dispatch_queue_t asyncQueue; +#else +@property (assign, nonatomic) dispatch_queue_t asyncQueue; +#endif +@property (strong, nonatomic) NSMutableDictionary *dates; +@property (strong, nonatomic) NSMutableDictionary *sizes; +@end + +@implementation PINDiskCache + +@synthesize willAddObjectBlock = _willAddObjectBlock; +@synthesize willRemoveObjectBlock = _willRemoveObjectBlock; +@synthesize willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock; +@synthesize didAddObjectBlock = _didAddObjectBlock; +@synthesize didRemoveObjectBlock = _didRemoveObjectBlock; +@synthesize didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock; +@synthesize byteLimit = _byteLimit; +@synthesize ageLimit = _ageLimit; +@synthesize ttlCache = _ttlCache; + +#if TARGET_OS_IPHONE +@synthesize writingProtectionOption = _writingProtectionOption; +#endif + +#pragma mark - Initialization - + +- (void)dealloc +{ +#if !OS_OBJECT_USE_OBJC + dispatch_release(_asyncQueue); + _asyncQueue = nil; +#endif +} + +- (instancetype)init +{ + @throw [NSException exceptionWithName:@"Must initialize with a name" reason:@"PINDiskCache must be initialized with a name. Call initWithName: instead." userInfo:nil]; + return [self initWithName:@""]; +} + +- (instancetype)initWithName:(NSString *)name +{ + return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]]; +} + +- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath +{ + if (!name) + return nil; + + if (self = [super init]) { + _name = [name copy]; + _asyncQueue = dispatch_queue_create([[NSString stringWithFormat:@"%@ Asynchronous Queue", PINDiskCachePrefix] UTF8String], DISPATCH_QUEUE_CONCURRENT); + _instanceLock = [[NSConditionLock alloc] initWithCondition:PINDiskCacheConditionNotReady]; + _willAddObjectBlock = nil; + _willRemoveObjectBlock = nil; + _willRemoveAllObjectsBlock = nil; + _didAddObjectBlock = nil; + _didRemoveObjectBlock = nil; + _didRemoveAllObjectsBlock = nil; + + _byteCount = 0; + _byteLimit = 0; + _ageLimit = 0.0; + + #if TARGET_OS_IPHONE + _writingProtectionOption = NSDataWritingFileProtectionNone; + #endif + + _dates = [[NSMutableDictionary alloc] init]; + _sizes = [[NSMutableDictionary alloc] init]; + + NSString *pathComponent = [[NSString alloc] initWithFormat:@"%@.%@", PINDiskCachePrefix, _name]; + _cacheURL = [NSURL fileURLWithPathComponents:@[ rootPath, pathComponent ]]; + + //we don't want to do anything without setting up the disk cache, but we also don't want to block init, it can take a while to initialize + dispatch_async(_asyncQueue, ^{ + //should always be able to aquire the lock unless the below code is running. + [_instanceLock lockWhenCondition:PINDiskCacheConditionNotReady]; + [self createCacheDirectory]; + [self initializeDiskProperties]; + [_instanceLock unlockWithCondition:PINDiskCacheConditionReady]; + }); + } + return self; +} + +- (NSString *)description +{ + return [[NSString alloc] initWithFormat:@"%@.%@.%p", PINDiskCachePrefix, _name, (void *)self]; +} + ++ (instancetype)sharedCache +{ + static id cache; + static dispatch_once_t predicate; + + dispatch_once(&predicate, ^{ + cache = [[self alloc] initWithName:PINDiskCacheSharedName]; + }); + + return cache; +} + +#pragma mark - Private Methods - + +- (NSURL *)encodedFileURLForKey:(NSString *)key +{ + if (![key length]) + return nil; + + return [_cacheURL URLByAppendingPathComponent:[self encodedString:key]]; +} + +- (NSString *)keyForEncodedFileURL:(NSURL *)url +{ + NSString *fileName = [url lastPathComponent]; + if (!fileName) + return nil; + + return [self decodedString:fileName]; +} + +- (NSString *)encodedString:(NSString *)string +{ + if (![string length]) { + return @""; + } + + if ([string respondsToSelector:@selector(stringByAddingPercentEncodingWithAllowedCharacters:)]) { + return [string stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@".:/%"] invertedSet]]; + } + else { + CFStringRef static const charsToEscape = CFSTR(".:/%"); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + CFStringRef escapedString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, + (__bridge CFStringRef)string, + NULL, + charsToEscape, + kCFStringEncodingUTF8); +#pragma clang diagnostic pop + return (__bridge_transfer NSString *)escapedString; + } +} + +- (NSString *)decodedString:(NSString *)string +{ + if (![string length]) { + return @""; + } + + if ([string respondsToSelector:@selector(stringByRemovingPercentEncoding)]) { + return [string stringByRemovingPercentEncoding]; + } + else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + CFStringRef unescapedString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, + (__bridge CFStringRef)string, + CFSTR(""), + kCFStringEncodingUTF8); +#pragma clang diagnostic pop + return (__bridge_transfer NSString *)unescapedString; + } +} + +#pragma mark - Private Trash Methods - + ++ (dispatch_queue_t)sharedTrashQueue +{ + static dispatch_queue_t trashQueue; + static dispatch_once_t predicate; + + dispatch_once(&predicate, ^{ + NSString *queueName = [[NSString alloc] initWithFormat:@"%@.trash", PINDiskCachePrefix]; + trashQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_SERIAL); + dispatch_set_target_queue(trashQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); + }); + + return trashQueue; +} + ++ (NSURL *)sharedTrashURL +{ + static NSURL *sharedTrashURL; + static dispatch_once_t predicate; + + dispatch_once(&predicate, ^{ + sharedTrashURL = [[[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:PINDiskCachePrefix isDirectory:YES]; + + if (![[NSFileManager defaultManager] fileExistsAtPath:[sharedTrashURL path]]) { + NSError *error = nil; + [[NSFileManager defaultManager] createDirectoryAtURL:sharedTrashURL + withIntermediateDirectories:YES + attributes:nil + error:&error]; + PINDiskCacheError(error); + } + }); + + return sharedTrashURL; +} + ++(BOOL)moveItemAtURLToTrash:(NSURL *)itemURL +{ + if (![[NSFileManager defaultManager] fileExistsAtPath:[itemURL path]]) + return NO; + + NSError *error = nil; + NSString *uniqueString = [[NSProcessInfo processInfo] globallyUniqueString]; + NSURL *uniqueTrashURL = [[PINDiskCache sharedTrashURL] URLByAppendingPathComponent:uniqueString]; + BOOL moved = [[NSFileManager defaultManager] moveItemAtURL:itemURL toURL:uniqueTrashURL error:&error]; + PINDiskCacheError(error); + return moved; +} + ++ (void)emptyTrash +{ + dispatch_async([self sharedTrashQueue], ^{ + PINBackgroundTask *task = [PINBackgroundTask start]; + + NSError *searchTrashedItemsError = nil; + NSArray *trashedItems = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[self sharedTrashURL] + includingPropertiesForKeys:nil + options:0 + error:&searchTrashedItemsError]; + PINDiskCacheError(searchTrashedItemsError); + + for (NSURL *trashedItemURL in trashedItems) { + NSError *removeTrashedItemError = nil; + [[NSFileManager defaultManager] removeItemAtURL:trashedItemURL error:&removeTrashedItemError]; + PINDiskCacheError(removeTrashedItemError); + } + + [task end]; + }); +} + +#pragma mark - Private Queue Methods - + +- (BOOL)createCacheDirectory +{ + if ([[NSFileManager defaultManager] fileExistsAtPath:[_cacheURL path]]) + return NO; + + NSError *error = nil; + BOOL success = [[NSFileManager defaultManager] createDirectoryAtURL:_cacheURL + withIntermediateDirectories:YES + attributes:nil + error:&error]; + PINDiskCacheError(error); + + return success; +} + +- (void)initializeDiskProperties +{ + NSUInteger byteCount = 0; + NSArray *keys = @[ NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ]; + + NSError *error = nil; + NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:_cacheURL + includingPropertiesForKeys:keys + options:NSDirectoryEnumerationSkipsHiddenFiles + error:&error]; + PINDiskCacheError(error); + + for (NSURL *fileURL in files) { + NSString *key = [self keyForEncodedFileURL:fileURL]; + + error = nil; + NSDictionary *dictionary = [fileURL resourceValuesForKeys:keys error:&error]; + PINDiskCacheError(error); + + NSDate *date = [dictionary objectForKey:NSURLContentModificationDateKey]; + if (date && key) + [_dates setObject:date forKey:key]; + + NSNumber *fileSize = [dictionary objectForKey:NSURLTotalFileAllocatedSizeKey]; + if (fileSize) { + [_sizes setObject:fileSize forKey:key]; + byteCount += [fileSize unsignedIntegerValue]; + } + } + + if (byteCount > 0) + self.byteCount = byteCount; // atomic +} + +- (BOOL)setFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL +{ + if (!date || !fileURL) { + return NO; + } + + NSError *error = nil; + BOOL success = [[NSFileManager defaultManager] setAttributes:@{ NSFileModificationDate: date } + ofItemAtPath:[fileURL path] + error:&error]; + PINDiskCacheError(error); + + if (success) { + NSString *key = [self keyForEncodedFileURL:fileURL]; + if (key) { + [_dates setObject:date forKey:key]; + } + } + + return success; +} + +- (BOOL)removeFileAndExecuteBlocksForKey:(NSString *)key +{ + NSURL *fileURL = [self encodedFileURLForKey:key]; + if (!fileURL || ![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) + return NO; + + if (_willRemoveObjectBlock) + _willRemoveObjectBlock(self, key, nil, fileURL); + + BOOL trashed = [PINDiskCache moveItemAtURLToTrash:fileURL]; + if (!trashed) + return NO; + + [PINDiskCache emptyTrash]; + + NSNumber *byteSize = [_sizes objectForKey:key]; + if (byteSize) + self.byteCount = _byteCount - [byteSize unsignedIntegerValue]; // atomic + + [_sizes removeObjectForKey:key]; + [_dates removeObjectForKey:key]; + + if (_didRemoveObjectBlock) + _didRemoveObjectBlock(self, key, nil, fileURL); + + return YES; +} + +- (void)trimDiskToSize:(NSUInteger)trimByteCount +{ + if (_byteCount <= trimByteCount) + return; + + NSArray *keysSortedBySize = [_sizes keysSortedByValueUsingSelector:@selector(compare:)]; + + for (NSString *key in [keysSortedBySize reverseObjectEnumerator]) { // largest objects first + [self removeFileAndExecuteBlocksForKey:key]; + + if (_byteCount <= trimByteCount) + break; + } +} + +- (void)trimDiskToSizeByDate:(NSUInteger)trimByteCount +{ + if (_byteCount <= trimByteCount) + return; + + NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; + + for (NSString *key in keysSortedByDate) { // oldest objects first + [self removeFileAndExecuteBlocksForKey:key]; + + if (_byteCount <= trimByteCount) + break; + } +} + +- (void)trimDiskToDate:(NSDate *)trimDate +{ + NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; + + for (NSString *key in keysSortedByDate) { // oldest files first + NSDate *accessDate = [_dates objectForKey:key]; + if (!accessDate) + continue; + + if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date + [self removeFileAndExecuteBlocksForKey:key]; + } else { + break; + } + } +} + +- (void)trimToAgeLimitRecursively +{ + [self lock]; + NSTimeInterval ageLimit = _ageLimit; + [self unlock]; + if (ageLimit == 0.0) + return; + + [self lock]; + NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-ageLimit]; + [self trimDiskToDate:date]; + [self unlock]; + + __weak PINDiskCache *weakSelf = self; + + dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_ageLimit * NSEC_PER_SEC)); + dispatch_after(time, _asyncQueue, ^(void) { + PINDiskCache *strongSelf = weakSelf; + [strongSelf trimToAgeLimitRecursively]; + }); +} + +#pragma mark - Public Asynchronous Methods - + +- (void)lockFileAccessWhileExecutingBlock:(void(^)(PINDiskCache *diskCache))block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (block) { + [strongSelf lock]; + block(strongSelf); + [strongSelf unlock]; + } + }); +} + +- (void)containsObjectForKey:(NSString *)key block:(PINDiskCacheContainsBlock)block +{ + if (!key || !block) + return; + + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + block([strongSelf containsObjectForKey:key]); + }); +} + +- (void)objectForKey:(NSString *)key block:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + NSURL *fileURL = nil; + id object = [strongSelf objectForKey:key fileURL:&fileURL]; + + if (block) { + [strongSelf lock]; + block(strongSelf, key, object, fileURL); + [strongSelf unlock]; + } + }); +} + +- (void)fileURLForKey:(NSString *)key block:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + NSURL *fileURL = [strongSelf fileURLForKey:key]; + + if (block) { + [strongSelf lock]; + block(strongSelf, key, nil, fileURL); + [strongSelf unlock]; + } + }); +} + +- (void)setObject:(id )object forKey:(NSString *)key block:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + NSURL *fileURL = nil; + [strongSelf setObject:object forKey:key fileURL:&fileURL]; + + if (block) { + [strongSelf lock]; + block(strongSelf, key, object, fileURL); + [strongSelf unlock]; + } + }); +} + +- (void)removeObjectForKey:(NSString *)key block:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + NSURL *fileURL = nil; + [strongSelf removeObjectForKey:key fileURL:&fileURL]; + + if (block) { + [strongSelf lock]; + block(strongSelf, key, nil, fileURL); + [strongSelf unlock]; + } + }); +} + +- (void)trimToSize:(NSUInteger)trimByteCount block:(PINDiskCacheBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + [strongSelf trimToSize:trimByteCount]; + + if (block) { + [strongSelf lock]; + block(strongSelf); + [strongSelf unlock]; + } + }); +} + +- (void)trimToDate:(NSDate *)trimDate block:(PINDiskCacheBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + [strongSelf trimToDate:trimDate]; + + if (block) { + [strongSelf lock]; + block(strongSelf); + [strongSelf unlock]; + } + }); +} + +- (void)trimToSizeByDate:(NSUInteger)trimByteCount block:(PINDiskCacheBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + [strongSelf trimToSizeByDate:trimByteCount]; + + if (block) { + [strongSelf lock]; + block(strongSelf); + [strongSelf unlock]; + } + }); +} + +- (void)removeAllObjects:(PINDiskCacheBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + [strongSelf removeAllObjects]; + + if (block) { + [strongSelf lock]; + block(strongSelf); + [strongSelf unlock]; + } + }); +} + +- (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block completionBlock:(PINDiskCacheBlock)completionBlock +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + [strongSelf enumerateObjectsWithBlock:block]; + + if (completionBlock) { + [self lock]; + completionBlock(strongSelf); + [self unlock]; + } + }); +} + +#pragma mark - Public Synchronous Methods - + +- (void)synchronouslyLockFileAccessWhileExecutingBlock:(void(^)(PINDiskCache *diskCache))block +{ + if (block) { + [self lock]; + block(self); + [self unlock]; + } +} + +- (BOOL)containsObjectForKey:(NSString *)key +{ + return ([self fileURLForKey:key updateFileModificationDate:NO] != nil); +} + +- (__nullable id)objectForKey:(NSString *)key +{ + return [self objectForKey:key fileURL:nil]; +} + +- (id)objectForKeyedSubscript:(NSString *)key +{ + return [self objectForKey:key]; +} + +- (__nullable id )objectForKey:(NSString *)key fileURL:(NSURL **)outFileURL +{ + NSDate *now = [[NSDate alloc] init]; + + if (!key) + return nil; + + id object = nil; + NSURL *fileURL = nil; + + [self lock]; + fileURL = [self encodedFileURLForKey:key]; + object = nil; + + if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]] && + // If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive + (!self->_ttlCache || self->_ageLimit <= 0 || fabs([[_dates objectForKey:key] timeIntervalSinceDate:now]) < self->_ageLimit)) { + @try { + object = [NSKeyedUnarchiver unarchiveObjectWithFile:[fileURL path]]; + } + @catch (NSException *exception) { + NSError *error = nil; + [[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error]; + PINDiskCacheError(error); + } + if (!self->_ttlCache) { + [self setFileModificationDate:now forURL:fileURL]; + } + } + [self unlock]; + + if (outFileURL) { + *outFileURL = fileURL; + } + + return object; +} + +/// Helper function to call fileURLForKey:updateFileModificationDate: +- (NSURL *)fileURLForKey:(NSString *)key +{ + // Don't update the file modification time, if self is a ttlCache + return [self fileURLForKey:key updateFileModificationDate:!self->_ttlCache]; +} + +- (NSURL *)fileURLForKey:(NSString *)key updateFileModificationDate:(BOOL)updateFileModificationDate +{ + if (!key) { + return nil; + } + + NSDate *now = [[NSDate alloc] init]; + NSURL *fileURL = nil; + + [self lock]; + fileURL = [self encodedFileURLForKey:key]; + + if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) { + if (updateFileModificationDate) { + [self setFileModificationDate:now forURL:fileURL]; + } + } else { + fileURL = nil; + } + [self unlock]; + return fileURL; +} + +- (void)setObject:(id )object forKey:(NSString *)key +{ + [self setObject:object forKey:key fileURL:nil]; +} + +- (void)setObject:(id)object forKeyedSubscript:(NSString *)key +{ + [self setObject:object forKey:key]; +} + +- (void)setObject:(id )object forKey:(NSString *)key fileURL:(NSURL **)outFileURL +{ + NSDate *now = [[NSDate alloc] init]; + + if (!key || !object) + return; + + PINBackgroundTask *task = [PINBackgroundTask start]; + + #if TARGET_OS_IPHONE + NSDataWritingOptions writeOptions = NSDataWritingAtomic | self.writingProtectionOption; + #else + NSDataWritingOptions writeOptions = NSDataWritingAtomic; + #endif + + NSURL *fileURL = nil; + + [self lock]; + fileURL = [self encodedFileURLForKey:key]; + + if (self->_willAddObjectBlock) + self->_willAddObjectBlock(self, key, object, fileURL); + + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object]; + NSError *writeError = nil; + + BOOL written = [data writeToURL:fileURL options:writeOptions error:&writeError]; + PINDiskCacheError(writeError); + + if (written) { + [self setFileModificationDate:now forURL:fileURL]; + + NSError *error = nil; + NSDictionary *values = [fileURL resourceValuesForKeys:@[ NSURLTotalFileAllocatedSizeKey ] error:&error]; + PINDiskCacheError(error); + + NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey]; + if (diskFileSize) { + NSNumber *prevDiskFileSize = [self->_sizes objectForKey:key]; + if (prevDiskFileSize) { + self.byteCount = self->_byteCount - [prevDiskFileSize unsignedIntegerValue]; + } + [self->_sizes setObject:diskFileSize forKey:key]; + self.byteCount = self->_byteCount + [diskFileSize unsignedIntegerValue]; // atomic + } + + if (self->_byteLimit > 0 && self->_byteCount > self->_byteLimit) + [self trimToSizeByDate:self->_byteLimit block:nil]; + } else { + fileURL = nil; + } + + if (self->_didAddObjectBlock) + self->_didAddObjectBlock(self, key, object, written ? fileURL : nil); + [self unlock]; + + if (outFileURL) { + *outFileURL = fileURL; + } + + [task end]; +} + +- (void)removeObjectForKey:(NSString *)key +{ + [self removeObjectForKey:key fileURL:nil]; +} + +- (void)removeObjectForKey:(NSString *)key fileURL:(NSURL **)outFileURL +{ + if (!key) + return; + + PINBackgroundTask *task = [PINBackgroundTask start]; + + NSURL *fileURL = nil; + + [self lock]; + fileURL = [self encodedFileURLForKey:key]; + [self removeFileAndExecuteBlocksForKey:key]; + [self unlock]; + + [task end]; + + if (outFileURL) { + *outFileURL = fileURL; + } +} + +- (void)trimToSize:(NSUInteger)trimByteCount +{ + if (trimByteCount == 0) { + [self removeAllObjects]; + return; + } + + PINBackgroundTask *task = [PINBackgroundTask start]; + + [self lock]; + [self trimDiskToSize:trimByteCount]; + [self unlock]; + + [task end]; +} + +- (void)trimToDate:(NSDate *)trimDate +{ + if (!trimDate) + return; + + if ([trimDate isEqualToDate:[NSDate distantPast]]) { + [self removeAllObjects]; + return; + } + + PINBackgroundTask *task = [PINBackgroundTask start]; + + [self lock]; + [self trimDiskToDate:trimDate]; + [self unlock]; + + [task end]; +} + +- (void)trimToSizeByDate:(NSUInteger)trimByteCount +{ + if (trimByteCount == 0) { + [self removeAllObjects]; + return; + } + + PINBackgroundTask *task = [PINBackgroundTask start]; + + [self lock]; + [self trimDiskToSizeByDate:trimByteCount]; + [self unlock]; + + [task end]; +} + +- (void)removeAllObjects +{ + PINBackgroundTask *task = [PINBackgroundTask start]; + + [self lock]; + if (self->_willRemoveAllObjectsBlock) + self->_willRemoveAllObjectsBlock(self); + + [PINDiskCache moveItemAtURLToTrash:self->_cacheURL]; + [PINDiskCache emptyTrash]; + + [self createCacheDirectory]; + + [self->_dates removeAllObjects]; + [self->_sizes removeAllObjects]; + self.byteCount = 0; // atomic + + if (self->_didRemoveAllObjectsBlock) + self->_didRemoveAllObjectsBlock(self); + [self unlock]; + + [task end]; +} + +- (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block +{ + if (!block) + return; + + PINBackgroundTask *task = [PINBackgroundTask start]; + + [self lock]; + NSDate *now = [NSDate date]; + NSArray *keysSortedByDate = [self->_dates keysSortedByValueUsingSelector:@selector(compare:)]; + + for (NSString *key in keysSortedByDate) { + NSURL *fileURL = [self encodedFileURLForKey:key]; + // If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive + if (!self->_ttlCache || self->_ageLimit <= 0 || fabs([[_dates objectForKey:key] timeIntervalSinceDate:now]) < self->_ageLimit) { + block(self, key, nil, fileURL); + } + } + [self unlock]; + + [task end]; +} + +#pragma mark - Public Thread Safe Accessors - + +- (PINDiskCacheObjectBlock)willAddObjectBlock +{ + PINDiskCacheObjectBlock block = nil; + + [self lock]; + block = _willAddObjectBlock; + [self unlock]; + + return block; +} + +- (void)setWillAddObjectBlock:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + [strongSelf lock]; + strongSelf->_willAddObjectBlock = [block copy]; + [strongSelf unlock]; + }); +} + +- (PINDiskCacheObjectBlock)willRemoveObjectBlock +{ + PINDiskCacheObjectBlock block = nil; + + [self lock]; + block = _willRemoveObjectBlock; + [self unlock]; + + return block; +} + +- (void)setWillRemoveObjectBlock:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_willRemoveObjectBlock = [block copy]; + [strongSelf unlock]; + }); +} + +- (PINDiskCacheBlock)willRemoveAllObjectsBlock +{ + PINDiskCacheBlock block = nil; + + [self lock]; + block = _willRemoveAllObjectsBlock; + [self unlock]; + + return block; +} + +- (void)setWillRemoveAllObjectsBlock:(PINDiskCacheBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_willRemoveAllObjectsBlock = [block copy]; + [strongSelf unlock]; + }); +} + +- (PINDiskCacheObjectBlock)didAddObjectBlock +{ + PINDiskCacheObjectBlock block = nil; + + [self lock]; + block = _didAddObjectBlock; + [self unlock]; + + return block; +} + +- (void)setDidAddObjectBlock:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_didAddObjectBlock = [block copy]; + [strongSelf unlock]; + }); +} + +- (PINDiskCacheObjectBlock)didRemoveObjectBlock +{ + PINDiskCacheObjectBlock block = nil; + + [self lock]; + block = _didRemoveObjectBlock; + [self unlock]; + + return block; +} + +- (void)setDidRemoveObjectBlock:(PINDiskCacheObjectBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_didRemoveObjectBlock = [block copy]; + [strongSelf unlock]; + }); +} + +- (PINDiskCacheBlock)didRemoveAllObjectsBlock +{ + PINDiskCacheBlock block = nil; + + [self lock]; + block = _didRemoveAllObjectsBlock; + [self unlock]; + + return block; +} + +- (void)setDidRemoveAllObjectsBlock:(PINDiskCacheBlock)block +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_didRemoveAllObjectsBlock = [block copy]; + [strongSelf unlock]; + }); +} + +- (NSUInteger)byteLimit +{ + NSUInteger byteLimit; + + [self lock]; + byteLimit = _byteLimit; + [self unlock]; + + return byteLimit; +} + +- (void)setByteLimit:(NSUInteger)byteLimit +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_byteLimit = byteLimit; + + if (byteLimit > 0) + [strongSelf trimDiskToSizeByDate:byteLimit]; + [strongSelf unlock]; + }); +} + +- (NSTimeInterval)ageLimit +{ + NSTimeInterval ageLimit; + + [self lock]; + ageLimit = _ageLimit; + [self unlock]; + + return ageLimit; +} + +- (void)setAgeLimit:(NSTimeInterval)ageLimit +{ + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_ageLimit = ageLimit; + [strongSelf unlock]; + + [strongSelf trimToAgeLimitRecursively]; + }); +} + +- (BOOL)isTTLCache { + BOOL isTTLCache; + + [self lock]; + isTTLCache = _ttlCache; + [self unlock]; + + return isTTLCache; +} + +- (void)setTtlCache:(BOOL)ttlCache { + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + [strongSelf lock]; + strongSelf->_ttlCache = ttlCache; + [strongSelf unlock]; + }); +} + +#if TARGET_OS_IPHONE +- (NSDataWritingOptions)writingProtectionOption { + NSDataWritingOptions option; + + [self lock]; + option = _writingProtectionOption; + [self unlock]; + + return option; +} + +- (void)setWritingProtectionOption:(NSDataWritingOptions)writingProtectionOption { + __weak PINDiskCache *weakSelf = self; + + dispatch_async(_asyncQueue, ^{ + PINDiskCache *strongSelf = weakSelf; + if (!strongSelf) + return; + + NSDataWritingOptions option = NSDataWritingFileProtectionMask & writingProtectionOption; + + [strongSelf lock]; + strongSelf->_writingProtectionOption = option; + [strongSelf unlock]; + }); +} +#endif + +- (void)lock +{ + [_instanceLock lockWhenCondition:PINDiskCacheConditionReady]; +} + +- (void)unlock +{ + [_instanceLock unlockWithCondition:PINDiskCacheConditionReady]; +} + +@end + +@implementation PINBackgroundTask + ++ (BOOL)isAppExtension { + + static BOOL isExtension; + static dispatch_once_t onceToken; + + dispatch_once(&onceToken, ^{ + NSDictionary *extensionDictionary = [[NSBundle mainBundle] infoDictionary][@"NSExtension"]; + isExtension = [extensionDictionary isKindOfClass:[NSDictionary class]]; + }); + + return isExtension; +} + +- (instancetype)init +{ + if (self = [super init]) { +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH + _taskID = UIBackgroundTaskInvalid; +#endif + } + return self; +} + ++ (instancetype)start +{ + PINBackgroundTask *task = nil; + +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH + if ([self.class isAppExtension]) { + return task; + } + + task = [[self alloc] init]; + + UIApplication *sharedApplication = [UIApplication performSelector:@selector(sharedApplication)]; + task.taskID = [sharedApplication beginBackgroundTaskWithExpirationHandler:^{ + UIBackgroundTaskIdentifier taskID = task.taskID; + task.taskID = UIBackgroundTaskInvalid; + [sharedApplication endBackgroundTask:taskID]; + }]; +#endif + + return task; +} + +- (void)end +{ +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH + if ([self.class isAppExtension]) { + return; + } + + UIBackgroundTaskIdentifier taskID = self.taskID; + self.taskID = UIBackgroundTaskInvalid; + + UIApplication *sharedApplication = [UIApplication performSelector:@selector(sharedApplication)]; + [sharedApplication endBackgroundTask:taskID]; +#endif +} + +@end diff --git a/clients/ios/Other Sources/TMCache/TMMemoryCache.h b/clients/ios/Other Sources/PINCache/PINMemoryCache.h similarity index 57% rename from clients/ios/Other Sources/TMCache/TMMemoryCache.h rename to clients/ios/Other Sources/PINCache/PINMemoryCache.h index 1e6a291fd..507122bb3 100755 --- a/clients/ios/Other Sources/TMCache/TMMemoryCache.h +++ b/clients/ios/Other Sources/PINCache/PINMemoryCache.h @@ -1,35 +1,59 @@ -/** - `TMMemoryCache` is a fast, thread safe key/value store similar to `NSCache`. On iOS it will clear itself - automatically to reduce memory usage when the app receives a memory warning or goes into the background. +// PINCache is a modified version of TMCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. - Access is natively asynchronous. Every method accepts a callback block that runs on a concurrent - , with cache writes protected by GCD barriers. Synchronous variations are provided. +#import +#import "Nullability.h" + +#import "PINCacheObjectSubscripting.h" + +NS_ASSUME_NONNULL_BEGIN + +@class PINMemoryCache; + +/** + A callback block which provides only the cache as an argument + */ +typedef void (^PINMemoryCacheBlock)(PINMemoryCache *cache); + +/** + A callback block which provides the cache, key and object as arguments + */ +typedef void (^PINMemoryCacheObjectBlock)(PINMemoryCache *cache, NSString *key, id __nullable object); + +/** + A callback block which provides a BOOL value as argument + */ +typedef void (^PINMemoryCacheContainmentBlock)(BOOL containsObject); + + +/** + `PINMemoryCache` is a fast, thread safe key/value store similar to `NSCache`. On iOS it will clear itself + automatically to reduce memory usage when the app receives a memory warning or goes into the background. + + Access is natively synchronous. Asynchronous variations are provided. Every asynchronous method accepts a + callback block that runs on a concurrent , with cache reads and writes protected by an semaphore. All access to the cache is dated so the that the least-used objects can be trimmed first. Setting an optional will trigger a GCD timer to periodically to trim the cache to that age. Objects can optionally be set with a "cost", which could be a byte count or any other meaningful integer. Setting a will automatically keep the cache below that value with . - - Values will not persist after application relaunch or returning from the background. See for + + Values will not persist after application relaunch or returning from the background. See for a memory cache backed by a disk cache. */ -@class TMMemoryCache; - -typedef void (^TMMemoryCacheBlock)(TMMemoryCache *cache); -typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id object); - -@interface TMMemoryCache : NSObject +@interface PINMemoryCache : NSObject #pragma mark - /// @name Core /** - A concurrent queue on which all work is done. It is exposed here so that it can be set to target some - other queue, such as a global concurrent queue with a priority other than the default. + A concurrent queue on which all callbacks are called. It is exposed here so that it can be set to + target some other queue, such as a global concurrent queue with a priority other than the default. */ -@property (readonly) dispatch_queue_t queue; +@property (readonly) dispatch_queue_t concurrentQueue; /** The total accumulated cost. @@ -48,6 +72,16 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id */ @property (assign) NSTimeInterval ageLimit; +/** + If ttlCache is YES, the cache behaves like a ttlCache. This means that once an object enters the + cache, it only lives as long as self.ageLimit. This has the following implications: + - Accessing an object in the cache does not extend that object's lifetime in the cache + - When attempting to access an object in the cache that has lived longer than self.ageLimit, + the cache will behave as if the object does not exist + + */ +@property (nonatomic, assign, getter=isTTLCache) BOOL ttlCache; + /** When `YES` on iOS the cache will remove all objects when the app receives a memory warning. Defaults to `YES`. @@ -65,51 +99,57 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id /** A block to be executed just before an object is added to the cache. This block will be excuted within - a barrier, i.e. all reads and writes are suspended for the duration of the block. + a lock, i.e. all reads and writes are suspended for the duration of the block. + Calling synchronous methods on the cache within this callback will likely cause a deadlock. */ -@property (copy) TMMemoryCacheObjectBlock willAddObjectBlock; +@property (copy) PINMemoryCacheObjectBlock __nullable willAddObjectBlock; /** A block to be executed just before an object is removed from the cache. This block will be excuted - within a barrier, i.e. all reads and writes are suspended for the duration of the block. + within a lock, i.e. all reads and writes are suspended for the duration of the block. + Calling synchronous methods on the cache within this callback will likely cause a deadlock. */ -@property (copy) TMMemoryCacheObjectBlock willRemoveObjectBlock; +@property (copy) PINMemoryCacheObjectBlock __nullable willRemoveObjectBlock; /** A block to be executed just before all objects are removed from the cache as a result of . - This block will be excuted within a barrier, i.e. all reads and writes are suspended for the duration of the block. + This block will be excuted within a lock, i.e. all reads and writes are suspended for the duration of the block. + Calling synchronous methods on the cache within this callback will likely cause a deadlock. */ -@property (copy) TMMemoryCacheBlock willRemoveAllObjectsBlock; +@property (copy) PINMemoryCacheBlock __nullable willRemoveAllObjectsBlock; /** A block to be executed just after an object is added to the cache. This block will be excuted within - a barrier, i.e. all reads and writes are suspended for the duration of the block. + a lock, i.e. all reads and writes are suspended for the duration of the block. + Calling synchronous methods on the cache within this callback will likely cause a deadlock. */ -@property (copy) TMMemoryCacheObjectBlock didAddObjectBlock; +@property (copy) PINMemoryCacheObjectBlock __nullable didAddObjectBlock; /** A block to be executed just after an object is removed from the cache. This block will be excuted - within a barrier, i.e. all reads and writes are suspended for the duration of the block. + within a lock, i.e. all reads and writes are suspended for the duration of the block. + Calling synchronous methods on the cache within this callback will likely cause a deadlock. */ -@property (copy) TMMemoryCacheObjectBlock didRemoveObjectBlock; +@property (copy) PINMemoryCacheObjectBlock __nullable didRemoveObjectBlock; /** A block to be executed just after all objects are removed from the cache as a result of . - This block will be excuted within a barrier, i.e. all reads and writes are suspended for the duration of the block. + This block will be excuted within a lock, i.e. all reads and writes are suspended for the duration of the block. + Calling synchronous methods on the cache within this callback will likely cause a deadlock. */ -@property (copy) TMMemoryCacheBlock didRemoveAllObjectsBlock; +@property (copy) PINMemoryCacheBlock __nullable didRemoveAllObjectsBlock; /** A block to be executed upon receiving a memory warning (iOS only) potentially in parallel with other blocks on the . This block will be executed regardless of the value of . Defaults to `nil`. */ -@property (copy) TMMemoryCacheBlock didReceiveMemoryWarningBlock; +@property (copy) PINMemoryCacheBlock __nullable didReceiveMemoryWarningBlock; /** - A block to be executed when the app enters the background (iOS only) potentially in parallel with other blocks on the . + A block to be executed when the app enters the background (iOS only) potentially in parallel with other blocks on the . This block will be executed regardless of the value of . Defaults to `nil`. */ -@property (copy) TMMemoryCacheBlock didEnterBackgroundBlock; +@property (copy) PINMemoryCacheBlock __nullable didEnterBackgroundBlock; #pragma mark - /// @name Shared Cache @@ -124,97 +164,117 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id #pragma mark - /// @name Asynchronous Methods +/** + This method determines whether an object is present for the given key in the cache. This method returns immediately + and executes the passed block after the object is available, potentially in parallel with other blocks on the + . + + @see containsObjectForKey: + @param key The key associated with the object. + @param block A block to be executed concurrently after the containment check happened + */ +- (void)containsObjectForKey:(NSString *)key block:(PINMemoryCacheContainmentBlock)block; + /** Retrieves the object for the specified key. This method returns immediately and executes the passed - block after the object is available, potentially in parallel with other blocks on the . + block after the object is available, potentially in parallel with other blocks on the . @param key The key associated with the requested object. @param block A block to be executed concurrently when the object is available. */ -- (void)objectForKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block; +- (void)objectForKey:(NSString *)key block:(nullable PINMemoryCacheObjectBlock)block; /** Stores an object in the cache for the specified key. This method returns immediately and executes the - passed block after the object has been stored, potentially in parallel with other blocks on the . + passed block after the object has been stored, potentially in parallel with other blocks on the . @param object An object to store in the cache. @param key A key to associate with the object. This string will be copied. @param block A block to be executed concurrently after the object has been stored, or nil. */ -- (void)setObject:(id)object forKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block; +- (void)setObject:(id)object forKey:(NSString *)key block:(nullable PINMemoryCacheObjectBlock)block; /** Stores an object in the cache for the specified key and the specified cost. If the cost causes the total to go over the the cache is trimmed (oldest objects first). This method returns immediately and executes the passed block after the object has been stored, potentially in parallel with other blocks - on the . + on the . @param object An object to store in the cache. @param key A key to associate with the object. This string will be copied. @param cost An amount to add to the . @param block A block to be executed concurrently after the object has been stored, or nil. */ -- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(TMMemoryCacheObjectBlock)block; +- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(nullable PINMemoryCacheObjectBlock)block; /** Removes the object for the specified key. This method returns immediately and executes the passed - block after the object has been removed, potentially in parallel with other blocks on the . + block after the object has been removed, potentially in parallel with other blocks on the . @param key The key associated with the object to be removed. @param block A block to be executed concurrently after the object has been removed, or nil. */ -- (void)removeObjectForKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block; +- (void)removeObjectForKey:(NSString *)key block:(nullable PINMemoryCacheObjectBlock)block; /** Removes all objects from the cache that have not been used since the specified date. This method returns immediately and executes the passed block after the cache has been trimmed, - potentially in parallel with other blocks on the . + potentially in parallel with other blocks on the . @param date Objects that haven't been accessed since this date are removed from the cache. @param block A block to be executed concurrently after the cache has been trimmed, or nil. */ -- (void)trimToDate:(NSDate *)date block:(TMMemoryCacheBlock)block; +- (void)trimToDate:(NSDate *)date block:(nullable PINMemoryCacheBlock)block; /** Removes objects from the cache, costliest objects first, until the is below the specified value. This method returns immediately and executes the passed block after the cache has been trimmed, - potentially in parallel with other blocks on the . + potentially in parallel with other blocks on the . @param cost The total accumulation allowed to remain after the cache has been trimmed. @param block A block to be executed concurrently after the cache has been trimmed, or nil. */ -- (void)trimToCost:(NSUInteger)cost block:(TMMemoryCacheBlock)block; +- (void)trimToCost:(NSUInteger)cost block:(nullable PINMemoryCacheBlock)block; /** Removes objects from the cache, ordered by date (least recently used first), until the is below the specified value. This method returns immediately and executes the passed block after the cache has been - trimmed, potentially in parallel with other blocks on the . - + trimmed, potentially in parallel with other blocks on the . + @param cost The total accumulation allowed to remain after the cache has been trimmed. @param block A block to be executed concurrently after the cache has been trimmed, or nil. */ -- (void)trimToCostByDate:(NSUInteger)cost block:(TMMemoryCacheBlock)block; +- (void)trimToCostByDate:(NSUInteger)cost block:(nullable PINMemoryCacheBlock)block; /** Removes all objects from the cache. This method returns immediately and executes the passed block after - the cache has been cleared, potentially in parallel with other blocks on the . + the cache has been cleared, potentially in parallel with other blocks on the . @param block A block to be executed concurrently after the cache has been cleared, or nil. */ -- (void)removeAllObjects:(TMMemoryCacheBlock)block; +- (void)removeAllObjects:(nullable PINMemoryCacheBlock)block; /** - Loops through all objects in the cache within a memory barrier (reads and writes are suspended during the enumeration). - This method returns immediately. - + Loops through all objects in the cache with reads and writes suspended. Calling serial methods which + write to the cache inside block may be unsafe and may result in a deadlock. This method returns immediately. + @param block A block to be executed for every object in the cache. @param completionBlock An optional block to be executed concurrently when the enumeration is complete. */ -- (void)enumerateObjectsWithBlock:(TMMemoryCacheObjectBlock)block completionBlock:(TMMemoryCacheBlock)completionBlock; +- (void)enumerateObjectsWithBlock:(PINMemoryCacheObjectBlock)block completionBlock:(nullable PINMemoryCacheBlock)completionBlock; #pragma mark - /// @name Synchronous Methods +/** + This method determines whether an object is present for the given key in the cache. + + @see containsObjectForKey:block: + @param key The key associated with the object. + @result YES if an object is present for the given key in the cache, otherwise NO. + */ +- (BOOL)containsObjectForKey:(NSString *)key; + /** Retrieves the object for the specified key. This method blocks the calling thread until the object is available. @@ -223,12 +283,12 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id @param key The key associated with the object. @result The object for the specified key. */ -- (id)objectForKey:(NSString *)key; +- (__nullable id)objectForKey:(nullable NSString *)key; /** Stores an object in the cache for the specified key. This method blocks the calling thread until the object has been set. - + @see setObject:forKey:block: @param object An object to store in the cache. @param key A key to associate with the object. This string will be copied. @@ -239,12 +299,12 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id Stores an object in the cache for the specified key and the specified cost. If the cost causes the total to go over the the cache is trimmed (oldest objects first). This method blocks the calling thread until the object has been stored. - + @param object An object to store in the cache. @param key A key to associate with the object. This string will be copied. @param cost An amount to add to the . */ -- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost; +- (void)setObject:(nullable id)object forKey:(nullable NSString *)key withCost:(NSUInteger)cost; /** Removes the object for the specified key. This method blocks the calling thread until the object @@ -252,7 +312,7 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id @param key The key associated with the object to be removed. */ -- (void)removeObjectForKey:(NSString *)key; +- (void)removeObjectForKey:(nullable NSString *)key; /** Removes all objects from the cache that have not been used since the specified date. @@ -260,7 +320,7 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id @param date Objects that haven't been accessed since this date are removed from the cache. */ -- (void)trimToDate:(NSDate *)date; +- (void)trimToDate:(nullable NSDate *)date; /** Removes objects from the cache, costliest objects first, until the is below the specified @@ -273,7 +333,7 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id /** Removes objects from the cache, ordered by date (least recently used first), until the is below the specified value. This method blocks the calling thread until the cache has been trimmed. - + @param cost The total accumulation allowed to remain after the cache has been trimmed. */ - (void)trimToCostByDate:(NSUInteger)cost; @@ -284,15 +344,18 @@ typedef void (^TMMemoryCacheObjectBlock)(TMMemoryCache *cache, NSString *key, id - (void)removeAllObjects; /** - Loops through all objects in the cache within a memory barrier (reads and writes are suspended during the enumeration). + Loops through all objects in the cache within a memory lock (reads and writes are suspended during the enumeration). This method blocks the calling thread until all objects have been enumerated. - + Calling synchronous methods on the cache within this callback will likely cause a deadlock. + @param block A block to be executed for every object in the cache. @warning Do not call this method within the event blocks (, etc.) Instead use the asynchronous version, . */ -- (void)enumerateObjectsWithBlock:(TMMemoryCacheObjectBlock)block; +- (void)enumerateObjectsWithBlock:(nullable PINMemoryCacheObjectBlock)block; @end + +NS_ASSUME_NONNULL_END diff --git a/clients/ios/Other Sources/PINCache/PINMemoryCache.m b/clients/ios/Other Sources/PINCache/PINMemoryCache.m new file mode 100755 index 000000000..a040c41f9 --- /dev/null +++ b/clients/ios/Other Sources/PINCache/PINMemoryCache.m @@ -0,0 +1,756 @@ +// PINCache is a modified version of TMCache +// Modifications by Garrett Moon +// Copyright (c) 2015 Pinterest. All rights reserved. + +#import "PINMemoryCache.h" + +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 +#import +#endif + +static NSString * const PINMemoryCachePrefix = @"com.pinterest.PINMemoryCache"; + +@interface PINMemoryCache () +#if OS_OBJECT_USE_OBJC +@property (strong, nonatomic) dispatch_queue_t concurrentQueue; +@property (strong, nonatomic) dispatch_semaphore_t lockSemaphore; +#else +@property (assign, nonatomic) dispatch_queue_t concurrentQueue; +@property (assign, nonatomic) dispatch_semaphore_t lockSemaphore; +#endif +@property (strong, nonatomic) NSMutableDictionary *dictionary; +@property (strong, nonatomic) NSMutableDictionary *dates; +@property (strong, nonatomic) NSMutableDictionary *costs; +@end + +@implementation PINMemoryCache + +@synthesize ageLimit = _ageLimit; +@synthesize costLimit = _costLimit; +@synthesize totalCost = _totalCost; +@synthesize ttlCache = _ttlCache; +@synthesize willAddObjectBlock = _willAddObjectBlock; +@synthesize willRemoveObjectBlock = _willRemoveObjectBlock; +@synthesize willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock; +@synthesize didAddObjectBlock = _didAddObjectBlock; +@synthesize didRemoveObjectBlock = _didRemoveObjectBlock; +@synthesize didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock; +@synthesize didReceiveMemoryWarningBlock = _didReceiveMemoryWarningBlock; +@synthesize didEnterBackgroundBlock = _didEnterBackgroundBlock; + +#pragma mark - Initialization - + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + #if !OS_OBJECT_USE_OBJC + dispatch_release(_concurrentQueue); + dispatch_release(_lockSemaphore); + _concurrentQueue = nil; + #endif +} + +- (instancetype)init +{ + if (self = [super init]) { + _lockSemaphore = dispatch_semaphore_create(1); + NSString *queueName = [[NSString alloc] initWithFormat:@"%@.%p", PINMemoryCachePrefix, (void *)self]; + _concurrentQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_CONCURRENT); + + _dictionary = [[NSMutableDictionary alloc] init]; + _dates = [[NSMutableDictionary alloc] init]; + _costs = [[NSMutableDictionary alloc] init]; + + _willAddObjectBlock = nil; + _willRemoveObjectBlock = nil; + _willRemoveAllObjectsBlock = nil; + + _didAddObjectBlock = nil; + _didRemoveObjectBlock = nil; + _didRemoveAllObjectsBlock = nil; + + _didReceiveMemoryWarningBlock = nil; + _didEnterBackgroundBlock = nil; + + _ageLimit = 0.0; + _costLimit = 0; + _totalCost = 0; + + _removeAllObjectsOnMemoryWarning = YES; + _removeAllObjectsOnEnteringBackground = YES; + +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(didReceiveEnterBackgroundNotification:) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(didReceiveMemoryWarningNotification:) + name:UIApplicationDidReceiveMemoryWarningNotification + object:nil]; + +#endif + } + return self; +} + ++ (instancetype)sharedCache +{ + static id cache; + static dispatch_once_t predicate; + + dispatch_once(&predicate, ^{ + cache = [[self alloc] init]; + }); + + return cache; +} + +#pragma mark - Private Methods - + +- (void)didReceiveMemoryWarningNotification:(NSNotification *)notification { + if (self.removeAllObjectsOnMemoryWarning) + [self removeAllObjects:nil]; + + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + if (!strongSelf) { + return; + } + + [strongSelf lock]; + PINMemoryCacheBlock didReceiveMemoryWarningBlock = strongSelf->_didReceiveMemoryWarningBlock; + [strongSelf unlock]; + + if (didReceiveMemoryWarningBlock) + didReceiveMemoryWarningBlock(strongSelf); + }); +} + +- (void)didReceiveEnterBackgroundNotification:(NSNotification *)notification +{ + if (self.removeAllObjectsOnEnteringBackground) + [self removeAllObjects:nil]; + + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + if (!strongSelf) { + return; + } + + [strongSelf lock]; + PINMemoryCacheBlock didEnterBackgroundBlock = strongSelf->_didEnterBackgroundBlock; + [strongSelf unlock]; + + if (didEnterBackgroundBlock) + didEnterBackgroundBlock(strongSelf); + }); +} + +- (void)removeObjectAndExecuteBlocksForKey:(NSString *)key +{ + [self lock]; + id object = _dictionary[key]; + NSNumber *cost = _costs[key]; + PINMemoryCacheObjectBlock willRemoveObjectBlock = _willRemoveObjectBlock; + PINMemoryCacheObjectBlock didRemoveObjectBlock = _didRemoveObjectBlock; + [self unlock]; + + if (willRemoveObjectBlock) + willRemoveObjectBlock(self, key, object); + + [self lock]; + if (cost) + _totalCost -= [cost unsignedIntegerValue]; + + [_dictionary removeObjectForKey:key]; + [_dates removeObjectForKey:key]; + [_costs removeObjectForKey:key]; + [self unlock]; + + if (didRemoveObjectBlock) + didRemoveObjectBlock(self, key, nil); +} + +- (void)trimMemoryToDate:(NSDate *)trimDate +{ + [self lock]; + NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; + NSDictionary *dates = [_dates copy]; + [self unlock]; + + for (NSString *key in keysSortedByDate) { // oldest objects first + NSDate *accessDate = dates[key]; + if (!accessDate) + continue; + + if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date + [self removeObjectAndExecuteBlocksForKey:key]; + } else { + break; + } + } +} + +- (void)trimToCostLimit:(NSUInteger)limit +{ + NSUInteger totalCost = 0; + + [self lock]; + totalCost = _totalCost; + NSArray *keysSortedByCost = [_costs keysSortedByValueUsingSelector:@selector(compare:)]; + [self unlock]; + + if (totalCost <= limit) { + return; + } + + for (NSString *key in [keysSortedByCost reverseObjectEnumerator]) { // costliest objects first + [self removeObjectAndExecuteBlocksForKey:key]; + + [self lock]; + totalCost = _totalCost; + [self unlock]; + + if (totalCost <= limit) + break; + } +} + +- (void)trimToCostLimitByDate:(NSUInteger)limit +{ + NSUInteger totalCost = 0; + + [self lock]; + totalCost = _totalCost; + NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; + [self unlock]; + + if (totalCost <= limit) + return; + + for (NSString *key in keysSortedByDate) { // oldest objects first + [self removeObjectAndExecuteBlocksForKey:key]; + + [self lock]; + totalCost = _totalCost; + [self unlock]; + if (totalCost <= limit) + break; + } +} + +- (void)trimToAgeLimitRecursively +{ + [self lock]; + NSTimeInterval ageLimit = _ageLimit; + [self unlock]; + + if (ageLimit == 0.0) + return; + + NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-ageLimit]; + + [self trimMemoryToDate:date]; + + __weak PINMemoryCache *weakSelf = self; + + dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ageLimit * NSEC_PER_SEC)); + dispatch_after(time, _concurrentQueue, ^(void){ + PINMemoryCache *strongSelf = weakSelf; + + [strongSelf trimToAgeLimitRecursively]; + }); +} + +#pragma mark - Public Asynchronous Methods - + +- (void)containsObjectForKey:(NSString *)key block:(PINMemoryCacheContainmentBlock)block +{ + if (!key || !block) + return; + + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + BOOL containsObject = [strongSelf containsObjectForKey:key]; + + block(containsObject); + }); +} + +- (void)objectForKey:(NSString *)key block:(PINMemoryCacheObjectBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + id object = [strongSelf objectForKey:key]; + + if (block) + block(strongSelf, key, object); + }); +} + +- (void)setObject:(id)object forKey:(NSString *)key block:(PINMemoryCacheObjectBlock)block +{ + [self setObject:object forKey:key withCost:0 block:block]; +} + +- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(PINMemoryCacheObjectBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf setObject:object forKey:key withCost:cost]; + + if (block) + block(strongSelf, key, object); + }); +} + +- (void)removeObjectForKey:(NSString *)key block:(PINMemoryCacheObjectBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf removeObjectForKey:key]; + + if (block) + block(strongSelf, key, nil); + }); +} + +- (void)trimToDate:(NSDate *)trimDate block:(PINMemoryCacheBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf trimToDate:trimDate]; + + if (block) + block(strongSelf); + }); +} + +- (void)trimToCost:(NSUInteger)cost block:(PINMemoryCacheBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf trimToCost:cost]; + + if (block) + block(strongSelf); + }); +} + +- (void)trimToCostByDate:(NSUInteger)cost block:(PINMemoryCacheBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf trimToCostByDate:cost]; + + if (block) + block(strongSelf); + }); +} + +- (void)removeAllObjects:(PINMemoryCacheBlock)block +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf removeAllObjects]; + + if (block) + block(strongSelf); + }); +} + +- (void)enumerateObjectsWithBlock:(PINMemoryCacheObjectBlock)block completionBlock:(PINMemoryCacheBlock)completionBlock +{ + __weak PINMemoryCache *weakSelf = self; + + dispatch_async(_concurrentQueue, ^{ + PINMemoryCache *strongSelf = weakSelf; + [strongSelf enumerateObjectsWithBlock:block]; + + if (completionBlock) + completionBlock(strongSelf); + }); +} + +#pragma mark - Public Synchronous Methods - + +- (BOOL)containsObjectForKey:(NSString *)key +{ + if (!key) + return NO; + + [self lock]; + BOOL containsObject = (_dictionary[key] != nil); + [self unlock]; + return containsObject; +} + +- (__nullable id)objectForKey:(NSString *)key +{ + if (!key) + return nil; + + NSDate *now = [[NSDate alloc] init]; + [self lock]; + id object = nil; + // If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive + if (!self->_ttlCache || self->_ageLimit <= 0 || fabs([[_dates objectForKey:key] timeIntervalSinceDate:now]) < self->_ageLimit) { + object = _dictionary[key]; + } + [self unlock]; + + if (object) { + [self lock]; + _dates[key] = now; + [self unlock]; + } + + return object; +} + +- (id)objectForKeyedSubscript:(NSString *)key +{ + return [self objectForKey:key]; +} + +- (void)setObject:(id)object forKey:(NSString *)key +{ + [self setObject:object forKey:key withCost:0]; +} + +- (void)setObject:(id)object forKeyedSubscript:(NSString *)key +{ + [self setObject:object forKey:key]; +} + +- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost +{ + if (!key || !object) + return; + + [self lock]; + PINMemoryCacheObjectBlock willAddObjectBlock = _willAddObjectBlock; + PINMemoryCacheObjectBlock didAddObjectBlock = _didAddObjectBlock; + NSUInteger costLimit = _costLimit; + [self unlock]; + + if (willAddObjectBlock) + willAddObjectBlock(self, key, object); + + [self lock]; + NSNumber* oldCost = _costs[key]; + if (oldCost) + _totalCost -= [oldCost unsignedIntegerValue]; + + _dictionary[key] = object; + _dates[key] = [[NSDate alloc] init]; + _costs[key] = @(cost); + + _totalCost += cost; + [self unlock]; + + if (didAddObjectBlock) + didAddObjectBlock(self, key, object); + + if (costLimit > 0) + [self trimToCostByDate:costLimit]; +} + +- (void)removeObjectForKey:(NSString *)key +{ + if (!key) + return; + + [self removeObjectAndExecuteBlocksForKey:key]; +} + +- (void)trimToDate:(NSDate *)trimDate +{ + if (!trimDate) + return; + + if ([trimDate isEqualToDate:[NSDate distantPast]]) { + [self removeAllObjects]; + return; + } + + [self trimMemoryToDate:trimDate]; +} + +- (void)trimToCost:(NSUInteger)cost +{ + [self trimToCostLimit:cost]; +} + +- (void)trimToCostByDate:(NSUInteger)cost +{ + [self trimToCostLimitByDate:cost]; +} + +- (void)removeAllObjects +{ + [self lock]; + PINMemoryCacheBlock willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock; + PINMemoryCacheBlock didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock; + [self unlock]; + + if (willRemoveAllObjectsBlock) + willRemoveAllObjectsBlock(self); + + [self lock]; + [_dictionary removeAllObjects]; + [_dates removeAllObjects]; + [_costs removeAllObjects]; + + _totalCost = 0; + [self unlock]; + + if (didRemoveAllObjectsBlock) + didRemoveAllObjectsBlock(self); + +} + +- (void)enumerateObjectsWithBlock:(PINMemoryCacheObjectBlock)block +{ + if (!block) + return; + + [self lock]; + NSDate *now = [[NSDate alloc] init]; + NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; + + for (NSString *key in keysSortedByDate) { + // If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive + if (!self->_ttlCache || self->_ageLimit <= 0 || fabs([[_dates objectForKey:key] timeIntervalSinceDate:now]) < self->_ageLimit) { + block(self, key, _dictionary[key]); + } + } + [self unlock]; +} + +#pragma mark - Public Thread Safe Accessors - + +- (PINMemoryCacheObjectBlock)willAddObjectBlock +{ + [self lock]; + PINMemoryCacheObjectBlock block = _willAddObjectBlock; + [self unlock]; + + return block; +} + +- (void)setWillAddObjectBlock:(PINMemoryCacheObjectBlock)block +{ + [self lock]; + _willAddObjectBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheObjectBlock)willRemoveObjectBlock +{ + [self lock]; + PINMemoryCacheObjectBlock block = _willRemoveObjectBlock; + [self unlock]; + + return block; +} + +- (void)setWillRemoveObjectBlock:(PINMemoryCacheObjectBlock)block +{ + [self lock]; + _willRemoveObjectBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheBlock)willRemoveAllObjectsBlock +{ + [self lock]; + PINMemoryCacheBlock block = _willRemoveAllObjectsBlock; + [self unlock]; + + return block; +} + +- (void)setWillRemoveAllObjectsBlock:(PINMemoryCacheBlock)block +{ + [self lock]; + _willRemoveAllObjectsBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheObjectBlock)didAddObjectBlock +{ + [self lock]; + PINMemoryCacheObjectBlock block = _didAddObjectBlock; + [self unlock]; + + return block; +} + +- (void)setDidAddObjectBlock:(PINMemoryCacheObjectBlock)block +{ + [self lock]; + _didAddObjectBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheObjectBlock)didRemoveObjectBlock +{ + [self lock]; + PINMemoryCacheObjectBlock block = _didRemoveObjectBlock; + [self unlock]; + + return block; +} + +- (void)setDidRemoveObjectBlock:(PINMemoryCacheObjectBlock)block +{ + [self lock]; + _didRemoveObjectBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheBlock)didRemoveAllObjectsBlock +{ + [self lock]; + PINMemoryCacheBlock block = _didRemoveAllObjectsBlock; + [self unlock]; + + return block; +} + +- (void)setDidRemoveAllObjectsBlock:(PINMemoryCacheBlock)block +{ + [self lock]; + _didRemoveAllObjectsBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheBlock)didReceiveMemoryWarningBlock +{ + [self lock]; + PINMemoryCacheBlock block = _didReceiveMemoryWarningBlock; + [self unlock]; + + return block; +} + +- (void)setDidReceiveMemoryWarningBlock:(PINMemoryCacheBlock)block +{ + [self lock]; + _didReceiveMemoryWarningBlock = [block copy]; + [self unlock]; +} + +- (PINMemoryCacheBlock)didEnterBackgroundBlock +{ + [self lock]; + PINMemoryCacheBlock block = _didEnterBackgroundBlock; + [self unlock]; + + return block; +} + +- (void)setDidEnterBackgroundBlock:(PINMemoryCacheBlock)block +{ + [self lock]; + _didEnterBackgroundBlock = [block copy]; + [self unlock]; +} + +- (NSTimeInterval)ageLimit +{ + [self lock]; + NSTimeInterval ageLimit = _ageLimit; + [self unlock]; + + return ageLimit; +} + +- (void)setAgeLimit:(NSTimeInterval)ageLimit +{ + [self lock]; + _ageLimit = ageLimit; + [self unlock]; + + [self trimToAgeLimitRecursively]; +} + +- (NSUInteger)costLimit +{ + [self lock]; + NSUInteger costLimit = _costLimit; + [self unlock]; + + return costLimit; +} + +- (void)setCostLimit:(NSUInteger)costLimit +{ + [self lock]; + _costLimit = costLimit; + [self unlock]; + + if (costLimit > 0) + [self trimToCostLimitByDate:costLimit]; +} + +- (NSUInteger)totalCost +{ + [self lock]; + NSUInteger cost = _totalCost; + [self unlock]; + + return cost; +} + +- (BOOL)isTTLCache { + BOOL isTTLCache; + + [self lock]; + isTTLCache = _ttlCache; + [self unlock]; + + return isTTLCache; +} + +- (void)setTtlCache:(BOOL)ttlCache { + [self lock]; + _ttlCache = ttlCache; + [self unlock]; +} + + +- (void)lock +{ + dispatch_semaphore_wait(_lockSemaphore, DISPATCH_TIME_FOREVER); +} + +- (void)unlock +{ + dispatch_semaphore_signal(_lockSemaphore); +} + +@end diff --git a/clients/ios/Other Sources/StringHelper.m b/clients/ios/Other Sources/StringHelper.m index 41c795c99..2343e5469 100644 --- a/clients/ios/Other Sources/StringHelper.m +++ b/clients/ios/Other Sources/StringHelper.m @@ -66,13 +66,8 @@ - (NSString *)urlEncode { - NSString* encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( - NULL, - (CFStringRef) self, - NULL, - (CFStringRef)@"!*'();:@&=+$,/?%#[]", - kCFStringEncodingUTF8 ); - return [encodedString autorelease]; + NSString *encodedString = [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + return encodedString; } @end diff --git a/clients/ios/Other Sources/TMCache/TMCache.m b/clients/ios/Other Sources/TMCache/TMCache.m deleted file mode 100755 index afba609c2..000000000 --- a/clients/ios/Other Sources/TMCache/TMCache.m +++ /dev/null @@ -1,387 +0,0 @@ -#import "TMCache.h" - -NSString * const TMCachePrefix = @"com.tumblr.TMCache"; -NSString * const TMCacheSharedName = @"TMCacheShared"; - -@interface TMCache () -#if OS_OBJECT_USE_OBJC -@property (strong, nonatomic) dispatch_queue_t queue; -#else -@property (assign, nonatomic) dispatch_queue_t queue; -#endif -@end - -@implementation TMCache - -#pragma mark - Initialization - - -#if !OS_OBJECT_USE_OBJC -- (void)dealloc -{ - dispatch_release(_queue); - _queue = nil; -} -#endif - -- (instancetype)initWithName:(NSString *)name -{ - return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]]; -} - -- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath -{ - if (!name) - return nil; - - if (self = [super init]) { - _name = [name copy]; - - NSString *queueName = [[NSString alloc] initWithFormat:@"%@.%p", TMCachePrefix, self]; - _queue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_CONCURRENT); - - _diskCache = [[TMDiskCache alloc] initWithName:_name rootPath:rootPath]; - _memoryCache = [[TMMemoryCache alloc] init]; - } - return self; -} - -- (NSString *)description -{ - return [[NSString alloc] initWithFormat:@"%@.%@.%p", TMCachePrefix, _name, self]; -} - -+ (instancetype)sharedCache -{ - static id cache; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^{ - cache = [[self alloc] initWithName:TMCacheSharedName]; - }); - - return cache; -} - -#pragma mark - Public Asynchronous Methods - - -- (void)objectForKey:(NSString *)key block:(TMCacheObjectBlock)block -{ - if (!key || !block) - return; - - __weak TMCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - __weak TMCache *weakSelf = strongSelf; - - [strongSelf->_memoryCache objectForKey:key block:^(TMMemoryCache *cache, NSString *key, id object) { - TMCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - if (object) { - [strongSelf->_diskCache fileURLForKey:key block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - // update the access time on disk - }]; - - __weak TMCache *weakSelf = strongSelf; - - dispatch_async(strongSelf->_queue, ^{ - TMCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf, key, object); - }); - } else { - __weak TMCache *weakSelf = strongSelf; - - [strongSelf->_diskCache objectForKey:key block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - TMCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - [strongSelf->_memoryCache setObject:object forKey:key block:nil]; - - __weak TMCache *weakSelf = strongSelf; - - dispatch_async(strongSelf->_queue, ^{ - TMCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf, key, object); - }); - }]; - } - }]; - }); -} - -- (void)setObject:(id )object forKey:(NSString *)key block:(TMCacheObjectBlock)block -{ - if (!key || !object) - return; - - dispatch_group_t group = nil; - TMMemoryCacheObjectBlock memBlock = nil; - TMDiskCacheObjectBlock diskBlock = nil; - - if (block) { - group = dispatch_group_create(); - dispatch_group_enter(group); - dispatch_group_enter(group); - - memBlock = ^(TMMemoryCache *cache, NSString *key, id object) { - dispatch_group_leave(group); - }; - - diskBlock = ^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - dispatch_group_leave(group); - }; - } - - [_memoryCache setObject:object forKey:key block:memBlock]; - [_diskCache setObject:object forKey:key block:diskBlock]; - - if (group) { - __weak TMCache *weakSelf = self; - dispatch_group_notify(group, _queue, ^{ - TMCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf, key, object); - }); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(group); - #endif - } -} - -- (void)removeObjectForKey:(NSString *)key block:(TMCacheObjectBlock)block -{ - if (!key) - return; - - dispatch_group_t group = nil; - TMMemoryCacheObjectBlock memBlock = nil; - TMDiskCacheObjectBlock diskBlock = nil; - - if (block) { - group = dispatch_group_create(); - dispatch_group_enter(group); - dispatch_group_enter(group); - - memBlock = ^(TMMemoryCache *cache, NSString *key, id object) { - dispatch_group_leave(group); - }; - - diskBlock = ^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - dispatch_group_leave(group); - }; - } - - [_memoryCache removeObjectForKey:key block:memBlock]; - [_diskCache removeObjectForKey:key block:diskBlock]; - - if (group) { - __weak TMCache *weakSelf = self; - dispatch_group_notify(group, _queue, ^{ - TMCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf, key, nil); - }); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(group); - #endif - } -} - -- (void)removeAllObjects:(TMCacheBlock)block -{ - dispatch_group_t group = nil; - TMMemoryCacheBlock memBlock = nil; - TMDiskCacheBlock diskBlock = nil; - - if (block) { - group = dispatch_group_create(); - dispatch_group_enter(group); - dispatch_group_enter(group); - - memBlock = ^(TMMemoryCache *cache) { - dispatch_group_leave(group); - }; - - diskBlock = ^(TMDiskCache *cache) { - dispatch_group_leave(group); - }; - } - - [_memoryCache removeAllObjects:memBlock]; - [_diskCache removeAllObjects:diskBlock]; - - if (group) { - __weak TMCache *weakSelf = self; - dispatch_group_notify(group, _queue, ^{ - TMCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf); - }); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(group); - #endif - } -} - -- (void)trimToDate:(NSDate *)date block:(TMCacheBlock)block -{ - if (!date) - return; - - dispatch_group_t group = nil; - TMMemoryCacheBlock memBlock = nil; - TMDiskCacheBlock diskBlock = nil; - - if (block) { - group = dispatch_group_create(); - dispatch_group_enter(group); - dispatch_group_enter(group); - - memBlock = ^(TMMemoryCache *cache) { - dispatch_group_leave(group); - }; - - diskBlock = ^(TMDiskCache *cache) { - dispatch_group_leave(group); - }; - } - - [_memoryCache trimToDate:date block:memBlock]; - [_diskCache trimToDate:date block:diskBlock]; - - if (group) { - __weak TMCache *weakSelf = self; - dispatch_group_notify(group, _queue, ^{ - TMCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf); - }); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(group); - #endif - } -} - -#pragma mark - Public Synchronous Accessors - - -- (NSUInteger)diskByteCount -{ - __block NSUInteger byteCount = 0; - - dispatch_sync([TMDiskCache sharedQueue], ^{ - byteCount = self.diskCache.byteCount; - }); - - return byteCount; -} - -#pragma mark - Public Synchronous Methods - - -- (id)objectForKey:(NSString *)key -{ - if (!key) - return nil; - - __block id objectForKey = nil; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self objectForKey:key block:^(TMCache *cache, NSString *key, id object) { - objectForKey = object; - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif - - return objectForKey; -} - -- (void)setObject:(id )object forKey:(NSString *)key -{ - if (!object || !key) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self setObject:object forKey:key block:^(TMCache *cache, NSString *key, id object) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)removeObjectForKey:(NSString *)key -{ - if (!key) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self removeObjectForKey:key block:^(TMCache *cache, NSString *key, id object) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToDate:(NSDate *)date -{ - if (!date) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToDate:date block:^(TMCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)removeAllObjects -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self removeAllObjects:^(TMCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -@end - -// HC SVNT DRACONES diff --git a/clients/ios/Other Sources/TMCache/TMDiskCache.m b/clients/ios/Other Sources/TMCache/TMDiskCache.m deleted file mode 100755 index a15f07d4d..000000000 --- a/clients/ios/Other Sources/TMCache/TMDiskCache.m +++ /dev/null @@ -1,1040 +0,0 @@ -#import "TMDiskCache.h" - -#define TMDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ - [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ - __LINE__, [error localizedDescription]); } - -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 - #define TMCacheStartBackgroundTask() UIBackgroundTaskIdentifier taskID = UIBackgroundTaskInvalid; \ - taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ \ - [[UIApplication sharedApplication] endBackgroundTask:taskID]; }]; - #define TMCacheEndBackgroundTask() [[UIApplication sharedApplication] endBackgroundTask:taskID]; -#else - #define TMCacheStartBackgroundTask() - #define TMCacheEndBackgroundTask() -#endif - -NSString * const TMDiskCachePrefix = @"com.tumblr.TMDiskCache"; -NSString * const TMDiskCacheSharedName = @"TMDiskCacheShared"; - -@interface TMDiskCache () -@property (assign) NSUInteger byteCount; -@property (strong, nonatomic) NSURL *cacheURL; -@property (assign, nonatomic) dispatch_queue_t queue; -@property (strong, nonatomic) NSMutableDictionary *dates; -@property (strong, nonatomic) NSMutableDictionary *sizes; -@end - -@implementation TMDiskCache - -@synthesize willAddObjectBlock = _willAddObjectBlock; -@synthesize willRemoveObjectBlock = _willRemoveObjectBlock; -@synthesize willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock; -@synthesize didAddObjectBlock = _didAddObjectBlock; -@synthesize didRemoveObjectBlock = _didRemoveObjectBlock; -@synthesize didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock; -@synthesize byteLimit = _byteLimit; -@synthesize ageLimit = _ageLimit; - -#pragma mark - Initialization - - -- (instancetype)initWithName:(NSString *)name -{ - return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]]; -} - -- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath -{ - if (!name) - return nil; - - if (self = [super init]) { - _name = [name copy]; - _queue = [TMDiskCache sharedQueue]; - - _willAddObjectBlock = nil; - _willRemoveObjectBlock = nil; - _willRemoveAllObjectsBlock = nil; - _didAddObjectBlock = nil; - _didRemoveObjectBlock = nil; - _didRemoveAllObjectsBlock = nil; - - _byteCount = 0; - _byteLimit = 0; - _ageLimit = 0.0; - - _dates = [[NSMutableDictionary alloc] init]; - _sizes = [[NSMutableDictionary alloc] init]; - - NSString *pathComponent = [[NSString alloc] initWithFormat:@"%@.%@", TMDiskCachePrefix, _name]; - _cacheURL = [NSURL fileURLWithPathComponents:@[ rootPath, pathComponent ]]; - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - [strongSelf createCacheDirectory]; - [strongSelf initializeDiskProperties]; - }); - } - return self; -} - -- (NSString *)description -{ - return [[NSString alloc] initWithFormat:@"%@.%@.%p", TMDiskCachePrefix, _name, self]; -} - -+ (instancetype)sharedCache -{ - static id cache; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^{ - cache = [[self alloc] initWithName:TMDiskCacheSharedName]; - }); - - return cache; -} - -+ (dispatch_queue_t)sharedQueue -{ - static dispatch_queue_t queue; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^{ - queue = dispatch_queue_create([TMDiskCachePrefix UTF8String], DISPATCH_QUEUE_SERIAL); - }); - - return queue; -} - -#pragma mark - Private Methods - - -- (NSURL *)encodedFileURLForKey:(NSString *)key -{ - if (![key length]) - return nil; - - return [_cacheURL URLByAppendingPathComponent:[self encodedString:key]]; -} - -- (NSString *)keyForEncodedFileURL:(NSURL *)url -{ - NSString *fileName = [url lastPathComponent]; - if (!fileName) - return nil; - - return [self decodedString:fileName]; -} - -- (NSString *)encodedString:(NSString *)string -{ - if (![string length]) - return @""; - - CFStringRef static const charsToEscape = CFSTR(".:/"); - CFStringRef escapedString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, - (__bridge CFStringRef)string, - NULL, - charsToEscape, - kCFStringEncodingUTF8); - return (__bridge_transfer NSString *)escapedString; -} - -- (NSString *)decodedString:(NSString *)string -{ - if (![string length]) - return @""; - - CFStringRef unescapedString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, - (__bridge CFStringRef)string, - CFSTR(""), - kCFStringEncodingUTF8); - return (__bridge_transfer NSString *)unescapedString; -} - -#pragma mark - Private Trash Methods - - -+ (dispatch_queue_t)sharedTrashQueue -{ - static dispatch_queue_t trashQueue; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^{ - NSString *queueName = [[NSString alloc] initWithFormat:@"%@.trash", TMDiskCachePrefix]; - trashQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_SERIAL); - dispatch_set_target_queue(trashQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); - }); - - return trashQueue; -} - -+ (NSURL *)sharedTrashURL -{ - static NSURL *sharedTrashURL; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^{ - sharedTrashURL = [[[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:TMDiskCachePrefix isDirectory:YES]; - - dispatch_async([self sharedTrashQueue], ^{ - if (![[NSFileManager defaultManager] fileExistsAtPath:[sharedTrashURL path]]) { - NSError *error = nil; - [[NSFileManager defaultManager] createDirectoryAtURL:sharedTrashURL - withIntermediateDirectories:YES - attributes:nil - error:&error]; - TMDiskCacheError(error); - } - }); - }); - - return sharedTrashURL; -} - -+(BOOL)moveItemAtURLToTrash:(NSURL *)itemURL -{ - if (![[NSFileManager defaultManager] fileExistsAtPath:[itemURL path]]) - return NO; - - NSError *error = nil; - NSString *uniqueString = [[NSProcessInfo processInfo] globallyUniqueString]; - NSURL *uniqueTrashURL = [[TMDiskCache sharedTrashURL] URLByAppendingPathComponent:uniqueString]; - BOOL moved = [[NSFileManager defaultManager] moveItemAtURL:itemURL toURL:uniqueTrashURL error:&error]; - TMDiskCacheError(error); - return moved; -} - -+ (void)emptyTrash -{ - TMCacheStartBackgroundTask(); - - dispatch_async([self sharedTrashQueue], ^{ - NSError *error = nil; - NSArray *trashedItems = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[self sharedTrashURL] - includingPropertiesForKeys:nil - options:0 - error:&error]; - TMDiskCacheError(error); - - for (NSURL *trashedItemURL in trashedItems) { - NSError *error = nil; - [[NSFileManager defaultManager] removeItemAtURL:trashedItemURL error:&error]; - TMDiskCacheError(error); - } - - TMCacheEndBackgroundTask(); - }); -} - -#pragma mark - Private Queue Methods - - -- (BOOL)createCacheDirectory -{ - if ([[NSFileManager defaultManager] fileExistsAtPath:[_cacheURL path]]) - return NO; - - NSError *error = nil; - BOOL success = [[NSFileManager defaultManager] createDirectoryAtURL:_cacheURL - withIntermediateDirectories:YES - attributes:nil - error:&error]; - TMDiskCacheError(error); - - return success; -} - -- (void)initializeDiskProperties -{ - NSUInteger byteCount = 0; - NSArray *keys = @[ NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ]; - - NSError *error = nil; - NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:_cacheURL - includingPropertiesForKeys:keys - options:NSDirectoryEnumerationSkipsHiddenFiles - error:&error]; - TMDiskCacheError(error); - - for (NSURL *fileURL in files) { - NSString *key = [self keyForEncodedFileURL:fileURL]; - - error = nil; - NSDictionary *dictionary = [fileURL resourceValuesForKeys:keys error:&error]; - TMDiskCacheError(error); - - NSDate *date = [dictionary objectForKey:NSURLContentModificationDateKey]; - if (date) - [_dates setObject:date forKey:key]; - - NSNumber *fileSize = [dictionary objectForKey:NSURLTotalFileAllocatedSizeKey]; - if (fileSize) { - [_sizes setObject:fileSize forKey:key]; - byteCount += [fileSize unsignedIntegerValue]; - } - } - - if (byteCount > 0) - self.byteCount = byteCount; // atomic -} - -- (BOOL)setFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL -{ - NSError *error = nil; - BOOL success = [[NSFileManager defaultManager] setAttributes:@{ NSFileModificationDate: date } - ofItemAtPath:[fileURL path] - error:&error]; - TMDiskCacheError(error); - - if (success) - [_dates setObject:date forKey:[self keyForEncodedFileURL:fileURL]]; - - return success; -} - -- (BOOL)removeFileAndExecuteBlocksForKey:(NSString *)key -{ - NSURL *fileURL = [self encodedFileURLForKey:key]; - if (!fileURL || ![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) - return NO; - - if (_willRemoveObjectBlock) - _willRemoveObjectBlock(self, key, nil, fileURL); - - BOOL trashed = [TMDiskCache moveItemAtURLToTrash:fileURL]; - if (!trashed) - return NO; - - [TMDiskCache emptyTrash]; - - NSNumber *byteSize = [_sizes objectForKey:key]; - if (byteSize) - self.byteCount = _byteCount - [byteSize unsignedIntegerValue]; // atomic - - [_sizes removeObjectForKey:key]; - [_dates removeObjectForKey:key]; - - if (_didRemoveObjectBlock) - _didRemoveObjectBlock(self, key, nil, fileURL); - - return YES; -} - -- (void)trimDiskToSize:(NSUInteger)trimByteCount -{ - if (_byteCount <= trimByteCount) - return; - - NSArray *keysSortedBySize = [_sizes keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in [keysSortedBySize reverseObjectEnumerator]) { // largest objects first - [self removeFileAndExecuteBlocksForKey:key]; - - if (_byteCount <= trimByteCount) - break; - } -} - -- (void)trimDiskToSizeByDate:(NSUInteger)trimByteCount -{ - if (_byteCount <= trimByteCount) - return; - - NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in keysSortedByDate) { // oldest objects first - [self removeFileAndExecuteBlocksForKey:key]; - - if (_byteCount <= trimByteCount) - break; - } -} - -- (void)trimDiskToDate:(NSDate *)trimDate -{ - NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in keysSortedByDate) { // oldest files first - NSDate *accessDate = [_dates objectForKey:key]; - if (!accessDate) - continue; - - if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date - [self removeFileAndExecuteBlocksForKey:key]; - } else { - break; - } - } -} - -- (void)trimToAgeLimitRecursively -{ - if (_ageLimit == 0.0) - return; - - NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-_ageLimit]; - [self trimDiskToDate:date]; - - __weak TMDiskCache *weakSelf = self; - - dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_ageLimit * NSEC_PER_SEC)); - dispatch_after(time, _queue, ^(void) { - TMDiskCache *strongSelf = weakSelf; - [strongSelf trimToAgeLimitRecursively]; - }); -} - -#pragma mark - Public Asynchronous Methods - - -- (void)objectForKey:(NSString *)key block:(TMDiskCacheObjectBlock)block -{ - NSDate *now = [[NSDate alloc] init]; - - if (!key || !block) - return; - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - NSURL *fileURL = [strongSelf encodedFileURLForKey:key]; - id object = nil; - - if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) { - object = [NSKeyedUnarchiver unarchiveObjectWithFile:[fileURL path]]; - [strongSelf setFileModificationDate:now forURL:fileURL]; - } - - block(strongSelf, key, object, fileURL); - }); -} - -- (void)fileURLForKey:(NSString *)key block:(TMDiskCacheObjectBlock)block -{ - NSDate *now = [[NSDate alloc] init]; - - if (!key || !block) - return; - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - NSURL *fileURL = [strongSelf encodedFileURLForKey:key]; - - if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) { - [strongSelf setFileModificationDate:now forURL:fileURL]; - } else { - fileURL = nil; - } - - block(strongSelf, key, nil, fileURL); - }); -} - -- (void)setObject:(id )object forKey:(NSString *)key block:(TMDiskCacheObjectBlock)block -{ - NSDate *now = [[NSDate alloc] init]; - - if (!key || !object) - return; - - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - NSURL *fileURL = [strongSelf encodedFileURLForKey:key]; - - if (strongSelf->_willAddObjectBlock) - strongSelf->_willAddObjectBlock(strongSelf, key, object, fileURL); - - BOOL written = [NSKeyedArchiver archiveRootObject:object toFile:[fileURL path]]; - - if (written) { - [strongSelf setFileModificationDate:now forURL:fileURL]; - - NSError *error = nil; - NSDictionary *values = [fileURL resourceValuesForKeys:@[ NSURLTotalFileAllocatedSizeKey ] error:&error]; - TMDiskCacheError(error); - - NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey]; - if (diskFileSize) { - [strongSelf->_sizes setObject:diskFileSize forKey:key]; - strongSelf.byteCount = strongSelf->_byteCount + [diskFileSize unsignedIntegerValue]; // atomic - } - - if (strongSelf->_byteLimit > 0 && strongSelf->_byteCount > strongSelf->_byteLimit) - [strongSelf trimToSizeByDate:strongSelf->_byteLimit block:nil]; - } else { - fileURL = nil; - } - - if (strongSelf->_didAddObjectBlock) - strongSelf->_didAddObjectBlock(strongSelf, key, object, written ? fileURL : nil); - - if (block) - block(strongSelf, key, object, fileURL); - - TMCacheEndBackgroundTask(); - }); -} - -- (void)removeObjectForKey:(NSString *)key block:(TMDiskCacheObjectBlock)block -{ - if (!key) - return; - - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - NSURL *fileURL = [strongSelf encodedFileURLForKey:key]; - [strongSelf removeFileAndExecuteBlocksForKey:key]; - - if (block) - block(strongSelf, key, nil, fileURL); - - TMCacheEndBackgroundTask(); - }); -} - -- (void)trimToSize:(NSUInteger)trimByteCount block:(TMDiskCacheBlock)block -{ - if (trimByteCount == 0) { - [self removeAllObjects:block]; - return; - } - - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - [strongSelf trimDiskToSize:trimByteCount]; - - if (block) - block(strongSelf); - - TMCacheEndBackgroundTask(); - }); -} - -- (void)trimToDate:(NSDate *)trimDate block:(TMDiskCacheBlock)block -{ - if (!trimDate) - return; - - if ([trimDate isEqualToDate:[NSDate distantPast]]) { - [self removeAllObjects:block]; - return; - } - - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - [strongSelf trimDiskToDate:trimDate]; - - if (block) - block(strongSelf); - - TMCacheEndBackgroundTask(); - }); -} - -- (void)trimToSizeByDate:(NSUInteger)trimByteCount block:(TMDiskCacheBlock)block -{ - if (trimByteCount == 0) { - [self removeAllObjects:block]; - return; - } - - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - [strongSelf trimDiskToSizeByDate:trimByteCount]; - - if (block) - block(strongSelf); - - TMCacheEndBackgroundTask(); - }); -} - -- (void)removeAllObjects:(TMDiskCacheBlock)block -{ - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - if (strongSelf->_willRemoveAllObjectsBlock) - strongSelf->_willRemoveAllObjectsBlock(strongSelf); - - [TMDiskCache moveItemAtURLToTrash:strongSelf->_cacheURL]; - [TMDiskCache emptyTrash]; - - [strongSelf createCacheDirectory]; - - [strongSelf->_dates removeAllObjects]; - [strongSelf->_sizes removeAllObjects]; - strongSelf.byteCount = 0; // atomic - - if (strongSelf->_didRemoveAllObjectsBlock) - strongSelf->_didRemoveAllObjectsBlock(strongSelf); - - if (block) - block(strongSelf); - - TMCacheEndBackgroundTask(); - }); -} - -- (void)enumerateObjectsWithBlock:(TMDiskCacheObjectBlock)block completionBlock:(TMDiskCacheBlock)completionBlock -{ - if (!block) - return; - - TMCacheStartBackgroundTask(); - - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) { - TMCacheEndBackgroundTask(); - return; - } - - NSArray *keysSortedByDate = [strongSelf->_dates keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in keysSortedByDate) { - NSURL *fileURL = [strongSelf encodedFileURLForKey:key]; - block(strongSelf, key, nil, fileURL); - } - - if (completionBlock) - completionBlock(strongSelf); - - TMCacheEndBackgroundTask(); - }); -} - -#pragma mark - Public Synchronous Methods - - -- (id )objectForKey:(NSString *)key -{ - if (!key) - return nil; - - __block id objectForKey = nil; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self objectForKey:key block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - objectForKey = object; - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif - - return objectForKey; -} - -- (NSURL *)fileURLForKey:(NSString *)key -{ - if (!key) - return nil; - - __block NSURL *fileURLForKey = nil; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self fileURLForKey:key block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - fileURLForKey = fileURL; - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif - - return fileURLForKey; -} - -- (void)setObject:(id )object forKey:(NSString *)key -{ - if (!object || !key) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self setObject:object forKey:key block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)removeObjectForKey:(NSString *)key -{ - if (!key) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self removeObjectForKey:key block:^(TMDiskCache *cache, NSString *key, id object, NSURL *fileURL) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToSize:(NSUInteger)byteCount -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToSize:byteCount block:^(TMDiskCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToDate:(NSDate *)date -{ - if (!date) - return; - - if ([date isEqualToDate:[NSDate distantPast]]) { - [self removeAllObjects]; - return; - } - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToDate:date block:^(TMDiskCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToSizeByDate:(NSUInteger)byteCount -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToSizeByDate:byteCount block:^(TMDiskCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)removeAllObjects -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self removeAllObjects:^(TMDiskCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)enumerateObjectsWithBlock:(TMDiskCacheObjectBlock)block -{ - if (!block) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self enumerateObjectsWithBlock:block completionBlock:^(TMDiskCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -#pragma mark - Public Thread Safe Accessors - - -- (TMDiskCacheObjectBlock)willAddObjectBlock -{ - __block TMDiskCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _willAddObjectBlock; - }); - - return block; -} - -- (void)setWillAddObjectBlock:(TMDiskCacheObjectBlock)block -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_willAddObjectBlock = [block copy]; - }); -} - -- (TMDiskCacheObjectBlock)willRemoveObjectBlock -{ - __block TMDiskCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _willRemoveObjectBlock; - }); - - return block; -} - -- (void)setWillRemoveObjectBlock:(TMDiskCacheObjectBlock)block -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_willRemoveObjectBlock = [block copy]; - }); -} - -- (TMDiskCacheBlock)willRemoveAllObjectsBlock -{ - __block TMDiskCacheBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _willRemoveAllObjectsBlock; - }); - - return block; -} - -- (void)setWillRemoveAllObjectsBlock:(TMDiskCacheBlock)block -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_willRemoveAllObjectsBlock = [block copy]; - }); -} - -- (TMDiskCacheObjectBlock)didAddObjectBlock -{ - __block TMDiskCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didAddObjectBlock; - }); - - return block; -} - -- (void)setDidAddObjectBlock:(TMDiskCacheObjectBlock)block -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didAddObjectBlock = [block copy]; - }); -} - -- (TMDiskCacheObjectBlock)didRemoveObjectBlock -{ - __block TMDiskCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didRemoveObjectBlock; - }); - - return block; -} - -- (void)setDidRemoveObjectBlock:(TMDiskCacheObjectBlock)block -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didRemoveObjectBlock = [block copy]; - }); -} - -- (TMDiskCacheBlock)didRemoveAllObjectsBlock -{ - __block TMDiskCacheBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didRemoveAllObjectsBlock; - }); - - return block; -} - -- (void)setDidRemoveAllObjectsBlock:(TMDiskCacheBlock)block -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didRemoveAllObjectsBlock = [block copy]; - }); -} - -- (NSUInteger)byteLimit -{ - __block NSUInteger byteLimit = 0; - - dispatch_sync(_queue, ^{ - byteLimit = _byteLimit; - }); - - return byteLimit; -} - -- (void)setByteLimit:(NSUInteger)byteLimit -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_byteLimit = byteLimit; - - if (byteLimit > 0) - [strongSelf trimDiskToSizeByDate:byteLimit]; - }); -} - -- (NSTimeInterval)ageLimit -{ - __block NSTimeInterval ageLimit = 0.0; - - dispatch_sync(_queue, ^{ - ageLimit = _ageLimit; - }); - - return ageLimit; -} - -- (void)setAgeLimit:(NSTimeInterval)ageLimit -{ - __weak TMDiskCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMDiskCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_ageLimit = ageLimit; - - [strongSelf trimToAgeLimitRecursively]; - }); -} - -@end diff --git a/clients/ios/Other Sources/TMCache/TMMemoryCache.m b/clients/ios/Other Sources/TMCache/TMMemoryCache.m deleted file mode 100755 index 2b88e417e..000000000 --- a/clients/ios/Other Sources/TMCache/TMMemoryCache.m +++ /dev/null @@ -1,870 +0,0 @@ -#import "TMMemoryCache.h" - -NSString * const TMMemoryCachePrefix = @"com.tumblr.TMMemoryCache"; - -@interface TMMemoryCache () -#if OS_OBJECT_USE_OBJC -@property (strong, nonatomic) dispatch_queue_t queue; -#else -@property (assign, nonatomic) dispatch_queue_t queue; -#endif -@property (strong, nonatomic) NSMutableDictionary *dictionary; -@property (strong, nonatomic) NSMutableDictionary *dates; -@property (strong, nonatomic) NSMutableDictionary *costs; -@end - -@implementation TMMemoryCache - -@synthesize ageLimit = _ageLimit; -@synthesize costLimit = _costLimit; -@synthesize totalCost = _totalCost; -@synthesize willAddObjectBlock = _willAddObjectBlock; -@synthesize willRemoveObjectBlock = _willRemoveObjectBlock; -@synthesize willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock; -@synthesize didAddObjectBlock = _didAddObjectBlock; -@synthesize didRemoveObjectBlock = _didRemoveObjectBlock; -@synthesize didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock; -@synthesize didReceiveMemoryWarningBlock = _didReceiveMemoryWarningBlock; -@synthesize didEnterBackgroundBlock = _didEnterBackgroundBlock; - -#pragma mark - Initialization - - -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; - - #if !OS_OBJECT_USE_OBJC - dispatch_release(_queue); - _queue = nil; - #endif -} - -- (id)init -{ - if (self = [super init]) { - NSString *queueName = [[NSString alloc] initWithFormat:@"%@.%p", TMMemoryCachePrefix, self]; - _queue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_CONCURRENT); - - _dictionary = [[NSMutableDictionary alloc] init]; - _dates = [[NSMutableDictionary alloc] init]; - _costs = [[NSMutableDictionary alloc] init]; - - _willAddObjectBlock = nil; - _willRemoveObjectBlock = nil; - _willRemoveAllObjectsBlock = nil; - - _didAddObjectBlock = nil; - _didRemoveObjectBlock = nil; - _didRemoveAllObjectsBlock = nil; - - _didReceiveMemoryWarningBlock = nil; - _didEnterBackgroundBlock = nil; - - _ageLimit = 0.0; - _costLimit = 0; - _totalCost = 0; - - _removeAllObjectsOnMemoryWarning = YES; - _removeAllObjectsOnEnteringBackground = YES; - - #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 - for (NSString *name in @[UIApplicationDidReceiveMemoryWarningNotification, UIApplicationDidEnterBackgroundNotification]) { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(didObserveApocalypticNotification:) - name:name - object:[UIApplication sharedApplication]]; - } - #endif - } - return self; -} - -+ (instancetype)sharedCache -{ - static id cache; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^{ - cache = [[self alloc] init]; - }); - - return cache; -} - -#pragma mark - Private Methods - - -- (void)didObserveApocalypticNotification:(NSNotification *)notification -{ - #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 - - if ([[notification name] isEqualToString:UIApplicationDidReceiveMemoryWarningNotification]) { - if (self.removeAllObjectsOnMemoryWarning) - [self removeAllObjects:nil]; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - if (strongSelf->_didReceiveMemoryWarningBlock) - strongSelf->_didReceiveMemoryWarningBlock(strongSelf); - }); - } else if ([[notification name] isEqualToString:UIApplicationDidEnterBackgroundNotification]) { - if (self.removeAllObjectsOnEnteringBackground) - [self removeAllObjects:nil]; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - if (strongSelf->_didEnterBackgroundBlock) - strongSelf->_didEnterBackgroundBlock(strongSelf); - }); - } - - #endif -} - -- (void)removeObjectAndExecuteBlocksForKey:(NSString *)key -{ - id object = [_dictionary objectForKey:key]; - NSNumber *cost = [_costs objectForKey:key]; - - if (_willRemoveObjectBlock) - _willRemoveObjectBlock(self, key, object); - - if (cost) - _totalCost -= [cost unsignedIntegerValue]; - - [_dictionary removeObjectForKey:key]; - [_dates removeObjectForKey:key]; - [_costs removeObjectForKey:key]; - - if (_didRemoveObjectBlock) - _didRemoveObjectBlock(self, key, nil); -} - -- (void)trimMemoryToDate:(NSDate *)trimDate -{ - NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in keysSortedByDate) { // oldest objects first - NSDate *accessDate = [_dates objectForKey:key]; - if (!accessDate) - continue; - - if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date - [self removeObjectAndExecuteBlocksForKey:key]; - } else { - break; - } - } -} - -- (void)trimToCostLimit:(NSUInteger)limit -{ - if (_totalCost <= limit) - return; - - NSArray *keysSortedByCost = [_costs keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in [keysSortedByCost reverseObjectEnumerator]) { // costliest objects first - [self removeObjectAndExecuteBlocksForKey:key]; - - if (_totalCost <= limit) - break; - } -} - -- (void)trimToCostLimitByDate:(NSUInteger)limit -{ - if (_totalCost <= limit) - return; - - NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in keysSortedByDate) { // oldest objects first - [self removeObjectAndExecuteBlocksForKey:key]; - - if (_totalCost <= limit) - break; - } -} - -- (void)trimToAgeLimitRecursively -{ - if (_ageLimit == 0.0) - return; - - NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-_ageLimit]; - [self trimMemoryToDate:date]; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_ageLimit * NSEC_PER_SEC)); - dispatch_after(time, _queue, ^(void){ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - __weak TMMemoryCache *weakSelf = strongSelf; - - dispatch_barrier_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - [strongSelf trimToAgeLimitRecursively]; - }); - }); -} - -#pragma mark - Public Asynchronous Methods - - -- (void)objectForKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block -{ - NSDate *now = [[NSDate alloc] init]; - - if (!key || !block) - return; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - id object = [strongSelf->_dictionary objectForKey:key]; - - if (object) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_barrier_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - [strongSelf->_dates setObject:now forKey:key]; - }); - } - - block(strongSelf, key, object); - }); -} - -- (void)setObject:(id)object forKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block -{ - [self setObject:object forKey:key withCost:0 block:block]; -} - -- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(TMMemoryCacheObjectBlock)block -{ - NSDate *now = [[NSDate alloc] init]; - - if (!key || !object) - return; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - if (strongSelf->_willAddObjectBlock) - strongSelf->_willAddObjectBlock(strongSelf, key, object); - - [strongSelf->_dictionary setObject:object forKey:key]; - [strongSelf->_dates setObject:now forKey:key]; - [strongSelf->_costs setObject:@(cost) forKey:key]; - - _totalCost += cost; - - if (strongSelf->_didAddObjectBlock) - strongSelf->_didAddObjectBlock(strongSelf, key, object); - - if (strongSelf->_costLimit > 0) - [strongSelf trimToCostByDate:strongSelf->_costLimit block:nil]; - - if (block) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf, key, object); - }); - } - }); -} - -- (void)removeObjectForKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block -{ - if (!key) - return; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - [strongSelf removeObjectAndExecuteBlocksForKey:key]; - - if (block) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf, key, nil); - }); - } - }); -} - -- (void)trimToDate:(NSDate *)trimDate block:(TMMemoryCacheBlock)block -{ - if (!trimDate) - return; - - if ([trimDate isEqualToDate:[NSDate distantPast]]) { - [self removeAllObjects:block]; - return; - } - - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - [strongSelf trimMemoryToDate:trimDate]; - - if (block) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf); - }); - } - }); -} - -- (void)trimToCost:(NSUInteger)cost block:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - [strongSelf trimToCostLimit:cost]; - - if (block) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf); - }); - } - }); -} - -- (void)trimToCostByDate:(NSUInteger)cost block:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - [strongSelf trimToCostLimitByDate:cost]; - - if (block) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf); - }); - } - }); -} - -- (void)removeAllObjects:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - if (strongSelf->_willRemoveAllObjectsBlock) - strongSelf->_willRemoveAllObjectsBlock(strongSelf); - - [strongSelf->_dictionary removeAllObjects]; - [strongSelf->_dates removeAllObjects]; - [strongSelf->_costs removeAllObjects]; - - strongSelf->_totalCost = 0; - - if (strongSelf->_didRemoveAllObjectsBlock) - strongSelf->_didRemoveAllObjectsBlock(strongSelf); - - if (block) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - block(strongSelf); - }); - } - }); -} - -- (void)enumerateObjectsWithBlock:(TMMemoryCacheObjectBlock)block completionBlock:(TMMemoryCacheBlock)completionBlock -{ - if (!block) - return; - - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - NSArray *keysSortedByDate = [strongSelf->_dates keysSortedByValueUsingSelector:@selector(compare:)]; - - for (NSString *key in keysSortedByDate) { - block(strongSelf, key, [strongSelf->_dictionary objectForKey:key]); - } - - if (completionBlock) { - __weak TMMemoryCache *weakSelf = strongSelf; - dispatch_async(strongSelf->_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (strongSelf) - completionBlock(strongSelf); - }); - } - }); -} - -#pragma mark - Public Synchronous Methods - - -- (id)objectForKey:(NSString *)key -{ - if (!key) - return nil; - - __block id objectForKey = nil; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self objectForKey:key block:^(TMMemoryCache *cache, NSString *key, id object) { - objectForKey = object; - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif - - return objectForKey; -} - -- (void)setObject:(id)object forKey:(NSString *)key -{ - [self setObject:object forKey:key withCost:0]; -} - -- (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost -{ - if (!object || !key) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self setObject:object forKey:key withCost:cost block:^(TMMemoryCache *cache, NSString *key, id object) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)removeObjectForKey:(NSString *)key -{ - if (!key) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self removeObjectForKey:key block:^(TMMemoryCache *cache, NSString *key, id object) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToDate:(NSDate *)date -{ - if (!date) - return; - - if ([date isEqualToDate:[NSDate distantPast]]) { - [self removeAllObjects]; - return; - } - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToDate:date block:^(TMMemoryCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToCost:(NSUInteger)cost -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToCost:cost block:^(TMMemoryCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)trimToCostByDate:(NSUInteger)cost -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self trimToCostByDate:cost block:^(TMMemoryCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)removeAllObjects -{ - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self removeAllObjects:^(TMMemoryCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -- (void)enumerateObjectsWithBlock:(TMMemoryCacheObjectBlock)block -{ - if (!block) - return; - - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - - [self enumerateObjectsWithBlock:block completionBlock:^(TMMemoryCache *cache) { - dispatch_semaphore_signal(semaphore); - }]; - - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - - #if !OS_OBJECT_USE_OBJC - dispatch_release(semaphore); - #endif -} - -#pragma mark - Public Thread Safe Accessors - - -- (TMMemoryCacheObjectBlock)willAddObjectBlock -{ - __block TMMemoryCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = self->_willAddObjectBlock; - }); - - return block; -} - -- (void)setWillAddObjectBlock:(TMMemoryCacheObjectBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_willAddObjectBlock = [block copy]; - }); -} - -- (TMMemoryCacheObjectBlock)willRemoveObjectBlock -{ - __block TMMemoryCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _willRemoveObjectBlock; - }); - - return block; -} - -- (void)setWillRemoveObjectBlock:(TMMemoryCacheObjectBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_willRemoveObjectBlock = [block copy]; - }); -} - -- (TMMemoryCacheBlock)willRemoveAllObjectsBlock -{ - __block TMMemoryCacheBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _willRemoveAllObjectsBlock; - }); - - return block; -} - -- (void)setWillRemoveAllObjectsBlock:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_willRemoveAllObjectsBlock = [block copy]; - }); -} - -- (TMMemoryCacheObjectBlock)didAddObjectBlock -{ - __block TMMemoryCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didAddObjectBlock; - }); - - return block; -} - -- (void)setDidAddObjectBlock:(TMMemoryCacheObjectBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didAddObjectBlock = [block copy]; - }); -} - -- (TMMemoryCacheObjectBlock)didRemoveObjectBlock -{ - __block TMMemoryCacheObjectBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didRemoveObjectBlock; - }); - - return block; -} - -- (void)setDidRemoveObjectBlock:(TMMemoryCacheObjectBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didRemoveObjectBlock = [block copy]; - }); -} - -- (TMMemoryCacheBlock)didRemoveAllObjectsBlock -{ - __block TMMemoryCacheBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didRemoveAllObjectsBlock; - }); - - return block; -} - -- (void)setDidRemoveAllObjectsBlock:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didRemoveAllObjectsBlock = [block copy]; - }); -} - -- (TMMemoryCacheBlock)didReceiveMemoryWarningBlock -{ - __block TMMemoryCacheBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didReceiveMemoryWarningBlock; - }); - - return block; -} - -- (void)setDidReceiveMemoryWarningBlock:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didReceiveMemoryWarningBlock = [block copy]; - }); -} - -- (TMMemoryCacheBlock)didEnterBackgroundBlock -{ - __block TMMemoryCacheBlock block = nil; - - dispatch_sync(_queue, ^{ - block = _didEnterBackgroundBlock; - }); - - return block; -} - -- (void)setDidEnterBackgroundBlock:(TMMemoryCacheBlock)block -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_didEnterBackgroundBlock = [block copy]; - }); -} - -- (NSTimeInterval)ageLimit -{ - __block NSTimeInterval ageLimit = 0.0; - - dispatch_sync(_queue, ^{ - ageLimit = _ageLimit; - }); - - return ageLimit; -} - -- (void)setAgeLimit:(NSTimeInterval)ageLimit -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_ageLimit = ageLimit; - - [strongSelf trimToAgeLimitRecursively]; - }); -} - -- (NSUInteger)costLimit -{ - __block NSUInteger costLimit = 0; - - dispatch_sync(_queue, ^{ - costLimit = _costLimit; - }); - - return costLimit; -} - -- (void)setCostLimit:(NSUInteger)costLimit -{ - __weak TMMemoryCache *weakSelf = self; - - dispatch_barrier_async(_queue, ^{ - TMMemoryCache *strongSelf = weakSelf; - if (!strongSelf) - return; - - strongSelf->_costLimit = costLimit; - - if (costLimit > 0) - [strongSelf trimToCostLimitByDate:costLimit]; - }); -} - -- (NSUInteger)totalCost -{ - __block NSUInteger cost = 0; - - dispatch_sync(_queue, ^{ - cost = _totalCost; - }); - - return cost; -} - -@end diff --git a/clients/ios/Other Sources/TUSafariActivity/TUSafariActivity.m b/clients/ios/Other Sources/TUSafariActivity/TUSafariActivity.m index 570a6289a..95a698717 100755 --- a/clients/ios/Other Sources/TUSafariActivity/TUSafariActivity.m +++ b/clients/ios/Other Sources/TUSafariActivity/TUSafariActivity.m @@ -72,9 +72,9 @@ - (void)performActivity { - BOOL completed = [[UIApplication sharedApplication] openURL:_URL]; - - [self activityDidFinish:completed]; + [[UIApplication sharedApplication] openURL:_URL options:@{} completionHandler:^(BOOL success) { + [self activityDidFinish:success]; + }]; } @end diff --git a/clients/ios/Resources-iPad/MainWindow~ipad.xib b/clients/ios/Resources-iPad/MainWindow~ipad.xib index 141e7353d..687e5663d 100644 --- a/clients/ios/Resources-iPad/MainWindow~ipad.xib +++ b/clients/ios/Resources-iPad/MainWindow~ipad.xib @@ -1,8 +1,12 @@ - - + + + + + - + + @@ -29,6 +33,7 @@ + @@ -41,8 +46,7 @@ - - + @@ -192,8 +196,8 @@ + - @@ -202,5 +206,16 @@ + + + + + + + + + + + diff --git a/clients/ios/cocoa-oauth/GCOAuth.m b/clients/ios/cocoa-oauth/GCOAuth.m index 7e0ee96b3..d3ee56423 100755 --- a/clients/ios/cocoa-oauth/GCOAuth.m +++ b/clients/ios/cocoa-oauth/GCOAuth.m @@ -164,7 +164,7 @@ static BOOL GCOAuthUseHTTPSCookieStorage = YES; NSURL *URL = self.URL; // Use CFURLCopyPath so that the path is preserved with trailing slash, then escape the percents ourselves - NSString *pathWithPrevervedTrailingSlash = [CFBridgingRelease(CFURLCopyPath((CFURLRef)URL)) stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *pathWithPrevervedTrailingSlash = [CFBridgingRelease(CFURLCopyPath((CFURLRef)URL)) stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSString *URLString = [NSString stringWithFormat:@"%@://%@%@", [[URL scheme] lowercaseString], @@ -244,7 +244,7 @@ static BOOL GCOAuthUseHTTPSCookieStorage = YES; oauth.HTTPMethod = HTTPMethod; oauth.requestParameters = parameters; - NSString *encodedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *encodedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; NSString *URLString = [NSString stringWithFormat:@"%@://%@%@", scheme, host, encodedPath]; if ([[HTTPMethod uppercaseString] isEqualToString:@"GET"]) { // Handle GET @@ -393,11 +393,6 @@ static BOOL GCOAuthUseHTTPSCookieStorage = YES; @implementation NSString (GCOAuthAdditions) - (NSString *)pcen { - CFStringRef string = CFURLCreateStringByAddingPercentEscapes(NULL, - (CFStringRef)self, - NULL, - CFSTR("!*'();:@&=+$,/?%#[]"), - kCFStringEncodingUTF8); - return [(NSString *)string autorelease]; + return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; } @end diff --git a/config/requirements.txt b/config/requirements.txt index eed21f1cb..e11def2f4 100755 --- a/config/requirements.txt +++ b/config/requirements.txt @@ -45,7 +45,7 @@ python-dateutil==2.6.0 python-digitalocean==1.10.1 python-gflags==3.1.0 pytz==2016.7 -raven==5.32.0 +raven==6.0.0 redis==2.10.5 requests==2.12.3 scipy==0.18.1 diff --git a/fabfile.py b/fabfile.py index 7747092d0..582118d4c 100644 --- a/fabfile.py +++ b/fabfile.py @@ -397,11 +397,19 @@ def setup_user(): run('echo `cat authorized_keys` >> ~sclay/.ssh/authorized_keys') run('rm authorized_keys') -def copy_ssh_keys(): - put(os.path.join(env.SECRETS_PATH, 'keys/newsblur.key.pub'), "local_keys") - run("echo \"\n\" >> ~sclay/.ssh/authorized_keys") - run("echo `cat local_keys` >> ~sclay/.ssh/authorized_keys") - run("rm local_keys") +def copy_ssh_keys(username='sclay', private=False): + sudo('mkdir -p ~%s/.ssh' % username) + + put(os.path.join(env.SECRETS_PATH, 'keys/newsblur.key.pub'), 'local.key.pub') + sudo('mv local.key.pub ~%s/.ssh/id_rsa.pub' % username) + if private: + put(os.path.join(env.SECRETS_PATH, 'keys/newsblur.key'), 'local.key') + sudo('mv local.key ~%s/.ssh/id_rsa' % username) + + sudo("echo \"\n\" >> ~%s/.ssh/authorized_keys" % username) + sudo("echo `cat ~%s/.ssh/id_rsa.pub` >> ~%s/.ssh/authorized_keys" % (username, username)) + sudo('chown -R %s.%s ~%s/.ssh' % (username, username, username)) + sudo('chmod 600 ~%s/.ssh/id_rsa*' % username) def setup_repo(): sudo('mkdir -p /srv') @@ -567,7 +575,7 @@ def config_pgbouncer(): sudo('/etc/init.d/pgbouncer start', pty=False) @parallel -def kill_pgbouncer(bounce=True): +def kill_pgbouncer(stop=False): # sudo('su postgres -c "/etc/init.d/pgbouncer stop"', pty=False) with settings(warn_only=True): sudo('/etc/init.d/pgbouncer stop') @@ -576,7 +584,7 @@ def kill_pgbouncer(bounce=True): with settings(warn_only=True): sudo('pkill -9 pgbouncer') run('sleep 2') - if bounce: + if not stop: run('sudo /etc/init.d/pgbouncer start', pty=False) def config_monit_task(): @@ -1005,17 +1013,14 @@ def copy_postgres_to_standby(master='db01'): # Make sure you can ssh from master to slave and back with the postgres user account. # Need to give postgres accounts keys in authroized_keys. - - # new: sudo su postgres - # new: ssh-keygen - # Copy old:/var/lib/postgresql/.ssh/id_dsa.pub to new:/var/lib/postgresql/.ssh/authorized_keys and vice-versa - # old: cat /var/lib/postgresql/.ssh/id_rsa.pub - # new: echo "" > /var/lib/postgresql/.ssh/authorized_keys + + # local: fab host:old copy_ssh_keys:postgres,private=True # new: ssh old # old: sudo su postgres -c "psql -c \"SELECT pg_start_backup('label', true)\"" - sudo('mkdir /var/lib/postgresql/9.4/archive') + sudo('mkdir -p /var/lib/postgresql/9.4/archive') sudo('chown postgres.postgres /var/lib/postgresql/9.4/archive') - sudo('su postgres -c "rsync -a --stats --progress postgres@db_pgsql:/var/lib/postgresql/9.4/main /var/lib/postgresql/9.4/ --exclude postmaster.pid"') + with settings(warn_only=True): + sudo('su postgres -c "rsync -Pav -e \'ssh -i ~postgres/.ssh/newsblur.key\' --stats --progress postgres@%s:/var/lib/postgresql/9.4/main /var/lib/postgresql/9.4/ --exclude postmaster.pid"' % master) put('config/postgresql_recovery.conf', '/var/lib/postgresql/9.4/main/recovery.conf', use_sudo=True) sudo('systemctl start postgresql') # old: sudo su postgres -c "psql -c \"SELECT pg_stop_backup()\"" diff --git a/media/css/reader.css b/media/css/reader.css index 13fec4725..559b78985 100644 --- a/media/css/reader.css +++ b/media/css/reader.css @@ -4382,7 +4382,9 @@ body { border-top: 1px solid #B7BBAA; } .NB-feeds-header-read.NB-feeds-header, -.NB-feeds-header-starred.NB-feeds-header { +.NB-feeds-header-searches.NB-feeds-header, +.NB-feeds-header-starred.NB-feeds-header, +.NB-searches-folder { border-bottom: 1px solid #B7BBAA; } .NB-feeds-header-river-container .NB-feeds-header.NB-empty .NB-feeds-header-count { @@ -4436,6 +4438,48 @@ body { display: none; } + +/* =========================== */ +/* = Header - Saved Searches = */ +/* =========================== */ + +.NB-feeds-header-searches { + cursor: default; + background-image: inherit; + background-color: #EAECE5; +} +.NB-feeds-header.NB-feeds-header-searches:hover { + background-image: inherit; + background-color: #EAECE5; +} +.NB-feeds-header-searches .NB-feeds-header-icon { + background: transparent url('/media/embed/icons/circular/g_icn_search_black.png') no-repeat 1px 1px; + background-size: 14px; +} + +.NB-feeds-header-searches .NB-feeds-header-count { + background-color: #506B9A; + margin-top: 4px; + margin-right: 3px; + display: block; +} + +.NB-searches-feeds .feed { + border-top: 1px solid #E9EBEE; + border-bottom: 1px solid #E9EBEE; + background-color: #E9EBEE; +} +.NB-searches-feeds .feed_title { + padding-right: 4px; +} +.NB-feeds-header-searches.NB-empty .NB-feeds-header-count { + display: none; +} + +.NB-searches-folder .NB-searches-feeds.NB-feedlist .feed { + display: block; +} + /* ===================== */ /* = Header - Try Feed = */ /* ===================== */ @@ -7144,7 +7188,8 @@ form.opml_import_form input { background-size: 18px; } -.NB-menu-manage .NB-menu-manage-delete .NB-menu-manage-image { +.NB-menu-manage .NB-menu-manage-delete .NB-menu-manage-image, +.NB-menu-manage .NB-menu-manage-delete-search .NB-menu-manage-image { background: transparent url('/media/embed/icons/circular/menu_icn_delete.png') no-repeat 0 0; background-size: 18px; } @@ -11901,7 +11946,7 @@ form.opml_import_form input { font-weight: bold; background: transparent; width: 82%; - padding: 2px 18px 1px 18px; + padding: 2px 52px 1px 18px; margin: 0; display: block; font-size: 11px; @@ -11943,10 +11988,12 @@ form.opml_import_form input { -ms-transition: all .22s ease-in-out; } + .NB-search-close:hover { opacity: 1; } -.NB-searching .NB-search-close { +.NB-searching .NB-search-close, +.NB-searching .NB-search-save { display: block; } .NB-search-container { @@ -11967,7 +12014,56 @@ form.opml_import_form input { margin: 0; } .NB-searching .NB-story-title-search-input { - width: 174px; + width: 154px; +} + +.NB-search-header { + overflow: hidden; + padding: 8px 8px 8px 36px; + background-image: -webkit-gradient(linear, left top, left bottom, from(#FEFEFE), to(#F3F4EF)); + background-image: -moz-linear-gradient(center top , #FEFEFE 0%, #F3F4EF 100%); + background-image: linear-gradient(top, #FEFEFE, #F3F4EF); + border-bottom: 1px solid #C2C5BE; + background-color: #F3F3EE; + background-image: -webkit-gradient(linear, left top, left bottom, from(#F3F3EE), to(#E8EBE4)); + background-image: -moz-linear-gradient(center top , #F3F3EE 0%, #E8EBE4 100%); + background-image: linear-gradient(top, #F3F3EE, #E8EBE4); +} + +.NB-search-header .NB-search-header-save { + float: right; + font-size: 9px; + text-transform: uppercase; + cursor: pointer; + padding: 1px 8px; + font-weight: bold; + line-height: 14px; + border: 1px solid rgba(0, 0, 0, .1); + border-radius: 16px; + z-index: 1; + margin: 0 0 0 8px; + color: rgba(0, 0, 0, .5); + + background-color: #F0F1EC; +} + +.NB-search-header .NB-search-header-save:hover, +.NB-search-header .NB-search-header-save.NB-active { + background-color: rgba(0, 0, 0, .1); +} + +.NB-search-header .NB-search-header-icon { + background: transparent url('/media/embed/reader/search_icon2.png') no-repeat 0 0; + background-size: 16px; + vertical-align: bottom; + width: 16px; + height: 16px; + position: absolute; + top: 8px; + left: 12px; +} +.NB-search-header .NB-search-header-title { + font-size: 12px; } /* ================== */ diff --git a/media/js/newsblur/common/assetmodel.js b/media/js/newsblur/common/assetmodel.js index e53c8aaa4..584726145 100644 --- a/media/js/newsblur/common/assetmodel.js +++ b/media/js/newsblur/common/assetmodel.js @@ -16,6 +16,7 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ this.stories = new NEWSBLUR.Collections.Stories(); this.dashboard_stories = new NEWSBLUR.Collections.Stories(); this.starred_feeds = new NEWSBLUR.Collections.StarredFeeds(); + this.searches_feeds = new NEWSBLUR.Collections.SearchesFeeds(); this.queued_read_stories = {}; this.classifiers = {}; this.friends = {}; @@ -291,6 +292,7 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ : [feed_id]; this.stories.each(function(story) { + if (!_.contains(feed_ids, story.get('story_feed_id'))) return; if (direction == "older" && cutoff_timestamp && story.get('story_timestamp') > cutoff_timestamp) { @@ -458,6 +460,7 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ self.starred_feeds.reset(subscriptions.starred_counts, {parse: true}); self.social_feeds.reset(subscriptions.social_feeds, {parse: true}); self.user_profile.set(subscriptions.social_profile); + self.searches_feeds.reset(subscriptions.saved_searches, {parse: true}); self.social_services = subscriptions.social_services; if (selected && self.feeds.get(selected)) { @@ -1053,6 +1056,12 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ return this.social_feeds.get(feed_id); } else if (_.string.startsWith(feed_id, 'starred:')) { return this.starred_feeds.get(feed_id); + } else if (_.string.startsWith(feed_id, 'search:')) { + return this.searches_feeds.get(feed_id); + } else if (_.string.startsWith(feed_id, 'river:')) { + return this.get_folder(feed_id); + } else if (_.string.startsWith(feed_id, 'feed:')) { + return this.feeds.get(parseInt(feed_id.replace('feed:', ''), 10)); } else { return this.feeds.get(feed_id); } @@ -1085,6 +1094,17 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ return this.starred_feeds; }, + get_search_feeds: function(feed_id, query) { + var self = this; + + return this.searches_feeds.detect(function(feed) { + if (!query) { + return feed.id == feed_id; + } + return feed.get('query') == query && feed.get('feed_id') == feed_id; + }); + }, + get_folders: function() { var self = this; @@ -1092,6 +1112,9 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ }, get_folder: function(folder_name) { + if (_.string.startsWith(folder_name, 'river:')) { + folder_name = folder_name.replace('river:', ''); + } return this.folders.find_folder(folder_name.toLowerCase()); }, @@ -1476,6 +1499,46 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ this.make_request('/reader/save_feed_order', {'folders': $.toJSON(folders)}, callback); }, + save_search: function(feed_id, query, callback) { + var self = this; + var pre_callback = function(data) { + if (data.saved_searches) { + self.searches_feeds.reset(data.saved_searches, {parse: true}); + } + + if (callback) callback(data); + }; + + if (NEWSBLUR.Globals.is_authenticated) { + this.make_request('/reader/save_search', { + 'feed_id': feed_id, + 'query': query + }, pre_callback); + } else { + if ($.isFunction(callback)) callback(); + } + }, + + delete_saved_search: function(feed_id, query, callback) { + var self = this; + var pre_callback = function(data) { + if (data.saved_searches) { + self.searches_feeds.reset(data.saved_searches, {parse: true}); + } + + if (callback) callback(data); + }; + + if (NEWSBLUR.Globals.is_authenticated) { + this.make_request('/reader/delete_search', { + 'feed_id': feed_id, + 'query': query + }, pre_callback); + } else { + if ($.isFunction(callback)) callback(); + } + }, + get_feed_statistics: function(feed_id, callback) { this.make_request('/rss_feeds/statistics/'+feed_id, {}, callback, callback, { 'ajax_group': 'statistics', @@ -1819,11 +1882,10 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ this.make_request('/oauth/unfollow_twitter_account', {'username': username}, callback); }, - fetch_original_text: function(story_id, feed_id, callback, error_callback) { - var story = this.get_story(story_id); + fetch_original_text: function(story_hash, callback, error_callback) { + var story = this.get_story(story_hash); this.make_request('/rss_feeds/original_text', { - story_id: story_id, - feed_id: feed_id + story_hash: story_hash }, function(data) { story.set('original_text', data.original_text); callback(data); diff --git a/media/js/newsblur/models/feeds.js b/media/js/newsblur/models/feeds.js index 982e1bb67..41680a06a 100644 --- a/media/js/newsblur/models/feeds.js +++ b/media/js/newsblur/models/feeds.js @@ -148,6 +148,10 @@ NEWSBLUR.Models.Feed = Backbone.Model.extend({ return false; }, + is_search: function() { + return false; + }, + is_light: function() { var is_light = this._is_light; if (!_.isUndefined(is_light)) { diff --git a/media/js/newsblur/models/folders.js b/media/js/newsblur/models/folders.js index b077e035d..75ed97534 100644 --- a/media/js/newsblur/models/folders.js +++ b/media/js/newsblur/models/folders.js @@ -10,6 +10,7 @@ NEWSBLUR.Models.FeedOrFolder = Backbone.Model.extend({ } } else if (model && model.fake) { this.folders = model.folders; + this.set('folder_title', this.fake_folder_title()); } else if (model) { var title = _.keys(model)[0]; var children = model[title]; diff --git a/media/js/newsblur/models/saved_searches.js b/media/js/newsblur/models/saved_searches.js new file mode 100644 index 000000000..cda33ed48 --- /dev/null +++ b/media/js/newsblur/models/saved_searches.js @@ -0,0 +1,158 @@ +NEWSBLUR.Models.SavedSearchFeed = Backbone.Model.extend({ + + initialize: function() { + var feed_title = this.feed_title(); + var favicon_url = this.favicon_url(); + this.set('feed_title', "\"" + this.get('query') + "\" in " + feed_title + ""); + this.set('favicon_url', favicon_url); + this.list_view; + }, + + feed_title: function() { + var feed_title; + var feed_id = this.get('feed_id'); + + if (feed_id == 'river:') { + feed_title = "All Site Stories"; + } else if (_.string.startsWith(feed_id, 'river:')) { + feed_title = NEWSBLUR.assets.get_feed(feed_id).get('folder_title'); + } else if (feed_id == "read") { + feed_title = "Read Stories"; + } else if (_.string.startsWith(feed_id, 'starred')) { + feed_title = "Saved Stories"; + var tag = feed_id.replace('starred:', ''); + var model = NEWSBLUR.assets.starred_feeds.detect(function(feed) { + return feed.tag_slug() == tag || feed.get('tag') == tag; + }); + if (model) { + feed_title = feed_title + " - " + model.get('tag'); + } + } else if (_.string.startsWith(feed_id, 'feed:')){ + feed_title = NEWSBLUR.assets.get_feed(parseInt(this.get('feed_id').replace('feed:', ''), 10)).get('feed_title'); + } else if (_.string.startsWith(feed_id, 'social:')){ + feed_title = NEWSBLUR.assets.get_feed(this.get('feed_id')).get('feed_title'); + } + + return feed_title; + + if (_.string.startsWith(this.get('feed_id'), 'saved:') || + _.string.startsWith(this.get('feed_id'), 'read')) { + feed_title = NEWSBLUR.reader.active_fake_folder_title(); + } else if (NEWSBLUR.reader.active_folder) { + feed_title = NEWSBLUR.reader.active_folder.get('folder_title'); + } else if (NEWSBLUR.reader.active_feed) { + + } + }, + + favicon_url: function() { + var url; + var feed_id = this.get('feed_id'); + + if (feed_id == 'river:') { + url = NEWSBLUR.Globals.MEDIA_URL + 'img/icons/circular/ak-icon-allstories.png'; + } else if (_.string.startsWith(feed_id, 'river:')) { + url = NEWSBLUR.Globals.MEDIA_URL + 'img/icons/circular/g_icn_folder.png'; + } else if (feed_id == "read") { + url = NEWSBLUR.Globals.MEDIA_URL + 'img/icons/circular/g_icn_unread.png'; + } else if (feed_id == "starred") { + url = NEWSBLUR.Globals.MEDIA_URL + 'img/icons/circular/clock.png'; + } else if (_.string.startsWith(feed_id, 'starred:')) { + url = NEWSBLUR.Globals.MEDIA_URL + 'img/reader/tag.png'; + } else if (_.string.startsWith(feed_id, 'feed:')) { + url = $.favicon(parseInt(feed_id.replace('feed:', ''), 10)); + } else if (_.string.startsWith(feed_id, 'social:')) { + url = $.favicon(NEWSBLUR.assets.get_feed(feed_id)); + } + + if (!url) { + url = NEWSBLUR.Globals.MEDIA_URL + 'img/icons/circular/g_icn_search_black.png'; + } + + return url; + }, + + is_social: function() { + return false; + }, + + is_feed: function() { + return false; + }, + + is_starred: function() { + return false; + }, + + is_search: function() { + return true; + }, + + unread_counts: function() { + return { + ps: this.get('count') || 0, + nt: 0, + ng: 0 + }; + }, + + tag_slug: function() { + return Inflector.sluggify(this.get('tag') || ''); + } + +}); + +NEWSBLUR.Collections.SearchesFeeds = Backbone.Collection.extend({ + + model: NEWSBLUR.Models.SavedSearchFeed, + + parse: function(models) { + _.each(models, function(feed) { + feed.id = 'search:' + feed.feed_id + ":" + feed.query; + }); + return models; + }, + + comparator: function(a, b) { + var sort_order = NEWSBLUR.reader.model.preference('feed_order'); + var title_a = a.get('query') || ''; + var title_b = b.get('query') || ''; + title_a = title_a.toLowerCase(); + title_b = title_b.toLowerCase(); + + if (sort_order == 'MOSTUSED') { + var opens_a = a.get('count'); + var opens_b = b.get('count'); + if (opens_a > opens_b) return -1; + if (opens_a < opens_b) return 1; + } + + // if (!sort_order || sort_order == 'ALPHABETICAL') + if (title_a > title_b) return 1; + else if (title_a < title_b) return -1; + return 0; + }, + + selected: function() { + return this.detect(function(feed) { return feed.get('selected'); }); + }, + + deselect: function() { + this.chain().select(function(feed) { + return feed.get('selected'); + }).each(function(feed){ + feed.set('selected', false); + }); + }, + + all_searches: function() { + return this.pluck('saved_search'); + }, + + get_feed: function(feed_id) { + return this.detect(function(feed) { + return feed.get('feed_id') == feed_id; + }); + } + +}); \ No newline at end of file diff --git a/media/js/newsblur/models/social_subscription.js b/media/js/newsblur/models/social_subscription.js index 5826a3e6c..9b811114f 100644 --- a/media/js/newsblur/models/social_subscription.js +++ b/media/js/newsblur/models/social_subscription.js @@ -41,6 +41,10 @@ NEWSBLUR.Models.SocialSubscription = Backbone.Model.extend({ return false; }, + is_search: function() { + return false; + }, + unread_counts: function() { return { ps: this.get('ps') || 0, diff --git a/media/js/newsblur/models/starred_counts.js b/media/js/newsblur/models/starred_counts.js index 2387d0c8b..59e01bb41 100644 --- a/media/js/newsblur/models/starred_counts.js +++ b/media/js/newsblur/models/starred_counts.js @@ -17,6 +17,10 @@ NEWSBLUR.Models.StarredFeed = Backbone.Model.extend({ return true; }, + is_search: function() { + return false; + }, + unread_counts: function() { return { ps: this.get('count') || 0, diff --git a/media/js/newsblur/models/stories.js b/media/js/newsblur/models/stories.js index 73b27009e..a0eddc005 100644 --- a/media/js/newsblur/models/stories.js +++ b/media/js/newsblur/models/stories.js @@ -353,7 +353,7 @@ NEWSBLUR.Collections.Stories = Backbone.Collection.extend({ if (!active_feed) { // River of News does not have an active feed. active_feed = story_feed; - } else if (active_feed && active_feed.is_social()) { + } else if (active_feed && active_feed.is_feed() && active_feed.is_social()) { friend_feeds = _.without(friend_feeds, active_feed); } diff --git a/media/js/newsblur/reader/reader.js b/media/js/newsblur/reader/reader.js index 9b5cc934e..e48c7a628 100644 --- a/media/js/newsblur/reader/reader.js +++ b/media/js/newsblur/reader/reader.js @@ -24,6 +24,7 @@ $feed_list: $('#feed_list'), $social_feeds: $('.NB-socialfeeds-folder'), $starred_feeds: $('.NB-starred-folder'), + $searches_feeds: $('.NB-searches-folder'), $story_titles: $('#story_titles'), $story_titles_header: $('.NB-story-titles-header'), $content_pane: $('.content-pane'), @@ -45,13 +46,15 @@ $river_blurblogs_header: $('.NB-feeds-header-river-blurblogs'), $river_global_header: $('.NB-feeds-header-river-global'), $starred_header: $('.NB-feeds-header-starred'), + $searches_header: $('.NB-feeds-header-searches'), $read_header: $('.NB-feeds-header-read'), $tryfeed_header: $('.NB-feeds-header-tryfeed'), $taskbar: $('.NB-taskbar-view'), $feed_floater: $('.NB-feed-story-view-floater'), $feedbar: $('.NB-feedbar'), $add_button: $('.NB-task-add'), - $taskbar_options: $('.NB-taskbar-options') + $taskbar_options: $('.NB-taskbar-options'), + $search_header: $('.NB-search-header') }; this.flags = { 'bouncing_callout': false, @@ -135,6 +138,7 @@ NEWSBLUR.app.dashboard_river = new NEWSBLUR.Views.DashboardRiver(); NEWSBLUR.app.taskbar_info = new NEWSBLUR.Views.ReaderTaskbarInfo().render(); NEWSBLUR.app.story_titles_header = new NEWSBLUR.Views.StoryTitlesHeader(); + NEWSBLUR.app.search_header = new NEWSBLUR.Views.FeedSearchHeader(); this.load_intelligence_slider(); this.handle_mouse_indicator_hover(); @@ -1290,6 +1294,8 @@ 'select_story_in_feed': 0 }); + // console.log(['reset feed', options, options.search]); + if (_.isUndefined(options.search)) { this.flags.search = ""; this.flags.searching = false; @@ -1323,6 +1329,7 @@ this.model.feeds.deselect(); this.model.stories.deselect(); this.model.starred_feeds.deselect(); + this.model.searches_feeds.deselect(); if (_.string.contains(this.active_feed, 'social:')) { this.model.social_feeds.deselect(); } @@ -1410,7 +1417,11 @@ feed.set("selected_title_view", selected_title_view, {silent: true}); } } - feed.set('selected', true, options); + if (options.feed && options.feed.set) { + options.feed.set('selected', true, options); + } else { + feed.set('selected', true, options); + } if (NEWSBLUR.app.story_unread_counter) { NEWSBLUR.app.story_unread_counter.remove(); } @@ -1706,6 +1717,45 @@ }); }, + // ================== + // = Saved Searches = + // ================== + + open_saved_search: function(options) { + var search_model = options.search_model; + var feed_id = search_model.get('feed_id'); + var query = search_model.get('query'); + var feed_model = NEWSBLUR.assets.get_feed(feed_id); + NEWSBLUR.reader.flags.searching = true; + NEWSBLUR.reader.flags.search = query; + options['search'] = query; + options['router'] = true; + search_model.set('selected', true); + options.feed = options.search_model; + + if (feed_id == 'river:') { + this.open_river_stories(options.$feed, feed_model, options); + } else if (_.string.startsWith(feed_id, 'river:')) { + this.open_river_stories(options.$feed, feed_model, options); + } else if (feed_id == "read") { + this.open_read_stories(options); + } else if (feed_id == 'starred') { + this.open_starred_stories(options); + } else if (_.string.startsWith(feed_id, 'starred:')) { + options.tag = feed_model.tag_slug(); + this.open_starred_stories(options); + } else if (_.string.startsWith(feed_id, 'feed:')) { + this.open_feed(parseInt(feed_id.replace('feed:', ''), 10), options); + } else if (_.string.startsWith(feed_id, 'social:')) { + this.open_social_stories(feed_id, options); + } + + window.history.replaceState({}, "", $.updateQueryString('search', query, window.location.pathname)); + + NEWSBLUR.reader.reload_feed(options); + NEWSBLUR.app.story_titles_header.show_hidden_story_titles(); + }, + // =================== // = Starred Stories = // =================== @@ -1755,10 +1805,18 @@ if (options.tag) { this.active_feed = options.model.id; this.flags['starred_tag'] = options.model.get('tag'); - options.model.set('selected', true); + if (options.feed) { + options.feed.set('selected', true); + } else { + options.model.set('selected', true); + } } else { this.active_feed = 'starred'; - this.$s.$starred_header.addClass('NB-selected'); + if (options.feed) { + options.feed.set('selected', true); + } else { + this.$s.$starred_header.addClass('NB-selected'); + } this.flags['starred_tag'] = null; } this.flags['starred_view'] = true; @@ -1890,8 +1948,6 @@ options = options || {}; var $story_titles = this.$s.$story_titles; $folder = $folder || this.$s.$feed_list; - var folder_view = NEWSBLUR.assets.folders.get_view($folder) || - this.active_folder && this.active_folder.folder_view; var folder_title = folder && folder.get('folder_title') || "Everything"; this.reset_feed(options); @@ -1903,10 +1959,20 @@ this.hide_splash_page(); if (!folder || folder.get('fake') || !folder.get('folder_title')) { this.active_feed = 'river:'; - this.$s.$river_sites_header.addClass('NB-selected'); + if (options.feed) { + options.feed.set('selected', true); + } else { + this.$s.$river_sites_header.addClass('NB-selected'); + } } else { this.active_feed = 'river:' + folder_title; - folder_view.model.set('selected', true); + if (options.feed) { + options.feed.set('selected', true); + } else { + var folder_view = NEWSBLUR.assets.folders.get_view($folder) || + this.active_folder && this.active_folder.folder_view; + folder_view.model.set('selected', true); + } } this.active_folder = folder || NEWSBLUR.assets.folders; @@ -2146,7 +2212,11 @@ this.iframe_scroll = null; this.flags['opening_feed'] = true; - feed.set('selected', true, options); + if (options.feed) { + options.feed.set('selected', true, options); + } else { + feed.set('selected', true, options); + } this.set_correct_story_view_for_feed(this.active_feed); this.make_feed_title_in_stories(); this.$s.$body.addClass('NB-view-river'); @@ -2723,6 +2793,8 @@ }, make_feed_title_in_stories: function(options) { + NEWSBLUR.app.search_header.render(); + if ((this.flags.search || this.flags.searching) && NEWSBLUR.app.story_titles_header.search_has_focus()) { console.log(["make_feed_title_in_stories not destroying", this.flags.search]); @@ -2739,6 +2811,26 @@ }); }, + active_fake_folder_title: function() { + var title = "All Site Stories"; + if (NEWSBLUR.reader.active_feed == "read") { + title = "Read Stories"; + } else if (NEWSBLUR.reader.flags['starred_view']) { + title = "Saved Stories"; + if (NEWSBLUR.reader.flags['starred_tag']) { + title = title + " - " + NEWSBLUR.reader.flags['starred_tag']; + } + } else if (NEWSBLUR.reader.flags['social_view']) { + if (NEWSBLUR.reader.flags['global_blurblogs']) { + title = "Global Shared Stories"; + } else { + title = "All Shared Stories"; + } + } + + return title; + }, + open_feed_intelligence_modal: function(score, feed_id, feed_loaded) { feed_id = feed_id || this.active_feed; @@ -3441,6 +3533,16 @@ ]); $manage_menu.data('feed_id', feed_id); $manage_menu.data('$feed', $item); + } else if (type == 'search') { + $manage_menu = $.make('ul', { className: 'NB-menu-manage NB-menu-manage-feed' }, [ + $.make('li', { className: 'NB-menu-separator-inverse' }), + $.make('li', { className: 'NB-menu-item NB-menu-manage-delete-search' }, [ + $.make('div', { className: 'NB-menu-manage-image' }), + $.make('div', { className: 'NB-menu-manage-title' }, 'Delete saved search') + ]) + ]); + $manage_menu.data('feed_id', feed_id); + $manage_menu.data('$feed', $item); } else if (type == 'folder') { $manage_menu = $.make('ul', { className: 'NB-menu-manage NB-menu-manage-folder' }, [ $.make('li', { className: 'NB-menu-separator-inverse' }), @@ -3722,6 +3824,9 @@ } else if (type == 'starred') { feed_id = options.feed_id; inverse = options.inverse || $item.hasClass("NB-hover-inverse"); + } else if (type == 'search') { + feed_id = options.feed_id; + inverse = options.inverse || $item.hasClass("NB-hover-inverse"); } else if (type == 'story') { story_id = options.story_id; if ($item.hasClass('NB-hover-inverse')) inverse = true; @@ -3769,7 +3874,7 @@ } $('.NB-task-manage').addClass('NB-hover'); } else if (type == 'feed' || type == 'folder' || type == 'story' || - type == 'socialfeed' || type == 'starred') { + type == 'socialfeed' || type == 'starred' || type == 'search') { var left, top; // NEWSBLUR.log(['menu open', $item, inverse, toplevel, type]); if (inverse) { @@ -3777,8 +3882,8 @@ if (type == 'feed') { left = toplevel ? 2 : -22; top = toplevel ? 1 : 3; - } else if (type == 'socialfeed' || type == 'starred') { - left = 2; + } else if (type == 'socialfeed' || type == 'starred' || type == 'search') { + left = 0; top = 2; } else if (type == 'folder') { left = toplevel ? 0 : -21; @@ -3807,7 +3912,7 @@ left = toplevel ? 0 : -2; top = toplevel ? 20 : 19; $align = $('.NB-feedlist-manage-icon', $item); - } else if (type == 'socialfeed' || type == 'starred') { + } else if (type == 'socialfeed' || type == 'starred' || type == 'search') { left = toplevel ? 0 : -18; top = toplevel ? 20 : 21; $align = $('.NB-feedlist-manage-icon', $item); @@ -3833,7 +3938,7 @@ // Create and position the arrow tab if (type == 'feed' || type == 'folder' || type == 'story' || - type == 'socialfeed' || type == 'starred') { + type == 'socialfeed' || type == 'starred' || type == 'search') { var $arrow = $.make('div', { className: 'NB-menu-manage-arrow' }, [ $.make('div', { className: 'NB-icon' }) ]); @@ -3900,7 +4005,7 @@ // Hide menu on scroll. var $scroll; this.flags['feed_list_showing_manage_menu'] = true; - if (type == 'feed' || type == 'socialfeed' || type == 'starred') { + if (type == 'feed' || type == 'socialfeed' || type == 'starred' || type == 'search') { $scroll = this.$s.$feed_list.parent(); } else if (type == 'story') { $scroll = this.$s.$story_titles.add(this.$s.$feed_scroll); @@ -3984,6 +4089,14 @@ var feed_view = feed.get_view($feed); feed.delete_feed({view: feed_view}); }, + + manage_menu_delete_search: function(search_model_id, $feed) { + var search_model = NEWSBLUR.assets.get_search_feeds(search_model_id); + NEWSBLUR.assets.delete_saved_search(search_model.get('feed_id'), search_model.get('query'), _.bind(function(e) { + console.log(['Saved searches', e]); + }, this)); + + }, show_confirm_unfollow_menu_item: function() { var $unfollow = $('.NB-menu-manage-socialfeed-delete'); @@ -4640,7 +4753,7 @@ if (_.contains(['starred', 'read'], feed_id)) { // Umm, no. Not yet. - } else if (feed) { + } else if (feed && feed.unread_counts) { return feed.unread_counts(); } else if (this.flags['river_view'] && !this.flags['social_view']) { var collection; @@ -5865,6 +5978,12 @@ var $feed = $t.parents('.NB-menu-manage').data('$feed'); self.manage_menu_delete_feed(feed_id, $feed); }); + $.targetIs(e, { tagSelector: '.NB-menu-manage-delete-search' }, function($t, $p){ + e.preventDefault(); + var search_model_id = $t.parents('.NB-menu-manage').data('feed_id'); + var $feed = $t.parents('.NB-menu-manage').data('$feed'); + self.manage_menu_delete_search(search_model_id, $feed); + }); $.targetIs(e, { tagSelector: '.NB-menu-manage-socialfeed-delete-confirm' }, function($t, $p){ e.preventDefault(); var feed_id = $t.parents('.NB-menu-manage').data('feed_id'); diff --git a/media/js/newsblur/reader/reader_admin.js b/media/js/newsblur/reader/reader_admin.js index cf69ebbac..132eedd12 100644 --- a/media/js/newsblur/reader/reader_admin.js +++ b/media/js/newsblur/reader/reader_admin.js @@ -102,6 +102,8 @@ _.extend(NEWSBLUR.ReaderUserAdmin.prototype, { $.make('dd', data.statistics.email), $.make('dt', 'Stripe Id:'), $.make('dd', $.make('a', { href: "https://manage.stripe.com/customers/" + data.statistics.stripe_id, className: 'NB-splash-link' }, data.statistics.stripe_id)), + $.make('dt', 'Paypal email:'), + $.make('dd', $.make('a', { href: "https://www.paypal.com/businessexp/transactions?nav=0.3.1&payer_email=" + data.statistics.paypal_email, className: 'NB-splash-link' }, data.statistics.paypal_email)), $.make('dt', 'Feeds:'), $.make('dd', Inflector.commas(data.statistics.feeds)), $.make('dt', 'Feed opens:'), diff --git a/media/js/newsblur/views/feed_list_view.js b/media/js/newsblur/views/feed_list_view.js index 092800471..b073eca00 100644 --- a/media/js/newsblur/views/feed_list_view.js +++ b/media/js/newsblur/views/feed_list_view.js @@ -40,9 +40,13 @@ NEWSBLUR.Views.FeedList = Backbone.View.extend({ NEWSBLUR.assets.starred_feeds.bind('reset', _.bind(function(models, options) { this.make_starred_tags(options); }, this)); + NEWSBLUR.assets.searches_feeds.bind('reset', _.bind(function(models, options) { + this.make_saved_searches(options); + }, this)); NEWSBLUR.assets.social_feeds.bind('change:selected', this.scroll_to_selected, this); NEWSBLUR.assets.feeds.bind('change:selected', this.scroll_to_selected, this); NEWSBLUR.assets.starred_feeds.bind('change:selected', this.scroll_to_selected, this); + NEWSBLUR.assets.searches_feeds.bind('change:selected', this.scroll_to_selected, this); if (!NEWSBLUR.assets.folders.size()) { NEWSBLUR.assets.load_feeds(); } @@ -201,6 +205,36 @@ NEWSBLUR.Views.FeedList = Backbone.View.extend({ $starred_feeds.animate({'opacity': 1}, {'duration': (collapsed || options.update) ? 0 : 700}); }, + make_saved_searches: function(options) { + options = options || {}; + var $searches_feeds = $('.NB-searches-feeds', this.$s.$searches_feeds); + var $feeds = _.compact(NEWSBLUR.assets.searches_feeds.map(function(feed) { + var feed_view = new NEWSBLUR.Views.FeedTitleView({ + model: feed, + type: 'feed', + depth: 0, + saved_search: true + }).render(); + feed.list_view = feed_view; + return feed_view.el; + })); + + $searches_feeds.empty().css({ + 'display': 'block', + 'opacity': options.update ? 1 : 0 + }); + $searches_feeds.html($feeds); + if (NEWSBLUR.assets.searches_feeds.length) { + $('.NB-feeds-header-searches-container').css({ + 'display': 'block', + 'opacity': 0 + }).animate({'opacity': 1}, {'duration': options.update ? 0 : 700}); + } + + var collapsed = NEWSBLUR.app.sidebar.check_searches_collapsed({skip_animation: true}); + $searches_feeds.animate({'opacity': 1}, {'duration': (collapsed || options.update) ? 0 : 700}); + }, + load_router: function() { if (!NEWSBLUR.router) { NEWSBLUR.router = new NEWSBLUR.Router; @@ -280,7 +314,8 @@ NEWSBLUR.Views.FeedList = Backbone.View.extend({ var $feed_lists = this.$s.$feed_lists; var model = NEWSBLUR.assets.feeds.selected() || NEWSBLUR.assets.social_feeds.selected() || - NEWSBLUR.assets.starred_feeds.selected(); + NEWSBLUR.assets.starred_feeds.selected() || + NEWSBLUR.assets.searches_feeds.selected(); if (!model) return; var feed_view = model.get("selected_title_view"); if (!feed_view) { @@ -324,6 +359,11 @@ NEWSBLUR.Views.FeedList = Backbone.View.extend({ if (folder) { $selected_view = folder.folder_view.$el; $selected_view = $selected_view.find('.folder_title').eq(0); + } else { + folder = NEWSBLUR.assets.searches_feeds.selected(); + if (folder) { + $selected_view = folder.list_view.$el; + } } if (!$selected_view && NEWSBLUR.reader.active_feed == 'river:') { diff --git a/media/js/newsblur/views/feed_search_header.js b/media/js/newsblur/views/feed_search_header.js new file mode 100644 index 000000000..20e93c102 --- /dev/null +++ b/media/js/newsblur/views/feed_search_header.js @@ -0,0 +1,86 @@ +NEWSBLUR.Views.FeedSearchHeader = Backbone.View.extend({ + + el: ".NB-search-header", + + className: "NB-search-header", + + events: { + "click .NB-search-header-save": "save_search" + }, + + unload: function() { + this.$el.addClass("NB-hidden"); + }, + + render: function() { + this.showing_fake_folder = NEWSBLUR.reader.flags['river_view'] && + NEWSBLUR.reader.active_folder && + (NEWSBLUR.reader.active_folder.get('fake') || !NEWSBLUR.reader.active_folder.get('folder_title')); + + if (NEWSBLUR.reader.flags.search && NEWSBLUR.reader.flags.searching && NEWSBLUR.reader.flags.search.length) { + this.$el.removeClass("NB-hidden"); + + var $title = this.make_title(); + this.$(".NB-search-header-title").html($title); + + var saved = this.is_saved() ? 'Saved' : 'Save Search'; + this.$(".NB-search-header-save").text(saved); + } else { + this.unload(); + } + }, + + make_title: function() { + var feed_title; + if (NEWSBLUR.reader.flags['starred_view'] || + NEWSBLUR.reader.active_feed == "read" || + this.showing_fake_folder) { + feed_title = NEWSBLUR.reader.active_fake_folder_title(); + } else if (NEWSBLUR.reader.active_folder) { + feed_title = NEWSBLUR.reader.active_folder.get('folder_title'); + } else if (NEWSBLUR.reader.active_feed) { + feed_title = NEWSBLUR.assets.get_feed(NEWSBLUR.reader.active_feed).get('feed_title'); + } + var $view = $(_.template('
\ + Searching \ + <%= feed_title %> for "<%= query %>"\ +
', { + feed_title: feed_title, + query: NEWSBLUR.reader.flags.search + })); + + return $view; + }, + + is_saved: function() { + return !!NEWSBLUR.assets.get_search_feeds(this.saved_feed_id(), NEWSBLUR.reader.flags.search); + }, + + saved_feed_id: function() { + var feed_id = NEWSBLUR.reader.active_feed; + if (_.isNumber(feed_id)) { + feed_id = "feed:" + feed_id; + } + return feed_id; + }, + + // ========== + // = Events = + // ========== + + save_search: function(e) { + var feed_id = this.saved_feed_id(); + if (this.is_saved()) { + NEWSBLUR.assets.delete_saved_search(feed_id, NEWSBLUR.reader.flags.search, _.bind(function(e) { + console.log(['Saved searches', e]); + this.render(); + }, this)); + } else { + NEWSBLUR.assets.save_search(feed_id, NEWSBLUR.reader.flags.search, _.bind(function(e) { + console.log(['Saved searches', e]); + this.render(); + }, this)); + } + } + +}); \ No newline at end of file diff --git a/media/js/newsblur/views/feed_search_view.js b/media/js/newsblur/views/feed_search_view.js index 25cceecee..528650df7 100644 --- a/media/js/newsblur/views/feed_search_view.js +++ b/media/js/newsblur/views/feed_search_view.js @@ -19,7 +19,6 @@ NEWSBLUR.Views.FeedSearchView = Backbone.View.extend({ render: function() { if (NEWSBLUR.app.active_search) { - NEWSBLUR.app.active_search.blur_search(); NEWSBLUR.app.active_search.remove(); } NEWSBLUR.app.active_search = this; diff --git a/media/js/newsblur/views/feed_title_view.js b/media/js/newsblur/views/feed_title_view.js index 1e310f2c9..3bd884ccd 100644 --- a/media/js/newsblur/views/feed_title_view.js +++ b/media/js/newsblur/views/feed_title_view.js @@ -317,6 +317,8 @@ NEWSBLUR.Views.FeedTitleView = Backbone.View.extend({ if (this.model.get('has_exception') && this.model.get('exception_type') == 'feed') { NEWSBLUR.reader.open_feed_exception_modal(this.model.id); + } else if (this.model.is_search()) { + NEWSBLUR.reader.open_saved_search({search_model: this.model, $feed: this.$el}); } else if (this.model.is_social()) { NEWSBLUR.reader.open_social_stories(this.model.id, {force: true, $feed: this.$el}); } else if (this.model.is_starred()) { @@ -453,6 +455,7 @@ NEWSBLUR.Views.FeedTitleView = Backbone.View.extend({ var feed_type = this.model.is_social() ? 'socialfeed' : this.model.is_starred() ? 'starred' : + this.model.is_search() ? 'search' : 'feed'; e.preventDefault(); e.stopPropagation(); diff --git a/media/js/newsblur/views/sidebar.js b/media/js/newsblur/views/sidebar.js index ec0623904..4ee6aeb69 100644 --- a/media/js/newsblur/views/sidebar.js +++ b/media/js/newsblur/views/sidebar.js @@ -49,6 +49,37 @@ NEWSBLUR.Views.Sidebar = Backbone.View.extend({ } }, + check_searches_collapsed: function(options) { + options = options || {}; + var collapsed = _.contains(NEWSBLUR.Preferences.collapsed_folders, 'searches'); + + if (collapsed) { + this.show_collapsed_searches(options); + } + + return collapsed; + }, + + show_collapsed_searches: function(options) { + options = options || {}; + var $header = NEWSBLUR.reader.$s.$starred_header; + var $folder = this.$('.NB-starred-folder'); + + $header.addClass('NB-folder-collapsed'); + + if (!options.skip_animation) { + $header.addClass('NB-feedlist-folder-title-recently-collapsed'); + $header.one('mouseover', function() { + $header.removeClass('NB-feedlist-folder-title-recently-collapsed'); + }); + } else { + $folder.css({ + display: 'none', + opacity: 0 + }); + } + }, + check_river_blurblog_collapsed: function(options) { options = options || {}; var show_folder_counts = NEWSBLUR.assets.preference('folder_counts'); diff --git a/media/js/newsblur/views/story_titles_header_view.js b/media/js/newsblur/views/story_titles_header_view.js index f48dcfde9..cbd366cfc 100644 --- a/media/js/newsblur/views/story_titles_header_view.js +++ b/media/js/newsblur/views/story_titles_header_view.js @@ -47,10 +47,10 @@ NEWSBLUR.Views.StoryTitlesHeader = Backbone.View.extend({ \
\
\ - Saved Stories<% if (tag) { %> - <%= tag %><% } %>\ + <%= folder_title %>\ \ ', { - tag: NEWSBLUR.reader.flags['starred_tag'] + folder_title: NEWSBLUR.reader.active_fake_folder_title() })); this.search_view = new NEWSBLUR.Views.FeedSearchView({ feedbar_view: this @@ -68,9 +68,11 @@ NEWSBLUR.Views.StoryTitlesHeader = Backbone.View.extend({ \
\
\ -
Read Stories
\ +
<%= folder_title %>
\ \ - ', {})); + ', { + folder_title: NEWSBLUR.reader.active_fake_folder_title() + })); } else if (this.showing_fake_folder) { $view = $(_.template('\
">\ @@ -102,7 +104,7 @@ NEWSBLUR.Views.StoryTitlesHeader = Backbone.View.extend({ <%= folder_title %>\
\ ', { - folder_title: this.fake_folder_title(), + folder_title: NEWSBLUR.reader.active_fake_folder_title(), folder_id: NEWSBLUR.reader.active_feed, all_stories: NEWSBLUR.reader.active_feed == "river:", show_options: !NEWSBLUR.reader.active_folder.get('fake') || @@ -142,19 +144,6 @@ NEWSBLUR.Views.StoryTitlesHeader = Backbone.View.extend({ return this; }, - fake_folder_title: function() { - var title = "All Site Stories"; - if (NEWSBLUR.reader.flags['social_view']) { - if (NEWSBLUR.reader.flags['global_blurblogs']) { - title = "Global Shared Stories"; - } else { - title = "All Shared Stories"; - } - } - - return title; - }, - remove: function() { if (this.view) { this.view.remove(); @@ -227,7 +216,7 @@ NEWSBLUR.Views.StoryTitlesHeader = Backbone.View.extend({ return story.score() < 0; }); - NEWSBLUR.log(['show_hidden_story_titles', hidden_stories_at_threshold, hidden_stories_below_threshold, unread_view_name, temp_unread_view_name, NEWSBLUR.reader.flags['unread_threshold_temporarily']]); + // NEWSBLUR.log(['show_hidden_story_titles', hidden_stories_at_threshold, hidden_stories_below_threshold, unread_view_name, temp_unread_view_name, NEWSBLUR.reader.flags['unread_threshold_temporarily']]); // First click, open neutral. Second click, open negative. if (temp_unread_view_name == 'positive' && diff --git a/media/js/newsblur/views/text_tab_view.js b/media/js/newsblur/views/text_tab_view.js index 18fc522e0..3f1c25b12 100644 --- a/media/js/newsblur/views/text_tab_view.js +++ b/media/js/newsblur/views/text_tab_view.js @@ -53,8 +53,7 @@ NEWSBLUR.Views.TextTabView = Backbone.View.extend({ this.$el.scrollTop(0); this.story_detail.attach_handlers(); this.show_loading(); - NEWSBLUR.assets.fetch_original_text(story.get('id'), story.get('story_feed_id'), - this.render, this.error); + NEWSBLUR.assets.fetch_original_text(story.get('story_hash'), this.render, this.error); return this; }, diff --git a/settings.py b/settings.py index 52cfd30ab..7bf2c9dd6 100644 --- a/settings.py +++ b/settings.py @@ -158,6 +158,11 @@ LOGGING = { 'class': 'logging.StreamHandler', 'formatter': 'verbose' }, + 'vendor.apns':{ + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'verbose' + }, 'log_file':{ 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', @@ -655,7 +660,7 @@ BROKER_URL = "redis://%s:6379/%s" % (REDIS['host'], CELERY_REDIS_DB_NUM) CELERY_RESULT_BACKEND = BROKER_URL SESSION_REDIS_HOST = REDIS_SESSIONS['host'] SESSION_REDIS_RETRY_ON_TIMEOUT = True -SESSION_REDIS_SOCKET_TIMEOUT = 1 +SESSION_REDIS_SOCKET_TIMEOUT = 10 CACHES = { 'default': { @@ -705,9 +710,9 @@ if DEBUG: S3_CONN = None if BACKED_BY_AWS.get('pages_on_s3') or BACKED_BY_AWS.get('icons_on_s3'): S3_CONN = S3Connection(S3_ACCESS_KEY, S3_SECRET, calling_format=OrdinaryCallingFormat()) - if BACKED_BY_AWS.get('pages_on_s3'): - S3_PAGES_BUCKET = S3_CONN.get_bucket(S3_PAGES_BUCKET_NAME) - if BACKED_BY_AWS.get('icons_on_s3'): - S3_ICONS_BUCKET = S3_CONN.get_bucket(S3_ICONS_BUCKET_NAME) + # if BACKED_BY_AWS.get('pages_on_s3'): + # S3_PAGES_BUCKET = S3_CONN.get_bucket(S3_PAGES_BUCKET_NAME) + # if BACKED_BY_AWS.get('icons_on_s3'): + # S3_ICONS_BUCKET = S3_CONN.get_bucket(S3_ICONS_BUCKET_NAME) django.http.request.host_validation_re = re.compile(r"^([a-z0-9.-_\-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$") diff --git a/templates/mail/email_story_notification.xhtml b/templates/mail/email_story_notification.xhtml index 851bdaa61..efa5ee7b8 100644 --- a/templates/mail/email_story_notification.xhtml +++ b/templates/mail/email_story_notification.xhtml @@ -14,7 +14,7 @@

{{ story.story_title }}

-

{{ story.story_content|safe }}

+

{{ story_content|safe }}

{{ story.story_permalink|safe }} diff --git a/templates/maintenance_off.html b/templates/maintenance_off.html index 09014de4f..742dc0eaf 100644 --- a/templates/maintenance_off.html +++ b/templates/maintenance_off.html @@ -85,7 +85,7 @@

NewsBlur is in maintenance mode

-

Upgrading the Postgresql server. I upgraded it last night but the new server was no better. This time I ensured I'm getting a fresh one. This should take about 5 minutes, hopefully less if all goes well.

+

Upgrading the Postgresql server. I upgraded it last week but the new server is no better. This time I ensured I'm getting a fresh one. This should take about 5 minutes, hopefully less if all goes well.

To pass the time, check out what's popular on MLKSHK.

diff --git a/templates/reader/feeds_skeleton.xhtml b/templates/reader/feeds_skeleton.xhtml index 1c361c009..5ff2a07d0 100644 --- a/templates/reader/feeds_skeleton.xhtml +++ b/templates/reader/feeds_skeleton.xhtml @@ -89,6 +89,21 @@ + + +
+
+
+
+ Saved Searches +
+
+
+ +
+
    +
    +
    @@ -258,6 +273,11 @@
    +
    +
    +
    +
    +
    diff --git a/utils/feed_fetcher.py b/utils/feed_fetcher.py index 1b60cb5a0..4354eb704 100644 --- a/utils/feed_fetcher.py +++ b/utils/feed_fetcher.py @@ -22,7 +22,7 @@ from apps.rss_feeds.icon_importer import IconImporter from apps.notifications.tasks import QueueNotifications, MUserFeedNotification from apps.push.models import PushSubscription from apps.social.models import MSocialServices -from apps.statistics.models import MAnalyticsFetcher +from apps.statistics.models import MAnalyticsFetcher, MStatistics from utils import feedparser from utils.story_functions import pre_process_story, strip_tags, linkify from utils import log as logging @@ -49,6 +49,7 @@ class FetchFeed: self.feed = Feed.get_by_id(feed_id) self.options = options self.fpf = None + self.raw_feed = None @timelimit(30) def fetch(self): @@ -58,7 +59,7 @@ class FetchFeed: start = time.time() identity = self.get_identity() log_msg = u'%2s ---> [%-30s] ~FYFetching feed (~FB%d~FY), last update: %s' % (identity, - self.feed.title[:30], + self.feed.log_title[:30], self.feed.id, datetime.datetime.now() - self.feed.last_update) logging.debug(log_msg) @@ -73,19 +74,19 @@ class FetchFeed: etag = None address = qurl(address, add={"_": random.randint(0, 10000)}) logging.debug(u' ---> [%-30s] ~FBForcing fetch: %s' % ( - self.feed.title[:30], address)) + self.feed.log_title[:30], address)) elif (not self.feed.fetched_once or not self.feed.known_good): modified = None etag = None if self.options.get('feed_xml'): logging.debug(u' ---> [%-30s] ~FM~BKFeed has been fat pinged. Ignoring fat: %s' % ( - self.feed.title[:30], len(self.options.get('feed_xml')))) + self.feed.log_title[:30], len(self.options.get('feed_xml')))) if self.options.get('fpf'): self.fpf = self.options.get('fpf') logging.debug(u' ---> [%-30s] ~FM~BKFeed fetched in real-time with fat ping.' % ( - self.feed.title[:30])) + self.feed.log_title[:30])) return FEED_OK, self.fpf if 'youtube.com' in address: @@ -95,7 +96,7 @@ class FetchFeed: youtube_feed = None if not youtube_feed: logging.debug(u' ***> [%-30s] ~FRYouTube fetch failed: %s.' % - (self.feed.title[:30], address)) + (self.feed.log_title[:30], address)) return FEED_ERRHTTP, None self.fpf = feedparser.parse(youtube_feed) elif re.match('(https?)?://twitter.com/\w+/?$', qurl(address, remove=['_'])): @@ -103,11 +104,11 @@ class FetchFeed: twitter_feed = self.fetch_twitter(address) # except Exception, e: # logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %e' % - # (self.feed.title[:30], address, e)) + # (self.feed.log_title[:30], address, e)) # twitter_feed = None if not twitter_feed: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s' % - (self.feed.title[:30], address)) + (self.feed.log_title[:30], address)) return FEED_ERRHTTP, None self.fpf = feedparser.parse(twitter_feed) @@ -129,17 +130,18 @@ class FetchFeed: headers['A-IM'] = 'feed' raw_feed = requests.get(address, headers=headers) if raw_feed.status_code >= 400: - logging.debug(" ---> [%-30s] ~FRFeed fetch was %s status code, trying fake user agent: %s" % (self.feed.title[:30], raw_feed.status_code, raw_feed.headers)) + logging.debug(" ***> [%-30s] ~FRFeed fetch was %s status code, trying fake user agent: %s" % (self.feed.log_title[:30], raw_feed.status_code, raw_feed.headers)) raw_feed = requests.get(address, headers=self.feed.fetch_headers(fake=True)) if raw_feed.content and raw_feed.status_code < 400: response_headers = raw_feed.headers response_headers['Content-Location'] = raw_feed.url - self.fpf = feedparser.parse(smart_unicode(raw_feed.content), + self.raw_feed = smart_unicode(raw_feed.content) + self.fpf = feedparser.parse(self.raw_feed, response_headers=response_headers) if self.options.get('debug', False): - logging.debug(" ---> [%-30s] ~FBFeed fetch status %s: %s length / %s" % (self.feed.title[:30], raw_feed.status_code, len(smart_unicode(raw_feed.content)), raw_feed.headers)) + logging.debug(" ---> [%-30s] ~FBFeed fetch status %s: %s length / %s" % (self.feed.log_title[:30], raw_feed.status_code, len(smart_unicode(raw_feed.content)), raw_feed.headers)) except Exception, e: - logging.debug(" ---> [%-30s] ~FRFeed failed to fetch with request, trying feedparser: %s" % (self.feed.title[:30], unicode(e)[:100])) + logging.debug(" ***> [%-30s] ~FRFeed failed to fetch with request, trying feedparser: %s" % (self.feed.log_title[:30], unicode(e)[:100])) if not self.fpf or self.options.get('force_fp', False): try: @@ -149,21 +151,21 @@ class FetchFeed: modified=modified) except (TypeError, ValueError, KeyError, EOFError, MemoryError), e: logging.debug(u' ***> [%-30s] ~FRFeed fetch error: %s' % - (self.feed.title[:30], e)) + (self.feed.log_title[:30], e)) pass if not self.fpf: try: logging.debug(u' ***> [%-30s] ~FRTurning off headers...' % - (self.feed.title[:30])) + (self.feed.log_title[:30])) self.fpf = feedparser.parse(address, agent=self.feed.user_agent) except (TypeError, ValueError, KeyError, EOFError, MemoryError), e: logging.debug(u' ***> [%-30s] ~FRFetch failed: %s.' % - (self.feed.title[:30], e)) + (self.feed.log_title[:30], e)) return FEED_ERRHTTP, None logging.debug(u' ---> [%-30s] ~FYFeed fetch in ~FM%.4ss' % ( - self.feed.title[:30], time.time() - start)) + self.feed.log_title[:30], time.time() - start)) return FEED_OK, self.fpf @@ -197,7 +199,7 @@ class FetchFeed: elif 'youtube.com/feeds/videos.xml?channel_id=' in address: try: channel_id = urlparse.parse_qs(urlparse.urlparse(address).query)['channel_id'][0] - except IndexError: + except (IndexError, KeyError): return elif 'youtube.com/playlist' in address: try: @@ -327,33 +329,35 @@ class FetchFeed: twitter_api = social_services.twitter_api() except tweepy.error.TweepError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) return else: usersubs = UserSubscription.objects.filter(feed=self.feed) if not usersubs: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: No subscriptions' % - (self.feed.title[:30], address)) + (self.feed.log_title[:30], address)) return for sub in usersubs: social_services = MSocialServices.get_user(sub.user_id) + if not social_services.twitter_uid: continue try: twitter_api = social_services.twitter_api() + break except tweepy.error.TweepError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) continue if not twitter_api: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: No twitter API for %s' % - (self.feed.title[:30], address, usersubs[0].user.username)) + (self.feed.log_title[:30], address, usersubs[0].user.username)) return try: twitter_user = twitter_api.get_user(username) except TypeError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed, disconnecting twitter: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) social_services.disconnect_twitter() return except tweepy.error.TweepError, e: @@ -362,39 +366,49 @@ class FetchFeed: ('temporarily locked' in message)): # Suspended logging.debug(u' ***> [%-30s] ~FRTwitter failed, user suspended, disconnecting twitter: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) social_services.disconnect_twitter() return elif 'suspended' in message: logging.debug(u' ***> [%-30s] ~FRTwitter user suspended, disconnecting twitter: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) + social_services.disconnect_twitter() + return + elif 'expired token' in message: + logging.debug(u' ***> [%-30s] ~FRTwitter user expired, disconnecting twitter: %s: %s' % + (self.feed.log_title[:30], address, e)) social_services.disconnect_twitter() return elif 'not found' in message: logging.debug(u' ***> [%-30s] ~FRTwitter user not found, disconnecting twitter: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) social_services.disconnect_twitter() return elif 'over capacity' in message: logging.debug(u' ***> [%-30s] ~FRTwitter over capacity, ignoring... %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) return else: - raise + raise e try: tweets = twitter_user.timeline() except tweepy.error.TweepError, e: - if 'Not authorized' in e.args[0]: + message = str(e).lower() + if 'not authorized' in message: logging.debug(u' ***> [%-30s] ~FRTwitter timeline failed, disconnecting twitter: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) social_services.disconnect_twitter() return - elif 'User not found' in e.args[0]: + elif 'user not found' in message: logging.debug(u' ***> [%-30s] ~FRTwitter user not found, disconnecting twitter: %s: %s' % - (self.feed.title[:30], address, e)) + (self.feed.log_title[:30], address, e)) social_services.disconnect_twitter() return + elif 'blocked from viewing' in message: + logging.debug(u' ***> [%-30s] ~FRTwitter user blocked, ignoring: %s' % + (self.feed.log_title[:30], e)) + return else: raise e @@ -475,10 +489,11 @@ class FetchFeed: return rss.writeString('utf-8') class ProcessFeed: - def __init__(self, feed_id, fpf, options): + def __init__(self, feed_id, fpf, options, raw_feed=None): self.feed_id = feed_id self.options = options self.fpf = fpf + self.raw_feed = raw_feed def refresh_feed(self): self.feed = Feed.get_by_id(self.feed_id) @@ -498,7 +513,7 @@ class ProcessFeed: if self.options['verbose']: if self.fpf.bozo and self.fpf.status != 304: logging.debug(u' ---> [%-30s] ~FRBOZO exception: %s ~SB(%s entries)' % ( - self.feed.title[:30], + self.feed.log_title[:30], self.fpf.bozo_exception, len(self.fpf.entries))) @@ -521,14 +536,14 @@ class ProcessFeed: self.feed.feed_address = address if not self.feed.known_good: self.feed.fetched_once = True - logging.debug(" ---> [%-30s] ~SB~SK~FRFeed is %s'ing. Refetching..." % (self.feed.title[:30], self.fpf.status)) + logging.debug(" ---> [%-30s] ~SB~SK~FRFeed is %s'ing. Refetching..." % (self.feed.log_title[:30], self.fpf.status)) self.feed = self.feed.schedule_feed_fetch_immediately() if not self.fpf.entries: self.feed = self.feed.save() self.feed.save_feed_history(self.fpf.status, "HTTP Redirect") return FEED_ERRHTTP, ret_values if self.fpf.status >= 400: - logging.debug(" ---> [%-30s] ~SB~FRHTTP Status code: %s. Checking address..." % (self.feed.title[:30], self.fpf.status)) + logging.debug(" ---> [%-30s] ~SB~FRHTTP Status code: %s. Checking address..." % (self.feed.log_title[:30], self.fpf.status)) fixed_feed = None if not self.feed.known_good: fixed_feed, feed = self.feed.check_feed_link_for_feed_address() @@ -540,13 +555,13 @@ class ProcessFeed: return FEED_ERRHTTP, ret_values if not self.fpf: - logging.debug(" ---> [%-30s] ~SB~FRFeed is Non-XML. No feedparser feed either!" % (self.feed.title[:30])) + logging.debug(" ---> [%-30s] ~SB~FRFeed is Non-XML. No feedparser feed either!" % (self.feed.log_title[:30])) self.feed.save_feed_history(551, "Broken feed") return FEED_ERRHTTP, ret_values if self.fpf and not self.fpf.entries: if self.fpf.bozo and isinstance(self.fpf.bozo_exception, feedparser.NonXMLContentType): - logging.debug(" ---> [%-30s] ~SB~FRFeed is Non-XML. %s entries. Checking address..." % (self.feed.title[:30], len(self.fpf.entries))) + logging.debug(" ---> [%-30s] ~SB~FRFeed is Non-XML. %s entries. Checking address..." % (self.feed.log_title[:30], len(self.fpf.entries))) fixed_feed = None if not self.feed.known_good: fixed_feed, feed = self.feed.check_feed_link_for_feed_address() @@ -557,7 +572,7 @@ class ProcessFeed: self.feed = self.feed.save() return FEED_ERRPARSE, ret_values elif self.fpf.bozo and isinstance(self.fpf.bozo_exception, xml.sax._exceptions.SAXException): - logging.debug(" ---> [%-30s] ~SB~FRFeed has SAX/XML parsing issues. %s entries. Checking address..." % (self.feed.title[:30], len(self.fpf.entries))) + logging.debug(" ---> [%-30s] ~SB~FRFeed has SAX/XML parsing issues. %s entries. Checking address..." % (self.feed.log_title[:30], len(self.fpf.entries))) fixed_feed = None if not self.feed.known_good: fixed_feed, feed = self.feed.check_feed_link_for_feed_address() @@ -611,7 +626,7 @@ class ProcessFeed: if self.options['force'] and new_feed_link: new_feed_link = qurl(new_feed_link, remove=['_']) if new_feed_link != self.feed.feed_link: - logging.debug(" ---> [%-30s] ~SB~FRFeed's page is different: %s to %s" % (self.feed.title[:30], self.feed.feed_link, new_feed_link)) + logging.debug(" ---> [%-30s] ~SB~FRFeed's page is different: %s to %s" % (self.feed.log_title[:30], self.feed.feed_link, new_feed_link)) redirects, non_redirects = self.feed.count_redirects_in_history('page') self.feed.save_page_history(301, "HTTP Redirect (%s to go)" % (10-len(redirects))) if len(redirects) >= 10 or len(non_redirects) == 0: @@ -644,14 +659,14 @@ class ProcessFeed: new_story_guid = unicode(story.get('published')) if self.options['verbose']: logging.debug(u' ---> [%-30s] ~FBReplacing guid (%s) with timestamp: %s' % ( - self.feed.title[:30], + self.feed.log_title[:30], story.get('guid'), new_story_guid)) story['guid'] = new_story_guid else: new_story_guid = Feed.get_permalink(story) if self.options['verbose']: logging.debug(u' ---> [%-30s] ~FBReplacing guid (%s) with permalink: %s' % ( - self.feed.title[:30], + self.feed.log_title[:30], story.get('guid'), new_story_guid)) story['guid'] = new_story_guid story['story_hash'] = MStory.feed_guid_hash_unsaved(self.feed.pk, story.get('guid')) @@ -688,17 +703,17 @@ class ProcessFeed: self.feed.active_subscribers > 0 and (push_expired or not self.feed.is_push or self.options.get('force'))): logging.debug(u' ---> [%-30s] ~BB~FW%sSubscribing to PuSH hub: %s' % ( - self.feed.title[:30], + self.feed.log_title[:30], "~SKRe-~SN" if push_expired else "", hub_url)) try: PushSubscription.objects.subscribe(self_url, feed=self.feed, hub=hub_url) except TimeoutError: logging.debug(u' ---> [%-30s] ~BB~FW~FRTimed out~FW subscribing to PuSH hub: %s' % ( - self.feed.title[:30], hub_url)) + self.feed.log_title[:30], hub_url)) elif (self.feed.is_push and (self.feed.active_subscribers <= 0 or not hub_url)): logging.debug(u' ---> [%-30s] ~BB~FWTurning off PuSH, no hub found' % ( - self.feed.title[:30])) + self.feed.log_title[:30])) self.feed.is_push = False self.feed = self.feed.save() @@ -708,21 +723,24 @@ class ProcessFeed: # All Done logging.debug(u' ---> [%-30s] ~FYParsed Feed: %snew=%s~SN~FY %sup=%s~SN same=%s%s~SN %serr=%s~SN~FY total=~SB%s' % ( - self.feed.title[:30], + self.feed.log_title[:30], '~FG~SB' if ret_values['new'] else '', ret_values['new'], '~FY~SB' if ret_values['updated'] else '', ret_values['updated'], '~SB' if ret_values['same'] else '', ret_values['same'], '~FR~SB' if ret_values['error'] else '', ret_values['error'], len(self.fpf.entries))) self.feed.update_all_statistics(has_new_stories=bool(ret_values['new']), force=self.options['force']) + fetch_date = datetime.datetime.now() if ret_values['new']: self.feed.trim_feed() self.feed.expire_redis() - self.feed.save_feed_history(200, "OK") + if MStatistics.get('raw_feed', None) == self.feed.pk: + self.feed.save_raw_feed(self.raw_feed, fetch_date) + self.feed.save_feed_history(200, "OK", date=fetch_date) if self.options['verbose']: logging.debug(u' ---> [%-30s] ~FBTIME: feed parse in ~FM%.4ss' % ( - self.feed.title[:30], time.time() - start)) + self.feed.log_title[:30], time.time() - start)) return FEED_OK, ret_values @@ -794,7 +812,7 @@ class Dispatcher: rand = "-" if skip: logging.debug(' ---> [%-30s] ~BGFaking fetch, skipping (%s/month, %s subs, %s < %s)...' % ( - feed.title[:30], + feed.log_title[:30], weight, feed.num_subscribers, rand, quick)) @@ -803,9 +821,10 @@ class Dispatcher: ffeed = FetchFeed(feed_id, self.options) ret_feed, fetched_feed = ffeed.fetch() feed_fetch_duration = time.time() - start_duration + raw_feed = ffeed.raw_feed if ((fetched_feed and ret_feed == FEED_OK) or self.options['force']): - pfeed = ProcessFeed(feed_id, fetched_feed, self.options) + pfeed = ProcessFeed(feed_id, fetched_feed, self.options, raw_feed=raw_feed) ret_feed, ret_entries = pfeed.process() feed = pfeed.feed feed_process_duration = time.time() - start_duration @@ -817,17 +836,17 @@ class Dispatcher: feed.fetched_once = True feed = feed.save() if self.options['force'] or random.random() <= 0.02: - logging.debug(' ---> [%-30s] ~FBPerforming feed cleanup...' % (feed.title[:30],)) + logging.debug(' ---> [%-30s] ~FBPerforming feed cleanup...' % (feed.log_title[:30],)) start_cleanup = time.time() feed.sync_redis() - logging.debug(' ---> [%-30s] ~FBDone with feed cleanup. Took ~SB%.4s~SN sec.' % (feed.title[:30], time.time() - start_cleanup)) + logging.debug(' ---> [%-30s] ~FBDone with feed cleanup. Took ~SB%.4s~SN sec.' % (feed.log_title[:30], time.time() - start_cleanup)) try: self.count_unreads_for_subscribers(feed) except TimeoutError: - logging.debug(' ---> [%-30s] Unread count took too long...' % (feed.title[:30],)) + logging.debug(' ---> [%-30s] Unread count took too long...' % (feed.log_title[:30],)) if self.options['verbose']: logging.debug(u' ---> [%-30s] ~FBTIME: unread count in ~FM%.4ss' % ( - feed.title[:30], time.time() - start)) + feed.log_title[:30], time.time() - start)) except urllib2.HTTPError, e: logging.debug(' ---> [%-30s] ~FRFeed throws HTTP error: ~SB%s' % (unicode(feed_id)[:30], e.fp.read())) feed_code = e.code @@ -843,7 +862,7 @@ class Dispatcher: feed_code = 559 feed.save_feed_history(feed_code, 'Timeout', e) except TimeoutError, e: - logging.debug(' ---> [%-30s] ~FRFeed fetch timed out...' % (feed.title[:30])) + logging.debug(' ---> [%-30s] ~FRFeed fetch timed out...' % (feed.log_title[:30])) feed_code = 505 feed.save_feed_history(feed_code, 'Timeout', e) fetched_feed = None @@ -887,7 +906,7 @@ class Dispatcher: (ret_feed == FEED_OK or (ret_feed == FEED_SAME and feed.stories_last_month > 10)))): - logging.debug(u' ---> [%-30s] ~FYFetching page: %s' % (feed.title[:30], feed.feed_link)) + logging.debug(u' ---> [%-30s] ~FYFetching page: %s' % (feed.log_title[:30], feed.feed_link)) page_importer = PageImporter(feed) try: page_data = page_importer.fetch_page() @@ -897,7 +916,7 @@ class Dispatcher: page_data = None feed.save_feed_history(557, 'Timeout', e) except TimeoutError, e: - logging.debug(' ---> [%-30s] ~FRPage fetch timed out...' % (feed.title[:30])) + logging.debug(' ---> [%-30s] ~FRPage fetch timed out...' % (feed.log_title[:30])) page_data = None feed.save_page_history(555, 'Timeout', '') except Exception, e: @@ -914,7 +933,7 @@ class Dispatcher: settings.RAVEN_CLIENT.captureException() feed = self.refresh_feed(feed.pk) - logging.debug(u' ---> [%-30s] ~FYFetching icon: %s' % (feed.title[:30], feed.feed_link)) + logging.debug(u' ---> [%-30s] ~FYFetching icon: %s' % (feed.log_title[:30], feed.feed_link)) force = self.options['force'] if random.random() > .99: force = True @@ -926,7 +945,7 @@ class Dispatcher: logging.debug(" ---> [%-30s] ~BR~FWTime limit hit!~SB~FR Moving on to next feed..." % feed) feed.save_feed_history(558, 'Timeout', e) except TimeoutError, e: - logging.debug(' ---> [%-30s] ~FRIcon fetch timed out...' % (feed.title[:30])) + logging.debug(' ---> [%-30s] ~FRIcon fetch timed out...' % (feed.log_title[:30])) feed.save_page_history(556, 'Timeout', '') except Exception, e: logging.debug('[%d] ! -------------------------' % (feed_id,)) @@ -939,7 +958,7 @@ class Dispatcher: settings.RAVEN_CLIENT): settings.RAVEN_CLIENT.captureException() else: - logging.debug(u' ---> [%-30s] ~FBSkipping page fetch: (%s on %s stories) %s' % (feed.title[:30], self.feed_trans[ret_feed], feed.stories_last_month, '' if feed.has_page else ' [HAS NO PAGE]')) + logging.debug(u' ---> [%-30s] ~FBSkipping page fetch: (%s on %s stories) %s' % (feed.log_title[:30], self.feed_trans[ret_feed], feed.stories_last_month, '' if feed.has_page else ' [HAS NO PAGE]')) feed = self.refresh_feed(feed.pk) delta = time.time() - start_time @@ -949,13 +968,13 @@ class Dispatcher: try: feed = feed.save(update_fields=['last_load_time', 'fetched_once']) except IntegrityError: - logging.debug(" ---> [%-30s] ~FRIntegrityError on feed: %s" % (feed.title[:30], feed.feed_address,)) + logging.debug(" ***> [%-30s] ~FRIntegrityError on feed: %s" % (feed.log_title[:30], feed.feed_address,)) if ret_entries and ret_entries['new']: self.publish_to_subscribers(feed, ret_entries['new']) done_msg = (u'%2s ---> [%-30s] ~FYProcessed in ~FM~SB%.4ss~FY~SN (~FB%s~FY) [%s]' % ( - identity, feed.title[:30], delta, + identity, feed.log_title[:30], delta, feed.pk, self.feed_trans[ret_feed],)) logging.debug(done_msg) total_duration = time.time() - start_duration @@ -976,9 +995,9 @@ class Dispatcher: r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) listeners_count = r.publish(str(feed.pk), 'story:new_count:%s' % new_count) if listeners_count: - logging.debug(" ---> [%-30s] ~FMPublished to %s subscribers" % (feed.title[:30], listeners_count)) + logging.debug(" ---> [%-30s] ~FMPublished to %s subscribers" % (feed.log_title[:30], listeners_count)) except redis.ConnectionError: - logging.debug(" ***> [%-30s] ~BMRedis is unavailable for real-time." % (feed.title[:30],)) + logging.debug(" ***> [%-30s] ~BMRedis is unavailable for real-time." % (feed.log_title[:30],)) def count_unreads_for_subscribers(self, feed): user_subs = UserSubscription.objects.filter(feed=feed, @@ -1008,15 +1027,15 @@ class Dispatcher: .read_preference(pymongo.ReadPreference.PRIMARY) missing_stories = Feed.format_stories(missing_stories, feed.pk) stories = missing_stories + stories - logging.debug(u' ---> [%-30s] ~FYFound ~SB~FC%s(of %s)/%s~FY~SN un-secondaried stories while computing scores' % (feed.title[:30], len(missing_stories), len(missing_story_hashes), len(stories))) + logging.debug(u' ---> [%-30s] ~FYFound ~SB~FC%s(of %s)/%s~FY~SN un-secondaried stories while computing scores' % (feed.log_title[:30], len(missing_stories), len(missing_story_hashes), len(stories))) cache.set("S:%s" % feed.pk, stories, 60) logging.debug(u' ---> [%-30s] ~FYComputing scores: ~SB%s stories~SN with ~SB%s subscribers ~SN(%s/%s/%s)' % ( - feed.title[:30], len(stories), user_subs.count(), + feed.log_title[:30], len(stories), user_subs.count(), feed.num_subscribers, feed.active_subscribers, feed.premium_subscribers)) self.calculate_feed_scores_with_stories(user_subs, stories) elif self.options.get('mongodb_replication_lag'): logging.debug(u' ---> [%-30s] ~BR~FYSkipping computing scores: ~SB%s seconds~SN of mongodb lag' % ( - feed.title[:30], self.options.get('mongodb_replication_lag'))) + feed.log_title[:30], self.options.get('mongodb_replication_lag'))) @timelimit(10) def calculate_feed_scores_with_stories(self, user_subs, stories): diff --git a/utils/s3_utils.py b/utils/s3_utils.py index 22f74e8a8..acb6e3c82 100644 --- a/utils/s3_utils.py +++ b/utils/s3_utils.py @@ -16,6 +16,18 @@ ACCESS_KEY = settings.S3_ACCESS_KEY SECRET = settings.S3_SECRET BUCKET_NAME = settings.S3_BACKUP_BUCKET # Note that you need to create this bucket first +import ssl + +_old_match_hostname = ssl.match_hostname + +def _new_match_hostname(cert, hostname): + if hostname.endswith('.s3.amazonaws.com'): + pos = hostname.find('.s3.amazonaws.com') + hostname = hostname[:pos].replace('.', '') + hostname[pos:] + return _old_match_hostname(cert, hostname) + +ssl.match_hostname = _new_match_hostname + def save_file_in_s3(filename): conn = S3Connection(ACCESS_KEY, SECRET) bucket = conn.get_bucket(BUCKET_NAME)