Add HTTP bridge so the CoAP server can be checked in a browser
Browsers speak HTTP over TCP and cannot open coap:// URLs, which made the server awkward to try without the CLI client. The bridge accepts an ordinary HTTP request, reissues it as CoAP over UDP, and maps the response code back onto an HTTP status (the raw code is also returned in X-Coap-Code). It serves a test console at / with a name field and a button per route, so GET, POST, 4.04 and 4.05 can all be exercised by clicking. The CoAP request logic moves from src/bin/client.rs into src/lib.rs so the client and the bridge share one implementation. The bridge is a development convenience: a small HTTP/1.1 subset with no keep-alive, chunked bodies or TLS. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
51
README.md
51
README.md
@@ -32,7 +32,44 @@ Listens on `udp://0.0.0.0:5683` (CoAP's IANA-assigned port).
|
||||
(`{"name":"Imran"}`). An empty payload returns `4.00`. An unknown path returns
|
||||
`4.04`; a known path with the wrong method returns `4.05`.
|
||||
|
||||
## Try it
|
||||
## Check it in a browser
|
||||
|
||||
Browsers speak HTTP over TCP; this server speaks CoAP over UDP, so `coap://`
|
||||
URLs cannot be opened directly. `src/bin/bridge.rs` is a small HTTP -> CoAP
|
||||
proxy that closes the gap. Run both:
|
||||
|
||||
```bash
|
||||
cargo run --release --bin coap-hello-udp
|
||||
```
|
||||
|
||||
```bash
|
||||
cargo run --release --bin bridge
|
||||
```
|
||||
|
||||
Then open <http://localhost:8080/> for a test console with a name field and a
|
||||
button per route. Every response shows both the CoAP code and the HTTP status
|
||||
it was mapped to; the raw CoAP code is also returned in an `X-Coap-Code` header.
|
||||
|
||||
These are now ordinary URLs — clickable, curl-able, bookmarkable:
|
||||
|
||||
| URL | Response |
|
||||
|--------------------------------------------------|------------------------------|
|
||||
| `http://localhost:8080/` | the test console |
|
||||
| `http://localhost:8080/hello` | `Hello, World!` |
|
||||
| `http://localhost:8080/hello?name=Imran` | `Hello, Imran!` |
|
||||
| `http://localhost:8080/health` | `{"status":"ok","uptime":12}`|
|
||||
|
||||
POST needs a body, so it is easiest from the console page or curl:
|
||||
|
||||
```bash
|
||||
curl -X POST -d "Imran" http://localhost:8080/hello
|
||||
```
|
||||
|
||||
The bridge is a development convenience: a deliberately small HTTP/1.1 subset
|
||||
with no keep-alive, no chunked bodies and no TLS. Do not expose it. Set
|
||||
`HTTP_PORT` if 8080 is taken.
|
||||
|
||||
## Try it from the CLI
|
||||
|
||||
With the bundled client:
|
||||
|
||||
@@ -64,15 +101,19 @@ decoding.
|
||||
| Path | What it is |
|
||||
|---------------------|---------------------------------------------------------|
|
||||
| `src/main.rs` | The server: socket, read loop, route table, helpers. |
|
||||
| `src/lib.rs` | Shared CoAP client used by the CLI and the bridge. |
|
||||
| `src/bin/client.rs` | CLI client for smoke tests. |
|
||||
| `src/bin/bridge.rs` | HTTP -> CoAP proxy so a browser can reach the server. |
|
||||
| `static/index.html` | The browser test console the bridge serves at `/`. |
|
||||
| `nodejs/` | The earlier Node.js version, kept for reference. |
|
||||
|
||||
## Config
|
||||
|
||||
| Env var | Default |
|
||||
|-------------|-----------|
|
||||
| `COAP_PORT` | `5683` |
|
||||
| `COAP_HOST` | `0.0.0.0` |
|
||||
| Env var | Default | Used by |
|
||||
|-------------|-----------|----------------|
|
||||
| `COAP_PORT` | `5683` | server, client, bridge |
|
||||
| `COAP_HOST` | `0.0.0.0` | server, client, bridge |
|
||||
| `HTTP_PORT` | `8080` | bridge |
|
||||
|
||||
## Design notes
|
||||
|
||||
|
||||
Reference in New Issue
Block a user