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

net/http/cgi: support Python etc non-executable scripts on Windows #18420

Open
zhw2101024 opened this issue Dec 23, 2016 · 20 comments
Open

net/http/cgi: support Python etc non-executable scripts on Windows #18420

zhw2101024 opened this issue Dec 23, 2016 · 20 comments
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@zhw2101024
Copy link

zhw2101024 commented Dec 23, 2016

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.7.4

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=
set GORACE=
set GOROOT=D:\Go
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

cgidemo.zip
Change to unziped directory, and execute go run cgi.go, then visit http://localhost:8000/cgi-bin/test.py

What did you expect to see?

Hello on my browser

What did you see instead?

The page hangs up

@robothor
Copy link

This works for me if I make sure test.py is executable:

#!/usr/bin/env python

print("Content-Type: text/html\n")
print("Hello")

Also, there were some strange whitespace characters in the file that were causing problems.

Note that I am running on darwin and am not really sure how to fix this on Windows.

@minux
Copy link
Member

minux commented Dec 23, 2016 via email

@minux minux closed this as completed Dec 23, 2016
@bradfitz
Copy link
Contributor

@minux, but "runnable" on a file is a concept that doesn't exist on Windows.

We should do whatever other popular CGI servers do. (Whatever that is.) Shebang support, perhaps?

@bradfitz bradfitz reopened this Dec 23, 2016
@bradfitz bradfitz changed the title net/http/cgi cannot support python script backend net/http/cgi: support Python etc non-executable scripts on Windows Dec 23, 2016
@bradfitz bradfitz added this to the Go1.9 milestone Dec 23, 2016
@bradfitz bradfitz added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. help wanted labels Dec 23, 2016
@minux
Copy link
Member

minux commented Dec 24, 2016 via email

@mattn
Copy link
Member

mattn commented Dec 24, 2016

For Windows, you should handle extension whether it will be runnable.

https://github.com/mattn/go-cgiserver/blob/master/cgiserver.go

BTW: s/alexbrainmain/alexbrainman/

@zhw2101024
Copy link
Author

There's a WINAPI called ShellExecute, equals to cmd /c script.py, but have no idea if it's applicable to use this here. Maybe will link to windows dll file.
cmd should work, as all supported windows versions have cmd and cmd's behavior seems won't change.

@zhw2101024
Copy link
Author

@bradfitz
Thanks for reopening this issue.
Please allow me to clarify this issue with more details.
currently golang's cgi only supports .com, .exe, .bat and .cmd files. If one want to use golang's cgi on windows, there's only two choices: one of compiling languages, or bat script. compiling languages have many advantages, but not so handy as script languages such as perl, one of most popular cgi backend languages. On the other hand, bat lack too many modern features, so it's difficult to implement a complete service with just bat.
So if cgi support script languages, it will be far more useful than before.

@minux
Copy link
Member

minux commented Dec 24, 2016 via email

@zhw2101024
Copy link
Author

zhw2101024 commented Dec 24, 2016

Thank you, This works for me at least.
@minux
But comparing with Python's cgi module or Apache's cgi module, this still have problems.
Imaging you have tones of python script use as cgi backend, and use apache's cgi module. One day you want to use golang's cgi, then you need to modify all existing scripts. A new batch file may do this work for you, but python scripts with bat suffix may terribly misleading your IDE or code editor, or other maintainers.

@minux
Copy link
Member

minux commented Dec 24, 2016 via email

@zhw2101024
Copy link
Author

zhw2101024 commented Dec 24, 2016

code editor will not be useful for these script files, as many functions will not work, such as highlighting, autocompletion, etc.

@rskumar
Copy link

rskumar commented Dec 24, 2016

MS Windows has different approach than *nix systems for FileType and Application association. There can be multiple associations, and association can have multiple options (first in list being default).
And verbs are like Open, Edit, Print etc...
So probably you have to call windows ShellExecute (Ex) for default or open verb action.
Read here - https://msdn.microsoft.com/en-us/library/windows/desktop/cc144148(v=vs.85).aspx

Or, we can query windows registry just once for every extensions to find registered launcher for the script, and cache it.
But if intelligent approach to auto-execute does not seems to be working, why don't we choose explicit approach? Like, if shebang specified with windows program path, and if available, use it or print sensible error on console for developer. And, also let developer specify a database of executable and their association to file types in program, which can be plugged to cgi handler, and then it will override shebang if that is also available and valid.

Apache HTTPD perhaps looks into cgi file shebang for per/python.

@alexbrainman
Copy link
Member

I agree we should use what windows shell already knows - see assoc and ftype windows commands. We could then use ShellExecute, or do it ourselves with AssocQueryString and CreateProcess.

Alex

@mattn
Copy link
Member

mattn commented Jan 1, 2017

I guess AssocQueryString make unexpected behavior when the file is .docx or something which is not expected as CGI.

@alexbrainman
Copy link
Member

unexpected behavior when the file is .docx

What does CGI do on unix when file is .docx? Or .html or whatever?

Alex

@mattn
Copy link
Member

mattn commented Jan 2, 2017

What does CGI do on unix when file is .docx? Or .html or whatever?

Windows doesn't have executable-permission as same as UNIX's one. So we have to handle the file-suffix which can runnable with white-list. If it use AssocQueryString without white-list, the server may start Microsoft Word for the .docx.

@bradfitz
Copy link
Contributor

bradfitz commented Jan 2, 2017

Could we just check the PE file's Console-vs-GUI type and not execute *.exe files if they're GUI exes?

@alexbrainman
Copy link
Member

Could we just check the PE file's Console-vs-GUI type and not execute *.exe files if they're GUI exes?

Yes we could do that.

Alex

@withrocks
Copy link

I would suggest (at least as a workaround) to create a bat script test.bat that acts as a proxy/entry for the script test.py:

@echo off
python.exe test.py

These proxy scripts can then be generated automatically for all scripts that should be made available via the CGI.

I believe this is a rather common alternative to shebang on Windows.

@mattn
Copy link
Member

mattn commented Mar 4, 2017

I believe this is a rather common alternative to shebang on Windows.

I think so. Over hack can be a trouble afterwards.

@bradfitz bradfitz modified the milestones: Go1.10, Go1.9 May 23, 2017
@bradfitz bradfitz removed this from the Go1.10 milestone Nov 29, 2017
@bradfitz bradfitz added this to the Unplanned milestone Nov 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

8 participants