MCPcopy
hub / github.com/caddyserver/caddy / handleAdapt

Method handleAdapt

caddyconfig/load.go:137–175  ·  view source on GitHub ↗

handleAdapt adapts the given Caddy config to JSON and responds with the result.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

135
136// handleAdapt adapts the given Caddy config to JSON and responds with the result.
137func (adminLoad) handleAdapt(w http.ResponseWriter, r *http.Request) error {
138 if r.Method != http.MethodPost {
139 return caddy.APIError{
140 HTTPStatus: http.StatusMethodNotAllowed,
141 Err: fmt.Errorf("method not allowed"),
142 }
143 }
144
145 buf := bufPool.Get().(*bytes.Buffer)
146 buf.Reset()
147 defer bufPool.Put(buf)
148
149 _, err := io.Copy(buf, r.Body)
150 if err != nil {
151 return caddy.APIError{
152 HTTPStatus: http.StatusBadRequest,
153 Err: fmt.Errorf("reading request body: %v", err),
154 }
155 }
156
157 result, warnings, err := adaptByContentType(r.Header.Get("Content-Type"), buf.Bytes())
158 if err != nil {
159 return caddy.APIError{
160 HTTPStatus: http.StatusBadRequest,
161 Err: err,
162 }
163 }
164
165 out := struct {
166 Warnings []Warning `json:"warnings,omitempty"`
167 Result json.RawMessage `json:"result"`
168 }{
169 Warnings: warnings,
170 Result: result,
171 }
172
173 w.Header().Set("Content-Type", "application/json")
174 return json.NewEncoder(w).Encode(out)
175}
176
177// adaptByContentType adapts body to Caddy JSON using the adapter specified by contentType.
178// If contentType is empty or ends with "/json", the input will be returned, as a no-op.

Callers

nothing calls this directly

Calls 6

adaptByContentTypeFunction · 0.85
GetMethod · 0.65
ResetMethod · 0.65
NewEncoderMethod · 0.65
SetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected