uclinux中boa的cgi程序示例

---------------------        uclinuxboacgi程序示例        --------------------

/* htmllib.c

 * HTML common library functions for the CGI programs. */

#include <stdio.h>

#include "htmllib.h"

void htmlHeader(char *title)

{

  printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>%s</TITLE></HEAD>",

                title);

}

void htmlBody()

{

    printf("<BODY>");

}

void htmlFooter()

{

    printf("</BODY></HTML>");

}

void addTitleElement(char *title)

{

       printf("<H1>%s</H1>", title);

}

----------------------------------------------------------------------------------------------------------------------

/* template.c */

#include <stdio.h>

#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("<li>DEBUG: [%s] = [%s]\n", postvars[i], postvars[i+1]);

#endif

              }

       }

       /* GET */

       printf("<FORM ACTION=\"%s\" METHOD=POST>", "/cgi-bin/cgi_demo");

       printf("<SELECT NAME=\"port\">");

       printf("<OPTION>COM 1");

       printf("<OPTION>COM 2");

       printf("</SELECT>");

       printf("</TD></TR>");

       printf("<BR><INPUT TYPE=submit VALUE=\"Submit\">");

       printf("<INPUT TYPE=reset VALUE=\"Reset\">");

       printf("</FORM>");

       return 0; 

}

----------------------------------------------------------------------------------------------------------------------

/* 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);

       /* 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';

     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;

}

----------------------------------------------------------------------------------------------------------------------

/* cgi.c */

#include <stdio.h>

#include <string.h>

#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.confindex.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:

<HTML>

<HEAD>

<TITLE>A test web page</TITLE>

</HEAD>

<BODY>

<H1>Test Page</H1>

<P>

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 :-).

<P>

If everything has built correctly then the

<A HREF=/cgi-bin/cgi_demo>CGI Demo</A> should be here.

</BODY>

</HTML>

如果上面的所有文件要实现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 <stdio.h>

#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("<h2 align=\"center\"><i>The Response Of CGI</i></h2><pre><hr>\n");

        printf("<p>&nbsp;</p>");

        printf("<p align=\"center\"><font size=\"4\" color=\"#000000\">Can you look your expectant result?</font></p>");

        printf("<p>&nbsp;</p>");

        printf("<p align=\"center\"><a href=\"//192.168.0.28\">Return</a></p>\n");

        return 0;

}

 

/*******************************    cgi.c    **********************************/

#include <stdio.h>

#include <string.h>

#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     *******************************/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>单选框网页实例</title>

</head>

<body>

<p> </p>

<p align="center"><b><font size="6" color="#0000FF">基于</font><font face="Times New Roman" size="6" color="#0000FF">uClinux</font><font size="6" color="#0000FF"></font><font face="Times New Roman" size="6" color="#0000FF">Web</font><font size="6" color="#0000FF">服务器控制三个</font><font face="Times New Roman" size="6" color="#0000FF">LED</font><font size="6" color="#0000FF">的测试</font></b></p>

<p></p>

<table width="651" height="230" border="1" align="center">

   <tr>

     <td width="670" height="200">

        <p align="center">通过下面的选项,你可以控制开发板上的led了。</p>

          <form action="/cgi-bin/cgi_test">

       <p align="center">

         <input type="radio" name="R1" value="V1" checked="checked" />1

         <input type="radio" name="R2" value="V1" checked="checked" />2

         <input type="radio" name="R3" value="V1" checked="checked" />3</p>

       <p align="center">

         <input type="radio" name="R1" value="V2" />1

              <input type="radio" name="R2" value="V2" />2

              <input type="radio" name="R3" value="V2" />3</p>

       <p align="center">

         <input type="submit" value="确认" name="B1" />

     </p></td>

        </form>

   </tr>

 </table>

<p>&nbsp; </p>

 <p align="center">说明:这个是基于s3c44b0uClinux(linux 2.4 kernel)系统设计的 </p>

 <p> </p>

</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值