OpenWrt Custom CGI implementations

Source: Internet
Author: User

This article has been published by the author Wu Zhi authorized NetEase Cloud community.

Welcome to NetEase Cloud Community, learn more about NetEase Technology product operation experience.

install UHTTPD.

Before compiling the OpenWrt, enter make Memuconfig to find uhttpd, Web servers/proxies, network, and tick if not checked. Then compile the firmware.


Modify the UHTTPD configuration.

After the installation runs OpenWrt, SSH login, modify the/ETC/CONFIG/UHTTPD configuration file, add a new Web service at the end of the file. The basic format for adding data is as follows:

Config ' uhttpd ' ServerName ' option ' home '/root/' list ' listen_http ' 0.0.0.0:8080 ' option ' cgi_prefix '/cgi-bin '

which


name description
uhttpd service name, do not duplicate the system names can be
home root directory of the service
listen_http cgi prefix, used to distinguish whether the called CGI
cgi_prefix CGI prefixes, which are used to distinguish whether or not the CGI is invoked


There are other configurations, not listed, such as Index_page, Error_page, Listen_https, cert, key, Script_timeout, Network_timeout, tcp_keepalive, etc.
To restart UHTTPD after modification, use the command:

/ETC/INIT.D/UHTTPD restart
Configure a static page.

Configure the Web page at the root of the service, such as index.html (the default page if the UHTTPD configuration is not specified). Here is a landing page found online:

 

When the user clicks the login, it jumps to the/cgi-bin/helloworld in the current directory.


Write a CGI program.

For example, the/cgi-bin/helloworld of the previous step is a CGI program for Web services. This article describes how to install a CGI program by IPK.
Generating IPK requires cross-compiling with openwrt, first creating a directory under the package directory in the OpenWrt directory, in this case: HelloWorld. The makefile file and the SRC directory are then created in the HelloWorld directory. Where the makefile file is as follows:

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

The CGI path where the installation path is a Web service is established.
Src directory into its own program and makefile, in this case:
HELLOWORLD.C content is as follows:

#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 (" 

Makefile content is as follows:

HELLOWORLD:HELLOWORLD.O (CC) (ldflags) helloworld.o-o HelloWorld helloworld.o:helloworld.c (cc) (CFLAGS)-C HelloWorld. C CLEAN:RM *.O Helloworldo

After compiling with OpenWrt, the IPA is uploaded to the router (or compiled directly into the firmware) via SCP and installed:

Opkg Install HELLOWORLD_1_X86.IPK

After that, you will see the HelloWorld program under/root/cgi-bin. The calling program can output the Web page normally:

[Email protected]:~/cgi-bin#./helloworld content-type:text/html;charset=utf-8 
result Output

The browser will open the default page (192.168.8.106 is the test router address), click on the default page of the OK button, will return to the CGI output, such as:

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



NetEase Cloud Free Experience Pavilion, 0 cost experience 20+ Cloud products!

More NetEase technology, products, operating experience sharing please click.


Related articles:
"Recommended" MongoDB replica set members and state transitions
"Recommended" Django project to deploy the pits encountered on Uwsgi+nginx
"Recommended" React technology stack Practice (1)

OpenWrt Custom CGI implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.