Skip to content

Update HtmlParser.m to handle the crashing#30

Closed
congsun wants to merge 1 commit into
nolanw:masterfrom
congsun:master
Closed

Update HtmlParser.m to handle the crashing#30
congsun wants to merge 1 commit into
nolanw:masterfrom
congsun:master

Conversation

@congsun

@congsun congsun commented Jul 17, 2015

Copy link
Copy Markdown

When there are several strange character, using the contentType to encode can return the initialString to be nil and thus crash all the parsing process since then(For my case is UTF8). So add the hard-coded ASCII contentType to handle the crash.

For example, try
http://www.macrumors.com/2015/07/16/apple-standardized-e-sim-cards/

When there are several strange character, using the contentType to encode can return the initialString to be nil and thus crash all the parsing process since then(For my case is UTF8). So add the hard-coded ASCII contentType to handle the crash.
@congsun

congsun commented Jul 17, 2015

Copy link
Copy Markdown
Author

It seems like the strange characters are gone from the website I provided. But it still may cause crash if the website has the characters that the specific content-type cannot handle

@nolanw

nolanw commented Jul 26, 2015

Copy link
Copy Markdown
Owner

@congsun can you show me how you were attempting to create the document that was causing the crash? The initialString shouldn't be nil by the time it gets to HTMLParser, so I've left that crash in to loudly point out where we're doing something unexpected.

Also, the ASCII string encoding expects clean 7-bit ASCII, and decoding will fail if any byte has the high bit set, so I'm not sure the solution you suggest here is generally applicable.

@congsun

congsun commented Jul 26, 2015

Copy link
Copy Markdown
Author

Hi. I was just use the regular way to create the document and somehow the app just crashed totally. The website that cause a crash was the one I provided before, and somehow they fixed it. However, before they fix that problem and when I try to open the webpage with chrome, there are several question mark inside a triangle shows up. My solution is not perfect, I just want you to notice this annoying problem and maybe we can fix it :)

@nolanw

nolanw commented Jul 26, 2015

Copy link
Copy Markdown
Owner

Oh, I was misreading your change, my apologies! I agree we shouldn't be crashing there.

My best guess is that the Content-Type header specified a known charset, but the data was not actually encoded using that charset. We should make a unit test for this, then decide how to fix it.

I don't think falling back to ASCII makes sense as that'll refuse to decode if any byte has its high bit set (NSASCIIStringEncoding is strictly 7-bit ASCII). If you look at the DeterminedStringEncodingForData() function in HTMLEncoding.m, we try to fall back to win1252 (which is the fallback in the spec), and finally we fall back to iso8859-1 as our Encoding of Last Resort (any set of bytes decodes as iso8859-1).

I'm having a hard time finding any suggestions in the spec about how to deal with a failed decoding. I'll check again to see if it has a suggestion, otherwise we should just do whatever browsers do.

Ultimately you've pointed out that an assumption we make in HTMLReader – that the Content-Type's charset will always work – is hilariously flawed.

@nolanw

nolanw commented Jul 29, 2015

Copy link
Copy Markdown
Owner

@congsun I decided to try treating an incorrect Content-Type's charset as if it didn't specify any charset at all. I just pushed my efforts to the master branch, in commit 5056ddd. What do you think?

@congsun

congsun commented Jul 29, 2015

Copy link
Copy Markdown
Author

Great, let me look at that 👍

@congsun

congsun commented Jul 29, 2015

Copy link
Copy Markdown
Author

@nolanw Hi, I checked out your code, it seem like it would still crash when the content-type is still correct, say UTF8, but there are several unidentifiable characters in the content of that data. I don't know if my understanding is wrong, but it seems like we would need a higher error-tolerant encoding way to deal with that if the encoding string returns nil.

@congsun

congsun commented Jul 29, 2015

Copy link
Copy Markdown
Author

I will try to find a web page that has those tricky characters :)

@nolanw

nolanw commented Jul 29, 2015

Copy link
Copy Markdown
Owner

I think the unit test I added in that commit covers that particular case? If there are unidentifiable characters in the content then I'd say the Content-Type wasn't correct.

I guess we do still crash if the data starts with a BOM but isn't actually encoded as the BOM suggests. Not sure what to do there. It's the same problem really: being "certain" about the wrong encoding.

@congsun

congsun commented Jul 29, 2015

Copy link
Copy Markdown
Author

I agree, nice work!!!

@nolanw

nolanw commented Jul 29, 2015

Copy link
Copy Markdown
Owner

Cool, I'll make a new release in a day or two. I wanna think more about tricky BOMs.

@nolanw

nolanw commented Jul 29, 2015

Copy link
Copy Markdown
Owner

Yep, I'm happy with this.

HTMLReader 0.8.1 includes this fix as commit 5056ddd. Thanks again @congsun!

@nolanw nolanw closed this Jul 29, 2015
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.

2 participants