Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: cgi.c
发信站: 哈工大紫丁香 (2002年06月09日21:23:53 星期天), 站内信件
/*
* NoseyParker, the search engine for FTP archives
* Copyright (C) 1993-96 by Jiri A. Randus
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* The author can be reached as follows:
* Internet: <Jiri.Randus@vslib.cz>
* Phone: ++42 48 5227374
* SnailMail: Jiri Randus
* KIN HF TU v Liberci
* Halkova 6
* 46117 Liberec
* Czech Republic
*/
#define CGI_VERSION "2.4"
#include "parker.h"
char Line[MAX];
unsigned long lines;
int i;
char search[MAX]="";
char typeS[MAX]="substring";
char Submit[MAX];
char Hits[MAX];
char Restart[MAX];
char TypeSearch[MAX];
int Mozilla=0;
void EnConv(char *s)
{
for(;*s;s++) if(isalpha(*s) || isdigit(*s)) printf("%c",*s);
else printf("%%%02x",*s);
}
void Conv(char *s)
{
char num[3];
int n;
if(!s) return;
num[2]='\0';
for(;*s;s++)
{
if(*s=='+') *s=' ';
else if(*s=='%') {
num[0]=*(s+1);
num[1]=*(s+2);
sscanf(num,"%x",&n);
*s=n;
strcpy(s+1,s+3);
}
}
}
void PutHeaders(void)
{
puts("Content-type:text/html\n\n");
// puts("");
puts("<html><head><TITLE>查询结果</TITLE></head>\n");
puts("<BODY BGCOLOR=#ffffff background='/images/summerbg.jpg'>");
{
puts("<IMG SRC=\"/image/9j.gif\" ALIGN=RIGHT");
puts("<dir><dir><H2>9# FTP 搜索引擎</H2>");
puts("<style>");
puts("a { color: rgb(0,0,150) ; Text-decoration:none ; font-weight : none
}");
puts("a:hover {color: rgb(250,40,40);text-decoration:underline; font-weight
: none}");
puts("</style>");
}
printf("我们为您找到了如下站点/文件:");
puts("<BR CLEAR=ALL>");
fflush(stdout);
}
void Search(void)
{
int fd[2];
FILE *in;
char *ptr,*ptr2;
char *extras;
char *newpath;
char *file;
char host[MAX];
char path[MAX];
int dir;
char RestartKey[MAX];
size_t skippath=1;
if(!strlen(typeS)) strcpy(typeS,"substring");
lines=0;
PutHeaders();
if(strlen(search) >= MINLENGTHOFSEARCH) {
printf("<H3>以 %s 方式查询 <I><font color='#990000'>%s</font></I>字符串&
nbsp; <a href='#More'>新的搜索</a></H3>\n",
(strcmp(typeS,"substring")==0)?"子串":"全部匹配",search);
if(Mozilla) printf("<FONT SIZE=\"-1\">按 ");
if(!strcmp(TypeSearch,"files"))
printf("<B>文件</B>排序: <EM>点击文件名可看到详细说明</EM>");
else printf("<B>主机</B>排序: <EM>点击文件名可看到详细说明</EM>");
if(Mozilla) printf("</FONT>");
printf("<P><table width='90%c'><tr><td>",'%');
fflush(stdout);
pipe(fd);
if(!fork()) {
close(fd[0]);
PQuery(typeS,search,fd[1],Hits,Restart,TypeSearch);
exit(0);
}
close(fd[1]);
*host=*path='\0';
if(!(in=fdopen(fd[0],"r"))) {perror("fdopen");return;}
*RestartKey='\0';
while(1)
{
fgets(Line,MAX,in);
if(feof(in)) break;
if((ptr=strchr(Line,CR))) *ptr='\0';
if((ptr=strchr(Line,LF))) *ptr='\0';
if(*Line=='@') {strcpy(RestartKey,Line+1); break;}
lines++;
if(!(ptr=strchr(Line,':'))) continue;
*ptr++='\0';
while(*ptr==' ') ptr++;
newpath=ptr;
if(extras=strchr(ptr,' ')) *extras++='\0';
if(newpath[strlen(newpath)-1]=='/') {
newpath[strlen(newpath)-1]='\0';
dir=1;
}
else dir=0;
ptr2=strrchr(newpath,'/');
if(!ptr2) {
newpath="";
file=ptr;
}
else {
*ptr2++='\0';
if(!strlen(newpath)) newpath="/";
file=ptr2;
}
if(*Line!='*') { /* not a `files' list */
if(strcmp(host,Line)) { /* Another server */
if(*host) printf("</BLOCKQUOTE>");
/* Find out how many chars to skip... damn those No-Well servers *
/
skippath=PSkip(Line);
strcpy(host,Line);
*path='\0';
printf("<H3><IMG SRC=\"/Parker/images/host.gif\" WIDTH=32 HEIGHT=3
2>");
printf(" <A HREF=\"ftp://%s/\">%s</A></H3>\n<BLOCKQUOTE>",host,hos
t);
}
if(strcmp(newpath,path)) {
strcpy(path,newpath);
printf("<IMG SRC=\"/Parker/images/dir.gif\" WIDTH=16 HEIGHT=12>");
printf(" <A HREF=\"ftp://%s/%s%s\">%s</A><BR>\n",host,path+skippat
h,
(strlen(path)>skippath)?"/":"",path);
}
if(dir)
printf("<IMG SRC=\"/Parker/images/diritem.gif\" WIDTH=42 HEIGHT=12
>");
else printf("<IMG SRC=\"/Parker/images/file.gif\" WIDTH=40 HEIGHT=12
>");
printf(" <A HREF=\"ftp://%s/%s%s%s%s\">%s</A>",host,path+skippath,
(strlen(path)>1)?"/":"", file,
(dir&&(strlen(path)>skippath))?"/":"", file);
}
else { /* a `files' list */
if(strcmp(newpath,path)) {
strcpy(path,newpath);
printf("<IMG SRC=\"/Parker/images/dir.gif\" WIDTH=16 HEIGHT=12>");
printf(" <EM>...%s</EM><BR>\n",path);
}
if(dir)
printf("<IMG SRC=\"/Parker/images/diritem.gif\" WIDTH=42 HEIGHT=12
>");
else printf("<IMG SRC=\"/Parker/images/file.gif\" WIDTH=40 HEIGHT=12
>");
printf(" <A HREF=\"/cgi-bin/parker/cgi?query="); EnConv(file);
printf("%s&search=wildcards&hits=%s&sortby=hosts",dir?"/":"",Hits);
printf("\">%s</A>",file);
}
if(extras) {
for(ptr=extras;*ptr;ptr++) if(*ptr==' ') break;
*ptr++='\0';
ptr2=ptr;
for(;*ptr;ptr++) if(*ptr=='_') *ptr=' ';
if(*ptr2) printf(", <EM>%s</EM>",ptr2);
printf(", <EM>%s</EM> 字节",extras);
}
puts("<BR>");
}
if(*host) printf("</BLOCKQUOTE>");
wait(NULL);
if(!lines) printf("奇怪,没有找到呀?<BR>");
else {
printf("</td></tr></table><BR><B>找到%lu 个对象",lines);
if(*RestartKey) { /* Can be restarted */
printf(", 点击这里 <A HREF=\"/cgi-bin/parkernew/cgi?");
printf("query="); EnConv(search);
printf("&search="); EnConv(typeS);
printf("&sortby="); EnConv(TypeSearch);
printf("&hits="); EnConv(Hits);
printf("&restart="); EnConv(RestartKey);
printf("\">查找更多项目</A>.");
}
}
}
else {
printf("<FONT SIZE=\"+1\">您输入的字符串小于%d个字节,",MINLENGTHOFSEARCH
);
printf("您的IP是:%s!",getenv("REMOTE_ADDR"));
}
printf("<a name='More'>");
printf("<hr size=3><FORM ACTION='/cgi-bin/parkernew/cgi' ID='s'><h2><font co
lor=#9C0000>新的搜索</font></h2>");
printf("<table><tr><td>");
printf("查找目标: </td><td ALIGN='LEFT'><INPUT TYPE='text' NAME='query' SIZ
E='25' ID='q'></Td><td>");
printf("查找方式: </td><td> <SELECT NAME='search'>");
printf(" <OPTION VALUE='substring'>查询子串");
printf(" <OPTION VALUE='wildcards'>通配符");
printf(" </SELECT></td></tr>");
printf("<tr><td> 显示结果数</td><td> <SELECT NAME='hits'>");
printf(" <OPTION VALUE='50'>50个");
printf(" <OPTION VALUE='100'>100个");
printf(" <OPTION VALUE='200'>200个");
printf(" </SELECT></Td><td>");
printf("排序: </td><td><SELECT NAME='sortby'>");
printf(" <OPTION VALUE='hosts'>按主机");
printf(" <OPTION VALUE='files'>按文件");
printf(" </SELECT></td></tr>");
printf("<tr><td COLSPAN='5'>");
printf("<dir><dir><BR><INPUT TYPE='submit' NAME='Submit' VALUE=' 查询 ' O
NCLICK='if(s.q.value==\"\")window.close()' >");
printf("<INPUT TYPE='reset' VALUE= ' 清除 '></FORM></td></tr></table>");
puts("<BR><HR>");
puts("<font color='#000000'>本页由 ");
puts("<A HREF=\"http://166.111.163.3\" TARGET=_top>清华大学酒井站</A>提供.
<IMG align=right SRC=\"/image/9j.gif\">");
puts("</BODY></HTML>");
}
void Redirect(char *path)
{
printf("Location: http://%s%s\n",SERVER,path);
printf("Content-type:text/html\n\n");
puts("<HEAD><TITLE>The document is here</TITLE></HEAD>");
puts("<BODY>The requested document is");
printf("<A HREF=\"http://%s%s\">here</A></BODY>\n",SERVER,path);
}
/* Either illegal invocation, or a form unable client */
void PutIndex(void)
{
puts("Content-type:text/html\n\n");
puts("<!-- This is a CGI script that can be executed only");
puts(" from within the environment of an HTTP server !!! -->\n");
puts("<HTML><HEAD><TITLE>Parker Query</TITLE><ISINDEX></HEAD>");
puts("<BODY>If the above search doesn't work for you, use");
printf("<A HREF=\"telnet://parker@%s/\">the terminal interface</A>.\n",SER
VER);
puts("</BODY></HTML>");
return;
}
int main(void)
{
char *query,*pptr;
char *agent;
alarm(CGIALARM);
query=getenv(QUERYSTRING);
agent=getenv(HTTPUSERAGENT);
Mozilla=((agent) && (!strncasecmp(agent,MOZILLA,strlen(MOZILLA))));
/* No query? */
if(!query || !*query) {PutIndex();return(0);}
/* Could be even a query through <ISINDEX> */
if(!strchr(query,'&'))
{
if(!strncmp(query,"query=",6))query+=6;
strcpy(search,query);
}
else while(query && *query)
{
if(!strncmp(query,"query=",6)) {
query+=6;
strcpy(search,query);
if(pptr=strchr(search,'&')) *pptr='\0';
query=strchr(query,'&');
if(query) query++;
}
else if(!strncmp(query,"Submit=",7))
{
query+=7;
strcpy(Submit,query);
if(pptr=strchr(Submit,'&')) *pptr='\0';
query=strchr(query,'&');
if(query) query++;
}
else if(!strncmp(query,"search=",7))
{
query+=7;
strcpy(typeS,query);
if(pptr=strchr(typeS,'&')) *pptr='\0';
query=strchr(query,'&');
if(query) query++;
}
else if(!strncmp(query,"hits=",5)) {
query+=5;
strcpy(Hits,query);
if(pptr=strchr(Hits,'&')) *pptr='\0';
query=strchr(query,'&');
if(query) query++;
}
else if(!strncmp(query,"restart=",8)) {
query+=8;
strcpy(Restart,query);
if(pptr=strchr(Restart,'&')) *pptr='\0';
query=strchr(query,'&');
if(query) query++;
}
else if(!strncmp(query,"sortby=",7)) {
query+=7;
strcpy(TypeSearch,query);
if(pptr=strchr(TypeSearch,'&')) *pptr='\0';
query=strchr(query,'&');
if(query) query++;
}
else query++;
}
/* Convert from the `%' notation */
Conv(typeS);
Conv(search);
Conv(Submit);
Conv(Hits);
Conv(Restart);
/* Depending on Submit, redirect to the appropriate docs or search. */
if(!strcasecmp(Submit,"Help")) {
if(Mozilla) Redirect("/Parker/Netscape/help.shtml");
else Redirect("/Parker/Common/help.shtml");
}
else if(!strcasecmp(Submit,"About")) {
if(Mozilla) Redirect("/Parker/Netscape/about.shtml");
else Redirect("/Parker/Common/about.shtml");
}
else if(!strcasecmp(Submit,"Stats")) {
if(Mozilla) Redirect("/Parker/Netscape/stats.shtml");
else Redirect("/Parker/Common/stats.shtml");
}
else Search();
return(0);
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:205.664毫秒