Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
proxywasm: big upload/download test
Browse files Browse the repository at this point in the history
  • Loading branch information
bonifaido committed Sep 4, 2023
1 parent dc27eeb commit 1f076e3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ jobs:
sudo dmesg -T
- name: Run proxy-wasm smoke test
working-directory: wasm-kernel-module-cli
working-directory: wasm-kernel-module
timeout-minutes: 1
run: |
sudo ./w3k server &
sleep 1
python3 -m http.server &
sleep 1
curl -v http://localhost:8000
sudo ../wasm-kernel-module-cli/w3k server &
go run test/file-server.go &
sleep 2
# Test downloading a bigger file
curl -v -O /tmp/opa_downloaded.o "http://localhost:8000/opa.o"
# Test uploading this file
curl -v -F "opa_downloaded.o.o=@/tmp/opa_downloaded.o" http://localhost:8000/upload
diff opa.o opa_downloaded.o
sudo dmesg -T | tail -100
sudo kill -9 $(jobs -p)
sudo pkill -9 w3k
Expand Down
2 changes: 1 addition & 1 deletion socket.rego
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ INPUT := 0
OUTPUT := 1

allowed_ports := {8000, 8080}
allowed_commands := {"curl", "python3"}
allowed_commands := {"curl", "python3", "file-server"}

allow = {
"mtls": true,
Expand Down
65 changes: 65 additions & 0 deletions test/file-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"io"
"log"
"mime/multipart"
"net"
"net/http"
"os"
)

func main() {
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
// parse the multipart form in the request with a 1MB max
err := r.ParseMultipartForm(1 << 20)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// write each uploaded file to disk
for _, fheaders := range r.MultipartForm.File {
for _, hdr := range fheaders {
// open uploaded
var infile multipart.File
infile, err = hdr.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// open destination file
var outfile *os.File
outfile, err = os.Create("./" + hdr.Filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// save the data to the file
var written int64
written, err = io.Copy(outfile, infile)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("uploaded file: %s (%d bytes)", hdr.Filename, written)
}
}
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})

http.Handle("/", http.FileServer(http.Dir("./")))

ls, err := net.Listen("tcp4", ":8000")
if err != nil {
log.Fatal(err)
}

log.Println("Listening on 0.0.0.0:8000...")
log.Fatal(http.Serve(ls, nil))
}
46 changes: 46 additions & 0 deletions test/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from http.server import SimpleHTTPRequestHandler, HTTPServer
import logging, multipart

class Handler(SimpleHTTPRequestHandler):

protocol_version = 'HTTP/1.1'

def do_POST(self):
content_length = int(self.headers['Content-Length'])

env = {
"CONTENT_TYPE" : self.headers['Content-Type'],
"REQUEST_METHOD": "POST",
"CONTENT_LENGTH": content_length,
"wsgi.input": self.rfile,
}

_, files = multipart.parse_form_data(env)
for k, v in files.items():
v.save_as(k)

body = "Uploaded {}bytes on path {}\n".format(self.path, content_length).encode('utf-8')
self.send_response(200)
self.send_header('Content-length', len(body))
self.end_headers()
self.wfile.write(body)

def run(server_class=HTTPServer, handler_class=Handler, port=8000):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Serving HTTP on 0.0.0.0 port %d (http://0.0.0.0:%d/) ...', port, port)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Interrupt received, exiting.')

if __name__ == '__main__':
from sys import argv

if len(argv) == 2:
run(port=int(argv[1]))
else:
run()

0 comments on commit 1f076e3

Please sign in to comment.