Beispiel HTTP GET

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_GET by th.iotkit2.ch

Revision:
7:9e64ad8d11e7
Child:
8:7d8db739ea8c
diff -r 5fbd3b258558 -r 9e64ad8d11e7 cgi-bin/rest.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/rest.txt	Mon Jun 08 15:30:08 2015 +0000
@@ -0,0 +1,64 @@
+#!/bin/bash
+#
+#   Abhandlung der REST Methoden GET, POST, PUT, DELETE
+
+# HTTP Header fuer Client
+echo "Content-type: text/plain"
+echo ""
+
+# / entfernen damit nicht in andere Verzeichnisse geschrieben werden kann
+QUERY_STRING=`echo $QUERY_STRING | sed 's:/::g'`
+
+#
+### GET
+#
+if [ "$REQUEST_METHOD" = "GET" ]
+then
+    if [ "$PATH_INFO" = "/time" ]   
+    then
+        date "+%Y.%m.%d %H:%M:%S"
+    elif [ "$PATH_INFO" = "/timestamp" ] 
+    then
+        date "+%s"
+    else
+        printf "hello $PATH_INFO from `hostname` at `date '+%Y.%m.%d %H:%M:%S'`\n" 
+    fi
+fi
+
+#
+### POST
+#
+if [ "$REQUEST_METHOD" = "POST" ]
+then
+    # Zerlegt den Querystring in Einzelteile
+    saveIFS=$IFS
+    IFS='=&'
+    parm=(`cat -`)
+    IFS=$saveIFS
+
+    echo "append to http://`hostname`/data/post.txt"
+    printf "${REMOTE_ADDR} `date "+%Y.%m.%d %H:%M:%S"` " >>/var/www/data/post.txt
+    for (( index = 0; index < ${#parm[@]}; index += 2 ))
+    do
+        printf "${parm[index]} = ${parm[index+1]} " >>/var/www/data/post.txt
+    done
+    printf "\n" >>/var/www/data/post.txt
+fi
+
+#
+### PUT
+#
+if [ "$REQUEST_METHOD" = "PUT" ]
+then
+    echo "write to http://`hostname`/data/$QUERY_STRING"
+    cat - >/var/www/data/$QUERY_STRING
+fi
+
+#
+### DELETE
+#
+if [ "$REQUEST_METHOD" = "DELETE" ]
+then
+    echo "delete http://`hostname`/data/$QUERY_STRING"
+    rm -f /var/www/data/$QUERY_STRING
+fi