-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
406 lines (349 loc) · 10.4 KB
/
main.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
)
// Global Variables
var server *Server
func init() {
// Loan config and init server
server = InitServer("nuget-server-config-gcp.json")
}
func main() {
// Handling Routing
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Local Varibles
var err error // Reusable error
apiKey := "" // APIKey (populated if found in headers)
accessLevel := accessDenied // Access Level (defaults to denied)
altFilePath := path.Join(`/F`, server.URL.Path, `api`, `v2`, `browse`) // Alternative API called by client
// Create new statusWriter
sw := statusWriter{ResponseWriter: w}
// Check if this is NOT part of the Api Routing
if !strings.HasPrefix(r.URL.Path, server.URL.Path) && !strings.HasPrefix(r.URL.Path, altFilePath) {
f := path.Base(r.URL.Path)
if f == "/" {
f = "index.html"
}
serveStaticFile(&sw, r, path.Join("_www", f))
goto End
}
// Open Access Routes (No ApiKey needed)
switch r.Method {
case http.MethodGet:
switch {
case r.URL.String() == server.URL.Path:
serveRoot(&sw, r)
goto End
case r.URL.String() == server.URL.Path+`$metadata`:
serveMetaData(&sw, r)
goto End
}
}
// Process Headers looking for API key (can't access direct as case may not match)
for name, headers := range r.Header {
// Grab ApiKey as it passes
if strings.ToLower(name) == "x-nuget-apikey" {
apiKey = headers[0]
}
}
accessLevel, err = server.fs.GetAccessLevel(apiKey)
if err != nil {
sw.WriteHeader(http.StatusInternalServerError)
goto End
}
// Bounce any unauthorised requests
if accessLevel == accessDenied {
sw.WriteHeader(http.StatusForbidden)
goto End
}
// Restricted Routes
switch r.Method {
case http.MethodGet:
// Perform Routing
switch {
case strings.HasPrefix(r.URL.String(), server.URL.Path+`Packages`):
servePackageFeed(&sw, r)
case strings.HasPrefix(r.URL.String(), server.URL.Path+`FindPackagesById`):
servePackageFeed(&sw, r)
case strings.HasPrefix(r.URL.String(), server.URL.Path+`nupkg`):
servePackageFile(&sw, r)
case strings.HasPrefix(r.URL.String(), server.URL.Path+`files`):
serveStaticFile(&sw, r, r.URL.String()[len(server.URL.Path+`files`):])
case strings.HasPrefix(r.URL.String(), altFilePath):
serveStaticFile(&sw, r, r.URL.String()[len(altFilePath):])
}
case http.MethodPut:
// Bounce any request without write accees
if accessLevel != accessReadWrite {
sw.WriteHeader(http.StatusForbidden)
return
}
// Route
switch {
case r.URL.String() == server.URL.Path:
// Process Request
uploadPackage(&sw, r)
default:
sw.WriteHeader(http.StatusNotFound)
goto End
}
default:
sw.WriteHeader(http.StatusNotFound)
goto End
}
End:
log.Println("Request::", sw.Status(), r.Method, r.URL.String())
if server.config.Loglevel > 0 {
log.Println("Request Headers:")
if len(w.Header()) == 0 {
log.Println(" None")
} else {
for name, headers := range r.Header {
for _, h := range headers {
// Log Key
log.Println(" " + name + "::" + h)
}
}
}
log.Println("Response Headers:")
if len(w.Header()) == 0 {
log.Println(" None")
} else {
for name, headers := range w.Header() {
for _, h := range headers {
// Log Key
log.Println(" " + name + "::" + h)
}
}
}
}
})
// Set port number (Defaults to 80)
p := ""
// if port is set in URL string
if server.URL.Port() != "" {
p = ":" + server.URL.Port()
}
// If PORT EnvVar is set (Google Cloud Run environment)
if os.Getenv("PORT") != "" {
p = ":" + os.Getenv("PORT")
}
// Log and Start server
log.Println("Starting Server on ", server.URL.String()+p)
log.Fatal(http.ListenAndServe(p, nil))
}
func serveRoot(w http.ResponseWriter, r *http.Request) {
// Create a new Service Struct
ns := NewNugetService(server.URL.String())
b := ns.ToBytes()
// Set Headers
w.Header().Set("Content-Type", "application/xml;charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
// Output Xml
w.Write(b)
}
func serveMetaData(w http.ResponseWriter, r *http.Request) {
// Set Headers
w.Header().Set("Content-Type", "application/xml;charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(server.MetaDataResponse)))
// Output Xml
w.Write(server.MetaDataResponse)
}
func serveStaticFile(w http.ResponseWriter, r *http.Request, fn string) {
// Get the file from the FileStore
b, c, err := server.fs.GetFile(fn)
if err == ErrFileNotFound {
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Set Headers
w.Header().Set("Content-Type", c)
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
// Output Xml
w.Write(b)
}
func servePackageFile(w http.ResponseWriter, r *http.Request) {
// get the last two parts of the URL
x := strings.Split(r.URL.String(), `/`)
// Get the file
b, t, err := server.fs.GetPackageFile(x[len(x)-2], x[len(x)-1])
if err == ErrFileNotFound {
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Set header to fix filename on client side
w.Header().Set("Cache-Control", "max-age=3600")
w.Header().Set("Content-Disposition", `filename=`+x[len(x)-2]+x[len(x)-1]+".nupkg")
w.Header().Set("Content-Type", t)
// Serve up the file
w.Write(b)
}
func servePackageFeed(w http.ResponseWriter, r *http.Request) {
// Local Variables
var err error
var b []byte
var params = &packageParams{}
var isMore bool
// Identify & process function parameters if they exist
if i := strings.Index(r.URL.Path, "("); i >= 0 { // Find opening bracket
if j := strings.Index(r.URL.Path[i:], ")"); j >= 0 { // Find closing bracket
params = newPackageParams(r.URL.Path[i+1 : i+j])
}
}
// For /Packages() Route
if strings.HasPrefix(r.URL.String(), server.URL.Path+`Packages`) {
// If params are populated then this is a single entry requests
if params.ID != "" && params.Version != "" {
// Find the entry required
npe, err := server.fs.GetPackageEntry(params.ID, params.Version)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Convert it to Bytes
b = npe.ToBytes()
} else {
// Create a new Service Struct
nf := NewNugetFeed("Packages", server.URL.String())
// Split out weird filter formatting
s := strings.SplitAfterN(r.URL.Query().Get("$filter"), " ", 3)
// Create empty id string
id := ""
// If relevant, repopulate id with
if strings.TrimSpace(s[0]) == "tolower(Id)" && strings.TrimSpace(s[1]) == "eq" {
id = s[2] // Assign to id
id = id[1 : len(id)-1] // Remove quote marks
}
// If $skiptoke is supplied, form it into a package name
startAfter := r.URL.Query().Get("$skiptoken")
startAfter = strings.ReplaceAll(startAfter, `'`, ``)
startAfter = strings.ReplaceAll(startAfter, `,`, `.`)
// Populate Packages from FileStore (100 max)
nf.Packages, isMore, err = server.fs.GetPackageFeedEntries(id, startAfter, 100)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Add link to next page if relevant
if r.URL.Query().Get("$top") != "" && isMore {
// Get the current $top, cast to Int
t, err := strconv.Atoi(r.URL.Query().Get("$top"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Get a copy of the request URL
u, err := url.Parse(r.URL.String())
u.Host = server.URL.Hostname()
u.Scheme = server.URL.Scheme
// Get working copy of Query
q := u.Query()
// Update Values
q.Del("$skip")
q.Set("$top", strconv.Itoa(t-100))
q.Set("$skiptoken", fmt.Sprintf(`'%s','%s'`, nf.Packages[len(nf.Packages)-1].Properties.ID, nf.Packages[len(nf.Packages)-1].Properties.Version))
//Re-assign
u.RawQuery = q.Encode()
// Get un-encoded URL
cleanURL, err := url.PathUnescape(u.String())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Println()
// Add to feed
nf.Link = append(nf.Link, &NugetLink{
Rel: "next",
Href: cleanURL,
})
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Output Xml
b = nf.ToBytes()
}
} else if strings.HasPrefix(r.URL.String(), server.URL.Path+`FindPackagesById`) {
// Get ID from query
id := r.URL.Query().Get("id") // Get Value
id = id[1 : len(id)-1] // Remove Quotes
// Create a new Service Struct
nf := NewNugetFeed("FindPackagesById", server.URL.String())
// Populate Packages from FileStore
nf.Packages, isMore, err = server.fs.GetPackageFeedEntries(id, "", 100)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Output Xml
b = nf.ToBytes()
}
if len(b) == 0 {
w.WriteHeader(404)
} else {
// Set Headers
w.Header().Set("Content-Type", "application/atom+xml;type=feed;charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Write(b)
}
}
func uploadPackage(w http.ResponseWriter, r *http.Request) {
log.Println("Putting Package into FileStore")
// Parse Mime type
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Detect and Decode based on mime type
if strings.HasPrefix(mediaType, "multipart/form-data") {
// Get a multipart.Reader
mr := multipart.NewReader(r.Body, params["boundary"])
// Itterate over parts/files uploaded
for {
// Get he next part from the multipart.Reader, exit loop if no more
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Store the package file in byte array for use
pkgFile, err := ioutil.ReadAll(p)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Store the file
exists, err := server.fs.StorePackage(pkgFile)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if exists == true {
w.WriteHeader(http.StatusConflict)
return
}
w.WriteHeader(http.StatusCreated)
}
}
}