Skip to content

0.32.3 - intercept{String,Buffer}Protocol mimeType not transfered to content-type response header #2788

Closed

Description

@neuroo

Using interceptStringProtocol, the mimeType of the object passed to the callback is not transferred back to the renderer's HTTP response headers.

For example, my renderer's application uses jQuery to make and XHR request and it's supposed to get JSON. jQuery inspects the HTTP response headers to get the content-type:

$.get("/api/get-json", function __cb(data) {
  if (typeof data !== 'object') throw 'Unexpected type';
  // process the object... 
});

The URL /api/get-json is intercepted as follow:

app.on('ready', function() {
  var protocol = require("protocol");
  protocol.interceptStringProtocol('http', function(request, callback) {
    if (request.url.indexOf("/api/get-json") >= 0) {
      // Send back some JSON
      callback({
        mimeType: "application/json",
        data: '{"value": 1}',
        charset: 'utf-8'
      });
    }
    else {
     // get some contents
    }
  });
  // create the window and load my url
});

Since no HTTP headers are passed back to the renderer, the client-side cannot decide to go ahead and JSON.parse the string it receives. It seems the HTTP response headers are intentionally left out:
https://github.com/atom/electron/blob/10b1ea724445d02563b2c37a99e43d4909f36ac9/atom/browser/net/js_asker.h#L76

Just as a dummy test, I forced the HTTP response headers to contain content-type: application/json my modifying the previously linked method and the behavior of the client-side application becomes identical as any other web browser: the string gets parsed as JSON and an object is sent back.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions