fix http group sending

This commit is contained in:
gitGnome
2026-03-30 14:46:31 +02:00
parent fcb936027e
commit 551b213c49
7 changed files with 58 additions and 45 deletions
BIN
View File
Binary file not shown.
+9
View File
@@ -281,28 +281,37 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
} }
func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) { func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
if !isMethodAllowed(&response, request) {
return
}
token := request.FormValue("token") token := request.FormValue("token")
if token == "" { if token == "" {
http.Error(response, "invalid token", http.StatusUnauthorized) http.Error(response, "invalid token", http.StatusUnauthorized)
return
} }
targetStr := request.FormValue("subject") targetStr := request.FormValue("subject")
if targetStr == "" { if targetStr == "" {
http.Error(response, "invalid subject", http.StatusBadRequest) http.Error(response, "invalid subject", http.StatusBadRequest)
return
} }
targetId, err := ConvertStringUint32(targetStr) targetId, err := ConvertStringUint32(targetStr)
if err != nil { if err != nil {
http.Error(response, "invalid subject", http.StatusBadRequest) http.Error(response, "invalid subject", http.StatusBadRequest)
return
} }
content := request.FormValue("content") content := request.FormValue("content")
if content == "" { if content == "" {
http.Error(response, "invalid content", http.StatusBadRequest) http.Error(response, "invalid content", http.StatusBadRequest)
return
} }
clientId, err := TokenValidateGetId(token) clientId, err := TokenValidateGetId(token)
if err != nil { if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized) http.Error(response, "invalid token", http.StatusUnauthorized)
return
} }
ctx := request.Context() ctx := request.Context()
err = WsSendToGroup(ctx, targetId, clientId, content) err = WsSendToGroup(ctx, targetId, clientId, content)
+1
View File
@@ -21,6 +21,7 @@ func main() {
http.HandleFunc("/new/token", withCORS(HttpHandleNewToken)) http.HandleFunc("/new/token", withCORS(HttpHandleNewToken))
http.HandleFunc("/new/group", withCORS(HttpHandeNewGroup)) http.HandleFunc("/new/group", withCORS(HttpHandeNewGroup))
http.HandleFunc("/mod/group/addclients", withCORS(HttpHandleGroupAddClient)) http.HandleFunc("/mod/group/addclients", withCORS(HttpHandleGroupAddClient))
http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage))
http.HandleFunc("/ws", ServeWsConnection) http.HandleFunc("/ws", ServeWsConnection)
log.Println("listening on :8080") log.Println("listening on :8080")
+2 -2
View File
@@ -1,5 +1,5 @@
TOKEN1=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZXhwIjoxNzc0ODY2MjE2LCJpYXQiOjE3NzQ4NjI2MTZ9.hxU-qpJ1Grz4yglr5TD_7ik5cIOU7_fkaIskoNdML0I TOKEN1=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZXhwIjoxNzc0ODc4MDY0LCJpYXQiOjE3NzQ4NzQ0NjR9.6X3z9j0z8USHh7GLKGWETr25_3Cqo9X9ZEfuhXIgePI
USER1_ID=1 USER1_ID=1
TOKEN2=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZXhwIjoxNzc0ODY2MjE3LCJpYXQiOjE3NzQ4NjI2MTd9.trOFcMUeFNrR2H9VlaM9vDYaupZq_ysGzCb1iz3Gxc0 TOKEN2=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZXhwIjoxNzc0ODc4MDY0LCJpYXQiOjE3NzQ4NzQ0NjR9.DCLl1_8VB1nCARc1c4eXpgUt8fzTjzw4KZNOjHv_bdY
USER2_ID=2 USER2_ID=2
GROUP_ID=1 GROUP_ID=1
+22 -21
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Send message from user_one to the group, verify user_two receives it # Send message from user_one to the group via HTTP, verify user_two receives it
source "$(dirname "$0")/config.sh" source "$(dirname "$0")/config.sh"
TOKEN1=$(load_state "TOKEN1") TOKEN1=$(load_state "TOKEN1")
@@ -16,7 +16,7 @@ fi
MESSAGE="Hello from user_one!" MESSAGE="Hello from user_one!"
echo "=== Sending message from user_one to group $GROUP_ID ===" echo "=== Sending message from user_one to group $GROUP_ID via HTTP ==="
TMPDIR=$(mktemp -d) TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT trap 'rm -rf "$TMPDIR"' EXIT
@@ -26,7 +26,7 @@ echo "[DEBUG] TOKEN1: ${TOKEN1:0:20}..."
echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..." echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..."
echo "[DEBUG] GROUP_ID: $GROUP_ID" echo "[DEBUG] GROUP_ID: $GROUP_ID"
# Receiver (user_two): authenticate then wait for messages # Receiver (user_two): authenticate via WebSocket then wait for messages
echo "[DEBUG] starting receiver (user_two)..." echo "[DEBUG] starting receiver (user_two)..."
{ echo '{"token":"'"$TOKEN2"'"}'; sleep 5; } \ { echo '{"token":"'"$TOKEN2"'"}'; sleep 5; } \
| stdbuf -oL websocat ws://localhost:8080/ws > "$TMPDIR/received" 2>"$TMPDIR/recv_err" & | stdbuf -oL websocat ws://localhost:8080/ws > "$TMPDIR/received" 2>"$TMPDIR/recv_err" &
@@ -34,32 +34,33 @@ RECV_PID=$!
echo "[DEBUG] receiver PID: $RECV_PID" echo "[DEBUG] receiver PID: $RECV_PID"
sleep 0.5 sleep 0.5
# Sender (user_one): authenticate, send message # Sender (user_one): send message via HTTP POST
SEND_PAYLOAD='{"subject":'"$GROUP_ID"',"content":"'"$MESSAGE"'"}' echo "[DEBUG] sending message via HTTP..."
echo "[DEBUG] starting sender (user_one)..." SEND_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/new/message" \
echo "[DEBUG] send payload: $SEND_PAYLOAD" -d "token=$TOKEN1" \
{ echo '{"token":"'"$TOKEN1"'"}'; sleep 0.5; echo "$SEND_PAYLOAD"; sleep 0.5; } \ -d "subject=$GROUP_ID" \
| websocat ws://localhost:8080/ws > "$TMPDIR/send_out" 2>"$TMPDIR/send_err" & -d "content=$MESSAGE")
SEND_PID=$! SEND_HTTP_CODE=$(echo "$SEND_RESPONSE" | tail -1)
echo "[DEBUG] sender PID: $SEND_PID" SEND_BODY=$(echo "$SEND_RESPONSE" | sed '$d')
echo "[DEBUG] send HTTP status: $SEND_HTTP_CODE"
echo "[DEBUG] send response body: $SEND_BODY"
if [[ "$SEND_HTTP_CODE" != "202" ]]; then
echo "FAIL: HTTP send failed with status $SEND_HTTP_CODE"
kill $RECV_PID 2>/dev/null
wait $RECV_PID 2>/dev/null
exit 1
fi
echo "[DEBUG] waiting 2s for message delivery..." echo "[DEBUG] waiting 2s for message delivery..."
sleep 2 sleep 2
echo "[DEBUG] killing connections..." echo "[DEBUG] killing receiver..."
kill $RECV_PID $SEND_PID 2>/dev/null kill $RECV_PID 2>/dev/null
wait $RECV_PID 2>/dev/null wait $RECV_PID 2>/dev/null
RECV_EXIT=$?
wait $SEND_PID 2>/dev/null
SEND_EXIT=$?
echo "[DEBUG] receiver exit: $RECV_EXIT, sender exit: $SEND_EXIT"
RECV_ERR=$(cat "$TMPDIR/recv_err") RECV_ERR=$(cat "$TMPDIR/recv_err")
SEND_ERR=$(cat "$TMPDIR/send_err")
SEND_OUT=$(cat "$TMPDIR/send_out")
[[ -n "$RECV_ERR" ]] && echo "[DEBUG] receiver stderr: $RECV_ERR" [[ -n "$RECV_ERR" ]] && echo "[DEBUG] receiver stderr: $RECV_ERR"
[[ -n "$SEND_ERR" ]] && echo "[DEBUG] sender stderr: $SEND_ERR"
[[ -n "$SEND_OUT" ]] && echo "[DEBUG] sender received: $SEND_OUT"
RESPONSE=$(cat "$TMPDIR/received") RESPONSE=$(cat "$TMPDIR/received")
echo "user_two received: $RESPONSE" echo "user_two received: $RESPONSE"
+22 -21
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Send message from user_two to the group, verify user_one receives it # Send message from user_two to the group via HTTP, verify user_one receives it
source "$(dirname "$0")/config.sh" source "$(dirname "$0")/config.sh"
TOKEN1=$(load_state "TOKEN1") TOKEN1=$(load_state "TOKEN1")
@@ -16,7 +16,7 @@ fi
MESSAGE="Hello from user_two!" MESSAGE="Hello from user_two!"
echo "=== Sending message from user_two to group $GROUP_ID ===" echo "=== Sending message from user_two to group $GROUP_ID via HTTP ==="
TMPDIR=$(mktemp -d) TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT trap 'rm -rf "$TMPDIR"' EXIT
@@ -26,7 +26,7 @@ echo "[DEBUG] TOKEN1: ${TOKEN1:0:20}..."
echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..." echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..."
echo "[DEBUG] GROUP_ID: $GROUP_ID" echo "[DEBUG] GROUP_ID: $GROUP_ID"
# Receiver (user_one): authenticate then wait for messages # Receiver (user_one): authenticate via WebSocket then wait for messages
echo "[DEBUG] starting receiver (user_one)..." echo "[DEBUG] starting receiver (user_one)..."
{ echo '{"token":"'"$TOKEN1"'"}'; sleep 5; } \ { echo '{"token":"'"$TOKEN1"'"}'; sleep 5; } \
| stdbuf -oL websocat ws://localhost:8080/ws > "$TMPDIR/received" 2>"$TMPDIR/recv_err" & | stdbuf -oL websocat ws://localhost:8080/ws > "$TMPDIR/received" 2>"$TMPDIR/recv_err" &
@@ -34,32 +34,33 @@ RECV_PID=$!
echo "[DEBUG] receiver PID: $RECV_PID" echo "[DEBUG] receiver PID: $RECV_PID"
sleep 0.5 sleep 0.5
# Sender (user_two): authenticate, send message # Sender (user_two): send message via HTTP POST
SEND_PAYLOAD='{"subject":'"$GROUP_ID"',"content":"'"$MESSAGE"'"}' echo "[DEBUG] sending message via HTTP..."
echo "[DEBUG] starting sender (user_two)..." SEND_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/new/message" \
echo "[DEBUG] send payload: $SEND_PAYLOAD" -d "token=$TOKEN2" \
{ echo '{"token":"'"$TOKEN2"'"}'; sleep 0.5; echo "$SEND_PAYLOAD"; sleep 0.5; } \ -d "subject=$GROUP_ID" \
| websocat ws://localhost:8080/ws > "$TMPDIR/send_out" 2>"$TMPDIR/send_err" & -d "content=$MESSAGE")
SEND_PID=$! SEND_HTTP_CODE=$(echo "$SEND_RESPONSE" | tail -1)
echo "[DEBUG] sender PID: $SEND_PID" SEND_BODY=$(echo "$SEND_RESPONSE" | sed '$d')
echo "[DEBUG] send HTTP status: $SEND_HTTP_CODE"
echo "[DEBUG] send response body: $SEND_BODY"
if [[ "$SEND_HTTP_CODE" != "202" ]]; then
echo "FAIL: HTTP send failed with status $SEND_HTTP_CODE"
kill $RECV_PID 2>/dev/null
wait $RECV_PID 2>/dev/null
exit 1
fi
echo "[DEBUG] waiting 2s for message delivery..." echo "[DEBUG] waiting 2s for message delivery..."
sleep 2 sleep 2
echo "[DEBUG] killing connections..." echo "[DEBUG] killing receiver..."
kill $RECV_PID $SEND_PID 2>/dev/null kill $RECV_PID 2>/dev/null
wait $RECV_PID 2>/dev/null wait $RECV_PID 2>/dev/null
RECV_EXIT=$?
wait $SEND_PID 2>/dev/null
SEND_EXIT=$?
echo "[DEBUG] receiver exit: $RECV_EXIT, sender exit: $SEND_EXIT"
RECV_ERR=$(cat "$TMPDIR/recv_err") RECV_ERR=$(cat "$TMPDIR/recv_err")
SEND_ERR=$(cat "$TMPDIR/send_err")
SEND_OUT=$(cat "$TMPDIR/send_out")
[[ -n "$RECV_ERR" ]] && echo "[DEBUG] receiver stderr: $RECV_ERR" [[ -n "$RECV_ERR" ]] && echo "[DEBUG] receiver stderr: $RECV_ERR"
[[ -n "$SEND_ERR" ]] && echo "[DEBUG] sender stderr: $SEND_ERR"
[[ -n "$SEND_OUT" ]] && echo "[DEBUG] sender received: $SEND_OUT"
RESPONSE=$(cat "$TMPDIR/received") RESPONSE=$(cat "$TMPDIR/received")
echo "user_one received: $RESPONSE" echo "user_one received: $RESPONSE"
+2 -1
View File
@@ -84,7 +84,8 @@ func WsSendToGroup(ctx context.Context, groupId uint32, senderId uint32, message
} }
client, err := CacheGetClientById(senderId) client, err := CacheGetClientById(senderId)
if err == nil { if err != nil {
client = &Client{Id: senderId}
err = DbSetClientById(ctx, client) err = DbSetClientById(ctx, client)
if err != nil { if err != nil {
return errors.New("non existing sender") return errors.New("non existing sender")