Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into evilaffliction-master
  • Loading branch information
DoubleDi committed Jul 4, 2020
2 parents e482166 + ec9f0f6 commit e78cb03
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
58 changes: 37 additions & 21 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser,
}
return nil, err
}

return resp.Body, nil
}

Expand All @@ -291,46 +290,63 @@ func (c *conn) buildRequest(ctx context.Context, query string, params []driver.V
return nil, err
}
}

var bodyReader io.Reader
if readonly {
method = http.MethodGet
} else {
method = http.MethodPost
bodyReader = strings.NewReader(query)
}
c.log("query: ", query)
req, err := http.NewRequest(method, c.url.String(), strings.NewReader(query))
req, err := http.NewRequest(method, c.url.String(), bodyReader)
if err != nil {
return nil, err
}

// http.Transport ignores url.User argument, handle it here
if c.user != nil {
p, _ := c.user.Password()
req.SetBasicAuth(c.user.Username(), p)
}
var queryID, quotaKey string
if ctx != nil {
quotaKey, _ = ctx.Value(QuotaKey).(string)
queryID, _ = ctx.Value(QueryID).(string)
}

if c.killQueryOnErr && queryID == "" {
queryUUID, err := uuid.NewV4()
if err != nil {
c.log("can't generate query_id: ", err)
} else {
queryID = queryUUID.String()
var reqQuery url.Values
if ctx != nil {
quotaKey, quotaOk := ctx.Value(QuotaKey).(string)
if quotaOk {
if reqQuery == nil {
reqQuery = req.URL.Query()
}
reqQuery.Add(quotaKeyParamName, quotaKey)
}
queryID, queryOk := ctx.Value(QueryID).(string)
if c.killQueryOnErr && (!queryOk || queryID == "") {
queryUUID, err := uuid.NewV4()
if err != nil {
c.log("can't generate query_id: ", err)
} else {
queryID = queryUUID.String()
}
}
if queryID != "" {
if reqQuery == nil {
reqQuery = req.URL.Query()
}
reqQuery.Add(queryIDParamName, queryID)
}
}

reqQuery := req.URL.Query()
if quotaKey != "" {
reqQuery.Add(quotaKeyParamName, quotaKey)
}
if queryID != "" {
reqQuery.Add(queryIDParamName, queryID)
if method == http.MethodGet {
if reqQuery == nil {
reqQuery = req.URL.Query()
}
reqQuery.Add("query", query)
}
if reqQuery != nil {
req.URL.RawQuery = reqQuery.Encode()
}
req.URL.RawQuery = reqQuery.Encode()

return req, nil
return req, err
}

func (c *conn) prepare(query string) (*stmt, error) {
Expand Down
19 changes: 9 additions & 10 deletions conn_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"context"
"database/sql/driver"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

Expand All @@ -16,25 +14,26 @@ func (c *conn) Ping(ctx context.Context) error {
if c.transport == nil {
return ErrTransportNil
}
// make request with empty body, response must be "Ok.\n"
u := &url.URL{Scheme: c.url.Scheme, User: c.url.User, Host: c.url.Host, Path: "/ping"}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)

req, err := c.buildRequest(ctx, "select 1", nil, true)
if err != nil {
return err
}

respBody, err := c.doRequest(ctx, req)
defer func() {
if respBody != nil {
respBody.Close()
}
c.cancel = nil
}()
if err != nil {
return err
}

resp, err := ioutil.ReadAll(respBody)
if err != nil {
return err
}
if len(resp) != 4 || !strings.HasPrefix(string(resp), "Ok.") {
return ErrIncorrectResponse
if err != nil || !strings.HasPrefix(string(resp), "1") {
return driver.ErrBadConn
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql/driver"
"io/ioutil"
"net/http"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -217,7 +218,7 @@ func (s *connSuite) TestBuildRequestReadonlyWithAuth() {
s.Equal("user", user)
s.Equal("password", password)
s.Equal(http.MethodGet, req.Method)
s.Equal(cn.url.String(), req.URL.String())
s.Equal(cn.url.String()+"&query="+url.QueryEscape("SELECT 1"), req.URL.String())
s.Nil(req.URL.User)
}
}
Expand Down

0 comments on commit e78cb03

Please sign in to comment.