Skip to content
Open
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
3 changes: 2 additions & 1 deletion cmd/goatcounter/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ List of format specifiers:
status Status code sent to the client.
http HTTP request protocol (i.e. HTTP/1.1).
path Path (e.g. "/mypage.html"). May contain the query string.
host Server name of the server serving the request.
url Full URL (e.g. "https://example.com/mypage.html"). Takes
precedence over $host and $path.
query Query string; only needed if not included in $path or $url.
Expand All @@ -171,7 +172,6 @@ List of format specifiers:

Some format specifiers that are not (yet) used anywhere:

host Server name of the server serving the request.
content_type Content-Type header of the response.
timing_sec Time to serve the request in seconds, with possible decimal.
timing_milli Time to serve the request in milliseconds.
Expand Down Expand Up @@ -388,6 +388,7 @@ func importLog(
Ref: line.Referrer(),
Query: line.Query(),
UserAgent: line.UserAgent(),
Host: line.Host(),
}

hit.CreatedAt, err = scan.Datetime(line)
Expand Down
4 changes: 4 additions & 0 deletions cron/hit_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestHitStats(t *testing.T) {
"count": 1,
"path_id": 1,
"path": "/asd",
"host": "",
"event": false,
"title": "aSd",
"max": 1,
Expand All @@ -73,6 +74,7 @@ func TestHitStats(t *testing.T) {
"count": 2,
"path_id": 1,
"path": "/asd",
"host": "",
"event": false,
"title": "aSd",
"max": 1,
Expand Down Expand Up @@ -133,6 +135,7 @@ func TestHitStatsNoCollect(t *testing.T) {
"count": 2,
"path_id": 1,
"path": "/asd",
"host": "",
"event": false,
"title": "aSd",
"max": 2,
Expand All @@ -145,6 +148,7 @@ func TestHitStatsNoCollect(t *testing.T) {
"count": 1,
"path_id": 2,
"path": "/zxc",
"host": "",
"event": false,
"title": "",
"max": 1,
Expand Down
1 change: 1 addition & 0 deletions db/migrate/2025-08-08-01-paths-host-postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table paths add column if not exists host varchar not null default '';
1 change: 1 addition & 0 deletions db/migrate/2025-08-08-01-paths-host-sqlite.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table paths add column host varchar not null default '';
2 changes: 1 addition & 1 deletion db/query/hit_list.List-counts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ with x as (
order by total desc, path_id desc
limit :limit
)
select path_id, paths.path, paths.title, paths.event from x
select path_id, paths.path, paths.title, paths.host, paths.event from x
join paths using (path_id)
order by total desc, path_id desc
3 changes: 2 additions & 1 deletion handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ type APICountRequestHit struct {
Session string `json:"session"`

// {omitdoc}
Host string `json:"-"`
Host string `json:"host" query:"h"`

// {omitdoc} Line when importing, for displaying errors.
Line string `json:"-"`
Expand Down Expand Up @@ -603,6 +603,7 @@ func (h api) count(w http.ResponseWriter, r *http.Request) error {
UserAgentHeader: a.UserAgent,
Location: a.Location,
RemoteAddr: a.IP,
Host: a.Host,
}

if a.UserAgent != "" {
Expand Down
68 changes: 37 additions & 31 deletions handlers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func TestAPIPaths(t *testing.T) {
}, "", 200, `{
"more": false,
"paths": [
{"event": false, "id": 1, "path": "/a", "title": "Hello"}
{"event": false, "id": 1, "path": "/a", "host": "", "title": "Hello"}
]}`,
},

Expand All @@ -669,26 +669,26 @@ func TestAPIPaths(t *testing.T) {
}, "", 200, `{
"more": true,
"paths": [
{"event": false, "id": 1, "path": "/1", "title": "1 - 1"},
{"event": false, "id": 2, "path": "/2", "title": "2 - 2"},
{"event": false, "id": 3, "path": "/3", "title": "3 - 3"},
{"event": false, "id": 4, "path": "/4", "title": "4 - 4"},
{"event": false, "id": 5, "path": "/5", "title": "5 - 5"},
{"event": false, "id": 6, "path": "/6", "title": "6 - 6"},
{"event": false, "id": 7, "path": "/7", "title": "7 - 7"},
{"event": false, "id": 8, "path": "/8", "title": "8 - 8"},
{"event": false, "id": 9, "path": "/9", "title": "9 - 9"},
{"event": false, "id": 10, "path": "/10", "title": "10 - 10"},
{"event": false, "id": 11, "path": "/11", "title": "11 - 11"},
{"event": false, "id": 12, "path": "/12", "title": "12 - 12"},
{"event": false, "id": 13, "path": "/13", "title": "13 - 13"},
{"event": false, "id": 14, "path": "/14", "title": "14 - 14"},
{"event": false, "id": 15, "path": "/15", "title": "15 - 15"},
{"event": false, "id": 16, "path": "/16", "title": "16 - 16"},
{"event": false, "id": 17, "path": "/17", "title": "17 - 17"},
{"event": false, "id": 18, "path": "/18", "title": "18 - 18"},
{"event": false, "id": 19, "path": "/19", "title": "19 - 19"},
{"event": false, "id": 20, "path": "/20", "title": "20 - 20"}
{"event": false, "id": 1, "path": "/1", "host": "", "title": "1 - 1"},
{"event": false, "id": 2, "path": "/2", "host": "", "title": "2 - 2"},
{"event": false, "id": 3, "path": "/3", "host": "", "title": "3 - 3"},
{"event": false, "id": 4, "path": "/4", "host": "", "title": "4 - 4"},
{"event": false, "id": 5, "path": "/5", "host": "", "title": "5 - 5"},
{"event": false, "id": 6, "path": "/6", "host": "", "title": "6 - 6"},
{"event": false, "id": 7, "path": "/7", "host": "", "title": "7 - 7"},
{"event": false, "id": 8, "path": "/8", "host": "", "title": "8 - 8"},
{"event": false, "id": 9, "path": "/9", "host": "", "title": "9 - 9"},
{"event": false, "id": 10, "path": "/10", "host": "", "title": "10 - 10"},
{"event": false, "id": 11, "path": "/11", "host": "", "title": "11 - 11"},
{"event": false, "id": 12, "path": "/12", "host": "", "title": "12 - 12"},
{"event": false, "id": 13, "path": "/13", "host": "", "title": "13 - 13"},
{"event": false, "id": 14, "path": "/14", "host": "", "title": "14 - 14"},
{"event": false, "id": 15, "path": "/15", "host": "", "title": "15 - 15"},
{"event": false, "id": 16, "path": "/16", "host": "", "title": "16 - 16"},
{"event": false, "id": 17, "path": "/17", "host": "", "title": "17 - 17"},
{"event": false, "id": 18, "path": "/18", "host": "", "title": "18 - 18"},
{"event": false, "id": 19, "path": "/19", "host": "", "title": "19 - 19"},
{"event": false, "id": 20, "path": "/20", "host": "", "title": "20 - 20"}
]}`,
},

Expand All @@ -698,11 +698,11 @@ func TestAPIPaths(t *testing.T) {
}, "after=19&limit=5", 200, `{
"more": true,
"paths": [
{"event": false, "id": 20, "path": "/20", "title": "20 - 20"},
{"event": false, "id": 21, "path": "/21", "title": "21 - 21"},
{"event": false, "id": 22, "path": "/22", "title": "22 - 22"},
{"event": false, "id": 23, "path": "/23", "title": "23 - 23"},
{"event": false, "id": 24, "path": "/24", "title": "24 - 24"}
{"event": false, "id": 20, "path": "/20", "host": "", "title": "20 - 20"},
{"event": false, "id": 21, "path": "/21", "host": "", "title": "21 - 21"},
{"event": false, "id": 22, "path": "/22", "host": "", "title": "22 - 22"},
{"event": false, "id": 23, "path": "/23", "host": "", "title": "23 - 23"},
{"event": false, "id": 24, "path": "/24", "host": "", "title": "24 - 24"}
]}`,
},

Expand All @@ -712,11 +712,11 @@ func TestAPIPaths(t *testing.T) {
}, "after=45&limit=5", 200, `{
"more": false,
"paths": [
{"event": false, "id": 46, "path": "/46", "title": "46 - 46"},
{"event": false, "id": 47, "path": "/47", "title": "47 - 47"},
{"event": false, "id": 48, "path": "/48", "title": "48 - 48"},
{"event": false, "id": 49, "path": "/49", "title": "49 - 49"},
{"event": false, "id": 50, "path": "/50", "title": "50 - 50"}
{"event": false, "id": 46, "path": "/46", "host": "", "title": "46 - 46"},
{"event": false, "id": 47, "path": "/47", "host": "", "title": "47 - 47"},
{"event": false, "id": 48, "path": "/48", "host": "", "title": "48 - 48"},
{"event": false, "id": 49, "path": "/49", "host": "", "title": "49 - 49"},
{"event": false, "id": 50, "path": "/50", "host": "", "title": "50 - 50"}
]}`,
},

Expand Down Expand Up @@ -781,6 +781,7 @@ func TestAPIHits(t *testing.T) {
"event": false,
"max": 1,
"path": "/50",
"host": "",
"path_id": 50,
"title": "title - 50",
"stats": [{
Expand Down Expand Up @@ -821,6 +822,7 @@ func TestAPIHits(t *testing.T) {
"event": false,
"max": 1,
"path": "/49",
"host": "",
"path_id": 49,
"title": "title - 49",
"stats": [{
Expand Down Expand Up @@ -861,6 +863,7 @@ func TestAPIHits(t *testing.T) {
"event": false,
"max": 1,
"path": "/48",
"host": "",
"path_id": 48,
"title": "title - 48",
"stats": [{
Expand Down Expand Up @@ -908,6 +911,7 @@ func TestAPIHits(t *testing.T) {
"event": false,
"max": 1,
"path": "/48",
"host": "",
"path_id": 48,
"title": "title - 48",
"stats": [{
Expand Down Expand Up @@ -935,6 +939,7 @@ func TestAPIHits(t *testing.T) {
"event": false,
"max": 1,
"path": "/10",
"host": "",
"path_id": 10,
"title": "title - 10",
"stats": [{
Expand Down Expand Up @@ -962,6 +967,7 @@ func TestAPIHits(t *testing.T) {
"event": false,
"max": 1,
"path": "/10",
"host": "",
"path_id": 10,
"title": "title - 10",
"stats": [{
Expand Down
7 changes: 7 additions & 0 deletions handlers/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net/http"
"net/url"

"github.com/monoculum/formam/v3"
"golang.org/x/text/language"
Expand Down Expand Up @@ -55,6 +56,12 @@ func (h backend) count(w http.ResponseWriter, r *http.Request) error {
CreatedAt: ztime.Now(r.Context()),
RemoteAddr: r.RemoteAddr,
}

referer := r.Referer()
if u, err := url.Parse(referer); err != nil {
hit.Host = u.Host
}

if site.Settings.Collect.Has(goatcounter.CollectLocation) {
var l goatcounter.Location
hit.Location = l.LookupIP(r.Context(), r.RemoteAddr)
Expand Down
3 changes: 3 additions & 0 deletions handlers/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ func TestBackendCount(t *testing.T) {
Path: "/" + strings.Repeat("a", 2047),
}},
{"too long", url.Values{"p": []string{"/" + strings.Repeat("a", 2048)}}, nil, 414, goatcounter.Hit{}},
{"host", url.Values{"p": {"/foo.html"}, "h": {"www.example.org"}}, nil, 200, goatcounter.Hit{
Path: "/foo.html", Host: "www.example.org",
}},
}

for _, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions handlers/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestSettingsMerge(t *testing.T) {
"paths": "2",
})
check(ctx, t, `
path_id site_id path title event
path_id site_id path title event host
1 1 /one 0
3 1 /three 0

Expand Down Expand Up @@ -391,7 +391,7 @@ func TestSettingsMerge(t *testing.T) {
"paths": "2,3",
})
check(ctx, t, `
path_id site_id path title event
path_id site_id path title event host
1 1 /one 0

site_id path_id hour total
Expand Down
7 changes: 6 additions & 1 deletion hit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Hit struct {
Width *int16 `db:"width" json:"width"`

Path string `db:"-" json:"p,omitempty"`
Host string `db:"-" json:"h,omitempty"`
Title string `db:"-" json:"t,omitempty"`
Ref string `db:"-" json:"r,omitempty"`
Event zbool.Bool `db:"-" json:"e,omitempty"`
Expand Down Expand Up @@ -256,7 +257,8 @@ func (h *Hit) Defaults(ctx context.Context, initial bool) error {
// vulnerability scanners and whatnot
if h.Bot == 0 {
// Get or insert path.
path := Path{Path: h.Path, Title: h.Title, Event: h.Event}

path := Path{Path: h.Path, Host: h.Host, Title: h.Title, Event: h.Event}
err := path.GetOrInsert(ctx)
if err != nil {
return errors.Wrap(err, "Hit.Defaults")
Expand Down Expand Up @@ -333,6 +335,7 @@ func (h *Hits) TestList(ctx context.Context, siteOnly bool) error {
B BrowserID `db:"browser_id"`
S SystemID `db:"system_id"`
P string `db:"path"`
H string `db:"host"`
T string `db:"title"`
E zbool.Bool `db:"event"`
R string `db:"ref"`
Expand All @@ -344,6 +347,7 @@ func (h *Hits) TestList(ctx context.Context, siteOnly bool) error {
browser_id,
system_id,
paths.path,
paths.host,
paths.title,
paths.event,
refs.ref
Expand All @@ -364,6 +368,7 @@ func (h *Hits) TestList(ctx context.Context, siteOnly bool) error {
x.Hit.BrowserID = x.B
x.Hit.SystemID = x.S
x.Hit.Path = x.P
x.Hit.Host = x.H
x.Hit.Title = x.T
x.Hit.Event = x.E
x.Hit.Ref = x.R
Expand Down
2 changes: 2 additions & 0 deletions hit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type HitList struct {
// Path ID
PathID PathID `db:"path_id" json:"path_id"`

Host string `db:"host" json:"host"`

// Path name (e.g. /hello.html).
Path string `db:"path" json:"path"`

Expand Down
Loading