Compare commits

...

2 Commits

Author SHA1 Message Date
hedy 593d4ca9cb
Don't show data error if destination URL not found 2023-12-06 09:40:28 +08:00
hedy 95ac946384
Update README 2023-07-02 14:03:11 +08:00
3 changed files with 70 additions and 19 deletions

View File

@ -8,14 +8,6 @@ A static spartan server with many features:
* CONF or TOML config file
* CGI
Table of Contents
=> #install install
=> #configuration configuation
=> #cli CLI
=> #cgi CGI
=> #todo todo
Known servers running spsrv
=> spartan://hedy.tilde.cafe:3333
@ -24,6 +16,20 @@ Known servers running spsrv
=> spartan://earthlight.xyz:3000
=> spartan://jdcard.com:3300
Questions / Support
=> irc://irc.tilde.chat:6697/#spartan #spartan on Tilde.Chat (please ping hedy)
=> mailto:~hedy/inbox@lists.sr.ht Public inbox on lists.sr.ht
Table of Contents
=> #install install
=> #configuration configuation
=> #cli CLI
=> #cgi CGI
=> #todo todo
## install
@ -166,6 +172,15 @@ Check out some example CGI scripts in the examples/ directory.
=> https://tildegit.org/hedy/spsrv/src/branch/main/examples/ examples/
Example systemd service configurations are also listed there. Feel free to contribute for other OSes :)
## Help / Issues / Feedback
Please either use the #spartan channel on tilde.chat IRC or my public inbox.
Both are listed at the top of this document.
## todo

View File

@ -15,6 +15,14 @@ Known servers running spsrv:
* [earthlight.xyz:3000](https://portal.mozz.us/spartan/earthlight.xyz:3000)
* [jdcard.com:3300](https://portal.mozz.us/spartan/jdcard.com:3300/)
**Questions / Support**
* [#spartan on Tilde.Chat IRC](https://tilde.chat/kiwi/#spartan) (please ping
hedy)
* [Public inbox](mailto:~hedy/inbox@lists.sr.ht) (general mailing list on
lists.sr.ht)
* Patches: { [~hedy/inbox at lists.sr.ht](https://lists.sr.ht/~hedy/inbox) }
---
**Table of contents**
@ -22,14 +30,15 @@ Known servers running spsrv:
<!-- vim-markdown-toc GFM -->
* [install](#install)
* [Option 1: prebuilt binaries](#option-1-prebuilt-binaries)
* [Option 2: with `go install`](#option-2-with-go-install)
* [Option 3: just build it yourself](#option-3-just-build-it-yourself)
* [otherwise...](#otherwise)
* [Option 1: prebuilt binaries](#option-1-prebuilt-binaries)
* [Option 2: with `go install`](#option-2-with-go-install)
* [Option 3: just build it yourself](#option-3-just-build-it-yourself)
* [otherwise...](#otherwise)
* [configuration](#configuration)
* [config options](#config-options)
* [config options](#config-options)
* [CLI](#cli)
* [CGI](#cgi)
* [Help / Issues / Feedback](#help--issues--feedback)
* [todo](#todo)
<!-- vim-markdown-toc -->
@ -176,6 +185,20 @@ their own CGI scripts. See configuration section for more details.
Check out some example CGI scripts in the [examples/](examples/) directory.
Example systemd service configurations are also listed there. Feel free to
contribute for other OSes :)
## Help / Issues / Feedback
Please either use the [#spartan channel on tilde.chat
IRC](https://tilde.chat/kiwi/#spartan) or my [public
inbox](https://lists.sr.ht/~hedy/inbox).
Both are listed at the top of this document.
**Patches** -> [public inbox](https://lists.sr.ht/~hedy/inbox)
## todo

View File

@ -241,12 +241,12 @@ func handleConnection(netConn net.Conn, conf *Config) {
// Reaching here means it is a static file
if dataLen != 0 {
log.Printf("Got data block of length %v, returning client error.", dataLen)
sendResponseHeader(conn, statusClientError, "Unwanted input data block received")
return
log.Printf("Got data block of length %v for request where CGI not found.", dataLen)
// Not erroring out here because if file not found, return not found rather
// than 'Unexpected input'
}
serveFile(conn, reqPath, path, conf)
serveFile(conn, reqPath, path, conf, dataLen != 0)
}
// resolvePath takes in teh request path and returns the cleaned filepath that needs to be fetched.
@ -293,7 +293,7 @@ func resolvePath(reqPath string, conf *Config, req *Request) (path string) {
}
// serveFile serves opens the requested path and returns the file content
func serveFile(conn io.ReadWriteCloser, reqPath, path string, conf *Config) {
func serveFile(conn io.ReadWriteCloser, reqPath, path string, conf *Config, hasData bool) {
// If the content directory is not specified as an absolute path, make it absolute.
// prefixDir := ""
// var rootDir http.Dir
@ -312,7 +312,11 @@ func serveFile(conn io.ReadWriteCloser, reqPath, path string, conf *Config) {
// be opened without errors
// Directory listing
if conf.DirlistEnable && strings.HasSuffix(path, "index.gmi") {
// fullPath := filepath.Join(fmt.Sprint(rootDir), path)
if hasData {
log.Println("Returning client error due to unexpected data block")
sendResponseHeader(conn, statusClientError, "Unexpected input data block received")
return
}
fullPath := path
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
// If and only if the path is index.gmi AND index.gmi does not exist
@ -333,11 +337,20 @@ func serveFile(conn io.ReadWriteCloser, reqPath, path string, conf *Config) {
}
}
log.Println(err)
log.Println("Returning not found")
sendResponseHeader(conn, statusClientError, "Not found")
return
}
defer f.Close()
// Only show this if we are certain that the request was for a static file.
// Which does not include the 'Not found'.
if hasData {
log.Println("Returning client error due to unexpected data block")
sendResponseHeader(conn, statusClientError, "Unexpected input data block received")
return
}
// Read da file
content, err = ioutil.ReadAll(f)
if err != nil {