From 8e3536d7f5478b5b0b4f0746cc88b4778e053b41 Mon Sep 17 00:00:00 2001 From: Nghia Tran Date: Fri, 15 Feb 2019 16:35:35 -0800 Subject: [PATCH] Use knative/pkg/websocket. Delete moved code. --- pkg/activator/handler/handler.go | 2 +- pkg/network/websocket/hijack.go | 34 ------------ pkg/network/websocket/hijack_test.go | 79 ---------------------------- pkg/queue/timeout.go | 2 +- 4 files changed, 2 insertions(+), 115 deletions(-) delete mode 100644 pkg/network/websocket/hijack.go delete mode 100644 pkg/network/websocket/hijack_test.go diff --git a/pkg/activator/handler/handler.go b/pkg/activator/handler/handler.go index 10c40c27e950..ab34af36bbb8 100644 --- a/pkg/activator/handler/handler.go +++ b/pkg/activator/handler/handler.go @@ -23,10 +23,10 @@ import ( "strconv" "time" + "github.com/knative/pkg/websocket" "github.com/knative/serving/pkg/activator" "github.com/knative/serving/pkg/activator/util" pkghttp "github.com/knative/serving/pkg/http" - "github.com/knative/serving/pkg/network/websocket" "go.uber.org/zap" ) diff --git a/pkg/network/websocket/hijack.go b/pkg/network/websocket/hijack.go deleted file mode 100644 index 8c7423c53f83..000000000000 --- a/pkg/network/websocket/hijack.go +++ /dev/null @@ -1,34 +0,0 @@ -/* -Copyright 2019 The Knative Authors -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package websocket - -import ( - "bufio" - "fmt" - "net" - "net/http" -) - -// TODO(tcnghia): Move this to knative/pkg. - -// HijackIfPossible calls Hijack() on the given http.ResponseWriter if it implements -// http.Hijacker interface, which is required for net/http/httputil/reverseproxy -// to handle connection upgrade/switching protocol. Otherwise returns an error. -func HijackIfPossible(w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) { - hj, ok := w.(http.Hijacker) - if !ok { - return nil, nil, fmt.Errorf("wrapped writer of type %T can't be hijacked", w) - } - return hj.Hijack() -} diff --git a/pkg/network/websocket/hijack_test.go b/pkg/network/websocket/hijack_test.go deleted file mode 100644 index 75d94bd06979..000000000000 --- a/pkg/network/websocket/hijack_test.go +++ /dev/null @@ -1,79 +0,0 @@ -/* -Copyright 2019 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package websocket - -import ( - "bufio" - "bytes" - "net" - "net/http" - "testing" -) - -// TODO(tcnghia): Move this to knative/pkg. - -// hijackable is a http.ResponseWriter that implements http.Hijacker -type hijackable struct { - http.ResponseWriter - w *bufio.ReadWriter -} - -// Hijack implements http.Hijacker -func (h *hijackable) Hijack() (net.Conn, *bufio.ReadWriter, error) { - return nil, h.w, nil -} - -func newHijackable() *hijackable { - var b *bytes.Buffer - return &hijackable{ - w: bufio.NewReadWriter(bufio.NewReader(b), bufio.NewWriter(b)), - } -} - -type notHijackable struct { - http.ResponseWriter -} - -func TestHijackIfPossible(t *testing.T) { - h := newHijackable() - for _, test := range []struct { - name string - w http.ResponseWriter - wantErr bool - wantReadWriter *bufio.ReadWriter - }{{ - name: "Hijacker type", - w: h, - wantErr: false, - wantReadWriter: h.w, - }, { - name: "non-Hijacker type", - w: ¬Hijackable{}, - wantErr: true, - wantReadWriter: nil, - }} { - t.Run(test.name, func(t *testing.T) { - _, w, err := HijackIfPossible(test.w) - if test.wantErr == (err == nil) { - t.Errorf("wantErr=%v, but got err=%v", test.wantErr, err) - } - if test.wantReadWriter != w { - t.Errorf("Wanted ReadWriter %v got %v", test.wantReadWriter, w) - } - }) - } -} diff --git a/pkg/queue/timeout.go b/pkg/queue/timeout.go index e82d99ef2aa9..92cddf647d37 100644 --- a/pkg/queue/timeout.go +++ b/pkg/queue/timeout.go @@ -25,7 +25,7 @@ import ( "sync" "time" - "github.com/knative/serving/pkg/network/websocket" + "github.com/knative/pkg/websocket" ) var defaultTimeoutBody = "Timeout

Timeout

"