You are currently offline, waiting for your internet to reconnect

BUG: Set-Cookie Is Ignored in CGI When Combined With Location

This article was previously published under Q176113
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 7.0 running on Microsoft Windows Server 2008. IIS 7.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:For more information about IIS 7.0, visit the following Microsoft Web site:
When a CGI application sends a Set-Cookie header with "302 Object Moved"response and Location header, Internet Information Server (IIS) ignores thecookie header.
This behavior is in violation of the CGI specification, which states, "Anyheaders that are not server directives are sent directly back to theclient. Currently, this specification defines three server directives..."

As a workaround, make sure the file name of the EXE begins with "nph-" andmanually create all HTTP headers in your program. "nph-" indicates to theserver that the CGI program is to be run in non-parsed headers mode. CGIhas two modes. In normal mode (parsed headers), you must send one of theCGI directives to standard output (Content-type, Location, or Status). CGIformats a valid HTTP response line based on the directive you sent. Itformats other standard HTTP headers for you, and it should include anyother headers that you have specified.

The other mode is non-parsed header mode. In this mode CGI does not set anyheaders itself. The CGI program must format a full HTTP response includingthe response line and all headers. The server will not add or modify anyheaders for you in this mode.

The convention is that a CGI program whose name begins with "nph-" is runin non-parsed header mode; otherwise, CGI programs are run in parsed headermode.
Microsoft has confirmed this to be a bug in the Microsoft products listedat the beginning of this article.

Steps to Reproduce Behavior

Compile this CGI program as a Win32 Console Application and place it in afolder on your IIS server where it can be executed:
#include <stdio.h>int main(){  printf("Location: %s\r\n", "");  printf("Set-Cookie: Name1=Value1; path=/;    expires=Fri, 22 May 1998 21:00:00 GMT\r\n\r\n");  return 0;}				

Call the CGI program from your browser and observe its output (via NetworkMonitor, for example). It will be similar to this:
HTTP/1.0 302 Object movedLocation: Microsoft-IIS/2.0Content-Type: text/htmlContent-Length: 145<head><title>Document moved</title></head><body><h1>Object Moved</h1>This document may be found<a HREF="">here</a></body>				

Note that the Set-Cookie header has not been sent by IIS. If you havecookie warnings turned on in your browser, no warning appears.

To allow a cookie to be set in a 302 response, use code similar to thefollowing, and prefix "nph-" to the name of the executable file:
#include <stdio.h>int main(){  printf("HTTP/1.0 302 Redirect\r\n");  printf("Location: %s\r\n", "");  printf("Set-Cookie: Name=Value; path=/; expires=Fri, 22 May 1998 21:00:00   GMT\r\n\r\n");  return 0;}				

The output is similar to the following. Note that the cookie is now sent,and no headers are added by the server.
HTTP/1.0 302 RedirectLocation: Name=Value; path=/; expires=Fri, 22 May 1998 21:00:00 GMT				
(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by LeonBraginski, Microsoft Corporation

Article ID: 176113 - Last Review: 07/03/2008 16:16:44 - Revision: 5.0

  • Microsoft Internet Information Server 3.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
  • kbbug kbnofix KB176113