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

📄 cgic.c

📁 一般的UNIX系统都支持ANSI C,增加相应的库函数(和相应的h文件)就可以实现CGI,用于CGI编程的ANSI C库
💻 C
📖 第 1 页 / 共 4 页
字号:
	}	if (!cgiWriteString(out, cgiPathInfo)) {		goto error;	}	if (!cgiWriteString(out, cgiPathTranslated)) {		goto error;	}	if (!cgiWriteString(out, cgiScriptName)) {		goto error;	}	if (!cgiWriteString(out, cgiQueryString)) {		goto error;	}	if (!cgiWriteString(out, cgiRemoteHost)) {		goto error;	}	if (!cgiWriteString(out, cgiRemoteAddr)) {		goto error;	}	if (!cgiWriteString(out, cgiAuthType)) {		goto error;	}	if (!cgiWriteString(out, cgiRemoteUser)) {		goto error;	}	if (!cgiWriteString(out, cgiRemoteIdent)) {		goto error;	}	if (!cgiWriteString(out, cgiContentType)) {		goto error;	}	if (!cgiWriteString(out, cgiAccept)) {		goto error;	}	if (!cgiWriteString(out, cgiUserAgent)) {		goto error;	}	if (!cgiWriteString(out, cgiReferrer)) {		goto error;	}	if (!cgiWriteString(out, cgiCookie)) {		goto error;	}	if (!cgiWriteInt(out, cgiContentLength)) {		goto error;	}	e = cgiFormEntryFirst;	while (e) {		cgiFilePtr fp;		if (!cgiWriteString(out, e->attr)) {			goto error;		}		if (!cgiWriteString(out, e->value)) {			goto error;		}		/* New 2.0 fields and file uploads */		if (!cgiWriteString(out, e->fileName)) {			goto error;		}		if (!cgiWriteString(out, e->contentType)) {			goto error;		}		if (!cgiWriteInt(out, e->valueLength)) {			goto error;		}		if (cgiFormFileOpen(e->attr, &fp) == cgiFormSuccess) {			char buffer[1024];			int got;			if (!cgiWriteInt(out, 1)) {				cgiFormFileClose(fp);				goto error;			}			while (cgiFormFileRead(fp, buffer, 				sizeof(buffer), &got) == cgiFormSuccess)			{				if (((int) fwrite(buffer, 1, got, out)) != got) {					cgiFormFileClose(fp);					goto error;				}			}			if (cgiFormFileClose(fp) != cgiFormSuccess) {				goto error;			}		} else {			if (!cgiWriteInt(out, 0)) {				goto error;			}		}		e = e->next;	}	fclose(out);	return cgiEnvironmentSuccess;error:	fclose(out);	/* If this function is not defined in your system,		you must substitute the appropriate 		file-deletion function. */	unlink(filename);	return cgiEnvironmentIO;}static int cgiWriteString(FILE *out, char *s) {	int len = (int) strlen(s);	cgiWriteInt(out, len);	if (((int) fwrite(s, 1, len, out)) != len) {		return 0;	}	return 1;}static int cgiWriteInt(FILE *out, int i) {	if (!fwrite(&i, sizeof(int), 1, out)) {		return 0;	}	return 1;}static int cgiReadString(FILE *out, char **s);static int cgiReadInt(FILE *out, int *i);cgiEnvironmentResultType cgiReadEnvironment(char *filename) {	FILE *in;	cgiFormEntry *e = 0, *p;	char *version;	cgiEnvironmentResultType result;	/* Free any existing data first */	cgiFreeResources();	/* Be sure to open in binary mode */	in = fopen(filename, "rb");	if (!in) {		/* Can't access file */		return cgiEnvironmentIO;	}	if (!cgiReadString(in, &version)) {		goto error;	}	if (strcmp(version, "CGIC" CGIC_VERSION)) {		/* 2.02: Merezko Oleg */		free(version);		return cgiEnvironmentWrongVersion;	}		/* 2.02: Merezko Oleg */	free(version);	if (!cgiReadString(in, &cgiServerSoftware)) {		goto error;	}	if (!cgiReadString(in, &cgiServerName)) {		goto error;	}	if (!cgiReadString(in, &cgiGatewayInterface)) {		goto error;	}	if (!cgiReadString(in, &cgiServerProtocol)) {		goto error;	}	if (!cgiReadString(in, &cgiServerPort)) {		goto error;	}	if (!cgiReadString(in, &cgiRequestMethod)) {		goto error;	}	if (!cgiReadString(in, &cgiPathInfo)) {		goto error;	}	if (!cgiReadString(in, &cgiPathTranslated)) {		goto error;	}	if (!cgiReadString(in, &cgiScriptName)) {		goto error;	}	if (!cgiReadString(in, &cgiQueryString)) {		goto error;	}	if (!cgiReadString(in, &cgiRemoteHost)) {		goto error;	}	if (!cgiReadString(in, &cgiRemoteAddr)) {		goto error;	}	if (!cgiReadString(in, &cgiAuthType)) {		goto error;	}	if (!cgiReadString(in, &cgiRemoteUser)) {		goto error;	}	if (!cgiReadString(in, &cgiRemoteIdent)) {		goto error;	}	if (!cgiReadString(in, &cgiContentType)) {		goto error;	}	if (!cgiReadString(in, &cgiAccept)) {		goto error;	}	if (!cgiReadString(in, &cgiUserAgent)) {		goto error;	}	if (!cgiReadString(in, &cgiReferrer)) {		goto error;	}	/* 2.0 */	if (!cgiReadString(in, &cgiCookie)) {		goto error;	}	if (!cgiReadInt(in, &cgiContentLength)) {		goto error;	}	p = 0;	while (1) {		int fileFlag;		e = (cgiFormEntry *) calloc(1, sizeof(cgiFormEntry));		if (!e) {			cgiFreeResources();			fclose(in);			return cgiEnvironmentMemory;		}		memset(e, 0, sizeof(cgiFormEntry));		if (!cgiReadString(in, &e->attr)) {			/* This means we've reached the end of the list. */			/* 2.02: thanks to Merezko Oleg */			free(e);			break;		}		if (!cgiReadString(in, &e->value)) {			goto outOfMemory;		}		if (!cgiReadString(in, &e->fileName)) {			goto outOfMemory;		}		if (!cgiReadString(in, &e->contentType)) {			goto outOfMemory;		}		if (!cgiReadInt(in, &e->valueLength)) {			goto outOfMemory;		}		if (!cgiReadInt(in, &fileFlag)) {			goto outOfMemory;		}		if (fileFlag) {			char buffer[1024];			FILE *out;			char tfileName[1024];			int got;			int len = e->valueLength;			if (getTempFileName(tfileName)				!= cgiParseSuccess)			{				result = cgiEnvironmentIO;				goto error;			}			out = fopen(tfileName, "w+b");			if (!out) {				result = cgiEnvironmentIO;				goto error;			}			while (len > 0) {						/* 2.01: try is a bad variable name in					C++, and it wasn't being used					properly either */				int tryr = len;				if (tryr > ((int) sizeof(buffer))) {					tryr = sizeof(buffer);				}				got = fread(buffer, 1, tryr, in);				if (got <= 0) {					result = cgiEnvironmentIO;					fclose(out);					unlink(tfileName);					goto error;				}				if (((int) fwrite(buffer, 1, got, out)) != got) {					result = cgiEnvironmentIO;					fclose(out);					unlink(tfileName);					goto error;				}				len -= got;			}			rewind(out);			e->tfileName = (char *) malloc((int) strlen(tfileName) + 1);			if (!e->tfileName) {				result = cgiEnvironmentMemory;				unlink(tfileName);				goto error;			}			strcpy(e->tfileName, tfileName);		} else {			e->tfileName = (char *) malloc(1);			if (!e->tfileName) {				result = cgiEnvironmentMemory;				goto error;			}		}			e->next = 0;		if (p) {			p->next = e;		} else {			cgiFormEntryFirst = e;		}			p = e;	}	fclose(in);	cgiRestored = 1;	return cgiEnvironmentSuccess;outOfMemory:	result = cgiEnvironmentMemory;error:	cgiFreeResources();	fclose(in);	if (e) {		if (e->attr) {			free(e->attr);		}		if (e->value) {			free(e->value);		}		if (e->fileName) {			free(e->fileName);		}		if (e->contentType) {			free(e->contentType);		}		if (e->tfileName) {			free(e->tfileName);		}		free(e);	}	return result;}static int cgiReadString(FILE *in, char **s) {	int len;	/* 2.0 fix: test cgiReadInt for failure! */ 	if (!cgiReadInt(in, &len)) {		return 0;	}	*s = (char *) malloc(len + 1);	if (!(*s)) {		return 0;	}		if (((int) fread(*s, 1, len, in)) != len) {		return 0;	}	(*s)[len] = '\0';	return 1;}static int cgiReadInt(FILE *out, int *i) {	if (!fread(i, sizeof(int), 1, out)) {		return 0;	}	return 1;}static int cgiStrEqNc(char *s1, char *s2) {	while(1) {		if (!(*s1)) {			if (!(*s2)) {				return 1;			} else {				return 0;			}		} else if (!(*s2)) {			return 0;		}		if (isalpha(*s1)) {			if (tolower(*s1) != tolower(*s2)) {				return 0;			}		} else if ((*s1) != (*s2)) {			return 0;		}		s1++;		s2++;	}}static int cgiStrBeginsNc(char *s1, char *s2) {	while(1) {		if (!(*s2)) {			return 1;		} else if (!(*s1)) {			return 0;		}		if (isalpha(*s1)) {			if (tolower(*s1) != tolower(*s2)) {				return 0;			}		} else if ((*s1) != (*s2)) {			return 0;		}		s1++;		s2++;	}}static char *cgiFindTarget = 0;static cgiFormEntry *cgiFindPos = 0;static cgiFormEntry *cgiFormEntryFindFirst(char *name) {	cgiFindTarget = name;	cgiFindPos = cgiFormEntryFirst;	return cgiFormEntryFindNext();}static cgiFormEntry *cgiFormEntryFindNext() {	while (cgiFindPos) {		cgiFormEntry *c = cgiFindPos;		cgiFindPos = c->next;		if (!strcmp(c -> attr, cgiFindTarget)) {			return c;		}	}	return 0;}static int cgiFirstNonspaceChar(char *s) {	int len = strspn(s, " \n\r\t");	return s[len];}void cgiStringArrayFree(char **stringArray) {	char *p;	char **arrayItself = stringArray;	p = *stringArray;	while (p) {		free(p);		stringArray++;		p = *stringArray;	}	/* 2.0: free the array itself! */	free(arrayItself);}	cgiFormResultType cgiCookies(char ***result) {	char **stringArray;	int i;	int total = 0;	char *p;	char *n;	p = cgiCookie;	while (*p) {		if (*p == '=') {			total++;		}		p++;	}	stringArray = (char **) malloc(sizeof(char *) * (total + 1));	if (!stringArray) {		*result = 0;		return cgiFormMemory;	}	/* initialize all entries to null; the last will stay that way */	for (i=0; (i <= total); i++) {		stringArray[i] = 0;	}	i = 0;	p = cgiCookie;	while (*p) {		while (*p && isspace(*p)) {			p++;		}		n = p;		while (*p && (*p != '=')) {			p++;		}		if (p != n) {			stringArray[i] = (char *) malloc((p - n) + 1);			if (!stringArray[i]) {				cgiStringArrayFree(stringArray);				*result = 0;				return cgiFormMemory;			}				memcpy(stringArray[i], n, p - n);			stringArray[i][p - n] = '\0';			i++;		}		while (*p && (*p != ';')) {			p++;			}		if (!*p) {			break;		}		if (*p == ';') {			p++;		}	}	*result = stringArray;	return cgiFormSuccess;}cgiFormResultType cgiFormEntries(char ***result) {	char **stringArray;	cgiFormEntry *e, *pe;	int i;	int total = 0;	e = cgiFormEntryFirst;	while (e) {		/* Don't count a field name more than once if			multiple values happen to be present for it */		pe = cgiFormEntryFirst;		while (pe != e) {			if (!strcmp(e->attr, pe->attr)) {				goto skipSecondValue;			}			pe = pe->next;							}		total++;skipSecondValue:		e = e->next;	}	stringArray = (char **) malloc(sizeof(char *) * (total + 1));	if (!stringArray) {		*result = 0;		return cgiFormMemory;	}	/* initialize all entries to null; the last will stay that way */	for (i=0; (i <= total); i++) {		stringArray[i] = 0;	}	/* Now go get the entries */	e = cgiFormEntryFirst;	i = 0;	while (e) {		int space;		/* Don't return a field name more than once if			multiple values happen to be present for it */		pe = cgiFormEntryFirst;		while (pe != e) {			if (!strcmp(e->attr, pe->attr)) {				goto skipSecondValue2;			}			pe = pe->next;							}				space = (int) strlen(e->attr) + 1;		stringArray[i] = (char *) malloc(space);		if (stringArray[i] == 0) {			/* Memory problems */			cgiStringArrayFree(stringArray);			*result = 0;			return cgiFormMemory;		}			strcpy(stringArray[i], e->attr);		i++;skipSecondValue2:		e = e->next;	}	*result = stringArray;	return cgiFormSuccess;}#define TRYPUTC(ch) \	{ \		if (putc((ch), cgiOut) == EOF) { \			return cgiFormIO; \		} \	} cgiFormResultType cgiHtmlEscapeData(char *data, int len){	while (len--) {		if (*data == '<') {			TRYPUTC('&');			TRYPUTC('l');			TRYPUTC('t');			TRYPUTC(';');		} else if (*data == '&') {			TRYPUTC('&');			TRYPUTC('a');			TRYPUTC('m');			TRYPUTC('p');			TRYPUTC(';');		} else if (*data == '>') {			TRYPUTC('&');			TRYPUTC('g');			TRYPUTC('t');			TRYPUTC(';');		} else {			TRYPUTC(*data);		}		data++;	}	return cgiFormSuccess;}cgiFormResultType cgiHtmlEscape(char *s){	return cgiHtmlEscapeData(s, (int) strlen(s));}/* Output data with the " character HTML-escaped, and no	other characters escaped. This is useful when outputting	the contents of a tag attribute such as 'href' or 'src'.	'data' is not null-terminated; 'len' is the number of	bytes in 'data'. Returns cgiFormIO in the event	of error, cgiFormSuccess otherwise. */cgiFormResultType cgiValueEscapeData(char *data, int len){	while (len--) {		if (*data == '\"') {			TRYPUTC('&');			TRYPUTC('#');			TRYPUTC('3');			TRYPUTC('4');			TRYPUTC(';');		} else {			TRYPUTC(*data);		}		data++;	}	return cgiFormSuccess;}cgiFormResultType cgiValueEscape(char *s){	return cgiValueEscapeData(s, (int) strlen(s));}

⌨️ 快捷键说明

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