hnakra/service/http.go

89 lines
2.2 KiB
Go
Raw Normal View History

2023-05-25 16:08:56 -06:00
package service
import "log"
import "errors"
import "net/http"
import "hnakra/protocol"
// HTTP is an https:// mount.
type HTTP struct {
2023-05-29 15:03:27 -06:00
MountInfo
2023-05-25 16:08:56 -06:00
// AllowInsecure allows this mount to respond to plain-text HTTP
// requests. You can get a TLS cert for free nowadays so there are very
// few cases where you'd want this on.
AllowInsecure bool
// Handler specifies the handler to invoke. If it is nil,
// http.DefaultServeMux is used.
Handler http.Handler
2023-05-29 15:03:27 -06:00
conn *Conn
2023-05-25 16:08:56 -06:00
requests requestManager
}
// Close closes the mount abruptly, interrupting any active connections.
2023-05-29 15:03:27 -06:00
func (htmount *HTTP) Close () error {
return htmount.conn.Close()
2023-05-25 16:08:56 -06:00
}
// Shutdown gracefully shuts down the service without interrupting any active
// connections.
2023-05-29 15:03:27 -06:00
func (htmount *HTTP) Shutdown () error {
2023-05-25 16:08:56 -06:00
// TODO
2023-05-29 15:03:27 -06:00
return htmount.Close()
2023-05-25 16:08:56 -06:00
}
// Run connects to the router, and blocks while fulfilling requests. This method
// will only return when the connection to the router has been closed.
2023-05-29 15:03:27 -06:00
func (htmount *HTTP) Run (service ServiceInfo) (err error) {
if htmount.AllowInsecure {
htmount.MountInfo.Scheme = "http"
2023-05-25 16:08:56 -06:00
} else {
2023-05-29 15:03:27 -06:00
htmount.MountInfo.Scheme = "https"
2023-05-25 16:08:56 -06:00
}
2023-05-29 15:03:27 -06:00
htmount.conn, err = Dial(htmount.MountInfo, service)
2023-05-25 16:08:56 -06:00
if err != nil { return }
2023-05-29 15:03:27 -06:00
htmount.requests.init()
2023-05-25 16:08:56 -06:00
for {
2023-05-29 15:03:27 -06:00
message, err := htmount.conn.Receive()
2023-05-25 16:08:56 -06:00
if err != nil { return err }
switch message.(type) {
case protocol.MessageHTTPRequest:
request := message.(protocol.MessageHTTPRequest)
2023-05-29 15:03:27 -06:00
htmount.requests.add(request.ID)
go htmount.handle(request)
2023-05-25 16:08:56 -06:00
case protocol.MessageHTTPBodySegment:
segment := message.(protocol.MessageHTTPBodySegment)
2023-05-29 15:03:27 -06:00
htmount.requests.feed(segment.ID, segment.Data)
2023-05-25 16:08:56 -06:00
case protocol.MessageHTTPBodyEnd:
end := message.(protocol.MessageHTTPBodyEnd)
2023-05-29 15:03:27 -06:00
htmount.requests.end(end.ID)
2023-05-25 16:08:56 -06:00
case protocol.MessageStatus:
status := message.(protocol.MessageStatus)
log.Println("router says:", status.Status)
default:
2023-05-29 15:03:27 -06:00
htmount.Close()
2023-05-25 16:08:56 -06:00
return errors.New("router sent unknown type code")
}
}
}
2023-05-29 15:03:27 -06:00
// NewHTTP creates a new HTTPS mount that uses the specified handler.
func NewHTTP (host, path string, handler http.Handler) *HTTP {
return &HTTP {
MountInfo: MountInfo {
Host: host,
Path: path,
},
Handler: handler,
}
2023-05-25 16:08:56 -06:00
}