This topic describes the background and limits of HTTP functions for custom container runtime. This topic also describes how to determine and invoke HTTP functions and provides sample code.

Background information

Function Compute forwards your requests, including the method, path, query, request headers, request body, and common headers generated by Function Compute to the HTTP server. HTTP functions allow you to smoothly migrate existing HTTP web applications. For more information, see HTTP functions.

Determine the type of the invoked function

For a custom container runtime, you can determine whether a request is used to invoke an HTTP function or an event function based on the x-fc-control-path header.

Note Generally, you do not need to take note of the x-fc-control-path header.
  • /invoke: The request is used to invoke an event function. /invoke indicates that the request is used to invoke an event function.
  • /initialize: The request is automatically initiated by Function Compute to invoke an initializer function when an execution environment is created for the first time. In the lifecycle of a container, the initializer function is invoked only once, which is similar to the class constructor.
  • /http-invoke: The request is used to invoke an HTTP function. /http-invoke indicates that the request is used to invoke an HTTP function. Function Compute adds common headers to your request that includes the HTTP method, request path, request body, query parameters, and request headers and forwards the request to the custom container runtime. The custom container runtime returns the response headers and body to the client.

Calls

When an HTTP function is called, the client directly accesses the web API and the web page provided by your function. Therefore, you do not need to implement /invoke which is otherwise required when a non-HTTP function is called. The custom runtime receives other headers except common headers.

Header Description
x-fc-base-path If you do not configure a custom domain name, the value of x-fc-base-path is /2016-08-15/proxy/${servicename}/${functionname}/. You may need to set this value to the Path Base of a web application in the code. The process varies with languages or frameworks.

Limits

  • After an HTTP trigger is set for a function, other types of triggers cannot be set for this function.
  • Only one HTTP trigger can be created for each function.
  • If version management is enabled for a function, only one HTTP trigger can be created for each version or alias of the function. In other words, one HTTP trigger can be created for one version or one alias of a function. For more information about versions and aliases, see Introduction to versions.
  • The following fields in a request header and request headers that start with x-fc- cannot be customized:
    • accept-encoding
    • connection
    • keep-alive
    • proxy-authorization
    • te
    • trailer
    • transfer-encoding
  • The following fields in a response header and response headers that start with x-fc- cannot be customized:
    • connection
    • content-length
    • content-encoding
    • date
    • keep-alive
    • proxy-authenticate
    • server
    • trailer
    • transfer-encoding
    • upgrade
    • content-disposition:attachment
      Note For security, when the default aliyuncs.com domain name of Function Compute is used, the server forcibly adds the content-disposition: attachment field to the response header. This field is used to download the returned result in the browser as an attachment. To remove this limit, you must set a custom domain name. For more information, see t1881050.html#multiTask2940.
  • Limits on HTTP requests

    If a request exceeds one of the following limits, the system returns status code 400 and error code InvalidArgument.

    • Header size: The total size of all keys and values in the headers cannot exceed 4 KB.
    • Path size: The total size of the path, including all of the query parameters, cannot exceed 4 KB.
    • Body size: The total size of the HTTP request body cannot exceed 6 MB.
  • Limits on HTTP responses

    If a response exceeds one of the following limits, the system returns status code 502 and error code BadResponse.

    • Header size: The total size of all keys and values in the headers cannot exceed 4 KB.
    • Body size: The total size of the HTTP response body cannot exceed 6 MB.
  • Other instructions

    You can map different HTTP paths for HTTP functions by binding a custom domain name. For more information, see t1881050.html#multiTask2940. You can also use API Gateway, set the backend service type to HTTP, and configure the HTTP function path as the backend service address to implement similar features. For more information, see Use Function Compute as the backend service of an API operation.

Sample code

In the following Node.js Express example, the GET and POST methods are routed to different handlers. You can map a path to a handler that you need.

'use strict';

const express = require('express');

// Constants
const PORT = 9000;
const HOST = '0.0.0.0';

// HTTP function get
const app = express();
app.get('/*', (req, res) => {
  res.send('Hello FunctionCompute, http GET');
});

app.post('/*', (req, res) => {
  res.send('Hello FunctionCompute, http POST');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);