|
|
|
@ -8,6 +8,10 @@ import (
|
|
|
|
|
"github.com/grufwub/go-errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// rootRedirectHeader stores the root redirect header byte slice,
|
|
|
|
|
// because there is no need in recalculating it every time it's needed
|
|
|
|
|
var rootRedirect []byte
|
|
|
|
|
|
|
|
|
|
// serve is the global gemini server's serve function
|
|
|
|
|
func serve(client *core.Client) {
|
|
|
|
|
// Receive line from client
|
|
|
|
@ -19,13 +23,6 @@ func serve(client *core.Client) {
|
|
|
|
|
}
|
|
|
|
|
raw := string(received)
|
|
|
|
|
|
|
|
|
|
// We don't accept empty requests
|
|
|
|
|
if len(raw) == 0 {
|
|
|
|
|
client.LogError(invalidRequestStr, raw)
|
|
|
|
|
handleError(client, core.ErrInvalidRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure is a valid URL string
|
|
|
|
|
if core.HasAsciiControlBytes(raw) {
|
|
|
|
|
client.LogError(invalidRequestStr, raw)
|
|
|
|
@ -77,13 +74,18 @@ func serve(client *core.Client) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redirect empty path to root
|
|
|
|
|
if len(path) < 1 {
|
|
|
|
|
client.LogInfo(clientRedirectStr, "/")
|
|
|
|
|
client.Conn().Write(rootRedirect)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build new Request from raw path and query
|
|
|
|
|
request := core.NewRequest(core.BuildPath(path), query)
|
|
|
|
|
|
|
|
|
|
// Handle the request!
|
|
|
|
|
// Handle the request! And finally, error
|
|
|
|
|
err = core.HandleClient(client, request)
|
|
|
|
|
|
|
|
|
|
// Final error handling
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleError(client, err)
|
|
|
|
|
client.LogError(clientServeFailStr, request.String())
|
|
|
|
|