Files
ippserver/main.go

100 lines
3.0 KiB
Go

package main
import (
"context"
"encoding/binary"
"ippserver/packages/mdnsserver"
"net/http"
log "github.com/sirupsen/logrus"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel when we are finished consuming integers
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)
}
var request ippRequest
request.operationAttributes = make(map[string]string)
request.requestedAttributes = make([]string, 0)
log.Info(r.Header)
log.Info(r.ContentLength)
//body := make([]byte, r.ContentLength)
//r.Body.Read(body)
binary.Read(r.Body, binary.BigEndian, &request.header.versionNumber)
binary.Read(r.Body, binary.BigEndian, &request.header.operationId)
binary.Read(r.Body, binary.BigEndian, &request.header.requestId)
var tag uint8
binary.Read(r.Body, binary.BigEndian, &tag)
if tag == operationAttributesTag {
var length uint16
binary.Read(r.Body, binary.BigEndian, &tag)
for tag != endOfAttributesTag {
log.Infof("Value tag %x", tag)
switch tag {
case charsetValueTag, uriValueTag, naturalLanguagageValueTag:
binary.Read(r.Body, binary.BigEndian, &length)
attributeName := make([]byte, length)
binary.Read(r.Body, binary.BigEndian, attributeName)
binary.Read(r.Body, binary.BigEndian, &length)
attributeValue := make([]byte, length)
binary.Read(r.Body, binary.BigEndian, attributeValue)
request.operationAttributes[string(attributeName)] = string(attributeValue)
log.Infof("%v %v", string(attributeName), string(attributeValue))
binary.Read(r.Body, binary.BigEndian, &tag)
case keywordValueTag:
binary.Read(r.Body, binary.BigEndian, &length)
attributeName := make([]byte, length)
set := make([]string, 0)
binary.Read(r.Body, binary.BigEndian, &attributeName)
binary.Read(r.Body, binary.BigEndian, &length)
value := make([]byte, length)
binary.Read(r.Body, binary.BigEndian, &value)
set = append(set, string(value))
binary.Read(r.Body, binary.BigEndian, &tag)
for tag == keywordValueTag {
var additionalValue uint16
binary.Read(r.Body, binary.BigEndian, &additionalValue)
binary.Read(r.Body, binary.BigEndian, &length)
value = make([]byte, length)
binary.Read(r.Body, binary.BigEndian, &value)
set = append(set, string(value))
binary.Read(r.Body, binary.BigEndian, &tag)
}
if string(attributeName) == "requested-attributes" {
request.requestedAttributes = set
}
log.Infof("%v %v", string(attributeName), set)
default:
log.Error("Unsupported tag")
}
}
log.Infof("Value tag %x", tag)
} else {
log.Error("unexpected tag")
// TODO Return something sensible here
}
log.Infof("request %+v", request)
//log.Infof("Body %v", string(body))
}