# Parser

<a id="parser-1"></a>

# Parser

```cpp
#include <icy/http/parser.h>

class Parser
```

[Defined in src/http/include/icy/http/parser.h:55](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L55)

HTTP request/response parser using the llhttp library.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Parser`](#parser-2)  | Creates a response parser. The response object is populated as data is parsed. |
|  | [`Parser`](#parser-3)  | Creates a request parser. The request object is populated as data is parsed. |
|  | [`Parser`](#parser-4)  | Creates a parser of the given type without binding a message object. |
|  | [`~Parser`](#parser-5)  |  |
|  | [`Parser`](#parser-6)  | Deleted constructor. |
| [`Parser`](#parser-1) & | [`operator=`](#operator-204)  | Deleted assignment operator. |
|  | [`Parser`](#parser-7)  | Deleted constructor. |
| [`Parser`](#parser-1) & | [`operator=`](#operator-205)  | Deleted assignment operator. |
| [`ParseResult`](https://auscaster.com/icey/docs/api-reference/icy-http-Parser-ParseResult.md#parseresult) | [`parse`](#parse-4)  | Feeds a buffer of raw HTTP data into the parser. |
| `void` | [`reset`](#reset-19)  | Reset the internal state (reinitialises llhttp). Safe to call externally, NOT from inside llhttp callbacks. |
| `void` | [`resetState`](#resetstate)  | Reset internal flags without reinitialising llhttp. Safe to call from inside llhttp callbacks (e.g. on_message_begin). |
| `bool` | [`complete`](#complete-3) `const` | Returns true if parsing is complete, either in success or error. |
| `bool` | [`upgrade`](#upgrade) `const` | Returns true if the connection should be upgraded. |
| `llhttp_type_t` | [`type`](#type-12) `const` `inline` | Returns the parser type (HTTP_REQUEST or HTTP_RESPONSE). |
| `void` | [`setRequest`](#setrequest)  | Binds an HTTP request object to populate during parsing. Must only be called when no message is currently set and type is HTTP_REQUEST. |
| `void` | [`setResponse`](#setresponse)  | Binds an HTTP response object to populate during parsing. Must only be called when no message is currently set and type is HTTP_RESPONSE. |
| `void` | [`setObserver`](#setobserver)  | Sets the observer that receives parser events. |
| `void` | [`clearMessage`](#clearmessage)  | Clear request/response pointers so they can be re-set. Used when resetting a pooled connection for reuse. |
| [`http::Message`](https://auscaster.com/icey/docs/api-reference/icy-http-Message.md#message-1) * | [`message`](#message-5)  | Returns the currently bound message (request or response), or nullptr. |
| [`ParserObserver`](https://auscaster.com/icey/docs/api-reference/icy-http-ParserObserver.md#parserobserver) * | [`observer`](#observer) `const` | Returns the current parser observer, or nullptr if none is set. |

---

<a id="parser-2"></a>

### Parser

```cpp
Parser(http::Response * response)
```

[Defined in src/http/include/icy/http/parser.h:72](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L72)

Creates a response parser. The response object is populated as data is parsed.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `response` | [`http::Response`](https://auscaster.com/icey/docs/api-reference/icy-http-Response.md#response-3) * | HTTP response object to populate. |

---

<a id="parser-3"></a>

### Parser

```cpp
Parser(http::Request * request)
```

[Defined in src/http/include/icy/http/parser.h:76](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L76)

Creates a request parser. The request object is populated as data is parsed.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `request` | [`http::Request`](https://auscaster.com/icey/docs/api-reference/icy-http-Request.md#request-6) * | HTTP request object to populate. |

---

<a id="parser-4"></a>

### Parser

```cpp
Parser(llhttp_type_t type)
```

[Defined in src/http/include/icy/http/parser.h:80](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L80)

Creates a parser of the given type without binding a message object.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `type` | `llhttp_type_t` | Either HTTP_REQUEST or HTTP_RESPONSE. |

---

<a id="parser-5"></a>

### ~Parser

```cpp
~Parser()
```

[Defined in src/http/include/icy/http/parser.h:81](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L81)

---

<a id="parser-6"></a>

### Parser

```cpp
Parser(const Parser &) = delete
```

[Defined in src/http/include/icy/http/parser.h:83](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L83)

Deleted constructor.

---

<a id="operator-204"></a>

### operator=

```cpp
Parser & operator=(const Parser &) = delete
```

[Defined in src/http/include/icy/http/parser.h:84](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L84)

Deleted assignment operator.

---

<a id="parser-7"></a>

### Parser

```cpp
Parser(Parser &&) = delete
```

[Defined in src/http/include/icy/http/parser.h:85](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L85)

Deleted constructor.

---

<a id="operator-205"></a>

### operator=

```cpp
Parser & operator=(Parser &&) = delete
```

[Defined in src/http/include/icy/http/parser.h:86](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L86)

Deleted assignment operator.

---

<a id="parse-4"></a>

### parse

```cpp
ParseResult parse(const char * data, size_t length)
```

[Defined in src/http/include/icy/http/parser.h:95](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L95)

Feeds a buffer of raw HTTP data into the parser.

May be called multiple times for streaming data. The parser state persists between calls. On completion or error, the observer is notified. 
#### Returns
Structured parse result including bytes consumed and terminal state.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `data` | `const char *` | Pointer to the input data buffer. |
| `length` | `size_t` | Number of bytes in the buffer. |

---

<a id="reset-19"></a>

### reset

```cpp
void reset()
```

[Defined in src/http/include/icy/http/parser.h:99](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L99)

Reset the internal state (reinitialises llhttp). Safe to call externally, NOT from inside llhttp callbacks.

---

<a id="resetstate"></a>

### resetState

```cpp
void resetState()
```

[Defined in src/http/include/icy/http/parser.h:103](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L103)

Reset internal flags without reinitialising llhttp. Safe to call from inside llhttp callbacks (e.g. on_message_begin).

---

<a id="complete-3"></a>

### complete

`const`

```cpp
bool complete() const
```

[Defined in src/http/include/icy/http/parser.h:107](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L107)

Returns true if parsing is complete, either in success or error.

---

<a id="upgrade"></a>

### upgrade

`const`

```cpp
bool upgrade() const
```

[Defined in src/http/include/icy/http/parser.h:110](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L110)

Returns true if the connection should be upgraded.

---

<a id="type-12"></a>

### type

`const` `inline`

```cpp
inline llhttp_type_t type() const
```

[Defined in src/http/include/icy/http/parser.h:113](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L113)

Returns the parser type (HTTP_REQUEST or HTTP_RESPONSE).

---

<a id="setrequest"></a>

### setRequest

```cpp
void setRequest(http::Request * request)
```

[Defined in src/http/include/icy/http/parser.h:118](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L118)

Binds an HTTP request object to populate during parsing. Must only be called when no message is currently set and type is HTTP_REQUEST.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `request` | [`http::Request`](https://auscaster.com/icey/docs/api-reference/icy-http-Request.md#request-6) * | The request object to populate. |

---

<a id="setresponse"></a>

### setResponse

```cpp
void setResponse(http::Response * response)
```

[Defined in src/http/include/icy/http/parser.h:123](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L123)

Binds an HTTP response object to populate during parsing. Must only be called when no message is currently set and type is HTTP_RESPONSE.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `response` | [`http::Response`](https://auscaster.com/icey/docs/api-reference/icy-http-Response.md#response-3) * | The response object to populate. |

---

<a id="setobserver"></a>

### setObserver

```cpp
void setObserver(ParserObserver * observer)
```

[Defined in src/http/include/icy/http/parser.h:127](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L127)

Sets the observer that receives parser events.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `observer` | [`ParserObserver`](https://auscaster.com/icey/docs/api-reference/icy-http-ParserObserver.md#parserobserver) * | Observer to notify. May be nullptr to clear. |

---

<a id="clearmessage"></a>

### clearMessage

```cpp
void clearMessage()
```

[Defined in src/http/include/icy/http/parser.h:131](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L131)

Clear request/response pointers so they can be re-set. Used when resetting a pooled connection for reuse.

---

<a id="message-5"></a>

### message

```cpp
http::Message * message()
```

[Defined in src/http/include/icy/http/parser.h:134](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L134)

Returns the currently bound message (request or response), or nullptr.

---

<a id="observer"></a>

### observer

`const`

```cpp
ParserObserver * observer() const
```

[Defined in src/http/include/icy/http/parser.h:137](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L137)

Returns the current parser observer, or nullptr if none is set.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`ParserObserver`](https://auscaster.com/icey/docs/api-reference/icy-http-ParserObserver.md#parserobserver) * | [`_observer`](#_observer)  |  |
| [`http::Request`](https://auscaster.com/icey/docs/api-reference/icy-http-Request.md#request-6) * | [`_request`](#_request-2)  |  |
| [`http::Response`](https://auscaster.com/icey/docs/api-reference/icy-http-Response.md#response-3) * | [`_response`](#_response-2)  |  |
| [`http::Message`](https://auscaster.com/icey/docs/api-reference/icy-http-Message.md#message-1) * | [`_message`](#_message)  |  |
| `llhttp_t` | [`_parser`](#_parser-1)  |  |
| `llhttp_settings_t` | [`_settings`](#_settings)  |  |
| `llhttp_type_t` | [`_type`](#_type)  |  |
| `bool` | [`_wasHeaderValue`](#_washeadervalue)  |  |
| `std::string` | [`_lastHeaderField`](#_lastheaderfield)  |  |
| `std::string` | [`_lastHeaderValue`](#_lastheadervalue)  |  |
| `bool` | [`_complete`](#_complete-2)  |  |
| `bool` | [`_upgrade`](#_upgrade)  |  |
| [`Error`](https://auscaster.com/icey/docs/api-reference/icy-Error.md#error) | [`_error`](#_error-5)  |  |
| [`ParseResult`](https://auscaster.com/icey/docs/api-reference/icy-http-Parser-ParseResult.md#parseresult) | [`_lastResult`](#_lastresult)  |  |
| [`MessageScratch`](https://auscaster.com/icey/docs/api-reference/icy-http-Parser-MessageScratch.md#messagescratch) | [`_scratch`](#_scratch)  |  |

---

<a id="_observer"></a>

### _observer

```cpp
ParserObserver * _observer
```

Type: [`ParserObserver`](https://auscaster.com/icey/docs/api-reference/icy-http-ParserObserver.md#parserobserver) *

[Defined in src/http/include/icy/http/parser.h:185](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L185)

---

<a id="_request-2"></a>

### _request

```cpp
http::Request * _request
```

Type: [`http::Request`](https://auscaster.com/icey/docs/api-reference/icy-http-Request.md#request-6) *

[Defined in src/http/include/icy/http/parser.h:186](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L186)

---

<a id="_response-2"></a>

### _response

```cpp
http::Response * _response
```

Type: [`http::Response`](https://auscaster.com/icey/docs/api-reference/icy-http-Response.md#response-3) *

[Defined in src/http/include/icy/http/parser.h:187](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L187)

---

<a id="_message"></a>

### _message

```cpp
http::Message * _message
```

Type: [`http::Message`](https://auscaster.com/icey/docs/api-reference/icy-http-Message.md#message-1) *

[Defined in src/http/include/icy/http/parser.h:188](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L188)

---

<a id="_parser-1"></a>

### _parser

```cpp
llhttp_t _parser
```

[Defined in src/http/include/icy/http/parser.h:190](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L190)

---

<a id="_settings"></a>

### _settings

```cpp
llhttp_settings_t _settings
```

[Defined in src/http/include/icy/http/parser.h:191](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L191)

---

<a id="_type"></a>

### _type

```cpp
llhttp_type_t _type
```

[Defined in src/http/include/icy/http/parser.h:192](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L192)

---

<a id="_washeadervalue"></a>

### _wasHeaderValue

```cpp
bool _wasHeaderValue
```

[Defined in src/http/include/icy/http/parser.h:194](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L194)

---

<a id="_lastheaderfield"></a>

### _lastHeaderField

```cpp
std::string _lastHeaderField
```

[Defined in src/http/include/icy/http/parser.h:195](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L195)

---

<a id="_lastheadervalue"></a>

### _lastHeaderValue

```cpp
std::string _lastHeaderValue
```

[Defined in src/http/include/icy/http/parser.h:196](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L196)

---

<a id="_complete-2"></a>

### _complete

```cpp
bool _complete
```

[Defined in src/http/include/icy/http/parser.h:198](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L198)

---

<a id="_upgrade"></a>

### _upgrade

```cpp
bool _upgrade
```

[Defined in src/http/include/icy/http/parser.h:199](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L199)

---

<a id="_error-5"></a>

### _error

```cpp
Error _error
```

Type: [`Error`](https://auscaster.com/icey/docs/api-reference/icy-Error.md#error)

[Defined in src/http/include/icy/http/parser.h:201](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L201)

---

<a id="_lastresult"></a>

### _lastResult

```cpp
ParseResult _lastResult
```

Type: [`ParseResult`](https://auscaster.com/icey/docs/api-reference/icy-http-Parser-ParseResult.md#parseresult)

[Defined in src/http/include/icy/http/parser.h:202](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L202)

---

<a id="_scratch"></a>

### _scratch

```cpp
MessageScratch _scratch
```

Type: [`MessageScratch`](https://auscaster.com/icey/docs/api-reference/icy-http-Parser-MessageScratch.md#messagescratch)

[Defined in src/http/include/icy/http/parser.h:203](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L203)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
| `void` | [`init`](#init-14)  |  |
| `void` | [`clearBoundMessage`](#clearboundmessage)  |  |
| `void` | [`storeHeader`](#storeheader)  |  |
| `void` | [`applyScratchToBoundMessage`](#applyscratchtoboundmessage)  |  |
| `void` | [`onHeader`](#onheader)  | Callbacks. |
| `void` | [`onHeadersEnd`](#onheadersend)  |  |
| `void` | [`onBody`](#onbody)  |  |
| `void` | [`onMessageEnd`](#onmessageend)  |  |
| `void` | [`onError`](#onerror-3)  |  |

---

<a id="init-14"></a>

### init

```cpp
void init()
```

[Defined in src/http/include/icy/http/parser.h:162](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L162)

---

<a id="clearboundmessage"></a>

### clearBoundMessage

```cpp
void clearBoundMessage()
```

[Defined in src/http/include/icy/http/parser.h:163](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L163)

---

<a id="storeheader"></a>

### storeHeader

```cpp
void storeHeader(std::string name, std::string value)
```

[Defined in src/http/include/icy/http/parser.h:164](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L164)

---

<a id="applyscratchtoboundmessage"></a>

### applyScratchToBoundMessage

```cpp
void applyScratchToBoundMessage()
```

[Defined in src/http/include/icy/http/parser.h:165](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L165)

---

<a id="onheader"></a>

### onHeader

```cpp
void onHeader(std::string name, std::string value)
```

[Defined in src/http/include/icy/http/parser.h:168](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L168)

Callbacks.

---

<a id="onheadersend"></a>

### onHeadersEnd

```cpp
void onHeadersEnd()
```

[Defined in src/http/include/icy/http/parser.h:169](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L169)

---

<a id="onbody"></a>

### onBody

```cpp
void onBody(const char * buf, size_t len)
```

[Defined in src/http/include/icy/http/parser.h:170](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L170)

---

<a id="onmessageend"></a>

### onMessageEnd

```cpp
void onMessageEnd()
```

[Defined in src/http/include/icy/http/parser.h:171](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L171)

---

<a id="onerror-3"></a>

### onError

```cpp
void onError(llhttp_errno_t errnum, const std::string & message = "")
```

[Defined in src/http/include/icy/http/parser.h:172](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/parser.h#L172)
