嵌入式web服务器
要实现在PC上通过网页控制连接到嵌入式开发板的相机。
限于开发板的环境,不能选择appche等大型web服务器,选择了boa。
要想pc端跨平台,那就不能用ActiveX控件,如果仅在windows平台上是可以的,使用这种方法,PC端要装插件。
所以我选择纯html页面。
如果仅仅是显示静态内容,那么直接把html页面放到boa的根目录下即可,但如果要创建动态页面,那就得使用cgi接口。
在嵌入式上运行的web服务器基本也都只支持cgi接口,不支持什么asp,jsp。
cgi是通用网关接口的意思,只是接口而已,能使用任何能实现输入输出的语言进行编程实现,web服务器对输入输出进行了重定向
web服务器把从网页得到的输入全部重定向输出到cgi-bin目录的cgi程序作为输入,而cgi程序的输出重定向到web服务器,通过web服务器发送到浏览器。
在嵌入式环境里现成的编程语言就是shell,c/c++,这是肯定支持的,php,python,perl也是可以支持的,但脚本语言要执行,那肯定得另外配置环境,
如同java要运行需要jre一样。
在这里,web服务器程序不仅要进行显示,还要控制相机,要跟相机控制程序进行通信,所以用shell不太现实,所以还是选择c语言。
用c语言首先要有两类个基本的函数要实现,一是从网页发送过来的数据中提取传递的参数,二是读写配置文件。这是在没有加入通讯功能的阶段。
借鉴别人的代码,首先是提取网页参数
头文件:
/* cgivars.h */
#ifndef _CGIVARS_H
#define _CGIVARS_H
/* method */
#define GET 0
#define POST 1
/* function prototypes */
int getRequestMethod();
char **getGETvars();
char **getPOSTvars();
int cleanUp(int form_method, char **getvars, char **postvars);
void adminCheck();
#endif /* !_CGIVARS_H */
源文件:
/* cgivars.c
* (C) Copyright 2000, Moreton Bay (http://www.moretonbay.com).
* see HTTP (www.w3.org) and RFC
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cgivars.h"
/* local function prototypes */
char hex2char(char *hex);
void unescape_url(char *url);
char x2c(char *what);
/* hex2char */
/* RFC */
char hex2char(char *hex) {
char char_value;
char_value = (hex[0] >= 'A' ? ((hex[0] & 0xdf) - 'A') + 10 : (hex[0] - '0'));
char_value *= 16;
char_value += (hex[1] >= 'A' ? ((hex[1] & 0xdf) - 'A') + 10 : (hex[1] - '0'));
return char_value;
}
/* unescape_url */
/* RFC */
void unescape_url(char *url) {
int n, k;
for(n=0, k=0;url[k];++n, ++k) {
if((url[n] = url[k]) == '%') {
url[n] = hex2char(&url[k+1]);
k += 2;
}
}
url[n] = '\0';
}
/* getRequestMethod
* retn: from_method (GET or POST) on success,
* -1 on failure. */
int getRequestMethod() {
char *request_method;
int form_method;
request_method = getenv("REQUEST_METHOD");
if(request_method == NULL)
return -1;
if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD") ) {
form_method = GET;
} else if (!strcmp(request_method, "POST")) {
form_method = POST;
} else {
/* wtf was it then?!! */
return -1;
}
return form_method;
}
/* getGETvars
* retn: getvars */
char **getGETvars() {
int i;
char **getvars;
char *getinput;
char **pairlist;
int paircount = 0;
char *nvpair;
char *eqpos;
getinput = getenv("QUERY_STRING");
if (getinput)
getinput = strdup(getinput);
//DEBUGMSG(1, ("gin=%s\n", getinput));
/* Change all plusses back to spaces */
for(i=0; getinput && getinput[i]; i++)
if(getinput[i] == '+')
getinput[i] = ' ';
pairlist = (char **) malloc(256*sizeof(char **));
paircount = 0;
nvpair = getinput ? strtok(getinput, "&") : NULL;
while (nvpair) {
pairlist[paircount++]= strdup(nvpair);
if(!(paircount%256))
pairlist = (char **) realloc(pairlist,(paircount+256)*sizeof(char **));
nvpair = strtok(NULL, "&");
}
pairlist[paircount] = 0;
getvars = (char **) malloc((paircount*2+1)*sizeof(char **));
for (i= 0; i<paircount; i++) {
if(eqpos=strchr(pairlist[i], '=')) {
*eqpos = '\0';
unescape_url(getvars[i*2+1] = strdup(eqpos+1));
} else {
unescape_url(getvars[i*2+1] = strdup(""));
}
unescape_url(getvars[i*2] = strdup(pairlist[i]));
}
getvars[paircount*2] = 0;
for(i=0;pairlist[i];i++)
free(pairlist[i]);
free(pairlist);
if (getinput)
free(getinput);
return getvars;
}
/* getPOSTvars
* retn: postvars */
char **getPOSTvars() {
int i;
int content_length;
char **postvars;
char *postinput;
char **pairlist;
int paircount = 0;
char *nvpair;
char *eqpos;
postinput = getenv("CONTENT_LENGTH");
if (!postinput)
exit(1);
if(!(content_length = atoi(postinput)))
exit(1);
if(!(postinput = (char *) malloc(content_length+1)))
exit(1);
if (!fread(postinput, content_length, 1, stdin))
exit(1);
postinput[content_length] = '\0';
//DEBUGMSG(1, ("pin=%s\n", postinput));
for(i=0;postinput[i];i++)
if(postinput[i] == '+')
postinput[i] = ' ';
pairlist = (char **) malloc(256*sizeof(char **));
paircount = 0;
nvpair = strtok(postinput, "&");
while (nvpair) {
pairlist[paircount++] = strdup(nvpair);
if(!(paircount%256))
pairlist = (char **) realloc(pairlist, (paircount+256)*sizeof(char **));
nvpair = strtok(NULL, "&");
}
pairlist[paircount] = 0;
postvars = (char **) malloc((paircount*2+1)*sizeof(char **));
for(i = 0;i<paircount;i++) {
if(eqpos = strchr(pairlist[i], '=')) {
*eqpos= '\0';
unescape_url(postvars[i*2+1] = strdup(eqpos+1));
} else {
unescape_url(postvars[i*2+1] = strdup(""));
}
unescape_url(postvars[i*2]= strdup(pairlist[i]));
}
postvars[paircount*2] = 0;
for(i=0;pairlist[i];i++)
free(pairlist[i]);
free(pairlist);
free(postinput);
return postvars;
}
/* cleanUp
* free the mallocs */
int cleanUp(int form_method, char **getvars, char **postvars) {
int i;
if (postvars) {
for(i=0;postvars[i];i++)
free(postvars[i]);
free(postvars);
}
if (getvars) {
for(i=0;getvars[i];i++)
free(getvars[i]);
free(getvars);
}
return 0;
}
void adminCheck(){
char * userName = NULL;
char * errPage = "Location: UnauthorizedError.htm\n\n";
userName = getenv("REMOTE_USER");
if(userName == NULL) goto err;
if(!strcmp("admin",userName))
{
return;
}
err: printf("%s\n",errPage);
fflush(stdout);
exit(0);
}
读取配置文件借鉴别人的,别人的读取是从配置文件读取,而写配置则只是写进一个临时文件,写进配置文件是由实际控制相机的服务程序实现的。
我要将它改为直接将参数写进配置文件。
头文件:
#include <stdio.h>
#define MAX_LINE_LENGTH 128
struct item{
char *session;
char *key;
char *value;};
int get_value(char *filename,const char *session,const char *key,char *value);
int set_value(char *filename,const char *session,const char *key,char *value);
源文件:
#include "parseini.h"
#include <string.h>
int get_value(char *filename,const char *session,const char *key,char *value)
{
int i,j;
FILE *fp;
char temp_line[128];
char fake_session[128], fake_key[128];
memset(temp_line, 0, MAX_LINE_LENGTH);
fp=fopen(filename,"r+");
while(fgets(temp_line, MAX_LINE_LENGTH, fp))
{
if(temp_line[0]=='[')
{
if(strstr(temp_line,session))
{
i=1; //no [
while(temp_line[i]!=']')
{
fake_session[i-1]=temp_line[i];
i++;
}
fake_session[i-1]=0;
if(!strcmp(fake_session,session))
{
memset(temp_line, 0, MAX_LINE_LENGTH);
do{
fgets(temp_line, MAX_LINE_LENGTH, fp);
if(temp_line[0]=='[')break;
if((temp_line[0]!=';')&&strstr(temp_line,key))
{
i=0;
while(temp_line[i]!='=')
{
fake_key[i]=temp_line[i];
i++;
}
fake_key[i]=0;
if(!strcmp(fake_key,key))
{
j=0;
i++;
while(temp_line[i]!='\n')
{
value[j]=temp_line[i];
i++;
j++;
}
value[j]=0;
fclose(fp);
return 1;
}
}
}while(1);
fclose(fp);
return 0;
}
}
}
}
fclose(fp);
return 0;
}
int set_value(char *filename,const char *session,const char *key,char *value)
{
FILE *fp,*temp_fp;
int i,j;
char temp_line[128];
char temp_file_name[]="/etc/temp_config.ini";
char fake_session[128], fake_key[128];
memset(temp_line, 0, MAX_LINE_LENGTH);
fp=fopen(filename,"rb");
temp_fp=fopen(temp_file_name,"w");
while(fgets(temp_line, MAX_LINE_LENGTH, fp))
{
if(temp_line[0]=='[')
{
if(strstr(temp_line,session))
{
i=1; //no [
while(temp_line[i]!=']')
{
fake_session[i-1]=temp_line[i];
i++;
}
fake_session[i-1]=0;
if(!strcmp(fake_session,session))
{
fputs(temp_line,temp_fp);
memset(temp_line, 0, MAX_LINE_LENGTH);
out: while(fgets(temp_line, MAX_LINE_LENGTH, fp))
{
if(temp_line[0]=='[')break;
if((temp_line[0]!=';')&&strstr(temp_line,key))
{
i=0;
while(temp_line[i]!='=')
{
fake_key[i]=temp_line[i];
i++;
}
fake_key[i]=0;
if(!strcmp(fake_key,key))
{
j=0;
i++;
while(value[j]!=0)
{
temp_line[i]=value[j];
i++;
j++;
}
temp_line[i]='\n';
i++;
while(temp_line[i]!=0)
{
temp_line[i]=0;
i++;
}
fputs(temp_line,temp_fp);
memset(temp_line, 0, MAX_LINE_LENGTH);
goto out;
}
}
fputs(temp_line,temp_fp);
}
}
}
}
fputs(temp_line,temp_fp);
memset(temp_line, 0, MAX_LINE_LENGTH);
}
fclose(fp);
fclose(temp_fp);
unlink(filename);
rename(temp_file_name,filename);
return 0;
}
嵌入式web服务器的更多相关文章
- 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...
- 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验
20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...
- 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验
20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验 20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验
- 20145210 20145226 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
20145210 20145226 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 结对伙伴:20145226 夏艺华 实验报告封面 实验目的与要求 · 掌握在ARM开发板实现 ...
- 20145221 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
20145221 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 实验报告 队友博客:20145326蔡馨熠 实验博客:<信息安全系统设计基础>实验五 简单嵌入式W ...
- 实验5 简单嵌入式WEB服务器实验 实验报告 20135303 20135326
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级: 1353 姓名:20135303 魏昊卿 学号:2013532 ...
- 20145303 20145339 《信息安全系统设计基础》 实验五 简单嵌入式WEB服务器实验
20145303 20145339 <信息安全系统设计基础> 实验五 简单嵌入式WEB服务器实验 实验目的与要求 1.掌握在ARM开发板实现一个简单WEB服务器的过程 2.学习在ARM开发 ...
- 三种嵌入式web服务器(Boa / lighttpd / shttpd)的 linux移植笔记
一:移植Boa(web服务器)到嵌入式Linux系统 一.Boa程序的移植 1.下载Boa源码 下载地址: http://www.boa.org/ 目前最新发行版本: 0.94.13 ...
- Atitit.嵌入式web 服务器 java android最佳实践
Atitit.嵌入式web 服务器 java android最佳实践 1. Android4.4.21 2. 自己的webserver1 3. CyberHTTP for Java cybergar ...
- 嵌入式web服务器BOA的移植及应用
嵌入式web服务器子系统 一.嵌入式web服务器的控制流程 如下图所示,嵌入式web服务器可实现通过网络远程控制嵌入式开发板,便捷实用. 控制流程:浏览器 --->>>嵌入式开发板 ...
随机推荐
- js传递参数中包含+号时的处理方法
encodeURI(url).replace(/\+/g, '%2B') 例子: $scope.getAnesthesiawaystatistical = function (annual, anes ...
- Bugtags 介绍视频 - App 测试 · 从未如此简单
Bugtags 是什么? Bugtags 是移动时代首选 Bug 管理系统,针对不同的使用场景,Bugtags 具有以下强大特性: 移动应用 Bug 管理 Bugtags 可以直接在应用中所见即所得提 ...
- 导入Eclipse工程 到 Android Studio
一.从Eclipse导入工程到Android Studio 根据官方的介绍,Android Studio可以兼容Eclipse的现有工程,但需要做一些操作: 首先升级ADT到最新版本,目前为版本号为2 ...
- C预处理和C库
#include <stdio.h> #define MAN(x) "n"##x int main(void) { printf("%s",MAN( ...
- 剑指offer习题集1
1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...
- OpenWrt镜像编译和ipv6支持
离成功实现路由器刷OpenWrt.接入校园网差不多一年了.路由工作比较稳定,还是很满意的. 这次回来有个新发现:学校有原生ipv6支持,在win7和ubuntu下什么都不用设置,自动获取global ...
- windows与OSX双操的时区-黑苹果之路
问题由来已久,原因好像是windows识别时间的方式跟OSX不一样,方法如下: 1,改苹果系统时区为冰岛 2,改window系统的注册表 在管理员cmd下运行 Reg add HKLM\SYSTEM\ ...
- 使用JDBC进行批处理
在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...
- openldap加密传输 nslcd
http://www.openldap.org/faq/data/cache/185.html https://www.ibm.com/developerworks/cn/linux/1312_zha ...
- PCI Express(四) - The transaction layer
原文出处:http://www.fpga4fun.com/PCI-Express4.html 感觉没什么好翻译的,都比较简单,主要讲了TLP的帧结构 In the transaction layer, ...