Geeks With Blogs

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


Lance's TextBox » About Me » My Resume »Twitter

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.

Posted on Thursday, December 13, 2007 1:58 PM | Back to top


Comments on this post: How to run CGI applications on IIS7

# re: How to run CGI applications on IIS7
Requesting 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.
Left by Matt on Jan 20, 2008 9:30 PM

# re: How to run CGI applications on IIS7
Requesting 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
Left by David on Jul 14, 2008 7:29 PM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Thank you very much!
Left by Allan on Sep 27, 2008 6:23 AM

# re: How to run CGI applications on IIS7
Requesting 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.
Left by Harald Mais on Feb 05, 2009 6:03 AM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Thank you!
I got the same problem.
Left by mofun.cc on Mar 19, 2009 8:21 AM

# re: How to run CGI applications on IIS7
Requesting 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?
Left by Marco on May 14, 2009 3:52 AM

# re: How to run CGI applications on IIS7
Requesting 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
Left by the birdie on Jul 03, 2009 5:22 AM

# re: How to run CGI applications on IIS7
Requesting 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 ....
Left by Caleb on Sep 10, 2009 1:02 AM

# re: How to run CGI applications on IIS7
Requesting 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
Left by Uttam Dhakal on Dec 18, 2009 9:39 AM

# re: How to run CGI applications on IIS7
Requesting 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
Left by Didier on Jan 25, 2010 3:10 PM

# re: How to run CGI applications on IIS7
Requesting 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
Left by Neto on Apr 15, 2010 3:59 PM

# re: How to run CGI applications on IIS7
Requesting 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....
Left by Nickstawns on Jun 22, 2010 11:54 AM

# re: How to run CGI applications on IIS7
Requesting 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..
Left by bar code scanners on Nov 26, 2010 3:37 AM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
hey dude this blog will help all the new readers.... Keep up the work going
Left by Deer Repellents on Dec 24, 2010 5:09 AM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Thank you! I was spinning my wheels until I found these instructions. Microsoft's documentation, as usual, was useless.
Left by Steve on Jan 04, 2011 9:52 AM

# re: How to run CGI applications on IIS7
Requesting 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
Left by Cesar on Jan 04, 2011 1:17 PM

# re: How to run CGI applications on IIS7
Requesting 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.
Left by Matt on Mar 26, 2011 12:02 AM

# re: How to run CGI applications on IIS7
Requesting 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?
Left by Dave on Oct 13, 2011 1:49 PM

# re: How to run CGI applications on IIS7
Requesting 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.
Left by HoppingYop on Oct 16, 2011 10:56 AM

# re: How to run CGI applications on IIS7
Requesting 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!
Left by Dave on Oct 17, 2011 9:01 AM

# re: How to run CGI applications on IIS7
Requesting 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
Left by Dave on Oct 18, 2011 11:38 AM

# re: How to run CGI applications on IIS7
Requesting 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.
Left by Brett Henrichsen on Jan 11, 2012 9:58 PM

# re: How to run CGI applications on IIS7
Requesting 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.
Left by Marc on Jan 24, 2012 3:25 PM

# re: How to run CGI applications on IIS7
Requesting 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Àë
Left by claude g on Feb 03, 2012 4:46 PM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Enable them by right clicking > feature permissions > place a check mark on Execute.
Left by NK on Jun 12, 2012 4:56 PM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Thanks you bro' :)
Left by Tylian on Jul 31, 2012 7:55 AM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
100% thanks!!! from:
Left by Nickstawns at 6/22/2010 11:54 AM
Left by Paul P on Oct 26, 2012 2:17 AM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Lance my friend,

God bless you........
Left by Hamid on Apr 16, 2013 5:17 PM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
Thank you Very. You teach the nations.
Left by Matondo Lam on Sep 10, 2013 1:37 AM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
The ISAPI and CGI Restrictions part really helped me. Thank you very much!
Left by Leonardo Herrera on Nov 06, 2013 2:43 PM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
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.

Left by Anton Treuenfels on Feb 06, 2014 12:09 PM

# re: How to run CGI applications on IIS7
Requesting Gravatar...
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.
Left by sankar on Jul 22, 2014 7:09 AM

Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net