Create a minimum REST Web Server with netcat nc

前端 未结 2 1733
半阙折子戏
半阙折子戏 2020-12-29 00:11

I was looking for the minimum REST web server using nc to be a one-liner for a Docker container. For instance:

* http://localhost/echo/marcello: prints marce         


        
2条回答
  •  一生所求
    2020-12-29 00:52

    Another full and simple solution in Bash with NetCat is this one below. It supports Content-type, file sizes and much more. Latest version may be found at https://github.com/jdoleczek/Bash-NetCat-HTTPD/blob/master/httpd.sh

    #!/bin/bash
    # LICENSE MIT
    
    PORT=${1:-8080}
    FILES=${2:-"./"}
    
    NS=$(netstat -taupen 2>/dev/null | grep ":$PORT ")
    test -n "$NS" && echo "Port $PORT is already taken" && exit 1
    
    echo -e "\n\tHTTPD started for files in $FILES:"
    
    for IP in $(ifconfig | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1) ; do
        echo -e "\tlistening at $IP:$PORT"
    done
    
    echo -e "\n"
    FIFO="/tmp/httpd$PORT"
    rm -f $FIFO
    mkfifo $FIFO
    trap ctrl_c INT
    
    function ctrl_c() {
        rm -f $FIFO && echo -e "\n\tServer shut down.\n" && exit
    }
    
    while true; do (
        read req < $FIFO;
        req=$(echo $req | cut -d" " -f2 | cut -d"#" -f1 | cut -d"?" -f1 | cut -c2-);
        >&2 echo -e -n "\tRequest: \"$req\"\t";
        test -z "$req" && req="index.html"
    
        if [ -f "$FILES$req" ] ; then
            ext="${req##*.}"
            ext=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
    
            case "$ext" in
                "html" | "htm") CONTENTTYPE="text/html; charset=UTF-8" ;;
                "json") CONTENTTYPE="application/json; charset=UTF-8" ;;
                "css" | "less" | "sass") CONTENTTYPE="text/css" ;;
                "txt") CONTENTTYPE="text/plain" ;;
                "xml") CONTENTTYPE="text/xml" ;;
                "js") CONTENTTYPE="application/javascript" ;;
                "jpg" | "jpeg") CONTENTTYPE="image/jpeg" ;;
                "png") CONTENTTYPE="image/png" ;;
                "gif") CONTENTTYPE="image/gif" ;;
                "ico") CONTENTTYPE="image/x-icon" ;;
                "wav") CONTENTTYPE="audio/wav" ;;
                "mp3") CONTENTTYPE="audio/mpeg3" ;;
                "avi") CONTENTTYPE="video/avi" ;;
                "mp4" | "mpg" | "mpeg" | "mpe") CONTENTTYPE="video/mpeg" ;;
                *) CONTENTTYPE="application/octet-stream"
            esac
    
            echo "HTTP/1.x 200 OK"
            echo "Date: $(LC_TIME=en_US date -u)"
            echo "Server: promyk.doleczek.pl"
            echo "Connection: close"
            echo "Pragma: public"
            echo "Content-Type: $CONTENTTYPE"
            FILESIZE=$(wc -c < "$FILES$req")
            echo -e "Content-Length: $FILESIZE\n"
            cat "$FILES$req"
            >&2 echo "[ ok ]"
        else
            echo -e "HTTP/1.x 404 Not found\n\n

    File not found.

    " >&2 echo "[ no file ]" fi ) | nc -l -k -w 1 -p $PORT > $FIFO; done;

提交回复
热议问题