DoH: fallback to GET if a 405 HTTP status code is returned

Some servers block POST queries. Retry with GET if we receive a
405 HTTP status code.
pull/19/head v0.4.0
Frank Denis 2021-04-24 18:06:25 +02:00 committed by Karan Sharma
parent 9da51ad997
commit 8dc23ce8f5
1 changed files with 12 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package resolvers
import ( import (
"bytes" "bytes"
"encoding/base64"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -64,6 +65,17 @@ func (r *DOHResolver) Lookup(question dns.Question) (Response, error) {
if err != nil { if err != nil {
return rsp, err return rsp, err
} }
if resp.StatusCode == http.StatusMethodNotAllowed {
url, err := url.Parse(r.server)
if err != nil {
return rsp, err
}
url.RawQuery = fmt.Sprintf("dns=%v", base64.RawURLEncoding.EncodeToString(b))
resp, err = r.client.Get(url.String())
if err != nil {
return rsp, err
}
}
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return rsp, fmt.Errorf("error from nameserver %s", resp.Status) return rsp, fmt.Errorf("error from nameserver %s", resp.Status)
} }