Skip to content

Commit 0bb26f4

Browse files
committed
Fix broken HTTP support.
HTTP image support was completely broken. Added check for updated Http.IncomingMessage.headers property in addition to checking for getHeader(). Also explicitly set the encoding option to 'null' so that the body value of the respons is always returned as a buffer preventing the parsing libraries from failing to read Encoding headers properly. Added three test cases to verify the fixes (one for each img type). They are pointed to the github raw test images, which is safe for now.
1 parent d975739 commit 0bb26f4

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Diff for: node-pixels.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,20 @@ module.exports = function getPixels(url, type, cb) {
149149
})
150150
}
151151
} else if(url.indexOf('http://') === 0 || url.indexOf('https://') === 0) {
152-
request(url, function(err, response, body) {
152+
request({url:url, encoding:null}, function(err, response, body) {
153153
if(err) {
154154
cb(err)
155155
return
156156
}
157-
type = type || response.getHeader('content-type')
157+
158+
type = type;
159+
if(!type){
160+
if(response.getHeader !== undefined){
161+
type = response.getHeader('content-type');
162+
}else if(response.headers !== undefined){
163+
type = response.headers['content-type'];
164+
}
165+
}
158166
if(!type) {
159167
cb(new Error('Invalid content-type'))
160168
return

Diff for: test/test.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,46 @@ test("get-pixels-buffer", function(t) {
128128
test_image(t, pixels)
129129
t.end()
130130
})
131-
})
131+
})
132+
133+
test("get-url png img", function(t) {
134+
var url = "https://raw.githubusercontent.com/scijs/get-pixels/master/test/test_pattern.png";
135+
getPixels(url, function(err, pixels){
136+
if(err) {
137+
console.log("Error:", err);
138+
t.error(err, "failed to read web image data");
139+
t.end();
140+
return;
141+
}
142+
test_image(t, pixels);
143+
t.end();
144+
});
145+
});
146+
147+
test("get-url jpg img", function(t) {
148+
var url = "https://raw.githubusercontent.com/scijs/get-pixels/master/test/test_pattern.jpg";
149+
getPixels(url, function(err, pixels){
150+
if(err) {
151+
console.log("Error:", err);
152+
t.error(err, "failed to read web image data");
153+
t.end();
154+
return;
155+
}
156+
test_image(t, pixels);
157+
t.end();
158+
});
159+
});
160+
161+
test("get-url gif img", function(t) {
162+
var url = "https://raw.githubusercontent.com/scijs/get-pixels/master/test/test_pattern.gif";
163+
getPixels(url, function(err, pixels){
164+
if(err) {
165+
console.log("Error:", err);
166+
t.error(err, "failed to read web image data");
167+
t.end();
168+
return;
169+
}
170+
test_image(t, pixels.pick(0));
171+
t.end();
172+
});
173+
});

0 commit comments

Comments
 (0)