-
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactoring previous fix in attempt to address ongoing issues
- Loading branch information
Showing
3 changed files
with
74 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lib | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type request struct { | ||
method string | ||
path string | ||
destination string | ||
} | ||
|
||
func newRequest(r *http.Request, prefix string) (*request, error) { | ||
ctx := &request{ | ||
method: r.Method, | ||
} | ||
|
||
if destination := r.Header.Get("Destination"); destination != "" { | ||
u, err := url.Parse(destination) | ||
if err != nil { | ||
return nil, errors.New("invalid destination header") | ||
} | ||
|
||
if prefix != "" { | ||
destination = strings.TrimPrefix(u.Path, prefix) | ||
if len(destination) >= len(u.Path) { | ||
return nil, errors.New("invalid url prefix") | ||
} | ||
} | ||
|
||
if !strings.HasPrefix(destination, "/") { | ||
destination = "/" + destination | ||
} | ||
|
||
ctx.destination = destination | ||
} | ||
|
||
path := r.URL.Path | ||
|
||
if prefix != "" { | ||
path = strings.TrimPrefix(r.URL.Path, prefix) | ||
if len(path) >= len(r.URL.Path) { | ||
return nil, errors.New("invalid url prefix") | ||
} | ||
} | ||
|
||
if !strings.HasPrefix(path, "/") { | ||
path = "/" + path | ||
} | ||
|
||
ctx.path = path | ||
|
||
return ctx, nil | ||
} |