Skip to content

Would bend have built-in std libs for Http/Tls/Crypto? #846

Closed

Description

@H4ad

What Bend should do

// this is a demo from https://github.com/H4ad/bend-stdlib/blob/master/demo/api.bend

import Base
import ./vendor/json.bend as JSON
import ./vendor/http/http.bend as HTTP
import ./vendor/http/router.bend as Router
import ./vendor/http/server.bend as Server

def hello(ctx: Router.Ctx) -> IO(HTTP.Response):
  Router.text(200, "bend-stdlib http api")

def health(ctx: Router.Ctx) -> IO(HTTP.Response):
  Router.json(200, JSON.Obj{[JSON.Member{"status", JSON.Str{"ok"}}]})

def greet(ctx: Router.Ctx) -> IO(HTTP.Response):
  Router.text(200, "hello, " ++ Router.param("name", "world", ctx))

def u32_or.read(mb: Maybe<&2, U32>) -> U32:
  match mb:
    case Some{+n}:
      n
    case None{}:
      0

def u32_or(s: String) -> U32:
  u32_or.read(U32.read(s))

def sum(ctx: Router.Ctx) -> IO(HTTP.Response):
  +ps = Router.query(ctx)
  a = u32_or(Router.param.list("a", "0", ps))
  b = u32_or(Router.param.list("b", "0", ps))
  Router.json(200, JSON.Obj{[JSON.Member{"sum", JSON.Num{U32.show(U32.add(a, b))}}]})

def echo(j: JSON.Json, ctx: Router.Ctx) -> IO(HTTP.Response):
  Router.json(200, j)

def powered(resp: HTTP.Response) -> HTTP.Response:
  Router.header("X-Powered-By", "bend-stdlib", resp)

def routes(ctx: Router.Ctx) -> IO(HTTP.Response):
  Router.dispatch([
    Router.GET("/", hello),
    Router.GET("/health", health),
    Router.GET("/hello/:name", greet),
    Router.GET("/sum", sum),
    Router.POST_JSON("/echo", echo)
  ], ctx)

def app(ctx: Router.Ctx) -> IO(HTTP.Response):
  Router.map(~powered, routes)(ctx)

def main() -> IO(Unit):
  Server.listen(~app, 8080)

Why

I'm doing a experiment to create a HTTP Framework for bend, and I'm missing few features in the standard lib:

patch effect it adds used by why Bend cannot
tcp: add recv_bytes and send_bytes effects TCP.recv_bytes, TCP.send_bytes http, pg TCP.recv and TCP.send speak String, a sequence of code points. A byte over 127 or an invalid UTF-8 sequence does not survive the round trip, and a read that splits a multi-byte character corrupts it. HTTP framing is byte counts and the pg wire is byte frames.
tcp: resolve host names in TCP.connect TCP.connect by name http, pg connects to a numeric quad only; a host name fails with EINVAL.
crypto: add random_bytes effect Crypto.random_bytes crypto, pg has one IO.random_u32; SCRAM needs N bytes of entropy.
crypto: add native pbkdf2 effect Crypto.pbkdf2.native crypto, pg has no KDF. This is OpenSSL PKCS5_PBKDF2_HMAC, the SCRAM key derivation. The patch also links -lssl -lcrypto for any program whose C includes OpenSSL.
tls: add OpenSSL effects TLS.wrap, TLS.accept, TLS.self_signed, TLS.send, TLS.recv, TLS.close tls, pg Bend has no TLS surface. Its own tests/io/tls_connect_close.bend pins that TLS.connect is undefined.

You can find these commits/patches at https://github.com/h4ad-forks/bend/commits/main/

Out of the current patches, the most blocking ones are TCP not accepting/working with bytes and no way to use OpenSSL in C code to be able to have TLS/Crypto

I wonder if this is something you want to bring to the language or you explicitly want to keep out of the language because it's not what it was suppose to do.

Activity

  1. added
    enhancement
    on Sep 19, 2026
  2. added 3 commits that reference this issue on Sep 20, 2026
    55327cc
    07fa70c
    d325edc
  3. victormeloasm commented on Sep 21, 2026

    @victormeloasm
    Contributor

    I am a mathematician and a cryptoanalyst, I could maintain this. I built an entire hash lib to Bend2, but it was refused...

    https://github.com/victormeloasm/bend-hashes

    I would port OpenSSL lite(Crypto) if it was accepted.

  4. VictorTaelin commented on Sep 27, 2026

    @VictorTaelin
    Contributor

    Fixed in Bend 2.0.32 (573002f): TCP.send_bytes and TCP.recv_bytes carry bytes as they are. DNS, TLS, crypto and HTTP stay out of Base (see WONTFIX): build them as hub packages over TCP bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions