𝗥𝗲𝗤𝗨𝗘𝗦𝗧 𝗔𝗡𝗗 𝗥𝗘𝗦𝗣𝗢𝗡𝗦𝗘 𝗗𝗘𝗘𝗣 𝗗𝗜𝗩𝗘

Every HTTP server follows a specific cycle. It accepts a connection, parses a request, matches a route, and sends a response.

Hyperlane makes this process easy. You can manage this cycle using direct method calls or simple attribute macros.

How Hyperlane Handles the Request Lifecycle:

  • The server accepts a TCP connection.
  • It parses the stream into a Request object.
  • Request middleware processes the data.
  • The server matches the request to a route.
  • The route handler executes.
  • Response middleware runs.
  • The response goes back to the client.

Accessing Request Data: You can use the Context object to pull data from a request. • get_method() for the HTTP method. • get_path() for the URL path. • get_headers() for all headers. • get_body_json() to parse JSON bodies. • try_get_query() to find specific parameters.

Hyperlane also uses attribute macros to automate data extraction. Instead of writing manual code, you can use tags like:

  • #[request_body_json] to parse JSON instantly.
  • #[request_query] to grab URL parameters.
  • #[request_header] to pull specific headers.

Route Management and Filtering: You can define dynamic routes using the {param} syntax. For example: "/test/{text}". Hyperlane also lets you filter requests at the route level. You can:

  • Match specific hosts with #[host].
  • Reject specific referers with #[reject_referer].
  • Apply custom logic with #[filter].

Managing Responses and Cookies: You control the response through the Context object. You can set status codes, headers, and bodies manually. To simplify this, use response macros:

  • #[response_status_code(200)]
  • #[response_body("Hello World")]

For session management, use the CookieBuilder. It allows you to set expiration dates, domains, and security flags like HttpOnly or Secure with a clean syntax.

Once your response is ready, you send it through the stream using methods like build() and try_send().

Hyperlane gives you two ways to work. Use method calls when you need explicit control. Use attribute macros when you want clean, fast code.

Project Code: https://github.com/hyperlane-dev/hyperlane

Source: https://dev.to/tengxgfyrz67s/request-and-response-deep-dive-1j1b