handleAdapt adapts the given Caddy config to JSON and responds with the result.
(w http.ResponseWriter, r *http.Request)
| 135 | |
| 136 | // handleAdapt adapts the given Caddy config to JSON and responds with the result. |
| 137 | func (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. |
nothing calls this directly
no test coverage detected