Skip to content

Conversation

@oliver-moran
Copy link

Please ensure that your pull request fulfills these requirements:

  • The pull request is being made against the master branch
  • Tests for the changes have been added (for bug fixes / features)

What is the purpose of this pull request? (bug fix, enhancement, new feature,...)

Adds a CGI feature. This enables execution of server-side scripts in a /cgi-bin/ directory using the Common Gateway Interface. Use case is for when small server-side scripting is needed to support static files (e.g. saving form data to a flat-file database, serving a file listing from a directory).

What changes did you make?

Added an interface to the cgi module, with error prevention and usability assumptions (e.g. .js files don't need to be set to executable on Unix).

Provide some example code that this change will affect, if applicable:

Place this file in /public/cgi-bin/ and name it script.js:

console.log('Content-Type: text/plain');
console.log('');
console.log(`Hello, ${process.env.REMOTE_ADDR}!`);

Run http-server with the --cgi option:

$ http-server --cgi

Open http://127.0.0.1:8080/cgi-bin/script.js and the script is executed:

Hello, 127.0.0.1!

Is there anything you'd like reviewers to focus on?

Error handling, especially common mistakes that novice users of CGI would make. One known issue is in the handling of malformed headers by the cgi module. Error handling is not implemented in the latest released version of this module. I've added a pull request to fix this but the module does not appear to be maintained.

Execution of scripts on different operating systems. For JavaScript files (.js), there should be no difference between OSes. These don't need to be set to executable in Unix systems. On Unix, there is better choice of executing scripts in different languages (e.g. Python, bash). However, these need to be set to executable. On Windows, .bat, .cmd and .exe are supported in addition to JavaScript (.js).

Please provide testing instructions, if applicable:

It's a scripting language, so go wild!

@oliver-moran
Copy link
Author

Forgot to push last set of edits to git. Closing to reopen a new pull request.

@oliver-moran
Copy link
Author

Correct pull request here: #682

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant