Simple roundtrip from ipptool works.
This commit is contained in:
85
main.go
85
main.go
@@ -2,7 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"ippserver/packages/ipp"
|
||||
"ippserver/packages/mdnsserver"
|
||||
"net/http"
|
||||
|
||||
@@ -10,9 +11,13 @@ import (
|
||||
)
|
||||
|
||||
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() // cancel when we are finished consuming integers
|
||||
defer cancel()
|
||||
go mdnsserver.Run(ctx)
|
||||
|
||||
http.HandleFunc("/ipp/print", handle)
|
||||
@@ -30,70 +35,18 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||
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")
|
||||
// body := make([]byte, r.ContentLength)
|
||||
// io.ReadFull(r.Body, body)
|
||||
// log.Infof("Body %x", body)
|
||||
|
||||
}
|
||||
}
|
||||
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))
|
||||
request := ipp.NewRequest()
|
||||
request.UnMarshal(r.Body)
|
||||
fmt.Printf("%v", request)
|
||||
response := ipp.NewResponse(ipp.SuccessfulOk, request.RequestId())
|
||||
a := ipp.NewCharSetValue("attributes-charset", "utf-8")
|
||||
response.AddOperatonAttribute(a)
|
||||
data := response.Marshal()
|
||||
log.Infof("% x", data)
|
||||
w.Write(data)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user