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:
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