# Request Lifecycle

## Introduction

When using any tool in the "real world", you feel more confident if you understand how it works. This page follows one request from the moment it arrives on port 8000 to the moment its process exits, which is about twenty milliseconds later.

## Lifecycle Overview

### First steps: socat

`./artisan serve` runs one command and becomes it:

```bash
exec socat TCP-LISTEN:8000,reuseaddr,fork EXEC:bootstrap/handle
```

`socat` listens on port 8000. For each connection, `fork` makes a copy of socat, and `EXEC` runs `bootstrap/handle` with the TCP socket as its **stdin** and **stdout**. The handler doesn't know about networks: it reads a request from stdin and writes a response to stdout, like any other Unix program.

> [!NOTE]
> Every request is a separate process with nothing shared, which is how PHP has worked since 1995. Laravel Octane exists to keep one process alive between requests. Laravel Bash has gone the other way.

### The HTTP kernel

`bootstrap/handle` sources `bootstrap/app.sh`, which turns on strict mode, loads `.env` and sources every file in `framework/`, `app/` and `routes/`. Then `http::handle` runs:

```bash
exec 3>&1 4>&2           # keep the socket on fd 3, and the server log on fd 4
http::read_request       # the request line, the headers, then the body
BODY_FILE=$(mktemp)
trap http::send EXIT     # send the response when this process exits, however it exits
trap 'http::exception $? "$BASH_COMMAND" $LINENO' ERR
exec 1>"$BODY_FILE"      # from here on, anything printed is the response body
router::dispatch
```

The trick is file descriptors. The socket is copied to fd 3 for safe keeping, then stdout is pointed at a temporary file. Controllers just print HTML. When the process exits, the `EXIT` trap measures the file for `Content-Length`, writes the status line and headers to the socket, then `cat`s the body after them.

### Reading the request

HTTP/1.1 is a text protocol, which is the only reason any of this is possible:

```text
POST /posts HTTP/1.1
Host: localhost:8000
Content-Type: application/x-www-form-urlencoded
Content-Length: 46

title=Hello&body=A+body+that+is+long+enough.
```

The request line and headers are read with `read -r`, one line at a time, until a blank line. Each line ends in `\r\n`, so `${line%$'\r'}` takes the `\r` off. The body has no newline at the end, so it's read with `read -N "$length"`, in the C locale, because `Content-Length` counts bytes and Bash otherwise counts characters.

### Routing

The router compares the path with each route's regex using `[[ =~ ]]` and calls the first match, with the captured parameters as arguments. See [Routing](/docs/routing).

### Sending the response

When the controller returns, the process exits, and `http::send` runs:

```text
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
x-powered-by: Laravel Bash
content-length: 2817
connection: close
```

Then a line goes to the server log on fd 4, and a `RequestHandled` event goes to the [Pulse](/pulse) page.

## Streaming responses

Server-Sent Events can't be buffered to a file, because they never end. `http::stream` writes the headers straight to fd 3 and marks the response as streamed, so the `EXIT` trap leaves it alone. See [Broadcasting](/docs/broadcasting).
