# MiniHTTP Example

## Overview

This example demonstrates how to use the MiniHTTP library for HTTP and HTTPS communication on K230. MiniHTTP is a lightweight HTTP client library that supports HTTP/HTTPS, SSL/TLS encryption, and file download.

## Functional Description

### MiniHTTP Capabilities

This example demonstrates the main features of MiniHTTP:

- **HTTP/HTTPS support**
- **SSL/TLS encryption**
- **simple APIs**
- **event-driven operation**
- **flexible configuration** for buffer size, timeout, and keep-alive behavior

### Sample Types

This example contains two sub-samples.

#### `minihttp_basic` - Basic Example

The simplest file-download example:

- one-shot download through a simple API
- automatic handling of HTTP connection and data reception
- automatic memory allocation and release

#### `minihttp_advanced` - Advanced Example

An event-driven HTTP client example:

- event callbacks for HTTP responses
- connection lifecycle management
- SSL certificate validation
- receive-data handling
- network-error and timeout handling

### Main APIs

#### Basic API

```cpp
char* minihttp::Download(const char* url);
```

#### Advanced APIs

```cpp
void minihttp::InitNetwork();
void minihttp::StopNetwork();
bool minihttp::HasSSL();

class HttpSocket {
public:
    virtual bool Download(const char* url);
    virtual void SetKeepAlive(int n);
    virtual void SetBufsizeIn(unsigned int size);

protected:
    virtual void _OnOpen();
    virtual void _OnClose();
    virtual void _OnRequestDone();
    virtual void _OnRecv(void* buf, unsigned int size);
};
```

## Source Location

```bash
src/rtsmart/examples/3rd-party/minihttp
```

## Usage

### Build Method

#### Firmware Build

From the `K230 RTOS SDK` root, use `make menuconfig` to enable the MiniHTTP example in the firmware, then build the firmware.

#### Standalone Build

Build the basic sample with CMake:

```bash
cd src/rtsmart/examples/3rd-party/minihttp/minihttp_basic
mkdir build && cd build
cmake ..
make
```

Build the advanced sample in the same way.

### Run Example

#### Basic Example

Copy the executable to the board, enter the target directory, then run:

```bash
./test_minihttp
```

### Expected Result

#### Basic Example

The program downloads the content of the target URL and prints it to the console:

```text
<!DOCTYPE html>
<html>
<head>
    <title>Example target page title</title>
...
```

#### Advanced Example

Copy the executable to the board, then run:

```bash
./minihttp_adv <url>
```

Parameter description:

| Parameter | Description |
| --- | --- |
| `url` | URL to download, supports both HTTP and HTTPS |

Example:

```bash
./minihttp_adv https://www.baidu.com
```

Representative output:

```text
minihttp have ssl support: YES
Connecting to: https://www.baidu.com
[Event] _OnOpen()
SSL status flags (0 is good): 0x0
...
[Event] _OnRequestDone(): /
[Event] _OnClose()
Test finished.
```

### SSL Notes

When using HTTPS:

1. the system time must be correct for certificate validation
1. SSL certificates are validated by default
1. an SSL error code of `0x1` often indicates incorrect system time

```{admonition} Tip
MiniHTTP supports HTTP Keep-Alive. You can use `SetKeepAlive()` to configure the keep-alive count and improve performance. For more MiniHTTP details, refer to the related API documentation.
```

```{admonition} Tip
When using HTTPS, make sure the system time is configured correctly; otherwise, SSL certificate validation may fail. You can use the `date` command to set the system time.
```
