Openwrt自訂CGI實現

來源:互聯網
上載者:User

標籤:體驗   lap   訪問   bottom   rgb   clean   https   interface   nbsp   

此文已由作者吳志猛授權網易雲社區發布。

歡迎訪問網易雲社區,瞭解更多網易技術產品運營經驗。

安裝uhttpd。

在編譯openwrt前,輸入make memuconfig,尋找Network -> Web Servers/Proxies -> uhttpd,如果沒勾選則勾選。然後編譯韌體。


修改uhttpd配置。

安裝運行openwrt後,通過ssh登陸,修改/etc/config/uhttpd設定檔,在檔案末尾添加新的web服務。添加資料基本格式如下:

config ‘uhttpd‘ ‘ServerName‘  option ‘home‘ ‘/root/‘  list ‘listen_http‘ ‘0.0.0.0:8080‘  option ‘cgi_prefix‘ ‘/cgi-bin‘

其中:


名稱 說明
uhttpd 服務名稱,不要和系統的重名即可
home 服務的根目錄
listen_http CGI首碼,用來區分是否調用的CGI
cgi_prefix CGI首碼,用來區分是否調用的CGI


還有其他配置,不一一列舉,比如index_page 、error_page 、listen_https、cert、key、script_timeout、network_timeout、tcp_keepalive等。
修改後需要重啟uhttpd,使用命令:

/etc/init.d/uhttpd restart
配置靜態頁面。

在服務的根目錄下配置web頁面,比如index.html(如果uhttpd配置未指定,則為預設頁面)。如下是一個網上找到的登陸網頁:

<html> <head><title>..................</title></head> <body> <form name="form1" action="/cgi-bin/helloworld"> <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>

當使用者點擊登陸時,會跳轉訪問目前的目錄下的/cgi-bin/helloworld。


編寫CGI程式。

例如上一步的/cgi-bin/helloworld即為web服務的CGI程式。本文介紹通過ipk的方式安裝CGI程式的方法。
產生ipk需要藉助openwrt的交叉編譯,首先在openwrt目錄下的package目錄下建立一個目錄,本例為:helloworld。然後在helloworld目錄下建立Makefile檔案和src目錄。其中Makefile檔案的內如如下:

include (TOPDIR)/rules.mkPKGNAME:=helloworldPKGRELEASE:=1PKGBUILDDIR:=(BUILD_DIR)/(PKGNAME)include(INCLUDE_DIR)/package.mk define Package/helloworld  SECTION:=utils  CATEGORY:=Utilities  TITLE:=Helloworld – prints a snarky message endef define Package/helloworld/description  If you can‘t figure out what this program does, you‘re  probablybrain-dead and need immediate medical attention. endef define Build/Prepare  mkdir -p (PKGBUILDDIR)(CP) ./src/* $(PKG_BUILD_DIR)/ endef define Package/helloworld/install  (INSTALLDIR)(1)/root/cgi-bin/  $(INSTALL_BIN) (PKGBUILDDIR)/helloworld(1)/root/cgi-bin/ endef (eval(call BuildPackage,helloworld))

其中制定安裝路徑為web服務的CGI路徑。
src目錄下放入自己的程式及Makefile,本例中有:
helloworld.c內容如下:

#include <stdio.h> #include <stdlib.h> #include <string.h>  void list_argument(void) {   char *env_var[] = {     "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",     "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",     "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",     "HTTP_HOST", "HTTP_USER_AGENT", "PATH",     "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",     "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",     "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",     "SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",     "SERVER_SIGNATURE","SERVER_SOFTWARE", "CONTENT_LENGTH" };   char *value, i;    for (i=0; i<25; i++) {     value = getenv(env_var[i]);      if (value)     {  printf("<p>");         printf ( "%s = %s \n", env_var[i], value);  printf("<\p>");     }   } }  int main(){ printf("Content-Type:text/html;charset=utf-8\n\n"); printf("<html>\n"); printf("<head>\n<title>test</title>\n</head>\n"); printf("<body>\n"); printf("<p>"); printf("hello~\n"); printf("</p>"); list_argument(); printf("</body>\n"); printf("</html>\n"); }

Makefile內容如下:

helloworld: helloworld.o  (CC)(LDFLAGS) helloworld.o -o helloworld helloworld.o: helloworld.c  (CC)(CFLAGS) -c helloworld.c clean:  rm *.o helloworldo

使用openwrt編譯好後,把ipa通過scp上傳到路由器下(或直接編譯到韌體中),安裝:

opkg install helloworld_1_x86.ipk

之後就會在/root/cgi-bin下看見helloworld程式。調用程式能正常輸出網頁:

[email protected]:~/cgi-bin# ./helloworld Content-Type:text/html;charset=utf-8  <html> <head> <title>test</title> </head> <body> <P>hello</p></body> </html>
結果輸出

瀏覽器就會開啟預設頁面(192.168.8.106是測試路由器地址),點擊預設頁面的確定按鈕,就會返回CGI的輸出,如:

hello~  DOCUMENT_ROOT = /root  GATEWAY_INTERFACE = CGI/1.1  HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8  HTTP_ACCEPT_ENCODING = gzip, deflate  HTTP_ACCEPT_LANGUAGE = zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3  HTTP_CONNECTION = keep-alive  HTTP_HOST = 192.168.8.106:8080  HTTP_USER_AGENT = Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:29.0) Gecko/20100101 Firefox/29.0  PATH = /sbin:/usr/sbin:/bin:/usr/bin  QUERY_STRING = Username=11&Password=222  REMOTE_ADDR = 192.168.8.103  REMOTE_PORT = 53874  REQUEST_METHOD = GET  REQUEST_URI = /cgi-bin/helloworld?Username=11&Password=222  SCRIPT_FILENAME = /root/cgi-bin/helloworld  SCRIPT_NAME = /cgi-bin/helloworld  SERVER_ADDR = 192.168.8.106  SERVER_NAME = 192.168.8.106  SERVER_PORT = 8080  SERVER_PROTOCOL = HTTP/1.1  SERVER_SOFTWARE = uHTTPd



網易雲免費體驗館,0成本體驗20+款雲產品! 

更多網易技術、產品、運營經驗分享請點擊。


相關文章:
【推薦】 MongoDB複製整合員及狀態轉換
【推薦】 django項目在uwsgi+nginx上部署遇到的坑
【推薦】 react技術棧實踐(1)

Openwrt自訂CGI實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.