Skip to content
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

Add host header #185

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
P_UNIX = 400
P_P2P = 421
P_IPFS = P_P2P // alias for backwards compatibility
P_HOST_HEADER = 481
P_HTTP = 480
P_HTTPS = 443 // deprecated alias for /tls/http
P_ONION = 444 // also for backwards compatibility
Expand Down Expand Up @@ -194,6 +195,13 @@ var (
Size: LengthPrefixedVarSize,
Transcoder: TranscoderCertHash,
}
protoHOSTHEADER = Protocol{
Code: P_HOST_HEADER,
Size: LengthPrefixedVarSize,
Name: "host-header",
VCode: CodeToVarint(P_HOST_HEADER),
Transcoder: TranscoderDns,
}
protoHTTP = Protocol{
Name: "http",
Code: P_HTTP,
Expand Down
26 changes: 26 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,29 @@ func certHashStB(s string) ([]byte, error) {
func certHashBtS(b []byte) (string, error) {
return multibase.Encode(multibase.Base64url, b)
}

var TranscoderHostHeader = NewTranscoderFromFunctions(hostHeaderStB, hostHeaderBtS, hostHeaderVal)

func hostHeaderVal(b []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func hostHeaderVal(b []byte) error {
func hostHeaderValidate(b []byte) error {

Copy link
Author

Choose a reason for hiding this comment

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

validation function for other protocols are all labeled <prococol>Val(. I feel like using <protocol>Validate is inconsistent.

s := string(b)
host, port, err := net.SplitHostPort(s)
if err != nil {
return fmt.Errorf("host %q is invalid %w", s, err)
}
if err := dnsVal([]byte(host)); err != nil {
return err
}
p, err := strconv.Atoi(port)
if err != nil || p < 0 || p > 65535 {
return fmt.Errorf("port %q is not valid", port)
}
return nil
}

func hostHeaderStB(s string) ([]byte, error) {
return []byte(s), nil
}

func hostHeaderBtS(b []byte) (string, error) {
return string(b), nil
}