Skip to content

Instantly share code, notes, and snippets.

@stokito
Created July 12, 2020 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stokito/c97e81ffef7b6c772f9ccfd9056c47c2 to your computer and use it in GitHub Desktop.
Save stokito/c97e81ffef7b6c772f9ccfd9056c47c2 to your computer and use it in GitHub Desktop.
Build template-able web page and more.

Build template-able web page and more.

busybox httpd

Start the httpd in debug mode:

$ busybox httpd -f -vvv -c ./httpd.conf

See config:

$ cat httpd.conf:
I:test.tt
*.tt:/usr/local/bin/haserl
/:duke:$1$lsqrABnV$g1231mabcDIsnR4mFUW1F/
  • Line 1 set the default page, default is index.html
  • Line 2 set xxx.tt run through an interpreter
  • Line 3 set / requested user name & passwd, user name here is duke, and the passwd is general by command 'busybox httpd -m passwd-string'

only the line 2 is all we need in this article, more busybox httpd usage can be found in busybox source code: ./busybox-1.21.0/networking/httpd.c

haserl

Haserl is a small program that uses shell or Lua script to create cgi web scripts. It is intended for environments where PHP or ruby are too big.

$ haserl -v
This is haserl version 0.9.30 (http://haserl.sourceforge.net)

basic usage:

$ cat test.tt
#!/usr/local/bin/haserl
Content-Type: text/html
 
<html>
<body>
 
<% if [ $REQUEST_METHOD == "POST" ]; then %>
 
<%   if [ $POST_twins == "sagill" ]; then %>
correct
<%   else %>
error
<%   fi %>
 
<% else %>
 
<form method="post" action="./test.tt" >
  twins: <input type="text" name="twins"/><br/>
  <input type="submit" value="Submit" />
</form>
 
<% fi %>
 
</body>
</html>

Tips 1, dynamic header & contents

#!/usr/local/bin/haserl
Content-Type: application/json
 
[{"a": "aa"}, {"b": "bb"}]

the Content-Type normally must on the top without blank line, and the contents must after at least one blank line. if we want to dynamic generation the Content-Type, we try:

#!/usr/local/bin/haserl
<% if [ 1 == 1 ]; then %>
Content-Type: application/json
 
[{"a": "aa"}, {"b": "bb"}]
 
<% else %>
Content-Type: text/html
 
normal test...
 
<% fi %>

but we got empty header, and the Content-Type is parsed as contents, you can checkout this using build-in debug console in chrome (Ctrl + Shift + c).

the issue can be solved in this way:

#!/usr/local/bin/haserl
 
<% if [ 1 == 1 ]; then %>
 
<% echo Content-Type: application/json %>
 
[{"a": "aa"}, {"b": "bb"}]
 
<% fi %>

if you want to dynamic generation the contents at the same time:

#!/usr/local/bin/haserl
 
<% if [ 1 == 1 ]; then %>
 
<% echo Content-Type: application/json %>
 
<% echo '[{"a": "aa"}, {"b": "bb"}]' %>
 
<% fi %>

this time, we got empty contents, and the contents we echo is parsed as header. the correct one is:

#!/usr/local/bin/haserl
 
<% if [ 1 == 1 ]; then %>
 
<% echo Content-Type: application/json %>
<% echo "" %>
<% echo '[{"a": "aa"}, {"b": "bb"}]' %>
 
<% fi %>

there are more than one line outputs, you can clean those blank line by:

<% echo -n '[{"a": "aa"}, {"b": "bb"}]'; exit %>

Tips 2, using template write template wrapper:

$ cat main-layout.tt
#!/usr/local/bin/haserl
Content-Type: text/html
 
<html>
<body>
<% haserl --accept-none "./tpl$REQUEST_URI" %>
</body>
</html>

write the sub page in the sub spl dir:

$ cat spl/page1.tt
#!/usr/local/bin/haserl
<p>this is sub page1</p>
then link the main-layout.tt to same name sub page in top dir:
$ ln -s main-layout.tt page1.tt

now the tree is:

$ tree 
.
├── httpd.conf
├── page1.tt -> tpl-main-layout.tt
├── tpl
│   └── page1.tt
└── tpl-main-layout.tt

now access /page.tt we got:

<html>
<body>
<p>this is sub page1</p>
</body>
</html>

Tips 3, background work if you want start some work at the background, you may try:

#!/usr/local/bin/haserl
Content-Type: text/html
 
some text
<% sleep 30 & %>

but the page is pending until 30 seconds past. using tool nohup, screen or something else can solve this:

<% nohup sleep 30 > /dev/null & %>

http://webcache.googleusercontent.com/search?q=cache:ix2JhHwnSAkJ:duke-blog.appspot.com/id/20130807094549293+&cd=18&hl=ru&ct=clnk&gl=ua&client=ubuntu

http://manpages.ubuntu.com/manpages/bionic/man1/haserl.1.html

http://haserl.sourceforge.net/manpage.html

https://wiki.alpinelinux.org/wiki/Mini_httpd_with_Haserl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment