MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / readSanitizeJSON

Method readSanitizeJSON

rest/handler.go:1334–1366  ·  view source on GitHub ↗

readSanitizeJSONInto reads and sanitizes a JSON request body and returns DatabaseConfig. Expands environment variables (if any) referenced in the config.

(val interface{})

Source from the content-addressed store, hash-verified

1332// readSanitizeJSONInto reads and sanitizes a JSON request body and returns DatabaseConfig.
1333// Expands environment variables (if any) referenced in the config.
1334func (h *handler) readSanitizeJSON(val interface{}) ([]byte, error) {
1335 // Performs the Content-Type validation and Content-Encoding check.
1336 input, err := processContentEncoding(h.rq.Header, h.requestBody, "application/json")
1337 if err != nil {
1338 return nil, err
1339 }
1340
1341 // Read body bytes to sanitize the content and substitute environment variables.
1342 defer func() { _ = input.Close() }()
1343 raw, err := io.ReadAll(input)
1344 if err != nil {
1345 return nil, err
1346 }
1347
1348 content, err := sanitiseConfig(h.ctx(), raw, h.server.Config.Unsupported.AllowDbConfigEnvVars)
1349 if err != nil {
1350 return nil, err
1351 }
1352
1353 // Decode the body bytes into target structure.
1354 decoder := base.JSONDecoder(bytes.NewReader(content))
1355 decoder.DisallowUnknownFields()
1356 decoder.UseNumber()
1357 err = decoder.Decode(&val)
1358
1359 if err != nil {
1360 err = base.WrapJSONUnknownFieldErr(err)
1361 if errors.Cause(err) != base.ErrUnknownField {
1362 err = base.HTTPErrorf(http.StatusBadRequest, "Bad JSON: %s", err.Error())
1363 }
1364 }
1365 return raw, err
1366}
1367
1368// readJavascript reads a javascript function from a request body.
1369func (h *handler) readJavascript() (string, error) {

Callers 1

Calls 11

ctxMethod · 0.95
JSONDecoderFunction · 0.92
WrapJSONUnknownFieldErrFunction · 0.92
HTTPErrorfFunction · 0.92
processContentEncodingFunction · 0.85
sanitiseConfigFunction · 0.85
DisallowUnknownFieldsMethod · 0.80
UseNumberMethod · 0.80
CloseMethod · 0.65
DecodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected