buildRelayURL builds the websocket URL for the chat stream endpoint on a peer replica. It maps http(s) schemes to ws(s).
(address string, chatID uuid.UUID)
| 835 | // buildRelayURL builds the websocket URL for the chat stream |
| 836 | // endpoint on a peer replica. It maps http(s) schemes to ws(s). |
| 837 | func buildRelayURL(address string, chatID uuid.UUID) (string, error) { |
| 838 | u, err := url.Parse(address) |
| 839 | if err != nil { |
| 840 | return "", xerrors.Errorf("parse relay address %q: %w", address, err) |
| 841 | } |
| 842 | switch u.Scheme { |
| 843 | case "http": |
| 844 | u.Scheme = "ws" |
| 845 | case "https": |
| 846 | u.Scheme = "wss" |
| 847 | case "ws", "wss": |
| 848 | // already a websocket URL, leave as-is. |
| 849 | default: |
| 850 | return "", xerrors.Errorf("unsupported relay address scheme %q", u.Scheme) |
| 851 | } |
| 852 | u.Path = fmt.Sprintf("/api/experimental/chats/%s/stream", chatID) |
| 853 | q := u.Query() |
| 854 | // Relays only need live message_part events, not the full |
| 855 | // history; pass the relay sentinel so the peer skips its |
| 856 | // durable DB snapshot and delivers in-flight parts only. |
| 857 | q.Set("after_id", strconv.FormatInt(osschatd.RelaySentinelAfterID, 10)) |
| 858 | u.RawQuery = q.Encode() |
| 859 | return u.String(), nil |
| 860 | } |
| 861 | |
| 862 | // extractSessionToken returns the session token carried by the |
| 863 | // given request headers. It mirrors the priority order used by |