Files
ippserver/main.go
2020-12-23 23:21:47 +01:00

69 lines
1.8 KiB
Go

package main
import (
"bytes"
"context"
"fmt"
"io"
"ippserver/packages/ipp"
"ippserver/packages/mdnsserver"
"net/http"
log "github.com/sirupsen/logrus"
)
func main() {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go mdnsserver.Run(ctx)
http.HandleFunc("/ipp/print", handle)
log.Info("http server started on :1234")
err := http.ListenAndServe(":1234", nil)
if err != nil {
log.Fatal("ListenAndServe: " + err.Error())
}
}
func handle(w http.ResponseWriter, r *http.Request) {
log.Infoln("handle")
if r.Method != http.MethodPost {
http.Error(w, "Unsupported method", http.StatusMethodNotAllowed)
}
// body := make([]byte, r.ContentLength)
// io.ReadFull(r.Body, body)
// log.Infof("Body %x", body)
request := ipp.NewRequest()
rdata := make([]byte, r.ContentLength)
io.ReadFull(r.Body, rdata)
//log.Printf("Data %x", rdata)
fmt.Printf("data % #0x", rdata)
buf := bytes.NewBuffer(rdata)
request.UnMarshal(buf)
fmt.Printf("%v", request)
response := ipp.NewResponse(ipp.SuccessfulOk, request.RequestId())
var a ipp.Attribute
a = ipp.NewCharSetValue("attributes-charset", "utf-8")
response.AddOperatonAttribute(a)
a = ipp.NewNaturalLanguage("attributes-natural-language", "en")
response.AddOperatonAttribute(a)
a = ipp.NewUriValue("printer-uri", "ipp://drpork:1234/ipp/print")
response.AddOperatonAttribute(a)
a = ipp.NewtextWithoutLanguage("printer-make-and-model", "ChroBro 001")
response.AddOperatonAttribute(a)
a = ipp.NewEnum("printer-state", uint16(3))
response.AddOperatonAttribute(a)
data := response.Marshal()
log.Infof("% x", data)
w.Write(data)
}