-
Notifications
You must be signed in to change notification settings - Fork 1
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
bf56568
commit 12846f8
Showing
14 changed files
with
256 additions
and
81 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,33 @@ | ||
// 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" | ||
) | ||
|
||
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,78 @@ | ||
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache 2.0 | ||
|
||
//go:build !tinygo | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"strconv" | ||
|
||
"github.com/fido-device-onboard/go-fdo/protocol" | ||
) | ||
|
||
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 (h Handler) debugRequest(ctx context.Context, w http.ResponseWriter, r *http.Request, msgType uint8, resp protocol.Responder) { | ||
// 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() | ||
h.handleRequest(ctx, rr, r, msgType, resp) | ||
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 debugRequest(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())) | ||
} |
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,47 @@ | ||
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache 2.0 | ||
|
||
//go:build tinygo | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/fido-device-onboard/go-fdo/protocol" | ||
) | ||
|
||
func msgTypeFromPath(w http.ResponseWriter, r *http.Request) (uint8, bool) { | ||
if r.Method != http.MethodPost { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return 0, false | ||
} | ||
path := strings.TrimPrefix(r.URL.Path, "/fdo/101/msg/") | ||
if strings.Contains(path, "/") { | ||
w.WriteHeader(http.StatusNotFound) | ||
return 0, false | ||
} | ||
typ, err := strconv.ParseUint(path, 10, 8) | ||
if err != nil { | ||
writeErr(w, 0, fmt.Errorf("invalid message type")) | ||
return 0, false | ||
} | ||
return uint8(typ), true | ||
} | ||
|
||
func (h Handler) debugRequest(ctx context.Context, w http.ResponseWriter, r *http.Request, msgType uint8, resp protocol.Responder) { | ||
// TODO: Implement | ||
} | ||
|
||
func debugRequest(req *http.Request, body *bytes.Buffer) { | ||
// TODO: Implement | ||
} | ||
|
||
func debugResponse(resp *http.Response) { | ||
// TODO: Implement | ||
} |
Oops, something went wrong.