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

Enable running iodocs under https #36

Closed
wants to merge 9 commits into from
79 changes: 55 additions & 24 deletions app.js
Expand Up @@ -78,7 +78,46 @@ fs.readFile('public/data/apiconfig.json', 'utf-8', function(err, data) {
}
});

var app = module.exports = express.createServer();
//
// Determine if we should launch as http/s and get keys and certs if needed
//

var app, httpsKey, httpsCert;

if (config.https && config.https.on && config.https.keyPath && config.https.certPath) {
console.log("Starting secure server (https)");

// try reading the key and cert files, die if that fails
try {
httpsKey = fs.readFileSync(config.https.keyPath);
}
catch (err) {
console.error("Failed to read https key", config.https.keyPath);
console.log(err);
process.exit(1);
}
try {
httpsCert = fs.readFileSync(config.https.certPath);
}
catch (err) {
console.error("Failed to read https cert", config.https.certPath);
console.log(err);
process.exit(1);
}

app = module.exports = express.createServer({
key: httpsKey,
cert: httpsCert
});

}
else if (config.https && config.https.on) {
console.error("No key or certificate specified.");
process.exit(1);
}
else {
app = module.exports = express.createServer();
}

if (process.env.REDISTOGO_URL) {
var rtg = require("url").parse(process.env.REDISTOGO_URL);
Expand Down Expand Up @@ -277,6 +316,8 @@ function processRequest(req, res, next) {

var reqQuery = req.body,
params = reqQuery.params || {},
content = reqQuery.requestContent || '',
contentType = reqQuery.contentType || '',
methodURL = reqQuery.methodUri,
httpMethod = reqQuery.httpMethod,
apiKey = reqQuery.apiKey,
Expand Down Expand Up @@ -315,13 +356,9 @@ function processRequest(req, res, next) {
host: baseHostUrl,
port: baseHostPort,
method: httpMethod,
path: apiConfig.publicPath + methodURL// + ((paramString.length > 0) ? '?' + paramString : "")
path: apiConfig.publicPath + methodURL + ((paramString.length > 0) ? '?' + paramString : "")
};

if (['POST','DELETE','PUT'].indexOf(httpMethod) !== -1) {
var requestBody = query.stringify(params);
}

if (apiConfig.oauth) {
console.log('Using OAuth');

Expand Down Expand Up @@ -472,9 +509,7 @@ function processRequest(req, res, next) {
function unsecuredCall() {
console.log('Unsecured Call');

if (['POST','PUT','DELETE'].indexOf(httpMethod) === -1) {
options.path += ((paramString.length > 0) ? '?' + paramString : "");
}
options.path += ((paramString.length > 0) ? '?' + paramString : "");

// Add API Key to params, if any.
if (apiKey != '' && apiKey != 'undefined' && apiKey != undefined) {
Expand Down Expand Up @@ -522,18 +557,10 @@ function processRequest(req, res, next) {
options.headers = headers;
}

if (!options.headers['Content-Length']) {
if (requestBody) {
options.headers['Content-Length'] = requestBody.length;
}
else {
options.headers['Content-Length'] = 0;
}
if (content.length > 0) {
options.headers['Content-Length'] = content.length;
}

if (requestBody) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
}

if (config.debug) {
console.log(util.inspect(options));
Expand All @@ -548,6 +575,12 @@ function processRequest(req, res, next) {
console.log('Protocol: HTTP');
doRequest = http.request;
}
if(contentType !== ''){
if (config.debug) {
console.log('Setting Content-Type: ' + contentType);
}
options.headers['Content-Type'] = contentType;
}

// API Call. response is the response from the API, res is the response we will send back to the user.
var apiCall = doRequest(options, function(response) {
Expand Down Expand Up @@ -602,12 +635,10 @@ function processRequest(req, res, next) {
};
});

if (requestBody) {
apiCall.end(requestBody, 'utf-8');
}
else {
apiCall.end();
if(content !== ''){
apiCall.write(content,'utf-8');
}
apiCall.end();
}
}

Expand Down
7 changes: 6 additions & 1 deletion config.json.sample
Expand Up @@ -9,5 +9,10 @@
"port" : 6379,
"password" : "",
"database" : "0"
}
},
"https" : {
"on": false,
"keyPath": "/Path/to/my.hostname.key.pem",
"certPath": "/Path/to/my.hostname.crt.pem"
}
}
11 changes: 11 additions & 0 deletions views/api.jade
Expand Up @@ -152,5 +152,16 @@ ul
a(href='#', class='remove') Remove
a(href='#', class='add-headers') Add Header
// Create header fields and button to add/remove headers.
- if (method.content)
div.content
h4.title
div.indicator
span Content
div.fields
select(name='contentType')
- each choice in method.content.contentType
option(value=choice) #{choice}
textarea(columns='80', rows='10', name='requestContent', placeholder='{}')
a(href='#', class='remove') Remove
- if (!method['read-only'])
input(type='submit', id=method.MethodName, value='Try it!')