Skip to main content
A CDN stage can run only one FastEdge application. Several checks or request modifications at the same stage belong in one handler. This example combines three operations in on_request_headers: blocking requests from selected countries, rejecting user agents containing bot, and forwarding a query token to the origin for authorization. The same approach combines other checks and request modifications. Before writing the handler, complete the CDN setup to create the my_cdn_app project.

Write the checks in one handler

Replace src/lib.rs in the my_cdn_app project with the following code. The handler checks the client country, user agent, and token query parameter in that order. If a check fails, it rejects the request. Otherwise, it forwards the request to the origin with the token in the Authorization header and the country code in x-client-country. request.country contains the two-letter country code derived from the client IP. Replace ZZ in BLOCKED_COUNTRIES with the country codes to block.
All three operations run in on_http_request_headers. Each check can stop the request by sending an HTTP response and returning Action::Pause. If no check rejects the request, the handler can modify its headers and return Action::Continue. To combine different operations, add their logic to the same handler. The handler uses add_http_request_header to forward Authorization and x-client-country to the origin. In this environment, set_http_request_header changes a header within the application but does not forward that updated value to the origin.

Build the binary

Build with the same target as the CDN setup.
The binary is target/wasm32-wasip1/release/my_cdn_app.wasm.

Deploy the application

To deploy through the Customer Portal, follow the CDN deploy guide to upload my_cdn_app.wasm and attach the application to on_request_headers. To deploy through the REST API, run the following commands. REST API requests require an API token. Set it as an environment variable before running the commands:

Step 1. Upload the binary

Use the returned binary id as BINARY_ID when creating the application.
The API returns:

Step 2. Create the application

Create a FastEdge application from the uploaded binary. Setting status to 1 enables the application.
The API returns:

Step 3. Attach the application

Attach the application to the CDN resource’s on_request_headers stage. The app_id field accepts a single application ID as a string.
Passing an array of application IDs instead returns HTTP 400:

Check the results

Test the application through the CDN hostname. 1. Blocked country Send a request from a country listed in BLOCKED_COUNTRIES. The application returns HTTP 403 with the body country-rejected, without running the user-agent or token checks. A request with an empty request.country value is rejected in the same way. 2. Bot user agent Send a request from a permitted country with a user agent containing bot and a token query parameter. The application returns HTTP 403 with the body bot-rejected. The match is case-insensitive. 3. Missing token Send a request from a permitted country with a user agent that does not contain bot and without a token query parameter. The application returns HTTP 401 with the body token-rejected. All rejected requests include content-type: text/plain, cache-control: no-store, and x-client-country in their responses. 4. Forward to origin Send a request from a permitted country with a user agent that does not contain bot and a nonempty token query parameter. The request continues to the origin with Authorization set to Bearer followed by the token value, and x-client-country set to the client country code.