Skip to content

Webcmdlets should parse the <html><head><meta charset="foo"> attribute for the correct encoding if not in http header #3267

Description

@SteveL-MSFT

Some websites do not populate the charset property of the content-type header so characters aren't rendered correctly. Suggestion is to expose a -charset parameter, however the user still needs to know the expected charset. Advanced users today can do the encoding translation in script. utf-8 probably works in most cases, so not entirely sure how useful this will be to expect the user to know ahead of time the correct charset.

See discussion from #3126 for details on how this came about

Activity

  1. iSazonov commented on Mar 6, 2017

    @iSazonov
    Collaborator

    I believe we should discuss here a default encoding for Web cmdlets.
    Currently we use RFC 2616 "ISO-8859-1"
    In fact, browsers for a long time use Windows-1252.
    And HTML5 default is UTF-8.

    Perhaps we should also use Windows 1252.

  2. iSazonov commented on Mar 7, 2017

    @iSazonov
    Collaborator

    I looked Web cmdlets and found that we use ContentType parameter to encode a request (Content-Type header can contains charset value). If we don't specify a charset in ContenType we use default charset ISO-8859-1.
    For decoding a response we use a charset from ContentType of the response. If a server return no ContentType we use the same default charset ISO-8859-1.

    So we should treat -CharSet as -ResponseCharSet.

    After some thought, I believe that using Windows 1252 as default is obsolete and we should aim at HTML5 and UTF-8 as defaults. https://w3techs.com/technologies/details/ml-html5/all/all

  3. added
    Committee-ReviewedPS-Committee has reviewed this and made a decision
    and removed
    Review - CommitteeThe PR/Issue needs a review from the PowerShell Committee
    on Mar 9, 2017
  4. SteveL-MSFT commented on Mar 9, 2017

    @SteveL-MSFT
    MemberAuthor

    @PowerShell/powershell-committee reviewed this and agree this is an issue for customers. proposal is to parse the HTTP header first, if charset is in content-type, we use that. otherwise if content-type is html, we parse <meta charset="X"> for the charset attribute and use that

  5. changed the title [-]Expose -CharSet parameter to webcmdlets[/-] [+]Webcmdlets should parse the <html><head><meta charset="foo"> attribute for the correct encoding if not in http header[/+] on Mar 9, 2017
  6. iSazonov commented on Mar 9, 2017

    @iSazonov
    Collaborator

    In Windows Powershell we use Internet Explorer to parse HTML. What portable parser we can use in PowerShell Core?

    And if HTML don't contain <meta charset="foo"> ? What defaults we should use for fallback?

  7. SteveL-MSFT commented on Mar 9, 2017

    @SteveL-MSFT
    MemberAuthor

    Ilya (@iSazonov) the proposal is that we don't rely on any browser for the html parsing (if complete parsing is needed, I still think it would make more sense in a convertfrom-html cmdlet). I think "best effort" for this is sufficient (perhaps even just a regular expression) to cover the majority of cases and we wouldn't worry about malformed html.

  8. iSazonov commented on Mar 9, 2017

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) Original Windows web cmdlet returns ParsedHtml : mshtml.HTMLDocumentClass - we want lost the functionality?

  9. SteveL-MSFT commented on Mar 9, 2017

    @SteveL-MSFT
    MemberAuthor

    Ilya (@iSazonov) .ParshedHtml relied on Internet Explorer. I don't think we can have a dependency on any particular web browser in the webcmdlets.

    To answer your other question I missed, if <meta charset> doesn't exist and charset isn't specified in the content-type HTTP header, then we do what we do today which is assume ISO-8859-1

  10. iSazonov commented on Mar 9, 2017

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT)

    .ParshedHtml relied on Internet Explorer

    If we use any ported library for HTML parsing we will solve this Issue, get ParshedHtml ported, as well as get a base for ConvertFrom-HTML.

    I wonder about ISO-8859-1. Why we do not want to accept the new standard HTML5?

  11. 48 remaining items

  12. added this to the milestone on Nov 3, 2017
  13. SteveL-MSFT commented on Nov 3, 2017

    @SteveL-MSFT
    MemberAuthor

    Any further work on this I'm deferring to 6.1.0

  14. MSAdministrator commented on Jul 15, 2018

    @MSAdministrator

    Submitted RFC for the creation of ConvertFrom-Html here: PowerShell/PowerShell-RFC#137

  15. iSazonov commented on Jul 16, 2020

    @iSazonov
    Collaborator

    We get new HttpClient with .Net 3+ so I remove the label.

  16. ghost added
    In-PRIndicates that a PR is out for the issue
    on Dec 8, 2022
  17. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Jan 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions