hono icon indicating copy to clipboard operation
hono copied to clipboard

Support CONNECT method

Open ianzone opened this issue 11 months ago • 11 comments

What is the feature you are proposing?

I'm trying to build an MITM proxy using Hono, but Hono misses the CONNECT method, which the MITM proxy requires.

ianzone avatar Jan 27 '25 15:01 ianzone

You can use app.on

EdamAme-x avatar Jan 28 '25 01:01 EdamAme-x

You can use app.on

It doesn't work. Considering code

import { Hono } from 'hono';

const app = new Hono();

app.on('CONNECT', '*', async (c) => {
  console.log('HTTP CONNECT:', c.req.method, c.req.url);
});

export default {
  port: 8080,
  fetch: app.fetch,
};

Set export http_proxy=http://127.0.0.1:8080 Then try curl -v https://github.com, you will get 400 bad request, and the console doesn't log.

ianzone avatar Jan 29 '25 07:01 ianzone

Sorry, please try to rewrite to lower case

EdamAme-x avatar Jan 29 '25 07:01 EdamAme-x

Sorry, please try to rewrite to lower case

same result

ianzone avatar Jan 29 '25 07:01 ianzone

Hmm

EdamAme-x avatar Jan 29 '25 08:01 EdamAme-x

Can you intercept request? (Use app.all)

EdamAme-x avatar Jan 29 '25 08:01 EdamAme-x

Can you intercept request? (Use app.all)

That's my first attempt.

ianzone avatar Jan 29 '25 08:01 ianzone

If so, I don't think it is possible to do that with Hono. This is true for other libraries as well.

EdamAme-x avatar Jan 29 '25 08:01 EdamAme-x

That's why it's a feature request.

ianzone avatar Jan 29 '25 08:01 ianzone

Hi @ianzone

I'm not so familiar with MITM proxy, but supporting the CONNECT method is out of Hono's score. Hono is built with just Web Standard API, but it does not support the CONNECT method. This is the reason.

yusukebe avatar Jan 29 '25 08:01 yusukebe

Hey there! 👋 I ran into a similar issue and I found a workaround.

So like others have mentioned in the thread, it makes sense that Hono doesn't support CONNECT because this method is not supported by Request and fetch. It is also unavailable in cloudflare. Meaning it would deviate a lot from how other HTTP methods are handled in Hono.

Ok so now, if you are running hono with Node, here's how you can add support for CONNECT:

import type { Server } from 'node:http';
import { createAdaptorServer } from '@hono/node-server';
import { Hono } from 'hono';

const app = new Hono();
const server = createAdaptorServer(app) as Server;

server.on('connect', (req, clientSocket, head) => {
   // extract host and port from `req.url`
});

(For more info see https://nodejs.org/api/http.html#event-connect_1)

Nemikolh avatar Oct 22 '25 10:10 Nemikolh