boa+cgi上传文件超过1M报错问题

写在前面
今天需要使用页面上传bin包,文件大概是3.9mb,结果一直报错
POST /cgi-bin/Upgrade.cgi undefined
Host: 192.168.137.200:8888
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------279364548015623859994223247018
Content-Length: 4102997
Origin: http://192.168.137.200:8888
Sec-GPC: 1
Connection: keep-alive
Referer: http://192.168.137.200:8888/system-upgrade.html
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache
可以看到是没有定位到cgi的程序,百思不得其解,但是又必须得使用method=“post” enctype=“multipart/form-data”,最后发现boa有文件上传限制,默认是1mb。。。。


解决方法如下

  1. 修改boa.conf配置文件
# SinglePostLimit: The maximum allowable number of bytes in 
# a single POST.  Default is normally 1MB.
SinglePostLimit 16777216 #16MB

这里把它改为16mb

之后使用ps -aux |grep boa kill -9 进程号
重启./boa即可,上传就没有问题了

  1. 修改源码

如果是修改boa源码宏定义是修改src文件夹中define.h:

#define SINGLE_POST_LIMIT_DEFAULT 1024 * 1024 * 16 /* 16 MB */

然后重新编译代码即可

贴下cgi代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "cgic.h"

#define BufferLen 4096

static unsigned char xor_mask[] =
{
46,175,116,168,85,253,15,163,167,87,
121,198,175,136,79,125,156,10,72,135,
208,71,39,164,238,167,42,101,154,14, 
235,225,76,27,106,109,193,208,89,35,
101,37,12,121,241,205,7,227,169,30, 
109,80,203,49,245,169,26,111,218,156,
145,78,186,219,215,80,28,186,60,191,
33,23,109,144,131,139,52,10,29,254,242,
236,30,144,84,124,46,95,30,240,217,211,
248,32,48,28,243,144
};


int cgiMain(int argc, char *argv[]) {
	cgiFilePtr file;
	int targetFile;
	
	char name[128];
	char fileNameOnServer[64];
	char contentType[1024];
	char buffer[BufferLen];
	int filelen;
	int fdNew, fdOld;
	char *msg;
	FILE *New, *Old;
	struct stat buf;
	
	int size;
	int got;
	int i;
	int ret = 0;
	int ret1 = 0;
	cgiHeaderContentType("text/html; charset=UTF-8");
	
	mkdir("/tmp/upgrade", 0755);
	
	if (cgiFormFileName("file_Upgrade", name, sizeof(name)) != cgiFormSuccess) {
		fprintf(cgiOut,"could not retrieve filename\n");
		goto FAIL;
	}

	cgiFormFileSize("file_Upgrade", &size);

	cgiFormFileContentType("file_Upgrade", contentType, sizeof(contentType));

	if (cgiFormFileOpen("file_Upgrade", &file) != cgiFormSuccess) {
		fprintf(cgiOut,"could not open the file\n");
		goto FAIL;
	}

	targetFile = open ("/tmp/upgrade/test", O_RDWR|O_CREAT|O_TRUNC|O_APPEND, 0644);
	if(targetFile<0){
		fprintf(cgiOut,"could not create the new file,%s\n", fileNameOnServer);
		goto FAIL;
	}

	while (cgiFormFileRead(file, buffer, BufferLen, &got) == cgiFormSuccess){
		ret = 0;
		if (got > 0) {
			while (ret < got) {
				ret += write(targetFile, buffer + ret, got - ret);
			}
		}
	}
	ret = 0;
	cgiFormFileClose(file);
	close(targetFile);
	goto END;
FAIL:
	fprintf(cgiOut, "Failed to upload");
	return 1;
END:
	stat("/tmp/upgrade/test", &buf);
	filelen = buf.st_size;
	New = fopen("/tmp/upgrade/upgrade.tar.gz", "wb");
	if (New == NULL) {
		return -1;	
	}
	
	Old = fopen("/tmp/upgrade/test", "rb");
	if (Old == NULL) {
		return -1;	
	}
	fdNew = fileno(New);
	fdOld = fileno(Old);
	msg = (char *)malloc(filelen * sizeof(char));
	memset(msg, 0, filelen);

    ret1 = 0;

    while((ret = fread(msg, sizeof(char), 1024, Old)) > 0 )
    {
        for (i = ret1; i < ret + ret1; i++) {
            msg[i - ret1] = msg[i - ret1] ^ xor_mask[i % sizeof(xor_mask)];
        }
        ret1 += ret;
        fwrite(msg, sizeof(char), ret, New);
    }

    ret1 = 0;
    free(msg);
    fflush(Old);
    fflush(New);
    fsync(fdOld);
    fsync(fdNew);
    fclose(New);
    fclose(Old);

	system("tar -xf /tmp/upgrade/upgrade.tar.gz -C /tmp/upgrade");
    
	return 0;
}

你可能感兴趣的:(ubuntu,linux)