Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-http-client

Source of release 0.5.1

0.5.1 / src / client.lucb

## Cleartext HTTP/1.1 client. Numeric IPv4 peers only. No DNS, TLS or redirects.
import io
import memory
from luce_std import net
import framing
import sync

pub let invalid = net.http_invalid
pub let limit_exceeded = net.http_limit
pub let max_response: usize = 67108864

pub struct Result:
    pub var status: u16
    pub var count: usize

func put_field(fields: net.HttpField[], used: usize*, name: str, value: str) -> !:
    if *used >= fields.length: error(invalid, "too many HTTP request fields")
    fields[*used] = net.HttpField(name = name, value = value)
    *used += 1

pub func request(address: net.SocketAddress, method: str, target: str, host: str,
    extra: const net.HttpField[], extra_count: usize, body: const u8[], output: u8[],
    timeout_ms: u64 = 30000, cancellation: sync.Cancellation*? = none) -> Result!:
    if extra_count > extra.length: error(invalid, "HTTP extra field count")
    if output.length > max_response: error(limit_exceeded, "HTTP response buffer exceeds 64 MiB")
    var fields: net.HttpField[16]
    var n: usize
    try put_field(fields, &n, "Host", host)
    for i in 0..<extra_count:
        try put_field(fields, &n, extra[i].name, extra[i].value)
    var head = net.HttpHead(method = method, target = target, version = "HTTP/1.1",
        fields = fields[..<n], keep_alive = false)
    if body.length > 0:
        head.body_kind = net.HttpBodyKind.fixed
        head.body_length = (u64)body.length
    var wire: u8[4096]
    let encoded = try net.http_encode_head(wire, head)
    let duration = timeout_ms *? 1000000 else error(net.invalid_options, "HTTP timeout overflow")
    let deadline = try net.Deadline.after(duration)
    var conn = try net.Connection.connect(address, deadline = deadline, cancellation = cancellation, nonblocking = true)
    defer _ = conn.close() catch: recover ()
    var stream = try net.DeadlineStream.over(&conn, deadline, cancellation)
    try io.write_all(&stream, encoded)
    if body.length > 0: try io.write_all(&stream, body)
    try conn.shutdown(net.ShutdownDirection.write)
    var incoming: u8[16384]
    var response = try framing.Decoder.create()
    while not response.finished():
        let got = try stream.read(incoming)
        if got == 0: break
        try response.consume(incoming[..<got], output, method)
    try response.finish()
    return Result(status = response.status, count = response.count)

pub func get(port: u16, target: str, output: u8[], timeout_ms: u64 = 30000, cancellation: sync.Cancellation*? = none) -> Result!:
    var unused: net.HttpField[1]
    return try request(net.SocketAddress.loopback(port), "GET", target, "127.0.0.1", unused, 0, b"", output, timeout_ms, cancellation)

pub func post(port: u16, target: str, body: const u8[], output: u8[], timeout_ms: u64 = 30000, cancellation: sync.Cancellation*? = none) -> Result!:
    var unused: net.HttpField[1]
    return try request(net.SocketAddress.loopback(port), "POST", target, "127.0.0.1", unused, 0, body, output, timeout_ms, cancellation)