1
0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-11-23 16:52:06 +01:00
Commit Graph

301 Commits

Author SHA1 Message Date
adnano
da4f693443 tofu: Fix example 2021-01-14 19:57:52 -05:00
adnano
b4b2bf7bc7 tofu: Update documentation 2021-01-14 19:56:04 -05:00
adnano
20df9ae470 tofu: Update documentation 2021-01-14 19:40:19 -05:00
adnano
b623184b87 Update examples/auth.go 2021-01-14 19:04:11 -05:00
adnano
abfcbacc72 tofu: Rename KnownHosts.Hosts to Entries 2021-01-14 18:52:43 -05:00
adnano
831235f365 Add KnownHosts.Hosts function 2021-01-14 18:50:03 -05:00
adnano
666d82d1a5 Update examples/client.go 2021-01-14 17:28:03 -05:00
adnano
bce9d2d257 tofu: Add KnownHosts.Load function 2021-01-14 17:09:31 -05:00
adnano
4cbae11a85 tofu: Add NewHostsFile function 2021-01-14 16:54:38 -05:00
adnano
7e3092f01f tofu: Protect HostWriter with a mutex 2021-01-14 16:35:54 -05:00
adnano
8e676a50f6 tofu: Refactor known hosts
This commit introduces the KnownHosts struct, whose purpose is simply to
store known hosts entries. The HostWriter struct is now in charge of
appending hosts to files, and the two are not dependent on each other.
Users are now responsible for opening the known hosts file and closing
it when they are finished with it.
2021-01-14 16:26:43 -05:00
adnano
6f347628f3 tofu: Rename KnownHost to Host 2021-01-14 14:15:08 -05:00
Hugo Wetterberg
76e8a9f0bd tofu: Refactor
This commit changes underlying file handling and known hosts parsing.

A known hosts file opened through Load() never closed the underlying
file. During known hosts parsing most errors were unchecked, or just
led to the line being skipped.

I removed the KnownHosts type, which didn't really have a role after
the refactor. The embedding of KnownHosts in KnownHosts file has been
removed as it also leaked the map unprotected by the mutex.

The Fingerprint type is now KnownHost and has taken over the
responsibility of marshalling and unmarshalling.

SetOutput now takes a WriteCloser so that we can close the underlying
writer when it's replaced, or when it's explicitly closed through the
new Close() function.

KnownHostsFile.Add() now also writes the known host to the output if
set. I think that makes sense expectation-wise for the type.

Turned WriteAll() into WriteTo() to conform with the io.WriterTo
interface.

Load() is now Open() to better reflect the fact that a file is opened,
and kept open. It can now also return errors from the parsing process.

The parser does a lot more error checking, and this might be an area
where I've changed a desired behaviour as invalid entries no longer
are ignored, but aborts the parsing process. That could be changed to
a warning, or some kind of parsing feedback.

I added KnownHostsFile.TOFU() to fill the developer experience gap
that was left after the client no longer knows about
KnownHostsFile. It implements a basic non-interactive TOFU flow.
2021-01-14 13:48:57 -05:00
Hugo Wetterberg
9f69c28198 client: set the client timout on the dialer, close connection on err
Client.Timout isn't respected for the dial. Requests will hang on dial
until OS-level timouts kick in unless there is a Request.Context with
a deadline. We also fail to close the connection on errors.

This change sets the client timeout as the dialer timeout so that it
will be respected. It also ensures that we close the connection if we
fail to make the request.
2021-01-13 17:13:56 -05:00
adnano
a3c883de8a Move tofu.go to a subpackage 2021-01-10 16:46:12 -05:00
adnano
c88ba29b17 Update documentation 2021-01-10 01:21:56 -05:00
adnano
381316b1e7 Move documentation back to doc.go 2021-01-10 01:16:50 -05:00
adnano
1456f82d66 Update examples/stream.go 2021-01-10 01:13:07 -05:00
adnano
106d0b1c1d Update comments 2021-01-10 01:07:38 -05:00
adnano
07fe3754a3 Update link to documentation 2021-01-10 00:55:39 -05:00
adnano
cd988d85ae Simplify ResponseWriter implementation 2021-01-10 00:50:35 -05:00
adnano
c76ff83964 Rename status.Message to status.Meta 2021-01-10 00:10:57 -05:00
Hugo Wetterberg
68767e6bf0 Add missing error handling
Error handling is currently missing is a couple of places. Most of
them are i/o related.

This change adds checks, an therefore sometimes also has to change
function signatures by adding an error return value. In the case of
the response writer the status and meta handling is changed and this
also breaks the API.

In some places where we don't have any reasonable I've added
assignment to a blank identifier to make it clear that we're ignoring
an error.

text: read the Err() that can be set by the scanner.

client: check if conn.SetDeadline() returns an error.

client: check if req.Write() returns an error.

fs: panic if mime type registration fails.

server: stop performing i/o in Header/Status functions

By deferring the actual header write to the first Write() or Flush()
call we don't have to do any error handling in Header() or Status().

As Server.respond() now defers a ResponseWriter.Flush() instead of
directly flushing the underlying bufio.Writer this has the added
benefit of ensuring that we always write a header
to the client, even if the responder is a complete NOOP.

tofu: return an error if we fail to write to the known hosts writer.
2021-01-09 23:53:07 -05:00
Hugo Wetterberg
010ce903ea server: abort request handling on bad requests
A request to a hostname that hasn't been registered with the server
currently results in a nil pointer deref panic in server.go:215 as
request handling continues even if ReadRequest() returns an error.

This change changes all if-else error handling in Server.respond() to
a WriteStatus-call and early return. This makes it clear when request
handling is aborted (and actually aborts when ReadRequest() fails).
2021-01-05 18:33:36 -05:00
adnano
10c87efbc1 client: Close connection for unsuccessful responses 2020-12-22 19:22:01 -05:00
adnano
add315920c examples/client: Fix fingerprint check 2020-12-19 13:44:33 -05:00
adnano
a9922727e9 client: Verify expiration time 2020-12-19 13:43:47 -05:00
adnano
b5cc06ad60 Add ResponseWriter.Flush function 2020-12-18 13:15:34 -05:00
adnano
896573601c Add examples/stream.go 2020-12-18 12:31:37 -05:00
adnano
8dde9b9cc9 Make NewResponseWriter take an io.Writer 2020-12-18 01:47:29 -05:00
adnano
c35dc332d3 Update Request documentation 2020-12-18 01:43:18 -05:00
adnano
cc962586d4 Add ReadRequest and ReadResponse functions 2020-12-18 01:42:05 -05:00
adnano
b9a9b65d0b Expose Request.Write and Response.Read functions 2020-12-18 01:14:06 -05:00
adnano
956c4bb5e4 server: Don't always assume TLS is used 2020-12-18 01:02:04 -05:00
adnano
e6f6366708 examples: Add missing descriptions 2020-12-18 00:47:30 -05:00
adnano
f2d93867c4 examples/html: Read from stdin and write to stdout 2020-12-18 00:45:09 -05:00
adnano
bbd8837651 Remove unnecessary variable 2020-12-18 00:35:08 -05:00
adnano
316a9f40ee Update QueryEscape documentation 2020-12-18 00:26:47 -05:00
adnano
70fbdb0e5c Remove Input function 2020-12-18 00:25:06 -05:00
adnano
2efc7a601b Remove Response.Request field 2020-12-18 00:19:53 -05:00
adnano
46e37ca9d6 client: Remove GetInput and CheckRedirect callbacks 2020-12-18 00:12:32 -05:00
adnano
6a0c970814 Update switch statement 2020-12-17 23:03:33 -05:00
adnano
11797c1adc client: Remove GetCertificate callback 2020-12-17 22:56:48 -05:00
adnano
c20ab2054a examples/auth: Fix crash on changing username 2020-12-17 21:10:53 -05:00
adnano
2bbc603627 Update GetCertificate documentation 2020-12-17 19:54:46 -05:00
adnano
aafbf9e590 Decouple Client from KnownHostsFile 2020-12-17 19:50:26 -05:00
adnano
70bb9c4e59 Allow Request.Context to be nil 2020-12-17 17:16:55 -05:00
adnano
d85abb61db Fix locking up of KnownHostsFile and CertificateDir 2020-12-17 17:15:24 -05:00
adnano
2d4989c002 Use RWMutex instead of Mutex 2020-12-17 17:08:45 -05:00
adnano
29d420ffc9 Add missing imports 2020-12-17 17:07:00 -05:00