| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package http |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"net/http" |
| 12 |
"sort" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
|
| 16 |
. "github.com/etix/mirrorbits/config" |
| 17 |
"github.com/etix/mirrorbits/mirrors" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
// ErrTemplatesNotFound is returned cannot be loaded |
| 22 |
ErrTemplatesNotFound = errors.New("please set a valid path to the templates directory") |
| 23 |
) |
| 24 |
|
| 25 |
// resultsRenderer is the interface for all result renderers |
| 26 |
type resultsRenderer interface { |
| 27 |
Write(ctx *Context, results *mirrors.Results) (int, error) |
| 28 |
Type() string |
| 29 |
} |
| 30 |
|
| 31 |
// JSONRenderer is used to render JSON formatted details about the current request |
| 32 |
type JSONRenderer struct{} |
| 33 |
|
| 34 |
// Type returns the type of renderer |
| 35 |
func (w *JSONRenderer) Type() string { |
| 36 |
return "JSON" |
| 37 |
} |
| 38 |
|
| 39 |
// Write is used to write the result to the ResponseWriter |
| 40 |
func (w *JSONRenderer) Write(ctx *Context, results *mirrors.Results) (statusCode int, err error) { |
| 41 |
|
| 42 |
if ctx.IsPretty() { |
| 43 |
output, err := json.MarshalIndent(results, "", " ") |
| 44 |
if err != nil { |
| 45 |
return http.StatusInternalServerError, err |
| 46 |
} |
| 47 |
|
| 48 |
ctx.ResponseWriter().Header().Set("Content-Type", "application/json; charset=utf-8") |
| 49 |
ctx.ResponseWriter().Header().Set("Content-Length", strconv.Itoa(len(output))) |
| 50 |
ctx.ResponseWriter().Write(output) |
| 51 |
} else { |
| 52 |
ctx.ResponseWriter().Header().Set("Content-Type", "application/json; charset=utf-8") |
| 53 |
err = json.NewEncoder(ctx.ResponseWriter()).Encode(results) |
| 54 |
if err != nil { |
| 55 |
return http.StatusInternalServerError, err |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return http.StatusOK, nil |
| 60 |
} |
| 61 |
|
| 62 |
// RedirectRenderer is a basic renderer that redirects the user to the first mirror in the list |
| 63 |
type RedirectRenderer struct{} |
| 64 |
|
| 65 |
// Type returns the type of renderer |
| 66 |
func (w *RedirectRenderer) Type() string { |
| 67 |
return "REDIRECT" |
| 68 |
} |
| 69 |
|
| 70 |
// Write is used to write the result to the ResponseWriter |
| 71 |
func (w *RedirectRenderer) Write(ctx *Context, results *mirrors.Results) (statusCode int, err error) { |
| 72 |
if len(results.MirrorList) > 0 { |
| 73 |
ctx.ResponseWriter().Header().Set("Content-Type", "text/html; charset=utf-8") |
| 74 |
|
| 75 |
path := strings.TrimPrefix(results.FileInfo.Path, "/") |
| 76 |
|
| 77 |
mh := len(results.MirrorList) |
| 78 |
maxheaders := GetConfig().MaxLinkHeaders |
| 79 |
if mh > maxheaders+1 { |
| 80 |
mh = maxheaders + 1 |
| 81 |
} |
| 82 |
|
| 83 |
if mh >= 1 { |
| 84 |
// Generate the header alternative links |
| 85 |
for i, m := range results.MirrorList[1:mh] { |
| 86 |
var countryCode string |
| 87 |
if len(m.CountryFields) > 0 { |
| 88 |
countryCode = strings.ToLower(m.CountryFields[0]) |
| 89 |
} |
| 90 |
ctx.ResponseWriter().Header().Add("Link", fmt.Sprintf("<%s>; rel=duplicate; pri=%d; geo=%s", m.AbsoluteURL+path, i+1, countryCode)) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Finally issue the redirect |
| 95 |
http.Redirect(ctx.ResponseWriter(), ctx.Request(), results.MirrorList[0].AbsoluteURL+path, http.StatusFound) |
| 96 |
return http.StatusFound, nil |
| 97 |
} |
| 98 |
// No mirror returned for this request |
| 99 |
http.NotFound(ctx.ResponseWriter(), ctx.Request()) |
| 100 |
return http.StatusNotFound, nil |
| 101 |
} |
| 102 |
|
| 103 |
// MirrorListRenderer is used to render the mirrorlist page using the HTML templates |
| 104 |
type MirrorListRenderer struct{} |
| 105 |
|
| 106 |
// Type returns the type of renderer |
| 107 |
func (w *MirrorListRenderer) Type() string { |
| 108 |
return "MIRRORLIST" |
| 109 |
} |
| 110 |
|
| 111 |
// Write is used to write the result to the ResponseWriter |
| 112 |
func (w *MirrorListRenderer) Write(ctx *Context, results *mirrors.Results) (statusCode int, err error) { |
| 113 |
if ctx.Templates().mirrorlist == nil { |
| 114 |
// No templates found for the mirrorlist |
| 115 |
return http.StatusInternalServerError, ErrTemplatesNotFound |
| 116 |
} |
| 117 |
// Sort the exclude reasons by message so they appear grouped |
| 118 |
sort.Sort(mirrors.ByExcludeReason{Mirrors: results.ExcludedList}) |
| 119 |
|
| 120 |
// Create a temporary output buffer to render the page |
| 121 |
var buf bytes.Buffer |
| 122 |
|
| 123 |
ctx.ResponseWriter().Header().Set("Content-Type", "text/html; charset=utf-8") |
| 124 |
|
| 125 |
// Render the page into the buffer |
| 126 |
err = ctx.Templates().mirrorlist.ExecuteTemplate(&buf, "base", results) |
| 127 |
if err != nil { |
| 128 |
// Something went wrong, discard the buffer |
| 129 |
return http.StatusInternalServerError, err |
| 130 |
} |
| 131 |
|
| 132 |
// Write the buffer to the socket |
| 133 |
buf.WriteTo(ctx.ResponseWriter()) |
| 134 |
return http.StatusOK, nil |
| 135 |
} |