mirror of
https://github.com/Xevion/banner.git
synced 2025-12-09 22:06:31 -06:00
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package banner
|
|
|
|
import (
|
|
"io"
|
|
"math/rand"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func BuildRequestWithBody(method string, path string, params map[string]string, body io.Reader) *http.Request {
|
|
// Builds a URL for the given path and parameters
|
|
url := baseURL + path
|
|
|
|
if params != nil {
|
|
takenFirst := false
|
|
for key, value := range params {
|
|
paramChar := "&"
|
|
if !takenFirst {
|
|
paramChar = "?"
|
|
takenFirst = true
|
|
}
|
|
|
|
url += paramChar + key + "=" + value
|
|
}
|
|
}
|
|
|
|
request, _ := http.NewRequest(method, url, body)
|
|
AddUserAgent(request)
|
|
return request
|
|
}
|
|
|
|
func BuildRequest(method string, path string, params map[string]string) *http.Request {
|
|
return BuildRequestWithBody(method, path, params, nil)
|
|
}
|
|
|
|
func AddUserAgent(req *http.Request) {
|
|
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
|
|
}
|
|
|
|
func ContainsContentType(header string, search string) bool {
|
|
// Split on commas, check if any of the types match
|
|
for _, content_type := range strings.Split(header, ";") {
|
|
if content_type == search {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
func RandomString(n int) string {
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
}
|
|
return string(b)
|
|
}
|