Fix escaping of queries

This commit is contained in:
Adnan Maolood 2020-11-27 22:26:22 -05:00
parent 24e488a4cb
commit 16739d20d0
3 changed files with 21 additions and 1 deletions

View File

@ -155,7 +155,7 @@ func (c *Client) do(req *Request, via []*Request) (*Response, error) {
input, ok := c.GetInput(resp.Meta, resp.Status == StatusSensitiveInput)
if ok {
req.URL.ForceQuery = true
req.URL.RawQuery = url.QueryEscape(input)
req.URL.RawQuery = QueryEscape(input)
return c.do(req, via)
}
}

17
query.go Normal file
View File

@ -0,0 +1,17 @@
package gemini
import (
"net/url"
"strings"
)
// QueryEscape properly escapes a string for use in a Gemini URL query.
// It is like url.PathEscape except that it also replaces plus signs with their percent-encoded counterpart.
func QueryEscape(query string) string {
return strings.ReplaceAll(url.PathEscape(query), "+", "%2B")
}
// QueryUnescape is identical to url.PathUnescape.
func QueryUnescape(query string) (string, error) {
return url.PathUnescape(query)
}

View File

@ -51,6 +51,9 @@ func NewRequest(rawurl string) (*Request, error) {
// NewRequestFromURL returns a new request for the given URL.
// The host is inferred from the URL.
//
// Callers should be careful that the URL query is properly escaped.
// See the documentation for QueryEscape for more information.
func NewRequestFromURL(url *url.URL) *Request {
host := url.Host
if url.Port() == "" {