Create redirect URLs

Extract the values of common pseudo headers to generate a redirect URL.

About pseudo headers

Pseudo headers are special headers that are used in HTTP/2 to provide metadata about the request or response in a structured way. Although they look like traditional HTTP/1.x headers, they come with specific characteristics:

  • Must always start with a colon (:).
  • Must appear before regular headers in the HTTP/2 frame.
  • Contain details about the request or response.

Common pseudo headers include:

  • :method: The HTTP method that is used, such as GET or POST.
  • :scheme: The protocol that is used, such as http or https.
  • :authority: The hostname and port number that the request is sent to.
  • :path: The path of the request.

Before you begin

  1. Follow the Get started guide to install kgateway.

  2. Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.

  3. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/http -n kgateway-system 8080:8080

Set up redirect URLs

  1. Create a TrafficPolicy resource with the following transformation rules:

    • Build a redirect URL with the values of the :authority and :path pseudo headers. These headers are extracted from the request with the request_header function that is provided in kgateway.
    • The :authority pseudo header contains the hostname that the request is sent to.
    • The :path pseudo header is set to the request path.
    • The redirect URL is added to the x-forwarded-uri response header.
    kubectl apply -f- <<EOF
    apiVersion: gateway.kgateway.dev/v1alpha1
    kind: TrafficPolicy
    metadata:
      name: transformation
      namespace: httpbin
    spec:
      transformation:
        request:  
          add:
          - name: x-forwarded-uri
            value: 'https://{{ request_header(":authority") }}{{ request_header(":path") }}'
    EOF
  2. Update the HTTPRoute resource to apply the TrafficPolicy to the httpbin route by using an extensionRef filter.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin
      namespace: httpbin
      labels:
        example: httpbin-route
    spec:
      parentRefs:
        - name: http
          namespace: kgateway-system
      hostnames:
        - "www.example.com"
      rules:
        - backendRefs:
            - name: httpbin
              port: 8000
          filters:
          - type: ExtensionRef
            extensionRef:
              group: gateway.kgateway.dev
              kind: TrafficPolicy
              name: transformation
    EOF
  3. Send a request to the httpbin app and include your base64-encoded string in the x-base64-encoded request header. Verify that you get back a 200 HTTP response code and that you see the trimmed decoded value of your base64-encoded string in the x-base64-decoded response header.

    curl -vi http://$INGRESS_GW_ADDRESS:8080/get \
     -H "host: www.example.com:8080" 
    curl -vi localhost:8080/get \
    -H "host: www.example.com" 

    Example output:

    ...
    < HTTP/1.1 200 OK
    HTTP/1.1 200 OK
    ...
    
    {
      "args": {},
      "headers": {
        "Accept": [
          "*/*"
        ],
        "Host": [
          "www.example.com:8080"
        ],
        "User-Agent": [
          "curl/8.7.1"
        ],
        "X-Envoy-Expected-Rq-Timeout-Ms": [
          "15000"
        ],
        "X-Envoy-External-Address": [
          "10.0.9.76"
        ],
        "X-Forwarded-For": [
          "10.0.9.76"
        ],
        "X-Forwarded-Proto": [
          "http"
        ],
        "X-Forwarded-Uri": [
          "https://www.example.com:8080/get"
        ],
        "X-Request-Id": [
          "55140fab-c68e-44d3-a4fc-7f6242ba8194"
        ]
      },
      "origin": "10.0.9.76",
      "url": "http://www.example.com:8080/get"
    }
    

Cleanup

You can remove the resources that you created in this guide.
  1. Delete the TrafficPolicy resource.

    kubectl delete TrafficPolicy transformation -n httpbin
  2. Remove the extensionRef filter from the HTTPRoute resource.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin
      namespace: httpbin
      labels:
        example: httpbin-route
    spec:
      parentRefs:
        - name: http
          namespace: kgateway-system
      hostnames:
        - "www.example.com"
      rules:
        - backendRefs:
            - name: httpbin
              port: 8000
    EOF