mini2440 httpd使用(busybox自帶)
httpd使用(busybox自帶)
by HYH | 2018 年 1 月 20 日 下午 4:17
一.說明
1.該功能需要busybox自帶httpd。busybox自帶的httpd的參數如下:
2.httpd可配置文件幫助(見源碼:networking/httpd.c):
* httpd.conf has the following format:
*
* H:/serverroot # define the server root. It will override -h
* A:172.20. # Allow address from 172.20.0.0/16
* A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127
* A:10.0.0.0/255.255.255.128 # Allow any address that previous set
* A:127.0.0.1 # Allow local loopback connections
* D:* # Deny from other IP connections
* E404:/path/e404.html # /path/e404.html is the 404 (not found) error page
* I:index.html # Show index.html when a directory is requested
*
* P:/url:[http://]hostname[:port]/new/path
* # When /urlXXXXXX is requested, reverse proxy
* # it to http://hostname[:port]/new/pathXXXXXX
*
* /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/
* /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
* /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
* /adm:root:* # or user root, pwd from /etc/passwd on urls starting with /adm/
* /wiki:*:* # or any user from /etc/passwd with according pwd on urls starting with /wiki/
* .au:audio/basic # additional mime type for audio.au files
* *.php:/path/php # run xxx.php through an interpreter
*
* A/D may be as a/d or allow/deny – only first char matters.
* Deny/Allow IP logic:
* – Default is to allow all (Allow all (A:*) is a no-op).
* – Deny rules take precedence over allow rules.
* – “Deny all” rule (D:*) is applied last.
*
* Example:
* 1. Allow only specified addresses
* A:172.20 # Allow any address that begins with 172.20.
* A:10.10. # Allow any address that begins with 10.10.
* A:127.0.0.1 # Allow local loopback connections
* D:* # Deny from other IP connections
*
* 2. Only deny specified addresses
* D:1.2.3. # deny from 1.2.3.0 – 1.2.3.255
* D:2.3.4. # deny from 2.3.4.0 – 2.3.4.255
* A:* # (optional line added for clarity)
*
* If a sub directory contains config file, it is parsed and merged with
* any existing settings as if it was appended to the original configuration.
*
* subdir paths are relative to the containing subdir and thus cannot
* affect the parent rules.
*
* Note that since the sub dir is parsed in the forked thread servicing the
* subdir http request, any merge is discarded when the process exits. As a
* result, the subdir settings only have a lifetime of a single request.
*
* Custom error pages can contain an absolute path or be relative to
* ‘home_httpd’. Error pages are to be static files (no CGI or script). Error
* page can only be defined in the root configuration file and are not taken
* into account in local (directories) config files.
*
* If -c is not set, an attempt will be made to open the default
* root configuration file. If -c is set and the file is not found, the
* server exits with an error.
3.mini2440本身自帶了一個httpd演示程序boa。
使用busybox的httpd的時候如果不使用80端口以外的端口,需要先把它殺掉。
killall boa
二.使用
1.直接使用很簡單
busybox httpd -h 網頁主目錄
但這樣有個不足之處,就是不能使用cgi程序,打開cgi程序時不會在服務器上運行,直接下載cgi程序本身。
2.使用cgi程序
php也可以算作一種cgi程序(不過也可以直接作為http服務器的一個模塊),實際處理是服務器會把*.php文件交給php運行,再讀取程序返回的結果發送給客戶端瀏覽器。因此,對於可以直接在服務器上運行的Shell腳本程序,直接交給/bin/sh運行即可。
mini2440自帶的led.cgi:
#!/bin/sh
type=0
period=1
case $QUERY_STRING in
*ping*)
type=0
;;
*counter*)
type=1
;;
*stop*)
type=2
;;
esac
case $QUERY_STRING in
*slow*)
period=0.25
;;
*normal*)
period=0.125
;;
*fast*)
period=0.0625
;;
esac
/bin/echo $type $period > /tmp/led-control
echo “Content-type: text/html; charset=gb2312”
echo
/bin/cat led-result.template
exit 0
配置的httpd.conf:
*.cgi:/bin/sh
最終效果:
為了能夠運行更多種類的cgi程序,需要專門編寫一個程序用於啟動cgi程序:
C代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[ ])
{
execv(argv[1],&argv[1]);
}
程序的原理很簡單,直接使用exec函數族替換當前進程(如果產生了子進程,運行cgi程序時會提示重定向錯誤)。
https://hyhsystem.cn/wordpress/
Copyright ?2018 何亞紅的博客 unless otherwise noted.
mini2440 httpd使用(busybox自帶)