From 94d191f62476fb7d65c16e7e5da7f9445caf9ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Fri, 12 Jul 2024 15:23:30 +0200 Subject: [PATCH] refactor: simplify code --- .../pkg/connector/httpadapter.go | 37 +++++++-------- .../collaboration/pkg/connector/utf7/utf7.go | 45 ++++++++++++------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/services/collaboration/pkg/connector/httpadapter.go b/services/collaboration/pkg/connector/httpadapter.go index e90756847..6dff8133b 100644 --- a/services/collaboration/pkg/connector/httpadapter.go +++ b/services/collaboration/pkg/connector/httpadapter.go @@ -23,6 +23,8 @@ const ( HeaderWopiSize string = "X-WOPI-Size" HeaderWopiValidRT string = "X-WOPI-ValidRelativeTarget" HeaderWopiRequestedName string = "X-WOPI-RequestedName" + HeaderContentLength string = "Content-Length" + HeaderContentType string = "Content-Type" ) // HttpAdapter will adapt the responses from the connector to HTTP. @@ -171,8 +173,8 @@ func (h *HttpAdapter) UnLock(w http.ResponseWriter, r *http.Request) { func (h *HttpAdapter) CheckFileInfo(w http.ResponseWriter, r *http.Request) { fileCon := h.con.GetFileConnector() - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Content-Length", "0") + w.Header().Set(HeaderContentType, "application/json") + w.Header().Set(HeaderContentLength, "0") fileInfo, err := fileCon.CheckFileInfo(r.Context()) if err != nil { @@ -190,10 +192,11 @@ func (h *HttpAdapter) CheckFileInfo(w http.ResponseWriter, r *http.Request) { jsonFileInfo, err := json.Marshal(fileInfo) if err != nil { logger.Error().Err(err).Msg("CheckFileInfo: failed to marshal fileinfo") + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } - w.Header().Set("Content-Length", strconv.Itoa(len(jsonFileInfo))) + w.Header().Set(HeaderContentLength, strconv.Itoa(len(jsonFileInfo))) w.WriteHeader(http.StatusOK) bytes, err := w.Write(jsonFileInfo) @@ -257,8 +260,8 @@ func (h *HttpAdapter) PutRelativeFile(w http.ResponseWriter, r *http.Request) { relativeTarget := r.Header.Get(HeaderWopiRT) suggestedTarget := r.Header.Get(HeaderWopiST) - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Content-Length", "0") + w.Header().Set(HeaderContentType, "application/json") + w.Header().Set(HeaderContentLength, "0") if relativeTarget != "" && suggestedTarget != "" { // headers are mutually exclusive @@ -290,17 +293,9 @@ func (h *HttpAdapter) PutRelativeFile(w http.ResponseWriter, r *http.Request) { } var conError *ConnectorError - if putErr != nil { - if errors.As(putErr, &conError) { - if headers != nil { - w.Header().Set(HeaderWopiValidRT, utf7.EncodeString(headers.ValidTarget)) - w.Header().Set(HeaderWopiLock, headers.LockID) - } - // we might still need to send a body, so we'll hold the write for now - } else { - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - return - } + if putErr != nil && !errors.As(putErr, &conError) { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return } logger := zerolog.Ctx(r.Context()) @@ -308,11 +303,16 @@ func (h *HttpAdapter) PutRelativeFile(w http.ResponseWriter, r *http.Request) { jsonFileInfo, err := json.Marshal(response) if err != nil { logger.Error().Err(err).Msg("PutRelativeFile: failed to marshal response") + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } - w.Header().Set("Content-Length", strconv.Itoa(len(jsonFileInfo))) + w.Header().Set(HeaderContentLength, strconv.Itoa(len(jsonFileInfo))) if conError != nil { + if headers != nil { + w.Header().Set(HeaderWopiValidRT, utf7.EncodeString(headers.ValidTarget)) + w.Header().Set(HeaderWopiLock, headers.LockID) + } w.WriteHeader(conError.HttpCodeOut) } else { w.WriteHeader(http.StatusOK) @@ -384,10 +384,11 @@ func (h *HttpAdapter) RenameFile(w http.ResponseWriter, r *http.Request) { jsonFileInfo, err := json.Marshal(response) if err != nil { logger.Error().Err(err).Msg("RenameFile: failed to marshal response") + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } - w.Header().Set("Content-Length", strconv.Itoa(len(jsonFileInfo))) + w.Header().Set(HeaderContentLength, strconv.Itoa(len(jsonFileInfo))) w.WriteHeader(http.StatusOK) bytes, err := w.Write(jsonFileInfo) diff --git a/services/collaboration/pkg/connector/utf7/utf7.go b/services/collaboration/pkg/connector/utf7/utf7.go index 76f357632..92456e451 100644 --- a/services/collaboration/pkg/connector/utf7/utf7.go +++ b/services/collaboration/pkg/connector/utf7/utf7.go @@ -100,23 +100,8 @@ func DecodeString(s string) (string, error) { } else { // utf7 range utf7ByteRange := byteArray[v.Low:v.High] - if len(utf7ByteRange) == 2 && utf7ByteRange[0] == '+' && utf7ByteRange[1] == '-' { - // special case for the "+-" sequence -> just write "+" as replacement - sb.WriteByte('+') - } else { - // utf7 range must start with "+" and should (but might not) end with "-" - // we need to remove those chars before decoding - toDecode := byteArray[v.Low+1 : v.High-1] - if byteArray[v.High-1] != '-' { - toDecode = byteArray[v.Low+1 : v.High] - } - runeArray, err := convertFromUtf7(toDecode) - if err != nil { - return "", err - } - for _, r := range runeArray { - sb.WriteRune(r) - } + if err := convertRangeFromUtf7(utf7ByteRange, &sb); err != nil { + return "", err } } } @@ -269,6 +254,32 @@ func convertToUtf7(runes []rune) []byte { return dst } +// convertRangeFromUtf7 will convert an utf7 byte range (enclosed in +// the "+" and "-" chars) and write the result in the provided string builder. +// The string builder won't be modified other than to append the result. +// An error might be returned if there is any problem with the conversion. +func convertRangeFromUtf7(utf7ByteRange []byte, sb *strings.Builder) error { + if len(utf7ByteRange) == 2 && utf7ByteRange[0] == '+' && utf7ByteRange[1] == '-' { + // special case for the "+-" sequence -> just write "+" as replacement + sb.WriteByte('+') + } else { + // utf7 range must start with "+" and should (but might not) end with "-" + // we need to remove those chars before decoding + toDecode := utf7ByteRange[1 : len(utf7ByteRange)-1] + if utf7ByteRange[len(utf7ByteRange)-1] != '-' { + toDecode = utf7ByteRange[1:] + } + runeArray, err := convertFromUtf7(toDecode) + if err != nil { + return err + } + for _, r := range runeArray { + sb.WriteRune(r) + } + } + return nil +} + // convertFromUtf7 will convert the sequence of bytes to runes. The sequence // of bytes is assumed to be an UTF-7 encoded sequence (without the "+" and // "-" limiters)