๐Ÿ’พ Archived View for source.community โ€บ ckaznocha โ€บ gemini โ€บ blob โ€บ main โ€บ request.go captured on 2024-05-26 at 14:51:57. Gemini links have been rewritten to link to archived content

View Raw

More Information

โฌ…๏ธ Previous capture (2024-02-05)

-=-=-=-=-=-=-

                                                         .
,-. ,-. . . ,-. ,-. ,-.    ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . .
`-. | | | | |   |   |-'    |   | | | | | | | | | | | | | |  | |
`-' `-' `-^ '   `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-|
                                                             /|
                                                            `-'

Profile for ckaznocha

ckaznocha / gemini

git clone https://source.community/ckaznocha/gemini.git

Branches

Log

Tree

/request.go (main)

โ†‘ /

blob

View raw contents of /request.go (main)

โ”€โ”€โ”€โ”€โ•ฎ
   1โ”‚ package gemini
   2โ”‚ 
   3โ”‚ import (
   4โ”‚ 	"bufio"
   5โ”‚ 	"crypto/x509/pkix"
   6โ”‚ 	"fmt"
   7โ”‚ 	"net/textproto"
   8โ”‚ 	"reflect"
   9โ”‚ 	"sync"
  10โ”‚ 	"unsafe"
  11โ”‚ )
  12โ”‚ 
  13โ”‚ const maxRequestLength = 1024
  14โ”‚ 
  15โ”‚ // Request is a Gemini request.
  16โ”‚ type Request struct {
  17โ”‚ 	Subject    *pkix.Name
  18โ”‚ 	URI        *URI
  19โ”‚ 	RemoteAddr string
  20โ”‚ }
  21โ”‚ 
  22โ”‚ var requestPool = sync.Pool{
  23โ”‚ 	New: func() interface{} {
  24โ”‚ 		b := make([]byte, 0, maxRequestLength)
  25โ”‚ 
  26โ”‚ 		return &b
  27โ”‚ 	},
  28โ”‚ }
  29โ”‚ 
  30โ”‚ // ReadRequest reads and parses a Gemini request from buffer. It returns an
  31โ”‚ // error if the request could not be read or was  malformed.
  32โ”‚ func ReadRequest(b *bufio.Reader) (*Request, error) {
  33โ”‚ 	u, ok := requestPool.Get().(*[]byte)
  34โ”‚ 	if !ok {
  35โ”‚ 		return nil, fmt.Errorf("%w: %s", ErrRequestRead, "request pool returned non-byte slice")
  36โ”‚ 	}
  37โ”‚ 
  38โ”‚ 	defer requestPool.Put(u)
  39โ”‚ 
  40โ”‚ 	sh := (*reflect.SliceHeader)(unsafe.Pointer(u)) //nolint:gosec // This is safe.
  41โ”‚ 	sh.Len = 0
  42โ”‚ 
  43โ”‚ 	cr := false
  44โ”‚ 
  45โ”‚ 	for {
  46โ”‚ 		b, err := b.ReadByte()
  47โ”‚ 		if err != nil {
  48โ”‚ 			return nil, fmt.Errorf("%w: %s", ErrRequestRead, err)
  49โ”‚ 		}
  50โ”‚ 
  51โ”‚ 		if b == '\r' {
  52โ”‚ 			cr = true
  53โ”‚ 
  54โ”‚ 			continue
  55โ”‚ 		}
  56โ”‚ 
  57โ”‚ 		if cr && b == '\n' {
  58โ”‚ 			break
  59โ”‚ 		}
  60โ”‚ 
  61โ”‚ 		cr = false
  62โ”‚ 
  63โ”‚ 		*u = append(*u, b)
  64โ”‚ 		if len(*u) > maxRequestLength {
  65โ”‚ 			return nil, ErrMaxRequestLengthExceeded
  66โ”‚ 		}
  67โ”‚ 	}
  68โ”‚ 
  69โ”‚ 	*u = textproto.TrimBytes(*u)
  70โ”‚ 
  71โ”‚ 	uri, err := ParseRequestURI(*(*string)(unsafe.Pointer(u))) //nolint:gosec // This is safe.
  72โ”‚ 	if err != nil {
  73โ”‚ 		return nil, err
  74โ”‚ 	}
  75โ”‚ 
  76โ”‚ 	return &Request{URI: uri}, nil
  77โ”‚ }
โ”€โ”€โ”€โ”€โ•ฏ

ยท ยท ยท

๐Ÿก Home

FAQs

Privacy Policy

Terms & Conditions

Official Gemlog

info@source.community

ยฉ 2024 source.community