Skip to content

Commit

Permalink
all: imp naming
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jul 18, 2023
1 parent 4f84b55 commit 98a705b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
22 changes: 14 additions & 8 deletions internal/filtering/blocked.go
Expand Up @@ -125,7 +125,10 @@ func (d *DNSFilter) handleBlockedServicesAll(w http.ResponseWriter, r *http.Requ
})
}

// Deprecated: Use handleBlockedServicesSchedule.
// handleBlockedServicesList is the handler for the GET
// /control/blocked_services/list HTTP API.
//
// Deprecated: Use handleBlockedServicesGet.
func (d *DNSFilter) handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
d.confLock.RLock()
list := d.Config.BlockedServices.IDs
Expand All @@ -134,7 +137,10 @@ func (d *DNSFilter) handleBlockedServicesList(w http.ResponseWriter, r *http.Req
_ = aghhttp.WriteJSONResponse(w, r, list)
}

// Deprecated: Use handleBlockedServicesScheduleUpdate.
// handleBlockedServicesSet is the handler for the POST
// /control/blocked_services/set HTTP API.
//
// Deprecated: Use handleBlockedServicesUpdate.
func (d *DNSFilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
list := []string{}
err := json.NewDecoder(r.Body).Decode(&list)
Expand All @@ -153,9 +159,9 @@ func (d *DNSFilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Requ
d.Config.ConfigModified()
}

// handleBlockedServicesSchedule is the handler for the GET
// /control/blocked_services/schedule HTTP API.
func (d *DNSFilter) handleBlockedServicesSchedule(w http.ResponseWriter, r *http.Request) {
// handleBlockedServicesGet is the handler for the GET
// /control/blocked_services/get HTTP API.
func (d *DNSFilter) handleBlockedServicesGet(w http.ResponseWriter, r *http.Request) {
var bsvc *BlockedServices
func() {
d.confLock.RLock()
Expand All @@ -167,9 +173,9 @@ func (d *DNSFilter) handleBlockedServicesSchedule(w http.ResponseWriter, r *http
_ = aghhttp.WriteJSONResponse(w, r, bsvc)
}

// handleBlockedServicesScheduleUpdate is the handler for the PUT
// /control/blocked_services/schedule/update HTTP API.
func (d *DNSFilter) handleBlockedServicesScheduleUpdate(w http.ResponseWriter, r *http.Request) {
// handleBlockedServicesUpdate is the handler for the PUT
// /control/blocked_services/update HTTP API.
func (d *DNSFilter) handleBlockedServicesUpdate(w http.ResponseWriter, r *http.Request) {
bsvc := &BlockedServices{}
err := json.NewDecoder(r.Body).Decode(bsvc)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/filtering/http.go
Expand Up @@ -565,8 +565,8 @@ func (d *DNSFilter) RegisterFilteringHandlers() {
registerHTTP(http.MethodGet, "/control/blocked_services/list", d.handleBlockedServicesList)
registerHTTP(http.MethodPost, "/control/blocked_services/set", d.handleBlockedServicesSet)

registerHTTP(http.MethodGet, "/control/blocked_services/schedule", d.handleBlockedServicesSchedule)
registerHTTP(http.MethodPut, "/control/blocked_services/schedule/update", d.handleBlockedServicesScheduleUpdate)
registerHTTP(http.MethodGet, "/control/blocked_services/get", d.handleBlockedServicesGet)
registerHTTP(http.MethodPut, "/control/blocked_services/update", d.handleBlockedServicesUpdate)

registerHTTP(http.MethodGet, "/control/filtering/status", d.handleFilteringStatus)
registerHTTP(http.MethodPost, "/control/filtering/config", d.handleFilteringConfig)
Expand Down
45 changes: 28 additions & 17 deletions internal/home/clientshttp.go
Expand Up @@ -57,16 +57,32 @@ type clientJSON struct {
IgnoreStatistics aghalg.NullBool `json:"ignore_statistics"`
}

// copySchedule returns a copy of weekly schedule from JSON, previous client,
// or creates new empty schedule.
func (j *clientJSON) copySchedule(prev *Client) (weekly *schedule.Weekly) {
// copySettings returns a copy of specific settings from JSON or a previous
// client.
func (j *clientJSON) copySettings(
prev *Client,
) (weekly *schedule.Weekly, ignoreQueryLog, ignoreStatistics bool) {
if j.Schedule != nil {
return j.Schedule.Clone()
weekly = j.Schedule.Clone()
} else if prev != nil && prev.BlockedServices != nil {
return prev.BlockedServices.Schedule.Clone()
weekly = prev.BlockedServices.Schedule.Clone()
} else {
weekly = schedule.EmptyWeekly()
}

if j.IgnoreQueryLog != aghalg.NBNull {
ignoreQueryLog = j.IgnoreQueryLog == aghalg.NBTrue
} else if prev != nil {
ignoreQueryLog = prev.IgnoreQueryLog
}

if j.IgnoreStatistics != aghalg.NBNull {
ignoreStatistics = j.IgnoreStatistics == aghalg.NBTrue
} else if prev != nil {
ignoreStatistics = prev.IgnoreStatistics
}

return schedule.EmptyWeekly()
return weekly, ignoreQueryLog, ignoreStatistics
}

type runtimeClientJSON struct {
Expand Down Expand Up @@ -135,7 +151,7 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C
}
}

weekly := cj.copySchedule(prev)
weekly, ignoreQueryLog, ignoreStatistics := cj.copySettings(prev)

c = &Client{
safeSearchConf: safeSearchConf,
Expand All @@ -156,18 +172,13 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C
ParentalEnabled: cj.ParentalEnabled,
SafeBrowsingEnabled: cj.SafeBrowsingEnabled,
UseOwnBlockedServices: !cj.UseGlobalBlockedServices,
IgnoreQueryLog: ignoreQueryLog,
IgnoreStatistics: ignoreStatistics,
}

if cj.IgnoreQueryLog != aghalg.NBNull {
c.IgnoreQueryLog = cj.IgnoreQueryLog == aghalg.NBTrue
} else if prev != nil {
c.IgnoreQueryLog = prev.IgnoreQueryLog
}

if cj.IgnoreStatistics != aghalg.NBNull {
c.IgnoreStatistics = cj.IgnoreStatistics == aghalg.NBTrue
} else if prev != nil {
c.IgnoreStatistics = prev.IgnoreStatistics
err = c.BlockedServices.Validate()
if err != nil {
return nil, fmt.Errorf("validating blocked services: %w", err)
}

if safeSearchConf.Enabled {
Expand Down
10 changes: 5 additions & 5 deletions openapi/CHANGELOG.md
Expand Up @@ -9,17 +9,17 @@
### Deprecated blocked services APIs

* The `GET /control/blocked_services/list` HTTP API; use the new `GET
/control/blocked_services/schedule` API instead.
/control/blocked_services/get` API instead.

* The `POST /control/blocked_services/set` HTTP API; use the new `PUT
/control/blocked_services/schedule/update` API instead.
/control/blocked_services/update` API instead.

### New blocked services APIs

* The new `GET /control/blocked_services/schedule` HTTP API.
* The new `GET /control/blocked_services/get` HTTP API.

* The new `PUT /control/blocked_services/schedule/update` HTTP API allows
config updates.
* The new `PUT /control/blocked_services/update` HTTP API allows config
updates.

These APIs accept and return a JSON object with the following format:

Expand Down
8 changes: 4 additions & 4 deletions openapi/openapi.yaml
Expand Up @@ -1003,7 +1003,7 @@
'get':
'deprecated': true
'description': >
Deprecated: Use `GET /blocked_services/schedule` instead.
Deprecated: Use `GET /blocked_services/get` instead.
'tags':
- 'blocked_services'
'operationId': 'blockedServicesList'
Expand All @@ -1019,7 +1019,7 @@
'post':
'deprecated': true
'description': >
Deprecated: Use `PUT /blocked_services/schedule/update` instead.
Deprecated: Use `PUT /blocked_services/update` instead.
'tags':
- 'blocked_services'
'operationId': 'blockedServicesSet'
Expand All @@ -1032,7 +1032,7 @@
'responses':
'200':
'description': 'OK.'
'/blocked_services/schedule':
'/blocked_services/get':
'get':
'tags':
- 'blocked_services'
Expand All @@ -1045,7 +1045,7 @@
'application/json':
'schema':
'$ref': '#/components/schemas/BlockedServicesSchedule'
'/blocked_services/schedule/update':
'/blocked_services/update':
'put':
'tags':
- 'blocked_services'
Expand Down

0 comments on commit 98a705b

Please sign in to comment.