easy +8 pts

Parse HTTP Status Line

Extract version, code, and reason phrase from an HTTP status line.

Write a function `parse_status_line(line: str) -> dict` that takes a single HTTP status line as a string and returns a dictionary with keys `'version'`, `'status_code'`, and `'reason'`. The input is guaranteed to be a well-formed HTTP status line as defined by RFC 7230: `HTTP-version SP status-code SP reason-phrase CRLF` Note: The trailing CRLF is part of the line and must be handled. The reason phrase can contain any printable characters except CR and LF. A reason phrase may be empty (i.e., the status line ends with a space before the CRLF). Your function should: - Remove the trailing CRLF (`\r\n`) from the line. - Split the remaining string into exactly three parts: the HTTP version, the status code, and the reason phrase. - Return a dictionary with: - `'version'`: the HTTP version string (e.g., `'HTTP/1.1'`) - `'status_code'`: the integer status code (e.g., `200`) - `'reason'`: the reason phrase string (could be an empty string) You should not use external libraries. Importing `re` or other modules is allowed but not necessary.

Constraints

- Input is a string containing exactly one HTTP status line (including the trailing CRLF). - The HTTP version is one of `HTTP/1.0`, `HTTP/1.1`, `HTTP/2.0`, `HTTP/3.0` (but any `HTTP/x.y` is acceptable). - The status code is a 3-digit integer between 100 and 599. - The reason phrase may contain spaces and arbitrary printable ASCII characters, but no CR or LF. - The reason phrase may be empty. - Complexity: O(n) time, O(n) space for output.

Example

```python
>>> parse_status_line('HTTP/1.1 200 OK\r\n')
{'version': 'HTTP/1.1', 'status_code': 200, 'reason': 'OK'}

>>> parse_status_line('HTTP/1.1 404 Not Found\r\n')
{'version': 'HTTP/1.1', 'status_code': 404, 'reason': 'Not Found'}

>>> parse_status_line('HTTP/1.0 204 No Content\r\n')
{'version': 'HTTP/1.0', 'status_code': 204, 'reason': 'No Content'}

>>> parse_status_line('HTTP/2.0 200 \r\n')
{'version': 'HTTP/2.0', 'status_code': 200, 'reason': ''}
```
8 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Remember the trailing CRLF (`\r\n`) — strip it first.
Use `line.split(' ', 2)` to split into three parts without breaking the reason phrase.
The status code is a string like '200' — convert it to int.
If the reason phrase is empty, the stripped line ends with a space, so split with maxsplit=2 will give an empty string as the third element.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.