Simple HTTP server in GAWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple HTTP server in GAWK
# 1  
Old 12-06-2009
Simple HTTP server in GAWK

Hi,

Not sure if this post belongs to this forum but I have been trying every so often to setup a simple http server in awk. After a couple of try and errors I finally came across this:
REMCONF - TCP/IP Internetworking With `gawk'

This tutorial is not to cut and paste without change, so here is a complete gawk snipet that creates a pseudo-server on any low-end configured *nix box running gawk.

This particular example could be used to access/modify a remote config file on a router/firewall running *nix.

Save this to a file, say myserver.awk and run it:
Code:
awk -f myserver.awk

Than from your browser:
Code:
http://localhost:8080

Amazing what you can do with gawk!

The code:
Code:
function SetUpServer() {
  TopHeader = "<html><title>Remote Configuration</title>"
  TopDoc = "<body>\
  <h2>Please choose one of the following actions:</h2>\
  <UL>\
  <li><a href=" MyPrefix "/AboutServer>About this server</a></li>\
  <li><a href=" MyPrefix "/ReadConfig>Read Configuration</a></li>\
  <li><a href=" MyPrefix "/CheckConfig>Check Configuration</a></li>\
  <li><a href=" MyPrefix "/ChangeConfig>Change Configuration</a></li>\
  <li><a href=" MyPrefix "/SaveConfig>Save Configuration</a></li>\
  </UL>"
  TopFooter  = "</body></html>"
  if (ConfigFile == "") ConfigFile = "config.asc"
}


function HandleGET() {
  if(MENU[2] == "AboutServer") {
    Document  = "This is a GUI for remote configuration of an\
    embedded system. It is is implemented as one GAWK script."
  } else if (MENU[2] == "ReadConfig") {
    RS = "\n"
    while ((getline < ConfigFile) > 0)
    config[$1] = $2;
    close(ConfigFile)
    RS = "\r\n"
    Document = "Configuration has been read."
  } else if (MENU[2] == "CheckConfig") {
    Document = "<table border=1 cellpadding=5>"
    for (i in config)
      Document = Document "<tr><td>" i "</td>" \
       "<td>" config[i] "</td></tr>"
      Document = Document "</table>"
  } else if (MENU[2] == "ChangeConfig") {
    if ("Param" in GETARG) {           # any parameter to set?
      if (GETARG["Param"] in config) {  # is  parameter valid?
        config[GETARG["Param"]] = GETARG["Value"]
        Document = (GETARG["Param"] " = " GETARG["Value"] ".")
      } else {
        Document = "Parameter <b>" GETARG["Param"] "</b> is invalid."
      }
    } else {
    Document = "<form method=\"GET\"><h4>Change one parameter</h4>\
     <table border cellpadding=5>\
     <tr><td>Parameter</td><td>Value</td></tr>\
     <tr><td><input type=text name=Param value=\"\" size=20></td>\
       <td><input type=text name=Value value=\"\" size=40></td>\
     </tr></table><input type=submit value=\"Set\"></form>"
    }
  } else if (MENU[2] == "SaveConfig") {
    for (i in config)
    printf("%s %s\n", i, config[i]) > ConfigFile
    close(ConfigFile)
    Document = "Configuration has been saved."
  }
}

function CGI_setup( method, uri, version, i) {
  delete GETARG;         delete MENU;        delete PARAM
  GETARG["Method"] = $1
  GETARG["URI"] = $2
  GETARG["Version"] = $3
  i = index($2, "?")
  # is there a "?" indicating a CGI request?
  if (i > 0) {
   split(substr($2, 1, i-1), MENU, "[/:]")
   split(substr($2, i+1), PARAM, "&")
   for (i in PARAM) {
    j = index(PARAM[i], "=")
    GETARG[substr(PARAM[i], 1, j-1)] =  substr(PARAM[i], j+1)
   }
  } else {    # there is no "?", no need for splitting PARAMs
    split($2, MENU, "[/:]")
  }
 }
 

BEGIN {
  if (MyHost == "") {
     "uname -n" | getline MyHost
     close("uname -n")
  }
  if (MyPort ==  0) MyPort = 8080
  HttpService = "/inet/tcp/" MyPort "/0/0"
  MyPrefix    = "http://" MyHost ":" MyPort
  SetUpServer()
  while ("awk" != "complex") {
    # header lines are terminated this way
    RS = ORS = "\r\n"
    Status  = 200          # this means OK
    Reason  = "OK"
    Header  = TopHeader
    Document = TopDoc
    Footer  = TopFooter
    if       (GETARG["Method"] == "GET") {
      HandleGET()
    } else if (GETARG["Method"] == "HEAD") {
      # not yet implemented
    } else if (GETARG["Method"] != "") {
      print "bad method", GETARG["Method"]
    }
    Prompt = Header Document Footer
    print "HTTP/1.0", Status, Reason      |& HttpService
    print "Connection: Close"       |& HttpService
    print "Pragma: no-cache"              |& HttpService
    len = length(Prompt) + length(ORS)
    print "Content-length:", len          |& HttpService
    print ORS Prompt                      |& HttpService
    # ignore all the header lines
    while ((HttpService |& getline) > 0)
      ;
    # stop talking to this client
    close(HttpService)
    # wait for new client request
    HttpService |& getline
    # do some logging
    print systime(), strftime(), $0
    # read request parameters
    CGI_setup($1, $2, $3)
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SIMPLE HTTP PROXY SERVER CHECKER (Completed)

Simple Http Proxy Server Checker Script with curl mirror proxies-scripts/proxc at master * Anoncheg1/proxies-scripts * GitHub output in terminal HTTP, HTTP Connect (HTTPS not supported) command line: proxc filename where filename is file like 119.110.69.185:8080 119.235.16.41:8080... (4 Replies)
Discussion started by: 654321
4 Replies

2. Solaris

Installing Oracle HTTP Server

Hi, i am trying to install Oracle 10g Companion Products on a Solaris 5.9 server. At the end of the installation the configuration assistant fails and it's unable to start the HTTP Server. Below is the error opmnctl: starting opmn and all managed processes... Failed to get IP for ... (0 Replies)
Discussion started by: saharookiedba
0 Replies

3. Linux

HTTP server

Hello all, Please tell me that how to configure a HTTP server in Fedora Core 3 Also tell me about WEB HOSTING under Linux. (4 Replies)
Discussion started by: jaibw
4 Replies

4. Cybersecurity

List of HTTP/FTP-clients on a server

The other day, a friend of mine had his Linux webserver compromised because he was running a vulnerable PHP-script. The "hacker" had used a malformed URL to include a wget-command to fetch some stuff off the net and install it in /dev/shm where it ran undetected. Fortunately, the webserver ran as a... (2 Replies)
Discussion started by: indo1144
2 Replies

5. UNIX for Advanced & Expert Users

how to know how many user's connected to ftp and http server

i need to write a program to know how many users are presently connected to my ftp server and http server . i need to keep a count of this and this count should be available to other different software . how to make this GLOBAL so that other softwares can access this count value (7 Replies)
Discussion started by: hariprasad
7 Replies

6. Linux

VNC Server http listening port

Hi All, I'm running RH 9.0 on a PII box with 160MB RAM. Just downloaded RealVNC X86 Linux (version 3.3.7). How can I get the HTTP listening port up ? Thanks, KENT (6 Replies)
Discussion started by: kxchen_home
6 Replies
Login or Register to Ask a Question