๐พ Archived View for source.community โบ ckaznocha โบ gemini โบ blob โบ main โบ response.go captured on 2024-02-05 at 09:53:06. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2023-01-29)
-=-=-=-=-=-=-
. ,-. ,-. . . ,-. ,-. ,-. ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . . `-. | | | | | | |-' | | | | | | | | | | | | | | | | | `-' `-' `-^ ' `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-| /| `-'
git clone https://source.community/ckaznocha/gemini.git
View raw contents of /response.go (main)
โโโโโฎ 1โ package gemini 2โ 3โ import ( 4โ "context" 5โ "io" 6โ "mime" 7โ ) 8โ 9โ // ResponseWriter is interface to interact with a Gemini response. Calling any 10โ // of its meethods after one has already been called or after 11โ // Handler.ServeGemini has returned is a no-op. 12โ type ResponseWriter interface { 13โ Failure(ctx context.Context, code StatusCode, msg string) 14โ Input(ctx context.Context, prompt string, isSensitive bool) 15โ Redirect(ctx context.Context, redirectURL string, isPermanant bool) 16โ Success(ctx context.Context, mimeType string) io.Writer 17โ } 18โ 19โ type response struct { 20โ conn io.Writer 21โ sent atomicBool 22โ } 23โ 24โ //nolint:gochecknoglobals // allocate some frequently used byte slices. 25โ var ( 26โ responseHeaderSpace = []byte{' '} 27โ responseHeaderLineTerminator = []byte("\r\n") 28โ ) 29โ 30โ func (r *response) writeHeader(ctx context.Context, code StatusCode, meta []byte) { 31โ if r.sent.isSet() { 32โ return 33โ } 34โ defer r.sent.set() 35โ 36โ logWriter := func(string, bool) {} 37โ 38โ srv, ok := ServerFromCtx(ctx) 39โ if ok && srv.LogHandler != nil { 40โ logWriter = srv.LogHandler 41โ } 42โ 43โ c, err := code.MarshalText() 44โ if err != nil { 45โ logWriter(err.Error(), true) 46โ 47โ return 48โ } 49โ 50โ if _, err = r.conn.Write(c); err != nil { 51โ logWriter(err.Error(), true) 52โ 53โ return 54โ } 55โ 56โ if _, err = r.conn.Write(responseHeaderSpace); err != nil { 57โ logWriter(err.Error(), true) 58โ 59โ return 60โ } 61โ 62โ if _, err = r.conn.Write(meta); err != nil { 63โ logWriter(err.Error(), true) 64โ 65โ return 66โ } 67โ 68โ if _, err = r.conn.Write(responseHeaderLineTerminator); err != nil { 69โ logWriter(err.Error(), true) 70โ 71โ return 72โ } 73โ } 74โ 75โ func (r *response) Input(ctx context.Context, prompt string, isSensitive bool) { 76โ code := StatusInput 77โ if isSensitive { 78โ code = StatusSensitiveInput 79โ } 80โ 81โ r.writeHeader(ctx, code, []byte(prompt)) 82โ } 83โ 84โ func (r *response) Redirect(ctx context.Context, redirectURL string, isPermanant bool) { 85โ code := StatusTemporaryRedirect 86โ if isPermanant { 87โ code = StatusPermanentRedirect 88โ } 89โ 90โ r.writeHeader(ctx, code, []byte(redirectURL)) 91โ } 92โ 93โ func (r *response) Success(ctx context.Context, mimeType string) io.Writer { 94โ if mimeType == "" { 95โ mimeType = mime.FormatMediaType("text/gemini", map[string]string{"charset": "utf-8", "lang": "en"}) 96โ } 97โ 98โ r.writeHeader(ctx, StatusSuccess, []byte(mimeType)) 99โ 100โ return r.conn 101โ } 102โ 103โ func (r *response) Failure(ctx context.Context, code StatusCode, msg string) { 104โ category := code.ToCategory() 105โ if category < StatusCategoryTemporaryFailure || category > StatusCategoryPermanentFailure { 106โ code = StatusPermanentFailure 107โ } 108โ 109โ r.writeHeader(ctx, code, []byte(msg)) 110โ } โโโโโฏ
ยท ยท ยท
ยฉ 2024 source.community