Skip to content
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

Add support for response compression in echoserver #730

Merged
merged 1 commit into from
Apr 17, 2019
Merged
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
28 changes: 26 additions & 2 deletions cmd/echo/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ limitations under the License.
package app

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"k8s.io/ingress-gce/pkg/version"
Expand Down Expand Up @@ -114,11 +117,32 @@ func echo(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Write(dumpData)
processedData, err := process(w, r, dumpData)
if err != nil {
klog.Errorf("error processing data dump: %v", err)
return
}

w.Write(processedData)
klog.V(3).Infof("echo: %v, %v, %v", time.Now(), r.UserAgent(), r.RemoteAddr)
}

// process does additional processing on response data if necessary.
func process(w http.ResponseWriter, r *http.Request, b []byte) ([]byte, error) {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious if there is any chance it will be double compressed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by double compressed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the incoming data is already compressed? Will this takes the data and compress it again?

var compressed bytes.Buffer
zw := gzip.NewWriter(&compressed)
_, err := zw.Write(b)
if err != nil {
return nil, fmt.Errorf("failed to compress data: %v", err)
}
zw.Close()
w.Header().Set("Content-Encoding", "gzip")
return compressed.Bytes(), nil
}
return b, nil
}

// setHeadersFromQueryString looks for certain keys in the request query string
// and sets response headers based on the values for those keys.
func setHeadersFromQueryString(w http.ResponseWriter, r *http.Request) {
Expand Down