summaryrefslogtreecommitdiff
path: root/http.go
blob: 486cf13f9423f5eb434fd1d7434e2934fc4d80a7 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main

import (
	"fmt"
	"net/http"
	"strings"

	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/lib/protobuf/httppb"
	"go.wit.com/log"
)

func logReqPB(pb *httppb.HttpRequest) {
	log.Info("LOG: httppb.HttpRequest START:")
	for i, line := range pb.Logs {
		log.Infof("%d, URL:%s LINE: %s\n", int(i), pb.URL, line)
	}
	log.Info("LOG: httppb.HttpRequest END")
}

func okHandler(w http.ResponseWriter, r *http.Request) {
	reqPB, err := httppb.ReqToPB(r)
	reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
	if err != nil {
		reqPB.Logf("httppb err %v", err)
	}

	route := reqPB.Route

	if route == "/" {
		w.Header().Set("Content-Type", "text")
		fmt.Fprintf(w, "go.wit.com/apps/utils/forged Version: %s\n", argv.Version())
		fmt.Fprintf(w, "\n")
		return
	}

	if strings.HasPrefix(route, "/repos/") {
		pb := gitpb.NewRepos()
		if err := pb.Unmarshal(reqPB.ClientData); err == nil {
			reqPB.Logf("Repos Unmarshal() len=%d", pb.Len())
		} else {
			reqPB.Logf("Repos Unmarshal() err=%v", err)
		}
		result := gitpb.NewRepos()
		switch route {
		case "/repos/check":
			result = addRequest(pb, reqPB)
			reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
		case "/repos/pull":
			result = pullRequest(pb, reqPB)
		case "/repos/add":
			result = addRequest(pb, reqPB)
		default:
			reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
			log.Info("repos", route, "unknown")
		}
		if err := result.SendReply(w, reqPB); err != nil {
			reqPB.Logf("Oh well, Send to client failed. err=%v", err)
		}
		// todo: logReq(reqPB)
		logReqPB(reqPB)
		return
	}

	if strings.HasPrefix(route, "/patches/") {
		pb := forgepb.NewPatches()
		if err := pb.Unmarshal(reqPB.ClientData); err == nil {
			reqPB.Logf("Patches Unmarshal() len=%d", pb.Len())
		} else {
			reqPB.Logf("Patches Unmarshal() err=%v", err)
		}
		result := forgepb.NewPatches()
		switch route {
		case "/patches/new":
			result = addNewPatches(pb, reqPB)
			reqPB.Logf("addNewPatches() pb.Len()=%d result.Len()=%d\n", pb.Len(), result.Len())
		case "/patches/applied":
			reqPB.Log("not really anything needs to be done on applied patches?")
		case "/patches/merged":
			reqPB.Log("a maintainer has merged these patches")
			result = handleMergedPatches(pb, reqPB)
			// result = handleAppliedPatches(pb, reqPB)
		case "/patches/get":
			result = sendPendingPatches(pb, reqPB)
		default:
			result = addNewPatches(pb, reqPB)
		}
		if err := result.SendReply(w, reqPB); err != nil {
			reqPB.Logf("Oh well, Send to client failed. err=%v", err)
		}
		me.forge.SavePatchsets()
		// todo: logReq(reqPB)
		logReqPB(reqPB)
		return
	}

	if strings.HasPrefix(route, "/patchsets/") {
		pb, err := makePatchsetsPB(reqPB)
		if err != nil {
			reqPB.Logf("%v", err)
		}
		result := forgepb.NewPatchsets()
		switch route {
		case "/patches/get":
			result = sendPendingPatchsets(pb, reqPB)
		default:
			result = sendPendingPatchsets(pb, reqPB)
		}
		if err := result.SendReply(w, reqPB); err != nil {
			log.Info("Oh well, Send to client failed. err =", err)
		}
		log.Info("Send to client seems to have worked. log:", reqPB.Log)
		me.forge.SavePatchsets()
		// todo: logReq(reqPB)
		logReqPB(reqPB)
		return
	}
	logReqPB(reqPB)

	// used for uptime monitor checking
	if route == "/uptime" {
		httppb.WriteFile(w, resources, "uptime.html")
		return
	}

	if route == "/goReference.svg" {
		w.Header().Set("Content-Type", "image/svg+xml")
		httppb.WriteFile(w, resources, "goReference.svg")
		return
	}
	if route == "/skeleton.v2.css" {
		httppb.WriteFile(w, resources, "skeleton.v2.css")
		return
	}
	if route == "/favicon.ico" {
		httppb.WriteFile(w, resources, "ipv6.png")
		return
	}
	log.Warn("BAD URL =", reqPB.URL, "from", reqPB.IP, "author", reqPB.Hostname)
	badurl(w, r.URL.String())
}

/*
func writeFile(w http.ResponseWriter, filename string) {
	// fmt.Fprintln(w, "GOT TEST?")
	fullname := "resources/" + filename
	pfile, err := resources.ReadFile(fullname)
	if err != nil {
		log.Println("ERROR:", err)
		// w.Write(pfile)
		return
	}

	var repohtml string
	repohtml = string(pfile)
	fmt.Fprintln(w, repohtml)
}
*/

func badurl(w http.ResponseWriter, badurl string) {
	fmt.Fprintln(w, "<!DOCTYPE html>")
	fmt.Fprintln(w, "<html>")
	fmt.Fprintln(w, "    <head>")
	fmt.Fprintln(w, "        <meta http-equiv=\"refresh\" content=\"3; url=https://"+HOSTNAME+"/\">")
	fmt.Fprintln(w, "    </head>")
	fmt.Fprintln(w, "    <body>")
	fmt.Fprintln(w, "        IPv4 MAY NOT WORK<br>")
	fmt.Fprintln(w, "        FORGE REQUIRES IPv6.<br>")
	fmt.Fprintln(w, "        <br>")
	fmt.Fprintln(w, "        bad url", badurl, "<a href=\"https://forge.wit.com/\">redirecting</a>")
	fmt.Fprintln(w, "    </body>")
	fmt.Fprintln(w, "</html>")
}