Chromium Code Reviews| Index: experimental/webtry/webtry.go |
| diff --git a/experimental/webtry/webtry.go b/experimental/webtry/webtry.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..70ee677087624bf4bb8f7beaf41fd5bb21a36317 |
| --- /dev/null |
| +++ b/experimental/webtry/webtry.go |
| @@ -0,0 +1,226 @@ |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "crypto/md5" |
| + "encoding/base64" |
| + "encoding/json" |
| + "flag" |
| + "fmt" |
| + "io/ioutil" |
| + "log" |
| + "net/http" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strings" |
| + "text/template" |
| +) |
| + |
| +const ( |
| + RESULT_COMPILE = `c++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -DSK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_CAN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I../../tools/flags -I../../include/config -I../../include/core -I../../include/pathops -I../../include/pipe -I../../include/effects -I../../include/ports -I../../src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno-exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unused-parameter -Wno-c++11-extensions -Werror -m64 -fno-rtti -Wnon-virtual-dtor -c ../../../cache/%s.cpp -o ../../../cache/%s.o` |
| + LINK = `c++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start-group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/experimental/webtry/webtry.syscall_reporter.o obj/gyp/libflags.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc.a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a libskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontconfig -ldl -lfreetype` |
| +) |
| + |
| +var ( |
| + // codeTemplate is the cpp code template the user's code is copied into. |
| + codeTemplate *template.Template = nil |
| + |
| + // index is the main index.html page we serve. |
| + index []byte |
| +) |
| + |
| +// flags |
| +var ( |
| + useChroot = flag.Bool("use_chroot", false, "Run the compiled code in the schroot jail.") |
|
mtklein
2014/04/08 19:00:23
My paranoia wants this to default to true now that
jcgregorio
2014/04/09 14:10:31
Not just local testing, but anyone can run this lo
|
| +) |
| + |
| +// lineNumbers adds #line numbering to the user's code. |
| +func LineNumbers(c string) string { |
| + lines := strings.Split(c, "\n") |
| + ret := []string{} |
| + for i, line := range lines { |
| + ret = append(ret, fmt.Sprintf("#line %d", i+1)) |
| + ret = append(ret, line) |
| + } |
| + return strings.Join(ret, "\n") |
| +} |
| + |
| +func init() { |
| + // Change the current working directory to be where the directory of the executable. |
|
mtklein
2014/04/08 19:00:23
Funky wording?
jcgregorio
2014/04/09 14:10:31
Done.
|
| + var err error |
| + cwd, err := filepath.Abs(filepath.Dir(os.Args[0])) |
| + if err != nil { |
| + log.Fatal(err) |
| + } |
| + os.Chdir(cwd) |
| + |
| + codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/template.cpp")) |
| + if err != nil { |
| + panic(err) |
| + } |
| + index, err = ioutil.ReadFile(filepath.Join(cwd, "templates/index.html")) |
| + if err != nil { |
| + panic(err) |
| + } |
| +} |
| + |
| +// userCode is used in template expansion. |
| +type userCode struct { |
|
mtklein
2014/04/08 19:00:23
I've got no problem with the struct if you like it
jcgregorio
2014/04/09 14:10:31
I think that would make the template for expansion
mtklein
2014/04/09 15:03:00
Yeah, it would and it does work. I think it's eve
|
| + UserCode string |
| +} |
| + |
| +// expandToFile expands the template and writes the result to the file. |
| +func expandToFile(filename string, c userCode, t *template.Template) error { |
| + f, err := os.Create(filename) |
| + if err != nil { |
| + return err |
| + } |
| + defer f.Close() |
| + |
| + return t.Execute(f, c) |
| +} |
| + |
| +// ExpandCode expands the template into a file and calculate the MD5 hash. |
| +func ExpandCode(code string) (string, error) { |
|
mtklein
2014/04/08 19:00:23
expandCode?
jcgregorio
2014/04/09 14:10:31
Done.
|
| + h := md5.New() |
| + h.Write([]byte(code)) |
| + hash := fmt.Sprintf("%x", h.Sum(nil)) |
| + content := userCode{ |
| + UserCode: code, |
| + } |
| + err := expandToFile(fmt.Sprintf("../../../cache/%s.cpp", hash), content, codeTemplate) |
|
mtklein
2014/04/08 19:00:23
Could be helpful to remind the reader where we are
jcgregorio
2014/04/09 14:10:31
Done. Also added a TODO to make into flags.
|
| + return hash, err |
| +} |
| + |
| +// Response is serialed to JSON as a response to POSTs. |
| +type Response struct { |
|
mtklein
2014/04/08 19:00:23
response?
jcgregorio
2014/04/09 14:10:31
Done.
|
| + Message string `json:"message"` |
| + Img string `json:"img"` |
| +} |
| + |
| +// doCmd executes the given command line string in either the out/Debug directory or the inout directory. |
| +func doCmd(commandLine string, moveToDebug bool) (string, error) { |
|
mtklein
2014/04/08 19:00:23
Not clear here what it returns? can this be (stdo
jcgregorio
2014/04/09 14:10:31
Done.
|
| + log.Printf("Command: %q\n", commandLine) |
| + programAndArgs := strings.SplitN(commandLine, " ", 2) |
| + program := programAndArgs[0] |
| + args := []string{} |
| + if len(programAndArgs) > 1 { |
| + args = strings.Split(programAndArgs[1], " ") |
| + } |
| + cmd := exec.Command(program, args...) |
| + abs, err := filepath.Abs("../../out/Debug") |
| + if err != nil { |
| + return "", fmt.Errorf("Failed to find absolute path to Debug directory.") |
| + } |
| + if moveToDebug { |
| + cmd.Dir = abs |
| + } else if *useChroot { |
|
mtklein
2014/04/08 19:00:23
?
if moveToDebug {
} else if !*useChroot {
}
?
jcgregorio
2014/04/09 14:10:31
Done.
|
| + } else { |
| + abs, err := filepath.Abs("../../../inout") |
|
mtklein
2014/04/08 19:00:23
Might help readability to make some of these liter
jcgregorio
2014/04/09 14:10:31
Added as TODO above.
|
| + if err != nil { |
| + return "", fmt.Errorf("Failed to find absolute path to inout directory.") |
| + } |
| + cmd.Dir = abs |
| + } |
| + log.Printf("Run in directory: %q\n", cmd.Dir) |
| + var stdOut bytes.Buffer |
| + cmd.Stdout = &stdOut |
| + var stdErr bytes.Buffer |
| + cmd.Stderr = &stdErr |
| + cmd.Start() |
| + err = cmd.Wait() |
| + message := stdOut.String() |
| + log.Printf("StdOut: %s\n", message) |
| + if err != nil { |
| + log.Printf("Exit status: %s\n", err.Error()) |
| + log.Printf("StdErr: %s\n", stdErr.String()) |
| + message += stdErr.String() |
| + return message, fmt.Errorf("Failed to run command.") |
| + } |
| + return message, nil |
| +} |
| + |
| +// reportError formats an HTTP error response and also logs the detailed error message. |
| +func reportError(w http.ResponseWriter, r *http.Request, err error, message string) { |
| + m := Response{ |
| + Message: message, |
| + } |
| + log.Printf("Error: %s\n%s", message, err.Error()) |
| + resp, err := json.Marshal(m) |
| + if err != nil { |
| + http.Error(w, "Failed to serialize a response", 500) |
| + return |
| + } |
| + w.Write(resp) |
| +} |
| + |
| +// mainHandler handles the GET and POST of the main page. |
| +func mainHandler(w http.ResponseWriter, r *http.Request) { |
| + if r.Method == "GET" { |
| + w.Write(index) |
| + } else if r.Method == "POST" { |
| + w.Header().Set("Content-Type", "application/json") |
| + b, err := ioutil.ReadAll(r.Body) |
| + if err != nil { |
| + reportError(w, r, err, "Failed to read a request body.") |
| + return |
| + } |
| + hash, err := ExpandCode(LineNumbers(string(b))) |
| + if err != nil { |
| + reportError(w, r, err, "Failed to write the code to compile.") |
| + return |
| + } |
| + message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), true) |
| + if err != nil { |
| + reportError(w, r, err, "Failed to compile the code.") |
| + return |
| + } |
| + linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true) |
| + if err != nil { |
| + reportError(w, r, err, "Failed to link the code.") |
| + return |
| + } |
| + message += linkMessage |
| + cmd := hash + " --out " + hash + ".png" |
| + if *useChroot { |
| + cmd = "schroot -c webtry --directory=/inout -- /inout/" + cmd |
| + } else { |
| + abs, err := filepath.Abs("../../../inout") |
| + if err != nil { |
| + reportError(w, r, err, "Failed to find executable directory.") |
| + return |
| + } |
| + cmd = abs + "/" + cmd |
| + } |
| + |
| + execMessage, err := doCmd(cmd, false) |
| + if err != nil { |
| + reportError(w, r, err, "Failed to run the code: "+execMessage) |
| + return |
| + } |
| + png, err := ioutil.ReadFile("../../../inout/" + hash + ".png") |
| + if err != nil { |
| + reportError(w, r, err, "Failed to open the generated PNG.") |
| + return |
| + } |
| + |
| + m := Response{ |
| + Message: message, |
| + Img: base64.StdEncoding.EncodeToString([]byte(png)), |
| + } |
| + resp, err := json.Marshal(m) |
| + if err != nil { |
| + reportError(w, r, err, "Failed to serialize a response.") |
| + return |
| + } |
| + w.Write(resp) |
| + } |
| +} |
| + |
| +func main() { |
| + flag.Parse() |
| + |
| + http.HandleFunc("/", mainHandler) |
| + log.Fatal(http.ListenAndServe(":8000", nil)) |
| +} |