2-add-keyword-support (#6)
More development. More types. Fixed attribute groups in requests. Started on client. Saving data to file. More types. Printing from chromeos works a little bit. More types. Spelling corrections. WIP: Fix keyword handling Move request to a separate file and add test. Co-authored-by: Henrik Sölver <henrik.solver@gmail.com> Reviewed-on: #6 Co-Authored-By: henrik <henrik.solver@gmail.com> Co-Committed-By: henrik <henrik.solver@gmail.com>
This commit was merged in pull request #6.
This commit is contained in:
8
server/handlegetjobs.go
Normal file
8
server/handlegetjobs.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package main
|
||||
|
||||
import "ippserver/packages/ipp"
|
||||
|
||||
func handleGetJobs(r *ipp.Request) *ipp.Response {
|
||||
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestId())
|
||||
return response
|
||||
}
|
||||
28
server/handlegetprinterattributes.go
Normal file
28
server/handlegetprinterattributes.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import "ippserver/packages/ipp"
|
||||
|
||||
func handleGetPrinterAttributes(r *ipp.Request) *ipp.Response {
|
||||
response := ipp.NewResponse(ipp.SuccessfulOk, r.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"))
|
||||
response.AddOperatonAttribute(ipp.NewResolution("printer-resolution-default", 600, 600))
|
||||
response.AddOperatonAttribute(ipp.NewBoolean("printer-is-accepting-jobs", true))
|
||||
return response
|
||||
}
|
||||
22
server/handleprintjob.go
Normal file
22
server/handleprintjob.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"ippserver/packages/ipp"
|
||||
"os"
|
||||
)
|
||||
|
||||
func handlePrintJob(r *ipp.Request, byteStream io.Reader) *ipp.Response {
|
||||
|
||||
a := r.GetAttribute("job-name")
|
||||
//a.(nameWithoutLanguage).Value
|
||||
f, err := os.Create(a.(*ipp.NameWithoutLanguage).Value())
|
||||
if err != nil {
|
||||
panic("fail")
|
||||
}
|
||||
defer f.Close()
|
||||
io.Copy(f, byteStream)
|
||||
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestId())
|
||||
|
||||
return response
|
||||
}
|
||||
69
server/main.go
Normal file
69
server/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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.InfoLevel)
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
log.Info(r.Header)
|
||||
//body := make([]byte, r.ContentLength)
|
||||
//io.ReadFull(r.Body, body)
|
||||
//log.Infof("Body %x", body)
|
||||
|
||||
request := ipp.NewRequest(0, 0)
|
||||
//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)
|
||||
log.Infof("Request: \n%v\n", request)
|
||||
var response *ipp.Response
|
||||
switch request.Operation() {
|
||||
case ipp.GetPrinterAttributes:
|
||||
response = handleGetPrinterAttributes(request)
|
||||
case ipp.PrintJob:
|
||||
response = handlePrintJob(request, r.Body)
|
||||
case ipp.GetJobs:
|
||||
response = handleGetJobs(request)
|
||||
default:
|
||||
response = ipp.NewResponse(ipp.ClientErrorBadRequest, request.RequestId())
|
||||
}
|
||||
|
||||
log.Infof("Response:\n%v\n", response)
|
||||
data := response.Marshal()
|
||||
|
||||
//log.Debugf("% x", data)
|
||||
w.Write(data)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user