fix http group sending
This commit is contained in:
@@ -281,28 +281,37 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
|
||||
}
|
||||
|
||||
func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
|
||||
token := request.FormValue("token")
|
||||
if token == "" {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
targetStr := request.FormValue("subject")
|
||||
if targetStr == "" {
|
||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
targetId, err := ConvertStringUint32(targetStr)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
content := request.FormValue("content")
|
||||
if content == "" {
|
||||
http.Error(response, "invalid content", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
clientId, err := TokenValidateGetId(token)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
err = WsSendToGroup(ctx, targetId, clientId, content)
|
||||
|
||||
@@ -21,6 +21,7 @@ func main() {
|
||||
http.HandleFunc("/new/token", withCORS(HttpHandleNewToken))
|
||||
http.HandleFunc("/new/group", withCORS(HttpHandeNewGroup))
|
||||
http.HandleFunc("/mod/group/addclients", withCORS(HttpHandleGroupAddClient))
|
||||
http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage))
|
||||
http.HandleFunc("/ws", ServeWsConnection)
|
||||
|
||||
log.Println("listening on :8080")
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
TOKEN1=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZXhwIjoxNzc0ODY2MjE2LCJpYXQiOjE3NzQ4NjI2MTZ9.hxU-qpJ1Grz4yglr5TD_7ik5cIOU7_fkaIskoNdML0I
|
||||
TOKEN1=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZXhwIjoxNzc0ODc4MDY0LCJpYXQiOjE3NzQ4NzQ0NjR9.6X3z9j0z8USHh7GLKGWETr25_3Cqo9X9ZEfuhXIgePI
|
||||
USER1_ID=1
|
||||
TOKEN2=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZXhwIjoxNzc0ODY2MjE3LCJpYXQiOjE3NzQ4NjI2MTd9.trOFcMUeFNrR2H9VlaM9vDYaupZq_ysGzCb1iz3Gxc0
|
||||
TOKEN2=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZXhwIjoxNzc0ODc4MDY0LCJpYXQiOjE3NzQ4NzQ0NjR9.DCLl1_8VB1nCARc1c4eXpgUt8fzTjzw4KZNOjHv_bdY
|
||||
USER2_ID=2
|
||||
GROUP_ID=1
|
||||
|
||||
+22
-21
@@ -1,5 +1,5 @@
|
||||
#!/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"
|
||||
|
||||
TOKEN1=$(load_state "TOKEN1")
|
||||
@@ -16,7 +16,7 @@ fi
|
||||
|
||||
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)
|
||||
trap 'rm -rf "$TMPDIR"' EXIT
|
||||
@@ -26,7 +26,7 @@ echo "[DEBUG] TOKEN1: ${TOKEN1:0:20}..."
|
||||
echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..."
|
||||
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 '{"token":"'"$TOKEN2"'"}'; sleep 5; } \
|
||||
| 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"
|
||||
sleep 0.5
|
||||
|
||||
# Sender (user_one): authenticate, send message
|
||||
SEND_PAYLOAD='{"subject":'"$GROUP_ID"',"content":"'"$MESSAGE"'"}'
|
||||
echo "[DEBUG] starting sender (user_one)..."
|
||||
echo "[DEBUG] send payload: $SEND_PAYLOAD"
|
||||
{ echo '{"token":"'"$TOKEN1"'"}'; sleep 0.5; echo "$SEND_PAYLOAD"; sleep 0.5; } \
|
||||
| websocat ws://localhost:8080/ws > "$TMPDIR/send_out" 2>"$TMPDIR/send_err" &
|
||||
SEND_PID=$!
|
||||
echo "[DEBUG] sender PID: $SEND_PID"
|
||||
# Sender (user_one): send message via HTTP POST
|
||||
echo "[DEBUG] sending message via HTTP..."
|
||||
SEND_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/new/message" \
|
||||
-d "token=$TOKEN1" \
|
||||
-d "subject=$GROUP_ID" \
|
||||
-d "content=$MESSAGE")
|
||||
SEND_HTTP_CODE=$(echo "$SEND_RESPONSE" | tail -1)
|
||||
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..."
|
||||
sleep 2
|
||||
|
||||
echo "[DEBUG] killing connections..."
|
||||
kill $RECV_PID $SEND_PID 2>/dev/null
|
||||
echo "[DEBUG] killing receiver..."
|
||||
kill $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")
|
||||
SEND_ERR=$(cat "$TMPDIR/send_err")
|
||||
SEND_OUT=$(cat "$TMPDIR/send_out")
|
||||
[[ -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")
|
||||
echo "user_two received: $RESPONSE"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/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"
|
||||
|
||||
TOKEN1=$(load_state "TOKEN1")
|
||||
@@ -16,7 +16,7 @@ fi
|
||||
|
||||
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)
|
||||
trap 'rm -rf "$TMPDIR"' EXIT
|
||||
@@ -26,7 +26,7 @@ echo "[DEBUG] TOKEN1: ${TOKEN1:0:20}..."
|
||||
echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..."
|
||||
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 '{"token":"'"$TOKEN1"'"}'; sleep 5; } \
|
||||
| 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"
|
||||
sleep 0.5
|
||||
|
||||
# Sender (user_two): authenticate, send message
|
||||
SEND_PAYLOAD='{"subject":'"$GROUP_ID"',"content":"'"$MESSAGE"'"}'
|
||||
echo "[DEBUG] starting sender (user_two)..."
|
||||
echo "[DEBUG] send payload: $SEND_PAYLOAD"
|
||||
{ echo '{"token":"'"$TOKEN2"'"}'; sleep 0.5; echo "$SEND_PAYLOAD"; sleep 0.5; } \
|
||||
| websocat ws://localhost:8080/ws > "$TMPDIR/send_out" 2>"$TMPDIR/send_err" &
|
||||
SEND_PID=$!
|
||||
echo "[DEBUG] sender PID: $SEND_PID"
|
||||
# Sender (user_two): send message via HTTP POST
|
||||
echo "[DEBUG] sending message via HTTP..."
|
||||
SEND_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/new/message" \
|
||||
-d "token=$TOKEN2" \
|
||||
-d "subject=$GROUP_ID" \
|
||||
-d "content=$MESSAGE")
|
||||
SEND_HTTP_CODE=$(echo "$SEND_RESPONSE" | tail -1)
|
||||
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..."
|
||||
sleep 2
|
||||
|
||||
echo "[DEBUG] killing connections..."
|
||||
kill $RECV_PID $SEND_PID 2>/dev/null
|
||||
echo "[DEBUG] killing receiver..."
|
||||
kill $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")
|
||||
SEND_ERR=$(cat "$TMPDIR/send_err")
|
||||
SEND_OUT=$(cat "$TMPDIR/send_out")
|
||||
[[ -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")
|
||||
echo "user_one received: $RESPONSE"
|
||||
|
||||
+2
-1
@@ -84,7 +84,8 @@ func WsSendToGroup(ctx context.Context, groupId uint32, senderId uint32, message
|
||||
}
|
||||
|
||||
client, err := CacheGetClientById(senderId)
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
client = &Client{Id: senderId}
|
||||
err = DbSetClientById(ctx, client)
|
||||
if err != nil {
|
||||
return errors.New("non existing sender")
|
||||
|
||||
Reference in New Issue
Block a user