Linux embedded Web server and CGI programming development BOA



** directory **
a, environment building
two, configuration (part)
Third, debugging and running
Fourth, the test reference source
five common mistakes
VI expansion (CCGI, SQLite)



# First, the environment to build
the operating system : Ubuntu12.04 LTS
boa Download (but I can not find ...): [http://www.boa.org/] (http://www.boa.org/ )
I find resources from other sites, but forget the URL, so I direct the cloud disk resource
links: [https://pan.baidu.com/s/1_SpR9MDcmSB8jpDm76fw6A](https://pan.baidu.com/s / 1_SpR9MDcmSB8jpDm76fw6A) extraction code: hb1e
can refer to: [Ubuntu configuration and build the server boa] (https://www.cnblogs.com/jasonxiang/archive/2012/10/17/ubuntu_boa.html)
CGI : direct terminal mounting sudo apt-get install apache2
can refer to: [cgi build environment ubuntu] (https://blog.csdn.net/heibaiyijing/article/details/8538413)

# Second, the configuration (part)
boa my configuration : / etc / boa $ sudo vi boa.conf

# The following are a few key points, basically wrong on several points
# cumentRoot /var/www
# The actual website address and location to be stored in the corresponding cgi
# ScriptAlias / cgi-bin / / var / www / cgi-bin /
Runtime can see #cgi script $ PATH (optional)
# CGIPath /bin:/usr/bin:/usr/local/bin
# If you want to be able to run cgi in any position, you want to add the (optional)
AddType application/x-httpd-cgi cgi
# Boa v0.94 configuration file
# File format has not changed from 0.93
# File format has changed little from 0.92
# version changes are noted in the comments
#
# The Boa configuration file is parsed with a lex/yacc or flex/bison
# generated parser. If it reports an error, the line number will be
# provided; it should be easy to spot. The syntax of each of these
# rules is very simple, and they can occur in any order. Where possible
# these directives mimic those of NCSA httpd 1.3; I saw no reason to 
# introduce gratuitous differences.

# $Id: boa.conf,v 1.25 2002/03/22 04:33:09 jnelson Exp $
# The "ServerRoot" is not in this configuration file. It can be compiled
# into the server (see defines.h) or specified on the command line with
# the -c option, for example:
#
# Good -c / usr / local / good

# Port: The port Boa runs on. The default port for http servers is 80.

# If it is less than 1024, the server must be started as root.
# Port always occupied, so I get rid of
Port 88

# Listen: the Internet address to bind(2) to. If you leave it out,
# it takes the behavior before 0.93.17.2, which is to bind to all
# addresses (INADDR_ANY). You only get one "Listen" directive,
# if you want service on multiple IP addresses, you have three choices:
# 1. Run boa without a "Listen" directive
# a. All addresses are treated the same; makes sense if the addresses
# are localhost, ppp, and eth0.
# b. Use the VirtualHost directive below to point requests to different
# files. Should be good for a very large number of addresses (web
# hosting clients).
# 2. Run one copy of boa per IP address, each has its own configuration
# with a "Listen" directive. No big deal up to a few tens of addresses.
# Nice separation between clients.
# The name you provide gets run through inet_aton(3), so you have to use dotted
# quad notation. This configuration is too important to trust some DNS.

#Listen 192.68.0.5
# User: The name or UID the server should run as.
# Group: The group name or GID the server should run as.

User 0 
Group 0

# ServerAdmin: The email address where server problems should be sent.
# Note: this is not currently used, except as an environment variable
# for CGIs.
#ServerAdmin root@localhost
# ErrorLog: The location of the error log file. If this does not start
# with /, it is considered relative to the server root.
# Set to /dev/null if you don't want errors logged.
# If unset, defaults to /dev/stderr

ErrorLog /var/log/boa/error_log

# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
# is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
# process if the receiving end of a pipe stops reading."
#ErrorLog "|/usr/sbin/cronolog --symlink=/var/log/boa/error_log /var/log/boa/error-%Y%m%d.log"

# AccessLog: The location of the access log file. If this does not
# start with /, it is considered relative to the server root.
# Comment out or set to /dev/null (less effective) to disable 
# Access logging.
# AccessLog / var / log / boa / access_log
# Please NOTE: Sending the logs to a pipe ('|'), as shown below,
# is somewhat experimental and might fail under heavy load.
# "Usual libc implementations of printf will stall the whole
# process if the receiving end of a pipe stops reading."
#AccessLog "|/usr/sbin/cronolog --symlink=/var/log/boa/access_log /var/log/boa/access-%Y%m%d.log"

# UseLocaltime: Logical switch. Uncomment to use localtime 
# instead of UTC time
#UseLocaltime

# VerboseCGILogs: this is just a logical switch.
# It simply notes the start and stop times of cgis in the error log
# Comment out to disable.

#VerboseCGILogs

# ServerName: the name of this server that should be sent back to 
# clients if different than that returned by gethostname + gethostbyname

ServerName www.your.org.here

# VirtualHost: a logical switch.
# Comment out to disable.
# Given DocumentRoot /var/www, requests on interface 'A' or IP 'IP-A'
# become /var/www/IP-A.
# Example: http://localhost/ becomes /var/www/127.0.0.1
#
# Not used until version 0.93.17.2. This "feature" also breaks commonlog
# output rules, it prepends the interface number to each access_log line.
# You are expected to fix that problem with a postprocessing script.

#VirtualHost

# DocumentRoot: The root directory of the HTML documents.
# Comment out to disable server non user files.

DocumentRoot /var/www

# UserDir: The name of the directory which is appended onto a user's home
# directory if a ~user request is recieved.

UserDir public_html

# DirectoryIndex: Name of the file to use as a pre-written HTML
# directory index. Please MAKE AND USE THESE FILES. On the
# fly creation of directory indexes can be _slow_.
# Comment out to always use DirectoryMaker

DirectoryIndex index.html

# DirectoryMaker: Name of program used to create a directory listing.
# Comment out to disable directory listings. If both this and
# DirectoryIndex are commented out, accessing a directory will give
# an error (though accessing files in the directory are still ok).

DirectoryMaker / usr / lib / good / boa_indexer

# DirectoryCache: If DirectoryIndex doesn't exist, and DirectoryMaker
# has been commented out, the the on-the-fly indexing of Boa can be used
# to generate indexes of directories. Be warned that the output is 
# extremely minimal and can cause delays when slow disks are used.
# Note: The DirectoryCache must be writable by the same user/group that 
# Good runs the.

# DirectoryCache / var / spool / good / dircache

# KeepAliveMax: Number of KeepAlive requests to allow per connection
# Comment out, or set to 0 to disable keepalive processing

KeepAliveMax 1000

# KeepAliveTimeout: seconds to wait before keepalive connection times out

KeepAliveTimeout 10

# MimeTypes: This is the file that is used to generate mime type pairs
# and Content-Type fields for boa.
# Set to /dev/null if you do not want to load a mime types file.
# Do *not* comment out (better use AddType!)

MimeTypes /etc/mime.types

# DefaultType: MIME type used if the file extension is unknown, or there
# is no file extension.

DefaultType text/plain

# CGIPath: The value of the $PATH environment variable given to CGI progs.

CGIPath /bin:/usr/bin:/usr/local/bin

# SinglePostLimit: The maximum allowable number of bytes in 
# a single POST. Default is normally 1MB.

# AddType: adds types without editing mime.types
# Example: AddType type extension [extension ...]

# Uncomment the next line if you want .cgi files to execute from anywhere
#AddType application/x-httpd-cgi cgi

# Redirect, Alias, and ScriptAlias all have the same semantics -- they
# match the beginning of a request and take appropriate action. Use
# Redirect for other servers, Alias for the same server, and ScriptAlias
# to enable directories for script execution.

# Redirect allows you to tell clients about documents which used to exist in
# your server's namespace, but do not anymore. This allows you to tell the
# clients where to look for the relocated document.
# Example: Redirect /bar http://elsewhere/feh/bar

# Aliases: Aliases one path to another.
# Example: Alias /path1/bar /path2/foo

Alias /doc /usr/doc

# ScriptAlias: Maps a virtual path to a directory for serving scripts
# Example: ScriptAlias /htbin/ /www/htbin/

ScriptAlias / cgi-bin / / var / www / cgi-bin /

 

cgi my configuration : sudo vi / etc / apache2 / sites-enabled / 000-default

ServerName 127.0.0.1 
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny 
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost> 

 

# Third, debugging and running
    are fitted after a good run sudo ./boa
    access http://127.0.0.1: port number
    I is http://127.0.0.1:88
    gcc test.c -o test.cgi compiled test. cgi
    copy the files to the cgi cgi-bin under the
    URL is HTTP: // localhost: 88 / cgi-bin / test.cgi
! [insert picture description here] (https://img-blog.csdnimg.cn/20191017173313468. PNG? X = Process-oss- image / Watermark, type_ZmFuZ3poZW5naGVpdGk, shadow_10, text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE =, size_16, color_FFFFFF, t_70)
! [inserted here described image] (https://img-blog.csdnimg.cn/20191017173501368.png)

# Fourth, the test reference source



Here are a few test code, transferred from other chiefs, but I can not find the URL.
1,

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

int main(void)
{
    char *data;
    long m,n;
    printf("Content-type:text/html\n\n");
    printf("<HTML>");
    printf("<HEAD>");
    printf("<TITLE>multi</TITLE>");
    printf("</HEAD>");
    printf("<BODY>");
    printf("<H2 ALIGN=\"center\">multi control</H2>");
    printf("<FORM METHOD=\"GET\" ACTION=\"test1.cgi\">");
    printf("<P>Direction:<INPUT TYPE=\"text\" NAME=\"m\" VALUE=\"\" size=\"18\">");
    printf("<P>Step Number:<INPUT TYPE=\"text\" NAME=\"n\" VALUE=\"\" size=\"17\">");
    printf("<P ALIGN=\"left\">");
    printf("<INPUT TYPE=\"SUBMIT\"  VALUE=\"Submit\">");
    printf("<INPUT TYPE=\"RESET\"    VALUE=\"Reset\">");
    printf("</P>");
    printf("</FORM>");
    printf("</BODY>");
    printf("</HTML>");

    data=getenv("QUERY_STRING");
    if(!data)
        printf("<P>get no datas and it's wrong.");
    else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
        printf("<P>the input must be numbers");
    else
        printf("<P>%ld and %ld multi= %ld",m,n,m*n);
    return 0;
}

 

! [Effect] (https://img-blog.csdnimg.cn/20191017173823233.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE=,size_16,color_FFFFFF,t_70)
2,
/ var / WWW pass.html under

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登陆验证</title>
</head>
<body>
<form name="form1" action="/cgi-bin/pass.cgi" method="POST">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="right">用户名</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td align="right">密 码</td>
<td><input type="password" name="Password"></td>
</tr>
<tr>
<td><input type="submit" value="登 录"></td>
<td><input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>

 

/ Var / pass.c / www cgi-bin under

/*=====================================================================
cgi examples
=====================================================================*/
//pass.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getcgidata(FILE* fp, char* requestmethod);
int main()
{
       char *input;
       char *req_method;
       char name[64];
       char pass[64];
       int i = 0;
       int j = 0;
      
//     printf("Content-type: text/plain; charset=iso-8859-1\n\n");
       printf("Content-type: text/html\n\n");
       printf("The following is query reuslt:<br><br>");
       req_method = getenv("REQUEST_METHOD");
       input = getcgidata (stdin, req_method);
        // we get the following input string may form as
        // the Username = "ADMIN" & Password = "AAAAA"
        // where "Username =" and "& Password =" is fixed
        // the "admin" and "aaaaa" is changing, but also we want to get the
      
       // first nine characters is UserName =
        // the "UserName =" and "&" between us to take out the user name 
       for (I = . 9 ; I <( int ) strlen (INPUT); I ++ )
       {
              if ( input[i] == '&' )
              {
                     name[j] = '\0';
                     break;
              }                  
              name[j++] = input[i];
       }
       // first nine characters + "& Password =" 10 characters + Username number of characters
        // we do not, it is omitted, not copied 
       for (I = . 19 + strlen (name), J = 0 ; I <( int ) strlen (INPUT); I ++ )
       {
              pass[j++] = input[i];
       }
       pass[j] = '\0';
       printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass);
      
       return 0;
}
char* getcgidata(FILE* fp, char* requestmethod)
{
       char* input;
       int len;
       int size = 1024;
       int i = 0;
      
       if (!strcmp(requestmethod, "GET"))
       {
              input = getenv("QUERY_STRING");
              return input;
       }
       else if (!strcmp(requestmethod, "POST"))
       {
              len = atoi(getenv("CONTENT_LENGTH"));
              input = (char*)malloc(sizeof(char)*(size + 1));
             
              if (len == 0)
              {
                     input[0] = '\0';
                     return input;
              }
             
              while(1)
              {
                     input[i] = (char)fgetc(fp);
                     if (i == size)
                     {
                            input[i+1] = '\0';
                            return input;
                     }
                    
                     --len;
                     if (feof(fp) || (!(len)))
                     {
                            i++;
                            input[i] = '\0';
                            return input;
                     }
                     i++;
                    
              }
       }
       return NULL;
}

 

效果图:
![pass.html](https://img-blog.csdnimg.cn/20191017174133818.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE=,size_16,color_FFFFFF,t_70)

Input data click on the "Login"

! [pass.cgi] (https://img-blog.csdnimg.cn/20191017174214965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE=,size_16,color_FFFFFF,t_70)
automatically jump to /cgi-bin/pass.cgi, to obtain data and print

# Five common mistakes
your URL configuration will affect 502 what is wrong configuration or permissions have caused the problem.
Such as:
502 Bad Gateway
at The CGI WAS not CGI / 1.1 Compliant.
Cgi_header: Unable to the Find LFLF
1. The URL may be wrong (and path corresponds profile)
2. configuration problems
3. authority did not give enough chmod 777 test .cgi

# Six expansion (CCGI, SQLite)
CGIC main site: [http://www.boutell.com/cgic/](http://www.boutell.com/cgic/)
SQLite official website: [http: // www.sqlite.org/](http://www.sqlite.org/)
configuration reference Gangster blog: [project combat] (https://blog.csdn.net/a642960662/article/category/6818637)
I debugging problems encountered Gangster blog also made the following comment ** ** If you encounter problems can refer to.
Supplementary :'ll compile-time error, use

gcc -o config.cgi config.c sqlite3.c cgic.c -lsqlite3 -lpthread -ldl

 

! [Involving file] (https://img-blog.csdnimg.cn/20191018150602684.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE=,size_16,color_FFFFFF,t_70)
! [HTML effects] (https://img-blog.csdnimg.cn/2019101815065393.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE=,size_16,color_FFFFFF,t_70)
! [cgi page] (https: // img-blog.csdnimg.cn/20191018150729649.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lrYXJvc181MjE=,size_16,color_FFFFFF,t_70)
data is written to the database, use the following command

sqlite3 person.db
.table
select * from person;

 

Guess you like

Origin www.cnblogs.com/ikaros-521/p/11698520.html