⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lycgi.c

📁 基于rtos开发的浏览器!
💻 C
📖 第 1 页 / 共 2 页
字号:
	    HTAlert(CONNECT_SET_FAILED);	    if (TRACE) {		perror("LYNXCGI: pipe() failed");	    }	    status = -3;	    	} else if (pipe(fd2) < 0) {	    HTAlert(CONNECT_SET_FAILED);	    if (TRACE) {		perror("LYNXCGI: pipe() failed");	    }	    close(fd1[0]);	    close(fd1[1]);	    status = -3;	    	} else {		    static BOOL first_time = TRUE;      /* One time setup flag */	    if (first_time) {	/* Set up static environment variables */		first_time = FALSE;	/* Only once */				add_environment_value("REMOTE_HOST=localhost");		add_environment_value("REMOTE_ADDR=127.0.0.1");				sprintf(user_agent, "HTTP_USER_AGENT=%s/%s libwww/%s",			LYNX_NAME, LYNX_VERSION, HTLibraryVersion);		add_environment_value(user_agent);				sprintf(server_software, "SERVER_SOFTWARE=%s/%s",			LYNX_NAME, LYNX_VERSION);		add_environment_value(server_software);	    }	    	    if ((pid = fork()) > 0) { /* The good, */		int chars, total_chars;				close(fd2[1]);				if (anAnchor->post_data) {		    int written, remaining, total_written = 0;		    close(fd1[0]);		    /* We have form data to push across the pipe */		    if (TRACE) {			fprintf(stderr, "LYNXCGI: Doing post, content-type '%s'\n",				anAnchor->post_content_type);			fprintf(stderr,				"LYNXCGI: Writing:\n%s----------------------------------\n",				anAnchor->post_data);					    }		    remaining = strlen(anAnchor->post_data);		    while ((written = write(fd1[1],					    anAnchor->post_data + total_written,					    remaining)) != 0) {			if (written < 0) {#ifdef EINTR			    if (errno == EINTR)				continue;#endif /* EINTR */#ifdef ERESTARTSYS			    if (errno == ERESTARTSYS)				continue;#endif /* ERESTARTSYS */			    if (TRACE) {				perror("LYNXCGI: write() of POST data failed");			    }			    break;			}			if (TRACE) {			    fprintf(stderr,				    "LYNXCGI: Wrote %d bytes of POST data.\n",				    written);			}			total_written += written;			remaining -= written;			if (remaining == 0)			    break;		    }		    if (remaining != 0) {			if (TRACE)			    fprintf(stderr,				    "LYNXCGI: %d bytes remain unwritten!\n",				    remaining);		    }		    close(fd1[1]);		}				total_chars = 0;		while((chars = read(fd2[0], buf, sizeof(buf))) > 0) {		    char line[40];		    		    total_chars += chars;		    sprintf (line, "Read %d bytes of data.", total_chars);		    HTProgress(line);		    if (TRACE) {			fprintf(stderr, "LYNXCGI: Rx: %.*s\n", chars, buf);		    }		    		    (*target->isa->put_block)(target, buf, chars);		}#if !HAVE_WAITPID		while (wait(&wstatus) != pid)		    ; /* do nothing */#else		while (-1 == waitpid(pid, &wstatus, 0)) { /* wait for child */#ifdef EINTR		    if (errno == EINTR)			continue;#endif /* EINTR */#ifdef ERESTARTSYS		    if (errno == ERESTARTSYS)			continue;#endif /* ERESTARTSYS */		    break;		}#endif /* !HAVE_WAITPID */		close(fd2[0]);		status = HT_LOADED;			    } else if (pid == 0) { /* The Bad, */		char **argv = NULL;		char post_len[32];		int argv_cnt = 3; /* name, one arg and terminator */		char **cur_argv = NULL;		char buf[BUFSIZ];		/* Set up output pipe */		close(fd2[0]);		dup2(fd2[1], fileno(stdout)); /* Should check success code */		dup2(fd2[1], fileno(stderr));		close(fd2[1]);		sprintf(buf, "HTTP_ACCEPT_LANGUAGE=%.*s",			     (int)(sizeof(buf) - 22), language);		buf[(sizeof(buf) - 1)] = '\0';		add_environment_value(buf);		if (pref_charset) {		    cp = NULL;		    StrAllocCopy(cp, "HTTP_ACCEPT_CHARSET=");		    StrAllocCat(cp, pref_charset);		    add_environment_value(cp);		}		if (anAnchor->post_data &&		    anAnchor->post_content_type) {		    cp = NULL;		    StrAllocCopy(cp, "CONTENT_TYPE=");		    StrAllocCat(cp, anAnchor->post_content_type);		    add_environment_value(cp);		}		if (anAnchor->post_data) { /* post script, read stdin */		    close(fd1[1]);		    dup2(fd1[0], fileno(stdin));		    close(fd1[0]);		    /* Build environment variables */		    add_environment_value("REQUEST_METHOD=POST");		    sprintf(post_len, "CONTENT_LENGTH=%d",			    strlen(anAnchor->post_data));		    add_environment_value(post_len);		} else {		    close(fileno(stdin));		    if (anAnchor->isHEAD) {			add_environment_value("REQUEST_METHOD=HEAD");		    }		}		/* 		 * Set up argument line, mainly for <index> scripts		 */		if (pgm_args != NULL) {		    for (cp = pgm_args; *cp != '\0'; cp++) {			if (*cp == '+') {			    argv_cnt++;			}		    }		}		argv = (char**)malloc(argv_cnt * sizeof(char*));		if (argv == NULL) {		    outofmem(__FILE__, "LYCgi");		}		cur_argv = argv + 1;		/* For argv[0] */		if (pgm_args != NULL) {				    char *cr;		    /* Data for a get/search form */		    if (is_www_index) {			add_environment_value("REQUEST_METHOD=SEARCH");		    } else if (!anAnchor->isHEAD) {			add_environment_value("REQUEST_METHOD=GET");		    }		    		    cp = NULL;		    StrAllocCopy(cp, "QUERY_STRING=");		    StrAllocCat(cp, pgm_args);		    add_environment_value(cp);		    /*		     * Split up arguments into argv array		     */		    cp = pgm_args;		    cr = cp;		    while(1) {			if (*cp == '\0') {			    *(cur_argv++) = HTUnEscape(cr);			    break;			    			} else if (*cp == '+') {			    *cp++ = '\0';			    *(cur_argv++) = HTUnEscape(cr);			    cr = cp;			}			cp++;		    }		}		*cur_argv = NULL;	/* Terminate argv */				argv[0] = pgm;		/* Begin WebSter Mods  -jkt */                		if (LYCgiDocumentRoot != NULL) {		    /* Add DOCUMENT_ROOT to env */		    cp = NULL;		    StrAllocCopy(cp, "DOCUMENT_ROOT=");		    StrAllocCat(cp, LYCgiDocumentRoot);		    add_environment_value(cp);		}		if (path_info != NULL ) {		    /* Add PATH_INFO to env */		    cp = NULL;		    StrAllocCopy(cp, "PATH_INFO=");		    StrAllocCat(cp, path_info);		    add_environment_value(cp);		}		if (LYCgiDocumentRoot != NULL && path_info != NULL ) {		    /* Construct and add PATH_TRANSLATED to env */		    StrAllocCopy(document_root, LYCgiDocumentRoot);		    if (document_root[strlen(document_root) - 1] == '/') {			document_root[strlen(document_root) - 1] = '\0';		    }		    path_translated = document_root;		    StrAllocCat(path_translated, path_info);		    cp = NULL;		    StrAllocCopy(cp, "PATH_TRANSLATED=");		    StrAllocCat(cp, path_translated);		    add_environment_value(cp);		    FREE(path_translated);		}		/* End WebSter Mods  -jkt */		execve(argv[0], argv, env);		if (TRACE) {		    perror("LYNXCGI: execve failed");		}			    } else {	/* and the Ugly */		HTAlert(CONNECT_FAILED);		if (TRACE) {		    perror("LYNXCGI: fork() failed");		}		status = HT_NO_DATA;		close(fd1[0]);		close(fd1[1]);		close(fd2[0]);		close(fd2[1]);		status = -1;	    }	}	if (target != NULL) {	    (*target->isa->_free)(target);	}    }    FREE(path_info);    FREE(pgm);    FREE(orig_pgm);#else  /* VMS */	HTStream *target;	char buf[256];	target = HTStreamStack(WWW_HTML, 			       format_out,			       sink, anAnchor);	sprintf(buf,"<head>\n<title>Good Advice</title>\n</head>\n<body>\n");	(*target->isa->put_block)(target, buf, strlen(buf));		sprintf(buf,"<h1>Good Advice</h1>\n");	(*target->isa->put_block)(target, buf, strlen(buf));	sprintf(buf, "An excellent http server for VMS is available via <a\n");	(*target->isa->put_block)(target, buf, strlen(buf));	sprintf(buf,	 "href=\"http://kcgl1.eng.ohio-state.edu/www/doc/serverinfo.html\"\n");	(*target->isa->put_block)(target, buf, strlen(buf));	sprintf(buf, ">this link</a>.\n");	(*target->isa->put_block)(target, buf, strlen(buf));	sprintf(buf,		"<p>It provides <b>state of the art</b> CGI script support.\n");	(*target->isa->put_block)(target, buf, strlen(buf));	sprintf(buf,"</body>\n");	(*target->isa->put_block)(target, buf, strlen(buf));	(*target->isa->_free)(target);	status = HT_LOADED;#endif /* VMS */#else /* LYNXCGI_LINKS */    _statusline(CGI_NOT_COMPILED);    sleep(MessageSecs);    status = HT_NOT_LOADED;#endif /* LYNXCGI_LINKS */    return(status);}#ifdef GLOBALDEF_IS_MACRO#define _LYCGI_C_GLOBALDEF_1_INIT { "lynxcgi", LYLoadCGI, 0 }GLOBALDEF (HTProtocol,LYLynxCGI,_LYCGI_C_GLOBALDEF_1_INIT);#elseGLOBALDEF PUBLIC HTProtocol LYLynxCGI = { "lynxcgi", LYLoadCGI, 0 };#endif /* GLOBALDEF_IS_MACRO */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -