-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TinyGo support for {DI,TO0,TO1}Server
Signed-off-by: Ben Krieger <ben.krieger@intel.com>
- Loading branch information
1 parent
2a8cf36
commit 580d17d
Showing
14 changed files
with
434 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache 2.0 | ||
|
||
//go:build !tinygo | ||
|
||
package cbor | ||
|
||
import "reflect" | ||
|
||
func mapClear(rv reflect.Value) { rv.Clear() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache 2.0 | ||
|
||
//go:build tinygo | ||
|
||
package cbor | ||
|
||
import "reflect" | ||
|
||
func mapClear(rv reflect.Value) { | ||
iter := rv.MapRange() | ||
for iter.Next() { | ||
rv.SetMapIndex(iter.Key(), reflect.Value{}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache 2.0 | ||
|
||
// Package main implements a WASM AIO server example (that would panic with | ||
// real use). | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/syumai/workers" | ||
|
||
"github.com/fido-device-onboard/go-fdo" | ||
"github.com/fido-device-onboard/go-fdo/custom" | ||
fdo_http "github.com/fido-device-onboard/go-fdo/http" | ||
"github.com/fido-device-onboard/go-fdo/sqlite" | ||
) | ||
|
||
// Build with tinygo build -target wasm -no-debug -o rv.wasm ./main.go | ||
|
||
func main() { | ||
db, err := sqlite.Init(nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
workers.Serve(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
(&fdo_http.Handler{ | ||
DIResponder: &fdo.DIServer[custom.DeviceMfgInfo]{Session: db}, | ||
TO0Responder: &fdo.TO0Server{Session: db}, | ||
TO1Responder: &fdo.TO1Server{Session: db}, | ||
// TO2Server uses goroutines and cannot be compiled with TinyGo | ||
}).ServeHTTP(w, r) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache 2.0 | ||
|
||
//go:build !tinygo | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"strconv" | ||
) | ||
|
||
func msgTypeFromPath(w http.ResponseWriter, r *http.Request) (uint8, bool) { | ||
typ, err := strconv.ParseUint(r.PathValue("msg"), 10, 8) | ||
if err != nil { | ||
writeErr(w, 0, fmt.Errorf("invalid message type")) | ||
return 0, false | ||
} | ||
return uint8(typ), true | ||
} | ||
|
||
func debugRequest(w http.ResponseWriter, r *http.Request, handler http.HandlerFunc) { | ||
// Dump request | ||
debugReq, _ := httputil.DumpRequest(r, false) | ||
var saveBody bytes.Buffer | ||
if _, err := saveBody.ReadFrom(r.Body); err == nil { | ||
r.Body = io.NopCloser(&saveBody) | ||
} | ||
slog.Debug("request", "dump", string(bytes.TrimSpace(debugReq)), | ||
"body", tryDebugNotation(saveBody.Bytes())) | ||
|
||
// Dump response | ||
rr := httptest.NewRecorder() | ||
handler(rr, r) | ||
debugResp, _ := httputil.DumpResponse(rr.Result(), false) | ||
slog.Debug("response", "dump", string(bytes.TrimSpace(debugResp)), | ||
"body", tryDebugNotation(rr.Body.Bytes())) | ||
|
||
// Copy recorded response into response writer | ||
for key, values := range rr.Header() { | ||
for _, value := range values { | ||
w.Header().Add(key, value) | ||
} | ||
} | ||
w.WriteHeader(rr.Code) | ||
_, _ = w.Write(rr.Body.Bytes()) | ||
} | ||
|
||
func debugRequestOut(req *http.Request, body *bytes.Buffer) { | ||
if !debugEnabled() { | ||
return | ||
} | ||
debugReq, _ := httputil.DumpRequestOut(req, false) | ||
slog.Debug("request", "dump", string(bytes.TrimSpace(debugReq)), | ||
"body", tryDebugNotation(body.Bytes())) | ||
} | ||
|
||
func debugResponse(resp *http.Response) { | ||
if !debugEnabled() { | ||
return | ||
} | ||
debugResp, _ := httputil.DumpResponse(resp, false) | ||
var saveBody bytes.Buffer | ||
if _, err := saveBody.ReadFrom(resp.Body); err == nil { | ||
resp.Body = io.NopCloser(&saveBody) | ||
} | ||
slog.Debug("response", "dump", string(bytes.TrimSpace(debugResp)), | ||
"body", tryDebugNotation(saveBody.Bytes())) | ||
} |
Oops, something went wrong.