Bug 96126 - Appcache fallback URL match should use the longest candidate
Summary: Appcache fallback URL match should use the longest candidate
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-07 10:09 PDT by Leo Yang
Modified: 2012-09-07 14:28 PDT (History)
8 users (show)

See Also:


Attachments
Patch (5.39 KB, patch)
2012-09-07 10:20 PDT, Leo Yang
no flags Details | Formatted Diff | Diff
Patch v2 (5.39 KB, patch)
2012-09-07 10:39 PDT, Leo Yang
ap: review-
ap: commit-queue-
Details | Formatted Diff | Diff
Patch v3 (6.97 KB, patch)
2012-09-07 12:44 PDT, Leo Yang
no flags Details | Formatted Diff | Diff
Patch v4 (7.06 KB, patch)
2012-09-07 13:31 PDT, Leo Yang
ap: review+
ap: commit-queue-
Details | Formatted Diff | Diff
Patch v5 (7.06 KB, patch)
2012-09-07 13:48 PDT, Leo Yang
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Leo Yang 2012-09-07 10:09:52 PDT
http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#concept-appcache-matches-fallback states the match as following:

A URL matches a fallback namespace if there exists a relevant application cache whose manifest's URL has the same origin as the URL in question, and that has a fallback namespace that is a prefix match for the URL being examined. If multiple fallback namespaces match the same URL, the longest one is the one that matches. A URL looking for a fallback namespace can match more than one application cache at a time, but only matches one namespace in each cache.
Comment 1 Leo Yang 2012-09-07 10:20:24 PDT
Created attachment 162801 [details]
Patch
Comment 2 Yong Li 2012-09-07 10:29:36 PDT
Comment on attachment 162801 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=162801&action=review

basically lgtm. but we should get some AppCache experts involved.

> Source/WebCore/loader/appcache/ApplicationCache.cpp:42
> +    bool operator()(const std::pair<KURL, KURL>& lhs, const std::pair<KURL, KURL>& rhs)

static bool operator() is better? If not static, it should be const.
Comment 3 Yong Li 2012-09-07 10:32:51 PDT
+Tony who reviewed r121062.
Comment 4 Leo Yang 2012-09-07 10:39:59 PDT
Created attachment 162812 [details]
Patch v2

Following Yong's comment -- making the operator const.
Comment 5 Alexey Proskuryakov 2012-09-07 10:46:45 PDT
Comment on attachment 162801 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=162801&action=review

Looks good to me. Since there are quite a few comments for such a small patch, I'd like to do another quick round of review on an updated one.

> LayoutTests/http/tests/appcache/resources/fallback.manifest:4
> +/resources/ empty.txt
>  /resources/network-simulator.php? simple.txt

This is a bit subtle - it would be very hard to tell what exactly failed from an error output of this test. If I try to run this updated test with current trunk, it just says "Loading an URL from fallback namespace when network is disabled returned unexpected response".

Not strictly a requirement for future r+, but please consider separating this new test from testing basic fallback functionality, or perhaps the test to have more informative failure output.

> Source/WebCore/ChangeLog:14
> +        http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#concept-appcache-matches-fallback
> +        A URL matches a fallback namespace if there exists a relevant application cache
> +        whose manifest's URL has the same origin as the URL in question, and that has a
> +        fallback namespace that is a prefix match for the URL being examined. If multiple
> +        fallback namespaces match the same URL, the longest one is the one that matches.
> +        A URL looking for a fallback namespace can match more than one application cache
> +        at a time, but only matches one namespace in each cache.

I'd omit this whole explanation, bug title is explanatory enough.

> Source/WebCore/ChangeLog:24
> +        (WebCore):

It's best to remove unhelpful lines like this. There is no way it would ever help anyone reading the ChangeLog.

> Source/WebCore/loader/appcache/ApplicationCache.cpp:34
> +

This blank line shouldn't be here.

> Source/WebCore/loader/appcache/ApplicationCache.cpp:42
> +struct FallbackURLGreaterThan {
> +    bool operator()(const std::pair<KURL, KURL>& lhs, const std::pair<KURL, KURL>& rhs)

Please just use a "static inline bool" function.

Also, the name would be more helpful if it said what the function was actually doing, e.g. "FallbackURLLongerThan".
Comment 6 Yong Li 2012-09-07 11:04:05 PDT
Comment on attachment 162812 [details]
Patch v2

View in context: https://bugs.webkit.org/attachment.cgi?id=162812&action=review

> Source/WebCore/loader/appcache/ApplicationCache.cpp:173
>  void ApplicationCache::setFallbackURLs(const FallbackURLVector& fallbackURLs)
>  {
>      ASSERT(m_fallbackURLs.isEmpty());
>      m_fallbackURLs = fallbackURLs;
> +    std::sort(m_fallbackURLs.begin(), m_fallbackURLs.end(), FallbackURLGreaterThan());
>  }
>  

A silly question: would a std::stable_sort be better here? it will be slower, but keep the original order unchanged as long as possible
Comment 7 Yong Li 2012-09-07 11:05:30 PDT
(In reply to comment #5)
> (From update of attachment 162801 [details])

> > Source/WebCore/loader/appcache/ApplicationCache.cpp:42
> > +struct FallbackURLGreaterThan {
> > +    bool operator()(const std::pair<KURL, KURL>& lhs, const std::pair<KURL, KURL>& rhs)
> 
> Please just use a "static inline bool" function.
> 

I would suggest the same, but not sure if it can be built with all STL providers and compilers.
Comment 8 Leo Yang 2012-09-07 11:21:52 PDT
(In reply to comment #6)
> (From update of attachment 162812 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=162812&action=review
> 
> > Source/WebCore/loader/appcache/ApplicationCache.cpp:173
> >  void ApplicationCache::setFallbackURLs(const FallbackURLVector& fallbackURLs)
> >  {
> >      ASSERT(m_fallbackURLs.isEmpty());
> >      m_fallbackURLs = fallbackURLs;
> > +    std::sort(m_fallbackURLs.begin(), m_fallbackURLs.end(), FallbackURLGreaterThan());
> >  }
> >  
> 
> A silly question: would a std::stable_sort be better here? it will be slower, but keep the original order unchanged as long as possible

Unless there are 2 same namespaces with different fallback urls.
Comment 9 Leo Yang 2012-09-07 11:23:21 PDT
(In reply to comment #7)
> (In reply to comment #5)
> > (From update of attachment 162801 [details] [details])
> 
> > > Source/WebCore/loader/appcache/ApplicationCache.cpp:42
> > > +struct FallbackURLGreaterThan {
> > > +    bool operator()(const std::pair<KURL, KURL>& lhs, const std::pair<KURL, KURL>& rhs)
> > 
> > Please just use a "static inline bool" function.
> > 
> 
> I would suggest the same, but not sure if it can be built with all STL providers and compilers.

Because we are going to use a function pointer, I think inline wouldn't help. And as Yong said, not sure if some compiler would complain.
Comment 10 Alexey Proskuryakov 2012-09-07 11:25:02 PDT
That's a good idea. We already use std::stable_sort in cross platform code, so it should be fine.

Effect of switching stable sort are worth testing in a regression test, too.

> Because we are going to use a function pointer, I think inline wouldn't help. And as Yong said, not sure if some compiler would complain.

std::sort does not use the function as a pointer, it uses its argument as a generic functor. You might be thinking of qsort() here.
Comment 11 Leo Yang 2012-09-07 12:44:32 PDT
Created attachment 162845 [details]
Patch v3
Comment 12 Alexey Proskuryakov 2012-09-07 13:09:25 PDT
Comment on attachment 162845 [details]
Patch v3

The change from sort to stable_sort is not tested AFAICT. Could you add a test?

In fact, I'm not sure what the right behavior is - is it the first or the last identical entry that wins? If we are doing it wrong, then this patch doesn't change it, and we don't need a test. But we need a FIXME then.
Comment 13 Leo Yang 2012-09-07 13:26:23 PDT
(In reply to comment #12)
> (From update of attachment 162845 [details])
> The change from sort to stable_sort is not tested AFAICT. Could you add a test?
> 
> In fact, I'm not sure what the right behavior is - is it the first or the last identical entry that wins? If we are doing it wrong, then this patch doesn't change it, and we don't need a test. But we need a FIXME then.

Neither me. I'll add a FIXME.
Comment 14 Leo Yang 2012-09-07 13:31:48 PDT
Created attachment 162855 [details]
Patch v4
Comment 15 Alexey Proskuryakov 2012-09-07 13:42:49 PDT
Comment on attachment 162855 [details]
Patch v4

View in context: https://bugs.webkit.org/attachment.cgi?id=162855&action=review

Thank you for updating the patch!

> Source/WebCore/loader/appcache/ApplicationCache.cpp:169
> +    std::stable_sort(m_fallbackURLs.begin(), m_fallbackURLs.end(), &fallbackURLLongerThan);

Sorry, I didn't notice this at first. Please remove the "&" sign.
Comment 16 Leo Yang 2012-09-07 13:48:21 PDT
Created attachment 162860 [details]
Patch v5
Comment 17 WebKit Review Bot 2012-09-07 14:28:34 PDT
Comment on attachment 162860 [details]
Patch v5

Clearing flags on attachment: 162860

Committed r127922: <http://trac.webkit.org/changeset/127922>
Comment 18 WebKit Review Bot 2012-09-07 14:28:38 PDT
All reviewed patches have been landed.  Closing bug.