Skip to content

Conversation

algebraic-dev
Copy link
Member

This PR adds a simple Http library to Std.

@algebraic-dev algebraic-dev self-assigned this Sep 20, 2025
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Sep 21, 2025
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Sep 21, 2025

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 4881c3042e8d205080b19779d8dedfc672da3cd0 --onto 4379002d0582ae96d7fc6ccf5921ff4e9aa7239e. You can force Mathlib CI using the force-mathlib-ci label. (2025-09-21 22:08:49)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase eabd7309b74f13cc12d0ab03d4826f34e3e49c0d --onto 0807f73171ca8f765c11ef37d69fd95e6613a878. You can force Mathlib CI using the force-mathlib-ci label. (2025-09-24 00:27:54)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 5ef7b45afaedd794fb39b0fbd70924b573694a00 --onto ac0b82933f6eac9914011ca2caf38d0e4e991160. You can force Mathlib CI using the force-mathlib-ci label. (2025-09-25 13:54:11)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 486d93c5fd4ec0e1735554f9143a66327ca3f5a9 --onto 54c6efea95a0d478f2659632749035962b1f4d8d. You can force Mathlib CI using the force-mathlib-ci label. (2025-10-07 18:22:48)

@leanprover-bot
Copy link
Collaborator

leanprover-bot commented Sep 21, 2025

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 4881c3042e8d205080b19779d8dedfc672da3cd0 --onto d3dda9f6d4428a906c096067ecb75e432afc4615. You can force reference manual CI using the force-manual-ci label. (2025-09-21 22:08:50)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase eabd7309b74f13cc12d0ab03d4826f34e3e49c0d --onto d3dda9f6d4428a906c096067ecb75e432afc4615. You can force reference manual CI using the force-manual-ci label. (2025-09-24 00:27:56)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 5ef7b45afaedd794fb39b0fbd70924b573694a00 --onto d3dda9f6d4428a906c096067ecb75e432afc4615. You can force reference manual CI using the force-manual-ci label. (2025-09-25 13:54:14)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 486d93c5fd4ec0e1735554f9143a66327ca3f5a9 --onto d3dda9f6d4428a906c096067ecb75e432afc4615. You can force reference manual CI using the force-manual-ci label. (2025-10-07 18:22:50)

@algebraic-dev algebraic-dev changed the base branch from sofia/async-streammap to sofia/async-cancellation-token September 22, 2025 22:18
@algebraic-dev algebraic-dev force-pushed the sofia/async-cancellation-token branch from dde6cfa to 380dae6 Compare September 24, 2025 23:52
Copy link
Member

@TwoFX TwoFX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some preliminary review for the "easy" part of the PR.

/--
Builds and returns the final HTTP Request with the specified body
-/
def body (builder : Builder) (body : t) : Request t :=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this function force the user to set the Content-Type header?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's something the HTTP client can infer and add when someone tries to send the request, to prevent it from being malformed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess for Request it's kind of moot right now, but it does matter for Response.ok. Right now, Std.Http.Response.new |>.status .ok |>.text "hi" returns something different than Std.Http.Response.ok "hi" and I think that's just confusing.

/--
Creates a new HTTP POST Request with the specified URI and body
-/
def post (uri : RequestTarget) (body : t) : Request t :=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems a bit strange to me, because it bypasses the nice helper functions with set the content type for you.

Copy link
Member Author

@algebraic-dev algebraic-dev Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's kinda nice to have a helper when you don't want to use the builder. Instead of writing Request.new |>.uri (parse! "/") |>.text "something", you can just use Request.post (parse! "/") "something"

Copy link
Member

@TwoFX TwoFX Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is nice to have, but my point is that the latter will not set the Content-Type header correctly, whereas the former does.

@algebraic-dev algebraic-dev force-pushed the sofia/async-cancellation-token branch 2 times, most recently from 8c47efc to 45e2368 Compare September 25, 2025 12:26
@TwoFX
Copy link
Member

TwoFX commented Sep 25, 2025

I don't have a full test case for this, but when I create a response like

    return Std.Http.Response.new
      |>.status .ok
      |>.binary ByteArray.empty

then I think the Content-Length header should be set to 0, but it seems to be missing.

@algebraic-dev algebraic-dev requested a review from TwoFX September 26, 2025 06:57
@TwoFX
Copy link
Member

TwoFX commented Sep 26, 2025

It seems to me that there are some problems with timeouts around keep-alive.

I have this super simple server:

import Std.Internal.Http

def main : IO Unit := do
  (Std.Http.Server.serve (Std.Net.SocketAddress.v6 ⟨.ofParts 0 0 0 0 0 0 0 0, 8007⟩) (fun _ => pure <| .ok "hi")).block

Then I have this script:

#!/bin/bash

while true; do
  printf "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
  sleep 2
done

When I run the server and do ./print.sh | nc localhost 8007, I expect the server to reply with hi every two seconds until the maximum number of requests is reached, and then I guess netcat should complain that the connection is closed. Instead, the server just seems to hang after the first request. It looks like it runs into the timeout (which is 1000 milliseconds by default), and then the Connection.handle function exists and the connection just... stays open in limbo.

This also reproduces in a browser. If I open localhost:8007, wait two seconds, and press F5, then the page will load indefinitely.

This seems to highlight several problems:

  • the request timeout starts too early, before the next request is even sent
  • the timeout is handled incorrectly
  • on a more basic level: it sounds problematic that we keep-alive connections only by number of requests, not by time. If a connection is used only once, it will stay open on the server for a potentially unbounded time. This last one might be acceptable because presumably a reverse proxy will timeout the connection after a while.

@algebraic-dev
Copy link
Member Author

It's probably a problem with the Timer API, the interaction with Selectable and the reference counting. I'm not sure how to fix this right now but I have a solution that changes a lot of stuff in the timer api :S

@algebraic-dev algebraic-dev force-pushed the sofia/async-cancellation-token branch from 60aeda4 to 4ac2b87 Compare October 7, 2025 01:41
@algebraic-dev algebraic-dev changed the base branch from sofia/async-cancellation-token to master October 7, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-library Library toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants