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

fix: handle includes without hosts #6

Closed
wants to merge 1 commit into from

Conversation

dkarlovi
Copy link
Contributor

Fixes #4.

@@ -52,6 +53,12 @@ func (i *includeTag) Process(b []byte, req *http.Request) ([]byte, int) {
return nil, len(b)
}

if strings.HasPrefix(i.src, "//") {
i.src = req.URL.Scheme + i.src
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks like it should work, but for some reason req seems to be empty when calling this.

@dkarlovi dkarlovi mentioned this pull request Nov 11, 2022
@@ -52,6 +53,12 @@ func (i *includeTag) Process(b []byte, req *http.Request) ([]byte, int) {
return nil, len(b)
}

if strings.HasPrefix(i.src, "//") {
Copy link

Choose a reason for hiding this comment

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

You should probably use URL.ResolveReference() instead

Copy link
Owner

Choose a reason for hiding this comment

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

That doesn't work.

Copy link
Owner

Choose a reason for hiding this comment

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

Because in the caddy's context, req.URL.Host is empty and only req.Host is filled.

Copy link

@dunglas dunglas Nov 11, 2022

Choose a reason for hiding this comment

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

I mean you can build the base URL instance by yourself, but relying on this method will be safer as it will support all edge cases

This is probably the kind of code you don't want to maintain yourself: https://cs.opensource.google/go/go/+/refs/tags/go1.19.3:src/net/url/url.go;drc=a11cd6f69aec5c783656601fbc7b493e0d63f605;l=995

@darkweak
Copy link
Owner

Got something working @dkarlovi

// writer.go
// ...

func NewWriter(buf *bytes.Buffer, rw http.ResponseWriter, rq *http.Request) *Writer {
	if rq.URL.Scheme == "" {
		if rq.TLS != nil {
			rq.URL.Scheme = "https"
		} else {
			rq.URL.Scheme = "http"
		}
	}

	if rq.URL.Host == "" {
		rq.URL.Host = rq.Host
	}

	return &Writer{
		buf:      buf,
		Rq:       rq,
		rw:       rw,
		AsyncBuf: make([]chan []byte, 0),
		Done:     make(chan bool),
	}
}

// ...
// include.go

// ...
	if e := i.loadAttributes(b[8:i.length]); e != nil {
		return nil, len(b)
	}

	u, _ := url.Parse(i.src)
	i.src = req.URL.ResolveReference(u).String()
// ...

@dkarlovi
Copy link
Contributor Author

Feel free to close this if you've solved it differently. 👍

@darkweak
Copy link
Owner

Resolved by #7

@darkweak darkweak closed this Nov 12, 2022
@dkarlovi dkarlovi deleted the fix/hostless-include branch November 18, 2022 17:28
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.

Support for hostless URLs
3 participants