I'm new to writing bash scripts and mostly using chatgpt to get templates right now. I am trying to create a simple netcat based http server which receives and stores .png files. I have a second script intended to test this and it is currently sending a broken pipe error. I don't entirely know what that means in this context since I'm not really sending data over a network. Any insight appreciated. Not new to programming but new to bash/networking/web dev.
Test script terminal output:
Sending PUT request to http://127.0.0.1:1881...
* Trying 127.0.0.1:1881...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 1881 (#0)
PUT /test_image.png HTTP/1.1
Host: 127.0.0.1:1881
User-Agent: curl/7.81.0
Accept: */*
Content-Length: 16074
Expect: 100-continue
* Mark bundle as not supporting multiuse
HTTP/1.1 100-continue
* Send failure: Broken pipe
0 16074 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Closing connection 0
curl: (55) Send failure: Broken pipe
Test Script:
#!/bin/bash
# Define the server address and port
SERVER_ADDRESS="127.0.0.1"
SERVER_PORT=1881
# File to send
TEST_FILE="test_image.png"
# Check if the test file exists
if [[ ! -f "$TEST_FILE" ]]; then
echo "Test file '$TEST_FILE' does not exist. Creating a dummy file..."
echo "This is a test file." > "$TEST_FILE"
fi
# Use curl to send a PUT request with the --upload-file option
echo "Sending PUT request to http://$SERVER_ADDRESS:$SERVER_PORT..."
RESPONSE=$(curl -v http://$SERVER_ADDRESS:$SERVER_PORT --upload-file $TEST_FILE)
# Check the HTTP response code
if [[ "$RESPONSE" == "200" ]]; then
echo "PUT request was successful. Server returned HTTP 200 OK."
else
echo "PUT request failed. Server returned HTTP $RESPONSE."
fi
Server Script:
#!/bin/bash
# Define the IP address and port for the HTTP server
IP_ADDRESS="127.0.0.1"
PORT=1881
# Directory to store received data
UPLOAD_DIR="Data"
# Start the HTTP server using netcat
echo "Starting HTTP server on $IP_ADDRESS:$PORT..."
while :
do
echo -e "HTTP/1.1 100-continue \r\n" | nc -l -s $IP_ADDRESS -p $PORT -q 1 > request.txt
# Generate a timestamp-based file name
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
OUTPUT_FILE_PATH="$UPLOAD_DIR/received_$TIMESTAMP.png"
# Save the received data to the defined output file path
sed -n '/\r\n\r\n/{n;p;}' request.txt > "$OUTPUT_FILE_PATH"
echo "Received PUT data saved to $OUTPUT_FILE_PATH"
done