{"action":"created","issue":{"url":"https://api.github.com/repos/ToothlessGear/node-gcm/issues/97","labels_url":"https://api.github.com/repos/ToothlessGear/node-gcm/issues/97/labels{/name}","comments_url":"https://api.github.com/repos/ToothlessGear/node-gcm/issues/97/comments","events_url":"https://api.github.com/repos/ToothlessGear/node-gcm/issues/97/events","html_url":"https://github.com/ToothlessGear/node-gcm/issues/97","id":58322778,"number":97,"title":"Not working in proxy","user":{"login":"mikkysati","id":6641129,"avatar_url":"https://avatars.githubusercontent.com/u/6641129?v=3","gravatar_id":"","url":"https://api.github.com/users/mikkysati","html_url":"https://github.com/mikkysati","followers_url":"https://api.github.com/users/mikkysati/followers","following_url":"https://api.github.com/users/mikkysati/following{/other_user}","gists_url":"https://api.github.com/users/mikkysati/gists{/gist_id}","starred_url":"https://api.github.com/users/mikkysati/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikkysati/subscriptions","organizations_url":"https://api.github.com/users/mikkysati/orgs","repos_url":"https://api.github.com/users/mikkysati/repos","events_url":"https://api.github.com/users/mikkysati/events{/privacy}","received_events_url":"https://api.github.com/users/mikkysati/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ToothlessGear/node-gcm/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-02-20T08:34:18Z","updated_at":"2015-12-07T23:14:16Z","closed_at":null,"body":"I am using this behind proxy and have set the environment variables http_proxy and https_proxy. But whenever I try to run the server I get this error:\r\n\r\n[Error: tunneling socket could not be established, cause=Parse Error] code: 'ECONNRESET'"},"comment":{"url":"https://api.github.com/repos/ToothlessGear/node-gcm/issues/comments/162701641","html_url":"https://github.com/ToothlessGear/node-gcm/issues/97#issuecomment-162701641","issue_url":"https://api.github.com/repos/ToothlessGear/node-gcm/issues/97","id":162701641,"user":{"login":"eladnava","id":4394189,"avatar_url":"https://avatars.githubusercontent.com/u/4394189?v=3","gravatar_id":"","url":"https://api.github.com/users/eladnava","html_url":"https://github.com/eladnava","followers_url":"https://api.github.com/users/eladnava/followers","following_url":"https://api.github.com/users/eladnava/following{/other_user}","gists_url":"https://api.github.com/users/eladnava/gists{/gist_id}","starred_url":"https://api.github.com/users/eladnava/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eladnava/subscriptions","organizations_url":"https://api.github.com/users/eladnava/orgs","repos_url":"https://api.github.com/users/eladnava/repos","events_url":"https://api.github.com/users/eladnava/events{/privacy}","received_events_url":"https://api.github.com/users/eladnava/received_events","type":"User","site_admin":false},"created_at":"2015-12-07T23:14:15Z","updated_at":"2015-12-07T23:14:15Z","body":"@pablonazareno @mikkysati I tracked down the source of the problem when supplying a `proxy` and found a solution.\r\n\r\n### The Problem\r\n\r\n1. On [sender.js#86](https://github.com/ToothlessGear/node-gcm/blob/e71405c812892b63b8d673f071874a515409c243/lib/sender.js#L86), we set a `Content-length` header. \r\n2. When setting proxy, `request` creates an `HTTP CONNECT` request through the proxy to establish a HTTPS tunnel, but sends the `Content-length` header along with this request, which is incorrect behavior. The result is that the GCM server is waiting for us to send data in the `CONNECT` request, when all we wanted to do was open an HTTPS tunnel:\r\n![fail](https://cloud.githubusercontent.com/assets/4394189/11642704/0b99427e-9d48-11e5-97e9-cf01ddf123b2.png)\r\n\r\n3. The GCM server responds with an error code.\r\n\r\nThis is caused by the following code, which constructs the `request`:\r\n\r\n```js\r\n    var post_options = {\r\n        method: 'POST',\r\n        headers: {\r\n            'Content-Type': 'application/json',\r\n            'Content-length': Buffer.byteLength(requestBody, 'utf8'),\r\n            'Authorization': 'key=' + this.key\r\n        },\r\n        uri: Constants.GCM_SEND_URI,\r\n        body: requestBody\r\n    };\r\n```\r\n\r\nSince we hard-coded the `Content-length` header, `request` sends it with every request, even the `CONNECT`.\r\n\r\n### The Solution\r\n\r\nThe solution is to let `request` construct these headers when it's time by changing the way we configure the `request` as follows:\r\n\r\n```js\r\n    var post_options = {\r\n        method: 'POST',\r\n        headers: {\r\n            'Authorization': 'key=' + this.key\r\n        },\r\n        uri: Constants.GCM_SEND_URI,\r\n        json: JSON.parse(requestBody)\r\n    };\r\n```\r\n\r\nThe main difference is that we no longer hard-code the content headers, and supply the JSON payload in the `json` parameter.\r\n\r\nI will submit a PR with this fix when I find some free time.\r\n\r\n@hypesystem fyi."}}