Skip to content

Commit 2220e5d

Browse files
authored
Allow HOST has no port (#22280)
Fix #22274 This PR will allow `HOST` without port. Then a default port will be given in future steps.
1 parent 477a1cc commit 2220e5d

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

Diff for: modules/setting/mailer.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313

1414
shellquote "github.com/kballard/go-shellquote"
15+
ini "gopkg.in/ini.v1"
1516
)
1617

1718
// Mailer represents mail service.
@@ -49,8 +50,8 @@ type Mailer struct {
4950
// MailService the global mailer
5051
var MailService *Mailer
5152

52-
func newMailService() {
53-
sec := Cfg.Section("mailer")
53+
func parseMailerConfig(rootCfg *ini.File) {
54+
sec := rootCfg.Section("mailer")
5455
// Check mailer setting.
5556
if !sec.Key("ENABLED").MustBool() {
5657
return
@@ -70,9 +71,14 @@ func newMailService() {
7071
if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
7172
givenHost := sec.Key("HOST").String()
7273
addr, port, err := net.SplitHostPort(givenHost)
73-
if err != nil {
74+
if err != nil && strings.Contains(err.Error(), "missing port in address") {
75+
addr = givenHost
76+
} else if err != nil {
7477
log.Fatal("Invalid mailer.HOST (%s): %v", givenHost, err)
7578
}
79+
if addr == "" {
80+
addr = "127.0.0.1"
81+
}
7682
sec.Key("SMTP_ADDR").MustString(addr)
7783
sec.Key("SMTP_PORT").MustString(port)
7884
}
@@ -172,6 +178,9 @@ func newMailService() {
172178
default:
173179
log.Error("unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps", MailService.SMTPPort)
174180
MailService.Protocol = "smtps"
181+
if MailService.SMTPPort == "" {
182+
MailService.SMTPPort = "465"
183+
}
175184
}
176185
}
177186
}

Diff for: modules/setting/mailer_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package setting
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
ini "gopkg.in/ini.v1"
11+
)
12+
13+
func TestParseMailerConfig(t *testing.T) {
14+
iniFile := ini.Empty()
15+
kases := map[string]*Mailer{
16+
"smtp.mydomain.com": {
17+
SMTPAddr: "smtp.mydomain.com",
18+
SMTPPort: "465",
19+
},
20+
"smtp.mydomain.com:123": {
21+
SMTPAddr: "smtp.mydomain.com",
22+
SMTPPort: "123",
23+
},
24+
":123": {
25+
SMTPAddr: "127.0.0.1",
26+
SMTPPort: "123",
27+
},
28+
}
29+
for host, kase := range kases {
30+
t.Run(host, func(t *testing.T) {
31+
iniFile.DeleteSection("mailer")
32+
sec := iniFile.Section("mailer")
33+
sec.NewKey("ENABLED", "true")
34+
sec.NewKey("HOST", host)
35+
36+
// Check mailer setting
37+
parseMailerConfig(iniFile)
38+
39+
assert.EqualValues(t, kase.SMTPAddr, MailService.SMTPAddr)
40+
assert.EqualValues(t, kase.SMTPPort, MailService.SMTPPort)
41+
})
42+
}
43+
}

Diff for: modules/setting/setting.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ func NewServices() {
13401340
newCacheService()
13411341
newSessionService()
13421342
newCORSService()
1343-
newMailService()
1343+
parseMailerConfig(Cfg)
13441344
newRegisterMailService()
13451345
newNotifyMailService()
13461346
newProxyService()
@@ -1357,5 +1357,5 @@ func NewServices() {
13571357
// NewServicesForInstall initializes the services for install
13581358
func NewServicesForInstall() {
13591359
newService()
1360-
newMailService()
1360+
parseMailerConfig(Cfg)
13611361
}

0 commit comments

Comments
 (0)