Awk.Info

"Cause a little auk awk
goes a long way."

About awk.info
 »  table of contents
 »  featured topics
 »  page tags


About Awk
 »  advocacy
 »  learning
 »  history
 »  Wikipedia entry
 »  mascot
Implementations
 »  Awk (rarely used)
 »  Nawk (the-one-true, old)
 »  Gawk (widely used)
 »  Mawk
 »  Xgawk (gawk + xml + ...)
 »  Spawk (SQL + awk)
 »  Jawk (Awk in Java JVM)
 »  QTawk (extensions to gawk)
 »  Runawk (a runtime tool)
 »  platform support
Coding
 »  one-liners
 »  ten-liners
 »  tips
 »  the Awk 100
Community
 »  read our blog
 »  read/write the awk wiki
 »  discussion news group

Libraries
 »  Gawk
 »  Xgawk
 »  the Lawker library
Online doc
 »  reference card
 »  cheat sheet
 »  manual pages
 »  FAQ

Reading
 »  articles
 »  books:

WHAT'S NEW?

Mar 01: Michael Sanders demos an X-windows GUI for AWK.

Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK

Feb 28: Tim Menzies asks this community to write an AWK cookbook.

Feb 28: Arnold Robbins announces a new debugger for GAWK.

Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK

Feb 28: Updated: the AWK FAQ

Feb 28: Tim Menzies offers a tiny content management system, in Awk.

Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk

Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).

Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us

Jan 31: Martin Cohen finds Awk on the Android platform.

Jan 31: Aleksey Cheusov released a new version of runawk.

Jan 31: Hirofumi Saito contributes a candidate Awk mascot.

Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.

Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.

[More ...]

Bookmark and Share

categories: ,Web,Dec,2009,MichealS

A Web Server in Awk

Contents

Server.awk - a simple, single user, web server built with gawk.

Download

Download from LAWKER.

About

This code creates an html menu of local applications which you can season to taste. The usage requires two steps...

  1. run: 'gawk -f server.awk'
  2. open browser at: http://localhost:8080

This code is based on the examples located at the TCP/IP Internetworking With `gawk' manual and is licensed under GPL 3.0. For updates to thos code, see http://topcat.hypermart.net/index.html.

Code

Set up

BEGIN { 
  x        = 1                         # script exits if x < 1 
  port     = 8080                      # port number 
  host     = "/inet/tcp/" port "/0/0"  # host string 
  url      = "http://localhost:" port  # server url 
  status   = 200                       # 200 == OK 
  reason   = "OK"                      # server response 
  RS = ORS = "\r\n"                    # header line terminators 
  doc      = Setup()                   # html document 
  len      = length(doc) + length(ORS) # length of document 
  while (x) { 
     if ($1 == "GET") RunApp(substr($2, 2)) 
     if (! x) break   
     print "HTTP/1.0", status, reason |& host 
     print "Connection: Close"        |& host 
     print "Pragma: no-cache"         |& host 
     print "Content-length:", len     |& host 
     print ORS doc                    |& host 
     close(host)     # close client connection 
     host |& getline # wait for new client request 
  } 
  # server terminated... 
  doc = Bye() 
  len = length(doc) + length(ORS) 
  print "HTTP/1.0", status, reason |& host 
  print "Connection: Close"        |& host 
  print "Pragma: no-cache"         |& host 
  print "Content-length:", len     |& host 
  print ORS doc                    |& host 
  close(host) 
} 

HTML Menu

function Setup() { 
  tmp = "<html>\
  <head><title>Simple gawk server</title></head>\
  <body>\
  <p><a href=" url "/xterm>xterm</a>\
  <p><a href=" url "/xcalc>xcalc</a>\
  <p><a href=" url "/xload>xload</a>\
  <p><a href=" url "/exit>terminate script</a>\
  </body>\
  </html>" 
  return tmp 
} 

Saying Good-bye

function Bye() { 
  tmp = "<html>\
  <head><title>Simple gawk server</title></head>\
  <body><p>Script Terminated...</body>\
  </html>" 
  return tmp 
} 

Running Applications

function RunApp(app) { 
  if (app == "xterm")  {system("xterm&"); return} 
  if (app == "xcalc" ) {system("xcalc&"); return} 
  if (app == "xload" ) {system("xload&"); return} 
  if (app == "exit")   {x = 0} 
}

Author

Michael Sanders

blog comments powered by Disqus