hostmetaget.go (2269B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package hostmeta 19 20 import ( 21 "bytes" 22 "encoding/xml" 23 "net/http" 24 25 "github.com/gin-gonic/gin" 26 apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" 27 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 28 ) 29 30 // HostMetaGETHandler swagger:operation GET /.well-known/host-meta hostMetaGet 31 // 32 // Returns a compliant hostmeta response to web host metadata queries. 33 // 34 // See: https://www.rfc-editor.org/rfc/rfc6415.html 35 // 36 // --- 37 // tags: 38 // - .well-known 39 // 40 // produces: 41 // - application/xrd+xml" 42 // 43 // responses: 44 // '200': 45 // schema: 46 // "$ref": "#/definitions/hostmeta" 47 func (m *Module) HostMetaGETHandler(c *gin.Context) { 48 if _, err := apiutil.NegotiateAccept(c, apiutil.HostMetaHeaders...); err != nil { 49 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 50 return 51 } 52 53 hostMeta := m.processor.Fedi().HostMetaGet() 54 55 // this setup with a separate buffer we encode into is used because 56 // xml.Marshal does not emit xml.Header by itself 57 var buf bytes.Buffer 58 59 // Preallocate buffer of reasonable length. 60 buf.Grow(len(xml.Header) + 64) 61 62 // No need to check for error on write to buffer. 63 _, _ = buf.WriteString(xml.Header) 64 65 // Encode host-meta as XML to in-memory buffer. 66 if err := xml.NewEncoder(&buf).Encode(hostMeta); err != nil { 67 apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1) 68 return 69 } 70 71 c.Data(http.StatusOK, HostMetaContentType, buf.Bytes()) 72 }