Skip to content

Implement onConnect #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2021
Merged

Conversation

jmatsushita
Copy link
Contributor

Description of the change

This PR allows to use the "connect" event which can be useful to implement an http proxy server. Following the example from the nodeJS documentation https://nodejs.org/api/http.html#http_event_connect

respond :: Request -> Response -> Effect Unit
respond req res = do
  setStatusCode res 200
  setHeader res "Content-Type" "text/plain"

main :: Effect Unit
main = do
  proxy <- createServer respond
  listen proxy { hostname: "localhost", port: 3030, backlog: Nothing} $ void do
    log "Listening on port 3030"
    onConnect proxy $ \req clientSocket head -> void do
      log (requestMethod req <> " " <> requestURL req)
      logShow $ requestHeaders req
      let url = parse $ "http://" <> requestURL req
      logShow url 
      case (toMaybe url.port), (toMaybe url.hostname) of
        Just stringPort, Just host -> case fromString stringPort of
          Just port -> do
            serverSocket <- Socket.createConnectionTCP port host do
                infoShow $ "CONNECT " <> host

            Socket.onConnect serverSocket do
              -- infoShow { _message: "Socket connected" }
              void $ Socket.writeString clientSocket 
                ("HTTP/1.1 200 Connection Established\r\n" <>
                "Proxy-agent: Node.js-Proxy\r\n" <>
                "\r\n") UTF8 $ pure unit
              void $ Socket.write serverSocket head $ pure unit
              void $ Socket.onData serverSocket $ case _ of
                Left buffer -> void $ Socket.write clientSocket buffer $ pure unit
                Right string -> void $ Socket.writeString clientSocket string UTF8 $ pure unit
              void $ Socket.onData clientSocket $ case _ of
                Left buffer -> void $ Socket.write serverSocket buffer $ pure unit
                Right string -> void $ Socket.writeString serverSocket string UTF8 $ pure unit
            Socket.onError serverSocket \err -> do
              infoShow { _message: "Server had an error", err }
            Socket.onError clientSocket \err -> do
              infoShow { _message: "Client had an error", err }
          _ -> log "port is not a string"
        _,_ -> log "port or hosts error"

No tests yet!


Checklist:

  • Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)

@JordanMartinez
Copy link
Contributor

Not sure what the CI error was, but I've restarted it.

Code looks fine otherwise.

@thomashoneyman
Copy link
Contributor

thomashoneyman commented Sep 22, 2021

The test failure is just that the tests don't terminate (which we should fix!). See also:

@JordanMartinez JordanMartinez merged commit 76513c9 into purescript-node:master Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants