Skip to content

feat!: Add req.hostname and req.port#4766

Merged
metcoder95 merged 15 commits intofastify:nextfrom
aarontravass:aaron/hostname
Jul 7, 2023
Merged

feat!: Add req.hostname and req.port#4766
metcoder95 merged 15 commits intofastify:nextfrom
aarontravass:aaron/hostname

Conversation

@aarontravass
Copy link
Contributor

@aarontravass aarontravass commented May 22, 2023

fixes #4682

Checklist

@aarontravass
Copy link
Contributor Author

Is there a better way to get the port?

lib/route.js Outdated
const request = new context.Request(id, params, req, query, childLogger, context)
const reply = new context.Reply(res, request, childLogger)

request.port = Number(request.host && request.host.split(':').length > 1 ? request.host.split(':')[1] : null)
Copy link
Member

Choose a reason for hiding this comment

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

This can be inferred directly from the headers when requested. This might not be really needed here 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But in case of a proxy, the port might not be there. So how can I infer the port?

Copy link
Member

Choose a reason for hiding this comment

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

Have into account that this information is already available for the Request object when it's being instantiated. For instance, if trustProxy is set, you can infer it with the host property, see how it works here

return this.raw.headers.host || this.raw.headers[':authority']
}
},
hostname: {
Copy link
Member

Choose a reason for hiding this comment

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

This is not correctly tested, can we add some tests for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean tests for the Request object or tests for the incoming request when the server is listening?

Copy link
Member

Choose a reason for hiding this comment

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

The latter 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not really sure what kind of tests I should write. I have written tests for

  1. Regular request without proxy Line 72
  2. Proxy Request Line 33

What other tests could be written? I'm kinda confused.

Copy link
Member

Choose a reason for hiding this comment

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

You change all the existing test from hostname to host.
So, there is lack of testing the hostname and port property.

Co-authored-by: Carlos Fuentes <me@metcoder.dev>
@mcollina mcollina added the semver-major Issue or PR that should land as semver major label May 23, 2023
@mcollina mcollina added this to the v5.0.0 milestone May 23, 2023
@mcollina
Copy link
Member

This must be rebased on top of the next branch.

@aarontravass aarontravass requested a review from metcoder95 May 25, 2023 18:16
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@aarontravass aarontravass requested a review from metcoder95 May 30, 2023 17:24
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@aarontravass
Copy link
Contributor Author

@mcollina @metcoder95 any update on this pr?

@metcoder95 metcoder95 requested a review from climba03003 June 21, 2023 21:38
@metcoder95
Copy link
Member

This is marked as major, @aarontravass can you point the PR to the next branch?

@aarontravass aarontravass changed the base branch from main to next June 23, 2023 15:10
@aarontravass
Copy link
Contributor Author

This is marked as major, @aarontravass can you point the PR to the next branch?

done 👍

@metcoder95
Copy link
Member

metcoder95 commented Jun 25, 2023

I think we need to rebase next with main first before merging
cc: @mcollina

@metcoder95 metcoder95 changed the title feat: Changed req.hostname to req.host and added req.hostname and req.port feat!: Add req.hostname and req.port Jul 7, 2023
@metcoder95 metcoder95 merged commit b5172f6 into fastify:next Jul 7, 2023
@github-actions
Copy link

github-actions bot commented Jul 8, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver-major Issue or PR that should land as semver major

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] Request hostname and host

4 participants