summaryrefslogtreecommitdiff
path: root/http.go
blob: 82aed1330494d0e1e9bf5e25fb123656c17f93ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0

package forgepb

/*
func (f *Forge) HttpPost(base string, route string, data []byte) ([]byte, error) {
	// Fix using url.JoinPath (Best Practice)
	baseURL, _ := url.Parse(config.Get("ForgeURL")) // "http://forge.grid.wit.com:2520")
	finalURL := baseURL.JoinPath(route)             // Correctly produces ...:2520/patches

	var err error
	var req *http.Request

	req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))
	if req == nil {
		return nil, err
	}

	req.Header.Set("author", config.Get("author"))
	req.Header.Set("hostname", config.Get("hostname"))

	return rawHttpPost(req)
}

func rawHttpPost(req *http.Request) ([]byte, error) {
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return []byte("client.Do(req) error"), err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return body, err
	}

	return body, nil
}

func getIpSimple(r *http.Request) string {
	host, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		log.Printf("could not split host port: %v", err)
		return r.RemoteAddr // Fallback
	}
	return host
}

// getClientIP inspects the request for common headers to find the true client IP.
func getClientIP(r *http.Request) string {
	// Caddy sets the X-Forwarded-For header.
	if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" {
		// The header can be a comma-separated list of IPs. The first one is the original client.
		ips := strings.Split(forwardedFor, ",")
		return strings.TrimSpace(ips[0])
	}

	// Fallback to RemoteAddr if the header is not present.
	host, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		return r.RemoteAddr
	}
	return host
}
*/