Skip to content

Issue #7382 Use descriptive enums instead of booleans for MIMEClassifier::classifer - #7447

Merged
bors-servo merged 5 commits into
servo:masterfrom
leaexplores:master
Sep 18, 2015
Merged

bors-servo merged 5 commits into
servo:masterfrom
leaexplores:master

Conversation

@leaexplores

Copy link
Copy Markdown
Contributor

Hi guys i've done a small pass of refactor in the MIMEClassifier implementation. (See issue #7382 )

  • Moved the predicates to separate functions
  • Added a mimetype enum so we can compare them easily after calling MIMEClassifier::get_media_type

I hope it follows rust good pratices (care it's my first time doing rust).

Improvements and tips are welcome :).

Thanks for looking at it.

Review on Reviewable

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Aug 29, 2015
@highfive

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @SimonSapin (or someone else) soon.

@jdm

jdm commented Aug 30, 2015

Copy link
Copy Markdown
Member

Huh; the original issue was actually filed about the boolean arguments to the classify function (which ironically weren't touched in this PR). I'll need to think about whether these changes are also worthwhile improvements!

@leaexplores

Copy link
Copy Markdown
Contributor Author

Oh I see, you wanted to swap out https://github.com/ddrmanxbxfr/servo/blob/master/components/net/mime_classifier.rs#L29
and
https://github.com/ddrmanxbxfr/servo/blob/master/components/net/mime_classifier.rs#L30

For a ENUM too ?

While having the hands in refactoring this I could undergo with adding a ENUM such as MIMEClassifyOptions which could include the check_for_apache_bug and no_sniff checks ?

This way we'll be able to drop out both booleans and add more check later if needed.

If it seems a sane idea please confirm and i'll improve it quickly :)

Thanks!

@jdm

jdm commented Aug 30, 2015

Copy link
Copy Markdown
Member

@leaexplores

Copy link
Copy Markdown
Contributor Author

Hi JDM,

I've added the following so we get it right :
- Added a ENUM for the Check Apache Bug Flag
- Added a ENUM for the No Sniff Flag
- Updated broken test + tidy
- Added unit test when no sniff flag is at on skips the processing as per the specs to whatwg.
- Added unit test when apache check bug flag is at on to make sure it bahaves as per https://mimesniff.spec.whatwg.org/#rules-for-text-or-binary

Thanks for looking into it,
Comments/improvements would be appreciated.

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #7454) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added S-needs-rebase There are merge conflict errors. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Aug 31, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

Just rebased and squashed the fixup together.

It should help for merge :).

Thanks for looking into it.

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #7499) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added S-needs-rebase There are merge conflict errors. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Sep 2, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

@jdm ping ? Just rebased a 2nd time against upstream/master.

@jdm

jdm commented Sep 6, 2015

Copy link
Copy Markdown
Member

Sorry about the delay. I'll review this by Tuesday at the latest (Monday is a holdiay).

@leaexplores

Copy link
Copy Markdown
Contributor Author

k82h8x

@jdm

jdm commented Sep 8, 2015

Copy link
Copy Markdown
Member

-S-awaiting-review +S-needs-code-changes


Reviewed 4 of 4 files at r1, 1 of 1 files at r2.
Review status: all files reviewed at latest revision, 15 unresolved discussions, all commit checks successful.


components/net/mime_classifier.rs, line 17 [r1] (raw file):
Let's remove the MIME from the name.


components/net/mime_classifier.rs, line 22 [r1] (raw file):
Let's remove this entry, treat Unknown as SniffForUnknownType, and make get_media_type return Option<MIMEMediaType> instead. I don't think get_media_type and MIMEMediaType need to be public, either.


components/net/mime_classifier.rs, line 26 [r1] (raw file):
I think we can remove the MIME from the name.


components/net/mime_classifier.rs, line 31 [r1] (raw file):
Same here.


components/net/mime_classifier.rs, line 47 [r1] (raw file):
These changes switch the order so that the the no_sniff_flag takes priority, which contradicts the specification: https://mimesniff.spec.whatwg.org/#mime-type-sniffing-algorithm


components/net/mime_classifier.rs, line 56 [r1] (raw file):
nit: please keep this with the relevant code:

MIMEMediaType:Html =>
    //Implied in section 7.3, but flow is not clear
    self.feeds_classifier.classify(...)

components/net/mime_classifier.rs, line 103 [r1] (raw file):
Let's call this is_explicit_unknown


components/net/mime_classifier.rs, line 130 [r1] (raw file):
Please move this back to its original location so it's easier to see the changes :)


components/net/mime_classifier.rs, line 132 [r1] (raw file):
I think the logic will be easier to follow like this:

let should_sniff_scriptable = no_sniff_flag == MIMENoSniffFlag::OFF;
let sniffed = if should_sniff_scriptable {
    self.scriptable_classifier.classify(data)
} else {
    None
};
sniffed.or_else(...)

This will require adding #[derive(PartialEq)] to MIMENoSniffFlag.


components/net/mime_classifier.rs, line 142 [r1] (raw file):
Please move this back to its original location so it's easier to see the changes :)


components/net/resource_task.rs, line 75 [r1] (raw file):
nit: no need for the explicit types here.


components/net/resource_task.rs, line 107 [r1] (raw file):
&[u8] is more efficient than &Vec<u8>.


tests/unit/net/mime_classifier.rs, line 40 [r1] (raw file):
Let's call this test_sniff_with_flags


tests/unit/net/mime_classifier.rs, line 79 [r1] (raw file):
nit: this should only be indented four spaces past the fn at the start.


tests/unit/net/mime_classifier.rs, line 493 [r1] (raw file):
I'm not sure these tests that enable the apache bug flag are that useful unless they cause the sniffing behaviour to actually change. It looks like we get the same result, right? What about a test for something like an A/V file that would not usually be classified as plaintext?


Comments from the review on Reviewable.io

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Sep 8, 2015
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Sep 9, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

Review status: 1 of 5 files reviewed at latest revision, 5 unresolved discussions, some commit checks pending.


components/net/mime_classifier.rs, line 47 [r1] (raw file):
Good catch, (unknown, unkown), (application, application) and (,) needs to be checked before going into the no_sniff flag logic. Refactoring them into a is_unknown_type method.


components/net/mime_classifier.rs, line 56 [r1] (raw file):
This belongs to the FeedsClassifier related to the mislabeled fields.

I am not quite sure where you want to move this comment.


components/net/mime_classifier.rs, line 132 [r1] (raw file):
Mhm indeed. It'll remove one pattern matching. I'll look into doing this change :).


tests/unit/net/mime_classifier.rs, line 493 [r1] (raw file):
Mhmh, it's the usual sniffing for mislabeled resource.

We had cases where the resource was interpreted as mislabeled / unknown but no test for the flags at on. They were all done with the flags at off even though they are supposed to have the same behavior.

If a resource is unknown mimetype OR comes from Apache Bug Flag, need to interpret it as mislabeled.

As I read they have case on UTF-8, 16, 16le bom's.

Section 7.2 for mislabeled rules.
Section 7 for the algorithm

https://mimesniff.spec.whatwg.org/#check-for-apache-bug-flag

If the check-for-apache-bug flag is set, execute the rules for distinguishing if a resource is text or binary and abort these steps.


Comments from the review on Reviewable.io

@leaexplores

Copy link
Copy Markdown
Contributor Author

Review status: 1 of 5 files reviewed at latest revision, 5 unresolved discussions, all commit checks successful.


components/net/mime_classifier.rs, line 22 [r1] (raw file):
SniffForUnknownType is basically the rule we were missing before the no_sniff check.

It shouldn't be in this enum... I removed it and fixed the original algorithm in the classify function.

Fixing the scope of get_media_type. It should be indeed private :).
Ditto for the Option modification, great idea.


Comments from the review on Reviewable.io

@jdm jdm added the S-needs-code-changes Changes have not yet been made that were requested by a reviewer. label Sep 9, 2015
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Sep 12, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

Review status: 2 of 4 files reviewed at latest revision, 3 unresolved discussions, some commit checks pending.


tests/unit/net/mime_classifier.rs, line 493 [r1] (raw file):
Indeed. I added a test with the mp4 file we already had in the project.


tests/wpt/css-tests/css21_dev/html4/border-left-color-125.htm, line 0 [r3] (raw file):
Seems unrelated, not sure how Reviewable says this... If I look in the Github diff viewer this file is not removed :. Maybe related because I rebased servo/master and then squashed the commits together ?


Comments from the review on Reviewable.io

@jdm

jdm commented Sep 12, 2015

Copy link
Copy Markdown
Member

-S-awaiting-review -S-needs-rebase +S-awaiting-answer


Reviewed 3 of 3 files at r4.
Review status: all files reviewed at latest revision, 3 unresolved discussions, all commit checks successful.


tests/unit/net/mime_classifier.rs, line 536 [r4] (raw file):
I don't understand the expected output here; the video classifier shouldn't be running when the apache bug flag is enabled, right?


tests/wpt/css-tests/css21_dev/html4/border-left-color-125.htm, line 0 [r3] (raw file):
This is a known reviewable bug that was recently fixed, so nevermind.


Comments from the review on Reviewable.io

@jdm jdm added S-awaiting-answer Someone asked a question that requires an answer. and removed S-awaiting-review There is new code that needs to be reviewed. S-needs-rebase There are merge conflict errors. labels Sep 12, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

Review status: all files reviewed at latest revision, 2 unresolved discussions, all commit checks successful.


tests/unit/net/mime_classifier.rs, line 536 [r4] (raw file):
Good call.

All the tests with Apache Flag ON needs to have a supplied type to trigger it correctly.

I should get Application/Octet-Stream from a video/mp4 file when I have the flag at on....

This means I have a bug in my tests regarding apache flag on.

While fixing them I found that utf16-be and utf16-le were not responding the good type. We had application/octet-stream instead of text/plain.

I fixed them in BinaryOrPlaintextClassifier.

I hope it goes ok with

If length is greater than or equal to 2 and the first 2 bytes of the resource header are equal to 0xFE 0xFF (UTF-16BE BOM) or 0xFF 0xFE (UTF-16LE BOM), the computed MIME type is "text/plain". Abort these steps.


Comments from the review on Reviewable.io

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Sep 13, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

We'll get it right @jdm once it looks ok let me know i'll rebase commits to not polute the git log.

@jdm jdm removed the S-awaiting-answer Someone asked a question that requires an answer. label Sep 13, 2015
@leaexplores

Copy link
Copy Markdown
Contributor Author

Ping? @jdm

@jdm

jdm commented Sep 18, 2015

Copy link
Copy Markdown
Member

@bors-servo: r+
Great work! Thank you for the PR!


Reviewed 1 of 1 files at r5, 2 of 2 files at r6.
Review status: :shipit: all files reviewed at latest revision, all discussions resolved, all commit checks successful.


Comments from the review on Reviewable.io

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit cc44448 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Sep 18, 2015
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit cc44448 with merge 8a8204f...

bors-servo pushed a commit that referenced this pull request Sep 18, 2015
Issue #7382 Use descriptive enums instead of booleans for MIMEClassifier::classifer

Hi guys i've done a small pass of refactor in the MIMEClassifier implementation. (See issue #7382 )
   - Moved the predicates to separate functions
   - Added a mimetype enum so we can compare them easily after calling MIMEClassifier::get_media_type

I hope it follows rust good pratices (care it's my first time doing rust).

Improvements and tips are welcome :).

Thanks for looking at it.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7447)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, gonk, linux-dev, linux-rel, mac-dev-ref-unit, mac-rel-css, mac-rel-wpt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants