How to run CGI applications on IIS7

While trying out the /n software IP*Works! 3-D Secure MPI C++ Edition (for implementing Verified By Visa, MasterCard SecureCode, or JCB J/Secure) I needed to figure out how to run a CGI application in IIS7.  I had a compiled c++ exe (3dsecure.exe) that would handle my 3d secure authentication, but what was the next step?  Here's how to do it:

First, go to the Internet Information Services (IIS) Manager snap-in.  Click on the server in the Connections list, and then click on "ISAPI and CGI Restrictions" under the IIS features view.  Here you'll want to specify the path to a specific cgi application that you want to allow.  Optionally, here you could also allow any cgi app to run, but that's not recommended.

Next, go back to the server (or a specific website) and click on Handler Mappings.  You may already have a "CGI-exe" handler (for *.exe) that is disabled by default.  Enable this, or add a new one (Request path = *.exe, Module = CgiModule, Name = whatever you like).  In the handler permissions you'll need to have "Execute" checked.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Thursday, December 13, 2007 1:58 PM

Feedback

# re: How to run CGI applications on IIS7

Left by Matt at 1/20/2008 9:30 PM
Gravatar Just thought I'd let you know that you just ended about 6 hours of frustration on my part. What a huge pain in the ass. Of course, now the exe is unable to write to a log file on my machine, but hopefully I'm 1 step closer.

Thanks for cataloging this.

# re: How to run CGI applications on IIS7

Left by David at 7/14/2008 7:29 PM
Gravatar
We also just wrote a tutorial about geting CGI's to work with IIS7 (and previous IIS releases) which might help some people.
http://www.wrensoft.com/zoom/support/faq_cgi_iis.html

# re: How to run CGI applications on IIS7

Left by Allan at 9/27/2008 6:23 AM
Gravatar Thank you very much!

# re: How to run CGI applications on IIS7

Left by Harald Mais at 2/5/2009 6:03 AM
Gravatar We try to run our cgi-app under IIS 7.0 and Vista.
In your tutorial you explained what to do under
IIS 7.0 (Windows Server 2008).
We configured "ISAPI and CGI Restrictions" and "Handler Mappings" as explained, but we get an Error "502.2 Bad Gateway".
Under Vista we can't find the "Server Manager".
(perhaps we ar blind)
Is there no "Server Manager" or can you give as a hint what to do instead?
Thanks for an answer.

Thanks for an answer.

# re: How to run CGI applications on IIS7

Left by mofun.cc at 3/19/2009 8:21 AM
Gravatar Thank you!
I got the same problem.

# re: How to run CGI applications on IIS7

Left by Marco at 5/14/2009 3:52 AM
Gravatar Same here!

We have converted the directory where the .exe is into 'application'. We entered Module Mapping for this directory to the .exe-file, with executable rights. And we checked ISAPI and CGI restrictions.

Still when calling the html-page that launches the .exe:

"HTTP Error 502.2 - Bad Gateway
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "".

Anyone out there with suggestions?

# re: How to run CGI applications on IIS7

Left by the birdie at 7/3/2009 5:22 AM
Gravatar Hi,
Anyone know how to run all cgi's on a given folder without specifiying each file? It seems kinda of a pain in the butt to do this. Would I have to make a dll file that does this? any help? heh

thanks
-- the birdie

# re: How to run CGI applications on IIS7

Left by Caleb at 9/10/2009 1:02 AM
Gravatar Hi,
I have done all of the above and got the same meaage:
"HTTP Error 502.2 - Bad Gateway
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "".
Does anybody have a resolution?
Thanks ....

# re: How to run CGI applications on IIS7

Left by Uttam Dhakal at 12/18/2009 9:39 AM
Gravatar I am looking for something like this.
I had a sharepoint server that runs on Windows Server 2008 and i am trying to run some bugzilla scripts in my sharepoint site but i am not getting any idea how to start it
Can anyone suggests me how can we run CGI scripts or Bugzilla Scripts in Sharepoint

# re: How to run CGI applications on IIS7

Left by Didier at 1/25/2010 3:10 PM
Gravatar [quote]
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are ""
[/quote]

same for me .. and does anybody found the solution
Thanks for all
Didier

# re: How to run CGI applications on IIS7

Left by Neto at 4/15/2010 3:59 PM
Gravatar about this problem

You can try Set the CGI Timeout more than 15 seconds(standard) , the communication its very slow
And
i think there is a problem with the OrderID
put 0 and check if works

here works...but now i have the problem with this OrderID stuff

# re: How to run CGI applications on IIS7

Left by Nickstawns at 6/22/2010 11:54 AM
Gravatar try this:
1. Go to "Handler Mappings"
2. "Add Module Mapping"
3. Use the following:
Request path : *.cgi
Module : IsapiModule
Executable : <Perl Directory>/bin/PerlEx30.dll

Hope this will be helpful....

# re: How to run CGI applications on IIS7

Left by bar code scanners at 11/26/2010 3:37 AM
Gravatar i dont know what to say after seeing these comments. but i would like to say that this site was very interesting. i had a great time. thanks for the good post..

# re: How to run CGI applications on IIS7

Left by Deer Repellents at 12/24/2010 5:09 AM
Gravatar hey dude this blog will help all the new readers.... Keep up the work going

# re: How to run CGI applications on IIS7

Left by Steve at 1/4/2011 9:52 AM
Gravatar Thank you! I was spinning my wheels until I found these instructions. Microsoft's documentation, as usual, was useless.

# re: How to run CGI applications on IIS7

Left by Cesar at 1/4/2011 1:17 PM
Gravatar Good instructions!

I've settup my GCC x64 environment and your recipe allowed me to prepare IIS 7.5 (Win 2008) very fast in order to run my C programs as CGI.

Thank you

Cesar

# re: How to run CGI applications on IIS7

Left by Matt at 3/26/2011 12:02 AM
Gravatar I just searched for this problem, and found this page...and I was actually here (the first commenter) over 3 years ago.

That's funny. Thanks again for the post.

# re: How to run CGI applications on IIS7

Left by Dave at 10/13/2011 1:49 PM
Gravatar I have similar problem but ONLY when form data is posted that is "large". The delphi created exe gets "stuck" after it has completed its processing (of dispatch events) but never closes out the application and return control to IIS7 - and eventually IIS7 times the process out and I get the 502.2 error. All the post data is coming through into application correctly and everything is working except as described above. Anyone have any ideas?

# re: How to run CGI applications on IIS7

Left by HoppingYop at 10/16/2011 10:56 AM
Gravatar Dave, that's a bug in Delphi and it is documented in a few websites but I can't remember where. We had the same issue where I work at and found the posts about it which included some workarounds. IIS 6 is not as strict as 7 in regards to the bug in Delphi and that's why you probably noticed that in IIS 6 the same exes will work. Sorry but I don't have the URLs of the websites where we found the information. If I get some time I'll look for them.

# re: How to run CGI applications on IIS7

Left by Dave at 10/17/2011 9:01 AM
Gravatar HoppingYop: Thanks - but I am aware of the bug in delphi I think you are referring. I have that fixed. That problem is when delphi tries to read data from STDIN but there is no STDIN data because it is a POST request. I got that fixed and that part is working fine.

I have done some extensive debugging over the weekend and it seems that the problem I am having is when there is a large amount of POST data. Delphi is reading all the POST content data fine, doing everything in programming, and the very last step before delphi exe completion is writing out the produced "text" to STDOUT. Application locks up during the "write". I changed the delphi source to do the writes in 4k blocks, and it locks up after successfully writing the entire header and then second block of the "content". It actually gets stuck and hangs doing the "write to STDOUT".

I am thinking this may be some odd issue with cgi-exe getting "stuck" reading in the data from the console (maybe something with internal buffering)? The real odd thing is that the exact same thing works fine (same exact output) with a smaller POST payload. I even changed the headers to ensure "content-length" is one of the first headers to be produced as well as adding in some additional HTTP/1.1 headers that say they are needed - but nothing I do seems to make it work.

I also played with "create new console every time" settings, various output caching settings, and even activated dynamic compression to see if anything would help, but nothing seems to cure problem.

I have found some mention of this type of problem occurring in some cases where the cgi application is writing data to STDERR - but I am pretty sure that nothing is getting written to STDERR file handle anywhere in my case.

Also - just to note... the POST is a standard POST type, not MULTIPART.

Sorry about the long post, but wanted to explain as best as I could because this is really creating a pain for me.

Please note server is 64bit OS running Windows Server Web SP2.

Thanks!

# re: How to run CGI applications on IIS7

Left by Dave at 10/18/2011 11:38 AM
Gravatar Just a quick followup:
There was definately something odd happening with console IO - I rewrote the console READSTRING function that gets the STDIN data so it allowed for overlapping and did a loop so it only read in the amount of available bytes. It seemed that IIS7 was not filling STDIN "immediately" and if I just did a "readfile" of the whole expected number of bytes, some windows thread was getting "stuck" somewhere. Oddly - with this change, I was reading in the same amount of total bytes, just not "all at once". This fixed the hanging issue.

Oddly, though - the content-length is being reported as 45193, I am able to read that same amount of bytes, but the content itself is greater than what is being reported so I am loosing content data. I have tried to adjust the "uploadReadAheadSize" to various values and IIS7 seems to be ignoring that configuration setting entirely.

I also tried different browsers, thinking that maybe there was a size limit built into browser for posting data - but every browser I try seems to react the same.

I know I should be using multi/part form but this should still work. At least cgi app is not locking up and timing out now.

-Dave

# re: How to run CGI applications on IIS7

Left by Brett Henrichsen at 1/11/2012 9:58 PM
Gravatar We have the default cgi-EXE and ISAPI.dll handlers you mention above, and, as you mention, they are DISABLED. How the hell do you ENABLE them? Your one line of "enable" them doesn't say how, and we don't see the option to enable anywhere.

# re: How to run CGI applications on IIS7

Left by Marc at 1/24/2012 3:25 PM
Gravatar Thank you very much.

You just saved my PC from flying through the window. I have been all day lost trying to make work my CGI WebService under IIS7.

Thanks again.

# re: How to run CGI applications on IIS7

Left by claude g at 2/3/2012 4:46 PM
Gravatar i got some prob too with cgi-EXE i suppose to run a .exe with some parameter and render it in html
but instead of running the .exe , it open it like we had open it with notepad any something like this :
MZÿÿ¸@€º´ Í!¸LÍ!This program cannot be run in DOS mode. $PELn@ð:à „f @ @ PT.textð‚„ `.rdata÷ ˆ@@.data$V°<@À.idataæÌ@À¸„Oèv*SUV„$W»3í3ÿPh(³@‰\$$‰l$‰l$(‰l$‰|$ ÆD$LÆ„$°èv ƒÄŒ$„(Qh$³@èa ƒÄ”$¨Rh³@èL ƒÄ¾³@„$¨ŠŠÊ:u„ÉtŠPŠÊ:VuƒÀƒÆ„Éuà3Àë

Your comment:





 
 

Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski