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

Charset problem #661

Open
romuloinnocencio opened this issue Jun 8, 2015 · 15 comments
Open

Charset problem #661

romuloinnocencio opened this issue Jun 8, 2015 · 15 comments

Comments

@romuloinnocencio
Copy link

I'm having problems with charset, naturally the codes in .aspx need (and I don't know ecxacly why) the ISO-8859-1. Prints someting like this: *capacita��o de *

        browserSync: {
            geral: {
                bsFiles: {
                    src: [
                        '**/*.html',
                        '**/*.css',
                        '**/*.min.js',
                        '**/*.aspx',
                        '**/*.master',
                        '**/*.ashx'
                    ]
                },
                options: {
                    watchTask: true,
                    startPath: "/default.aspx",
                    proxy: config.host + ':' + config.port,
                    notify: false,
                    open: false
                }
            }
        },

With aspx, ours programmers insert the content of news, in web.config I found this.
http://prntscr.com/7eitat

But if i change the value of charset here, nothing more works.

@andreldm
Copy link

andreldm commented Jul 7, 2015

Same problem here, we are also stuck with ISO-8859-1.

@shinnn
Copy link
Contributor

shinnn commented Jul 20, 2015

None of matter with you but http://prnt.sc/ is too slow... Just pasting the image to the Github issue comment is easier and more useful. https://help.github.com/articles/issue-attachments/

@andreldm
Copy link

I'm trying to nail this down, so far the problem is confirmed to be upstream, related to the foxy proxy, and there is even an open issue regarding encoding problems:
shakyShane/foxy#13

@andreldm
Copy link

Here's a small gist for testing:
https://gist.github.com/andreldm/36a16b85d45a9fefceb7

@micheltlutz
Copy link

Any solution? I am using iso-8859-1 unfortunately I can not change the application Chasert it causes problems in other services.

@d2s
Copy link

d2s commented Jul 11, 2016

Similar problem when using proxy mode of BrowserSync.

Application on the background is made with Java, and sadly can't change the existing content encoding settings from there.

Now text displays like:
2016-07-11-browsersync-proxy-mode-bug-report-example

Without proxy (and without BrowserSync...), it looks like:
2016-07-11-browsersync-proxy-mode-bug-report-example-without-proxy

@FFirmenich
Copy link

please fix this one

@jzatt
Copy link

jzatt commented May 9, 2017

I'm having this problem too. Any fix available?

@Yankee93
Copy link

Yankee93 commented Jun 7, 2017

@shakyShane Please fix!

@bunam
Copy link

bunam commented Jul 12, 2017

me too 👅

@CaballoCarpiano
Copy link

Any news or workaround on this problem?

@skotniczny
Copy link

Based on @kokarn's and @youngzhaosignifyd's examples and with the help of iconv-lite, plus using proxyRes option I am successfully using this script:

proxyRes: [
  (proxyRes, req, res) => {
    if( proxyRes.headers && proxyRes.headers["content-type"] &&
        proxyRes.headers["content-type"].match("text/html") ) {
            
      const _end = res.end
      const _writeHead = res.writeHead
      let writeHeadArgs
      let buffer = new Buffer("")

      proxyRes.on("data", (chunk) => {
        buffer = Buffer.concat([buffer, chunk])
      })

      res.write = () => {};
      res.writeHead = (...args) => {writeHeadArgs = args}

      res.end = () => {
        _writeHead.apply(res, writeHeadArgs)
        _end.call(res, iconv.decode(buffer, "iso-8859-2"))
      }
    }
  }
]

@awaigand
Copy link

awaigand commented Feb 14, 2019

My solution, strongly based on @skotniczny solution. I had to set a content-type header, because my backend insisted on the broken encoding.
You have to have iconv-lite and browser-sync installed.
Note: We have windows-1252 encoding in the backend.

var browserSync = require("browser-sync").create();
var iconv = require("iconv-lite");

/**
 * This example will serve files from the './app' directory
 * and will automatically watch for html/css/js changes
 */
browserSync.init({
  watch: false,
  proxy: {
    target: "http://localhost/WorkServer/admin",
    proxyRes: [
      (proxyRes, req, res) => {
        if (
          proxyRes.headers &&
          proxyRes.headers["content-type"] &&
          proxyRes.headers["content-type"].match("text/html")
        ) {
          const _end = res.end;
          const _writeHead = res.writeHead;
          let writeHeadArgs;
          let buffer = new Buffer("");

          proxyRes.on("data", chunk => {
            buffer = Buffer.concat([buffer, chunk]);
          });

          res.write = () => {};
          res.writeHead = (...args) => {
            writeHeadArgs = args;
          };

          res.end = () => {
            _writeHead.apply(res, writeHeadArgs);
            _end.call(res, iconv.decode(buffer, "win1252"));
          };
        }
      },
      function(res) {
        if (
          res.headers &&
          res.headers["content-type"] &&
          res.headers["content-type"].match("text/html")
        )
        res.headers["content-type"] = "text/html; charset=utf-8";
      }
    ]
  }
});

@therealgilles
Copy link

I'm trying to use the code above but I'm getting the following error:

node:_http_server:279
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
    ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined

Any idea what could be missing in my case?

@jwv
Copy link

jwv commented Apr 21, 2022

I'm trying to use the code above but I'm getting the following error:

node:_http_server:279
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
    ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined

Any idea what could be missing in my case?

Fixed version. Tested node v17.9.0.

var browserSync = require("browser-sync").create();
var iconv = require("iconv-lite");

browserSync.init({
  watch: false,
  proxy: {
    target: "http://mydomain.local",
    proxyRes: [
      (proxyRes, req, res) => {
        if (
          proxyRes.headers &&
          proxyRes.headers["content-type"] &&
          proxyRes.headers["content-type"].match("text/html")
        ) {
          const _end = res.end;
          const _writeHead = res.writeHead;
          let buffer = Buffer.from("");

          proxyRes.on("data", chunk => {
            buffer = Buffer.concat([buffer, chunk]);
          });

          res.write = () => {};
          res.writeHead = (...args) => {
            _writeHead.apply(res, args);
          };

          res.end = () => {
            _end.call(res, iconv.decode(buffer, "iso-8859-1"));
          };
        }
      },
      function(res) {
        if (
          res.headers &&
          res.headers["content-type"] &&
          res.headers["content-type"].match("text/html")
        )
        res.headers["content-type"] = "text/html; charset=utf-8";
      }
    ]
  }
});

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

No branches or pull requests