~gsthnz/satellite

c959cb5c76df6178473c890a1d9235c9c62f95af — Gustavo Heinz 3 years ago c1d97fa
gofmt
1 files changed, 47 insertions(+), 47 deletions(-)

M gemini.go
M gemini.go => gemini.go +47 -47
@@ 9,21 9,21 @@ import (
	"os"
	"path"
	"strings"
    "unicode/utf8"
	"unicode/utf8"
)

type Status int

const (
	Input = 10
	Success = 20
	Redirect = 30
	RedirectPermanent = 31
	TemporaryFailure = 40
	PermanentFailure = 50
	NotFound = 51
	ProxyRequestRefused = 53
	BadRequest = 59
	Input                     = 10
	Success                   = 20
	Redirect                  = 30
	RedirectPermanent         = 31
	TemporaryFailure          = 40
	PermanentFailure          = 50
	NotFound                  = 51
	ProxyRequestRefused       = 53
	BadRequest                = 59
	ClientCertificateRequired = 60
)



@@ 34,40 34,40 @@ const GeminiMIME = "text/gemini"

func handleRequest(c net.Conn, rawURL string) {
	rawURL = strings.TrimSpace(rawURL)
    if rawURL == "" {
        sendError(c, BadRequest, "Empty URL")
        return
    } else if !utf8.ValidString(rawURL) {
        sendError(c, BadRequest, "Non UTF-8 Request")
        return
    } else if len(rawURL) > MaxURLSize {
        sendError(c, BadRequest, "URL Larger than 1024 bytes")
        return
    }
	if rawURL == "" {
		sendError(c, BadRequest, "Empty URL")
		return
	} else if !utf8.ValidString(rawURL) {
		sendError(c, BadRequest, "Non UTF-8 Request")
		return
	} else if len(rawURL) > MaxURLSize {
		sendError(c, BadRequest, "URL Larger than 1024 bytes")
		return
	}

	parsedURL, err := url.Parse(rawURL)
	if err != nil {
        sendError(c, BadRequest, "Bad URL")
		sendError(c, BadRequest, "Bad URL")
	}

    if parsedURL.Scheme != "" && parsedURL.Scheme != "gemini" {
        sendError(c, ProxyRequestRefused, fmt.Sprintf("Unknown scheme '%s'", parsedURL.Scheme))
        return
    } else if parsedURL.Host == "" {
        sendError(c, BadRequest, "Host not supplied")
        return
	} else if parsedURL.Host != rootDomain && parsedURL.Host != fmt.Sprintf("%s:%d", rootDomain, rootPort){
        sendError(c, ProxyRequestRefused, fmt.Sprintf("Host not found '%s'", parsedURL.Host))
        return
    } else if parsedURL.Path == "" {
		redirectPermanent(c, rawURL + "/")
	if parsedURL.Scheme != "" && parsedURL.Scheme != "gemini" {
		sendError(c, ProxyRequestRefused, fmt.Sprintf("Unknown scheme '%s'", parsedURL.Scheme))
		return
	} else if parsedURL.Host == "" {
		sendError(c, BadRequest, "Host not supplied")
		return
	} else if parsedURL.Host != rootDomain && parsedURL.Host != fmt.Sprintf("%s:%d", rootDomain, rootPort) {
		sendError(c, ProxyRequestRefused, fmt.Sprintf("Host not found '%s'", parsedURL.Host))
		return
	} else if parsedURL.Path == "" {
		redirectPermanent(c, rawURL+"/")
		return
	} else if parsedURL.Path != path.Clean(parsedURL.Path) {
        sendError(c, BadRequest, "Path error")
        return
		sendError(c, BadRequest, "Path error")
		return
	}

    if parsedURL.Path == "/" || parsedURL.Path == "." {
	if parsedURL.Path == "/" || parsedURL.Path == "." {
		serve(c, IndexFile)
	} else {
		serve(c, parsedURL.Path)


@@ 77,23 77,23 @@ func handleRequest(c net.Conn, rawURL string) {
func serve(c net.Conn, filepath string) {
	fullPath := path.Join(rootPath, filepath)

    _, err := os.Stat(fullPath)
    if err != nil {
        sendError(c, NotFound, "Not found")
        return
    }
	_, err := os.Stat(fullPath)
	if err != nil {
		sendError(c, NotFound, "Not found")
		return
	}

	mimeType := getMimeType(fullPath)

	if mimeType == "" {
        sendError(c, NotFound, "")
		sendError(c, NotFound, "")
		return
	}

	file, err := os.Open(fullPath)
	if err != nil {
        sendError(c, NotFound, "")
        return
		sendError(c, NotFound, "")
		return
	}
	defer file.Close()



@@ 101,7 101,7 @@ func serve(c net.Conn, filepath string) {
}

func redirectPermanent(c net.Conn, url string) {
    c.Write(getResponseBytes(RedirectPermanent, url))
	c.Write(getResponseBytes(RedirectPermanent, url))
}

func sendError(c net.Conn, code int, message string) {


@@ 114,9 114,9 @@ func success(c net.Conn, file *os.File, mimeType string) {
}

func getResponseBytes(code int, message string) []byte {
    if message == "" {
        return []byte(fmt.Sprintf("%d%s", code, ResponseEnd))
    }
	if message == "" {
		return []byte(fmt.Sprintf("%d%s", code, ResponseEnd))
	}
	return []byte(fmt.Sprintf("%d %s%s", code, message, ResponseEnd))
}