-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
782de99
commit a95c274
Showing
6 changed files
with
95 additions
and
88 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,55 @@ | ||
//go:build windows | ||
|
||
package dhcpd | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghos" | ||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
// jsonError is a generic JSON error response. | ||
// | ||
// TODO(a.garipov): Merge together with the implementations in .../home and | ||
// other packages after refactoring the web handler registering. | ||
type jsonError struct { | ||
// Message is the error message, an opaque string. | ||
Message string `json:"message"` | ||
} | ||
|
||
// notImplemented returns a handler that replies to any request with an HTTP 501 | ||
// Not Implemented status and a JSON error with the provided message msg. | ||
// | ||
// TODO(a.garipov): Either take the logger from the server after we've | ||
// refactored logging or make this not a method of *Server. | ||
func (s *Server) notImplemented(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusNotImplemented) | ||
|
||
err := json.NewEncoder(w).Encode(&jsonError{ | ||
Message: aghos.Unsupported("dhcp").Error(), | ||
}) | ||
if err != nil { | ||
log.Debug("writing 501 json response: %s", err) | ||
} | ||
} | ||
|
||
// registerHandlers sets the handlers for DHCP HTTP API that always respond with | ||
// an HTTP 501, since DHCP server doesn't work on Windows yet. | ||
// | ||
// TODO(a.garipov): This needs refactoring. We shouldn't even try and | ||
// initialize a DHCP server on Windows, but there are currently too many | ||
// interconnected parts--such as HTTP handlers and frontend--to make that work | ||
// properly. | ||
func (s *Server) registerHandlers() { | ||
s.conf.HTTPRegister(http.MethodGet, "/control/dhcp/status", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodGet, "/control/dhcp/interfaces", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/set_config", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/find_active_dhcp", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/add_static_lease", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/remove_static_lease", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/reset", s.notImplemented) | ||
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/reset_leases", s.notImplemented) | ||
} |
11 changes: 8 additions & 3 deletions
11
internal/dhcpd/http_test.go → internal/dhcpd/http_windows_test.go
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 |
---|---|---|
@@ -1,23 +1,28 @@ | ||
//go:build windows | ||
|
||
package dhcpd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghos" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestServer_notImplemented(t *testing.T) { | ||
s := &Server{} | ||
h := s.notImplemented("never!") | ||
|
||
w := httptest.NewRecorder() | ||
r, err := http.NewRequest(http.MethodGet, "/unsupported", nil) | ||
require.NoError(t, err) | ||
|
||
h(w, r) | ||
s.notImplemented(w, r) | ||
assert.Equal(t, http.StatusNotImplemented, w.Code) | ||
assert.Equal(t, `{"message":"never!"}`+"\n", w.Body.String()) | ||
|
||
wantStr := fmt.Sprintf("{%q:%q}", "message", aghos.Unsupported("dhcp")) | ||
assert.JSONEq(t, wantStr, w.Body.String()) | ||
} |
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