Run CGI in IIS 7 to work with GET without Requiring POST Request

9,840

Solution 1

Shortly: it might be a Delphi CGI application compiled with a Delphi version previous to D2007 last update (11.0.2902.10471). If it is the case you need the sources and a Delphi installation and recompile the application. Alternatively you should ask them just to recompile the app using a recent Delphi version.

(from http://forums.iis.net/t/1100323.aspx?PageIndex=2)

We found the problem which was causing our CGI to not respond on IIS 7. We actually narrowed the reason down to a line of code in the base class of all CGI applications written in Delphi (at least up to version 2005). This line of code is in the Run method of TCGIApplication and simply resets the standard input. On IIS 6 and prior, this line executes with no problem but on IIS 7 is raises an exception which causes the CGI application to crash and therefore not respond to requests. We are still investigating the side-effects of commenting this line out but that is our current solution. By removing this line, our CGI applications are now executing correctly on IIS 7.

Original author: Steven

Solution 2

We have a Delphi 5 CGI application that experienced the same issue on IIS 7. Our solution was similar to the one Steven posted above, however, we don't completely comment out the line marius mentions above. When I did, I saw that any Request.ContentString statement would bring back an empty string in the application. Our solution was to implement the code fix suggested at this article near the bottom. For your reference, here is a code snipped of our copy of CGIApp.pas with the change.

procedure TCGIApplication.Run;
var
  HTTPRequest: TCGIRequest;
  HTTPResponse: TCGIResponse;
begin
  inherited Run;
  if IsConsole then
  begin
    Rewrite(Output);
    //Win 7/2008 IIS7
    //Reset(Input);
    {$i-} {!!IIS7}
    Reset(Input);
    if IOResult <>0 then ;
    {$i+}
  end;
  try
    HTTPRequest := NewRequest;
    try
      HTTPResponse := NewResponse(HTTPRequest);
      try
        HandleRequest(HTTPRequest, HTTPResponse);
      finally
        HTTPResponse.Free;
      end;
    finally
      HTTPRequest.Free;
    end;
  except
    HandleServerException(Exception(ExceptObject), FOutputFileName);
  end;
end;
Share:
9,840

Related videos on Youtube

Meligy
Author by

Meligy

Since 2005, Meligy has been involved in various kinds of web systems from payment gateways, SaaS CMS and eCommerce platforms, business workflow automations to private social media integrations and public social network integrations, working with commercial and non-profit organizations in Egypt, U.A.E, Azerbaijan, U.S., and Australia. In addition to mentoring other developers during gigs, Meligy is also an active blogger about new technologies in the .NET and web spaces, as well as a technical presenter on technologies and Agile processes in both offline usergroup communities, and his own online video tutorials.

Updated on September 17, 2022

Comments

  • Meligy
    Meligy over 1 year

    I'm trying to migrate an old CGI application from an existing Windows 2003 server (IIS 6.0) where it works just fine to a new Windows 2008 server with IIS 7.0 where we're getting the following problem:

    After setting up the module handler and everything, I find that I can only access the CGI application (rdbweb.exe) file if I'm calling it via POST request (form submit from another page).

    If I just try to type in the URL of the file (issuing a GET request) I get the following error:

    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 "Exception EInOutError in module rdbweb.exe at 00039B44. I/O error 6. ".

    This is a very old application for one of our clients. When we tried to call the vendor they said we need to pay ~ $3000 annual support fee in order to start the talk about it. Of course I'm trying to avoid that!

    Note that:

    • If we create a normal HTML form that submits to "rdbweb.exe", we get the CGI working normally. We can't use this as workaround though because some pages in the application link to "rdbweb.exe" with normal link not form submit.
    • If we run "rdbweb.exe". from a Console (Command Prompt) Window not IIS, we get the normal HTML we'd typically expect, no problem.

    We have tried the following:

    • Ensuring the CGI module mapped to "rdbweb.exe".in IIS has all permissions (read, write, execute) enabled and also all verbs are allowed not just specific ones, also tried allowing GET, POST explicitely.
    • Ensuring the application bool has "enable 32 bit applications" set to true. Ensuring the website runs with an account that has full permissions on the "rdbweb.exe".file and whole website (although we know it "read", "execute" should be enough).
    • Ensuring the machine wide IIS setting for "ISAPI and CGI Restrictions" has the full path to "rdbweb.exe".allowed.
    • Changing the module from CGI to Fast CGI, not working also
    • Making sure we have the latest Windows Updates (for IIS6 we found knowledge base articles stating bugs that require hot fixes for IIS6, but nothing similar was found for IIS7).

    Now the only remaining possibility we have instigated is the following Microsoft Knowledge Base article: http://support.microsoft.com/kb/145661 - Which is about:

    CGI Error: The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

    the article suggests the following solution:

    Modify the source code for the CGI application header output. The following is an example of a correct header:

     print "HTTP/1.0 200 OK\n";  
     print "Content-Type: text/html\n\n\n";
    

    Unfortunately we do not have the source to try this out, and I'm not sure anyway whether this is the issue we're having.

    Can you help me with this problem? Is there a way to make the application work without requiring POST request? Note that on the old IIS6 server the application is working just fine, and I could not find any special IIS configuration that I may want to try its equivalent on IIS7.

  • Mark Mather
    Mark Mather over 11 years
    Heck this site removed my PHP code! It was: <?php putenv('QUERY_STRING=' . $_SERVER['QUERY_STRING']); echo system('C:... path your cgi...//...delphicgi.exe'); ?>