The HTTP Signature for Scale Functions is meant to be used to implement HTTP handlers and middleware in supported guest languages.

It’s designed to allow developers to read and modify the incoming request as well as the outgoing response.

Usage

The HTTP Signature is implemented differently between the Guest and Host languages. The Host language has direct access to the HTTP Request and Response objects, while the Guest language has accessor methods to read and modify the request and response.

This allows the Host language complete control over the HTTP request and response, while the Guest language has stricter restrictions on what can be modified and how it can be modified.

Request

The HTTP Request object contains the following fields:

FieldTypeDescription
uristringThe URI of the request.
methodenumThe HTTP method of the request.
content_lengthuint64The length of the request body.
protocolenumThe HTTP protocol of the request.
ipstringThe IP address of the request.
bodybytesThe request body.
headersstring_mapThe request headers.

It can be accessed using the Request(): Request method. The following are methods that can be used on the returned object.

URI

The uri field is a string that contains the URI of the request. It can be accessed using the URI(): string method and modified using the SetURI(uri: string) method.

Method

The method field is an enum that contains the HTTP method of the request. It can be accessed using the Method(): method method and modified using the SetMethod(method: method) method.

Valid values for the method field are:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • HEAD
  • OPTIONS

Content Length

The content_length field is a uint64 that contains the length of the request body. It can be accessed using the ContentLength(): uint64 method and modified using the SetContentLength(length: uint64) method.

Protocol

The protocol field is an enum that contains the HTTP protocol of the request. It can be accessed using the Protocol(): protocol method and modified using the SetProtocol(protocol: protocol) method.

Valid values for the protocol field are:

  • HTTP/1.1
  • HTTP/2
  • HTTP/3

IP

The ip field is a string that contains the IP address of the request. It can be accessed using the IP(): string method and modified using the SetIP(ip: string) method.

Body

The body field is a bytes that contains the request body. It can be accessed using the Body(): []bytes method and modified using the SetBody(body: []bytes) method.

If you’d like to set the body to a string, you can use the SetBodyString(body: string) method.

Headers

The headers field is a string_map that contains the request headers. It can be accessed using the Header() method.

Writing a key to the headers returned by the Header(): header method can be done using the Set(key: string, value: []string) method. Reading a key from the headers returned by the Header(): header method can be done using the Get(key: string): []string method.

Response

The HTTP Response object contains the following fields:

FieldTypeDescription
status_codeuint32The status code of the response.
bodybytesThe response body.
headersstring_mapThe response headers.

It can be accessed using the Response(): Response method. The following are methods that can be used on the returned object.

Status Code

The status_code field is a uint32 that contains the status code of the response. It can be accessed using the StatusCode(): uint32 method and modified using the SetStatusCode(status_code: uint32) method.

Body

The body field is a bytes that contains the response body. It can be accessed using the Body(): []bytes method and modified using the SetBody(body: []bytes) method.

If you’d like to set the body to a string, you can use the SetBodyString(body: string) method.

Headers

The headers field is a string_map that contains the response headers. It can be accessed using the Header() method.

Writing a key to the headers returned by the Header(): header method can be done using the Set(key: string, value: []string) method. Reading a key from the headers returned by the Header(): header method can be done using the Get(key: string): []string method.

Definition

The HTTP Signature is defined as follows using the Scale Signature syntax:

http.signature
version = "v1alpha"
context = "request"

model Headers {
    string "values" {
        default = ""
        accessor = true
    }
}

model Request {
    string "uri" {
        default = ""
        accessor = true
    }

    string "method" {
      default     = "GET"
    }


    uint64 ContentLength {
        default = 0
        accessor = true
    }

    string "protocol" {
        default = "HTTP/1.1"
    }

    string "ip" {
        default = ""
        accessor = true
    }

    bytes "body" {
        accessor = true
        initial_size = 0
    }

    string_map HttpHeaders {
      value = "headers"
    }
}

model Response {
    uint32 StatusCode {
        default = 200
        accessor = true
        limit_validator {
            min = 100
            max = 599
        }
    }

    bytes "body" {
        initial_size = 0
        accessor = true
    }

    string_map HttpHeaders {
      value = "headers"
    }
}