readChatContextBody reads and validates the request body for chat context endpoints. It handles MaxBytesReader wrapping, error responses, and body rewind. If the body is empty or whitespace-only and allowEmpty is true, it returns false without writing an error. nolint:revive // Add and clear endpoi
(ctx context.Context, rw http.ResponseWriter, r *http.Request, dst any, allowEmpty bool)
| 2454 | // |
| 2455 | //nolint:revive // Add and clear endpoints only differ by empty-body handling. |
| 2456 | func readChatContextBody(ctx context.Context, rw http.ResponseWriter, r *http.Request, dst any, allowEmpty bool) bool { |
| 2457 | r.Body = http.MaxBytesReader(rw, r.Body, maxChatContextRequestBodyBytes) |
| 2458 | body, err := io.ReadAll(r.Body) |
| 2459 | if err != nil { |
| 2460 | var maxBytesErr *http.MaxBytesError |
| 2461 | if errors.As(err, &maxBytesErr) { |
| 2462 | httpapi.Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{ |
| 2463 | Message: "Request body too large.", |
| 2464 | Detail: fmt.Sprintf("Maximum request body size is %d bytes.", maxChatContextRequestBodyBytes), |
| 2465 | }) |
| 2466 | return false |
| 2467 | } |
| 2468 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 2469 | Message: "Failed to read request body.", |
| 2470 | Detail: err.Error(), |
| 2471 | }) |
| 2472 | return false |
| 2473 | } |
| 2474 | if allowEmpty && len(bytes.TrimSpace(body)) == 0 { |
| 2475 | r.Body = http.NoBody |
| 2476 | return false |
| 2477 | } |
| 2478 | |
| 2479 | r.Body = io.NopCloser(bytes.NewReader(body)) |
| 2480 | return httpapi.Read(ctx, rw, r, dst) |
| 2481 | } |
| 2482 | |
| 2483 | // @x-apidocgen {"skip": true} |
| 2484 | func (api *API) workspaceAgentAddChatContext(rw http.ResponseWriter, r *http.Request) { |
no test coverage detected