Webif^2 tutorial part 01
This article tries to explain how you can extend Webif^2 with your own functionality. I am myself new to Webif^2. The reason i am writing this article is to get acquainted enough with the code so i can decide if i would like to use it to create some NAS specific functionality.
Webif^2 runs on devices with some severe CPU and memory limits. Given that using one of the mainstream web development platforms was apparently off the table. Instead a combination of shell/awk and sed is used to generate dynamic content. Basically the way your father would have done it back in the days (given that there had been a web to develop for…) Having done some work with Ruby on Rails recently i am not too excited by this prospect but lets see how it goes.
My first goal is to have some pages which are displaying friendly greeting messages. These pages should be grouped into a new category in the main menu. I assume that you have checked out a copy of webif^2. All path references are relative to trunk/package/webif/.
Step1: Adding a new category.
files/www/cgi-bin/webif/.categories contains the entries of the main menu. We just add our entry here (”Test”).
-
##WEBIF:category:Info
-
##WEBIF:category:Graphs
-
##WEBIF:category:Status
-
##WEBIF:category:Log
-
##WEBIF:category:-
-
##WEBIF:category:System
-
##WEBIF:category:Network
-
##WEBIF:category:VPN
-
##WEBIF:category:Test
-
##WEBIF:category:-
-
##WEBIF:category:Logout
If you now reload your web browser you should see a new category “Test”. The next step will be to add content to this category.
Step2: Putting content into the new category.
I add the file test-hello.sh in files/www/cgi-bin/webif. The name of the file doesn’t matter but the convention seems to be to name it $(category)-$(name).sh.
-
#!/usr/bin/webif-page
-
<?
-
. /usr/lib/webif/webif.sh
-
-
header "Test" "Hello" "@TR<<Hello>>" ” "$SCRIPT_NAME"
-
-
echo "hello openWrt"
-
-
footer ?>
-
-
<!–
-
##WEBIF:name:Test:0:Hello
-
–>
Webif^2 uses an interpreter called Haserl. It will parse html files and execute the shell fragments it finds enclosed by <? … ?> (basically like it is done by php/jsp/…). The documentation for it can be found here.
The first line tells the linux kernel which interpreter to use when trying to execute this file (webif-page is the actual haserl program). The lines 02 to 09 contain the actual program that will generate our page. 03 pulls in webif.sh which is a library containing the functions we will use. 05 calls the header function (found in webif.sh). It will generate the html header, the head section of page and also the main- and sub-menu. $SCRIPT_NAME is set by haserl. On line 07 we say “hello” to openWrt. The only thing left now is to finish the page. To accomplish this we call the footer function (also found in webif.sh). This function will generate the content you see in the bottom section (”Apply Changes”, “Clear Changes”, …). It will also close the HTML document.
The last three lines contain a strange HTML comment. Lets have a closer look. When the main menu is generated webif^2 scans all files in files/www/cgi-bin/webif/ having the .sh extensions, looking for this special marker. The marker tells it to put an entry “Hello” into the category “Test”. The number can be used to allow a non-alphabetical ordering of the entries. The entry with the lowest number comes first. It will also be the page being selected by default of you click on the category.
So far so good. Next time we try to use some forms.
on March 29th, 2008 at 1:37 pm
Hi Frank! Good article.
Are you creating some NAS-oriented WebIf^2 Page? If so, it should be great including them into next OpenWRT for Gemini releases…
on March 30th, 2008 at 9:50 pm
Hi Paolo,
i am actually toying around with adding some NAS related functionality to Webif^2.
One thing i haven’t made up my mind yet is whether i should do it in shell/awk/sed or if i should give the lua approach a chance. Some of the hotspot extensions are actually written in lua so there is already some ground work to build up from. I will try to evaluate which language to use in the next few days so i can get some actual work done.
on March 23rd, 2009 at 8:24 am
hi frank, how can i made the script running on my httpd.. i am using busybox httpd server and my command are “sudo busybox httpd -h /var/www/ -p 8080 -v -c /etc/httpd.conf”.. the index.html are appeared but when it redirect to info.sh, the web page are trying to download the file instead of execute it..
Thank you in advance..