Files
ippserver/main.go

78 lines
2.4 KiB
Go

package main
import (
"context"
"fmt"
"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
log.SetLevel(log.DebugLevel)
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(r.Body)
fmt.Printf("Request: \n%v\n", 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", int32(ipp.Idle))
response.AddOperatonAttribute(a)
a = ipp.NewKeyWord("ipp-versions-supported", "1.0", "1.1", "2.0")
response.AddOperatonAttribute(a)
response.AddOperatonAttribute(ipp.NewKeyWord("ipp-features-supported", "wfds-print-1.0"))
response.AddOperatonAttribute(ipp.NewMimeMediaType("document-format-supported", "image/pwg-raster"))
response.AddOperatonAttribute(ipp.NewKeyWord("media-supported", "iso_a4_210x297mm"))
response.AddOperatonAttribute(ipp.NewKeyWord("sides-supported", "one-sided", "two-sided-long-edge", "two-sided-short-edge"))
response.AddOperatonAttribute(ipp.NewKeyWord("print-color-mode-supported", "auto", "color", "monochrome"))
fmt.Printf("Response:\n%v\n", response)
data := response.Marshal()
log.Debugf("% x", data)
w.Write(data)
}