http.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package engine
  2. import (
  3. "net/http"
  4. "path"
  5. )
  6. // ServeHTTP executes a job as specified by the http request `r`, and sends the
  7. // result as an http response.
  8. // This method allows an Engine instance to be passed as a standard http.Handler interface.
  9. //
  10. // Note that the protocol used in this methid is a convenience wrapper and is not the canonical
  11. // implementation of remote job execution. This is because HTTP/1 does not handle stream multiplexing,
  12. // and so cannot differentiate stdout from stderr. Additionally, headers cannot be added to a response
  13. // once data has been written to the body, which makes it inconvenient to return metadata such
  14. // as the exit status.
  15. //
  16. func (eng *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  17. jobName := path.Base(r.URL.Path)
  18. jobArgs, exists := r.URL.Query()["a"]
  19. if !exists {
  20. jobArgs = []string{}
  21. }
  22. w.Header().Set("Job-Name", jobName)
  23. for _, arg := range jobArgs {
  24. w.Header().Add("Job-Args", arg)
  25. }
  26. job := eng.Job(jobName, jobArgs...)
  27. job.Stdout.Add(w)
  28. job.Stderr.Add(w)
  29. // FIXME: distinguish job status from engine error in Run()
  30. // The former should be passed as a special header, the former
  31. // should cause a 500 status
  32. w.WriteHeader(http.StatusOK)
  33. // The exit status cannot be sent reliably with HTTP1, because headers
  34. // can only be sent before the body.
  35. // (we could possibly use http footers via chunked encoding, but I couldn't find
  36. // how to use them in net/http)
  37. job.Run()
  38. }