#!/usr/bin/env bash if [[ "$REQUEST_METHOD" == "DELETE" ]]; then rm -f ../uploads/* printf "%s\r\n\r\n" "HTTP/1.1 200 OK" fi if [[ "$REQUEST_METHOD" == "GET" ]]; then printf "%s\r\n" "HTTP/1.1 200 OK" printf "%s\r\n\r\n" "Content-Type: text/html" echo "" fi if [[ "$REQUEST_METHOD" == "POST" ]]; then BOUNDARY="${HTTP_CONTENT_TYPE#*=}" declare post_multipart tmpfile=$(mktemp) dd iflag=fullblock of=$tmpfile ibs=${CONTENT_LENGTH} count=1 obs=1M delimeter_len=$(echo -n "${BOUNDARY}"$'\015' | wc -c) boundaries_list=$(echo -ne $(grep $tmpfile -ao -e ${BOUNDARY} --byte-offset | sed -E 's/:(.*)//g') | sed -E 's/ [0-9]+$//') outfile="../uploads/$(head -n2 $tmpfile | tail -n1 | sed 's/^.*filename="\(.*\)".*/\1/')" for i in $boundaries_list; do tmpout=$(mktemp) dd iflag=fullblock if=$tmpfile ibs=$(($i+$delimeter_len)) obs=1M skip=1 | while true; do read line if [[ $line == $'\015' ]]; then cat - > $tmpout break fi done length=$(grep $tmpout --byte-offset -ae ${BOUNDARY} | sed -E 's/:(.*)//' | head -n 1) post_multipart+=($outfile) dd iflag=fullblock if=$tmpout ibs=$length count=1 obs=1M of=$outfile rm $tmpout done rm $tmpfile chmod 777 "$outfile" chmod -x "$outfile" printf "%s\r\n" "HTTP/1.1 302 OK" printf "%s\r\n\r\n" "Location: ../upload.html" fi