goahead5.1.1移植arm Linux开发板

1.goahead源码获取

        链接:https://pan.baidu.com/s/1JH60KeTF3AoAhbb1dMIWnQ 
        提取码:1111

2.修改源码

2.1修改IP地址

在http.c中setLocalHost()中将以下所有类似代码注释掉

struct hostent  *hp;
    if ((hp = gethostbyname(host)) == NULL) {
        error("Cannot get host address for host %s: errno %d", host, errno);
        return -1;
    }
    memcpy((char*) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length);
    ipaddr = inet_ntoa(intaddr);

在注释代码后加入以下代码

/*struct hostent  *hp;
    if ((hp = gethostbyname(host)) == NULL) {
        error("Cannot get host address for host %s: errno %d", host, errno);
        return -1;
    }
    memcpy((char*) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length);
    ipaddr = inet_ntoa(intaddr);*/
    ipaddr="0.0.0.0";

2.2上传文件相关代码修改

(1) 在upload.c的processUploadHeader函数里 需要将 if (value == ‘.’ || !websValidUriChars(value) || strpbrk(value, "\/:?<>|~"’%`^\n\r\t\f")) {
改为if(*value == ‘.’){

(2) 文件大小限制,goahead-linux-default-me.h里(goahead-linux-default-me.h会替换me.h)ME_GOAHEAD_LIMIT_POST的大小调大,否则上传大文件无法通过。

(3) 临时文件存放路径 #define ME_GOAHEAD_UPLOAD_DIR "tmp"该为#define ME_GOAHEAD_UPLOAD_DIR “/tmp”,上传后的文件会保存在/tmp目录下,路径可以自主调整。


原文链接:https://blog.csdn.net/weixin_40732273/article/details/107831176

3.交叉编译

本人交叉编译器:arm-linux-gcc 

make CC=arm-linux-gcc LD=arm-linux-ld

注意:当编译提示/home/coidea/goahead-5.1.1/src/goahead.c:196:对‘__stack_chk_fail’未定义的引用
/home/coidea/goahead-5.1.1/src/goahead.c:196:对‘__stack_chk_guard’未定义的引用时需要修改projects/goahead-linux-default.mk,修改标红处,关闭堆栈保护

拷贝goahead和libgo.so到开发板

 拷贝self.key  self.crt route.txt   auth.txt到开发板

 把goahead self.key self.crt route.txt auth.txt 放在开发板同一目录下

把libgo.so放在开发板/lib目录下

在存放goahead的目录下新建一个tmp目录,用于存放上传文件的临时文件夹,例如如图所示

 我的www目录存放的是html文件里面有一个cgi-bin目录用于存放cgi程序

4.修改route.txt

 5.启动goahaed

./goahaed -v www
在浏览器输入开发板IP地址(需要把网站的第一个页面改名为index.html)即可

6.goahead之文件传输

1.system.html

<style>
  form {
    margin: 0 auto;
    width: 400px;
  }
</style>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>led control</title>
  <script>
  function fileForm() {
      var fileInput = document.getElementById("file_path");
      if (fileInput.files.length === 0) {
        alert("请选择一个文件");
        return false;
      }
      return true;
    }
  </script>
</head>

<body>
 

  <h2 align="center">远程升级</h2>
  <!--新建一个表单,动作链接到开发板的cgi-bin/upgrade.cgi,采用的方法为POST-->
  <form action="cgi-bin/aa.cgi" method="post" enctype="multipart/form-data" onsubmit="return fileForm()">
    <p align="center">固件存储路径:<input type="file" name="file_path" id="file_path" required/></p>
    <p align="center"><input type="submit" name="upgrade" value="启动" /></p>
  </form>

</body>
</html>

 2.aa.c交叉编译为aa.cgi

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UPLOAD_DIR "/app/goahead/"  // 设置文件保存的目录

int main(int argc, char **argv)
{
	// 获取请求方法
	printf("Content-Type: text/html\n\n");
	char *requestMethod = getenv("REQUEST_METHOD");
	if (requestMethod == NULL || strcmp(requestMethod, "POST") != 0) {
		printf("Status: 405 Method Not Allowed\n\n");
		printf("Method Not Allowed");
		return 0;
	}

	// 获取上传的文件数据
	char *contentLengthStr = getenv("CONTENT_LENGTH");
	printf("contentLengthStr:%s\n\n",contentLengthStr);
	if (contentLengthStr == NULL) {
		printf("Status: 400 Bad Request\n\n");
		printf("Bad Request");
		return 0;
	}

	long contentLength = strtol(contentLengthStr, NULL, 10);//把长度转换为long类型
	printf("contentLength:%ld\n\n<br>",contentLength );
	if (contentLength <= 0) {
		printf("Status: 400 Bad Request\n\n");
		printf("Bad Request");
		return 0;
	}

	char *fileData = malloc(contentLength);
	
	// 获取文件名
	//	char *filename = getenv("HTTP_X_FILENAME");
	char *filename =getenv("FILE_CLIENT_FILENAME_file_path");
	printf("file_name=%s\n\n<br>",filename);
	if (filename == NULL) {
		printf("Status: 400 Bad Request\n\n<br>");
		printf("Bad Request");
		free(fileData);
		return 0;
	}
    //读取临时文件的文件名
	char *tmp = getenv("FILE_FILENAME_file_path");
	printf("tmp:%s\n\n",tmp);
	if (tmp == NULL) {
		printf("    the tmp is null\n\n<br>");
		printf("Status: 400 Bad Request\n\n");
		printf("Bad Request");
		free(fileData);
		return 0;
	}
	char *tempfile[128];
	sprintf(tempfile,"/app/goahead/%s",tmp);
	printf("tempfile:%s\n\n",tempfile);
	
	// 保存文件到指定目录
	char destPath[128];
	char cmd[128];
	snprintf(destPath, sizeof(destPath), "%s%s", UPLOAD_DIR, filename);
	sleep(3);
	sprintf(cmd,"cp %s %s",tempfile,destPath);
	system(cmd);
	FILE *file = fopen(destPath, "r");
	if(file == NULL){
		printf("the file is null<br>");
		return 0;
	}
	
	fseek(file,0,SEEK_END);
	int fileSize = ftell(file);
	printf("the file size is %d<br>",fileSize);
	fseek(file,0,SEEK_SET);
	fread(fileData,1,fileSize,file);
	printf("<br>context:<br>%s<br>",fileData);
	

	printf("Content-Type: text/html\n\n");
	printf("<html><body>");
	printf("File uploaded successfully!");
	printf("</body></html>");

	return 0;
}

网页上传上来的文件在一个临时文件夹里,我的临时文件夹是/app/goahead/tmp/

 FILE_FILENAME_file_path环境变量存储了临时文件的路径

只需要用getenv解析 FILE_FILENAME_file_path即可

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值