ServeHTTP handles POST requests to the transport endpoint.
(w http.ResponseWriter, req *http.Request)
| 138 | |
| 139 | // ServeHTTP handles POST requests to the transport endpoint. |
| 140 | func (t *SSEServerTransport) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 141 | if t.incoming == nil { |
| 142 | http.Error(w, "session not connected", http.StatusInternalServerError) |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | // Read and parse the message. |
| 147 | data, err := io.ReadAll(req.Body) |
| 148 | if err != nil { |
| 149 | http.Error(w, "failed to read body", http.StatusBadRequest) |
| 150 | return |
| 151 | } |
| 152 | // Optionally, we could just push the data onto a channel, and let the |
| 153 | // message fail to parse when it is read. This failure seems a bit more |
| 154 | // useful |
| 155 | msg, err := jsonrpc2.DecodeMessage(data) |
| 156 | if err != nil { |
| 157 | http.Error(w, "failed to parse body", http.StatusBadRequest) |
| 158 | return |
| 159 | } |
| 160 | if req, ok := msg.(*jsonrpc.Request); ok { |
| 161 | if _, err := checkRequest(req, serverMethodInfos); err != nil { |
| 162 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 163 | return |
| 164 | } |
| 165 | } |
| 166 | select { |
| 167 | case t.incoming <- msg: |
| 168 | w.WriteHeader(http.StatusAccepted) |
| 169 | case <-t.done: |
| 170 | http.Error(w, "session closed", http.StatusBadRequest) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Connect sends the 'endpoint' event to the client. |
| 175 | // See [SSEServerTransport] for more details on the [Connection] implementation. |
nothing calls this directly
no test coverage detected