posts - 598 , comments - 849 , trackbacks - 247

My Links

News

qrcode

Lance Robinson

Create Your Badge

Lance Robinson is a software engineer in Durham, Chapel Hill, Raleigh, and surrounding areas. More about Lance.

 Subscribe

Tag Cloud

Article Categories

Archives

Post Categories

Blogs

Miscellanous

Noteworthy Stuff

Popular Posts

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.

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

Feedback

Gravatar

# re: How to run CGI applications on IIS7

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.
1/20/2008 9:30 PM | Matt
Gravatar

# re: How to run CGI applications on IIS7


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
7/14/2008 7:29 PM | David
Gravatar

# re: How to run CGI applications on IIS7

Thank you very much!
9/27/2008 6:23 AM | Allan
Gravatar

# re: How to run CGI applications on IIS7

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.
2/5/2009 6:03 AM | Harald Mais
Gravatar

# re: How to run CGI applications on IIS7

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

# re: How to run CGI applications on IIS7

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?
5/14/2009 3:52 AM | Marco
Gravatar

# re: How to run CGI applications on IIS7

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
7/3/2009 5:22 AM | the birdie
Gravatar

# re: How to run CGI applications on IIS7

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 ....
9/10/2009 1:02 AM | Caleb
Gravatar

# re: How to run CGI applications on IIS7

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
12/18/2009 9:39 AM | Uttam Dhakal
Gravatar

# re: How to run CGI applications on IIS7

[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
1/25/2010 3:10 PM | Didier
Gravatar

# re: How to run CGI applications on IIS7

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
4/15/2010 3:59 PM | Neto
Gravatar

# re: How to run CGI applications on IIS7

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....
6/22/2010 11:54 AM | Nickstawns
Gravatar

# re: How to run CGI applications on IIS7

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..
11/26/2010 3:37 AM | bar code scanners
Gravatar

# re: How to run CGI applications on IIS7

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

# re: How to run CGI applications on IIS7

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

# re: How to run CGI applications on IIS7

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
1/4/2011 1:17 PM | Cesar
Gravatar

# re: How to run CGI applications on IIS7

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.
3/26/2011 12:02 AM | Matt
Gravatar

# re: How to run CGI applications on IIS7

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?
10/13/2011 1:49 PM | Dave
Gravatar

# re: How to run CGI applications on IIS7

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.
10/16/2011 10:56 AM | HoppingYop
Gravatar

# re: How to run CGI applications on IIS7

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!
10/17/2011 9:01 AM | Dave
Gravatar

# re: How to run CGI applications on IIS7

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
10/18/2011 11:38 AM | Dave
Gravatar

# re: How to run CGI applications on IIS7

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.
1/11/2012 9:58 PM | Brett Henrichsen
Gravatar

# re: How to run CGI applications on IIS7

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.
1/24/2012 3:25 PM | Marc
Gravatar

# re: How to run CGI applications on IIS7

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Àë
2/3/2012 4:46 PM | claude g
Gravatar

# re: How to run CGI applications on IIS7

Enable them by right clicking > feature permissions > place a check mark on Execute.
6/12/2012 4:56 PM | NK
Gravatar

# re: How to run CGI applications on IIS7

Thanks you bro' :)
7/31/2012 7:55 AM | Tylian
Gravatar

# re: How to run CGI applications on IIS7

100% thanks!!! from:
Left by Nickstawns at 6/22/2010 11:54 AM
10/26/2012 2:17 AM | Paul P
Gravatar

# re: How to run CGI applications on IIS7

Lance my friend,

God bless you........
4/16/2013 5:17 PM | Hamid
Gravatar

# re: How to run CGI applications on IIS7

Thank you Very. You teach the nations.
9/10/2013 1:37 AM | Matondo Lam
Gravatar

# re: How to run CGI applications on IIS7

The ISAPI and CGI Restrictions part really helped me. Thank you very much!
11/6/2013 2:43 PM | Leonardo Herrera
Gravatar

# re: How to run CGI applications on IIS7

Like a lot of people I'm having trouble with getting a CGI application that runs perfectly in an IDE but not quite so nicely in a local IIS7 server (WinVista Home Premium).

My first app (written in Delphi 6) was just "Hello, World!". After setting up the "allow the app to run" via the restrictions panel it ran just fine.

My "real" app failed with a "Bad Gateway 502" (bad headers) and what the server said it got instead looked a lot like a Delphi exception message. The app attempts not just to write but to read another file. But I knew from experiment one that writing works, so I added this:

==============================


{ show rudimentary HTML error page }

procedure UIshowerror(const mesg: String);
begin

writeln( Output, 'CONTENT-TYPE: TEXT/HTML' );
writeln( Output );

writeln( Output, '<HTML><HEAD><TITLE>CGI APP ERROR</TITLE></HEAD><BODY>' );
writeln( Output, '<P>The CGI application reports an error:</P>' );
writeln( Output, '<H3>' + mesg + '</H3>' );
writeln( Output, '</BODY></HTML>' );

end;

{ check if a file can be accessed }

function canaccess(const fname: String): Boolean;
begin

canaccess := False;
if ( FileExists(fname) ) then
canaccess := True
else
UIshowerror( 'File not found: ' + fname );

end;

===========================

...and called it before all file open-for-read attempts.

The IIS7 server likes it fine. It doesn't let me actually open any file for reading, but now my browser shows me this instead of "Bad Gateway". So I know what's happening - I just don't know why.

2/6/2014 12:09 PM | Anton Treuenfels
Gravatar

# re: How to run CGI applications on IIS7

IIS7 is using ‘FastCGI’ where as previous IIS(IIS6) uses just CGI.
In FastCGI “host CGI process remain alive, even after one request finishes” Where as in CGI –“ Every HTTP request creates a new process which processes that request and terminates”
stdout buffer fflush(stdout) -is not needed for CGI(IIS6), since next request is handled by a new process. But, cleanup becomes mandatory for FastCGI(IIS7), since same old CGI process remains alive and being reused repeatedly, for all requests.
7/22/2014 7:09 AM | sankar
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: