rem: s = "80";
add: if (https) { s = "443"; } else { s = "80"; }
context: CgiExec(Cgi *cgiPtr, Ns_Conn *conn){    int i, index, opipe[2];    char *s, *e, *p, **envp;    Ns_DString *dsPtr;    Mod *modPtr = cgiPtr->modPtr;    /*     * Get a dstring which will be used to setup env variables     * and the arg list.     */         dsPtr = CgiDs(cgiPtr);    /*     * Setup and merge the environment set.     */    cgiPtr->env = Ns_SetCreate(NULL);    if (cgiPtr->interpEnv != NULL) {        cgiPtr->env = Ns_SetCopy(cgiPtr->interpEnv);    } else {        cgiPtr->env = Ns_SetCreate(NULL);    }    if (modPtr->mergeEnv != NULL) {        Ns_SetMerge(cgiPtr->env, modPtr->mergeEnv);    }    if (modPtr->flags & CGI_SYSENV) {	envp = Ns_CopyEnviron(dsPtr);	while (*envp != NULL) {	    s = *envp;	    e = strchr(s, '=');	    if (e != NULL) {		*e = '\0';        	i = Ns_SetFind(cgiPtr->env, s);		if (i < 0) {        	    Ns_SetPut(cgiPtr->env, s, e+1);		}		*e = '=';	    }	    ++envp;	}	Ns_DStringTrunc(dsPtr, 0);    }    /*     * PATH is the only variable copied from the running environment if it     * isn't already in the server default environment.     */    if (Ns_SetFind(cgiPtr->env, "PATH") < 0) {        s = getenv("PATH");        if (s != NULL) {            SetUpdate(cgiPtr->env, "PATH", s);        }    }    /*     * Set all the CGI specified variables.     */    SetUpdate(cgiPtr->env, "SCRIPT_NAME", cgiPtr->name);    if (cgiPtr->pathinfo != NULL && *cgiPtr->pathinfo != '\0') {    	Ns_DString tmp;	        if (Ns_DecodeUrl(dsPtr, cgiPtr->pathinfo) != NULL) {            SetUpdate(cgiPtr->env, "PATH_INFO", dsPtr->string);        } else {            SetUpdate(cgiPtr->env, "PATH_INFO", cgiPtr->pathinfo);        }	Ns_DStringTrunc(dsPtr, 0);	Ns_DStringInit(&tmp);        Ns_UrlToFile(dsPtr, modPtr->server, cgiPtr->pathinfo);        if (Ns_DecodeUrl(&tmp, dsPtr->string) != NULL) {            SetUpdate(cgiPtr->env, "PATH_TRANSLATED", tmp.string);        } else {            SetUpdate(cgiPtr->env, "PATH_TRANSLATED", dsPtr->string);        }	Ns_DStringFree(&tmp);	Ns_DStringTrunc(dsPtr, 0);    } else {        SetUpdate(cgiPtr->env, "PATH_INFO", "");    }    SetUpdate(cgiPtr->env, "GATEWAY_INTERFACE", "CGI/1.1");    Ns_DStringVarAppend(dsPtr, Ns_InfoServer(), "/", Ns_InfoVersion(), NULL);    SetUpdate(cgiPtr->env, "SERVER_SOFTWARE", dsPtr->string);    Ns_DStringTrunc(dsPtr, 0);    Ns_DStringPrintf(dsPtr, "HTTP/%2.1f", conn->request->version);    SetUpdate(cgiPtr->env, "SERVER_PROTOCOL", dsPtr->string);    Ns_DStringTrunc(dsPtr, 0);    /*     * Determine SERVER_NAME from the conn location.     */    s = Ns_ConnLocation(conn);    p = NULL;    if (s != NULL) {        if (strstr(s, "://") == NULL) {            Ns_Log(Warning, "nscgi: location does not contain '://'");            s = NULL;        } else {            s = strchr(s, ':');         /* Get past the http */            if (s != NULL) {                s += 3;                 /* Get past the // */                p = strchr(s, ':');     /* Get to the port number */             }        }    }    if (s == NULL) {        s = Ns_ConnHost(conn);        SetUpdate(cgiPtr->env, "SERVER_NAME", s);    } else {        if (p == NULL) {            Ns_DStringAppend(dsPtr, s);           /* No port number */        } else {            Ns_DStringNAppend(dsPtr, s, (p - s)); /* Port number exists */        }        s = Ns_DStringExport(dsPtr);        SetUpdate(cgiPtr->env, "SERVER_NAME", s);        ns_free(s);    }    /*     * Determine SERVER_PORT from the conn location.     */    s = Ns_ConnLocation(conn);    if (s != NULL) {        s = strchr(s, ':');             /* Skip past http. */        if (s != NULL) {            ++s;            s = strchr(s, ':');         /* Skip past hostname. */            if (s != NULL) {                ++s;            }        }    }    if (s == NULL) {        s = "80";    }    SetUpdate(cgiPtr->env, "SERVER_PORT", s);    SetUpdate(cgiPtr->env, "AUTH_TYPE", "Basic");    SetUpdate(cgiPtr->env, "REMOTE_USER", conn->authUser);    s = Ns_ConnPeer(conn);    if (s != NULL) {        SetUpdate(cgiPtr->env, "REMOTE_ADDR", s);        if ((modPtr->flags & CGI_GETHOST)) {            if (Ns_GetHostByAddr(dsPtr, s)) {                SetUpdate(cgiPtr->env, "REMOTE_HOST", dsPtr->string);            }            Ns_DStringTrunc(dsPtr, 0);        } else {            SetUpdate(cgiPtr->env, "REMOTE_HOST", s);        }    }    SetUpdate(cgiPtr->env, "REQUEST_METHOD", conn->request->method);    SetUpdate(cgiPtr->env, "QUERY_STRING", conn->request->query);    s = Ns_SetIGet(conn->headers, "Content-Type");    if (s == NULL) {        if (STREQ("POST", conn->request->method)) {            s = "application/x-www-form-urlencoded";        } else {            s = "";        }    }    SetUpdate(cgiPtr->env, "CONTENT_TYPE", s);    if (conn->contentLength <= 0) {        SetUpdate(cgiPtr->env, "CONTENT_LENGTH", "");    } else {        Ns_DStringPrintf(dsPtr, "%u", (unsigned) conn->contentLength);        SetUpdate(cgiPtr->env, "CONTENT_LENGTH", dsPtr->string);        Ns_DStringTrunc(dsPtr, 0);    }    /*     * Set the HTTP_ header variables.     */    Ns_DStringAppend(dsPtr, "HTTP_");    for (i = 0; i < Ns_SetSize(conn->headers); ++i) {        s = Ns_SetKey(conn->headers, i);        e = Ns_SetValue(conn->headers, i);        Ns_DStringAppend(dsPtr, s);        s = dsPtr->string + 5;        while (*s != '\0') {            if (*s == '-') {                *s = '_';            } else if (islower(UCHAR(*s))) {                *s = toupper(UCHAR(*s));            }            ++s;        }        index = Ns_SetFind(cgiPtr->env, dsPtr->string);        if (index < 0) {            Ns_SetPut(cgiPtr->env, dsPtr->string, e);        } else {	    SetAppend(cgiPtr->env, index, ", ", e);        }        Ns_DStringTrunc(dsPtr, 5);    }    /*     * Build up the argument block.     */    Ns_DStringTrunc(dsPtr, 0);    if (cgiPtr->interp != NULL) {        Ns_DStringAppendArg(dsPtr, cgiPtr->interp);    }    if (cgiPtr->path != NULL) {        Ns_DStringAppendArg(dsPtr, cgiPtr->path);    }    s = conn->request->query;    if (s != NULL) {	if (strchr(s, '=') == NULL) {    	    do {	    	e = strchr(s, '+');		if (e != NULL) {		    *e = '\0';		}		Ns_UrlDecode(dsPtr, s);		Ns_DStringNAppend(dsPtr, "", 1);		if (e != NULL) {		    *e++ = '+';		}		s = e;	    } while (s != NULL);	}	Ns_DStringNAppend(dsPtr, "", 1);    }    /*     * Create the output pipe.     */         if (ns_pipe(opipe) != 0) {	Ns_Log(Error, "nscgi: pipe() failed: %s", strerror(errno));	return NS_ERROR;    }    /*     * Execute the CGI.     */         cgiPtr->pid = Ns_ExecProcess(cgiPtr->exec, cgiPtr->dir,	cgiPtr->ifd < 0 ? devNull : cgiPtr->ifd,	opipe[1], dsPtr->string, cgiPtr->env);    close(opipe[1]);    if (cgiPtr->pid < 0) {    	close(opipe[0]);	return NS_ERROR;    }    cgiPtr->ofd = opipe[0];    return NS_OK;}