Ejemplo n.º 1
0
int cgiMain(void){
    cgiFilePtr file;
    int targetFile;
    mode_t  mode;
    char name[128];
    char fileNameOnServer[64];
    char contentType[1024];
    char buffer[BufferLen];
    char *tmpStr=NULL;
    int size;
    int got,t;
    cgiHeaderContentType("text/html");
    //取得html页面中file元素的值,应该是文件在客户机上的路径名
    if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
        fprintf(stderr,"could not retrieve filename\n");
        goto FAIL;
    }  
    cgiFormFileSize("file", &size);
    //取得文件类型,不过本例中并未使用
    cgiFormFileContentType("file", contentType, sizeof(contentType));
    //目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
    if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
        fprintf(stderr,"could not open the file\n");
        goto FAIL;
    }
    t=-1; 
    //从路径名解析出用户文件名
    while(1){
        tmpStr=strstr(name+t+1,"\\");
        if(NULL==tmpStr)
            tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
        if(NULL!=tmpStr)
            t=(int)(tmpStr-name);
        else
            break;
    } 
    strcpy(fileNameOnServer,"/app/update/");
    strcat(fileNameOnServer,name+t+1);
    mode=S_IRWXU|S_IRGRP|S_IROTH;    
    //在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件    
    targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
    if(targetFile<0){
        fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);
        goto FAIL;
    }
    //从系统临时文件中读出文件内容,并放到刚创建的目标文件中 
    while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
        if(got>0)
            write(targetFile,buffer,got);    
    }
    cgiFormFileClose(file);
    close(targetFile);
    goto END; 
FAIL:
    fprintf(stderr,"Failed to upload");
    return 1;
END:    
    printf("File \"%s\" has been uploaded",fileNameOnServer);
    return 0;
}
Ejemplo n.º 2
0
void File()
{
    cgiFilePtr file;
    char name[1024];
    char contentType[1024];
    char buffer[1024];
    int size;
    int got;
    if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {
        printf("<p>No file was uploaded.<p>\n");
        return;
    }
    fprintf(cgiOut, "The filename submitted was: ");
    cgiHtmlEscape(name);
    fprintf(cgiOut, "<p>\n");
    cgiFormFileSize("file", &size);
    fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
    cgiFormFileContentType("file", contentType, sizeof(contentType));
    fprintf(cgiOut, "The alleged content type of the file was: ");
    cgiHtmlEscape(contentType);
    fprintf(cgiOut, "<p>\n");
    fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.<p>\n");
    fprintf(cgiOut, "The file's contents are shown here:<p>\n");
    if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
        fprintf(cgiOut, "Could not open the file.<p>\n");
        return;
    }
    fprintf(cgiOut, "<pre>\n");
    while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
            cgiFormSuccess)
    {
        cgiHtmlEscapeData(buffer, got);
    }
    fprintf(cgiOut, "</pre>\n");
    cgiFormFileClose(file);
}
Ejemplo n.º 3
0
int cgiMain(void){
    cgiFilePtr file;
    int targetFile;
    mode_t  mode;
    char name[128];
    char fileNameOnServer[64];
    char contentType[1024];
    char buffer[BufferLen];
    char *tmpStr=NULL;
    int size;
    int got,t;
	 char cmd[256];
	long sessionId;            //会话ID
	char value[20];            //临时变量
	char username[40];         //用户名
	char actName[50];          //请求动作
	char msg[64];					//提示信息 
	int itemId = 0;				//网元ID 
	char cardtypes[128];
	char slots[128]; 
	 /* ==================基本校验 ====================== */
	
	/*  获取请求参数  */
	cgiFormString("sessionId",value,20);
	sessionId = atol(value);
	cgiFormString("username",username,40);
	cgiFormString("actName",actName,50);
	cgiFormString("QItemId",value,50);//网元ID
	itemId = atol(value);
	cgiFormString("cardtypes",cardtypes,128);
	cgiFormString("slots",slots,128);
	/* 检查session有效性  */
	//无效
	if(!isSessionValid(sessionId,username)){
		webLog("no");
		//无效session
		sprintf(msg,"未登录或会话已过期!");
		goto FAIL;
	}
	//有效
	else{
		webLog("yes");
		//更新session访问时间
		updateSessionService(sessionId);
	}
	
	/*  权限验证  */
	//权限不足
	if(!isAuthorityValid(actName,username)){
		//输出权限不足信息
		sprintf(msg,"您无此操作权限!");
		goto FAIL;
	}
	 
	
	//清除旧文件 
//	sprintf(cmd,"rm -f /backup/app.tar.gz ");
//	system(cmd);
	  
//	sprintf(cmd,"rm -rf %s/*",UPDATEDIR);
//	system(cmd);
	 
    //取得html页面中file元素的值,应该是文件在客户机上的路径名
    if (cgiFormFileName("updatePkg", name, sizeof(name)) !=cgiFormSuccess) {
        strcpy(msg,"系统出错:无法读取上传文件");
        goto FAIL;
    }  
    cgiFormFileSize("updatePkg", &size);
    if(size >30*1024*1024){
    	strcpy(msg,"不能上传大于30M的文件");
    	goto FAIL;
    }
    //取得文件类型,不过本例中并未使用
    cgiFormFileContentType("updatePkg", contentType, sizeof(contentType));
    //目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
    if (cgiFormFileOpen("updatePkg", &file) != cgiFormSuccess) {
        fprintf(stderr,"could not open the file\n");
        goto FAIL;
    }
    t=-1; 
    //从路径名解析出用户文件名
    while(1){
        tmpStr=strstr(name+t+1,"\\");
        if(NULL==tmpStr)
            tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
        if(NULL!=tmpStr)
            t=(int)(tmpStr-name);
        else
            break;
    } 
    strcpy(fileNameOnServer,"/app/webapp/httpd/html/file/");
    strcat(fileNameOnServer,name+t+1);
    mode=S_IRWXU|S_IRGRP|S_IROTH;  
    
    //删除同名文件 
    sprintf(cmd,"%s",fileNameOnServer);
    remove(cmd);
      
    //在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件    
    targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
    if(targetFile<0){
        fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);
        goto FAIL;
    }
    //从系统临时文件中读出文件内容,并放到刚创建的目标文件中 
    while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
        if(got>0)
            write(targetFile,buffer,got);    
    }
    cgiFormFileClose(file);
    close(targetFile);
    strcpy(msg,"升级文件上传成功!");
    
    
//    sprintf(cmd,"nohup /bin/Online &");
//	 system(cmd);
//	 sleep(1);
	 SendUpdateCmd(itemId,slots,cardtypes,name+t+1);
    goto END; 
FAIL:
   cgiHeaderContentType("text/html; charset=utf-8");
	printf("<html >\n");
	printf("<head> \n");
	printf("<title>Upload Information</title> \n");
	printf("</head> \n");
	printf("<body> \n");
	printf("<script type=\"text/javascript\">");
	//提示操作信息 
	printf("alert(\"%s\");",msg);	
	//提示信息后返回原页面 
	printf("window.location.href=\"/net/update.html\";");
	printf("</script>");
	printf("</body> \n");
	printf("</html> \n");
	return 0;
END:    
   cgiHeaderContentType("text/html; charset=utf-8");
	printf("<html >\n");
	printf("<head> \n");
	printf("<title>Upload Information</title> \n");
	printf("</head> \n");
	printf("<body> \n");
	printf("<script type=\"text/javascript\">");
	//提示操作信息 
	//printf("alert(\"%s\");",msg);	
	//提示信息后返回原页面 
	printf("window.location.href=\"/net/progress.html?itemId=%d\";",itemId);
	printf("</script>");
	printf("</body> \n");
	printf("</html> \n");
	return 0;
}
Ejemplo n.º 4
0
int cgiMain(void){
	cgiFilePtr file;
	int targetFile;
	mode_t mode;
	char name[128];
	char nameForCheck[64];
	char fileNameOnServer[64];
	char contentType[1024];
	char buffer[BufferLen];
	char *tmpStr=NULL;
	int size;
	int got,t;

	//取得html页面中file元素的值--文件在客户机上的路径名
	if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
	}
	cgiFormFileSize("file", &size);
	//取得文件类型
	cgiFormFileContentType("file", contentType, sizeof(contentType));
	//目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件
	if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
		goto FAIL;
	}
	t=-1;
	//从路径名解析出用户文件名
	while(1){
		tmpStr=strstr(name+t+1,"\\");
		if(NULL==tmpStr)
			tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
		if(NULL!=tmpStr)
			t=(int)(tmpStr-name);
		else
			break;
	}
	strcpy(fileNameOnServer,name+t+1);
	mode=S_IRWXU|S_IRGRP|S_IROTH;
	//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件
	targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
	if(targetFile<0){
		goto FAIL;
	}
	//从系统临时文件中读出文件内容,并放到刚创建的目标文件中
	while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
		if(got>0)
			write(targetFile,buffer,got);
	}
	cgiFormFileClose(file);
	close(targetFile);
	goto END;

	FAIL:
		cgiHeaderLocation("../upgrade_error.html");
		system("rm /tmp/*");
		return 1;
	END:
		//升级动作
		if(cgiFormString("upfile1",nameForCheck,64)==cgiFormSuccess){
			system("mv /app/www/cgi-bin/rtu /app/");
			system("chmod 777 /app/rtu");
		}else if(cgiFormString("upfile2",nameForCheck,64)==cgiFormSuccess){
			system("");//预留,以后做好安装包再说
		}

		//以下为网页部分
		printf("Content-type: text/html\r\n\r\n");
		printf("<html>");
		printf("<head>");
		printf("    <meta charset=\"utf-8\" content=\"text/html\">");
		printf("    <title>");
		printf("        ctt-2000e动环设备");
		printf("    </title>");
		printf("</head>");
		printf("<body style=\"background:#1782dd\">");
		printf("<div id=\"all\" style=\"width:500px;margin:250px auto;color:#fff;font-size:30px;text-align:center;line-height:60px;\">");
		printf("    升级成功<br>系统将在 <i id=\"time\" style=\"font-size:45px;\">5</i> 秒后重启");
		printf("</div>");
		printf("<script type=\"text/javascript\">");
		printf("    var id_of_time=setInterval(setTime,1000);");
		printf("    setTimeout(close,5000);");
		printf("    function setTime(){");
		printf("        var lastTime=document.getElementById(\"time\");");
		printf("        var number=lastTime.innerHTML;");
		printf("        number=number-1;");
		printf("        lastTime.innerHTML=number;");
		printf("    }");
		printf("    function close(){");
		printf("	clearInterval(id_of_time);");
		printf("	var all=document.getElementById(\"all\");");
		printf("	all.innerHTML=\"系统重启中,请稍后点此重试连接\";");
		printf("	all.onclick=function(){");
		printf("	    window.open(\"http://\"+location.hostname+\":\"+location.port,\"_self\",\"\");");
		printf("        };");
		printf("    }");
		printf("</script>");
		printf("</body>");
		printf("</html>");

		system("rm /tmp/*");
		system("/sbin/reboot");
		return 0;
}