Skip to content

Commit cd10dca

Browse files
author
Anthony Nowell
committed
Safer content-type checking (fixed create dir)
1 parent f25a528 commit cd10dca

3 files changed

Lines changed: 47 additions & 16 deletions

File tree

examples/dir-make-del.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
dir-list.js
3+
4+
Example shows how to iterate over contents of a directory using Algorithmia's DataAPI.
5+
6+
*/
7+
8+
var algorithmia = require("../lib/algorithmia.js");
9+
var client = algorithmia.client(process.env.ALGORITHMIA_API_KEY);
10+
11+
console.log("Creating directory...");
12+
var testDir = client.dir("data://.my/node_client_test");
13+
testDir.create(function(err) {
14+
if(err) {
15+
console.log("Error: " + JSON.stringify(err));
16+
} else {
17+
console.log("Create directory succeeded. Deleting directory...");
18+
}
19+
20+
console.log("Deleting directory...");
21+
testDir.delete(function(err) {
22+
if(err) {
23+
return console.log("Error: " + JSON.stringify(err));
24+
}
25+
console.log("Delete directory succeeded.");
26+
});
27+
});
28+

lib/algorithmia.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@ AlgorithmiaClient = (function() {
6969
});
7070
res.on('end', function() {
7171
var body, buff, ct;
72-
ct = res.headers['content-type'] || accept;
73-
if (ct.startsWith('application/json')) {
74-
buff = chunks.join('');
75-
body = buff === '' ? {} : JSON.parse(buff);
76-
} else if (ct.startsWith('text/plain')) {
77-
body = chunks.join('');
78-
} else {
79-
body = Buffer.concat(chunks);
72+
ct = res.headers['content-type'];
73+
if (typeof ct === 'string') {
74+
if (ct.startsWith('application/json')) {
75+
buff = chunks.join('');
76+
body = buff === '' ? {} : JSON.parse(buff);
77+
} else if (ct.startsWith('text/plain')) {
78+
body = chunks.join('');
79+
} else {
80+
body = Buffer.concat(chunks);
81+
}
8082
}
8183
if (callback) {
8284
if (res.statusCode < 200 || res.statusCode >= 300) {

src/algorithmia.coffee

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ class AlgorithmiaClient
6666
chunks.push chunk
6767

6868
res.on 'end', ->
69-
ct = res.headers['content-type'] || accept
70-
if ct.startsWith('application/json')
71-
buff = chunks.join('')
72-
body = if buff == '' then {} else JSON.parse(buff)
73-
else if ct.startsWith('text/plain')
74-
body = chunks.join('')
75-
else
76-
body = Buffer.concat(chunks)
69+
ct = res.headers['content-type']
70+
if typeof ct == 'string'
71+
if ct.startsWith('application/json')
72+
buff = chunks.join('')
73+
body = if buff == '' then {} else JSON.parse(buff)
74+
else if ct.startsWith('text/plain')
75+
body = chunks.join('')
76+
else
77+
body = Buffer.concat(chunks)
7778

7879
if callback
7980
if res.statusCode < 200 || res.statusCode >= 300

0 commit comments

Comments
 (0)