--------------------- uclinux中boa的cgi程序示例 --------------------
/* htmllib.c
* HTML common library functions for the CGI programs. */
#include
#include "htmllib.h"
void htmlHeader(char *title)
{
printf("Content-type: text/html\n\n
title);
}
void htmlBody()
{
printf("");
}
void htmlFooter()
{
printf("");
}
void addTitleElement(char *title)
{
printf("%s
", title);
}
----------------------------------------------------------------------------------------------------------------------
/* template.c */
#include
#include "cgivars.h"
#include "htmllib.h"
#define DEBUG 1
int template_page(char **postvars, int form_method) {
int i;
addTitleElement("Demo CGI");
if(form_method == POST) {
for (i=0; postvars[i]; i+= 2) {
#if DEBUG
printf("
#endif
}
}
/* GET */
printf("
printf("
printf("
printf("
printf("");
printf("");
printf("
");
printf("");
printf("");
return 0;
}
----------------------------------------------------------------------------------------------------------------------
/* cgivars.c
* (C) Copyright 2000, Moreton Bay (http://www.moretonbay.com).
* see HTTP (www.w3.org) and RFC */
#include
#include
#include
#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);
/* 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
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';
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
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;
}
----------------------------------------------------------------------------------------------------------------------
/* cgi.c */
#include
#include
#include "cgivars.h"
#include "htmllib.h"
#include "template.h"
/*对应的头文件中的对应c文件中函数的声明*/
int main() {
char **postvars = NULL; /* POST request data repository */
char **getvars = NULL; /* GET request data repository */
int form_method; /* POST = 1, GET = 0 */
form_method = getRequestMethod();
if(form_method == POST) {
getvars = getGETvars();
postvars = getPOSTvars();
} else if(form_method == GET) {
getvars = getGETvars();
}
htmlHeader("Demo Web Page");
htmlBody();
template_page(postvars, form_method);
htmlFooter();
cleanUp(form_method, getvars, postvars);
fflush(stdout);
exit(0);
}
-----------------------------------------------------------------------------------------------------
#makefile
EXEC = cgi_demo
OBJS = cgi.o cgivars.o htmllib.o template.o
all: $(EXEC)
romfs:
$(ROMFSINST) $(ROOTDIR)/vendors/Generic/httpd /home/httpd //拷贝文件
$(ROMFSINST) /home/httpd/cgi-bin/cgi_demo //表示将当前文件夹下的文件拷贝到对应目录
$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
clean:
-rm -f $(EXEC) *.elf *.gdb *.o
$(OBJS): cgivars.h htmllib.h template.h
----------------------------------------------------------------------------------------------------------------------
在$(ROOTDIR)/vendors/Generic/httpd目录下有两个文件:boa.conf和index.html
----------------------------------------------------------------------------------------------------------------------
#boa.conf:
#
# A minimal config that makes the home page
# an unauthenticated CGI
#
ServerName uClinux
DocumentRoot /home/httpd
ScriptAlias /cgi-bin/ /home/httpd/cgi-bin/
Alias /img /home/httpd/img
# Auth /cgi-bin/cgi_demo /etc/config/config
AddType text/plain txt
AddType image/gif gif
AddType text/html html
AddType text/html htm
AddType text/xml xml
AddType image/jpeg jpe
AddType image/jpeg jpeg
AddType image/jpeg jpg
AddType image/x-icon ico
……………………………………………………………………………………………………..
//index.html:
Test Page
If you are seeing this page, then your web server is working, and now
you need to create some nice pages to replace this one :-).
If everything has built correctly then the
/cgi-bin/cgi_demo>CGI Demo should be here.
如果上面的所有文件要实现web服务的功能,除了在配置内核是在网络应用程序选上boa和在杂余配置中选上cgi外,还需要修改$(ROOTDIR)/vendors/Samsung/44B0/Makefile文件.
ROMFS_DIRS = bin dev etc home lib mnt proc usr var
修改为:
ROMFS_DIRS = bin dev etc home lib mnt proc usr var home/httpd \
home/httpd/cgi-bin //因为上面make romfs的缘故,也可以修改上面的romfs的那个位置(黑体字位置)
注意:最好在vi/vim编辑器中修改,不要在gedit中修改,避免出错。
控制led测试实例:
/************************** template.c ****************************/
#include
#include "cgivars.h"
#include "htmllib.h"
#define DEBUG 1
int template_page(char **getvars, int form_method) {
int i;
addTitleElement("The Embedded Web Servers Of LED Control Test");
printf("The Response Of CGI
\n");
printf("
printf(" Can you look your expectant result?
printf("
printf("\n");
return 0;
}
/******************************* cgi.c **********************************/
#include
#include
#include "cgivars.h"
#include "htmllib.h"
#include "template.h"
static volatile unsigned int *iopctl = (volatile unsigned int *)(0x01d20010);
static volatile unsigned int *iopdat = (volatile unsigned int *)(0x01d20014);
void out(int arg)
{
*iopctl &= ~(1u<<(2*arg+1));
*iopctl |= 1u<<(2*arg);
}
int main() {
char **postvars = NULL; /* POST request data repository */
char **getvars = NULL; /* GET request data repository */
int form_method; /* POST = 1, GET = 0 */
form_method = getRequestMethod();
if(form_method == POST) {
getvars = getGETvars();
postvars = getPOSTvars();
}
else if(form_method == GET)
{
getvars = getGETvars();
}
htmlHeader("Demo Web Page");
htmlBody();
out(1);out(2);out(3);
if(getvars)
{
int i=0;
while(getvars[i])
{
if (strcmp(getvars[i],"R1")==0)
{
if(strcmp(getvars[i+1],"V1")==0)
*iopdat |= 1u<<1;
else
*iopdat &= ~(1u<<1);
}
if (strcmp(getvars[i],"R2")==0)
{
if(strcmp(getvars[i+1],"V1")==0)
*iopdat |= 1u<<2;
else
*iopdat &= ~(1u<<2);
}
if (strcmp(getvars[i],"R3")==0)
{
if(strcmp(getvars[i+1],"V1")==0)
*iopdat |= 1u<<3;
else
*iopdat &= ~(1u<<3);
}
i += 2;
}
}
template_page(getvars, form_method);
htmlFooter();
cleanUp(form_method, getvars, postvars);
fflush(stdout);
exit(0);
}
/******************************** Makefile *******************************/
EXEC = cgi_test
OBJS = cgi.o cgivars.o htmllib.o template.o
all: $(EXEC)
romfs:
$(ROMFSINST) /home/cgi-bin/cgi_test
$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
clean:
-rm -f $(EXEC) *.elf *.gdb *.o
$(OBJS): cgivars.h htmllib.h template.h
/********************************** html *******************************/
基于uClinux的Web服务器控制三个LED的测试
通过下面的选项,你可以控制开发板上的led了。 1开 2开 3开 1关 2关 3关 " name="B1" />
说明:这个是基于s3c44b0的uClinux(linux 2.4 kernel)系统设计的