Skip to content

chore: remove unnecessary utils #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"net/http"
"net/http/httputil"
"strings"

"github.com/golang/glog"
"github.com/pkg/errors"
Expand All @@ -30,7 +31,6 @@ import (
"golang.stackrox.io/grpc-http1/internal/grpcweb"
"golang.stackrox.io/grpc-http1/internal/httputils"
"golang.stackrox.io/grpc-http1/internal/pipeconn"
"golang.stackrox.io/grpc-http1/internal/stringutils"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
Expand All @@ -49,7 +49,7 @@ func modifyResponse(resp *http.Response) error {
// Make sure headers do not get flushed, as otherwise the gRPC client will complain about missing trailers.
resp.Header.Set(dontFlushHeadersHeaderKey, "true")
}
contentType, contentSubType := stringutils.Split2(resp.Header.Get("Content-Type"), "+")
contentType, contentSubType, _ := strings.Cut(resp.Header.Get("Content-Type"), "+")
if contentType != "application/grpc-web" {
// No modification necessary if we aren't handling a gRPC web response.
return nil
Expand Down
8 changes: 3 additions & 5 deletions internal/grpcweb/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import (
"bytes"
"encoding/binary"
"net/http"
"slices"
"strings"

"golang.stackrox.io/grpc-http1/internal/sliceutils"
"golang.stackrox.io/grpc-http1/internal/stringutils"
)

type responseWriter struct {
Expand Down Expand Up @@ -69,12 +67,12 @@ func (w *responseWriter) prepareHeadersIfNecessary() {
}

hdr := w.w.Header()
w.announcedTrailers = sliceutils.ShallowClone(hdr["Trailer"])
w.announcedTrailers = slices.Clone(hdr["Trailer"])
// Trailers are sent in a data frame, so don't announce trailers as otherwise downstream proxies might get confused.
hdr.Del("Trailer")

// "Downgrade" response content type to grpc-web.
contentType, contentSubtype := stringutils.Split2(hdr.Get("Content-Type"), "+")
contentType, contentSubtype, _ := strings.Cut(hdr.Get("Content-Type"), "+")

respContentType := "application/grpc-web"
if contentType == "application/grpc" && contentSubtype != "" {
Expand Down
42 changes: 0 additions & 42 deletions internal/sliceutils/utils.go

This file was deleted.

87 changes: 0 additions & 87 deletions internal/sliceutils/utils_test.go

This file was deleted.

28 changes: 0 additions & 28 deletions internal/stringutils/split.go

This file was deleted.

41 changes: 0 additions & 41 deletions internal/stringutils/split_test.go

This file was deleted.

9 changes: 4 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"strings"
"sync"
"unicode"
Expand All @@ -27,8 +28,6 @@ import (
"golang.stackrox.io/grpc-http1/internal/grpcweb"
"golang.stackrox.io/grpc-http1/internal/grpcwebsocket"
"golang.stackrox.io/grpc-http1/internal/size"
"golang.stackrox.io/grpc-http1/internal/sliceutils"
"golang.stackrox.io/grpc-http1/internal/stringutils"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -108,11 +107,11 @@ func handleGRPCWeb(w http.ResponseWriter, req *http.Request, validPaths map[stri
}

acceptedContentTypes := strings.FieldsFunc(strings.Join(req.Header["Accept"], ","), spaceOrComma)
acceptGRPCWeb := sliceutils.Find(acceptedContentTypes, "application/grpc-web") != -1
acceptGRPCWeb := slices.Index(acceptedContentTypes, "application/grpc-web") != -1
// The standard gRPC client doesn't actually send an `Accept: application/grpc` header, so always assume
// the client accepts gRPC _unless_ it explicitly specifies an `application/grpc-web` accept header
// WITHOUT an `application/grpc` accept header.
acceptGRPC := !acceptGRPCWeb || sliceutils.Find(acceptedContentTypes, "application/grpc") != -1
acceptGRPC := !acceptGRPCWeb || slices.Index(acceptedContentTypes, "application/grpc") != -1

// Only consider sending a gRPC response if we are not told to prefer gRPC-Web or the client doesn't support
// gRPC-Web.
Expand Down Expand Up @@ -197,7 +196,7 @@ func CreateDowngradingHandler(grpcSrv *grpc.Server, httpHandler http.Handler, op
}

func isContentTypeValid(contentType string) bool {
ct, _ := stringutils.Split2(contentType, "+")
ct, _, _ := strings.Cut(contentType, "+")
return ct == "application/grpc" || ct == "application/grpc-web"
}

Expand Down
4 changes: 2 additions & 2 deletions server/websocket_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"bytes"
"io"
"net/http"
"slices"
"strings"

"github.com/golang/glog"
"golang.stackrox.io/grpc-http1/internal/grpcproto"
"golang.stackrox.io/grpc-http1/internal/sliceutils"
)

// wsResponseWriter is a http.ResponseWriter to be used for WebSocket connections.
Expand Down Expand Up @@ -67,7 +67,7 @@ func (w *wsResponseWriter) WriteHeader(statusCode int) {
}

hdr := w.header
w.announcedTrailers = sliceutils.ShallowClone(hdr["Trailer"])
w.announcedTrailers = slices.Clone(hdr["Trailer"])
// Trailers will be sent un-announced in non-Trailers-only responses.
hdr.Del("Trailer")

Expand Down
Loading