c实现cgi的编写

系统:OS X EI Capitancss

首先配置apache对CGI的支持html

编辑apache配置文件/Applications/XAMPP/xamppfiles/apache2/conf/httpd.confshell

取消注释apache

描述什么样的文件视如cgi文件,好比以`.cgi`结尾的文件
AddHandlercgi-script .cgi     
返回服务器返回形式
AddType text/html.shtml       

AddOutputFilterINCLUDES .shtml

检查cgi_module是否被注释掉了:
LoadModule cgi_module modules/mod_cgi.so

找到并在原有内容上添加如下代码
<Directory>
Options Indexes FollowSymLinks MultiViews ExecCGI
DirectoryIndex index.html index.cgi
AllowOverride None
Order allow,deny
Allow from all
</Directory>

重启 apacheapi

首先编写一个简单的CGI文件服务器

#include<stdio.h>
int main(){
printf("Content-Type:text/html;charset=gbk\r\n\r\n");
printf("<html><head></head><body> fuck </body></html>");
}

此处博主写了一个简单的shell脚本,直接对.c文件进行检查而且用gcc编译ide

#!/bin/sh
read filename
#获取前缀名称
prefix=${filename%.*}
#获取后缀扩展名称
extension=${filename#*.}

if [ "$extension" = "c" ]

then

result= ` gcc -o $prefix $filename`
    
 if [ $? -eq 0 ]
       
  then

  echo "succsss create$prefix.c"
 fi
 exit 0
fi
exit 0

经过访问cgi,返回htmlcode

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
	得到用户请求的查询字符串
	char * qs = getenv("QUERY_STRING");
char name[250] = {0};
sscanf(qs,"name=%s",name);
if(strcmp(name,"admin")==0){
 printf("Location:1.html\r\n\r\n");
}else{
	printf("Content-Type:text/html;charset=gbk\r\n\r\n");
	printf("<html><head></head><body> <font color='red'>Fuck u man</font> </body></html>");
}


	printf("Location:https://www.baidu.com\r\n");
printf("Content-Type:text/html;charset=gbk\r\n\r\n");
printf("<html><head></head><body> <font color='red'>%s</font> </body></html>");
}

调用写好的cgicorm

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cgic.h"
#include"cgic.c"
int cgiMain(){

	char name[256] = {0};
	char password[256] = {0};
	cgiHeaderContentType("text/html;charset=gbk");
	if(cgiFormString("name",name,sizeof(name))!=cgiFormSuccess){
		fprintf(cgiOut,"<html><head></head><body><font color='red'>请填写用户名</font></body></html>");

	}
	if(cgiFormString("password",password,sizeof(password))!=cgiFormSuccess){
		fprintf(cgiOut,"<html><head></head><body><font color='red'>请填写密码</font></body></html>");
	}

	if(strcmp(name,"admin")==0){
		cgiHeaderLocation("https://www.baidu.com");
	}else{
		fprintf(cgiOut,"<html><head></head><body><font color='red'>失败</font></body></html>");

	}
相关文章
相关标签/搜索