Skip to content

Add ConvertNetHttpRequestToFastHttpRequest adaptor function#2104

Open
aaydin-tr wants to merge 7 commits intovalyala:masterfrom
aaydin-tr:feature/convert-net-http-request-to-fasthttp-request
Open

Add ConvertNetHttpRequestToFastHttpRequest adaptor function#2104
aaydin-tr wants to merge 7 commits intovalyala:masterfrom
aaydin-tr:feature/convert-net-http-request-to-fasthttp-request

Conversation

@aaydin-tr
Copy link
Copy Markdown

One of my personal project i need to convert net http request to fasthttp request instead of doing it directly in my own project i want to add this functionality to fasthttp for possible future uses and i also looking for a review if its correct way to convert both request object.

@aaydin-tr aaydin-tr requested a review from gaby December 7, 2025 15:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new ConvertNetHttpRequestToFastHttpRequest function to enable conversion from net/http.Request to fasthttp.RequestCtx. This provides the inverse operation of the existing ConvertRequest function, allowing users to adapt standard Go HTTP requests for use with fasthttp.

Key Changes:

  • Added new conversion function ConvertNetHttpRequestToFastHttpRequest that handles method, URI, protocol, host, headers, body, and remote address conversion
  • Implemented helper functions parseRemoteAddr and parsePort to handle remote address parsing
  • Added comprehensive test suite covering basic conversion, header handling, body streaming, and remote address formats

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.

File Description
fasthttpadaptor/request.go Implements the core conversion function and remote address parsing helpers
fasthttpadaptor/request_test.go Adds benchmark and comprehensive unit tests for various conversion scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@erikdubbelboer
Copy link
Copy Markdown
Collaborator

I'm sorry I don't have the time to look into this closely. On an initial glance the function seems kinda small, are you sure it supports all fields and cases (such as the various way a fasthttp request can have a body)?

@aaydin-tr
Copy link
Copy Markdown
Author

I'm sorry I don't have the time to look into this closely. On an initial glance the function seems kinda small, are you sure it supports all fields and cases (such as the various way a fasthttp request can have a body)?

if r.Body != nil {
	ctx.Request.SetBodyStream(r.Body, int(r.ContentLength))
}

this is how i pass net/http request body to fasthttp request body. indeed fasthttp request has various ways to set the body, but as far as i see, ctx.Request.Body() and all other functions to get the request body fall through to the req.bodyBytes() function, and it reads req.bodyStream if it’s not nil. based on that, i guess ctx.Request.SetBodyStream(r.Body, int(r.ContentLength)) will handle all cases. but i am not experienced with fasthttp, so if you have an edge case in mind, i can try to convert that too.

@gaby
Copy link
Copy Markdown
Contributor

gaby commented Dec 28, 2025

@erikdubbelboer Can you trigger copilot to also check this?

@aaydin-tr aaydin-tr requested a review from gaby December 28, 2025 14:15
@gaby
Copy link
Copy Markdown
Contributor

gaby commented Dec 28, 2025

I'm sorry I don't have the time to look into this closely. On an initial glance the function seems kinda small, are you sure it supports all fields and cases (such as the various way a fasthttp request can have a body)?

if r.Body != nil {
	ctx.Request.SetBodyStream(r.Body, int(r.ContentLength))
}

this is how i pass net/http request body to fasthttp request body. indeed fasthttp request has various ways to set the body, but as far as i see, ctx.Request.Body() and all other functions to get the request body fall through to the req.bodyBytes() function, and it reads req.bodyStream if it’s not nil. based on that, i guess ctx.Request.SetBodyStream(r.Body, int(r.ContentLength)) will handle all cases. but i am not experienced with fasthttp, so if you have an edge case in mind, i can try to convert that too.

Body Streams larger than 4GB will fail with this, since those need an uint64 instead of int.

@aaydin-tr
Copy link
Copy Markdown
Author

I'm sorry I don't have the time to look into this closely. On an initial glance the function seems kinda small, are you sure it supports all fields and cases (such as the various way a fasthttp request can have a body)?

if r.Body != nil {
	ctx.Request.SetBodyStream(r.Body, int(r.ContentLength))
}

this is how i pass net/http request body to fasthttp request body. indeed fasthttp request has various ways to set the body, but as far as i see, ctx.Request.Body() and all other functions to get the request body fall through to the req.bodyBytes() function, and it reads req.bodyStream if it’s not nil. based on that, i guess ctx.Request.SetBodyStream(r.Body, int(r.ContentLength)) will handle all cases. but i am not experienced with fasthttp, so if you have an edge case in mind, i can try to convert that too.

Body Streams larger than 4GB will fail with this, since those need an uint64 instead of int.

	if r.Body != nil {
		contentLength := int(r.ContentLength)
		if r.ContentLength >= int64(math.MaxInt) {
			contentLength = -1
		}

		ctx.Request.SetBodyStream(r.Body, contentLength)
	}

i make this change if r.ContentLength is larger than math.MaxInt contentLength is set -1 which means unknown and body stream will read until its see EOF according to docs

@gaby
Copy link
Copy Markdown
Contributor

gaby commented Dec 28, 2025

@copilot review

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 11 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@aaydin-tr
Copy link
Copy Markdown
Author

@erikdubbelboer is there any update for this pr

}

func parseRemoteAddr(addr string) net.Addr {
if tcpAddr, err := net.ResolveTCPAddr("tcp", addr); err == nil {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is no timeout on this and it could potentially hang. We have this code here, I'm not sure if it should somehow be reused since it's not exposed:

func resolveTCPAddrs(addr string, dualStack bool, resolver Resolver, deadline time.Time) ([]net.TCPAddr, error) {

//
// After calling this function, you should treat r.Body as effectively owned by
// ctx.Request for the lifetime of that context.
func ConvertNetHTTPRequestToFastHTTPRequest(r *http.Request, ctx *fasthttp.RequestCtx) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The function doesn't reset ctx.Request. This should be documented that users need to do this themselves if needed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also why have a fasthttp.RequestCtx as argument? From the function name I would expect a fasthttp.Request as argument, which makes much more sense and makes it much more usable.

}

ctx.Request.Header.SetProtocol(r.Proto)
ctx.Request.SetHost(r.Host)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

r.Host can be empty in which case it should be r.URL.Host.

addr := parseRemoteAddr(r.RemoteAddr)
ctx.SetRemoteAddr(addr)
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You are missing some properties that aren't being copied right now. For example r.TLS and r.Close, r.TransferEncoding, r.Trailer, and r.URL.Scheme which are all used by net/http.

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.

4 participants