Skip to content

Commit a1f8305

Browse files
authored
chore: remove unnecessary utils (#267)
1 parent b718899 commit a1f8305

File tree

8 files changed

+11
-212
lines changed

8 files changed

+11
-212
lines changed

client/proxy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"net/http"
2323
"net/http/httputil"
24+
"strings"
2425

2526
"github.com/golang/glog"
2627
"github.com/pkg/errors"
@@ -30,7 +31,6 @@ import (
3031
"golang.stackrox.io/grpc-http1/internal/grpcweb"
3132
"golang.stackrox.io/grpc-http1/internal/httputils"
3233
"golang.stackrox.io/grpc-http1/internal/pipeconn"
33-
"golang.stackrox.io/grpc-http1/internal/stringutils"
3434
"google.golang.org/grpc"
3535
"google.golang.org/grpc/codes"
3636
"google.golang.org/grpc/connectivity"
@@ -49,7 +49,7 @@ func modifyResponse(resp *http.Response) error {
4949
// Make sure headers do not get flushed, as otherwise the gRPC client will complain about missing trailers.
5050
resp.Header.Set(dontFlushHeadersHeaderKey, "true")
5151
}
52-
contentType, contentSubType := stringutils.Split2(resp.Header.Get("Content-Type"), "+")
52+
contentType, contentSubType, _ := strings.Cut(resp.Header.Get("Content-Type"), "+")
5353
if contentType != "application/grpc-web" {
5454
// No modification necessary if we aren't handling a gRPC web response.
5555
return nil

internal/grpcweb/response_writer.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import (
1818
"bytes"
1919
"encoding/binary"
2020
"net/http"
21+
"slices"
2122
"strings"
22-
23-
"golang.stackrox.io/grpc-http1/internal/sliceutils"
24-
"golang.stackrox.io/grpc-http1/internal/stringutils"
2523
)
2624

2725
type responseWriter struct {
@@ -69,12 +67,12 @@ func (w *responseWriter) prepareHeadersIfNecessary() {
6967
}
7068

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

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

7977
respContentType := "application/grpc-web"
8078
if contentType == "application/grpc" && contentSubtype != "" {

internal/sliceutils/utils.go

-42
This file was deleted.

internal/sliceutils/utils_test.go

-87
This file was deleted.

internal/stringutils/split.go

-28
This file was deleted.

internal/stringutils/split_test.go

-41
This file was deleted.

server/server.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919
"fmt"
2020
"net/http"
21+
"slices"
2122
"strings"
2223
"sync"
2324
"unicode"
@@ -27,8 +28,6 @@ import (
2728
"golang.stackrox.io/grpc-http1/internal/grpcweb"
2829
"golang.stackrox.io/grpc-http1/internal/grpcwebsocket"
2930
"golang.stackrox.io/grpc-http1/internal/size"
30-
"golang.stackrox.io/grpc-http1/internal/sliceutils"
31-
"golang.stackrox.io/grpc-http1/internal/stringutils"
3231
"google.golang.org/grpc"
3332
)
3433

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

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

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

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

server/websocket_writer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
"bytes"
1919
"io"
2020
"net/http"
21+
"slices"
2122
"strings"
2223

2324
"github.com/golang/glog"
2425
"golang.stackrox.io/grpc-http1/internal/grpcproto"
25-
"golang.stackrox.io/grpc-http1/internal/sliceutils"
2626
)
2727

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

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

0 commit comments

Comments
 (0)