-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address comments, add a simple test.
- Loading branch information
1 parent
2096ae2
commit 43b60b2
Showing
12 changed files
with
1,976 additions
and
9 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
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,112 @@ | ||
package wasi_http | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stealthrocket/wasi-go" | ||
"github.com/stealthrocket/wasi-go/imports" | ||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/sys" | ||
) | ||
|
||
type handler struct { | ||
urls []string | ||
} | ||
|
||
func (h *handler) ServeHTTP(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(200) | ||
res.Write([]byte("Response")) | ||
|
||
h.urls = append(h.urls, req.URL.String()) | ||
} | ||
|
||
func TestHttp(t *testing.T) { | ||
filePaths, _ := filepath.Glob("../../testdata/c/http/*.wasm") | ||
for _, file := range filePaths { | ||
fmt.Printf("%v\n", file) | ||
} | ||
if len(filePaths) == 0 { | ||
t.Log("nothing to test") | ||
} | ||
|
||
h := handler{} | ||
s := &http.Server{ | ||
Addr: "127.0.0.1:8080", | ||
Handler: &h, | ||
} | ||
go s.ListenAndServe() | ||
defer s.Shutdown(context.TODO()) | ||
|
||
expectedPaths := [][]string{ | ||
[]string{"/get?some=arg&goes=here"}, | ||
} | ||
|
||
for testIx, test := range filePaths { | ||
name := test | ||
for strings.HasPrefix(name, "../") { | ||
name = name[3:] | ||
} | ||
|
||
t.Run(name, func(t *testing.T) { | ||
bytecode, err := os.ReadFile(test) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
runtime := wazero.NewRuntime(ctx) | ||
defer runtime.Close(ctx) | ||
|
||
builder := imports.NewBuilder(). | ||
WithName("http"). | ||
WithArgs() | ||
var system wasi.System | ||
ctx, system, err = builder.Instantiate(ctx, runtime) | ||
if err != nil { | ||
t.Error("Failed to build WASI module: ", err) | ||
} | ||
defer system.Close(ctx) | ||
|
||
Instantiate(ctx, runtime) | ||
|
||
instance, err := runtime.Instantiate(ctx, bytecode) | ||
if err != nil { | ||
switch e := err.(type) { | ||
case *sys.ExitError: | ||
if exitCode := e.ExitCode(); exitCode != 0 { | ||
t.Error("exit code:", exitCode) | ||
} | ||
default: | ||
t.Error("instantiating wasm module instance:", err) | ||
} | ||
} | ||
if instance != nil { | ||
if err := instance.Close(ctx); err != nil { | ||
t.Error("closing wasm module instance:", err) | ||
} | ||
} | ||
ok := true | ||
if len(h.urls) != len(expectedPaths[testIx]) { | ||
ok = false | ||
} else { | ||
for ix := range h.urls { | ||
if h.urls[ix] != expectedPaths[testIx][ix] { | ||
ok = false | ||
break | ||
} | ||
} | ||
} | ||
if !ok { | ||
t.Errorf("Unexpected paths: %v vs %v", h.urls, expectedPaths[testIx]) | ||
} | ||
h.urls = []string{} | ||
}) | ||
} | ||
} |
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
Binary file not shown.
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,117 @@ | ||
#include "proxy.h" | ||
#include <stdio.h> | ||
|
||
void http_handle(uint32_t arg, uint32_t arg0) { | ||
|
||
} | ||
|
||
int request(uint8_t method_tag, uint8_t scheme_tag, const char * authority_str, const char* path_str, const char* query_str, const char* body) { | ||
types_tuple2_string_string_t content_type[] = {{ | ||
.f0 = { .ptr = "User-agent", .len = 10 }, | ||
.f1 = { .ptr = "WASI-HTTP/0.0.1", .len = 15}, | ||
}, | ||
{ | ||
.f0 = { .ptr = "Content-type", .len = 12 }, | ||
.f1 = { .ptr = "application/json", .len = 16}, | ||
}}; | ||
types_list_tuple2_string_string_t headers_list = { | ||
.ptr = &content_type[0], | ||
.len = 2, | ||
}; | ||
types_fields_t headers = types_new_fields(&headers_list); | ||
types_method_t method = { .tag = method_tag }; | ||
types_scheme_t scheme = { .tag = scheme_tag }; | ||
proxy_string_t path, authority, query; | ||
proxy_string_set(&path, path_str); | ||
proxy_string_set(&authority, authority_str); | ||
proxy_string_set(&query, query_str); | ||
|
||
default_outgoing_http_outgoing_request_t req = types_new_outgoing_request(&method, &path, &query, &scheme, &authority, headers); | ||
default_outgoing_http_future_incoming_response_t res; | ||
|
||
if (req == 0) { | ||
printf("Error creating request\n"); | ||
return 4; | ||
} | ||
if (body != NULL) { | ||
types_outgoing_stream_t ret; | ||
if (!types_outgoing_request_write(req, &ret)) { | ||
printf("Error getting output stream\n"); | ||
return 7; | ||
} | ||
streams_list_u8_t buf = { | ||
.ptr = (uint8_t *) body, | ||
.len = strlen(body), | ||
}; | ||
uint64_t ret_val; | ||
streams_write(ret, &buf, &ret_val, NULL); | ||
} | ||
|
||
res = default_outgoing_http_handle(req, NULL); | ||
if (res == 0) { | ||
printf("Error sending request\n"); | ||
return 5; | ||
} | ||
|
||
types_result_incoming_response_error_t result; | ||
if (!types_future_incoming_response_get(res, &result)) { | ||
printf("failed to get value for incoming request\n"); | ||
return 1; | ||
} | ||
|
||
if (result.is_err) { | ||
printf("response is error!\n"); | ||
return 2; | ||
} | ||
// poll_drop_pollable(res); | ||
|
||
types_status_code_t code = types_incoming_response_status(result.val.ok); | ||
printf("STATUS: %d\n", code); | ||
|
||
types_headers_t header_handle = types_incoming_response_headers(result.val.ok); | ||
types_list_tuple2_string_string_t header_list; | ||
types_fields_entries(header_handle, &header_list); | ||
|
||
for (int i = 0; i < header_list.len; i++) { | ||
char name[128]; | ||
char value[128]; | ||
strncpy(name, header_list.ptr[i].f0.ptr, header_list.ptr[i].f0.len); | ||
name[header_list.ptr[i].f0.len] = 0; | ||
strncpy(value, header_list.ptr[i].f1.ptr, header_list.ptr[i].f1.len); | ||
value[header_list.ptr[i].f1.len] = 0; | ||
printf("%s: %s\n", name, value); | ||
} | ||
|
||
|
||
types_incoming_stream_t stream; | ||
if (!types_incoming_response_consume(result.val.ok, &stream)) { | ||
printf("stream is error!\n"); | ||
return 3; | ||
} | ||
|
||
printf("Stream is %d\n", stream); | ||
|
||
int32_t len = 64 * 1024; | ||
streams_tuple2_list_u8_bool_t body_res; | ||
streams_stream_error_t err; | ||
if (!streams_read(stream, len, &body_res, &err)) { | ||
printf("BODY read is error!\n"); | ||
return 6; | ||
} | ||
printf("data from read: %s\n", body_res.f0.ptr); | ||
streams_tuple2_list_u8_bool_free(&body_res); | ||
|
||
|
||
types_drop_outgoing_request(req); | ||
streams_drop_input_stream(stream); | ||
types_drop_incoming_response(result.val.ok); | ||
|
||
return 0; | ||
} | ||
|
||
int main() { | ||
request(TYPES_METHOD_GET, TYPES_SCHEME_HTTP, "localhost:8080", "/get", "?some=arg&goes=here", NULL); | ||
// request(TYPES_METHOD_POST, TYPES_SCHEME_HTTPS, "postman-echo.com", "/post", "", "{\"foo\": \"bar\"}"); | ||
// request(TYPES_METHOD_PUT, TYPES_SCHEME_HTTP, "postman-echo.com", "/put", "", NULL); | ||
return 0; | ||
} |
Binary file not shown.
Oops, something went wrong.