build-a-url-parser / lesson-03.md
Lesson 03 · The five components

Splitting off the authority

After the scheme, a URI may carry an authority - the host part, introduced by a double slash. Today you recognize that // marker and slice the authority out, distinguishing it from a plain absolute path that starts with a single slash.

The goal

Recognize the // marker and split the authority from the path that follows it.

Start here - the target
TO DO
Scenario: Detecting the authority
Giventhe input "//example.com/over/there"
WhenParse is called
ThenHasAuthority is true, Authority is "example.com", and Path is "/over/there"
AndParse("/over/there") has HasAuthority false and Path "/over/there", because a single leading slash is a path, not an authority
Background

The authority is the part that names who or where - typically a host, sometimes with a user and a port. RFC 3986 introduces it with a literal double slash // right after the scheme’s colon (or at the very start, for a scheme-relative reference). Everything from just after the // up to the next /, ?, or # is the authority; that following slash, if present, begins the path. If there is no such delimiter, the authority is the entire remainder and the path is empty.

The single-slash case is the trap. //example.com/p has an authority, but /p is just an absolute path with no authority at all - the double slash is what makes the difference. That distinction is real and load-bearing: later, resolving a reference like //g against a base means “keep the scheme but replace the authority”, which only works because you can tell an authority-bearing reference from a path-only one. Store the raw authority string for now; breaking it into userinfo, host, and port is the next chapter’s work.

Make it work
// after the scheme, if what remains begins with "//",
// the authority runs up to the next '/', '?', or '#'
if strings.HasPrefix(rest, "//") {
rest = rest[2:]
j := strings.IndexAny(rest, "/?#")
// j < 0 means the authority is the whole remainder
u.HasAuthority = true
}
CheckpointDONE
Parse now extracts the authority when the URI has one. Commit and stop here.