Skip to content
Merged
2 changes: 1 addition & 1 deletion packages/api/internal/orchestrator/create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (o *Orchestrator) CreateSandbox(
return sandbox.Sandbox{}, &api.APIError{
Code: http.StatusInternalServerError,
ClientMsg: "Failed to create sandbox",
Err: fmt.Errorf("failed to get create sandbox: %w", err),
Err: fmt.Errorf("failed to place sandbox: %w", err),
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/client-proxy/internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port
reverseproxy.ClientProxyRetries,
idleTimeout,
func(r *http.Request) (*pool.Destination, error) {
sandboxId, port, err := reverseproxy.ParseHost(r.Host)
sandboxId, port, err := reverseproxy.GetHostPort(r)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewSandboxProxy(meterProvider metric.MeterProvider, port uint16, sandboxes
reverseproxy.SandboxProxyRetries,
idleTimeout,
func(r *http.Request) (*pool.Destination, error) {
sandboxId, port, err := reverseproxy.ParseHost(r.Host)
sandboxId, port, err := reverseproxy.GetHostPort(r)
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions packages/shared/pkg/proxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import (

type InvalidHostError struct{}

func (e *InvalidHostError) Error() string {
func (e InvalidHostError) Error() string {
return "invalid url host"
}

type InvalidSandboxPortError struct{}

func (e *InvalidSandboxPortError) Error() string {
func (e InvalidSandboxPortError) Error() string {
return "invalid sandbox port"
}

func NewErrSandboxNotFound(sandboxId string) *SandboxNotFoundError {
return &SandboxNotFoundError{
func NewErrSandboxNotFound(sandboxId string) SandboxNotFoundError {
return SandboxNotFoundError{
SandboxId: sandboxId,
}
}
Expand All @@ -35,7 +35,7 @@ type SandboxNotFoundError struct {
SandboxId string
}

func (e *SandboxNotFoundError) Error() string {
func (e SandboxNotFoundError) Error() string {
return "sandbox not found"
}

Expand All @@ -61,7 +61,9 @@ func handler(p *pool.ProxyPool, getDestination func(r *http.Request) (*pool.Dest

var notFoundErr *SandboxNotFoundError
if errors.As(err, &notFoundErr) {
zap.L().Warn("sandbox not found", zap.String("host", r.Host), logger.WithSandboxID(notFoundErr.SandboxId))
zap.L().Warn("sandbox not found",
zap.String("host", r.Host),
logger.WithSandboxID(notFoundErr.SandboxId))

err := template.
NewSandboxNotFoundError(notFoundErr.SandboxId, r.Host).
Expand Down
41 changes: 37 additions & 4 deletions packages/shared/pkg/proxy/host.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,66 @@
package proxy

import (
"net/http"
"strconv"
"strings"
)

func ParseHost(host string) (sandboxID string, port uint64, err error) {
func GetHostPort(r *http.Request) (string, uint64, error) {
sandboxId, port, err := parseHost(r.Host)
if err != nil {
sandboxId, port, err = parseHeader(r.Header)
}
return sandboxId, port, err
}

func parseHost(host string) (sandboxID string, port uint64, err error) {
dot := strings.Index(host, ".")

// There must be always domain part used
if dot == -1 {
return "", 0, &InvalidHostError{}
return "", 0, InvalidHostError{}
}

// Keep only the left-most subdomain part, i.e. everything before the
host = host[:dot]

hostParts := strings.Split(host, "-")
if len(hostParts) < 2 {
return "", 0, &InvalidHostError{}
return "", 0, InvalidHostError{}
}

sandboxPortString := hostParts[0]
sandboxID = hostParts[1]

sandboxPort, err := strconv.ParseUint(sandboxPortString, 10, 64)
if err != nil {
return "", 0, &InvalidSandboxPortError{}
return "", 0, InvalidSandboxPortError{}
}

return sandboxID, sandboxPort, nil
}

const (
headerSandboxID = "x-sandbox-id"
headerSandboxPort = "x-sandbox-port"
)

func parseHeader(h http.Header) (sandboxID string, port uint64, err error) {
sandboxID = h.Get(headerSandboxID)
if sandboxID == "" {
return "", 0, InvalidHostError{}
}

portString := h.Get(headerSandboxPort)
if portString == "" {
return "", 0, InvalidSandboxPortError{}
}

port, err = strconv.ParseUint(portString, 10, 64)
if err != nil {
return "", 0, InvalidSandboxPortError{}
}

return sandboxID, port, nil
}
Loading