Skip to content
Open
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
27 changes: 22 additions & 5 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,7 @@ func (h *uploadHandler) serveResumable(w http.ResponseWriter, r *http.Request) {
errorResponse(ctx, w, errInternalError(err.Error()))
return
}
addr := server.httpServer.Addr
if !strings.HasPrefix(addr, "http") {
addr = "http://" + addr
}
addr = strings.TrimRight(addr, "/")
addr := uploadBaseURL(r, server.httpServer.Addr)
w.Header().Add(
"Location",
fmt.Sprintf(
Expand Down Expand Up @@ -3501,3 +3497,24 @@ func (h *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
errorResponse(ctx, w, errInternalError(fmt.Sprintf("unexpected request path: %s", html.EscapeString(r.URL.Path))))
}

// uploadBaseURL returns the scheme and host the client used for r, so the
// resumable upload session URL is reachable by that client. The listen
// address (e.g. 0.0.0.0:9050) is only a fallback when r carries no Host.
func uploadBaseURL(r *http.Request, listenAddr string) string {
if r.Host == "" {
addr := listenAddr
if !strings.HasPrefix(addr, "http") {
addr = "http://" + addr
}
return strings.TrimRight(addr, "/")
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
if proto := r.Header.Get("X-Forwarded-Proto"); proto == "http" || proto == "https" {
scheme = proto
}
return scheme + "://" + r.Host
}
65 changes: 65 additions & 0 deletions server/upload_location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package server_test

import (
"context"
"net/http"
"strings"
"testing"

"github.com/goccy/bigquery-emulator/server"
"github.com/goccy/bigquery-emulator/types"
)

// The resumable upload session URL must point at the host the client used,
// not at the listen address: with --host=0.0.0.0 the client would otherwise
// PUT to http://0.0.0.0:9050, which Windows rejects (WinError 10049).
func TestResumableUploadLocationUsesRequestHost(t *testing.T) {
ctx := context.Background()
bqServer, err := server.New(server.TempStorage)
if err != nil {
t.Fatal(err)
}
if err := bqServer.Load(server.StructSource(types.NewProject("test", types.NewDataset("ds")))); err != nil {
t.Fatal(err)
}
ts := bqServer.TestServer()
t.Cleanup(func() {
ts.Close()
bqServer.Stop(ctx)
})
baseURL := ts.URL
for _, tc := range []struct {
name string
headers map[string]string
want string
}{
{name: "host header", want: "http://localhost:9050"},
{name: "forwarded https", headers: map[string]string{"X-Forwarded-Proto": "https"}, want: "https://localhost:9050"},
} {
t.Run(tc.name, func(t *testing.T) {
jobID := "location_" + strings.ReplaceAll(tc.name, " ", "_")
body := `{"jobReference":{"projectId":"test","jobId":"` + jobID + `"},"configuration":{"load":{"destinationTable":{"projectId":"test","datasetId":"ds","tableId":"t"}}}}`
req, err := http.NewRequest(http.MethodPost, baseURL+"/upload/bigquery/v2/projects/test/jobs?uploadType=resumable", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
req.Host = "localhost:9050"
req.Header.Set("Content-Type", "application/json")
for k, v := range tc.headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("start resumable upload: %d", resp.StatusCode)
}
want := tc.want + "/upload/bigquery/v2/projects/test/jobs?uploadType=resumable&upload_id=" + jobID
if got := resp.Header.Get("Location"); got != want {
t.Fatalf("Location = %q, want %q", got, want)
}
})
}
}