Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Avoid analyzing /favicon.ico multiple times #430

Merged
merged 1 commit into from
Aug 23, 2017

Conversation

qzhou1607-zz
Copy link
Contributor

CDP already sends request to /favicon.ico automatically.
So when forcing to load favicon, we shouldn't send request
to favicon.ico again.

Fix #427

@@ -488,7 +488,11 @@ export class Connector implements IConnector {
* * uses `favicon.ico` and the final url after redirects.
*/
private async getFavicon(element?: AsyncHTMLElement) {
const href = element ? element.getAttribute('href') : '/favicon.ico';
if (!element || !element.getAttribute('href')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also the case when href is empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I don't think getFavicon should be responsible of checking these things, and instead, and at this point:

https://github.com/sonarwhal/sonar/blob/941d439affca16e3af1b0df90e739ee746df2313/src/lib/connectors/shared/remote-debugging-connector.ts#L329-L331

all the checks should be made to determine if valid favicon was specified or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alrra If a page doesn't have favicon in the root directory and /favicon.ico returns 404. Do we still want to list all the warnings/errors under this url?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want to list all the warnings/errors under this url?

@qzhou1607 Sorry, I don't quite understand the question. Can you be more specific, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alrra Like http://www.example.com/favicon.ico returns 404. In the output of running sonar against http://www.example.com, we listed:

http://www.example.com: 1 issues
...
http://www.example.com/favicon.ico: 1 issues
...

But http://www.example.com/favicon.ico doesn't exist, it looks a little weird to still check its response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But http://www.example.com/favicon.ico doesn't exist, it looks a little weird to still check its response.

Yes. However, do make sure that CDP aligns with jsdom.

@qzhou1607-zz
Copy link
Contributor Author

@alrra @sarvaje I pushed up some more changes. I've talked to you both about this issue and please let me know your thought about the current solution. Here is a summary:

The cause of the duplicate messages : CDP requests favicon from the root directory automatically if no favicon is present in the html. In our previous code we didn't set the _faviconLoaded flag to true in this scenario, so getFavicon got called and fetch::end event was emitted twice.

Changes in the current solution :

  1. If cdp has sent request to /favicon.ico already, set the _faviconLoaded flag to true as well.
  2. getFavicon doesn't need to request /favicon.ico. This function should have never been reached in the first place if favicon is in the root directory.
  3. getFavicon shouldn't include the code to check if the element exists. If the element doesn't exist, it shouldn't reach getFavicon.

Copy link
Member

@molant molant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qzhou1607 on top of the comment I left, can you fix the conflicts?

const hasFaviconLinkTag = hasAttributeWithValue(data.element, 'link', 'rel', 'icon');
// If favicon is not present in the html, CDP will send a request to get favicon from the root directory.
// We should set the tag to true in this case to avoid fetching favicon twice.
const hasFaviconInRootDir = data.request.url.endsWith('/favicon.ico');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. I could very well have the following url https://sonarwhal.com/images/favicon.ico and the favicon will not be in the root dir.
We should add a test for this case too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the name isn't appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was talking with Qing, and you are right, if it isn't in the root, hasFaviconLinkTag should be true, she is going to change how to calculate hasFaviconInRootDir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@molant I think your comment is correct. There is a scenario that /images/favicon.ico is included in the html as the resource a img instead of a link tag in the head (rel set as icon). In this scenario we shouldn't set the flag to true since the favicon image isn't included in the page in an appropriate way. We should still go into getFavicon trying to manually download the favicon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will work on adding more tests too :)

@qzhou1607-zz
Copy link
Contributor Author

qzhou1607-zz commented Aug 21, 2017

@molant @alrra I added some tests to make sure that there are no duplicate requests to favicon, but some of them are failing in cdp due to a difference in the headless mode - It seems that cdp in the headless mode doesn't send requests to /favicon.ico automatically. Should I skip these tests for cdp too?

It might be relevant to this issue reported.

@molant
Copy link
Member

molant commented Aug 21, 2017

It seems that cdp in the headless mode doesn't send requests to /favicon.ico automatically
Then I think we should download it. If by the end of the traversing nothing has happened with should manually download it.

@qzhou1607-zz
Copy link
Contributor Author

I modify the code and use an approach suggested by @sarvaje before: Ignore automatic requests(and responses) from cdp to /favicon.ico to avoid emitting duplicates events. This way favicon is downloaded only once from the root and the tests work in both the headless and regular cdp. Ready for review @sarvaje @molant @alrra .

@@ -226,7 +226,7 @@ class JSDOMConnector implements IConnector {
* * uses `favicon.ico` and the final url after redirects.
*/
private async getFavicon(element?: HTMLElement) {
const href = element ? element.getAttribute('href') : '/favicon.ico';
const href = (element && element.getAttribute('href')) || '/favicon.ico';
Copy link
Contributor

@sarvaje sarvaje Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to do the assignment in this way instead of the old way?

In terms of readability I prefer the old way.

Copy link
Contributor Author

@qzhou1607-zz qzhou1607-zz Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarvaje This is due to the scenario when element exists but href is an empty string. We still want to send out a request to /favicon.ico in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, ok!

private async getFavicon(element?: AsyncHTMLElement) {
const href = element ? element.getAttribute('href') : '/favicon.ico';
private async getFavicon(element: AsyncHTMLElement) {
const href = (element && element.getAttribute('href')) || '/favicon.ico';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as before


await runTest(t, connectorBuilder, serverConfig);

t.plan(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.plan is not necessary here. Take a look to this

It makes sense when you need to be sure that a block of code with t.is (or another test) is run.
For example:

t.plan(2);
try {
    await runTest(t, connectorBuilder, serverConfig);
    t.is(t.context.sonar.emitAsync.withArgs('fetch::end').callCount, 1);
    t.is(t.context.sonar.emitAsync.withArgs('fetch::end').args[0][1].request.url, faviconInLinkElementDir);
}catch(e){}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarvaje Done. Thanks for the information!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants